<style type="text/css">
#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;
$supplier_name = array_key_exists($invoice_data['supplier'], $suppliers) ? $suppliers[$invoice_data['supplier']]['name'] : NULL;
$client_name = array_key_exists($invoice_data['supplier'], $clients) ? $clients[$invoice_data['supplier']]['name'] : NULL;
?>

<table style="width: 100%; margin: 10px 0px; background: #fff;">
    <tr style="margin: 10px 0px;">
        <td style="width: 25%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important;"><?= print_lang($app_lang,'date'); ?>:</strong></span> <?= $date;?> / <?= get_persian_date($date).' '.date('H:i:s',strtotime($date)); ?></td>
        <td style="width: 25%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important;"><?= print_lang($app_lang,'user'); ?>:</strong></span> <?= get_logged_user_data('name'); ?></td>
        <td style="width: 25%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important;"><?= print_lang($app_lang,'account'); ?>:</strong></span> <?= $supplier_name.$client_name; ?></td>
        <td style="width: 16%; background: #bb0d0d !important; color: #fff!important; border-radius: 0px 15px 0px 15px !important; padding: 7px 0px; text-align: center;"><strong><span style="color: #fff !important; font-size: 15px;"><?= print_lang($app_lang,'purchase_invoice_num'); ?>:</span><span style="color: #fff !important;"> <?= $invoice_data['invoice']; ?></span> -/- <span style="color: #fff !important; font-size: 15px;"><?= print_lang($app_lang,'purchase_sr_num'); ?>:</span><span style="color: #fff !important;"> <?= $invoice_data['serial_id']; ?></span></strong></td>
    </tr>
</table>

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

<table style="width: 100%; background: #fff;">
    <!-- Table Heead with red backgorund -->
    <tr style="border: 1px solid black;">
        <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; ">#</th>
        <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; "><?= strtoupper(print_lang($app_lang,'item_description')); ?></th>
        <?php if($item_settings['cbm'] == 'disable'){ ?>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'batch')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'mfg')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'expiry')); ?></th>
        <?php } ?>
        <?php if($item_settings['item_measurements'] != 'get'){ ?>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'unit')); ?></th>
        <?php } ?>
        <?php if($item_settings['item_measurements'] == 'get'){ ?>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'width')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'height')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'m2')); ?></th>
        <?php } ?>
        <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'qty')); ?></th>
        <?php if($item_settings['cbm'] == 'disable'){ ?>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'bonus')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'discount')); ?></th>
        <?php } ?>

        <?php if($item_settings['cbm'] == 'enable'){ ?>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center;"><?= strtoupper(print_lang($app_lang,'item_cbm_width')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center;"><?= strtoupper(print_lang($app_lang,'item_cbm_length')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center;"><?= strtoupper(print_lang($app_lang,'item_cbm_height')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center;"><?= strtoupper(print_lang($app_lang,'item_cbm')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center;"><?= strtoupper(print_lang($app_lang,'item_cbm_total')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center;"><?= strtoupper(print_lang($app_lang,'item_weight')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center;"><?= strtoupper(print_lang($app_lang,'item_weight_total')); ?></th>
        <?php } ?>

        <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'bundle_thead')); ?></th>
            <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'toop_thead')); ?></th>
        <?php } ?>
        <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'cost')); ?></th>
        <th style="font-size: 19px; border: 1px solid black; background: #bb0d0d !important; color: #fff !important; padding: 5px; text-align: center; "><?= strtoupper(print_lang($app_lang,'total')); ?></th>
    </tr>
    <!-- Table Heead with red backgorund -->

    <?php
        $purchase_items = json_decode($invoice_data['items'],true);
        $isn = 1;
        foreach($purchase_items as $row)
        {            
            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 = '';
            }

            if(isset($row['batch'])){$batch = $row['batch'];}else{$batch = '';}
            $purchase_item_data = array_key_exists($row['item'], $items) ? $items[$row['item']] : array('purchase_unit' => NULL, 'sale_unit' => NULL, 'production_unit' => NULL);

            $purchase_item_unit_name = NULL;
            if($row['unit'] == 'purchase')
            {
                $purchase_item_unit_name = array_key_exists($purchase_item_data['purchase_unit'],$units) ? $units[$purchase_item_data['purchase_unit']]['name'] : NULL;
            }
            if($row['unit'] == 'sale')
            {
                $purchase_item_unit_name = array_key_exists($purchase_item_data['sale_unit'],$units) ? $units[$purchase_item_data['sale_unit']]['name'] : NULL;
            }
            if($row['unit'] == 'production')
            {
                $purchase_item_unit_name = array_key_exists($purchase_item_data['production_unit'],$units) ? $units[$purchase_item_data['production_unit']]['name'] : NULL;
            }

    ?>
        <!-- items details body -->
        <tr>
            <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $isn++; ?></td>
            <td style="color: black !important; border: 1px solid black; padding: 5px; "><?= array_key_exists($row['item'], $items) ? $items[$row['item']]['name'] : NULL; ?></td>
            <?php if($item_settings['cbm'] == 'disable'){ ?>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $batch; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $mfg; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $expiry; ?></td>
            <?php } ?>

            <?php if($item_settings['item_measurements'] != 'get'){ ?>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $purchase_item_unit_name; ?></td>
            <?php } ?>

            <?php if($item_settings['item_measurements'] == 'get'){ ?>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= isset($row['width']) ? $row['width'] : 0; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= isset($row['height']) ? $row['height'] : 0; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= isset($row['m2']) ? $row['m2'] : 0; ?></td>
            <?php } ?>

            <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $row['qty']; ?></td>

            <?php if($item_settings['cbm'] == 'disable'){ ?>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $row['bonus']; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $currency_symbol; ?><?= amount_number_format($row['discount']); ?></td>
            <?php } ?>

            <?php if($item_settings['cbm'] == 'enable'){ ?>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $purchase_item_data['item_width']; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $purchase_item_data['item_length']; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $purchase_item_data['item_height']; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $purchase_item_data['cbm']; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $purchase_item_data['cbm'] * $row['qty']; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $purchase_item_data['weight']; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $purchase_item_data['weight'] * $row['qty']; ?></td>
            <?php } ?>

            <?php
                $row['bundle'] = isset($row['bundle']) ? $row['bundle'] : 0;
                $row['toop'] = isset($row['toop']) ? $row['toop'] : 0;
            ?>
            <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $row['bundle']; ?></td>
                <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $row['toop']; ?></td>
            <?php } ?>
            <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $currency_symbol; ?> <?= amount_number_format($row['cost']); ?></td>
            <td style="color: black !important; border: 1px solid black; padding: 5px; text-align: center; "><?= $currency_symbol; ?> <?= amount_number_format($row['total']); ?></td>
        </tr>
        <!-- items details body -->
    <?php } ?>

    <?php
        $colspan1 = 10;
        $colspan2 = 15;
        if($item_settings['item_measurements'] == 'get')
        {
            if($item_settings['cbm'] == 'enable')
            {
                $colspan1 = 14;
                $colspan2 = 16;
            }
            else
            {
                $colspan1 = 12;
                $colspan2 = 14;
            }
        }
        else
        {
            if($item_settings['cbm'] == 'enable')
            {
                $colspan1 = 12;
                $colspan2 = 15;
            }
            else
            {
                $colspan1 = 10;
                $colspan2 = 15;
            }
        }
    ?>

    <!-- Total Tr -->
    <tr>
        <td colspan="<?php echo $colspan1; ?>" style="border: 1px solid black; padding-bottom: 10px; border-bottom: none; border-left: none;">
            <span style="border: 2px dotted; padding: 6px; width: 90%; margin-left: 4px; margin-top: 20px; background: #f3f3f3 !important; border-radius: 0px 12px 0px 12px; display: block; height: 60px;" ><?= $invoice_data['memo']; ?></span><br>
            <span style="margin-left: 4px; margin-top: 50px;"><?= print_lang($app_lang,'signature_stamp'); ?>_______________________________</span>
        </td>
        <td colspan="3" style="border: 1px solid black; padding: 4px; background: #f3f3f3 !important;">
            <strong><?= strtoupper(print_lang($app_lang,'invoice_total')); ?>: <span style="<?php if($app_direction == 'rtl'){echo 'float: left;';}else{echo 'float: right;';}?>"><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['sub_total_amount']); ?></span></strong><br>
            <strong><?= strtoupper(print_lang($app_lang,'cash_paid')); ?>: <span style="<?php if($app_direction == 'rtl'){echo 'float: left;';}else{echo 'float: right;';}?>"><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['paid_amount']); ?></span></strong><br>
            <strong><?= strtoupper(print_lang($app_lang,'new_balance')); ?>: <span style="<?php if($app_direction == 'rtl'){echo 'float: left;';}else{echo 'float: right;';}?>"><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['pending_amount']); ?></span></strong><br>
            <strong><?= strtoupper(print_lang($app_lang,'old_balance')); ?>: <span style="<?php if($app_direction == 'rtl'){echo 'float: left;';}else{echo 'float: right;';}?>"><?= $currency_symbol; ?> <?= amount_number_format($invoice_data['old_balance']); ?></span></strong><br>
            <hr style="border-top: 1px solid #131212; margin-top: 0px; margin-bottom: 9px;">
            
            <?php
            if((float)$invoice_data['old_balance'] > 0)
            {
                $grand_total_balance = ($invoice_data['old_balance']) + ($invoice_data['pending_amount']);
            }
            else
            {
                $grand_total_balance = ($invoice_data['old_balance']) - ($invoice_data['pending_amount']);
            }
            ?>

            <strong style="color: #bb0d0d !important; font-size: 16px;"><?= strtoupper(print_lang($app_lang,'total_balance')); ?>: <span style="<?php if($app_direction == 'rtl'){echo 'float: left;';}else{echo 'float: right;';}?> color: #bb0d0d !important;"><?= $currency_symbol; ?> <?= amount_number_format( $grand_total_balance ); ?></span></strong>
        </td>
    </tr>
    <tr>
        <td colspan="<?php echo $colspan2; ?>" style="<?php if($app_direction == 'rtl'){echo 'text-align: left;';}else{echo 'text-align: right;';}?> font-size: 10px; color: #928e8e; padding-top: 10px;">ALPHA SOFTWARE | 0700573290 |Development By En Sudais Noori </td>
    </tr>
</table>