<?php
 namespace App\Config; class CashingDocuments extends XmlDynamicRecordsAbstract { protected $fileName = CONFIG_FILE_CASHING_DOCUMENTS_LIST; protected $xmlRootTagName = 'cashingDocuments'; protected function getAll_rawData_fileNotExists() { return []; } protected function getAll_rawData_extractDataFromFile_list(\SimpleXMLElement $xml) { $return = []; foreach ($xml->doc as $docXml) { $docTypeCode = (String)$docXml['code']; $return[$docTypeCode] = [ 'name' => (String)$docXml->name, 'active' => (Int)$docXml['active'], 'group' => (String)$docXml->group, 'number' => (String)$docXml->number, 'sign' => (Int)$docXml->sign, 'bankAccount' => (String)$docXml->bankAccount, 'explanations' => (String)$docXml->explanations, 'comments' => (String)$docXml->comments, ]; } return $return; } protected function getAll_rawData_extractDataFromFile_default(\SimpleXMLElement $xml) { return ''; } protected function getAll_rawData_extractDataFromFile_mandatory(\SimpleXMLElement $xml) { return true; } protected function itemCheck($code, $itemDetails) { $errorMessages = []; if (!mb_strlen($code)) { $errorMessages[] = 'No document code set'; } elseif (mb_strlen($code)>25) { $errorMessages[] = 'The document code can have max 25 chars.'; } elseif (!\Engine\Validator::set($code)->isAlnum()) { $errorMessages[] = 'The document code can only contain alphanumeric chars.'; } if (!$itemDetails['name']) { $errorMessages[] = 'No document name set'; } elseif (mb_strlen($itemDetails['name'])>25) { $errorMessages[] = 'The document name can have max 25 chars.'; } else { $docsList = $this->getList(); unset($docsList[$code]); $cleanDocsList = array_map('mb_strtolower', $docsList); if ( in_array( mb_strtolower($itemDetails['name']), $cleanDocsList) ) { $errorMessages[] = 'The document name already exists.'; } } if (!$itemDetails['group']) { $errorMessages[] = 'No document group set'; } elseif (!in_array($itemDetails['group'], ['cash', 'bank'])) { $errorMessages[] = 'The document group is not valid.'; } if (!is_numeric($itemDetails['sign']) or !in_array($itemDetails['sign'], [-1, 0, 1])) { $errorMessages[] = 'The doc sign is not valid - it must be set as -1, 0 or 1.'; } if (!in_array($itemDetails['number'], ['hidden', 'optional', 'mandatory', 'series'])) { $errorMessages[] = 'The doc number type is not set/valid.'; } $validVisibilities = ['hidden', 'optional', 'mandatory']; if (!in_array($itemDetails['bankAccount'], $validVisibilities)) { $errorMessages[] = 'The bank account visibility is not set/valid.'; } if (!in_array($itemDetails['explanations'], $validVisibilities)) { $errorMessages[] = 'The explanations visibility is not set/valid.'; } if (!in_array($itemDetails['comments'], $validVisibilities)) { $errorMessages[] = 'The comments visibility is not set/valid.'; } if ($errorMessages) { $this->addErrorMessage(implode(' ', $errorMessages)); } } protected function save_check() { foreach(self::getInstance()->getAll() as $prevDocTypeCode=>$nada) { if (!$this->itemsList[$prevDocTypeCode]) { $this->addErrorMessage(sprintf('The doc type `%s` must not be deleted.', $prevDocTypeCode)); } } } protected function save_write_do_getXml() { $xml=simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><'.$this->xmlRootTagName.' />'); foreach ($this->itemsList as $docTypeCode=>$docTypeDet) { $thisDocTypeXml = $xml->addChild('doc', ''); $thisDocTypeXml['code'] = $docTypeCode; $thisDocTypeXml['active'] = (bool)$docTypeDet['active']; $thisDocTypeXml->name = $docTypeDet['name']; $thisDocTypeXml->group = $docTypeDet['group']; $thisDocTypeXml->number = $docTypeDet['number']; $thisDocTypeXml->sign = $docTypeDet['sign']; $thisDocTypeXml->bankAccount = $docTypeDet['bankAccount']; $thisDocTypeXml->explanations = $docTypeDet['explanations']; $thisDocTypeXml->comments = $docTypeDet['comments']; } return $xml; } public function getList() { $return = []; foreach ($this->getAll() as $docTypeCode=>$docDetails) { if ($docDetails['active']) { $return[$docTypeCode] = $docDetails['name']; } } return $return; } public function exists($docType) { return $this->get($docType) ? true : false; } public function isActive($docType) { return (bool)$this->get($docType)['active']; } public function getGroup($docType) { return $this->get($docType)['group']; } public function getName($docType) { return $this->get($docType)['name']; } public function getNumber($docType) { return $this->get($docType)['number']; } public function getSign($docType) { return $this->get($docType)['sign']; } public function getBankAccount($docType) { return $this->get($docType)['bankAccount']; } public function getExplanations($docType) { return $this->get($docType)['explanations']; } public function getComments($docType) { return $this->get($docType)['comments']; } }