<?php 
session_start();
//include ('../common/library.php');  
include "../lib/db/connection.php";
include ('../common/helper.php'); 

   $item_id       = $_POST['item_id'];
   $supplier      = $_POST['supplier_id'];
   $purchase_date = $_POST['purchase_date'];
   $entry_by      = $_SESSION['user_id'];
   $po_no         =$_SESSION['purchase_no'];
   $warehouse     = $_SESSION['warehouse'];
   $category_id  = $_POST['category_id'];
   $model_no    = $_POST['model_no'];
	
   $supplier_name = find_a_field('supplier_info','supplier_name','id="'.$supplier.'"');
   
  
   if($_SESSION['purchase_no']==''){
   $master =$conn->query("insert into purchase_master(`purchase_date`,`supplier_id`,`supplier_name`,`entry_by`,`group_for`,`warehouse`,`category_id`,`model_no`) 
   Values('".$purchase_date."','".$supplier."','".$supplier_name."','".$entry_by."','".$_SESSION['group_for']."', '".$warehouse."','".$category_id."','".$model_no."')");
   $_SESSION['purchase_no'] = mysqli_insert_id($conn);
   }
   else {
     $update =$conn->query("update purchase_master p,journal_item ji,journal j
	  set p.purchase_date='".$purchase_date."',p.`supplier_id`=".$supplier.", p.supplier_name='".$supplier_name."', p.entry_by=".$entry_by.", 
     ji.`journal_date`='".$purchase_date."', ji.`tr_no`=".$po_no.",ji.`entry_by`".$entry_by.",ji.tr_from='Purchase', j.`jv_date`='".$purchase_date."', 
     j.`tr_no`=".$po_no.",j.`edit_by`".$entry_by.",j.tr_from='Purchase',p.category_id='".$category_id."',p.model_no='".$model_no."',p.warehouse='".$warehouse."'

    
     where p.purchase_no=".$po_no." and p.purchase_no=ji.tr_no and p.ledger_id=j.ledger_id and ji.tr_no=j.tr_no");
	 
	 
	 // $journal_item_update = $conn->query("update `journal_item` set `journal_date`='".$purchase_date."', `tr_no`=".$po_no.",`entry_by`".$entry_by.",tr_from="Purchase"");
//	  
//	   $journal_item_update = $conn->query("update `journal` set `jv_date`='".$purchase_date."', `tr_no`=".$po_no.",`edit_by`".$entry_by.",tr_from="Purchase"");
   }
   
  $details = $conn->query("INSERT INTO `purchase_order` (`purchase_no`,`item_id`,`purchase_date`,`group_for`,`supplier_id`,`warehouse`,`category_id`,`model_no`) 
  VALUES ('".$_SESSION['purchase_no']."','".$item_id."','".$purchase_date."','".$_SESSION['group_for']."','".$supplier."','".$warehouse."','".$category_id."','".$model_no."')"); 
  
  
	 // $journal_item_update = $conn->query("update `journal_item` set `journal_date`='".$purchase_date."', `tr_no`=".$po_no.",`entry_by`".$entry_by.",tr_from="Purchase"");
	  
	  // $journal_update = $conn->query("update `journal` set `jv_date`='".$purchase_date."', `tr_no`=".$po_no.",`edit_by`".$entry_by.",tr_from="Purchase"");
   
   //$count_sql = $conn->query("select count(purchase_no) as total_row from sales_order where purchase_no='".$_SESSION['purchase_no']."'");
   //$count_data = $count_sql->fetch_assoc();
   //$count = $count_data['total_row'];
   
  
  if ($details==true) {
		echo json_encode(array("statusCode"=>200,"row"=>$count));
	} //elseif($journal_item_update==true){
//	
//	echo json_encode(array("statusCode"=>200,"row"=>$count));
//	}
//	elseif($journal_update==true){
//	
//	echo json_encode(array("statusCode"=>200,"row"=>$count));
//	}
	else {
		echo json_encode(array("statusCode"=>201,"row"=>$count));
	}
  

?>

		
		