<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<style>
    page {
        width: 21.6cm !important;
        height: 28cm !important;
        padding: 0px !important;  
        margin: 0px !important;
        border: 0 !important;
        color: #4f977a;
    }
    /*.nopadding{
        padding: 0px;
        margin: 0px;
    }*/
    .modal-body {
        height:auto;
    }
    .modal-lg{
        width: 21.6cm !important;
    }
    .modal-content{
        overflow-x: hidden;
        position: relative !important;
    }
    .top_section h3{
        margin-top: 0 !important;
        margin-bottom: 0 !important;
    }
    .top_section{
        height: 4.5cm !important;
    }
    .blank_td {
        height: 2.2cm !important;
        width: 13.5cm !important;
        padding: 0 !important;
    }
    .qrcode_td img {
        width: 75%;
        position: relative;
        top: 4px;
    }
    .customer_box {
        border: 1px solid #0e2797;
        /* padding: 4px; */
        border-radius: 9px;
        height: 1.1cm;
    }
    .customer_box .col-md-1, .customer_box .col-md-10{
        padding: 0px;
        margin: 0px;
    }
    .product_table_section{
        height: 15.8cm !important;
        border: 1px solid #0e2797;
        width: 20.6cm;
        margin: 0 auto;
    }
    .product_table_section .table > thead:first-child > tr:first-child > th, .product_table_section .table > thead:first-child > tr:first-child > td, .product_table_section .table-striped thead tr.primary:nth-child(odd) th {
        background-color: #b4e3c7;
        color: #329775;
        border-color: #285881;
        border-top: 1px solid #285881;
        text-align: center;
        height: 1.3cm !important;
    }
    .table>tbody>tr>td{
        vertical-align: top !important;
    }
    .logo{
        margin-left: -27px;
    }
    .top_section h2 b{
        color: #123ebc;
        font-size: 21px;
        line-height: 2;
    }
    .table_th_1{
       width:2cm; 
    }
    .table_th_2{
       width:9.5cm;  
    }
    .table_th_3{
        width:2cm; 
    }
    .table_th_4{
        width:2cm; 
    }
    .table_th_5{
        width:2cm; 
    }
    .table_th_6{
        width:1.2cm; 
    }
    .table_th_7{
        width:1.9cm; 
    }
    .customer_box_en{
        border-right: 1px solid #0e2797 !important;
        padding-left: 2px !important;
    }
    .customer_box_ar{
        border-left: 1px solid #0e2797 !important;
        padding-right: 2px !important;
        height: 40px !important;
    }
    .customer_box_print {
        padding-left: 7px !important;
        padding-right: 7px !important; 
    }
    .product_table_section .col-md-12, .invoice_table_bottom .col-md-12{
        padding: 0 !important;
    }
    .table_row{
        
    }
    .table-striped>tbody>tr>td{
        height: 1cm !important;
    }
    .product_table_section .table-bordered>thead>tr>th, .product_table_section .table-bordered>tbody>tr>th, .product_table_section .table-bordered>tfoot>tr>th, .product_table_section .table-bordered>thead>tr>td, .product_table_section .table-bordered>tbody>tr>td, .product_table_section .table-bordered>tfoot>tr>td {
        border: 1px solid #285881;
    }
    .product_table_section .table-bordered {
        border: 1px solid #fff;
    }
    .product_table_section .table-bordered>thead>tr>th, .product_table_section .table-bordered>thead>tr>td {
        border-bottom-width: 1px;
    }
    .invoice_table_bottom{
        width: 20.6cm;
        margin: 0 auto;
    }
    .invoice_table_bottom .table-bordered {
        border: 1px solid #285881;
    }
    .vat_div{
        display: flex !important;
    }
    @media print{
        .modal-content{
            -webkit-print-color-adjust: exact !important;
            position: relative !important;
        }
        *{
            overflow: visible !important;
        } 
        @page{
            /*page-break-after:always !important;*/
            page-break-after: auto !important;
        }
        page {
            width: 21.6cm !important;
            /*height: 28cm !important;*/
            padding: 0px !important;
            margin: 0px !important;
            border: 0 !important;
            color: #4f977a;
            height: 0% !important;
            font-size:17px ; 
        }
        .no_print{
            display: none !important;
        }
        .nopadding{
            padding: 0px;
            margin: 0px;
        }
        .modal-body {
            position: relative;
            padding: 0;
        }
        .top_section .col-md-4, .date_section .col-md-4, .bottom_section .col-md-4{
            width: 33.33333333%;
            float:left;
        }
        .text-center{
            text-align: center;
        }
        .col-md-12{
            width: 100%;
        }
        .top_section h3{
            margin-top: 0 !important;
            margin-bottom: 0 !important;
        }
        .date_section{
            margin-top: 4.1cm !important;
        }
        .date_print{
            margin-left: .5cm !important;
        }
        .invoiceno_print{
            margin-left: 5.8cm !important;
        }
        .customer_detail_section{
            margin-top: 0.2cm !important;
            height: 1cm !important;
            position: relative;
        }
        .customer_print{
            margin-left: 1cm !important;
            position: absolute !important;
            top:0.2cm !important;
        }
        .product_table_section{
            height: 15.8cm !important;
            border: 1px solid #0e2797;
            width: 20.6cm !important;
            margin: 0 auto !important;
            z-index:99999 !important;
        }
        /*.table_head{
            height:1.3cm !important;
        }
        .table_body_print{
            margin-top:1.3cm !important;
            height: 14.5cm !important;
        }*/
        /*.product_table_section .table>tbody>tr>td {
            padding: 2px !important;
        }*/
        .table_td_1{
            position: relative !important;
            top: 2.6cm !important;
            left:-.8cm !important;
            width: 2cm !important;
            border: 0px solid #fff !important;
            padding-bottom:0cm !important;
        }
        .table_td_2{
            position: relative !important;
            top: 2.6cm !important;
            left:-.5cm !important;
            width: 9.5cm !important;
            border: 0px solid #fff !important;
            padding-bottom:0cm !important;
        }
        .table_td_3{
            position: relative !important;
            top: 2.6cm !important;
            left:3.5cm !important;
            width: 2cm !important;
            border: 0px solid #fff !important;
            padding-bottom:0cm !important;
        }
        .table_td_4{
            position: relative !important;
            top: 2.6cm !important;
            left:4cm !important;
            width: 2cm !important;
            border: 0px solid #fff !important;
            padding-bottom:0cm !important;
        }
        .table_td_5{
            position: relative !important;
            top: 2.6cm !important;
            left:4.5cm !important;
            width: 2cm !important;
            border: 0px solid #fff !important;
            padding-bottom:0cm !important;
        }
        .table_td_6{
            position: relative !important;
            top: 2.6cm !important;
            left:4.9cm !important;
            width: 1.2cm !important;
            border: 0px solid #fff !important;
            padding-bottom:.5cm !important;
        }
        .table_td_7{
            position: relative !important;
            top: 2.6cm !important; 
            /*left:5.35cm !important;*/
            left:4.8cm;
            width: 1.9cm !important;
            border: 0px solid #fff !important;
            padding-bottom:.5cm !important;
            padding-right: 0 !important;
            /*text-align: right !important;*/
        }
        .invoice_table_bottom{
            margin-top: 2.3cm !important;
            position:relative;
            top:3.5cm!important;
        }
        .blank_td{
            width: 13.5cm !important;
            height: 2.2cm !important;
        }
        .qrcode_td img {
            width: 100% !important;
            position: relative;
            top:3.7cm;
            z-index:99999 !important;
            left: 12cm;
        }
        .count_td_print{
            width: 2.5cm !important;
            height: .8cm !important;
            position: relative;
            left:4.5cm;
        }
        .table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td, .table>tbody>tr>td, .table>tfoot>tr>td {
            padding: 8px;
            line-height: 1.42857143;
            vertical-align: top;
            border-top: 0px solid #fff !important;
        }
        .table-bordered>thead>tr>th, .table-bordered>tbody>tr>th, .table-bordered>tfoot>tr>th, .table-bordered>thead>tr>td, .table-bordered>tbody>tr>td, .table-bordered>tfoot>tr>td {
            border: 0px solid #fff !important;
        }
        .customer_box {
            border: 1px solid #fff;
            /* padding: 4px; */
            border-radius: 9px;
            height: 1.1cm;
            position: relative;
            top:-.2cm;
        }
        .customer_box_print .en_name{
            margin-left: .8cm !important;
        }
        .cust_vat{
            margin-left: .8cm !important;
        }
        .customer_box_print .ar_name{
            /*margin-right: 7cm !important;*/
            position:relative;
            right:-3.5cm;
        }  
        .pull-right{
            float: right !important;
        }
        .col-md-1 {
            width: 8.33333333% !important;
        }
        .col-md-10 {
            width: 83.33333333% !important;
        }
        .col-md-5 {
            width: 41.66666667%  !important;
        }
        .col-md-2 {
            width: 16.66666667% !important;
        }
        .product_table_section .col-md-12, .invoice_table_bottom .col-md-12{
            padding: 0px !important;
            margin:0px !important;
        }
    }
</style>


<div class="modal-dialog modal-lg no-modal-header nopadding">
    <div class="modal-content nopadding">
        <page class="nopadding">
            <div class="modal-body nopadding">
                <div class="row no_print">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        <i class="fa fa-2x">&times;</i>
                    </button>
                    <button type="button" class="btn btn-xs btn-default no-print pull-right" style="margin-right:15px;" onclick="window.print();">
                        <i class="fa fa-print"></i> <?= lang('print'); ?>
                    </button>
                </div>
                
                <div class="row top_section no_print">
                    <div class="col-md-4 text-center no_print">
                        <h3><b><?= $biller->company ?></b></h3>
                        <span>
                            <?php
                    echo $biller->address . "<br>" . $biller->city . " " . $biller->postal_code . " " . $biller->state . "<br>" . $biller->country;

                    echo lang("tel") . ": " . $biller->phone;
                    ?>
                            <small>C.R <?php echo $biller->cf6; ?> - VAT No.: <?php echo $biller->vat_no; ?></small>
                        </span>
                    </div>
                    <div class="col-md-4 text-center no_print">
                        <img class="logo" src="<?= base_url() . 'assets/uploads/logos/' . $Settings->logo; ?>" alt="logo">
                        <h2><b>Tax Invoice/فاتورة ضريبية</b></h2>
                    </div>
                    <div class="col-md-4 text-center no_print" dir="rtl">
                        <?php
                    

                    echo "<p>";

                   
                    if ($biller->cf1 != "-" && $biller->cf1 != "") {
                        echo "<b>" . $biller->cf1 . "</b>" . "<br>";
                    }
                    if ($biller->cf2 != "-" && $biller->cf2 != "") {
                        echo "" . $biller->cf2 . "<br>";
                    }
                    if ($biller->cf3 != "-" && $biller->cf3 != "") {
                        echo "" . $biller->cf3;
                    }
                    if ($biller->cf4 != "-" && $biller->cf4 != "") {
                        echo "," . $biller->cf4;
                    }
                    if ($biller->cf5 != "-" && $biller->cf5 != "") {
                        echo "," . $biller->cf5;
                    }
                    if ($biller->cf6 != "-" && $biller->cf6 != "") {
                        echo "," . $biller->cf6;
                    }
                     if ($biller->vat_no != "-" && $biller->vat_no != "") {
                        echo "<br>" . $biller->vat_no;
                    }

                    echo "</p>"; ?>
                    </div>
                </div>
                
                <!--<div class="row top_heading_section">
                    <div class="col-md-12 text-center">
                        
                    </div>
                </div>-->
                
                <div class="row date_section nopadding">
                    <div class="col-md-4 nopadding"> 
                        <span class="no_print">Date: </span> <span class="date_print"><?= $this->sma->hrld($inv->date); ?></span> <span class="pull-right no_print">تاريخ</span>
                    </div>
                    <div class="col-md-4 nopadding">
                        <span class="no_print">A/</span>
                    </div>
                    <div class="col-md-4 nopadding">
                        <span class="no_print">Invoice No.</span> <span class="invoiceno_print">#<?= $inv->reference_no; ?></span> <span class="pull-right no_print">رقم الفاتورة</span>
                    </div>
                </div>
                
                <div class="row customer_detail_section nopadding">
                    <div class="col-md-12 nopadding">
                        <div class="customer_box">
                            <div class="no_print col-md-1 nopadding customer_box_en">Customer<br>Name</div>
                            <div class="customer_print col-md-10 nopadding customer_box_print">
                                <span class="en_name"><?= $customer->company ? $customer->company : $customer->name; ?></span>
                                <span class="ar_name pull-right"><?= $customer->cf1 ? $customer->cf1 : $customer->cf2; ?></span>
                                <br>
                                <div class="vat_div">
                                   <span class="cust_vat">VAT No/ الرقم الض: </span> &nbsp;&nbsp;<span><?= $customer->vat_no ?></span>
                                </div>
                            </div>
                            <div class="pull-right no_print col-md-1 nopadding customer_box_ar">اسم الزبون</div>
                        </div>
                    </div>
                </div>
                
                <div class="row product_table_section nopadding">
                    <div class="col-md-12 nopadding">
                        <div class="table-responsive nopadding">
                            <table class="table table-bordered table-hover table-striped print-table order-table nopadding">
                                <thead class="no_print table_head">
                                    <tr>
                                        <th class="table_th_1" rowspan="2">رمز الصنف<br>Item Code</th>
                                        <th class="table_th_2" rowspan="2">وصف<br>Description</th>
                                        <th class="table_th_3" rowspan="2">وحدة<br>Unit</th>
                                        <th class="table_th_4" rowspan="2">كمية<br>Qty</th>
                                        <th class="table_th_5">سعر الوحده<br>Unit Price</th>
                                        <th class="table_th_6" rowspan="2">ضريبة<br>VAT</th>
                                        <th class="table_th_7">السعر الكلي<br>Total Price</th>
                                    </tr>
                                    <tr>
                                        <th class="text-center">S.R</th>
                                        <th class="text-center">S.R</th>
                                    </tr>
                                </thead>
                                <tbody class="table_body_print">
                                    <?php $r = 1; $sub=$qty=0;$va=0;
                        foreach ($rows as $row):
                                                            
                                $qty = $row->unit_quantity * $row->net_unit_price;
                                $sub+=$qty;
                        ?>
                                     <tr class="table_row">
                                        <td class="table_td_1" style="border: 0px solid #fff !important;"><?= $row->product_code ?></td>
                                        <td class="table_td_2" style="border: 0px solid #fff !important;"><?= $row->product_name.' - '.$row->details ?></td>
                                        <td class="table_td_3" style="border: 0px solid #fff !important;"><?= $row->product_unit_code ?></td>
                                        <td class="table_td_4" style="border: 0px solid #fff !important;"><?= $this->sma->formatQuantity($row->unit_quantity); ?></td>
                                        <td class="table_td_5" style="border: 0px solid #fff !important;"><?= $this->sma->formatMoney1($row->net_unit_price); ?></td>
                                        <td class="table_td_6" style="border: 0px solid #fff !important;"><?= $this->sma->formatMoney1($row->item_tax); ?></td>
                                        <td class="table_td_7" style="border: 0px solid #fff !important;"><?= $this->sma->formatMoney1($qty); ?></td>
                                    </tr>
                                    <?php 
                            $tax = $row->item_tax;
							$va+= $tax;
						
                              
                            ?>
                                    <?php
                                    
                            $r++;
                        endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                
                <div class="row invoice_table_bottom nopadding">
                    <div class="col-md-12 nopadding">
                        <div class="table-responsive nopadding">   
                            <table class="table table-bordered vat_invoice_table_bottom nopadding">
                                <tbody>
                                    <tr>
                                        <td class="blank_td text-center" colspan="3" rowspan="3" style="border: 0px solid #fff !important;">
                                            <!--<img src="https://cdn.ttgtmedia.com/rms/misc/qr_code_barcode.jpg">-->
                                        </td>
                                        <td colspan="3" style="border: 0px solid #fff !important;"><span class="no_print">Total Amount</span> <span class="no_print">المبلغ الإجمالي</span></td>
                                        <td class="count_td_print" style="border: 0px solid #fff !important;"><?= $this->sma->formatMoney($sub); ?></td>
                                    </tr>
                                    <tr>
                                        <td colspan="3" style="border: 0px solid #fff !important;"><span class="no_print">VAT 15%</span> <span class="no_print"> الضريبة 15٪ </span></td>
                                        <td class="count_td_print" style="border: 0px solid #fff !important;"><?= $inv->product_tax ?></td>
                                    </tr>
                                    <tr>
                                        <td colspan="3" style="border: 0px solid #fff !important;"><span class="no_print">Discount</span> <span class="no_print"> تخفيض </span></td>
                                        <td class="count_td_print" style="border: 0px solid #fff !important;">0.00</td>
                                    </tr>
                                    <tr>
                                        <td colspan="3" style="border: 0px solid #fff !important;"></td>
                                        <td colspan="3" style="border: 0px solid #fff !important;"><span class="no_print">Net Amount</span> <span class="no_print">  كمية الشبكة </span></td>
                                        <td class="count_td_print" style="border: 0px solid #fff !important;">
                                        <?php
                                        $sub_tot=$sub;
                                        $sub_vat=$inv->product_tax;
                                        $net_amount=$sub_tot +$sub_vat;
                                        echo $this->sma->formatMoney($net_amount);
                                        ?>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                
                <div class="row bottom_section">
                    <div class="col-md-5 text-center no_print">
                        <span>استلام البضاعة بحالة جيدة كاملة</span><br>
                        <span>Goods are received complete good condition</span><br>
                        <span>Received:......................................................<span>تم الاستلام:</span></span><br>
                        <span>Sign:................................................................<span>لافتة:</span></span>
                    </div>
                    <div class="col-md-2 qrcode_td text-center">
                        <div class="order_barcodes">
                            <?php 
                             $seller_name = $biller->company;
                            // $seller_name = bin2hex($seller_name);
                            $name = strlen($biller->company);
                            $tag_name ='01';
                            $length_name =$name;
                            $first_tag = $tag_name.''.$name;
                            $first_tag_hex = $this->sma->dec2hex($length_name);
                            if(strlen($first_tag_hex) == '1'){
                                 $first_tag_hex ='0'.''.$first_tag_hex;
                             }
                             else{
                                 $first_tag_hex = $first_tag_hex;
                             }
                             
                             //$first_tag_hex_to_string = hex2bin($first_tag_hex);
                             $seller_name = $this->sma->hex2str($tag_name).''.$this->sma->hex2str($first_tag_hex).''.$seller_name;
                             //$this->sma->hex2str($content)
                             $seller_vat = $biller->vat_no;
                            // $seller_vat = bin2hex($seller_vat);
                             $vat = strlen($seller_vat);
                            $tag_vat ='02';
                             $length_vat =$vat;
                             $second_tag = $tag_vat.''.$vat;
                             $second_tag_hex =  $this->sma->dec2hex($length_vat);
                            if(strlen($second_tag_hex) == '1'){
                            $second_tag_hex ='0'.''.$second_tag_hex;
                             }
                             else{
                             $second_tag_hex = $second_tag_hex;
                            }
                            // $second_tag_hex_to_string = hex2bin($second_tag_hex);
                            
                             $seller_vat = $this->sma->hex2str($tag_vat).''.$this->sma->hex2str($second_tag_hex).''.$seller_vat;
                                   
                             $seller_date = $inv->date;
                             //$seller_date = bin2hex($seller_date);
                             $date = strlen($inv->date);
                             $tag_date ='03';
                             $length_date =$date;
                             $third_tag = $tag_date.''.$date;
                             $third_tag_hex = $this->sma->dec2hex($length_date);
                             if(strlen($third_tag_hex) == '1'){
                                 $third_tag_hex ='0'.''.$third_tag_hex;
                             }
                            else{
                             $third_tag_hex = $third_tag_hex;
                             }
                            // $third_tag_hex_to_string = hex2bin($third_tag_hex);
                             $seller_date = $this->sma->hex2str($tag_date).''.$this->sma->hex2str($third_tag_hex).''.$seller_date;
                               
                             $seller_invoice_total = (round(abs($inv->grand_total),2));
                            // $seller_invoice_total =bin2hex($seller_invoice_total);
                             $grand = strlen(round(abs($inv->grand_total),2));
                             $tag_grand ='04';
                             $length_tot =$grand;
                             $fourth_tag = $tag_grand.''.$grand;
                             $fourth_tag_hex = $this->sma->dec2hex($length_tot);
                             if(strlen($fourth_tag_hex) == '1'){
                                 $fourth_tag_hex ='0'.''.$fourth_tag_hex;
                             }
                             else{
                                 $fourth_tag_hex = $fourth_tag_hex;
                             }
                             //$fourth_tag_hex_to_string = hex2bin($fourth_tag_hex);
                             $seller_invoice_total =$this->sma->hex2str($tag_grand).''.$this->sma->hex2str($fourth_tag_hex).''.$seller_invoice_total;
                            
                             $vat_values = $va;

                            // Remove commas and decimals (ZATCA requires integers)
                            $seller_vat_value = (string)(float)$vat_values;
                            
                            //Calculate correct length
                            $vat_value_len = strlen($seller_vat_value);
                            
                            $tag_vat_value = '05';
                            $fifth_tag_hex = $this->sma->dec2hex($vat_value_len);
                            if(strlen($fifth_tag_hex) == 1){
                                $fifth_tag_hex = '0' . $fifth_tag_hex;
                            }
                            
                            // Append correctly
                            $seller_vat_value = $this->sma->hex2str($tag_vat_value) . $this->sma->hex2str($fifth_tag_hex) . $seller_vat_value;
                            $content = $seller_name.''.$seller_vat.''.$seller_date.''.$seller_invoice_total.''.$seller_vat_value;
                             
                              ?>
                              <!--<br>-->
                              <?php  //$bin =$this->sma->hex2str($content);
                                $b64 = base64_encode($content); //print_r($content);
                            
                            ?>
                              <?= $this->sma->new_qrcode('link',$b64, 256); ?>
                        </div>
                    </div>
                    <div class="col-md-5 text-center no_print">
                        <span>Salesman  بائع</span><br>
                        <span>Mobile 0551089541   الجوال 0551089541 </span>
                    </div>
                </div>
            </div>
        </page>
    </div>
</div>

<script type="text/javascript">
    $(document).ready( function() {
        $('.tip').tooltip();
    });
</script>
