You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.2 KiB
102 lines
3.2 KiB
<?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) {
|
|
if (!$item->supplier_id) { //自营产品供应商为空
|
|
continue;
|
|
}
|
|
$supplier = Supplier::query()->where('id',$item->supplier_id)->lockForUpdate()->first();
|
|
if (!$supplier) { //自营产品供应商为空
|
|
continue;
|
|
}
|
|
$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 ['确定要设置为“线下已付款”吗?', ''];
|
|
}
|
|
}
|