Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Eichhorn committed May 12, 2024
1 parent c8152cc commit e246180
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ jobs:
- uses: actions/first-interaction@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
issue-message: 'Thank you for createing this issue. We will check it as soon as possible.'
issue-message: 'Thank you for creating this issue. We will check it as soon as possible.'
pr-message: 'Thank you for your pull request. We will check it as soon as possible.'
56 changes: 0 additions & 56 deletions Admin/Install/Media.install.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,5 @@
"name": "Items",
"virtualPath": "/Modules/ItemManagement",
"user": 1
},
{
"type": "type",
"name": "item_profile_image",
"l11n": [
{
"title": "Profile image",
"lang": "en"
},
{
"title": "Profilbild",
"lang": "de"
}
]
},
{
"type": "type",
"name": "item_demo_download",
"l11n": [
{
"title": "Public Download",
"lang": "en"
},
{
"title": "Oeffentlich Download",
"lang": "de"
}
]
},
{
"type": "type",
"name": "item_private_download",
"l11n": [
{
"title": "Private Download",
"lang": "en"
},
{
"title": "Privat Download",
"lang": "de"
}
]
},
{
"type": "type",
"name": "item_purchase_download",
"l11n": [
{
"title": "Purchased Download",
"lang": "en"
},
{
"title": "Gekaufter Download",
"lang": "de"
}
]
}
]
23 changes: 23 additions & 0 deletions Admin/Install/Tag.install.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"name": "item_demo_download",
"l11n": {
"en": "Item Download",
"de": "Artikel Download"
}
},
{
"name": "item_private_download",
"l11n": {
"en": "Private Item Download",
"de": "Privater Artikel Download"
}
},
{
"name": "item_purchase_download",
"l11n": {
"en": "Purchased Download",
"de": "Gekaufter Download"
}
}
]
43 changes: 43 additions & 0 deletions Admin/Install/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Jingga
*
* PHP Version 8.2
*
* @package Modules\ItemManagement\Admin\Install
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);

namespace Modules\ItemManagement\Admin\Install;

use phpOMS\Application\ApplicationAbstract;

/**
* Tag class.
*
* @package Modules\ItemManagement\Admin\Install
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
class Tag
{
/**
* Install media providing
*
* @param ApplicationAbstract $app Application
* @param string $path Module path
*
* @return void
*
* @since 1.0.0
*/
public static function install(ApplicationAbstract $app, string $path) : void
{
\Modules\Tag\Admin\Installer::installExternal($app, ['path' => __DIR__ . '/Tag.install.json']);
}
}
4 changes: 2 additions & 2 deletions Admin/Install/attributes.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@
{
"value": 1,
"l11n": {
"en": "Type 1",
"de": "Typ 1"
"en": "Product Type 1",
"de": "Produkttyp 1"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Controller/ApiAttributeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function apiItemAttributeCreate(RequestAbstract $request, ResponseAbstrac
->where('id', (int) $request->getData('type'))
->execute();

if (!$type->repeatable) {
if (!$type->isRepeatable) {
$attr = ItemAttributeMapper::count()
->with('type')
->where('type/id', $type->id)
Expand Down
132 changes: 124 additions & 8 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
use Modules\ItemManagement\Models\StockIdentifierType;
use Modules\Media\Models\Collection;
use Modules\Media\Models\CollectionMapper;
use Modules\Media\Models\MediaTypeMapper;
use Modules\Media\Models\PathSettings;
use Modules\Tag\Models\TagMapper;
use phpOMS\Account\PermissionType;
use phpOMS\Localization\BaseStringL11n;
use phpOMS\Localization\BaseStringL11nType;
Expand Down Expand Up @@ -256,15 +256,57 @@ public function apiItemCreate(RequestAbstract $request, ResponseAbstract $respon
$this->createModel($request->header->account, $item, ItemMapper::class, 'item', $request->getOrigin());
$this->app->dbPool->get()->con->commit();

// Define names
$names = [];
if ($request->hasData('name1')) {
$names = ItemL11nTypeMapper::getAll()
->where('title', ['name1', 'name2'])
->executeGetArray();

foreach ($names as $type) {
$names[$type->title] = $type;
}

$internalResponse = new HttpResponse();
$internalRequest = new HttpRequest();

$internalRequest->header->account = $request->header->account;

$internalRequest->setData('item', $item->id);
$internalRequest->setData('type', $names['name1']->id);
$internalRequest->setData('content', $request->getDataString('name1'));
$internalRequest->setData('language', $request->getDataString('language') ?? $request->header->l11n->language);

$this->apiItemL11nCreate($internalRequest, $internalResponse);

if ($request->hasData('name2')) {
$internalResponse = new HttpResponse();
$internalRequest = new HttpRequest();

$internalRequest->header->account = $request->header->account;

$internalRequest->setData('item', $item->id);
$internalRequest->setData('type', $names['name2']->id);
$internalRequest->setData('content', $request->getDataString('name2'));
$internalRequest->setData('language', $request->getDataString('language') ?? $request->header->l11n->language);

$this->apiItemL11nCreate($internalRequest, $internalResponse);
}
}

// Define default item containers
/** @var \Modules\Attribute\Models\AttributeType[] $types */
$types = ItemAttributeTypeMapper::getAll()
->where('name', ['default_sales_container', 'default_purchase_container'], 'IN')
->where('name', ['default_sales_container', 'default_purchase_container', 'hs_code'], 'IN')
->executeGetArray();

$primaryContainer = \reset($item->container);
if ($primaryContainer !== false) {
foreach ($types as $type) {
if (\stripos($type->name, '_container') === false) {
continue;
}

$internalResponse = clone $response;
$internalRequest = new HttpRequest();

Expand All @@ -277,6 +319,63 @@ public function apiItemCreate(RequestAbstract $request, ResponseAbstract $respon
}
}

// Guess hs_code
/* Alternatively find via segmentation
if ($item->stockIdentifier !== StockIdentifierType::NONE && $request->hasData('name1')) {
$hsCode = null;
foreach ($types as $type) {
if ($type->name === 'has_code') {
$hsCode = $type;
break;
}
}
$nameString = $request->getDataString('name1') . ' ' . ($request->getDataString('name2') ?? '');
$names = \explode(' ', $nameString);
$con = new \phpOMS\DataStorage\Database\Connection\SQLiteConnection(['db' => 'sqlite', 'database' => __DIR__ . '/../../../phpOMS/Api/TARIC/taric.sqlite']);
$con->connect();
\usort($names, function (string $a, string $b) {
return \strlen($b) - \strlen($a);
});
$results = [];
foreach ($names as $name) {
$query = new Builder($con);
$query->bind('name', '%' . $name . '%');
$sql = <<<SQL
SELECT taric_good.Goods_code
FROM taric_good
WHERE taric_good.Description LIKE :name
ORDER BY taric_good.Goods_code DESC;
SQL;
$results = $query->raw($sql)->execute();
if (!empty($results)) {
break;
}
}
if ($hsCode !== null && !empty($results)) {
$internalResponse = clone $response;
$internalRequest = new HttpRequest();
$internalRequest->header->account = $request->header->account;
$internalRequest->setData('ref', $item->id);
$internalRequest->setData('type', $hsCode->id);
$internalRequest->setData('value', $results[0]['Goods_code']);
$this->app->moduleManager->get('ItemManagement', 'ApiAttribute')->apiItemAttributeCreate($internalRequest, $internalResponse);
}
$con->close();
}
*/

// Define prices
if ($this->app->moduleManager->isActive('Billing')) {
$billing = $this->app->moduleManager->get('Billing', 'ApiPrice');

Expand Down Expand Up @@ -310,11 +409,11 @@ public function apiItemCreate(RequestAbstract $request, ResponseAbstract $respon
$this->createMediaDirForItem($item->id, $request->header->account);
$path = $this->createItemDir($item);

$uploadedFiles = $request->files['item_profile_image'] ?? [];
$uploadedFiles = $request->files['profile_image'] ?? [];
if (!empty($uploadedFiles)) {
/** @var \Modules\Media\Models\MediaType $profileImageType */
$profileImageType = MediaTypeMapper::get()
->where('name', 'item_profile_image')
/** @var \Modules\Tag\Models\Tag $profileImageType */
$profileImageType = TagMapper::get()
->where('name', 'profile_image')
->execute();

// upload image
Expand All @@ -326,7 +425,7 @@ public function apiItemCreate(RequestAbstract $request, ResponseAbstract $respon
basePath: __DIR__ . '/../../../Modules/Media/Files' . $path,
virtualPath: $path,
pathSettings: PathSettings::FILE_PATH,
type: $profileImageType->id,
tag: $profileImageType->id,
rel: $item->id,
mapper: ItemMapper::class,
field: 'files'
Expand Down Expand Up @@ -693,6 +792,23 @@ public function apiItemL11nCreate(RequestAbstract $request, ResponseAbstract $re
return;
}

// @question This kind of thing should happen in a separate function?!
$exists = ItemL11nMapper::get()
->where('ref', $request->getDataInt('item') ?? 0)
->where('type', $request->getDataInt('type') ?? 0)
->where('language', $request->getDataString('language') ?? $request->header->l11n->language)
->execute();

if ($exists->id > 0) {
$response->header->status = RequestStatusCode::R_400;
$this->createInvalidCreateResponse($request, $response, $val);

return;
}

// @todo This is always the same for every l11n.
// Create a L11n module or something similar to the Attribute module,
// where this can be done for every other module
$itemL11n = $this->createItemL11nFromRequest($request);
$this->createModel($request->header->account, $itemL11n, ItemL11nMapper::class, 'item_l11n', $request->getOrigin());
$this->createStandardCreateResponse($request, $response, $itemL11n);
Expand Down Expand Up @@ -784,7 +900,7 @@ public function apiFileCreate(RequestAbstract $request, ResponseAbstract $respon
basePath: __DIR__ . '/../../../Modules/Media/Files' . $path,
virtualPath: $path,
pathSettings: PathSettings::FILE_PATH,
type: $request->getDataInt('type'),
tag: $request->getDataInt('tag'),
rel: $item->id,
mapper: ItemMapper::class,
field: 'files'
Expand Down
Loading

0 comments on commit e246180

Please sign in to comment.