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

    <?php
$is_integrated_credit_sale = is_sale_integrated_cc_processing();
if (isset($error_message))
{
	echo '<h1 style="text-align: center;">'.$error_message.'</h1>';
	exit;
}


?>
<br>
<div id="receipt_wrapper" style="width: 40%; height: 5%;">
	<div id="receipt_header">
		<div id="company_name" ><?php echo $this->config->item('company'); ?></div>
		<?php if($this->config->item('company_logo')) {?>
		<div id="company_logo" style="margin-top:8px; "><?php echo img(array('src' => $this->Appconfig->get_logo_image())); ?></div>
		<?php } ?>
		<div id="company_address"><?php echo nl2br($this->Location->get_info_for_key('address')); ?></div>
		<div id="company_phone"><?php echo $this->Location->get_info_for_key('phone'); ?></div>
		<?php if($this->config->item('website')) { ?>
			<div id="website"><?php echo $this->config->item('website'); ?></div>
		<?php } ?>
     
		    
        
        <div class="row">
        	<!-- English -->
        <div class="col-md-12">
				<div id="sale_receipt" style="text-align: left;"><?php echo  "Bill NO:" ?>   <span><?php echo $id; ?></span><span style="float: right;">:رقم الفاتورة  </span>
				</div>
				
				<div id="sale_receipt" style="text-align: left;"><?php echo  "DATE:" ?>   <span><?php echo $time = date("d:m:y",strtotime($transaction_time)); ?></span>
					<span style="float: right;">: تاريخ   </span>
				</div>
				
				<div id="sale_receipt" style="text-align: left;"><?php echo  "TIME:" ?>   <span><?php echo $time = date("H:i:s",strtotime($transaction_time)); ?></span>
					<span style="float: right;"> :وقت   </span>
				</div>
				<div id="sale_receipt" style="text-align: left;"><?php echo  "CASHIER:" ?>   <span><?php echo $employee; ?></span>
					<span style="float: right;"> :أمين الصندوق    </span>
				</div>
				

				<?php
				foreach($payments as $payment_id=>$payment)
			{ ?>
				
				<div colspan="<?php echo $discount_exists ? '3' : '2'; ?>" style="text-align:right;">
				
				<?php if ($is_integrated_credit_sale || sale_has_partial_credit_card_payment()) { ?>
					<div colspan="1" style="text-align:right;"> <?php $splitpayment=explode(':',$payment['payment_type']); echo $splitpayment[0]; ?>: <?php echo $payment['card_issuer']. ' '.$payment['truncated_card']; ?></div>
				<?php } else 
				{ ?>
					<div id="sale_receipt" style="text-align: left;"><?php echo  "Payment Method:" ?>   <span><?php $splitpayment=explode(':',$payment['payment_type']); echo $splitpayment[0]; ?></span>
			
												
				<?php if($payment['payment_type']=="Cash")
		         {?>
				<span style="float: right;">:طريقة الدفع / نقد
				</span> 
		        <?php  }
		         elseif ($payment['payment_type']=="Check") 
		         {?>
		         	<span style="float: right;">:طريقة الدفع / شيك
				</span>  
		       <?php } 
		       else{?>
		      <span style="float: right;">:طريقة الدفع / نقد
				</span> 
		        <?php } ?>
				
			<?php
			}}
			?>	

</div>
	</div>
	</div>
</div>

		

		<hr size="10pt" style="border-top:2px dotted #000;">

		<table id="receipt_items">
	<tr>
	<th style="width:10%;text-align:center;"><?php echo "SI"; ?> </th>
	<th style="width:10%;text-align:center;"><?php echo "ITEM"; ?> / صنف  </th>
	<th style="width:10%;text-align:center;"><?php echo lang('sales_quantity'); ?> / عدد </th>
	
	<th style="width:10%;text-align:right;"><?php echo "PRICE"; ?> /سعر </th>
	 <?php if($discount_exists) { ?>
	<th style="width:10%;text-align:right;"><?php echo "Discount"; ?> / خصم   </th>
	
<?php } ?>
	<th style="width:12%;text-align:right;"><?php echo "AMOUNT"; ?> / مجموع</th>

	</tr>

  </table>
  <hr size="10pt" style="border-top:2px dotted #000;">

 <table id="receipt_items">
	<?php $final=0;
	foreach(array_reverse($cart, true) as $line=>$item)
	{
	?>
		<tr>
		<th style="width:8%;text-align:center;"><?php echo $sal_id; ?></th>
		<th style="width:8%;text-align:center;"><?php echo $item['name']; ?></th>
		<th style="width:8%;text-align:center;"><?php if($item['name']!='Credit Account Payment') echo to_quantity($item['quantity']); ?></th>
		<th style="width:8%;text-align:center;"><?php echo to_currency($item['price']); ?></th>
		<?php
		if($discount_exists) {
		   $actual_prices=$item['quantity'] * $item['price'];
		   $final+=$item['price']*$item['quantity'];

			$dis=$item['discount'] / 100;
            $discount_amount=$dis*$item['price'];

			?>
			<th style="width:10%;text-align:center;"><?php echo $item['discount']."%"; ?></th>
			
		<?php }?> 
		<th style="width:8%;text-align:right;"><?php
		 echo to_currency($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100); ?></th>

	
		</tr>
		<table id="receipt_items">
	    <tr style="display:none">         
	    <td colspan="2" align="center"><?php echo $item['description']; ?></td>
        <td colspan="2" ><?php echo isset($item['serialnumber']) ? $item['serialnumber'] : ''; ?></td>
		
		<?php if($discount_exists) {?>
		<td colspan="1"><?php echo '&nbsp;' .$item['discount']; ?></td>
		<?php } ?>
	    </tr>

	<?php
	}
	?>


		</table>

		  <hr size="10pt" style="border-top:2px dotted #000;">
		

        <div class="row">
        	<!-- English -->
        <div class="col-md-6">
        	<?php foreach($taxes as $name=>$value) { //print_r($taxes); ?>
			<div colspan="<?php echo "<b>".$discount_exists."</b>" ? '4' : '3'; ?>" style='text-align:left;'><?php echo "<b>TOTAL TAX</b>"; ?> </div>

			<div colspan="1" style='text-align:left;'><?php echo "<b>".$name.": SAR</b>"; ?><?php echo "<b>".number_format($value,2)."</b>" ; ?><b>  /ضريبة   </b>
				
			</div>
			<?php }; ?>

			<?php if($discount_exists) 
		{
		?>
		
		<div style='text-align:left;'><?php echo "<b>Total Discount </b>";?>
<br>     <p><b></b> <?php echo "<b>SAR".$total_discount=$final-$subtotal." /خصم  </b>"; ?></p>

		</div>
		<?php
		}
		?>
	
     
		</div>
		
		<div class="col-md-6">
		<div colspan="<?php echo "<b>".$discount_exists."</b>" ? '4' : '3'; ?>" style='text-align:right;border-top:0px solid #000000;'><?php echo "<b>". lang('sales_sub_total')."</b>"; ?><b>/ مجموع </b> </div>
	<div colspan="1" style='text-align:right;border-top:0px solid #000000;'> <?php echo "<b>".to_currency($subtotal)."</b>"; ?> 
		</div>
	
	<div colspan="<?php echo "<b>".$discount_exists."</b>" ? '4' : '3'; ?>" style='text-align:right;'><?php echo "<b>". lang('sales_total')."</b>"; ?><b> / المبلغ المستحق </b></div>
	<div colspan="1" style='text-align:right'><?php echo  $this->config->item('round_cash_on_sales') && $is_sale_cash_payment ?  to_currency(round_to_nearest_05($total)) :"<b>". to_currency($total)."</b>"; ?></div>
	
<div colspan="1" style='text-align:right'><b>Paid Amount / المبلغ المدفوع



 </b><br><?php echo "<b>".$payment_total."</b>";?></p>		</div>
	
<?php foreach($payments as $payment) {?>
		<?php if (strpos($payment['payment_type'], lang('sales_giftcard'))!== FALSE) {?>
	
		<div colspan="<?php echo "<b>".$discount_exists."</b>" ? '3' : '2'; ?>" style="text-align:right;"><?php echo "<b>". lang('sales_giftcard_balance')."</b>"; ?></div>
		<div colspan="1" style="text-align:right;"><?php echo "<b>".$payment['payment_type']."</b>";?> </div>
		<div colspan="1" style="text-align:right"><?php echo "<b>".to_currency($this->Giftcard->get_giftcard_value(end(explode(':', $payment['payment_type']))))."</b>"; ?></div>
	
		<?php }?>
	<?php }?>

	<?php if ($sales_total < $payment_total) {?>
	
		<div colspan="<?php echo "<b>".$discount_exists."</b>" ? '4' : '3'; ?>" style='text-align:right;'><?php echo "<b>".lang('sales_change_due')." /المبلغ المتبقى </b>"; ?></div>
		<div colspan="1" style='text-align:right'>
		<?php echo $this->config->item('round_cash_on_sales')  && $is_sale_cash_payment ?  to_currency(round_to_nearest_05($amount_change)) :"<b>". to_currency($amount_change)."</b>"; ?> </div>
	
	<?php
	}
	else
	{
	?>

		<div colspan="<?php echo "<b>".$discount_exists."</b>" ? '4' : '3'; ?>" style='text-align:right;'><?php echo "<b>".lang('sales_amount_due')."</b>"; ?>  </div>
		<div colspan="1" style='text-align:right'><?php echo $this->config->item('round_cash_on_sales')  && $is_sale_cash_payment ?  to_currency(round_to_nearest_05($amount_change )) : "<b>".to_currency($amount_change )."</b>"; ?></div>
	
	<?php
	} 
	?>
</div>

	</div>

	

   
 

       
      
	
	
	
	<?php if (isset($customer_balance_for_sale) && $customer_balance_for_sale !== FALSE) {?>
	<tr>
		<td colspan="<?php echo $discount_exists ? '4' : '3'; ?>" style='text-align:right;'><?php echo lang('sales_customer_account_balance'); ?></td>
		<td colspan="1" style='text-align:right'>
		<?php echo to_currency($customer_balance_for_sale); ?> </td>
	</tr>
	<?php
	}
	?>
	
	<?php
	if ($ref_no)
	{
	?>
	<tr>
		<td colspan="<?php echo $discount_exists ? '4' : '3'; ?>" style='text-align:right;'><?php echo lang('sales_ref_no'); ?></td>
		<td colspan="1" style='text-align:right'><?php echo $ref_no; ?></td>
	</tr>	
	<?php
	}
	if (isset($auth_code) && $auth_code)
	{
	?>
	<tr>
		<td colspan="<?php echo $discount_exists ? '4' : '3'; ?>" style='text-align:right;'><?php echo lang('sales_auth_code'); ?></td>
		<td colspan="1" style='text-align:right'><?php echo $auth_code; ?></td>
	</tr>	
	<?php }?>
	
	<tr>
		<td colspan="<?php echo $discount_exists ? '5' : '4'; ?>" align="right">
		<?php if($show_comment_on_receipt==1)
			{
				echo "<b>" .$comment."</b>" ;
			}
		?>
		</td>
	</tr>
	</table>
        
       

    
            <div class="row">
                <?php if(isset($customer)){
                    ?>
                <div class="col-xs-6">
                      
                    <div id="customer" ><?php echo lang('customers_customer').": ".$customer; ?></div>
                    <?php if(!empty($customer_email)){ ?><div><?php echo lang('common_email'); ?> : <?php echo $customer_email; ?></div><?php } ?>
                    <?php if(!empty($customer_phone)){ ?><div><?php echo lang('common_phone_number'); ?> : <?php echo $customer_phone; ?></div><?php } ?>    
                    <?php if(!empty($customer_address_1)){ ?><div><?php echo lang('common_address'); ?> : <?php echo $customer_address_1. ' '.$customer_address_2; ?></div><?php } ?>
                    <?php if (!empty($customer_city)) { echo $customer_city.' '.$customer_state.', '.$customer_zip;} ?>
                    <?php if (!empty($customer_country)) { echo '<div>'.$customer_country.'</div>';} ?>			
                       
                </div>
                <?php
                    } ?>
                <?php 

                if($this->Location->get_info_for_key('enable_credit_card_processing'))
                {
                        echo '<div id="mercahnt_id">'.lang('config_merchant_id').': '.$this->Location->get_info_for_key('merchant_id').'</div>';
                }
                ?>
               
                <?php if(!isset($customer)){ ?>
                <div class="col-xs-2">
                    <div class="pull-right">
                       <button class="btn btn-primary text-white hidden-print" id="new_sale_button_1" onclick="window.location='<?php echo site_url('sales'); ?>'" > <?php echo lang('sales_new_sale'); ?> </button>
                    </div>
                </div>
                <?php }?>
            </div>
    </div>
       
        
        
	<div id="sale_return_policy">
	<?php echo nl2br($this->config->item('return_policy')); ?>
  

	</div>
	<div id='barcode'>
	<?php echo "<img src='".site_url('barcode')."?barcode=$sale_id&text=$sale_id' />"; ?>
	</div>
	<?php if(!$this->config->item('hide_signature')) { ?>
	
	<div id="signature">
	
	<?php foreach($payments as $payment) {?>
		<?php if (strpos($payment['payment_type'], lang('sales_credit'))!== FALSE) {?>
			<?php echo lang('sales_signature'); ?> --------------------------------- <br />	
			<?php 
			echo lang('sales_card_statement');
			break;
			?>
	
		<?php }?>
	<?php }?>
	
	</div>
	<?php } ?>
	<CENTER><h4>THANK YOU FOR SHOPPING WITH US</h4></CENTER>
	<?php 
	 if (!$store_account_payment && $this->Employee->has_module_action_permission('sales', 'edit_sale', $this->Employee->get_logged_in_employee_info()->person_id)){

		$pieces = explode(' ',$sale_id);

	echo form_open("sales/change_sale/".$pieces[1],array('id'=>'sales_change_form')); ?>
	<button class="btn btn-primary text-white hidden-print" id="edit_sale" onclick="submit()" > <?php echo lang('sales_edit'); ?> </button>

	<?php }	?>
	</form>
	
<button class="btn btn-primary text-white hidden-print" id="print_button" onclick="print_receipt()" > <?php echo lang('sales_print'); ?> </button>
<br />
<button class="btn btn-primary text-white hidden-print" id="new_sale_button_2" onclick="window.location='<?php echo site_url('sales'); ?>'" > <?php echo lang('sales_new_sale'); ?> </button>
	
</div>
<?php $this->load->view("partial/footer"); ?>

<?php if ($this->config->item('print_after_sale'))
{
?>
<script type="text/javascript">
$(window).bind("load", function() {
	window.print();
});
</script>
<?php }  ?>

<script type="text/javascript">
function print_receipt()
 {
 
 	window.print();
 }
</script>

<?php if($is_integrated_credit_sale && $is_sale) { ?>
<script type="text/javascript">
gritter(<?php echo json_encode(lang('common_success')); ?>, <?php echo json_encode(lang('sales_credit_card_processing_success'))?>, 'gritter-item-success',false,false);
</script>
<?php } ?>

<!-- This is used for mobile apps to print receipt-->
<script type="text/print" id="print_output"><?php echo $this->config->item('company'); ?>

<?php echo $this->Location->get_info_for_key('address'); ?>

<?php echo $this->Location->get_info_for_key('phone'); ?>

<?php if($this->config->item('website')) { ?>
	<?php echo $this->config->item('website'); ?>
<?php } ?>

<?php echo $receipt_title; ?>

<?php echo $transaction_time; ?>

<?php if(isset($customer))
{
?>
<?php echo lang('customers_customer').": ".$customer; ?>

<?php if(!empty($customer_address_1)){ ?><?php echo lang('common_address'); ?>: <?php echo $customer_address_1. ' '.$customer_address_2; ?>
	
<?php } ?>
<?php if (!empty($customer_city)) { echo $customer_city.' '.$customer_state.', '.$customer_zip; ?>

<?php } ?>
<?php if (!empty($customer_country)) { echo $customer_country; ?>
	
<?php } ?>
<?php if(!empty($customer_phone)){ ?><?php echo lang('common_phone_number'); ?> : <?php echo $customer_phone; ?>
	
<?php } ?>
<?php if(!empty($customer_email)){ ?><?php echo lang('common_email'); ?> : <?php echo $customer_email; ?><?php } ?>

<?php
}
?>
<?php echo lang('sales_id').": ".$sale_id; ?>

<?php echo lang('employees_employee').": ".$employee; ?>

<?php 
if($this->Location->get_info_for_key('enable_credit_card_processing'))
{
	echo lang('config_merchant_id').': '.$this->Location->get_info_for_key('merchant_id');
}
?>

<?php echo lang('items_item'); ?>            <?php echo lang('common_price'); ?> <?php echo lang('sales_quantity'); ?><?php if($discount_exists){echo ' '.lang('sales_discount');}?> <?php echo lang('sales_total'); ?>

---------------------------------------
<?php
foreach(array_reverse($cart, true) as $line=>$item)
{
?>
<?php echo character_limiter($item['name'], 14,'...'); ?><?php echo strlen($item['name']) < 14 ? str_repeat(' ', 14 - strlen($item['name'])) : ''; ?> <?php echo str_replace('&#8209;', '-', to_currency($item['price'])); ?> <?php echo to_quantity($item['quantity']); ?><?php if($discount_exists){echo ' '.$item['discount'];}?> <?php echo str_replace('&#8209;', '-', to_currency($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100)); ?>

  <?php echo $item['description']; ?>  <?php echo isset($item['serialnumber']) ? $item['serialnumber'] : ''; ?>
	

<?php
}
?>

<?php echo lang('sales_sub_total'); ?>: <?php echo str_replace('&#8209;', '-', to_currency($subtotal)); ?>




<?php echo lang('sales_total'); ?>: <?php echo $this->config->item('round_cash_on_sales') && $is_sale_cash_payment ?  str_replace('&#8209;', '-', to_currency(round_to_nearest_05($total))) : str_replace('&#8209;', '-', to_currency($total)); ?>

<?php
	foreach($payments as $payment_id=>$payment)
{ ?>

<?php echo (isset($show_payment_times) && $show_payment_times) ?  date(get_date_format().' '.get_time_format(), strtotime($payment['payment_date'])) : lang('sales_payment'); ?>  <?php if ($is_integrated_credit_sale || sale_has_partial_credit_card_payment()) { ?><?php $splitpayment=explode(':',$payment['payment_type']);echo $splitpayment[0]; ?>: <?php echo $payment['card_issuer']. ' '.$payment['truncated_card']; ?> <?php } else { ?><?php $splitpayment=explode(':',$payment['payment_type']); echo $splitpayment[0]; ?> <?php } ?><?php echo $this->config->item('round_cash_on_sales') && $payment['payment_type'] == lang('sales_cash') ?  str_replace('&#8209;', '-', to_currency(round_to_nearest_05($payment['payment_amount']))) : str_replace('&#8209;', '-', to_currency($payment['payment_amount'])); ?>
<?php
}
?>	

<?php foreach($payments as $payment) {?>
<?php if (strpos($payment['payment_type'], lang('sales_giftcard'))!== FALSE) {?><?php echo lang('sales_giftcard_balance'); ?>  <?php echo $payment['payment_type'];?>: <?php echo str_replace('&#8209;', '-', to_currency($this->Giftcard->get_giftcard_value(end(explode(':', $payment['payment_type']))))); ?>
	<?php }?>
<?php }?>

<?php if ($amount_change >= 0) {?>
<?php echo lang('sales_change_due'); ?>: <?php echo $this->config->item('round_cash_on_sales')  && $is_sale_cash_payment ?  str_replace('&#8209;', '-', to_currency(round_to_nearest_05($amount_change))) : str_replace('&#8209;', '-', to_currency($amount_change)); ?>
<?php
}
else
{
?>
<?php echo lang('sales_amount_due'); ?>: <?php echo $this->config->item('round_cash_on_sales')  && $is_sale_cash_payment ?  str_replace('&#8209;', '-', to_currency(round_to_nearest_05($amount_change * -1))) : str_replace('&#8209;', '-', to_currency($amount_change * -1)); ?>
<?php
} 
?>
<?php if (isset($customer_balance_for_sale) && $customer_balance_for_sale !== FALSE) {?>
	
<?php echo lang('sales_customer_account_balance'); ?>: <?php echo to_currency($customer_balance_for_sale); ?>
<?php
}
?>
<?php
if ($ref_no)
{
?>

<?php echo lang('sales_ref_no'); ?>: <?php echo $ref_no; ?>
<?php
}
if (isset($auth_code) && $auth_code)
{
?>

<?php echo lang('sales_auth_code'); ?>: <?php echo $auth_code; ?>
<?php
}
?>
<?php if($show_comment_on_receipt==1){echo $comment;} ?>

<?php $this->config->item('return_policy'); ?>

<?php if(!$this->config->item('hide_signature')) { ?>
<?php foreach($payments as $payment) {?>
	<?php if (strpos($payment['payment_type'], lang('sales_credit'))!== FALSE) {?>
		
	<?php echo lang('sales_signature'); ?>: 
---------------------------------------
<?php 
echo lang('sales_card_statement');
break;
?><?php }?><?php }?><?php } ?></script>