<?php

namespace Modules\Admin\ImportEntities\Model\Import;

class ImportedServicesRefund extends \Modules\Admin\ImportEntities\Model\ImportAbstract
{

	/**
	 * The fields that can be imported for this entity
	 * @var array
	 */
	protected $fields = [
		'serviceId', 'refundDate', 'issueUser', 'issueOffice', 'formOfPayment',
		'currency', 'fareUsedSupplierValue', 'fareUsedCommissionValue', 'fareUsedTaxValue', 'fareUsedTaxDetails', 'penaltyValue', 'serviceFeeValue', 'returnOriginalServiceFee',
	];


	/**
	 * This entity uses the date format
	 * @var Boolean
	 */
	protected $useDateFormat = true;


	/**
	 *
	 * {@inheritDoc}
	 * @see \Modules\Admin\ImportEntities\Model\ImportAbstract::process()
	 */
	public function process()
	{
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		$messagesFromImport = new \App\MessagesCollector();

		// LOOP THROUGH EACH ROW
		foreach ($this->fileData as $row) {

			try {

				\DB::startTransaction();

				$importedOrderObj = new \App\ImportedOrder\Order();
				$this->populateImportedOrder($row, $importedOrderObj);

				$importedOrderEnt = \Entity\ImportedOrder::insert();
				$importedOrderEnt->order = $importedOrderObj;

				$importedOrderEnt->save();

				\DB::commitTransaction();
				$messagesFromImport->addSuccess( sprintf( $_LANG['Admin']['ImportEntities']['index']['msgs']['impServicesRefunds']['saveOk'], $row['serviceId'], $importedOrderEnt->getId() ) );

			} catch (\App\Entity\Exception $e) {
				$messagesFromImport->addWarning(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['impServicesRefunds']['saveError'], $row['serviceId']));
				$messagesFromImport->appendMessages($e->getMessagesCollector());
				\DB::rollbackTransaction();
			}
		}

		return $messagesFromImport;

	}

	/**
	 *
	 * @param array $row
	 * @param \App\ImportedOrder\Order $importedOrderObj
	 */
	protected function populateImportedOrder(array $row, \App\ImportedOrder\Order $importedOrderObj)
	{

		// config
		$CFG  = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// MAIN IO OBJECTS //////////////////////////////////////////////////////////////////
		$importedOrderObj->externalSystemCode = $CFG['Admin']['ImportEntities']['ImportedServicesInsert']['system']['default'];

		$importedServiceObj = new \App\ImportedOrder\Service();
		$importedServiceObj->idSystem = $row['serviceId'];
		$importedServiceObj->operation = 'refund';
		$importedServiceObj->forceOperation =	false;

		// DEFINE AND POPULATE THE REFUND DETAILS /////////////////////////////////////////////////

		$refundDetailsObj = new \App\ImportedOrder\RefundDetails();

		$refundDetailsObj->date = \Engine\DateTime::createFromFormat($this->dateFormat, $row['refundDate'])->formatDateIso();

		// issue user
		if ($row['issueUser']) {
			$issueUserObj = new \App\ImportedOrder\User();
			$issueUserObj->idSystem = $row['issueUser'];
			$refundDetailsObj->issueUser = $issueUserObj;
		}

		// issue office
		if ($row['issueOffice']) {
			$issueOfficeObj = new \App\ImportedOrder\Office();
			$issueOfficeObj->idSystem = $row['issueOffice'];
			$refundDetailsObj->issueOffice = $issueOfficeObj;
		}

		// STANDARD FIELDS ------------------------------------------------------------------------
		if (strlen($row['formOfPayment'])) {
			$refundDetailsObj->formOfPayment = $row['formOfPayment'];
		}
		if ($row['currency']) {
			$refundDetailsObj->currency = $row['currency'];
		}
		if (strlen($row['fareUsedSupplierValue'])) {
			$refundDetailsObj->fareUsedSupplierValue = $row['fareUsedSupplierValue'];
		}
		if (strlen($row['fareUsedCommissionValue'])) {
			$refundDetailsObj->fareUsedCommissionValue = $row['fareUsedCommissionValue'];
		}
		if (strlen($row['fareUsedTaxValue'])) {
			$refundDetailsObj->fareUsedTaxValue = $row['fareUsedTaxValue'];
		}
		if (strlen($row['penaltyValue'])) {
			$refundDetailsObj->penaltyValue = $row['penaltyValue'];
		}
		if (strlen($row['serviceFeeValue'])) {
			$refundDetailsObj->serviceFeeValue = $row['serviceFeeValue'];
		}
		if (strlen($row['returnOriginalServiceFee'])) {
			$refundDetailsObj->returnOriginalServiceFee = (bool)$row['returnOriginalServiceFee'];
		}
		if ($row['fareUsedTaxDetails']) {
			foreach ( explodeClean('/', $row['fareUsedTaxDetails']) as $thisTax) {
				$thisTaxClean = trim($thisTax);
				$taxCode = substr($thisTaxClean, 0, 2);
				$taxValue = substr($thisTaxClean, 2);
				$refundDetailsObj->addFareUsedTaxCode($taxCode, $taxValue, $row['currency']);
			}
		}

		// FINISH THE IMPORTED ORDER POPULATION ---------------------------------------------------

		// add refund details to the service
		$importedServiceObj->refundDetails = $refundDetailsObj;

		// add the service to the imported order
		$importedOrderObj->addService($importedServiceObj);

	}

}
