<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <base href="<?php echo base_url(); ?>" />
        <title><?php echo $this->config->item('company') ?></title>
        <link rel="stylesheet" rev="stylesheet" href="<?php echo base_url(); ?>css/ospos.css" />
        <link rel="stylesheet" rev="stylesheet" href="<?php echo base_url(); ?>css/ospos_print.css"  media="print"/>
        <script>BASE_URL = '<?php echo site_url(); ?>';</script>

        <script src="<?php echo base_url(); ?>js/jquery-1.2.6.min.js" type="text/javascript" language="javascript" charset="UTF-8"></script> 
<!--        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>-->
        <script src="<?php echo base_url(); ?>js/printThis.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
        <script src="<?php echo base_url(); ?>js/jQuery.print.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
        <script src="<?php echo base_url(); ?>js/jquery.jqprint-0.3.js" type="text/javascript" language="javascript" charset="UTF-8"></script>

<!--         <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>-->
        <link type="text/css" href="<?php echo base_url(); ?>css/custom.css" rel="stylesheet" />

        <style type="text/css">
            html {
                overflow: auto;
            }
        </style>

    </head>
    <?php
    // $this->load->view("partial/header");


    if (isset($error_message)) {
    	echo '<h1 style="text-align: center;">' . $error_message . '</h1>';
    	exit;
    }
    include_once(APPPATH.'views/sales/code128.class.php');
    $barcode = new phpCode128($receipt_no, 150, 'c:\windows\fonts\verdana.ttf', 18);
    $barcode->saveBarcode("images/barcode_images/" . $receipt_no . '.png');

    $barHtml= "<div style='float:left;width:100%;'><img style='padding-top: 0px;' src='".LOCAL_PATH."/images/barcode_images/" . $receipt_no . ".png' ></div>";
    $barHtml.= "<div style='float:left;font-size: 13px;background-color: #FFFFFF;line-height:12px;margin:-14px 0px 0 80px;'>".$receipt_no."</div>";
    ?>

    <div id="receipt_wrapper" style="display:none;margin-top:0px;">
        <!--<div id="receipt_wrapper">-->
        <div id="receipt-"  style="width: 270px; height: auto; padding: 5px 2px; font-family: times new roman;font-size: 12px;margin-top:-55px;">	
            <div id="receipt_header" style="margin-top:50px; border-bottom:3px double #000000;">
                <div id="company_name" style="width:97%;"><?php echo COMPANYNAME; ?></div>
                <div id="company_address" style="width:97%;"><?php echo nl2br(LOCATION_ADDRESS); ?></div>
                <div id="company_phone" style="width:97%;"><?php echo $this->lang->line('company_phone') ?>&nbsp;<?php echo LOCATION_PHONE; ?></div>
                <div id="vat_reg_number" style="width:97%;"><?php echo $this->lang->line('vat_reg_no') ?>:&nbsp;<?php echo VAT_REG_NO; ?></div>
                <div id="website" style="width:97%;"><?php echo "Web: " . COMPANYWEB; ?></div>
            </div>
            <table id="receipt_items" style="width:97%;">    
                <tr>
                    <td  style='text-align:center;width:100%;' colspan="2">RE-PRINT</td>
                </tr>
                <tr>
                    <td  style='text-align:left;width:97%;'>Invoice# &nbsp;<?php echo $receipt_no; ?></td>
                    <td style='text-align:right;width:97%;'>Date:&nbsp; <?php echo date('d/m/Y'); ?> </td>
                </tr>
                <tr>
                    <td colspan="2" style='text-align:left;width:97%;'><?php echo $this->lang->line('sales_prepared_by') . ": " . $employee; ?></td>
                </tr>
                
               <?php
               if (isset($customer)) {
                            ?>
                            <tr>
                                <td colspan="2" style='text-align:left;width:97%;'><?php echo $this->lang->line('customers_customer_id') . ": " . $customer; ?></td>
                            </tr>
                            <tr>
                                <td colspan="2" style='text-align:left;width:97%;'><?php echo "Customer Card No" . ": " . $customer_card_no; ?></td>
                            </tr>
                            <?php
               }
                        ?>
                        <?php if ($mode == "global") { ?>
                            <tr>
                                <td colspan="2" style='text-align:left;width:97%;'><?php echo $this->lang->line('recieve_location') . ": " . $receive_location; ?></td>
                            </tr>
                        <?php } 
                        ?>

                
            </table>
          
            <?php
            //                        $colspan = 6;
            //                        $colspan2 = 3;
            //                        $colspan3 = 5;
            //                        $colspanTotal = 7;
            ?>
            <table id="receipt_items" style="width:97%;">
                <thead>
                    <tr>
                        <th style="width:10%;text-align:left"><?php echo $this->lang->line('sales_item_number'); ?></th>
                        <th style="width:17%;"><?php echo $this->lang->line('common_price'); ?></th>
                        <th style="width:16%;text-align:center;"><?php echo $this->lang->line('sales_quantity'); ?></th>
                        <th style="width:16%;text-align:center;"><?php echo $this->lang->line('sales_discount'); ?></th>
                        <th style="width:17%;text-align:right;"><?php echo $this->lang->line('sales_total'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td colspan="5" style='border-top:3px double #000000;'></td>
                    </tr>

                    <?php
                    $totalQty = 0;
                    $subTotal = 0;
                    $totaldiscount = 0;
                    foreach (array_reverse($cart, true) as $line => $item) {
                    	//$vatStr = isset($item_tax[$item['item_id']]) ? '*' : '';
                    	$vatStr = '';
                    	$totalQty = $totalQty + $item['quantity'];
                    	$subTotal = $subTotal + round(($item['quantity'] * $item['price']), 2);
                    	$discount = round((($item['discount'] / 100) * $item['price']), 2) * $item['quantity'];
                    	$totaldiscount = $totaldiscount + $discount;
                        ?>
                        <tr>
                            <td style='padding-top:5px;'><?php echo $item['item_number']; ?></td>
                            <td style='text-align:right;padding-top:5px;'><?php echo number_format(round($item['price'], 2), 2); ?></td>
                            <td style='text-align:center;padding-top:5px;'><?php echo $item['quantity']; ?></td>
                            <td style='text-align:center;padding-top:5px;'><?php echo $item['discount']; ?></td>
                            <!--<td style='text-align:right;padding-top:5px;'><?php //echo number_format(round($item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100, 2), 2);      ?></td>-->
                            <td style='text-align:right;padding-top:5px;'><?php echo number_format(round(($item['price'] * $item['quantity']), 2), 2); ?></td>
                        </tr>
                        <tr>
                        <?php
                        $cat_pos=0;
                        $cat_pos = strpos($item['name'], "</br>");
                        
                        if(intval($cat_pos)){
                        	$cat_pos=$cat_pos+5;
                        }
                        ?>
                            <td colspan="5" style='text-align:left;border-bottom:1px dashed #000000;padding-bottom:7px'><?php echo $vatStr . str_replace("</br>"," - ",substr($item['name'],$cat_pos))." @".$item['lot_no']; ?></td>
                        </tr>
                        <?php
                    }
                    ?>
                    <tr>
                        <td style='text-align:right;border-top:3px double #000000;'><?php echo $this->lang->line('sales_qty_total'); ?></td>
                        <td style='text-align:center;border-top:3px double #000000;'><?php echo $totalQty; ?></td>
                        <td colspan="2" style='text-align:right;border-top:3px double #000000;'><?php echo $this->lang->line('sales_sub_total'); ?>:</td>
                        <td style='text-align:right;border-top:3px double #000000;'><?php echo number_format(round($subTotal, 2), 2); ?></td>
                    </tr>

                    <tr>
                        <td colspan="4" style='text-align:right;'><?php echo $this->lang->line('discount_amount') ?>:</td>
                        <td style='text-align:right'><?php echo number_format(round($totaldiscount, 2), 2); ?></td>
                    </tr>
                    <?php foreach ($taxes as $name => $value) { ?>
                        <tr>
                            <td colspan="4" style='text-align:right;'><?php echo $name; ?>:</td>
                            <td style='text-align:right;'><?php echo number_format(round($value, 2), 2); ?></td>
                        </tr>
                        <?php
                        $total_tax = $total_tax + $value;
                    }
                    ?>

                    <?php if ($this->session->userdata['customer'] != -1) { ?>
                        <tr>
                            <td colspan="4" style="text-align:right;">Use Points:</td>
                            <td style="text-align:right;"><?php echo "-" . number_format(round($cart_point_cost, 2), 2); ?></td>
                        </tr> 
                        <?php 	if(POINT_CARD_ACTIVE==1){ ?>
                        <tr>
                            <td colspan="4" style="text-align:right;">Purchase Discount:</td>
                            <td style="text-align:right;"><?php echo "-" . number_format(round($purchase_discount_amount, 2), 2); ?></td>
                        </tr>
                    <?php }
                    }
                     ?>
                    <tr>
                        <?php
                        $adjectment = ceil($total) - ($subTotal + $total_tax) + $cart_point_cost+$totaldiscount;
                        ?>
                        <td colspan="4" style="text-align:right;">Adjustment(+/-):</td>
                        <td style="text-align:right;"><?php echo $adjectment > 0 ? "+" . number_format(round($adjectment, 2), 2) : number_format(round($adjectment, 2), 2); ?></td>
                    </tr>
                    
                    <?php if($final_due>0){?>
                    <tr>
                        <td colspan="4" style="text-align:right;">Previous Due(+):</td>
                        <td style="text-align:right;"><?php echo number_format(round($final_due, 2), 2); ?></td>
                    </tr>
                    <?php }?>
                    
                    <?php if($final_due<0){?>
                    <tr>
                        <td colspan="4" style="text-align:right;">Advance Payment(-):</td>
                        <td style="text-align:right;"><?php echo number_format(round($final_due, 2), 2); ?></td>
                    </tr>
                    <?php }?>
                    <tr>
                        <td colspan="4" style='text-align:right;border-top:1px solid #000000;border-bottom:1px solid #000000;'><?php echo $this->lang->line('sales_total'); ?></td>
                        <td style='text-align:right;border-top:1px solid #000000;border-bottom:1px solid #000000;'><?php echo number_format(round($total + $final_due, 2)-round($purchase_discount_amount, 2), 2); ?></td>
                    </tr>
					
                    <?php /* if($previous_due>=0){?>
                        <tr>
                        <td colspan="4" style='text-align:right;'><?php echo "Previous Due"; ?></td>
                        <td colspan="2" style='text-align:right'><?php echo to_currency($previous_due); ?></td>
                        </tr>

                        <?php }else{ ?>
                        <tr>
                        <td colspan="4" style='text-align:right;'><?php echo "Previous Advance"; ?></td>
                        <td colspan="2" style='text-align:right'><?php echo to_currency($previous_due); ?></td>
                        </tr>
                        <?php }

                        <tr>
                        <td colspan="4" style='text-align:right;'><?php echo "Grand Total"; ?></td>
                        <td colspan="2" style='text-align:right'><?php echo to_currency($grand_total); ?></td>
                        </tr>
                     */ ?>



                    <?php
                    $this_trans_due = 0;
                    $other_paid = 0;

                    foreach ($payments as $payment_id => $payment) {
                    	if ($payment['payment_type'] != 'User Point') {

                    		if ($payment['payment_type'] == "Due") {
                    			$this_trans_due = $this_trans_due + $payment['payment_amount'];
                    		} else {
                    			$other_paid = $other_paid + $payment['payment_amount'];
                    		}
                    		$splitpayment = explode(':', $payment['payment_type']);
                            ?>
                            <tr>
                                <?php if ($payment['payment_type'] == "Due") { ?>
                                    <td style="text-align:left;"><?php echo "This Invoice:"; ?></td>

                                    <?php if ($payment['payment_amount'] < 0) { ?>
                                        <td colspan="2" style="text-align:left;"><?php echo "Advance:"; ?> </td>
                                    <?php } else { ?>
                                        <td colspan="2" style="text-align:right;"><?php
                                        echo $splitpayment[0];
                                            ?> </td>
                                    <?php } ?>
                                    <td colspan="2" style="text-align:right"><?php echo number_format($payment['payment_amount'], 2, '.', ','); ?>  </td>
                                <?php } else { 
                                	
                                	$payment_type_show=$splitpayment[0];
                                	if($splitpayment[0]=="Purchase Discount"){
                                		$payment_type_show="Sales Discount";
                                	}elseif($splitpayment[0]=="Purchase Bonus"){
                                		$payment_type_show="Special Sales Discount";
                                	}
                                	?>
                                    <td colspan="4" style="text-align:right;"><?php echo $this->lang->line('sales_payment') . ' (' . $payment_type_show . '):'; ?></td>
                                    <td style="text-align:right;"><?php echo number_format($payment['payment_amount'], 2, '.', ','); ?>  </td>
                                <?php } ?>

                            </tr>
                            <?php
                    	}
                    }
                    ?>


                    <tr>
                            <td colspan="4" style='text-align:right;'><?php echo $this->lang->line('sales_change_due'); ?></td>
                            <?php
                            if ($previous_due <= 0) {
                            	echo"<td style='text-align:right'>0.00</td>";
                            } else {
                            	echo"<td style='text-align:right'>" . number_format(round($grand_total) - round($total + $final_due), 2) . "</td>";
                            }
                            ?>
                        </tr>
                  
                </tbody>
            </table>

            <!--	<div id="sale_return_policy">
            <?php // echo nl2br($this->config->item('return_policy'));        ?>
                    </div>-->
            <div id='foot' style="text-align:center;width:97%;">
                <p>Exchange Not Allowed Without This Recept.</p>
                <p>Thank You.</p>
            </div>
            
           <table id="receipt_items">
           		 <tr>
                 	<td style='text-align:center;'>
                 	  <?php echo $barHtml;?>
                 	</td>
                 </tr>
            </table>
          
        </div>
    </div>
    <?php //exit(); ?>
    <?php
    // if ($this->Appconfig->get('print_after_sale'))
    //{
    ?>

    <script type="text/javascript">


    $(document).ready(function () {

    	//$("#receipt_wrapper").hide();

    	$("#receipt_wrapper").printThis({
    		debug: false,
    		printContainer: false,
    		pageTitle: "",
    		removeInline: false
    	});
    });
    </script>