else
            //insert begins from here
        {
            $Vendor_Code     = vendorCode($conn);
            $act_code      = accountCode($conn);

            $check_truck_no = "SELECT * FROM `truck_mst` WHERE `truck_no` = '$truck_no'";
            $truck_no_result = mysqli_query($conn, $check_truck_no);
           if (mysqli_num_rows($truck_no_result) <= 0)
            {
                 $truck_code = turckCode($conn);
                $insert_truck_query   = mysqli_query($conn,"INSERT INTO `truck_mst` (`truck_no`,`truck_code`) VALUES ('$truck_no','$truck_code')");
            }
            // check consignor
            $check_consignor  = "SELECT * FROM `vendor_master` WHERE name = '".$consignor_name."'";
            $check_res        =  mysqli_query($conn, $check_consignor);
            if (mysqli_num_rows($check_res) <= 0){

                $consignor_query = mysqli_query($conn, "INSERT INTO `vendor_master`(`code`,`type`,`name`,`p_name`,`address`,`p_address`,`city`,`contact`,`pan`,`gstin`,`company_id`,`branch_id`,`created_id`,`created_datetime`,`updated_id`,`updated_datetime`)VALUES ('$Vendor_Code','V','$consignor_vendor_name','$consignor_vendor_name','$consignor_vendor_address','$consignor_vendor_address','$consignor_vendor_city','$consignor_vendor_contact','$consignor_vendor_pan','$consignor_vendor_gstin','$company_id','$created_id','$branch_id','$created_datetime','$updated_id','$updated_datetime')");
            }
            // check consignee
            $check_consignee  = "SELECT * FROM `vendor_master` WHERE name = '".$consignee_name."'";
            $consignee_res        =  mysqli_query($conn, $check_consignee);
            if (mysqli_num_rows($consignee_res) <= 0){

                $consignee_query = mysqli_query($conn, "INSERT INTO `vendor_master`(`code`,`type`,`name`,`p_
                    name`,`address`,`p_address`,`city`,`contact`,`pan`,`gstin`,`company_id`,`branch_id`,`created_id`,`created_datetime`,`updated_id`,`updated_datetime`) VALUES ('$Vendor_Code', 'V', '$consignee_name', '$consignee_name', '$consignee_vendor_address', '$consignee_vendor_address', '$consignee_vendor_city', '$consignee_vendor_contact', '$consignee_vendor_gstin', '$company_id', '$created_id', '$branch_id', '$created_datetime', '$updated_id', '$updated_datetime')");
             }
             // check account master
             $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 ('".$act_code."','".$_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) );

            // account master consignor
            $agent_consignor = "SELECT * FROM  `act_master` WHERE `ac_name` = '".$consignor_name."'";
            $act = mysqli_query($conn, $agent_consignor);
            if (mysqli_num_rows($act) <= 0){
                 $act_mstnew = "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 ('".$act_code."','".$consignor_name."','0.00','0.00','SUNDRY CREDITORS','NULL','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
                 
                 $act_qyu = mysqli_query($conn,$act_mstnew) or die( mysqli_error($conn) );

                  
            }
            //account master consignee
            $agent_consignee = "SELECT * FROM  `act_master` WHERE `ac_name` = '".$consignee_name."'";
            $act_data = mysqli_query($conn, $agent_consignee);
            if (mysqli_num_rows($act_data) <= 0){
                 $act_mstnewd = "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 ('".$act_code."','".$consignee_name."','0.00','0.00','SUNDRY CREDITORS','NULL','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";

                    $act_qyud = mysqli_query($conn,$act_mstnewd) or die( mysqli_error($conn) );
                
            }

         $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) 
         {
            $consignor_id     = "SELECT * FROM vendor_master WHERE name = '".$consignor_name."' ";
            $consignor_id_res = mysqli_query($conn,$consignor_id);
            $consignor_id_row = mysqli_fetch_assoc($consignor_id_res);

            $consignee_id    = "SELECT * FROM vendor_master WHERE name = '".$consignee_name."' ";
            $consignee_id_res = mysqli_query($conn,$consignee_id);
            $consignee_id_row = mysqli_fetch_assoc($consignee_id_res);

            $insert = "INSERT INTO `lr_mst`(`lr_type`, `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`, `emp_id`,
                                            `truck_driver_no`, `eway_bill_no`, `packing`,
                                            `charged_weight`, `eway_date`, `eway_ex_date`,
                                            `invoice_date`, `po_no`, `po_date`, `tracking_status`,
                                            `branch_id`, `company_id`, `address_delivery`,
                                            `lorry_type`, `created_id`, `created_datetime`,
                                            `updated_id`, `updated_datetime`) 
                                            VALUES ('".$lr_type."','".$lr_no."','".$l_date."',
                                            '".$party_invoice."','".$party_amount."','".$truck_no."'
                                            ,'".$from_place."','".$to_place."',
                                            '".$consignor_id_row['id']."',
                                            '".$consignee_id_row['id']."','".$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."',
                                            '".$emp_id."','".$truck_driver_no."','".$eway_bill_no."'
                                            ,'".$packing."','".$charged_weight."','".$eway_date."',
                                            '".$eway_ex_date."','".$invoice_date."','".$po_no."',
                                            '".$po_date."','".$tracking_status."','".$branch_id."',
                                            '".$company_id."','".$address_delivery."',
                                            '".$lorry_type."','".$created_id."',
                                            '".$created_datetime."','".$updated_id."',
                                            '".$updated_datetime."')";
                $result = mysqli_query($conn,$insert);
                $vendor_last_id = mysqli_insert_id($conn);
                
                    // Agent exists, insert multiple rows into lr_grid table
                    foreach ($_POST['product'] as $product)
                    {
                        $master_id   = $agent_rows['id'];
                        $description = $product['description'];
                        $pcs         = $product['pcs'];
                        $weight      = $product['weight'];

                         $insert_query = "INSERT INTO lr_grid (description, master_id, pcs, weight) 
                                                     VALUES ('$description','$vendor_last_id','$pcs',
                                                     '$weight')";
                                                     
                        $insert_result = mysqli_query($conn, $insert_query);
                    } 
            } 
                  $type               = 'lr_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/list.php') );die();
            }else{
              echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => BASE_URL . 'index.php?page=lr/add.php') );die();
          }  
      ?>