From 02fbb6f1ef504870fb1f20630b111c0af3df72b1 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 16 Apr 2023 01:55:34 +0200 Subject: [PATCH] fix demoSetup --- Admin/Install/db.json | 21 ++++- Admin/Install/types.json | 58 +++++++++++++ Admin/Installer.php | 90 ++++++++++++++++++++ Controller/ApiController.php | 43 ++++++++-- Models/Contract.php | 7 +- Models/ContractMapper.php | 2 + Models/ContractType.php | 35 +++++--- Models/ContractTypeL11n.php | 135 ------------------------------ Models/ContractTypeL11nMapper.php | 13 ++- Models/ContractTypeMapper.php | 11 ++- Models/NullContractTypeL11n.php | 47 ----------- 11 files changed, 254 insertions(+), 208 deletions(-) create mode 100644 Admin/Install/types.json delete mode 100755 Models/ContractTypeL11n.php delete mode 100755 Models/NullContractTypeL11n.php diff --git a/Admin/Install/db.json b/Admin/Install/db.json index 9bbdb4d..e3b51c9 100755 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -8,6 +8,11 @@ "null": false, "primary": true, "autoincrement": true + }, + "contractmgmt_type_name": { + "name": "contractmgmt_type_name", + "type": "VARCHAR(255)", + "null": false } } }, @@ -52,6 +57,11 @@ "primary": true, "autoincrement": true }, + "contractmgmt_contract_template": { + "name": "contractmgmt_contract_template", + "type": "TINYINT", + "null": false + }, "contractmgmt_contract_title": { "name": "contractmgmt_contract_title", "type": "VARCHAR(255)", @@ -109,10 +119,19 @@ "contractmgmt_contract_type": { "name": "contractmgmt_contract_type", "type": "INT", - "null": false, + "null": true, + "default": null, "foreignTable": "contractmgmt_type", "foreignKey": "contractmgmt_type_id" }, + "contractmgmt_contract_parent": { + "name": "contractmgmt_contract_parent", + "type": "INT", + "null": true, + "default": null, + "foreignTable": "contractmgmt_contract", + "foreignKey": "contractmgmt_contract_id" + }, "contractmgmt_contract_unit": { "name": "contractmgmt_contract_unit", "type": "INT", diff --git a/Admin/Install/types.json b/Admin/Install/types.json new file mode 100644 index 0000000..fcdb49e --- /dev/null +++ b/Admin/Install/types.json @@ -0,0 +1,58 @@ +[ + { + "name": "rent", + "l11n": { + "en": "Rent", + "de": "Miete" + } + }, + { + "name": "it", + "l11n": { + "en": "IT", + "de": "IT" + } + }, + { + "name": "utilities", + "l11n": { + "en": "Utilities", + "de": "Nebenkosten" + } + }, + { + "name": "energy", + "l11n": { + "en": "Energy", + "de": "Energie" + } + }, + { + "name": "procurement", + "l11n": { + "en": "Procurement", + "de": "Einkauf" + } + }, + { + "name": "sales", + "l11n": { + "en": "Sales", + "de": "Vertrieb" + } + }, + { + "name": "sales_subscription", + "l11n": { + "en": "Sales subscription", + "de": "Vertriebsabonnement" + } + }, + { + "name": "nda", + "l11n": { + "en": "NDA", + "de": "NDA" + } + } +] \ No newline at end of file diff --git a/Admin/Installer.php b/Admin/Installer.php index 3fe846e..d129c20 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -14,7 +14,13 @@ declare(strict_types=1); namespace Modules\ContractManagement\Admin; +use phpOMS\Application\ApplicationAbstract; +use phpOMS\Config\SettingsInterface; +use phpOMS\Message\Http\HttpRequest; +use phpOMS\Message\Http\HttpResponse; use phpOMS\Module\InstallerAbstract; +use phpOMS\Module\ModuleInfo; +use phpOMS\Uri\HttpUri; /** * Installer class. @@ -33,4 +39,88 @@ final class Installer extends InstallerAbstract * @since 1.0.0 */ public const PATH = __DIR__; + + /** + * {@inheritdoc} + */ + public static function install(ApplicationAbstract $app, ModuleInfo $info, SettingsInterface $cfgHandler) : void + { + parent::install($app, $info, $cfgHandler); + + /* Bill types */ + $fileContent = \file_get_contents(__DIR__ . '/Install/types.json'); + if ($fileContent === false) { + return; + } + + /** @var array $types */ + $types = \json_decode($fileContent, true); + if ($types === false) { + return; + } + + self::createContractTypes($app, $types); + } + + /** + * Install default contract types + * + * @param ApplicationAbstract $app Application + * @param array $types Bill types + * @param int $template Default template + * + * @return array + * + * @since 1.0.0 + */ + private static function createContractTypes(ApplicationAbstract $app, array $types) : array + { + $contractTypes = []; + + /** @var \Modules\ContractManagement\Controller\ApiController $module */ + $module = $app->moduleManager->getModuleInstance('ContractManagement'); + + foreach ($types as $type) { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('name', $type['name'] ?? ''); + $request->setData('title', \reset($type['l11n'])); + $request->setData('language', \array_keys($type['l11n'])[0] ?? 'en'); + + $module->apiContractTypeCreate($request, $response); + + $responseData = $response->get(''); + if (!\is_array($responseData)) { + continue; + } + + $billType = !\is_array($responseData['response']) + ? $responseData['response']->toArray() + : $responseData['response']; + + $contractTypes[] = $billType; + + $isFirst = true; + foreach ($type['l11n'] as $language => $l11n) { + if ($isFirst) { + $isFirst = false; + continue; + } + + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('title', $l11n); + $request->setData('language', $language); + $request->setData('type', $billType['id']); + + $module->apiContractTypeL11nCreate($request, $response); + } + } + + return $contractTypes; + } } diff --git a/Controller/ApiController.php b/Controller/ApiController.php index d0ea70a..6b919f5 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -18,13 +18,13 @@ use Modules\Admin\Models\NullAccount; use Modules\ContractManagement\Models\Contract; use Modules\ContractManagement\Models\ContractMapper; use Modules\ContractManagement\Models\ContractType; -use Modules\ContractManagement\Models\ContractTypeL11n; use Modules\ContractManagement\Models\ContractTypeL11nMapper; use Modules\ContractManagement\Models\ContractTypeMapper; use Modules\ContractManagement\Models\NullContractType; use Modules\Media\Models\MediaMapper; use Modules\Media\Models\PathSettings; use Modules\Organization\Models\NullUnit; +use phpOMS\Localization\BaseStringL11n; use phpOMS\Localization\ISO639x1Enum; use phpOMS\Message\Http\RequestStatusCode; use phpOMS\Message\NotificationLevel; @@ -92,6 +92,21 @@ final class ApiController extends Controller return []; } + /** + * Create media directory path + * + * @param Contract $contract Contract + * + * @return string + * + * @since 1.0.0 + */ + private function createContractDir(Contract $contract) : string + { + return '/Modules/ContractManagement/Contract/' + . $contract->getId(); + } + /** * Method to create item l11n from request. * @@ -141,14 +156,22 @@ final class ApiController extends Controller return; } + /** @var \Modules\ContractManagement\Models\Contract */ + $contract = ContractMapper::get() + ->where('id', $request->getDataInt('contract')) + ->execute(); + + $path = $this->createContractDir($contract); + $uploaded = $this->app->moduleManager->get('Media')->uploadFiles( names: $request->getDataList('names'), fileNames: $request->getDataList('filenames'), files: $uploadedFiles, account: $request->header->account, - basePath: __DIR__ . '/../../../Modules/Media/Files/Modules/ContractManagement/Contracts/' . ($request->getData('contract_title') ?? '0'), - virtualPath: '/Modules/ContractManagement/Contracts/' . ($request->getData('contract_title') ?? '0'), - pathSettings: PathSettings::FILE_PATH + basePath: __DIR__ . '/../../../Modules/Media/Files' . $path, + virtualPath: $path, + pathSettings: PathSettings::FILE_PATH, + readContent: true ); if ($request->hasData('type')) { @@ -216,6 +239,7 @@ final class ApiController extends Controller { $contractType = new ContractType(); $contractType->setL11n($request->getDataString('title') ?? '', $request->getDataString('language') ?? ISO639x1Enum::_EN); + $contractType->name = $request->getDataString('name') ?? ''; return $contractType; } @@ -272,15 +296,16 @@ final class ApiController extends Controller * * @param RequestAbstract $request Request * - * @return ContractTypeL11n + * @return BaseStringL11n * * @since 1.0.0 */ - private function createContractTypeL11nFromRequest(RequestAbstract $request) : ContractTypeL11n + private function createContractTypeL11nFromRequest(RequestAbstract $request) : BaseStringL11n { - $typeL11n = new ContractTypeL11n( - $request->getDataInt('type') ?? 0, - $request->getDataString('title') ?? '', + $typeL11n = new BaseStringL11n(); + $typeL11n->ref = $request->getDataInt('type') ?? 0; + $typeL11n->content = $request->getDataString('title') ?? ''; + $typeL11n->setLanguage( $request->getDataString('language') ?? $request->getLanguage() ); diff --git a/Models/Contract.php b/Models/Contract.php index 4e6e832..900a162 100755 --- a/Models/Contract.php +++ b/Models/Contract.php @@ -38,6 +38,10 @@ class Contract */ protected int $id = 0; + public ?int $parent = null; + + public bool $isTemplate = false; + /** * Files. * @@ -81,7 +85,7 @@ class Contract public ?Money $costs = null; - public ContractType $type; + public ?ContractType $type = null; public ?Unit $unit = null; @@ -94,7 +98,6 @@ class Contract { $this->createdAt = new \DateTimeImmutable('now'); $this->account = new NullAccount(); - $this->type = new ContractType(); } /** diff --git a/Models/ContractMapper.php b/Models/ContractMapper.php index a70c639..52b750c 100755 --- a/Models/ContractMapper.php +++ b/Models/ContractMapper.php @@ -42,6 +42,8 @@ final class ContractMapper extends DataMapperFactory */ public const COLUMNS = [ 'contractmgmt_contract_id' => ['name' => 'contractmgmt_contract_id', 'type' => 'int', 'internal' => 'id'], + 'contractmgmt_contract_parent' => ['name' => 'contractmgmt_contract_parent', 'type' => 'int', 'internal' => 'parent'], + 'contractmgmt_contract_template' => ['name' => 'contractmgmt_contract_template', 'type' => 'int', 'internal' => 'isTemplate'], 'contractmgmt_contract_title' => ['name' => 'contractmgmt_contract_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true], 'contractmgmt_contract_description' => ['name' => 'contractmgmt_contract_description', 'type' => 'string', 'internal' => 'description'], 'contractmgmt_contract_account' => ['name' => 'contractmgmt_contract_account', 'type' => 'int', 'internal' => 'account'], diff --git a/Models/ContractType.php b/Models/ContractType.php index 7679b71..c83a236 100755 --- a/Models/ContractType.php +++ b/Models/ContractType.php @@ -14,6 +14,7 @@ declare(strict_types=1); namespace Modules\ContractManagement\Models; +use phpOMS\Localization\BaseStringL11n; use phpOMS\Localization\ISO639x1Enum; /** @@ -34,12 +35,22 @@ class ContractType implements \JsonSerializable */ protected int $id = 0; + /** + * Name. + * + * Name used for additional identification, doesn't have to be unique. + * + * @var string + * @since 1.0.0 + */ + public string $name = ''; + /** * Localization * - * @var ContractTypeL11n + * @var BaseStringL11n */ - protected string | ContractTypeL11n $l11n; + protected string | BaseStringL11n $l11n; /** * Constructor. @@ -68,22 +79,23 @@ class ContractType implements \JsonSerializable /** * Set l11n * - * @param string|ContractTypeL11n $l11n Tag article l11n - * @param string $lang Language + * @param string|BaseStringL11n $l11n Tag article l11n + * @param string $lang Language * * @return void * * @since 1.0.0 */ - public function setL11n(string|ContractTypeL11n $l11n, string $lang = ISO639x1Enum::_EN) : void + public function setL11n(string|BaseStringL11n $l11n, string $lang = ISO639x1Enum::_EN) : void { - if ($l11n instanceof ContractTypeL11n) { + if ($l11n instanceof BaseStringL11n) { $this->l11n = $l11n; - } elseif (isset($this->l11n) && $this->l11n instanceof ContractTypeL11n) { - $this->l11n->title = $l11n; + } elseif (isset($this->l11n) && $this->l11n instanceof BaseStringL11n) { + $this->l11n->content = $l11n; } else { - $this->l11n = new ContractTypeL11n(); - $this->l11n->title = $l11n; + $this->l11n = new BaseStringL11n(); + $this->l11n->ref = $this->id; + $this->l11n->content = $l11n; $this->l11n->setLanguage($lang); } } @@ -95,7 +107,7 @@ class ContractType implements \JsonSerializable */ public function getL11n() : string { - return $this->l11n instanceof ContractTypeL11n ? $this->l11n->title : $this->l11n; + return $this->l11n instanceof BaseStringL11n ? $this->l11n->content : $this->l11n; } /** @@ -106,6 +118,7 @@ class ContractType implements \JsonSerializable return [ 'id' => $this->id, 'l11n' => $this->l11n, + 'name' => $this->name, ]; } diff --git a/Models/ContractTypeL11n.php b/Models/ContractTypeL11n.php deleted file mode 100755 index 1d64d90..0000000 --- a/Models/ContractTypeL11n.php +++ /dev/null @@ -1,135 +0,0 @@ -type = $type; - $this->title = $title; - $this->language = $language; - } - - /** - * Get id - * - * @return int - * - * @since 1.0.0 - */ - public function getId() : int - { - return $this->id; - } - - /** - * Get language - * - * @return string - * - * @since 1.0.0 - */ - public function getLanguage() : string - { - return $this->language; - } - - /** - * Set language - * - * @param string $language Language - * - * @return void - * - * @since 1.0.0 - */ - public function setLanguage(string $language) : void - { - $this->language = $language; - } - - /** - * {@inheritdoc} - */ - public function toArray() : array - { - return [ - 'id' => $this->id, - 'title' => $this->title, - 'type' => $this->type, - 'language' => $this->language, - ]; - } - - /** - * {@inheritdoc} - */ - public function jsonSerialize() : mixed - { - return $this->toArray(); - } -} diff --git a/Models/ContractTypeL11nMapper.php b/Models/ContractTypeL11nMapper.php index 03b1380..aba79ee 100755 --- a/Models/ContractTypeL11nMapper.php +++ b/Models/ContractTypeL11nMapper.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Modules\ContractManagement\Models; use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; +use phpOMS\Localization\BaseStringL11n; /** * Contract type l11n mapper class. @@ -37,8 +38,8 @@ final class ContractTypeL11nMapper extends DataMapperFactory */ public const COLUMNS = [ 'contractmgmt_type_l11n_id' => ['name' => 'contractmgmt_type_l11n_id', 'type' => 'int', 'internal' => 'id'], - 'contractmgmt_type_l11n_title' => ['name' => 'contractmgmt_type_l11n_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true], - 'contractmgmt_type_l11n_type' => ['name' => 'contractmgmt_type_l11n_type', 'type' => 'int', 'internal' => 'type'], + 'contractmgmt_type_l11n_title' => ['name' => 'contractmgmt_type_l11n_title', 'type' => 'string', 'internal' => 'content', 'autocomplete' => true], + 'contractmgmt_type_l11n_type' => ['name' => 'contractmgmt_type_l11n_type', 'type' => 'int', 'internal' => 'ref'], 'contractmgmt_type_l11n_lang' => ['name' => 'contractmgmt_type_l11n_lang', 'type' => 'string', 'internal' => 'language'], ]; @@ -57,4 +58,12 @@ final class ContractTypeL11nMapper extends DataMapperFactory * @since 1.0.0 */ public const PRIMARYFIELD = 'contractmgmt_type_l11n_id'; + + /** + * Model to use by the mapper. + * + * @var class-string + * @since 1.0.0 + */ + public const MODEL = BaseStringL11n::class; } diff --git a/Models/ContractTypeMapper.php b/Models/ContractTypeMapper.php index e9be544..e336163 100755 --- a/Models/ContractTypeMapper.php +++ b/Models/ContractTypeMapper.php @@ -37,6 +37,7 @@ final class ContractTypeMapper extends DataMapperFactory */ public const COLUMNS = [ 'contractmgmt_type_id' => ['name' => 'contractmgmt_type_id', 'type' => 'int', 'internal' => 'id'], + 'contractmgmt_type_name' => ['name' => 'contractmgmt_type_name', 'type' => 'string', 'internal' => 'name'], ]; /** @@ -50,11 +51,19 @@ final class ContractTypeMapper extends DataMapperFactory 'mapper' => ContractTypeL11nMapper::class, 'table' => 'contractmgmt_type_l11n', 'self' => 'contractmgmt_type_l11n_type', - 'column' => 'title', + 'column' => 'content', 'external' => null, ], ]; + /** + * Model to use by the mapper. + * + * @var class-string + * @since 1.0.0 + */ + public const MODEL = ContractType::class; + /** * Primary table. * diff --git a/Models/NullContractTypeL11n.php b/Models/NullContractTypeL11n.php deleted file mode 100755 index adad4a1..0000000 --- a/Models/NullContractTypeL11n.php +++ /dev/null @@ -1,47 +0,0 @@ -id = $id; - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - public function jsonSerialize() : mixed - { - return ['id' => $this->id]; - } -}