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
2.4 KiB
102 lines
2.4 KiB
<?php
|
|
|
|
namespace App\AdminSupplier\Extensions\Grid;
|
|
|
|
use App\Common\ProductStatus;
|
|
use App\Models\IndustryOrder;
|
|
use App\Models\IndustryProduct;
|
|
use App\Models\IndustryProductSpec;
|
|
use Dcat\Admin\Grid\RowAction;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* 行业订单审核
|
|
* Class AuditSupplier
|
|
* @package App\Admin\Extensions\Grid
|
|
*/
|
|
class IndustryOrderAudit extends RowAction
|
|
{
|
|
private $action;
|
|
|
|
public function __construct($title = null, $action = 1)
|
|
{
|
|
parent::__construct($title);
|
|
$this->action = $action; //$action:1=通过;2=拒绝
|
|
$this->title = $action == 1 ? '通过' : '拒绝';
|
|
}
|
|
|
|
protected function html()
|
|
{
|
|
$class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger';
|
|
$this->appendHtmlAttribute('class', $class);
|
|
$this->defaultHtmlAttribute('href', 'javascript:;');
|
|
|
|
return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>";
|
|
}
|
|
|
|
protected function actionScript()
|
|
{
|
|
if ($this->action == 2) {
|
|
return <<<JS
|
|
function (data, target, action) {
|
|
data.audit_opinion = prompt('请输入拒绝理由');
|
|
if (!data.audit_opinion) {
|
|
return false;
|
|
}
|
|
}
|
|
JS;
|
|
}
|
|
return parent::actionScript();
|
|
}
|
|
|
|
public function handle(Request $request)
|
|
{
|
|
$id = $this->getKey();
|
|
$status = $request->action == 1 ? ProductStatus::ON_SALE : ProductStatus::REFUSE;
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$order = IndustryOrder::with(['industryProduct', 'spec'])
|
|
->where('audit_status', 0)->find($id);
|
|
if (!$order) {
|
|
throw new \Exception('订单不存在或已经审核过了');
|
|
}
|
|
|
|
if (!is_null($request->audit_opinion)) {
|
|
$order->audit_opinion = $request->audit_opinion;
|
|
}
|
|
$order->audit_status = $status;
|
|
$order->save();
|
|
|
|
# 如果是拒绝,产品及规格加回库存
|
|
if ($status == ProductStatus::REFUSE && !empty($order->industryProduct) && !empty($order->spec)) {
|
|
$order->industryProduct->stock += $order->num;
|
|
$order->industryProduct->save();
|
|
|
|
$order->spec->stock += $order->num;
|
|
$order->spec->save();
|
|
}
|
|
|
|
DB::commit();
|
|
return $this->response()->success('操作成功')->refresh();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $this->response()->error($e->getMessage())->refresh();
|
|
}
|
|
}
|
|
|
|
public function confirm()
|
|
{
|
|
if ($this->action == 1) {
|
|
return ['确定要设置为已通过吗?', ''];
|
|
}
|
|
}
|
|
|
|
public function parameters()
|
|
{
|
|
return [
|
|
'action' => $this->action,
|
|
];
|
|
}
|
|
}
|