<?php 
include("../config.php");
if(!empty($_POST)){
	$g_act_codes         = accountCode($conn);
	$act_code         	 = $g_act_codes;
	
	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  vendor_master WHERE code = '".$code."'");
        if(mysqli_num_rows($checkpoint) > 0) {
            $code   = vendorCode($conn);
        }
    }

	$name          		 = mysqli_real_escape_string($conn, $_POST['name']);
	$p_name          	 = mysqli_real_escape_string($conn, $_POST['p_name']);
	$address       		 = mysqli_real_escape_string($conn, $_POST['address']);
	$p_address           = mysqli_real_escape_string($conn, $_POST['p_address']);
	$country       		 = mysqli_real_escape_string($conn, $_POST['country']);
	$state         		 = mysqli_real_escape_string($conn, $_POST['state']);
	$district      		 = mysqli_real_escape_string($conn, $_POST['district']);
	$city          		 = mysqli_real_escape_string($conn, $_POST['city']);
	$pincode       		 = mysqli_real_escape_string($conn, $_POST['pincode']);
	$contact       		 = mysqli_real_escape_string($conn, $_POST['contact']);
	$mobile        		 = mysqli_real_escape_string($conn, $_POST['mobile']);
	$email         		 = mysqli_real_escape_string($conn, $_POST['email']);
	$pan           		 = mysqli_real_escape_string($conn, $_POST['pan']);
	$gstin          	 = mysqli_real_escape_string($conn, $_POST['gstin']);
	$group_id          	 = mysqli_real_escape_string($conn, $_POST['group_id']);
	$status          	 = mysqli_real_escape_string($conn, $_POST['status']);
	$contact_person      = mysqli_real_escape_string($conn, $_POST['contact_person']);
	$designation         = mysqli_real_escape_string($conn, $_POST['designation']);
	$con_per_mobile      = mysqli_real_escape_string($conn, $_POST['con_per_mobile']);
	$con_per_email       = mysqli_real_escape_string($conn, $_POST['con_per_email']);
	$contact_person1     = mysqli_real_escape_string($conn, $_POST['contact_person1']);
	$designation1        = mysqli_real_escape_string($conn, $_POST['designation1']);
	$con_per_mobile1     = mysqli_real_escape_string($conn, $_POST['con_per_mobile1']);
	$con_per_email1      = mysqli_real_escape_string($conn, $_POST['con_per_email1']);
	$contact_person2     = mysqli_real_escape_string($conn, $_POST['contact_person2']);
	$designation2        = mysqli_real_escape_string($conn, $_POST['designation2']);
	$con_per_mobile2     = mysqli_real_escape_string($conn, $_POST['con_per_mobile2']);
	$con_per_email2      = mysqli_real_escape_string($conn, $_POST['con_per_email2']);
	$proprietor      	 = mysqli_real_escape_string($conn, $_POST['proprietor']);
	$file_img       	 = mysqli_real_escape_string($conn, strtoupper($_POST['file_img']));
	
	if($_POST['type'] == 'A'){
		$account_group = 'AGENT';
	}else{
		$account_group = 'VENDOR';
	}
	$v_type= mysqli_real_escape_string($conn, $_POST['type']);

	$account_group       = 'SUNDRY DEBTORS';
	$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'];

	$attachment = NULL;
	if(NULL != $_FILES) {
		$attachment = $_FILES;
	}

	if($_POST['old_image'] != ''){
		
        if($_FILES["image"]["name"] != ''){
            $rc_front_image = '';
            if($_FILES["image"]["name"] != ''){
                $rc_front_file_extension = pathinfo($_FILES["image"]["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["image"]["size"]) {
                    echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                        die();
                }
                $image             = rand(1,99999).'.'.basename($_FILES['image']['name']);
                $rc_front_upload_file_path= $config['attachment']['vendor_folder_path'] . $image;
                @move_uploaded_file($_FILES["image"]["tmp_name"], $rc_front_upload_file_path);
                $rc_front_image = $image;
            }
        }else{
            $image  = $_POST['old_image'];    
        }
    }else{
        $rc_front_image = '';
        if($_FILES["image"]["name"] != ''){
            $rc_front_file_extension = pathinfo($_FILES["image"]["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["image"]["size"]) {
                echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
                    die();
            }
            $image             = rand(1,99999).'.'.basename($_FILES['image']['name']);
            $rc_front_upload_file_path= $config['attachment']['vendor_folder_path'] . $image;
            @move_uploaded_file($_FILES["image"]["tmp_name"], $rc_front_upload_file_path);
            $rc_front_image = $image;
        }
    }

	if($_POST["id"] != ''){
  	 	$type               = 'vendor_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
  	 	$insert = "UPDATE vendor_master SET code='$code',type='$v_type',name='$name',p_name='$p_name',address='$address',p_address='$p_address',country='$country',state='$state',district='$district',city='$city',pincode='$pincode',contact='$contact',mobile='$mobile',email='$email',pan='$pan',gstin='$gstin',group_id='$group_id',`file_img`='$image',proprietor='$proprietor',contact_person='$contact_person',designation='$designation',con_per_mobile='$con_per_mobile',con_per_email='$con_per_email',contact_person1='$contact_person1',designation1='$designation1',con_per_mobile1='$con_per_mobile1',con_per_email1='$con_per_email1',contact_person2='$contact_person2',designation2='$designation2',con_per_mobile2='$con_per_mobile2',con_per_email2='$con_per_email2',status='$status',branch_id='$branch_id',company_id='$company_id',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE id='".$_POST['id']."'";
  	 	$result = mysqli_query($conn,$insert) or die( mysqli_error($conn) );

  	 	$act_edit = "UPDATE `act_master` SET `ac_name` = '".$name."',`account_group` = '".$account_group."'WHERE `party_code` = '".$_POST['code']."'";
    	$act_edit_res = mysqli_query($conn,$act_edit);

    	$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               = 'vendor_add';
	$user_id            = $_SESSION['user_detail']['id'];
	$data               = serialize($_POST);
	$act_mst    		= "SELECT * FROM act_master WHERE ac_name='".strtoupper($_POST['name'])."'";
    $act_mst_res 		= mysqli_query($conn,$act_mst) or die( mysqli_error($conn) );
    if (mysqli_num_rows($act_mst_res) != 0){
    	$insert = "INSERT INTO `vendor_master`(`code`, `type`, `name`, `p_name`, `address`, `p_address`, `country`, `state`, `district`, `city`, `pincode`, `contact`, `mobile`, `email`, `pan`, `gstin`, `group_id`, `file_img`, `proprietor`, `contact_person`, `designation`, `con_per_mobile`, `con_per_email`, `contact_person1`, `designation1`, `con_per_mobile1`, `con_per_email1`, `contact_person2`, `designation2`, `con_per_mobile2`, `con_per_email2`, `status`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`)VALUES ('".$code."','".$v_type."','".$name."','".$p_name."','".$address."','".$p_address."','".$country."','".$state."','".$district."','".$city."','".$pincode."','".$contact."','".$mobile."','".$email."','".$pan."','".$gstin."','".$group_id."','".$image."','".$proprietor."','".$contact_person."','".$designation."','".$con_per_mobile."','".$con_per_email."','".$contact_person1."','".$designation1."','".$con_per_mobile1."','".$con_per_email1."','".$contact_person2."','".$designation2."','".$con_per_mobile2."','".$con_per_email2."','".$status."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";

    
		$result = mysqli_query($conn,$insert);	
		
    }else{
    	$insert = "INSERT INTO `vendor_master`(`code`, `type`, `name`, `p_name`, `address`, `p_address`, `country`, `state`, `district`, `city`, `pincode`, `contact`, `mobile`, `email`, `pan`, `gstin`, `file_img`, `contact_person`, `designation`, `con_per_mobile`, `con_per_email`, `contact_person1`, `designation1`, `con_per_mobile1`, `con_per_email1`, `contact_person2`, `designation2`, `con_per_mobile2`, `con_per_email2`, `status`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`)VALUES ('".$code."', '".$v_type."', '".$name."','".$p_name."','".$address."','".$p_address."','".$country."','".$state."','".$district."','".$city."','".$pincode."','".$contact."','".$mobile."','".$email."','".$pan."','".$gstin."','".$image_name."','".$contact_person."','".$designation."','".$con_per_mobile."','".$con_per_email."','".$contact_person1."','".$designation1."','".$con_per_mobile1."','".$con_per_email1."','".$contact_person2."','".$designation2."','".$con_per_mobile2."','".$con_per_email2."','".$status."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	
    	
    		
		$result = mysqli_query($conn,$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 ('".$act_code."','".$name."','0.00','0.00','".$account_group."','".$code."','".$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);

	
  }
  echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=vendor/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=vendor/add.php') );die();
} 