Skip to content

Commit 08dd99c

Browse files
commit
1 parent c56de84 commit 08dd99c

File tree

5 files changed

+160
-7
lines changed

5 files changed

+160
-7
lines changed

app/base/controllers/Admin/Commerce/Orders.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,17 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For
134134

135135
case 'view':
136136

137-
if (in_array($order->getOrderStatus()?->getStatus(), [OrderStatus::PAID, OrderStatus::WAITING_FOR_PAYMENT])) {
137+
if (!is_null($order->getOrderPayment())) {
138138
$orderPayment = OrderPayment::getCollection()->where(['order_id' => $order->getId()])->addOrder(['created_at' => 'DESC'])->getFirst();
139139
if ($orderPayment) {
140-
$this->addActionLink('payment-btn', 'payment-btn', $this->getHtmlRenderer()->getIcon('dollar-sign') . ' ' . $this->getUtils()->translate('Payment', locale: $this->getCurrentLocale()), $this->getUrl('admin.commerce.orderpayments') . '?' . http_build_query(['action' => 'view', 'payment_id' => $orderPayment->getId()]) );
140+
$this->addActionLink(
141+
'payment-btn',
142+
'payment-btn',
143+
$this->getHtmlRenderer()->getIcon('dollar-sign') . ' ' . $this->getUtils()->translate('Payment', locale: $this->getCurrentLocale()),
144+
//$this->getUrl('admin.commerce.orderpayments') . '?' . http_build_query(['action' => 'view', 'payment_id' => $orderPayment->getId()]) ,
145+
$this->getUrl('crud.app.base.controllers.admin.json.orderpayment', ['id' => $this->getRequest()->query->get('order_id')]) . '?order_id=' . $this->getRequest()->query->get('order_id') . '&action=payment',
146+
'btn btn-sm btn-light inToolSidePanel'
147+
);
141148
}
142149
}
143150

@@ -311,7 +318,7 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For
311318
])->addField('ship_'.$orderItem->getId().'_qty', [
312319
'type' => 'number',
313320
'title' => 'Quantity',
314-
'default_value' => $orderItem->getQuantity(),
321+
'default_value' => $orderItem->remainingShippableQuantity(),
315322
'label_class' => 'mr-2',
316323
'container_class' => 'col-11 d-flex',
317324
]);
@@ -350,8 +357,30 @@ protected function getCardHtml(string $title, string $content): string
350357
*/
351358
public function formValidate(FAPI\Form $form, &$form_state): bool|string
352359
{
353-
//$values = $form->values();
354-
// @todo : check if page language is in page website languages?
360+
/**
361+
* @var OrderModel $order
362+
*/
363+
$order = $this->getObject();
364+
365+
$values = $form->values();
366+
367+
switch ($values['action']) {
368+
case 'ship':
369+
$hasAtLeastOne = false;
370+
foreach ($order->getItems() as $orderItem) {
371+
/** @var OrderItem $orderItem */
372+
if ($orderItem->requireShipping()) {
373+
if ($values['row_'.$orderItem->getId()]['ship_'.$orderItem->getId()]) {
374+
$hasAtLeastOne = true;
375+
}
376+
}
377+
}
378+
if (!$hasAtLeastOne) {
379+
return $this->getUtils()->translate('You must select at least one item to ship.', locale: $this->getCurrentLocale());
380+
}
381+
break;
382+
}
383+
355384
return true;
356385
}
357386

@@ -424,6 +453,16 @@ public function formSubmitted(FAPI\Form $form, &$form_state): mixed
424453

425454
$order->ship($values['shipping_method'], $values['shipment_code'], $shipmentItems);
426455

456+
if (!$order->requiresShipping()) {
457+
$order->setOrderStatus(OrderStatus::getCollection()->where(['status' => OrderStatus::SHIPPED])->getFirst())->persist();
458+
459+
$this->setAdminActionLogData('Order ' . $order->getId() . ' marked as shipped.');
460+
461+
$this->addInfoFlashMessage($this->getUtils()->translate("Order marked as shipped."));
462+
463+
return new JsonResponse(['success' => true, 'js' => '$(\'#admin\').appAdmin(\'closeSidePanel\'); document.location.reload()']);
464+
}
465+
427466
// $this->addInfoFlashMessage($this->getUtils()->translate("Shipment created."));
428467

429468
return new JsonResponse(['success' => true, 'js' => '$(\'#admin\').appAdmin(\'closeSidePanel\')']);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* SiteBase
5+
* PHP Version 8.3
6+
*
7+
* @category CMS / Framework
8+
* @package Degami\Sitebase
9+
* @author Mirko De Grandis <degami@github.com>
10+
* @license MIT https://opensource.org/licenses/mit-license.php
11+
* @link https://github.com/degami/sitebase
12+
*/
13+
14+
namespace App\Base\Controllers\Admin\Json;
15+
16+
use Degami\Basics\Exceptions\BasicException;
17+
use App\Base\Abstracts\Controllers\AdminJsonPage;
18+
use App\Base\Controllers\Admin\Commerce\Orders;
19+
use App\Base\Models\Order;
20+
use DI\DependencyException;
21+
use DI\NotFoundException;
22+
23+
/**
24+
* order shipment JSON
25+
*/
26+
class Orderpayment extends AdminJsonPage
27+
{
28+
/**
29+
* return route path
30+
*
31+
* @return string
32+
*/
33+
public static function getRoutePath(): string
34+
{
35+
return 'json/orders/{id:\d+}/payment';
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*
41+
* @return string
42+
*/
43+
public static function getAccessPermission(): string
44+
{
45+
return 'administer_orders';
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*
51+
* @return array
52+
* @throws BasicException
53+
* @throws DependencyException
54+
* @throws NotFoundException
55+
*/
56+
protected function getJsonData(): array
57+
{
58+
$route_data = $this->getRouteData();
59+
60+
$order = Order::load($this->getRequest()->query->get('order_id'));
61+
62+
$orderPayment = $order->getOrderPayment();
63+
64+
$data = $orderPayment->getData();
65+
$data['additional_data'] = json_decode($orderPayment->getAdditionalData(), true);
66+
$html = (string) $this->getHtmlRenderer()->renderArrayOnTable($data);
67+
68+
return [
69+
'success' => true,
70+
'params' => $this->getRequest()->query->all(),
71+
'html' => $html,
72+
'js' => "",
73+
];
74+
}
75+
}

app/base/models/OrderItem.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ public function requireShipping(): bool
180180
return true;
181181
}
182182

183+
return $this->remainingShippableQuantity() > 0;
184+
}
185+
186+
public function remainingShippableQuantity(): int
187+
{
188+
if (!$this->getProduct()) {
189+
return 0;
190+
}
191+
192+
if (!$this->getProduct()->isPhysical()) {
193+
return 0;
194+
}
195+
196+
if (is_null($this->getId())) {
197+
return $this->getQuantity();
198+
}
199+
183200
$stmt = App::getInstance()->getPdo()->prepare("
184201
SELECT SUM(quantity)
185202
FROM order_shipment_item
@@ -190,6 +207,6 @@ public function requireShipping(): bool
190207

191208
$sumShipmentsQty = $stmt->fetchColumn();
192209

193-
return $sumShipmentsQty < $this->getQuantity();
210+
return max(0, $this->getQuantity() - (int)$sumShipmentsQty);
194211
}
195212
}

js/src/admin/admin.methods.alert.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@
7878
onConfirm: callback
7979
});
8080
},
81+
info: function(text, callback) {
82+
let that = this;
83+
84+
$(that).appAdmin('showAlertDialog', {
85+
type: 'info',
86+
title: __('Info'),
87+
message: text,
88+
onConfirm: callback
89+
});
90+
},
8191
warning: function(text, callback) {
8292
let that = this;
8393

js/src/admin/admin.methods.panel.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,19 @@
7777
data: formData,
7878
processData: false,
7979
contentType: false,
80-
success: function(data) {
80+
success: function(data, textStatus, jqXHR) {
81+
// if response is html and a tag with class 'alert' is found, show alert dialog
82+
if (jqXHR.getResponseHeader("Content-Type").search("text/html") !== -1) {
83+
let tempDiv = $('<div>').html(data);
84+
let alertDiv = tempDiv.find('.alert');
85+
if (alertDiv.length > 0) {
86+
// based on alert (alert-danger, alert-info) class, determine type and call alertType method
87+
let alertType = alertDiv.hasClass('alert-danger') ? 'error' : 'info';
88+
$(that).appAdmin(alertType, alertDiv.text());
89+
return;
90+
}
91+
}
92+
8193
if( $.trim(data.js) != '' ){
8294
eval(data.js);
8395
return;

0 commit comments

Comments
 (0)