oms-Organization/Controller/ApiAttributeController.php
Dennis Eichhorn 88c8a92fc4
Some checks failed
Image optimization / general_image_workflow (push) Has been cancelled
CI / general_module_workflow_php (push) Has been cancelled
CI / general_module_workflow_js (push) Has been cancelled
fix permissions
2025-04-02 14:15:06 +00:00

561 lines
20 KiB
PHP

<?php
/**
* Jingga
*
* PHP Version 8.2
*
* @package Modules\Organization
* @copyright Dennis Eichhorn
* @license OMS License 2.2
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\Organization\Controller;
use Modules\Attribute\Models\Attribute;
use Modules\Attribute\Models\AttributeType;
use Modules\Attribute\Models\AttributeValue;
use Modules\Organization\Models\Attribute\UnitAttributeMapper;
use Modules\Organization\Models\Attribute\UnitAttributeTypeL11nMapper;
use Modules\Organization\Models\Attribute\UnitAttributeTypeMapper;
use Modules\Organization\Models\Attribute\UnitAttributeValueL11nMapper;
use Modules\Organization\Models\Attribute\UnitAttributeValueMapper;
use phpOMS\Localization\BaseStringL11n;
use phpOMS\Message\Http\RequestStatusCode;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
/**
* Organization class.
*
* @package Modules\Organization
* @license OMS License 2.2
* @link https://jingga.app
* @since 1.0.0
*
* @question Change localization from setting to unit attribute
* but keep server localization in settings as default setting when creating a new unit?
* https://github.com/Karaka-Management/Organization-Guide/issues/6
*
* @todo Allow multiple tax office registrations
* Currently only one is allowed through the attributes.
* This is bad because a company can be registered in multiple countries.
* One idea could be to use the address_attr for this purpose.
* https://github.com/Karaka-Management/oms-Organization/issues/6
*/
final class ApiAttributeController extends Controller
{
use \Modules\Attribute\Controller\ApiAttributeTraitController;
/**
* Api method to create unit attribute
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeCreate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeCreate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidCreateResponse($request, $response, $val);
return;
}
$type = UnitAttributeTypeMapper::get()
->with('defaults')
->where('id', (int) $request->getData('type'))
->execute();
if (!$type->isRepeatable) {
$attr = UnitAttributeMapper::count()
->with('type')
->where('type/id', $type->id)
->where('ref', (int) $request->getData('ref'))
->executeCount();
if ($attr > 0) {
$response->header->status = RequestStatusCode::R_409;
$this->createInvalidCreateResponse($request, $response, $val);
return;
}
}
$attribute = $this->createAttributeFromRequest($request, $type);
$this->createModel($request->header->account, $attribute, UnitAttributeMapper::class, 'attribute', $request->getOrigin());
$this->createStandardCreateResponse($request, $response, $attribute);
}
/**
* Api method to create unit attribute l11n
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeTypeL11nCreate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeTypeL11nCreate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidCreateResponse($request, $response, $val);
return;
}
$attrL11n = $this->createAttributeTypeL11nFromRequest($request);
$this->createModel($request->header->account, $attrL11n, UnitAttributeTypeL11nMapper::class, 'attr_type_l11n', $request->getOrigin());
$this->createStandardCreateResponse($request, $response, $attrL11n);
}
/**
* Api method to create unit attribute type
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeTypeCreate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeTypeCreate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidCreateResponse($request, $response, $val);
return;
}
$attrType = $this->createAttributeTypeFromRequest($request);
$this->createModel($request->header->account, $attrType, UnitAttributeTypeMapper::class, 'attr_type', $request->getOrigin());
$this->createStandardCreateResponse($request, $response, $attrType);
}
/**
* Api method to create unit attribute value
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeValueCreate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeValueCreate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidCreateResponse($request, $response, $val);
return;
}
/** @var \Modules\Attribute\Models\AttributeType $type */
$type = UnitAttributeTypeMapper::get()
->where('id', $request->getDataInt('type') ?? 0)
->execute();
if ($type->isInternal) {
$response->header->status = RequestStatusCode::R_403;
$this->createInvalidCreateResponse($request, $response, $val);
return;
}
$attrValue = $this->createAttributeValueFromRequest($request, $type);
$this->createModel($request->header->account, $attrValue, UnitAttributeValueMapper::class, 'attr_value', $request->getOrigin());
if ($attrValue->isDefault) {
$this->createModelRelation(
$request->header->account,
$type->id,
$attrValue->id,
UnitAttributeTypeMapper::class, 'defaults', '', $request->getOrigin()
);
}
$this->createStandardCreateResponse($request, $response, $attrValue);
}
/**
* Api method to create unit attribute l11n
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeValueL11nCreate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeValueL11nCreate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidCreateResponse($request, $response, $val);
return;
}
$attrL11n = $this->createAttributeValueL11nFromRequest($request);
$this->createModel($request->header->account, $attrL11n, UnitAttributeValueL11nMapper::class, 'attr_value_l11n', $request->getOrigin());
$this->createStandardCreateResponse($request, $response, $attrL11n);
}
/**
* Api method to update UnitAttribute
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeUpdate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeUpdate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidUpdateResponse($request, $response, $val);
return;
}
/** @var Attribute $old */
$old = UnitAttributeMapper::get()
->with('type')
->with('type/defaults')
->with('value')
->where('id', $request->getDataInt('id') ?? 0)
->execute();
$new = $this->updateAttributeFromRequest($request, clone $old);
if ($new->id === 0) {
// Set response header to invalid request because of invalid data
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidUpdateResponse($request, $response, $new);
return;
}
$this->updateModel($request->header->account, $old, $new, UnitAttributeMapper::class, 'unit_attribute', $request->getOrigin());
if ($new->value->getValue() !== $old->value->getValue()
&& $new->type->custom
) {
$this->updateModel($request->header->account, $old->value, $new->value, UnitAttributeValueMapper::class, 'attribute_value', $request->getOrigin());
}
$this->createStandardUpdateResponse($request, $response, $new);
}
/**
* Api method to delete UnitAttribute
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeDelete(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeDelete($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidDeleteResponse($request, $response, $val);
return;
}
$unitAttribute = UnitAttributeMapper::get()
->with('type')
->where('id', $request->getDataInt('id') ?? 0)
->execute();
if ($unitAttribute->type->isRequired) {
$this->createInvalidDeleteResponse($request, $response, []);
return;
}
$this->deleteModel($request->header->account, $unitAttribute, UnitAttributeMapper::class, 'unit_attribute', $request->getOrigin());
$this->createStandardDeleteResponse($request, $response, $unitAttribute);
}
/**
* Api method to update UnitAttributeTypeL11n
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeTypeL11nUpdate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeTypeL11nUpdate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidUpdateResponse($request, $response, $val);
return;
}
/** @var BaseStringL11n $old */
$old = UnitAttributeTypeL11nMapper::get()->where('id', $request->getDataInt('id') ?? 0)->execute();
$new = $this->updateAttributeTypeL11nFromRequest($request, clone $old);
$this->updateModel($request->header->account, $old, $new, UnitAttributeTypeL11nMapper::class, 'unit_attribute_type_l11n', $request->getOrigin());
$this->createStandardUpdateResponse($request, $response, $new);
}
/**
* Api method to delete UnitAttributeTypeL11n
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeTypeL11nDelete(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeTypeL11nDelete($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidDeleteResponse($request, $response, $val);
return;
}
/** @var BaseStringL11n $unitAttributeTypeL11n */
$unitAttributeTypeL11n = UnitAttributeTypeL11nMapper::get()->where('id', $request->getDataInt('id') ?? 0)->execute();
$this->deleteModel($request->header->account, $unitAttributeTypeL11n, UnitAttributeTypeL11nMapper::class, 'unit_attribute_type_l11n', $request->getOrigin());
$this->createStandardDeleteResponse($request, $response, $unitAttributeTypeL11n);
}
/**
* Api method to update UnitAttributeType
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeTypeUpdate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeTypeUpdate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidUpdateResponse($request, $response, $val);
return;
}
/** @var AttributeType $old */
$old = UnitAttributeTypeMapper::get()->with('defaults')->where('id', $request->getDataInt('id') ?? 0)->execute();
$new = $this->updateAttributeTypeFromRequest($request, clone $old);
$this->updateModel($request->header->account, $old, $new, UnitAttributeTypeMapper::class, 'unit_attribute_type', $request->getOrigin());
$this->createStandardUpdateResponse($request, $response, $new);
}
/**
* Api method to delete UnitAttributeType
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @todo Implement API function
*
* @since 1.0.0
*/
public function apiUnitAttributeTypeDelete(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeTypeDelete($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidDeleteResponse($request, $response, $val);
return;
}
/** @var AttributeType $unitAttributeType */
$unitAttributeType = UnitAttributeTypeMapper::get()->with('defaults')->where('id', $request->getDataInt('id') ?? 0)->execute();
$this->deleteModel($request->header->account, $unitAttributeType, UnitAttributeTypeMapper::class, 'unit_attribute_type', $request->getOrigin());
$this->createStandardDeleteResponse($request, $response, $unitAttributeType);
}
/**
* Api method to update UnitAttributeValue
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeValueUpdate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeValueUpdate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidUpdateResponse($request, $response, $val);
return;
}
/** @var AttributeValue $old */
$old = UnitAttributeValueMapper::get()->where('id', $request->getDataInt('id') ?? 0)->execute();
/** @var \Modules\Attribute\Models\Attribute $attr */
$attr = UnitAttributeMapper::get()
->with('type')
->where('id', $request->getDataInt('attribute') ?? 0)
->execute();
$new = $this->updateAttributeValueFromRequest($request, clone $old, $attr);
$this->updateModel($request->header->account, $old, $new, UnitAttributeValueMapper::class, 'unit_attribute_value', $request->getOrigin());
$this->createStandardUpdateResponse($request, $response, $new);
}
/**
* Api method to delete UnitAttributeValue
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeValueDelete(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
// @todo I don't think values can be deleted? Only Attributes
// However, It should be possible to remove UNUSED default values
// either here or other function?
// if (!empty($val = $this->validateAttributeValueDelete($request))) {
// $response->header->status = RequestStatusCode::R_400;
// $this->createInvalidDeleteResponse($request, $response, $val);
// return;
// }
// /** @var \Modules\Organization\Models\UnitAttributeValue $unitAttributeValue */
// $unitAttributeValue = UnitAttributeValueMapper::get()->where('id', $request->getDataInt('id') ?? 0)->execute();
// $this->deleteModel($request->header->account, $unitAttributeValue, UnitAttributeValueMapper::class, 'unit_attribute_value', $request->getOrigin());
// $this->createStandardDeleteResponse($request, $response, $unitAttributeValue);
}
/**
* Api method to update UnitAttributeValueL11n
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeValueL11nUpdate(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeValueL11nUpdate($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidUpdateResponse($request, $response, $val);
return;
}
/** @var BaseStringL11n $old */
$old = UnitAttributeValueL11nMapper::get()->where('id', $request->getDataInt('id') ?? 0);
$new = $this->updateAttributeValueL11nFromRequest($request, clone $old);
$this->updateModel($request->header->account, $old, $new, UnitAttributeValueL11nMapper::class, 'unit_attribute_value_l11n', $request->getOrigin());
$this->createStandardUpdateResponse($request, $response, $new);
}
/**
* Api method to delete UnitAttributeValueL11n
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function apiUnitAttributeValueL11nDelete(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
if (!empty($val = $this->validateAttributeValueL11nDelete($request))) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidDeleteResponse($request, $response, $val);
return;
}
/** @var BaseStringL11n $unitAttributeValueL11n */
$unitAttributeValueL11n = UnitAttributeValueL11nMapper::get()->where('id', $request->getDataInt('id') ?? 0)->execute();
$this->deleteModel($request->header->account, $unitAttributeValueL11n, UnitAttributeValueL11nMapper::class, 'unit_attribute_value_l11n', $request->getOrigin());
$this->createStandardDeleteResponse($request, $response, $unitAttributeValueL11n);
}
}