<?php

namespace Modules\Invoices\Cashings\Listing;

class Model extends \App\Mvc\Model_ListingAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Model_ListingAbstract::list_execute()
	 */
	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{

		$filters=$listingParams->getFilters();

		// GET THE CASHING DOCUMENTS LIST ---------------------------------------------------------
		$listObj = new \App\EntityList\InvoiceCashings();
		$listObj->setColumns(['doc.docDate', 'doc.docNumber', 'doc.docType', 'doc.value',
				'doc.currency', 'doc.idUser', 'doc.idBankAccount', 'doc.idOffice', 'doc.addTime',
				'doc.voidTime', 'doc.issueTime', 'doc.blockTime', 'doc.idInvoice', 'doc.idImportedDoc',
				'inv.invoiceNumber', 'inv.invoiceDate', 'inv.idCorporate', 'inv.idCustomer',
		]);

		// APPLY FILTERS --------------------------------------------------------------------------

		// id
		if ($filters['id']) {
			$listObj->setFilter('doc.id', '=', $filters['id']);
		}

		// invoice id(s)
		if ($filters['idInvoices']) {
			$idInvoices = explode(",", $filters['idInvoices']);
			$idInvoices = array_map(function($elm){return trim($elm);}, $idInvoices);
			$listObj->setFilter('doc.idInvoice', 'IN', $idInvoices);
		}

		// doc start/end date
		if ($filters['docDate']['start'] and $filters['docDate']['end']) {
			$listObj->setFilter('doc.docDate', 'BETWEEN', [$filters['docDate']['start'], $filters['docDate']['end']]);
		} elseif ($filters['docDate']['start']) {
			$listObj->setFilter('doc.docDate', '>=', $filters['docDate']['start']);
		} elseif ($filters['docDate']['end']) {
			$listObj->setFilter('doc.docDate', '<=', $filters['docDate']['end']);
		}
		// doc series
		if ($filters['idSeries']) {
			$listObj->setFilter('doc.idCashingSeries', '=', $filters['idSeries']);
		}
		// doc number
		if ($filters['docNumber']) {
			$listObj->setFilter('doc.docNumber', '=', $filters['docNumber']);
		}
		// user / office
		if ($filters['idOfficeUser']['id']) {
			list($idOffice, $idUser)=explode('|', $filters['idOfficeUser']['id']);
			if ($idUser) {
				$listObj->setFilter('doc.idUser', '=', $idUser);
			} elseif ($idOffice) {
				$listObj->setFilter('doc.idOffice', '=', $idOffice);
			}
		}
		// doc type
		if ($filters['docType']) {
			$listObj->setFilter('doc.docType', '=', $filters['docType']);
		}
		// doc currency
		if ($filters['currency']) {
			$listObj->setFilter('doc.currency', '=', $filters['currency']);
		}
		// doc voided
		if ($filters['voided'] == 'voided') {
			$listObj->setFilter('doc.voidTime', '>', 0);
		}
		elseif ($filters['voided'] == 'notVoided') {
			$listObj->setFilter('doc.voidTime', '=', 0);
		}
		// doc issued
		if ($filters['issued'] == 'issued') {
			$listObj->setFilter('doc.issueTime', '>', 0);
		}
		elseif ($filters['issued'] == 'notIssued') {
			$listObj->setFilter('doc.issueTime', '=', 0);
		}
		// doc blocked
		if ($filters['blocked'] == 'blocked') {
			$listObj->setFilter('doc.blockTime', '>', 0);
		}
		elseif ($filters['blocked'] == 'notBlocked') {
			$listObj->setFilter('doc.blockTime', '=', 0);
		}
		// bank account
		if ($filters['idBankAccount']) {
			$listObj->setFilter('doc.idBankAccount', '=', $filters['idBankAccount']);
		}
		// inv date
		if ($filters['invDate']['start'] and $filters['invDate']['end']) {
			$listObj->setFilter('inv.invoiceDate', 'BETWEEN', [$filters['invDate']['start'], $filters['invDate']['end']]);
		} elseif ($filters['invDate']['start']) {
			$listObj->setFilter('inv.invoiceDate', '>=', $filters['invDate']['start']);
		} elseif ($filters['invDate']['end']) {
			$listObj->setFilter('inv.invoiceDate', '<=', $filters['invDate']['end']);
		}
		// invoice number
		if ($filters['invNumber']) {
			$listObj->setFilter('inv.invoiceNumber', '=', $filters['invNumber']);
		}
		// text filter
		if ($filters['text']) {
			$listObj->setCustomFilter("doc.explanations LIKE ? OR doc.comments LIKE ?", ["%{$filters['text']}%", "%{$filters['text']}%"]);
		}
		// id imported doc
		if ($filters['idImpDoc']) {
			$listObj->setFilter('doc.idImportedDoc', '=', $filters['idImpDoc']);
		}
		// imported document
		if ($filters['documentImported']=='yes') {
			$listObj->setFilter('doc.idImportedDoc', '!=', '0');
		} elseif ($filters['documentImported']=='no') {
			$listObj->setFilter('doc.idImportedDoc', '=', '0');
		}

		// document add date
		if ($filters['docAddDate']['start']) {
			$start = \Engine\DateTime::createFromIsoDateOrDatetime(sprintf('%s 00:00:00', $filters['docAddDate']['start']))->getTimestamp();
			$listObj->setFilter('doc.addTime', '>=', $start);
		}
		if ($filters['docAddDate']['end']) {
			$end = \Engine\DateTime::createFromIsoDateOrDatetime(sprintf('%s 23:59:59',$filters['docAddDate']['end']))->getTimestamp();
			$listObj->setFilter('doc.addTime', '<=', $end);
		}

		if ($filters['customer']['id']) {
			$listObj->setFilter('inv.idCustomer', 'IN', \Entity\Customer::select($filters['customer']['id'])->fullDuplicatedCustomersGroup);
		}

		if ($filters['corporate']['id']) {
			$listObj->setFilter('inv.idCorporate', 'IN', \Entity\Corporate::select($filters['corporate']['id'])->fullDuplicatedCorporatesGroup);
		}

		$listObj->applyUserRestriction(true);
		$listObj = $this->entityList_inheritListingParams($listingParams, $listObj);

		// these must be called in this specific order
		$this->recordsList = $listObj->exportRecords();
		$this->totalRecordsCount = $listObj->getRecordsCount();
	}

}
