<?php 
include("../config.php");
if(!empty($_POST)){
    
    if($_POST['id'] == ''){
        $lr_no               = mysqli_real_escape_string($conn, 'O'.$_POST['lr_no']);    
    }else{
        $lr_no               = mysqli_real_escape_string($conn, $_POST['lr_no']);
    }
    
    $l_date              = mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_POST['l_date'])));
    $party_invoice       = mysqli_real_escape_string($conn, $_POST['party_invoice']);
    $party_amount        = mysqli_real_escape_string($conn, $_POST['party_amount']);
    $truck_no            = mysqli_real_escape_string($conn, $_POST['truck_no']);
    $from_place          = mysqli_real_escape_string($conn, $_POST['from_place']);
    $to_place            = mysqli_real_escape_string($conn, $_POST['to_place']);
    $consignor_name      = mysqli_real_escape_string($conn, $_POST['consignor_name']);
    $consignee_name      = mysqli_real_escape_string($conn, $_POST['consignee_name']);
    $billing_to          = mysqli_real_escape_string($conn, $_POST['billing_to']);
    $agent               = mysqli_real_escape_string($conn, $_POST['agent']);
    $nature_of_goods     = mysqli_real_escape_string($conn, $_POST['nature_of_goods']);
    $weight              = mysqli_real_escape_string($conn, $_POST['weight']);
    $actual_weight       = mysqli_real_escape_string($conn, $_POST['actual_weight']);
    $height              = mysqli_real_escape_string($conn, $_POST['height']);
    $extra_height        = mysqli_real_escape_string($conn, $_POST['extra_height']);
    $pcs                 = mysqli_real_escape_string($conn, $_POST['pcs']);
    $rate                = mysqli_real_escape_string($conn, $_POST['rate']);
    $vehicle_in          = mysqli_real_escape_string($conn, $_POST['vehicle_in']);
    $gst_paid_by         = mysqli_real_escape_string($conn, $_POST['gst_paid_by']);
    $vehicle_out         = mysqli_real_escape_string($conn, $_POST['vehicle_out']);
    $billing_party       = mysqli_real_escape_string($conn, $_POST['billing_party']);
    $remark              = mysqli_real_escape_string($conn, $_POST['remark']);
    $rate_on             = mysqli_real_escape_string($conn, $_POST['rate_on']);
    $freight             = mysqli_real_escape_string($conn, $_POST['freight']);
    $advance             = mysqli_real_escape_string($conn, $_POST['advance']);
    $total_amnt          = mysqli_real_escape_string($conn, $_POST['total_amnt']);
    $company_id          = $_SESSION['company_id'];
    $branch_id           = $_SESSION['branch_id'];
    $created_datetime    = date('Y-m-d H:i:s');
    $created_id          = $_SESSION['user_detail']['id'];
    $updated_datetime    = date('Y-m-d H:i:s');
    $updated_id          = $_SESSION['user_detail']['id'];

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

    // update
    $insert = "UPDATE lr_opening SET lr_no='$lr_no',l_date='$l_date',party_invoice='$party_invoice',party_amount='$party_amount',truck_no='$truck_no',from_place='$from_place',to_place='$to_place',consignor_name='$consignor_name',consignee_name='$consignee_name',billing_to='$billing_to',agent='$agent',nature_of_goods='$nature_of_goods',weight='$weight',actual_weight='$actual_weight',height='$height',extra_height='$extra_height',pcs='$pcs',rate='$rate',vehicle_in='$vehicle_in',gst_paid_by='$gst_paid_by',vehicle_out='$vehicle_out',billing_party='$billing_party',remark='$remark',rate_on='$rate_on',freight='$freight',advance='$advance',total_amnt='$total_amnt',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE lr_no='".$_POST["id"]."'";
    $result = mysqli_query($conn,$insert);
    
    // activity
    $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{
    $agents = "SELECT * FROM `act_master` WHERE `ac_name` = '".$agent."'";
    $agent_res = mysqli_query($conn,$agents) or die( mysqli_error($conn) );
    if (mysqli_num_rows($agent_res) != 0) {
        $insert = "INSERT INTO `lr_opening`(`lr_no`, `l_date`, `party_invoice`, `party_amount`, `truck_no`, `from_place`, `to_place`, `consignor_name`, `consignee_name`, `billing_to`, `agent`, `nature_of_goods`, `weight`, `actual_weight`, `height`, `extra_height`,`pcs`, `rate`, `vehicle_in`, `gst_paid_by`, `vehicle_out`, `billing_party`, `remark`, `rate_on`, `freight`, `advance`, `total_amnt`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$lr_no."','".$l_date."','".$party_invoice."','".$party_amount."','".$truck_no."','".$from_place."','".$to_place."','".$consignor_name."','".$consignee_name."','".$billing_to."','".$agent."','".$nature_of_goods."','".$weight."','".$actual_weight."','".$height."','".$extra_height."','".$pcs."','".$rate."','".$vehicle_in."','".$gst_paid_by."','".$vehicle_out."','".$billing_party."','".$remark."','".$rate_on."','".$freight."','".$advance."','".$total_amnt."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
        $result = mysqli_query($conn,$insert);
    }else{
      $voucher_no          = accountCode($conn);
      $insert = "INSERT INTO `lr_opening`(`lr_no`, `l_date`, `party_invoice`, `party_amount`, `truck_no`, `from_place`, `to_place`, `consignor_name`, `consignee_name`, `billing_to`, `agent`, `nature_of_goods`, `weight`, `actual_weight`, `height`, `extra_height`,`pcs`, `rate`, `vehicle_in`, `gst_paid_by`, `vehicle_out`, `billing_party`, `remark`, `rate_on`, `freight`, `advance`, `total_amnt`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$lr_no."','".$l_date."','".$party_invoice."','".$party_amount."','".$truck_no."','".$from_place."','".$to_place."','".$consignor_name."','".$consignee_name."','".$billing_to."','".$agent."','".$nature_of_goods."','".$weight."','".$actual_weight."','".$height."','".$extra_height."','".$pcs."','".$rate."','".$vehicle_in."','".$gst_paid_by."','".$vehicle_out."','".$billing_party."','".$remark."','".$rate_on."','".$freight."','".$advance."','".$total_amnt."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
      $result = mysqli_query($conn,$insert);
      // ACCOUNT MASTRE INSERT
      $act_mst = "INSERT INTO `act_master`(`act_code`, `ac_name`, `dr_open_bal`, `cr_open_bal`, `account_group`, `party_code`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$voucher_no."','".$_POST['agent']."','0.00','0.00','SUNDRY CREDITORS','NULL','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
      $act_res = mysqli_query($conn,$act_mst) or die( mysqli_error($conn) );
    }

    $type               = 'lr_opening_add';
    $user_id            = $_SESSION['user_detail']['id'];
    $data               = serialize($_POST);
    $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=lr_opeing/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => BASE_URL . 'index.php?page=lr_opeing/add.php') );die();
}  
?>