105 lines
3.8 KiB
PHP
105 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Peidikeji\Order\Form;
|
|
|
|
use App\Services\Kuaidi100Service;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Form\NestedForm;
|
|
use Dcat\Admin\Models\Administrator;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Peidikeji\Order\Enums\ShipWay;
|
|
use Peidikeji\Order\Exceptions\OrderException;
|
|
use Peidikeji\Order\Models\Order;
|
|
use Peidikeji\Order\Models\OrderGoods;
|
|
use Peidikeji\Order\OrderService;
|
|
|
|
class ShipForm extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
protected $buttons = ['reset' => false, 'submit' => true];
|
|
|
|
public function handle(array $input)
|
|
{
|
|
try {
|
|
if (count($input['goods']) === 0) {
|
|
throw new OrderException('请选择要发货的商品');
|
|
}
|
|
DB::beginTransaction();
|
|
$order = Order::findOrFail($this->payload['id']);
|
|
|
|
$order->update(['ship_way' => $input['ship_way']]);
|
|
|
|
$code = data_get($input, 'company_code');
|
|
$company = data_get($input, 'company');
|
|
$sn = data_get($input, 'sn');
|
|
OrderService::make()->ship($order, $input['goods'], compact('code', 'company', 'sn'));
|
|
|
|
$admin = Admin::user();
|
|
$order->options()->create([
|
|
'user_type' => get_class($admin),
|
|
'user_id' => $admin->id,
|
|
'description' => '管理员: ' . $admin->name . ' 发货',
|
|
'attribute' => $input,
|
|
]);
|
|
|
|
DB::commit();
|
|
return $this->response()->success('操作成功')->refresh();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $this->response()->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function form()
|
|
{
|
|
$orderGoods = OrderGoods::where('order_id', $this->payload['id'])->get();
|
|
$goods = [];
|
|
foreach ($orderGoods as $item) {
|
|
array_push($goods, [
|
|
'order_goods_id' => $item->id,
|
|
'cover_image' => $item->cover_image,
|
|
'goods_name' => $item->goods_name,
|
|
'amount' => $item->amount - $item->ship_amount,
|
|
'spec' => $item->spec,
|
|
'ship_amount' => $item->ship_amount,
|
|
'total_amount' => $item->amount,
|
|
]);
|
|
}
|
|
$this->fill(['goods' => $goods, 'ship_way' => $this->payload['ship_way']]);
|
|
|
|
$this->select('ship_way')->options(ShipWay::options())->required();
|
|
$this->text('company', '快递公司');
|
|
$this->text('company_code', '快递公司编码');
|
|
$this->text('sn', '快递单号');
|
|
$this->table('goods', '', function (NestedForm $table) {
|
|
$spec = data_get($table->model(), 'goods.spec');
|
|
$table->hidden('order_goods_id');
|
|
$table->hidden('cover_image');
|
|
$table->display('goods_name', '商品');
|
|
if ($spec) {
|
|
$table->display('spec', '规格')->with(function ($v) {
|
|
if (!$v) {
|
|
return '';
|
|
}
|
|
if (is_string($v)) {
|
|
return $v;
|
|
}
|
|
$html = '';
|
|
foreach($v as $i) {
|
|
$html .= '<span class="label bg-primary mr-1">'.$i['value'].'</span>';
|
|
}
|
|
return $html;
|
|
});
|
|
}
|
|
$table->display('total_amount', '总数量');
|
|
$table->display('ship_amount', '已发');
|
|
$table->number('amount', '数量')->default(1)->min(0)->prepend('')->append('');
|
|
})->horizontal(false)->setFieldClass('col-12')->options(['allowCreate' => false])->required();
|
|
}
|
|
}
|