<?php
require_once("../include/config.php");
require_once(GLOBAL_INCLUDE_PATH."include/startup.php");
//require_once(GLOBAL_INCLUDE_PATH."include/email/sendEmail.php");
	
if($G_memberid > 0)
{
	
	$companyid        = intval($_REQUEST['txtcompanyid']);
	$memberinvoicesid = intval($_REQUEST['txtmemberinvoicesid']);
	$memberid         = $G_memberid;
	 $memberuserscompanyid = $G_memberuserscompanyid;
	$membercompanyid= $G_membercompanyid;
	$obj_client  	   = new Client();
	$obj 				   = new Company();
	$obj_proforma 	   = new Invoice_frproforma();
	$obj_Invoice  	   = new Invoice();
		
	if(isset($_POST['SUBMIT']))
	{
		
		$Product['Email1'] 	      			= sanitize($_POST['Email1'],SQL);
	 	
	
		$whereclient = "clientcompanyid = ".$companyid;
		$editData	 = $obj_client -> EditClient($Product,$whereclient);
		$todays = (date("d-m-Y")) ;
	if($memberuserscompanyid > 0)
	{
		$query="select c.head_Email ,c.Email1,c.panno,a.duedate,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.invoicedate,a.paid_total_amount,
					a.duedate,a.billed_membername,a.discount_amount,a.inv_Add1,a.inv_Pin,a.inv_CityName,a.inv_Add2,a.inv_StateName,a.invoiceno,a.InvoiceTotalAmount,a.InvoiceBaseAmount,d.gstno,a.inv_GST_no,
					a.InvoiceBaseAmountBeforeDiscount,a.cgst_amount,a.sgst_amount,a.igst_amount,c.PersonName1,d.head_Email as companyEmail,d.companyname,d.head_Pin,d.head_TelNo,d.panno,d.Bankname,d.BankAC,d.Branch,d.IFSC_Code,b.* from mstmember x
				  	inner join trninvoicetomember a on x.memberid = a.memberid 
				  	inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
              	inner join mstclients c on b.billed_membercompanyid = c.clientcompanyid
              	inner join user_company d on a.billing_membercompanyid=d.membercompanyid
              	inner join trnuser_company e on x.memberid = e.memberid
             	where x.memberid =$memberid and e.memberuserscompanyid = $memberuserscompanyid and a.billing_membercompanyid=$membercompanyid and  a.isdeleted= 0 and a.duedate > $todays and quotes_proforma_invoice = 2 and a.memberinvoicesid = $memberinvoicesid";
	}
	else
	{
		$query="select c.head_Email ,c.Email1,c.panno,a.duedate,a.billing_membercompanyid,a.invoiceno,a.InvoiceTotalAmount,a.invoicedate,a.paid_total_amount,
					a.duedate,a.billed_membername,a.discount_amount,a.inv_Add1,a.inv_Pin,a.inv_CityName,a.inv_Add2,a.inv_StateName,a.invoiceno,a.InvoiceTotalAmount,a.InvoiceBaseAmount,d.gstno,a.inv_GST_no,
					a.InvoiceBaseAmountBeforeDiscount,a.cgst_amount,a.sgst_amount,a.igst_amount,c.PersonName1,d.head_Email as companyEmail,d.companyname,d.head_Pin,d.head_TelNo,d.panno,d.Bankname,d.BankAC,d.Branch,d.IFSC_Code,b.* from mstmember x
				  	inner join trninvoicetomember a on x.memberid = a.memberid 
				  	inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
              	inner join mstclients c on b.billed_membercompanyid = c.clientcompanyid
              	inner join user_company d on a.billing_membercompanyid=d.membercompanyid
             	where x.memberid =$memberid and  a.isdeleted= 0 and a.duedate > $todays and quotes_proforma_invoice = 2 and a.memberinvoicesid = $memberinvoicesid";
	}	
	//echo $query;
		$querylist = $obj_proforma ->getInvoice('',$query);
		
		$rowtoproforma 		=count($querylist);
		$counter = 0;
		$igst_amount = 0;
		for($i=0;$i<$rowtoproforma;$i++)
		{		
			$counter++;
			$memberid										= $memberid;
			$memberinvoicesid								= $memberinvoicesid;	
			$billed_membercompanyid						= $querylist[$i]['billed_membercompanyid'];
			$billing_membercompanyid					= $billing_membercompanyid_proforma;
			$billed_membername 							= $querylist[$i]['billed_membername'];
			$billed_PersonName 							= $querylist[$i]['PersonName1'];
			$InvoiceBaseAmountBeforeDiscount       = $querylist[$i]['InvoiceBaseAmountBeforeDiscount'];
			$inv_Add1										= $querylist[$i]['inv_Add1'];
			$inv_Add2										= $querylist[$i]['inv_Add2'];
			$inv_Add3										= $querylist[$i]['inv_Add3'];
			$inv_Add4										= $querylist[$i]['inv_Add4'];
			$address                        			= $querylist[$i]['inv_Add1'].'</br>'.$querylist[$i]['inv_Add2'].'</br>'.$querylist[$i]['inv_CityName'].' '.$querylist[$i]['inv_Pin'];
			$panno											= $querylist[$i]['panno'];
			$gstno											= $querylist[$i]['gstno'];
			$quotes_proforma_invoice					=	0;
			$inv_CityName 									= $querylist[$i]['inv_CityName'];
			$inv_Pin 										= $querylist[$i]['inv_Pin'];
			$inv_StateName 								= $querylist[$i]['inv_StateName'];
			$inv_head_Stateid								= $querylist[$i]['inv_head_Stateid'];
			$invoiceno 										= $querylist[$i]['invoiceno'];
			$invoicedate									= date('d-m-Y',strtotime($querylist[$i]['invoicedate']));
			$duedate											= $querylist[$i]['duedate'];
			$status 											= $querylist[$i]['status'];
			$igst_percentage								= $querylist[$i]['igst_percentage'];
			$igst_amount									= $querylist[$i]['igst_amount'];
			$cgst_percentage								= $querylist[$i]['cgst_percentage'];
			$cgst_amount									= $querylist[$i]['cgst_amount'];
			$sgst_percentage								= $querylist[$i]['sgst_percentage'];
			$sgst_amount									= $querylist[$i]['sgst_amount'];
			$ugst_percentage								= $querylist[$i]['ugst_percentage'];
			$ugst_amount									= $querylist[$i]['ugst_amount'];
			$discount_percentage							= $querylist[$i]['discount_percentage'];
			$discount_amount								= number_format($querylist[$i]['discount_amount']);
			$InvoiceBaseAmountBeforeDiscount			= $querylist[$i]['InvoiceBaseAmountBeforeDiscount'];
			$InvoiceBaseAmount							= number_format($querylist[$i]['InvoiceBaseAmount']);
			$InvoiceTotalAmount							= number_format($querylist[$i]['InvoiceTotalAmount']);
			$InvoiceTotalAmount1							= $querylist[$i]['InvoiceTotalAmount'];
			$quantity										= $querylist[$i]['quantity'];
			//$discount_amount							= $querylist[$i]['discount_amount'];
			//$price											= $querylist[$i]['InvoiceTotalAmount'];
			$ProductBaseAmount							= number_format($querylist[$i]['ProductBaseAmount']);
			$ProductTotalAmount							= number_format($querylist[$i]['ProductTotalAmount']);
			$productname									= $querylist[$i]['productname'];
			//$product_description						= str_replace(' ', "\n", $querylist[$i]['product_description']);
			$product_description							= $querylist[$i]['product_description'];
			$product_unit									= $querylist[$i]['product_unit'];
			$inv_GST_no										= $querylist[$i]['inv_GST_no'];
			//$toEmails											= $querylist[$i]['head_Email'].','.$querylist[$i]['Email1'];
			$InvoiceTotalAmountinword					= number_to_words($InvoiceTotalAmount1);
			$TotalAmount                  = $querylist[$i]['TotalAmount'];
			$paid_total_amount				= $querylist[$i]['paid_total_amount'];
   		if($paid_total_amount == '0'){
   		
   			 $balance1 							= $InvoiceTotalAmount1 - $TotalAmount;
   		}
   		else{
   	
				$balance1 =  $InvoiceTotalAmount1 - $paid_total_amount;
   		}
   		
   		
		}
		
					
$companyname 		= $querylist[0]['companyname'];
		$gstno 				= $querylist[0]['gstno'];
		$panno 				= $querylist[0]['panno'];
		$Addline1 			= $querylist[0]['Addline1'];
		$Addline2 			= $querylist[0]['Addline2'];
		$head_CityName 	= $querylist[0]['head_CityName'];
		$head_StateidName = $querylist[0]['head_StateidName'];
		$Country 			= $querylist[0]['Country'];
		$head_Pin 			= $querylist[0]['head_Pin'];
		$head_TelNo 		= $querylist[0]['head_TelNo'];
		$head_Email 		= $querylist[0]['head_Email'];
		$upload_logo_display 	= $querylist[0]['upload_logo'];
		$upload_header_display  = $querylist[0]['upload_header'];
		$formEmails										= $querylist[0]['companyEmail'];
		$toEmails									= $querylist[0]['head_Email'].','.$querylist[0]['Email1'];

		//$toEmails											= $querylist[$i]['head_Email'].','.$querylist[$i]['Email1'];
		$coverletters										= $querylist[0]['coverletter'];
		$BankName = $querylist[0]['Bankname'];
		$Bank_ACno      = $querylist[0]['BankAC'];
	    $Branch  		= $querylist[0]['Branch'];
		$IFSC_Code    = $querylist[0]['IFSC_Code'];
 				$swiftdir = GLOBAL_INCLUDE_PATH."htmltopdf";
				require_once($swiftdir.'/dompdf/dompdf_config.inc.php');
					require_once($swiftdir.'/swift/swift_required.php');	
		 		 $from_name1= 'Admin';
		 		 $fromEmail =  array($formEmails =>'Invoice Payment remainder');
           //  $fromEmail = 'nagma.m@evergrowsolutions.com';
             $to_name1 = $billed_membername; 
             $to_email =$toEmails;
            
        
             $subject ='Invoice Payment remainder'; 
          // $html_message1	=  " .<br>".$resultstr.""; 
          
          			//$html_message	= 	' <br>Dear '.$billed_membername.', <br><br>
							
										//	Payment remainder! <br> <br>
										//	Your invoice '.$invoiceno.' due date is '.$duedate.' with payment '.$balance1.'
											
										//	 <br>'; 


						$html_message = '<table><tr><b>Dear '.$billed_PersonName.',</b></tr><tr><br></tr><tr>Payment remainder! <br> Company Name : invoice '.$billed_membername.'<br><br>
											Your invoice '.$invoiceno.' due date is '.$duedate.' with payment Rs.'.$balance1.'/-</tr><tr><br><br></tr><tr>Regards,</tr><tr><b>'.$companyname.'</b></tr><tr><b>'.$head_TelNo.'</b></tr></table>';
											  
									$mailer = new Swift_Mailer(new Swift_MailTransport());
									$message = Swift_Message::newInstance()
										       ->setSubject($subject) // Message subject
											   ->setTo($to_email) // Array of people to send to
											   ->setFrom($fromEmail) // From:
											   ->setBody($html_message, 'text/html'); // Attach that HTML message from earlier
											  
							  if ($mailer->send($message))
								{
									
														//$success = true;
									$mail_array['is_payment_remainder_mail_send']=1;
									$mail_array['payment_remainder_mail_send_by']=$G_memberid;
									$mail_array['payment_remainder_mail_send_date']=date('Y-m-d');
									$wheremail="memberinvoicesid = $memberinvoicesid";
								
								 	$editemail =$obj_Invoice->EditInvoice($mail_array,$wheremail);
							
						
								echo "<script type='text/javascript'>alert('Email Send Successfully.');

													window.location.href='".DOC_ROOT."admin/pending-payment-invoices.php';
														window.location.reload();
													</script>";	
								}	
								else
								{
									$error = true;
									$strmessageHeader = "There was Error while Sending Emails. Please try again";
									//echo $strmessageHeader;		
								}


			
					 
				}	
				
						
    		
	
	
	else 
	{
			if($memberuserscompanyid > 0)
			{
			 	 $sql = "select e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,a.is_payment_remainder_mail_send,sum(c.tds) as tds
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		 inner join trnuser_company e on x.memberid = e.memberid
					  		 where x.memberid = $memberid and e.memberuserscompanyid = $memberuserscompanyid and a.billing_membercompanyid=$membercompanyid and  a.isdeleted= 0 and quotes_proforma_invoice = 2  and InvoiceTotalAmount !=paid_total_amount
					  		  group by a.memberinvoicesid 
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned)";
			}
			else
			{
				$sql = "select e.head_Email,a.memberinvoicesid,a.billed_membercompanyid,a.paid_total_amount,a.billing_membercompanyid,a.invoiceno,a.is_mail_send,
			 				 a.InvoiceTotalAmount,a.invoicedate,a.duedate,a.billed_membername,c.TotalAmount,a.approved,a.is_payment_remainder_mail_send,sum(c.tds) as tds
			 			    from mstmember x
					  		 inner join trninvoicetomember a on x.memberid = a.memberid 
					  		 inner join trninvoicetomemberdetails b on a.memberinvoicesid = b.memberinvoicesid
					  		 inner join mstclients e on b.billed_membercompanyid = e.clientcompanyid
					  		 left join user_company_payment_header  c on a.memberinvoicesid = c.memberinvoicesid
					  		 left join user_company_payment_detail  d on a.memberinvoicesid = d.memberinvoicesid
					  		 where x.memberid = $memberid and  a.isdeleted= 0 and quotes_proforma_invoice = 2 and InvoiceTotalAmount !=paid_total_amount
					  		  group by a.memberinvoicesid 
					  		 order by a.invoicedate,CAST(a.invoiceno AS unsigned)";
			}		  		 
				//	echo $sql;
					$sqllist = $obj->getCompany('','',$sql);
					if(is_array($sqllist))
					{
						$rowcount = count($sqllist);
					}
					
					
					
			
				  
		}
 	
		require_once("header1.html");
		require_once("leftnav.html");
		require_once("pending-payment-invoices1.html");
		require_once("footer1.html");
}
else if (!headers_sent($filename, $linenum)) 
{
				$redirect_url = DOC_ROOT."login.php";
				header("Location: $redirect_url");
    				exit;
					// You would most likely trigger an error here.
}
else 
{
				echo "Headers already sent in $filename on line $linenum\n" .
          			"Cannot redirect, for now please click this <a ";
    				exit;
}   



	
	
?>
