<?php  
	require "commonincludefiles.php";
	
	if($_SESSION["cUserId"]!="" && isset($_SESSION["cUserId"]))
	{
		
		//print_r($_REQUEST);
		
		$objlms_custinvoicereceiptMgmt= new lms_custinvoicereceiptMgmt();
		$objlms_custinvoicereceiptMgmt->receipt_id=$_POST['receipt_id'];
		$objlms_custinvoicereceiptMgmt->receipt_no=$_POST['receipt_no'];
		$objlms_custinvoicereceiptMgmt->receipt_date=$_POST['receipt_date'];
		if(!isset($_POST['chk_date']) || $_POST['chk_date'] == "")
		{
			$objlms_custinvoicereceiptMgmt->chk_date=date("d/m/Y");
		}
		else{
			$objlms_custinvoicereceiptMgmt->chk_date=$_POST['chk_date'];
		}
		
		$objlms_custinvoicereceiptMgmt->customer_id=$_POST['customer_id'];
		$objlms_custinvoicereceiptMgmt->invoice_no=$_POST['invoicenos'];
		$objlms_custinvoicereceiptMgmt->amount=$_POST['amount'];
		if($_POST['cash'] == true)
			$objlms_custinvoicereceiptMgmt->payment_mode=1;
		else
			$objlms_custinvoicereceiptMgmt->payment_mode=0;
			
		$objlms_custinvoicereceiptMgmt->chequeno=$_POST['chequeno'];
		$objlms_custinvoicereceiptMgmt->bankname=$_POST['bankname'];
		$objlms_custinvoicereceiptMgmt->branchname=$_POST['branchname'];
		$objlms_custinvoicereceiptMgmt->remark=$_POST['remark'];	
		
		if($_POST["invoicenos"] != "")
		{
		$objCustomerInvoiceMgmt = new CustomerInvoiceMgmt();  
		$condition .= " AND li.customerinvoice_no in (".$_POST["invoicenos"].")";
		$result = $objCustomerInvoiceMgmt->getAllRecords(0,0,$condition,$order_field,$order_type) ;
		
		if(dbRows($result) > 0){
			$paidinvoicenos="";
			$remamount=$_POST['amount'];
			
			while($row = dbArray($result))
			{	$con="";
				if($remamount >= $row['dueamount'])
					{
					$con .= " dueamount=0, status='Paid'";	
					$remamount=$remamount-$row['dueamount'];
					$paidinvoicenos=$paidinvoicenos.$row['customerinvoice_no'].",";	
					$objCustomerInvoiceMgmt->updatestatus($row['customerinvoice_id'],$con);
					}
				else if($remamount != 0)
					{
						
						$rem= $row['dueamount']-$remamount;
						$con .= " dueamount=".$rem;
						$remamount=0;
						$paidinvoicenos=$paidinvoicenos.$row['customerinvoice_no'].",";
						$objCustomerInvoiceMgmt->updatestatus($row['customerinvoice_id'],$con);
					}
				
			}
			$objlms_custinvoicereceiptMgmt->invoice_no=substr($paidinvoicenos, 0, -1);
		}
				
				
		}
		if($objlms_custinvoicereceiptMgmt->receipt_id == "0" ||  $objlms_custinvoicereceiptMgmt->receipt_id== "")
		{ 
			$objlms_custinvoicereceiptMgmt->add(); 
			
	//---------------------ADD RECEIPT DETAIL SEND Mail ------------------------------------
			
			$emailcontent .= '<style>
				table td
				{
					border-bottom:solid;
					border-bottom-width:1px;
					border-bottom-color:#959595;
				}
				</style><div align="left"><div style="color:red; font-size:14px">';
			$emailcontent .="RECEIPT No. ".$_REQUEST['receipt_no']." has been Created by ".$_SESSION["cUserName"]." from ".$_SESSION["cCompany_name"]." .</div><br>";
			$newcustomereceipt_no='-';
			$newcustomerinvoice_no='-';
			$newreceipt_date='-';
			$newcustomer_orgname='-';
			$pnames='-';
			$newpnames='-';
			$newtamount='-';
			$newupdateon='-';
			$newcustomercheque_no ='-';
			$newcustomerbank_name ='-';
			$newbranch_name ='-';
			$newcheque_date='-';
			$newremarks='-';
			
			
			
			
			$qry_mail = "select
					cir.*,
					c.customer_orgname,
					group_concat(cip.product_id) as proids,
					group_concat(product_name) as pnames 
				FROM 
					`lms_custinvoicereceipt`  cir,
					`lms_customerinvoice` ci,
					lms_customerinvoiceproduct cip,
					lms_productmaster p,
					lms_customer c
				WHERE 
					 cir.receipt_id = '".$objlms_custinvoicereceiptMgmt->receipt_id."'  and
					 cir.company_id=4 and
					 cir.customer_id= '".$objlms_custinvoicereceiptMgmt->customer_id."' and
					 ci.customerinvoice_no in(".$objlms_custinvoicereceiptMgmt->invoice_no.") and
					 cip.customerinvoice_id in (ci.customerinvoice_id) and 
					 p.product_id in (cip.product_id) and 
					 ci.customer_id=c.customer_id ";

			$rs1=dbQuery($qry_mail);
			
			if (dbRows($rs1)>0)
			{
				while($row1 = dbArray($rs1))
				{
					if($row1['receipt_no'] != '') $newcustomereceipt_no=$row1['receipt_no'];
					if($row1['remark'] != '') $newremarks=$row1['remark'];
					if($row1['invoice_no'] != '') $newcustomerinvoice_no=$row1['invoice_no'];
					if($row1['receipt_date'] != '') $newreceipt_date=date('d-m-Y',strtotime($row1['receipt_date']));
					if($row1['customer_orgname'] != '') $newcustomer_orgname=$row1['customer_orgname'];
					if($row1['pnames'] != '') $pnames=$row1['pnames'];
					if($row1['amount'] != '') $newtamount=$row1['amount'];
					$payment_mode=$row1['payment_mode'];
					if($row1['payment_mode'] != 0){
						
						 $newcustomercheque_no=$row1['chequeno'];
						 $newcustomerbank_name=$row1['bankname'];
						 $newbranch_name=$row1['branchname'];
						 $newcheque_date=date('d-m-Y',strtotime($row1['chk_date']));
					}
					$newupdateon = date('d-m-Y H:i:s',strtotime($row1['createon']));
					
				}
				// for bold the updated record
				$bs1 = "<b style='color:green'>";
				  $be1 = "</b>";
									   
			}
			if($pnames != '' || $pnames !='-'){
				$newpnames = str_replace(",", "<br/>", $pnames);	
			}
			$emailcontent .= '<table width="650px" cellpadding="5" cellspacing="0">
				<tr style="font-size:16px; font-weight:bold; background:#E6E6E6">
					<td width="40%">Title</td>
					<td width="60%" style=" color:#009F00">New Data</td>
				</tr>
				<tr>
					<td style="font-weight:bold">Receipt No.</td>
					<td>'.$newcustomereceipt_no.'</td>
				</tr>
				<tr>
					<td style="font-weight:bold">Customer Name</td>
					<td>'.$bs1.$newcustomer_orgname.$bs1.'</td>
				</tr>						
				<tr>
					<td style="font-weight:bold">Receipt Date</td>
					<td>'.$bs1.$newreceipt_date.$be1.'</td>
				</tr>
				<tr>
					<td style="font-weight:bold">Product Name</td>
					<td>'.$bs1.$newpnames.$bs1.'</td>
				</tr>';
				if($payment_mode !=0){
					$emailcontent .= '
					<tr>
						<td style="font-weight:bold">Bank Name</td>
						<td>'.$bs1.$newcustomerbank_name.$bs1.'</td>
					</tr>
					<tr>
						<td style="font-weight:bold">Branch Name</td>
						<td>'.$bs1.$newcustomercheque_no.$bs1.'</td>
					</tr>
					<tr>
						<td style="font-weight:bold">Bank Name</td>
						<td>'.$bs1.$newcustomerbank_name.$bs1.'</td>
					</tr>
					<tr>
						<td style="font-weight:bold">Cheque Date</td>
						<td>'.$bs1.$newcheque_date.$bs1.'</td>
					</tr>
					';
				}
			$emailcontent .= '<tr>
					<td style="font-weight:bold">Total Amount</td>
					<td>'.$bs1.$newtamount.$bs1.'</td>
				</tr>
				<tr>
					<td style="font-weight:bold">Remark</td>
					<td>'.$bs1.$newremarks.$bs1.'</td>
				</tr>
				<tr>
					<td style="font-weight:bold">Created On</td>
					<td>'.$bs1.$newupdateon.$bs1.'</td>
				</tr>

				';
					
			$emailcontent .= '</table>';
			$To = "hemen@auminfosys.com";
			$To1 = "jatin@auminfosys.com"; 
			$Subject = 'Invoice Receipt Creation Notification';
			
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers  .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			$headers  .= 'From: ERP Notification <support@auminfosys.com>' . "\r\n";
			
			mail($To, $Subject, $emailcontent, $headers); 
			mail($To1, $Subject, $emailcontent, $headers); 
			
		
	// END ADD INVOICE MAIL 	
		
	///-----------------------------------------------------------------------------
		}  
		else
		{
		if($_REQUEST[act] == "del")
		{
		 	$objlms_custinvoicereceiptMgmt->updatedelstatus();
		}
		else
		{
		 	$objlms_custinvoicereceiptMgmt->edit();
		}
		}
		echo'{"success":"true"}'; 
		 
		
	}
	else
	{
		echo '{"success":"false","msg":"session expiry"}';
	}
?>