fix demoSetup

This commit is contained in:
Dennis Eichhorn 2023-04-16 01:55:34 +02:00
parent 291ac578db
commit 02fbb6f1ef
11 changed files with 254 additions and 208 deletions

View File

@ -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",

58
Admin/Install/types.json Normal file
View File

@ -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"
}
}
]

View File

@ -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;
}
}

View File

@ -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()
);

View File

@ -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();
}
/**

View File

@ -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'],

View File

@ -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,
];
}

View File

@ -1,135 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Modules\ContractManagement\Models
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\ContractManagement\Models;
use phpOMS\Localization\ISO639x1Enum;
/**
* Contract type l11n class.
*
* @package Modules\ContractManagement\Models
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
class ContractTypeL11n implements \JsonSerializable
{
/**
* ID.
*
* @var int
* @since 1.0.0
*/
protected int $id = 0;
/**
* Model ID.
*
* @var int|ContractType
* @since 1.0.0
*/
public int | ContractType $type = 0;
/**
* Language.
*
* @var string
* @since 1.0.0
*/
protected string $language = ISO639x1Enum::_EN;
/**
* Title.
*
* @var string
* @since 1.0.0
*/
public string $title = '';
/**
* Constructor.
*
* @param int|ContractType $type Attribute type
* @param string $title Localized title
* @param string $language Language
*
* @since 1.0.0
*/
public function __construct(int | ContractType $type = 0, string $title = '', string $language = ISO639x1Enum::_EN)
{
$this->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();
}
}

View File

@ -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<T>
* @since 1.0.0
*/
public const MODEL = BaseStringL11n::class;
}

View File

@ -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<T>
* @since 1.0.0
*/
public const MODEL = ContractType::class;
/**
* Primary table.
*

View File

@ -1,47 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Modules\ContractManagement\Models
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\ContractManagement\Models;
/**
* Contract model.
*
* @package Modules\ContractManagement\Models
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
final class NullContractTypeL11n extends ContractTypeL11n
{
/**
* Constructor
*
* @param int $id Model id
*
* @since 1.0.0
*/
public function __construct(int $id = 0)
{
$this->id = $id;
parent::__construct();
}
/**
* {@inheritdoc}
*/
public function jsonSerialize() : mixed
{
return ['id' => $this->id];
}
}