<?php
 namespace App\OfferProfileRules; class ApplyProfile { protected $commissionDetails; protected $discountDetails; protected $serviceFeeDetails; public function setCommissionDetails(OfferDetails_Commission $commissionDetails) { $this->commissionDetails = $commissionDetails; } public function setDiscountDetails(OfferDetails_Discount $discountDetails) { $this->discountDetails = $discountDetails; } public function setServiceFeeDetails(OfferDetails_ServiceFee $serviceFeeDetails) { $this->serviceFeeDetails = $serviceFeeDetails; } public function populateOfferFields(\Entity\Offer $offer, \Entity\OfferItem $offerItem) { if ($offer->getOperation()!='insert' and $offer->getOperation()!='update') { throw new \Exception('The offer operation must be insert/update.'); } if ($offerItem->getOperation()!='insert' and $offerItem->getOperation()!='update') { throw new \Exception('The offer item operation must be insert/update.'); } $this->populateOfferFields_commission($offer, $offerItem); $this->populateOfferFields_serviceFee($offer, $offerItem); $this->populateOfferFields_discount($offer, $offerItem); } private function populateOfferFields_commission(\Entity\Offer $offer, \Entity\OfferItem $offerItem) { if (!$this->commissionDetails) { return ; } $offer->commissionable = $this->commissionDetails->included; if ($this->commissionDetails->percent!==null) { $offerItem->commissionPercent = $this->commissionDetails->percent; $offerItem->commissionInitialValue = null; } elseif ($this->commissionDetails->currency) { $offerItem->commissionInitialCurrency = $this->commissionDetails->currency; $offerItem->commissionInitialValue = $this->commissionDetails->value; } } private function populateOfferFields_serviceFee(\Entity\Offer $offer, \Entity\OfferItem $offerItem) { if (!$this->serviceFeeDetails) { return ; } if ($this->serviceFeeDetails->mode) { $offer->serviceFeeMode = $this->serviceFeeDetails->mode; } if ($this->serviceFeeDetails->percentSource) { $offer->serviceFeePercentSource = $this->serviceFeeDetails->percentSource; } if (in_array($this->serviceFeeDetails->mode, ['value', 'segment', 'duration'])) { $offerItem->serviceFeeInitialValue = (Float)$this->serviceFeeDetails->value; $offerItem->serviceFeeInitialCurrency = $this->serviceFeeDetails->currency; } if ($this->serviceFeeDetails->mode=='percent') { $offerItem->serviceFeePercent = $this->serviceFeeDetails->percent; $offerItem->serviceFeeInitialValue = null; } } private function populateOfferFields_discount(\Entity\Offer $offer, \Entity\OfferItem $offerItem) { if (!$this->discountDetails) { return ; } $offerItemFields = $offerItem->getFields(); if ($this->discountDetails->percentSource) { $offer->discountPercentSource = $this->discountDetails->percentSource; } if ($this->discountDetails->percent!==null) { $offerItem->discountPercent = $this->discountDetails->percent; $offerItem->discountInitialCurrency = $offerItemFields['supplierInitialCurrency']; $offerItem->discountInitialValue = null; } else { $offerItem->discountInitialValue = $this->discountDetails->value; $offerItem->discountInitialCurrency = $this->discountDetails->currency; } } } 