<?php 
 class    lms_custinvoicereceiptMgmt
{ 
public  $receipt_id;
public  $receipt_no;
public $company_id;
public  $receipt_date;
public  $customer_id;
public  $invoice_no;
public  $amount;
public  $payment_mode;
public  $chequeno;
public  $bankname;
public  $branchname;
public  $chk_date;
public  $remark;
public  $isdeleted;
public  $updateon;
public  $createon;


 function getAllRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
{
$qry="SELECT * FROM lms_custinvoicereceipt where company_id= '".$_SESSION["cCompany_id"]."' ";

if($condition != "" ){
$qry.= $condition;
} 
if ($order_field !="" && $order_type !=""){
$qry.= " order by $order_field $order_type";
}

if(intval($limit) > 0 && intval($start) >=0) {
$qry.= " Limit $start , $limit";
}

$out=dbQuery($qry);
if (dbRows($out)>0){
return $out;
}
return $out;
}




 function getAllRecords_customer($limit=0,$start=0,$condition="",$order_field="",$order_type="")
{
	$qry="SELECT r.*,c.customer_orgname 
		FROM lms_custinvoicereceipt r, `lms_customer`  c
		where r.company_id= '".$_SESSION["cCompany_id"]."' and
		r.customer_id = c.customer_id ";
	
	if($condition != "" ){
	$qry.= $condition;
	} 
	if ($order_field !="" && $order_type !=""){
	$qry.= " order by $order_field $order_type";
	}
	
	if(intval($limit) > 0 && intval($start) >=0) {
	$qry.= " Limit $start , $limit";
	}
	
	$out=dbQuery($qry);
	if (dbRows($out)>0){
	return $out;
	}
	return $out;
}

function add(){
	//print_r($_REQUEST);
	
	$SQLDateFormat = '%d/%m/%Y';
$Qry1 = "";
$Qry1 .= "receipt_id   ='"  .Escape($this->receipt_id)."',";
$Qry1 .= "receipt_no   ='"  .Escape($this->receipt_no)."',";
$Qry1 .= "company_id='"	  .Escape($_SESSION['cCompany_id'])."',";
$Qry1 .= "receipt_date   =   STR_TO_DATE('".Escape($this->receipt_date)."','$SQLDateFormat'),";	
$Qry1 .= "customer_id   ='"  .Escape($this->customer_id)."',";
$Qry1 .= "invoice_no   ='"  .Escape($this->invoice_no)."',";
$Qry1 .= "amount   ='"  .Escape($this->amount)."',";
$Qry1 .= "payment_mode   ='"  .Escape($this->payment_mode)."',";
$Qry1 .= "chequeno   ='"  .Escape($this->chequeno)."',";
$Qry1 .= "bankname   ='"  .Escape($this->bankname)."',";
$Qry1 .= "branchname   ='"  .Escape($this->branchname)."',";
$Qry1 .= "remark   ='"  .Escape($this->remark)."',";
$Qry1 .= "chk_date   =   STR_TO_DATE('".Escape($this->chk_date)."','$SQLDateFormat'),";	
$Qry1 .= "createdby='"	      .Escape($_SESSION['cUserId'])."',";
$Qry1 .= "createon   ='"  .date("Y-m-d H:i:s")."',";
$Qry1 .= "updateon   ='"  .date("Y-m-d H:i:s")."',";
if($Qry1 != ""){
$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
$qry = "insert into lms_custinvoicereceipt set " . $Qry1 . " ";
dbQuery($qry);
$this->receipt_id=dbInsertedId();
} 

}

function 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 .="Receipt No. ".$this->receipt_no." has been updated by ".$_SESSION["cUserName"]." from ".$_SESSION["cCompany_name"]." .</div><br>";

$qry1="SELECT * FROM lms_custinvoicereceipt cr,lms_customer c where cr.customer_id 	=c.customer_id and cr.company_id= '".$_SESSION["cCompany_id"]."' and  receipt_id =".$this->receipt_id;
$rs1=dbQuery($qry1);
$oldreceipt_no='-';
$oldreceipt_date='-';
$oldcustomer_orgname='-';
$oldinvoice_no='-';
$oldamount='-';
$oldpayment_mode='-';
$oldchequeno='-';
$oldbankname='-';
$oldbranchname='-';
$oldupdateon='-';
if (dbRows($rs1)>0)
{
	while($row1 = dbArray($rs1))
	{
		if($row1['receipt_no'] != '') $oldreceipt_no=$row1['receipt_no'];
		if($row1['receipt_date'] != '') $oldreceipt_date=date('d-m-Y',strtotime($row1['receipt_date']));
		
		if($row1['customer_orgname'] != '') $oldcustomer_orgname=$row1['customer_orgname'];
		if($row1['invoice_no'] != '') $oldinvoice_no=$row1['invoice_no'];
		if($row1['amount'] != '') $oldamount=$row1['amount'];
		if($row1['payment_mode'] == 0)
			{
				$oldpayment_mode = 'Cash';
				$oldchequeno='-';
				$oldbankname='-';
				$oldbranchname='-';
			}
		else
			{
				$oldpayment_mode = 'Cheque';
				$oldchequeno = $row1['chequeno'];
				$oldbankname = $row1['bankname'];
				$oldbranchname = $row1['branchname'];
			}
		$oldupdateon = date('d-m-Y H:i:s',strtotime($row1['updateon']));
		
	}
}



// end
$SQLDateFormat = '%d/%m/%Y';
$Qry1 = "";
$Qry1 .= "receipt_id   ='"  .Escape($this->receipt_id)."',";
$Qry1 .= "receipt_no   ='"  .Escape($this->receipt_no)."',";
$Qry1 .= "company_id='"	  .Escape($_SESSION['cCompany_id'])."',";
$Qry1 .= "receipt_date   =STR_TO_DATE('".Escape($this->receipt_date)."','$SQLDateFormat'),";
$Qry1 .= "customer_id   ='"  .Escape($this->customer_id)."',";
$Qry1 .= "invoice_no   ='"  .Escape($this->invoice_no)."',";
$Qry1 .= "amount   ='"  .Escape($this->amount)."',";
$Qry1 .= "payment_mode   ='"  .Escape($this->payment_mode)."',";
$Qry1 .= "chequeno   ='"  .Escape($this->chequeno)."',";
$Qry1 .= "bankname   ='"  .Escape($this->bankname)."',";
$Qry1 .= "branchname   ='"  .Escape($this->branchname)."',";
$Qry1 .= "remark   ='"  .Escape($this->remark)."',";
$Qry1 .= "chk_date   =   STR_TO_DATE('".Escape($this->chk_date)."','$SQLDateFormat'),";	
$Qry1 .= "updateon   ='"  .date("Y-m-d H:i:s")."',";
if($Qry1 != ""){ 
$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
$qry = "update lms_custinvoicereceipt set " . $Qry1 . " where  receipt_id=".$this->receipt_id;
dbQuery($qry);
}


$qry1="SELECT * FROM lms_custinvoicereceipt cr,lms_customer c where cr.customer_id 	=c.customer_id and cr.company_id= '".$_SESSION["cCompany_id"]."' and  receipt_id =".$this->receipt_id;
$rs1=dbQuery($qry1);

$newreceipt_no='-';
$newreceipt_date='-';
$newcustomer_orgname='-';
$newinvoice_no='-';
$newamount='-';
$newpayment_mode='-';
$newchequeno='-';
$newbankname='-';
$newbranchname='-';
$newupdateon='-';
if (dbRows($rs1)>0)
{
	while($row1 = dbArray($rs1))
	{
		if($row1['receipt_no'] != '') $newreceipt_no=$row1['receipt_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['invoice_no'] != '') $newinvoice_no=$row1['invoice_no'];
		if($row1['amount'] != '') $newamount=$row1['amount'];
		if($row1['payment_mode'] == 0)
			{
				$newpayment_mode = 'Cash';
				$newchequeno='-';
				$newbankname='-';
				$newbranchname='-';
			}
		else
			{
				$newpayment_mode = 'Cheque';
				$newchequeno = $row1['chequeno'];
				$newbankname = $row1['bankname'];
				$newbranchname = $row1['branchname'];
			}
		$newupdateon = date('d-m-Y H:i:s',strtotime($row1['updateon']));
		
	}
}

$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">Receipt No.</td>
        <td>'.$oldreceipt_no.'</td>
        <td>'.$newreceipt_no.'</td>
    </tr>
    <tr>
    	<td style="font-weight:bold">Receipt Date</td>
        <td>'.$oldreceipt_date.'</td>
         <td>'.$newreceipt_date.'</td>
    </tr>
    <tr>
    	<td style="font-weight:bold">Customer Name</td>
        <td>'.$oldcustomer_orgname.'</td>
         <td>'.$newcustomer_orgname.'</td>
    </tr>
    <tr>
    	<td style="font-weight:bold">Invoice No.</td>
        <td>'.$oldinvoice_no.'</td>
         <td>'.$newinvoice_no.'</td>
    </tr>
    <tr>
    	<td style="font-weight:bold">Amount</td>
        <td>'.$oldamount.'</td>
        <td>'.$newamount.'</td>
    </tr>
    <tr>
    	<td style="font-weight:bold">Payment Mode</td>
        <td>'.$oldpayment_mode.'</td>
        <td>'.$newpayment_mode.'</td>
    </tr>';
if($oldpayment_mode == 'Cheque' || $newpayment_mode == 'Cheque')
{
	$emailcontent .= '<tr>
    	<td style="font-weight:bold">Cheque No.</td>
        <td>'.$oldchequeno.'</td>
         <td>'.$newchequeno.'</td>
    </tr>
    <tr>
    	<td style="font-weight:bold">Bank Name</td>
        <td>'.$oldbankname.'</td>
        <td>'.$newbankname.'</td>
    </tr>
    <tr>
    	<td style="font-weight:bold">Branch Name</td>
        <td>'.$oldbranchname.'</td>
        <td>'.$newbranchname.'</td>
    </tr>';
}
$emailcontent .= '<tr>
    	<td style="font-weight:bold">Last Updated On</td>
        <td>'.$oldupdateon.'</td>
         <td>'.$newupdateon.'</td>
    </tr>';
	
$emailcontent .= '</table>';
$To = "support@auminfosys.com";
$Subject = 'Receipt 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); 

} 



function updatedelstatus()
{
// update status in database	
$qry = "update lms_custinvoicereceipt set isdeleted=1, updateon='".date("Y-m-d H:i:s")."' where receipt_id=".$this->receipt_id;
dbQuery($qry);
//end

// email content fron database
$emailcontent = '<div align="left"><div style="color:red; font-size:14px">';
$emailcontent .="Receipt No. ".$this->receipt_no." has been deleted by ".$_SESSION["cUserName"]." from ".$_SESSION["cCompany_name"]." .Detail of receipt are as under :</div><br>";
$qry1="SELECT * FROM lms_custinvoicereceipt cr,lms_customer c where cr.customer_id 	=c.customer_id and cr.company_id= '".$_SESSION["cCompany_id"]."' and  receipt_id =".$this->receipt_id;
$rs1=dbQuery($qry1);
if (dbRows($rs1)>0)
{
	while($row1 = dbArray($rs1))
	{
		$emailcontent .= "<div><b>Receipt No. : </b>".$row1['receipt_no']." </div>
						  <div><b>Receipt Date : </b>".date('d-m-Y',strtotime($row1['receipt_date']))."</div>
						  <div><b>Customer Name : </b>".$row1['customer_orgname']."</div>
						  <div><b>Invoice No. : </b>".$row1['invoice_no']."</div>
						  <div><b>Amount : </b>".$row1['amount']."</div>";
		if($row1['payment_mode'] == 0)
			{
				$emailcontent .= "<div><b>Payment Mode : </b>Cash</div>";
			}
		else
			{
				$emailcontent .= "<div><b>Payment Mode : </b>Cheque</div>
				<div><b>Cheque No. : </b>".$row1['chequeno']."</div>
				<div><b>Bank Name : </b>".$row1['bankname']."</div>
				<div><b>Branch Name : </b>".$row1['branchname']."</div>";
			}
			$emailcontent .= "<div><b>Deleted On : </b>".date('d-m-Y H:i:s',strtotime($row1['updateon']))."</div>";
			$emailcontent .= "</div>";
	}
}


$To = "support@auminfosys.com";
$Subject = 'Receipt deletion 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); 

//end
}


 
function delete() 
{ 
	$qry = "delete from lms_custinvoicereceipt where receipt_id in (".$this->receipt_id.") ";
	dbQuery($qry);
} 

function getReceiptNO()
    	{	
			$qry = "";
			 $qry = "SELECT Max(`receipt_no`)+1 As receipt_no 
					FROM `lms_custinvoicereceipt` where company_id= '".$_SESSION["cCompany_id"]."' ";
			
			$out=dbQuery($qry);
			
			if (dbRows($out)>0){
            	$resut=dbArray($out);
				if($resut["receipt_no"]=="")
				{
					return "1";
				}
				else{		 
					return $resut["receipt_no"];							
				}
        	}
			return $out;
		}



}/* Code For EXTJS START \n{
 xtype: 'textfield',
name: 'receipt_id',
id:'receipt_id',
allowBlank:false,
tabIndex:1,
labelSeparator:'',
fieldLabel :'receipt_id:',
width:100
},
{
 xtype: 'textfield',
name: 'receipt_no',
id:'receipt_no',
allowBlank:false,
tabIndex:2,
labelSeparator:'',
fieldLabel :'receipt_no:',
width:100
},
{
 xtype: 'textfield',
name: 'receipt_date',
id:'receipt_date',
allowBlank:false,
tabIndex:3,
labelSeparator:'',
fieldLabel :'receipt_date:',
width:100
},
{
 xtype: 'textfield',
name: 'customer_id',
id:'customer_id',
allowBlank:false,
tabIndex:4,
labelSeparator:'',
fieldLabel :'customer_id:',
width:100
},
{
 xtype: 'textfield',
name: 'invoice_no',
id:'invoice_no',
allowBlank:false,
tabIndex:5,
labelSeparator:'',
fieldLabel :'invoice_no:',
width:100
},
{
 xtype: 'textfield',
name: 'amount',
id:'amount',
allowBlank:false,
tabIndex:6,
labelSeparator:'',
fieldLabel :'amount:',
width:100
},
{
 xtype: 'textfield',
name: 'payment_mode',
id:'payment_mode',
allowBlank:false,
tabIndex:7,
labelSeparator:'',
fieldLabel :'payment_mode:',
width:100
},
{
 xtype: 'textfield',
name: 'chequeno',
id:'chequeno',
allowBlank:false,
tabIndex:8,
labelSeparator:'',
fieldLabel :'chequeno:',
width:100
},
{
 xtype: 'textfield',
name: 'bankname',
id:'bankname',
allowBlank:false,
tabIndex:9,
labelSeparator:'',
fieldLabel :'bankname:',
width:100
},
{
 xtype: 'textfield',
name: 'branchname',
id:'branchname',
allowBlank:false,
tabIndex:10,
labelSeparator:'',
fieldLabel :'branchname:',
width:100
},
{
 xtype: 'textfield',
name: 'remark',
id:'remark',
allowBlank:false,
tabIndex:11,
labelSeparator:'',
fieldLabel :'remark:',
width:100
},
Code For EXTJS END */




/* Code For Save File START 


$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'];
$objlms_custinvoicereceiptMgmt->customer_id=$_POST['customer_id'];
$objlms_custinvoicereceiptMgmt->invoice_no=$_POST['invoice_no'];
$objlms_custinvoicereceiptMgmt->amount=$_POST['amount'];
$objlms_custinvoicereceiptMgmt->payment_mode=$_POST['payment_mode'];
$objlms_custinvoicereceiptMgmt->chequeno=$_POST['chequeno'];
$objlms_custinvoicereceiptMgmt->bankname=$_POST['bankname'];
$objlms_custinvoicereceiptMgmt->branchname=$_POST['branchname'];
$objlms_custinvoicereceiptMgmt->remark=$_POST['remark'];
if($objlms_custinvoicereceiptMgmt->receipt_id == "0" ||  $objlms_custinvoicereceiptMgmt->receipt_id== ""){ 
$objlms_custinvoicereceiptMgmt->add(); 
} 
else{
if($_REQUEST[act] == "del")
 {$objlms_custinvoicereceiptMgmt->delete();
 }else
 {$objlms_custinvoicereceiptMgmt->edit();
}
echo'{"success":"true"}'; 
 }

Code For Save File END */

  ?>