<?php

namespace App\GenDocument\SpecialObject;

class InvoiceCashingsTable extends TableAbstract
{
	/**
	 * Populates the table content.
	 */
	protected function table_populateRows()
	{
		$invCashingsList = \Entity\Invoice::select($this->idPivot)->cashingDocumentsList;

		// GET THE INVOICE CASHINGS ROWS
		$invoiceCashingsRows = [];
		$currentNumber = 0;

		foreach ($invCashingsList as $thisCashing) {

			$currentNumber++;
			$docType = \App\Config\CashingDocuments::getInstance()->getName($thisCashing['docType']);

			$docSeriesCode = \Entity\DocumentSeries::select($thisCashing['idCashingSeries'])->code;
			if ($docSeriesCode) {
				$docSeriesAndNumber = $docSeriesCode." / ".($thisCashing['docNumber']?:"-");
			}
			else {
				$docSeriesAndNumber = $thisCashing['docNumber']?:"-";
			}

			$docDate = $thisCashing['docDate'];
			$docValDocCurr = $thisCashing['value'];
			$docValInvCurr = $thisCashing['invoiceCashedValue'];
			$docValInDefCurr = \App\CurrencyExchange\Rates::getInstance($thisCashing['docDate'])
				->convert($thisCashing['value'], $thisCashing['currency'], \App\Config\Currencies::getInstance()->getDefaultCurrency());

			$invoiceCashingsRows[] = [
				'currentNumber' =>		['content' => $currentNumber, 'type' => 'number',],
				'docType'	=>			['content' => $docType, 'type' => 'string',],
				'docSeriesAndNumber'=>	['content' => $docSeriesAndNumber, 'type' => 'string',],
				'docDate'	=>			['content' => $docDate, 'type' => 'date',],
				'docValDocCurr'	=>		['content' => $docValDocCurr, 'type' => 'price',],
				'docValInvCurr'	=>		['content' => $docValInvCurr, 'type' => 'price',],
				'docValDefCurr'	=>		['content' => $docValInDefCurr, 'type' => 'price',],
			];

		}

		$this->tableRows = $invoiceCashingsRows;
	}

}
