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

</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="margin: 10px 0px;">
        <td style="width: 25%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important;"><?= strtoupper(print_lang($app_lang,'date')); ?>:</strong></span> <?= display_true_date($date); ?></td>
        <td style="width: 15%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important;"><?= strtoupper(print_lang($app_lang,'user')); ?>:</strong></span> <?= get_logged_user_data('name'); ?></td>
        <td style="width: 20%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important;"><?= strtoupper(print_lang($app_lang,'client')); ?>:</strong></span> <?= $client_name.$supplier_name; ?></td>
        <td style="width: 22%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important;"><?= strtoupper(print_lang($app_lang,'mobile')); ?>:</strong></span> <?= $client_mobile.$supplier_mobile; ?></td>
        <td style="width: 18%; background: #bb0d0d !important; color: #fff!important; <?php if($app_direction == 'rtl'){echo 'border-radius: 20px 0px 20px 0px;';}else{echo 'border-radius: 0px 20px 0px 20px !important;';} ?> padding: 8px 0px; text-align: center;"><strong style="font-size: 15px;"><span style="color: #fff !important;"><?= strtoupper(print_lang($app_lang,'QOUT_#')); ?>:</span> <span style="color: #fff !important;"><?= $invoice_data['invoice']; ?></span></strong></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; <?php if($app_direction == 'rtl'){echo 'border-radius: 0px 30px 0px 0px;';}else{echo 'border-radius: 30px 0px 0px 0px;';} ?> background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; width: 5%;">#</th>
        <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 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,'invoice_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,'invoice_unit_2')); ?></th>
            <?php }else{ ?>
                <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 15%;"><?= 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: 15%;"><?= 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 } ?>

        <th style=" font-size: 20px !important; background: #bb0d0d !important; color: #fff !important; padding: 8px; text-align: center; width: 10%;"><?= strtoupper(print_lang($app_lang,'price')); ?></th>
        <th style=" font-size: 20px !important; <?php if($app_direction == 'rtl'){echo 'border-radius: 0px 0px 0px 30px;';}else{echo 'border-radius: 0px 0px 30px 0px;';} ?>; background: #bb0d0d !important; color: #fff !important; padding: 15px; text-align: center; width: 20%;"><?= strtoupper(print_lang($app_lang,'total')); ?></th>
    </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 = array_key_exists($row['item'], $items) ? $items[$row['item']] : 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; }

    // 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>
        <td style="padding: 8px; background: <?= $background_color; ?> !important; width: 40%;"><strong style="font-size: 15px;"><?= array_key_exists($row['item'], $items) ? $items[$row['item']]['name'] : NULL; ?></strong><br><?= $row['narration']; ?></td>
        
        <?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_qty; ?></strong></td>
                <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 10%; font-size: 15px;"><strong><?= $unit_2_qty; ?></strong></td>
            <?php }else{ ?>
                <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 15%; font-size: 15px;"><strong><?= $unit_0_name; ?></strong></td>
                <td style="padding: 8px; background: <?= $background_color; ?> !important; text-align: center; width: 15%; font-size: 15px;"><strong><?= $unit_0_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 } ?>

        <td style="padding: 8px; 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; 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 } ?>
</table>



<table style="width: 100%; margin-top: 10px;">
    <tr style="border: none; ">
        <td colspan="4" style="padding-bottom: 10px; border: none;">
            <span style="border: 2px dotted; width: 90%; margin-left: 4px; padding: 10px; background: #f3f3f3 !important; border-radius: 0px 25px 0px 25px; display: block; height: 100px; position: relative;" ><?= $invoice_data['memo']; ?></span><br>
            <span style="margin-left: 4px; margin-top: 50px;"><?= strtoupper(print_lang($app_lang,'signature_stamp')); ?>_______________________________</span>
            <p style=" margin-left: 4px; margin-top: 30px; font-size: 12px;"><?= print_lang($app_lang,'quotation_invoice_thanks_note'); ?></p>
        </td>
        <td colspan="2" style="padding: 4px; display: block;width: 100%;">
            <p style="background: #f3f3f3 !important; margin-bottom: 0px; border-bottom: 1px dotted; padding: 5px 10px;"><strong style="margin-left: 5px;"><?= strtoupper(print_lang($app_lang,'quotation_total')); ?>: <span <?php if($app_direction == 'rtl'){echo 'style="float: left; margin-right: 5px;"';}else{echo 'style="float: right; margin-right: 5px;"';}?>><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['gross_total_amount']); ?></span></strong></p>
            
            <hr style="border-top: 1px solid #0e0d0d; margin: 0; margin-top: 4px;">
        </td>
    </tr>
    <tr>
        <td colspan="6" <?php if($app_direction == 'rtl'){echo 'style="text-align: left;  font-size: 9px; color: #928e8e;"';}else{echo 'style="text-align: right;  font-size: 9px; color: #928e8e;"';}?>>FASTBOOKS SOFTWARE | 0707009795 | www.fastbooks.com</td>
    </tr>
</table>