<?php require_once('../auth/auth.php');  ?>
<?php $pritner_setup_query = mysqli_query($conn, "SELECT co_logo FROM company
           WHERE coid ='$coid'  ") or die(mysqli_error($conn));
$rowps = mysqli_fetch_assoc($pritner_setup_query);
$co_logo = $rowps['co_logo'];
?>
<?php
$search     = $_GET['search'];
$bbid       = $_GET['bbid'];
$bbrid      = $_GET['bbrid'];
$itype      = $_GET['itype'];
$nrow       = $_GET['nrow'];
$fdate      = $_GET['fdate'];
$edate      = $_GET['edate'];
$us_em_id      = $_GET['us_em_id'];

$co_rs = mysqli_query($conn, "SELECT co_name,owner_name,co_address,co_phone,co_email FROM company WHERE coid='$coid'") or die(mysqli_error($conn));
$cor = mysqli_fetch_assoc($co_rs);

if ($bbid == '0') {
    $bbidcond = '';
} elseif ($bbid > 0) {
    $bbidcond = " AND t1.bid=$bbid";
}

if ($bbrid == '0') {
    $bbridcond = '';
    $branch_name = 'All Branch';
} elseif ($bbrid > 0) {
    $bbridcond = " AND t1.brid=$bbrid";
    $branch_name = mysqli_fetch_assoc(mysqli_query($conn, "SELECT branch_name FROM branch WHERE coid='$coid' AND brid='$bbrid'"))['branch_name'];
}

if ($itype == '0') {
    $itypecond = " AND (t1.ttype='ECSL' || t1.ttype='ERSL' || t1.ttype='INSL')";
} elseif ($itype == 'ECSL') {
    $itypecond = " AND t1.ttype='ECSL'";
} elseif ($itype == 'ERSL') {
    $itypecond = " AND t1.ttype='ERSL'";
} elseif ($itype == 'INSL') {
    $itypecond = " AND t1.ttype='INSL'";
}

if ($us_em_id == '0') {
    $s_uidcond = '';
} elseif ($us_em_id > 0) {
    $s_uidcond =  " AND t1.em_id='$us_em_id' ";
}


if ($search == '0') {
    $blacksearch = '';
} else {
    $blacksearch = " AND (accounts.ac_name LIKE '%$search%' || t1.invoice LIKE '%$search%' || accounts.ac_alise LIKE '%$search%' || accounts.ac_number LIKE '%$search%' || t1.invoice LIKE '%$search%')";
}


$inv_query = mysqli_query($conn, "SELECT DATE_FORMAT(t1.date, '%d/%m/%Y') tdate,t1.invoice,t1.ttype,t1.amount,accounts.outlet_id,t1.m_invoice,
     IF((t1.ttype='ERSL' || t1.ttype='INSL'),ac_name,c_name) name,
     IF((t1.ttype='ERSL' || t1.ttype='INSL'),ac_number,c_gsm) gsm,em_acname,
      COALESCE((SELECT SUM(profit) FROM order_list WHERE invoice =t1.invoice AND active=1),0) netproft,
      COALESCE((SELECT amount FROM transaction WHERE invoice =t1.invoice AND note='esale_discount' ),0) discount,
      COALESCE((SELECT amount FROM transaction WHERE invoice =t1.invoice AND note='esale_payment' ),0) payment,
      COALESCE((SELECT amount FROM transaction WHERE invoice =t1.invoice AND note='esale_delivary'),0) delivery
      FROM transaction t1 
        LEFT JOIN accounts ON (accounts.acid=t1.dr_acid)
        LEFT JOIN c_customer ON (c_customer.c_id=t1.e_acid)
        LEFT JOIN user ON (user.uid=t1.userid)
        LEFT JOIN em_employee ON (em_employee.em_id=t1.em_id)
        WHERE t1.coid='$coid' $bbidcond $bbridcond $blacksearch $itypecond  $s_uidcond  AND date(t1.date) BETWEEN '$fdate' AND '$edate'  GROUP BY t1.invoice ORDER BY t1.date ASC LIMIT $nrow;") or die(mysqli_error($conn));
$dataa = [];
while ($sales_row = mysqli_fetch_assoc($inv_query)) {
    $dataa[] = $sales_row;
}
?>



<table class="ttable border-bottom-0 d-none" id="header" cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th rowspan="3" class="thborder border-bottom-0 text-center" style="width:10%;">
                <?php if ($co_logo != "") : ?>
                    <img class="profile-user-img img-fluid img-circle" src="data:image/gif;base64,<?php echo $co_logo; ?>" style="width:80px;height:75px;">
                <?php else : ?>
                    <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:75px;">
                <?php endif; ?>

            </th>
            <th rowspan="3" class="thborder border-bottom-0" style="width:70%;">
                <dt style="font-size:18px;font-weight: 600;"><?php echo $cor['co_name']; ?></dt>
                <dt><?php echo $cor['owner_name']; ?></dt>
                <dt><?php echo $cor['co_address']; ?></dt>
                <dt><?php echo $cor['co_phone']; ?>#<?php echo $cor['co_email']; ?></dt>
            </th>
            <th class="thborder text-center" style="width:20%;"><?php echo $branch_name; ?></th>
        </tr>
        <tr>
            <th class="thborder text-center"><?php echo language(186); ?></th>
        </tr>
        <tr>
            <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($fdate)); ?> To <?php echo date('d/m/y', strtotime($edate)); ?>
            </th>
        </tr>
    </thead>
</table>

<table id="sl_main_table" class="ttable" cellpadding="0" cellspacing="0">
    <thead>
        <tr class="sticky-top">
            <th class="text-center" style="width:2%;"><?php echo language(15); ?></th>
            <th class="text-center" style="width:7%;"><?php echo language(131); ?></th>
            <th style="width:17%;"><?php echo language(215); ?></th>
            <th class="text-center" style="width:8%;"><?php echo language(111); ?></th>
            <th class="text-center" style="width:12%;"><?php echo language(151); ?></th>
            <th class="text-center" style="width:8%;"><?php echo language(253); ?></th>
            <th class="text-center" style="width:7%;"><?php echo language(254); ?></th>
            <th class="text-center" style="width:7%;"><?php echo language(88); ?></th>
            <th class="text-center" style="width:8%;"><?php echo language(145); ?></th>
            <th class="text-center" style="width:8%;"><?php echo language(146); ?></th>
            <th class="text-center" style="width:8%;"><?php echo language(255); ?></th>
            <th class="text-center" style="width:13%;"><?php echo language(247); ?></th>
            <th class="text-center" style="width:8%;">### </th>

        </tr>
    </thead>
    <tbody id="sinv_show"></tbody>
    <tfoot>
        <tr class="sticky-bottom">
            <th class="text-right" colspan="5"><?php echo language(233); ?></th>
            <th class="text-right">
                <dt id="t_bill"></dt>
            </th>
            <th class="text-right">
                <dt id="t_delivery"></dt>
            </th>
            <th class="text-right">
                <dt id="t_discount"></dt>
            </th>
            <th class="text-right">
                <dt id="t_net_bill"></dt>
            </th>
            <th class="text-right">
                <dt id="t_payment"></dt>
            </th>
            <th class="text-right">
                <dt id="t_due_bill"></dt>
            </th>

            <th class="text-right"></th>
        </tr>
    </tfoot>
</table>
<script id="inv_script" type="text/javascript">
    $(document).ready(function() {
        let dataa = '';
        let i = 0;
        let t_bill = 0;
        let t_discount = 0;
        let net_bill = 0;
        let t_net_bill = 0;
        let t_payment = 0;
        let due_bill = 0;
        let t_due_bill = 0;
        let t_delivery = 0;

        $.each(<?php echo json_encode($dataa); ?>, function(key, value) {
            t_bill += Number(value.amount);
            t_delivery += Number(value.delivery);
            t_discount += Number(value.discount);
            net_bill = ((Number(value.amount) + Number(value.delivery)) - Number(value.discount));
            t_net_bill += Number(net_bill);
            t_payment += Number(value.payment);
            due_bill = (Number(net_bill) - Number(value.payment));
            t_due_bill += Number(due_bill);


            dataa += '<tr>';
            dataa += '<td class="text-center">' + ++i + '</td>';
            dataa += '<td class="text-center">' + value.tdate + '</td>';
            dataa += '<td>' + value.name + ' / ' + value.outlet_id + '</td>';
            dataa += '<td class="text-center">' + value.gsm + '</td>';
            dataa += '<td class="text-center btn-outline-dark" onclick="vprint(this)" invoice="' + value.invoice + '" itype="' + value.ttype + '"> ' + value.m_invoice + ' / ' + (value.invoice).split("-")[5] + '</td>';
            dataa += '<td class="text-right">' + bden.format(Number(value.amount)) + '</td>';
            dataa += '<td class="text-right">' + bden.format(Number(value.delivery)) + '</td>';
            dataa += '<td class="text-right">' + bden.format(Number(value.discount)) + '</td>';
            dataa += '<td class="text-right">' + bden.format(net_bill) + '</td>';
            dataa += '<td class="text-right">' + bden.format(Number(value.payment)) + '</td>';
            dataa += '<td class="text-right">' + bden.format(due_bill) + '</td>';
            dataa += '<td class="text-center">' + value.em_acname + '</td>';
            dataa += '<td class="text-center btn-outline-dark" onclick="retrun_index(this)"   invoice="' + value.invoice + '" itype="' + value.ttype + '"> ' + ' Retrun Now' + '</td>';
            dataa += '</tr>';
        });
        $("#sinv_show").html(dataa);
        $("#t_bill").text(bden.format(t_bill));
        $("#t_delivery").text(bden.format(t_delivery));
        $("#t_discount").text(bden.format(t_discount));
        $("#t_net_bill").text(bden.format(t_net_bill));
        $("#t_payment").text(bden.format(t_payment));
        $("#t_due_bill").text(bden.format(t_due_bill));
        $("#t_profit").text(bden.format(t_profit));
        $("#inv_script").remove();
    });
</script>

<script type="text/javascript">
    function retrun_index(retrun) {
        pagecall('../sales_retrun/sales_retrun_index.php?old_invoice=' + $(retrun).attr('invoice'), '#salmview');
    }
</script>



<script>
    function vprint(tdd) {
        <?php if ($printer_t == 'P') { ?>
            $("#modal-sm").modal({
                backdrop: "static"
            });
            if ($(tdd).attr('itype') == 'ECSL') {
                pagecall('../sales/c_sales_invoice_pos.php?invoice=' + $(tdd).attr('invoice'), '#smdview');
            } else if ($(tdd).attr('itype') == 'ERSL') {
                pagecall('../sales/sales_invoice_pos.php?invoice=' + $(tdd).attr('invoice'), '#smdview');
            } else if ($(tdd).attr('itype') == 'INSL') {
                pagecall('../sales/ins_sales_invoice.php?invoice=' + $(tdd).attr('invoice'), '#mdview');
            }
        <?php } elseif ($printer_t == 'R') { ?>
            $("#modal-lg").modal({
                backdrop: "static"
            });
            if ($(tdd).attr('itype') == 'ECSL') {
                pagecall('../sales/c_sales_invoice.php?invoice=' + $(tdd).attr('invoice'), '#mdview');
            } else if ($(tdd).attr('itype') == 'ERSL') {
                pagecall('../sales/sales_invoice.php?invoice=' + $(tdd).attr('invoice'), '#mdview');
            } else if ($(tdd).attr('itype') == 'INSL') {
                pagecall('../sales/ins_sales_invoice.php?invoice=' + $(tdd).attr('invoice'), '#mdview');
            }
        <?php } ?>
    }
</script>