<?php
include("../../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$datefrom = "";
$dateto = "";
$customer_name = "";
$product_id = "";
$totalDiscount = 0;
$productQtyTotal = 0;
$costPriceTotal = 0;
$unitPriceTotal = 0;
$totalProfit = 0;
$mainCat = "";
$catName = "";
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
extract($_REQUEST);
if ($datefrom == "") {
    $datefrom = date('Y-m-d', strtotime("-30 days"));
} else {
    $datefrom = date("Y-m-d", strtotime($datefrom));
}
if ($dateto == "") {
    $dateto = date('Y-m-d');
} else {
    $dateto = date("Y-m-d", strtotime($dateto));
}
$datefrom = date("d-m-Y", strtotime($datefrom));
$dateto = date("d-m-Y", strtotime($dateto));

$qryDate = date("Y-m-d", strtotime($datefrom));
$qryTo = date("Y-m-d", strtotime($dateto));
//$filterReport = " LEFT JOIN " . TABLE_INVOICE_PRODUCT . " AS invPr ON inv.id = invPr.invoice_id INNER JOIN " . TABLE_CUSTOMERSETUP . " AS cs ON inv.customer_id = cs.id WHERE DATE_FORMAT(inv.create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(inv.create_date,'%Y-%m-%d')<='$qryTo' AND cs.is_mart = 0 ";
$filterReport = " LEFT JOIN " . TABLE_INVOICE_PRODUCT . " AS invPr ON inv.id = invPr.invoice_id INNER JOIN " . TABLE_CUSTOMERSETUP . " AS cs ON inv.customer_id = cs.id WHERE DATE_FORMAT(inv.create_date,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(inv.create_date,'%Y-%m-%d')<='$qryTo' and inv.status <> 'Cancel' ";

if (isset($customer_name) && $customer_name != 'All') {
    $filterReport .= " AND inv.customer_id ='$customer_name'";
}
if (isset($product_id) && $product_id != 'All') {
    $filterReport .= " AND invPr.product_id ='$product_id'";
} else {
    $filterReport .= " GROUP BY inv.id";
}
$filterReport .= " ORDER BY inv.code ";

$filterExpenseReport = " AS ex RIGHT JOIN " . TABLE_EXPENSECATEGORYSETUP . " AS exCat ON ex.category_id = exCat.id WHERE DATE_FORMAT(lastupdated,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(lastupdated,'%Y-%m-%d')<='$qryTo' ";
$filterExpenseReport .= " ORDER BY exCat.parent_id";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('../includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Product Profit/Loss Report</h3>
            </div>
            <script>
                $(function () {
                    $('#datepicker').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                      selectYears: true,
                      
                    });
                    $('#datepicker1').pickadate({
                        format: 'dd-mm-yyyy',
                        selectMonths: true,
                      selectYears: true,
                    });
                });
            </script>
            <form action="" method="get" name="search"
                  id="searchfrm">
                <table class="table">
                    <tr>
                        <td class="col s6">Date From<br>
                            <input type="text" name="datefrom" id="datepicker"
                                   placeholder="Date From"
                                   value="<?= $datefrom ?>" class="form-control"/>
                            <input type="hidden" name="view" id="view" class="form-control"/>
                        </td>
                        <td class="col s6"> Date To<br>
                            <input type="text" name="dateto" id="datepicker1"
                                   placeholder="Date To"
                                   value="<?= $dateto ?>" class="form-control"/>
                        </td>
                    </tr>
                    <tr>
                        <td class="col s6">Customer<br>
                            <?php
                            //$customersfilter = " WHERE status = 'active' AND is_mart = 0";
                            $customersfilter = " WHERE status = 'active' ORDER BY name";
                            $customers = CustomerSetup::GetCustomerSetupList($customersfilter);
                            ?>
                            <select class="form-control" required id="customer_name"
                                    name="customer_name">
                                <option>All</option>
                                <?php
                                if ($customers) {
                                    foreach ($customers AS $customersRow) {
                                        echo '<option ';
                                        if ($customersRow->id == $customer_name) {
                                            echo 'selected';
                                        }
                                        echo ' value="' . $customersRow->id . '" >' . $customersRow->name . '</option>';
                                    }
                                }
                                ?>
                            </select><br/>
                        </td>
                        <td class="col s6"> Product<br>
                            <?php
                            $filter = " where status = 'active' ORDER BY name";
                            $qry = ProductSetup::GetProductSetupList($filter, $pageno = "null", $pagesize = "null");
                            if ($qry) {
                                echo '<select id="product_id" name="product_id" class="form-control"><option>All</option>';
                                foreach ($qry as $qery) {
                                    echo '<option ';
                                    if ($qery->id == $product_id) {
                                        echo 'selected';
                                    }
                                    echo ' value=' . $qery->id . '>' . $qery->name . '</option>';
                                }
                                echo '</select>';
                            }
                            ?><br/>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="col s12">
                            <button type="submit" class="btn waves-effect waves-light"
                                    style="float:left;">
                                Search
                            </button>
                            <?php if (isset($_GET['view'])) { ?>
                                <button
                                    onclick="exportTableToCSV('invoice-<?= date('d-m-Y-h-i-a') ?>.csv')"
                                    class="btn waves-effect waves-light"
                                    style="float:left; margin-left: 15px;">
                                    Export CSV
                                </button>
                                <button
                                    id="print"
                                    class="btn waves-effect waves-light"
                                    style="float:left; margin-left: 15px;">
                                    Print
                                </button>
                            <?php } ?>
                        </td>
                    </tr>
                </table>
            </form>
            <hr><?php
            if (isset($_GET['view'])) {
                ?>
                <div id="floatThead" class="section">
                    <table class="striped demo1 bordered">
                        <thead>
                        <tr style="border: 1px solid #666;">
                            <th>Date</th>
                            <th>Invoice #</th>
                            <th>Customer</th>
                            <th>Product</th>
                            <th>Sales Person</th>
                            <th>Status</th>
                            <th>Qty</th>
                            <th>Cost Price</th>
                            <th>Sale Price</th>
                            <?php if ((PROJECT_DISCOUNT == 1 || !empty($discount_price))) { ?>
                                <th>Discount</th>
                            <?php } ?>
                            <th>Profit / Loss</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        if ($customer_name != 'All' && $customer_name != '') {
                            $customer = CustomerSetup::GetCustomerSetupById($customer_name);
                            echo "<h2 style='text-align: center;'>" . $customer->name . "</h2>";
                        }
                        $qry = Invoice::GetInvoiceReportList($filterReport);
                        if ($qry) {
                            foreach ($qry as $qery) { ?>
                                <tr style="border: 1px solid #666;">
                                    <td><?php echo date('Y-m-d h:i A', strtotime($qery->create_date)); ?></td>
                                    <?php
                                    $customer = CustomerSetup::GetCustomerSetupById($qery->customer_id);
                                    $salesPerson = User_setup::GetUserSetupId($qery->sales_person_id);
                                    $discount_price = $qery->discount_price;
                                    ?>
                                    <td><?php echo $qery->code; ?></td>
                                    <td><?php echo $customer->name; ?></td>
                                    <?php
                                    if ($product_id == 'All' || $product_id == '') {
                                        $productsNames = array();
                                        $productsQtys = 0;
                                        $productsUnitCostPrice = 0;
                                        $productsUnitSalePrice = 0;

                                        $filterProducts = " WHERE invoice_id = '$qery->invoice_id'";
                                        $productSetup = Invoice::GetInvoiceProducts($filterProducts);
                                        if ($productSetup) {
                                            foreach ($productSetup as $productSetupRows) {
                                                $product = ProductSetup::GetProductSetupById($productSetupRows->product_id);
                                                $productsNames[] = $product->name;
                                                $productsQtys += $productSetupRows->product_qty;
                                                $productsUnitCostPrice += number_format(($productSetupRows->product_qty * $productSetupRows->unit_cost_price), 2, '.', '');
                                                if ($qery->is_mart == 0) {
                                                    $productsUnitSalePrice += number_format(($productSetupRows->product_qty * $productSetupRows->unit_sale_price), 2, '.', '');
                                                } else {
                                                    $productsUnitSalePrice += number_format(($productSetupRows->product_qty * $productSetupRows->unit_distributor_price), 2, '.', '');
                                                }
                                            }
                                        }
                                        $subtotal = $productsUnitSalePrice - $productsUnitCostPrice;
                                        if ($qery->is_mart == 0) {
                                            if (PROJECT_DISCOUNT == 1 || !empty($discount_price) && ($product_id == '' || $product_id == 'All')) {

                                                $subtotal -= $discount_price;
                                                $totalDiscount += $discount_price;
                                            }
                                        }
                                        $productsName = implode(', ', $productsNames);
                                        $productQtyTotal += $productsQtys;
                                        $costPriceTotal += $productsUnitCostPrice;
                                        $unitPriceTotal += $productsUnitSalePrice;
                                        $totalProfit += $subtotal;
                                        ?>
                                        <td style="font-size: 12px;"><?php echo $productsName; ?></td>
                                        <td><?php echo $salesPerson->username; ?></td>
                                        <td><?php echo $qery->status; ?></td>
                                        <td><?php echo $productsQtys; ?></td>
                                        <td><?php echo $productsUnitCostPrice; ?></td>
                                        <td><?php echo $productsUnitSalePrice; ?></td>
                                        <?php
                                        if ($qery->is_mart == 0) {
                                            if (PROJECT_DISCOUNT == 1 || !empty($discount_price)) {
                                                ?>
                                                <td><?php echo $discount_price; ?></td>
                                            <?php }
                                        } else {
                                            ?>
                                            <td>0</td>
                                            <?php
                                        } ?>
                                        <td><?php echo number_format(($subtotal), 2, '.', ''); ?></td>
                                    <?php } else {
                                        $productName = ProductSetup::GetProductSetupById($qery->product_id);
                                        $productQty = $qery->product_qty;
                                        $productQtyTotal += $productQty;
                                        $costPrice = number_format(($qery->product_qty * $qery->unit_cost_price), 2, '.', '');
                                        $costPriceTotal += $costPrice;
                                        if ($qery->is_mart == 0) {
                                            $unitPrice = number_format(($qery->product_qty * $qery->unit_sale_price), 2, '.', '');
                                            $unitPriceTotal += $unitPrice;
                                            $subtotal = $unitPrice - $costPrice;
                                            if (PROJECT_DISCOUNT == 1 || $discount_price != 0) {
                                                $filterProducts = " WHERE invoice_id = '$qery->invoice_id'";
                                                $productSetup = Invoice::GetInvoiceProducts($filterProducts);
                                                $count = 0;
                                                if ($productSetup) {
                                                    foreach ($productSetup as $productSetupRows) {
                                                        $count++;
                                                    }
                                                }
                                                $totalDiscountper = $qery->discount;

                                                $subTotalDiscount = ($unitPrice * ($totalDiscountper / 100));
                                                $totalDiscount += $subTotalDiscount;
                                                $subtotal -= $subTotalDiscount;
                                            }
                                            $totalProfit += $subtotal;
                                        } else {
                                            $unitPrice = number_format(($qery->product_qty * $qery->unit_distributor_price), 2, '.', '');
                                            $unitPriceTotal += $unitPrice;
                                            $subtotal = $unitPrice - $costPrice;
                                            $totalProfit += $subtotal;
                                        }
                                        ?>
                                        <td style="font-size: 12px;"><?php echo $productName->name; ?></td>
                                        <td><?php echo $salesPerson->username; ?></td>
                                        <td><?php echo $qery->status; ?></td>
                                        <td><?php echo $qery->product_qty; ?></td>
                                        <td><?php echo $costPrice; ?></td>
                                        <td><?php echo $unitPrice; ?></td>
                                        <?php if ($qery->is_mart == 0) { ?>
                                            <td><?php echo number_format(($subTotalDiscount), 2, '.', ''); ?></td>
                                        <?php } else { ?>
                                            <td>0</td>
                                        <?php } ?>
                                        <td><?php echo number_format(($subtotal), 2, '.', ''); ?></td>
                                    <?php } ?>
                                </tr>
                                <?php
                            }
                        }
                        ?>
                        <tr>

                            <th colspan="6" style="text-align: right; border: 1px solid #000;">
                                Total
                            </th>
                            <th style="border: 1px solid #666;"><?= $productQtyTotal ?></th>
                            <th style="border: 1px solid #666;"><?= $costPriceTotal ?></th>
                            <th style="border: 1px solid #666;"><?= $unitPriceTotal ?></th>
                            <?php if ((PROJECT_DISCOUNT == 1 || $discount_price != 0)) { ?>
                                <th style="border: 1px solid #666;"><?= number_format(($totalDiscount), 2, '.', '') ?></th>
                            <?php } ?>
                            <th style="border: 1px solid #666;"><?= number_format(($totalProfit), 2, '.', '') ?></th>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <br>
                <br>
                <br>
                <?php
                if ($_SESSION['clone'] == 1 && $customer_name == 'All') {
                    global $db;
                    $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
                    $db->connect();
                    ?>
                    <div id="floatThead" class="section">
                        <table class="striped demo1 bordered">
                            <thead>
                            <tr style="border: 1px solid #666;">
                                <th>Date</th>
                                <th>Invoice #</th>
                                <th>Customer</th>
                                <th>Product</th>
                                <th>Sales Person</th>
                                <th>Status</th>
                                <th>Qty</th>
                                <th>Cost Price</th>
                                <th>Sale Price</th>
                                <?php if ((PROJECT_DISCOUNT == 1 || !empty($discount_price))) { ?>
                                    <th>Discount</th>
                                <?php } ?>
                                <th>Profit / Loss</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            if ($customer_name != 'All' && $customer_name != '') {
                                $customer = CustomerSetup::GetCustomerSetupById($customer_name);
                                echo "<h2 style='text-align: center;'>" . $customer->name . "</h2>";
                            }

                            $qry = Invoice::GetInvoiceReportList($filterReport);
                            if ($qry) {
                                foreach ($qry as $qery) { ?>
                                    <tr style="border: 1px solid #666;">
                                        <td><?php echo date('Y-m-d h:i A', strtotime($qery->create_date)); ?></td>
                                        <?php
                                        $customer = CustomerSetup::GetCustomerSetupById($qery->customer_id);
                                        $salesPerson = User_setup::GetUserSetupId($qery->sales_person_id);
                                        $discount_price = $qery->discount_price;
                                        ?>
                                        <td><?php echo $qery->code; ?></td>
                                        <td><?php echo $customer->name; ?></td>
                                        <?php
                                        if ($product_id == 'All' || $product_id == '') {
                                            $productsNames = array();
                                            $productsQtys = 0;
                                            $productsUnitCostPrice = 0;
                                            $productsUnitSalePrice = 0;

                                            $filterProducts = " WHERE invoice_id = '$qery->invoice_id'";
                                            $productSetup = Invoice::GetInvoiceProducts($filterProducts);
                                            if ($productSetup) {
                                                foreach ($productSetup as $productSetupRows) {
                                                    $product = ProductSetup::GetProductSetupById($productSetupRows->product_id);
                                                    $productsNames[] = $product->name;
                                                    $productsQtys += $productSetupRows->product_qty;
                                                    $productsUnitCostPrice += number_format(($productSetupRows->product_qty * $productSetupRows->unit_cost_price), 2, '.', '');
                                                    if ($qery->is_mart == 0) {
                                                        $productsUnitSalePrice += number_format(($productSetupRows->product_qty * $productSetupRows->unit_sale_price), 2, '.', '');
                                                    } else {
                                                        $productsUnitSalePrice += number_format(($productSetupRows->product_qty * $productSetupRows->unit_distributor_price), 2, '.', '');
                                                    }
                                                }
                                            }
                                            $subtotal = $productsUnitSalePrice - $productsUnitCostPrice;
                                            if ($qery->is_mart == 0) {
                                                if (PROJECT_DISCOUNT == 1 || !empty($discount_price) && ($product_id == '' || $product_id == 'All')) {

                                                    $subtotal -= $discount_price;
                                                    $totalDiscount += $discount_price;
                                                }
                                            }
                                            $productsName = implode(', ', $productsNames);
                                            $productQtyTotal += $productsQtys;
                                            $costPriceTotal += $productsUnitCostPrice;
                                            $unitPriceTotal += $productsUnitSalePrice;
                                            $totalProfit += $subtotal;
                                            ?>
                                            <td style="font-size: 12px;"><?php echo $productsName; ?></td>
                                            <td><?php echo $salesPerson->username; ?></td>
                                            <td><?php echo $qery->status; ?></td>
                                            <td><?php echo $productsQtys; ?></td>
                                            <td><?php echo $productsUnitCostPrice; ?></td>
                                            <td><?php echo $productsUnitSalePrice; ?></td>
                                            <?php
                                            if ($qery->is_mart == 0) {
                                                if (PROJECT_DISCOUNT == 1 || !empty($discount_price)) {
                                                    ?>
                                                    <td><?php echo $discount_price; ?></td>
                                                <?php }
                                            } else {
                                                ?>
                                                <td>0</td>
                                                <?php
                                            } ?>
                                            <td><?php echo number_format(($subtotal), 2, '.', ''); ?></td>
                                        <?php } else {
                                            $productName = ProductSetup::GetProductSetupById($qery->product_id);
                                            $productQty = $qery->product_qty;
                                            $productQtyTotal += $productQty;
                                            $costPrice = number_format(($qery->product_qty * $qery->unit_cost_price), 2, '.', '');
                                            $costPriceTotal += $costPrice;
                                            if ($qery->is_mart == 0) {
                                                $unitPrice = number_format(($qery->product_qty * $qery->unit_sale_price), 2, '.', '');
                                                $unitPriceTotal += $unitPrice;
                                                $subtotal = $unitPrice - $costPrice;
                                                if (PROJECT_DISCOUNT == 1 || $discount_price != 0) {
                                                    $filterProducts = " WHERE invoice_id = '$qery->invoice_id'";
                                                    $productSetup = Invoice::GetInvoiceProducts($filterProducts);
                                                    $count = 0;
                                                    if ($productSetup) {
                                                        foreach ($productSetup as $productSetupRows) {
                                                            $count++;
                                                        }
                                                    }
                                                    $totalDiscountper = $qery->discount;

                                                    $subTotalDiscount = ($unitPrice * ($totalDiscountper / 100));
                                                    $totalDiscount += $subTotalDiscount;
                                                    $subtotal -= $subTotalDiscount;
                                                }
                                                $totalProfit += $subtotal;
                                            } else {
                                                $unitPrice = number_format(($qery->product_qty * $qery->unit_distributor_price), 2, '.', '');
                                                $unitPriceTotal += $unitPrice;
                                                $subtotal = $unitPrice - $costPrice;
                                                $totalProfit += $subtotal;
                                            }
                                            ?>
                                            <td style="font-size: 12px;"><?php echo $productName->name; ?></td>
                                            <td><?php echo $salesPerson->username; ?></td>
                                            <td><?php echo $qery->status; ?></td>
                                            <td><?php echo $qery->product_qty; ?></td>
                                            <td><?php echo $costPrice; ?></td>
                                            <td><?php echo $unitPrice; ?></td>
                                            <?php if ($qery->is_mart == 0) { ?>
                                                <td><?php echo number_format(($subTotalDiscount), 2, '.', ''); ?></td>
                                            <?php } else { ?>
                                                <td>0</td>
                                            <?php } ?>
                                            <td><?php echo number_format(($subtotal), 2, '.', ''); ?></td>
                                        <?php } ?>
                                    </tr>
                                    <?php
                                }
                            }

                            ?>
                            <tr>

                                <th colspan="6" style="text-align: right; border: 1px solid #000;">
                                    Total
                                </th>
                                <th style="border: 1px solid #666;"><?= $productQtyTotal ?></th>
                                <th style="border: 1px solid #666;"><?= $costPriceTotal ?></th>
                                <th style="border: 1px solid #666;"><?= $unitPriceTotal ?></th>
                                <?php if ((PROJECT_DISCOUNT == 1 || $discount_price != 0)) { ?>
                                    <th style="border: 1px solid #666;"><?= number_format(($totalDiscount), 2, '.', '') ?></th>
                                <?php } ?>
                                <th style="border: 1px solid #666;"><?= number_format(($totalProfit), 2, '.', '') ?></th>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                    <?php
                    global $db;
                    $db = new Database(DB_SERVERS, DB_USERS, DB_PASS, DB_DATABASES);
                    $db->connect();
                }
                ?>
                <table class="display bordered table striped">
                <?php
                if ($customer_name == 'All') {
                    $qryExpense = ExpenseSetup::GetExpenseSetupList($filterExpenseReport);
                    if ($qryExpense) {
                        $counter = 0;
                        $counterSerial = 0;
                        $amountTotal = 0;
                        $amountSubTotal = 0;
                        foreach ($qryExpense as $qery) {
                            $exPCategory = ExpenseCategorySetup::GetExpenseCategorySetupById($qery->parent_id);
                            $counter++;
                            if ($exPCategory->name != $mainCat && $counter != 1) {
                                echo '<span class="right">' . $amountSubTotal . '</span></th></tr>';
                            }
                            if ($catName != $exPCategory->name) {
                                $amountSubTotal = 0;
                                $counterSerial = 0;
                                echo '
    <tr><th colspan="4" style="font-size: 16px;border: 1px solid #666;"><span class="left">' . $exPCategory->name . '</span>';
                            }
                            $counterSerial++;
                            $amountSubTotal += $qery->amount;
                            $amountTotal += $qery->amount;
                            $catName = $exPCategory->name;
                            $mainCat = $exPCategory->name;
                        }
                        echo '<span class="right">' . $amountSubTotal . '</span></th></tr>';
                    }
                    if ($_SESSION['clone'] == 1 && $customer_name == 'All') {
                        global $db;
                        $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
                        $db->connect();
                        $qryExpense = ExpenseSetup::GetExpenseSetupList($filterExpenseReport);
                        if ($qryExpense) {
                            $counter = 0;
                            $counterSerial = 0;
                            $amountTotal = 0;
                            $amountSubTotal = 0;
                            foreach ($qryExpense as $qery) {
                                $exPCategory = ExpenseCategorySetup::GetExpenseCategorySetupById($qery->parent_id);
                                $counter++;
                                if ($exPCategory->name != $mainCat && $counter != 1) {
                                    echo '<span class="right">' . $amountSubTotal . '</span></th></tr>';
                                }
                                if ($catName != $exPCategory->name) {
                                    $amountSubTotal = 0;
                                    $counterSerial = 0;
                                    echo '
    <tr><th colspan="4" style="font-size: 16px;border: 1px solid #666;"><span class="left">' . $exPCategory->name . '</span>';
                                }
                                $counterSerial++;
                                $amountSubTotal += $qery->amount;
                                $amountTotal += $qery->amount;
                                $catName = $exPCategory->name;
                                $mainCat = $exPCategory->name;
                            }
                            echo '<span class="right">' . $amountSubTotal . '</span></th></tr>';
                        }

                        global $db;
                        $db = new Database(DB_SERVERS, DB_USERS, DB_PASS, DB_DATABASES);
                        $db->connect();
                    }
                    echo '</table>
    <table class="display bordered table striped">
    <tr><th colspan="4" style="font-size: 18px;border: 1px solid #666; border-top:none;"><span class="left">Total Expense</span>';
                    echo '<span class="right">' . $amountTotal . '</span></th></tr>';
                    ?>
                    </table>
                    <?php

                    //Salary
                    $filterSalaryReport = " WHERE DATE_FORMAT(date_time,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(date_time,'%Y-%m-%d')<='$qryTo' ";
                    $filterSalaryReport .= " ORDER BY employee_id";
                    $qrySalary = SalarySetup::GetSalarySetupList($filterSalaryReport);
                    if ($qrySalary) {
                        $totalSalary = 0;
                        foreach ($qrySalary as $qery) {
                            $totalSalary += $qery->amount;
                        }
                        //                                    if($qery->advance == 1){
                        //                                        echo '<table class="display table table-bordered table-striped">
                        //<tr><th colspan="4"><span class="left">'.$empId->name.' + (advance)'.'</span>';
                        //                                    }else{
                        //                                        echo '<table class="display table table-bordered table-striped">
                        //<tr><th colspan="4"><span class="left">'.$empId->name.'</span>';
                        //                                    }

                        echo '<table class="display bordered table striped">
    <tr><th colspan="4" style="font-size: 18px;border: 1px solid #666; border-top:none; border-bottom: none;"><span class="left">Total Salary</span>';
                        echo '<span class="right">' . $totalSalary . '</span></th></tr></table>';
                    }
                    if ($_SESSION['clone'] == 1 && $customer_name == 'All') {
                        global $db;
                        $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
                        $db->connect();

                        $filterSalaryReport = " WHERE DATE_FORMAT(date_time,'%Y-%m-%d')>='$qryDate' and DATE_FORMAT(date_time,'%Y-%m-%d')<='$qryTo' ";
                        $filterSalaryReport .= " ORDER BY employee_id";
                        $qrySalary = SalarySetup::GetSalarySetupList($filterSalaryReport);
                        if ($qrySalary) {
                            $totalSalary = 0;
                            foreach ($qrySalary as $qery) {
                                $totalSalary += $qery->amount;
                            }
                            //                                    if($qery->advance == 1){
                            //                                        echo '<table class="display table table-bordered table-striped">
                            //<tr><th colspan="4"><span class="left">'.$empId->name.' + (advance)'.'</span>';
                            //                                    }else{
                            //                                        echo '<table class="display table table-bordered table-striped">
                            //<tr><th colspan="4"><span class="left">'.$empId->name.'</span>';
                            //                                    }

                            echo '<table class="display bordered table striped">
    <tr><th colspan="4" style="font-size: 18px;border: 1px solid #666; border-top:none; border-bottom: none;"><span class="left">Total Salary</span>';
                            echo '<span class="right">' . $totalSalary . '</span></th></tr></table>';
                        }

                        global $db;
                        $db = new Database(DB_SERVERS, DB_USERS, DB_PASS, DB_DATABASES);
                        $db->connect();
                    }

                    $totalProfit -= $amountTotal;
                    $totalProfit -= $totalSalary;

                    echo '<table class="display bordered table striped">
    <tr><th colspan="4" style="font-size: 24px;background: #ffb3b3;border: 1px solid #666;"><span class="left">Grand Expense</span>';
                    echo '<span class="right">' . ($amountTotal + $totalSalary) . '</span></th></tr></table>';

                    echo '<table class="display bordered table striped">
    <tr><th colspan="4" style="font-size: 24px;background: #e6ffcc;border: 1px solid #666;"><span class="left">Total Profit</span>';
                    echo '<span class="right">' . $totalProfit . '</span></th></tr></table>';
                }
            } // close view
            ?>
        </div>
    </div>
</div>
</div>
<?php include('../includes/inc_footer.php'); ?>

<script type="text/javascript" src="<?= PROJECT_URL ?>js/download.js"></script>
<?php include('../includes/inc_js.php'); ?>
<script type="text/javascript" src="<?= PROJECT_URL ?>js/plugins/floatThead/jquery.floatThead.min.js"></script>
<script type="text/javascript"
        src="<?= PROJECT_URL ?>js/plugins/floatThead/jquery.floatThead-slim.min.js"></script>
<script type="application/javascript">
    $(document).ready(function () {
        $("#print").on("click", function (e) {
            var fewSeconds = 2;
            e.preventDefault();
            $('#searchfrm').prop('target', '_blank');
            $('#searchfrm').prop('action', "<?=PROJECT_ADMIN_URL?>report/print/invoice.php?").submit();
            setTimeout(function () {
                $('#searchfrm').prop('target', "");
                $('#searchfrm').prop('action', "");
            }, fewSeconds * 1000);
        });
        $('table.demo1').floatThead({
            position: 'fixed',
            scrollingTop: 65
        });
        $('table.demo1 thead').css('background', '#ECEFF1');
    });
</script>
</body>
</html>