<?php
namespace App\EntityClone;

/**
 * this class attach information to an empty Offer entity, from another offer
 * it is important to attach empty offer items to the empty order
 *
 *  $offerNewEnt = \Entity\Offer::insert();
 *	$offerItemArr = [];
 *	foreach ($offerOldEnt->offerItems as $value){
 *		$offerItemArr[] = \Entity\OfferItem::insert();
 *	}
 *	$offerNewEnt->offerItems = $offerItemArr;
 *
 * @author dcs
 *
 */

class Offer extends \App\EntityClone\BaseAbstract
{

	protected function attachFields()
	{

		$offerItemsNew = $this->entityNew->getField('offerItems');

		// Old Offer
		$entityOld 	= \Entity\Offer::select($this->id);
		$offerItemsOld = $entityOld->offerItems;

		// VALIDATE  OFFER ITEMS //////////////////////////////////////////////////////////////////
		if ( count($offerItemsNew) != count($offerItemsOld) ) {
			throw new \Exception('Offer items are not correctly set.');
		}

		// POPULATE THE OFFER FIELDS //////////////////////////////////////////////////////////////
		$this->entityNew->idSupplier  			= $entityOld->idSupplier;
		$this->entityNew->productName			= $entityOld->productName;
		$this->entityNew->currencyType			= $entityOld->currencyTypeCode;
		$this->entityNew->currencyDate			= $entityOld->currencyDate;
		$this->entityNew->comments				= $entityOld->comments;
		$this->entityNew->offerCurrency			= $entityOld->offerCurrency;
		$this->entityNew->offerType				= $entityOld->offerType;
		$this->entityNew->serviceCode			= $entityOld->serviceCode;
		$this->entityNew->location				= $entityOld->locationCode;

		$this->entityNew->totalCurrency			= $entityOld->totalCurrency;
		$this->entityNew->expirationDate		= $entityOld->expirationDate;
		$this->entityNew->optionDate			= $entityOld->optionDate;
		$this->entityNew->idSupplierProduct		= $entityOld->idSupplierProduct;

		$this->entityNew->itinerary					= $entityOld->itinerary;
		$this->entityNew->departureCountryCode 		= $entityOld->departureCountryCode;
		$this->entityNew->departureCityId			= $entityOld->departureCityId;
		$this->entityNew->departureStationCode		= $entityOld->departureStationCode;
		$this->entityNew->destinationCountryCode	= $entityOld->destinationCountryCode;
		$this->entityNew->destinationCityId			= $entityOld->destinationCityId;
		$this->entityNew->destinationStationCode 	= $entityOld->destinationStationCode;

		$this->entityNew->ignoreSupplierOnDocuments	= $entityOld->ignoreSupplierOnDocuments;

		$this->entityNew->startDatetime		= $entityOld->startDatetime;
		if (!$entityOld->endDatetime->isEmpty()) {
			$this->entityNew->endDatetime	= $entityOld->endDatetime;
		}

		$this->entityNew->formOfPayment			= $entityOld->formOfPayment;
		$this->entityNew->paymentCcEntityType	= $entityOld->paymentCcEntityType;
		$this->entityNew->paymentCcEntityId		= $entityOld->paymentCcEntityId;
		$this->entityNew->paymentCcApprovalCode	= $entityOld->paymentCcApprovalCode;
		$this->entityNew->paymentCcValue		= (float)$entityOld->paymentCcValue;
		$this->entityNew->paymentCcCurrency		= $entityOld->paymentCcCurrency;

		$this->entityNew->formOfCashing			= $entityOld->formOfCashing;
		$this->entityNew->cashingCcEntityType	= $entityOld->cashingCcEntityType;
		$this->entityNew->cashingCcEntityId		= $entityOld->cashingCcEntityId;
		$this->entityNew->cashingCcApprovalCode	= $entityOld->cashingCcApprovalCode;

		$this->entityNew->calculationType	= $entityOld->calculationType;

		// supplier
		$this->entityNew->supplierVatIncluded	= $entityOld->supplierVatIncluded;

		// commission
		$this->entityNew->commissionVatIncluded	= $entityOld->commissionVatIncluded;
		$this->entityNew->commissionable		= $entityOld->commissionable;

		// discount
		$this->entityNew->discountVatIncluded	= $entityOld->discountVatIncluded;
		$this->entityNew->discountPercentSource	= $entityOld->discountPercentSource;

		// serviceFee
		$this->entityNew->serviceFeeVatIncluded		= $entityOld->serviceFeeVatIncluded;
		$this->entityNew->serviceFeeMode			= $entityOld->serviceFeeMode;
		$this->entityNew->serviceFeeCurrency		= $entityOld->serviceFeeCurrency;
		$this->entityNew->serviceFeePercentSource	= $entityOld->serviceFeePercentSource;

		// tax
		$this->entityNew->taxVatIncluded	= $entityOld->taxVatIncluded;

		// confirmation number
		$this->entityNew->confirmationNumber = $entityOld->confirmationNumber;

		// externalSystemReservationId and externalSystemReservationDate
		$this->entityNew->externalSystemReservationId	= $entityOld->externalSystemReservationId;
		$this->entityNew->externalSystemReservationDate	= $entityOld->externalSystemReservationDate;


		// POPULATE OFFER ITEMS ///////////////////////////////////////////////////////////////////
		reset($offerItemsNew);

		if ($offerItemsOld) {
			foreach ($offerItemsOld as $idOfferItem => $oi) {

				$offerItemOldEnt = \Entity\OfferItem::select($idOfferItem);
				$offerItemNewEnt = current($offerItemsNew);

				$offerItemNewEnt->idCustomer 		= $offerItemOldEnt->idCustomer;
				$offerItemNewEnt->customerType		= $offerItemOldEnt->customerType;
				$offerItemNewEnt->productMultiplier	= $offerItemOldEnt->productMultiplier;
				$offerItemNewEnt->quantity			= $offerItemOldEnt->quantity;

				// supplier
				$offerItemNewEnt->supplierInitialValue		= $offerItemOldEnt->supplierInitialValue;
				$offerItemNewEnt->supplierInitialVat		= $offerItemOldEnt->supplierInitialVat;
				$offerItemNewEnt->supplierInitialCurrency	= $offerItemOldEnt->supplierInitialCurrency;

				// commission
				$offerItemNewEnt->commissionInitialValue		= $offerItemOldEnt->commissionInitialValue;
				$offerItemNewEnt->commissionInitialCurrency 	= $offerItemOldEnt->commissionInitialCurrency;

				// discount
				if ($offerItemOldEnt->discountInitialCurrency) {
					$offerItemNewEnt->discountInitialValue  			= $offerItemOldEnt->discountInitialValue;
					$offerItemNewEnt->discountInitialCurrency			= $offerItemOldEnt->discountInitialCurrency;
				}

				// serviceFee
				if ($offerItemOldEnt->serviceFeeInitialCurrency) {
					$offerItemNewEnt->serviceFeeInitialValue	= $offerItemOldEnt->serviceFeeInitialValue;
					$offerItemNewEnt->serviceFeeInitialCurrency	= $offerItemOldEnt->serviceFeeInitialCurrency;
				}

				//  tax
				if ($offerItemOldEnt->taxInitialCurrency) {
					$offerItemNewEnt->taxInitialValue	= $offerItemOldEnt->taxInitialValue;
					$offerItemNewEnt->taxInitialVat  	= $offerItemOldEnt->taxInitialVat;
					$offerItemNewEnt->taxInitialCurrency= $offerItemOldEnt->taxInitialCurrency;
				}

				next($offerItemsNew);
			}
		}
	}
}
