<?php 
//OJB: Check if for excel export process
if($export_excel == 1){
	ob_start();
	$this->load->view("partial/header_excel");
}else{
	$this->load->view("partial/header");
}

$title="Challan";


$cart_data_array=array();

$index_cart=1;

foreach ($cart as $c_key => $cvalue) {
	
	$cart_data_array[$index_cart]['item_id']=$cvalue['item_id'];
	$cart_data_array[$index_cart]['lot_no']=$cvalue['lot_no'];
	$cart_data_array[$index_cart]['name']=str_replace("</br>"," - ",$cvalue['name']);
	$cart_data_array[$index_cart]['quantity']=$cvalue['quantity'];
	$cart_data_array[$index_cart]['item_cost_price']=$cvalue['item_cost_price'];
	$cart_data_array[$index_cart]['total_discount']=$cvalue['total_discount'];
	$cart_data_array[$index_cart]['vat_amount']=$cvalue['vat_amount'];
	$cart_data_array[$index_cart]['total_sale_amount']=$cvalue['total_sale_amount'];
	$index_cart++;
}


$safe_string_to_store = base64_encode(serialize($summary_cart));
$safe_string_to_payment = base64_encode(serialize($cart_data_array));
$safe_string_title = base64_encode(serialize($title));
$safe_string_invoice_no = base64_encode(serialize($receipt_no));
$safe_string_invoice_date = base64_encode(serialize($invoice_date));
$safe_string_customer_name = base64_encode(serialize($customer));



$stock_items_gender = array(
'fabric' => _("Fabric"),
'male' => _("Male"),
'female' => _("Female"),
'boy' => _("Boy"),
'girl' => _("Girl"),
'newlook' => _("New Look"),
'others' => _("Others"),
'' => _("Miscellaneous")
);


?>
<div id="page_title" style="margin-bottom:8px;"><?php echo $title; ?></div>
<div id="page_subtitle" style="margin-bottom:8px;text-align:left;">
<table width="100%">
	<tr>
		<td width="50%" align="left" style="font-size: 17px; color:#000000;">Invoice No: <?php echo $receipt_no;?></td>
		<td width="50%" align="right" style="font-size: 17px; color:#000000;">Invoice Date: <?php echo $invoice_date;?></td>
	</tr>
	<tr>
		<td width="70%" align="left" style="font-size: 17px; color:#000000;">Customer Name: <?php echo $customer;?></td>
		<td width="30%" align="right" style="font-size: 17px; color:#000000;"><a href="<?php echo LOCAL_PATH."/pdf_report/mpdf60/print/sales_challan.php?data=".$safe_string_to_store."&cart_data=".$safe_string_to_payment."&title=".$safe_string_title."&invoice_no=".$safe_string_invoice_no."&invoice_date=".$safe_string_invoice_date."&customer=".$safe_string_customer_name; ?>" target="_blank">PRINT</a></td>
	</tr>
</table>
</div>

<style>
thead{
    background-color:white;
}
table tr td:first-child::after {
   content: "";
   display: inline-block;
   vertical-align: middle;
   min-height: 22px;
}
table tr th:first-child::after {
   content: "";
   display: inline-block;
   vertical-align: middle;
   min-height: 22px;
}
</style>
<script type="text/javascript" language="javascript">
function moveScroll(){
	var scroll = $(window).scrollTop();
	var anchor_top = $("#maintable").offset().top;
	var anchor_bottom = $("#bottom_anchor").offset().top;
	if (scroll>anchor_top && scroll<anchor_bottom) {
		clone_table = $("#clone");
		if(clone_table.length == 0){
			clone_table = $("#maintable").clone();
			clone_table.attr('id', 'clone');
			clone_table.css({position:'fixed',
			'pointer-events': 'none',
			top:0});
			clone_table.width($("#maintable").width());
			$("#table-container").append(clone_table);
			$("#clone").css({visibility:'hidden'});
			$("#clone thead").css({'visibility':'visible','pointer-events':'auto'});
		}
	} else {
		$("#clone").remove();
	}
}
$(window).scroll(moveScroll);
</script>

<div id="table-container" style="width:100%">


<table id="maintable" width="100%">

    <thead>
    <tr>
          	<td colspan="6" style=" font-size:16px; font-weight:bold;background-color: #204132; color:#FFFFFF;text-align:center;border: 1px solid #000000;border-collapse: collapse;"> Challan Summary</td>
        </tr>
        <tr>
		<th style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Item Description
		</th>
		
		<th style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total Qty
		</th>
		
		<th style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total Price
		</th>
		
		<th style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total Discount
		</th>
		
		<th style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total VAT
		</th>
		
		<th style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total Invoice Amount
		</th>
		
	</tr>

 </thead>
    <tbody>
<?php

$serial=1;

$grand_total_qty=0;
$grand_total_sale_amount=0;
$grand_total_discount=0;
$grand_vat_amount=0;
$grand_total_invoice_amount=0;


foreach ($summary_cart as $summary_key => $summary_value) {

	$line_total_qty=0;
	$line_total_sale_amount=0;
	$line_total_discount=0;
	$line_vat_amount=0;
	$line_total_invoice_amount=0;

	?>
		<tr>
          	<td colspan="6" style=" font-size:14px; font-weight:bold;background-color: #b2e2ea; color:#000000;text-align:left;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".$serial." ".$stock_items_gender[$summary_key];?></td>
        </tr>
    	<?php
    	foreach ($summary_value as $s_key => $s_value) {
    		?>
    		<tr>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:left;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".$s_key;?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($s_value['total_qty'],2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($s_value['total_sale_amount'],2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($s_value['total_discount'],2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($s_value['vat_amount'],2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($s_value['total_invoice_amount'],2,'.',',')."&nbsp;";?></td>
       		</tr>
    		
        <?php
        $line_total_qty+=$s_value['total_qty'];
        $line_total_sale_amount+=$s_value['total_sale_amount'];
        $line_total_discount+=$s_value['total_discount'];
        $line_vat_amount+=$s_value['vat_amount'];
        $line_total_invoice_amount+=$s_value['total_invoice_amount'];

    	}
	?>
	
			<tr>
          		<td style=" font-size:12px; font-weight:bold;background-color: #e3aa37; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "Sub-Total&nbsp;"?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #e3aa37; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($line_total_qty,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #e3aa37; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($line_total_sale_amount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #e3aa37; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($line_total_discount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #e3aa37; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($line_vat_amount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #e3aa37; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($line_total_invoice_amount,2,'.',',')."&nbsp;";?></td>
       		</tr>
	
	<?php

	$grand_total_qty+=$line_total_qty;
	$grand_total_sale_amount+=$line_total_sale_amount;
	$grand_total_discount+=$line_total_discount;
	$grand_vat_amount+=$line_vat_amount;
	$grand_total_invoice_amount+=$line_total_invoice_amount;
	$serial++;
}
?>

<tr>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "Grand Total&nbsp;"?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_total_qty,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_total_sale_amount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_total_discount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_vat_amount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_total_invoice_amount,2,'.',',')."&nbsp;";?></td>
       		</tr>


</tbody>
</table>

</br>


<table id="maintable" width="100%">

    <thead>
        <tr>
        <td colspan="9" style=" font-size:16px; font-weight:bold;background-color: #204132; color:#FFFFFF;text-align:center;border: 1px solid #000000;border-collapse: collapse;"> Challan Details</td>
        </tr>
        <tr>
		<th  rowspan="2" style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
			SL
		</th>
        
		<th colspan="2" style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Item Description
		</th>
		
		<th  rowspan="2" style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
			LOT No
		</th>
		
		<th  rowspan="2" style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total Qty
		</th>
		
		<th rowspan="2" style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total Price
		</th>
		
		<th rowspan="2" style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total Discount
		</th>
		
		<th rowspan="2" style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total VAT
		</th>
		
		<th rowspan="2" style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
		 Total Invoice Amount
		</th>
		
	</tr> 
	<tr>
		<th style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
			Code
		</th>
		<th style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;">
			Name
		</th>
	</tr>
 </thead>
    <tbody>
<?php

$serial=1;

$grand_total_qty=0;
$grand_total_sale_amount=0;
$grand_total_discount=0;
$grand_vat_amount=0;
$grand_total_invoice_amount=0;

foreach ($cart as $cart_key => $cart_value) {

	?>
	
	<tr>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:center;border: 1px solid #000000;border-collapse: collapse;"> <?php echo $serial;?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:left;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".$cart_value['item_id'];?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:left;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".str_replace("</br>"," - ",$cart_value['name']);?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:center;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "&nbsp;".$cart_value['lot_no'];?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($cart_value['quantity'],2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($cart_value['item_cost_price']*$cart_value['quantity'],2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($cart_value['total_discount'],2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($cart_value['vat_amount'],2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:normal;background-color: #FFFFFF; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($cart_value['total_sale_amount']+$cart_value['vat_amount'],2,'.',',')."&nbsp;";?></td>
       		</tr>
	
	
	
	<?php

	$grand_total_qty+=number_format($cart_value['quantity'],2,'.','');
	$grand_total_sale_amount+=number_format($cart_value['item_cost_price']*$cart_value['quantity'],2,'.','');
	$grand_total_discount+=number_format($cart_value['total_discount'],2,'.','');
	$grand_vat_amount+=number_format($cart_value['vat_amount'],2,'.','');
	$grand_total_invoice_amount+=number_format($cart_value['total_sale_amount']+$cart_value['vat_amount'],2,'.','');
	$serial++;
}
?>

<tr>
          		<td colspan="4" style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo "Grand Total&nbsp;"?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_total_qty,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_total_sale_amount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_total_discount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_vat_amount,2,'.',',')."&nbsp;";?></td>
          		<td style=" font-size:12px; font-weight:bold;background-color: #ecbdb5; color:#000000;text-align:right;border: 1px solid #000000;border-collapse: collapse;"> <?php echo number_format($grand_total_invoice_amount,2,'.',',')."&nbsp;";?></td>
       		</tr>


</tbody>
</table>

</br>

<div id="bottom_anchor"></div>
</div>





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