<?php
$path_to_root="../../";
include($path_to_root.'/application/config/config.php');

$invoice_no_execute=base64_decode($_REQUEST['invoice_no_execute']);

define("BASEPATH",'../../');
require_once (BASEPATH.'application/config/database.php');

define("SERVICE_HOST",$db['default']['hostname']);
define("SERVICE_USER",$db['default']['username']);
define("SERVICE_PASSWORD",$db['default']['password']);
define("SERVICE_DB",$db['default']['database']);


$db = mysql_connect(SERVICE_HOST, SERVICE_USER, SERVICE_PASSWORD);
mysql_select_db(SERVICE_DB, $db);


$sql = "SELECT * FROM  ospos_sales WHERE receipt_no='".$invoice_no_execute."'";

$result = mysql_query($sql, $db);
$sale_id=0;
$is_synced=0;

$sales_data_root=array();

while ($row = mysql_fetch_object($result)) {
	$sales_data_root[] = (object)$row;
	$sale_id=$row->sale_id;
	$is_synced=$row->is_sync;
}

if(intval($is_synced)==0){
	$sql = "SELECT *  FROM ospos_sales_items WHERE sale_id ='".$sale_id."'";
	$result = mysql_query($sql, $db);
	$i = 0;
	$sale_id=0;

	$return_cash_amount=0;

	$can_be=0;
	while ($row = mysql_fetch_object($result)) {
		$sales_data[] = (object)$row;
		$sale_id=$row->sale_id;

		if($row->quantity_purchased<0){
			$return_cash_amount+=round(abs($row->vat_enable_price_after_discount)+(abs($row->vat_enable_price_after_discount)/100*$row->vat_percentage));
		}elseif ($row->quantity_purchased>0){
			$can_be=1;
		}
	}
	$error_message="";
	/////////////////////DELETE NON RETURN DATA////////////
	if(intval($sale_id)){

		if($can_be==1){
			$delete_sql = "DELETE FROM ospos_sales_items WHERE sale_id ='".$sale_id."' AND quantity_purchased>0";
			mysql_query($delete_sql, $db);

			$sql_cash = "SELECT * FROM  ospos_sales_payments WHERE sale_id='".$sale_id."' AND payment_type='Cash'";

			$result_cash = mysql_query($sql_cash, $db);
			$has_cash=0;
			$has_cash_amount=0;

			while ($row_cash = mysql_fetch_object($result_cash)) {
				$has_cash=1;
				$has_cash_amount=$row_cash->payment_amount;
			}

			if($has_cash==1){
				$final_cash=$has_cash_amount-$return_cash_amount;
				$update_sql = "UPDATE ospos_sales_payments SET payment_amount='".$final_cash."' WHERE sale_id='".$sale_id."' AND payment_type='Cash'";
				mysql_query($update_sql, $db);

			}else{
				$insert_sql = "INSERT INTO ospos_sales_payments(sale_id,payment_type,payment_amount) VALUES('".$sale_id."','Cash','".($return_cash_amount*-1)."')";
				mysql_query($insert_sql, $db);

			}

			$delete_sql_stock_moves="DELETE FROM stock_moves WHERE reference ='".$invoice_no_execute."' AND qty<0";

			mysql_query($delete_sql_stock_moves, $db);

			$force_reference=substr($invoice_no_execute,3).substr($invoice_no_execute,0,3);

			$delete_sql_stock_moves="DELETE FROM stock_moves WHERE reference ='".$force_reference."'";

			mysql_query($delete_sql_stock_moves, $db);

		}else{
			$error_message="Already Execute Invoice No : ".$invoice_no_execute."";
		}

	}else{
		$error_message="Invoice No : ".$invoice_no_execute." invalid.";
	}
}else{
	$error_message="Invoice No : ".$invoice_no_execute." already synced. Please contact with administrator.";
}
if(trim($error_message)){

	echo '<table style="text-align:center; width:100%;">
					<tr style="text-align:center;padding:8px;">
					<td style="text-align:center;padding:8px;">
					<font style="color:#97FF00;font-weight:bold;background-color: #FF0000; padding:16px;font-size: 20px;">'.$error_message.'</font>
					</td>
					</tr>
					</table>';
}else{

	echo '<table style="text-align:center; width:100%;">
					<tr style="text-align:center;padding:8px;">
					<td style="text-align:center;padding:8px;">
					<font style="color:#97FF00;font-weight:bold;background-color: #008148; padding:16px;font-size: 20px;">Invoice No: '.$invoice_no_execute.' return adjustment successfully. Cash Amount: '.$return_cash_amount.'</font>
					</td>
					</tr>
					</table>';
}
?>