登录
This commit is contained in:
@@ -0,0 +1,357 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\validate\PurchaseOrdersValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\PurchaseOrders;
|
||||
use app\common\logic\PurchaseOrdersLogic;
|
||||
use app\common\model\PurchaseOrderItems;
|
||||
use app\common\model\User;
|
||||
use support\Db;
|
||||
use support\exception\BusinessException;
|
||||
use support\Request;
|
||||
|
||||
class PurchaseOrdersService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = PurchaseOrders::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = PurchaseOrdersValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
|
||||
public function getLogic()
|
||||
{
|
||||
return new PurchaseOrdersLogic();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前置预处理数据
|
||||
*
|
||||
* @param array $data 原始返回值
|
||||
* @return array 处理后的返回值
|
||||
*/
|
||||
public function preQueryData(array $data)
|
||||
{
|
||||
$result = match ($data['scene']) {
|
||||
'pi_no' => $this->getLogic()->getPiList(),
|
||||
'order_no' => $this->getLogic()->getOrderNoListByPiNo($data['pi_no']),
|
||||
'order_items' => $this->getLogic()->getOrderItemsListByOrderNo($data['order_id']),
|
||||
default => $data,
|
||||
};
|
||||
return $this->postProcess('PreData', $result->toArray());
|
||||
}
|
||||
|
||||
public function afterServicePreData(array $data)
|
||||
{
|
||||
foreach ($data as $key => &$item) {
|
||||
if (isset($item['biz_date'])) {
|
||||
$item['biz_date'] = date('Y-m-d', $item['biz_date']);
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function beforeServiceStore(array $data, Request $request)
|
||||
{
|
||||
$data['order_no'] = generateStockNo('CG');
|
||||
$data['buyer_id'] = $request->user->uid;
|
||||
$data['buyer_name'] = User::where('id', $request->user->uid)->value('name');
|
||||
if ($data['is_draft'] == 0) {
|
||||
$data['status'] = 1;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function serviceStore(Request $request, array $data = [], string $scene = 'store')
|
||||
{
|
||||
// 使用事务确保数据一致性
|
||||
return Db::transaction(function () use ($request, $data, $scene) {
|
||||
$data = $this->preProcess(ucfirst($scene), $data, $request);
|
||||
$dl = $this->getDataLimitFromRequest($request);
|
||||
// 创建采购单
|
||||
$created = $this->logicCreate($data, $dl);
|
||||
if (isset($data['stock_order'])) {
|
||||
foreach ($data['stock_order'] as $order) {
|
||||
foreach ($order['stock_items'] as $item) {
|
||||
// 创建采购单明细
|
||||
PurchaseOrderItems::create([
|
||||
'purchase_order_id' => $created->id,
|
||||
'stock_id' => $order['stock_orders_id'],
|
||||
'stock_items_id' => $item['stock_order_items_id'],
|
||||
'stock_items_uqid' => $item['unique_id'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->postProcess(ucfirst($scene), $created);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询配置
|
||||
*
|
||||
* @param Request $request
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
|
||||
{
|
||||
$p = $request->all();
|
||||
|
||||
$config = [];
|
||||
$config['model'] = $this->modelClass;
|
||||
|
||||
switch ($scene) {
|
||||
case 'index':
|
||||
$config['order']['id'] = 'desc';
|
||||
break;
|
||||
case 'update':
|
||||
$config['where']['id'] = $p['id'] ?? 0;
|
||||
break;
|
||||
case 'show':
|
||||
$config['where']['id'] = $p['id'];
|
||||
$config['with'] = [
|
||||
'items', 'items.stockOrder', 'items.stockOrderItems'
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除采购订单
|
||||
*
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function serviceDestroy(Request $request)
|
||||
{
|
||||
// 使用事务确保数据一致性
|
||||
return Db::transaction(function () use ($request) {
|
||||
// 构建查询条件
|
||||
$config = $this->buildQueryConfig($request, 'show');
|
||||
|
||||
// 软删除采购单明细
|
||||
$order = $this->logicShow($config);
|
||||
if ($order && $order->items) {
|
||||
foreach ($order->items as $item) {
|
||||
$item->delete();
|
||||
}
|
||||
}
|
||||
|
||||
// 软删除采购单主表
|
||||
$res = $order->delete();
|
||||
|
||||
return $this->postProcess('Destroy', $res);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建采购单成功后处理
|
||||
*/
|
||||
protected function afterServiceStore($res)
|
||||
{
|
||||
if ($res->is_draft == 0) {
|
||||
$this->startApproval($res, ['resubmitted'=>0]);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function startApproval($item)
|
||||
{
|
||||
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
$appro->start('purchase_orders', 'purchase_orders', $item->id, ['resubmitted'=>0], $item->buyer_id);
|
||||
}
|
||||
|
||||
public function beforeServiceUpdate(array $data, Request $request)
|
||||
{
|
||||
if ($data['is_draft'] == 0) {
|
||||
$data['status'] = 1;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新采购单
|
||||
*
|
||||
* @param Request $request
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function serviceUpdate(Request $request, array $data = [], string $scene = 'update')
|
||||
{
|
||||
// 使用事务确保数据一致性
|
||||
return Db::transaction(function () use ($request, $data, $scene) {
|
||||
$data = $this->preProcess(ucfirst($scene), $data, $request);
|
||||
|
||||
// 构建查询条件
|
||||
$config = $this->buildQueryConfig($request, $scene);
|
||||
|
||||
// 更新主表信息
|
||||
$udata = $data;
|
||||
unset($udata['stock_order']);
|
||||
unset($udata['is_resubmit']);
|
||||
unset($udata['approval_desc']);
|
||||
$this->logicUpdateBy($config, $udata);
|
||||
|
||||
// 获取更新后的数据
|
||||
$order = $this->logicShow($config);
|
||||
|
||||
// 更新采购单明细
|
||||
if (isset($data['stock_order'])) {
|
||||
// 先删除原有的明细
|
||||
PurchaseOrderItems::where('purchase_order_id', $order->id)->delete();
|
||||
|
||||
// 重新创建明细
|
||||
foreach ($data['stock_order'] as $orderData) {
|
||||
foreach ($orderData['stock_items'] as $item) {
|
||||
PurchaseOrderItems::create([
|
||||
'purchase_order_id' => $order->id,
|
||||
'stock_id' => $orderData['stock_orders_id'],
|
||||
'stock_items_id' => $item['stock_order_items_id'],
|
||||
'stock_items_uqid' => $item['unique_id'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->postProcess(
|
||||
ucfirst($scene),
|
||||
$order,
|
||||
[
|
||||
'is_resubmit'=>$data['is_resubmit'],
|
||||
'approval_desc'=>$data['approval_desc']
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
protected function afterServiceUpdate($res, $data)
|
||||
{
|
||||
if ($data['is_resubmit'] == 0 && $res->status == 1) {
|
||||
$this->startApproval($res);
|
||||
}
|
||||
if ($data['is_resubmit'] == 1) {
|
||||
$instance = \app\common\model\ApprovalInstances::where('biz_id', $res->id)->where('flow_type', 'purchase_orders')->first();
|
||||
if ( $instance === null) {
|
||||
throw new BusinessException('未找到审批实例');
|
||||
}
|
||||
// 审批服务
|
||||
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
// 重新提交
|
||||
$appro->resubmit($instance->id, $instance->initiator_id, ['resubmitted'=>1], $data['approval_desc']);
|
||||
// 添加到讨论
|
||||
if (!empty($data['approval_desc'])) {
|
||||
$appro->postDiscussion($instance->id, $instance->initiator_id, $data['approval_desc'], false, [], 'name');
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
public function serviceResubmit(Request $request, array $data = [])
|
||||
{
|
||||
$p = $request->all();
|
||||
$config = [];
|
||||
$config['model'] = $this->modelClass;
|
||||
$config['where']['id'] = $p['id'];
|
||||
$config['where']['buyer_id'] = $request->user->uid;
|
||||
$info = $this->logicShow($config);
|
||||
if ($info === null) {
|
||||
throw new BusinessException('未找到采购单');
|
||||
}
|
||||
if ($info->status != 3) {
|
||||
throw new BusinessException('该状态不能重新提交');
|
||||
}
|
||||
$instance = \app\common\model\ApprovalInstances::where('biz_id', $info->id)->where('flow_type', 'purchase_orders')->first();
|
||||
if ( $instance === null) {
|
||||
throw new BusinessException('未找到审批实例');
|
||||
}
|
||||
// 审批服务
|
||||
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
// 重新提交
|
||||
$result = $appro->resubmit($instance->id, $instance->initiator_id, ['resubmitted'=>1], $data['remark']);
|
||||
// 添加到讨论
|
||||
if (!empty($data['remark'])) {
|
||||
$appro->postDiscussion($instance->id, $instance->initiator_id, $data['remark'], false, [], 'name');
|
||||
}
|
||||
|
||||
$info->status = 1;
|
||||
$info->save();
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function updateStatus(array $data = [])
|
||||
{
|
||||
|
||||
$info = PurchaseOrders::where('id', $data['id'])->first();
|
||||
if ($info === null) {
|
||||
throw new BusinessException('未找到采购单');
|
||||
}
|
||||
$info->status = $data['status'];
|
||||
return $info->save();
|
||||
}
|
||||
|
||||
|
||||
public function formatData($data)
|
||||
{
|
||||
$contractFile = $data['contract_file'] ? explode(',', $data['contract_file']) : [];
|
||||
$contractFileUrl = [];
|
||||
if (count($contractFile) > 0) {
|
||||
foreach ($contractFile as $file) {
|
||||
$contractFileUrl[] = (new \app\common\service\OssService)->generatePrivateUrl($file);
|
||||
}
|
||||
}
|
||||
$result = [
|
||||
'id' => $data['id'],
|
||||
'order_no' => $data['order_no'],
|
||||
'pi_no' => $data['pi_no'],
|
||||
'buyer_id' => $data['buyer_id'],
|
||||
'buyer_name' => $data['buyer_name'],
|
||||
'contract_file' => $contractFile,
|
||||
'contract_file_url' => $contractFileUrl,
|
||||
'status' => $data['status'],
|
||||
'remark' => $data['remark']
|
||||
];
|
||||
$temps = [];
|
||||
if (isset($data['items']) && count($data['items']) > 0) {
|
||||
foreach ($data['items'] as $record) {
|
||||
if (!in_array($record['stockOrder']['id'], array_keys($temps))) {
|
||||
$temps[$record['stockOrder']['id']] = [
|
||||
'id' => $record['stockOrder']['id'],
|
||||
'order_no' => $record['stockOrder']['order_no'],
|
||||
'salesperson_snapshot' => $record['stockOrder']['salesperson_snapshot'],
|
||||
'biz_date' => date('Y-m-d', $record['stockOrder']['biz_date'])
|
||||
];
|
||||
}
|
||||
$temps[$record['stockOrder']['id']]['stock_order_items'][] =[
|
||||
'id' => $record['stockOrderItems']['id'],
|
||||
'model_snapshot' => $record['stockOrderItems']['model_snapshot'],
|
||||
'unique_id' => $record['stockOrderItems']['unique_id'],
|
||||
];
|
||||
}
|
||||
}
|
||||
$result['stock_order'] = array_values($temps);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user