<?php
ob_start();
$Manage = isset($_GET['Manage']) ? $_GET['Manage'] : 'tree';
if ($Manage == "cost_centers") {
    $do = isset($_GET['do']) ? $_GET['do'] : 'costs';
    $pageTitle = 'تقرير مراكز التكلفة';
} else {
    $do = isset($_GET['do']) ? $_GET['do'] : 'Manage';
    $pageTitle = 'إدارة الحسابات';
}

include 'init.php';
use classs\YomyaTransaction;
// use PhpOffice\PhpSpreadsheet\Spreadsheet;
// use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
if (isset($_GET['do']) && $_GET['do'] == "Add" || isset($_GET['do']) && $_GET['do'] == "Insert") {
    $go = 1;
}
    $Manage = isset($_GET['Manage']) ? $_GET['Manage'] : 'tree';
    if ($Manage == "cost_centers") {
        $do = isset($_GET['do']) ? $_GET['do'] : 'costs';
    } else {
        $do = isset($_GET['do']) ? $_GET['do'] : 'Manage';
    }

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (isset($_POST['start']) && !empty($_POST['start']) && isset($_POST['end']) && !empty($_POST['end'])) {

            $start_day = "01";
            $end_day = "31";
            $start_mon = "01";
            $end_mon = "12";

            $start_date = $_POST['start'];
            $date_start_all = explode('-', $start_date);
            $thisyear_start = $date_start_all[0];
            $thismon_start = $date_start_all[1];
            $start_day_start = $date_start_all[2];

            $end_date = $_POST['end'];
            $date_end_all = explode('-', $end_date);
            $thisyear_end = $date_end_all[0];
            $thismon_end = $date_end_all[1];
            $thisday_end = $date_end_all[2];
            $start_date = $start_day_start . "-" . $thismon_start . "-" . $thisyear_start;
            $end_date = $thisday_end . "-" . $thismon_end . "-" . $thisyear_end;
            $start_date_sql = $thisyear_start . "-" . $thismon_start . "-" . $start_day_start;
            $end_date_sql = $thisyear_end . "-" . $thismon_end . "-" . $thisday_end;
        } else {

            $start_day = "01";
            $end_day = "31";
            $start_mon = "01";
            $end_mon = "12";
            $thisyear_start = $thisyear;
            $thismon_start = $thismon;
            $start_day_start = $thisday;
            $thisyear_end = $thisyear;
            $thismon_end = $thismon;
            $thisday_end = $days;
            $start_date = $start_day . "-" . $thismon . "-" . $thisyear;
            $end_date = $days . "-" . $thismon . "-" . $thisyear;
            $start_date_sql = $thisyear . "-" . $thismon . "-" . $start_day;
            $end_date_sql = $thisyear . "-" . $thismon . "-" . $days;
        }
    } else {
        $start_day = "01";
        $end_day = "31";
        $start_mon = "01";
        $end_mon = "12";
        $thisyear_start = $thisyear;
        $thismon_start = $thismon;
        $start_day_start = $thisday;
        $thisyear_end = $thisyear;
        $thismon_end = $thismon;
        $thisday_end = $days;
        $start_date = $start_day . "-" . $thismon . "-" . $thisyear;
        $end_date = $days . "-" . $thismon . "-" . $thisyear;
        $start_date_sql = $thisyear . "-" . $thismon . "-" . $start_day;
        $end_date_sql = $thisyear . "-" . $thismon . "-" . $days;
    }
    if ($Manage == "tree") {
        if (checkadmin($_SESSION['admin'], 12, 2) == 1) {
            ?>
            <a href="?Manage=tree&do=Add&lv=lv1" class="btn btn-primary">
                <i class="fa fa-plus"></i> مستوى 1
            </a>
            <a href="?Manage=tree&do=Add&lv=lv2" class="btn btn-primary">
                <i class="fa fa-plus"></i> مستوى 2
            </a>
            <a href="?Manage=tree&do=Add&lv=lv3" class="btn btn-primary">
                <i class="fa fa-plus"></i> مستوى 3
            </a>
            <a href="?Manage=tree&do=Add&lv=lv4" class="btn btn-primary">
                <i class="fa fa-plus"></i> مستوى 4
            </a>
            <a href="?Manage=tree&do=Add&lv=lv5" class="btn btn-primary">
                <i class="fa fa-plus"></i> مستوى 5
            </a>
            <a href="?Manage=tree&do=yomya" class="btn btn-primary">
                <i class="fa fa-cog"></i> اليومية
            </a>
            <a href="?Manage=tree&do=account_statements" class="btn btn-primary">
                <i class="fa fa-cog"></i> كشف الحساب
            </a>
            <!-- <a href="?Manage=tree&do=ledger" class="btn btn-primary">
                <i class="fa fa-cog"></i> دفتر الأستاذ
            </a> -->
            <a href="?Manage=tree&do=balance" class="btn btn-primary">
                <i class="fa fa-cog"></i> ميزان المراجعة
            </a>
            <a href="?Manage=tree&do=ditels_earn" class="btn btn-primary">
                <i class="fa fa-cog"></i> قائمة الدخل
            </a>
            <a href="?Manage=tree&do=financial_center" class="btn btn-primary">
                <i class="fa fa-cog"></i> المركز المالى
            </a>
            <br />
            <!-- <div class="alert alert-secondary" role="alert">
                *ملحوظة : كل المعاملات تظهر بعملة الجنيه المصري
            </div> -->
            <br />
            <?php
            if ($do == 'Manage') {
                $countlevel = checkItem("lev_acc", "level_acc", 1);
                if (!empty($countlevel) && $countlevel > 0) {
                    echo '<div class="container"><section class="buy"><div class="panel panel-primary text-right table-responsive"><div class="panel-heading">شجرة الحسابات</div>';
                    generateList(1); // استدعاء الدالة لبدء العملية
                    echo '</div></section></div>';
                } else {
                    echo '<div class="alert alert-success">لا يوجد حسابات حتى الآن.</div>';
                }
            } elseif ($do == 'Add') { // Add Page 
                if (!empty($_GET['lv'])) {
                    $level = $_GET['lv'];
                } else {
                    $level = "";
                }
                if (!empty($level) && $level == "lv1") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب مستوى أول</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=tree&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="1" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } elseif (!empty($level) && $level == "lv2") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب مستوى ثاني</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=tree&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="2" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv1" class="input-group-text">إختر المستوى الأول من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv1" name="lv1" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM level_acc WHERE lev_acc = 1 ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر الحساب</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_account']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حسابات حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <!-- <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control" >
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div> -->
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <!-- <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control" >
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div> -->
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } elseif (!empty($level) && $level == "lv3") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب مستوى ثالث</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=tree&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="3" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv1" class="input-group-text">إختر المستوى الأول من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv1" name="lv1" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM level_acc WHERE lev_acc = 1 ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر الحساب</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_account']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حسابات حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv2" class="input-group-text">إختر المستوى الثانى من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv2" name="lv2" class="form-control" required="required">
                                            <option value="">إختر حساب المستوى الأول أولا</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <!-- <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div> -->
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                        <!--  <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div> -->
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } elseif (!empty($level) && $level == "lv4") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب مستوى رابع</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=tree&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="4" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv1" class="input-group-text">إختر المستوى الأول من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv1" name="lv1" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM level_acc WHERE lev_acc = 1 ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر الحساب</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_account']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حسابات حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv2" class="input-group-text">إختر المستوى الثانى من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv2" name="lv2" class="form-control" required="required">
                                            <option value="">إختر حساب المستوى الأول أولا</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <div class="col-sm-10 col-md-6">
                                <div class="input-group-prepend">
                                    <label for="lv3" class="input-group-text">إختر المستوى الثالث من الحساب</label>
                                </div>
                                <div class="input-group mb-3">
                                    <select id="lv3" name="lv3" class="form-control" required="required">
                                        <option value="">إختر حساب المستوى الثانى أولا</option>
                                    </select>
                                </div>
                            </div>
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <!-- <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div> -->
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <!-- <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div> -->
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } elseif (!empty($level) && $level == "lv5") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب تحليلى</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=tree&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="5" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv1" class="input-group-text">إختر المستوى الأول من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv1" name="lv1" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM level_acc WHERE lev_acc = 1 ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر الحساب</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_account']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حسابات حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv2" class="input-group-text">إختر المستوى الثانى من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv2" name="lv2" class="form-control" required="required">
                                            <option value="">إختر حساب المستوى الأول أولا</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <div class="col-sm-10 col-md-6">
                                <div class="input-group-prepend">
                                    <label for="lv3" class="input-group-text">إختر المستوى الثالث من الحساب</label>
                                </div>
                                <div class="input-group mb-3">
                                    <select id="lv3" name="lv3" class="form-control" required="required">
                                        <option value="">إختر حساب المستوى الثانى أولا</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-10 col-md-6">
                                <div class="input-group-prepend">
                                    <label for="lv4" class="input-group-text">إختر المستوى الرابع من الحساب</label>
                                </div>
                                <div class="input-group mb-3">
                                    <select id="lv4" name="lv4" class="form-control" required="required">
                                        <option value="">إختر حساب المستوى الثالث أولا</option>
                                    </select>
                                </div>
                            </div>
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <!-- <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div> -->
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <!-- <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div> -->
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } else {
                    header('Location: index.php');
                    exit();
                }
            } elseif ($do == 'Insert') {
                if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    $lvacc = $_POST['lvacc'];
                    if (isset($_POST['lv1']) && !empty($_POST['lv1'])) {
                        $id_lv1 = $_POST['lv1'];
                    } else {
                        $id_lv1 = "";
                    }
                    if (isset($_POST['lv2']) && !empty($_POST['lv2'])) {
                        $id_lv2 = $_POST['lv2'];
                    } else {
                        $id_lv2 = "";
                    }
                    if (isset($_POST['lv3']) && !empty($_POST['lv3'])) {
                        $id_lv3 = $_POST['lv3'];
                    } else {
                        $id_lv3 = "";
                    }
                    if (isset($_POST['lv4']) && !empty($_POST['lv4'])) {
                        $id_lv4 = $_POST['lv4'];
                    } else {
                        $id_lv4 = "";
                    }
                    $nameacc = isset($_POST['nameacc']) ? $_POST['nameacc'] : '';
                    $nature = isset($_POST['nature']) ? $_POST['nature'] : '';
                    $list_account = isset($_POST['list_account']) ? $_POST['list_account'] : '';
                    // Validate The Form
                    $formErrors = array();
                    if (empty($nameacc)) {
                        $formErrors[] = 'لا يجب أن يكون إسم الحساب  <strong>فارغ</strong>';
                    }
                    if ($lvacc == 1) {
                        if (empty($nature)) {
                            $formErrors[] = 'لا يجب أن يكون طبيعة  <strong>فارغ</strong>';
                        }
                    }
                    if ($lvacc == 1) {
                        if (empty($list_account)) {
                            $formErrors[] = 'لا يجب أن تكون القائمة المالية  <strong>فارغه</strong>';
                        }
                    }
                    if ($lvacc == 2 || $lvacc == 3 || $lvacc == 4 || $lvacc == 5) {
                        if (empty($id_lv1)) {
                            $formErrors[] = 'لا يجب أن يكون حساب المستوى الأول <strong>فارغ</strong>';
                        }
                    }
                    if ($lvacc == 3 || $lvacc == 4 || $lvacc == 5) {
                        if (empty($id_lv2)) {
                            $formErrors[] = 'لا يجب أن يكون حساب المستوى الثاني <strong>فارغ</strong>';
                        }
                    }
                    if ($lvacc == 4 || $lvacc == 5) {
                        if (empty($id_lv3)) {
                            $formErrors[] = 'لا يجب أن يكون حساب المستوى الثالث <strong>فارغ</strong>';
                        }
                    }
                    if ($lvacc == 5) {
                        if (empty($id_lv4)) {
                            $formErrors[] = 'لا يجب أن يكون حساب المستوى الثالث <strong>فارغ</strong>';
                        }
                    }
                    // Loop Into Errors Array And Echo It
                    foreach ($formErrors as $error) {
                        echo '<div class="alert alert-danger">' . $error . '</div>';
                    }
                    // Check If There's No Error Proceed The Update Operation
                    if (empty($formErrors)) {
                        $lv1 = "";
                        $lv2 = "";
                        $lv3 = "";
                        $lv4 = "";
                        $lv5 = "";
                        if ($lvacc == 1) {
                            $acc_up = "";
                            $account = $con->prepare("SELECT MAX(lv1) as last_code_account FROM level_acc where lev_acc = 1 ");
                            $account->execute();
                            $account1s = $account->fetchAll();
                            foreach ($account1s as $key => $account1) {
                                $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv1 = $last_code_account + 1;
                                } else {
                                    $lv1 = 1;
                                }
                            }
                            $lv = $lv1;
                        } elseif ($lvacc == 2) {
                            $lv1 = getName("level_acc", "ID", $id_lv1, "lv1");
                            $acc_up = $id_lv1;
                            $account = $con->prepare("SELECT MAX(CAST(SUBSTRING_INDEX(lv2, '.', -1) AS UNSIGNED)) AS last_code_account FROM level_acc where lev_acc = 2 AND acc_up = $id_lv1");
                            // $account = $con->prepare("SELECT MAX(lv2) as last_code_account FROM level_acc where lev_acc = 2 AND acc_up = $id_lv1 ");
                            $account->execute();
                            // احصل على البيانات
                            $result = $account->fetch(PDO::FETCH_ASSOC);
                            // احصل على أكبر قيمة
                            $last_code_account = $result['last_code_account'];
                            // $account1s = $account->fetchAll();
                            // foreach ($account1s as $key => $account1) {
                                // $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv2 = $last_code_account + 1;
                                    $lv2 = $lv1.".".$lv2;
                                } else {
                                    $lv2 = $lv1 . ".1";
                                }
                            // }
                            $lv = $lv2;
                        } elseif ($lvacc == 3) {
                            $lv1 = getName("level_acc", "ID", $id_lv1, "lv1");
                            $lv2 = getName("level_acc", "ID", $id_lv2, "lv2");
                            $acc_up = $id_lv2;
                            $account = $con->prepare("SELECT MAX(CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(lv3, '.', -1), '.', -1) AS UNSIGNED)) AS last_code_account FROM level_acc where lev_acc = 3 AND acc_up = $id_lv2 ");
                            // $account = $con->prepare("SELECT MAX(lv3) as last_code_account FROM level_acc where lev_acc = 3 AND acc_up = $id_lv2 ");
                            $account->execute();

                            // احصل على البيانات
                            $result = $account->fetch(PDO::FETCH_ASSOC);

                            // احصل على أكبر قيمة
                            $last_code_account = $result['last_code_account'];

                            // $account1s = $account->fetchAll();
                            // foreach ($account1s as $key => $account1) {
                                // $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv3 = $last_code_account + 1;
                                    $lv3 = $lv2.".".$lv3;
                                } else {
                                    $lv3 = $lv2 . ".1";
                                }
                            // }
                            $lv = $lv3;
                        } elseif ($lvacc == 4) {
                            $lv1 = getName("level_acc", "ID", $id_lv1, "lv1");
                            $lv2 = getName("level_acc", "ID", $id_lv2, "lv2");
                            $lv3 = getName("level_acc", "ID", $id_lv3, "lv3");
                            $acc_up = $id_lv3;
                            $account = $con->prepare("SELECT MAX(CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(lv4, '.', -1), '.', -1), '.', -1) AS UNSIGNED)) AS last_code_account FROM level_acc where lev_acc = 4 AND acc_up = $id_lv3 ");
                            // $account = $con->prepare("SELECT MAX(lv4) as last_code_account FROM level_acc where lev_acc = 4 AND acc_up = $id_lv3 ");
                            $account->execute();
                            // احصل على البيانات
                            $result = $account->fetch(PDO::FETCH_ASSOC);
                            // احصل على أكبر قيمة
                            $last_code_account = $result['last_code_account'];

                            // $account1s = $account->fetchAll();
                            // foreach ($account1s as $key => $account1) {
                            //     $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv4 = $last_code_account + 1;
                                    $lv4 = $lv3.".".$lv4;
                                } else {
                                    $lv4 = $lv3 . ".1";
                                }
                            // }
                            $lv = $lv4;
                        } elseif ($lvacc == 5) {
                            $lv1 = getName("level_acc", "ID", $id_lv1, "lv1");
                            $lv2 = getName("level_acc", "ID", $id_lv2, "lv2");
                            $lv3 = getName("level_acc", "ID", $id_lv3, "lv3");
                            $lv4 = getName("level_acc", "ID", $id_lv4, "lv4");
                            $acc_up = $id_lv4;
                            $account = $con->prepare("SELECT MAX(CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(lv5, '.', -1), '.', -1), '.', -1), '.', -1) AS UNSIGNED)) AS last_code_account FROM level_acc where lev_acc = 5 AND acc_up = $id_lv4 ");

                            // $account = $con->prepare("SELECT MAX(lv5) as last_code_account FROM level_acc where lev_acc = 5 AND acc_up = $id_lv4 ");
                            $account->execute();
                            // احصل على البيانات
                            $result = $account->fetch(PDO::FETCH_ASSOC);
                            // احصل على أكبر قيمة
                            $last_code_account = $result['last_code_account'];

                            // $account1s = $account->fetchAll();
                            // foreach ($account1s as $key => $account1) {
                                // $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv5 = $last_code_account + 1;
                                    $lv5 = $lv4.".".$lv5;
                                } else {
                                    $lv5 = $lv4 . ".1";
                                }
                            // }
                            $lv = $lv5;
                        }
                        if ($lvacc != 1){
                            $list_account = getName("level_acc", "ID", $id_lv1, "list_account");
                            $nature = getName("level_acc", "ID", $id_lv1, "nature_account");
                        }
                        // Insert Userinfo In Database
                        $stmt = $con->prepare("INSERT INTO 
                                                        level_acc(name_account, lev_acc, lv, lv1, lv2, lv3, lv4, lv5, acc_up, nature_account, list_account, user_id_add, DateInsert)
                                                    VALUES(:zname_account, :zlev_acc, :zlv, :zlv1, :zlv2, :zlv3, :zlv4, :zlv5, :zacc_up, :znature_account, :zlist_account, :zuser_id_add, now()) ");
                        $stmt->execute(
                            array(
                                'zname_account' => $nameacc,
                                'zlev_acc' => $lvacc,
                                'zlv' => $lv,
                                'zlv1' => $lv1,
                                'zlv2' => $lv2,
                                'zlv3' => $lv3,
                                'zlv4' => $lv4,
                                'zlv5' => $lv5,
                                'zacc_up' => $acc_up,
                                'znature_account' => $nature,
                                'zlist_account' => $list_account,
                                'zuser_id_add' => $_SESSION['admin']
                            )
                        );
                        // Echo Success Message
                        $theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' تمت الإضافة</div>';
                        redirectlink($theMsg, '?Manage=tree&do=Manage', "شجرة الحسابات", 5);
                    }
                } else {

                    echo "<div class='container'>";

                    $theMsg = '<div class="alert alert-danger">أسف، لا يمكنك الدخول إلى هذه الصفحة مباشرة</div>';

                    redirectHome($theMsg);

                    echo "</div>";
                }
            } elseif ($do == 'Edit') {

                // Check If Get Request userid Is Numeric & Get Its Integer Value

                $id = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;

                // Select All Data Depend On This ID

                $stmt = $con->prepare("SELECT * FROM cust_import WHERE ID = ? LIMIT 1");

                // Execute Query

                $stmt->execute(array($id));

                // Fetch The Data

                $row = $stmt->fetch();

                // The Row Count

                $count = $stmt->rowCount();

                // If There's Such ID Show The Form

                if ($count > 0) {
                ?>

                    <h3 class="text-center">تعديل بيانات <?php echo $row['Name_CI'] ?></h3>
                    <div class="container">
                        <form class="form-horizontal" action="?do=Update" method="POST">
                            <input type="hidden" name="id" value="<?php echo $id ?>" />
                            <!-- Start namecust Field -->
                            <div class="form-group form-group-lg">
                                <label class="col-sm-2 control-label">إسم العميل</label>
                                <div class="col-sm-10 col-md-6">
                                    <input type="text" name="name" class="form-control" value="<?php echo $row['Name_CI'] ?>" autocomplete="off" required="required" />
                                </div>
                            </div>
                            <!-- End namecust Field -->
                            <!-- Start phone Field -->
                            <div class="form-group form-group-lg">
                                <label class="col-sm-2 control-label">رقم الهاتف</label>
                                <div class="col-sm-10 col-md-6">
                                    <input type="text" name="phone" class="form-control" value="<?php echo $row['Phone'] ?>" />
                                </div>
                            </div>
                            <!-- End phone Field -->
                            <!-- Start address Field -->
                            <div class="form-group form-group-lg">
                                <label class="col-sm-2 control-label">العنوان</label>
                                <div class="col-sm-10 col-md-6">
                                    <input type="text" name="address" value="<?php echo $row['Address'] ?>" class="form-control" />
                                </div>
                            </div>
                            <!-- End address Field -->
                            <!-- Start permission Field -->
                            <div class="form-group form-group-lg">
                                <label class="col-sm-2 control-label">الرتبة</label>
                                <div class="col-sm-10 col-md-6">
                                    <select name="permission" class="form-control" required="required">
                                        <?php if ($row['vip'] == 0) {
                                        ?>
                                            <option value="0">عميل عادى</option>
                                            <option value="1">عميل مميز</option>
                                        <?php
                                        } else {
                                        ?>
                                            <option value="1">عميل مميز</option>
                                            <option value="0">عميل عادى</option>
                                        <?php
                                        } ?>
                                    </select>
                                </div>
                            </div>
                            <!-- End permission Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <input type="submit" value="حفظ التعديل" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                    // If There's No Such ID Show Error Message
                } else {

                    echo "<div class='container'>";

                    $theMsg = '<div class="alert alert-danger">من فضلك إتيع رابط صحيح</div>';

                    redirectHome($theMsg);

                    echo "</div>";
                }
            } elseif ($do == 'Update') { // Update Page

                echo "<h1 class='text-center'>تحديث البيانات</h1>";
                echo "<div class='container'>";

                if ($_SERVER['REQUEST_METHOD'] == 'POST') {

                    $formErrors = array();
                    // Get Variables From The Form

                    $id = $_POST['id'];
                    $user = $_POST['name'];
                    $phone = $_POST['phone'];
                    $address = $_POST['address'];
                    $permission = $_POST['permission'];

                    // Validate The Form

                    if (empty($user)) {
                        $formErrors[] = 'لا يجب أن يكون إسم العميل  <strong>فارغ</strong>';
                    }

                    // Loop Into Errors Array And Echo It

                    foreach ($formErrors as $error) {
                        echo '<div class="alert alert-danger">' . $error . '</div>';
                    }

                    // Check If There's No Error Proceed The Update Operation

                    if (empty($formErrors)) {

                        // Update The Database With This Info

                        $stmt = $con->prepare("UPDATE cust_import SET Name_CI = ?, vip = ?, Phone = ?, Address = ? WHERE ID = ?");

                        $stmt->execute(array($user, $permission, $phone, $address, $id));

                        // Echo Success Message

                        $theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' تم تحديث البيانات</div>';

                        redirectHome($theMsg, 'back');
                    }
                } else {

                    $theMsg = '<div class="alert alert-danger">أسف، لا يمكنك الدخول إلى هذه الصفحة مباشرة</div>';

                    redirectHome($theMsg);
                }

                echo "</div>";
            } elseif ($do == 'Delete') { // Delete Member Page

                echo "<h3 class='text-center'>حذف</h3>";
                echo "<div class='container'>";

                // Check If Get Request id Is Numeric & Get The Integer Value Of It

                $id = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;

                // Select All Data Depend On This ID

                $check = checkItem('ID', 'cust_import', $id);

                // If There's Such ID Show The Form

                if ($check > 0) {

                    $stmt = $con->prepare("DELETE FROM cust_import WHERE ID = :zid");

                    $stmt->bindParam(":zid", $id);

                    $stmt->execute();

                    $theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' تم الحذف بنجاح</div>';

                    redirectHome($theMsg, 'back');
                } else {

                    $theMsg = '<div class="alert alert-danger">برجاء إتباع رابط صحيح</div>';

                    redirectHome($theMsg);
                }

                echo '</div>';
            } elseif ($do == 'yomya') {
                if (isset($_POST['currency_code']) && !empty($_POST['currency_code'])){
                    $id_currency_code = $_POST['currency_code'];
                } else {
                    $id_currency_code = 1;
                }
                ?>
                
                <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=yomya" method="post">
                        <div class="row">
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="start_date" class="control-label">من تاريخ:</label>
                                    <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="end_date" class="control-label">الى تاريخ:</label>
                                    <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <!-- <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="end_date" class="control-label">رقم الحساب</label>
                                    <input type="text" name="num_acount" id="num_acount" class="form-control" value="<?=$id_ledger?>" placeholder="ادخل رقم الحساب" required>
                                </div>
                            </div> -->
                            <div class="form-group form-group-lg col-sm-2">
                                <div class="col-sm-12">
                                    <label for="currency_code" class="control-label">حدد عملة عرض الكشف</label>
                                    <select name="currency_code" class="form-select" required>
                                        <?php
                                        // Query to retrieve agents from the agents table
                                        $personsQuery = "SELECT * FROM currency where ID = 1 OR ID = 2";
                                        $personsResult = $con->query($personsQuery);
                                        while ($persons = $personsResult->fetch(PDO::FETCH_ASSOC)) {
                                            echo "<option value='{$persons['ID']}'>{$persons['currency_shortcut']}</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-1">
                                <div class="col-sm-12">
                                    <label></label>
                                    <button type="submit" class="btn btn-primary form-control" name="view_reservations"><i class="fa-solid fa-magnifying-glass"></i> بحث</button>
                                </div>
                            </div>
                        </div>
                    </form>
                <section class="container">
                    <div class="panel panel-primary text-center table-responsive">
                        <!-- Default panel contents -->
                        <div class="panel-heading">اليومية العامة بتاريخ <?php echo $start_date . " : " . $end_date; ?></div>
                        <!-- Table -->
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>
                                        التاريخ
                                    </th>
                                    <th>
                                        رقم القيد
                                    </th>
                                    <th>
                                        البيان
                                    </th>
                                    <th>
                                        مدين
                                    </th>
                                    <th>
                                        دائن
                                    </th>
                                    <th>
                                        #
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $currency_code = getId('currency','ID',$id_currency_code, 'currency_code');
                                $allmadeen = 0;
                                $alldaen = 0;
                                $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                $sqlyomya->execute();
                                $yomyas = $sqlyomya->fetchAll();
                                foreach ($yomyas as $yomya) {
                                    $id_yomya = $yomya['id_yomya'];
                                    ?>
                                    <tr class="success" style="border-top: 3px solid;">
                                        <td>
                                            <?php echo $yomya['dateinsert'] ?>
                                        </td>

                                        <td>
                                            <?php echo $yomya['registration_number'] ?>
                                        </td>

                                        <td style="text-align: right;">
                                            <?php
                                            $id_account_lv4 = 0;
                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' ");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            $count_yomyas = $sqldetails_yomya->rowCount();
                                            if ($count_yomyas > 2) {
                                                # code...
                                            }
                                            $count_from = 0;
                                            $count_to = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                if ($from_or_to == 1) {
                                                    $count_from += 1;
                                                }

                                                if ($from_or_to == 2) {
                                                    $count_to++;
                                                }
                                            }
                                            $name_from_or_to_m = "";
                                            $count_from_num = 0;
                                            $count_to_num = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];

                                                if($id_currency_code != 1){
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                }
                                                if ($from_or_to == 1) {
                                                    $madeen = $cash;
                                                    $allmadeen += $madeen;
                                                    $id_account_lv3 = $details_yomya['lv3'];
                                                    $id_account_lv4 = $details_yomya['lv4'];

                                                    if ($count_from == 1) {
                                                        $name_from_or_to = "من حـ/ ";
                                                    } elseif ($count_from > 1) {
                                                        if ($count_from_num == 0) {
                                                            $name_from_or_to_m = "<b>من مذكورين/</b> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                            $name_from_or_to = "";
                                                        } else {
                                                            $name_from_or_to_m = "";
                                                            $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                        }
                                                    }
                                                    $count_from_num++;
                                                } elseif ($from_or_to == 2) {
                                                    $daen = $cash;
                                                    $alldaen += $daen;
                                                    $id_account_lv3 = $details_yomya['lv3'];
                                                    $id_account_lv4 = $details_yomya['lv4'];

                                                    if ($count_to == 1) {
                                                        $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;إلى حـ/ ";
                                                    } elseif ($count_to > 1) {
                                                        if ($count_to_num == 0) {
                                                            $name_from_or_to_m = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>إلى مذكورين/</b> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                            $name_from_or_to = "";
                                                        } else {
                                                            $name_from_or_to_m = "";
                                                            $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                        }
                                                    }
                                                    $count_to_num++;
                                                }
                                                $sqlaccounts = $con->prepare("SELECT * from level_acc where lv3 = '$id_account_lv3' AND lev_acc = 3 ");
                                                $sqlaccounts->execute();
                                                $accounts = $sqlaccounts->fetchAll();
                                                foreach ($accounts as $account) {
                                                    $name_account_lv3 = $account['name_account'];
                                                }
                                                $name_account_lv4 = "";
                                                if (isset($id_account_lv4) && !empty($id_account_lv4)) {
                                                    $name_account_lv4 = " - " . getName("level_acc", "lv4 ", $id_account_lv4, "name_account");
                                                }
                                                echo $name_from_or_to . $name_from_or_to_m . $name_account_lv3 . $name_account_lv4;
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo $yomya['payan'];
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $count_from_num = 0;
                                            $allmadeen_thes = 0;
                                            $alldaen_thes = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];

                                                if($id_currency_code != 1){
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                }
                                                if ($from_or_to == 1) {
                                                    
                                                    $madeen = $cash;
                                                    $allmadeen_thes += $madeen;
                                                    if ($count_from == 1) {
                                                        echo $madeen. ' '. $currency_code;
                                                    } elseif ($count_from > 1) {
                                                        if ($count_from_num == 0) {
                                                            echo "<br>" . $madeen. ' '. $currency_code;
                                                        } else {
                                                            echo "<br>" . $madeen. ' '. $currency_code;
                                                        }
                                                    }
                                                    $count_from_num++;
                                                }
                                            }
                                            if ($count_to > 1) {
                                                $new_count_to = $count_to + 1;
                                            } else {
                                                $new_count_to = $count_to;
                                            }
                                            for ($i = 0; $i <= $new_count_to; $i++) {
                                                echo "<br>";
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo "<b>" . $allmadeen_thes. ' '. $currency_code . "</b>";
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $count_to_num = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];

                                                if($id_currency_code != 1){
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                }
                                                if ($from_or_to == 2) {
                                                    $daen = $cash;
                                                    $alldaen_thes += $daen;
                                                    if ($count_to == 1) {
                                                        echo "<br>" . $daen. ' '. $currency_code;
                                                    } elseif ($count_to > 1) {
                                                        if ($count_to_num == 0) {
                                                            for ($i = 0; $i <= $count_from; $i++) {
                                                                echo "<br>";
                                                            }
                                                            echo "<br>" . $daen. ' '. $currency_code;
                                                        } else {
                                                            echo "<br>" . $daen. ' '. $currency_code;
                                                        }
                                                    }
                                                    $count_to_num++;
                                                }
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo "<b>" . $alldaen_thes. ' '. $currency_code . "</b>";
                                            ?>
                                        </td>
                                        <td>
                                        <a href='?Manage=tree&do=edit_yomya&id=<?=$yomya['registration_number']?>&currency_code=<?=$id_currency_code?>' class='btn btn-success'><i class="fa-solid fa-user-pen"></i> </a>
                                        </td>
                                    </tr>
                                <?php
                                }
                                ?>
                            </tbody>
                            <tfoot>
                                <tr class="success">
                                    <td>

                                    </td>
                                    <td>

                                    </td>
                                    <th>
                                        الإجمالى
                                    </th>
                                    <th>
                                        <?php echo $allmadeen. ' '. $currency_code ?>
                                    </th>
                                    <th>
                                        <?php echo $alldaen. ' '. $currency_code ?>
                                    </th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    </section>
                    <?php
            } elseif ($do == 'ledger') {
                ?>
                <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=ledger" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                            </div>
                        </div>
                    </div>
                </form>
                <section class="container">
                    <div class="panel panel-primary text-center table-responsive">
                        <!-- Default panel contents -->
                        <div class="panel-heading">دفتر الأستاذ بتاريخ <?php echo $start_date . " : " . $end_date; ?></div>
                        <!-- Table -->
                        <!-- <table class="table table-bordered"> -->
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>
                                        الحساب
                                        <a data-bs-toggle="collapse" href=".code_accountlv3" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3" title='عرض المستوى الرابع'>
                                            <i class='fa fa-caret-down' style="font-size:18px"></i>
                                        </a>
                                    </th>
                                    <th>
                                        مدين
                                    </th>
                                    <th>
                                        دائن
                                    </th>
                                    <th>
                                        رصيد مرحل مدين
                                    </th>
                                    <th>
                                        رصيد مرحل دائن
                                    </th>
                                </tr>
                            </thead>
                            <tbody id="myTable">
                                <?php
                                $allcashfrom_end = 0;
                                $allcashfromto_end = 0;
                                $mora7lfrom = 0;
                                $mora7lto = 0;
                                $all_mora7lfrom = 0;
                                $all_mora7lto = 0;
                                $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE lev_acc = 2 ");
                                $sqlaccounts->execute();
                                $accounts = $sqlaccounts->fetchAll();
                                foreach ($accounts as $account) {
                                    $ID = $account['ID'];
                                    $name_account = $account['name_account'];
                                    $code_account = $account['lv2'];
                                    $nature_acc = $account['nature_account'];
                                ?>
                                    <tr class="success" style="height: 40px;">
                                        <th style="text-align: right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <a href="reports_account.php?Manage=tree&do=ditels_ledger&id_ledger=<?php echo $ID; ?>">
                                                حــ / <?php echo $name_account; ?>
                                            </a>
                                            <a data-bs-toggle="collapse" href=".code_accountlv3_<?php echo $ID ?>" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3_<?php echo $ID ?>" title='عرض المستوى الثالث'>
                                                <i class='fa fa-caret-down' style="font-size:18px"></i>
                                            </a>
                                        </th>
                                        <td>
                                            <?php
                                            $sqldetails_yomya = $con->prepare("SELECT SUM(cash) AS 'cashfrom' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv2 = '$code_account' AND from_or_to = 1");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            //$countfrom = $sqldetails_yomya->rowCount();
                                            foreach ($details_yomyas as $details_yomya) {
                                                $allcashfrom = $details_yomya['cashfrom'];
                                                $allcashfrom_end += $allcashfrom;
                                            }
                                            $sqldetails_yomya = $con->prepare("SELECT SUM(cash) AS 'cashto' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv2 = '$code_account' AND from_or_to = 2");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            //$countfrom = $sqldetails_yomya->rowCount();
                                            foreach ($details_yomyas as $details_yomya) {
                                                $allcashfromto = $details_yomya['cashto'];
                                                $allcashfromto_end += $allcashfromto;
                                            }
                                            echo $allcashfrom;
                                            if ($allcashfrom < $allcashfromto) {
                                                $mora7lto = $allcashfromto - $allcashfrom;
                                            }
                                            //echo $allcashfrom;
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            echo $allcashfromto;
                                            if ($allcashfromto < $allcashfrom) {
                                                $mora7lfrom = $allcashfrom - $allcashfromto;
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            if (isset($mora7lfrom) && $mora7lfrom >= 0) {
                                                echo $mora7lfrom;
                                                $all_mora7lfrom += $mora7lfrom;
                                                $mora7lfrom = 0;
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            if (isset($mora7lto) && $mora7lto >= 0) {
                                                echo $mora7lto;
                                                $all_mora7lto += $mora7lto;
                                                $mora7lto = 0;
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                    <?php
                                    $sqlaccounts = $con->prepare("SELECT * FROM level_acc WHERE lv2 = :code_account AND lev_acc = 3");
                                    $sqlaccounts->bindParam(':code_account', $code_account);
                                    $sqlaccounts->execute();
                                    $accounts = $sqlaccounts->fetchAll();
                                    foreach ($accounts as $account) {
                                        $IDlv4 = $account['ID'];
                                        $name_account = $account['name_account'];
                                        $lv4 = $account['lv3'];
                                        $nature_acc = $account['nature_account'];
                                    ?>
                                        <tr id="code_accountlv3_<?php echo $ID ?>" class="code_accountlv3_<?php echo $ID ?> code_accountlv3 table-danger collapse" aria-expanded="false" style="height: 40px;">
                                            <th style="text-align: right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                <a href="reports_account.php?Manage=tree&do=ditels_ledger&id_lv4=<?php echo $IDlv4; ?>">
                                                    حــ / <?php echo $name_account; ?>
                                                </a>
                                            </th>
                                            <td>
                                                <?php
                                                $sqldetails_yomya = $con->prepare("SELECT SUM(cash) AS 'cashfrom' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv3 = '$lv4' AND from_or_to = 1");
                                                $sqldetails_yomya->execute();
                                                $details_yomyas = $sqldetails_yomya->fetchAll();
                                                //$countfrom = $sqldetails_yomya->rowCount();
                                                foreach ($details_yomyas as $details_yomya) {
                                                    $allcashfrom = $details_yomya['cashfrom'];
                                                    // $allcashfrom_end += $allcashfrom;
                                                }
                                                $sqldetails_yomya = $con->prepare("SELECT SUM(cash) AS 'cashto' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv3 = '$lv4' AND from_or_to = 2");
                                                $sqldetails_yomya->execute();
                                                $details_yomyas = $sqldetails_yomya->fetchAll();
                                                //$countfrom = $sqldetails_yomya->rowCount();
                                                foreach ($details_yomyas as $details_yomya) {
                                                    $allcashfromto = $details_yomya['cashto'];
                                                    // $allcashfromto_end += $allcashfromto;
                                                }
                                                echo $allcashfrom;
                                                if ($allcashfrom < $allcashfromto) {
                                                    $mora7lto = $allcashfromto - $allcashfrom;
                                                }
                                                //echo $allcashfrom;
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                echo $allcashfromto;
                                                if ($allcashfromto < $allcashfrom) {
                                                    $mora7lfrom = $allcashfrom - $allcashfromto;
                                                }
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                if (isset($mora7lfrom) && $mora7lfrom >= 0) {
                                                    echo $mora7lfrom;
                                                    // $all_mora7lfrom += $mora7lfrom;
                                                    $mora7lfrom = 0;
                                                }
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                if (isset($mora7lto) && $mora7lto >= 0) {
                                                    echo $mora7lto;
                                                    // $all_mora7lto += $mora7lto;
                                                    $mora7lto = 0;
                                                }
                                                ?>
                                            </td>
                                        </tr>
                                <?php
                                    }
                                }
                                ?>
                                <tr>
                                    <th>
                                        الإجمالى
                                    </th>
                                    <th>
                                        <?php
                                        echo $allcashfrom_end;
                                        ?>
                                    </th>
                                    <th>
                                        <?php
                                        echo $allcashfromto_end;
                                        ?>
                                    </th>
                                    <th>
                                        <?php
                                        echo $all_mora7lfrom;
                                        ?>
                                    </th>
                                    <th>
                                        <?php
                                        echo $all_mora7lto;
                                        ?>
                                    </th>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </section>
                <?php
            } elseif ($do == 'ditels_ledger') {
                if (!empty($_GET['id_ledger']) && is_numeric($_GET['id_ledger'])) {
                    $id_ledger = $_GET['id_ledger'];
                ?>
                <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=ditels_ledger&id_ledger=<?php echo $id_ledger; ?>" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                            </div>
                        </div>
                    </div>
                </form>
                    <section class="container">
                        <div class="panel panel-primary text-center table-responsive">
                            <!-- Default panel contents -->
                            <div class="panel-heading">دفتر الأستاذ بتاريخ <?php echo $start_date . " : " . $end_date; ?></div>
                            <!-- Table -->
                            <?php
                            $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE ID = '$id_ledger' ");
                            $sqlaccounts->execute();
                            $accounts = $sqlaccounts->fetchAll();
                            foreach ($accounts as $account) {
                                $ID = $account['ID'];
                                $name_account = $account['name_account'];
                                $code_account = $account['lv2'];
                                // $nature_acc = $account['nature_acc'];
                            ?>
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th colspan="2">
                                                حــ / <?php echo $name_account; ?>
                                            </th>
                                        </tr>
                                        <tr>
                                            <th style="width: 50%;">
                                                مدين
                                            </th>
                                            <th style="width: 50%;">
                                                دائن
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="success">
                                            <td>
                                                <table class="table table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>
                                                                المبلغ
                                                            </th>
                                                            <th>
                                                                البيان
                                                            </th>
                                                            <th>
                                                                رقم القيد
                                                            </th>
                                                            <th>
                                                                التاريخ
                                                            </th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        $allcashfrom = 0;
                                                        $allcashfromto = 0;
                                                        $trow = 0;
                                                        $trowto = 0;
                                                        $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $registration_number = $yomya['registration_number'];
                                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND lv2 = '$code_account'");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            $countfrom = $sqldetails_yomya->rowCount();
                                                            foreach ($details_yomyas as $details_yomya) {
                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_yomya2 = $details_yomya['id_yomya'];
                                                                $dateinsert = $details_yomya['dateinsert'];

                                                                $name_account2 = '';
                                                                $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 2");
                                                                $sqldetails_yomya2->execute();
                                                                $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                foreach ($details_yomyas2 as $details_yomya2) {
                                                                    $code_lv2 = $details_yomya2['lv2'];
                                                                    $code_lv3 = $details_yomya2['lv3'];
                                                                    $code_lv4 = $details_yomya2['lv4'];
                                                                    $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv2 = '$code_lv2' AND lev_acc = 2 ");
                                                                    $sqlaccounts2->execute();
                                                                    $accounts2 = $sqlaccounts2->fetchAll();
                                                                    foreach ($accounts2 as $account2) {
                                                                        $name_account2 = $account2['name_account'];
                                                                    }
                                                                }
                                                                $count_code_lv4 = 0;
                                                                $name_accountlv4 = "";
                                                                if (!empty($code_lv4)) {
                                                                    $count_code_lv4 = strlen($code_lv4);
                                                                    if ($count_code_lv4 < 7) {
                                                                        $name_accountlv4 = "";
                                                                    } else {
                                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv4 = '$code_lv4' ");
                                                                        $sqlaccounts2->execute();
                                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                                        foreach ($accounts2 as $account2) {
                                                                            $name_accountlv4 = $account2['name_account'];
                                                                        }
                                                                    }
                                                                } else {
                                                                    $name_accountlv4 = "";
                                                                }
                                                                if ($from_or_to == 2) {
                                                                    $allcashfromto += $cash;
                                                                }
                                                                if ($from_or_to == 1) {
                                                        ?>
                                                                    <tr class="success" style="height: 40px;">
                                                                        <td>
                                                                            <?php
                                                                            $allcashfrom += $cash;
                                                                            echo $cash;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php

                                                                            ?>
                                                                            إلى حــ /
                                                                            <?php
                                                                            echo $name_account2 . " - " . $name_accountlv4;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php
                                                                            echo $registration_number;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php
                                                                            echo $dateinsert;
                                                                            ?>
                                                                        </td>
                                                                    </tr>
                                                                <?php
                                                                    $trowto += 1;
                                                                } elseif ($from_or_to == 2) {
                                                                    $trow += 1;
                                                                }
                                                            }
                                                        }
                                                        $newrow = $trowto - $trow;
                                                        if ($newrow < 0) {
                                                            $newrow = $newrow - $newrow - $newrow;
                                                            for ($i = 0; $i < $newrow; $i++) {
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                </tr>
                                                        <?php
                                                            }
                                                        }

                                                        ?>
                                                        <tr style="height: 40px;">
                                                            <td>
                                                                <?php
                                                                if ($allcashfrom > $allcashfromto) {
                                                                    $mora7l = $allcashfrom - $allcashfromto;
                                                                    $allfromto = $allcashfrom;
                                                                ?>
                                                            </td>
                                                            <td>
                                                            </td>
                                                        <?php
                                                                } else {
                                                                    echo $mora7l = $allcashfromto - $allcashfrom;
                                                                    $allfromto = $allcashfromto;
                                                        ?>
                                            </td>
                                            <td>
                                                رصيد مرحل
                                            </td>
                                        <?php
                                                                }
                                                                //echo $allcashfrom;
                                        ?>

                                        <td>

                                        </td>
                                        <td>

                                        </td>
                                        </tr>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <th>
                                                <?php
                                                echo $allfromto;
                                                ?>
                                            </th>
                                            <th>
                                                الإجمالى
                                            </th>
                                            <td>

                                            </td>
                                            <td>

                                            </td>
                                        </tr>
                                    </tfoot>
                                </table>
                                </td>
                                <td>
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>
                                                    المبلغ
                                                </th>
                                                <th>
                                                    البيان
                                                </th>
                                                <th>
                                                    رقم القيد
                                                </th>
                                                <th>
                                                    التاريخ
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $allcashto = 0;
                                            $allcashtofrom = 0;
                                            $trow2 = 0;
                                            $trowfrom = 0;
                                            $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $id_yomya = $yomya['id_yomya'];
                                                $registration_number = $yomya['registration_number'];
                                                $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND lv2 = '$code_account'");
                                                $sqldetails_yomya->execute();
                                                $details_yomyas = $sqldetails_yomya->fetchAll();
                                                foreach ($details_yomyas as $details_yomya) {
                                                    $from_or_to = $details_yomya['from_or_to'];
                                                    $cash = $details_yomya['cash'];
                                                    $id_yomya2 = $details_yomya['id_yomya'];
                                                    $dateinsert = $details_yomya['dateinsert'];

                                                    $name_account2 = '';
                                                    $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1");
                                                    $sqldetails_yomya2->execute();
                                                    $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                    foreach ($details_yomyas2 as $details_yomya2) {
                                                        $code_lv2 = $details_yomya2['lv2'];
                                                        $code_lv3 = $details_yomya2['lv3'];
                                                        $code_lv4 = $details_yomya2['lv4'];
                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv2 = '$code_lv2' AND lev_acc = 2 ");
                                                        $sqlaccounts2->execute();
                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                        foreach ($accounts2 as $account2) {
                                                            $name_account2 = $account2['name_account'];
                                                        }
                                                    }
                                                    $count_code_lv4 = 0;
                                                    $name_accountlv4 = "";
                                                    if (!empty($code_lv4)) {
                                                        $count_code_lv4 = strlen($code_lv4);
                                                        if ($count_code_lv4 < 7) {
                                                            $name_accountlv4 = "";
                                                        } else {
                                                            $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv4 = '$code_lv4' ");
                                                            $sqlaccounts2->execute();
                                                            $accounts2 = $sqlaccounts2->fetchAll();
                                                            foreach ($accounts2 as $account2) {
                                                                $name_accountlv4 = $account2['name_account'];
                                                            }
                                                        }
                                                    } else {
                                                        $name_accountlv4 = "";
                                                    }
                                                    if ($from_or_to == 1) {
                                                        $allcashtofrom += $cash;
                                                    }
                                                    if ($from_or_to == 2) {
                                            ?>
                                                        <tr class="success" style="height: 40px;">
                                                            <td>
                                                                <?php
                                                                $allcashto += $cash;
                                                                echo $cash;
                                                                ?>
                                                            </td>
                                                            <td>
                                                                من حــ /
                                                                <?php
                                                                echo $name_account2 . " - " . $name_accountlv4;
                                                                ?>
                                                            </td>
                                                            <td>
                                                                <?php
                                                                echo $registration_number;
                                                                ?>
                                                            </td>
                                                            <td>
                                                                <?php
                                                                echo $dateinsert;
                                                                ?>
                                                            </td>
                                                        </tr>
                                                    <?php
                                                        $trowfrom += 1;
                                                    } elseif ($from_or_to == 1) {
                                                        $trow2 += 1;
                                                    }
                                                }
                                            }
                                            $newrow = $trowfrom - $trow2;
                                            if ($newrow < 0) {
                                                $newrow = $newrow - $newrow - $newrow;
                                                for ($i = 0; $i < $newrow; $i++) {
                                                    ?>
                                                    <tr class="success" style="height: 40px;">
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                    </tr>
                                            <?php
                                                }
                                            }
                                            ?>
                                            <tr style="height: 40px;">
                                                <td>
                                                    <?php
                                                    if ($allcashto > $allcashtofrom) {
                                                        $mora7l = $allcashto - $allcashtofrom;
                                                        $allfromto = $allcashto;
                                                    ?>
                                                </td>
                                                <td>
                                                </td>
                                            <?php
                                                    } else {
                                                        echo $mora7l = $allcashtofrom - $allcashto;
                                                        $allfromto = $allcashtofrom;
                                            ?>
                                </td>
                                <td>
                                    رصيد مرحل
                                </td>
                            <?php
                                                    }
                                                    //echo $allcashfrom;
                            ?>

                            <td>

                            </td>
                            <td>

                            </td>
                            </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th>
                                        <?php
                                        echo $allfromto;
                                        ?>
                                    </th>
                                    <th>
                                        الإجمالى
                                    </th>
                                    <td>

                                    </td>
                                    <td>

                                    </td>
                                </tr>
                            </tfoot>
                            </table>
                            </td>
                            </tr>

                            </tbody>
                            </table>
                        <?php
                            }
                        ?>
                        </div>
                    </section>
                <?php
                } elseif (!empty($_GET['id_lv4']) && is_numeric($_GET['id_lv4'])) {
                    $id_ledger = $_GET['id_lv4'];
                ?>
                    <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=ditels_ledger&id_lv4=<?php echo $id_ledger; ?>" method="post">
                        <h3>بحث خلال الفترة من : </h3>
                        <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                        <h3> إلى : </h3>
                        <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                        <input class="form-control" type="submit" value="بحث">
                    </form>
                    <!-- start -->
                    <section class="container">
                        <div class="panel panel-primary text-center table-responsive">
                            <!-- Default panel contents -->
                            <div class="panel-heading">دفتر الأستاذ بتاريخ <?php echo $start_date . " : " . $end_date; ?></div>
                            <!-- Table -->
                            <?php
                            $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE ID = '$id_ledger' ");
                            $sqlaccounts->execute();
                            $accounts = $sqlaccounts->fetchAll();
                            foreach ($accounts as $account) {
                                $ID = $account['ID'];
                                $name_account = $account['name_account'];
                                $code_account = $account['lv3'];
                                //$lv4 = $account['lv4'];
                                $nature_acc = $account['nature_account'];
                            ?>
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th colspan="2">
                                                حــ / <?php echo $name_account; ?>
                                            </th>
                                        </tr>
                                        <tr>
                                            <th style="width: 50%;">
                                                مدين
                                            </th>
                                            <th style="width: 50%;">
                                                دائن
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="success">
                                            <td>
                                                <table class="table table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>
                                                                المبلغ
                                                            </th>
                                                            <th>
                                                                البيان
                                                            </th>
                                                            <th>
                                                                رقم القيد
                                                            </th>
                                                            <th>
                                                                التاريخ
                                                            </th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        $allcashfrom = 0;
                                                        $allcashfromto = 0;
                                                        $trow = 0;
                                                        $trowto = 0;
                                                        $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $registration_number = $yomya['registration_number'];
                                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND lv3 = '$code_account'");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            $countfrom = $sqldetails_yomya->rowCount();
                                                            foreach ($details_yomyas as $details_yomya) {
                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_yomya2 = $details_yomya['id_yomya'];
                                                                $dateinsert = $details_yomya['dateinsert'];
                                                                $name_account2 = '';
                                                                $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 2");
                                                                $sqldetails_yomya2->execute();
                                                                $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                foreach ($details_yomyas2 as $details_yomya2) {
                                                                    $code_lv2 = $details_yomya2['lv2'];
                                                                    $code_lv3 = $details_yomya2['lv3'];
                                                                    $code_lv4 = $details_yomya2['lv4'];
                                                                    $count_code_lv4 = 0;
                                                                    if (!empty($code_lv4) || !empty($code_lv3) || !empty($code_lv2)) {
                                                                        $count_code_lv4 = strlen($code_lv4); 
                                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv2 = '$code_lv2' and lev_acc = 2 ");
                                                                        $sqlaccounts2->execute();
                                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                                        foreach ($accounts2 as $account2) {
                                                                            $name_account2 = $account2['name_account'];
                                                                            $lv = $account2['lv'];
                                                                        }
                                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv3 = '$code_lv3' and lev_acc = 3 ");
                                                                        $sqlaccounts2->execute();
                                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                                        foreach ($accounts2 as $account2) {
                                                                            $name_account2 = $account2['name_account'];
                                                                            $lv = $account2['lv'];
                                                                        }
                                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv4 = '$code_lv4' and lev_acc = 4 ");
                                                                        $sqlaccounts2->execute();
                                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                                        foreach ($accounts2 as $account2) {
                                                                            $name_account2 .=" - " .$account2['name_account'];
                                                                            $lv = $account2['lv'];
                                                                        }
                                                                    } else {
                                                                        $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 2");
                                                                        $sqldetails_yomya2->execute();
                                                                        $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                        foreach ($details_yomyas2 as $details_yomya2) {
                                                                            $code_lv3 = $details_yomya2['lv3'];
                                                                        }
                                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv3 = '$code_lv3' AND lev_acc = 3 ");
                                                                        $sqlaccounts2->execute();
                                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                                        foreach ($accounts2 as $account2) {
                                                                            $name_account2 = $account2['name_account'];
                                                                            $lv = $account2['lv'];
                                                                        }
                                                                    }
                                                                }
                                                                if ($from_or_to == 2) {
                                                                    $allcashfromto += $cash;
                                                                }
                                                                if ($from_or_to == 1) {
                                                        ?>
                                                                    <tr class="success" style="height: 40px;">
                                                                        <td>
                                                                            <?php
                                                                            $allcashfrom += $cash;
                                                                            echo $cash;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php

                                                                            ?>
                                                                            إلى حــ /
                                                                            <?php
                                                                            echo $name_account2 . " حساب رقم " .$lv;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php
                                                                            echo $registration_number;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php
                                                                            echo $dateinsert;
                                                                            ?>
                                                                        </td>
                                                                    </tr>
                                                                <?php
                                                                    $trowto += 1;
                                                                } elseif ($from_or_to == 2) {
                                                                    $trow += 1;
                                                                }
                                                            }
                                                        }
                                                        $newrow = $trowto - $trow;
                                                        if ($newrow < 0) {
                                                            $newrow = $newrow - $newrow - $newrow;
                                                            for ($i = 0; $i < $newrow; $i++) {
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                </tr>
                                                        <?php
                                                            }
                                                        }
                                                        ?>
                                                        <tr style="height: 40px;">
                                                            <td>

                                                                <?php
                                                                if ($allcashfrom > $allcashfromto) {
                                                                    $mora7l = $allcashfrom - $allcashfromto;
                                                                    $allfromto = $allcashfrom;
                                                                    echo '</td><td>';
                                                                } else {
                                                                    echo $mora7l = $allcashfromto - $allcashfrom;
                                                                    $allfromto = $allcashfromto;
                                                                    echo '</td><td>';
                                                                    echo 'رصيد مرحل';
                                                                }
                                                                //echo $allcashfrom;
                                                                ?>
                                                            </td>
                                                            <td>

                                                            </td>
                                                            <td>

                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th>
                                                                <?php
                                                                echo $allfromto;
                                                                ?>
                                                            </th>
                                                            <th>
                                                                الإجمالى
                                                            </th>
                                                            <td>

                                                            </td>
                                                            <td>

                                                            </td>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </td>
                                            <td>
                                                <table class="table table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>
                                                                المبلغ
                                                            </th>
                                                            <th>
                                                                البيان
                                                            </th>
                                                            <th>
                                                                رقم القيد
                                                            </th>
                                                            <th>
                                                                التاريخ
                                                            </th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        $allcashto = 0;
                                                        $allcashtofrom = 0;
                                                        $trow2 = 0;
                                                        $trowfrom = 0;
                                                        $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $registration_number = $yomya['registration_number'];
                                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND lv3 = '$code_account'");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            foreach ($details_yomyas as $details_yomya) {
                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_yomya2 = $details_yomya['id_yomya'];
                                                                $dateinsert = $details_yomya['dateinsert'];
                                                                $name_account2 = "";
                                                                $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1");
                                                                $sqldetails_yomya2->execute();
                                                                $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                foreach ($details_yomyas2 as $details_yomya2) {
                                                                    $code_lv3 = $details_yomya2['lv3'];
                                                                     $code_lv4 = $details_yomya2['lv4'];
                                                                    $count_code_lv4 = 0;
                                                                    if (!empty($code_lv3) || !empty($code_lv4)) {
                                                                        
                                                                            $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv3 = '$code_lv3' and lev_acc = 3 ");
                                                                            $sqlaccounts2->execute();
                                                                            $accounts2 = $sqlaccounts2->fetchAll();
                                                                            foreach ($accounts2 as $account2) {
                                                                                $name_account2 = $account2['name_account'];
                                                                            }
                                                                            $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv4 = '$code_lv4' and lev_acc = 4 ");
                                                                            $sqlaccounts2->execute();
                                                                            $accounts2 = $sqlaccounts2->fetchAll();
                                                                            foreach ($accounts2 as $account2) {
                                                                                $name_account2 .=" - " .$account2['name_account'];
                                                                            }
                                                                        
                                                                    } else {
                                                                        $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1");
                                                                        $sqldetails_yomya2->execute();
                                                                        $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                        foreach ($details_yomyas2 as $details_yomya2) {
                                                                            $code_lv2 = $details_yomya2['lv2'];
                                                                            $code_lv3 = $details_yomya2['lv3'];
                                                                        }
                                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv2 = '$code_lv2' ");
                                                                        $sqlaccounts2->execute();
                                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                                        foreach ($accounts2 as $account2) {
                                                                            $name_account2 = $account2['name_account'];
                                                                        }
                                                                    }
                                                                }
                                                                if ($from_or_to == 1) {
                                                                    $allcashtofrom += $cash;
                                                                }
                                                                if ($from_or_to == 2) {
                                                        ?>
                                                                    <tr class="success" style="height: 40px;">
                                                                        <td>
                                                                            <?php
                                                                            $allcashto += $cash;
                                                                            echo $cash;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            من حــ /
                                                                            <?php
                                                                            echo $name_account2;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php
                                                                            echo $registration_number;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php
                                                                            echo $dateinsert;
                                                                            ?>
                                                                        </td>
                                                                    </tr>
                                                                <?php
                                                                    $trowfrom += 1;
                                                                } elseif ($from_or_to == 1) {
                                                                    $trow2 += 1;
                                                                }
                                                            }
                                                        }
                                                        $newrow = $trowfrom - $trow2;
                                                        if ($newrow < 0) {
                                                            $newrow = $newrow - $newrow - $newrow;
                                                            for ($i = 0; $i < $newrow; $i++) {
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                </tr>
                                                        <?php
                                                            }
                                                        }
                                                        ?>
                                                        <tr style="height: 40px;">
                                                            <td>
                                                                <?php
                                                                if ($allcashto > $allcashtofrom) {
                                                                    $mora7l = $allcashto - $allcashtofrom;
                                                                    $allfromto = $allcashto;
                                                                    echo '</td><td>';
                                                                } else {
                                                                    echo $mora7l = $allcashtofrom - $allcashto;
                                                                    $allfromto = $allcashtofrom;
                                                                    echo '</td><td>';
                                                                    echo 'رصيد مرحل';
                                                                }
                                                                //echo $allcashfrom;
                                                                ?>
                                                            </td>
                                                            <td>
                                                            </td>
                                                            <td>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th>
                                                                <?php
                                                                echo $allfromto;
                                                                ?>
                                                            </th>
                                                            <th>
                                                                الإجمالى
                                                            </th>
                                                            <td>

                                                            </td>
                                                            <td>

                                                            </td>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            <?php
                            }
                            ?>
                        </div>
                    </section>
                <?php
                } else {
                ?>
                    <div class="alert alert-danger">
                        عذراً هناك خطاء.. برجاء إتبع رابط صحيح
                    </div>
                <?php
                }
            } elseif ($do == 'account_statements') {
                ?>
                    <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=account_statement" method="post">
                        <div class="row">
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="start_date" class="control-label">من تاريخ:</label>
                                    <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="end_date" class="control-label">الى تاريخ:</label>
                                    <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="end_date" class="control-label">رقم الحساب</label>
                                    <input type="text" name="num_acount" id="num_acount" class="form-control" value="" placeholder="ادخل رقم الحساب" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-2">
                            <div class="col-sm-12">
                                <label for="currency_code" class="control-label">حدد عملة عرض الكشف</label>
                                <select name="currency_code" class="form-select" required>
                                    <?php
                                    // Query to retrieve agents from the agents table
                                    $personsQuery = "SELECT * FROM currency where ID = 1 OR ID = 2";
                                    $personsResult = $con->query($personsQuery);
                                    while ($persons = $personsResult->fetch(PDO::FETCH_ASSOC)) {
                                        echo "<option value='{$persons['ID']}'>{$persons['currency_shortcut']}</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                            <div class="form-group form-group-lg col-sm-1">
                                <div class="col-sm-12">
                                    <label></label>
                                    <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <?php
            } elseif ($do == 'account_statement') {
                if (!empty($_POST['num_acount']) ) {
                    $id_ledger = $_POST['num_acount'];
                    if (!empty($_POST['currency_code'])){
                        $id_currency_code = $_POST['currency_code'];
                    } else {
                        $id_currency_code = 1;
                    }
                ?>
                <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=account_statement&id_ledger=<?php echo $id_ledger; ?>" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">رقم الحساب</label>
                                <input type="text" name="num_acount" id="num_acount" class="form-control" value="<?=$id_ledger?>" placeholder="ادخل رقم الحساب" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-2">
                            <div class="col-sm-12">
                                <label for="currency_code" class="control-label">حدد عملة عرض الكشف</label>
                                <select name="currency_code" class="form-select" required>
                                    <?php
                                    // Query to retrieve agents from the agents table
                                    $personsQuery = "SELECT * FROM currency where ID = 1 OR ID = 2";
                                    $personsResult = $con->query($personsQuery);
                                    while ($persons = $personsResult->fetch(PDO::FETCH_ASSOC)) {
                                        echo "<option value='{$persons['ID']}'>{$persons['currency_shortcut']}</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                            </div>
                        </div>
                    </div>
                </form>
                    <section class="container">
                        <div class="panel panel-primary text-center table-responsive">
                            <?php
                            $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE lv = '$id_ledger' ");
                            $sqlaccounts->execute();
                            $accounts = $sqlaccounts->fetchAll();
                            foreach ($accounts as $account) {
                                $ID = $account['ID'];
                                $name_account = $account['name_account'];
                                $lev_acc = $account['lev_acc'];
                                $name_lev_acc = 'lv'.$lev_acc;
                                $code_account = $account['lv'];
                                $currency_code = getId('currency','ID',$id_currency_code, 'currency_code');
                                // $nature_acc = $account['nature_acc'];
                                ?>
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th colspan="2">
                                                كشف حساب  / <?php echo $name_account; ?>
                                            </th>
                                        </tr>
                                        <tr>
                                            <th style="width: 50%;">
                                            عن الفترة من تاريخ  <?php echo $start_date . " الى تاريخ " . $end_date; ?>
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="success">
                                            <td>
                                                <table class="table table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>
                                                                تاريخ
                                                            </th>
                                                            <th>
                                                                رقم القيد
                                                            </th>
                                                            <th>
                                                                رقم الإذن
                                                            </th>
                                                            <th>
                                                                بيان
                                                            </th>
                                                            <th>
                                                                مدين
                                                            </th>
                                                            <th>
                                                                دائن
                                                            </th>
                                                            <th>
                                                                الرصيد
                                                            </th>
                                                        </tr>
                                                        <tr>
                                                            <th>
                                                                <?php
                                                                
                                                                $allcashfrom = 0;
                                                                $allcashto = 0;
                                                                $cashend = 0;
                                                                $cashfrom = 0;
                                                                $cashto = 0;
                                                                $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert < '$start_date_sql' ");
                                                                $sqlyomya->execute();
                                                                $yomyas = $sqlyomya->fetchAll();
                                                                foreach ($yomyas as $yomya) {
                                                                    $id_yomya = $yomya['id_yomya'];
                                                                    $dateyomya = $yomya['dateinsert'];
                                                                    $payan = $yomya['payan'];
                                                                    $registration_number = $yomya['registration_number'];
        
                                                                    $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND $name_lev_acc = '$code_account'");
                                                                    $sqldetails_yomya->execute();
                                                                    $details_yomyas = $sqldetails_yomya->fetchAll();
                                                                    $countfrom = $sqldetails_yomya->rowCount();
                                                                    $cashfrom = 0;
                                                                    $cashto = 0;
                                                                    foreach ($details_yomyas as $details_yomya) {
                                                                        $from_or_to = $details_yomya['from_or_to'];
                                                                        $cash = $details_yomya['cash'];
                                                                        $id_currency_codes = $details_yomya['id_currency_codes'];
                                                                        $amountSold = $details_yomya['amountSold'];
                                                                        $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                                                        if($id_currency_code != 1){
                                                                            if($id_currency_codes != 1){
                                                                                $cash = $amountSold;
                                                                            } else {
                                                                                $cash = $currencyExchangeRate * $cash;
                                                                            }
                                                                        }
                                                                        
                                                                        if ($from_or_to == 1) {
                                                                            
                                                                            $cashfrom = $cash;
                                                                            $allcashfrom += $cash;
                                                                            $cashend -= $cash;
                                                                        } elseif ($from_or_to == 2) {
                                                                            $cashto = $cash;
                                                                            $allcashto += $cash;
                                                                            $cashend += $cash;
                                                                        }
                                                                        ?>
                                                                        <?php
                                                                    }
                                                                }
                                                                ?>
                                                                
                                                            </th>
                                                            <th></th>
                                                            <th></th>
                                                            <th>
                                                                رصيد سابق
                                                            </th>
                                                            <th>
                                                                <?=$allcashfrom?>
                                                                <?=$currency_code?>
                                                            </th>
                                                            <th>
                                                            <?=$allcashto?>
                                                            <?=$currency_code?>
                                                            </th>
                                                            <th>
                                                            <?=$cashend?>
                                                            <?=$currency_code?>
                                                            </th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        // $allcashfrom = 0;
                                                        // $allcashto = 0;
                                                        // $cashend = 0;
                                                        $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $dateyomya = $yomya['dateinsert'];
                                                            $payan = $yomya['payan'];
                                                            $registration_number = $yomya['registration_number'];

                                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND $name_lev_acc = '$code_account'");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            $countfrom = $sqldetails_yomya->rowCount();
                                                            $cashfrom = 0;
                                                            $cashto = 0;
                                                            foreach ($details_yomyas as $details_yomya) {
                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                                $amountSold = $details_yomya['amountSold'];
                                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                                                if($id_currency_code != 1){
                                                                    if($id_currency_codes != 1){
                                                                        $cash = $amountSold;
                                                                    } else {
                                                                        $cash = $currencyExchangeRate * $cash;
                                                                    }
                                                                }
                                                                if ($from_or_to == 1) {
                                                                    
                                                                    $cashfrom = $cash;
                                                                    $allcashfrom += $cash;
                                                                    $cashend += $cash;
                                                                } elseif ($from_or_to == 2) {
                                                                    $cashto = $cash;
                                                                    $allcashto += $cash;
                                                                    $cashend -= $cash;
                                                                }
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                        <?php
                                                                        echo $dateyomya;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <a href="reports_account.php?Manage=tree&do=add_yomya&id=<?=$id_yomya;?>" target="_blank"> <?=$registration_number;?></a>
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        $codeizn = getId('sanadat','id_yomya',$id_yomya, 'ID');;
                                                                        echo $codeizn;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        echo $payan;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        echo $cashfrom . ' '. $currency_code;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        echo $cashto . ' '. $currency_code;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        echo $cashend . ' '.$currency_code;
                                                                        ?>
                                                                    </td>
                                                                </tr>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th>
                                                                
                                                            </th>
                                                            <th>
                                                                
                                                            </th>
                                                            <th>
                                                                الإجمالى
                                                            </th>
                                                            <td>
                                                            <?$allcashfrom?>
                                                            <?=$currency_code?>
                                                            </td>
                                                            <td>
                                                            <?$allcashto?>
                                                            <?=$currency_code?>
                                                            </td>
                                                            <td>
                                                            <?=$cashend?>
                                                            <?=$currency_code?>
                                                            </td>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            <?php
                            }
                            ?>
                        </div>
                    </section>
                <?php
                } else {
                ?>
                    <div class="alert alert-danger">
                        عذراً هناك خطاء.. برجاء إتبع رابط صحيح
                    </div>
                <?php
                }
            } elseif ($do == 'treasury') {
                $_POST['num_acount'] = '2.1.1';
                if (!empty($_POST['num_acount']) ) {
                    $id_ledger = $_POST['num_acount'];

                    if (isset($_POST['currency_code']) && !empty($_POST['currency_code'])){
                        $id_currency_code = $_POST['currency_code'];
                    } else {
                        $id_currency_code = 1;
                    }
                    ?>
                    <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=treasury" method="post">
                        <div class="row">
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="start_date" class="control-label">من تاريخ:</label>
                                    <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="end_date" class="control-label">الى تاريخ:</label>
                                    <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <!-- <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="end_date" class="control-label">رقم الحساب</label>
                                    <input type="text" name="num_acount" id="num_acount" class="form-control" value="<?=$id_ledger?>" placeholder="ادخل رقم الحساب" required>
                                </div>
                            </div> -->
                            <!-- <div class="form-group form-group-lg col-sm-2">
                                <div class="col-sm-12">
                                    <label for="currency_code" class="control-label">حدد عملة عرض الكشف</label>
                                    <select name="currency_code" class="form-select" required>
                                        <?php
                                        // Query to retrieve agents from the agents table
                                        $personsQuery = "SELECT * FROM currency where ID = 1 OR ID = 2";
                                        $personsResult = $con->query($personsQuery);
                                        while ($persons = $personsResult->fetch(PDO::FETCH_ASSOC)) {
                                            echo "<option value='{$persons['ID']}'>{$persons['currency_shortcut']}</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div> -->
                            <div class="form-group form-group-lg col-sm-1">
                                <div class="col-sm-12">
                                    <label></label>
                                    <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <section class="container-xxl">
                        <div class="panel panel-primary text-center table-responsive">
                            <?php
                            $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE lv = '$id_ledger' ");
                            $sqlaccounts->execute();
                            $accounts = $sqlaccounts->fetchAll();
                            foreach ($accounts as $account) {
                                $ID = $account['ID'];
                                $name_account = $account['name_account'];
                                $lev_acc = $account['lev_acc'];
                                $name_lev_acc = 'lv'.$lev_acc;
                                $code_account = $account['lv'];
                                $currency_code = getId('currency','ID',$id_currency_code, 'currency_code');
                                // $nature_acc = $account['nature_acc'];
                                ?>
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th colspan="2">
                                                كشف حركة  / <?php echo $name_account; ?>
                                            </th>
                                        </tr>
                                        <tr>
                                            <th  colspan="2" style="width: 50%;">
                                            عن الفترة من تاريخ  <?php echo $start_date . " الى تاريخ " . $end_date; ?>
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>المقبوضات</td>
                                            <td>المدفوعات</td>
                                        </tr>
                                        <tr class="success">
                                            <td>
                                                <table class="table table-bordered" style="direction: ltr;">
                                                    <thead >
                                                        <tr>
                                                            <th rowspan="2" style="max-width: 40px;">
                                                                NO.
                                                            </th>
                                                            <th rowspan="2" style="max-width: 40px;">
                                                                رقم القيد
                                                            </th>
                                                            <th rowspan="2">
                                                                تاريخ
                                                            </th>
                                                            <th rowspan="2">
                                                                بيان
                                                            </th>
                                                            <?php
                                                            $sqlaccountscurncy = getCurrency();
                                                            foreach ($sqlaccountscurncy as $curncy_name) {
                                                                $ID_Currency = $curncy_name['ID'];
                                                                $currency_shortcut = $curncy_name['currency_code'];
                                                                ?>
                                                                <th colspan="2">
                                                                <?='عملة '.$currency_shortcut?>
                                                                </th>
                                                                <?php
                                                            }
                                                            ?>
                                                        </tr>
                                                        <tr>
                                                        <?php
                                                            $arraycurncy_name = array();
                                                            foreach ($sqlaccountscurncy as $curncy_name) {
                                                                $arraycurncy_name[$curncy_name['ID']] = 0;
                                                                $ID_Currency = $curncy_name['ID'];
                                                                $currency_shortcut = $curncy_name['currency_shortcut'];
                                                                ?>
                                                                <th>
                                                                    <?php if ($ID_Currency == 1) {
                                                                        echo "جنيه";
                                                                    } else {
                                                                        echo $currency_shortcut;
                                                                    } ?>
                                                                </th>
                                                                <th>
                                                                <?php if ($ID_Currency == 1) {
                                                                        echo "قرش";
                                                                    } else {
                                                                        echo '';
                                                                    } ?>
                                                                </th>
                                                                <?php
                                                            }
                                                            ?>
                                                        </tr>
                                                    </thead>
                                                    <?php
                                                    // تجهيز مصفوفة لتخزين إجماليات كل عملة
                                                    $totalsPerCurrency = [];

                                                    $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN ? AND ?");
                                                    $sqlyomya->execute([$start_date_sql, $end_date_sql]);
                                                    $yomyas = $sqlyomya->fetchAll();

                                                    foreach ($yomyas as $yomya) {
                                                        $id_yomya = $yomya['id_yomya'];

                                                        $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = ? AND $name_lev_acc = ? AND from_or_to = 1");
                                                        $sqldetails_yomya->execute([$id_yomya, $code_account]);
                                                        $details_yomyas = $sqldetails_yomya->fetchAll();

                                                        foreach ($details_yomyas as $details_yomya) {
                                                            $id_currency_codes = $details_yomya['id_currency_codes'];
                                                            if ($id_currency_codes == 1) {
                                                                $amountSold = $details_yomya['cash'];
                                                            } else{
                                                                $amountSold = $details_yomya['amountSold'];
                                                            }
                                                            
                                                            // جمع القيم لكل عملة
                                                            if (!isset($totalsPerCurrency[$id_currency_codes])) {
                                                                $totalsPerCurrency[$id_currency_codes] = 0;
                                                            }
                                                            $totalsPerCurrency[$id_currency_codes] += $amountSold;
                                                        }
                                                    }
                                                    ?>
                                                    <tbody style="direction: rtl;">
                                                        <?php
                                                        // $allcashfrom = 0;
                                                        // $allcashto = 0;
                                                        // $cashend = 0;
                                                        /* $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll(); */
                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $dateyomya = $yomya['dateinsert'];
                                                            $payan = $yomya['payan'];
                                                            $registration_number = $yomya['registration_number'];

                                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND $name_lev_acc = '$code_account' AND from_or_to = 1");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            $countfrom = $sqldetails_yomya->rowCount();
                                                            $cashfrom = 0;
                                                            $cashto = 0;
                                                            foreach ($details_yomyas as $details_yomya) {

                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                                $amountSold = $details_yomya['amountSold'];
                                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                                                
                                                                if($id_currency_code == 1){
                                                                    if($id_currency_codes == 1){
                                                                        $amountSold = $cash;
                                                                        // $cash = $amountSold;
                                                                    } else {
                                                                        $cash = $amountSold;
                                                                    }
                                                                }
                                                                if ($from_or_to == 1) {
                                                                    $cashfrom = $cash;
                                                                    // $allcashfrom += $cash;
                                                                    // $cashend += $cash;
                                                                } elseif ($from_or_to == 2) {
                                                                    $cashto = $cash;
                                                                    // $allcashto += $cash;
                                                                    // $cashend -= $cash;
                                                                }
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                        <?php
                                                                        $codeizn = getId('sanadat','id_yomya',$id_yomya, 'id_sanadat');
                                                                        echo $codeizn;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <a href="reports_account.php?Manage=tree&do=add_yomya&id=<?=$registration_number;?>" target="_blank"> <?=$registration_number;?></a>
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        echo $dateyomya;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        echo $payan;
                                                                        ?>
                                                                    </td>
                                                                    <?php
                                                                    foreach ($sqlaccountscurncy as $curncy_name) {
                                                                        $ID_Currency = $curncy_name['ID'];
                                                                        $currency_shortcut = $curncy_name['currency_shortcut'];
                                                                        ?>
                                                                        <td>
                                                                        <?php
                                                                        if ($ID_Currency == $id_currency_codes) {
                                                                            $amountSold_cash = explode(".", $amountSold);
                                                                            echo $amountSold_cash[0];
                                                                        }
                                                                        ?>
                                                                        </td>
                                                                        <td>
                                                                        <?php
                                                                        if ($ID_Currency == $id_currency_codes) {
                                                                            if (isset($amountSold_cash[1]) && !empty($amountSold_cash[1])) {
                                                                                echo $amountSold_cash[1] ;
                                                                            }
                                                                        }
                                                                        ?>
                                                                        </td>
                                                                        <?php
                                                                    }
                                                                    ?>
                                                                </tr>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th colspan="4">إجمالي المقبوضات</th>
                                                            <?php foreach ($sqlaccountscurncy as $curncy_name): ?>
                                                                <?php
                                                                $ID_Currency = $curncy_name['ID'];
                                                                $total = $totalsPerCurrency[$ID_Currency] ?? 0;
                                                                $amountSold_cash = explode(".", (string)$total);
                                                                ?>
                                                                <th><?= $amountSold_cash[0] ?? '0' ?></th>
                                                                <th><?= $amountSold_cash[1] ?? '00' ?></th>
                                                            <?php endforeach; ?>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </td>
                                            <td>
                                            <table class="table table-bordered" style="direction: ltr;">
                                                    <thead >
                                                    <tr>
                                                        <th rowspan="2" style="max-width: 40px;">
                                                            NO.
                                                        </th>
                                                        <th rowspan="2" style="max-width: 40px;">
                                                            رقم القيد
                                                        </th>
                                                        <th rowspan="2">
                                                            تاريخ
                                                        </th>
                                                        <th rowspan="2">
                                                            بيان
                                                        </th>
                                                        <?php
                                                        $sqlaccountscurncy = getCurrency();
                                                        foreach ($sqlaccountscurncy as $curncy_name) {
                                                            $ID_Currency = $curncy_name['ID'];
                                                            $currency_shortcut = $curncy_name['currency_code'];
                                                            ?>
                                                            <th colspan="2">
                                                            <?='عملة '.$currency_shortcut?>
                                                            </th>
                                                            <?php
                                                        }
                                                        ?>
                                                    </tr>
                                                        <tr>
                                                        <?php
                                                            foreach ($sqlaccountscurncy as $curncy_name) {
                                                                $ID_Currency = $curncy_name['ID'];
                                                                $currency_shortcut = $curncy_name['currency_shortcut'];
                                                                ?>
                                                                <th>
                                                                    <?php if ($ID_Currency == 1) {
                                                                        echo "جنيه";
                                                                    } else {
                                                                        echo $currency_shortcut;
                                                                    } ?>
                                                                </th>
                                                                <th>
                                                                <?php if ($ID_Currency == 1) {
                                                                        echo "قرش";
                                                                    } else {
                                                                        echo '';
                                                                    } ?>
                                                                </th>
                                                                <?php
                                                            }
                                                            ?>
                                                        </tr>
                                                        
                                                    </thead>
                                                    <?php
                                                    // تجهيز مصفوفة لتخزين إجماليات كل عملة
                                                    $totalsPerCurrencyto = [];

                                                    $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN ? AND ?");
                                                    $sqlyomya->execute([$start_date_sql, $end_date_sql]);
                                                    $yomyas = $sqlyomya->fetchAll();

                                                    foreach ($yomyas as $yomya) {
                                                        $id_yomya = $yomya['id_yomya'];

                                                        $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = ? AND $name_lev_acc = ? AND from_or_to = 2");
                                                        $sqldetails_yomya->execute([$id_yomya, $code_account]);
                                                        $details_yomyas = $sqldetails_yomya->fetchAll();

                                                        foreach ($details_yomyas as $details_yomya) {
                                                            $id_currency_codes = $details_yomya['id_currency_codes'];
                                                            if ($id_currency_codes == 1) {
                                                                $amountSold = $details_yomya['cash'];
                                                            } else{
                                                                $amountSold = $details_yomya['amountSold'];
                                                            }
                                                            
                                                            // جمع القيم لكل عملة
                                                            if (!isset($totalsPerCurrencyto[$id_currency_codes])) {
                                                                $totalsPerCurrencyto[$id_currency_codes] = 0;
                                                            }
                                                            $totalsPerCurrencyto[$id_currency_codes] += $amountSold;
                                                        }
                                                    }
                                                    ?>
                                                    <tbody style="direction: rtl;">
                                                        <?php
                                                        // $allcashfrom = 0;
                                                        // $allcashto = 0;
                                                        // $cashend = 0;
                                                        /* $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll(); */
                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $dateyomya = $yomya['dateinsert'];
                                                            $payan = $yomya['payan'];
                                                            $registration_number = $yomya['registration_number'];

                                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND $name_lev_acc = '$code_account' AND from_or_to = 2");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            $countfrom = $sqldetails_yomya->rowCount();
                                                            $cashfrom = 0;
                                                            $cashto = 0;
                                                            foreach ($details_yomyas as $details_yomya) {

                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                                $amountSold = $details_yomya['amountSold'];
                                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                                                
                                                                if($id_currency_code == 1){
                                                                    if($id_currency_codes == 1){
                                                                        $amountSold = $cash;
                                                                        // $cash = $amountSold;
                                                                    } else {
                                                                        $cash = $amountSold;
                                                                    }
                                                                }
                                                                if ($from_or_to == 1) {
                                                                    
                                                                    $cashfrom = $cash;
                                                                    $allcashfrom += $cash;
                                                                    $cashend += $cash;
                                                                } elseif ($from_or_to == 2) {
                                                                    $cashto = $cash;
                                                                    // $allcashto += $cash;
                                                                    // $cashend -= $cash;
                                                                }
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                        <?php
                                                                        $codeizn = getId('sanadat','id_yomya',$id_yomya, 'id_sanadat');
                                                                        echo $codeizn;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <a tabindex="0" role="button" data-bs-toggle="popover" data-bs-trigger="focus"
                                                                        data-bs-title="قيد رقم" data-bs-content="And here's some amazing content. It's very engaging. Right?" 
                                                                        href="reports_account.php?Manage=tree&do=add_yomya&id=<?=$registration_number;?>&currency_code=<?=$id_currency_codes;?>" target="_blank">
                                                                        <?=$registration_number;?> </a>
                                                                        
                                                                        <!-- <button type="button" class="btn btn-primary" id="liveToastBtn<?=$id_yomya;?>">Show live toast</button>

                                                                        <div class="toast-container position-fixed bottom-0 end-0 p-3">
                                                                            <div id="liveToast<?=$id_yomya;?>" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
                                                                                <div class="toast-header">
                                                                                    <strong class="me-auto">قيد رقم <?=$registration_number;?></strong>
                                                                                     <small>11 mins ago</small> 
                                                                                    <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
                                                                                </div>

                                                                                <div class="toast-body">
                                                                                Hello, world! This is a toast message.
                                                                                </div>

                                                                            </div>
                                                                        </div> -->
                                                                        <!-- <script>
                                                                            $(document).ready(function () {
                                                                            const toastTrigger = document.getElementById('liveToastBtn<?=$id_yomya;?>')
                                                                            const toastLiveExample = document.getElementById('liveToast<?=$id_yomya;?>')

                                                                            if (toastTrigger) {
                                                                            const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample)
                                                                            toastTrigger.addEventListener('click', () => {
                                                                                toastBootstrap.show()
                                                                            })
                                                                            }
                                                                        });
                                                                        </script> -->
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        echo $dateyomya;
                                                                        ?>
                                                                    </td>
                                                                    <td>
                                                                        <?php
                                                                        echo $payan;
                                                                        ?>
                                                                    </td>
                                                                    <?php
                                                                    foreach ($sqlaccountscurncy as $curncy_name) {
                                                                        $ID_Currency = $curncy_name['ID'];
                                                                        $currency_shortcut = $curncy_name['currency_shortcut'];
                                                                        ?>
                                                                        <td>
                                                                        <?php
                                                                        if ($ID_Currency == $id_currency_codes) {
                                                                            $amountSold_cash = explode(".", $amountSold);
                                                                            echo $amountSold_cash[0];
                                                                        }
                                                                        ?>
                                                                        </td>
                                                                        <td>
                                                                        <?php
                                                                        if ($ID_Currency == $id_currency_codes) {
                                                                            if (isset($amountSold_cash[1]) && !empty($amountSold_cash[1])) {
                                                                                echo $amountSold_cash[1] ;
                                                                            }
                                                                        }
                                                                        ?>
                                                                        </td>
                                                                        <?php
                                                                    }
                                                                    ?>
                                                                </tr>
                                                                <?php
                                                            }
                                                        }
                                                        ?>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th colspan="4">إجمالي المدفوعات</th>
                                                            <?php foreach ($sqlaccountscurncy as $curncy_name): ?>
                                                                <?php
                                                                $ID_Currency = $curncy_name['ID'];
                                                                $total = $totalsPerCurrencyto[$ID_Currency] ?? 0;
                                                                $amountSold_cash = explode(".", (string)$total);
                                                                ?>
                                                                <th><?= $amountSold_cash[0] ?? '0' ?></th>
                                                                <th><?= $amountSold_cash[1] ?? '00' ?></th>
                                                            <?php endforeach; ?>
                                                        </tr>
                                                    </tfoot>
                                            </table>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <table class="table table-bordered" style="direction: ltr;">
                                                <tr>
                                                    <th  colspan="3" rowspan="2">
                                                        بيان
                                                    </th>
                                                    <?php
                                                    $sqlaccountscurncy = getCurrency();
                                                    foreach ($sqlaccountscurncy as $curncy_name) {
                                                        $ID_Currency = $curncy_name['ID'];
                                                        $currency_shortcut = $curncy_name['currency_code'];
                                                        ?>
                                                        <th colspan="2">
                                                        <?='عملة '.$currency_shortcut?>
                                                        </th>
                                                        <?php
                                                    }
                                                    ?>
                                                </tr>
                                                <tr>
                                                        <?php
                                                            $arraycurncy_name = array();
                                                            foreach ($sqlaccountscurncy as $curncy_name) {
                                                                $arraycurncy_name[$curncy_name['ID']] = 0;
                                                                $ID_Currency = $curncy_name['ID'];
                                                                $currency_shortcut = $curncy_name['currency_shortcut'];
                                                                ?>
                                                                <th>
                                                                    <?php if ($ID_Currency == 1) {
                                                                        echo "جنيه";
                                                                    } else {
                                                                        echo $currency_shortcut;
                                                                    } ?>
                                                                </th>
                                                                <th>
                                                                <?php if ($ID_Currency == 1) {
                                                                        echo "قرش";
                                                                    } else {
                                                                        echo '';
                                                                    } ?>
                                                                </th>
                                                                <?php
                                                            }
                                                            ?>
                                                        </tr>
                                                <tr>
                                                <?php
                                                // تجهيز مصفوفة لتخزين إجماليات كل عملة
                                                $totalsPerCurrencyold = [];
                                                $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert < ? ");
                                                $sqlyomya->execute([$start_date_sql]);
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $id_yomya = $yomya['id_yomya'];

                                                    $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = ? AND $name_lev_acc = ? AND from_or_to = 1");
                                                    $sqldetails_yomya->execute([$id_yomya, $code_account]);
                                                    $details_yomyas = $sqldetails_yomya->fetchAll();

                                                    foreach ($details_yomyas as $details_yomya) {
                                                        $id_currency_codes = $details_yomya['id_currency_codes'];
                                                        if ($id_currency_codes == 1) {
                                                            $amountSold = $details_yomya['cash'];
                                                        } else{
                                                            $amountSold = $details_yomya['amountSold'];
                                                        }
                                                        
                                                        // جمع القيم لكل عملة
                                                        if (!isset($totalsPerCurrencyold[$id_currency_codes])) {
                                                            $totalsPerCurrencyold[$id_currency_codes] = 0;
                                                        }
                                                        $totalsPerCurrencyold[$id_currency_codes] += $amountSold;
                                                    }
                                                }
                                                $totalsPerCurrencyoldto = [];
                                                $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert < ? ");
                                                $sqlyomya->execute([$start_date_sql]);
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $id_yomya = $yomya['id_yomya'];

                                                    $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = ? AND $name_lev_acc = ? AND from_or_to = 2");
                                                    $sqldetails_yomya->execute([$id_yomya, $code_account]);
                                                    $details_yomyas = $sqldetails_yomya->fetchAll();

                                                    foreach ($details_yomyas as $details_yomya) {
                                                        $id_currency_codes = $details_yomya['id_currency_codes'];
                                                        if ($id_currency_codes == 1) {
                                                            $amountSold = $details_yomya['cash'];
                                                        } else{
                                                            $amountSold = $details_yomya['amountSold'];
                                                        }
                                                        
                                                        // جمع القيم لكل عملة
                                                        if (!isset($totalsPerCurrencyoldto[$id_currency_codes])) {
                                                            $totalsPerCurrencyoldto[$id_currency_codes] = 0;
                                                        }
                                                        $totalsPerCurrencyoldto[$id_currency_codes] += $amountSold;
                                                    }
                                                }

                                                ?>
                                                <th colspan="3">رصيد سابق</th>
                                                <?php foreach ($sqlaccountscurncy as $curncy_name): ?>
                                                    <?php
                                                    $ID_Currency = $curncy_name['ID'];
                                                    $total = $totalsPerCurrencyold[$ID_Currency] ?? 0;
                                                    $totalto = $totalsPerCurrencyoldto[$ID_Currency] ?? 0;
                                                    $totalold = $total - $totalto;
                                                    $amountSold_cash = explode(".", (string)$totalold);
                                                    ?>
                                                    <th><?= $amountSold_cash[0] ?? '0' ?></th>
                                                    <th><?= $amountSold_cash[1] ?? '00' ?></th>
                                                <?php endforeach; ?>
                                            </tr>
                                            <tr>
                                                <th colspan="3">يضاف المقبوضات</th>
                                                <?php foreach ($sqlaccountscurncy as $curncy_name): ?>
                                                    <?php
                                                    $ID_Currency = $curncy_name['ID'];
                                                    $total = $totalsPerCurrency[$ID_Currency] ?? 0;
                                                    $amountSold_cash = explode(".", (string)$total);
                                                    ?>
                                                    <th><?= $amountSold_cash[0] ?? '0' ?></th>
                                                    <th><?= $amountSold_cash[1] ?? '00' ?></th>
                                                <?php endforeach; ?>
                                            </tr>
                                            <tr>
                                                <th colspan="3">يخصم إجمالى المدفوعات</th>
                                                <?php foreach ($sqlaccountscurncy as $curncy_name): ?>
                                                    <?php
                                                    $ID_Currency = $curncy_name['ID'];
                                                    $total = $totalsPerCurrencyto[$ID_Currency] ?? 0;
                                                    $amountSold_cash = explode(".", (string)$total);
                                                    ?>
                                                    <th><?= $amountSold_cash[0] ?? '0' ?></th>
                                                    <th><?= $amountSold_cash[1] ?? '00' ?></th>
                                                <?php endforeach; ?>
                                            </tr>
                                            <tr>
                                                <th colspan="3">الرصيد</th>
                                                <?php foreach ($sqlaccountscurncy as $curncy_name): ?>
                                                    <?php
                                                    $ID_Currency = $curncy_name['ID'];
                                                    $totalfromold = $totalsPerCurrencyold[$ID_Currency] ?? 0;
                                                    $totaltoold = $totalsPerCurrencyoldto[$ID_Currency] ?? 0;
                                                    $totalfrom = $totalsPerCurrency[$ID_Currency] ?? 0;
                                                    $totalto = $totalsPerCurrencyto[$ID_Currency] ?? 0;
                                                    $totalnow = ($totalfromold - $totaltoold) + $totalfrom - $totalto;
                                                    $amountSold_cash = explode(".", (string)$totalnow);
                                                    ?>
                                                    <th><?= $amountSold_cash[0] ?? '0' ?></th>
                                                    <th><?= $amountSold_cash[1] ?? '00' ?></th>
                                                <?php endforeach; ?>
                                            </tr>
                                                </table>
                                            </td>
                                            <td></td>
                                        </tr>
                                        
                                    </tbody>
                                </table>
                            <?php
                            }
                            ?>
                        </div>
                    </section>
                    <?php
                } else {
                ?>
                    <div class="alert alert-danger">
                        عذراً هناك خطاء.. برجاء إتبع رابط صحيح
                    </div>
                <?php
                }
            } elseif ($do == 'balance') {
                $data = array();
                $datahtml = '<section class="container">
                <div class="panel panel-primary text-center table-responsive">
                <div class="panel-heading">ميزان المراجعة بتاريخ '.$start_date . ' : ' . $end_date.'</div>
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th rowspan="2">
                                رقم الحساب
                            </th>
                            <th rowspan="2">
                                إسم الحساب
                            </th>
                            <th colspan="2">
                                أرصدة إفتتاحية
                            </th>
                            <th colspan="2">
                                أرصدة خلال الفترة
                            </th>
                            <th colspan="2">
                                أرصدة ختامية
                            </th>
                        </tr>
                        <tr>
                            <th>
                                مدين
                            </th>
                            <th>
                                دائن
                            </th>
                            <th>
                                مدين
                            </th>
                            <th>
                                دائن
                            </th>
                            <th>
                                مدين
                            </th>
                            <th>
                                دائن
                            </th>
                        </tr>
                    </thead>';
                    $data[] = ['رقم الحساب','إسم الحساب','أرصدة إفتتاحية', '', 'أرصدة خلال الفترة', '', 'أرصدة ختامية', ''];
                    $data[] = ['','','مدين', 'دائن', 'مدين', 'دائن', 'مدين', 'دائن'];
                    $nameReport = "ميزان المراجعة";
                ?>
                <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=balance" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                            </div>
                        </div>
                    </div>
                </form>
                <section class="container">
                    <div class="panel panel-primary text-center table-responsive">
                        <input class="form-control" id="myInput" type="text" placeholder="Search..">
                        <!-- Default panel contents -->
                        <div class="panel-heading">
                            <div class="row">
                                <div class="col-sm-9">
                                ميزان المراجعة بتاريخ <?php echo $start_date . " : " . $end_date; ?>
                                </div>
                                <div class="col-sm-1">
                                    <form class="form-inline" id="downloadFormExcel" action="generate_file.php" method="post" target="_blank">
                                        <input type="hidden" name="data" id="dataExcel">
                                        <input type="hidden" name="downloadType" value="excel">
                                        <input type="hidden" name="namereport" value="<?=$nameReport?>">
                                        <button type="submit" class="btn form-control generate">Excel <i class="fa-solid fa-download"></i></button>
                                    </form>
                                </div>
                                <div class="col-sm-1">
                                    <form class="form-inline" id="downloadFormPDF" action="generate_file.php" method="post" target="_blank">
                                        <input type="hidden" name="data" id="dataPDF">
                                        <input type="hidden" name="downloadType" value="pdf">
                                        <input type="hidden" name="namereport" value="<?=$nameReport?>">
                                        <button type="submit" class="btn form-control generate">PDF <i class="fa-solid fa-download"></i></button>
                                    </form>
                                </div>
                                <div class="col-sm-1">
                                    <form class="form-inline" id="downloadFormPDFP" action="generate_file.php" method="post" target="_blank">
                                        <input type="hidden" name="data" id="dataPDFP">
                                        <input type="hidden" name="downloadType" value="pdfP">
                                        <input type="hidden" name="namereport" value="<?=$nameReport?>">
                                        <button type="submit" class="btn form-control generate">Print <i class="fa-solid fa-print fa-xl"></i></button>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <!-- Table -->
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th rowspan="2">
                                        رقم الحساب
                                    </th>
                                    <th rowspan="2">
                                        إسم الحساب
                                        <a data-bs-toggle="collapse" href=".code_accountlv3" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3_<?php echo $IDlv3 ?>" title='عرض المستوى الرابع'>
                                            <i class='fa fa-caret-down' style="font-size:18px"></i>
                                        </a>
                                    </th>
                                    <th colspan="2">
                                        أرصدة إفتتاحية
                                    </th>
                                    <th colspan="2">
                                        أرصدة خلال الفترة
                                    </th>
                                    <th colspan="2">
                                        أرصدة ختامية
                                    </th>
                                </tr>
                                <tr>
                                    <th>
                                        مدين
                                    </th>
                                    <th>
                                        دائن
                                    </th>
                                    <th>
                                        مدين
                                    </th>
                                    <th>
                                        دائن
                                    </th>
                                    <th>
                                        مدين
                                    </th>
                                    <th>
                                        دائن
                                    </th>
                                </tr>
                            </thead>
                            <tbody id="myTable">
                                <?php
                                $cashallfrom1 = 0;
                                $cashallto1 = 0;
                                $cashallfrom2 = 0;
                                $cashallto2 = 0;
                                $cashallfrom3 = 0;
                                $cashallto3 = 0;
                                $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE lev_acc = 1");
                                $sqlaccounts->execute();
                                $accounts = $sqlaccounts->fetchAll();
                                foreach ($accounts as $account) {
                                    $IDlv1 = $account['ID'];
                                    $name_accountlv1 = $account['name_account'];
                                    $code_accountlv1 = $account['lv1'];
                                    $nature_acclv1 = $account['nature_account'];
                                    ?>
                                    <tr class="success">
                                        <td>
                                            <?php
                                            echo $code_accountlv1;
                                            ?>
                                        </td>
                                        <td style="text-align: right;">
                                            <?php
                                            echo $name_accountlv1;
                                            ?>
                                            <a data-bs-toggle="collapse" href=".code_accountlv3_<?php echo $IDlv1 ?>" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3_<?php echo $IDlv1 ?>" title='عرض المستوى الثانى'>
                                                <i class='fa fa-caret-down' style="font-size:18px"></i>
                                            </a>
                                        </td>
                                        <td>
                                            <?php
                                            $cashfrom1 = 0;
                                            $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert < '$start_date_sql' AND lv1 = '$code_accountlv1' AND from_or_to = 1 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $cashfrom1 += $yomya['cash'];
                                                // $cashallfrom1 += $cashfrom1;
                                            }
                                            $cashto1 = 0;
                                            $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert < '$start_date_sql' AND lv1 = '$code_accountlv1' AND from_or_to = 2 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $cashto1 += $yomya['cash'];
                                            }
                                            $cashfrom3old = $cashfrom1;
                                            $cashto3old = $cashto1;
                                            $cashendfromold = $cashfrom3old - $cashto3old;
                                            $cashendtoold = $cashto3old - $cashfrom3old;
                                            if ($cashendfromold > 0) {
                                                echo $cashendfromold;
                                                $cashendfromold_exc = $cashendfromold;
                                                $cashallfrom1 += $cashendfromold;
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            if ($cashendtoold > 0) {
                                                echo $cashendtoold;
                                                $cashendtoold_exc = $cashendtoold;
                                                $cashallto1 += $cashendtoold;
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $cashfrom2 = 0;
                                            $sqlyomya = $con->prepare("SELECT * from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv1 = '$code_accountlv1' AND from_or_to = 1 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $cashfrom2 += $yomya['cash'];
                                            }
                                            $cashallfrom2 += $cashfrom2;
                                            echo $cashfrom2;
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $cashto2 = 0;
                                            $sqlyomya = $con->prepare("SELECT * from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv1 = '$code_accountlv1' AND from_or_to = 2 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $cashto2 += $yomya['cash'];
                                            }
                                            $cashallto2 += $cashto2;
                                            echo $cashto2;
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $cashprintt= 0;
                                            $cashprintf= 0;
                                            $cashfrom3 = $cashfrom1 + $cashfrom2;
                                            $cashto3 = $cashto1 + $cashto2;
                                            $cashendfrom = $cashfrom3 - $cashto3;
                                            $cashendto = $cashto3 - $cashfrom3;
                                            if ($cashendfrom > 0) {
                                                echo $cashendfrom;
                                                $cashallfrom3 += $cashendfrom;
                                                $cashprintf = $cashendfrom;
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            if ($cashendto > 0) {
                                                echo $cashendto;
                                                $cashallto3 += $cashendto;
                                                $cashprintt = $cashendto;
                                            }
                                            $data[] = [$code_accountlv1,$name_accountlv1,$cashendfromold, $cashendtoold, $cashfrom2, $cashto2, $cashprintf, $cashprintt];
                                            $datahtml .= '<tr class="">
                                            <td style="text-align: right; font-weight: 700;">' . $code_accountlv1.'</td>
                                            <td style="text-align: right; font-weight: 700;">' . $name_accountlv1.'</td>
                                            <td style="text-align: right; font-weight: 700;">' . $cashendfromold.'</td>
                                            <td style="text-align: right; font-weight: 700;">' . $cashendtoold.'</td>
                                            <td style="text-align: right; font-weight: 700;">' . $cashfrom2.'</td>
                                            <td style="text-align: right; font-weight: 700;">' . $cashto2.'</td>
                                            <td style="text-align: right; font-weight: 700;">' . $cashprintf.'</td>
                                            <td style="text-align: right; font-weight: 700;">' . $cashprintt.'</td>
                                            </tr>';
                                        ?>
                                        </td>
                                    </tr>
                                    <?php
                                    $sqlaccounts = $con->prepare("SELECT * from level_acc where lv1 = '$code_accountlv1' AND lev_acc = 2");
                                    $sqlaccounts->execute();
                                    $accounts = $sqlaccounts->fetchAll();
                                    foreach ($accounts as $account) {
                                        $IDlv2 = $account['ID'];
                                        $name_accountlv2 = $account['name_account'];
                                        $code_accountlv2 = $account['lv2'];
                                        ?>
                                        <tr class="success">
                                            <td>
                                                <?php
                                                echo $code_accountlv2;
                                                ?>
                                            </td>
                                            <td style="text-align: right;">
                                                <?php
                                                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv2;
                                                ?>
                                                <a data-bs-toggle="collapse" href=".code_accountlv3_<?php echo $IDlv2 ?>" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3_<?php echo $IDlv2 ?>" title='عرض المستوى الثانى'>
                                                    <i class='fa fa-caret-down' style="font-size:18px"></i>
                                                </a>
                                            </td>
                                            <td>
                                                <?php
                                                $cashfrom1 = 0;
                                                $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert < '$start_date_sql' AND lv2 = '$code_accountlv2' AND from_or_to = 1 ");
                                                $sqlyomya->execute();
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $cashfrom1 += $yomya['cash'];
                                                    // $cashallfrom1 += $cashfrom1;
                                                }
                                                $cashto1 = 0;
                                                $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert < '$start_date_sql' AND lv2 = '$code_accountlv2' AND from_or_to = 2 ");
                                                $sqlyomya->execute();
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $cashto1 += $yomya['cash'];
                                                }
                                                $cashfrom3old = $cashfrom1;
                                                $cashto3old = $cashto1;
                                                $cashendfromold = $cashfrom3old - $cashto3old;
                                                $cashendtoold = $cashto3old - $cashfrom3old;
                                                if ($cashendfromold > 0) {
                                                    echo $cashendfromold;
                                                    $cashendfromold_exc = $cashendfromold;
                                                    // $cashallfrom1 += $cashendfromold;
                                                }
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                if ($cashendtoold > 0) {
                                                    echo $cashendtoold;
                                                    $cashendtoold_exc = $cashendtoold;
                                                    // $cashallto1 += $cashendtoold;
                                                }
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                $cashfrom2 = 0;
                                                $sqlyomya = $con->prepare("SELECT * from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv2 = '$code_accountlv2' AND from_or_to = 1 ");
                                                $sqlyomya->execute();
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $cashfrom2 += $yomya['cash'];
                                                }
                                                // $cashallfrom2 += $cashfrom2;
                                                echo $cashfrom2;
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                $cashto2 = 0;
                                                $sqlyomya = $con->prepare("SELECT * from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv2 = '$code_accountlv2' AND from_or_to = 2 ");
                                                $sqlyomya->execute();
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $cashto2 += $yomya['cash'];
                                                }
                                                // $cashallto2 += $cashto2;
                                                echo $cashto2;
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                $cashprintt= 0;
                                                $cashprintf= 0;
                                                $cashfrom3 = $cashfrom1 + $cashfrom2;
                                                $cashto3 = $cashto1 + $cashto2;
                                                $cashendfrom = $cashfrom3 - $cashto3;
                                                $cashendto = $cashto3 - $cashfrom3;
                                                if ($cashendfrom > 0) {
                                                    echo $cashendfrom;
                                                    // $cashallfrom3 += $cashendfrom;
                                                    $cashprintf = $cashendfrom;
                                                }
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                if ($cashendto > 0) {
                                                    echo $cashendto;
                                                    // $cashallto3 += $cashendto;
                                                    $cashprintt = $cashendto;
                                                }
                                                $data[] = [$code_accountlv2,$name_accountlv2,$cashendfromold, $cashendtoold, $cashfrom2, $cashto2, $cashprintf, $cashprintt];
                                                $datahtml .= '<tr class="">
                                                <td style="text-align: right; font-weight: 700;">' . $code_accountlv2.'</td>
                                                <td style="text-align: right; font-weight: 700;">' . $name_accountlv2.'</td>
                                                <td style="text-align: right; font-weight: 700;">' . $cashendfromold.'</td>
                                                <td style="text-align: right; font-weight: 700;">' . $cashendtoold.'</td>
                                                <td style="text-align: right; font-weight: 700;">' . $cashfrom2.'</td>
                                                <td style="text-align: right; font-weight: 700;">' . $cashto2.'</td>
                                                <td style="text-align: right; font-weight: 700;">' . $cashprintf.'</td>
                                                <td style="text-align: right; font-weight: 700;">' . $cashprintt.'</td>
                                                </tr>';
                                            ?>
                                            </td>
                                        </tr>
                                        <?php
                                        $sqlaccounts = $con->prepare("SELECT * from level_acc where lv2 = '$code_accountlv2' AND lev_acc = 3 ");
                                        $sqlaccounts->execute();
                                        $accounts = $sqlaccounts->fetchAll();
                                        foreach ($accounts as $account) {
                                            $IDlv3 = $account['ID'];
                                            $name_accountlv3 = $account['name_account'];
                                            $code_accountlv3 = $account['lv3'];
                                            ?>
                                            <tr class="success">
                                                <td>
                                                    <?php
                                                    echo $code_accountlv3;
                                                    ?>
                                                </td>
                                                <td style="text-align: right;">
                                                    <?php
                                                    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv3;
                                                    ?>
                                                    <a data-bs-toggle="collapse" href=".code_accountlv3_<?php echo $IDlv3 ?>" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3_<?php echo $IDlv3 ?>" title='عرض المستوى الرابع'>
                                                        <i class='fa fa-caret-down' style="font-size:18px"></i>
                                                    </a>
                                                </td>
                                                <td>
                                                    <?php
                                                    $cashfrom1 = 0;
                                                    $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert < '$start_date_sql' AND lv3 = '$code_accountlv3' AND from_or_to = 1 ");
                                                    $sqlyomya->execute();
                                                    $yomyas = $sqlyomya->fetchAll();
                                                    foreach ($yomyas as $yomya) {
                                                        $cashfrom1 += $yomya['cash'];
                                                        // $cashallfrom1 += $cashfrom1;
                                                    }
                                                    $cashto1 = 0;
                                                    $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert < '$start_date_sql' AND lv3 = '$code_accountlv3' AND from_or_to = 2 ");
                                                    $sqlyomya->execute();
                                                    $yomyas = $sqlyomya->fetchAll();
                                                    foreach ($yomyas as $yomya) {
                                                        $cashto1 += $yomya['cash'];
                                                    }
                                                    $cashfrom3old = $cashfrom1;
                                                    $cashto3old = $cashto1;
                                                    $cashendfromold = $cashfrom3old - $cashto3old;
                                                    $cashendtoold = $cashto3old - $cashfrom3old;
                                                    if ($cashendfromold > 0) {
                                                        echo $cashendfromold;
                                                        $cashendfromold_exc = $cashendfromold;
                                                        // $cashallfrom1 += $cashendfromold;
                                                    }
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    if ($cashendtoold > 0) {
                                                        echo $cashendtoold;
                                                        $cashendtoold_exc = $cashendtoold;
                                                        // $cashallto1 += $cashendtoold;
                                                    }
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    $cashfrom2 = 0;
                                                    $sqlyomya = $con->prepare("SELECT * from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv3 = '$code_accountlv3' AND from_or_to = 1 ");
                                                    $sqlyomya->execute();
                                                    $yomyas = $sqlyomya->fetchAll();
                                                    foreach ($yomyas as $yomya) {
                                                        $cashfrom2 += $yomya['cash'];
                                                    }
                                                    // $cashallfrom2 += $cashfrom2;
                                                    echo $cashfrom2;
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    $cashto2 = 0;
                                                    $sqlyomya = $con->prepare("SELECT * from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv3 = '$code_accountlv3' AND from_or_to = 2 ");
                                                    $sqlyomya->execute();
                                                    $yomyas = $sqlyomya->fetchAll();
                                                    foreach ($yomyas as $yomya) {
                                                        $cashto2 += $yomya['cash'];
                                                    }
                                                    // $cashallto2 += $cashto2;
                                                    echo $cashto2;
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    $cashprintt= 0;
                                                    $cashprintf= 0;
                                                    $cashfrom3 = $cashfrom1 + $cashfrom2;
                                                    $cashto3 = $cashto1 + $cashto2;
                                                    $cashendfrom = $cashfrom3 - $cashto3;
                                                    $cashendto = $cashto3 - $cashfrom3;
                                                    if ($cashendfrom > 0) {
                                                        echo $cashendfrom;
                                                        // $cashallfrom3 += $cashendfrom;
                                                        $cashprintf = $cashendfrom;
                                                    }
                                                    ?>
                                                </td>
                                                <td>
                                                    <?php
                                                    if ($cashendto > 0) {
                                                        echo $cashendto;
                                                        // $cashallto3 += $cashendto;
                                                        $cashprintt = $cashendto;
                                                    }
                                                    $data[] = [$code_accountlv3,$name_accountlv3,$cashendfromold, $cashendtoold, $cashfrom2, $cashto2, $cashprintf, $cashprintt];
                                                    $datahtml .= '<tr class="">
                                                    <td style="text-align: right; font-weight: 700;">' . $code_accountlv3.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $name_accountlv3.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashendfromold.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashendtoold.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashfrom2.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashto2.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashprintf.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashprintt.'</td>
                                                    </tr>';
                                                ?>
                                                </td>
                                            </tr>
                                            <?php
                                            $sqlaccounts = $con->prepare("SELECT * from level_acc where lv3 = '$code_accountlv3' AND lev_acc = 4 ");
                                            $sqlaccounts->execute();
                                            $accounts = $sqlaccounts->fetchAll();
                                            foreach ($accounts as $account) {
                                                $IDlv4 = $account['ID'];
                                                $name_accountlv4 = $account['name_account'];
                                                $code_accountlv4 = $account['lv4'];
                                                $cashendfromold_exc = 0;
                                                $cashendtoold_exc = 0;
                                                $cashendfrom_exc = 0;
                                                $cashendto_exc = 0;
                                                ?>
                                                <tr id="code_accountlv3_<?php echo $IDlv3 ?>" class="code_accountlv3_<?php echo $IDlv3 ?> code_accountlv3 table-danger collapse" aria-expanded="false">
                                                    <td>
                                                        <?php
                                                        echo $code_accountlv4;
                                                        ?>
                                                    </td>
                                                    <td style="text-align: right;">
                                                        <?php
                                                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv4;
                                                        ?>
                                                    </td>
                                                    <td>
                                                        <?php
                                                        $cashfrom1 = 0;
                                                        $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert < '$start_date_sql' AND lv4 = '$code_accountlv4' AND from_or_to = 1 ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $cashfrom1 += $yomya['cash'];
                                                        }
                                                        $cashto1 = 0;
                                                        $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert < '$start_date_sql' AND lv4 = '$code_accountlv4' AND from_or_to = 2 ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $cashto1 += $yomya['cash'];
                                                        }
                                                        $cashfrom3old = $cashfrom1;
                                                        $cashto3old = $cashto1;
                                                        $cashendfromold = $cashfrom3old - $cashto3old;
                                                        $cashendtoold = $cashto3old - $cashfrom3old;
                                                        if ($cashendfromold > 0) {
                                                            echo $cashendfromold;
                                                            $cashendfromold_exc = $cashendfromold;
                                                        }
                                                        ?>
                                                    </td>
                                                    <td>
                                                        <?php
                                                        if ($cashendtoold > 0) {
                                                            echo $cashendtoold;
                                                            $cashendtoold_exc = $cashendtoold;
                                                            // $cashallto1 += $cashendtoold;
                                                        }
                                                        /* $cashallto1 += $cashto1;
                                                                                    echo $cashto1; */
                                                        ?>
                                                    </td>
                                                    <td>
                                                        <?php
                                                        $cashfrom2 = 0;
                                                        $sqlyomya = $con->prepare("SELECT * from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv4 = '$code_accountlv4' AND from_or_to = 1 ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            //echo $yomya['cash'] ;
                                                            $cashfrom2 += $yomya['cash'];
                                                        }
                                                        // $cashallfrom2 += $cashfrom2;
                                                        echo $cashfrom2;
                                                        ?>
                                                    </td>
                                                    <td>
                                                        <?php
                                                        $cashto2 = 0;
                                                        $sqlyomya = $con->prepare("SELECT * from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv4 = '$code_accountlv4' AND from_or_to = 2 ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $cashto2 += $yomya['cash'];
                                                        }
                                                        // $cashallto2 += $cashto2;
                                                        echo $cashto2;
                                                        ?>
                                                    </td>
                                                    <td>
                                                        <?php
                                                        $cashfrom3 = $cashfrom1 + $cashfrom2;
                                                        $cashto3 = $cashto1 + $cashto2;
                                                        $cashendfrom = $cashfrom3 - $cashto3;
                                                        $cashendto = $cashto3 - $cashfrom3;
                                                        if ($cashendfrom > 0) {
                                                            echo $cashendfrom;
                                                            $cashendfrom_exc = $cashendfrom;
                                                            // $cashallfrom3 += $cashendfrom;
                                                        }
                                                        ?>
                                                    </td>
                                                    <td>
                                                        <?php
                                                        if ($cashendto > 0) {
                                                            echo $cashendto;
                                                            $cashendto_exc = $cashendto;
                                                            // $cashallto3 += $cashendto;
                                                        }
                                                        // $data[] = [$code_accountlv4,$name_accountlv4,$cashendfromold, $cashendtoold, $cashfrom2, $cashto2, $cashendfrom, $cashendto];
                                                        ?>
                                                    </td>
                                                </tr>
                                <?php
                                            }
                                        }
                                    }
                                }
                                ?>
                            </tbody>
                            <tfoot>
                                <tr class="success">
                                    <td>

                                    </td>
                                    <th>

                                    </th>
                                    <!-- start رصيد افتتاحى -->
                                    <td>
                                        <?php
                                        echo $cashallfrom1
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                        echo $cashallto1
                                        ?>
                                    </td>
                                    <!-- end رصيد افتتاحى -->
                                    <!-- start رصيد خلال المدة -->
                                    <td>
                                        <?php
                                        echo $cashallfrom2
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                        echo $cashallto2
                                        ?>
                                    </td>
                                    <!-- end رصيد خلال المدة -->
                                    <!-- start رصيد ختامى -->
                                    <td>
                                        <?php
                                        echo $cashallfrom3
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                        echo $cashallto3;
                                        $data[] = ['','',$cashallfrom1, $cashallto1, $cashallfrom2, $cashallto2, $cashallfrom3, $cashallto3];
                                        $datahtml .= '<tr class="">
                                                    <td style="text-align: right; font-weight: 700;"></td>
                                                    <td style="text-align: right; font-weight: 700;"></td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashallfrom1.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashallto1.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashallfrom2.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashallto2.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashallfrom3.'</td>
                                                    <td style="text-align: right; font-weight: 700;">' . $cashallto3.'</td>
                                                    </tr></table></div></section>';
                                        ?>
                                    </td>
                                    <!-- end رصيد ختامى -->

                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    <script>
                        <?php
                        $dataString = json_encode($data);
                        ?>
                        $(document).ready(function () {
                            $("#downloadFormExcel").submit(function (event) {
                                event.preventDefault();
                                var data = <?php echo $dataString; ?>;
                                var dataString = JSON.stringify(data);
                                $("#dataExcel").val(dataString);
                                $("#successMessage").show();
                                this.submit();
                            });
                            var datahtml = <?php echo json_encode($datahtml); ?>;
                            $("#downloadFormPDF").submit(function (event) {
                                event.preventDefault();
                                var dataString = JSON.stringify(datahtml);
                                console.log(dataString);
                                $("#dataPDF").val(dataString);
                                $("#successMessage").show();
                                this.submit();
                            });
                            $("#downloadFormPDFP").submit(function (event) {
                                event.preventDefault();
                                var dataString = JSON.stringify(datahtml);
                                console.log(dataString);
                                $("#dataPDFP").val(dataString);
                                $("#successMessage").show();
                                this.submit();
                            });
                        });
                    </script>
                </section>
                <script>
                    $(document).ready(function() {
                        $("#myInput").on("keyup", function() {
                            var value = $(this).val().toLowerCase();
                            $("#myTable tr").filter(function() {
                                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
                            });
                        });
                    });
                </script>
                <?php

            } elseif ($do == 'ditels_earn') {
                // $spreadsheet = new Spreadsheet();
                $data = array();
                $datahtml = '<section class="container">
                <div class="panel panel-primary text-center table-responsive table_width">
                <div class="panel-heading">قائمة الدخل خلال الفترة من '.$start_date . ' : ' . $end_date.'</div>
                <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>
                            البيان
                        </th>
                        <th>
                            القيمة
                        </th>
                    </tr>
                </thead>';
                $nameReport = "قائمة الدخل";
                ?>
                <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=ditels_earn" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                            </div>
                        </div>
                    </div>
                </form>
                <br>
                
                <section class="container">
                    <!-- <a href="#"><i class="fa fa-print"></i></a> -->
                    <div class="panel panel-primary text-center table-responsive table_width">
                        <!-- Default panel contents -->
                        <div class="panel-heading">
                            <div class="row">
                                <div class="col-sm-9">
                                        قائمة الدخل خلال الفترة من <?php echo $start_date . " : " . $end_date; ?>
                                </div>
                                <div class="col-sm-1">
                                    <form class="form-inline" id="downloadFormExcel" action="generate_file.php" method="post" target="_blank">
                                        <input type="hidden" name="data" id="dataExcel">
                                        <input type="hidden" name="downloadType" value="excel">
                                        <input type="hidden" name="namereport" value="<?=$nameReport?>">
                                        <button type="submit" class="btn form-control generate">Excel <i class="fa-solid fa-download"></i></i></button>
                                    </form>
                                </div>
                                <div class="col-sm-1">
                                    <form class="form-inline" id="downloadFormPDF" action="generate_file.php" method="post" target="_blank">
                                        <input type="hidden" name="data" id="dataPDF">
                                        <input type="hidden" name="downloadType" value="pdf">
                                        <input type="hidden" name="namereport" value="<?=$nameReport?>">
                                        <button type="submit" class="btn form-control generate">PDF <i class="fa-solid fa-download"></i></i></button>
                                    </form>
                                </div>
                                <div class="col-sm-1">
                                    <form class="form-inline" id="downloadFormPDFP" action="generate_file.php" method="post" target="_blank">
                                        <input type="hidden" name="data" id="dataPDFP">
                                        <input type="hidden" name="downloadType" value="pdfP">
                                        <input type="hidden" name="namereport" value="<?=$nameReport?>">
                                        <button type="submit" class="btn form-control generate">Print<i class="fa-solid fa-print fa-xl"></i></button>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <!-- Table -->
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>
                                        البيان
                                    </th>
                                    <th>
                                        القيمة
                                    </th>
                                </tr>
                            </thead>
                                <?php 
                                $data[] = ['حساب رئيسي','حساب فرعى','حساب تحليلى', 'القيمة للرئيسي', 'القيمة للفرعى', 'القيمة للتحليلي'];
                                ?>
                            <tbody>
                                <?php
                                /* $AllSumcashold = 0;
                                            $AllSumcashend = 0;
                                            $cashfrom1 = 0; */
                                /* $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date' AND '$end_date' AND lv3 = 4301 AND from_or_to = 1 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $AllSumcashbuy = $yomya['cash']; # قيمة إجمالى المشتريات خلال الفترة
                                            } */
                                /* $sqlStarting = $con->prepare("SELECT sum(Quantity) AS K_G
                                                                    from data_processing 
                                                                    where id_operation_type = 7 ");
                                            $sqlStarting->execute();
                                            $starting = $sqlStarting->fetchAll();
                                            foreach ($starting as $start) {
                                                $startkg = $start['K_G']; # كمية البضاعة فى بداية البرنامج
                                            } */

                                /* $sqlkg = $con->prepare("SELECT sum(Quantity) AS 'sumkgb' FROM data_processing where id_operation_type = 1 ");
                                            $sqlkg->execute();
                                            $sumKgBs = $sqlkg->fetchAll();
                                            foreach ($sumKgBs as $sumkgb) {
                                                $SumKgBuyall = $sumkgb['sumkgb']; # إجمالى كمية البضاعة المشتراه
                                            } */

                                /* $sqlkg = $con->prepare("SELECT sum(Quantity) AS 'sumkgb' FROM data_processing where id_operation_type = 2 
                                                AND DateInsert < '$start_date_sql' ");
                                            $sqlkg->execute();
                                            $sumkgbs = $sqlkg->fetchAll();
                                            foreach ($sumkgbs as $sumkgb) {
                                                $SumKgSaleold = $sumkgb['sumkgb']; # كمية البضاعة المباعة قبل الفترة المحاسبية
                                            } */

                                /* $sqlkg = $con->prepare("SELECT sum(Quantity) AS 'sumkg' FROM data_processing where id_operation_type = 2 ");
                                            $sqlkg->execute();
                                            $sumkgs = $sqlkg->fetchAll();
                                            foreach ($sumkgs as $sumkg) {
                                                $SumKgSaleall = $sumkg['sumkg']; # إجمالى كمية البضاعة المباعة
                                            } */
                                /* $kgallstart = $startkg + $SumKgBuyall - $SumKgSaleold; # إجمالى البضاعة المتاحة للبيع
                                            $kgallend = $startkg + $SumKgBuyall - $SumKgSaleall; # إجمالى كمية مخزون أخر المدة
                                            if ($kgallstart != 0) {
                                                $AllSumcashend = (($AllSumcashold + $AllSumcashbuy) / $kgallstart) * $kgallend; # قيمة مخزون أخر المدة
                                            } else {
                                                $AllSumcashend = $AllSumcashold + $AllSumcashbuy;
                                            } */
                                // $AllSumcashend = ceil($AllSumcashend);
                                $cashall1 = 0;
                                $sqlaccounts = $con->prepare("SELECT * from level_acc where lev_acc = 1 AND list_account = 2 ORDER BY ID DESC");
                                $sqlaccounts->execute();
                                $accounts = $sqlaccounts->fetchAll();
                                foreach ($accounts as $account) {
                                    $IDlv1 = $account['ID'];
                                    $name_accountlv1 = $account['name_account'];
                                    $code_accountlv1 = $account['lv1'];
                                    $nature_acclv1 = $account['nature_account'];
                                    if ($code_accountlv1 == 5) {
                                        $class = "success";
                                    } elseif ($code_accountlv1 == 4) {
                                        $class = "danger";
                                    }
                                ?>
                                    <tr class="<?php echo $class; ?>">
                                        <td style="text-align: right; font-size: 18px; font-weight: 700;">
                                            <?php echo "&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv1; ?>
                                        </td>
                                        <td style="text-align: right; font-size: 20px; font-weight: 700;">
                                            <?php
                                            
                                            $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv1 = '$code_accountlv1' AND from_or_to = 1 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $cashfrom1 = $yomya['cash'];
                                                //$cashallfrom1 += $cashfrom1;
                                            }
                                            $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv1 = '$code_accountlv1' AND from_or_to = 2 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $cashto1 = $yomya['cash'];
                                                //$cashallto1 += $cashto1;
                                            }
                                            $cashend1 = $cashto1 - $cashfrom1;
                                            $cashall1 += $cashend1;
                                            echo "&nbsp;&nbsp;&nbsp;&nbsp;" . $cashend1;

                                            $data[] = [$name_accountlv1,'','', $cashend1, '', ''];
                                            ?>
                                        </td>
                                    </tr>
                                    <?php
                                    $datahtml .= '<tr class="'.$class.'"><td style="text-align: right; font-weight: 700;">
                                    &nbsp;&nbsp;&nbsp;&nbsp;' . $name_accountlv1.'</td>
                                    <td style="text-align: right; font-weight: 700;">
                                    &nbsp;&nbsp;&nbsp;&nbsp;' . $cashend1.'</td></tr>';
                                    $sqlaccounts = $con->prepare("SELECT * from level_acc where lev_acc = 2 AND lv1 = '$code_accountlv1' ");
                                    $sqlaccounts->execute();
                                    $accounts = $sqlaccounts->fetchAll();
                                    foreach ($accounts as $account) {
                                        $IDlv2 = $account['ID'];
                                        $name_accountlv2 = $account['name_account'];
                                        $code_accountlv2 = $account['lv2'];
                                    ?>
                                        <tr class="<?php echo $class; ?>">
                                            <td style="text-align: right; font-size: 18px; font-weight: 700;">
                                                <?php echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv2; ?>
                                            </td>
                                            <td style="text-align: right; font-size: 18px; font-weight: 700;">
                                                <?php
                                                $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv2 = '$code_accountlv2' AND from_or_to = 1 ");
                                                $sqlyomya->execute();
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $cashfrom2 = $yomya['cash'];
                                                    //$cashallfrom1 += $cashfrom1;
                                                }
                                                $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv2 = '$code_accountlv2' AND from_or_to = 2 ");
                                                $sqlyomya->execute();
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $cashto2 = $yomya['cash'];
                                                    //$cashallto1 += $cashto1;
                                                }
                                                $cashend2 = $cashto2 - $cashfrom2;
                                                /* if ($code_accountlv2 == 43) {
                                                                    $cashend2 = $cashend2 + $AllSumcashend - $AllSumcashold;
                                                                } */
                                                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $cashend2;
                                                $data[] = ['',$name_accountlv2,'', '', $cashend2, ''];
                                                ?>
                                            </td>
                                        </tr>
                                        <?php
                                        $datahtml .= '<tr class="'.$class.'"><td style="text-align: right; font-weight: 700;">
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $name_accountlv2.'</td>
                                        <td style="text-align: right; font-weight: 700;">
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $cashend2.'</td></tr>';
                                        $sqlaccounts = $con->prepare("SELECT * from level_acc where lev_acc = 3 AND lv2 = '$code_accountlv2' ");
                                        $sqlaccounts->execute();
                                        $accounts = $sqlaccounts->fetchAll();
                                        foreach ($accounts as $account) {
                                            $IDlv3 = $account['ID'];
                                            $name_accountlv3 = $account['name_account'];
                                            $code_accountlv3 = $account['lv3'];
                                        ?>
                                            <tr class="<?php echo $class; ?>">
                                                <td style="text-align: right; font-size: 18px; font-weight: 700;">
                                                    <?php echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv3; ?>
                                                </td>
                                                <td style="text-align: right; font-size: 16px; font-weight: 700;">
                                                    <?php
                                                    $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv3 = '$code_accountlv3' AND from_or_to = 1 ");
                                                    $sqlyomya->execute();
                                                    $yomyas = $sqlyomya->fetchAll();
                                                    foreach ($yomyas as $yomya) {
                                                        $cashfrom3 = $yomya['cash'];
                                                        //$cashallfrom1 += $cashfrom1;
                                                    }
                                                    $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv3 = '$code_accountlv3' AND from_or_to = 2 ");
                                                    $sqlyomya->execute();
                                                    $yomyas = $sqlyomya->fetchAll();
                                                    foreach ($yomyas as $yomya) {
                                                        $cashto3 = $yomya['cash'];
                                                        //$cashallto1 += $cashto1;
                                                    }
                                                    $cashend3 = $cashto3 - $cashfrom3;
                                                    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $cashend3;
                                                    $data[] = ['','',$name_accountlv3, '', '', $cashend3];
                                                    ?>
                                                </td>
                                            </tr>
                                            <?php
                                            $datahtml .= '<tr class="'.$class.'"><td style="text-align: right; font-weight: 700;">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $name_accountlv3.'</td>
                                            <td style="text-align: right; font-weight: 700;">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $cashend3.'</td></tr>';
                                        }
                                    }
                                }
                                ?>
                                <tr class="success">
                                    <td></td>
                                    <td></td>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th style="font-size: 18px; font-weight: 700;">
                                        صافى <span class="green">الربح</span> / <span class="red">الخسارة</span>
                                    </th>

                                    <th style="text-align: right; font-size: 20px; font-weight: 700;">
                                        <?php
                                        if ($cashall1 >= 0) {
                                            $color = "green";
                                            $colorpdf = "#008000";
                                        } elseif ($cashall1 < 0) {
                                            $color = "red";
                                            $colorpdf = "#ff0000";
                                        }
                                        ?>
                                        <div class="<?=$color?>">
                                            <?php
                                            echo $cashall1;
                                            $data[] = ['صافى الربح/الخسارة','','', $cashall1, '', ''];
                                            // إضافة البيانات إلى الورقة
                                            /* $sheet = $spreadsheet->getActiveSheet();
                                            $sheet->fromArray($data, null, 'A1');
                                            foreach ($sheet->getRowIterator() as $row) {
                                                foreach ($row->getCellIterator() as $cell) {
                                                    $column = $cell->getColumn();
                                                    $sheet->getColumnDimension($column)->setWidth(20); // حد أقصى للعرض
                                                }
                                            } */
                                            /* $filename = 'ditels_earn.xlsx';
                                            $writer = new Xlsx($spreadsheet); */
                                            $dataString = json_encode($data);
                                            ?>
                                        </div>

                                    </th>
                                </tr>
                            </tfoot>
                        </table>
                        <?php 

                        $datahtml .= '<tr><th style="font-weight: 700;">
                        صافى <span class="green" style="color: #008000;">الربح</span>/<span class="red" style="color: #ff0000;">الخسارة</span>
                        </th>
                        <th style="text-align: right; font-size: 20px; font-weight: 700; color: '.$colorpdf.';">' . $cashall1.'</th></tr></table></div></section>';
                        ?>
                    </div>
                    <script>
                        $(document).ready(function () {
                            $("#downloadFormExcel").submit(function (event) {
                                event.preventDefault();
                                var data = <?php echo $dataString; ?>;
                                var dataString = JSON.stringify(data);
                                $("#dataExcel").val(dataString);
                                $("#successMessage").show();
                                this.submit();
                            });
                            var datahtml = <?php echo json_encode($datahtml); ?>;
                            $("#downloadFormPDF").submit(function (event) {
                                event.preventDefault();
                                var dataString = JSON.stringify(datahtml);
                                console.log(dataString);
                                $("#dataPDF").val(dataString);
                                $("#successMessage").show();
                                this.submit();
                            });
                            $("#downloadFormPDFP").submit(function (event) {
                                event.preventDefault();
                                var dataString = JSON.stringify(datahtml);
                                console.log(dataString);
                                $("#dataPDFP").val(dataString);
                                $("#successMessage").show();
                                this.submit();
                            });
                        });
                    </script>
                </section>
                <script>
                    $(document).ready(function() {
                        $("#myInput").on("keyup", function() {
                            var value = $(this).val().toLowerCase();
                            $("#myTable tr").filter(function() {
                                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
                            });
                        });
                    });
                </script>
                <?php

            } elseif ($do == 'financial_center') {
                ?>
                <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=financial_center" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                            </div>
                        </div>
                    </div>
                </form>
                <section class="container">
                    <div class="panel panel-primary text-center table-responsive table_width">
                        <!-- Default panel contents -->
                        <div class="panel-heading">قائمة المركز المالى خلال الفترة من <?php echo $start_date . " : " . $end_date; ?></div>
                        <!-- Table -->
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>
                                        البيان
                                    </th>

                                    <th>
                                        القيمة
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $AllSumcashold = 0;
                                $AllSumcashend = 0;
                                $cashfrom1 = 0;
                                // $sqlyomya = $con->prepare("SELECT * from balance_relay where dateinsert = '$start_date' AND lv3 = 1202 AND operation = 1");
                                // $sqlyomya->execute();
                                // $yomyas = $sqlyomya->fetchAll();
                                // foreach ($yomyas as $yomya) {
                                //     $AllSumcashold = $yomya['total_cash']; # قيمة مخزون أول المدة
                                //     //$AllSumcashold += $cashfrom1; # مخزون أول المدة
                                // }
                                $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where lv3 = 4201 AND from_or_to = 1 AND dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                $sqlyomya->execute();
                                $yomyas = $sqlyomya->fetchAll();
                                foreach ($yomyas as $yomya) {
                                    $AllSumcashbuy = $yomya['cash']; # قيمة إجمالى المشتريات خلال الفترة
                                }
                                /*  $sqlStarting = $con->prepare("SELECT sum(Quantity) AS K_G
                                                                    from data_processing 
                                                                    where id_operation_type = 7 ");
                                            $sqlStarting->execute();
                                            $starting = $sqlStarting->fetchAll();
                                            foreach ($starting as $start) {
                                                $startkg = $start['K_G']; # كمية البضاعة فى بداية البرنامج
                                            }

                                            $sqlkg = $con->prepare("SELECT sum(Quantity) AS 'sumkgb' FROM data_processing where id_operation_type = 1 ");
                                            $sqlkg->execute();
                                            $sumKgBs = $sqlkg->fetchAll();
                                            foreach ($sumKgBs as $sumkgb) {
                                                $SumKgBuyall = $sumkgb['sumkgb']; # إجمالى كمية البضاعة المشتراه
                                            }

                                            $sqlkg = $con->prepare("SELECT sum(Quantity) AS 'sumkgb' FROM data_processing where id_operation_type = 2 
                                                AND DateInsert < '$start_date_sql' ");
                                            $sqlkg->execute();
                                            $sumkgbs = $sqlkg->fetchAll();
                                            foreach ($sumkgbs as $sumkgb) {
                                                $SumKgSaleold = $sumkgb['sumkgb']; # كمية البضاعة المباعة قبل الفترة المحاسبية
                                            }

                                            $sqlkg = $con->prepare("SELECT sum(Quantity) AS 'sumkg' FROM data_processing where id_operation_type = 2 ");
                                            $sqlkg->execute();
                                            $sumkgs = $sqlkg->fetchAll();
                                            foreach ($sumkgs as $sumkg) {
                                                $SumKgSaleall = $sumkg['sumkg']; # إجمالى كمية البضاعة المباعة
                                            } */

                                /* $kgallstart = $startkg + $SumKgBuyall - $SumKgSaleold; */ # إجمالى البضاعة المتاحة للبيع
                                /* $kgallend = $startkg + $SumKgBuyall - $SumKgSaleall; */ # إجمالى كمية مخزون أخر المدة
                                /* $AllSumcashend = (($AllSumcashold + $AllSumcashbuy) / $kgallstart) * $kgallend; */ # قيمة مخزون أخر المدة
                                /* $AllSumcashend = ceil($AllSumcashend); */

                                //echo $AllSumcashend . " - " . $AllSumcashold. " - " . $AllSumKgend;

                                $cashall2 = 0;
                                $sqlaccounts = $con->prepare("SELECT * from level_acc where lev_acc = 1 AND list_account = 2 ORDER BY ID DESC");
                                $sqlaccounts->execute();
                                $accounts = $sqlaccounts->fetchAll();
                                foreach ($accounts as $account) {
                                    $IDlv1 = $account['ID'];
                                    $name_accountlv1 = $account['name_account'];
                                    $code_accountlv1 = $account['lv1'];
                                    $nature_acclv1 = $account['nature_account'];

                                    $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date' AND '$end_date' AND lv1 = '$code_accountlv1' AND from_or_to = 1 ");
                                    $sqlyomya->execute();
                                    $yomyas = $sqlyomya->fetchAll();
                                    foreach ($yomyas as $yomya) {
                                        $cashfrom1 = $yomya['cash'];
                                    }

                                    $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date' AND '$end_date' AND lv1 = '$code_accountlv1' AND from_or_to = 2 ");
                                    $sqlyomya->execute();
                                    $yomyas = $sqlyomya->fetchAll();
                                    foreach ($yomyas as $yomya) {
                                        $cashto1 = $yomya['cash'];
                                    }

                                    $cashend1 = $cashto1 - $cashfrom1;
                                    /* if ($code_accountlv1 == 4) {
                                                    $cashend1 = $cashend1 + $AllSumcashend - $AllSumcashold;
                                                } */
                                    $cashall2 += $cashend1;
                                }
                                //////////////////////////////////////////////////////////////////////// end
                                $cashall1 = 0;
                                $sqlaccounts = $con->prepare("SELECT * from level_acc where lev_acc = 1 AND list_account = 1 ORDER BY ID ASC");
                                $sqlaccounts->execute();
                                $accounts = $sqlaccounts->fetchAll();
                                foreach ($accounts as $account) {
                                    $IDlv1 = $account['ID'];
                                    $name_accountlv1 = $account['name_account'];
                                    $code_accountlv1 = $account['lv1'];
                                    $nature_acclv1 = $account['nature_account'];
                                    if ($code_accountlv1 == 1) {
                                        $class = "success";
                                    } elseif ($code_accountlv1 == 2 || $code_accountlv1 == 3) {
                                        $class = "danger";
                                    }
                                ?>
                                    <tr class="<?php echo $class; ?>">
                                        <td style="text-align: right; font-size: 18px; font-weight: 700;">
                                            <?php echo "&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv1; ?>
                                        </td>
                                        <td style="text-align: right; font-size: 20px; font-weight: 700;">
                                            <?php

                                            $cashfromold1 = 0;
                                            // $sqlyomya = $con->prepare("SELECT * from balance_relay where dateinsert = '$start_date' AND lv1 = '$code_accountlv1' AND operation = 1 AND from_or_to = 1 ");
                                            // $sqlyomya->execute();
                                            // $yomyas = $sqlyomya->fetchAll();
                                            // foreach ($yomyas as $yomya) {
                                            //     $cashfromold1 += $yomya['total_cash'];
                                            // }
                                            $cashtoold1 = 0;
                                            // $sqlyomya = $con->prepare("SELECT * from balance_relay where dateinsert = '$start_date' AND lv1 = '$code_accountlv1' AND operation = 1 AND from_or_to = 2 ");
                                            // $sqlyomya->execute();
                                            // $yomyas = $sqlyomya->fetchAll();
                                            // foreach ($yomyas as $yomya) {
                                            //     $cashtoold1 += $yomya['total_cash'];
                                            // }

                                            $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv1 = '$code_accountlv1' AND from_or_to = 1 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $cashfromnew1 = $yomya['cash'];
                                            }
                                            $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv1 = '$code_accountlv1' AND from_or_to = 2 ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $cashtonew1 = $yomya['cash'];
                                            }
                                            $cashfrom1 = $cashfromold1 + $cashfromnew1;
                                            $cashto1 = $cashtoold1 + $cashtonew1;

                                            $cashend1 = $cashto1 - $cashfrom1;
                                            if ($code_accountlv1 == 3) {
                                                $cashend1 = $cashend1 + $cashall2;
                                            } elseif ($code_accountlv1 == 1) {
                                                $AllSumcashend = $AllSumcashend - $AllSumcashend - $AllSumcashend;
                                                $cashend1 = $cashend1 + $AllSumcashend;
                                            }
                                            $cashall1 += $cashend1;
                                            echo "&nbsp;&nbsp;&nbsp;&nbsp;" . $cashend1;
                                            ?>
                                        </td>
                                    </tr>
                                    <?php
                                    $sqlaccounts = $con->prepare("SELECT * from level_acc where lev_acc = 2 AND lv1 = '$code_accountlv1' ");
                                    $sqlaccounts->execute();
                                    $accounts = $sqlaccounts->fetchAll();
                                    foreach ($accounts as $account) {
                                        $IDlv2 = $account['ID'];
                                        $name_accountlv2 = $account['name_account'];
                                        $code_accountlv2 = $account['lv2'];
                                    ?>
                                        <tr class="<?php echo $class; ?>">
                                            <td style="text-align: right; font-size: 20px; font-weight: 700;">
                                                <?php echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv2; ?>
                                            </td>
                                            <td style="text-align: right; font-size: 18px; font-weight: 700;">
                                                <?php

                                                $cashfromold2 = 0;
                                                // $sqlyomya = $con->prepare("SELECT * from balance_relay where dateinsert = '$start_date_sql' AND lv2 = '$code_accountlv2' AND operation = 1 AND from_or_to = 1 ");
                                                // $sqlyomya->execute();
                                                // $yomyas = $sqlyomya->fetchAll();
                                                // foreach ($yomyas as $yomya) {
                                                //     $cashfromold2 += $yomya['total_cash'];
                                                // }
                                                $cashtoold2 = 0;
                                                // $sqlyomya = $con->prepare("SELECT * from balance_relay where dateinsert = '$start_date_sql' AND lv2 = '$code_accountlv2' AND operation = 1 AND from_or_to = 2 ");
                                                // $sqlyomya->execute();
                                                // $yomyas = $sqlyomya->fetchAll();
                                                // foreach ($yomyas as $yomya) {
                                                //     $cashtoold2 += $yomya['total_cash'];
                                                // }

                                                $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv2 = '$code_accountlv2' AND from_or_to = 1 ");
                                                $sqlyomya->execute();
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $cashfromnew2 = $yomya['cash'];
                                                    //$cashallfrom1 += $cashfrom1;
                                                }
                                                $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv2 = '$code_accountlv2' AND from_or_to = 2 ");
                                                $sqlyomya->execute();
                                                $yomyas = $sqlyomya->fetchAll();
                                                foreach ($yomyas as $yomya) {
                                                    $cashtonew2 = $yomya['cash'];
                                                    //$cashallto1 += $cashto1;
                                                }
                                                $cashfrom2 = $cashfromold2 + $cashfromnew2;
                                                $cashto2 = $cashtoold2 + $cashtonew2;
                                                $cashend2 = $cashto2 - $cashfrom2;
                                                if ($code_accountlv2 == 31) {
                                                    if ($cashend2 < 0) {
                                                        $cashend2 = $cashend2 - $cashend2 - $cashend2;
                                                    }
                                                    $cashend2 = $cashend2 + $cashall2;
                                                } elseif ($code_accountlv2 == 12) {
                                                    // $cashend2 = $cashend2 + $AllSumcashend;
                                                }
                                                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $cashend2;
                                                ?>
                                            </td>
                                        </tr>
                                        <?php
                                        $sqlaccounts = $con->prepare("SELECT * from level_acc where lev_acc = 3 AND lv2 = '$code_accountlv2' ");
                                        $sqlaccounts->execute();
                                        $accounts = $sqlaccounts->fetchAll();
                                        foreach ($accounts as $account) {
                                            $IDlv3 = $account['ID'];
                                            $name_accountlv3 = $account['name_account'];
                                            $code_accountlv3 = $account['lv3'];
                                        ?>
                                            <tr class="<?php echo $class; ?>">
                                                <td style="text-align: right; font-size: 18px; font-weight: 700;">
                                                    <?php echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv3; ?>
                                                    <a data-bs-toggle="collapse" href=".code_accountlv3_<?php echo $IDlv3 ?>" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3_<?php echo $IDlv3 ?>" title='عرض المستوى الرابع'>
                                                        <i class='fa fa-caret-down' style="font-size:18px"></i>
                                                    </a>
                                                </td>
                                                <td style="text-align: right; font-size: 16px; font-weight: 700;">
                                                    <?php
                                                    $cashfromold3 = 0;
                                                    // $sqlyomya = $con->prepare("SELECT * from balance_relay where dateinsert = '$start_date' AND lv3 = '$code_accountlv3' AND operation = 1 AND from_or_to = 1 ");
                                                    // $sqlyomya->execute();
                                                    // $yomyas = $sqlyomya->fetchAll();
                                                    // foreach ($yomyas as $yomya) {
                                                    //     $cashfromold3 += $yomya['total_cash'];
                                                    // }
                                                    $cashtoold3 = 0;
                                                    // $sqlyomya = $con->prepare("SELECT * from balance_relay where dateinsert = '$start_date' AND lv3 = '$code_accountlv3' AND operation = 1 AND from_or_to = 2 ");
                                                    // $sqlyomya->execute();
                                                    // $yomyas = $sqlyomya->fetchAll();
                                                    // foreach ($yomyas as $yomya) {
                                                    //     $cashtoold3 += $yomya['total_cash'];
                                                    // }

                                                    $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv3 = '$code_accountlv3' AND from_or_to = 1 ");
                                                    $sqlyomya->execute();
                                                    $yomyas = $sqlyomya->fetchAll();
                                                    foreach ($yomyas as $yomya) {
                                                        $cashfromnew3 = $yomya['cash'];
                                                        //$cashallfrom1 += $cashfrom1;
                                                    }
                                                    $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv3 = '$code_accountlv3' AND from_or_to = 2 ");
                                                    $sqlyomya->execute();
                                                    $yomyas = $sqlyomya->fetchAll();
                                                    foreach ($yomyas as $yomya) {
                                                        $cashtonew3 = $yomya['cash'];
                                                        //$cashallto1 += $cashto1;
                                                    }
                                                    $cashfrom3 = $cashfromold3 + $cashfromnew3;
                                                    $cashto3 = $cashtoold3 + $cashtonew3;
                                                    echo $cashend3 = $cashto3 - $cashfrom3;
                                                    // if ($code_accountlv3 == 1202) {

                                                    //     echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .  $AllSumcashend ;
                                                    // } else{
                                                    //     echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .  $cashend3 ;
                                                    // }
                                                    ?>
                                                </td>
                                            </tr>
                                            <?php
                                            $sqlaccounts = $con->prepare("SELECT * from level_acc where lev_acc = 4 AND lv3 = '$code_accountlv3' ");
                                            $sqlaccounts->execute();
                                            $accounts = $sqlaccounts->fetchAll();
                                            foreach ($accounts as $account) {
                                                $IDlv4 = $account['ID'];
                                                $name_accountlv4 = $account['name_account'];
                                                $code_accountlv4 = $account['lv4'];
                                            ?>
                                                <tr id="code_accountlv3_<?php echo $IDlv3 ?>" class="<?php echo $class; ?> code_accountlv3_<?php echo $IDlv3 ?> code_accountlv3 table-danger collapse" aria-expanded="false">
                                                    <td style="text-align: right; font-size: 18px; font-weight: 700;">
                                                        <?php echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $name_accountlv4; ?>
                                                    </td>
                                                    <td style="text-align: right; font-size: 16px; font-weight: 700;">
                                                        <?php

                                                        $cashfromold4 = 0;
                                                        /* $sqlyomya = $con->prepare("SELECT * from balance_relay where lv4 = '$code_accountlv4' AND operation = 1 AND from_or_to = 1 ");
                                                                                    $sqlyomya->execute();
                                                                                    $yomyas = $sqlyomya->fetchAll();
                                                                                    foreach ($yomyas as $yomya) {
                                                                                        $cashfromold3 += $yomya['total_cash'];
                                                                                    } */
                                                        $cashtoold4 = 0;
                                                        /* $sqlyomya = $con->prepare("SELECT * from balance_relay where lv3 = '$code_accountlv3' AND operation = 1 AND from_or_to = 2 ");
                                                                                    $sqlyomya->execute();
                                                                                    $yomyas = $sqlyomya->fetchAll();
                                                                                    foreach ($yomyas as $yomya) {
                                                                                        $cashtoold3 += $yomya['total_cash'];
                                                                                    } */

                                                        $cashfromnew4 = 0;
                                                        $cashtonew4 = 0;
                                                        $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv4 = '$code_accountlv4' AND from_or_to = 1 ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $cashfromnew4 = $yomya['cash'];
                                                            //$cashallfrom1 += $cashfrom1;
                                                        }
                                                        $sqlyomya = $con->prepare("SELECT sum(cash) AS 'cash' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND lv4 = '$code_accountlv4' AND from_or_to = 2 ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $cashtonew4 = $yomya['cash'];
                                                            //$cashallto1 += $cashto1;
                                                        }
                                                        $cashfrom4 = $cashfromold4 + $cashfromnew4;
                                                        $cashto4 = $cashtoold4 + $cashtonew4;
                                                        $cashend4 = $cashto4 - $cashfrom4;
                                                        if ($code_accountlv4 == 1202) {
                                                            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $AllSumcashend;
                                                        } else {
                                                            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $cashend4;
                                                        }
                                                        ?>
                                                    </td>
                                                </tr>
                                <?php
                                            }
                                        }
                                    }
                                }
                                ?>
                                <tr class="success">
                                    <td></td>
                                    <td></td>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th style="font-size: 20px; font-weight: 700;">
                                        صافى <span class="green">الميزانية</span>
                                    </th>
                                    <th style="text-align: right; font-size: 20px; font-weight: 700;">
                                        <?php
                                        if ($cashall1 >= 0) {
                                            $color = "green";
                                        } elseif ($cashall1 < 0) {
                                            $color = "red";
                                        }
                                        ?>
                                        <div class="<?php echo $color; ?>">
                                            <?php echo $cashall1; ?>
                                        </div>

                                    </th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </section>
                <script>
                    $(document).ready(function() {
                        $("#myInput").on("keyup", function() {
                            var value = $(this).val().toLowerCase();
                            $("#myTable tr").filter(function() {
                                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
                            });
                        });
                    });
                </script>
                <?php
            } elseif ($do == "add_yomya") {
                if (isset($_GET['currency_code']) && !empty($_GET['currency_code'])){
                    $id_currency_code = $_GET['currency_code'];
                } else {
                    $id_currency_code = 1;
                }
                if (isset($_GET['err']) && !empty($_GET['err'])) {
                    $nameErr = array();
                    $nameErr = $_GET['err'];
                    foreach ($nameErr as $value) {
                        if (!empty($value)) {
                            echo '<div class="alert alert-danger">';
                            echo $value;
                            echo '</div>';
                        }
                    }
                }
                if (isset($_GET['success']) && !empty($_GET['success'])) {
                    $nameSuc = array();
                    $nameSuc = $_GET['success'];
                    foreach ($nameSuc as $value) {
                        if (!empty($value)) {
                            echo '<div class="alert alert-success">';
                            echo $value;
                            echo '</div>';
                        }
                    }
                }
                $currentYear = date("Y");
                $sqlLastRegistrationNumber = "SELECT MAX(registration_number) AS last_ID FROM yomya
                WHERE YEAR(dateinsert) = :currentYear";

                $stmtLastRegistrationNumber = $con->prepare($sqlLastRegistrationNumber);
                $stmtLastRegistrationNumber->bindParam(":currentYear", $currentYear, PDO::PARAM_INT);
                $stmtLastRegistrationNumber->execute();

                $resultLastRegistrationNumber = $stmtLastRegistrationNumber->fetch(PDO::FETCH_ASSOC);
                if (!empty($resultLastRegistrationNumber['last_ID'])) {
                    $last_ID_yomya = $resultLastRegistrationNumber['last_ID'];
                } else {
                    $last_ID_yomya = 1;
                }

                /* $account = $con->prepare("SELECT MAX(id_yomya) as last_ID FROM yomya");
                $account->execute();
                $account1s = $account->fetchAll();
                foreach ($account1s as $key => $account1) {
                    if (!empty($account1['last_ID'])) {
                        $last_ID_yomya = $account1['last_ID'];
                    } else {
                        $last_ID_yomya = 1;
                    }
                } */
                $hid_no_id = "";
                if (isset($_GET['id']) && !empty($_GET['id'])) {
                    $last_idyomya = $_GET['id'];
                    if ($last_idyomya >= $last_ID_yomya) {
                        $hid_no_id = "disabled";
                    }
                    $text_head = 'قيد اليومية';
                } else {
                    $last_idyomya = $last_ID_yomya;
                    $hid_no_id = "disabled";
                    $text_head = 'قيد اليومية';
                }
                ?>
                <section class="container">
                    <div class="text-center">
                        <form class="was-validated" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="get">
                            <div class="row">
                                <div class="col-sm-2">
                                    <input type="hidden" name="Manage" id="Manage" value="tree">
                                    <input type="hidden" name="do" id="do" value="add_yomya">
                                    <input type="text" name="id" id="id" class="form-control" value="<?php echo $last_idyomya ?>" placeholder="رقم القيد" required>
                                </div>
                                <div class="col-sm-4">
                                    <div class="form-group">
                                        <select name="currency_code" class="form-select" required>
                                            <?php
                                            // Query to retrieve agents from the agents table
                                            $personsQuery = "SELECT * FROM currency where ID = 1 OR ID = 2";
                                            $personsResult = $con->query($personsQuery);
                                            while ($persons = $personsResult->fetch(PDO::FETCH_ASSOC)) {
                                                echo "<option value='{$persons['ID']}'>{$persons['currency_shortcut']}</option>";
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-sm-1">
                                    <div class="form-group">
                                        <input class="btn btn-primary" type="submit" value="عرض" name="search">
                                    </div>
                                </div>
                                <div class="col-sm-4">
                                    <a href="?Manage=tree&do=add_yomya&id=<?php echo $last_idyomya - 1; ?>" class="btn btn-primary">السابق <i class="fa-solid fa-right-long" style="font-size:24px"></i></a>
                                    <a href="?Manage=tree&do=add_yomya&id=<?php echo $last_idyomya + 1; ?>" class="btn btn-primary <?php echo $hid_no_id; ?>">التالى <i class="fa fa-left-long" style="font-size:24px"></i></a>
                                    <a href="?Manage=tree&do=add_yomya&id=<?php echo $last_ID_yomya; ?>" class="btn btn-primary">أخر قيد <i class="fa fa-angles-left" style="font-size:24px"></i></a>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="panel panel-primary text-center table-responsive">
                        <!-- Default panel contents -->
                        <div class="panel-heading"><?php echo $text_head; ?></div>
                        <!-- Table -->
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>
                                        التاريخ
                                    </th>
                                    <th>
                                        رقم القيد
                                    </th>
                                    <th>
                                        البيان
                                    </th>
                                    <th>
                                        مدين
                                    </th>
                                    <th>
                                        دائن
                                    </th>
                                    <th>
                                        #
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $currency_code = getId('currency','ID',$id_currency_code, 'currency_code');
                                $allmadeen = 0;
                                $alldaen = 0;
                                $sqlyomya = $con->prepare("SELECT * from yomya where registration_number = ? ");
                                $sqlyomya->execute(array($last_idyomya));
                                $yomyas = $sqlyomya->fetchAll();
                                foreach ($yomyas as $yomya) {
                                    $id_yomya = $yomya['id_yomya'];
                                    ?>
                                    <tr class="success">
                                        <td>
                                            <?php echo $yomya['dateinsert'] ?>
                                        </td>
                                        <td>
                                            <?php echo $yomya['registration_number'] ?>
                                        </td>
                                        <td style="text-align: right;">
                                            <?php
                                            $id_account_lv4 = 0;
                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' ");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            $count_yomyas = $sqldetails_yomya->rowCount();
                                            if ($count_yomyas > 2) {
                                                # code...
                                            }
                                            $count_from = 0;
                                            $count_to = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                if ($from_or_to == 1) {
                                                    $count_from += 1;
                                                }

                                                if ($from_or_to == 2) {
                                                    $count_to++;
                                                }
                                            }
                                            $name_from_or_to_m = "";
                                            $count_from_num = 0;
                                            $count_to_num = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];

                                                if($id_currency_code != 1){
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                }
                                                if ($from_or_to == 1) {
                                                    $madeen = $cash;
                                                    $allmadeen += $madeen;
                                                    if (!empty($details_yomya['lv5'])) {
                                                        $lvt = $details_yomya['lv5'];
                                                        $lvt1 = $details_yomya['lv4'];
                                                    } elseif (!empty($details_yomya['lv4'])) {
                                                        $lvt = $details_yomya['lv4'];
                                                        $lvt1 = $details_yomya['lv3'];
                                                    } elseif (!empty($details_yomya['lv3'])) {
                                                        $lvt = $details_yomya['lv3'];
                                                        $lvt1 = $details_yomya['lv2'];
                                                    } elseif (!empty($details_yomya['lv2'])) {
                                                        $lvt = $details_yomya['lv2'];
                                                        $lvt1 = $details_yomya['lv1'];
                                                    } elseif (!empty($details_yomya['lv1'])) {
                                                        $lvt = $details_yomya['lv1'];
                                                    }
                                                    /* $id_account_lv3 = $details_yomya['lv3'];
                                                    $id_account_lv4 = $details_yomya['lv4']; */

                                                    if ($count_from == 1) {
                                                        $name_from_or_to = "من حـ/ ";
                                                    } elseif ($count_from > 1) {
                                                        if ($count_from_num == 0) {
                                                            $name_from_or_to_m = "<b>من مذكورين/</b> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                            $name_from_or_to = "";
                                                        } else {
                                                            $name_from_or_to_m = "";
                                                            $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                        }
                                                    }
                                                    $count_from_num++;
                                                } elseif ($from_or_to == 2) {
                                                    $daen = $cash;
                                                    $alldaen += $daen;
                                                    if (!empty($details_yomya['lv5'])) {
                                                        $lvt = $details_yomya['lv5'];
                                                        $lvt1 = $details_yomya['lv4'];
                                                    } elseif (!empty($details_yomya['lv4'])) {
                                                        $lvt = $details_yomya['lv4'];
                                                        $lvt1 = $details_yomya['lv3'];
                                                    } elseif (!empty($details_yomya['lv3'])) {
                                                        $lvt = $details_yomya['lv3'];
                                                        $lvt1 = $details_yomya['lv2'];
                                                    } elseif (!empty($details_yomya['lv2'])) {
                                                        $lvt = $details_yomya['lv2'];
                                                        $lvt1 = $details_yomya['lv1'];
                                                    } elseif (!empty($details_yomya['lv1'])) {
                                                        $lvt = $details_yomya['lv1'];
                                                    }
                                                    /* $id_account_lv3 = "";
                                                    $id_account_lv3 = $details_yomya['lv3'];
                                                    $id_account_lv4 = $details_yomya['lv4']; */

                                                    if ($count_to == 1) {
                                                        $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;إلى حـ/ ";
                                                    } elseif ($count_to > 1) {
                                                        if ($count_to_num == 0) {
                                                            $name_from_or_to_m = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>إلى مذكورين/</b> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                            $name_from_or_to = "";
                                                        } else {
                                                            $name_from_or_to_m = "";
                                                            $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                        }
                                                    }
                                                    $count_to_num++;
                                                }
                                                $name_account_lv3 = "";
                                                if (isset($lvt1) && !empty($lvt1)) {
                                                    $sqlaccounts = $con->prepare("SELECT * from level_acc where lv1 = '$lvt1'");
                                                    $sqlaccounts->execute();
                                                    $accounts = $sqlaccounts->fetchAll();
                                                    foreach ($accounts as $account) {
                                                        $name_account_lv3 = $account['name_account'];
                                                    }
                                                }
                                                $name_account_lv4 = "";
                                                if (isset($lvt) && !empty($lvt)) {
                                                    $name_account_lv4 = " - " . getName("level_acc", "lv", $lvt, "name_account");
                                                }
                                                echo $name_from_or_to . $name_from_or_to_m . $name_account_lv3 . $name_account_lv4;
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo $yomya['payan'];
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $count_from_num = 0;
                                            $allmadeen_thes = 0;
                                            $alldaen_thes = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];

                                                if($id_currency_code != 1){
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                }
                                                if ($from_or_to == 1) {
                                                    $madeen = $cash;
                                                    $allmadeen_thes += $madeen;
                                                    if ($count_from == 1) {
                                                        echo $madeen. ' '. $currency_code;
                                                    } elseif ($count_from > 1) {
                                                        if ($count_from_num == 0) {
                                                            echo "<br>" . $madeen. ' '. $currency_code;
                                                        } else {
                                                            echo "<br>" . $madeen. ' '. $currency_code;
                                                        }
                                                    }
                                                    $count_from_num++;
                                                }
                                            }
                                            if ($count_to > 1) {
                                                $new_count_to = $count_to + 1;
                                            } else {
                                                $new_count_to = $count_to;
                                            }
                                            for ($i = 0; $i <= $new_count_to; $i++) {
                                                echo "<br>";
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo "<b>" . $allmadeen_thes. ' '. $currency_code . "</b>";
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $count_to_num = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                if ($from_or_to == 2) {
                                                    $daen = $cash;
                                                    $alldaen_thes += $daen;
                                                    if ($count_to == 1) {
                                                        echo "<br>" . $daen. ' '. $currency_code;
                                                    } elseif ($count_to > 1) {
                                                        if ($count_to_num == 0) {
                                                            for ($i = 0; $i <= $count_from; $i++) {
                                                                echo "<br>";
                                                            }
                                                            echo "<br>" . $daen. ' '. $currency_code;
                                                        } else {
                                                            echo "<br>" . $daen. ' '. $currency_code;
                                                        }
                                                    }
                                                    $count_to_num++;
                                                }
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo "<b>" . $alldaen_thes. ' '. $currency_code . "</b>";
                                            ?>
                                        </td>
                                        <td>
                                        <a href='?Manage=tree&do=edit_yomya&id=<?=$yomya['registration_number']?>&currency_code=<?=$id_currency_code?>' class='btn btn-success'><i class="fa-solid fa-user-pen"></i> </a>
                                        </td>
                                    </tr>
                                <?php
                                }
                                ?>
                            </tbody>
                            <tfoot>
                                <tr class="success">
                                    <td>

                                    </td>
                                    <td>

                                    </td>
                                    <th>
                                        الإجمالى
                                    </th>
                                    <th>
                                        <?php echo $allmadeen. ' '. $currency_code ?>
                                    </th>
                                    <th>
                                        <?php echo $alldaen. ' '. $currency_code ?>
                                    </th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </section>
                <!-- start -->
                <section class="container">
                    <!-- <a href="#"><i class="fa fa-print"></i></a> -->
                    <div class="panel panel-primary text-center table-responsive mb-5">
                        <!-- Default panel contents -->
                        <div class="panel-heading">إضافة قيد يومية
                            <button form="add_yomya" type="submit" value="1" name="go_state" class="btn btn-success btn-sm">
                                حفظ القيد <i class=" fa-solid fa-floppy-disk"> </i>
                            </button>
                        </div>
                        <form id="add_yomya" class="was-validated" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=insert_yomya" method="post">
                            <div class="container-fluid text-center">
                                <div class="row">
                                    <div class="col-sm-12">
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <input type="date" name="mydate" id="mydate" class="form-control" value="<?php echo $thisyear . "-" . $thismon . "-" . $thisday ?>" placeholder="التاريخ" required>
                                            <label for="mydate" class="form-label">التاريخ</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <input type="text" readonly name="lastidyomya" id="lastidyomya" class="form-control" value="<?php echo $last_ID_yomya + 7 ?>" placeholder="التاريخ" required>
                                            <label for="mydate" class="form-label">رقم القيد</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <select name="currency" id="currency" class="form-select" required>
                                                <?php
                                                // Query to retrieve agents from the agents table
                                                $agentsQuery = "SELECT * FROM currency";
                                                $agentsResult = $con->query($agentsQuery);
                                                while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                                        echo "<option value='{$agent['ID']}'>{$agent['name_ar']}{$agent['currency_shortcut']}</option>";
                                                }
                                                ?>
                                            </select>
                                            <label for="currency" class="form-label">العملة:</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-12 mt-4 mb-4">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="ml7oza" id="ml7oza" placeholder="البيان" required>
                                            <label for="ml7oza" class="form-label">البيان</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-10">
                                        <label for="lv3f" class="input-group-text">من حساب</label>
                                    </div>
                                    <div class="col-sm-2">
                                        <label for="cash_f" class="input-group-text">الملبغ</label>
                                    </div>
                                    <div class="col-sm-1">
                                        <span id="new_lvfrom" class="btn btn-success"><i class="fa fa-plus"></i></span>
                                    </div>
                                    <div class="col-sm-5" id="div_lv3f">
                                        <div class="form-group mb-4">
                                            <select id="lv3f" name="lv3f[]" class="form-control lv3f" required>
                                                <option value="">إختر الحساب</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccounts(0, $topLevel);
                                                printAccountOptions($topAccounts, $topLevel);
                                                ?>
                                            </select>
                                            <br>
                                        </div>
                                        
                                    </div>
                                    <!-- <div class="col-sm-2" id="div_lv4f">
                                        <div class="form-group">
                                            <select id="lv4f" name="lv4f[]" class="form-control" required>
                                                <option value="">الحساب الفرعي</option>
                                            </select>
                                        </div>
                                    </div> -->
                                    <!-- <div class="col-sm-2" id="div_lv5f">
                                        <div class="form-group">
                                            <select id="lv5f" name="lv5f[]" class="form-control" required>
                                                <option value="">الحساب التحليلى</option>
                                            </select>
                                        </div>
                                    </div> -->
                                    <div class="col-sm-4" id="div_ccf">
                                        <div class="form-group">
                                            <select id="ccf" name="ccf[]" class="form-control">
                                                <option value="">مركز التكلفة</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccountsCost(0, $topLevel);
                                                printAccountOptionsCost($topAccounts, $topLevel);
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-2" id="div_cashfrom">
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="cash_f[]" id="cash_f" placeholder="المبلغ" required>
                                        </div>
                                    </div>
                                    <div class="col-sm-12">
                                        <label for="lv3t" class="input-group-text">الى حساب</label>
                                    </div>
                                    <div class="col-sm-1">
                                    </div>
                                    <div class="col-sm-1">
                                        <span id="new_lvto" class="btn btn-success"><i class="fa fa-plus"></i></span>
                                    </div>
                                    <div class="col-sm-4" id="div_lv3t">
                                        <div class="form-group">
                                            <select id="lv3t" name="lv3t[]" class="form-control lv3t" required>
                                                <option value="">إختر الحساب</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccounts(0, $topLevel);
                                                printAccountOptions($topAccounts, $topLevel);
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <!-- <div class="col-sm-2" id="div_lv4t">
                                        <div class="form-group">
                                            <select id="lv4t" name="lv4t[]" class="form-control" required>
                                                <option value="">الحساب الفرعي</option>
                                            </select>
                                        </div>
                                    </div> -->
                                    <!-- <div class="col-sm-2" id="div_lv5t">
                                        <div class="form-group">
                                            <select id="lv5t" name="lv5t[]" class="form-control" required>
                                                <option value="">الحساب التحليلى</option>
                                            </select>
                                        </div>
                                    </div> -->
                                    <div class="col-sm-4"  id="div_cct">
                                        <div class="form-group">
                                            <select id="cct" name="cct[]" class="form-control">
                                                <option value="">مركز التكلفة</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccountsCost(0, $topLevel);
                                                printAccountOptionsCost($topAccounts, $topLevel);
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <!-- <div class="col-sm-1">
                                    </div> -->
                                    <div class="col-sm-2" id="div_cashto">
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="cash_t[]" id="cash_t" placeholder="المبلغ" required>
                                        </div>
                                    </div>
                                    <?php
                                    /* $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE lev_acc = 3");
                                    $sqlaccounts->execute();
                                    $rowlv3s = $sqlaccounts->fetchAll();
                                    $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE lev_acc = 4");
                                    $sqlaccounts->execute();
                                    $rowlv4s = $sqlaccounts->fetchAll();
                                    $sqlaccounts = $con->prepare("SELECT * from level_acc WHERE lev_acc = 5");
                                    $sqlaccounts->execute();
                                    $rowlv5s = $sqlaccounts->fetchAll(); */
                                    ?>
                                    <script>
                                        function initializeSelect2(selector) {
                                            $(selector).select2({
                                                dir: 'rtl',
                                                width: '100%'
                                            });
                                        }
                                        $(document).ready(function() {
                                            initializeSelect2('.lv3f');
                                            initializeSelect2('.lv3t');
                                        });
                                    // var counter = 0;
                                    <?php
                                    $topLevel = 1;
                                    $topAccounts = getSubAccounts(0, $topLevel);
                                    $displayedAccounts = [];
                                    ob_start();
                                    printAccountOptions($topAccounts, $topLevel, $displayedAccounts);
                                    $printedOptions = ob_get_clean();

                                    $topLevel = 1;
                                    $topAccounts = getSubAccountsCost(0, $topLevel);
                                    $displayedAccounts = [];
                                    ob_start();
                                    printAccountOptionsCost($topAccounts, $topLevel, $displayedAccounts);
                                    $printedOptionsCost = ob_get_clean();
                                    ?>
                                        $("#new_lvfrom").click(function() {
                                            $("#div_lv3f").append('<div class="mt-4"><select name="lv3f[]" class="form-control lv3f"> <option value="">إختر الحساب</option><?php echo addslashes($printedOptions); ?></select></div>');
                                            $("#div_ccf").append('<div class="mt-4"><select name="ccf[]" class="form-control ccf"> <option value="">إختر مركز التكلفة</option><?php echo addslashes($printedOptionsCost); ?></select></div>');
                                            $("#div_cashfrom").append('<br><input type="text" name="cash_f[]" class="form-control " placeholder="المبلغ" />');
                                            // ++counter
                                            // updateEvents($("#div_lv3f"));
                                            initializeSelect2('.lv3f');
                                        });
                                        $("#new_lvto").click(function() {
                                            $("#div_lv3t").append('<div class="mt-4"><select name="lv3t[]" class="form-control lv3t"> <option value="">إختر الحساب</option><?php echo addslashes($printedOptions); ?></select></div>');
                                            $("#div_cct").append('<div class="mt-4"><select name="cct[]" class="form-control cct"> <option value="">إختر مركز التكلفة</option><?php echo addslashes($printedOptionsCost); ?></select></div>');
                                            
                                             $("#div_cashto").append('<br><input type="text" name="cash_t[]" class="form-control " placeholder="المبلغ" />');
                                            // ++counter
                                            initializeSelect2('.lv3t');
                                        });
                                    </script>
                                    <br>
                                    <br>
                                    <div class="col-md-8">

                                    </div>
                                    <div class="col-md-4 m-3">
                                        <div class="form-group">
                                            <input class="form-control btn btn-primary" type="submit" value="حفظ" name="add">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </section>
                <!-- end -->
                <?php

            } elseif ($do == "edit_yomya") {

                if (isset($_GET['currency_code']) && !empty($_GET['currency_code'])){
                    $id_currency_code = $_GET['currency_code'];
                } else {
                    $id_currency_code = 1;
                }
                if (isset($_GET['err']) && !empty($_GET['err'])) {
                    $nameErr = array();
                    $nameErr = $_GET['err'];
                    foreach ($nameErr as $value) {
                        if (!empty($value)) {
                            echo '<div class="alert alert-danger">';
                            echo $value;
                            echo '</div>';
                        }
                    }
                }
                if (isset($_GET['success']) && !empty($_GET['success'])) {
                    $nameSuc = array();
                    $nameSuc = $_GET['success'];
                    foreach ($nameSuc as $value) {
                        if (!empty($value)) {
                            echo '<div class="alert alert-success">';
                            echo $value;
                            echo '</div>';
                        }
                    }
                }
                $currentYear = date("Y");
                $sqlLastRegistrationNumber = "SELECT MAX(registration_number) AS last_ID FROM yomya
                WHERE YEAR(dateinsert) = :currentYear";

                $stmtLastRegistrationNumber = $con->prepare($sqlLastRegistrationNumber);
                $stmtLastRegistrationNumber->bindParam(":currentYear", $currentYear, PDO::PARAM_INT);
                $stmtLastRegistrationNumber->execute();

                $resultLastRegistrationNumber = $stmtLastRegistrationNumber->fetch(PDO::FETCH_ASSOC);
                if (!empty($resultLastRegistrationNumber['last_ID'])) {
                    $last_ID_yomya = $resultLastRegistrationNumber['last_ID'];
                } else {
                    $last_ID_yomya = 1;
                }

                /* $account = $con->prepare("SELECT MAX(id_yomya) as last_ID FROM yomya");
                $account->execute();
                $account1s = $account->fetchAll();
                foreach ($account1s as $key => $account1) {
                    if (!empty($account1['last_ID'])) {
                        $last_ID_yomya = $account1['last_ID'];
                    } else {
                        $last_ID_yomya = 1;
                    }
                } */
                $hid_no_id = "";
                if (isset($_GET['id']) && !empty($_GET['id'])) {
                    $last_idyomya = $_GET['id'];
                    if ($last_idyomya >= $last_ID_yomya) {
                        $hid_no_id = "disabled";
                    }
                    $text_head = 'قيد اليومية';
                } else {
                    $last_idyomya = $last_ID_yomya;
                    $hid_no_id = "disabled";
                    $text_head = 'قيد اليومية';
                }
                ?>
                <section class="container">
                    <div class="text-center">
                        <form class="was-validated" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="get">
                            <div class="row">
                                <div class="col-sm-2">
                                    <input type="hidden" name="Manage" id="Manage" value="tree">
                                    <input type="hidden" name="do" id="do" value="edit_yomya">
                                    <input type="text" name="id" id="id" class="form-control" value="<?php echo $last_idyomya ?>" placeholder="رقم القيد" required>
                                </div>
                                <div class="col-sm-4">
                                    <div class="form-group">
                                        <select name="currency_code" class="form-select" required>
                                            <?php
                                            // Query to retrieve agents from the agents table
                                            $personsQuery = "SELECT * FROM currency where ID = 1 OR ID = 2";
                                            $personsResult = $con->query($personsQuery);
                                            while ($persons = $personsResult->fetch(PDO::FETCH_ASSOC)) {
                                                echo "<option value='{$persons['ID']}'>{$persons['currency_shortcut']}</option>";
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-sm-1">
                                    <div class="form-group">
                                        <input class="btn btn-primary" type="submit" value="عرض" name="search">
                                    </div>
                                </div>
                                <div class="col-sm-4">
                                    <a href="?Manage=tree&do=edit_yomya&id=<?php echo $last_idyomya - 1; ?>" class="btn btn-primary">السابق <i class="fa-solid fa-right-long" style="font-size:24px"></i></a>
                                    <a href="?Manage=tree&do=edit_yomya&id=<?php echo $last_idyomya + 1; ?>" class="btn btn-primary <?php echo $hid_no_id; ?>">التالى <i class="fa fa-left-long" style="font-size:24px"></i></a>
                                    <a href="?Manage=tree&do=edit_yomya&id=<?php echo $last_ID_yomya; ?>" class="btn btn-primary">أخر قيد <i class="fa fa-angles-left" style="font-size:24px"></i></a>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="panel panel-primary text-center table-responsive">
                        <!-- Default panel contents -->
                        <div class="panel-heading"><?=$text_head?></div>
                        <!-- Table -->
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>
                                        التاريخ
                                    </th>
                                    <th>
                                        رقم القيد
                                    </th>
                                    <th>
                                        البيان
                                    </th>
                                    <th>
                                        مدين
                                    </th>
                                    <th>
                                        دائن
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $currency_code = getId('currency','ID',$id_currency_code, 'currency_code');
                                $allmadeen = 0;
                                $alldaen = 0;
                                $sqlyomya = $con->prepare("SELECT * from yomya where registration_number = ? ");
                                $sqlyomya->execute(array($last_idyomya));
                                $yomyas = $sqlyomya->fetchAll();
                                foreach ($yomyas as $yomya) {
                                    $id_yomya = $yomya['id_yomya'];
                                    ?>
                                    <tr class="success">
                                        <td>
                                            <?php echo $yomya['dateinsert'] ?>
                                        </td>

                                        <td>
                                            <?php echo $yomya['registration_number'] ?>
                                        </td>

                                        <td style="text-align: right;">
                                            <?php
                                            $id_account_lv4 = 0;
                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' ");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            $count_yomyas = $sqldetails_yomya->rowCount();
                                            $count_from = 0;
                                            $count_to = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                if ($from_or_to == 1) {
                                                    $count_from += 1;
                                                }

                                                if ($from_or_to == 2) {
                                                    $count_to++;
                                                }
                                            }
                                            $name_from_or_to_m = "";
                                            $count_from_num = 0;
                                            $count_to_num = 0;
                                            $lvt = '';
                                            $lvt1 = '';
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];

                                                if($id_currency_code != 1){
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                }
                                                if ($from_or_to == 1) {
                                                    $madeen = $cash;
                                                    $allmadeen += $madeen;
                                                    if (!empty($details_yomya['lv5'])) {
                                                        $lvt = $details_yomya['lv5'];
                                                        $lvt1 = $details_yomya['lv4'];
                                                    } elseif (!empty($details_yomya['lv4'])) {
                                                        $lvt = $details_yomya['lv4'];
                                                        $lvt1 = $details_yomya['lv3'];
                                                    } elseif (!empty($details_yomya['lv3'])) {
                                                        $lvt = $details_yomya['lv3'];
                                                        $lvt1 = $details_yomya['lv2'];
                                                    } elseif (!empty($details_yomya['lv2'])) {
                                                        $lvt = $details_yomya['lv2'];
                                                        $lvt1 = $details_yomya['lv1'];
                                                    } elseif (!empty($details_yomya['lv1'])) {
                                                        $lvt = $details_yomya['lv1'];
                                                    }
                                                    /* $id_account_lv3 = $details_yomya['lv3'];
                                                    $id_account_lv4 = $details_yomya['lv4']; */

                                                    if ($count_from == 1) {
                                                        $name_from_or_to = "من حـ/ ";
                                                    } elseif ($count_from > 1) {
                                                        if ($count_from_num == 0) {
                                                            $name_from_or_to_m = "<b>من مذكورين/</b> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                            $name_from_or_to = "";
                                                        } else {
                                                            $name_from_or_to_m = "";
                                                            $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                        }
                                                    }
                                                    $count_from_num++;
                                                } elseif ($from_or_to == 2) {
                                                    $daen = $cash;
                                                    $alldaen += $daen;
                                                    if (!empty($details_yomya['lv5'])) {
                                                        $lvt = $details_yomya['lv5'];
                                                        $lvt1 = $details_yomya['lv4'];
                                                    } elseif (!empty($details_yomya['lv4'])) {
                                                        $lvt = $details_yomya['lv4'];
                                                        $lvt1 = $details_yomya['lv3'];
                                                    } elseif (!empty($details_yomya['lv3'])) {
                                                        $lvt = $details_yomya['lv3'];
                                                        $lvt1 = $details_yomya['lv2'];
                                                    } elseif (!empty($details_yomya['lv2'])) {
                                                        $lvt = $details_yomya['lv2'];
                                                        $lvt1 = $details_yomya['lv1'];
                                                    } elseif (!empty($details_yomya['lv1'])) {
                                                        $lvt = $details_yomya['lv1'];
                                                    }
                                                    /* $id_account_lv3 = "";
                                                    $id_account_lv3 = $details_yomya['lv3'];
                                                    $id_account_lv4 = $details_yomya['lv4']; */

                                                    if ($count_to == 1) {
                                                        $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;إلى حـ/ ";
                                                    } elseif ($count_to > 1) {
                                                        if ($count_to_num == 0) {
                                                            $name_from_or_to_m = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>إلى مذكورين/</b> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                            $name_from_or_to = "";
                                                        } else {
                                                            $name_from_or_to_m = "";
                                                            $name_from_or_to = "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                                        }
                                                    }
                                                    $count_to_num++;
                                                }
                                                $name_account_lv3 = "";
                                                if (isset($lvt1) && !empty($lvt1)) {
                                                    $sqlaccounts = $con->prepare("SELECT * from level_acc where lv = '$lvt1' ");
                                                    $sqlaccounts->execute();
                                                    $accounts = $sqlaccounts->fetchAll();
                                                    foreach ($accounts as $account) {
                                                        $name_account_lv3 = $account['name_account'];
                                                    }
                                                }
                                                $name_account_lv4 = "";
                                                if (isset($lvt) && !empty($lvt)) {
                                                    $name_account_lv4 = " - " . getName("level_acc", "lv ", $lvt, "name_account");
                                                }
                                                echo $name_from_or_to . $name_from_or_to_m . $name_account_lv3 . $name_account_lv4;
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo $yomya['payan'];
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $count_from_num = 0;
                                            $allmadeen_thes = 0;
                                            $alldaen_thes = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];

                                                if($id_currency_code != 1){
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                }
                                                if ($from_or_to == 1) {
                                                    $madeen = $cash;
                                                    $allmadeen_thes += $madeen;
                                                    if ($count_from == 1) {
                                                        echo $madeen. ' '. $currency_code;
                                                    } elseif ($count_from > 1) {
                                                        if ($count_from_num == 0) {
                                                            echo "<br>" . $madeen. ' '. $currency_code;
                                                        } else {
                                                            echo "<br>" . $madeen. ' '. $currency_code;
                                                        }
                                                    }
                                                    $count_from_num++;
                                                }
                                            }
                                            if ($count_to > 1) {
                                                $new_count_to = $count_to + 1;
                                            } else {
                                                $new_count_to = $count_to;
                                            }
                                            for ($i = 0; $i <= $new_count_to; $i++) {
                                                echo "<br>";
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo "<b>" . $allmadeen_thes. ' '. $currency_code . "</b>";
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            $count_to_num = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                if ($from_or_to == 2) {
                                                    $daen = $cash;
                                                    $alldaen_thes += $daen;
                                                    if ($count_to == 1) {
                                                        echo "<br>" . $daen. ' '. $currency_code;
                                                    } elseif ($count_to > 1) {
                                                        if ($count_to_num == 0) {
                                                            for ($i = 0; $i <= $count_from; $i++) {
                                                                echo "<br>";
                                                            }
                                                            echo "<br>" . $daen. ' '. $currency_code;
                                                        } else {
                                                            echo "<br>" . $daen. ' '. $currency_code;
                                                        }
                                                    }
                                                    $count_to_num++;
                                                }
                                            }
                                            ?>
                                            <hr>
                                            <?php
                                            echo "<b>" . $alldaen_thes. ' '. $currency_code . "</b>";
                                            ?>
                                        </td>
                                    </tr>
                                <?php
                                }
                                ?>
                            </tbody>
                            <tfoot>
                                <tr class="success">
                                    <td>

                                    </td>
                                    <td>

                                    </td>
                                    <th>
                                        الإجمالى
                                    </th>
                                    <th>
                                        <?php echo $allmadeen. ' '. $currency_code ?>
                                    </th>
                                    <th>
                                        <?php echo $alldaen. ' '. $currency_code ?>
                                    </th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </section>
                <!-- start -->
                <section class="container">
                    <!-- <a href="#"><i class="fa fa-print"></i></a> -->
                    <div class="panel panel-primary text-center table-responsive mb-5">
                        <!-- Default panel contents -->
                        <div class="panel-heading">تعديل قيد يومية
                            <button form="add_yomya" type="submit" value="1" name="go_state" class="btn btn-success btn-sm">
                                حفظ القيد <i class=" fa-solid fa-floppy-disk"> </i>
                            </button>
                        </div>
                        <?php
                        $sqlyomya = $con->prepare("SELECT * from yomya where registration_number = ? ");
                        $sqlyomya->execute(array($last_idyomya));
                        $yomyas = $sqlyomya->fetch();
                        // foreach ($yomyas as $yomya) {
                        ?>
                        <form id="add_yomya" class="was-validated" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=insert_yomya" method="post">
                            <input type="hidden" name="id_yomya" id="id_yomya" value="<?=$yomya['id_yomya']?>">
                            <div class="container-fluid text-center">
                                <div class="row">
                                    <div class="col-sm-12">
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <input type="date" name="mydate" id="mydate" class="form-control" value="<?=$yomya['dateinsert'] ?>" placeholder="التاريخ" required>
                                            <label for="mydate" class="form-label">التاريخ</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <input type="text" readonly name="idyomya" id="idyomya" class="form-control" value="<?=$yomya['registration_number']?>" placeholder="التاريخ" required>
                                            <label for="mydate" class="form-label">رقم القيد</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <select name="currency" id="currency" class="form-select" required>
                                                <?php
                                                    $id_currency = getId('details_yomya', 'id_yomya', $id_yomya, 'id_currency_codes');
                                                    // Query to retrieve agents from the agents table
                                                    $agentsQuery = "SELECT * FROM currency where ID = $id_currency";
                                                    $agentsResult = $con->query($agentsQuery);
                                                    while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                                            echo "<option value='{$agent['ID']}'>{$agent['name_ar']}{$agent['currency_shortcut']}</option>";
                                                    }
                                                    $agentsQuery = "SELECT * FROM currency where ID != $id_currency";
                                                    $agentsResult = $con->query($agentsQuery);
                                                    while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                                            echo "<option value='{$agent['ID']}'>{$agent['name_ar']}{$agent['currency_shortcut']}</option>";
                                                    }
                                                ?>
                                            </select>
                                            <label for="currency" class="form-label">العملة:</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-12 mt-4 mb-4">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="ml7oza" id="ml7oza" value="<?=$yomya['payan']?>" placeholder="البيان" required>
                                            <label for="ml7oza" class="form-label">البيان</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-10">
                                        <label for="lv3f" class="input-group-text">من حساب</label>
                                    </div>
                                    <div class="col-sm-2">
                                        <label for="cash_f" class="input-group-text">الملبغ</label>
                                    </div>
                                    <?php
                                    $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1 ");
                                    $sqldetails_yomya->execute();
                                    $details_yomyas = $sqldetails_yomya->fetchAll();
                                    $count_yomyas = $sqldetails_yomya->rowCount();
                                    $count_from = 0;
                                    $count_to = 0;
                                    $cc_f = 0;

                                    foreach ($details_yomyas as $details_yomya) {

                                        if (!empty($details_yomya['lv5'])) {
                                            $lvf = $details_yomya['lv5'];
                                        } elseif (!empty($details_yomya['lv4'])) {
                                            $lvf = $details_yomya['lv4'];
                                        } elseif (!empty($details_yomya['lv3'])) {
                                            $lvf = $details_yomya['lv3'];
                                        } elseif (!empty($details_yomya['lv2'])) {
                                            $lvf = $details_yomya['lv2'];
                                        } elseif (!empty($details_yomya['lv1'])) {
                                            $lvf = $details_yomya['lv1'];
                                        }
                                        
                                        $cc_f = $details_yomya['cost_center_code'];

                                        $cash_f = $details_yomya['id_currency_codes'] == 1 ? $details_yomya['cash'] : $details_yomya['amountSold'];
                                        ?>
                                        <div class="col-sm-1">
                                        </div>
                                        <div class="col-sm-5" >
                                            <div class="form-group mb-4">
                                                <select name="lv3f[<?php ?>]" class="form-control lv3f" required>
                                                    <?php
                                                    $stmt = $con->prepare("SELECT * FROM level_acc WHERE lv = ? ");
                                                    $stmt->execute([$lvf]);
                                                    $namne_lvs = $stmt->fetchAll();
                                                    foreach ($namne_lvs as $namne_lv) {
                                                        ?>
                                                            <option value="<?=$namne_lv['ID']?>"><?=$namne_lv['name_account']?></option>
                                                        <?php
                                                    }
                                                    ?>
                                                    <option value="off">إلغاء الحساب</option>
                                                    <?php
                                                    $topLevel = 1;
                                                    $topAccounts = getSubAccounts(0, $topLevel);
                                                    printAccountOptions($topAccounts, $topLevel);
                                                    ?>
                                                </select>
                                                <br>
                                            </div>
                                        </div>
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <select name="ccf[]" class="form-control">
                                                    <?php
                                                    $stmt = $con->prepare("SELECT * FROM cost_centers WHERE cost_center_code = ? ");
                                                    $stmt->execute([$cc_f]);
                                                    $namne_lvs = $stmt->fetchAll();
                                                    $count_lvs = $stmt->rowCount();
                                                    foreach ($namne_lvs as $namne_lv) {
                                                        ?>
                                                            <option value="<?=$namne_lv['ID']?>"><?=$namne_lv['cost_center_name']?></option>
                                                        <?php
                                                    }
                                                    ?>
                                                        <option value="off">الغاء مركز التكلفة</option>
                                                    <?php
                                                    $topLevel = 1;
                                                    $topAccounts = getSubAccountsCost(0, $topLevel);
                                                    printAccountOptionsCost($topAccounts, $topLevel);
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-sm-2">
                                            <div class="form-group">
                                                <input type="text" class="form-control" name="cash_f[]" id="cash_f" value="<?=$cash_f?>" placeholder="المبلغ" >
                                            </div>
                                        </div>
                                        <?php
                                    }
                                    ?>
                                    <div class="col-sm-1">
                                        <span id="new_lvfrom" class="btn btn-success"><i class="fa fa-plus"></i></span>
                                    </div>
                                    <div class="col-sm-5" id="div_lv3f">
                                        <!-- <div class="form-group mb-4">
                                            <select id="lv3f" name="lv3f[]" class="form-control lv3f" required>
                                                <option value="">إختر الحساب</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccounts(0, $topLevel);
                                                printAccountOptions($topAccounts, $topLevel);
                                                ?>
                                            </select>
                                            <br>
                                        </div> -->
                                    </div>
                                    <div class="col-sm-4" id="div_ccf">
                                        <!-- <div class="form-group">
                                            <select id="ccf" name="ccf[]" class="form-control">
                                                <option value="">مركز التكلفة</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccountsCost(0, $topLevel);
                                                printAccountOptionsCost($topAccounts, $topLevel);
                                                ?>
                                            </select>
                                        </div> -->
                                    </div>
                                    <div class="col-sm-2" id="div_cashfrom">
                                        <!-- <div class="form-group">
                                            <input type="text" class="form-control" name="cash_f[]" id="cash_f" placeholder="المبلغ" required>
                                        </div> -->
                                    </div>

                                    <div class="col-sm-10">
                                        <label for="lv3t" class="input-group-text">الى حساب</label>
                                    </div>
                                    <div class="col-sm-2">
                                        <label for="cash_f" class="input-group-text">الملبغ</label>
                                    </div>
                                    <?php
                                    $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 2 ");
                                    $sqldetails_yomya->execute();
                                    $details_yomyas = $sqldetails_yomya->fetchAll();
                                    $count_yomyas = $sqldetails_yomya->rowCount();
                                    $count_from = 0;
                                    $count_to = 0;
                                    $cc_f = 0;
                                    foreach ($details_yomyas as $details_yomya) {

                                        if (!empty($details_yomya['lv5'])) {
                                            $lvt = $details_yomya['lv5'];
                                        } elseif (!empty($details_yomya['lv4'])) {
                                            $lvt = $details_yomya['lv4'];
                                        } elseif (!empty($details_yomya['lv3'])) {
                                            $lvt = $details_yomya['lv3'];
                                        } elseif (!empty($details_yomya['lv2'])) {
                                            $lvt = $details_yomya['lv2'];
                                        } elseif (!empty($details_yomya['lv1'])) {
                                            $lvt = $details_yomya['lv1'];
                                        }
                                        
                                        $cc_t = $details_yomya['cost_center_code'];

                                        $cash_t = $details_yomya['id_currency_codes'] == 1 ? $details_yomya['cash'] : $details_yomya['amountSold'];
                                        ?>
                                        <div class="col-sm-2">
                                        </div>
                                        <div class="col-sm-4">
                                            <div class="form-group mb-4">
                                                <select name="lv3t[]" class="form-control lv3t" required>
                                                    <?php
                                                    $stmt = $con->prepare("SELECT * FROM level_acc WHERE lv = ? ");
                                                    $stmt->execute([$lvt]);
                                                    $namne_lvs = $stmt->fetchAll();
                                                    foreach ($namne_lvs as $namne_lv) {
                                                        ?>
                                                            <option value="<?=$namne_lv['ID']?>"><?=$namne_lv['name_account']?></option>
                                                        <?php
                                                    }
                                                    ?>
                                                    <option value="off">إلغاء الحساب</option>
                                                    <?php
                                                    $topLevel = 1;
                                                    $topAccounts = getSubAccounts(0, $topLevel);
                                                    printAccountOptions($topAccounts, $topLevel);
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <select name="cct[]" class="form-control">
                                                    <?php
                                                    $stmt = $con->prepare("SELECT * FROM cost_centers WHERE cost_center_code = ? ");
                                                    $stmt->execute([$cc_t]);
                                                    $namne_lvs = $stmt->fetchAll();
                                                    $count_lvs = $stmt->rowCount();
                                                    foreach ($namne_lvs as $namne_lv) {
                                                        ?>
                                                            <option value="<?=$namne_lv['ID']?>"><?=$namne_lv['cost_center_name']?></option>
                                                        <?php
                                                    }
                                                    ?>
                                                        <option value="off">الغاء مركز التكلفة</option>
                                                    <?php
                                                    $topLevel = 1;
                                                    $topAccounts = getSubAccountsCost(0, $topLevel);
                                                    printAccountOptionsCost($topAccounts, $topLevel);
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-sm-2">
                                            <div class="form-group">
                                                <input type="text" class="form-control" name="cash_t[]" id="cash_t" value="<?=$cash_t?>" placeholder="المبلغ" >
                                            </div>
                                        </div>
                                        <?php
                                    }
                                    ?>
                                    <div class="col-sm-1">
                                    </div>
                                    <div class="col-sm-1">
                                        <span id="new_lvto" class="btn btn-success"><i class="fa fa-plus"></i></span>
                                    </div>
                                    <div class="col-sm-4" id="div_lv3t">
                                        <?php
                                        /* $topLevel = 1;
                                        $topAccounts = getSubAccounts(0, $topLevel);
                                        printAccountOptions($topAccounts, $topLevel); */
                                        ?>
                                    </div>
                                    <div class="col-sm-4"  id="div_cct">
                                        <?php
                                        /* $topLevel = 1;
                                        $topAccounts = getSubAccountsCost(0, $topLevel);
                                        printAccountOptionsCost($topAccounts, $topLevel); */
                                        ?>
                                    </div>
                                    <div class="col-sm-2" id="div_cashto">
                                        
                                    </div>

                                    <script>
                                        function initializeSelect2(selector) {
                                            $(selector).select2({
                                                dir: 'rtl',
                                                width: '100%'
                                            });
                                        }
                                        $(document).ready(function() {
                                            initializeSelect2('.lv3f');
                                            initializeSelect2('.lv3t');
                                        });
                                    // var counter = 0;
                                    <?php
                                    $topLevel = 1;
                                    $topAccounts = getSubAccounts(0, $topLevel);
                                    $displayedAccounts = [];
                                    ob_start();
                                    printAccountOptions($topAccounts, $topLevel, $displayedAccounts);
                                    $printedOptions = ob_get_clean();

                                    $topLevel = 1;
                                    $topAccounts = getSubAccountsCost(0, $topLevel);
                                    $displayedAccounts = [];
                                    ob_start();
                                    printAccountOptionsCost($topAccounts, $topLevel, $displayedAccounts);
                                    $printedOptionsCost = ob_get_clean();
                                    ?>
                                        $("#new_lvfrom").click(function() {
                                            $("#div_lv3f").append('<div class="mt-4"><select name="lv3f[]" class="form-control lv3f"> <option value="">إختر الحساب</option><?php echo addslashes($printedOptions); ?></select></div>');
                                            $("#div_ccf").append('<div class="mt-4"><select name="ccf[]" class="form-control ccf"> <option value="">إختر مركز التكلفة</option><?php echo addslashes($printedOptionsCost); ?></select></div>');
                                            $("#div_cashfrom").append('<br><input type="text" name="cash_f[]" class="form-control " placeholder="المبلغ" />');
                                            // ++counter
                                            // updateEvents($("#div_lv3f"));
                                            initializeSelect2('.lv3f');
                                        });
                                        $("#new_lvto").click(function() {
                                            $("#div_lv3t").append('<div class="mt-4"><select name="lv3t[]" class="form-control lv3t"> <option value="">إختر الحساب</option><?php echo addslashes($printedOptions); ?></select></div>');
                                            $("#div_cct").append('<div class="mt-4"><select name="cct[]" class="form-control cct"> <option value="">إختر مركز التكلفة</option><?php echo addslashes($printedOptionsCost); ?></select></div>');
                                            
                                             $("#div_cashto").append('<br><input type="text" name="cash_t[]" class="form-control " placeholder="المبلغ" />');
                                            // ++counter
                                            initializeSelect2('.lv3t');
                                        });
                                    </script>
                                    <br>
                                    <br>
                                    <div class="col-md-8">

                                    </div>
                                    <div class="col-md-4 m-3">
                                        <div class="form-group">
                                            <input class="form-control btn btn-primary" type="submit" value="حفظ" name="add">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <?php 
                        // }
                        ?>
                    </div>
                </section>
                <!-- end -->
                <?php

            } elseif ($do == "insert_yomya") {
                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    $idyomya = $_POST['idyomya'] ?? null;
                    $id_yomya = $_POST['id_yomya'] ?? null;
                    $nameErr = array();
                    if (empty($_POST["lv3f"])) {
                        $nameErr[] = "برجاء إختيار حساب المدين";
                    }
                   /*  if (empty($_POST["lv4f"])) {
                        $nameErr[] = "برجاء إختيار الحساب المدين تحليلي";
                    } */
                    if (empty($_POST["lv3t"])) {
                        $nameErr[] = "برجاء إختيار حساب الدائن";
                    }
                    /* if (empty($_POST["lv4t"])) {
                        $nameErr[] = "برجاء إختيار الحساب الدائن تحليلي";
                    } */
                    if (empty($_POST["cash_f"])) {
                        $nameErr[] = "برجاء إدخال المبلغ";
                    }
                    if (empty($_POST["cash_t"])) {
                        $nameErr[] = "برجاء إدخال المبلغ";
                    }
                    if (empty($_POST["ml7oza"])) {
                        $nameErr[] = "برجاء إدخال البيان";
                    }
                    $cash_f = $_POST['cash_f'];
                    $cash_t = $_POST['cash_t'];
                    $id_currency_codes = $_POST['currency'];
                    // $currencyExchangeRate = $_POST['currencyExchangeRate'];
                    $all_cash_f = 0;
                    $all_cash_t = 0;

                    foreach ($cash_f as $key => $cashf) {
                        if (!empty($cashf)) {
                            $all_cash_f += $cashf;
                        }
                    }
                    foreach ($cash_t as $key => $casht) {
                        if (!empty($casht)) {
                            $all_cash_t += $casht;
                        }
                    }
                    if ($all_cash_f != $all_cash_t) {
                        $nameErr[] = "برجاء مراجعة المبلغ لأن الجانب المدين لا يساوى الجانب الدائن";
                    } else {
                        $all_cash = $all_cash_f;
                    }

                    if (empty($nameErr)) {
                        $mydate = $_POST['mydate'];
                        $cash_f = $_POST['cash_f'];
                        $cash_t = $_POST['cash_t'];
                        $ml7oza = $_POST['ml7oza'];
                        $ccf = $_POST['ccf'];
                        $cct = $_POST['cct'];
                        $currencyExchangeRate = getId('currency','ID',$id_currency_codes,'currencyExchangeRate');

                        $transaction = new YomyaTransaction($con);

                        if (empty($id_yomya)) {
                            $getstm = $transaction->insertTransaction($mydate, $all_cash, $ml7oza, $sessionUser, $_POST['cash_f'], $_POST['lv3f'], $_POST['cash_t'], $_POST['lv3t'], $id_currency_codes, $currencyExchangeRate, $ccf, $cct);

                            if ($getstm) {
                                $msg = array();
                                $msg[] = "تم إضافة " . "القيد رقم " . $getstm ." بنجاح فى قاعدة البيانات";
                                header('location:  reports_account.php?Manage=tree&do=add_yomya&id=' . $id_yomya . '&success[]=' . $msg[0]);
                            } else {
                                $theMsg = "هناك خطاء فى الترحيل برجاء الرجوع للمبرمج";
                                redirectlink($theMsg, $url = 'reports_account.php?Manage=tree&do=add_yomya&err[]=' . $msgErr[0], $name = "الرجوع للخلف", $seconds = 15);
                            }
                        } else {
                            $getstm = $transaction->updateTransaction($id_yomya, $mydate, $all_cash, $ml7oza, $sessionUser, $_POST['cash_f'], $_POST['lv3f'], $_POST['cash_t'], $_POST['lv3t'], $id_currency_codes, $currencyExchangeRate, $ccf, $cct);

                            if ($getstm) {
                                $msg = array();
                                $msg[] = "تم تعديل " . "القيد رقم " . $getstm ." بنجاح فى قاعدة البيانات";
                                header('location:  reports_account.php?Manage=tree&do=edit_yomya&id=' . $idyomya . '&success[]=' . $msg[0]);
                            } else {
                                $theMsg = "هناك خطاء فى الترحيل برجاء الرجوع للمبرمج";
                                redirectlink($theMsg, $url = 'reports_account.php?Manage=tree&do=edit_yomya&id='.$idyomya, $name = "الرجوع للخلف", $seconds = 15);
                            }
                        }

                    } else {
                        $num = count($nameErr);
                        for ($i = 0; $i < $num; $i++) {
                            echo '&err[' . $i . ']=' . $nameErr[$i];
                        }
                        header('location:  reports_account.php?Manage=tree&do=add_yomya&err[]=' . $nameErr[0] . '&err[]=' . $nameErr[1] . '&err[]=' . $nameErr[2] . '&err[]=' . $nameErr[3] . '&err[]=' . $nameErr[4] . '&err[]=' . $nameErr[5]);
                    }
                }
            } elseif ($do == "add_sanad") {
                if (isset($_GET['err']) && !empty($_GET['err'])) {
                    $nameErr = array();
                    $nameErr = $_GET['err'];
                    foreach ($nameErr as $value) {
                        if (!empty($value)) {
                            echo '<div class="alert alert-danger">';
                            echo $value;
                            echo '</div>';
                        }
                    }
                }
                if (isset($_GET['success']) && !empty($_GET['success'])) {
                    $nameSuc = array();
                    $nameSuc = $_GET['success'];
                    foreach ($nameSuc as $value) {
                        if (!empty($value)) {
                            echo '<div class="alert alert-success">';
                            echo $value;
                            echo '</div>';
                        }
                    }
                }
                $currentYear = date("Y");
                if (isset($_GET['id']) && $_GET['id'] == 1) {
                    ?>
                    <section class="container">
                    <!-- <a href="#"><i class="fa fa-print"></i></a> -->
                    <div class="panel panel-primary text-center table-responsive mb-5">
                        <!-- Default panel contents -->
                        <div class="panel-heading">إضافة إذن صرف
                            <button form="add_sanad" type="submit" value="1" name="go_state" class="btn btn-success btn-sm">
                                حفظ <i class=" fa-solid fa-floppy-disk"> </i>
                            </button>
                        </div>
                        <form id="add_sanad" class="was-validated" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=insert_sanad" method="post">
                        <input type="hidden" name="id_sanad_type" value="1">
                            <div class="container-fluid text-center">
                                <div class="row">
                                    <div class="col-sm-12">

                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <input type="date" name="mydate" id="mydate" class="form-control" value="<?php echo $thisyear . "-" . $thismon . "-" . $thisday ?>" placeholder="التاريخ" required>
                                            <label for="mydate" class="form-label">التاريخ</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-2">
                                        <div class="form-floating">
                                            <input type="text" readonly name="lastidyomya" id="lastidyomya" class="form-control" value="<?php echo $last_ID_yomya + 7 ?>" placeholder="التاريخ" required>
                                            <label for="lastidyomya" class="form-label">رقم القيد</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-2">
                                        <!-- <div class="form-floating">
                                            <input type="text" readonly name="lastidyomya" id="lastidyomya" class="form-control" value="" placeholder="التاريخ" required>
                                            <label for="mydate" class="form-label">رقم السند</label>
                                        </div> -->
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <select name="currency" id="currency" class="form-select" required>
                                                <?php
                                                // Query to retrieve agents from the agents table
                                                $agentsQuery = "SELECT * FROM currency";
                                                $agentsResult = $con->query($agentsQuery);
                                                while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                                        echo "<option value='{$agent['ID']}'>{$agent['name_ar']}{$agent['currency_shortcut']}</option>";
                                                }
                                                ?>
                                            </select>
                                            <label for="currency" class="form-label">العملة:</label>
                                        </div>
                                    </div>
                                    <!-- <div class="col-sm-2">
                                        <div class="form-floating">
                                            <input type="text" name="currencyExchangeRate" id="currencyExchangeRate" class="form-control"  placeholder="سعر الصرف">
                                            <label for="currencyExchangeRate" class="form-label">سعر الصرف</label>
                                        </div>
                                    </div> -->
                                    
                                    <div class="col-sm-4 mt-4 mb-4" id="div_cashfrom">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="name" id="name" placeholder="صرفنا الى السيد" required>
                                            <label for="name" class="form-label">صرفنا الى السيد</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-4 mt-4 mb-4" id="div_cashfrom">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="cash" id="cash" placeholder="المبلغ" required>
                                            <label for="cash" class="form-label">المبلغ</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-3 mt-4 ">
                                        <div class="form-floating">
                                            <select name="payment_methods" id="payment_methods" class="form-select" required>
                                                <?php
                                                // Query to retrieve agents from the agents table
                                                $agentsQuery = "SELECT * FROM payment_methods";
                                                $agentsResult = $con->query($agentsQuery);
                                                while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                                    echo "<option value='{$agent['ID']}'>{$agent['name_payment']}</option>";
                                                }
                                                ?>
                                            </select>
                                            <label for="payment_methods" class="form-label">طريقة الدفع:</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-12 mt-4 mb-4">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="ml7oza" id="ml7oza" placeholder="ذلك عن " required>
                                            <label for="ml7oza" class="form-label">وذلك عن </label>
                                        </div>
                                    </div>
                                    <div class="col-sm-6 mt-4 mb-4" id="div_cashfrom">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="NO_check" id="NO_check" placeholder="رقم الشيك">
                                            <label for="NO_check" class="form-label">رقم الشيك</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-6 mt-4 mb-4" id="div_cashfrom">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="national_ID" id="national_ID" placeholder="الرقم القومى">
                                            <label for="national_ID" class="form-label">الرقم القومى</label>
                                        </div>
                                    </div>
                                    <!-- <div class="col-sm-1">
                                        <span id="new_lvfrom" class="btn btn-success"><i class="fa fa-plus"></i></span>
                                    </div> -->
                                    <div class="col-sm-3" id="div_lv3f">
                                        <div class="form-group mb-4">
                                            <select id="lv3f" name="lv3f" class="form-control lv3f" required>
                                                <option value="">إختر الحساب</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccounts(3, 2);
                                                $topAccounts1 = getSubAccounts(6, 2);
                                                printAccountOptions($topAccounts, 2);
                                                printAccountOptions($topAccounts1, 2);
                                                ?>
                                            </select>
                                            <br>
                                        </div>
                                        
                                    </div>
                                    <div class="col-sm-3" id="div_ccf">
                                        <div class="form-group">
                                            <select id="ccf" name="ccf" class="form-control">
                                                <option value="">مركز التكلفة</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccountsCost(0, $topLevel);
                                                printAccountOptionsCost($topAccounts, $topLevel);
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-12">
                                        <label for="lv3t" class="input-group-text">الى حساب</label>
                                    </div>
                                    <div class="col-sm-1">
                                    </div>
                                    <!-- <div class="col-sm-1">
                                        <span id="new_lvto" class="btn btn-success"><i class="fa fa-plus"></i></span>
                                    </div> -->
                                    <div class="col-sm-4" id="div_lv3t">
                                        <div class="form-group">
                                            <select id="lv3t" name="lv3t" class="form-control lv3t" required>
                                                <option value="">إختر الحساب</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccounts(12, 3);
                                                $topAccounts1 = getSubAccounts(13, 3);
                                                printAccountOptions($topAccounts, 3);
                                                printAccountOptions($topAccounts1, 3);
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-4"  id="div_cct">
                                        <div class="form-group">
                                            
                                        </div>
                                    </div>
                                    <!-- <div class="col-sm-1">
                                    </div> -->
                                    <?php
                                    ?>
                                    <script>
                                        function initializeSelect2(selector) {
                                            $(selector).select2({
                                                dir: 'rtl',
                                                width: '100%'
                                            });
                                        }
                                        $(document).ready(function() {
                                            initializeSelect2('.lv3f');
                                            initializeSelect2('.lv3t');
                                        });
                                    </script>
                                    <br>
                                    <br>
                                    <div class="col-md-4 m-3">
                                        <div class="form-group">
                                            <input class="form-control btn btn-primary" type="submit" value="حفظ" name="add">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    </section>
                    <?php
                } elseif (isset($_GET['id']) && $_GET['id'] == 2) {
                    ?>
                    <section class="container">
                    <!-- <a href="#"><i class="fa fa-print"></i></a> -->
                    <div class="panel panel-primary text-center table-responsive mb-5">
                        <!-- Default panel contents -->
                        <div class="panel-heading">إضافة إذن قبض
                            <button form="add_sanad" type="submit" value="1" name="go_state" class="btn btn-success btn-sm">
                                حفظ <i class=" fa-solid fa-floppy-disk"> </i>
                            </button>
                        </div>
                        <form id="add_sanad" class="was-validated" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=insert_sanad" method="post">
                            <input type="hidden" name="id_sanad_type" value="2">
                            <div class="container-fluid text-center">
                                <div class="row">
                                    <div class="col-sm-12">
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <input type="date" name="mydate" id="mydate" class="form-control" value="<?php echo $thisyear . "-" . $thismon . "-" . $thisday ?>" placeholder="التاريخ" required>
                                            <label for="mydate" class="form-label">التاريخ</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-2">
                                        <div class="form-floating">
                                            <input type="text" readonly name="lastidyomya" id="lastidyomya" class="form-control" value="<?php echo $last_ID_yomya + 7 ?>" placeholder="التاريخ" required>
                                            <label for="lastidyomya" class="form-label">رقم القيد</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-2">
                                    </div>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <select name="currency" id="currency" class="form-select" required>
                                                <?php
                                                // Query to retrieve agents from the agents table
                                                $agentsQuery = "SELECT * FROM currency";
                                                $agentsResult = $con->query($agentsQuery);
                                                while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                                        echo "<option value='{$agent['ID']}'>{$agent['name_ar']}{$agent['currency_shortcut']}</option>";
                                                }
                                                ?>
                                            </select>
                                            <label for="currency" class="form-label">العملة:</label>
                                        </div>
                                    </div>
                                    <!-- <div class="col-sm-2">
                                        <div class="form-floating">
                                            <input type="text" name="currencyExchangeRate" id="currencyExchangeRate" class="form-control"  placeholder="سعر الصرف">
                                            <label for="currencyExchangeRate" class="form-label">سعر الصرف</label>
                                        </div>
                                    </div> -->
                                    
                                    <div class="col-sm-4 mt-4 mb-4" id="div_cashfrom">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="name" id="name" placeholder="إستلمنا من السيد" required>
                                            <label for="name" class="form-label">إستلمنا من السيد</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-4 mt-4 mb-4" id="div_cashfrom">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="cash" id="cash" placeholder="المبلغ" required>
                                            <label for="cash" class="form-label">المبلغ</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-3 mt-4 ">
                                        <div class="form-floating">
                                            <select name="payment_methods" id="payment_methods" class="form-select" required>
                                                <?php
                                                // Query to retrieve agents from the agents table
                                                $agentsQuery = "SELECT * FROM payment_methods";
                                                $agentsResult = $con->query($agentsQuery);
                                                while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                                    echo "<option value='{$agent['ID']}'>{$agent['name_payment']}</option>";
                                                }
                                                ?>
                                            </select>
                                            <label for="payment_methods" class="form-label">طريقة الدفع:</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-12 mt-4 mb-4">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="ml7oza" id="ml7oza" placeholder="ذلك عن " required>
                                            <label for="ml7oza" class="form-label">وذلك عن </label>
                                        </div>
                                    </div>
                                    <div class="col-sm-6 mt-4 mb-4" id="div_cashfrom">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="NO_check" id="NO_check" placeholder="رقم الشيك">
                                            <label for="NO_check" class="form-label">رقم الشيك</label>
                                        </div>
                                    </div>
                                    <div class="col-sm-6 mt-4 mb-4" id="div_cashfrom">
                                        <div class="form-floating">
                                            <input type="text" class="form-control" name="national_ID" id="national_ID" placeholder="الرقم القومى">
                                            <label for="national_ID" class="form-label">الرقم القومى</label>
                                        </div>
                                    </div>
                                    <!-- <div class="col-sm-1">
                                        <span id="new_lvfrom" class="btn btn-success"><i class="fa fa-plus"></i></span>
                                    </div> -->
                                    <div class="col-sm-3" id="div_lv3f">
                                        <div class="form-group mb-4">
                                            <select id="lv3f" name="lv3f" class="form-control lv3f" required>
                                                <option value="">إختر الحساب</option>
                                                <?php
                                                $topLevel = 3;
                                                $topAccounts = getSubAccounts(12, $topLevel);
                                                $topAccounts1 = getSubAccounts(13, $topLevel);
                                                printAccountOptions($topAccounts, $topLevel);
                                                printAccountOptions($topAccounts1, $topLevel);
                                                ?>
                                            </select>
                                            <br>
                                        </div>
                                        
                                    </div>
                                    
                                    <div class="col-sm-12">
                                        <label for="lv3t" class="input-group-text">الى حساب</label>
                                    </div>
                                    <div class="col-sm-1">
                                    </div>
                                    <!-- <div class="col-sm-1">
                                        <span id="new_lvto" class="btn btn-success"><i class="fa fa-plus"></i></span>
                                    </div> -->
                                    <div class="col-sm-4" id="div_lv3t">
                                        <div class="form-group">
                                            <select id="lv3t" name="lv3t" class="form-control lv3t" required>
                                                <option value="">إختر الحساب</option>
                                                <?php
                                                $topLevel = 2;
                                                $topAccounts = getSubAccounts(2, $topLevel);
                                                $topAccounts1 = getSubAccounts(7, $topLevel);
                                                printAccountOptions($topAccounts, $topLevel);
                                                printAccountOptions($topAccounts1, $topLevel);
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-3" id="div_ccf">
                                        <div class="form-group">
                                            <select id="cct" name="cct" class="form-control">
                                                <option value="">مركز التكلفة</option>
                                                <?php
                                                $topLevel = 1;
                                                $topAccounts = getSubAccountsCost(0, $topLevel);
                                                printAccountOptionsCost($topAccounts, $topLevel);
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <!-- <div class="col-sm-4"  id="div_cct">
                                        <div class="form-group">
                                            
                                        </div>
                                    </div> -->
                                    <!-- <div class="col-sm-1">
                                    </div> -->
                                    <?php
                                    ?>
                                    <script>
                                        function initializeSelect2(selector) {
                                            $(selector).select2({
                                                dir: 'rtl',
                                                width: '100%'
                                            });
                                        }
                                        $(document).ready(function() {
                                            initializeSelect2('.lv3f');
                                            initializeSelect2('.lv3t');
                                        });
                                    </script>
                                    <br>
                                    <br>
                                    <div class="col-md-4 m-3">
                                        <div class="form-group">
                                            <input class="form-control btn btn-primary" type="submit" value="حفظ" name="add">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    </section>
                    <?php
                }
                ?>
                
                <!-- end -->
                <?php
            } elseif ($do == "insert_sanad") {
                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    $nameErr = array();
                    if (empty($_POST["lv3f"])) {
                        $nameErr[] = "برجاء إختيار حساب المدين";
                    }
                    if (empty($_POST["lv3t"])) {
                        $nameErr[] = "برجاء إختيار حساب الدائن";
                    }
                    if (empty($_POST["payment_methods"])) {
                        $nameErr[] = "برجاء إختيار طريقة الدفع";
                    }
                    if (empty($_POST["cash"])) {
                        $nameErr[] = "برجاء إدخال المبلغ";
                    }
                    if (empty($_POST["ml7oza"])) {
                        $nameErr[] = "برجاء إدخال بيان الاذن";
                    }
                    $cash= $_POST['cash'];
                    $id_currency_codes = $_POST['currency'];
                    // $currencyExchangeRate = $_POST['currencyExchangeRate'];
                    $all_cash_f = $cash;
                    $all_cash_t = $cash;
                    $all_cash = $cash;

                    if (empty($nameErr)) {
                        $id_sanad_type= $_POST['id_sanad_type'];
                        $lv3f = $_POST['lv3f'];
                        $lv3t = $_POST['lv3t'];
                        $mydate = $_POST['mydate'];
                        $name = $_POST['name'];
                        $payment_methods = $_POST['payment_methods'];
                        $NO_check = $_POST['NO_check'];
                        $national_ID = $_POST['national_ID'];
                        $cash = $_POST['cash'];
                        $ml7oza = $_POST['ml7oza'];

                        if ($id_sanad_type == 1) {
                            $ccf = $_POST['ccf'];
                            $cct = NULL;
                        } elseif ($id_sanad_type == 2) {
                            $reservation_id = getId('reservations','id_cost_center',$_POST['cct'],'id');
                            if ($reservation_id > 0) {
                                updatepayment_status($reservation_id);
                            }
                            $cct = $_POST['cct'];
                            $ccf = NULL;
                        }

                        $currencyExchangeRate = getId('currency','ID',$id_currency_codes,'currencyExchangeRate');
                        $currencyExchangeRateUSD = getId('currency','ID', 2,'currencyExchangeRate');
                        if ($currencyExchangeRateUSD > 0) {
                            $currencyExchangeRateEGP = 1 / $currencyExchangeRateUSD;
                        }
                        
                        if ($id_currency_codes != 1 ) {
                            $amountSold = $cash;
                            $cashsanad = $cash * $currencyExchangeRate;
                        } else {
                            $currencyExchangeRate = $currencyExchangeRateEGP;
                            $amountSold = 0;
                            $cashsanad = $cash;
                        }
                        $transaction = new YomyaTransaction($con);
                        $id_yomya = $transaction->insertTransaction($mydate, $cash, $ml7oza, $sessionUser, [$cash], [$lv3f], [$cash], [$lv3t], $id_currency_codes, $currencyExchangeRate, [$ccf], [$cct]);
                        $getstm = $transaction->insertSanad(
                            $id_sanad_type,
                            $payment_methods,
                            $mydate,
                            $id_yomya,
                            $name,
                            $national_ID,
                            $id_currency_codes,
                            $amountSold,
                            $currencyExchangeRate,
                            $cashsanad,
                            $payan,
                            $NO_check,
                            $name_bank,
                            $sessionUser
                        );

                        if ($getstm) {
                            $msg = array();
                            $msg[] = "تم إضافة " . "الاذن بنجاح فى قاعدة البيانات";
                            header('location:  reports_account.php?Manage=tree&do=add_sanad&id='.$id_sanad_type.'&success[]=' . $msg[0]);
                        } else {

                            $theMsg = "هناك خطاء فى الترحيل برجاء الرجوع للمبرمج";
                            redirectlink($theMsg, $url = 'reports_account.php?Manage=tree&do=add_sanad&id='.$id_sanad_type, $name = "الرجوع للخلف", $seconds = 15);
                        }
                    } else {
                        $num = count($nameErr);
                        for ($i = 0; $i < $num; $i++) {
                            echo '&err[' . $i . ']=' . $nameErr[$i];
                        }
                        header('location:  reports_account.php?Manage=tree&do=add_sanad&id='.$id_sanad_type.'&err[]=' . $nameErr[0] . '&err[]=' . $nameErr[1] . '&err[]=' . $nameErr[2] . '&err[]=' . $nameErr[3] . '&err[]=' . $nameErr[4] . '&err[]=' . $nameErr[5]);
                    }
                }
            } elseif ($do == "add_ExchangeRate") {
                if (isset($_GET['err']) && !empty($_GET['err'])) {
                    $nameErr = array();
                    $nameErr = $_GET['err'];
                    foreach ($nameErr as $value) {
                        if (!empty($value)) {
                            echo '<div class="alert alert-danger">';
                            echo $value;
                            echo '</div>';
                        }
                    }
                }
                if (isset($_GET['success']) && !empty($_GET['success'])) {
                    $nameSuc = array();
                    $nameSuc = $_GET['success'];
                    foreach ($nameSuc as $value) {
                        if (!empty($value)) {
                            echo '<div class="alert alert-success">';
                            echo $value;
                            echo '</div>';
                        }
                    }
                }
                $currentYear = date("Y");
                    ?>
                    <section class="container">
                    <!-- <a href="#"><i class="fa fa-print"></i></a> -->
                    <div class="panel panel-primary text-center table-responsive mb-5">
                        <!-- Default panel contents -->
                        <div class="panel-heading">تغيير سعر صرف العملات
                            <button form="add_sanad" type="submit" value="1" name="go_state" class="btn btn-success btn-sm confirm">
                                حفظ <i class=" fa-solid fa-floppy-disk"> </i>
                            </button>
                        </div>
                        <form id="add_sanad" class="was-validated" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=insert_ExchangeRate" method="post">
                        <input type="hidden" name="id_sanad_type" value="1">
                        <input type="hidden" name="msgcon" id="msgcon" value="هل أنت متأكد من تغيير سعر الصرف">
                            <div class="container-fluid text-center">
                                <div class="row mt-2 mb-2">
                                    <!-- <div class="col-sm-3">
                                        <div class="form-floating">
                                            <select name="currency" id="currency" class="form-select" required>
                                                <?php
                                                $agentsQuery = "SELECT * FROM currency";
                                                $agentsResult = $con->query($agentsQuery);
                                                while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                                        echo "<option value='{$agent['ID']}'>{$agent['name_ar']}{$agent['currency_shortcut']}</option>";
                                                }
                                                ?>
                                            </select>
                                            <label for="currency" class="form-label">العملة:</label>
                                        </div>
                                    </div> -->
                                    <?php
                                    $agentsQuery = "SELECT * FROM currency";
                                    $agentsResult = $con->query($agentsQuery);
                                    while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                                        ?>
                                    <div class="col-sm-3">
                                        <div class="form-floating">
                                            <input type="number" class="form-control" id="currencyExchangeRate" step="0.001" name="currencyExchangeRate[<?=$agent['ID']?>]" value="<?=$agent['currencyExchangeRate']?>" placeholder="سعر الصرف">
                                            <label for="currencyExchangeRate" class="form-label"><?=$agent['name_ar'].$agent['currency_shortcut']?></label>
                                        </div>
                                    </div>
                                    <?php
                                        }
                                    ?>
                                </div>
                            </div>
                        </form>
                    </div>
                    </section>
                <!-- end -->
                <?php
            } elseif ($do == "insert_ExchangeRate") {
                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    $nameErr = array();
                    $currencyExchangeRate = $_POST['currencyExchangeRate'];
                    // var_dump($currencyExchangeRate);
                    if (empty($nameErr)) {
                        $successCount = 0; // عداد لتتبع عدد العمليات الناجحة
                        foreach ($_POST['currencyExchangeRate'] as $currencyID => $newCurrencyExchangeRate) {
                            // تحقق من أن القيمة المرسلة صحيحة
                            $newCurrencyExchangeRate = filter_var($newCurrencyExchangeRate, FILTER_VALIDATE_FLOAT);
                            if (empty($newCurrencyExchangeRate)) {
                                $newCurrencyExchangeRate = NULL;
                            }
                            if ($newCurrencyExchangeRate !== false) {
                                // تحديث القيمة في قاعدة البيانات لكل عملة باستخدام معرّف العملة
                                $updateQuery = "UPDATE currency SET currencyExchangeRate = :newCurrencyExchangeRate WHERE ID = :currencyID";
                                $statement = $con->prepare($updateQuery);
                                $statement->bindParam(':newCurrencyExchangeRate', $newCurrencyExchangeRate);
                                $statement->bindParam(':currencyID', $currencyID);
                                if ($statement->execute()) {
                                    $successCount++; // زيادة العداد في حالة النجاح
                                }
                            }
                        }
                        if ($successCount > 0) {
                            $theMsg = "تم تحديث سعر الصرف لـ" . $successCount . " عملة بنجاح.";
                            redirectlink($theMsg, $url = "reports_account.php?Manage=tree&do=add_ExchangeRate", $name = "تغير سعر الصرف", $seconds = 0);
                        } else {
                            $theMsg = "فشل في تحديث سعر الصرف لأي عملة.";
                            redirectlink($theMsg, $url = "reports_account.php?Manage=tree&do=add_ExchangeRate", $name = "تغير سعر الصرف", $seconds = 0);
                        }
                    } else {
                        $num = count($nameErr);
                        for ($i = 0; $i < $num; $i++) {
                            echo '&err[' . $i . ']=' . $nameErr[$i];
                        }
                        header('location:  reports_account.php?Manage=tree&do=add_ExchangeRate&err[]=' . $nameErr[0] . '&err[]=' . $nameErr[1] . '&err[]=' . $nameErr[2] . '&err[]=' . $nameErr[3] . '&err[]=' . $nameErr[4] . '&err[]=' . $nameErr[5]);
                    } 
                }
            } else {
                header('Location: index.php');
                exit();
            }
        }else {
            echo '<div class="alert alert-danger">ليس لديك صلاحية الدخول الى هذا القسم</div>';
            include $tpl . 'footer.php';
            exit();
        }
    } elseif ($Manage == "cost_centers") {
        if (checkadmin($_SESSION['admin'], 12, 2) == 1) {
            ?>
            <a href="?Manage=cost_centers&do=Add&lv=lv1" class="btn btn-primary">
                <i class="fa fa-plus"></i> اضافة مركز تكلفة
            </a>
            <a href="?Manage=cost_centers&do=costs" class="btn btn-primary">
                <i class="fa fa-cog"></i> تقرير مراكز التكلفة
            </a>
            <br />
            <br />
            <?php
            if ($do == 'costs') { 
                    ?>
                <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=cost_centers&do=costs" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                            </div>
                        </div>
                    </div>
                </form>
                <section class="container">
                    <div class="panel panel-primary text-center table-responsive">
                        <div class="panel-heading">تقرير مراكز التكلفة خلال الفترة من <?php echo $start_date . " : " . $end_date; ?></div>
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>
                                        الحساب
                                        <a data-bs-toggle="collapse" href=".code_accountlv3" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3" title='عرض المستوى الرابع'>
                                            <i class='fa fa-caret-down' style="font-size:18px"></i>
                                        </a>
                                    </th>
                                    <th>
                                        مدين
                                    </th>
                                    <th>
                                        دائن
                                    </th>
                                    <th>
                                        رصيد مرحل مدين
                                    </th>
                                    <th>
                                        رصيد مرحل دائن
                                    </th>
                                </tr>
                            </thead>
                            <tbody id="myTable">
                                <?php
                                $allcashfrom_end = 0;
                                $allcashfromto_end = 0;
                                $mora7lfrom = 0;
                                $mora7lto = 0;
                                $all_mora7lfrom = 0;
                                $all_mora7lto = 0;
                                $allcashfrom = 0;
                                $allcashfromto = 0;
                                $sqlaccounts = $con->prepare("SELECT * from cost_centers WHERE cost_center_up = 0 ");
                                $sqlaccounts->execute();
                                $accounts = $sqlaccounts->fetchAll();
                                foreach ($accounts as $account) {
                                    $ID = $account['ID'];
                                    $name_account = $account['cost_center_name'];
                                    $code_account = $account['cost_center_code'];
                                    // $nature_acc = $account['nature_account'];
                                    ?>
                                    <tr class="success" style="height: 40px;">
                                        <th style="text-align: right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <a href="reports_account.php?Manage=cost_centers&do=cost&id_cost=<?php echo $ID; ?>">
                                                م.ت / <?php echo $name_account; ?>
                                            </a>
                                            <a data-bs-toggle="collapse" href=".code_accountlv3_<?php echo $ID ?>" role="button" class="collapsed btn" aria-expanded="false" aria-controls="code_accountlv3_<?php echo $ID ?>" title='عرض المستوى الرابع'>
                                                <i class='fa fa-caret-down' style="font-size:18px"></i>
                                            </a>
                                        </th>
                                        <td>
                                            <?php
                                            $allcashfrom = getTotalCash($con, $ID, $start_date_sql, $end_date_sql, $code_account, 1);
                                            $allcashfrom_end += $allcashfrom;
                                            
                                            $allcashfromto = getTotalCash($con, $ID, $start_date_sql, $end_date_sql, $code_account, 2);
                                            $allcashfromto_end += $allcashfromto;
                                            
                                            echo $allcashfrom;
                                            
                                            if ($allcashfrom < $allcashfromto) {
                                                $mora7lto = $allcashfromto - $allcashfrom;
                                            }
                                            //echo $allcashfrom;
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            echo $allcashfromto;
                                            if ($allcashfromto < $allcashfrom) {
                                                $mora7lfrom = $allcashfrom - $allcashfromto;
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            if (isset($mora7lfrom) && $mora7lfrom >= 0) {
                                                echo $mora7lfrom;
                                                $all_mora7lfrom += $mora7lfrom;
                                                $mora7lfrom = 0;
                                            }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            if (isset($mora7lto) && $mora7lto >= 0) {
                                                echo $mora7lto;
                                                $all_mora7lto += $mora7lto;
                                                $mora7lto = 0;
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                    <?php
                                    $sqlaccounts = $con->prepare("SELECT * from cost_centers WHERE cost_center_up = $ID");
                                    $sqlaccounts->execute();
                                    $accounts = $sqlaccounts->fetchAll();
                                    foreach ($accounts as $account) {
                                        $IDf = $account['ID'];
                                        $name_account = $account['cost_center_name'];
                                        $cost_center_code = $account['cost_center_code'];
                                        // $nature_acc = $account['nature_account'];
                                            ?>
                                        <tr id="code_accountlv3_<?php echo $ID ?>" class="code_accountlv3_<?php echo $ID ?> code_accountlv3 table-danger collapse" aria-expanded="false" style="height: 40px;">
                                            <th style="text-align: right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                <a href="reports_account.php?Manage=cost_centers&do=cost&id_cost_f=<?php echo $IDf; ?>">
                                                    م.ت / <?php echo $name_account; ?>
                                                </a>
                                            </th>
                                            <td>
                                                <?php
                                                $sqldetails_yomya = $con->prepare("SELECT SUM(cash) AS 'cashfrom' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND cost_center_code = '$cost_center_code' AND from_or_to = 1");
                                                $sqldetails_yomya->execute();
                                                $details_yomyas = $sqldetails_yomya->fetchAll();
                                                //$countfrom = $sqldetails_yomya->rowCount();
                                                foreach ($details_yomyas as $details_yomya) {
                                                    $allcashfrom = $details_yomya['cashfrom'];
                                                    // $allcashfrom_end += $allcashfrom;
                                                }
                                                $sqldetails_yomya = $con->prepare("SELECT SUM(cash) AS 'cashto' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND cost_center_code = '$cost_center_code' AND from_or_to = 2");
                                                $sqldetails_yomya->execute();
                                                $details_yomyas = $sqldetails_yomya->fetchAll();
                                                //$countfrom = $sqldetails_yomya->rowCount();
                                                foreach ($details_yomyas as $details_yomya) {
                                                    $allcashfromto = $details_yomya['cashto'];
                                                    // $allcashfromto_end += $allcashfromto;
                                                }
                                                echo $allcashfrom;
                                                if ($allcashfrom < $allcashfromto) {
                                                    $mora7lto = $allcashfromto - $allcashfrom;
                                                }
                                                //echo $allcashfrom;
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                echo $allcashfromto;
                                                if ($allcashfromto < $allcashfrom) {
                                                    $mora7lfrom = $allcashfrom - $allcashfromto;
                                                }
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                if (isset($mora7lfrom) && $mora7lfrom >= 0) {
                                                    echo $mora7lfrom;
                                                    // $all_mora7lfrom += $mora7lfrom;
                                                    $mora7lfrom = 0;
                                                }
                                                ?>
                                            </td>
                                            <td>
                                                <?php
                                                if (isset($mora7lto) && $mora7lto >= 0) {
                                                    echo $mora7lto;
                                                    // $all_mora7lto += $mora7lto;
                                                    $mora7lto = 0;
                                                }
                                                ?>
                                            </td>
                                        </tr>
                                        <?php
                                    }
                                }
                                ?>
                                <tr>
                                    <th>
                                        الإجمالى
                                    </th>
                                    <th>
                                        <?php
                                        echo $allcashfrom_end;
                                        ?>
                                    </th>
                                    <th>
                                        <?php
                                        echo $allcashfromto_end;
                                        ?>
                                    </th>
                                    <th>
                                        <?php
                                        echo $all_mora7lfrom;
                                        ?>
                                    </th>
                                    <th>
                                        <?php
                                        echo $all_mora7lto;
                                        ?>
                                    </th>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </section>
                <?php
            } elseif ($do == 'Add') { // Add Page 
                if (!empty($_GET['lv'])) {
                    $level = $_GET['lv'];
                } else {
                    $level = "";
                }
                if (!empty($level) && $level == "lv1") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب مستوى أول</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=cost_centers&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="1" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } elseif (!empty($level) && $level == "lv2") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب مستوى ثاني</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=cost_centers&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="2" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv1" class="input-group-text">إختر المستوى الأول من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv1" name="lv1" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM level_acc WHERE lev_acc = 1 ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر الحساب</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_account']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حسابات حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } elseif (!empty($level) && $level == "lv3") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب مستوى ثالث</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=cost_centers&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="3" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv1" class="input-group-text">إختر المستوى الأول من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv1" name="lv1" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM level_acc WHERE lev_acc = 1 ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر الحساب</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_account']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حسابات حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv2" class="input-group-text">إختر المستوى الثانى من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv2" name="lv2" class="form-control" required="required">
                                            <option value="">إختر حساب المستوى الأول أولا</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } elseif (!empty($level) && $level == "lv4") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب مستوى رابع</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=cost_centers&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="4" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv1" class="input-group-text">إختر المستوى الأول من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv1" name="lv1" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM level_acc WHERE lev_acc = 1 ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر الحساب</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_account']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حسابات حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv2" class="input-group-text">إختر المستوى الثانى من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv2" name="lv2" class="form-control" required="required">
                                            <option value="">إختر حساب المستوى الأول أولا</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <div class="col-sm-10 col-md-6">
                                <div class="input-group-prepend">
                                    <label for="lv3" class="input-group-text">إختر المستوى الثالث من الحساب</label>
                                </div>
                                <div class="input-group mb-3">
                                    <select id="lv3" name="lv3" class="form-control" required="required">
                                        <option value="">إختر حساب المستوى الثانى أولا</option>
                                    </select>
                                </div>
                            </div>
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } elseif (!empty($level) && $level == "lv5") {
                ?>
                    <h3 class="input-group-text text-center">إضافة حساب تحليلى</h3>
                    <div class="container">
                        <form class="form-horizontal" action="?Manage=cost_centers&do=Insert" method="POST">
                            <input type="hidden" name="lvacc" value="5" class="form-control" />
                            <!-- <input type="hidden" name="phone" class="form-control" /> -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv1" class="input-group-text">إختر المستوى الأول من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv1" name="lv1" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM level_acc WHERE lev_acc = 1 ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر الحساب</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_account']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حسابات حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="lv2" class="input-group-text">إختر المستوى الثانى من الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="lv2" name="lv2" class="form-control" required="required">
                                            <option value="">إختر حساب المستوى الأول أولا</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <div class="col-sm-10 col-md-6">
                                <div class="input-group-prepend">
                                    <label for="lv3" class="input-group-text">إختر المستوى الثالث من الحساب</label>
                                </div>
                                <div class="input-group mb-3">
                                    <select id="lv3" name="lv3" class="form-control" required="required">
                                        <option value="">إختر حساب المستوى الثانى أولا</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-10 col-md-6">
                                <div class="input-group-prepend">
                                    <label for="lv4" class="input-group-text">إختر المستوى الرابع من الحساب</label>
                                </div>
                                <div class="input-group mb-3">
                                    <select id="lv4" name="lv4" class="form-control" required="required">
                                        <option value="">إختر حساب المستوى الثالث أولا</option>
                                    </select>
                                </div>
                            </div>
                            <!-- Start Username Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nameacc" class="input-group-text" id="basic-addon3">إسم الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <input type="text" name="nameacc" id="nameacc" class="form-control" aria-describedby="basic-addon3" autocomplete="off" required="required" placeholder="إدخل إسم الحساب" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Username Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="nature" class="input-group-text">طبيعة الحساب</label>
                                    </div>
                                    <div class="input-group mb-3">

                                        <select id="nature" name="nature" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM nature_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_nature']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>

                                </div>
                            </div>
                            <!-- End Standard Field -->
                            <!-- Start Standard Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-10 col-md-6">
                                    <div class="input-group-prepend">
                                        <label for="list_account" class="input-group-text">القائمة المالية</label>
                                    </div>
                                    <div class="input-group mb-3">
                                        <select id="list_account" name="list_account" class="form-control" required="required">
                                            <?php
                                            $stmt = $con->prepare("SELECT * FROM list_account ORDER BY ID ASC");
                                            $stmt->execute();
                                            $rows = $stmt->fetchAll();
                                            ?>
                                            <option value="">إختر</option>
                                            <?php
                                            if (!empty($rows)) {
                                                foreach ($rows as $row) {
                                            ?>
                                                    <option value="<?php echo $row['ID']; ?>"><?php echo $row['name_list']; ?></option>
                                                <?php
                                                }
                                            } else {
                                                ?>
                                                <option value="" disabled>لا يوجد قوائم حتى الأن</option>
                                            <?php
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <!-- End Standard Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <br />
                                    <input type="submit" value="إضافة" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                } else {
                    header('Location: index.php');
                    exit();
                }
            } elseif ($do == 'Insert') {
                // Insert Member Page
                if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    echo "<h1 class='text-center'>إدخال حساب</h1>";
                    echo "<div class='container'>";
                    // Get Variables From The Form
                    $lvacc = $_POST['lvacc'];
                    if (isset($_POST['lv1']) && !empty($_POST['lv1'])) {
                        $id_lv1 = $_POST['lv1'];
                    } else {
                        $id_lv1 = "";
                    }
                    if (isset($_POST['lv2']) && !empty($_POST['lv2'])) {
                        $id_lv2 = $_POST['lv2'];
                    } else {
                        $id_lv2 = "";
                    }
                    if (isset($_POST['lv3']) && !empty($_POST['lv3'])) {
                        $id_lv3 = $_POST['lv3'];
                    } else {
                        $id_lv3 = "";
                    }
                    if (isset($_POST['lv4']) && !empty($_POST['lv4'])) {
                        $id_lv4 = $_POST['lv4'];
                    } else {
                        $id_lv4 = "";
                    }
                    $nameacc = $_POST['nameacc'];
                    // Validate The Form
                    $formErrors = array();
                    if (empty($nameacc)) {
                        $formErrors[] = 'لا يجب أن يكون إسم الحساب  <strong>فارغ</strong>';
                    }
                    if ($lvacc == 2 || $lvacc == 3 || $lvacc == 4 || $lvacc == 5) {
                        if (empty($id_lv1)) {
                            $formErrors[] = 'لا يجب أن يكون حساب المستوى الأول <strong>فارغ</strong>';
                        }
                    }
                    if ($lvacc == 3 || $lvacc == 4 || $lvacc == 5) {
                        if (empty($id_lv2)) {
                            $formErrors[] = 'لا يجب أن يكون حساب المستوى الثاني <strong>فارغ</strong>';
                        }
                    }
                    if ($lvacc == 4 || $lvacc == 5) {
                        if (empty($id_lv3)) {
                            $formErrors[] = 'لا يجب أن يكون حساب المستوى الثالث <strong>فارغ</strong>';
                        }
                    }
                    if ($lvacc == 5) {
                        if (empty($id_lv4)) {
                            $formErrors[] = 'لا يجب أن يكون حساب المستوى الثالث <strong>فارغ</strong>';
                        }
                    }
                    // Loop Into Errors Array And Echo It
                    foreach ($formErrors as $error) {
                        echo '<div class="alert alert-danger">' . $error . '</div>';
                    }
                    // Check If There's No Error Proceed The Update Operation
                    if (empty($formErrors)) {
                        $lv1 = "";
                        $lv2 = "";
                        $lv3 = "";
                        $lv4 = "";
                        $lv5 = "";
                        if ($lvacc == 1) {
                            $acc_up = 0;
                            $account = $con->prepare("SELECT MAX(cost_center_code) as last_code_account FROM cost_centers where cc_lv = 1 ");
                            $account->execute();
                            $account1s = $account->fetchAll();
                            foreach ($account1s as $key => $account1) {
                                $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv1 = $last_code_account + 1;
                                } else {
                                    $lv1 = 1;
                                }
                            }
                        } elseif ($lvacc == 2) {
                            $lv1 = getName("level_acc", "ID", $id_lv1, "lv1");
                            $acc_up = $id_lv1;
                            $account = $con->prepare("SELECT MAX(lv2) as last_code_account FROM level_acc where lev_acc = 2 AND acc_up = $id_lv1 ");
                            $account->execute();
                            $account1s = $account->fetchAll();
                            foreach ($account1s as $key => $account1) {
                                $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv2 = $last_code_account + 1;
                                } else {
                                    $lv2 = ($lv1 * 10) + 1;
                                }
                            }
                        } elseif ($lvacc == 3) {
                            $lv1 = getName("level_acc", "ID", $id_lv1, "lv1");
                            $lv2 = getName("level_acc", "ID", $id_lv2, "lv2");
                            $acc_up = $id_lv2;
                            $account = $con->prepare("SELECT MAX(lv3) as last_code_account FROM level_acc where lev_acc = 3 AND acc_up = $id_lv2 ");
                            $account->execute();
                            $account1s = $account->fetchAll();
                            foreach ($account1s as $key => $account1) {
                                $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv3 = $last_code_account + 1;
                                } else {
                                    $lv3 = ($lv2 * 100) + 1;
                                }
                            }
                        } elseif ($lvacc == 4) {
                            $lv1 = getName("level_acc", "ID", $id_lv1, "lv1");
                            $lv2 = getName("level_acc", "ID", $id_lv2, "lv2");
                            $lv3 = getName("level_acc", "ID", $id_lv3, "lv3");
                            $acc_up = $id_lv3;
                            $account = $con->prepare("SELECT MAX(lv4) as last_code_account FROM level_acc where lev_acc = 4 AND acc_up = $id_lv3 ");
                            $account->execute();
                            $account1s = $account->fetchAll();
                            foreach ($account1s as $key => $account1) {
                                $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv4 = $last_code_account + 1;
                                } else {
                                    $lv4 = ($lv3 * 1000) + 1;
                                }
                            }
                        } elseif ($lvacc == 5) {
                            $lv1 = getName("level_acc", "ID", $id_lv1, "lv1");
                            $lv2 = getName("level_acc", "ID", $id_lv2, "lv2");
                            $lv3 = getName("level_acc", "ID", $id_lv3, "lv3");
                            $lv4 = getName("level_acc", "ID", $id_lv4, "lv4");
                            $acc_up = $id_lv4;
                            $account = $con->prepare("SELECT MAX(lv5) as last_code_account FROM level_acc where lev_acc = 5 AND acc_up = $id_lv4 ");
                            $account->execute();
                            $account1s = $account->fetchAll();
                            foreach ($account1s as $key => $account1) {
                                $last_code_account = $account1['last_code_account'];
                                if (!empty($last_code_account)) {
                                    $lv5 = $last_code_account + 1;
                                } else {
                                    $lv5 = ($lv4 * 1000) + 1;
                                }
                            }
                        }

                        // Insert Userinfo In Database
                        $stmt = $con->prepare("INSERT INTO 
                                                        cost_centers(cost_center_name, cc_lv, cost_center_code, cost_center_up, description)
                                                    VALUES(:cost_center_name, :cc_lv, :cost_center_code, :cost_center_up, :zdescription) ");
                        $stmt->execute(
                            array(
                                'cost_center_name' => $nameacc,
                                'cc_lv' => $lvacc,
                                'cost_center_code' => $lv1,
                                'cost_center_up' => $acc_up,
                                'zdescription' => ''
                            )
                        );
                        // Echo Success Message
                        $theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' تمت الإضافة</div>';
                        redirectlink($theMsg, '?Manage=cost_centers&do=costs', "مراكز التكلفة", 5);
                    }
                } else {

                    echo "<div class='container'>";

                    $theMsg = '<div class="alert alert-danger">أسف، لا يمكنك الدخول إلى هذه الصفحة مباشرة</div>';

                    redirectHome($theMsg);

                    echo "</div>";
                }

                echo "</div>";
            } elseif ($do == 'Edit') {

                // Check If Get Request userid Is Numeric & Get Its Integer Value

                $id = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;

                // Select All Data Depend On This ID

                $stmt = $con->prepare("SELECT * FROM cust_import WHERE ID = ? LIMIT 1");

                // Execute Query

                $stmt->execute(array($id));

                // Fetch The Data

                $row = $stmt->fetch();

                // The Row Count

                $count = $stmt->rowCount();

                // If There's Such ID Show The Form

                if ($count > 0) {
                ?>

                    <h3 class="text-center">تعديل بيانات <?php echo $row['Name_CI'] ?></h3>
                    <div class="container">
                        <form class="form-horizontal" action="?do=Update" method="POST">
                            <input type="hidden" name="id" value="<?php echo $id ?>" />
                            <!-- Start namecust Field -->
                            <div class="form-group form-group-lg">
                                <label class="col-sm-2 control-label">إسم العميل</label>
                                <div class="col-sm-10 col-md-6">
                                    <input type="text" name="name" class="form-control" value="<?php echo $row['Name_CI'] ?>" autocomplete="off" required="required" />
                                </div>
                            </div>
                            <!-- End namecust Field -->
                            <!-- Start phone Field -->
                            <div class="form-group form-group-lg">
                                <label class="col-sm-2 control-label">رقم الهاتف</label>
                                <div class="col-sm-10 col-md-6">
                                    <input type="text" name="phone" class="form-control" value="<?php echo $row['Phone'] ?>" />
                                </div>
                            </div>
                            <!-- End phone Field -->
                            <!-- Start address Field -->
                            <div class="form-group form-group-lg">
                                <label class="col-sm-2 control-label">العنوان</label>
                                <div class="col-sm-10 col-md-6">
                                    <input type="text" name="address" value="<?php echo $row['Address'] ?>" class="form-control" />
                                </div>
                            </div>
                            <!-- End address Field -->
                            <!-- Start permission Field -->
                            <div class="form-group form-group-lg">
                                <label class="col-sm-2 control-label">الرتبة</label>
                                <div class="col-sm-10 col-md-6">
                                    <select name="permission" class="form-control" required="required">
                                        <?php if ($row['vip'] == 0) {
                                        ?>
                                            <option value="0">عميل عادى</option>
                                            <option value="1">عميل مميز</option>
                                        <?php
                                        } else {
                                        ?>
                                            <option value="1">عميل مميز</option>
                                            <option value="0">عميل عادى</option>
                                        <?php
                                        } ?>
                                    </select>
                                </div>
                            </div>
                            <!-- End permission Field -->

                            <!-- Start Submit Field -->
                            <div class="form-group form-group-lg">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <input type="submit" value="حفظ التعديل" class="btn btn-primary btn-lg" />
                                </div>
                            </div>
                            <!-- End Submit Field -->
                        </form>
                    </div>
                <?php
                    // If There's No Such ID Show Error Message
                } else {

                    echo "<div class='container'>";

                    $theMsg = '<div class="alert alert-danger">من فضلك إتيع رابط صحيح</div>';

                    redirectHome($theMsg);

                    echo "</div>";
                }
            } elseif ($do == 'Update') { // Update Page

                echo "<h1 class='text-center'>تحديث البيانات</h1>";
                echo "<div class='container'>";

                if ($_SERVER['REQUEST_METHOD'] == 'POST') {

                    $formErrors = array();
                    // Get Variables From The Form

                    $id = $_POST['id'];
                    $user = $_POST['name'];
                    $phone = $_POST['phone'];
                    $address = $_POST['address'];
                    $permission = $_POST['permission'];

                    // Validate The Form

                    if (empty($user)) {
                        $formErrors[] = 'لا يجب أن يكون إسم العميل  <strong>فارغ</strong>';
                    }

                    // Loop Into Errors Array And Echo It

                    foreach ($formErrors as $error) {
                        echo '<div class="alert alert-danger">' . $error . '</div>';
                    }

                    // Check If There's No Error Proceed The Update Operation

                    if (empty($formErrors)) {

                        // Update The Database With This Info

                        $stmt = $con->prepare("UPDATE cust_import SET Name_CI = ?, vip = ?, Phone = ?, Address = ? WHERE ID = ?");

                        $stmt->execute(array($user, $permission, $phone, $address, $id));

                        // Echo Success Message

                        $theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' تم تحديث البيانات</div>';

                        redirectHome($theMsg, 'back');
                    }
                } else {

                    $theMsg = '<div class="alert alert-danger">أسف، لا يمكنك الدخول إلى هذه الصفحة مباشرة</div>';

                    redirectHome($theMsg);
                }

                echo "</div>";
            } elseif ($do == 'Delete') { // Delete Member Page

                echo "<h3 class='text-center'>حذف عميل</h3>";
                echo "<div class='container'>";

                // Check If Get Request id Is Numeric & Get The Integer Value Of It

                $id = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;

                // Select All Data Depend On This ID

                $check = checkItem('ID', 'cust_import', $id);

                // If There's Such ID Show The Form

                if ($check > 0) {

                    $stmt = $con->prepare("DELETE FROM cust_import WHERE ID = :zid");

                    $stmt->bindParam(":zid", $id);

                    $stmt->execute();

                    $theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' تم الحذف بنجاح</div>';

                    redirectHome($theMsg, 'back');
                } else {

                    $theMsg = '<div class="alert alert-danger">برجاء إتباع رابط صحيح</div>';

                    redirectHome($theMsg);
                }

                echo '</div>';
            } elseif ($do == 'cost') {
                if (!empty($_GET['id_cost']) && is_numeric($_GET['id_cost'])) {
                    $id_cost = $_GET['id_cost'];
                    ?>
                    <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=cost_centers&do=cost&id_cost=<?php echo $id_cost; ?>" method="post">
                        <div class="row">
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="start_date" class="control-label">من تاريخ:</label>
                                    <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="end_date" class="control-label">الى تاريخ:</label>
                                    <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-1">
                                <div class="col-sm-12">
                                    <label></label>
                                    <button type="submit" class="btn btn-primary form-control" name="search">بحث</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <section class="container">
                        <div class="panel panel-primary text-center table-responsive">
                            <!-- Default panel contents -->
                            <div class="panel-heading">تقرير مركز التكلفة بتاريخ <?php echo $start_date . " : " . $end_date; ?></div>
                            <!-- Table -->
                            <?php
                            $sqlaccounts = $con->prepare("SELECT * from cost_centers WHERE ID = '$id_cost' AND cost_center_up = 0 ");
                            $sqlaccounts->execute();
                            $accounts = $sqlaccounts->fetchAll();
                            foreach ($accounts as $account) {
                                $ID = $account['ID'];
                                $name_account = $account['cost_center_name'];
                                $code_account = $account['cost_center_code'];
                                // $nature_acc = $account['nature_acc'];
                                ?>
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th colspan="2">
                                                حــ / <?php echo $name_account; ?>
                                            </th>
                                        </tr>
                                        <tr>
                                            <th style="width: 50%;">
                                                مدين
                                            </th>
                                            <th style="width: 50%;">
                                                دائن
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="success">
                                            <td>
                                                <table class="table table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>
                                                                المبلغ
                                                            </th>
                                                            <th>
                                                                البيان
                                                            </th>
                                                            <th>
                                                                رقم القيد
                                                            </th>
                                                            <th>
                                                                التاريخ
                                                            </th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        $allcashfrom = $allcashfromto = $trow = $trowto = 0;

                                                        $sqlyomya = $con->prepare("SELECT * FROM yomya WHERE dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql'");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();

                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $sqldetails_yomya = $con->prepare("SELECT * FROM details_yomya WHERE id_yomya = '$id_yomya' AND cost_center_code = '$code_account'");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            $countfrom = $sqldetails_yomya->rowCount();

                                                            foreach ($details_yomyas as $details_yomya) {
                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_yomya2 = $details_yomya['id_yomya'];
                                                                $dateinsert = $details_yomya['dateinsert'];

                                                                $name_account2 = '';

                                                                $sqldetails_yomya2 = $con->prepare("SELECT * FROM details_yomya WHERE id_yomya = '$id_yomya' AND from_or_to = 2");
                                                                $sqldetails_yomya2->execute();
                                                                $details_yomyas2 = $sqldetails_yomya2->fetchAll();

                                                                foreach ($details_yomyas2 as $details_yomya2) {
                                                                    $code_lv3 = $details_yomya2['lv3'];
                                                                    $code_lv4 = $details_yomya2['lv4'];
                                                                    $sqlaccounts2 = $con->prepare("SELECT * FROM level_acc WHERE lv3 = '$code_lv3' AND lev_acc = 3");
                                                                    $sqlaccounts2->execute();
                                                                    $accounts2 = $sqlaccounts2->fetchAll();
                                                                    $name_account2 = $accounts2 ? $accounts2[0]['name_account'] : '';
                                                                }

                                                                $count_code_lv4 = $code_lv4 ? strlen($code_lv4) : 0;
                                                                $name_accountlv4 = '';

                                                                if ($count_code_lv4 >= 7) {
                                                                    $sqlaccounts2 = $con->prepare("SELECT * FROM level_acc WHERE lv4 = '$code_lv4'");
                                                                    $sqlaccounts2->execute();
                                                                    $accounts2 = $sqlaccounts2->fetchAll();
                                                                    $name_accountlv4 = $accounts2 ? $accounts2[0]['name_account'] : '';
                                                                }

                                                                if ($from_or_to == 2) {
                                                                    $allcashfromto += $cash;
                                                                }

                                                                if ($from_or_to == 1) {
                                                                    $allcashfrom += $cash;
                                                        ?>
                                                                    <tr class="success" style="height: 40px;">
                                                                        <td><?= $cash ?></td>
                                                                        <td>حــ / <?= $name_account2 . " - " . $name_accountlv4 ?></td>
                                                                        <td><?= $id_yomya ?></td>
                                                                        <td><?= $dateinsert ?></td>
                                                                    </tr>
                                                                <?php
                                                                    $trowto++;
                                                                } elseif ($from_or_to == 2) {
                                                                    $trow++;
                                                                }
                                                            }
                                                        }

                                                        $newrow = $trowto - $trow;
                                                        if ($newrow < 0) {
                                                            $newrow = $newrow - $newrow - $newrow;
                                                            for ($i = 0; $i < $newrow; $i++) {
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                </tr>
                                                        <?php
                                                            }
                                                        }

                                                        ?>
                                                        <tr style="height: 40px;">
                                                            <td>
                                                                <?php
                                                                if ($allcashfrom > $allcashfromto) {
                                                                    $mora7l = $allcashfrom - $allcashfromto;
                                                                    $allfromto = $allcashfrom;
                                                                ?>
                                                            </td>
                                                            <td>
                                                            </td>
                                                        <?php
                                                                } else {
                                                                    echo $mora7l = $allcashfromto - $allcashfrom;
                                                                    $allfromto = $allcashfromto;
                                                        ?>
                                            </td>
                                            <td>
                                                رصيد مرحل
                                            </td>
                                        <?php
                                                                }
                                                                //echo $allcashfrom;
                                        ?>

                                        <td>

                                        </td>
                                        <td>

                                        </td>
                                        </tr>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <th>
                                                <?php
                                                echo $allfromto;
                                                ?>
                                            </th>
                                            <th>
                                                الإجمالى
                                            </th>
                                            <td>

                                            </td>
                                            <td>

                                            </td>
                                        </tr>
                                    </tfoot>
                                </table>
                                </td>
                                <td>
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>
                                                    المبلغ
                                                </th>
                                                <th>
                                                    البيان
                                                </th>
                                                <th>
                                                    رقم القيد
                                                </th>
                                                <th>
                                                    التاريخ
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $allcashto = 0;
                                            $allcashtofrom = 0;
                                            $trow2 = 0;
                                            $trowfrom = 0;
                                            $sqlyomya = $con->prepare("SELECT * from yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                                            $sqlyomya->execute();
                                            $yomyas = $sqlyomya->fetchAll();
                                            foreach ($yomyas as $yomya) {
                                                $id_yomya = $yomya['id_yomya'];
                                                $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND cost_center_code = '$code_account'");
                                                $sqldetails_yomya->execute();
                                                $details_yomyas = $sqldetails_yomya->fetchAll();
                                                foreach ($details_yomyas as $details_yomya) {
                                                    $from_or_to = $details_yomya['from_or_to'];
                                                    $cash = $details_yomya['cash'];
                                                    $id_yomya2 = $details_yomya['id_yomya'];
                                                    $dateinsert = $details_yomya['dateinsert'];

                                                    $name_account2 = '';
                                                    $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1");
                                                    $sqldetails_yomya2->execute();
                                                    $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                    foreach ($details_yomyas2 as $details_yomya2) {
                                                        $code_lv3 = $details_yomya2['lv3'];
                                                        $code_lv4 = $details_yomya2['lv4'];
                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv3 = '$code_lv3' AND lev_acc = 3 ");
                                                        $sqlaccounts2->execute();
                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                        foreach ($accounts2 as $account2) {
                                                            $name_account2 = $account2['name_account'];
                                                        }
                                                    }
                                                    $count_code_lv4 = 0;
                                                    $name_accountlv4 = "";
                                                    if (!empty($code_lv4)) {
                                                        $count_code_lv4 = strlen($code_lv4);
                                                        if ($count_code_lv4 < 7) {
                                                            $name_accountlv4 = "";
                                                        } else {
                                                            $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv4 = '$code_lv4' ");
                                                            $sqlaccounts2->execute();
                                                            $accounts2 = $sqlaccounts2->fetchAll();
                                                            foreach ($accounts2 as $account2) {
                                                                $name_accountlv4 = $account2['name_account'];
                                                            }
                                                        }
                                                    } else {
                                                        $name_accountlv4 = "";
                                                    }
                                                    if ($from_or_to == 1) {
                                                        $allcashtofrom += $cash;
                                                    }
                                                    if ($from_or_to == 2) {
                                            ?>
                                                        <tr class="success" style="height: 40px;">
                                                            <td>
                                                                <?php
                                                                $allcashto += $cash;
                                                                echo $cash;
                                                                ?>
                                                            </td>
                                                            <td>
                                                                من حــ /
                                                                <?php
                                                                echo $name_account2 . " - " . $name_accountlv4;
                                                                ?>
                                                            </td>
                                                            <td>
                                                                <?php
                                                                echo $id_yomya;
                                                                ?>
                                                            </td>
                                                            <td>
                                                                <?php
                                                                echo $dateinsert;
                                                                ?>
                                                            </td>
                                                        </tr>
                                                    <?php
                                                        $trowfrom += 1;
                                                    } elseif ($from_or_to == 1) {
                                                        $trow2 += 1;
                                                    }
                                                }
                                            }
                                            $newrow = $trowfrom - $trow2;
                                            if ($newrow < 0) {
                                                $newrow = $newrow - $newrow - $newrow;
                                                for ($i = 0; $i < $newrow; $i++) {
                                                    ?>
                                                    <tr class="success" style="height: 40px;">
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                    </tr>
                                            <?php
                                                }
                                            }
                                            ?>
                                            <tr style="height: 40px;">
                                                <td>
                                                    <?php
                                                    if ($allcashto > $allcashtofrom) {
                                                        $mora7l = $allcashto - $allcashtofrom;
                                                        $allfromto = $allcashto;
                                                    ?>
                                                </td>
                                                <td>
                                                </td>
                                            <?php
                                                    } else {
                                                        echo $mora7l = $allcashtofrom - $allcashto;
                                                        $allfromto = $allcashtofrom;
                                            ?>
                                </td>
                                <td>
                                    رصيد مرحل
                                </td>
                            <?php
                                                    }
                                                    //echo $allcashfrom;
                            ?>

                            <td>

                            </td>
                            <td>

                            </td>
                            </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th>
                                        <?php
                                        echo $allfromto;
                                        ?>
                                    </th>
                                    <th>
                                        الإجمالى
                                    </th>
                                    <td>

                                    </td>
                                    <td>

                                    </td>
                                </tr>
                            </tfoot>
                            </table>
                            </td>
                            </tr>

                            </tbody>
                            </table>
                        <?php
                            }
                        ?>
                        </div>
                    </section>
                <?php
                } elseif (!empty($_GET['id_cost_f']) && is_numeric($_GET['id_cost_f'])) {
                    $id_cost_f = $_GET['id_cost_f'];
                    ?>
                    <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=cost_centers&do=cost&id_cost_f=<?php echo $id_cost_f; ?>" method="post">
                        <div class="row">
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="start_date" class="control-label">من تاريخ:</label>
                                    <input type="date" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-3">
                                <div class="col-sm-12">
                                    <label for="end_date" class="control-label">الى تاريخ:</label>
                                    <input type="date" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" required>
                                </div>
                            </div>
                            <div class="form-group form-group-lg col-sm-1">
                                <div class="col-sm-12">
                                    <label></label>
                                    <button type="submit" class="btn btn-primary form-control" name="search">بحث</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <!-- start -->
                    <section class="container">
                        <div class="panel panel-primary text-center table-responsive">
                            <!-- Default panel contents -->
                            <div class="panel-heading">تقرير مركز التكلفة </div>
                            <!-- <div class="panel-heading">تقرير مركز التكلفة بتاريخ <?php echo $start_date . " : " . $end_date; ?></div> -->
                            <!-- Table -->
                            <?php
                            $sqlaccounts = $con->prepare("SELECT * from cost_centers WHERE ID = '$id_cost_f' AND cost_center_up != 0 ");
                            $sqlaccounts->execute();
                            $accounts = $sqlaccounts->fetchAll();
                            foreach ($accounts as $account) {
                                $ID = $account['ID'];
                                $name_account = $account['cost_center_name'];
                                $code_account = $account['cost_center_code'];
                                //$lv4 = $account['lv4'];
                                // $nature_acc = $account['nature_account'];
                                ?>
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th colspan="2">
                                                م.ت / <?php echo $name_account; ?>
                                            </th>
                                        </tr>
                                        <tr>
                                            <th style="width: 50%;">
                                                التكاليف
                                            </th>
                                            <th style="width: 50%;">
                                                الايرادات
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="success">
                                            <td>
                                                <table class="table table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>
                                                                المبلغ
                                                            </th>
                                                            <th>
                                                                البيان
                                                            </th>
                                                            <th>
                                                                رقم القيد
                                                            </th>
                                                            <th>
                                                                التاريخ
                                                            </th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        $allcashfrom = 0;
                                                        $allcashfromto = 0;
                                                        $trow = 0;
                                                        $trowto = 0;
                                                        $sqlyomya = $con->prepare("SELECT * from yomya");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $payan = $yomya['payan'];
                                                            $registration_number = $yomya['registration_number'];
                                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND cost_center_code = '$code_account'");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            $countfrom = $sqldetails_yomya->rowCount();
                                                            foreach ($details_yomyas as $details_yomya) {
                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                                $amountSold = $details_yomya['amountSold'];
                                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                                    
                                                                if($id_currency_codes != 1){
                                                                    $cash = $amountSold;
                                                                } else {
                                                                    $cash = $currencyExchangeRate * $cash;
                                                                }
                                                                $id_yomya2 = $details_yomya['id_yomya'];
                                                                $dateinsert = $details_yomya['dateinsert'];

                                                                $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1");
                                                                $sqldetails_yomya2->execute();
                                                                $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                foreach ($details_yomyas2 as $details_yomya2) {
                                                                    $code_lv4 = $details_yomya2['lv4'];
                                                                    $count_code_lv4 = 0;
                                                                    if (!empty($code_lv4)) {
                                                                        $count_code_lv4 = strlen($code_lv4);
                                                                        if ($count_code_lv4 < 7) {
                                                                            $code_lv3 = $code_lv4;
                                                                            $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv3 = '$code_lv3' AND lev_acc = 3 ");
                                                                            $sqlaccounts2->execute();
                                                                            $accounts2 = $sqlaccounts2->fetchAll();
                                                                            foreach ($accounts2 as $account2) {
                                                                                $name_account2 = $account2['name_account'];
                                                                                $lv = $account2['lv'];
                                                                            }
                                                                        } else {
                                                                            $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv4 = '$code_lv4' ");
                                                                            $sqlaccounts2->execute();
                                                                            $accounts2 = $sqlaccounts2->fetchAll();
                                                                            foreach ($accounts2 as $account2) {
                                                                                $name_account2 = $account2['name_account'];
                                                                                $lv = $account2['lv'];
                                                                            }
                                                                        }
                                                                    } else {
                                                                        $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1");
                                                                        $sqldetails_yomya2->execute();
                                                                        $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                        foreach ($details_yomyas2 as $details_yomya2) {
                                                                            $code_lv3 = $details_yomya2['lv3'];
                                                                        }
                                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv3 = '$code_lv3' AND lev_acc = 3 ");
                                                                        $sqlaccounts2->execute();
                                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                                        foreach ($accounts2 as $account2) {
                                                                            $name_account2 = $account2['name_account'];
                                                                            $lv = $account2['lv'];
                                                                        }
                                                                    }
                                                                }
                                                                if ($from_or_to == 2) {
                                                                    $allcashfromto += $cash;
                                                                }
                                                                if ($from_or_to == 1) {
                                                                    ?>
                                                                    <tr class="success" style="height: 40px;">
                                                                        <td>
                                                                            <?php
                                                                            $allcashfrom += $cash;
                                                                            echo $cash;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                        <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=account_statement&id_ledger=<?php echo $lv; ?>" method="post">
                                                                            
                                                                            <input type="hidden" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" >
                                                                        
                                                                            <input type="hidden" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" >
                                                                        
                                                                            <input type="hidden" name="num_acount" id="num_acount" class="form-control" value="<?=$lv?>" placeholder="" >
                                                                            <input type="hidden" name="currency_code" id="currency_code" class="form-control" value="2" placeholder="" >
                                                                            <button type="submit" class="btn" name="view_reservations">حــ / <?php echo $name_account2; ?></button>
                                                                        </form>
                                                                            <?php
                                                                            echo $payan;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                            <a href="reports_account.php?Manage=tree&do=add_yomya&id=<?=$registration_number;?>&currency_code=2" target="_blank"> <?=$registration_number;?></a>
                                                                            
                                                                        </td>
                                                                        <td>
                                                                            <?php
                                                                            echo $dateinsert;
                                                                            ?>
                                                                        </td>
                                                                    </tr>
                                                                <?php
                                                                    $trowto += 1;
                                                                } elseif ($from_or_to == 2) {
                                                                    $trow += 1;
                                                                }
                                                            }
                                                        }
                                                        $newrow = $trowto - $trow;
                                                        if ($newrow < 0) {
                                                            $newrow = $newrow - $newrow - $newrow;
                                                            for ($i = 0; $i < $newrow; $i++) {
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                </tr>
                                                            <?php
                                                            }
                                                        }
                                                        ?>
                                                        <tr style="height: 40px;">
                                                            <td>

                                                                <?php
                                                                if ($allcashfrom > $allcashfromto) {
                                                                    $mora7l = $allcashfrom - $allcashfromto;
                                                                    $allfromto = $allcashfrom;
                                                                    echo '</td><td>';
                                                                } else {
                                                                    echo $mora7l = $allcashfromto - $allcashfrom;
                                                                    $allfromto = $allcashfromto;
                                                                    echo '</td><td>';
                                                                    echo 'صافى ربح';
                                                                }
                                                                //echo $allcashfrom;
                                                                ?>
                                                            </td>
                                                            <td>

                                                            </td>
                                                            <td>

                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th>
                                                                <?php
                                                                echo $allfromto;
                                                                ?>
                                                            </th>
                                                            <th>
                                                                الإجمالى
                                                            </th>
                                                            <td>

                                                            </td>
                                                            <td>

                                                            </td>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </td>
                                            <td>
                                                <table class="table table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>
                                                                المبلغ
                                                            </th>
                                                            <th>
                                                                البيان
                                                            </th>
                                                            <th>
                                                                رقم القيد
                                                            </th>
                                                            <th>
                                                                التاريخ
                                                            </th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        $allcashto = 0;
                                                        $allcashtofrom = 0;
                                                        $trow2 = 0;
                                                        $trowfrom = 0;
                                                        $sqlyomya = $con->prepare("SELECT * from yomya ");
                                                        $sqlyomya->execute();
                                                        $yomyas = $sqlyomya->fetchAll();
                                                        foreach ($yomyas as $yomya) {
                                                            $id_yomya = $yomya['id_yomya'];
                                                            $payan = $yomya['payan'];
                                                            $registration_number = $yomya['registration_number'];
                                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND cost_center_code = '$code_account'");
                                                            $sqldetails_yomya->execute();
                                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                                            foreach ($details_yomyas as $details_yomya) {
                                                                $from_or_to = $details_yomya['from_or_to'];
                                                                $cash = $details_yomya['cash'];
                                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                                $amountSold = $details_yomya['amountSold'];
                                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                                    
                                                                if($id_currency_codes != 1){
                                                                    $cash = $amountSold;
                                                                } else {
                                                                    $cash = $currencyExchangeRate * $cash;
                                                                }
                                                                $id_yomya2 = $details_yomya['id_yomya'];
                                                                $dateinsert = $details_yomya['dateinsert'];

                                                                $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1");
                                                                $sqldetails_yomya2->execute();
                                                                $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                foreach ($details_yomyas2 as $details_yomya2) {
                                                                    $code_lv4 = $details_yomya2['lv4'];
                                                                    $count_code_lv4 = 0;
                                                                    if (!empty($code_lv4)) {
                                                                        $count_code_lv4 = strlen($code_lv4);
                                                                        if ($count_code_lv4 < 7) {
                                                                            $code_lv3 = $code_lv4;
                                                                            $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv3 = '$code_lv3' ");
                                                                            $sqlaccounts2->execute();
                                                                            $accounts2 = $sqlaccounts2->fetchAll();
                                                                            foreach ($accounts2 as $account2) {
                                                                                $name_account2 = $account2['name_account'];
                                                                                $lv = $account2['lv'];
                                                                            }
                                                                        } else {
                                                                            $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv4 = '$code_lv4' ");
                                                                            $sqlaccounts2->execute();
                                                                            $accounts2 = $sqlaccounts2->fetchAll();
                                                                            foreach ($accounts2 as $account2) {
                                                                                $name_account2 = $account2['name_account'];
                                                                                $lv = $account2['lv'];
                                                                            }
                                                                        }
                                                                    } else {
                                                                        $sqldetails_yomya2 = $con->prepare("SELECT * from details_yomya where id_yomya = '$id_yomya' AND from_or_to = 1");
                                                                        $sqldetails_yomya2->execute();
                                                                        $details_yomyas2 = $sqldetails_yomya2->fetchAll();
                                                                        foreach ($details_yomyas2 as $details_yomya2) {
                                                                            $code_lv3 = $details_yomya2['lv3'];
                                                                        }
                                                                        $sqlaccounts2 = $con->prepare("SELECT * from level_acc where lv3 = '$code_lv3' ");
                                                                        $sqlaccounts2->execute();
                                                                        $accounts2 = $sqlaccounts2->fetchAll();
                                                                        foreach ($accounts2 as $account2) {
                                                                            $name_account2 = $account2['name_account'];
                                                                            $lv = $account2['lv'];
                                                                        }
                                                                    }
                                                                }
                                                                if ($from_or_to == 1) {
                                                                    $allcashtofrom += $cash;
                                                                }
                                                                if ($from_or_to == 2) {
                                                                    ?>
                                                                    <tr class="success" style="height: 40px;">
                                                                        <td>
                                                                            <?php
                                                                            $allcashto += $cash;
                                                                            echo $cash;
                                                                            ?>
                                                                        </td>
                                                                        <td>
                                                                        <form class="form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?Manage=tree&do=account_statement&id_ledger=<?php echo $lv; ?>" method="post">
                                                                            
                                                                            <input type="hidden" name="start" id="start" class="form-control" value="<?php echo $thisyear_start . "-" . $thismon_start . "-" . $start_day_start ?>" placeholder="التاريخ" >
                                                                        
                                                                            <input type="hidden" name="end" id="end" class="form-control" value="<?php echo $thisyear_end . "-" . $thismon_end . "-" . $thisday_end ?>" placeholder="التاريخ" >
                                                                        
                                                                            <input type="hidden" name="num_acount" id="num_acount" class="form-control" value="<?=$lv?>" placeholder="" >
                                                                            <input type="hidden" name="currency_code" id="currency_code" class="form-control" value="2" placeholder="" >
                                                                            <button type="submit" class="btn" name="view_reservations">حــ / <?php echo $name_account2; ?></button>
                                                                        </form>
                                                                        <?php
                                                                            echo $payan;
                                                                            ?>
                                                                            
                                                                        </td>
                                                                        <td>
                                                                            <a href="reports_account.php?Manage=tree&do=add_yomya&id=<?=$registration_number;?>&currency_code=2" target="_blank"> <?=$registration_number;?></a>
                                                                            
                                                                        </td>
                                                                        <td>
                                                                            <?php
                                                                            echo $dateinsert;
                                                                            ?>
                                                                        </td>
                                                                    </tr>
                                                                <?php
                                                                    $trowfrom += 1;
                                                                } elseif ($from_or_to == 1) {
                                                                    $trow2 += 1;
                                                                }
                                                            }
                                                        }
                                                        $newrow = $trowfrom - $trow2;
                                                        if ($newrow < 0) {
                                                            $newrow = $newrow - $newrow - $newrow;
                                                            for ($i = 0; $i < $newrow; $i++) {
                                                                ?>
                                                                <tr class="success" style="height: 40px;">
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                    <td>
                                                                    </td>
                                                                </tr>
                                                        <?php
                                                            }
                                                        }
                                                        ?>
                                                        <tr style="height: 40px;">
                                                            <td>
                                                                <?php
                                                                if ($allcashto > $allcashtofrom) {
                                                                    $mora7l = $allcashto - $allcashtofrom;
                                                                    $allfromto = $allcashto;
                                                                    echo '</td><td>';
                                                                } else {
                                                                    echo $mora7l = $allcashtofrom - $allcashto;
                                                                    $allfromto = $allcashtofrom;
                                                                    echo '</td><td>';
                                                                    echo 'صافى خسار';
                                                                }
                                                                //echo $allcashfrom;
                                                                ?>
                                                            </td>
                                                            <td>
                                                            </td>
                                                            <td>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th>
                                                                <?php
                                                                echo $allfromto;
                                                                ?>
                                                            </th>
                                                            <th>
                                                                الإجمالى
                                                            </th>
                                                            <td>

                                                            </td>
                                                            <td>

                                                            </td>
                                                        </tr>
                                                    </tfoot>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            <?php
                            }
                            ?>
                        </div>
                    </section>
                <?php
                } else {
                ?>
                    <div class="alert alert-danger">
                        عذراً هناك خطاء.. برجاء إتبع رابط صحيح
                    </div>
            <?php
                }
            } else {
                header('Location: index.php');
                exit();
            }
        }else {
            echo '<div class="alert alert-danger">ليس لديك صلاحية الدخول الى هذا القسم</div>';
            exit();
        }
    } else {
        header('Location: index.php');
        exit();
    }
    include $tpl . 'footer.php';
ob_end_flush(); // Release The Output
?>