<?php

namespace App\GenDocument\SpecialObject;

class InvoiceVatDetails extends TableAbstract
{

	/**
	 * Populates the table content.
	 */
	protected function table_populateRows() {

		global $CFG;

		// the invoice rows list
		$invoiceRows=\Entity\Invoice::select($this->idPivot)->rowsList;

		$invoiceCurrency=\Entity\Invoice::select($this->idPivot)->currency;
		$invoiceDate=\Entity\Invoice::select($this->idPivot)->invoiceDate;

		// GET THE INVOICE ROWS VATS (GROUPED) ////////////////////////////////////////////////////
		$groupedVat=[];
		foreach($invoiceRows as $thisRow) {
			// the current key
			$k=$thisRow['vatText'];
			// SIMPLE COLUMNS ---------------------------------------------------------------------
			$groupedVat[$k]['vatQuota'] =		$thisRow['vatText'];
			$groupedVat[$k]['value'] += 		$thisRow['pricePrintValue'];
			$groupedVat[$k]['vat'] += 			$thisRow['vatPrintValue'];
			$groupedVat[$k]['totalAmount'] += 	$thisRow['pricePrintValue']+$thisRow['vatPrintValue'];

			// DETERMINE VAT BASE COLUMN ----------------------------------------------------------
			if ($this->tableColumns['vatBase']) {
				$groupedVat[$k]['vatBase'] += \Entity\InvoiceRow::select($thisRow['id'])->vatBasePrintValue;
			}
		}

		// RETURN THE ROWS IN THE SPECIAL OBJECT'S FORMAT /////////////////////////////////////////
		$rows=[];

		foreach($groupedVat as $vatDetails) {
			// determine some values in default curr
			$valInDefCurr=\App\CurrencyExchange\Rates::getInstance($invoiceDate)->convert($vatDetails['value'], $invoiceCurrency, $CFG['currencies']['defaultCurrency']);
			$vatInDefCurr=\App\CurrencyExchange\Rates::getInstance($invoiceDate)->convert($vatDetails['vat'], $invoiceCurrency, $CFG['currencies']['defaultCurrency']);
			$ttlInDefCurr=\App\CurrencyExchange\Rates::getInstance($invoiceDate)->convert($vatDetails['totalAmount'], $invoiceCurrency, $CFG['currencies']['defaultCurrency']);

			$replacements = [
				'%invoiceVatDetails_vatQuota%' => $vatDetails['vatQuota'],
			];

			$vatDetails['prefix'] = str_replace(array_keys($replacements), array_values($replacements), $this->settings['column_prefixValue']);

			$vatColValue = $vatDetails['vat'];
			$vatBaseColValue = $vatDetails['vatBase'];
			$vatDefCurrColValue = $vatInDefCurr;
			$vatColType  = 'price';

			// show VAT 0 as blank		@JIRA TD-10112
			if ($this->settings['options_zeroVatBlank']['blank'] && $vatDetails['vat']==0) {
				$vatColValue = '';
				$vatBaseColValue = '';
				$vatDefCurrColValue = '';
				$vatColType = 'string';
			}

			// create row info
			$row = [
				'prefix'		=>			['content'=>$vatDetails['prefix'], 		'type'=>'string'],
				'vatQuota'		=>			['content'=>$vatDetails['vatQuota'], 	'type'=>'price'],
				'value'			=>			['content'=>$vatDetails['value'], 		'type'=>'price'],
				'vatBase'		=>			['content'=>$vatBaseColValue, 			'type'=>$vatColType],
				'vat'			=>			['content'=>$vatColValue, 				'type'=>$vatColType],
				'totalAmount'	=>			['content'=>$vatDetails['totalAmount'], 'type'=>'price'],
				'valueInDefCurrency'	=>	['content'=>$valInDefCurr, 				'type'=>'price'],
				'vatInDefCurrency'		=>	['content'=>$vatDefCurrColValue, 		'type'=>$vatColType],
				'totalAmountInDefCurrency'=>['content'=>$ttlInDefCurr, 				'type'=>'price'],
			];

			// associate this row
			$rows[]=$row;
		}

		// ASSOCIATE ALL INVOICE ROWS /////////////////////////////////////////////////////////////
		$this->tableRows=$rows;

	}

	/**
	 * Add support for disabling cell borders for thre prefix column
	 * */
	protected function render_table_setTableBorderAttributes() {
		parent::render_table_setTableBorderAttributes();

		if($this->settings['column_prefixBorders']=='none') {
			foreach($this->table->children as $tr) {
				foreach($tr->children as $tdIndex=>$td) {
					if($tdIndex==0) {
						$td->setAttribute('border_color', '#FFFFFF');
					}
				}
			}
		}
	}

}
