<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Formatter
{
	public $flag = "INVOICE";

	public function __construct()
	{
		
	}

	private function prepare_tax($trans)
	{
		if (empty($trans["tax"]))
		{
			throw new Exception("Can't create e-vat due to no tax presence.");
			return false;
		}

		$return = array();
		// $totalLevy = array();

		foreach ($trans["tax"] as $tax)
		{
			if (empty($return[$tax["transItemID"]]))
			{
				$return[$tax["transItemID"]] = array(
					"levyAmountA" => 0,
					"levyAmountB" => 0,
					"levyAmountC" => 0,
					"levyAmountD" => 0,
					"levyAmountE" => 0,
					"exciseAmount" => 0,
					"totalLevy" => 0,
					"totalVat" => 0,
				);
			}

			switch(strtoupper(trim($tax["taxTypeCode"])))
			{
				case "GETFUND":
					$return[$tax["transItemID"]]["levyAmountA"] = $tax["taxValue"];
					$return[$tax["transItemID"]]["totalLevy"] += $tax["taxValue"];
				break;
				case "NHIL":
					$return[$tax["transItemID"]]["levyAmountB"] = $tax["taxValue"];
					$return[$tax["transItemID"]]["totalLevy"] += $tax["taxValue"];
				break;
				case "COVID":
					$return[$tax["transItemID"]]["levyAmountC"] = $tax["taxValue"];
					$return[$tax["transItemID"]]["totalLevy"] += $tax["taxValue"];
				break;
				case "CST":
					$return[$tax["transItemID"]]["levyAmountD"] = $tax["taxValue"];
					$return[$tax["transItemID"]]["totalLevy"] += $tax["taxValue"];
				break;
				case "TOURISM":
					$return[$tax["transItemID"]]["levyAmountE"] = $tax["taxValue"];
					$return[$tax["transItemID"]]["totalLevy"] += $tax["taxValue"];
				break;
				case "VAT":
					$return[$tax["transItemID"]]["totalVat"] = $tax["taxValue"];
				break;
			}
		}

		return $return;
	}

	private function prepare_attributes($detail)
	{
		$return = array(
			"batch" => "",
			"expiryDate" => "",
			"serialNo" => ""
		);

		$detail;

		return $return;
	}

	public function format($trans=array())
	{
		$taxes = $this->prepare_tax($trans);

		$return = array(
			"currency" => "GHS",
			"exchangeRate" => 1.0,
			"invoiceNumber" => $trans["tNo"],
			"totalLevy" => $trans["totTax"],
			"userName" => $trans["conName"],
			"flag" => $this->flag,
			"calculationType" => $trans["includeTax"] ? "INCLUSIVE" : "EXCLUSIVE",
			"totalVat" => $trans["totTax"],
			"transactionDate" => $trans["tDate"],
			"totalAmount" => $trans["totAmount"],
			"totalExciseAmount" => 0.00,
			"voucherAmount" => 0,
			"businessPartnerName" => $trans["conName"],
			"businessPartnerTin" => $trans["conTaxNo"],
			"saleType" => "NORMAL",
			"discountType" => "GENERAL",
			"discountAmount" => $trans["totDisc"],
			"reference" => "",
			"groupReferenceId" => "",
			"purchaseOrderReference" => "",
			"taxType" => "FLAT",
			"items" => array()
		);

		$totalLevy = 0;
		$totalVat = 0;
		$discountAmount = 0;
		foreach($trans["details"] as $idx=>$detail)
		{
			$attr = $this->prepare_attributes($detail);

			$tax = $taxes[$detail["id"]];

			$totalLevy += $tax["totalLevy"];

			$totalVat += $tax["totalVat"];

			$discountAmount += $detail["discAmount"];

			$item = array(
				"itemCode" => $detail["id"] . "_" . ($idx + 1) . (!empty($detail["SKU"]) ? "" . $detail["SKU"] : ""),
				"itemCategory" => "",
				"expireDate" => $attr["expiryDate"],
				"description" => $detail["itemName"],
				"quantity" => $detail["qty"],
				"levyAmountA" => $tax["levyAmountA"],
				"levyAmountB" => $tax["levyAmountB"],
				"levyAmountC" => $tax["levyAmountC"],
				"levyAmountD" => $tax["levyAmountD"],
				"levyAmountE" => $tax["levyAmountE"],
				"discountAmount" => $detail["discAmount"],
				"exciseAmount" => $tax["exciseAmount"],
				"batchCode" => $attr["batch"],
				"unitPrice" => $trans["includeTax"] ? $detail["rateWithTax"] : $detail["rate"]
			);

			$return["items"][] = $item;
		}

		$return["totalLevy"] = $totalLevy;
		$return["totalVat"] = $totalVat;
		$return["discountAmount"] = $discountAmount;

		$return["totalAmount"] = $trans["includeTax"] ? $trans["totAmount"] : ($trans["totAmount"] - ($totalLevy + $totalVat));

		return $return;
	}
}

