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

require_once(APPPATH . "interfaces/Transaction_processor_interface.php");

class TransactionProcessorFactory
{
	public static function GetProcessor(&$postData, &$params)
	{
		require_once(APPPATH . 'processors/TransFlag.php');

		TransFlag::$isNewtrans = false;

		if (empty($postData['id']))
		{
			TransFlag::$isNewtrans = true;
    	}

		$postData["isSales"] = false;

		if (in_array($postData['tType'], array(SALINV, SALRETAIL, CRNOTE, DBNOTE, EST, SALORD, DELCHALLAN, TRANSDEL, MATDEL)))
		{
			$postData["isSales"] = true;
		}

		$postData["isPmt"] = false;
		if (in_array($postData['tType'], array(SALPMT,PURPMT)))
		{
			$postData["isPmt"] = true;
		}

		$params->isMaterial = !empty($postData["isMaterial"]) ? $postData["isMaterial"] : "";

		$processorName = "";

		switch($postData["tType"])
		{
			case SALINV:
			case SALRETAIL:
				$processorName = "Salinv_processor";
				//if ($postData["classCode"] == "EXPWP" || $postData["classCode"] == "EXPWOP")
				//{
				//	$processorName = "Salinv_export_processor";
				//}
			break;
			case SALORD:
				$processorName = "Salord_processor";
			break;
			case PURINV:
				$processorName = "Purinv_processor";
			break;
			case PURORD:
				$processorName = "Purord_processor";
			break;
			case JOURNAL_CONTRA:
			case JOURNAL_VOUCHER:
				$processorName = "Journal_processor";
			break;
			case SALPMT:
			case PURPMT:
				$processorName = "Payment_processor";
			break;
			case MATRPT:
			case MATDEL:
				$processorName = "Material_processor";
			break;
			/*
			case CRNOTE:
			case PURCRNOTE:
				$processorName = "Crnote_processor";
			break;
			case SALPMT:
				$processorName = "Salpmt_processor";
			break;
			case PURPMT:
				$processorName = "Purpmt_processor";
			break;
			case JOURNAL_CONTRA:
				$processorName = "Contra_processor";
			break;
			case JOURNAL_VOUCHER:
				$processorName = "Journal_processor";
			break;
			*/
			default:
				$processorName = "Transaction_processor";
			break;
		}

		$processorName = ucfirst(strtolower($processorName));

		$processorFile = APPPATH . "processors/" . $processorName . ".php";

		if (file_exists($processorFile))
		{
			require_once($processorFile);

			if (class_exists($processorName))
			{
				$processor = new $processorName($params);
				self::addChild($processor, $postData, $params);

				return $processor;
			}
		}

		return null;
	}

	public static function addChild(&$processor, &$postData, &$params)
	{
		require_once(APPPATH . "processors/TransFieldsProcessor.php");
		$processor->addChild(new TransFieldsProcessor($params));

		// in_array($data['tType'],array(SALINV,CRNOTE,DELCHALLAN,PURRECP)) && !in_array($data['status'], $this->CI->config->item('ignore_status'))
		if (sal_mod_active(RETURN_GOODS) && in_array($postData['tType'], array(DELCHALLAN, PURRECP)))
		{
			require_once(APPPATH . "processors/RentalItemsProcessor.php");
			$processor->addChild(new RentalItemsProcessor($params));
		}

		if (in_array($postData['tType'], array(EST,SALORD,PUREST,PURORD)))
		{
			require_once(APPPATH . "processors/NegativeItemsProcessor.php");
			$processor->addChild(new NegativeItemsProcessor($params));
		}

		if (in_array($postData['tType'],array(SALINV,SALRETAIL,DBNOTE)))
		{
			require_once(APPPATH . "processors/MaxDiscountProcessor.php");
			$processor->addChild(new MaxDiscountProcessor($params));
		}

		$salModCodes = get_company('salModCodes');
		$sal_codes = explode(',',$salModCodes);
		$hasAuditlogs = in_array('AL',$sal_codes);
		if(!empty($hasAuditlogs))
		{
			require_once(APPPATH . "processors/ActivityTracerProcessor.php");
			$processor->addChild(new ActivityTracerProcessor($params));
		}
	}

	public static function addValidators(&$postData, Transaction_processor_interface $transProcessor)
	{
		require_once(APPPATH . "processors/TransSaveValidator.php");

		$transProcessor->addValidator(new TransSaveValidator($postData));
	}
}

/*

$transProcessor = $TransactionProcessorFactory::GetProcessor($postData);

*/

