<?php
namespace App\Galileo\StructureParser\Structures\RefundRaw;

class Price extends \App\Galileo\StructureParser\Structures\Base\Price
{
	protected function _getData()
	{
		list($parsedStructure, , , ) = $this->getTransactionDetails();

		$item = new \App\Galileo\StructureParser\Types\Price();

		$a23 = $parsedStructure['A23'];

		// handle total
		$item->componentSet(new \App\Utils\BaseParser\Structure\StructureParser\Types\PriceComponent(
			'total',
			'A23RAA',
			-1*$a23['A23RA']['A23RAA'],
			null,
			$a23['A23RA']['A23RAC']
		));

		// handle taxes
		if ($a23['A23ERT']) {
			$this->transaction->getMessagesCollector()->addInfo(sprintf("[%s] Loading the taxes from the A23ERT tag", __METHOD__));

			foreach ($a23['A23ERT'] as $A23ERT) {
				$item->tax->append(new \App\Galileo\StructureParser\Types\PriceTaxComponent(
					$A23ERT['tax'],
					-1*$A23ERT['amount'],
					$a23['A23RA']['A23RAC']
				));
			}
			
		}
		else {
			$this->transaction->getMessagesCollector()->addInfo(sprintf("[%s] Loading the taxes from the A23BF tag", __METHOD__));
			if ($a23['A23BF']['taxes']['A07TT1']) {
				$item->tax->append(new \App\Galileo\StructureParser\Types\PriceTaxComponent(
					$a23['A23BF']['taxes']['A07TT1']['tax'],
					-1*$a23['A23BF']['taxes']['A07TT1']['amount'],
					$a23['A23BF']['A23CRB']
				));
			}

			if ($a23['A23BF']['taxes']['A07TT2']) {
				$item->tax->append(new \App\Galileo\StructureParser\Types\PriceTaxComponent(
					$a23['A23BF']['taxes']['A07TT2']['tax'],
					-1*$a23['A23BF']['taxes']['A07TT2']['amount'],
					$a23['A23BF']['A23CRB']
				));
			}

			if ($a23['A23BF']['taxes']['A07TT3']) {
				$item->tax->append(new \App\Galileo\StructureParser\Types\PriceTaxComponent(
					$a23['A23BF']['taxes']['A07TT3']['tax'],
					-1*$a23['A23BF']['taxes']['A07TT3']['amount'],
					$a23['A23BF']['A23CRB']
				));
			}

			if ($a23['A23IT']) {
				foreach ($a23['A23IT'] as $A23IT) {
					if (!$A23IT['exempt']) {
						$item->tax->append(new \App\Galileo\StructureParser\Types\PriceTaxComponent(
							$A23IT['tax'],
							-1*$A23IT['amount'],
							$a23['A23BF']['A23CRB']
						));
					}
				}
			}
		}

		// handle penalty
		$item->componentSet(new \App\Utils\BaseParser\Structure\StructureParser\Types\PriceComponent(
			'penalty',
			'A23PFA',
			$a23['A23PF']['A23PFA'],
			null,
			$a23['A23RA']['A23RAC']
		));

		// handle supplier
		$item->componentSet(new \App\Utils\BaseParser\Structure\StructureParser\Types\PriceComponent(
			'supplier',
			'sub',
			$item->total - $item->tax->getAmount() - $item->penalty,
			null,
			$item->totalCurrency
		));

		// DEBUG: enable these
		// echo "<div style='font-size: 1.5em; margin-top: 0.5em; padding-left: 0.5em; border-left: 0.25em solid hsl( 10, 100%, 50%)'>{$this->transaction->getType()} {$this->transaction->documents->docNumber}";
		// echo $item->componentsDebug();
		// echo "</div>";

		return $item;
	}

}
