<style type="text/css">
::-webkit-scrollbar {
    display: none;
}
#dynamic-content-container{background: #fff;}
    .navbar-default.navbar-static-side{display: none !important;}
    #page-wrapper{margin: 0px !important; padding: 0 !important;}
    .row.border-bottom{display: none !important;}
    .footer.fixed{display: none;}
    .ibox-content{padding: 4px !important;}
    body {
    -webkit-print-color-adjust:exact;
    background: #fff;
    padding: 4px;
    }
    .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
        border-top: 1px dashed black !important;
    }
    td,th{padding: 5px !important; font-weight: 700 !important; color: black !important;}

</style>



<table style="width: 100%; background: #fff;">
    <tr>
        <td><img style="width: 100%; height: 200px;" src="<?= get_branch_image('banner'); ?>"></td>
    </tr>
</table>
<?php

$date = date_create($invoice_data['created_at']);
$date = date_format($date,"Y-m-d H:i:s");

?>
<table style="width: 100%; background: #fff; border-bottom: 3px solid black !important;">
    <tr>
        <td><strong><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_date')); ?>:&nbsp;&nbsp;<?= $date;?> / <?= get_persian_date($date).' '.date('H:i:s',strtotime($date)); ?></strong></td>
    </tr>
</table>

<?php

$date = date_create($invoice_data['created_at']);
$date = date_format($date,"Y-m-d h:i:s");
$currency_symbol = array_key_exists($invoice_data['currency'], $currencies) ? $currencies[$invoice_data['currency']]['symbol'] : NULL;
$currency_name = array_key_exists($invoice_data['currency'], $currencies) ? $currencies[$invoice_data['currency']]['name'] : NULL;

// if(strpos($invoice_data['client'],"CASHSALE") === FALSE)
// {
//     $client_name = array_key_exists($invoice_data['client'], $clients) ? $clients[$invoice_data['client']]['name'] : NULL;
//     $client_mobile = array_key_exists($invoice_data['client'], $clients) ? $clients[$invoice_data['client']]['mobile'] : NULL;
// }
// else
// {
//     $client_name = $invoice_data['cash_client_name'];
//     $client_mobile = $invoice_data['cash_client_mobile'];
// }

$client_name = array_key_exists($invoice_data['client'], $clients) ? $clients[$invoice_data['client']]['name'] : NULL;
$client_mobile = array_key_exists($invoice_data['client'], $clients) ? $clients[$invoice_data['client']]['mobile'] : NULL;
$category_name = '';
$sales_man_name = '';
$marketer_name = '';
$client_data = fetch_table_where('accounts','uid',$invoice_data['client'],true);
$category = $client_data['category'];
$sales_man = $client_data['sales_man'];
$marketer = $client_data['marketer'];

if(!empty($category))
{
    $category_data = fetch_table_where('client_categories','uid',$category,true);
    $category_name = $category_data['name'];
}

if(!empty($invoice_data['sales_man']))
{
    $sales_man_data = fetch_table_where('sales_man','uid',$invoice_data['sales_man'],true);
    $sales_man_name = $sales_man_data['name'].' '.$sales_man_data['last_name'];
}

if(!empty($invoice_data['marketer']))
{
    $marketer_data = fetch_table_where('marketers','uid',$invoice_data['marketer'],true);
    if(!empty($marketer_data))
    {
        $marketer_name = $marketer_data['name'].' '.$marketer_data['last_name'];
    }
    else
    {
        $marketer_name = '';
    }
}
?>





<table style="width: 100%; margin: 5px 0px; background: #fff; border-bottom: 3px solid black !important;">
    <tr style="height: 20px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_customer')); ?>:</strong>&nbsp;&nbsp;<strong><?= $client_name; ?></strong>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: right; width: 150px;"';}else{echo 'style="text-align: right;"';}?>>
            <strong style="font-size: 13px;"><span><?= strtoupper(print_lang($app_lang,'trans_out_invoice_num')); ?>:</span> &nbsp;&nbsp;</strong><span><strong><?= $invoice_data['invoice']; ?></strong></span>
        </td>
    </tr>
    <tr style="height: 20px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_phone')); ?>:</strong>&nbsp;&nbsp;<strong><?= $client_mobile; ?></strong>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: right; width: 150px;"';}else{echo 'style="text-align: right;"';}?>>
            <strong style="font-size: 13px;"><span><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_currency')); ?>:</span> &nbsp;&nbsp;</strong><span><strong><?= $currency_name; ?></strong></span>
        </td>
    </tr>
</table>


<table class="table table-striped" style="width: 100%; margin: 0; background: #fff;">
    <thead>
        <tr style="border-top: 2px solid black !important; border-bottom: 2px solid black !important;">
            <th style="width: 21%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_product')); ?></th>
            <th style="width: 7%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_batch')); ?></th>
            <th style="width: 10%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_mfg')); ?></th>
            <th style="width: 10%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_expiry')); ?></th>
            <th style="width: 6%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_price')); ?></th>
            <th style="width: 6%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_qty')); ?></th>
            <th style="width: 6%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_bonus')); ?></th>
            <th style="width: 6%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_dis_1')); ?></th>
            <th style="width: 16%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_dis_2')); ?></th>
            <th style="width: 22%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_net_amount')); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $branch_id = active_branch_id();
        $details = json_decode($invoice_data['items'],true);
        $total_sale_price = array();
        $total_qty = array();
        $total_bonus = array();
        $total_discount = array();
        $total_discount_2 = array();
        $total_net = array();
        foreach($details as $row){
            $item_data = fetch_table_where('items','uid',$row['item'],true);
            $item_name = $item_data['name'];
            if(isset($row['mfg']))
            {
                if(!empty($row['mfg']))
                {
                    $mfg = display_true_date($row['mfg']);
                }
                else
                {
                    $mfg = '';
                }
            }
            else
            {
                $mfg = '';
            }

            if(isset($row['expiry']))
            {
                if(!empty($row['expiry']))
                {
                    $expiry = display_true_date($row['expiry']);
                }
                else
                {
                    $expiry = '';
                }
            }
            else
            {
                $expiry = '';
            }
            
        ?>
            <tr>
                <td><?= ucfirst($item_name); ?></td>
                <td><?= $row['batch']; ?></td>
                <td><?php echo $mfg; ?></td>
                <td><?php echo $expiry; ?></td>
                <td><?= amount_number_format($row['sale_price']); $total_sale_price[] = $row['sale_price']; ?></td>
                <td><?= amount_number_format($row['qty']); $total_qty[] = $row['qty']; ?></td>
                <td><?= amount_number_format($row['bonus']); $total_bonus[] = $row['bonus']; ?></td>
                <td><?= amount_number_format($row['discount']); $total_discount[] = $row['discount']; ?></td>
                <td><?= amount_number_format($row['discount_2']); $total_discount_2[] = $row['discount_2']; ?></td>
                <td><?= amount_number_format($row['item_total']); $total_net[] = $row['item_total']; ?></td>
            </tr>
        <?php } ?>
            <tr>
                <td><strong><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_no_of_items')); ?>:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong><?= count($details);?></td>
                <td></td>
                <td></td>
                <td></td>
                <td><strong><?= amount_number_format(array_sum($total_sale_price)); ?></strong></td>
                <td><strong><?= amount_number_format(array_sum($total_qty)); ?></strong></td>
                <td><strong><?= amount_number_format(array_sum($total_bonus)); ?></strong></td>
                <td><strong><?= amount_number_format(array_sum($total_discount)); ?></strong></td>
                <td><strong><?= amount_number_format(array_sum($total_discount_2)); ?></strong></td>
                <td><strong><?= amount_number_format(array_sum($total_net)); ?></strong></td>
            </tr>




            <tr>
                <td rowspan="5" colspan="8" >
                    <?= $invoice_data['memo']; ?>
                </td>
                <td style="background: #c6e0b4 !important; border: 1px solid transparent !important; border-top: 1px dashed black !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'invoice_total')); ?></strong>
                </td>
                <td style="background: #e2efda !important; border: 1px solid transparent !important; border-top: 1px dashed black !important; <?php if($app_direction == 'rtl'){echo 'text-align: left;';}else{echo 'text-align: right;';}?>">
                    <strong><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['grand_total']); ?></strong>
                </td>
            </tr>

            <tr>
                <td style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'discount')); ?></strong>
                </td>
                <td style="background: #e2efda !important; border: 1px solid transparent !important; <?php if($app_direction == 'rtl'){echo 'text-align: left;';}else{echo 'text-align: right;';}?>">
                    <strong><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['total_discount_amount']); ?></strong>
                </td>
            </tr>

            <tr>
                <td style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'sub_total')); ?></strong>
                </td>
                <td style="background: #e2efda !important;  border: 1px solid transparent !important; <?php if($app_direction == 'rtl'){echo 'text-align: left;';}else{echo 'text-align: right;';}?>">
                    <strong><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['gross_total_amount']); ?></strong>
                </td>
            </tr>
            <tr>
                <td style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'cash_received')); ?></strong>
                </td>
                <td style="background: #e2efda !important;  border: 1px solid transparent !important; <?php if($app_direction == 'rtl'){echo 'text-align: left;';}else{echo 'text-align: right;';}?>">
                    <strong><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['cash_received']); ?></strong>
                </td>
            </tr>

            <tr>
                <td style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'new_balance')); ?></strong>
                </td>
                <td style="background: #e2efda !important;  border: 1px solid transparent !important; <?php if($app_direction == 'rtl'){echo 'text-align: left;';}else{echo 'text-align: right;';}?>">
                    <strong><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['pending_amount']); ?></strong>
                </td>
            </tr>

            <tr>
                <td colspan="8" style="border-top: none !important;"></td>
                <td style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'old_balance')); ?></strong>
                </td>
                <td style="background: #e2efda !important;border: 1px solid transparent !important; <?php if($app_direction == 'rtl'){echo 'text-align: left;';}else{echo 'text-align: right;';}?>">
                    <strong><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['old_balance']); ?></strong>
                </td>
            </tr>

            <?php
                $grand_total_balance = ($invoice_data['pending_amount']) + ($invoice_data['old_balance']);
            ?>
            <tr>
                <td colspan="8" style="background: #e2efda !important; "></td>
                <td style="background: #c6e0b4 !important; border: 1px solid transparent !important; border-bottom: 1px dashed black !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'total_balance')); ?></strong>
                </td>
                <td style="background: #e2efda !important; border: 1px solid transparent !important; border-bottom: 1px dashed black !important; <?php if($app_direction == 'rtl'){echo 'text-align: left;';}else{echo 'text-align: right;';}?>">
                    <strong><?= $currency_symbol; ?> <?= amount_number_format($grand_total_balance); ?></strong>
                </td>
            </tr>
            <tr>
                <td colspan="10" style="border-top: 1px dashed black !important;"><strong style="font-size: 14px !important;"><?= print_lang($app_lang,'pharma_sales_inv_note'); ?></strong></td>
            </tr>
            <tr>
                <td colspan="9" <?php if($app_direction == 'rtl'){echo 'style="text-align: right; font-size: 9px; color: #928e8e;"';}else{echo 'style="text-align: left; font-size: 9px; color: #928e8e;"';}?>><strong>FASTBOOKS SOFTWARE | 0707009795 | www.fastbooks.com</strong></td>
                <td colspan="2" colspan="2" class="text-center"><strong style="font-size: 14px !important;"></strong></td>
            </tr>
    </tbody>
</table>