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

require_once APPPATH . 'processors/Transaction_processor.php';

class Journal_processor extends Transaction_processor
{
	public function init_save(&$postData)
	{
		parent::init_save($postData);

		if (empty($data['acc_details']))
		{
			return true;
		}

		TransFlag::$sundry = array();

		$acc_contact = 0;
		$tmpacc_ids = $tmpcon_ids = array();
		foreach ($postData['acc_details'] as $key => $row)
		{
			if ($postData['tType'] == JOURNAL_CONTRA && !in_array($row['groupCode'], array('CH','BA','BO','CC')))
			{
				throw new Exception(lang('JOURNAL_CONTRA_WITH_OUT_BANK'));
				return false;
			}

			if (!in_array($row['groupCode'], array('SD','SC')))
			{
				if (!empty($row['conId']))
				{
					TransFlag::$sundry[$row['groupCode']][] = $row;
					$acc_contact = $row['conId'];
				}
			}
		}

		$this->params->sundry = TransFlag::$sundry;

		if ($postData['tType'] == JOURNAL_VOUCHER)
		{
			$this->_journal_init_save($postData);
		}

		if (isset($postData['dueDate']))
		{
			unset($postData['dueDate']);
		}
	}

	private function _journal_init_save(&$postData)
	{
		$postData['acc_tax'] = array();
		$input = $output = false;
		// $acc_contact = 0;
		$postData['taxableAmount'] = $postData['totAmount'];
		$postData['totTax'] = 0;

		if (!TransFlag::$sundry)
		{
			return;
		}

		foreach ($postData['acc_details'] as $key => $row)
		{
			if (!empty($row['taxTypeCode']))
			{
				$ipos = strpos($row['accCode'],'-I-');
				$opos = strpos($row['accCode'],'-O-');

				/*if ($ipos && $row['debit']==0) { //input tax amount must in debit
					$this->error = lang('INPUT_TAX_DEBIT_ZERO');
					return false;
				}

				if ($opos && $row['credit']==0) { // ouput tax amount must in credit
					$this->error = lang('OUTPUT_TAX_CREDIT_ZERO');
					return false;
				}*/

				if ($ipos || $opos)
				{
					$acc_tax = array(
						'transItemId' => 0,
						'taxTypeCode' => $row['taxTypeCode'],
						'taxRate' => substr($row['accCode'],strrpos($row['accCode'],'-')+1)
					);

					if (!empty($row['debit']))
					{
						$input=true;
						$acc_tax['taxValue'] = $row['debit'];

						$acc_tax['itc'] = 1;
						$postData['taxableAmount'] -= $row['debit'];
						$postData['totTax'] += $row['debit'];
					}
					else
					{
						$output=true;
						$acc_tax['taxValue'] = $row['credit'];
						$acc_tax['itc'] = 1;
						$postData['taxableAmount'] -= $row['credit'];
						$postData['totTax'] +=$row['credit'];
					}

					$postData['acc_tax'][] = $acc_tax;
				}
			}
		}

		if ($input || $output)
		{
			if (!empty(TransFlag::$sundry['SC']) && count(TransFlag::$sundry['SC']) > 1)
			{
				throw new Exception(lang('MULTIPLE_PAY_ACCOUNT'));
				return false;
			}

			if (!empty(TransFlag::$sundry['SD']) && count(TransFlag::$sundry['SD']) > 1)
			{
				throw new Exception(lang('MULTIPLE_RECEIVE_ACCOUNT'));
				return false;
			}

			if (($input || $output) && (empty(TransFlag::$sundry['SD']) && empty(TransFlag::$sundry['SC'])))
			{
				throw new Exception(lang('INPUT_OUTPUT_ACCOUNT_MUST'));
				return false;
			}
		}

		if (!empty($postData['acc_tax']) && $acc_contact)
		{
			$this->CI->db->select('id AS conId, name AS conName, stateCode AS conStateCode, stateId AS conStateId, taxNo AS conTaxNo, conTaxScheme');
			$this->CI->db->from('contacts');
			$this->CI->db->where(array('cId' => $this->cId, 'id' => $acc_contact));
			$tmp_con = $this->CI->db->get()->row_array();

			$postData	= array_merge($postData, $tmp_con);
		}
	}
}
