<?php 
include("../config.php");
if(!empty($_POST)){
	$product            = $_POST['product'];
	
	if($_POST["id"] != ''){
		$code  = mysqli_real_escape_string($conn, $_POST['code']);
	}else{
		$code  = mysqli_real_escape_string($conn, $_POST['code']);
        $checkpoint = mysqli_query($conn,"SELECT code FROM  general_purchase WHERE code = '".$code."'");
        if(mysqli_num_rows($checkpoint) > 0) {
            $code   = GeneralPurchase($conn);
        }

	}
	$bill_no 			= mysqli_real_escape_string($conn, $_POST['bill_no']);
	$vendor_id 			= mysqli_real_escape_string($conn, $_POST['vendor_id']);
	$bill_date 			= mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_POST['bill_date'])));
	$total_amnt 		= mysqli_real_escape_string($conn, $_POST['total_amnt']);
	$net_amt 			= mysqli_real_escape_string($conn, $_POST['net_amnt']);
	$gross_amt 			= mysqli_real_escape_string($conn, $_POST['gross_amnt']);
	$other_add          = mysqli_real_escape_string($conn, $_POST['other_add']);
	$other_less         = mysqli_real_escape_string($conn, $_POST['other_less']);
	$remarks         	= mysqli_real_escape_string($conn, $_POST['remarks']);

	if(!empty($_POST['sgst_per'])){
		$sgst_per  = mysqli_real_escape_string($conn, $_POST['sgst_per']);
	}else{
		$sgst_per  = '';
	}

	if(!empty($_POST['sgst_amount'])){
    	$sgst_amount = mysqli_real_escape_string($conn, $_POST['sgst_amount']);
	}else{
	    $sgst_amount = '';
	}

	if(!empty($_POST['cgst_per'])){
    	$cgst_per = mysqli_real_escape_string($conn, $_POST['cgst_per']);
	}else{
	    $cgst_per = '';
	}

	if(!empty($_POST['cgst_amount'])){
    	$cgst_amount = mysqli_real_escape_string($conn, $_POST['cgst_amount']);
	}else{
	    $cgst_amount = '';
	}

	if(!empty($_POST['igst_per'])){
    	$igst_per = mysqli_real_escape_string($conn, $_POST['igst_per']);
	}else{
	    $igst_per = '';
	}

	if(!empty($_POST['igst_amount'])){
    	$igst_amount = mysqli_real_escape_string($conn, $_POST['igst_amount']);
	}else{
	    $igst_amount = '';
	}

	$company_id         = $_SESSION['company_id'];
  	$branch_id          = $_SESSION['branch_id'];
	$created_id         = mysqli_real_escape_string($conn, $_SESSION['user_detail']['id']);
	$created_datetime   = mysqli_real_escape_string($conn, date('Y-m-d H:i:s'));
	$updated_id         = mysqli_real_escape_string($conn, $_SESSION['user_detail']['id']);
	$updated_datetime   = mysqli_real_escape_string($conn, date('Y-m-d H:i:s'));
	$master_accounts    = combine(getAllRecords('act_master'), 'id', 'act_code');

	if($_POST["id"] != ''){
		$type          = 'general_purchase_updated';
		$user_id       = $_SESSION['user_detail']['id'];
		$data          = serialize($_POST);

		// UPDATE
		$bill = mysqli_query($conn, "DELETE FROM `general_purchase` WHERE `id` = '".$_POST['id']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

		$bill_detail = mysqli_query($conn, "DELETE FROM `general_purchase_details` WHERE `gs_id` = '".$_POST['id']."'");

		//GENERAL PURCHASE TAX
 		$gst = mysqli_query($conn, "DELETE FROM `general_purchase_tax_history` WHERE `gs_id` = '".$_POST['id']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'");

		$gl = "DELETE FROM general_ledger  WHERE voucher_no = '".$bill_no."' AND `flag` = 'GENERAL PURCHASE' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $gl_res = mysqli_query($conn,$gl);
 		
 		$tra 			= "DELETE FROM trans  WHERE voucher_no = '".$bill_no."' AND `flag` = 'GENERAL PURCHASE' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $tra_res 		= mysqli_query($conn,$tra);

	    $trans_de 	    = "DELETE FROM trans_details  WHERE voucher_no = '".$bill_no."' AND `flag` = 'GENERAL PURCHASE' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
	    $result 		= mysqli_query($conn,$trans_de);

	    $vendor 	   = "SELECT * FROM `vendor_master` WHERE `id` = '".$vendor_id."'";
		$ven_res 	   = mysqli_query($conn,$vendor);
		$ven_row 	   = mysqli_fetch_array($ven_res);
		
		$act_msetr  	= "SELECT * FROM `act_master` WHERE `ac_name` = '".$ven_row['name']."'";
      	$act_ma_res 	= mysqli_query($conn,$act_msetr);
      	$act_ma_row 	= mysqli_fetch_array($act_ma_res);
      	$vendor_code 	= $act_ma_row['act_code'];
		
		$query = mysqli_query($conn, "INSERT INTO `general_purchase`(`code`, `bill_no`, `bill_date`, `vendor_id`, `sgst_per`, `sgst_amount`, `cgst_per`, `cgst_amount`, `igst_per`, `igst_amount`, `total_amnt`, `net_amt`, `gross_amt`, `remark`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$bill_no."','".$bill_date."','".$vendor_id."','".$sgst_per."','".$sgst_amount."','".$cgst_per."','".$cgst_amount."','".$igst_per."','".$igst_amount."','".$total_amnt."','".$net_amt."','".$gross_amt."','".$remarks."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
		$gs_id = mysqli_insert_id($conn);
		
		foreach ($product as $product) {

			if( isset($product['qty']) && !empty($product['qty']) ) {
				
				$insert = mysqli_query($conn, "INSERT INTO `general_purchase_details`(`gs_id`, `bill_no`, `lhc_no`, `date`, `from`, `to`, `qty`, `rate`, `amount`, `remark`) VALUES ('".$gs_id."','".$bill_no."','".$product['lhc_no']."','".risFormatDate($product['date'], 'Y-m-d')."','".$product['from']."','".$product['to']."','".$product['qty']."','".$product['rate']."','".$product['amount']."','".$product['remark']."')");
			}
		}
		
		// FRIGHT CHARGES
	    $ad          = "SELECT * FROM account_control WHERE descr = 'LHC'";
    	$ad_res      = mysqli_query($conn,$ad);
    	$ad_rows     = mysqli_fetch_assoc($ad_res);
    	$fright_code = @$master_accounts[$ad_rows['account_group']];

    	$lr = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','GENERAL PURCHASE','".$vendor_code."','".$fright_code."','".$total_amnt."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$lr_res = mysqli_query($conn,$lr);
    	
		// ADDITION
	    $adds = $_POST['add'];
	    if(count($adds) > 0) {
	      foreach ($adds as $add_key => $add) {
	        if($add['tax_mst_id'] > 0 && $add['amount'] > 0 ) {
	          $addition = "INSERT INTO `general_purchase_tax_history`(`gs_id`, `bill_no`, `type`, `key`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$gs_id."','".$bill_no."','add','". $add_key ."','".$add['tax_mst_id']."','".$add['amount']."','".$_SESSION['branch_id']."','".$_SESSION['company_id']."')";
	          $addition_result = mysqli_query($conn,$addition);

	          // GENERAL LEDGER
	          $get_tax       = "SELECT * FROM `tax_mst` WHERE `id` = '".$add['tax_mst_id']."'";
	          $get_tax_res   = mysqli_query($conn,$get_tax);
	          $get_tax_row   = mysqli_fetch_array($get_tax_res);
	          $ac_id         = $get_tax_row['account_group'];

	          $acc_name      = "SELECT * FROM `act_master` WHERE `id` = '".$ac_id."'";
	          $acc_name_res  = mysqli_query($conn,$acc_name);
	          $acc_name_row  = mysqli_fetch_array($acc_name_res);
	                
	          $add_two = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','GENERAL PURCHASE','".$vendor_code."','".$acc_name_row['act_code']."','".$add['amount']."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	          $add_one_res = mysqli_query($conn,$add_two);

			}
	      }
	    }

	    // SGST,CGST,IGST
	    if( isset($sgst_per) && !empty($sgst_per) && $sgst_per > 0 ) {	
			$master_accounts = combine(getAllRecords('act_master'), 'id', 'act_code');
			$account       = "SELECT * FROM account_control WHERE descr = 'SGST'";
			$account_res   = mysqli_query($conn,$account);
			$account_rows  = mysqli_fetch_assoc($account_res);
			$sgst_code     = @$master_accounts[$account_rows['account_group']];

			$sgst = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','SGST','".$vendor_code."','".$sgst_code."','".$sgst_amount."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	        $sgst_result = mysqli_query($conn,$sgst);
	    }

	    if( isset($cgst_per) && !empty($cgst_per) && $cgst_per > 0 ) {	
			$master_accounts       = combine(getAllRecords('act_master'), 'id', 'act_code');
			$account       = "SELECT * FROM account_control WHERE descr = 'CGST'";
			$account_res   = mysqli_query($conn,$account);
			$account_rows  = mysqli_fetch_assoc($account_res);
			$cgst_code     = @$master_accounts[$account_rows['account_group']];

			$cgst = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','CGST','".$vendor_code."','".$cgst_code."','".$cgst_amount."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	        $cgst_result = mysqli_query($conn,$cgst);
	    }

	    if( isset($igst_per) && !empty($igst_per) && $igst_per > 0 ) {	
			$master_accounts       = combine(getAllRecords('act_master'), 'id', 'act_code');
			$account       = "SELECT * FROM account_control WHERE descr = 'IGST'";
			$account_res   = mysqli_query($conn,$account);
			$account_rows  = mysqli_fetch_assoc($account_res);
			$igst_code     = @$master_accounts[$account_rows['account_group']];



			$igst = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','IGST','".$vendor_code."','".$igst_code."','".$igst_amount."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	        $igst_result = mysqli_query($conn,$igst);
	    }

	    // DEDUCTION
	    $subs = $_POST['sub'];
	    if(count($subs) > 0) {
	      foreach ($subs as $sub_key => $sub) {
	        if($sub['tax_mst_id'] > 0 && $sub['amount'] > 0 ) {
	          	$lhc_details = "INSERT INTO `general_purchase_tax_history`(`gs_id`, `bill_no`, `type`, `key`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$gs_id."','".$bill_no."','sub','". $sub_key ."','".$sub['tax_mst_id']."','".$sub['amount']."','".$_SESSION['branch_id']."','".$_SESSION['company_id']."')";
	          	$lhc_del_res = mysqli_query($conn,$lhc_details);	
	          	$tax     	 = "select * from tax_mst where id = '".$sub['tax_mst_id']."'";
	  			$tax_res 	 = mysqli_query($conn,$tax);
	          	$tax_row 	 = mysqli_fetch_array($tax_res);
	          	$sub_id  	 = $tax_row['account_group'];

	          	$act_tds     = "select * from act_master WHERE `id` = '".$sub_id."'";
            	$act_tds_res = mysqli_query($conn,$act_tds);
            	$act_tds_row = mysqli_fetch_array($act_tds_res);

            	$tds_am = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','GENERAL PURCHASE','".$act_tds_row['act_code']."','".$vendor_code."','".$sub['amount']."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
            	$tds_am_res = mysqli_query($conn,$tds_am);
	          
	        }
	      }
	    }

    	$trans = "INSERT INTO `trans`(`voucher_no`, `voucher_date`, `act_code`, `amount`, `adjust`, `balance`, `discount`, `tds`, `other_add`, `other_less`, `flag`, `mark`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','".$vendor_code."','".$net_amt."','','".$net_amt."','','','".$other_add."','".$other_less."','GENERAL PURCHASE','','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$trans_res = mysqli_query($conn,$trans);

    	//TRANS LAST ID
          
    	$last_id = mysqli_insert_id($conn);
    	$trans_det = "INSERT INTO `trans_details`(`voucher_no`, `trans_id`, `adjust`, `flag`, `balance`, `discount`, `tds`, `other_add`, `other_less`, `branch_id`, `company_id`) VALUES ('".$bill_no."','".$last_id."','','GENERAL PURCHASE','".$net_amt."','','','".$other_add."','".$other_less."','".$branch_id."','".$company_id."')";
    	$trans_det_res = mysqli_query($conn,$trans_det);

		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
		$acttivity_res = mysqli_query($conn,$activiti);

	}else{
		$type          = 'general_purchase_added';
		$user_id       = $_SESSION['user_detail']['id'];
		$data          = serialize($_POST);

		$vendor 	   = "SELECT * FROM `vendor_master` WHERE `id` = '".$vendor_id."'";
		$ven_res 	   = mysqli_query($conn,$vendor);
		$ven_row 	   = mysqli_fetch_array($ven_res);
		
		$act_msetr  	= "SELECT * FROM `act_master` WHERE `ac_name` = '".$ven_row['name']."'";
      	$act_ma_res 	= mysqli_query($conn,$act_msetr);
      	$act_ma_row 	= mysqli_fetch_array($act_ma_res);
      	$vendor_code 	= $act_ma_row['act_code'];
		
		$query = mysqli_query($conn, "INSERT INTO `general_purchase`(`code`, `bill_no`, `bill_date`, `vendor_id`, `sgst_per`, `sgst_amount`, `cgst_per`, `cgst_amount`, `igst_per`, `igst_amount`, `total_amnt`, `net_amt`, `gross_amt`, `remark`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$bill_no."','".$bill_date."','".$vendor_id."','".$sgst_per."','".$sgst_amount."','".$cgst_per."','".$cgst_amount."','".$igst_per."','".$igst_amount."','".$total_amnt."','".$net_amt."','".$gross_amt."','".$remarks."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
		$gs_id = mysqli_insert_id($conn);
		
		foreach ($product as $product) {

			if( isset($product['qty']) && !empty($product['qty']) ) {
				
				$insert = mysqli_query($conn, "INSERT INTO `general_purchase_details`(`gs_id`, `bill_no`, `lhc_no`, `date`, `from`, `to`, `qty`, `rate`, `amount`, `remark`) VALUES ('".$gs_id."','".$bill_no."','".$product['lhc_no']."','".risFormatDate($product['date'], 'Y-m-d')."','".$product['from']."','".$product['to']."','".$product['qty']."','".$product['rate']."','".$product['amount']."','".$product['remark']."')");
			}
		}
		
		// FRIGHT CHARGES
	    $ad          = "SELECT * FROM account_control WHERE descr = 'LHC'";
    	$ad_res      = mysqli_query($conn,$ad);
    	$ad_rows     = mysqli_fetch_assoc($ad_res);
    	$fright_code = @$master_accounts[$ad_rows['account_group']];

    	$lr = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','GENERAL PURCHASE','".$vendor_code."','".$fright_code."','".$total_amnt."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$lr_res = mysqli_query($conn,$lr);
    	
		// ADDITION
	    $adds = $_POST['add'];
	    if(count($adds) > 0) {
	      foreach ($adds as $add_key => $add) {
	        if($add['tax_mst_id'] > 0 && $add['amount'] > 0 ) {
	          $addition = "INSERT INTO `general_purchase_tax_history`(`gs_id`, `bill_no`, `type`, `key`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$gs_id."','".$bill_no."','add','". $add_key ."','".$add['tax_mst_id']."','".$add['amount']."','".$_SESSION['branch_id']."','".$_SESSION['company_id']."')";
	          $addition_result = mysqli_query($conn,$addition);

	          // GENERAL LEDGER
	          $get_tax       = "SELECT * FROM `tax_mst` WHERE `id` = '".$add['tax_mst_id']."'";
	          $get_tax_res   = mysqli_query($conn,$get_tax);
	          $get_tax_row   = mysqli_fetch_array($get_tax_res);
	          $ac_id         = $get_tax_row['account_group'];

	          $acc_name      = "SELECT * FROM `act_master` WHERE `id` = '".$ac_id."'";
	          $acc_name_res  = mysqli_query($conn,$acc_name);
	          $acc_name_row  = mysqli_fetch_array($acc_name_res);
	                
	          $add_two = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','GENERAL PURCHASE','".$vendor_code."','".$acc_name_row['act_code']."','".$add['amount']."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	          $add_one_res = mysqli_query($conn,$add_two);

			}
	      }
	    }

	    // SGST,CGST,IGST
	    if( isset($sgst_per) && !empty($sgst_per) && $sgst_per > 0 ) {	
			$master_accounts = combine(getAllRecords('act_master'), 'id', 'act_code');
			$account       = "SELECT * FROM account_control WHERE descr = 'SGST'";
			$account_res   = mysqli_query($conn,$account);
			$account_rows  = mysqli_fetch_assoc($account_res);
			$sgst_code     = @$master_accounts[$account_rows['account_group']];

			$sgst = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','SGST','".$vendor_code."','".$sgst_code."','".$sgst_amount."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	        $sgst_result = mysqli_query($conn,$sgst);
	    }

	    if( isset($cgst_per) && !empty($cgst_per) && $cgst_per > 0 ) {	
			$master_accounts       = combine(getAllRecords('act_master'), 'id', 'act_code');
			$account       = "SELECT * FROM account_control WHERE descr = 'CGST'";
			$account_res   = mysqli_query($conn,$account);
			$account_rows  = mysqli_fetch_assoc($account_res);
			$cgst_code     = @$master_accounts[$account_rows['account_group']];

			$cgst = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','CGST','".$vendor_code."','".$cgst_code."','".$cgst_amount."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	        $cgst_result = mysqli_query($conn,$cgst);
	    }

	    if( isset($igst_per) && !empty($igst_per) && $igst_per > 0 ) {	
			$master_accounts       = combine(getAllRecords('act_master'), 'id', 'act_code');
			$account       = "SELECT * FROM account_control WHERE descr = 'IGST'";
			$account_res   = mysqli_query($conn,$account);
			$account_rows  = mysqli_fetch_assoc($account_res);
			$igst_code     = @$master_accounts[$account_rows['account_group']];

			$igst = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','IGST','".$vendor_code."','".$igst_code."','".$igst_amount."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	        $igst_result = mysqli_query($conn,$igst);
	    }

	    // DEDUCTION
	    $subs = $_POST['sub'];
	    if(count($subs) > 0) {
	      foreach ($subs as $sub_key => $sub) {
	        if($sub['tax_mst_id'] > 0 && $sub['amount'] > 0 ) {
	          	$lhc_details = "INSERT INTO `general_purchase_tax_history`(`gs_id`, `bill_no`, `type`, `key`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$gs_id."','".$bill_no."','sub','". $sub_key ."','".$sub['tax_mst_id']."','".$sub['amount']."','".$_SESSION['branch_id']."','".$_SESSION['company_id']."')";
	          	$lhc_del_res = mysqli_query($conn,$lhc_details);	
	          	$tax     	 = "select * from tax_mst where id = '".$sub['tax_mst_id']."'";
	  			$tax_res 	 = mysqli_query($conn,$tax);
	          	$tax_row 	 = mysqli_fetch_array($tax_res);
	          	$sub_id  	 = $tax_row['account_group'];

	          	$act_tds     = "select * from act_master WHERE `id` = '".$sub_id."'";
            	$act_tds_res = mysqli_query($conn,$act_tds);
            	$act_tds_row = mysqli_fetch_array($act_tds_res);

            	$tds_am = "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','GENERAL PURCHASE','".$act_tds_row['act_code']."','".$vendor_code."','".$sub['amount']."','','','','".$net_amt."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
            	$tds_am_res = mysqli_query($conn,$tds_am);
	          
	        }
	      }
	    }

    	$trans = "INSERT INTO `trans`(`voucher_no`, `voucher_date`, `act_code`, `amount`, `adjust`, `balance`, `discount`, `tds`, `other_add`, `other_less`, `flag`, `mark`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$bill_no."','".$bill_date."','".$vendor_code."','".$net_amt."','','".$net_amt."','','','".$other_add."','".$other_less."','GENERAL PURCHASE','','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$trans_res = mysqli_query($conn,$trans);

    	//TRANS LAST ID
          
    	$last_id = mysqli_insert_id($conn);
    	$trans_det = "INSERT INTO `trans_details`(`voucher_no`, `trans_id`, `adjust`, `flag`, `balance`, `discount`, `tds`, `other_add`, `other_less`, `branch_id`, `company_id`) VALUES ('".$bill_no."','".$last_id."','','GENERAL PURCHASE','".$net_amt."','','','".$other_add."','".$other_less."','".$branch_id."','".$company_id."')";
    	$trans_det_res = mysqli_query($conn,$trans_det);

		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
		$acttivity_res = mysqli_query($conn,$activiti);	
	}
	
	echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY', 'alert' => 1, 'redirect' => 'index.php?page=general_purchase/list.php') );die();
}else{
	echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=general_purchase/add.php') );die();
}  
?>