<?php include_once('database_connection.php');
if(!(isset($_SESSION['userid'])))
{
   $res=array('status'=>"Error",'description'=>'Session Expired , Please Login  First ');
   echo json_encode($res);
   return;
}
else
{
  $Userid=$_SESSION['userid'];
}
if(isset($_GET['action']))
{
	if($_GET['action']=="groupjobcardinsert")
	{

		//insert into 1 jobcard 
		      odbc_autocommit($connection, FALSE);
  $SideBarID=$_GET['SideBarID'];    
  $permission=userrights($Userid,$SideBarID,'[Create]',$connection);
    if($permission==1)
    {
		$main=json_decode($_POST['data']);
		$details=$_POST['detail'];
		$checklistdetail=$_POST['checklistdetail'];
		$warningdetail=$_POST['warningdetail'];
		$inspectiondetail=$_POST['inspectiondetail'];

		$mrdetails=json_decode($details);
		//$sql="update InvoiceMaster set ";
		/*foreach($main as $key=>$value )
		{

			//echo $key."---".$value;
			if(getDataType2($connection,"invoicemaster",$key)==1)
					{

						$sql.=$key."='".$value."',";
					}
					else
					{
						if($value=="")$value="0";
					$sql .="$key=".$value.",";	
					}
		}*/

			$ShipperID =isset($main->ShipperID)?$main->ShipperID:0;
            $GodownID =isset($main->GodownID)?$main->GodownID:0;
            $SupplierID =isset($main->SupplierID)?$main->SupplierID:0;
            $SupplierName =isset($main->SupplierName)?$main->SupplierName:'';
            $Location =isset($main->Location)?$main->Location:0;
      		
            $Narration =isset($main->Narration)?$main->Narration:'';
            $Narration=htmlentities($Narration);
            $InvoiceType =isset($main->InvoiceType)?$main->InvoiceType:0;
            $QuoteReference =isset($main->QuoteReference)?$main->QuoteReference:null;
            $Notes =isset($main->Notes)?$main->Notes:'';
            $DueDate =isset($main->DeliveryDate)?$main->DeliveryDate:'';
            $InvoiceDate =isset($main->BillDate)?$main->BillDate:'';
            $InvoiceID =isset($main->InvoiceID)?$main->InvoiceID:0;
            $PurchaseLedgerID =isset($main->DebitToHeadID)?$main->DebitToHeadID:0;
            $VATLedgerID =isset($main->VATLedgerID)?$main->VATLedgerID:0;
            $TotalAmount =str_replace('_','.',isset($main->TotalAmt)?$main->TotalAmt:0);
            $DiscountAmount =str_replace('_','.',isset($main->TotalDisountAmount)?$main->TotalDisountAmount:0);
            $TotalTaxAmount =str_replace('_','.',isset($main->TotalTaxAmount)?$main->TotalTaxAmount:0);
            $InculsiveAmount =str_replace('_','.',isset($main->InculsiveAmount)?$main->InculsiveAmount:0);
            $NetAmount =str_replace('_','.',isset($main->NetAmount)?$main->NetAmount:0);
            $MobileNumber =isset($main->Mobile)?$main->Mobile:'';
            $Email =isset($main->Email)?$main->Email:'';
            $CustomerName =isset($main->CustomerName)?$main->CustomerName:'';
            $ChassisNumber =isset($main->ChassisNumber)?$main->ChassisNumber:'';
            $PlateNumber =isset($main->PlateNumber)?$main->PlateNumber:'';
            $TRNNo =isset($main->TRNNo)?$main->TRNNo:'';
            $PreparedBy =isset($main->PreparedBy)?$main->PreparedBy:'';
            $CustomerType =isset($main->CustomerType)?$main->CustomerType:0;
            $VehicleBrand =isset($main->VehicleBrand)?$main->VehicleBrand:0;
            $ChallanNo =(isset($main->ChallanNo) && $main->ChallanNo!="")?$main->ChallanNo:'0';
            $VehicleModel =isset($main->VehicleModel)?$main->VehicleModel:0; 
            $VehicleType =isset($main->VehicleType)?$main->VehicleType:0; 
            $VehicleColor =isset($main->VehicleColor)?$main->VehicleColor:0;  
            $VehicleYear =isset($main->VehicleYear)?$main->VehicleYear:0;  
            $FuelLevel =(isset($main->FuelLevel) && $main->FuelLevel>0)?$main->FuelLevel:0;  
            $MulkiyaNo =isset($main->MulkiyaNo)?$main->MulkiyaNo:'0';  
            $ParkingNo =isset($main->ParkingNo)?$main->ParkingNo:'';  
            $KeyNo =isset($main->KeyNo)?$main->KeyNo:'';  
            $SalesManID =(isset($main->SalesManID) && $main->SalesManID>0 )?$main->SalesManID:'0';  
			$SalesManID2 =(isset($main->SalesManID2) && $main->SalesManID2>0 )?$main->SalesManID2:'0';  
           /* $SalesManID=0;
            $SalesManID2=0;*/
            $PassCode='0';
            $Active=1;
            $InvoiceStatus=1;
            $KeyField="";
          //  $ChallanNo="0";
            $ItemID='0';
            $BillNO=$QuoteReference;
            $GRNno="0";
            $ReferenceInvoiceID="0";
            $ReferenceInvoiceID =isset($mrmaster->ReferenceInvoiceID)?$mrmaster->ReferenceInvoiceID:0;
            if($InvoiceType==6)$GodownID=getnamesingle("invoicemaster","GodownID","InvoiceID",$ReferenceInvoiceID,$connection);
            $BiltyNo=0;
            $ItemID='';
            $InvoiceStatusID=1;
            $DeliveryDate=$DueDate;
            if($InvoiceID=="")$InvoiceID=0;
		    $sql="select * from invoicemaster where invoiceid=".$InvoiceID;
            $result=odbc_exec($connection, $sql);
            if(odbc_num_rows($result)>0)
            {
            	$row=odbc_fetch_array($result);
            	$KeyField=$row['KeyField'];
		 $sql="update InvoiceMaster set SupplierID=".$SupplierID.",MobileNumber='".$MobileNumber."',CustomerName='".$CustomerName."',Email='".$Email."',InvoiceDate='".$InvoiceDate."',DeliveryDate='".$DeliveryDate."',PlateNumber='".$PlateNumber."',ChassisNumber='".$ChassisNumber."',ChallanNo='".$ChallanNo."',SalesManID=".$SalesManID.",SalesManID2=".$SalesManID2.",TRNNo='".$TRNNo."',CustomerType=".$CustomerType.",VehicleBrand=".$VehicleBrand.",VehicleModel=".$VehicleModel.",VehicleType=".$VehicleType.",VehicleColor=".$VehicleColor.",VehicleYear=".$VehicleYear.",Notes='".$Notes."',FuelLevel=".$FuelLevel.",GodownID=".$GodownID.",InvoiceType=".$InvoiceType.",TotalAmount=".$TotalAmount.",DiscountAmount=".$DiscountAmount.",TotalTaxAmount=".$TotalTaxAmount.",NetAmount=".$NetAmount.",MulkiyaNo='".$MulkiyaNo."',ParkingNo='".$ParkingNo."',KeyNo='".$KeyNo."',PictureLocation='',EditLog='".$_SESSION['UserName']."' where InvoiceID=".$InvoiceID;
         /*$sql="update invoicemaster set InvoiceDate='$InvoiceDate',Narration='$Narration',GodownID='$GodownID',DeliveryDate='$DueDate',PurchaseLedgerID=".$PurchaseLedgerID.",VATLedgerID=".$VATLedgerID.",SupplierID=".$SupplierID.",ReferenceInvoiceID=".$ReferenceInvoiceID.",BillNO='".$QuoteReference."',NetAmount=".$NetAmount.",TotalTaxAmount=".$TotalTaxAmount.",TotalAmount=".$TotalAmount."  where InvoiceID=".$InvoiceID;*/      	
            //delete preivous record 
            $sqldelete="delete from invoicedetail where InvoiceID=".$InvoiceID;
            odbc_exec($connection,$sqldelete);
            }
            else
            {
            $InvoiceID=getmaxId("InvoiceMaster","InvoiceID",$connection)+1;
            $PictureLocation='';
           	if($InvoiceType==5)
           	{
           	$where="InvoiceType=5 and GodownID=".$GodownID;	
           	$KeyField=generateCode("InvoiceMaster","KeyField",$where,$connection)+1;
           	//getnamesingle("InvoiceMaster","count(*)","TableID","71",$connection)+1;
           	if($GodownID==1)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="MVR-".$KeyField2;
           	}
           	if($GodownID==2)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="CCL-".$KeyField2;
           	}
           	if($GodownID==3)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="RDL-".$KeyField2;
           	}
           	if($GodownID==4)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="PWS-".$KeyField2;
           	}
           	if($GodownID==0)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="GRP-".$KeyField2;
           	}
           	$ACNarration='AUTO VOUCHER for DOC: '.$KeyField.' from Jobcard';
           	}
            // insert query for invoice master 
               $sql="insert into invoicemaster ([InvoiceID]
           ,InvoiceDate
           ,Narration
           ,InvoiceType
           ,Active
           ,SupplierID
           ,GodownID
           ,ChallanNo
           ,KeyField
           ,BillNO
           ,GRNno
			,TotalAmount
           ,DiscountAmount
           ,TotalTaxAmount
           ,NetAmount
           ,ReferenceInvoiceID
           ,ShipperID
           ,BiltyNo
           ,DueDate
           ,InvoiceStatusID
       	   ,DeliveryDate
           ,InvoiceStatus
           ,VATLedgerID
           ,PurchaseLedgerID,
           MobileNumber,CustomerName,Email,PlateNumber,ChassisNumber,SalesManID,SalesManID2,TRNNo,CustomerType,VehicleBrand,VehicleModel,VehicleType,VehicleColor,VehicleYear,Notes,FuelLevel,MulkiyaNo,ParkingNo,KeyNo,PictureLocation,CreateLog
       ) values (".$InvoiceID.",'".
			$InvoiceDate."'
           ,'".$Narration."'
           ,".$InvoiceType
           .",".$Active
           .",".$SupplierID
           .",".$GodownID
           .",'".$ChallanNo
           ."','".$KeyField."'
           ,'".$BillNO
           ."',".$GRNno
			.",".$TotalAmount
           .",".$DiscountAmount
           .",".$TotalTaxAmount           
           .",".$NetAmount
           .",".$ReferenceInvoiceID
           .",".$ShipperID
           .",'".$BiltyNo
           ."','".$DueDate."'
           ,".$InvoiceStatusID
       	   .",'".$DeliveryDate."'
           ,".$InvoiceStatus."
           ,".$VATLedgerID."
           ,".$PurchaseLedgerID.",'".$MobileNumber."','".$CustomerName."','".$Email."','".$PlateNumber."','".$ChassisNumber."',".$SalesManID.",".$SalesManID2.",'".$TRNNo."',".$CustomerType.",".$VehicleBrand.",".$VehicleModel.",".$VehicleType.",".$VehicleColor.",".$VehicleYear.",'".$Notes."',".$FuelLevel.",'".$MulkiyaNo."','".$ParkingNo."','".$KeyNo."','".$PictureLocation."','".$_SESSION['UserName']."')";
       		}//end else for insert */

       		odbc_exec($connection,$sql);
		//print_r(array_keys($_POST));
		//print_r(array_values($_POST));
		//$data=json_decode($_POST);
		//print_r($_POST);
       		//code for details 
       		insertInvoiceDetail($InvoiceID,$mrdetails,$InvoiceType,$connection);
       				InsertJList($InvoiceID,$checklistdetail,'CheckList',$connection);
       				InsertJList($InvoiceID,$warningdetail,'WarningList',$connection);
       				InsertJList($InvoiceID,$inspectiondetail,'InspectionList',$connection);     

       		// Insert Voucher 

       			$ReferenceInvoiceID=$InvoiceID;
       
	
		//end jobcard insert 

		//get invoice details and insert into company jobcards
            //delete previous record
            $sql="delete from invoiceDetail where invoiceid in(select Invoiceid from InvoiceMaster where ReferenceInvoiceID=$ReferenceInvoiceID)";

            $resultdel=odbc_exec($connection,$sql);

             /* $sql="delete  from InvoiceMaster where ReferenceInvoiceID=$ReferenceInvoiceID";

              odbc_exec($connection,$sql);
*/

       $sql="select * from invoicedetail where InvoiceID=".$ReferenceInvoiceID;
       $result=odbc_exec($connection,$sql);
       while($row=odbc_fetch_array($result))
       {
       	$ItemID=$row['ItemID'];
       	//get items from ingredients 
       	$companyid=0;
       	$mainQty=$row['QTY'];
       	$UnitPrice=$row['UnitPrice'];
       	$Amount=$row['Amount'];
       	$NetAmount=$row['NetAmount'];
       	$TaxAmount=$row['TaxAmount'];
       	$TaxPercent=$row['TaxPercent'];
       	$DiscountAmount=$row['DiscountAmount'];
       	$DiscountPercent=$row['DiscountPercent'];       	
       	// for packages
       	$sqling="select * from Inventory_Ingredients where ItemID=".$ItemID." order by CompanyID";
       	$resultind=odbc_exec($connection,$sqling);
       	if(odbc_num_rows($resultind)>0)
       	{
       		  while($row1=odbc_fetch_array($resultind))
      		 {
      		 	$GodownID=$row1['CompanyID'];
      		 	if($companyid!=$row1['CompanyID'])
      		 	{
      		 		$CompanyInvoiceID=0;
      		 	  $sqlcheck1="select * from invoicemaster where  ReferenceInvoiceID=$ReferenceInvoiceID and GodownID=$GodownID ";
      		 		$resultcheck=odbc_exec($connection,$sqlcheck1) or die(odbc_error().$sqlcheck1);
              $chksql2=getnamewhere("invoicemaster","count(*) as total","ReferenceInvoiceID=$ReferenceInvoiceID and GodownID=$GodownID",$connection);
             // print_r($chksql2);
            //  return;
              $isNew=true;
              while($cht=odbc_fetch_array($resultcheck))
              {
                //echo "inside";
                $InvoiceID=$cht['InvoiceID'];
                $isNew=false;
                break;
              }
              //echo "c=".odbc_num_rows($resultcheck);
              //echo odbc_num_rows($resultcheck);
              //($resultcheck);//odbc_num_rows($resultcheck)<1
      		 		if($isNew==true)
      		 		{
      		 		//insert invoicemaster

            $InvoiceID=getmaxId("InvoiceMaster","InvoiceID",$connection)+1;
            $PictureLocation='';
           	if($InvoiceType==5)
           	{
           	$where="InvoiceType=5 and GodownID=".$GodownID;	
           	$KeyField=generateCode("InvoiceMaster","KeyField",$where,$connection)+1;
           	//getnamesingle("InvoiceMaster","count(*)","TableID","71",$connection)+1;
           	if($GodownID==1)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="MVR-".$KeyField2;
           	}
           	if($GodownID==2)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="CCL-".$KeyField2;
           	}
           	if($GodownID==3)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="RDL-".$KeyField2;
           	}
           	if($GodownID==4)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="PWS-".$KeyField2;
           	}
           	if($GodownID==0)
           	{
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="GRP-".$KeyField2;

           	}
           	$ACNarration='AUTO VOUCHER for DOC: '.$KeyField.' from Jobcard';
           	}
            // insert query for invoice master 
              $sql=" insert into invoicemaster ([InvoiceID]
           ,InvoiceDate
           ,Narration
           ,InvoiceType
           ,Active
           ,SupplierID
           ,GodownID
           ,ChallanNo
           ,KeyField
           ,BillNO
           ,GRNno
			,TotalAmount
           ,DiscountAmount
           ,TotalTaxAmount
           ,NetAmount
           ,ReferenceInvoiceID
           ,ShipperID
           ,BiltyNo
           ,DueDate
           ,InvoiceStatusID
       	   ,DeliveryDate
           ,InvoiceStatus
           ,VATLedgerID
           ,PurchaseLedgerID,
           MobileNumber,CustomerName,Email,PlateNumber,ChassisNumber,SalesManID,SalesManID2,TRNNo,CustomerType,VehicleBrand,VehicleModel,VehicleType,VehicleColor,VehicleYear,Notes,FuelLevel,MulkiyaNo,ParkingNo,KeyNo,PictureLocation,CreateLog
       ) values (".$InvoiceID.",'".
			$InvoiceDate."'
           ,'".$Narration."'
           ,".$InvoiceType
           .",".$Active
           .",".$SupplierID
           .",".$GodownID
           .",".$ChallanNo
           .",'".$KeyField."'
           ,'".$BillNO
           ."',".$GRNno
			.",".($TotalAmount)
           .",".$DiscountAmount
           .",".$TotalTaxAmount           
           .",".$NetAmount
           .",".$ReferenceInvoiceID
           .",".$ShipperID
           .",".$BiltyNo
           .",'".$DueDate."'
           ,".$InvoiceStatusID
       	   .",'".$DeliveryDate."'
           ,".$InvoiceStatus."
           ,".$VATLedgerID."
           ,".$PurchaseLedgerID.",'".$MobileNumber."','".$CustomerName."','".$Email."','".$PlateNumber."','".$ChassisNumber."',".$SalesManID.",".$SalesManID2.",'".$TRNNo."',".$CustomerType.",".$VehicleBrand.",".$VehicleModel.",".$VehicleType.",".$VehicleColor.",".$VehicleYear.",'".$Notes."',".$FuelLevel.",'".$MulkiyaNo."','".$ParkingNo."','".$KeyNo."','".$PictureLocation."','".$_SESSION['UserName']."')";
       		     odbc_exec($connection,$sql) or die(odbc_error().$sql);

      		 		}
      		 		else
      		 		{
      		 			$CompanyInvoiceID=odbc_result($resultcheck,'InvoiceID');
      		 			$updatesql="update InvoiceMaster set  where ";
      		 	//delete if the previous record exists 

      		 		
      		 		}
      		 	}
      		 					$ItemID=$row1['ItemID'];
			$MaterialID=$row1['MaterialID'];
			$Qty=$row1['Qty']*$mainQty;
			$price=$row1['RetailPrice'];
			$vatAmount=$row1['vatAmount']*$mainQty;
			$vatPercent=$row1['VatPercent'];
			$NetAmount=$row1['NetAmount']*$mainQty;			
			$Narration=$row1['Narration'];
			$DiscountAmount=$row1['DiscountAmount'];
			$DiscountPercent=$row1['DiscountPercent'];			
			$Amount=$Qty*$price;
      		  $checksql="select TransactionID from invoicedetail where invoiceID=$CompanyInvoiceID and BOM_InventoryIngredientID='$ItemID' and ItemID='$MaterialID' ";
      		 		$checkresult=odbc_exec($connection,$checksql);
      		 		if(odbc_num_rows($checkresult)>0)
      		 		{
      		 			$TransactionID=odbc_result($checkresult,1);
      		 			//update record
      		 			$update="update invoicedetail set qty=$Qty,unitprice=$price,NetAmount=$NetAmount,TaxAmount=$vatAmount where TransactionID=$TransactionID";
      		 			odbc_exec($connection,$update);
      		 		  $update="update invoicemaster set NetAmount=$NetAmount,TotalTaxAmount=$vatAmount,DiscountAmount=$DiscountAmount where InvoiceID=$CompanyInvoiceID";
      		 			odbc_exec($connection,$update);
      		 		}
      		 		else
      		 		{
      		 			//insert invoice detail
      		 			 $TransactionID=getmaxId("invoiceDetail","TransactionID",$connection)+1;
                $ReceivedQty=0;
                $StockQty=0;
                $UnitID=1;
                $TotalNetAmount=$NetAmount;
                 $sql="insert into invoicedetail (
InvoiceID
           ,ItemID
           ,UnitPrice
           ,QTY
           ,Amount
           ,UnitID
           ,NetAmount
           ,TaxPercent
           ,TaxAmount
           ,Narration
           ,TotalNetAmount       
           ,ReceivedQty
           ,StockQty
           ,isDelete
           ,DiscountAmount
           ,DiscountPercent
           ,BOM_InventoryIngredientID
        ) values("
       .$InvoiceID."
           ,".$MaterialID."
           ,".$price."
           ,".$Qty."
           ,".$Amount."
           ,".$UnitID."
           ,".$NetAmount."
           ,".$vatPercent."
           ,".$vatAmount."
           ,'".$Narration."'
           ,".$TotalNetAmount."
           ,".$ReceivedQty.
          ",".$StockQty.",0,".$DiscountAmount.",0,".$ItemID.")";
          odbc_exec($connection, $sql) or die(odbc_error().$sql);
          $update="update invoicemaster set TotalAmount=(select sum(Amount) from invoicedetail where invoiceid=$InvoiceID), NetAmount=(select sum(NetAmount) from invoicedetail where invoiceid=$InvoiceID),TotalTaxAmount=(select sum(TaxAmount) from invoicedetail where invoiceid=$InvoiceID),DiscountAmount=$DiscountAmount where InvoiceID=$InvoiceID";
      		 			odbc_exec($connection,$update);
      		 		}

			//Invoice detail company entry 

      		 }//end while Inventory_Ingredients 
       }//end if
       else
       {
       	// for other services/items 
       	//check the services companyid 
       	$CompanyID=getnamesingle("inventory","CompanyID","ID",$ItemID,$connection);
       	// check if jobcard already exists 
       	   $sql="select * from invoicemaster  where ReferenceInvoiceID=$ReferenceInvoiceID and GodownID=$CompanyID";//
       	$resultjobcard=odbc_exec($connection,$sql) or die(odbc_error().$sql);
        $isNew=true;
        while($cht=odbc_fetch_array($resultjobcard))
        {
          $InvoiceID=$cht['InvoiceID'];
          $isNew=false;
          break;
        }
        //odbc_num_rows($resultjobcard)>0
       	if($isNew==false)
       	{
//          echo "called";
       		//update jobcard
       		$InvoiceID=odbc_result($resultjobcard,1);
       	 $sql="update invoicemaster where invoiceid=$InvoiceID";
       	}
       	else
       	{
       		//insert jobcard

      		 		//insert invoicemaster
       		$GodownID=$CompanyID;
            $InvoiceID=getmaxId("InvoiceMaster","InvoiceID",$connection)+1;
            $PictureLocation='';
           	if($InvoiceType==5)
           	{
           	$where="InvoiceType=5 and GodownID=".$GodownID;	
           	$KeyField=generateCode("InvoiceMaster","KeyField",$where,$connection)+1;
           	//getnamesingle("InvoiceMaster","count(*)","TableID","71",$connection)+1;
           	if($GodownID==1)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="MVR-".$KeyField2;
           	}
           	if($GodownID==2)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="CCL-".$KeyField2;
           	}
           	if($GodownID==3)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="RDL-".$KeyField2;
           	}
           	if($GodownID==4)
           	{	
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="PWS-".$KeyField2;
           	}
           	if($GodownID==0)
           	{
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="GRP-".$KeyField2;

           	}
           	$ACNarration='AUTO VOUCHER for DOC: '.$KeyField.' from Jobcard';
           	}
            // insert query for invoice master 
          $sql=" insert into invoicemaster ([InvoiceID]
           ,InvoiceDate
           ,Narration
           ,InvoiceType
           ,Active
           ,SupplierID
           ,GodownID
           ,ChallanNo
           ,KeyField
           ,BillNO
           ,GRNno
			,TotalAmount
           ,DiscountAmount
           ,TotalTaxAmount
           ,NetAmount
           ,ReferenceInvoiceID
           ,ShipperID
           ,BiltyNo
           ,DueDate
           ,InvoiceStatusID
       	   ,DeliveryDate
           ,InvoiceStatus
           ,VATLedgerID
           ,PurchaseLedgerID,
           MobileNumber,CustomerName,Email,PlateNumber,ChassisNumber,SalesManID,SalesManID2,TRNNo,CustomerType,VehicleBrand,VehicleModel,VehicleType,VehicleColor,VehicleYear,Notes,FuelLevel,MulkiyaNo,ParkingNo,KeyNo,PictureLocation,CreateLog
       ) values (".$InvoiceID.",'".
			$InvoiceDate."'
           ,'".$Narration."'
           ,".$InvoiceType
           .",".$Active
           .",".$SupplierID
           .",".$GodownID
           .",'".$ChallanNo
           ."','".$KeyField."'
           ,'".$BillNO
           ."',".$GRNno
			.",".$TotalAmount
           .",".$DiscountAmount
           .",".$TotalTaxAmount           
           .",".$NetAmount
           .",".$ReferenceInvoiceID
           .",".$ShipperID
           .",".$BiltyNo
           .",'".$DueDate."'
           ,".$InvoiceStatusID
       	   .",'".$DeliveryDate."'
           ,".$InvoiceStatus."
           ,".$VATLedgerID."
           ,".$PurchaseLedgerID.",'".$MobileNumber."','".$CustomerName."','".$Email."','".$PlateNumber."','".$ChassisNumber."',".$SalesManID.",".$SalesManID2.",'".$TRNNo."',".$CustomerType.",".$VehicleBrand.",".$VehicleModel.",".$VehicleType.",".$VehicleColor.",".$VehicleYear.",'".$Notes."',".$FuelLevel.",'".$MulkiyaNo."','".$ParkingNo."','".$KeyNo."','".$PictureLocation."','".$_SESSION['UserName']."')";
       		     odbc_exec($connection,$sql);

      		 		       	}

      		 			//insert invoice detail
      		 			// $TransactionID=getmaxId("invoiceDetail","TransactionID",$connection)+1;
                $ReceivedQty=0;
                $StockQty=0;
                $UnitID=1;
                $TotalNetAmount=$NetAmount;
                $sql="insert into invoicedetail (
InvoiceID
           ,ItemID
           ,UnitPrice
           ,QTY
           ,Amount
           ,UnitID
           ,NetAmount
           ,TaxPercent
           ,TaxAmount
           ,Narration
           ,TotalNetAmount       
           ,ReceivedQty
           ,StockQty
           ,isDelete
           ,DiscountAmount
           ,DiscountPercent
           ,BOM_InventoryIngredientID
        ) values("
       .$InvoiceID."
           ,".$ItemID."
           ,".$UnitPrice."
           ,".$mainQty."
           ,".$Amount."
           ,".$UnitID."
           ,".$NetAmount."
           ,".$TaxPercent."
           ,".$TaxAmount."
           ,'".$Narration."'
           ,".$TotalNetAmount."
           ,".$ReceivedQty.
          ",".$StockQty.",0,".$DiscountAmount.",0,0)";
          odbc_exec($connection, $sql) or die(odbc_error().$sql);
        $update="update invoicemaster set TotalAmount=(select sum(Amount) from invoicedetail where invoiceid=$InvoiceID), NetAmount=(select sum(NetAmount) from invoicedetail where invoiceid=$InvoiceID),TotalTaxAmount=(select sum(TaxAmount) from invoicedetail where invoiceid=$InvoiceID),DiscountAmount=$DiscountAmount where InvoiceID=$InvoiceID";
          odbc_exec($connection,$update);
       }//end else 
       
       } //end while invoice detail 
        if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
  }
else
{
      odbc_rollback($connection);
      $res=array('status'=>"Error",'description'=>'Record  Not Updated');

}
    $res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
       }
       else
       {
        $res=array('status'=>"Error",'description'=>'Sorry You Dont have Permission to Enter JobCard');
       }

       echo json_encode($res);
	}//end group jobcard insert
}//end action 

//

?>