<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;
$client_address = array_key_exists($invoice_data['client'], $clients) ? $clients[$invoice_data['client']]['address'] : 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'];
}
$supplier_name = array_key_exists($invoice_data['client'], $suppliers) ? $suppliers[$invoice_data['client']]['name'] : NULL;
$supplier_mobile = array_key_exists($invoice_data['client'], $suppliers) ? $suppliers[$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 = NULL;
    }
}
?>





<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.$supplier_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,'sale_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.$supplier_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>

    <tr style="height: 15px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_area')); ?>:</strong>&nbsp;&nbsp;<strong><?= $category_name; ?></strong>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: right; width: 150px;"';}else{echo 'style="text-align: right;"';}?>>
            <strong><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_deliverd_by')); ?>:</strong>&nbsp;&nbsp;<strong><?= $sales_man_name; ?></strong>
        </td>
    </tr>

    <tr style="height: 15px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_address')); ?>:</strong>&nbsp;&nbsp;<strong><?= $client_address; ?></strong>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: right; width: 150px;"';}else{echo 'style="text-align: right;"';}?>>
            <strong><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_booked_by')); ?>:</strong>&nbsp;&nbsp;<strong><?= $marketer_name; ?></strong>
        </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>
            <?php if($item_settings['distribution'] == 'enable'){ ?>
                <th style="width: 7%;"><?= ucfirst(print_lang($app_lang,'pharma_sales_inv_dis_2')); ?></th>
            <?php } ?>
            <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
                <th style="width: 6%;"><?= ucfirst(print_lang($app_lang,'bundle_thead')); ?></th>
                <th style="width: 6%;"><?= ucfirst(print_lang($app_lang,'toop_thead')); ?></th>
            <?php } ?>
            <th style="width: 22%; text-align: center;"><?= 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_details'],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);
            $sale_bundle = isset($row['bundle']) ? $row['bundle'] : NULL;
            $sale_toop = isset($row['toop']) ? $row['toop'] : NULL;
            $set_name = isset($row['set_name']) ? ucwords($row['set_name']) : null;
            if(!empty($set_name))
            {
                $set_name_to_show = '('.$set_name.')';
            }
            else
            {
                $set_name_to_show = null;
            }
            if(strpos($row['item'],"MITNAME_") === FALSE)
            {
                $item_name = isset($item_data['name']) ? $item_data['name'] : NULL;
            }
            else
            {
                $item_name = ucfirst(str_replace('MITNAME_', '', $row['item']));
            }
            // $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).' <span style="color: #d50000 !important; font-size: 11px !important;">'.$set_name_to_show.'</span>'; ?></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>
                <?php if($item_settings['distribution'] == 'enable'){ ?>
                    <td><?= amount_number_format($row['discount_2']); $total_discount_2[] = $row['discount_2']; ?></td>
                <?php } ?>
                <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
                    <td><?= $sale_bundle; ?></td>
                    <td><?= $sale_toop; ?></td>
                <?php } ?>
                <td style="text-align: center;"><?= 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>
                <?php if($item_settings['distribution'] == 'enable'){ ?>
                    <td><strong><?= amount_number_format(array_sum($total_discount_2)); ?></strong></td>
                <?php } ?>
                <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
                    <td></td>
                    <td></td>
                <?php } ?>
                <td style="text-align: center;"><strong><?= amount_number_format(array_sum($total_net)); ?></strong></td>
            </tr>


            <?php 
            $colspan8 = '8';
            $colspan10 = '10';
            if($item_settings['bundle_toop'] == 'disable')
            {
                $colspan8 = '6';
                $colspan10 = '8';
            }
            ?>

            <tr>
                <td rowspan="3" colspan="<?= $colspan8; ?>">
                    <?= $invoice_data['memo']; ?>
                </td>
                <td colspan="2" 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 colspan="2" 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['total_sales_amount']); ?></strong>
                </td>
            </tr>

            <tr>
                <td colspan="2" style="background: #c6e0b4 !important; border: 1px dashed transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'transport_expenses')); ?></strong>
                </td>
                <td colspan="2" 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['trans_expense']); ?></strong>
                </td>
            </tr>

            <tr>
                <td colspan="2" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'discount')); ?></strong>
                </td>
                <td colspan="2" 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 rowspan="3" colspan="<?= $colspan8; ?>" style=" border-bottom: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'pharma_sales_inv_user')); ?>: <?= get_logged_user_data('name'); ?></strong>&nbsp;<br><br>
                    <strong><?= strtoupper(print_lang($app_lang,'signature_stamp')); ?></strong>
                </td>
                <td colspan="2" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'sub_total')); ?></strong>
                </td>
                <td colspan="2" 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['sub_total']); ?></strong>
                </td>
            </tr>

            <tr>
                <td colspan="2" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'cash_received')); ?></strong>
                </td>
                <td colspan="2" 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 colspan="2" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'new_balance')); ?></strong>
                </td>
                <td colspan="2" 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['remaining_amount']); ?></strong>
                </td>
            </tr>


            <?php
            if(strpos($invoice_data['client'],"CASHSALE") === FALSE){
            ?>
            <tr>
                <td colspan="<?= $colspan8; ?>"></td>
                <td colspan="2" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                    <strong><?= strtoupper(print_lang($app_lang,'old_balance')); ?></strong>
                </td>
                <td colspan="2" 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 } ?>

            <?php
            if(strpos($invoice_data['client'],"CASHSALE") === FALSE){
                $grand_total_balance = ($invoice_data['remaining_amount']) + ($invoice_data['old_balance']);
            }
            else
            {
                if((float)$invoice_data['old_balance'] > 0)
                {
                    $grand_total_balance = ($invoice_data['remaining_amount']);
                }
                else
                {
                    $grand_total_balance = ($invoice_data['remaining_amount']);
                }
            }               
            ?>

            <tr>
                <td colspan="<?= $colspan8; ?>" style="background: #e2efda !important; "></td>
                <td colspan="2" 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 colspan="2" 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="<?= $colspan10; ?>"><strong style="font-size: 14px !important;"><?= print_lang($app_lang,'pharma_sales_inv_note'); ?></strong></td>
            </tr>
            <tr>
                <td colspan="<?= $colspan8; ?>" <?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="4" class="text-center"><strong style="font-size: 14px !important;"></strong></td>
            </tr>
    </tbody>
</table>