<?php
require "commonincludefiles.php";
//error_reporting(1);
if ($_SESSION["cCompany_id"] != "" && isset($_SESSION["cCompany_id"])) {
	$objCustomerSupportMgmt = new CustomerSupportMgmt();
	$objSupporProductMgmt = new SupporProductMgmt();
	$objSupportExpencsesMgmt = new SupportExpencsesMgmt();


	$expensesname = json_decode(stripslashes($_REQUEST['expensesname']));
	$expensesamount = json_decode(stripslashes($_REQUEST['expensesamount']));
	$product_id = explode(',', $_REQUEST["product_ids"]);
	$servicetype = explode(',', $_REQUEST["servicetype"]);

	$objCustomerSupportMgmt->support_id = $_REQUEST["support_id"];
	$objCustomerSupportMgmt->customer_id = $_REQUEST["customer_id"];
	$objCustomerSupportMgmt->support_date = $_REQUEST['support_date'];
	$objCustomerSupportMgmt->support_stime = $_REQUEST['support_timefrom'];
	$objCustomerSupportMgmt->support_etime = $_REQUEST['support_timeto'];
	$objCustomerSupportMgmt->support_distance = $_REQUEST["distance"];
	$objCustomerSupportMgmt->support_charge = $_REQUEST["Charges"];
	$objCustomerSupportMgmt->support_close = $_REQUEST['supportclose'];
	$objCustomerSupportMgmt->nextsupport_date = $_REQUEST['nextsupportdate'];
	$objCustomerSupportMgmt->nextsupport_time = $_REQUEST['nextsupporttime'];
	$objCustomerSupportMgmt->support_work = $_REQUEST['workcomment'];
	$objCustomerSupportMgmt->support_pending = $_REQUEST['pendingcomment'];
	$objCustomerSupportMgmt->courier_no = $_REQUEST['courier_no'];
	$objCustomerSupportMgmt->invoice_date = $_REQUEST['invoice_date'];
	$objCustomerSupportMgmt->invoice_no = $objCustomerSupportMgmt->getInvoiceNo();
	if ($objCustomerSupportMgmt->support_id == "0" || $objCustomerSupportMgmt->support_id == "") {

		$objCustomerSupportMgmt->add();

		if ($objCustomerSupportMgmt->support_id > 0) {
			for ($i = 0; $i < count($product_id); $i++) {
				$objSupporProductMgmt->support_id = $objCustomerSupportMgmt->support_id;
				$objSupporProductMgmt->product_id = $product_id[$i];
				$objSupporProductMgmt->support_type = $servicetype[$i];
				$objSupporProductMgmt->add();
			}
			for ($i = 0; $i < count($expensesname); $i++) {

				$objSupportExpencsesMgmt->support_id = $objCustomerSupportMgmt->support_id;
				$objSupportExpencsesMgmt->expenses = $expensesname[$i];
				$objSupportExpencsesMgmt->cost = $expensesamount[$i];
				$objSupportExpencsesMgmt->add();
			}
		}
	} else {

		$objCustomerSupportMgmt->edit();

		$objSupporProductMgmt->support_id = $_REQUEST["support_id"];
		$objSupporProductMgmt->delete();

		$objSupportExpencsesMgmt->support_id = $_REQUEST["support_id"];
		$objSupportExpencsesMgmt->delete();

		for ($i = 0; $i < count($product_id); $i++) {
			$objSupporProductMgmt->support_id = $objCustomerSupportMgmt->support_id;
			$objSupporProductMgmt->product_id = $product_id[$i];
			$objSupporProductMgmt->support_type = $servicetype[$i];
			$objSupporProductMgmt->add();
		}
		for ($i = 0; $i < count($expensesname); $i++) {

			$objSupportExpencsesMgmt->support_id = $objCustomerSupportMgmt->support_id;
			$objSupportExpencsesMgmt->expenses = $expensesname[$i];
			$objSupportExpencsesMgmt->cost = $expensesamount[$i];
			$objSupportExpencsesMgmt->add();
		}
	}
	echo '{"success":"true","msg":""}';
} else {
	echo '{"success":"false","msg":"session expiry"}';
}
?>