<?php 
include("../config.php");
if(!empty($_POST)){
	$g_act_codes         = accountCode($conn);
	$act_code         	 = $g_act_codes;
	
	$code          		 = mysqli_real_escape_string($conn, $_POST['code']);
	$name          		 = mysqli_real_escape_string($conn, $_POST['name']);
	$address       		 = mysqli_real_escape_string($conn, $_POST['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']);
	$pan_img          	 = mysqli_real_escape_string($conn, $_POST['image']);
	$status          	 = mysqli_real_escape_string($conn, $_POST['status']);
	
	$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'];

	

	if($_POST["id"] != ''){
  	 	$type               = 'agent_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
  	 	$insert = "UPDATE agent SET code='$code',name='$name',address='$address',country='$country',state='$state',district='$district',city='$city',pincode='$pincode',contact='$contact',mobile='$mobile',email='$email',pan='$pan',pan_img='$pan_img',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               = 'agent_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) );
	    $act_rows           = mysqli_fetch_assoc($act_mst_res);
    
	    if (mysqli_num_rows($act_mst_res) != 0){
	    	
	    	$acode = $act_rows['act_code'];
	    	$insert = "INSERT INTO `agent`(`code`, `act_code`, `name`, `address`, `country`, `state`, `district`, `city`, `pincode`, `contact`, `mobile`, `email`, `pan`, `pan_img`, `status`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`)VALUES ('".$code."','".$acode."','".$name."','".$address."','".$country."','".$state."','".$district."','".$city."','".$pincode."','".$contact."','".$mobile."','".$email."','".$pan."','".$pan_img."','".$status."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";

			$result = mysqli_query($conn,$insert);	
	    }else{
	    	$insert = "INSERT INTO `agent`(`code`,`act_code`, `name`, `address`, `country`, `state`, `district`, `city`, `pincode`, `contact`, `mobile`, `email`, `pan`, `pan_img`, `status`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`)VALUES ('".$code."','".$act_code."','".$name."','".$address."','".$country."','".$state."','".$district."','".$city."','".$pincode."','".$contact."','".$mobile."','".$email."','".$pan."','".$pan_img."','".$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=agent/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=agent/add.php') );die();
} 