<?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']))
{
//	print_r($_GET);
	//print_r($_POST);
	$data=array();
	if($_GET['action']=="geteVoucher")
  {


    $data=array();
    $PackageID=$_GET['PackageID'];
    $sql="select ID,Degree,[group],InitiateDate,Name,standardprice2,under,Attr1,Attr2,(select NAme from inventory as i where i.id=Inventory.under) as Category,ProductBaseType,Active,KeyField,itemType from Inventory where id=$PackageID";
    $result=odbc_exec($connection,$sql);
    while ($row = odbc_fetch_array($result)) 
      {
        foreach ($row as $key => $value) {
        $row[$key]=utf8_encode($value);
        }
      $data[][]=$row;
      }
$sql="select *,(select Name from Inventory where ID=eVoucher_policy.MaterialID) as Name,(select Name from Inventory where ID=eVoucher_policy.Under) as SubCategory,(select Name from Inventory where ID=(select Under from Inventory where ID=eVoucher_policy.Under)) as Category,(select Under from Inventory where ID=eVoucher_policy.Under) as MainCategory_ID from eVoucher_policy where ItemID=$PackageID";

          $result=odbc_exec($connection,$sql);
    while($row=odbc_fetch_array($result))
    {
      $data['detailitems'][]=$row;
    }   
echo json_encode($data);
  
  }
  if($_GET['action']=="getPackage")
	{

		$data=array();
		$PackageID=$_GET['PackageID'];
		$sql="select ID,Degree,[group],InitiateDate,Name,standardprice2,under,(select NAme from inventory as i where i.id=Inventory.under) as Category,ProductBaseType,Active,KeyField from Inventory where id=$PackageID";
		$result=odbc_exec($connection,$sql);
		while ($row = odbc_fetch_array($result)) 
  		{
  			foreach ($row as $key => $value) {
  			$row[$key]=utf8_encode($value);
  			}
      $data[][]=$row;
  		}
$sql="select *,(select Name from Inventory where ID=Inventory_Ingredients.MaterialID) as Name from Inventory_Ingredients where ItemID=$PackageID";

  		    $result=odbc_exec($connection,$sql);
    while($row=odbc_fetch_array($result))
    {
      $data['detailitems'][]=$row;
    }   
echo json_encode($data);
	}
  if($_GET['action']=="getProductMaterial")
  {


    $data=array();
    $PackageID=$_GET['PackageID'];
    $sql="select ID,Degree,[group],InitiateDate,Name,standardprice2,under as SubCategoryID,(select NAme from inventory as i where i.id=Inventory.under) as Category,(select ID from inventory as i where i.id=Inventory.under) as CategoryID,ProductBaseType,Active,KeyField from Inventory where id=$PackageID";
    $result=odbc_exec($connection,$sql);
    while ($row = odbc_fetch_array($result)) 
      {
        foreach ($row as $key => $value) {
        $row[$key]=utf8_encode($value);
        }
      $data[][]=$row;
      }
$sql="select *,(select Name from inventory where id=(select Under from inventory where id=(select under from inventory as i where i.id=Inventory_Ingredients.MaterialID))) as Category,(select Under from inventory where id=(select under from inventory as i where i.id=Inventory_Ingredients.MaterialID)) as CategoryID,(select Name from inventory where id=(select under from inventory as i where i.id=Inventory_Ingredients.MaterialID)) as SubCategory,(select Under from inventory as i where i.id=Inventory_Ingredients.MaterialID) as SubCategoryID,(select Name from Inventory where ID=Inventory_Ingredients.MaterialID) as Name from Inventory_Ingredients where ItemID=$PackageID";

          $result=odbc_exec($connection,$sql);
    while($row=odbc_fetch_array($result))
    {
      $data['detailitems'][]=$row;
    }   
echo json_encode($data);
  
  }
	if($_GET['action']=="DisapprovePackage")
	{
       odbc_autocommit($connection, FALSE);
		$PackageID=$_GET['PackageID'];
		$sql="update inventory set Active=0 where id=$PackageID";
		odbc_exec($connection,$sql) or die(odbc_error().$sql);
			$res=array('status'=>"ok",'description'=>'Package Disapproved Sucessfully');
		echo json_encode($res);
       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');

}
	}
	if($_GET['action']=="ApprovePackage")
	{
       odbc_autocommit($connection, FALSE);
		$PackageID=$_GET['PackageID'];
		$sql="update inventory set Active=1 where id=$PackageID";
		odbc_exec($connection,$sql) or die(odbc_error().$sql);
			$res=array('status'=>"ok",'description'=>'Package Approved Sucessfully');
		echo json_encode($res);
       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');

}
	
  }
  if($_GET['action']=="evoucherlist")
  {

    $data=array();
    $sql="select CASE when active=1 then 'Active' else 'DeActive'  end as Active1,ID,Degree,[group],InitiateDate,Name,StandardPrice2,under,ProductBaseType,Active,KeyField from inventory where ProductBaseType='eVoucher' and [group]=0";
    $result=odbc_exec($connection,$sql);
    while ($row = odbc_fetch_array($result)) 
    {
      foreach ($row as $key => $value) {
        $row[$key]=utf8_encode($value);
      }
        $data []=$row;
    }
          echo json_encode($data); 

  
  }
  if($_GET['action']=="ProductMaterialList")
  {
    $data=array();
    $sql="select CASE when active=1 then 'Active' else 'DeActive'  end as Active1,ID,Degree,[group],InitiateDate,Name,StandardPrice2,under,ProductBaseType,Active,KeyField from inventory where exists (select * from Inventory_Ingredients where ItemID=inventory.id and ProductBaseType='item') and [group]=0";
    $result=odbc_exec($connection,$sql);
    while ($row = odbc_fetch_array($result)) 
    {
      foreach ($row as $key => $value) {
        $row[$key]=utf8_encode($value);
      }
        $data []=$row;
    }
          echo json_encode($data); 

  }
	if($_GET['action']=="packagelist")
	{
		$data=array();
		$sql="select CASE when active=1 then 'Active' else 'DeActive'  end as Active1,ID,Degree,[group],InitiateDate,Name,StandardPrice2,under,ProductBaseType,Active,KeyField from inventory where ProductBaseType='Package' and [group]=0";
		$result=odbc_exec($connection,$sql);
		while ($row = odbc_fetch_array($result)) 
  	{
  		foreach ($row as $key => $value) {
  			$row[$key]=utf8_encode($value);
  		}
  	    $data []=$row;
  	}
  	  		echo json_encode($data); 

	}
  if($_GET['action']=="eVoucherInsert")
  {

       odbc_autocommit($connection, FALSE);
$PackageID=0;
  $main=json_decode($_POST['data']);
    $details=$_POST['detail'];
    
    $mrdetail=json_decode($details);      
   /* print_r($main);
    print_r($mrdetail);    
    return;*/
        if(isset($main->InvoiceID))
      $ID=$main->InvoiceID;
      else
      $ID=0;
    
    $itemType=(isset($main->VoucherTypeID) && $main->VoucherTypeID!="")?$main->VoucherTypeID:0;
    $COAID=(isset($main->COAID) && $main->COAID!="")?$main->COAID:0;
    $PackageName =isset($main->PackageName)?$main->PackageName:'';
    $ExpiryDate =isset($main->ExpiryDate)?$main->ExpiryDate:Date("Y-m-d");
    $Price =isset($main->TotalAmt)?$main->TotalAmt:0;
    $TotalTaxAmount=(isset($main->TotalTaxAmount) && $main->TotalTaxAmount!="")?$main->TotalTaxAmount:0;
    $Discount=(isset($main->ServiceDiscount) && $main->ServiceDiscount!="")?$main->ServiceDiscount:0;
    if($ID==0)
    {
    //echo "<br>insert here";
      $where=" ProductBaseType='eVoucher' ";
       $KeyField=generateCode("Inventory","KeyField",$where,$connection)+1;
            $KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
            $KeyField="EV-".$KeyField2;


            $Under=3200;//package
$ID=getMaxId("inventory","ID",$connection)+1;
     $sql="insert into inventory (ID,Degree,[group],InitiateDate,Name,standardprice2,under,ProductBaseType,Active,KeyField,VatPercent,itemType,Attr1,Attr2) values($ID,'2',0,'$ExpiryDate','$PackageName',$Price,$Under,'eVoucher',1,'$KeyField',5,$itemType,'$COAID','$Discount')";
$PackageID=$ID;
    }
    else
    {
    $PackageID=$ID; 
    $PackageName =isset($main->PackageName)?$main->PackageName:'';
    $ExpiryDate =isset($main->ExpiryDate)?$main->ExpiryDate:'';
    $Price =isset($main->NetAmount)?$main->NetAmount:'';
  $sql="update inventory set InitiateDate='$ExpiryDate',standardprice2=$Price,Name='$PackageName',itemType=$itemType,Attr1='$COAID' where id=$PackageID";
    }
    odbc_exec($connection, $sql) or die(odbc_error().$sql);
    //update 
    if(count($mrdetail)>0)
    {
$sqldelete="delete from eVoucher_policy where ItemID=$ID";
odbc_exec($connection, $sqldelete) or die(odbc_error().$sqldelete);

    for($a=0;$a<count($mrdetail);$a++)
            {
            $Flag =isset($mrdetail[$a]->Flag)?$mrdetail[$a]->Flag:'';
            if($Flag=="D")continue;
            if($Flag!="D")
            {
//
            $ItemID =(isset($mrdetail[$a]->Item_ItemID) && $mrdetail[$a]->Item_ItemID!="")?$mrdetail[$a]->Item_ItemID:0;
            
 $ItemName =isset($mrdetail[$a]->Item_ItemName)?$mrdetail[$a]->Item_ItemName:'';
     
            $UnitPrice =(isset($mrdetail[$a]->Rate) && $mrdetail[$a]->Rate!="")?$mrdetail[$a]->Rate:0;
            $StockQty =isset($mrdetail[$a]->StockQty)?$mrdetail[$a]->StockQty:0;
             $Qty =isset($mrdetail[$a]->Qty)?$mrdetail[$a]->Qty:0;
            //}
    $Amount =(isset($mrdetail[$a]->Amount) && $mrdetail[$a]->Amount>0)?$mrdetail[$a]->Amount:($ReceivedQty*$UnitPrice);
            $Discount =(isset($mrdetail[$a]->Discount) && $mrdetail[$a]->Discount>=0)?(double)$mrdetail[$a]->Discount:0;
            $TaxPercent =(isset($mrdetail[$a]->VAT) && $mrdetail[$a]->VAT>0)?(double)$mrdetail[$a]->VAT:0;
            $NetAmount =(isset($mrdetail[$a]->NetAmount) && $mrdetail[$a]->NetAmount>0)?$mrdetail[$a]->NetAmount:0;
            $TaxAmount =(isset($mrdetail[$a]->VATAmount) && $mrdetail[$a]->VATAmount>0)?$mrdetail[$a]->VATAmount:0;
            $DiscountAmount =(isset($mrdetail[$a]->DiscountAmount) && $mrdetail[$a]->DiscountAmount>0)?$mrdetail[$a]->DiscountAmount:0;
            $Narration=(isset($mrdetail[$a]->Narration) && $mrdetail[$a]->Narration!="")?$mrdetail[$a]->Narration:'-';
            $UnitID=1;
            $TaxableAmount=($NetAmount-$TaxAmount);
            $TotalNetAmount=0;
            $InvoiceType=0;
              if($InvoiceType==0)
              {

                $Category =isset($mrdetail[$a]->MainCategory_ID)?$mrdetail[$a]->MainCategory_ID:0;
                $SubCategory =isset($mrdetail[$a]->SubCategory_ID)?$mrdetail[$a]->SubCategory_ID:0;                
                $Level_ID =isset($mrdetail[$a]->Level_ID)?$mrdetail[$a]->Level_ID:0;
                $VATPercent= (isset($mrdetail[$a]->VAT) && $mrdetail[$a]->VAT>0)?(double)$mrdetail[$a]->VAT:0;
                $VATAmount =(isset($mrdetail[$a]->VATAmount) && $mrdetail[$a]->VATAmount>0)?$mrdetail[$a]->VATAmount:0;
              }
            else
            {
$Category =isset($mrdetail[$a]->MainCategory_ID)?$mrdetail[$a]->MainCategory_ID:0;
                $SubCategory =isset($mrdetail[$a]->SubCategory_ID)?$mrdetail[$a]->SubCategory_ID:0;    
                 $VATPercent=$TaxPercent;
            $VATAmount=$TaxAmount;
            }
$CompanyID=getnamesingle("Inventory","CompanyID","ID",$mrdetail[$a]->MainCategory_ID,$connection);
                    
              $TransactionID=getmaxId("eVoucher_policy","ID",$connection)+1;
              if($TransactionID==0 || $TransactionID==null)$TransactionID=0;
                  $sql="insert into eVoucher_policy (ID,
       ItemID
       ,MaterialID
           ,RetailPrice
           ,QTY
           ,RetailAmount
           ,NetAmount
           ,VatPercent
           ,VatAmount
           ,Narration
           ,TaxableAmount
           
           ,CompanyID
           ,Under
        ) values($TransactionID,".$PackageID."
           ,".$ItemID."
           ,".$UnitPrice."
           ,".$Qty."
           ,".$Amount."
           ,".$NetAmount."
           ,".$TaxPercent."
           ,".$TaxAmount."
           ,'".$Narration."'
           ,".$TaxableAmount."
          ,".$CompanyID."
      ,".$SubCategory.")";
          odbc_exec($connection, $sql) or die(odbc_error().$sql);
          }//end if flag
          }//end for loop
      }
//  $res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
        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');

}
    echo json_encode($res);
  
  }
  //
  if($_GET['action']=="MaterialInsert")
  {

       odbc_autocommit($connection, FALSE);
$PackageID=0;
  $main=json_decode($_POST['data']);
    $details=$_POST['detail'];
    
    $mrdetail=json_decode($details);      
  // print_r($main);
  // print_r($mrdetail);    
    //print_r($ProductID);

    
   $ProductID=isset($main->ProductID)?json_decode($main->ProductID):0; 
 
 $PackageID=isset($ProductID->ID)?$ProductID->ID:$ProductID;
//return;
        if(isset($main->InvoiceID))
      $ID=$main->InvoiceID;
      else
      $ID=0;


    
  //  $PackageID=isset($main->ProductID)?$main->ProductID:0;    ; 
 //   $PackageName =isset($main->PackageName)?$main->PackageName:'';
 //   $ExpiryDate =isset($main->ExpiryDate)?$main->ExpiryDate:'';
 //   $Price =isset($main->NetAmount)?$main->NetAmount:'';
  /*$sql="update inventory set InitiateDate='$ExpiryDate',standardprice2=$Price,Name='$PackageName' where id=$PackageID";*/
    
    //odbc_exec($connection, $sql) or die(odbc_error().$sql);
    //update 
    if(count($mrdetail)>0)
    {
$sqldelete="delete from Inventory_Ingredients where ItemID=$PackageID";
odbc_exec($connection, $sqldelete) or die(odbc_error().$sqldelete);

$CostPrice=0;
    for($a=0;$a<count($mrdetail);$a++)
            {
            $Flag =isset($mrdetail[$a]->Flag)?$mrdetail[$a]->Flag:'';
            if($Flag=="D")continue;
            if($Flag!="D")
            {
//
            $ItemID =isset($mrdetail[$a]->Item_ItemID)?$mrdetail[$a]->Item_ItemID:'';
            
 $ItemName =isset($mrdetail[$a]->Item_ItemName)?$mrdetail[$a]->Item_ItemName:'';
     
            $UnitPrice =(isset($mrdetail[$a]->Rate) && $mrdetail[$a]->Rate!="")?$mrdetail[$a]->Rate:0;
            $StockQty =isset($mrdetail[$a]->StockQty)?$mrdetail[$a]->StockQty:0;
             $Qty =isset($mrdetail[$a]->Qty)?(double)$mrdetail[$a]->Qty:0;
            //}
    $Amount =(isset($mrdetail[$a]->Amount) && $mrdetail[$a]->Amount>0)?$mrdetail[$a]->Amount:($ReceivedQty*$UnitPrice);
            $Discount =(isset($mrdetail[$a]->Discount) && $mrdetail[$a]->Discount>=0)?(double)$mrdetail[$a]->Discount:0;
            $TaxPercent =(isset($mrdetail[$a]->VAT) && $mrdetail[$a]->VAT>0)?(double)$mrdetail[$a]->VAT:0;
            $NetAmount =(isset($mrdetail[$a]->NetAmount) && $mrdetail[$a]->NetAmount>0)?$mrdetail[$a]->NetAmount:0;
$CostPrice=$CostPrice+$NetAmount;            
            $TaxAmount =(isset($mrdetail[$a]->VATAmount) && $mrdetail[$a]->VATAmount>0)?$mrdetail[$a]->VATAmount:0;
            $DiscountAmount =(isset($mrdetail[$a]->DiscountAmount) && $mrdetail[$a]->DiscountAmount>0)?$mrdetail[$a]->DiscountAmount:0;
            $Narration=(isset($mrdetail[$a]->Narration) && $mrdetail[$a]->Narration!="")?$mrdetail[$a]->Narration:'-';
            $UnitID=1;
            $TaxableAmount=($NetAmount-$TaxAmount);
            $TotalNetAmount=0;
            $InvoiceType=0;
              if($InvoiceType==8)
              {

                $Category =isset($mrdetail[$a]->MainCategory_ID)?$mrdetail[$a]->MainCategory_ID:0;
                $Level_ID =isset($mrdetail[$a]->Level_ID)?$mrdetail[$a]->Level_ID:0;
                $VATPercent= (isset($mrdetail[$a]->VAT) && $mrdetail[$a]->VAT>0)?(double)$mrdetail[$a]->VAT:0;
                $VATAmount =(isset($mrdetail[$a]->VATAmount) && $mrdetail[$a]->VATAmount>0)?$mrdetail[$a]->VATAmount:0;
              }
            else
            {
                 $VATPercent=$TaxPercent;
            $VATAmount=$TaxAmount;
            }
                    $CompanyID=1;//getnamesingle("Inventory","CompanyID","ID",$ItemID,$connection);
                    
              $TransactionID=getmaxId("Inventory_Ingredients","InventoryIngredientID",$connection)+1;
                  $sql="insert into Inventory_Ingredients (
       ItemID
       ,MaterialID
           ,RetailPrice
           ,QTY
           ,RetailAmount
           ,NetAmount
           ,VatPercent
           ,VatAmount
           ,Narration
           ,TaxableAmount
           ,CompanyID
        ) values(".$PackageID."
           ,".$ItemID."
           ,".$UnitPrice."
           ,".$Qty."
           ,".$Amount."
           ,".$NetAmount."
           ,".$TaxPercent."
           ,".$TaxAmount."
           ,'".$Narration."'
           ,".$TaxableAmount."
           ,".$CompanyID.")";
          odbc_exec($connection, $sql) or die(odbc_error().$sql);
          }//end if flag
          }//end for loop
  $sqlupdate="update Inventory_Ingredients set CostPrice=$CostPrice where ItemID=$PackageID";
odbc_exec($connection, $sqlupdate) or die(odbc_error().$sqlupdate);       
$sqlupdate="update Inventory set StandardPrice1=$CostPrice where ID=$PackageID";  
odbc_exec($connection, $sqlupdate) or die(odbc_error().$sqlupdate);          
         
      }
//  $res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
        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');

}
    echo json_encode($res);
  
  }
	if($_GET['action']=="packageInsert")
	{
       odbc_autocommit($connection, FALSE);
$PackageID=0;
	$main=json_decode($_POST['data']);
    $details=$_POST['detail'];
    
    $mrdetail=json_decode($details);			
   /* print_r($main);
    print_r($mrdetail);    
    return;*/
    		if(isset($main->InvoiceID))
			$ID=$main->InvoiceID;
			else
			$ID=0;
		
		if($ID==0)
		{
		//echo "<br>insert here";
			$where=" ProductBaseType='Package' ";
			 $KeyField=generateCode("Inventory","KeyField",$where,$connection)+1;
           	$KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
           	$KeyField="Pack-".$KeyField2;

		$PackageName =isset($main->PackageName)?$main->PackageName:'';
		$ExpiryDate =isset($main->ExpiryDate)?$main->ExpiryDate:'';
		$Price =isset($main->TotalAmt)?$main->TotalAmt:'';
    $TotalTaxAmount=isset($main->TotalTaxAmount)?$main->TotalTaxAmount:'';

						$Under=3200;//package
$ID=getMaxId("inventory","ID",$connection)+1;
		 $sql="insert into inventory (ID,Degree,[group],InitiateDate,Name,standardprice2,under,ProductBaseType,Active,KeyField,VatPercent) values($ID,'2',0,'$ExpiryDate','$PackageName',$Price,$Under,'Package',1,'$KeyField',5)";
$PackageID=$ID;
		}
		else
		{
		$PackageID=$ID;	
		$PackageName =isset($main->PackageName)?$main->PackageName:'';
		$ExpiryDate =isset($main->ExpiryDate)?$main->ExpiryDate:'';
		$Price =isset($main->NetAmount)?$main->NetAmount:'';
	$sql="update inventory set InitiateDate='$ExpiryDate',standardprice2=$Price,Name='$PackageName' where id=$PackageID";
		}
		odbc_exec($connection, $sql) or die(odbc_error().$sql);
		//update 
		if(count($mrdetail)>0)
		{
$sqldelete="delete from Inventory_Ingredients where ItemID=$ID";
odbc_exec($connection, $sqldelete) or die(odbc_error().$sqldelete);

		for($a=0;$a<count($mrdetail);$a++)
            {
            $Flag =isset($mrdetail[$a]->Flag)?$mrdetail[$a]->Flag:'';
            if($Flag=="D")continue;
            if($Flag!="D")
            {
//
            $ItemID =isset($mrdetail[$a]->Item_ItemID)?$mrdetail[$a]->Item_ItemID:'';
            
 $ItemName =isset($mrdetail[$a]->Item_ItemName)?$mrdetail[$a]->Item_ItemName:'';
     
            $UnitPrice =(isset($mrdetail[$a]->Rate) && $mrdetail[$a]->Rate!="")?$mrdetail[$a]->Rate:0;
            $StockQty =isset($mrdetail[$a]->StockQty)?$mrdetail[$a]->StockQty:0;
             $Qty =isset($mrdetail[$a]->Qty)?$mrdetail[$a]->Qty:0;
            //}
    $Amount =(isset($mrdetail[$a]->Amount) && $mrdetail[$a]->Amount>0)?$mrdetail[$a]->Amount:($ReceivedQty*$UnitPrice);
            $Discount =(isset($mrdetail[$a]->Discount) && $mrdetail[$a]->Discount>=0)?(double)$mrdetail[$a]->Discount:0;
            $TaxPercent =(isset($mrdetail[$a]->VAT) && $mrdetail[$a]->VAT>0)?(double)$mrdetail[$a]->VAT:0;
            $NetAmount =(isset($mrdetail[$a]->NetAmount) && $mrdetail[$a]->NetAmount>0)?$mrdetail[$a]->NetAmount:0;
            $TaxAmount =(isset($mrdetail[$a]->VATAmount) && $mrdetail[$a]->VATAmount>0)?$mrdetail[$a]->VATAmount:0;
            $DiscountAmount =(isset($mrdetail[$a]->DiscountAmount) && $mrdetail[$a]->DiscountAmount>0)?$mrdetail[$a]->DiscountAmount:0;
            $Narration=(isset($mrdetail[$a]->Narration) && $mrdetail[$a]->Narration!="")?$mrdetail[$a]->Narration:'-';
            $UnitID=1;
            $TaxableAmount=($NetAmount-$TaxAmount);
            $TotalNetAmount=0;
            $InvoiceType=0;
              if($InvoiceType==8)
              {

                $Category =isset($mrdetail[$a]->MainCategory_ID)?$mrdetail[$a]->MainCategory_ID:0;
                $Level_ID =isset($mrdetail[$a]->Level_ID)?$mrdetail[$a]->Level_ID:0;
                $VATPercent= (isset($mrdetail[$a]->VAT) && $mrdetail[$a]->VAT>0)?(double)$mrdetail[$a]->VAT:0;
                $VATAmount =(isset($mrdetail[$a]->VATAmount) && $mrdetail[$a]->VATAmount>0)?$mrdetail[$a]->VATAmount:0;
              }
            else
            {
                 $VATPercent=$TaxPercent;
            $VATAmount=$TaxAmount;
            }
                    $CompanyID=getnamesingle("Inventory","CompanyID","ID",$ItemID,$connection);
                    
              $TransactionID=getmaxId("Inventory_Ingredients","InventoryIngredientID",$connection)+1;
                  $sql="insert into Inventory_Ingredients (
		   ItemID
		   ,MaterialID
           ,RetailPrice
           ,QTY
           ,RetailAmount
           ,NetAmount
           ,VatPercent
           ,VatAmount
           ,Narration
           ,TaxableAmount
           ,DiscountAmount
           ,DiscountPercent
           ,CompanyID
        ) values(".$PackageID."
           ,".$ItemID."
           ,".$UnitPrice."
           ,".$Qty."
           ,".$Amount."
           ,".$NetAmount."
           ,".$TaxPercent."
           ,".$TaxAmount."
           ,'".$Narration."'
           ,".$TaxableAmount."
			,".$DiscountAmount."
			,".$Discount."
      ,".$CompanyID.")";
          odbc_exec($connection, $sql) or die(odbc_error().$sql);
          }//end if flag
          }//end for loop
      }
//	$res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
	      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');

}
  	echo json_encode($res);
	}
}
?>