<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;
    }
    td,th{padding: 5px !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");
$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'];
}
$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;
?>





<table style="width: 100%; margin: 5px 0px; background: #fff;">
    <tr style="height: 30px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'client')); ?>:</strong>&nbsp;&nbsp;<strong><?= $client_name.$supplier_name; ?></strong>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: left; width: 50%;"';}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: 30px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'mobile')); ?>:</strong>&nbsp;&nbsp;<strong><?= $client_mobile.$supplier_mobile; ?></strong>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: left; width: 50%;"';}else{echo 'style="text-align: right;"';}?>>
            <strong style="font-size: 13px;"><span><?= strtoupper(print_lang($app_lang,'date')); ?>:</span> &nbsp;&nbsp;</strong><span><strong><?= $date;?> / <?= get_persian_date($date).' '.date('H:i:s',strtotime($date)); ?></strong></span>
        </td>
    </tr>

    <tr style="height: 30px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'user')); ?>:</strong>&nbsp;&nbsp;<strong><?= get_logged_user_data('name'); ?></strong>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: left; width: 50%;"';}else{echo 'style="text-align: right;"';}?>>
            <strong style="font-size: 13px;"><span><?= strtoupper(print_lang($app_lang,'currency')); ?>:</span> &nbsp;&nbsp;</strong><span><strong><?= $currency_name; ?></strong></span>
        </td>
    </tr>
</table>







<?php $item_settings = get_item_settings(); ?>




<table class="table table-bordered" style="width: 100%; margin: 0; background: #fff;">
    <tr style="border: 1px solid black !important;">
        <th style=" font-size: 18px !important; border: 1px solid black !important; background: #c6e0b4 !important; padding: 5px; text-align: center; width: 5%;">#</th>
        <?php if($item_settings['cbm'] == 'enable'){?>
            <th style=" font-size: 18px !important; background: #c6e0b4 !important; border: 1px solid black !important; padding: 8px; text-align: center; width: 12%;"><?= strtoupper(print_lang($app_lang,'qty1')); ?></th>
            <th style=" font-size: 18px !important; background: #c6e0b4 !important; border: 1px solid black !important; padding: 8px; text-align: center; width: 12%;"><?= strtoupper(print_lang($app_lang,'qty2')); ?></th>
        <?php }else{ ?>
            <th style=" font-size: 18px !important; background: #c6e0b4 !important; border: 1px solid black !important; padding: 8px; text-align: center; width: 8%;"><?= strtoupper(print_lang($app_lang,'qty')); ?></th>
        <?php } ?>
        <th style=" font-size: 18px !important; background: #c6e0b4 !important; border: 1px solid black !important; padding: 8px; width: 20%;"><?= strtoupper(print_lang($app_lang,'item_description')); ?></th>
        <th style=" font-size: 18px !important; background: #c6e0b4 !important; border: 1px solid black !important; padding: 8px; width: 10%;"><?= strtoupper(print_lang($app_lang,'item_country_thead')); ?></th>
        <th style=" font-size: 18px !important; background: #c6e0b4 !important; border: 1px solid black !important; padding: 8px; width: 10%;"><?= strtoupper(print_lang($app_lang,'item_company_thead')); ?></th>
        <th style=" font-size: 18px !important; background: #c6e0b4 !important; border: 1px solid black !important; padding: 8px; text-align: center; width: 18%;"><?= strtoupper(print_lang($app_lang,'price')); ?></th>
        <th style=" font-size: 18px !important; background: #c6e0b4 !important; border: 1px solid black !important; padding: 8px; text-align: center; width: 18%;"><?= strtoupper(print_lang($app_lang,'total')); ?></th>
    </tr>



    <?php
    $item_details = json_decode($invoice_data['items_details'],true);
    $item_settings = get_item_settings();
    $sic = 1;
    $unit_0_name_total_qty = [];
    $unit_cbm_name_total_qty = [];
    foreach($item_details as $row)
    {
        $sale_item_data_db = fetch_table_where('items','uid',$row['item'],true);
        $sale_item_data = isset($sale_item_data_db['uid']) ? $sale_item_data_db : array('purchase_unit' => NULL, 'purchase_unit_equalance' => 1, 'sale_unit' => NULL, 'sale_unit_equalance' => 1, 'production_unit' => NULL, 'production_unit_equalance' => 1);

        $unit_0_name = NULL;
        $unit_0_qty = NULL;
        $unit_1_name = NULL;
        $unit_1_qty = NULL;
        $unit_2_name = NULL;
        $unit_2_qty = NULL;
        $unit_cbm_name = NULL;
        $unit_cbm_qty = NULL;
        $item_selling_unit = $row['unit'];
        $item_selling_qty = (float)$row['qty'];
        $item_selling_bonus = (float)$row['bonus'];
        
        $item_measure_selling_width = isset($row['width']) ? (float)$row['width'] : 0;
        $item_measure_selling_height = isset($row['height']) ? (float)$row['height'] : 0;
        $item_measure_selling_m2 = isset($row['m2']) ? (float)$row['m2'] : 0;
        $item_measure_selling_qty = $item_selling_qty;

        // units and their equalances
        $purchase_unit_name = array_key_exists($sale_item_data['purchase_unit'],$units) ? $units[$sale_item_data['purchase_unit']]['name'] : NULL;
        $purchase_unit_equalance = (float)$sale_item_data['purchase_unit_equalance'];
        if((float)$sale_item_data['purchase_unit_equalance'] <= 0){ $purchase_unit_equalance = 1; }

        $sale_unit_name = array_key_exists($sale_item_data['sale_unit'],$units) ? $units[$sale_item_data['sale_unit']]['name'] : NULL;
        $sale_unit_equalance = (float)$sale_item_data['sale_unit_equalance'];
        if($sale_unit_equalance <= 0){ $sale_unit_equalance = 1; }

        $production_unit_name = array_key_exists($sale_item_data['production_unit'],$units) ? $units[$sale_item_data['production_unit']]['name'] : NULL;
        $production_unit_equalance = (float)$sale_item_data['production_unit_equalance'];
        if($production_unit_equalance <= 0){ $production_unit_equalance = 1; }

        $item_selling_unit_in_purchase_unit = 1;
        $item_selling_unit_in_purchase_unit = 1;
        $item_selling_unit_in_purchase_unit = 1;

        // convert all units qty to purchase unit qty
        if($item_selling_unit == 'purchase'){ $item_selling_unit_in_purchase_unit = $item_selling_qty / $purchase_unit_equalance; }
        if($item_selling_unit == 'sale'){ $item_selling_unit_in_purchase_unit = $item_selling_qty / $sale_unit_equalance; }
        if($item_selling_unit == 'production'){ $item_selling_unit_in_purchase_unit = $item_selling_qty / $production_unit_equalance; }

        $unit_1_name = $purchase_unit_name;
        $unit_1_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $purchase_unit_equalance);

        // if sale is in 1st unit
        if($item_selling_unit == 'purchase')
        {
            $unit_0_name = $purchase_unit_name;
            $unit_0_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $purchase_unit_equalance);
            if(!empty($sale_item_data['sale_unit']) AND $sale_item_data['sale_unit'] != $sale_item_data['purchase_unit'])
            {
                $unit_2_name = $sale_unit_name;
                $unit_2_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $sale_unit_equalance);
            }
            else if(!empty($sale_item_data['production_unit']) AND $sale_item_data['production_unit'] != $sale_item_data['purchase_unit'])
            {
                $unit_2_name = $production_unit_name;
                $unit_2_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $production_unit_equalance);
            }

            // unit 2 for cbm
            if($item_settings['cbm'] == 'enable')
            {
                if(!empty($sale_item_data['sale_unit']))
                {
                    $unit_cbm_name = $sale_unit_name;
                    $unit_cbm_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $sale_unit_equalance);
                }
            }

        }

        // if sale is in 2nd unit
        if($item_selling_unit == 'sale')
        {
            $unit_0_name = $sale_unit_name;
            $unit_0_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $sale_unit_equalance);
            $unit_2_name = $sale_unit_name;
            $unit_2_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $sale_unit_equalance);

            // unit 2 for cbm
            if($item_settings['cbm'] == 'enable')
            {
                if(!empty($sale_item_data['production_unit']))
                {
                    $unit_cbm_name = $production_unit_name;
                    $unit_cbm_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $production_unit_equalance);
                }
            }
        }

        // if sale is in 3rd unit
        if($item_selling_unit == 'production')
        {
            $unit_0_name = $production_unit_name;
            $unit_0_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $production_unit_equalance);
            $unit_2_name = $production_unit_name;
            $unit_2_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $production_unit_equalance);

            // unit 2 for cbm
            if($item_settings['cbm'] == 'enable')
            {
                if(!empty($sale_item_data['purchase_unit']))
                {
                    $unit_cbm_name = $purchase_unit_name;
                    $unit_cbm_qty = round_decimels_digits($item_selling_unit_in_purchase_unit * $purchase_unit_equalance);
                }
            }
        }

        if ($sic % 2 == 0){ $background_color = ''; }else{ $background_color = ''; }
    ?>

        <tr style="border: 1px solid black !important;">
            <td style="padding: 8px; border: 1px solid black !important; background: #e2efda !important; text-align: center; width: 5%; font-size: 15px;"><strong><?= $sic++; ?></strong></td>
            <td style="padding: 8px; border: 1px solid black !important; background: <?= $background_color; ?> !important; text-align: center; width: 5%; font-size: 15px;"><strong><?= round_decimels_digits( $item_measure_selling_qty ).' '.$unit_0_name; $unit_0_name_total_qty[] = $item_measure_selling_qty; ?></strong></td>
            <?php if($item_settings['cbm'] == 'enable'){?>
                <td style="padding: 8px; border: 1px solid black !important; background: <?= $background_color; ?> !important; text-align: center; width: 5%; font-size: 15px;"><strong><?= round_decimels_digits( $unit_cbm_qty ).' '.$unit_cbm_name; $unit_cbm_name_total_qty[] = $unit_cbm_qty; ?></strong></td>
            <?php } ?>
            
            <td style="padding: 8px; border: 1px solid black !important; background: <?= $background_color; ?> !important; width: 20%;">
                <strong style="font-size: 15px;">

                    <?php 
                    if(strpos($row['item'],"MITNAME_") === FALSE)
                    {
                        echo isset($sale_item_data_db['name']) ? $sale_item_data_db['name'] : NULL;
                        echo ' - '.$sale_item_data_db['details'];
                    }
                    else
                    {
                        echo ucfirst(str_replace('MITNAME_', '', $row['item']));
                        echo ' - '.$sale_item_data_db['details'];
                    }
                    ?>
                </strong>
                <br>
                <?= $row['narration']; ?>
            </td>
            <td style="padding: 8px; border: 1px solid black !important; background: <?= $background_color; ?> !important; text-align: center; width: 10%; font-size: 15px;"><strong><?= $sale_item_data_db['country']; ?></strong></td>
            <td style="padding: 8px; border: 1px solid black !important; background: <?= $background_color; ?> !important; text-align: center; width: 10%; font-size: 15px;"><strong><?= $sale_item_data_db['company']; ?></strong></td>
            <td style="padding: 8px; border: 1px solid black !important; background: <?= $background_color; ?> !important; text-align: center; width: 10%; font-size: 15px;"><strong><?= $currency_symbol; ?> <?= amount_number_format($row['sale_price']); ?></strong></td>
            <td style="padding: 8px; border: 1px solid black !important; background: <?= $background_color; ?> !important; text-align: center; width: 20%; font-size: 15px;"><strong><?= $currency_symbol; ?> <?= amount_number_format($row['item_total']); ?></strong></td>
        </tr>

    <?php } ?>

        <?php 
        if($item_settings['cbm'] == 'enable')
        {
            $colspan = 6; 
            $colspan_t = 8; 
            $colspan_fb = 4; 
            $colspan_in = NULL;
        }
        else
        {
            $colspan = 5; 
            $colspan_t = 7; 
            $colspan_fb = 4; 
            $colspan_in = NULL;
        } 
        ?>
        <tr>
            <td rowspan="3" colspan="<?php echo $colspan; ?>" style="border: 1px solid black !important;">
                <?= $invoice_data['memo']; ?>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'invoice_total')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" 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_sales_amount']); ?></strong>
            </td>
        </tr>

        <tr>
            <td colspan="<?php echo $colspan_in; ?>" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'transport_expenses')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" 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="<?php echo $colspan_in; ?>" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'discount')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" 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="<?php echo $colspan; ?>" style="border: 1px solid black !important; border-bottom: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'signature_stamp')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'sub_total')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" 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="<?php echo $colspan_in; ?>" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'cash_received')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" 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="<?php echo $colspan_in; ?>" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'new_balance')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" 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="<?php echo $colspan; ?>" style="border: 1px solid black !important;"></td>
            <td colspan="<?php echo $colspan_in; ?>" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'old_balance')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" 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="<?php echo $colspan; ?>" style="background: #e2efda !important; font-size: 16px; text-align: center; border: 1px solid black !important;">
                <?= strtoupper(print_lang($app_lang,'total_item_qty')).': '.amount_number_format(array_sum($unit_0_name_total_qty)); ?>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" style="background: #c6e0b4 !important; border: 1px solid transparent !important;">
                <strong><?= strtoupper(print_lang($app_lang,'total_balance')); ?></strong>
            </td>
            <td colspan="<?php echo $colspan_in; ?>" 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($grand_total_balance); ?></strong>
            </td>
        </tr>

        <tr>
            <td colspan="<?php echo $colspan_t; ?>" style="border: 1px solid black !important;"><strong style="font-size: 14px !important;"><?= print_lang($app_lang,'sale_invoice_thanks_note'); ?></strong></td>
        </tr>
        <tr>
            <td colspan="<?php echo $colspan; ?>" <?php if($app_direction == 'rtl'){echo 'style="text-align: right; border: 1px solid black !important; font-size: 9px; color: #928e8e;"';}else{echo 'style="text-align: left; border: 1px solid black !important; font-size: 9px; color: #928e8e;"';}?>><strong>ALPHA SOFTWARE | 0700573290 | Development By En Sudais Noori</strong></td>
            <td colspan="<?php echo $colspan_fb; ?>" style="border: 1px solid black !important;" class="text-center"><strong style="font-size: 14px !important;">Print Time: &nbsp;&nbsp;<?= date("h:i:s a"); ?></strong></td>
        </tr>




</table>