Merge branch 'develop'
This commit is contained in:
@@ -57,7 +57,7 @@ class ApprovalRecordsService
|
||||
|
||||
if ($p['scene'] === 'todo') {
|
||||
// if (!in_array(1, $request->user->role_ids)) {
|
||||
$config['where']['approver_id'] = $request->user->uid;
|
||||
// $config['where']['approver_id'] = $request->user->uid;
|
||||
// }
|
||||
$config['where']['status'] = 'pending';
|
||||
$config['where']['instance.status'] = 'processing';
|
||||
@@ -95,6 +95,17 @@ class ApprovalRecordsService
|
||||
$config['where']['instance_id'] = $p['instance_id'];
|
||||
$config['order']['id'] = 'desc';
|
||||
break;
|
||||
case 'border':
|
||||
$config['where']['id'] = $p['id'];
|
||||
$config['order']['id'] = 'desc';
|
||||
$config['with'] = [
|
||||
'instance',
|
||||
'approver',
|
||||
'instance.flows',
|
||||
'instance.initiator',
|
||||
'instance.bizInfo'
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
return $config;
|
||||
@@ -115,4 +126,60 @@ class ApprovalRecordsService
|
||||
$item = $this->logicListAll($config);
|
||||
return $this->postProcess('Logs', $item);
|
||||
}
|
||||
|
||||
|
||||
public function serviceBusinessOrder(Request $request, array $data = [])
|
||||
{
|
||||
$config = $this->buildQueryConfig($request, 'border');
|
||||
$config = $this->borderConfig($config, $data['flow_type']);
|
||||
$item = $this->logicShow($config);
|
||||
$bizInfo = [];
|
||||
$service = match ($data['flow_type']) {
|
||||
'stock_orders' => new \app\common\service\StockOrdersService(),
|
||||
'purchase_orders' => new \app\common\service\PurchaseOrdersService(),
|
||||
default => new \app\common\service\StockOrdersService()
|
||||
};
|
||||
$bizInfo = $service->formatData($item->instance->bizInfo);
|
||||
return $this->postProcess('Border', $bizInfo);
|
||||
}
|
||||
|
||||
public function borderConfig($config, $type = 'stock_orders')
|
||||
{
|
||||
$with = match ($type) {
|
||||
'stock_orders' => [
|
||||
'instance.bizInfo.items',
|
||||
'instance.bizInfo.items.options',
|
||||
'instance.bizInfo.items.moneyValues',
|
||||
'instance.bizInfo.accounts',
|
||||
'instance.bizInfo.moneyValues',
|
||||
'instance.bizInfo.salesperson',
|
||||
],
|
||||
'purchase_orders' => [
|
||||
'instance.bizInfo.items',
|
||||
'instance.bizInfo.items.stockOrder',
|
||||
'instance.bizInfo.items.stockOrderItems'
|
||||
]
|
||||
};
|
||||
$config['with'] = array_merge($config['with'], $with);
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
public function transferApproval(Request $request, array $data = [])
|
||||
{
|
||||
$service = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
$result = $service->transferApproval($data['instance_id'], $data['step_index'], $request->user->uid, $data['to_uid'], $data['reason'], 'name');
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function serviceApprovalTimeline(Request $request, array $data = [])
|
||||
{
|
||||
$instance = \app\common\model\ApprovalInstances::where('biz_id', $data['id'])->where('flow_type', $data['flow_type'])->first();
|
||||
if (!$instance) {
|
||||
return [];
|
||||
}
|
||||
$result = \app\common\model\ApprovalRecords::where('instance_id', $instance->id)->with('approver')->orderBy('id', 'desc')->get();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\validate\PurchaseOrderItemsValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\PurchaseOrderItems;
|
||||
|
||||
class PurchaseOrderItemsService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = PurchaseOrderItems::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = PurchaseOrderItemsValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -140,7 +140,7 @@ class StockOrdersService
|
||||
{
|
||||
// 启动审批
|
||||
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
|
||||
$appro->start('stock_orders', 'stock_orders', $item->id, $item->toArray(), $item->salesperson_id);
|
||||
$appro->start('stock_orders', 'stock_orders', $item->id, [], $item->salesperson_id);
|
||||
// 异步生成pdf文件
|
||||
\Webman\RedisQueue\Redis::send('stock-order-pdf', ['id'=>$item->id]);
|
||||
}
|
||||
@@ -160,7 +160,7 @@ class StockOrdersService
|
||||
$config['with'][] = 'moneyValues';
|
||||
$config['with'][] = 'salesperson';
|
||||
$item = $this->logicShow($config);
|
||||
$pdfData = $this->pdfData($item);
|
||||
$pdfData = $this->formatData($item);
|
||||
|
||||
// 渲染模板为最终HTML
|
||||
$tplPath = app_path('common/utils/pdf/template/stockOrder.stub');
|
||||
@@ -200,7 +200,7 @@ class StockOrdersService
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function pdfData($item)
|
||||
public function pdfData2($item)
|
||||
{
|
||||
$vehList = [];
|
||||
$index = 0;
|
||||
@@ -375,8 +375,8 @@ class StockOrdersService
|
||||
{
|
||||
$config = $this->buildQueryConfig($request, 'detail');
|
||||
$item = $this->logicShow($config);
|
||||
$pdfData = $this->pdfData($item);
|
||||
return $this->postProcess('Detail', $pdfData);
|
||||
$detail = $this->formatData($item);
|
||||
return $this->postProcess('Detail', $detail);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -620,7 +620,10 @@ class StockOrdersService
|
||||
'fob_transport_fee' => 'FOB运输费',
|
||||
'port_misc_fee' => '港杂',
|
||||
'retrofit_fee' => '改装费',
|
||||
'ocean_freight' => '海运费'
|
||||
'ocean_freight' => '海运费',
|
||||
'commission' => '佣金',
|
||||
'purchase_tax' => '购置税',
|
||||
'tax_refund' => '预估退税'
|
||||
];
|
||||
|
||||
foreach ($priceFields as $field => $description) {
|
||||
@@ -703,8 +706,12 @@ class StockOrdersService
|
||||
$portMiscFee = (float)($veh['port_misc_fee']['amount'] ?? 0);
|
||||
$retrofitFee = (float)($veh['retrofit_fee']['amount'] ?? 0);
|
||||
$oceanFreight = (float)($veh['ocean_freight']['amount'] ?? 0);
|
||||
$commission = (float)($veh['commission']['amount'] ?? 0);
|
||||
$purchaseTax = (float)($veh['purchase_tax']['amount'] ?? 0);
|
||||
$taxRefund = (float)($veh['tax_refund']['amount'] ?? 0);
|
||||
|
||||
return $unitPrice - $ttPrice - $ddPrice - $handlingFee - $fobTransportFee - $portMiscFee - $retrofitFee - $oceanFreight;
|
||||
// 退税额 是增加利润
|
||||
return $unitPrice - $ttPrice - $ddPrice - $handlingFee - $fobTransportFee - $portMiscFee - $retrofitFee - $oceanFreight - $commission - $purchaseTax + $taxRefund;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -739,4 +746,247 @@ class StockOrdersService
|
||||
StockOrderAccounts::create($account);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ====== ======= ====== ======= ====== 信息处理 ======= ====== ======= ====== =======
|
||||
|
||||
// PDF数据
|
||||
public function formatData($item)
|
||||
{
|
||||
// 提取车辆列表信息
|
||||
$vehListData = $this->prepareVehicleListData($item);
|
||||
|
||||
// 提取订单金额信息
|
||||
$moneyValues = $this->prepareOrderMoneyValues($item);
|
||||
|
||||
// 提取账户列表信息
|
||||
$accountList = $this->prepareAccountList($item);
|
||||
|
||||
// 构建返回数据
|
||||
$veh = [
|
||||
// 基本信息
|
||||
'order_no' => $item->order_no,
|
||||
'salesperson_snapshot' => $item->salesperson_snapshot,
|
||||
'sign_img_url' => $this->getSignatureImageUrl($item->salesperson->sign_img ?? null),
|
||||
'created_at' => date('Y-m-d', strtotime($item->created_at)),
|
||||
|
||||
'pi_no' => $item->pi_no,
|
||||
'biz_date' => date('Y-m-d', $item->biz_date),
|
||||
'clearance_country_snapshot' => $item->clearance_country_snapshot,
|
||||
'customer_source_snapshot' => $item->customer_source_snapshot,
|
||||
'customer_snapshot' => $item->customer_snapshot,
|
||||
'customer_country_snapshot' => $item->customer_country_snapshot,
|
||||
|
||||
// 订单项
|
||||
'veh_list' => array_values($vehListData['list']),
|
||||
'contract_exchange_rate' => $item->contract_exchange_rate,
|
||||
'cif_total_amount' => $moneyValues['cif_total_amount'] ?? [],
|
||||
'profit_total' => $moneyValues['profit_total'] ?? [],
|
||||
|
||||
// 运输和交付信息
|
||||
'declarant_name' => $item->declarant_name,
|
||||
'eta_ship_date' => date('Y-m-d', $item->eta_ship_date),
|
||||
'shipping_method_snapshot' => $item->shipping_method_snapshot,
|
||||
'domestic_port_snapshot' => $item->domestic_port_snapshot,
|
||||
'trade_term_snapshot' => $item->trade_term_snapshot,
|
||||
'clearance_port_snapshot' => $item->clearance_port_snapshot,
|
||||
'latest_arrival_date' => date('Y-m-d', $item->latest_arrival_date),
|
||||
'pick_up_date' => date('Y-m-d', $item->pick_up_date),
|
||||
'collect_all_date' => date('Y-m-d', $item->collect_all_date),
|
||||
'is_final' => $item->is_final,
|
||||
|
||||
// 账户信息
|
||||
'account_list' => $accountList,
|
||||
'acc_index' => count($accountList) + 1,
|
||||
'currency_symbol' => $item->settlement_currency,
|
||||
'sign_imgs' => $this->queryAppro($item->id),
|
||||
'contract_url' => $this->getContractUrl($item->contract ?? null)
|
||||
];
|
||||
|
||||
return $veh;
|
||||
}
|
||||
|
||||
/**
|
||||
* 准备车辆列表数据
|
||||
*/
|
||||
private function prepareVehicleListData($item): array
|
||||
{
|
||||
$vehList = [];
|
||||
$index = 0;
|
||||
$currencyInfo = $this->getCurrencyInfo($item->settlement_currency);
|
||||
|
||||
foreach ($item->items as $v) {
|
||||
// 如果已存在相同车辆,增加数量
|
||||
if (isset($vehList[$v->unique_id])) {
|
||||
$this->incrementVehicleCount($vehList[$v->unique_id], $currencyInfo);
|
||||
// 拼接 remark 值
|
||||
if (!empty($v->remark)) {
|
||||
if (!empty($vehList[$v->unique_id]['remark'])) {
|
||||
$vehList[$v->unique_id]['remark'] .= "; " . $v->remark;
|
||||
} else {
|
||||
$vehList[$v->unique_id]['remark'] = $v->remark;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$index++;
|
||||
// 处理金额信息
|
||||
$moneyValues = $this->processItemMoneyValues($v);
|
||||
|
||||
// 处理改装信息
|
||||
$modificationText = $this->getModificationText($v->options);
|
||||
|
||||
// 计算单项利润
|
||||
$itemProfit = $this->calculateProfit($moneyValues, $item);
|
||||
|
||||
// 构建车辆信息
|
||||
$vehList[$v->unique_id] = [
|
||||
'index' => $index,
|
||||
'model_snapshot' => $v->model_snapshot,
|
||||
'color_interior_snapshot' => $v->color_interior_snapshot,
|
||||
'color_exterior_snapshot' => $v->color_exterior_snapshot,
|
||||
'num' => 1,
|
||||
'modify' => $modificationText,
|
||||
'mileage' => $v->mileage,
|
||||
'remark' => $v->remark,
|
||||
] + $moneyValues;
|
||||
|
||||
// 添加利润信息
|
||||
$vehList[$v->unique_id]['profit'] = [
|
||||
'currency' => $item->settlement_currency,
|
||||
'amount' => $itemProfit,
|
||||
'currency_info' => $currencyInfo['currency_symbol'] . number_format($itemProfit, 2, '.', ',')
|
||||
];
|
||||
}
|
||||
|
||||
return ['list' => $vehList];
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单项金额值
|
||||
*/
|
||||
private function processItemMoneyValues($item): array
|
||||
{
|
||||
$moneyValues = [];
|
||||
foreach ($item->moneyValues as $money) {
|
||||
$moneyValues[$money->field] = [
|
||||
'currency' => $money->currency,
|
||||
'amount' => $money->amount,
|
||||
'rate' => $money->exchange_rate,
|
||||
'currency_info' => $this->formatCurrency($money->amount, $money->currency)
|
||||
];
|
||||
}
|
||||
return $moneyValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取改装文本
|
||||
*/
|
||||
private function getModificationText($options): string
|
||||
{
|
||||
$modificationText = '';
|
||||
foreach ($options as $option) {
|
||||
$modificationText .= $option->option_name_snapshot . ' ';
|
||||
}
|
||||
return trim($modificationText);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加车辆数量并更新相关金额显示
|
||||
*/
|
||||
private function incrementVehicleCount(array &$vehicleData, array $currencyInfo): void
|
||||
{
|
||||
$vehicleData['num'] += 1;
|
||||
|
||||
// 更新总价显示
|
||||
if (isset($vehicleData['total_price']['amount'])) {
|
||||
$totalAmount = $vehicleData['total_price']['amount'] * $vehicleData['num'];
|
||||
$vehicleData['total_price']['currency_info'] = $currencyInfo['currency_symbol'] . number_format($totalAmount, 2, '.', ',');
|
||||
}
|
||||
|
||||
// 更新利润显示
|
||||
if (isset($vehicleData['profit']['amount'])) {
|
||||
$profitAmount = $vehicleData['profit']['amount'] * $vehicleData['num'];
|
||||
$vehicleData['profit']['currency_info'] = $currencyInfo['currency_symbol'] . number_format($profitAmount, 2, '.', ',');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 准备订单金额值
|
||||
*/
|
||||
private function prepareOrderMoneyValues($item): array
|
||||
{
|
||||
$moneyValues = [];
|
||||
foreach ($item->moneyValues as $money) {
|
||||
$moneyValues[$money->field] = [
|
||||
'currency' => $money->currency,
|
||||
'amount' => $money->amount,
|
||||
'rate' => $money->exchange_rate,
|
||||
'currency_info' => $this->formatCurrency($money->amount, $money->currency)
|
||||
];
|
||||
}
|
||||
return $moneyValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* 准备账户列表
|
||||
*/
|
||||
private function prepareAccountList($item): array
|
||||
{
|
||||
$accountList = [];
|
||||
foreach ($item->accounts as $k => $account) {
|
||||
$accountList[] = [
|
||||
'index' => '收款' . ($k + 1),
|
||||
'remit_country_name_snapshot' => $account->remit_country_name_snapshot,
|
||||
'remit_date' => date('Y-m-d', $account->remit_date),
|
||||
'currency_name_snapshot' => $account->currency_name_snapshot,
|
||||
'account_name_snapshot' => $account->account_name_snapshot,
|
||||
'remitter_name' => $account->remitter_name,
|
||||
'amount' => $account->amount,
|
||||
];
|
||||
}
|
||||
return $accountList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取货币信息
|
||||
*/
|
||||
private function getCurrencyInfo(string $currencyCode): array
|
||||
{
|
||||
return SysCurrency::where('currency_code', $currencyCode)
|
||||
->first(['currency_symbol', 'currency_name'])
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化货币显示
|
||||
*/
|
||||
private function formatCurrency(float $amount, string $currencyCode): string
|
||||
{
|
||||
$currencySymbol = SysCurrency::where('currency_code', $currencyCode)->value('currency_symbol');
|
||||
return $currencySymbol . number_format($amount, 2, '.', ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签名图片URL
|
||||
*/
|
||||
private function getSignatureImageUrl(?string $signImg): string
|
||||
{
|
||||
if (empty($signImg)) {
|
||||
return '';
|
||||
}
|
||||
return (new \app\common\service\OssService)->generatePrivateUrl($signImg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取合同URL
|
||||
*/
|
||||
private function getContractUrl(?string $contract): string
|
||||
{
|
||||
if (empty($contract)) {
|
||||
return '';
|
||||
}
|
||||
return (new \app\common\service\OssService)->generatePrivateUrl($contract);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace app\common\service;
|
||||
use app\common\validate\VehMoneyValueValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\VehMoneyValue;
|
||||
use support\Db;
|
||||
|
||||
class VehMoneyValueService
|
||||
{
|
||||
@@ -28,4 +29,49 @@ class VehMoneyValueService
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 复制金额记录
|
||||
*
|
||||
* @param string $fromScene 源场景
|
||||
* @param int $fromId 源对象ID
|
||||
* @param string $toScene 目标场景
|
||||
* @param int $toId 目标对象ID
|
||||
* @return int 插入条数
|
||||
* @throws BusinessException
|
||||
*/
|
||||
public function copyMoneyValues(string $fromScene, int $fromId, string $toScene, int $toId): int
|
||||
{
|
||||
// 1. 查询源数据
|
||||
$rows = Db::table('veh_money_value')
|
||||
->where('object_scene', $fromScene)
|
||||
->where('object_id', $fromId)
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
if (empty($rows)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$now = time();
|
||||
$insertData = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$insertData[] = [
|
||||
'object_scene' => $toScene,
|
||||
'object_id' => $toId,
|
||||
'field' => $row->field,
|
||||
'currency' => $row->currency,
|
||||
'amount' => $row->amount,
|
||||
'exchange_rate' => $row->exchange_rate,
|
||||
'remark' => $row->remark,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
];
|
||||
}
|
||||
|
||||
// 2. 插入新数据
|
||||
return Db::table('veh_money_value')->insert($insertData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user