<?php
 namespace Modules\Admin\ImportEntities\Model\Import; class OfferProfileRulesUpdate extends \Modules\Admin\ImportEntities\Model\ImportAbstract { protected $fields = [ 'id', 'name', 'active', 'priority', 'commissionIncluded', 'commissionValue', 'commissionCurrency', 'commissionPercent', 'serviceFeeMode', 'serviceFeeAppliedFrom', 'serviceFeeValue', 'serviceFeeCurrency', 'serviceFeePercent', 'discountPercentSource', 'discountValue', 'discountCurrency', 'discountPercent', ]; public function process() { $_LANG = \App\Loader_Lang::getInstance()->getGlobal(); $messagesFromImport = new \App\MessagesCollector(); $count = 0; foreach($this->fileData as $row) { $count++; try { \DB::startTransaction(); $offerProfileRuleOldDetailsEnt = \Entity\OfferProfileRule::selectForUpdate($row['id']); $offerProfileRuleEnt = \Entity\OfferProfileRule::update($row['id']); if (strlen($row['name'])) { $offerProfileRuleEnt->name = $row['name']; } if (strlen($row['active'])) { $offerProfileRuleEnt->active = $row['active']; } if (strlen($row['priority'])) { $offerProfileRuleEnt->priority = $row['priority']; } if (strlen($row['commissionPercent']) or strlen($row['commissionValue'])) { $offerProfileRuleEnt->commissionDetails = $this->createOfferDetailsCommission($row); } else { $offerProfileRuleEnt->commissionDetails = $offerProfileRuleOldDetailsEnt->commissionDetails; } if ($row['serviceFeeMode']) { $offerProfileRuleEnt->serviceFeeDetails = $this->createOfferDetailsServiceFee($row); } else { $offerProfileRuleEnt->serviceFeeDetails = $offerProfileRuleOldDetailsEnt->serviceFeeDetails; } if ($row['discountPercentSource']) { $offerProfileRuleEnt->discountDetails = $this->createOfferDetailsDiscount($row); } else { $offerProfileRuleEnt->discountDetails = $offerProfileRuleOldDetailsEnt->discountDetails; } $offerProfileRuleEnt->save(); \DB::commitTransaction(); $messagesFromImport->addSuccess(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['updateOfferProfileRules']['saveOk'], $row['id'])); } catch (\App\Entity\Exception $e) { \DB::rollbackTransaction(); $messagesFromImport->addWarning(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['updateOfferProfileRules']['saveError'], $row['id'])); $messagesFromImport->appendMessages($e->getMessagesCollector()); } } return $messagesFromImport; } private function createOfferDetailsCommission(Array $row) { $commissionDet = new \App\OfferProfileRules\OfferDetails_Commission(); $commissionDet->included = $row['commissionIncluded']; if (strlen($row['commissionPercent'])) { $commissionDet->percent = $row['commissionPercent']; } elseif (strlen($row['commissionValue'])) { $commissionDet->currency = $row['commissionCurrency']; $commissionDet->value = $row['commissionValue']; } return $commissionDet; } private function createOfferDetailsServiceFee(Array $row) { $serviceFeeDet = new \App\OfferProfileRules\OfferDetails_ServiceFee(); $serviceFeeDet->mode = $row['serviceFeeMode']; $serviceFeeDet->percentSource = $row['serviceFeeAppliedFrom']; if ($row['serviceFeeMode'] == 'percent') { $serviceFeeDet->percent = $row['serviceFeePercent']; } else { $serviceFeeDet->currency = $row['serviceFeeCurrency']; $serviceFeeDet->value = $row['serviceFeeValue']; } return $serviceFeeDet; } private function createOfferDetailsDiscount(Array $row) { $discountDet = new \App\OfferProfileRules\OfferDetails_Discount(); $discountDet->percentSource = $row['discountPercentSource']; if (strlen($row['discountPercent'])) { $discountDet->percent = $row['discountPercent']; } elseif (strlen($row['discountValue'])) { $discountDet->value = $row['discountValue']; $discountDet->currency = $row['discountCurrency']; } return $discountDet; } } 