<?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");
    }    
    $tax_code = $_POST['tax_code'];
    $supplier_code = $_POST['supplier_code'];
    $supplier_name = $_POST['supplier_name'];
    $supplier_address_one = $_POST['supplier_address_one'];
    $supplier_address_two = $_POST['supplier_address_two'];
    $supplier_address_three = $_POST['supplier_address_three'];
    $supplier_place = $_POST['supplier_place'];
    $supplier_pin_code = $_POST['supplier_pin_code'];
    $supplier_phone = $_POST['supplier_phone'];
    $supplier_mobile = $_POST['supplier_mobile'];
    $supplier_email = $_POST['supplier_email'];
    $supplier_gstno = $_POST['supplier_gstno'];
    $supplier_pan_no = $_POST['supplier_pan_no'];
    $supplier_aadhar_no = $_POST['supplier_aadhar_no'];
    $supplier_st_code = $_POST['supplier_st_code'];
    $supplier_st_name = $_POST['supplier_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'];
    $grn_number = $_POST['grn_number'];
    $grn_date = $_POST['grn_date'];
    $srnoupd = $_POST['grn_number'] + 1;
    $inv_type = 'INVOICE';
		if($tax_code == 'GST-B2B'){
			$update="update control_mst set srno = '".$srnoupd."' where description='GRN'";
			mysqli_query($connect, $update) or die(mysqli_error());
		}
		if($tax_code == 'I-GST'){
			$update="update control_mst set srno = '".$srnoupd."' where description='GRN'";
			mysqli_query($connect, $update) or die(mysqli_error());
		}
		if($tax_code == 'GST-B2C'){
			$update="update control_mst set srno = '".$srnoupd."' where description='GRN'";
			mysqli_query($connect, $update) or die(mysqli_error());
		}		
		if ($rs = mysqli_query($connect, "select * from company_mst order by company_name"))
		{
			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'];				
			}			
		}
		if ($rs10 = mysqli_query($connect, "select * from supplier_mst where supplier_code = '".$supplier_code."'"))
		{
			while($row1 = mysqli_fetch_array($rs10))
			{				
				$company_bank_name=$row1['bankname'];
				$company_bank_accno=$row1['bankaccno'];
				$company_ifsc_code=$row1['ifsccode'];
				$company_bank_address=$row1['bankaccname'];
			}			
		}
		$delinv = mysqli_query($connect,"delete from pinvprt_mst") or die(mysqli_last_error());
		$delinvtotaltemp = mysqli_query($connect,"delete from pinvtotalprt_mst") or die(mysqli_last_error());		
		if ($result1 = mysqli_query($connect, "select * from pinvtemp_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 pinv_mst(inv_number,inv_date,grn_number,grn_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,supplier_code,supplier_name,supplier_address_one,supplier_address_two,supplier_address_three,supplier_place,supplier_pin_code,supplier_phone,supplier_mobile,supplier_email,supplier_gstno,supplier_pan_no,supplier_aadhar_no,supplier_st_code,supplier_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','$grn_number','$grn_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','$supplier_code','$supplier_name','$supplier_address_one','$supplier_address_two','$supplier_address_three','$supplier_place','$supplier_pin_code','$supplier_phone','$supplier_mobile','$supplier_email','$supplier_gstno','$supplier_pan_no','$supplier_aadhar_no','$supplier_st_code','$supplier_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 pinvprt_mst(inv_number,inv_date,grn_number,grn_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,supplier_code,supplier_name,supplier_address_one,supplier_address_two,supplier_address_three,supplier_place,supplier_pin_code,supplier_phone,supplier_mobile,supplier_email,supplier_gstno,supplier_pan_no,supplier_aadhar_no,supplier_st_code,supplier_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','$grn_number','$grn_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','$supplier_code','$supplier_name','$supplier_address_one','$supplier_address_two','$supplier_address_three','$supplier_place','$supplier_pin_code','$supplier_phone','$supplier_mobile','$supplier_email','$supplier_gstno','$supplier_pan_no','$supplier_aadhar_no','$supplier_st_code','$supplier_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 pinvtotal_mst(inv_number,inv_date,grn_number,grn_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,supplier_code,supplier_name,supplier_address_one,supplier_address_two,supplier_address_three,supplier_place,supplier_phone,supplier_pin_code,supplier_email,supplier_mobile,supplier_gstno,supplier_st_code,supplier_st_name,supplier_aadhar_no,supplier_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','$grn_number','$grn_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','$supplier_code','$supplier_name','$supplier_address_one','$supplier_address_two','$supplier_address_three','$supplier_place','$supplier_phone','$supplier_pin_code','$supplier_email','$supplier_mobile','$supplier_gstno','$supplier_st_code','$supplier_st_name','$supplier_aadhar_no','$supplier_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 pinvtotalprt_mst(inv_number,inv_date,grn_number,grn_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,supplier_code,supplier_name,supplier_address_one,supplier_address_two,supplier_address_three,supplier_place,supplier_phone,supplier_pin_code,supplier_email,supplier_mobile,supplier_gstno,supplier_st_code,supplier_st_name,supplier_aadhar_no,supplier_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','$grn_number','$grn_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','$supplier_code','$supplier_name','$supplier_address_one','$supplier_address_two','$supplier_address_three','$supplier_place','$supplier_phone','$supplier_pin_code','$supplier_email','$supplier_mobile','$supplier_gstno','$supplier_st_code','$supplier_st_name','$supplier_aadhar_no','$supplier_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 pinvtemp_mst") or die(mysqli_last_error());
				
				$query = "SELECT supplier_code,supplier_name,opening,debit,credit,clbalance from supplier_mst where supplier_code = '" . $supplier_code . "'";
				$resultstk = mysqli_query($connect,$query);    
				while($rows = mysqli_fetch_array($resultstk)){				
					$opening = $rows["opening"];
					$debit =$rows["debit"];
					$credit =$rows["credit"];
					$clbalance =$rows["clbalance"];
					
					$totalcredit = $credit + $total_inv_val;
					$finalbal = $opening + $totalcredit - $debit;
					$updatestk="update supplier_mst set credit = '".$totalcredit."',clbalance = '".$finalbal."' where supplier_code='". $supplier_code . "'";
					mysqli_query($connect, $updatestk) or die(mysqli_last_error());
				}
		}
		else
		{
		echo "Query failed: (" . $connect->errno . ") " . $connect->error;
		}
		mysqli_close($connect);		
		header("Location:invprt");
	}
?>