<!DOCTYPE html>
<html>
<head>
<!-- TABLES CSS CODE -->
<title><?= $page_title;?></title>
<!-- Bootstrap 3.3.6 -->
<link rel="stylesheet" href="<?php echo $theme_link; ?>bootstrap/css/bootstrap.min.css">
<style type="text/css">
	body{
		font-family: arial;
		font-size: 12px;
		/*font-weight: bold;*/
		padding-top:15px;
	}

	@media print {
        .no-print { display: none; }
    }
</style>
</head>
<body onload="window.print();"><!--   -->
	<?php
	$CI =& get_instance();
	
    $q1=$this->db->query("select * from db_company where id=1 and status=1");
    $res1=$q1->row();
    $company_name		=$res1->company_name;
    $company_mobile		=$res1->mobile;
    $company_phone		=$res1->phone;
    $company_email		=$res1->email;
    $company_country	=$res1->country;
    $company_state		=$res1->state;
    $company_city		=$res1->city;
    $company_address	=$res1->address;
    $company_postcode	=$res1->postcode;
	$company_logo=$res1->company_logo;
    $company_gst_no		=$res1->gst_no;//Goods and Service Tax Number (issued by govt.)
    $company_vat_number		=$res1->vat_no;//Goods and Service Tax Number (issued by govt.)

  	$q3=$this->db->query("SELECT a.sales_due,a.customer_name,a.mobile,a.phone,a.gstin,a.tax_number,a.email,
                           a.opening_balance,a.country_id,a.state_id,
                           a.postcode,a.address

                           FROM db_customers a

                           WHERE 
                           a.`id`='$customer_id'
                           ");
                           /*GROUP BY 
                           b.`customer_code`*/
    
    $res3=$q3->row();
    $customer_name=$res3->customer_name;
    $customer_mobile=$res3->mobile;
    $customer_phone=$res3->phone;
    $customer_email=$res3->email;
    $customer_country=$res3->country_id;
    $customer_state=$res3->state_id;
    $customer_address=$res3->address;
    $customer_postcode=$res3->postcode;
    $customer_gst_no=$res3->gstin;
    $customer_tax_number=$res3->tax_number;
    $customer_opening_balance=$res3->opening_balance;
    $customer_due=$res3->sales_due;

	
    ?>
	<table width="98%" align="center">
		<tr>
			
			<td align="center">
				<center><img src="<?= base_url('uploads/company/'.$company_logo);?>" width='100'></center>
				<span>													 
                <strong><?= $company_name; ?></strong><br>
                	<?php echo (!empty(trim($company_address))) ? $this->lang->line('company_address')."".$company_address."<br>" : '';?> 
		            <?= $company_city; ?>
		            <?php echo (!empty(trim($company_postcode))) ? "-".$company_postcode : '';?>
		            <br>
		            <?php echo (!empty(trim($company_gst_no))) ? $this->lang->line('gst_number').": ".$company_gst_no."<br>" : '';?>
		            <?php echo (!empty(trim($company_vat_number))) ? $this->lang->line('vat_number').": ".$company_vat_number."<br>" : '';?>
		            <?php if(!empty(trim($company_mobile))) 
		            		{ 
		            			echo $this->lang->line('phone').": ".$company_mobile;
		            			if(!empty($company_phone)){
		            				echo ",".$company_phone;
		            			}
		            			echo "<br>";
		            		}

		            ?> 
			</span>
			</td>
		</tr>
		<tr style="font-size: 20px; font-weight:bold; height:50px;"><td align="center"><strong>Credit Summary</strong></td></tr>
		<tr style="font-size: 14px; font-weight:bold;">
			<td style="padding-bottom: 20px;">
				<table width="100%">
					<tr>
						<td><?= $this->lang->line('name'); ?></td>
						<td><?= $customer_name; ?></td>
					</tr>
					<tr>
						<td><?= $this->lang->line('mobile'); ?></td>
						<td><?= $customer_mobile; ?></td>
					</tr>
					<tr>
						<td><?= $this->lang->line('address'); ?></td>
						<td><?= $company_address; ?></td>
					</tr>
					<tr>
						<td>Opening Balance</td>
						<td>Rs. <?= number_format(($customer_opening_balance),2,'.',''); ?></td>
					</tr>
					<tr>
						<td>Sales Due</td>
						<td>Rs. <?= number_format(($customer_due),2,'.',''); ?></td>
					</tr>
				</table>
				
			</td>
		</tr>
		<tr>
			<td>

				<table width="100%" cellpadding="0" cellspacing="0"  >
					<thead>
					<tr>
						<th style="font-size: 14px; text-align: center;padding-left: 2px; padding-right: 2px;">#</th>
						<th style="font-size: 14px; text-align: center;padding-left: 2px; padding-right: 2px;">Invoice No.</th>
						<th style="font-size: 14px; text-align: center;padding-left: 2px; padding-right: 2px;">Sales Date</th>
						<th style="font-size: 14px; text-align: center;padding-left: 2px; padding-right: 2px;">Days Difference</th>
						<th style="font-size: 14px; text-align: right;padding-left: 2px; padding-right: 2px;">Credit Amount</th>
						<th style="font-size: 14px; text-align: right;padding-left: 2px; padding-right: 2px;">Paid Amount</th>
						<th style="font-size: 14px; text-align: right;padding-left: 2px; padding-right: 2px;">Due Amount</th>
					</tr>
					</thead>
					<tbody style="border-width: 0.1px; font-size: 14px;">
						<?php
			              $i=0;
			              $tot_qty=0;
			              $total_grand_paid=0;
			              $total_paid=0;
			              $q2=$this->db->query(" SELECT id, sales_code, reference_no, sales_date, sales_status, customer_id, warehouse_id, other_charges_input, other_charges_tax_id, other_charges_amt, discount_to_all_input, discount_to_all_type, tot_discount_to_all_amt, subtotal, round_off, grand_total, sales_note, payment_status, paid_amount, created_date, created_time, created_by, company_id, platform, agent FROM db_sales WHERE customer_id='$customer_id' AND payment_status<>'Paid';");
			              foreach ($q2->result() as $res2) {

							$target_date = new DateTime($res2->sales_date);
							$today_date = new DateTime(); // Today's date
							$interval = $today_date->diff($target_date);
							$day_output=  $interval->days . " days";

							$grand_amt = (float)($res2->grand_total);
							$paid_amt = (float)($res2->paid_amount);
							$due_total = (float)$grand_amt-(float)$paid_amt;

								echo "<tr>";  
								echo "<td style='text-align: center; padding-left: 2px; padding-right: 2px;' valign='top'>".++$i."</td>";
								echo "<td style='text-align: center;padding-left: 2px; padding-right: 2px;'>".$res2->sales_code."</td>";
								echo "<td style='text-align: center; padding-left: 2px; padding-left: 2px;'>".$res2->sales_date."</td>";
								echo "<td style='text-align: center;padding-left: 2px; padding-left: 2px;'>".$day_output."</td>";
								
								echo "<td style='text-align: right;padding-left: 2px; padding-right: 2px;'>".number_format(($grand_amt),2,'.','')."</td>";
								
								echo "<td style='text-align: right;padding-left: 2px; padding-right: 2px;'>".number_format(($paid_amt),2,'.','')."</td>";
								echo "<td style='text-align: right;padding-left: 2px; padding-right: 2px;' >".number_format(($due_total),2,'.','')."</td>";
								echo "</tr>";  
			                  //$tot_qty+=$res2->sales_qty;
			                  $total_grand_paid+=$due_total;
			              }
			              ?>					
				   	</tbody>
					<tfoot style="font-size: 14px;">
						<tr>
							<td style=" padding-left: 2px; padding-right: 2px;font-weight: bold; padding-top: 20px;" colspan="6" align="right">Total Amount</td>
							<td style=" padding-left: 2px; padding-right: 2px;font-weight: bold;  padding-top: 20px;" align="right"><?= number_format($total_grand_paid,2,'.',''); ?></td>
						</tr>
						<tr>
							<td style=" padding-left: 2px; padding-right: 2px;font-weight: bold;" colspan="6" align="right">Opening Balance</td>
							<td style=" padding-left: 2px; padding-right: 2px;font-weight: bold;" align="right"><?= number_format($customer_opening_balance,2,'.',''); ?></td>
						</tr>
						<tr>
							<td style=" padding-left: 2px; padding-right: 2px;font-weight: bold;" colspan="6" align="right">Total Due</td>
							<td style=" padding-left: 2px; padding-right: 2px;font-weight: bold;" align="right"><?= number_format($total_grand_paid+(float)$customer_opening_balance,2,'.',''); ?></td>
						</tr>						
					</tfoot>
				</table>
			</td>
		</tr>
	</table>
	<center >
  <div class="row no-print">
  <div class="col-md-12">
  <div class="col-md-2 col-md-offset-5 col-xs-4 col-xs-offset-4 form-group">
    <button type="button" id="" class="btn btn-block btn-success btn-xs" onclick="window.print();" title="Print">Print</button>
    <?php if(isset($_GET['redirect'])){ ?>
		<a href="<?= base_url().$_GET['redirect'];?>"><button type="button" class="btn btn-block btn-danger btn-xs" title="Back">Back</button></a>
	<?php } ?>
   </div>
   </div>
   </div>
   <br>&nbsp;&nbsp;&nbsp;
   <br>&nbsp;&nbsp;&nbsp;
   <br>.
   <br>.
</center>
</body>
</html>