<?php
 namespace App\Utils\InvoiceDueDate; abstract class GeneralAbstract { protected $idEntity; protected $profile = null; public function __construct($idEntity) { $this->idEntity = $idEntity; } public function getProfile() { if ($this->profile===null) { $this->profile = false; $entPrefObj = $this->getEntityPreferenceObject(); $profileArrSer = $entPrefObj->invoiceDueDate; if ($profileArrSer) { $profileArr = unserialize($profileArrSer); if (!$profileArr) { throw new \RuntimeException(sprintf('Invalid entity preference for due date profile for `%s`.', get_called_class())); } $this->profile = new \App\Utils\InvoiceDueDate\Profile(); $this->profile->importFromArray($profileArr); } } return $this->profile; } public function setProfile(\App\Utils\InvoiceDueDate\Profile $profile) { $profileArr = $profile->exportAsArray(); $entPrefObj = $this->getEntityPreferenceObject(); $entPrefObj->invoiceDueDate = serialize($profileArr); $this->profile = $profile; } public function unsetProfile() { $entPrefObj = $this->getEntityPreferenceObject(); unset($entPrefObj->invoiceDueDate); $this->profile = false; } public function getMinimalDueDays() { $profile = $this->getProfile(); $minDueDays = NULL; if ($this->getProfile()) { $profile = $this->getProfile(); } else { $profile = $this->getDefaultProfile(); } foreach ($profile->getElements() as $element) { if ($element->getType() == 'intervals') { foreach($element->getIntervals() as $interval) { if (!$minDueDays or $minDueDays > $interval['offset']) { $minDueDays = $interval['offset']; } } } elseif ($element->getType() == 'days') { if (!$minDueDays or $minDueDays > $element->getOffset()) { $minDueDays = $element->getOffset(); } } } return $minDueDays; } abstract protected function getEntityPreferenceObject(); public function calculateDueDate($invoiceDate, $serviceCode) { $profile = $this->getProfile(); if (!$profile) { $profile = $this->getDefaultProfile(); if ($profile and get_class($profile)!='App\Utils\InvoiceDueDate\Profile') { throw new \Exception('Invalid object outputted - the return of the getDefaultProfile() must be an object of type \App\Utils\InvoiceDueDate\Profile.'); } } if ($profile) { return $profile->calculateDueDate($invoiceDate, $serviceCode); } else { return null; } } abstract protected function getDefaultProfile(); } 