<?php  
require "commonincludefiles.php";
if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"]))
{
	$actionflag='';
	$objCustomerInvoiceMgmt = new CustomerInvoiceMgmt();
	$objCustomerInvoiceProductMgmt = new CustomerInvoiceProductMgmt();
	$objCustomerInvoiceTaxMgmt = new CustomerInvoiceTaxMgmt();

	$productid=json_decode(stripslashes($_REQUEST['productid']));
    $productqty=json_decode(stripslashes($_REQUEST['productqty']));
	$productrate=json_decode(stripslashes($_REQUEST['productrate']));
	
	$taxid=json_decode(stripslashes($_REQUEST['taxid']));
    $taxpercentade=json_decode(stripslashes($_REQUEST['taxpercentade']));
	    	
 	$objCustomerInvoiceMgmt ->customerinvoice_id    = $_REQUEST["customerinvoice_id"] ;
	$objCustomerInvoiceMgmt ->customer_id             = $_REQUEST["customer_id1"] ;
	$objCustomerInvoiceMgmt ->customerinvoice_no			= $_REQUEST['customerinvoice_no'] ;
	$objCustomerInvoiceMgmt ->customerinvoice_date			= $_REQUEST['customerinvoice_date'] ;
	$objCustomerInvoiceMgmt ->remark	= $_REQUEST['remark'] ;
	$objCustomerInvoiceMgmt ->tamount	= $_REQUEST['customerinvoice_grosstotal'] ;
	$objCustomerInvoiceMgmt ->dueamount	= $_REQUEST['customerinvoice_grosstotal'];
	
	if($_REQUEST['statusflag']==true)
		{
		$objCustomerInvoiceMgmt ->status	= 'Cancelled' ;
		}
	else
		$objCustomerInvoiceMgmt ->status	= 'DuePayment' ;

	if($objCustomerInvoiceMgmt ->customerinvoice_id=="0" || $objCustomerInvoiceMgmt ->customerinvoice_id==""){
		$objCustomerInvoiceMgmt ->add();	
		for($i=0;$i<count($productid);$i++){
					 
					    $objCustomerInvoiceProductMgmt->customerinvoice_id		= $objCustomerInvoiceMgmt ->customerinvoice_id;
						$objCustomerInvoiceProductMgmt->product_id		= $productid[$i];
						$objCustomerInvoiceProductMgmt->product_qty		= $productqty[$i];	
						$objCustomerInvoiceProductMgmt->product_rate	= $productrate[$i];	
						$objCustomerInvoiceProductMgmt->add();	
					}	
		for($i=0;$i<count($taxid);$i++){
		 
			$objCustomerInvoiceTaxMgmt->customerinvoice_id		= $objCustomerInvoiceMgmt ->customerinvoice_id;
			$objCustomerInvoiceTaxMgmt->tax_id			= $taxid[$i];
			$objCustomerInvoiceTaxMgmt->tax_percentage	= $taxpercentade[$i];	
			$objCustomerInvoiceTaxMgmt->add();	
		}	

		// ADD INVOICE 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 .="Invoice No. ".$_REQUEST['customerinvoice_no']." has been Created by ".$_SESSION["cUserName"]." from ".$_SESSION["cCompany_name"]." .</div><br>";
		
		
		$newcustomerinvoice_no='-';
		$newcustomerinvoice_date='-';
		$newcustomer_orgname='-';
		$newpnames='-';
		$newtamount='-';
		$newupdateon='-';
		
		$qry1="SELECT ci.*,c.customer_orgname,group_concat(cip.product_id) as proids,group_concat(product_name) as pnames 
				FROM `lms_customerinvoice` ci,lms_customerinvoiceproduct cip,lms_productmaster p,lms_customer c 
				WHERE ci.customerinvoice_id=cip.customerinvoice_id and 
						p.product_id=cip.product_id and 
						ci.customerinvoice_id='".$objCustomerInvoiceMgmt ->customerinvoice_id."' and 
						ci.customer_id=c.customer_id and ci.company_id='".$_SESSION["cCompany_id"]."'";
	
		$rs1=dbQuery($qry1);
		 $bs1 = "";
		 $be1 = "";
		 $bs2 = "";
		 $be2 = "";
		 $bs3 = "";
		 $be3 = "";
		 $bs4 = "";
		 $be4 = "";
		 $bs5 = "";
		 $be5 = "";
		if (dbRows($rs1)>0)
				{
					while($row1 = dbArray($rs1))
					{
						if($row1['customerinvoice_no'] != '') $newcustomerinvoice_no=$row1['customerinvoice_no'];
						if($row1['customerinvoice_date'] != '') $newcustomerinvoice_date=date('d-m-Y',strtotime($row1['customerinvoice_date']));
						if($row1['customer_orgname'] != '') $newcustomer_orgname=$row1['customer_orgname'];
						if($row1['pnames'] != '') $newpnames=$row1['pnames'];
						if($row1['tamount'] != '') $newtamount=$row1['tamount'];
						$newupdateon = date('d-m-Y H:i:s',strtotime($row1['createdon']));
						
					}
					// for bold the updated record
					 
					 if($oldcustomerinvoice_date != $newcustomerinvoice_date )
					   {
						  $bs1 = "<b style='color:green'>";
						  $be1 = "</b>";
					   }
					   if(strlen(trim($oldpnames)) != strlen(trim($newpnames)) )
					   {
						  $bs2 = "<b style='color:green'>";
						  $be2 = "</b>";
					   }
					   if($oldtamount != $newtamount )
					   {
						  $bs3 = "<b style='color:green'>";
						  $be3 = "</b>";
					   }
					   if($oldupdateon != $newupdateon )
					   {
						  $bs4 = "<b style='color:green'>";
						  $be4 = "</b>";
					   }
					   if($oldcustomer_orgname != $newcustomer_orgname )
					   {
						  $bs5 = "<b style='color:green'>";
						  $be5 = "</b>";
					   }
					       
				}
					
					$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">Invoice No.</td>
							<td>'.$newcustomerinvoice_no.'</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Customer Name</td>
							<td>'.$bs5.$newcustomer_orgname.$be5.'</td>
						</tr>						
						<tr>
							<td style="font-weight:bold">Invoice Date</td>
							<td>'.$bs1.$newcustomerinvoice_date.$be1.'</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Product Name</td>
							<td>'.$bs2.$newpnames.$be2.'</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Total Amount</td>
							<td>'.$bs3.$newtamount.$be3.'</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Updated On</td>
							<td>'.$bs4.$newupdateon.$be4.'</td>
						</tr>';
					
					$emailcontent .= '</table>';
					$To = "hemen@auminfosys.com";
					$To1 = "jatin@auminfosys.com"; 
					$Subject = 'Invoice 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 MAIL 	
		
	}
	else{
		$emailcontent = '';
		$oldcustomerinvoice_no='-';
		$oldcustomerinvoice_date='-';
		$oldcustomer_orgname='-';
		$oldpnames='-';
		$oldtamount='-';
		$oldupdateon='-';
		if($_REQUEST['statusflag']==true)
		{
			$actionflag='cancel';
		}
		else
		{
			$actionflag='edit';
			//email content for sending email before updation
				$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 .="Invoice No. ".$_REQUEST['customerinvoice_no']." has been updated by ".$_SESSION["cUserName"]." from ".$_SESSION["cCompany_name"]." .</div><br>";
				
				$qry1="SELECT ci.*,c.customer_orgname,group_concat(cip.product_id) as proids,group_concat(product_name) as pnames FROM `lms_customerinvoice` ci,lms_customerinvoiceproduct cip,lms_productmaster p,lms_customer c WHERE ci.customerinvoice_id=cip.customerinvoice_id and p.product_id=cip.product_id and ci.customerinvoice_id='".$_REQUEST["customerinvoice_id"]."' and ci.customer_id=c.customer_id and ci.company_id='".$_SESSION["cCompany_id"]."'";
				
				$rs1=dbQuery($qry1);
				
				
				if (dbRows($rs1)>0)
				{
					while($row1 = dbArray($rs1))
					{
						if($row1['customerinvoice_no'] != '') $oldcustomerinvoice_no=$row1['customerinvoice_no'];
						if($row1['customerinvoice_date'] != '') $oldcustomerinvoice_date=date('d-m-Y',strtotime($row1['customerinvoice_date']));
						if($row1['customer_orgname'] != '') $oldcustomer_orgname=$row1['customer_orgname'];
						if($row1['pnames'] != '') $oldpnames=$row1['pnames'];
						if($row1['tamount'] != '') $oldtamount=$row1['tamount'];
						$oldupdateon = date('d-m-Y H:i:s',strtotime($row1['createdon']));
						
					}
				}
				
				// end
							
		}
		$objCustomerInvoiceMgmt ->edit();	
		$objCustomerInvoiceProductMgmt->customerinvoice_id = $_REQUEST["customerinvoice_id"];
		$objCustomerInvoiceProductMgmt->delete();	
		for($i=0;$i<count($productid);$i++){
					 
					    $objCustomerInvoiceProductMgmt->customerinvoice_id		= $objCustomerInvoiceMgmt ->customerinvoice_id;
						$objCustomerInvoiceProductMgmt->product_id		= $productid[$i];
						$objCustomerInvoiceProductMgmt->product_qty		= $productqty[$i];	
						$objCustomerInvoiceProductMgmt->product_rate	= $productrate[$i];	
						$objCustomerInvoiceProductMgmt->add();	
					}	
		$objCustomerInvoiceTaxMgmt->customerinvoice_id = $_REQUEST["customerinvoice_id"];
		$objCustomerInvoiceTaxMgmt->delete();	
		for($i=0;$i<count($taxid);$i++){
		 
			$objCustomerInvoiceTaxMgmt->customerinvoice_id		= $objCustomerInvoiceMgmt ->customerinvoice_id;
			$objCustomerInvoiceTaxMgmt->tax_id			= $taxid[$i];
			$objCustomerInvoiceTaxMgmt->tax_percentage	= $taxpercentade[$i];	
			$objCustomerInvoiceTaxMgmt->add();	
		}	
		
		if($actionflag=='cancel')
		{
			// email content fron database
			$emailcontent = '<div align="left"><div style="color:red; font-size:14px">';
			$emailcontent .="Invoice No. ".$_REQUEST['customerinvoice_no']." has been deleted by ".$_SESSION["cUserName"]." from ".$_SESSION["cCompany_name"]." .Detail of Invoice are as under :</div><br>";
			$qry1="SELECT ci.*,c.customer_orgname,group_concat(cip.product_id) as proids,group_concat(product_name) as pnames FROM `lms_customerinvoice` ci,lms_customerinvoiceproduct cip,lms_productmaster p,lms_customer c WHERE ci.customerinvoice_id=cip.customerinvoice_id and p.product_id=cip.product_id and ci.customerinvoice_id='".$_REQUEST["customerinvoice_id"]."' and ci.customer_id=c.customer_id and ci.company_id='".$_SESSION["cCompany_id"]."'";
			$rs1=dbQuery($qry1);
			if (dbRows($rs1)>0)
			{
				while($row1 = dbArray($rs1))
				{
					$emailcontent .= "<div><b>Invoice No. : </b>".$row1['customerinvoice_no']." </div>
									  <div><b>Invoice Date : </b>".date('d-m-Y',strtotime($row1['customerinvoice_date']))."</div>
									  <div><b>Customer Name : </b>".$row1['customer_orgname']."</div>
									  <div><b>Products : </b>".$row1['pnames']."</div>
									  <div><b>Total Amount : </b>".$row1['tamount']."</div>";
	
						$emailcontent .= "<div><b>Deleated On : </b>".date('d-m-Y H:i:s',strtotime($row1['createdon']))."</div>";
						$emailcontent .= "</div>";
				}
			}
			$to2 = "jatin@auminfosys.com";
			$to1 = "hemen@auminfosys.com";
			$To = "support@auminfosys.com";
			$Subject = 'Invoice Cancellation 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); 
			mail($To2, $Subject, $emailcontent, $headers); 
			
			//end
		}
		else if($actionflag == 'edit' )
				{
	
					$newcustomerinvoice_no='-';
					$newcustomerinvoice_date='-';
					$newcustomer_orgname='-';
					$newpnames='-';
					$newtamount='-';
					$newupdateon='-';
					
					$qry1="SELECT ci.*,c.customer_orgname,group_concat(cip.product_id) as proids,group_concat(product_name) as pnames FROM `lms_customerinvoice` ci,lms_customerinvoiceproduct cip,lms_productmaster p,lms_customer c WHERE ci.customerinvoice_id=cip.customerinvoice_id and p.product_id=cip.product_id and ci.customerinvoice_id='".$_REQUEST["customerinvoice_id"]."' and ci.customer_id=c.customer_id and ci.company_id='".$_SESSION["cCompany_id"]."'";
				
				$rs1=dbQuery($qry1);
				 $bs1 = "";
				 $be1 = "";
				 $bs2 = "";
				 $be2 = "";
				 $bs3 = "";
				 $be3 = "";
				 $bs4 = "";
				 $be4 = "";
				 $bs5 = "";
				 $be5 = "";
				
				if (dbRows($rs1)>0)
				{
					while($row1 = dbArray($rs1))
					{
						if($row1['customerinvoice_no'] != '') $newcustomerinvoice_no=$row1['customerinvoice_no'];
						if($row1['customerinvoice_date'] != '') $newcustomerinvoice_date=date('d-m-Y',strtotime($row1['customerinvoice_date']));
						if($row1['customer_orgname'] != '') $newcustomer_orgname=$row1['customer_orgname'];
						if($row1['pnames'] != '') $newpnames=$row1['pnames'];
						if($row1['tamount'] != '') $newtamount=$row1['tamount'];
						$newupdateon = date('d-m-Y H:i:s',strtotime($row1['createdon']));
						
					}
					// for bold the updated record
					 
					 if($oldcustomerinvoice_date != $newcustomerinvoice_date )
					   {
						  $bs1 = "<b style='color:green'>";
						  $be1 = "</b>";
					   }
					   if(strlen(trim($oldpnames)) != strlen(trim($newpnames)) )
					   {
						  $bs2 = "<b style='color:green'>";
						  $be2 = "</b>";
					   }
					   if($oldtamount != $newtamount )
					   {
						  $bs3 = "<b style='color:green'>";
						  $be3 = "</b>";
					   }
					   if($oldupdateon != $newupdateon )
					   {
						  $bs4 = "<b style='color:green'>";
						  $be4 = "</b>";
					   }
					   if($oldcustomer_orgname != $newcustomer_orgname )
					   {
						  $bs5 = "<b style='color:green'>";
						  $be5 = "</b>";
					   }
					       
				}
					
					$emailcontent .= '<table width="650px" cellpadding="5" cellspacing="0">
						<tr style="font-size:16px; font-weight:bold; background:#E6E6E6">
							<td width="24%">Title</td>
							<td width="38%" style=" color:#A00">Old Data</td>
							<td width="38%" style=" color:#009F00">New Data</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Invoice No.</td>
							<td>'.$oldcustomerinvoice_no.'</td>
							<td>'.$newcustomerinvoice_no.'</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Customer Name</td>
							<td>'.$bs5.$oldcustomer_orgname.$be5.'</td>
							<td>'.$bs5.$newcustomer_orgname.$be5.'</td>
						</tr>						
						<tr>
							<td style="font-weight:bold">Invoice Date</td>
							<td>'.$bs1.$oldcustomerinvoice_date.$be1.'</td>
							 <td>'.$bs1.$newcustomerinvoice_date.$be1.'</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Product Name</td>
							<td>'.$bs2.$oldpnames.$be2.'</td>
							 <td>'.$bs2.$newpnames.$be2.'</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Total Amount</td>
							<td>'.$bs3.$oldtamount.$be3.'</td>
							 <td>'.$bs3.$newtamount.$be3.'</td>
						</tr>
						<tr>
							<td style="font-weight:bold">Updated On</td>
							<td>'.$bs4.$oldupdateon.$be4.'</td>
							<td>'.$bs4.$newupdateon.$be4.'</td>
						</tr>';
					
					$emailcontent .= '</table>';
					$To = "hemen@auminfosys.com,support@auminfosys.com";
					$To1 = "jatin@auminfosys.com"; 
					$Subject = 'Invoice Updation 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); 
					
					}
					
	}
	
	
	
	echo '{"success":"true","msg":""}';
}
else {
	echo '{"success":"false","msg":"session expiry"}';
}	         
?>
