<?php
/**
 * This is general file which contains
 * connection file, common function file, etc.,
 *
 * PHP version 5
 *
 * @category Admin_Tool
 * @package  Admin_Tool
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  ACE
 * @version  CVS:1.0.1
 * @link     http://auminfosys.com/
 * @filesource
 */

/**
 * This is general file which contains
 * connection file, common function file, etc.,
 *
 */
require "../commonincludefiles.php";
error_reporting(1);
$arr = array();
$total = 0;
$product_id = $_SESSION["cCompany_id"];
$k = 0;

$customer_id = Escape($_POST['customer_id']);
if ($customer_id != "") {

	$condition .= " AND customer_id='" . $customer_id . "'";

	$objCustmerProductMgmt = new CustmerProductMgmt();

	$result = $objCustmerProductMgmt->getAllRecords(0, 0, $condition, $order_field, $order_type);
	$num = dbRows($result);

	$i = 0;
	$product_ids = [];
	while ($mainrow = dbArray($result)) {
		$product_ids[$i] = $mainrow['product_id'];
		$purchase_date[$i] = $mainrow['purchase_date'];
		$i++;

	}
	$objProductMgmt = new ProductMgmt();


	$where .= " And company_id= '" . $_SESSION["cCompany_id"] . "'";

	$rs = $objProductMgmt->getAllRecords($limit, $start, $where, $order_field, $order_type);
	$total = $objProductMgmt->getCountRecords(0, 0, $where, "", "");
	$i = 0;
	if (dbRows($rs) > 0) {
		$temparray = array();
		while ($row = dbArray($rs)) {
			if (in_array($row["product_id"], $product_ids)) {
				$temparray["product_id"] = $row["product_id"];
				$temparray["product_name"] = $row["product_name"];
				$temparray["product_price"] = $row["product_price"];
				$temparray["product_active"] = $row["product_active"];
				$temparray["product_description"] = $row["product_description"];
				$temparray["selected"] = "true";
				$temparray["purchasedate"] = $purchase_date[$i];
				$temparray["servicetype"] = checkservicetype($purchase_date[$i]);



			} else {
				$temparray["selected"] = "false";
				$temparray["product_id"] = $row["product_id"];
				$temparray["product_name"] = $row["product_name"];
				$temparray["product_price"] = $row["product_price"];
				$temparray["product_active"] = $row["product_active"];
				$temparray["product_description"] = $row["product_description"];
				$temparray["purchasedate"] = "";
				$temparray["servicetype"] = "";
			}

			$arr[$i] = $temparray;
			$i++;

		}
		echo '{"total":"' . $total . '","data":' . json_encode($arr) . '}';
	}


}

$support_id = Escape($_POST['support_id']);
$editcustomer_id = Escape($_POST['editcustomer_id']);

if ($support_id != "" && $editcustomer_id != "") {
	$condition = "";

	$condition .= " AND support_id='" . $support_id . "'";
	$order_field = "product_id";
	$order_type = "ASC";

	$objSupporProductMgmt = new SupporProductMgmt();

	$result = $objSupporProductMgmt->getAllRecords(0, 0, $condition, $order_field, $order_type);
	$num = dbRows($result);

	$objCustmerProductMgmt = new CustmerProductMgmt();

	$i = 0;
	while ($mainrow = dbArray($result)) {
		$product_ids[$i] = $mainrow['product_id'];
		$purchasedate[$i] = $mainrow['purchase_date'];
		$support_type[$i] = $mainrow['support_type'];
		$i++;

	}
	$objProductMgmt = new ProductMgmt();
	$where .= " And company_id= '" . $_SESSION["cCompany_id"] . "'";
	$rs = $objProductMgmt->getAllRecords($limit, $start, $where, $order_field, $order_type);
	$total = $objProductMgmt->getCountRecords(0, 0, $where, "", "");
	$i = 0;
	if (dbRows($rs) > 0) {
		$temparray = array();
		while ($row = dbArray($rs)) {
			$sql = "";
			$sql = " And customer_id='" . $editcustomer_id . "'
					    AND	product_id='" . $row["product_id"] . "' 
						AND  company_id= '" . $_SESSION["cCompany_id"] . "'";
			if (in_array($row["product_id"], $product_ids)) {
				$temparray["product_id"] = $row["product_id"];
				$temparray["product_name"] = $row["product_name"];
				$temparray["product_price"] = $row["product_price"];
				$temparray["product_active"] = $row["product_active"];
				$temparray["product_description"] = $row["product_description"];
				$temparray["selected"] = "true";
				$temparray["purchasedate"] = $objCustmerProductMgmt->getpurchaseDate(0, 0, $sql, "", "");
				$temparray["servicetype"] = $support_type[$i];
			} else {
				$temparray["selected"] = "false";
				$temparray["product_id"] = $row["product_id"];
				$temparray["product_name"] = $row["product_name"];
				$temparray["product_price"] = $row["product_price"];
				$temparray["product_active"] = $row["product_active"];
				$temparray["product_description"] = $row["product_description"];
				$temparray["purchasedate"] = $objCustmerProductMgmt->getpurchaseDate(0, 0, $sql, $order_field, $order_type);
				$temparray["servicetype"] = "";
			}

			$arr[$i] = $temparray;
			$i++;

		}
		echo '{"total":"' . $total . '","data":' . json_encode($arr) . '}';
	}




}
function checkservicetype($purchasedate)
{
	// echo $purchasedate;

	$currentDate = date("Y-m-d");

	$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($purchasedate)) . " +1 year");
	$dateOneYearAdded = date("Y-m-d", $dateOneYearAdded);

	//echo $nextdate= date('Y-m-d', strtotime('+1 year, +0 days', $purchasedate)); 
	if ($dateOneYearAdded >= $currentDate) {
		return "Free";
	} else {
		return "Chargeble";

	}


}
?>