6
0
Fork 0

order auto complete

base
panliang 2023-02-26 10:11:14 +08:00
parent 22155ec344
commit e52f42d0b6
9 changed files with 124 additions and 47 deletions

View File

@ -66,14 +66,14 @@ class DeskController extends AdminController
$show->field('store_id')->as(fn() => data_get($this->store, 'name'));
$show->field('name');
$show->field('status')->bool();
$show->field('wxcode');
$show->field('wxcode')->image('', 100, 100);
$show->field('remarks');
$model = $show->model();
$category_id = data_get($model->extra, 'category_id');
if ($category_id) {
$category = ProductCategory::find($category_id);
$show->field('category_id')->as(fn() => $category ? $category->name : $category_id);
if (!is_null($category_id)) {
$name = $category_id ? ProductCategory::where('id', $category_id)->value('name') : '顶级';
$show->field('category_id')->as($name);
}
$show->disableEditButton();

View File

@ -53,8 +53,8 @@ class ProductController extends AdminController
$grid->column('store.title', '店铺');
$grid->column('productSku.category.name', '分类');
$grid->column('productSku.name', '名称');
$grid->column('productSku.sell_price', '销售价')->display(fn ($value) => bcdiv($value, 100, 2));
$grid->column('productSku.specs', '规格')->label();
$grid->column('productSku.sell_price', '销售价')->display(fn ($value) => bcdiv($value, 100, 2));
$grid->column('productSku.cost_price', '成本价')->display(fn ($value) => bcdiv($value, 100, 2));
$grid->column('profit', '毛利')->display(function () {
$sell_price = data_get($this->productSku, 'sell_price');

View File

@ -40,7 +40,10 @@ class OrderReduce extends Form implements LazyRenderable
$orderId = $this->payload['id'] ?? 0;
$order = Order::findOrFail($orderId);
$reduceAmount = $input['reduce_amount']??0;
//获取调整价格;
if ($reduceAmount < 0) {
throw new BizException('价格不能小于0');
}
// 获取调整价格;
if ($reduceAmount > $order->total_amount) {
throw new BizException('订单价格无法上浮');
}
@ -49,7 +52,7 @@ class OrderReduce extends Form implements LazyRenderable
}
$orderService = new OrderService();
$reduced = $order->total_amount-$reduceAmount;
$reduced = $order->total_amount - $reduceAmount;
//判断是否在当前操作人调价权限范围内
$adminUser = Admin::user();
if (! $adminUser->isAdministrator() && ! $adminUser->inReduceRange($reduced)) {

View File

@ -174,11 +174,19 @@ class OrderPackageService
*/
public function createAll(Order $order, $params = [])
{
$shipping_company = data_get($params, 'shipping_company');
$shipping_number = data_get($params, 'shipping_number');
$products = [];
foreach($order->products as $item) {
$shipping_company = data_get($params, 'shipping_company', '自提');
$shipping_number = data_get($params, 'shipping_number', '0');
$products = $order->products->map(function ($item) {
return [
'order_product_id' => $item->id,
'quantity' => $item->quantity
];
});
}
return $this->createPackage($order, [
'shipping_company' => $shipping_company,
'shipping_number' => $shipping_number,
'packages' => $products,
]);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Models\Store\{Desk, ProductSku};
use App\Models\Order;
/**
* 自动完成订单
*
* 发货: 自提发货, 修改门店商品库存, 添加库存记录(提货)
* 收货
*/
class OrderAutoComplete
{
public function handle($event)
{
$order = $event->order;
// 门店, 桌号订单
if ($order->source_type === Desk::class && $store = $order->store) {
$order = Order::find($order->id);
$package = (new \App\Admin\Services\OrderPackageService())->createAll($order);
// 添加门店库存记录
foreach($package->orderProducts as $item) {
$sku = ProductSku::where('store_id', $store->id)->where('product_sku_id', $item->sku_id)->first();
if ($sku) {
$amount = $item->pivot->quantity;
$balance = $sku->amount - $amount;
$store->stockLogs()->create([
'amount' => 0 - $amount,
'product_sku_id' => $item->sku_id,
'remarks' => '桌号订自动提货',
'balance' => $balance,
'source_id' => $order->id,
'source_type' => $order->getMorphClass(),
'tag_id' => 3
]);
$sku->update(['amount' => $balance]);
}
}
(new \App\Services\OrderService())->confirm($order);
}
}
}

View File

@ -6,6 +6,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Exceptions\BizException;
use App\Services\OrderService;
use App\Models\Store\Desk;
/**
* 打印订单小票

View File

@ -30,6 +30,10 @@ class Store extends Model
$model->stockLogs()->delete();
// 管理员记录
$model->administrators()->detach();
// 桌号记录
$model->desks()->delete();
// 设备记录
$model->devices()->delete();
});
}

View File

@ -22,6 +22,7 @@ class EventServiceProvider extends ServiceProvider
\App\Listeners\OrderPackage::class,
\App\Listeners\OrderDistribute::class,
\App\Listeners\OrderPrint::class,
\App\Listeners\OrderAutoComplete::class,
],
];

View File

@ -444,7 +444,7 @@ class OrderService
'weight' => $sku->weight,
'sell_price' => $sku->sell_price,
'vip_price' => $sku->vip_price,
'sales_value' => $sku->sales_value,
'sales_value' => $product['total_sales_value'],
'market_price' => $sku->cost_price,
'cost_price' => $sku->cost_price,
'quantity' => $qty,
@ -1363,6 +1363,7 @@ class OrderService
* 打印订单小票
*
* @param Order $order 订单
* @return array 打印结果 [{code: 0(成功), msg: ''}, ...]
* @throws BizException
*/
public function print(Order $order)
@ -1378,43 +1379,53 @@ class OrderService
if (!$desk) {
throw new BizException('未找到桌号');
}
$devices = $store->devices()->where('status', 1)->get();
if ($devices->count() == 0) {
throw new BizException('门店未找到可用的设备');
}
$templateId = data_get($store->extra, 'order_desk_print_template');
if ($devices->count() > 0 && $templateId) {
$service = PrintService::make();
$products = [];
foreach($order->products as $item) {
array_push($products, [
'name' => $item->name,
'price' => round($item->sell_price / 100, 2, PHP_ROUND_HALF_DOWN),
'amount' => $item->quantity,
'money' => round($item->total_amount / 100, 2, PHP_ROUND_HALF_DOWN),
]);
}
$data = [
'name' => $store->title,
'sn' => $order->sn,
'time' => $order->created_at->format('Y-m-d H:i:s'),
'desk' => $desk->name,
'products' => $products,
'total' => round($order->total_amount, 2, PHP_ROUND_HALF_DOWN),
'remarks' => $order->note ?: '',
];
foreach($devices as $item) {
$result = $service->template($item->device_no, $templateId, $data);
$status = data_get($result, 'code') == 0 ? DeviceRecord::STATUS_SUCCESS : DeviceRecord::STATUS_FAIL;
// 添加打印的日志记录
$item->records()->create([
'data' => $data,
'device_id' => $item->id,
'result' => $result,
'resource_id' => $order->id,
'resource_type' => $order->getMorphClass(),
'status' => $status,
'store_id' => $store->id
]);
}
if (!$templateId) {
throw new BizException('门店未配置打印模板');
}
$service = PrintService::make();
$products = [];
foreach($order->products as $item) {
array_push($products, [
'name' => $item->name,
'price' => round($item->sell_price / 100, 2, PHP_ROUND_HALF_DOWN),
'amount' => $item->quantity,
'money' => round($item->total_amount / 100, 2, PHP_ROUND_HALF_DOWN),
]);
}
$data = [
'name' => $store->title,
'sn' => $order->sn,
'time' => $order->created_at->format('Y-m-d H:i:s'),
'desk' => $desk->name,
'products' => $products,
'total' => round($order->total_amount / 100, 2, PHP_ROUND_HALF_DOWN),
'remarks' => $order->note ?: '',
];
$results = [];
foreach($devices as $item) {
$result = $service->template($item->device_no, $templateId, $data);
$status = data_get($result, 'code') == 0 ? DeviceRecord::STATUS_SUCCESS : DeviceRecord::STATUS_FAIL;
array_push($results, $result);
// 添加打印的日志记录
$item->records()->create([
'data' => $data,
'device_id' => $item->id,
'result' => $result,
'resource_id' => $order->id,
'resource_type' => $order->getMorphClass(),
'status' => $status,
'store_id' => $store->id
]);
}
return $results;
}
}