Files
thetool/application/WarehouseEShopOrder/WarehouseEShopOrderController.php
2024-07-24 13:25:49 +00:00

111 lines
4.8 KiB
PHP

<?php
class WarehouseEShopOrderController extends TTCrud {
protected string $headerTitle = 'Energie Steiermark Bestellungen';
protected bool $createText = false;
protected array $columns = [
['key' => 'id', 'text' => 'ID', 'modal' => false],
['key' => 'status', 'text' => 'Status', 'required' => true],
['key' => 'deliveryMode', 'text' => 'Liefermodus', 'required' => true, 'modal' => ['type' => 'select', 'items' => [
['value' => 'singleAddress', 'text' => 'Einzelne Adresse'],
// ['value' => 'multipleAddresses', 'text' => 'Mehrere Adressen'],
]]],
['key' => 'deliveryAddressName', 'text' => 'Lieferadresse Name', 'required' => true, 'table' => false],
['key' => 'deliveryAddressLine', 'text' => 'Lieferadresse', 'required' => true, 'required_length' => 4],
['key' => 'deliveryAddressPLZ', 'text' => 'Lieferadresse PLZ', 'required' => true, 'regex' => '/^\d{4}$/', 'table' => false],
['key' => 'deliveryAddressCity', 'text' => 'Lieferadresse Stadt', 'required' => true, 'required_length' => 3, 'table' => false],
['key' => 'create', 'text' => 'Erstellt', 'required' => true, 'modal' => false, 'filter' => 'datetime'],
['key' => 'createBy', 'text' => 'Erstellt von', 'required' => true,'modal' => ['type' => 'select', 'items' => []]],
['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center']],
];
protected array $additionalActions = [
['key' => 'openHistory', 'title' => 'Historie', 'class' => 'fas fa-history text-primary'],
];
protected array $infoMessages = [
'create' => 'Bestellung wurde erfolgreich erstellt, sie erhalten in Kürze eine Bestätigungsmail',
'update' => 'Lagerort wurde aktualisiert',
'delete' => 'Lagerort wurde gelöscht',
'noChanges' => 'Keine Änderungen',
];
public function permissionCheck(): bool {
return $this->user->can(["WarehouseEShop"]);
}
protected function prepareCrudConfig() {
$users = array_map(function($user) {
return ['value' => intval($user->id), 'text' => $user->name];
}, UserModel::search(['employee' => true]));
$this->columns[8]['modal']['items'] = $users;
}
protected function createOrderAction() {
//TODO: change this to beforeCreate and afterCreate
$json = json_decode(file_get_contents('php://input'), true);
$shoppingCart = $json['shoppingCart'];
unset($json['shoppingCart']);
$json['status'] = 'new';
$json['create'] = time();
$json['createBy'] = $this->user->id;
Helper::validateArray($json, $this->getCheckArray());
$id = WarehouseEShopOrderModel::create([
'status' => 'new',
'deliveryMode' => $json['deliveryMode'],
'deliveryAddressName' => $json['deliveryAddressName'],
'deliveryAddressLine' => $json['deliveryAddressLine'],
'deliveryAddressPLZ' => $json['deliveryAddressPLZ'],
'deliveryAddressCity' => $json['deliveryAddressCity'],
'create' => $json['create'],
'createBy' => $json['createBy'],
]);
// now create WarehouseEShopOrderItems for each item in the shopping cart
foreach ($shoppingCart as $item) {
// itemId can either be P-[PACKETID] or I-[ARTICLEID]
// parse this and either fill articleId or articlePacketId for warehouseEShopOrderItem
if (strpos($item['itemId'], 'P-') === 0) {
WarehouseEShopOrderItemModel::create([
'orderId' => $id,
'articlePacketId' => intval(substr($item['itemId'], 2)),
'quantity' => intval($item['amount']),
]);
} else if (strpos($item['itemId'], 'I-') === 0) {
WarehouseEShopOrderItemModel::create([
'orderId' => $id,
'articleId' => intval(substr($item['itemId'], 2)),
'quantity' => intval($item['amount']),
]);
} else {
self::returnJson(['success' => false, 'message' => 'Invalid item id']);
die();
}
}
self::returnJson(['success' => true,
'message' => $this->infoMessages['create'],
'id' => $id]);
$json['id'] = $id;
die(json_encode($json));
}
protected function beforeUpdate($postData): bool {
(new WarehouseHistoryController)->create($postData, $this->mod);
return true;
}
protected function getHistoryAction() {
self::returnJson((new WarehouseHistoryController)->getHistory($this->request->id, $this->mod, $this->columns));
}
}