<?php

namespace Modules\Invoices\BalanceView;

class Model extends \App\Mvc\ModelAbstract
{

	public function getResult($idBalance)
	{
		// get global $CFG
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// results
		$balanceDetails=\Entity\Balance::select($idBalance);

		$currencyArr = [];
		$clientData = [];
		$supplierData = [];

		if ($balanceDetails->recordExists()) {
			$balance['balanceNumber'] = $balanceDetails->balanceNumber;
			$balance['balanceDate'] = $balanceDetails->balanceDate;
			$balance['balanceType'] = $balanceDetails->balanceType;
			$balance['balanceStatus'] = $balanceDetails->balanceStatus;
			$balance['balanceCode'] = $balanceDetails->balanceCode;
			$balance['balanceName'] = $balanceDetails->balanceName;
			$balance['balanceComments'] = $balanceDetails->comments;
			$balance['generation'] = $balanceDetails->generation;
			$balance['idUser'] = $balanceDetails->idUser;
			$balance['idAddUser'] = $balanceDetails->idAddUser;
			$balance['addTime'] = $balanceDetails->addTime;
			$balance['idConfirmUser'] = $balanceDetails->idConfirmUser;
			$balance['confirmTime'] = $balanceDetails->confirmTime;
			$balance['remarks'] = $balanceDetails->remarks;
			$balance['processedRemarks'] = $balanceDetails->processedRemarks;

			$balance['clientData']['total'] = 0;
			$balance['clientData']['totalE'] = 0;

			$balance['supplierData']['total'] = 0;
			$balance['supplierData']['totalE'] = 0;

			$balance['totalIncomeDefCurr'] = $balanceDetails->totalIncomeDefCurr;
			$balance['totalExpensesDefCurr'] = $balanceDetails->totalExpensesDefCurr;
			$balance['totalProfitDefCurr'] = $balanceDetails->totalProfitDefCurr;
			$balance['defaultCurrency'] = $CFG['currencies']['defaultCurrency'];

			if ($balanceDetails->clientInvoiceRowsList) {
				foreach($balanceDetails->clientInvoiceRowsList as $idService=>$invoiceRows) {
					foreach($invoiceRows as $idRow=>$rowDetails) {
						$clientInvValue = 0;

						// get clientData key
						$clKey = $idService.'_'.$rowDetails['idInvoice'];

						// populate idService and refund
						if (substr($idService, 0, 1)=='R') {
							$clientData[$clKey]['idService']	= substr($idService, 1);
							$clientData[$clKey]['refund']		= true;
						} else {
							$clientData[$clKey]['idService']	= $idService;
							$clientData[$clKey]['refund']		= false;
						}
						// add service name
						if ($rowDetails['serviceCode']) {
							$clientData[$clKey]['serviceName'] = \App\Config\Services_Basic::getInstance()->getServiceName($rowDetails['serviceCode']);
						}

						// add some invoice details
						$clientData[$clKey]['idInvoice']	=$rowDetails['idInvoice'];

						$invDet=\Entity\Invoice::select($rowDetails['idInvoice']);
						$clientData[$clKey]['invoiceNumber']=$invDet->invoiceNumber;
						$clientData[$clKey]['invoiceDate']	=$invDet->invoiceDate;
						$clientData[$clKey]['currency']		=$invDet->currency;
						$clientData[$clKey]['idCorporate']	=$invDet->idCorporate;
						$clientData[$clKey]['idCustomer']	=$invDet->idCustomer;

						$currencyArr[] = $invDet->currency;

						// add invoice row details
						$clientInvValue =  $rowDetails['pricePrintValue']+$rowDetails['vatPrintValue'];
						$clientData[$clKey][$invDet->currency]['value']	+= $clientInvValue;
						$clientData[$clKey]['valueE']					= \App\CurrencyExchange\Rates::getInstance($invDet->invoiceDate)->convert($clientData[$clKey][$invDet->currency]['value'], $invDet->currency, $CFG['currencies']['defaultCurrency']);

						$balance['clientData'][$invDet->currency]['total']+=$clientInvValue;

					}

				}
			}

			if ($balanceDetails->supplierInvoiceRowsList) {
				foreach($balanceDetails->supplierInvoiceRowsList as $idRow) {

					$supplierValue = 0;

					// get supplier invoice row details
					$supplierRow=\Entity\SupplierInvoiceRow::select($idRow);

					// the supplier invoice id
					$idSupplierInvoice=$supplierRow->idSupplierInvoice;

					// GROUP BY idInvoice /////////////////////////////////////////////////////////////////////
					// if invoice already included in this invoice
					$supplierData[$idSupplierInvoice]['idInvoice'] 		= $idSupplierInvoice;
					$supplierData[$idSupplierInvoice]['invoiceNumber']	= $supplierRow->supplierInvoice->invoiceNumber;
					$supplierData[$idSupplierInvoice]['invoiceDate']	= $supplierRow->supplierInvoice->invoiceDate;
					$supplierData[$idSupplierInvoice]['currency']		= $supplierRow->supplierInvoice->currency;
					$supplierData[$idSupplierInvoice]['idSupplier']		= $supplierRow->supplierInvoice->idSupplier;

					$supplierValue = $supplierRow->priceValue+$supplierRow->vatValue;
					$supplierData[$idSupplierInvoice][$supplierRow->supplierInvoice->currency]['value']	+= $supplierValue;
					$supplierData[$idSupplierInvoice]['valueE'] = \App\CurrencyExchange\Rates::getInstance($supplierRow->supplierInvoice->invoiceDate)->convert($supplierData[$idSupplierInvoice][$supplierRow->supplierInvoice->currency]['value'], $supplierRow->supplierInvoice->currency, $CFG['currencies']['defaultCurrency']);

					$currencyArr[] = $supplierRow->supplierInvoice->currency;

					$balance['supplierData'][$supplierRow->supplierInvoice->currency]['total']+= $supplierValue;
					if ($supplierRow->serviceName) {
						if ( !$supplierData[$idSupplierInvoice]['serviceNamesList'] or (!in_array($supplierRow->serviceName, $supplierData[$idSupplierInvoice]['serviceNamesList'])) ) {
							$supplierData[$idSupplierInvoice]['serviceNamesList'][]=$supplierRow->serviceName;
						}
					}
				}
			}
		}

		$result['clientData'] 	= $clientData;
		$result['supplierData'] = $supplierData;
		$result['balance'] 		= $balance;
		$result['currencies'] 	= array_unique($currencyArr);

		return $result;

	}
}