<?php
require('../config/dbcon.php');//connect
	if(isset($_POST['addinv'])){
		$inv_number=$_POST['srno'];
		$orgDate = $_POST['invdt'];  
		$newDate = date("Y-m-d H:i:s", strtotime($orgDate));
		$inv_date=$newDate;
		$ypart = date('Y', strtotime($inv_date));
		$mpart  = date('m', strtotime($inv_date));
		$dpart  = date('d', strtotime($inv_date));
		$curr_month = date('m', strtotime($inv_date));
		if($curr_month >= 4 && $curr_month <= 12){
			$year = date('y') . '-' . date('y', strtotime("+1 year"));
		} else{
			$year = date("y",strtotime("-1 year")) . '-' . date("y");
		}
		//$inv_date=$_POST['invdt'];
		$tax_code=$_POST['tax_code'];//
		$customer_code=$_POST['customer_code'];
		$customer_name=$_POST['customer_name'];		
		$cust_address_one=$_POST['cust_address_one'];
		$cust_address_two=$_POST['cust_address_two'];
		$cust_address_three=$_POST['cust_address_three'];
		$cust_place=$_POST['cust_place'];
		$cust_pin_code=$_POST['cust_pin_code'];
		$cust_phone=$_POST['cust_phone'];
		$cust_mobile=$_POST['cust_mobile'];
		$cust_email=$_POST['cust_email'];
		$cust_gstno=$_POST['cust_gstno'];
		$cust_pan_no=$_POST['cust_pan_no'];
		$cust_aadhar_no=$_POST['cust_aadhar_no'];
		$cust_st_code=$_POST['cust_st_code'];
		$cust_st_name=$_POST['cust_st_name'];
		$po_no=$_POST['po_no'];
		$po_date=$_POST['po_date'];
		$challan_no=$_POST['challan_no'];
		$challan_date=$_POST['challan_date'];
		$lr_no=$_POST['lr_no'];
		$lr_dt=$_POST['lr_dt'];
		$transporter_name=$_POST['transporter_name'];
		$no_of_parcel=$_POST['no_of_parcel'];
		$due_days=$_POST['due_days'];
		$due_date=$_POST['due_date'];
		$rsinwords=$_POST['rsinwords'];		
		$total_qty=$_POST['total_qty'];
		$total_disc_amt=$_POST['total_disc_amt'];
		$total_list_value=$_POST['total_list_value'];
		$total_sales_val=$_POST['total_sales_val'];
		$total_sgstamt=$_POST['total_sgstamt'];
		$total_cgstamt=$_POST['total_cgstamt'];
		$total_igstamt=$_POST['total_igstamt'];
		$total_val=$_POST['total_val'];
		$roundoff=$_POST['roundoff'];
		$total_inv_val=$_POST['total_inv_val'];
		$total_cost=$_POST['total_cost'];
		$srnoupd = $_POST['inv_number'] + 1;
		$inv_type ='INVOICE';
		if($tax_code == 'GST-B2B'){
			$update="update control_mst set srno = '".$srnoupd."' where description='GST-B2B'";
			mysqli_query($connect, $update) or die(mysqli_error());
		}
		if($tax_code == 'I-GST'){
			$update="update control_mst set srno = '".$srnoupd."' where description='I-GST'";
			mysqli_query($connect, $update) or die(mysqli_error());
		}
		if($tax_code == 'GST-B2C'){
			$update="update control_mst set srno = '".$srnoupd."' where description='GST-B2C'";
			mysqli_query($connect, $update) or die(mysqli_error());
		}		
		if ($rs = mysqli_query($connect, "select * from company_mst"))
		{
			while($row1 = mysqli_fetch_array($rs))
			{
				$company_code = $row1["company_code"];
				$company_name=$row1['company_name'];		
				$company_address_one=$row1['company_address_one'];
				$company_address_two=$row1['company_address_two'];
				$company_address_three=$row1['company_address_three'];
				$company_address_four=$row1['company_address_four'];
				$company_pin=$row1['company_pin'];
				$company_place=$row1['company_place'];
				$company_mobile=$row1['company_mobile'];
				$company_phone=$row1['company_phone'];
				$company_email=$row1['company_email'];
				$company_gstno=$row1['company_gstno'];
				$company_pan_no=$row1['company_pan_no'];
				$company_aadhar_no=$row1['company_aadhar_no'];
				$company_st_code=$row1['company_st_code'];
				$company_st_name=$row1['company_st_name'];
				$company_bank_name=$row1['company_bank_name'];
				$company_bank_accno=$row1['company_bank_accno'];
				$company_ifsc_code=$row1['company_ifsc_code'];
				$company_bank_address=$row1['company_bank_address'];
			}			
		}
		$delinv = mysqli_query($connect,"delete from invprt_mst") or die(mysqli_last_error());
		$delinvtotaltemp = mysqli_query($connect,"delete from invtotalprt_mst") or die(mysqli_last_error());		
		if ($result1 = mysqli_query($connect, "select * from invtemp_mst order by partno"))
		{
			while($row = mysqli_fetch_array($result1))
			{
				$partno = $row["partno"];
				$description_one=$row['description_one'];		
				$description_two=$row['description_two'];
				$list=$row['list'];
				$qty=$row['qty'];
				$disc_per=$row['disc_per'];
				$list_value=$row['list_value'];
				$disc_amt=$row['disc_amt'];
				$sales_val=$row['sales_val'];
				$sgst_per=$row['sgst_per'];
				$sgst_amt=$row['sgst_amt'];
				$cgst_per=$row['cgst_per'];
				$cgst_amt=$row['cgst_amt'];
				$igst_per=$row['igst_per'];
				$igst_amt=$row['igst_amt'];
				$inv_val=$row['inv_val'];
				$cost=$row['cost'];
				$cost_val=$row['cost_val'];	
				$rate_one=$row['rate_one'];	
				$hsn_code=$row['hsn_code'];				
				$segment=$row['segment'];
				$recamt = "0.00";
				$status = "PENDING";
				$query = "SELECT partno,stk_qty,stk_val,cost,cy_sales from item_mst where partno = '".$partno."'";
				$resultstk = mysqli_query($connect,$query);    
				while($rows = mysqli_fetch_array($resultstk)){				
					$csales = $rows["cy_sales"];
					$stock =$rows["stk_qty"];
					$costt =$rows["cost"];
					$stockqty = $stock - $qty;
					$stockvalue1 = $stockqty * $costt;
					$csales1 = $csales + $qty;
					$updatestk="update item_mst set stk_qty = '".$stockqty."',stk_val = '".$stockvalue1."',cy_sales = '".$csales1."' where partno='".$partno."'";
					mysqli_query($connect, $updatestk) or die(mysqli_last_error());
				}
				$result2 = mysqli_query($connect,"insert into inv_mst(inv_number,inv_date,partno,description_one,description_two,list,qty,disc_per,list_value,disc_amt,sales_val,sgst_per,sgst_amt,cgst_per,cgst_amt,igst_per,igst_amt,inv_val,cost,cost_val,rate_one,hsn_code,segment,tax_code,customer_code,customer_name,cust_address_one,cust_address_two,cust_address_three,cust_place,cust_pin_code,cust_phone,cust_mobile,cust_email,cust_gstno,cust_pan_no,cust_aadhar_no,cust_st_code,cust_st_name,company_code,company_name,company_address_one,company_address_two,company_address_three,company_address_four,company_pin,company_place,company_mobile,company_phone,company_email,company_gstno,company_pan_no,company_aadhar_no,company_st_code,company_st_name,company_bank_name,company_bank_accno,company_ifsc_code,company_bank_address,po_no,po_date,challan_no,challan_date,lr_no,lr_dt,transporter_name,no_of_parcel,due_days,due_date,rsinwords,dpart,mpart,ypart,fyear) values ('$inv_number','$inv_date','$partno','$description_one','$description_two','$list','$qty','$disc_per','$list_value','$disc_amt','$sales_val','$sgst_per','$sgst_amt','$cgst_per','$cgst_amt','$igst_per','$igst_amt','$inv_val','$cost','$cost_val','$rate_one','$hsn_code','$segment','$tax_code','$customer_code','$customer_name','$cust_address_one','$cust_address_two','$cust_address_three','$cust_place','$cust_pin_code','$cust_phone','$cust_mobile','$cust_email','$cust_gstno','$cust_pan_no','$cust_aadhar_no','$cust_st_code','$cust_st_name','$company_code','$company_name','$company_address_one','$company_address_two','$company_address_three','$company_address_four','$company_pin','$company_place','$company_mobile','$company_phone','$company_email','$company_gstno','$company_pan_no','$company_aadhar_no','$company_st_code','$company_st_name','$company_bank_name','$company_bank_accno','$company_ifsc_code','$company_bank_address','$po_no','$po_date','$challan_no','$challan_date','$lr_no','$lr_dt','$transporter_name','$no_of_parcel','$due_days','$due_date','$rsinwords','$dpart','$mpart','$ypart','$year')");
				$result = mysqli_query($connect,"insert into invprt_mst(inv_number,inv_date,partno,description_one,description_two,list,qty,disc_per,list_value,disc_amt,sales_val,sgst_per,sgst_amt,cgst_per,cgst_amt,igst_per,igst_amt,inv_val,cost,cost_val,rate_one,hsn_code,segment,tax_code,customer_code,customer_name,cust_address_one,cust_address_two,cust_address_three,cust_place,cust_pin_code,cust_phone,cust_mobile,cust_email,cust_gstno,cust_pan_no,cust_aadhar_no,cust_st_code,cust_st_name,company_code,company_name,company_address_one,company_address_two,company_address_three,company_address_four,company_pin,company_place,company_mobile,company_phone,company_email,company_gstno,company_pan_no,company_aadhar_no,company_st_code,company_st_name,company_bank_name,company_bank_accno,company_ifsc_code,company_bank_address,po_no,po_date,challan_no,challan_date,lr_no,lr_dt,transporter_name,no_of_parcel,due_days,due_date,rsinwords,dpart,mpart,ypart,fyear) values ('$inv_number','$inv_date','$partno','$description_one','$description_two','$list','$qty','$disc_per','$list_value','$disc_amt','$sales_val','$sgst_per','$sgst_amt','$cgst_per','$cgst_amt','$igst_per','$igst_amt','$inv_val','$cost','$cost_val','$rate_one','$hsn_code','$segment','$tax_code','$customer_code','$customer_name','$cust_address_one','$cust_address_two','$cust_address_three','$cust_place','$cust_pin_code','$cust_phone','$cust_mobile','$cust_email','$cust_gstno','$cust_pan_no','$cust_aadhar_no','$cust_st_code','$cust_st_name','$company_code','$company_name','$company_address_one','$company_address_two','$company_address_three','$company_address_four','$company_pin','$company_place','$company_mobile','$company_phone','$company_email','$company_gstno','$company_pan_no','$company_aadhar_no','$company_st_code','$company_st_name','$company_bank_name','$company_bank_accno','$company_ifsc_code','$company_bank_address','$po_no','$po_date','$challan_no','$challan_date','$lr_no','$lr_dt','$transporter_name','$no_of_parcel','$due_days','$due_date','$rsinwords','$dpart','$mpart','$ypart','$year')");
				if ($result2 == FALSE) {
					echo "Insert failed: (" . $connect->errno . ") " . $connect->error;
				}
			}
				$result3 = mysqli_query($connect,"insert into invtotal_mst(inv_number,inv_date,total_qty,total_disc_amt,total_list_value,total_sales_val,total_sgstamt,total_cgstamt,total_igstamt,total_val,roundoff,total_inv_val,total_cost,tax_code,customer_code,customer_name,cust_address_one,cust_address_two,cust_address_three,cust_place,cust_phone,cust_pin_code,cust_email,cust_mobile,cust_gstno,cust_st_code,cust_st_name,cust_aadhar_no,cust_pan_no,po_no,po_date,challan_no,challan_date,lr_no,lr_dt,transporter_name,no_of_parcel,rsinwords,due_days,due_date,dpart,mpart,ypart,fyear,rec_amount,balance_amount,status,ch_amount,inv_type) values ('$inv_number','$inv_date','$total_qty','$total_disc_amt','$total_list_value','$total_sales_val','$total_sgstamt','$total_cgstamt','$total_igstamt','$total_val','$roundoff','$total_inv_val','$total_cost','$tax_code','$customer_code','$customer_name','$cust_address_one','$cust_address_two','$cust_address_three','$cust_place','$cust_phone','$cust_pin_code','$cust_email','$cust_mobile','$cust_gstno','$cust_st_code','$cust_st_name','$cust_aadhar_no','$cust_pan_no','$po_no','$po_date','$challan_no','$challan_date','$lr_no','$lr_dt','$transporter_name','$no_of_parcel','$rsinwords','$due_days','$due_date','$dpart','$mpart','$ypart','$year','$recamt','$total_inv_val','$status','$recamt','$inv_type')");
				$result4 = mysqli_query($connect,"insert into invtotalprt_mst(inv_number,inv_date,total_qty,total_disc_amt,total_list_value,total_sales_val,total_sgstamt,total_cgstamt,total_igstamt,total_val,roundoff,total_inv_val,total_cost,tax_code,customer_code,customer_name,cust_address_one,cust_address_two,cust_address_three,cust_place,cust_phone,cust_pin_code,cust_email,cust_mobile,cust_gstno,cust_st_code,cust_st_name,cust_aadhar_no,cust_pan_no,po_no,po_date,challan_no,challan_date,lr_no,lr_dt,transporter_name,no_of_parcel,rsinwords,due_days,due_date,dpart,mpart,ypart,fyear) values ('$inv_number','$inv_date','$total_qty','$total_disc_amt','$total_list_value','$total_sales_val','$total_sgstamt','$total_cgstamt','$total_igstamt','$total_val','$roundoff','$total_inv_val','$total_cost','$tax_code','$customer_code','$customer_name','$cust_address_one','$cust_address_two','$cust_address_three','$cust_place','$cust_phone','$cust_pin_code','$cust_email','$cust_mobile','$cust_gstno','$cust_st_code','$cust_st_name','$cust_aadhar_no','$cust_pan_no','$po_no','$po_date','$challan_no','$challan_date','$lr_no','$lr_dt','$transporter_name','$no_of_parcel','$rsinwords','$due_days','$due_date','$dpart','$mpart','$ypart','$year')");
				$delinvtemp = mysqli_query($connect,"delete from invtemp_mst") or die(mysqli_last_error());	
				
				$query = "SELECT customer_code,customer_name,opening,debit,credit,clbalance from customer_mst where customer_code = '" . $customer_code . "'";
				$resultstk = mysqli_query($connect,$query);    
				while($rows = mysqli_fetch_array($resultstk)){				
					$opening = $rows["opening"];
					$debit =$rows["debit"];
					$credit =$rows["credit"];
					$clbalance =$rows["clbalance"];
					
					$totaldebit = $debit + $total_inv_val;
					$finalbal = $opening + $totaldebit - $credit;
					$updatestk="update customer_mst set debit = '".$totaldebit."',clbalance = '".$finalbal."' where customer_code='". $customer_code . "'";
					mysqli_query($connect, $updatestk) or die(mysqli_last_error());
				}
		}
		else
		{
		echo "Query failed: (" . $connect->errno . ") " . $connect->error;
		}
		mysqli_close($connect);		
		header("Location:invprt");
	}
?>