|
|
<?php
namespace App\AdminAgent\Extensions\Grid;use App\Common\OrderStatus;use App\Common\StatementType;use App\Models\Order;use App\Models\Agent;use App\Models\OrderProductItem;use App\Models\Supplier;use App\Service\WithdrawalService;use App\Traits\DemandTraits;use App\Traits\StatementTraits;use Dcat\Admin\Admin;use Dcat\Admin\Grid\RowAction;use Illuminate\Support\Facades\DB;
/** * 改变订单状态 * Class ChangeOrderStatus * @package App\AdminAgent\Extensions\Grid */class ChangeOrderStatus extends RowAction{ protected $title = '设为已付款';
protected function html() { $this->appendHtmlAttribute('class', 'btn btn-sm btn-success'); $this->defaultHtmlAttribute('href', 'javascript:;');
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>"; }
public function handle() { DB::beginTransaction(); try { $order = Order::firstWhere(['id' => $this->getKey(), 'agent_id' => Admin::user()->id, 'status' => OrderStatus::OFFLINE_UNPAID]); if (!$order) { return $this->response()->error("订单不存在或已处理过了")->refresh(); } $agent = Agent::query()->where('id', Admin::user()->id)->lockForUpdate()->first(); if ($agent->balance < $order->price) { return $this->response()->error("账户余额不足,请先进行充值")->refresh(); }
$order->status = OrderStatus::OFFLINE_PAID; $order->verify_code = uniqid(); //生成核销码
$order->save(); //扣供应商余额
$service = new WithdrawalService(); $agentPrice = bcsub($agent->balance,$order->price,6); $agent->balance = $agentPrice; $agent->save(); $statementCreate[] = $service->createByOrder( bcmul($agentPrice, -1, 6), StatementType::ORDER, $order->agent_id, DemandTraits::$col[0], $order->id, StatementTraits::$type[0] );
$orderItem = OrderProductItem::query()->where('order_id',$order->id)->get();
foreach ($orderItem as $item) { $supplier = Supplier::query()->where('id',$item->supplier_id)->lockForUpdate()->first(); $supplier->balance = bcadd($supplier->balance,$item->price,6); $supplier->save(); $statementCreate[] = $service->createByOrder( $item->price, StatementType::ORDER, $item->supplier_id, DemandTraits::$col[1], $order->id, StatementTraits::$type[0] ); }
if (!empty($statementCreate)) { $order->statement()->createMany($statementCreate); }
DB::commit(); return $this->response()->success("操作成功,已设置为“线下已付款”")->refresh(); } catch (\Exception $e) { DB::rollBack(); return $this->response()->error($e->getMessage()); } }
public function confirm() { return ['确定要设置为“线下已付款”吗?', '']; }}
|