<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: 0 !important;}
    body {
    -webkit-print-color-adjust:exact;
    background: #fff;
    padding: 5px;
    }

</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;


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;

// $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;
?>

<table style="width: 100%; margin: 5px 0px; background: #fff;">
    <tr style="height: 30px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'client')); ?>:</strong>&nbsp;&nbsp;<?= $client_name.$supplier_name; ?>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: left;"';}else{echo 'style="text-align: right;"';}?>>
            <strong style="font-size: 13px;"><span><?= strtoupper(print_lang($app_lang,'delivery_#')); ?>:</span> &nbsp;&nbsp;</strong><span><?= $invoice_data['invoice']; ?></span>
        </td>
    </tr>
    <tr style="height: 30px;">
        <td>
            <strong><?= strtoupper(print_lang($app_lang,'mobile')); ?>:</strong>&nbsp;&nbsp;<?= $client_mobile.$supplier_mobile; ?>
        </td>
        <td <?php if($app_direction == 'rtl'){echo 'style="text-align: left;"';}else{echo 'style="text-align: right;"';}?>>
            <strong style="font-size: 13px;"><span><?= strtoupper(print_lang($app_lang,'date')); ?>:</span> &nbsp;&nbsp;</strong><span><?= display_true_date($date); ?></span>
        </td>
    </tr>

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

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

<table style="width: 100%; background: #fff;">
    <!-- Table Heead with red backgorund -->
    <tr style="">
        <th style=" font-size: 20px !important;  background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; width: 5%;">#</th>
        
        <?php if($item_settings['fuel_parts'] == 'enable'){ ?>
            <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; width: 20%;"><?= strtoupper(print_lang($app_lang,'item_description')); ?></th>
            <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 10%;"><?= strtoupper(print_lang($app_lang,'agent')); ?></th>
            <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 10%;"><?= strtoupper(print_lang($app_lang,'plate_num')); ?></th>
        <?php }else{ ?>
            <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; width: 40%;"><?= strtoupper(print_lang($app_lang,'item_description')); ?></th>
        <?php } ?>


        
        <?php if($item_settings['item_measurements'] != 'get'){ ?>
            <?php if($item_settings['invoice_units'] == 'all_units'){ ?>
                <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 10%;"><?= strtoupper(print_lang($app_lang,'unit_1')); ?></th>
                <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 10%;"><?= strtoupper(print_lang($app_lang,'unit_2')); ?></th>
            <?php }else{ ?>
                <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 20%;"><?= strtoupper(print_lang($app_lang,'unit')); ?></th>
                <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 20%;"><?= strtoupper(print_lang($app_lang,'qty')); ?></th>
            <?php } ?>
        <?php } ?>

        <?php if($item_settings['item_measurements'] == 'get'){ ?>
            <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 5%;"><?= strtoupper(print_lang($app_lang,'width')); ?></th>
            <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 5%;"><?= strtoupper(print_lang($app_lang,'height')); ?></th>
            <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 5%;"><?= strtoupper(print_lang($app_lang,'m2')); ?></th>
            <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 5%;"><?= strtoupper(print_lang($app_lang,'qty')); ?></th>
        <?php } ?>
    </tr>
</table>


<!-- Table body data -->
<table style="width: 100%; margin-top: 15px; background: #fff !important;">
<?php
$item_details = json_decode($invoice_data['items_details'],true);
$item_settings = get_item_settings();
$sic = 1;
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;
    $item_selling_unit = $row['unit'];
    $item_selling_qty = (float)$row['qty'];
    
    $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);
        }
    }

    // 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);
    }

    // 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);
    }

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

    <tr style="border-bottom: 1px dotted black;">
        <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 5%; font-size: 15px;"><strong><?= $sic++; ?></strong></td>
        

        <?php if($item_settings['fuel_parts'] == 'enable'){ ?>
            <td style="padding: 8px; 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;
                    }
                    else
                    {
                        echo ucfirst(str_replace('MITNAME_', '', $row['item']));
                    }
                    ?>
                </strong>
                <br>
                <?= $row['narration']; ?>
            </td>
            <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 10%; font-size: 15px;"><strong><?= isset($row['agent']) ? $row['agent'] : NULL; ?></strong></td>
            <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 10%; font-size: 15px;"><strong><?= isset($row['plate']) ? $row['plate'] : NULL; ?></strong></td>
        <?php }else{ ?>
            <td style="padding: 8px; background: <?= $background_color; ?> !important; width: 40%;">
                <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;
                    }
                    else
                    {
                        echo ucfirst(str_replace('MITNAME_', '', $row['item']));
                    }
                    ?>
                </strong>
                <br>
                <?= $row['narration']; ?>
            </td>
        <?php } ?>
        
        <?php if($item_settings['item_measurements'] != 'get'){ ?>
            <?php if($item_settings['invoice_units'] == 'all_units'){ ?>
                <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 10%; font-size: 15px;"><strong><?= $unit_1_name.': '.$unit_1_qty; ?></strong></td>
                <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 10%; font-size: 15px;"><strong><?= $unit_2_name.': '.$unit_2_qty; ?></strong></td>
            <?php }else{ ?>
                <!-- <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 20%; font-size: 15px;"><strong><?= $unit_0_name; ?></strong></td>
                <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 20%; font-size: 15px;"><strong><?= $unit_0_qty; ?></strong></td> -->
                <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 20%; font-size: 15px;">
                    <strong>
                        <?php 
                        if(strpos($row['item'],"MITNAME_") === FALSE)
                        {
                            echo $unit_0_name;
                        }
                        else
                        {
                            echo ucfirst(str_replace('MITNAME_', '', $row['unit']));
                        }
                        ?>
                    </strong>
                </td>
                <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 20%; font-size: 15px;">
                    <strong>
                        <?php 
                        if(strpos($row['item'],"MITNAME_") === FALSE)
                        {
                            echo $unit_0_qty;
                        }
                        else
                        {
                            echo ucfirst(str_replace('MITNAME_', '', $row['qty']));
                        }
                        ?>
                    </strong>
                </td>
            <?php } ?>
        <?php } ?>

        <?php if($item_settings['item_measurements'] == 'get'){ ?>
            <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 5%; font-size: 15px;"><strong><?= round_decimels_digits( $item_measure_selling_width ); ?></strong></td>
            <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 5%; font-size: 15px;"><strong><?= round_decimels_digits( $item_measure_selling_height ); ?></strong></td>
            <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 5%; font-size: 15px;"><strong><?= round_decimels_digits( $item_measure_selling_m2 ); ?></strong></td>
            <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 5%; font-size: 15px;"><strong><?= round_decimels_digits( $item_measure_selling_qty ); ?></strong></td>
        <?php } ?>
    </tr>

<?php } ?>
</table>



<table style="width: 100%; margin-top: 20px;">
    <tr style="border: none;">
        <td colspan="4" style="padding-bottom: 10px; border: none;">
            <p style=" margin-left: 4px; margin-top: 30px; font-size: 12px;"><?= print_lang($app_lang,'sale_delivery_thanks_note'); ?></p>
        </td>
        <td colspan="2" style="padding: 4px; width: 250px;">
            <span style="margin-left: 4px; margin-top: 30px;"><?= strtoupper(print_lang($app_lang,'signature_stamp')); ?>_______________________________</span>
        </td>
    </tr>
</table>