153 lines
5.1 KiB
PHP
153 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Admin\Services\OrderPackageService;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\{Order, Tag};
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
use Dcat\Admin\Admin;
|
|
use App\Models\Store\ProductSku as StoreProductSku;
|
|
use App\Models\Store\{Store, StockLog};
|
|
|
|
class OrderPackage extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
/**
|
|
* 权限判断,如不需要可以删除此方法
|
|
*
|
|
* @param Model|Authenticatable|HasPermissions|null $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function authorize($user): bool
|
|
{
|
|
return $user->can('dcat.admin.orders.create_package');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$orderId = $this->payload['id'] ?? 0;
|
|
|
|
$order = Order::findOrFail($orderId);
|
|
try {
|
|
DB::beginTransaction();
|
|
$orderPackageService = new OrderPackageService();
|
|
$orderPackageService->createPackage($order, $input);
|
|
// 店铺发货, 添加出库记录
|
|
$store_id = data_get($input, 'store_id', $order->store_id);
|
|
$tag_id = StockLog::TAG_SEND;
|
|
if ($store_id) {
|
|
$packageProducts = $input['packages'];
|
|
$operator = Admin::user();
|
|
$store = Store::findOrFail($store_id);
|
|
foreach($packageProducts as $item) {
|
|
$order_product = $order->products()->findOrFail($item['order_product_id']);
|
|
$amount = $item['quantity'];
|
|
$sku_id = $order_product->sku_id;
|
|
$product = $store->productSkus()->findOrFail($sku_id);
|
|
if ($product->pivot->amount - $amount < 0) {
|
|
throw new BizException('店铺的 ' . $product->name .' 库存不足');
|
|
}
|
|
|
|
$store->productSkus()->updateExistingPivot($product->id, [
|
|
'amount' => $product->pivot->amount - $amount
|
|
]);
|
|
$store->stockLogs()->create([
|
|
'operator_type' => $operator->getMorphClass(),
|
|
'operator_id' => $operator->id,
|
|
'operator_name' => $operator->name,
|
|
'source_type' => Order::class,
|
|
'source_id' => $order->id,
|
|
'amount' => 0-$amount,
|
|
'product_sku_id' => $product->id,
|
|
'remarks' => '后台发货',
|
|
'tag_id' => $tag_id,
|
|
]);
|
|
}
|
|
}
|
|
DB::commit();
|
|
} catch (BizException $e) {
|
|
DB::rollBack();
|
|
report($e);
|
|
throw new BizException('操作失败:'.$e->getMessage());
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('操作失败:'.$th->getMessage());
|
|
}
|
|
return $this->response()
|
|
->success(__('admin.update_succeeded'))
|
|
->refresh();
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$orderId = $this->payload['id'] ?? 0;
|
|
|
|
$order = Order::findOrFail($orderId);
|
|
|
|
$this->select('shipping_company')->options([
|
|
'圆通速递'=>'圆通速递',
|
|
'韵达快递'=>'韵达快递',
|
|
'中通快递'=>'中通快递',
|
|
'申通快递'=>'申通快递',
|
|
'百世快递'=>'百世快递',
|
|
'EMS'=>'EMS',
|
|
'顺丰速运'=>'顺丰速运',
|
|
'德邦快递'=>'德邦快递',
|
|
'提货' => '提货',
|
|
])->default('韵达快递')->required();
|
|
$this->text('shipping_number')->required();
|
|
|
|
$list = StoreProductSku::with(['store'])->whereIn('product_sku_id', $order->products->pluck('sku_id'))->get();
|
|
$stores = $list->pluck('store');
|
|
$this->select('store_id', '发货门店')->options($stores->pluck('title', 'id'))->value($order->store_id);
|
|
$products = $order->products->filter(function ($items) {
|
|
if ($items->after_sale_state != 1) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
});
|
|
$options= [];
|
|
$products->map(function ($item, $key) use (&$options) {
|
|
$options[$item->id]= '剩余【'.$item->remain_quantity.'】'.$item->name;
|
|
});
|
|
$this->hasMany('packages', function (Form $form) use ($options) {
|
|
$form->select('order_product_id')->options($options);
|
|
$form->number('quantity')->default(1)->min(1);
|
|
})->required();
|
|
|
|
$this->disableResetButton();
|
|
}
|
|
|
|
/**
|
|
* The data of the form.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function default()
|
|
{
|
|
return [
|
|
// 'name' => 'John Doe',
|
|
// 'email' => 'John.Doe@gmail.com',
|
|
];
|
|
}
|
|
}
|