<?php
 namespace Modules\RemoteData\EntityManagementWs\Model\Entity\SupplierInvoice; class Update extends \Modules\RemoteData\EntityManagementWs\Model\Entity\UpdateAbstract { protected $entityFields = [ 'idSupplier', 'invoiceNumber', 'invoiceDate', 'currency', 'dueDate', 'fulfillmentDate', 'invoiceSeries', 'formOfPayment', 'invoiceComments', 'remarks', ]; private $invoiceRowEntityFields = [ 'serviceCode', 'description', 'rowType', 'externalReservationId', 'externalReservationDate', 'confirmationNumber', 'idPassenger', 'checkIn', 'checkOut', 'contractNumber', 'ticketNo', 'idSupplierProduct', 'countryCode', 'cityId', 'reservationCode', 'reservationNumber', 'reservationId', 'idService', 'measureUnit', 'quantity', 'priceValue', 'vatValue', 'vatText' ]; protected function initEntityObject() : \App\Entity\GeneralAbstract { return \Entity\SupplierInvoice::update($this->id); } protected function populateEntityFields(\App\Entity\GeneralAbstract $entityObj) { parent::populateEntityFields($entityObj); if (property_exists($this->request, 'rows')) { $suppInvEnt = \Entity\SupplierInvoice::selectForUpdate($this->id); $currentEntityRowsIdsList = array_combine(array_keys($suppInvEnt->rowsList), array_keys($suppInvEnt->rowsList)); $finalEntityRows = []; $reqRowsList = $this->request->rows; foreach ($reqRowsList as $rowDetails) { switch ($rowDetails->operation) { case 'insert': $finalEntityRows[] = $this->insertRow($rowDetails); break; case 'update': if ($currentEntityRowsIdsList[$rowDetails->id]) { $finalEntityRows[] = $this->updateRow($rowDetails); unset($currentEntityRowsIdsList[$rowDetails->id]); } else { throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException(sprintf("Invalid invoice row id received for `update` operation (id: %s)", $rowDetails->id)); } break; case 'delete': if ($currentEntityRowsIdsList[$rowDetails->id]) { $finalEntityRows[] = $this->deleteRow($rowDetails); unset($currentEntityRowsIdsList[$rowDetails->id]); } else { throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException(sprintf("Invalid invoice row id received for `delete` operation (id: %s)", $rowDetails->id)); } break; default: throw new \Modules\RemoteData\EntityManagementWs\Model\RuntimeException(sprintf("Unsupported operation (%s) received for row id: %s", $rowDetails->operation, $rowDetails->id)); } } if ($currentEntityRowsIdsList) { foreach ($currentEntityRowsIdsList as $supplierInvoiceRowId) { $currentRow = new \stdClass(); $currentRow->id = $supplierInvoiceRowId; $finalEntityRows[] = $this->updateRow($currentRow); } } $entityObj->rows = $finalEntityRows; } } private function insertRow(\stdClass $rowDetails) : \Entity\SupplierInvoiceRow { $rowEnt = \Entity\SupplierInvoiceRow::insert(); foreach ($this->invoiceRowEntityFields as $entityField => $requestField) { if (is_numeric($entityField)) { if (property_exists($rowDetails, $requestField)) { $rowEnt->$requestField = $rowDetails->$requestField; } } else { if (property_exists($rowDetails, $requestField)) { $rowEnt->$entityField = $rowDetails->$requestField; } } } return $rowEnt; } private function updateRow(\stdClass $rowDetails) : \Entity\SupplierInvoiceRow { $rowEnt = \Entity\SupplierInvoiceRow::update($rowDetails->id); $rowEnt->importOriginalFields(); foreach ($this->invoiceRowEntityFields as $entityField => $requestField) { if (is_numeric($entityField)) { if (property_exists($rowDetails, $requestField)) { $rowEnt->$requestField = $rowDetails->$requestField; } } else { if (property_exists($rowDetails, $requestField)) { $rowEnt->$entityField = $rowDetails->$requestField; } } } return $rowEnt; } private function deleteRow(\stdClass $rowDetails) : \Entity\SupplierInvoiceRow { $rowEnt = \Entity\SupplierInvoiceRow::delete($rowDetails->id); return $rowEnt; } } 