<?php 
include("../config.php");
if(!empty($_POST)){

    if($_POST["id"] != ''){
        $truck_code = mysqli_real_escape_string($conn, $_POST['truck_code']);
    }else{
        $truck_code = mysqli_real_escape_string($conn, $_POST['truck_code']);
        $checkpoint = mysqli_query($conn,"SELECT id FROM  truck_mst WHERE truck_code = '".$truck_code."'");
        if(mysqli_num_rows($checkpoint) > 0) {
            $truck_code = turckCode($conn);
        }
    }

	$g_act_codes         = accountCode($conn);
	$act_no         	 = $g_act_codes;
	$own               = mysqli_real_escape_string($conn, $_POST['own']);
    $truck_no          = mysqli_real_escape_string($conn, $_POST['truck_no']);
    $o_name            = mysqli_real_escape_string($conn, $_POST['o_name']);
    $o_address         = mysqli_real_escape_string($conn, $_POST['o_address']);
    $o_city            = mysqli_real_escape_string($conn, $_POST['o_city']);
    $o_contact         = mysqli_real_escape_string($conn, $_POST['o_contact']);
    $o_pan             = mysqli_real_escape_string($conn, $_POST['o_pan']);
    $t_name            = mysqli_real_escape_string($conn, $_POST['t_name']);
    $t_address         = mysqli_real_escape_string($conn, $_POST['t_address']);
    $t_city            = mysqli_real_escape_string($conn, $_POST['t_city']);
    $t_contact         = mysqli_real_escape_string($conn, $_POST['t_contact']);
    $t_pan             = mysqli_real_escape_string($conn, $_POST['t_pan']);
    $d_name            = mysqli_real_escape_string($conn, $_POST['d_name']);
    $d_address         = mysqli_real_escape_string($conn, $_POST['d_address']);
    $d_city            = mysqli_real_escape_string($conn, $_POST['d_city']);
    $d_contact         = mysqli_real_escape_string($conn, $_POST['d_contact']);
    $lic_no            = mysqli_real_escape_string($conn, $_POST['lic_no']);
    $rto               = mysqli_real_escape_string($conn, $_POST['rto']);
    $tr_name           = mysqli_real_escape_string($conn, $_POST['tr_name']);
    $tr_address        = mysqli_real_escape_string($conn, $_POST['tr_address']);
    $tr_city           = mysqli_real_escape_string($conn, $_POST['tr_city']);
    $tr_contact        = mysqli_real_escape_string($conn, $_POST['tr_contact']);
    $reference         = mysqli_real_escape_string($conn, $_POST['reference']);
    $remark            = mysqli_real_escape_string($conn, $_POST['remark']);
    $lorry_type        = mysqli_real_escape_string($conn, $_POST['lorry_type']);
    $chasis_no         = mysqli_real_escape_string($conn, $_POST['chasis_no']);
    $eng_no            = mysqli_real_escape_string($conn, $_POST['eng_no']);
    $per_no            = mysqli_real_escape_string($conn, $_POST['per_no']);
    $from              = date('Y-m-d', strtotime($_POST['from']));
    $up_to             = date('Y-m-d', strtotime($_POST['up_to']));
    $state_per_no            = mysqli_real_escape_string($conn, $_POST['state_per_no']);
    $state_from        = date('Y-m-d', strtotime($_POST['state_from']));
    $state_up_to       = date('Y-m-d', strtotime($_POST['state_up_to']));
    $insurance         = mysqli_real_escape_string($conn, $_POST['insurance']);
    $branch            = mysqli_real_escape_string($conn, $_POST['branch']);
    $policy            = mysqli_real_escape_string($conn, $_POST['policy']);
    $height            = mysqli_real_escape_string($conn, $_POST['height']);
    $weight            = mysqli_real_escape_string($conn, $_POST['weight']);
    $up_too            = date('Y-m-d', strtotime($_POST['up_too']));
    $puc_no            = mysqli_real_escape_string($conn, $_POST['puc_no']);
    $fitness            = mysqli_real_escape_string($conn, $_POST['fitness']);
    $tax_no            = mysqli_real_escape_string($conn, $_POST['tax_no']);

    $puc_up_to         = date('Y-m-d', strtotime($_POST['puc_up_to']));
    $fitness_up_to     = date('Y-m-d', strtotime($_POST['fitness_up_to']));
    $tax_up_to         = date('Y-m-d', strtotime($_POST['tax_up_to']));
    $rc_no             = mysqli_real_escape_string($conn, $_POST['rc_no']);
    $pan_no            = mysqli_real_escape_string($conn, $_POST['pan_no']);
    $insurance         = mysqli_real_escape_string($conn, $_POST['insurance']);
    $account_group     = 'SUNDRY CREDITORS';
	$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['old_rc_img'] != ''){
        if($_FILES["rc_img"]["name"] != ''){
            $rc_front_image = '';
            if($_FILES["rc_img"]["name"] != ''){
                $rc_front_file_extension = pathinfo($_FILES["rc_img"]["name"], PATHINFO_EXTENSION);
                if (!in_array($rc_front_file_extension, $config['attachment']['valid_types'])) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                            die();
                }else if ($config['attachment']['file_size'] < $_FILES["rc_img"]["size"]) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                        die();
                }
                $rc_img             = rand(1,99999).'.'.basename($_FILES['rc_img']['name']);
                $rc_front_upload_file_path= $config['attachment']['rc_folder_path'] . $rc_img;
                @move_uploaded_file($_FILES["rc_img"]["tmp_name"], $rc_front_upload_file_path);
                $rc_front_image = $rc_img;
            }
        }else{
            $rc_img  = $_POST['old_rc_img'];    
        }
    }else{
        $rc_front_image = '';
        if($_FILES["rc_img"]["name"] != ''){
            $rc_front_file_extension = pathinfo($_FILES["rc_img"]["name"], PATHINFO_EXTENSION);
            if (!in_array($rc_front_file_extension, $config['attachment']['valid_types'])) {
                echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                        die();
            }else if ($config['attachment']['file_size'] < $_FILES["rc_img"]["size"]) {
                echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                    die();
            }
            $rc_img             = rand(1,99999).'.'.basename($_FILES['rc_img']['name']);
            $rc_front_upload_file_path= $config['attachment']['rc_folder_path'] . $rc_img;
            @move_uploaded_file($_FILES["rc_img"]["tmp_name"], $rc_front_upload_file_path);
            $rc_front_image = $rc_img;
        }
    }

    /*RC BACK*/
    if($_POST['rc_back_img'] != ''){
        if($_FILES["rc_img_back"]["name"] != ''){
            $rc_back_image = '';
            if($_FILES["rc_img_back"]["name"] != ''){
                $rc_back_file_extension = pathinfo($_FILES["rc_img_back"]["name"], PATHINFO_EXTENSION);
                if (!in_array($rc_back_file_extension, $config['attachment']['valid_types'])) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                            die();
                }else if ($config['attachment']['file_size'] < $_FILES["rc_img_back"]["size"]) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                        die();
                }
                $rc_img_back             = rand(1,99999).'.'.basename($_FILES['rc_img_back']['name']);
                $rc_front_upload_file_path= $config['attachment']['rc_folder_path'] . $rc_img_back;
                @move_uploaded_file($_FILES["rc_img_back"]["tmp_name"], $rc_front_upload_file_path);
                $rc_front_image = $rc_img_back;
            }
        }else{
            $rc_img_back  = $_POST['rc_back_img'];    
        }
    }else{
        $rc_back_image = '';
        if($_FILES["rc_img_back"]["name"] != ''){
            $rc_back_file_extension = pathinfo($_FILES["rc_img_back"]["name"], PATHINFO_EXTENSION);
            if (!in_array($rc_back_file_extension, $config['attachment']['valid_types'])) {
                echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                        die();
            }else if ($config['attachment']['file_size'] < $_FILES["rc_img_back"]["size"]) {
                echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                    die();
            }
            $rc_img_back             = rand(1,99999).'.'.basename($_FILES['rc_img_back']['name']);
            $rc_front_upload_file_path= $config['attachment']['rc_folder_path'] . $rc_img_back;
            @move_uploaded_file($_FILES["rc_img_back"]["tmp_name"], $rc_front_upload_file_path);
            $rc_front_image = $rc_img_back;
        }
    }

    /*PANCARD*/
    if($_POST['old_pan_img'] != ''){
        if($_FILES["pan_img"]["name"] != ''){
            $pan_image = '';
            if($_FILES["pan_img"]["name"] != ''){
                $pan_file_extension = pathinfo($_FILES["pan_img"]["name"], PATHINFO_EXTENSION);
                if (!in_array($pan_file_extension, $config['attachment']['valid_types'])) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                            die();
                }else if ($config['attachment']['file_size'] < $_FILES["pan_img"]["size"]) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                        die();
                }
                $pan_img             = rand(1,99999).'.'.basename($_FILES['pan_img']['name']);
                $pan_upload_file_path= $config['attachment']['folder_path'] . $pan_img;
                @move_uploaded_file($_FILES["pan_img"]["tmp_name"], $pan_upload_file_path);
                $pan_image = $pan_img;
            }
        }else{
            $pan_img  = $_POST['old_pan_img'];    
        }
    }else{
        $pan_image = '';
        if($_FILES["pan_img"]["name"] != ''){
            $pan_file_extension = pathinfo($_FILES["pan_img"]["name"], PATHINFO_EXTENSION);
            if (!in_array($pan_file_extension, $config['attachment']['valid_types'])) {
                echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                        die();
            }else if ($config['attachment']['file_size'] < $_FILES["pan_img"]["size"]) {
                echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                    die();
            }
            $pan_img             = rand(1,99999).'.'.basename($_FILES['pan_img']['name']);
            $pan_upload_file_path= $config['attachment']['folder_path'] . $pan_img;
            @move_uploaded_file($_FILES["pan_img"]["tmp_name"], $pan_upload_file_path);
            $pan_image = $pan_img;
        }
    }

    /*DECLARATION*/
    if($_POST['dec_imgs'] != ''){
        if($_FILES["dec_img"]["name"] != ''){
            $dec_image = '';
            if($_FILES["dec_img"]["name"] != ''){
                $dec_file_extension = pathinfo($_FILES["dec_img"]["name"], PATHINFO_EXTENSION);
                if (!in_array($dec_file_extension, $config['attachment']['valid_types'])) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                            die();
                }else if ($config['attachment']['file_size'] < $_FILES["dec_img"]["size"]) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                        die();
                }
                $dec_img             = rand(1,99999).'.'.basename($_FILES['dec_img']['name']);
                $dec_upload_file_path= $config['attachment']['dec_folder_path'] . $dec_img;
                @move_uploaded_file($_FILES["dec_img"]["tmp_name"], $dec_upload_file_path);
                $dec_image = $dec_img;
            }
        }else{
            $dec_img = mysqli_real_escape_string($conn, $_POST['dec_imgs']);    
        }
    }else{
        $dec_image = '';
        if($_FILES["dec_img"]["name"] != ''){
            $dec_file_extension = pathinfo($_FILES["dec_img"]["name"], PATHINFO_EXTENSION);
            if (!in_array($dec_file_extension, $config['attachment']['valid_types'])) {
                echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                        die();
            }else if ($config['attachment']['file_size'] < $_FILES["dec_img"]["size"]) {
                echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                    die();
            }
            $dec_img             = rand(1,99999).'.'.basename($_FILES['dec_img']['name']);
            $dec_upload_file_path= $config['attachment']['dec_folder_path'] . $dec_img;
            @move_uploaded_file($_FILES["dec_img"]["tmp_name"], $dec_upload_file_path);
            $dec_image = $dec_img;
        }
    }
    
    if($_POST["id"] != ''){
  	 	$type      = 'truck_update';
		$user_id   = $_SESSION['user_detail']['id'];
		$data      = serialize($_POST);
  	 	$insert    = "UPDATE truck_mst SET truck_code='$truck_code',truck_no='$truck_no',o_name='$o_name',o_address='$o_address',o_city='$o_city',o_contact='$o_contact',o_pan='$o_pan',t_name='$t_name',t_address='$t_address',t_city='$t_city',t_contact='$t_contact',t_pan='$t_pan',d_name='$d_name',d_address='$d_address',d_city='$d_city',d_contact='$d_contact',lic_no='$lic_no',rto='$rto',tr_name='$tr_name',tr_address='$tr_address',tr_city='$tr_city',tr_contact='$tr_contact',reference='$reference',remark='$remark',lorry_type='$lorry_type',chasis_no='$chasis_no',eng_no='$eng_no',per_no='$per_no',`from`='$from',up_to='$up_to',insurance='$insurance',branch='$branch',policy='$policy',height='$height',weight='$weight',up_too='$up_too',rc_no='$rc_no',rc_img='$rc_img',rc_img_back='$rc_img_back',pan_no='$pan_no',pan_img='$pan_img',dec_img='$dec_img',branch_id='$branch_id',company_id='$company_id',updated_id='$updated_id',updated_datetime='$updated_datetime',own='$own',state_per_no='$state_per_no',state_from='$state_from',state_up_to='$state_up_to',puc_no='$puc_no',puc_up_to='$puc_up_to',fitness='$fitness',fitness_up_to='$fitness_up_to',tax_no='$tax_no',tax_up_to='$tax_up_to' WHERE id='".$_POST['id']."'";
        
  	 	$result = mysqli_query($conn,$insert) or die( mysqli_error($conn) );
        $check_act = "SELECT * FROM `act_master` WHERE `ac_name` = '".$_POST['o_name']."'";
        $check_res = mysqli_query($conn,$check_act);
        $check_num = mysqli_num_rows($check_res);

        if($check_num == 0 ){
            $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_no."','".$o_name."','0.00','0.00','".$account_group."','','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";

            $act_res = mysqli_query($conn,$act_mst) or die( mysqli_error($conn) );    
        }

        $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{
    	$new_name = '';
    	$type               = 'truck_add';
    	$user_id            = $_SESSION['user_detail']['id'];
    	$data               = serialize($_POST);
    	$insert = "INSERT INTO `truck_mst`(`truck_code`, `truck_no`, `o_name`, `o_address`, `o_city`,      `o_contact`, `o_pan`, `t_name`, `t_address`, `t_city`, `t_contact`, `t_pan`,
                  `d_name`, `d_address`, `d_city`, `d_contact`, `lic_no`, `rto`, `tr_name`,
                  `tr_address`, `tr_city`, `tr_contact`, `reference`, `remark`, `lorry_type`, 
                  `chasis_no`, `eng_no`, `per_no`, `from`, `up_to`, `insurance`, `branch`, `policy`,
                  `height`, `weight`, `up_too`, `rc_no`, `rc_img`, `rc_img_back`, `pan_no`, 
                  `pan_img`, `dec_img`, `branch_id`, `company_id`, `created_id`, `created_datetime`,
                  `updated_id`, `updated_datetime`,`own`,`state_per_no`,`state_from`,`state_up_to`,`puc_no`,`puc_up_to`,`fitness`,`fitness_up_to`,`tax_no`,`tax_up_to`) VALUES ('".$truck_code."','".$truck_no."',
                  '".$o_name."','".$o_address."','".$o_city."','".$o_contact."','".$o_pan."',
                  '".$t_name."','".$t_address."','".$t_city."','".$t_contact."','".$t_pan."',
                  '".$d_name."','".$d_address."','".$d_city."','".$d_contact."','".$lic_no."',
                  '".$rto."','".$tr_name."','".$tr_address."','".$tr_city."','".$tr_contact."',
                  '".$reference."','".$remark."','".$lorry_type."','".$chasis_no."','".$eng_no."',
                  '".$per_no."','".$from."','".$up_to."','".$insurance."','".$branch."','".$policy."'
                  ,'".$height."','".$weight."','".$up_too."','".$rc_no."','".$rc_img."',
                  '".$rc_img_back."','".$pan_no."','".$pan_img."','".$dec_img."','".$branch_id."',
                  '".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."',
                  '".$updated_datetime."','".$own."','".$state_per_no."','".$state_from."','".$state_up_to."','".$puc_no."','".$puc_up_to."','".$fitness."','".$fitness_up_to."','".$tax_no."','".$tax_up_to."')";

	    $result = mysqli_query($conn,$insert);
        $check_act = "SELECT * FROM `act_master` WHERE `ac_name` = '".$_POST['o_name']."'";
        $check_res = mysqli_query($conn,$check_act);
        $check_num = mysqli_num_rows($check_res);
        if($check_num == 0 ){
            $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_no."',
                       '".$o_name."','0.00','0.00','".$account_group."','','".$branch_id."',
                       '".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."',
                       '".$updated_datetime."')";

            $act_res = mysqli_query($conn,$act_mst) or die( mysqli_error($conn) );    
        }
	

    	$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);
  }
  if($_POST['page_name'] == 'lr'){
    echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=lr/add.php') );die();
  }else{
    echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=truck_mst/list.php') );die();
  }
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=truck_mst/add.php') );die();
} 