<?php  
include(dirname(dirname(dirname(__FILE__)))."/objects/class_connection.php");
include(dirname(dirname(dirname(__FILE__)))."/objects/class_setting.php");
include(dirname(dirname(dirname(__FILE__)))."/header.php");
include(dirname(dirname(dirname(__FILE__))).'/objects/class_staff_commision.php');
include(dirname(dirname(dirname(__FILE__))). "/objects/class_adminprofile.php");

$con = new cleanto_db();
$conn = $con->connect();

$settings = new cleanto_setting();
$settings->conn = $conn;

$staff_commision = new cleanto_staff_commision();
$staff_commision->conn=$conn;

$objadminprofile = new cleanto_adminprofile();
$objadminprofile->conn = $conn;

$ct_currency_symbol=$settings->get_option('ct_currency_symbol');
$getdateformat=$settings->get_option('ct_date_picker_date_format');
$time_format = $settings->get_option('ct_time_format');

$lang = $settings->get_option("ct_language");
$label_language_values = array();
$language_label_arr = $settings->get_all_labelsbyid($lang);

if ($language_label_arr[1] != "" || $language_label_arr[3] != "" || $language_label_arr[4] != "" || $language_label_arr[5] != "")
{
	$default_language_arr = $settings->get_all_labelsbyid("en");
	if($language_label_arr[1] != ''){
		$label_decode_front = base64_decode($language_label_arr[1]);
	}else{
		$label_decode_front = base64_decode($default_language_arr[1]);
	}
	if($language_label_arr[3] != ''){
		$label_decode_admin = base64_decode($language_label_arr[3]);
	}else{
		$label_decode_admin = base64_decode($default_language_arr[3]);
	}
	if($language_label_arr[4] != ''){
		$label_decode_error = base64_decode($language_label_arr[4]);
	}else{
		$label_decode_error = base64_decode($default_language_arr[4]);
	}
	if($language_label_arr[5] != ''){
		$label_decode_extra = base64_decode($language_label_arr[5]);
	}else{
		$label_decode_extra = base64_decode($default_language_arr[5]);
	}
	
	$label_decode_front_unserial = unserialize($label_decode_front);
	$label_decode_admin_unserial = unserialize($label_decode_admin);
	$label_decode_error_unserial = unserialize($label_decode_error);
	$label_decode_extra_unserial = unserialize($label_decode_extra);
    
	$label_language_arr = array_merge($label_decode_front_unserial,$label_decode_admin_unserial,$label_decode_error_unserial,$label_decode_extra_unserial);
	
	foreach($label_language_arr as $key => $value){
		$label_language_values[$key] = urldecode($value);
	}
}
else
{
    $default_language_arr = $settings->get_all_labelsbyid("en");
    
	$label_decode_front = base64_decode($default_language_arr[1]);
	$label_decode_admin = base64_decode($default_language_arr[3]);
	$label_decode_error = base64_decode($default_language_arr[4]);
	$label_decode_extra = base64_decode($default_language_arr[5]);
		
	
	$label_decode_front_unserial = unserialize($label_decode_front);
	$label_decode_admin_unserial = unserialize($label_decode_admin);
	$label_decode_error_unserial = unserialize($label_decode_error);
	$label_decode_extra_unserial = unserialize($label_decode_extra);
    
	$label_language_arr = array_merge($label_decode_front_unserial,$label_decode_admin_unserial,$label_decode_error_unserial,$label_decode_extra_unserial);
	
	foreach($label_language_arr as $key => $value){
		$label_language_values[$key] = urldecode($value);
	}
}

include(dirname(dirname(dirname(__FILE__))).'/assets/lib/date_translate_array.php');
if(isset($_POST['staff_payment_details'])){
	$all = $staff_commision->get_staff_detail($_POST['staff_ids']);
	$i = 1;
	if(mysqli_num_rows($all)>0){
		while($a = mysqli_fetch_array($all)){
		?>
			<tr>
				<td><?php echo $i; ?><input type="hidden" class="staff_pymnt_id" value="<?php echo $a['id']; ?>" /><input type="hidden" class="staff_pymnt_orderid" value="<?php echo $_POST['order_id']; ?>" /></td>
				<td><?php echo $a['fullname']; ?></td>
				<td>
					<div class="input-group">
						<span class="input-group-addon"><?php echo $ct_currency_symbol; ?></span>
						<input type="text" class="form-control sp_staff_amount" value="0" id="sp_staff_amount<?php  echo $a['id']; ?>" data-id="<?php echo $a['id']; ?>" placeholder="00">
					</div>
				</td>
				<td>
					<div class="input-group">
						<span class="input-group-addon"><?php echo $ct_currency_symbol; ?></span>
						<input type="text" class="form-control sp_staff_advance_paid" value="0" id="sp_staff_advance_paid<?php  echo $a['id']; ?>" data-id="<?php echo $a['id']; ?>" placeholder="00">
					</div>
				</td>
				<td>
					<div class="input-group">
						<span><?php echo $ct_currency_symbol; ?><span class="sp_staff_net_total<?php  echo $a['id']; ?>">0</span></span>
						<input type="hidden" class="form-control sp_staff_net_total" value="0" id="sp_staff_net_total<?php  echo $a['id']; ?>" data-id="<?php echo $a['id']; ?>" placeholder="00">
					</div>
				</td>
			</tr>
		<?php 
			$i++;
		}
	}else{
		?>
		<tr><td align="center" colspan='5'>Loading...</td></tr>
		<?php 
	}
}
if(isset($_POST['staff_payment_save'])){
  $data_staff_pymnt_id = explode(',',$_POST['staff_pymnt_id']);
  $data_sp_staff_amount = explode(',',$_POST['sp_staff_amount']);
  $data_sp_staff_advance_paid = explode(',',$_POST['sp_staff_advance_paid']);
  $data_sp_staff_net_total = explode(',',$_POST['sp_staff_net_total']);
  $order_id = $_POST['staff_pymnt_orderid'];
  
  $staff_commision->order_id=$order_id;
  $get_order_net_total = $staff_commision->get_total_payment();
  
  $get_pay_comission_total = 0;
  for($i=0;$i<sizeof((array)$data_staff_pymnt_id);$i++){
    $get_pay_comission_total += (float)$data_sp_staff_net_total[$i];
    $staff_id = $data_staff_pymnt_id[$i];
    $get_pay_comission_total += (float)$staff_commision->get_booking_nettotal($staff_id,$order_id);
  }
  
  if($get_order_net_total < $get_pay_comission_total){
    die("not_ok_comission");
  }
  
  for($i=0;$i<sizeof((array)$data_staff_pymnt_id);$i++){
    $staff_id = $data_staff_pymnt_id[$i];
    $amt_payable = $data_sp_staff_amount[$i];
    $advance_paid = $data_sp_staff_advance_paid[$i];
    $net_total = $data_sp_staff_net_total[$i];
    $payment_method = 'cash';
    $transaction_id = '';
    $payment_date = date('Y-m-d');
    
    $chk_existing = $staff_commision->check_staff_commision_payment($order_id,$staff_id);   
    if($chk_existing == 0){
      $staff_commision->insert_staff_commision($order_id,$staff_id,$amt_payable,$advance_paid,$net_total,$payment_method,$transaction_id,$payment_date);
    }else{
      $staff_commision->insert_staff_commision($order_id,$staff_id,$amt_payable,$advance_paid,$net_total,$payment_method,$transaction_id,$payment_date);
    }
    
  }

  $new_wallet_point =  $_POST['sp_staff_amount'];
  $objadminprofile->id = $_POST['staff_pymnt_id'];
  $wallet_amount = $objadminprofile->get_previous_staff_wallet();
  $objadminprofile->update_wallet_value = $wallet_amount[0] + $new_wallet_point;
  $objadminprofile->update_staff_wallet();
}