88 lines
2.8 KiB
PHP
88 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use App\Component\Models\OrderItems;
|
|
use App\Component\Models\Orders;
|
|
use App\Console\Commands\HRCCommand;
|
|
use App\Console\Commands\HRCCommandInterface;
|
|
|
|
class POSTOrderItem extends HRCCommand implements HRCCommandInterface {
|
|
protected $signature = 'postorderitem';
|
|
|
|
public function command($input, $output = null) {
|
|
if (isset($input['task']) && isset($input['order_id'])) {
|
|
if ($input['task'] == 'create' && isset($input['order_id'])) {
|
|
$order_item = new OrderItems;
|
|
$order_item->order_id = $input['order_id'];
|
|
$order_item->item_id = $input['item_id'];
|
|
$order_item->item_count = $input['item_count'];
|
|
$order_item->item_price = $input['item_price'];
|
|
$order_item->staff_id = 1;
|
|
$order_item->save();
|
|
|
|
return [
|
|
'status' => 'success',
|
|
'order' => $order_item,
|
|
'message' => 'Товар добавлен в заказ №' . $input['order_id'],
|
|
];
|
|
}
|
|
|
|
if ($input['task'] == 'delete' && isset($input['id'])) {
|
|
$order_item = OrderItems::where('order_id', '=', $input['order_id'])->where('id', '=', $input['id'])->first();
|
|
$order_item->delete();
|
|
$order = Orders::where('id', '=', $input['order_id'])->first();
|
|
$orderItems = OrderItems::where('order_id', '=', $input['order_id'])->get();
|
|
$totalCount = 0;
|
|
$totalPrice = 0;
|
|
foreach ($orderItems as $key => $item) {
|
|
$totalCount = $totalCount + $item->item_count;
|
|
$totalPrice = $totalPrice + $item->item_count * $item->item_price;
|
|
}
|
|
$order->total_count = $totalCount;
|
|
$order->total_price = $totalPrice;
|
|
$order->save();
|
|
return [
|
|
'status' => 'success',
|
|
'message' => 'Позиция удалена',
|
|
];
|
|
}
|
|
|
|
if ($input['task'] == 'update' && isset($input['id']) && isset($input['item_count'])) {
|
|
$order_item = OrderItems::where('order_id', '=', $input['order_id'])->where('id', '=', $input['id'])->first();
|
|
if ($input['item_count'] == 0) {
|
|
$order_item->delete();
|
|
return [
|
|
'status' => 'success',
|
|
'message' => 'Позиция удалена',
|
|
];
|
|
} else {
|
|
$order_item->item_count = $input['item_count'];
|
|
$order_item->save();
|
|
|
|
$order = Orders::where('id', '=', $input['order_id'])->first();
|
|
$orderItems = OrderItems::where('order_id', '=', $input['order_id'])->get();
|
|
$totalCount = 0;
|
|
$totalPrice = 0;
|
|
foreach ($orderItems as $key => $item) {
|
|
//$totalCount = $totalCount + $item->item_count;
|
|
$totalPrice = $totalPrice + $item->item_count * $item->item_price;
|
|
}
|
|
$order->total_count = count($orderItems);
|
|
$order->total_price = $totalPrice;
|
|
$order->save();
|
|
return [
|
|
'status' => 'success',
|
|
'message' => 'Заказ обновлен',
|
|
];
|
|
}
|
|
}
|
|
|
|
} else {
|
|
return [
|
|
'status' => 'success',
|
|
'error_message' => 'Проверьте введенные данные',
|
|
];
|
|
}
|
|
}
|
|
} |