Files
fdesk/app/common/service/StockOrdersService.php
T

904 lines
30 KiB
PHP
Raw Normal View History

2025-09-02 09:36:37 +08:00
<?php
namespace app\common\service;
2025-09-04 10:26:28 +08:00
use app\common\model\StockOrderAccounts;
use app\common\model\StockOrderItemOptions;
use app\common\model\StockOrderItems;
2025-09-02 09:36:37 +08:00
use app\common\validate\StockOrdersValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\StockOrders;
2025-09-08 11:34:19 +08:00
use app\common\model\SysCurrency;
2025-09-04 10:26:28 +08:00
use support\Request;
use app\common\model\User;
2025-09-04 11:52:40 +08:00
use support\Db;
use support\Model;
2025-09-18 07:04:14 +00:00
use app\common\model\ApprovalInstances;
use app\common\model\ApprovalRecords;
2025-10-12 10:32:13 +00:00
use support\exception\BusinessException;
2025-09-02 09:36:37 +08:00
class StockOrdersService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = StockOrders::class;
/**
* 验证器
*/
protected $validator = StockOrdersValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
2025-09-04 10:26:28 +08:00
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'index':
2025-09-04 18:22:24 +08:00
// ====== ====== 筛选 ====== ======
// 业务id
if (isset($p['salesperson_id']) && $p['salesperson_id']) {
$config['where']['salesperson_id'] = $p['salesperson_id'];
}
// 初、终版
$config['where']['is_final'] = $p['is_final'] ?? 0;
// 关键字
if (isset($p['keyword']) && $p['keyword']) {
$k = '%' . $p['keyword'] . '%';
foreach (['pi_no', 'order_no', 'customer_snapshot'] as $col) {
$config['orWhere'][] = [$col, 'like', $k];
}
}
2025-09-04 10:26:28 +08:00
$config['order']['id'] = 'desc';
2025-09-10 13:39:06 +08:00
2025-09-15 16:38:00 +08:00
// $config['where']['salesperson_id'] = $request->user->uid;
2025-09-04 10:26:28 +08:00
break;
2025-09-08 11:34:19 +08:00
case 'print':
case 'approval':
2025-09-04 10:26:28 +08:00
$config['where']['id'] = $p['id'];
2025-09-09 16:15:48 +08:00
break;
case 'detail':
2025-09-09 16:15:48 +08:00
case 'show':
$config['where']['id'] = $p['id'];
2025-09-04 11:52:40 +08:00
$config['with'][] = 'items';
$config['with'][] = 'items.options';
$config['with'][] = 'items.moneyValues';
$config['with'][] = 'accounts';
$config['with'][] = 'moneyValues';
2025-09-09 13:42:00 +08:00
$config['with'][] = 'salesperson';
2025-09-04 10:26:28 +08:00
break;
case 'store':
break;
case 'update':
$config['where']['id'] = $p['id'];
break;
case 'destroy':
$config['where']['id'] = $p['id'];
break;
}
return $config;
}
public function beforeServiceStore($data, Request $request)
{
$userId = $request->user->uid;
$data['order_no'] = generateStockNo();
$data['biz_date'] = strtotime($data['biz_date']);
$data['eta_ship_date'] = strtotime($data['eta_ship_date']);
$data['latest_arrival_date'] = strtotime($data['latest_arrival_date']);
$data['salesperson_id'] = $userId;
$data['salesperson_snapshot'] = User::where('id', $userId)->value('name') ?? '';
$data['approval_status'] = $data['approval_status'] == 1 ? 1 : 0;
2025-09-04 10:26:28 +08:00
// if ($data['is_final'] == 1) {
$data['pick_up_date'] = $data['pick_up_date'] ? strtotime($data['pick_up_date']) : 0;
$data['collect_all_date'] = $data['collect_all_date'] ? strtotime($data['collect_all_date']) : 0;
// }
2025-10-12 10:32:13 +00:00
if ($data['is_final'] == 1) {
// 检查是否至少有一个车辆填写了vin
$hasVin = false;
foreach ($data['veh_list'] as $k => $v) {
if ($v['vin'] != '') {
$hasVin = true;
break;
}
}
// 如果没有任何车辆填写vin,则抛出异常
if (!$hasVin) {
throw new BusinessException('至少需要为一辆车辆填写vin,才能提交审核');
}
}
2025-10-13 15:40:46 +08:00
// 检查绑定
(new UserService())->checkBind($userId);
2025-09-04 10:26:28 +08:00
return $data;
}
2025-09-04 18:22:24 +08:00
protected function afterServiceStore($res)
2025-09-04 10:26:28 +08:00
{
2025-09-04 18:22:24 +08:00
// 处理原单
if ($res->is_final == 1 && $res->object_id > 0) {
$order = StockOrders::find($res->object_id);
$order->is_use = 1;
$order->object_id = $res->id;
if (!$order->save()) {
flg('备货单【'.$order->order_no.'】创建终版成功,但更新原单失败');
}
}
2025-09-09 13:42:00 +08:00
// 启动审批
if ($res->approval_status == 1) {
$this->startApproval($res);
}
2025-09-04 18:22:24 +08:00
return $res;
2025-09-04 10:26:28 +08:00
}
2025-09-08 11:34:19 +08:00
/**
* 启动审批
*/
public function startApproval($item)
{
// 启动审批
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
2025-09-19 07:43:48 +00:00
$appro->start('stock_orders', 'stock_orders', $item->id, [], $item->salesperson_id);
// 异步生成pdf文件
\Webman\RedisQueue\Redis::send('stock-order-pdf', ['id'=>$item->id]);
}
2025-09-08 11:34:19 +08:00
/**
* 创建PDF文件
*/
2025-09-09 16:15:48 +08:00
public function createdPdfFile($id)
2025-09-08 11:34:19 +08:00
{
2025-09-09 16:15:48 +08:00
$config = [];
$config['model'] = $this->modelClass;
$config['where']['id'] = $id;
$config['with'][] = 'items';
$config['with'][] = 'items.options';
$config['with'][] = 'items.moneyValues';
$config['with'][] = 'accounts';
$config['with'][] = 'moneyValues';
$config['with'][] = 'salesperson';
2025-09-08 11:34:19 +08:00
$item = $this->logicShow($config);
$pdfData = $this->formatData($item);
2025-09-08 11:34:19 +08:00
// 渲染模板为最终HTML
$tplPath = app_path('common/utils/pdf/template/stockOrder.stub');
$html = \app\common\utils\pdf\service\PdfHelper::render($tplPath, ['data' => $pdfData]);
// 生成PDF
$driver = new \app\common\utils\pdf\service\MpdfDriver();
$pdfBin = $driver->htmlToPdf($html);
// 存储
2025-09-08 20:00:12 +08:00
$dir = public_path('pdf').'/';
$fileName = ($pdfData['order_no'] ?? date('YmdHis')) . '.pdf';
2025-09-08 11:34:19 +08:00
// 本地保存
2025-09-09 13:42:00 +08:00
\app\common\utils\pdf\service\PdfStorage::saveBinary(
2025-09-08 11:34:19 +08:00
$pdfBin,
$dir,
$fileName
);
2025-09-09 16:15:48 +08:00
// 上传云端
$param = [
'path' => $dir . $fileName,
'scene' => 'stock_orders',
'category' => 'pdf',
'original_name' => $fileName
2025-09-08 20:00:12 +08:00
];
$up = new \app\common\service\UploadService();
2025-09-09 16:15:48 +08:00
$res = $up->uploadLocalFile($param);
@unlink($dir . $fileName);
// 更新文件地址
$item->file_url = $res['file_name'];
$item->save();
return $res;
}
2025-10-13 18:54:32 +08:00
/**
* 查询审批
*/
public function queryAppro($bizId)
{
$instances = ApprovalInstances::where('biz_id', $bizId)->where('biz_type', 'stock_orders')->first();
if (!$instances) {
return [];
}
$records = ApprovalRecords::where('instance_id', $instances->id)->where('action', 'approve')->orderBy('step_index', 'asc')->pluck('approver_id')->toArray();
if (count($records) == 0) {
return [];
}
$signImgs = [];
foreach ($records as $k => $v) {
$signImg = User::where('id', $v)->value('sign_img');
$signImgs[$k] = (!empty($signImg))?(new \app\common\service\OssService)->generatePrivateUrl($signImg):'';
}
return $signImgs;
}
/**
* 打印数据
*/
public function servicePrint(Request $request)
{
$config = $this->buildQueryConfig($request, 'show');
$item = $this->logicShow($config);
return $this->postProcess('Print', $item);
}
public function serviceDetail(Request $request)
{
$config = $this->buildQueryConfig($request, 'detail');
$item = $this->logicShow($config);
$detail = $this->formatData($item);
return $this->postProcess('Detail', $detail);
}
/**
* 审批
*/
public function serviceApproval(Request $request)
{
$config = $this->buildQueryConfig($request, 'approval');
$this->logicUpdateBy($config, [
'approval_status' => 1
]);
$item = $this->logicShow($config);
$this->startApproval($item);
return $this->postProcess('Approval', $item);
}
2025-10-10 11:09:37 +00:00
/**
* 作废审批
*/
public function serviceApprovalNullify(Request $request)
{
$config = $this->buildQueryConfig($request, 'approval');
$item = $this->logicShow($config);
$this->approvalNullify($item);
return $this->postProcess('Approval', $item);
}
/**
* 审批作废
*/
public function approvalNullify($item)
{
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
2025-10-11 08:41:11 +00:00
$appro->startNullify('stock_orders_nullify', 'stock_orders', $item->id, [], $item->salesperson_id);
2025-10-10 11:09:37 +00:00
}
/**
* 撤销审批
*/
public function serviceApprovalRevoke(Request $request)
{
$config = $this->buildQueryConfig($request, 'approval');
$item = $this->logicShow($config);
$this->approvalRevoke($item);
return $this->postProcess('Approval', $item);
}
public function approvalRevoke($item)
{
$instance = \app\common\model\ApprovalInstances::where('biz_id', $item->id)->where('flow_type', 'stock_orders')->first();
$appro = new \app\common\utils\approval\service\ApprovalFlowService();
$appro->revoke($instance->id, $item->salesperson_id);
}
/**
* 创建
*
* @param Request $request
* @param array $data
* @return mixed
*/
public function serviceStore(Request $request, array $data = [])
{
// 关联表较多,增加事务处理
return Db::transaction(function () use ($request, $data) {
$data = $this->preProcess('Store', $data, $request);
$dl = $this->getDataLimitFromRequest($request);
$created = $this->logicCreate($data, $dl);
// 处理车辆和账户信息
$this->processOrderItems($created, $data);
return $this->postProcess('Store', $created);
});
}
protected function onlyFillableData(Model|string $model, array $data, array $extraWhitelist = []): array
{
// 兼容传类名或实例
$m = \is_string($model) ? new $model : $model;
// 1) 优先使用 fillable
$keys = $m->getFillable();
// 2) 若没配置 fillable(常见:用 guarded),回退到完整表字段,避免误伤
if (empty($keys)) {
$keys = $m->getConnection()
->getSchemaBuilder()
->getColumnListing($m->getTable());
}
// 3) 允许额外白名单(如虚拟字段、JSON映射字段)
if ($extraWhitelist) {
$keys = array_merge($keys, $extraWhitelist);
}
return array_intersect_key($data, array_flip($keys));
}
/**
* 更新前处理
*
* @param array $data
* @param Request $request
* @return array
*/
public function beforeServiceUpdate($data, Request $request)
{
$data['biz_date'] = strtotime($data['biz_date']);
$data['eta_ship_date'] = strtotime($data['eta_ship_date']);
$data['latest_arrival_date'] = strtotime($data['latest_arrival_date']);
$data['approval_status'] = $data['approval_status'] == 1 ? 1 : 0;
$data['pick_up_date'] = $data['pick_up_date'] ? strtotime($data['pick_up_date']) : 0;
$data['collect_all_date'] = $data['collect_all_date'] ? strtotime($data['collect_all_date']) : 0;
return $data;
}
/**
* 更新后处理
*
* @param mixed $res
* @return mixed
*/
protected function afterServiceUpdate($res)
{
// 启动审批
if ($res->approval_status == 1) {
$this->startApproval($res);
}
return $res;
}
/**
* 更新
*
* @param Request $request
* @param array $data
* @return mixed
*/
public function serviceUpdate(Request $request, array $data = [])
{
// 关联表较多,增加事务处理
return Db::transaction(function () use ($request, $data) {
$data = $this->preProcess('Update', $data, $request);
$config = $this->buildQueryConfig($request, 'update');
// 过滤掉非库字段
$dbData = $this->onlyFillableData($config['model'], $data);
// 更新主表
$updated = $this->logicUpdateBy($config, $dbData);
// 获取更新后的数据
$order = $this->logicShow($config);
// 处理车辆和账户信息
$this->processOrderItems($order, $data, true);
return $this->postProcess('Update', $order);
});
}
/**
* 处理订单项(车辆和账户)
* @param StockOrders $order 订单对象
* @param array $data 数据
* @param bool $isUpdate 是否为更新操作
*/
private function processOrderItems($order, array $data, bool $isUpdate = false): void
{
// 如果是更新操作,先删除原有的关联数据
if ($isUpdate) {
$this->clearExistingOrderItems($order->id);
}
// 处理车辆项
$this->processVehicleItems($order, $data['veh_list'] ?? []);
// 处理账户项
$this->processAccountItems($order, $data['account_list'] ?? []);
}
/**
* (更新时)清除已存在的订单项
* @param int $orderId 订单ID
*/
private function clearExistingOrderItems(int $orderId): void
{
// 删除车辆改装项
StockOrderItemOptions::whereHas('item', function ($query) use ($orderId) {
$query->where('order_id', $orderId);
})->delete();
// 删除车辆项及其相关金额记录
$orderItems = StockOrderItems::where('order_id', $orderId)->get();
foreach ($orderItems as $item) {
// 删除车辆项的金额记录
Db::table('veh_money_value')
->where('object_scene', 'stock_order_items')
->where('object_id', $item->id)
->delete();
}
// 删除车辆项
StockOrderItems::where('order_id', $orderId)->delete();
// 删除订单本身的金额记录
Db::table('veh_money_value')
->where('object_scene', 'stock_orders')
->where('object_id', $orderId)
->delete();
// 删除账户项
StockOrderAccounts::where('order_id', $orderId)->delete();
}
/**
* 处理车辆项
*/
private function processVehicleItems($order, array $vehList): void
{
$cifTotalAmount = 0.00;
$profitTotalAmount = 0.00;
foreach ($vehList as $veh) {
$veh['order_id'] = $order->id;
$veh['unique_id'] = $this->generateVehicleUniqueId($veh);
$item = StockOrderItems::create($veh);
// 处理金额信息
$this->processVehiclePrices($item, $veh);
// 计算利润
$profit = $this->calculateProfit($veh, $order);
$item->addMoney('profit', $order->settlement_currency, $profit, $order->contract_exchange_rate, '利润');
// 累计到岸价
$cifTotalAmount += $veh['total_price']['amount'] ?? 0;
$profitTotalAmount += $profit;
// 处理改装项
$this->processModificationItems($item->id, $veh['modify_list'] ?? []);
}
// 保存合计到岸价
$order->addMoney('cif_total_amount', $order->settlement_currency, $cifTotalAmount, $order->contract_exchange_rate, '合计到岸价');
// 利润
$order->addMoney('profit_total', $order->settlement_currency, $profitTotalAmount, $order->contract_exchange_rate, '总利润');
}
/**
* 生成车辆唯一标识
*/
private function generateVehicleUniqueId(array $veh): string
{
$uniqueIdStr = "";
$uniqueIdStr .= $veh['sku_id'] ?? '';
$uniqueIdStr .= $veh['model_snapshot'] ?? '';
$uniqueIdStr .= $veh['color_interior_code'] ?? '';
$uniqueIdStr .= $veh['color_exterior_code'] ?? '';
$uniqueIdStr .= $veh['mileage'] ?? '';
$uniqueIdStr .= json_encode($veh['modify_list'] ?? []);
return md5($uniqueIdStr);
}
/**
* 处理车辆价格信息
*/
private function processVehiclePrices($item, array $veh): void
{
// 基础费用
$priceFields = [
'unit_price' => '单价',
'total_price' => '总价',
'handling_fee' => '手续费',
'fob_transport_fee' => 'FOB运输费',
'port_misc_fee' => '港杂',
'retrofit_fee' => '改装费',
2025-09-22 18:49:18 +08:00
'ocean_freight' => '海运费',
'commission' => '佣金',
'purchase_tax' => '购置税',
2025-10-13 18:54:32 +08:00
'tax_refund' => '预估退税',
'clearance_price' => '报关价',
];
foreach ($priceFields as $field => $description) {
if (!empty($veh[$field]['amount'])) {
$item->addMoney(
$field,
$veh[$field]['currency'],
$veh[$field]['amount'],
$veh[$field]['rate'],
$description
);
}
}
// 根据里程处理新车或二手车价格
if (!empty($veh['mileage']) && $veh['mileage'] > 0) {
// 二手车
if (!empty($veh['listing_price']['amount'])) {
$item->addMoney(
'listing_price',
$veh['listing_price']['currency'],
$veh['listing_price']['amount'],
$veh['listing_price']['rate'],
'二手挂牌价'
);
}
if (!empty($veh['reduction_price']['amount'])) {
$item->addMoney(
'reduction_price',
$veh['reduction_price']['currency'],
$veh['reduction_price']['amount'],
$veh['reduction_price']['rate'],
'二手减价'
);
}
} else {
// 新车
if (!empty($veh['official_price']['amount'])) {
$item->addMoney(
'official_price',
$veh['official_price']['currency'],
$veh['official_price']['amount'],
$veh['official_price']['rate'],
'官方指导价'
);
}
if (!empty($veh['discount_price']['amount'])) {
$item->addMoney(
'discount_price',
$veh['discount_price']['currency'],
$veh['discount_price']['amount'],
$veh['discount_price']['rate'],
'优惠价'
);
}
}
}
/**
* 计算利润
*/
private function calculateProfit(array $veh, $order): float
{
$unitPrice = (float)($veh['unit_price']['amount'] ?? 0);
if (!empty($veh['mileage']) && $veh['mileage'] > 0) {
// 二手车
$ttPrice = ($veh['listing_price']['amount'] ?? 0) / ($order->contract_exchange_rate ?: 1);
$ddPrice = $veh['reduction_price']['amount'] ?? 0;
} else {
// 新车
$ttPrice = ($veh['official_price']['amount'] ?? 0) / ($order->contract_exchange_rate ?: 1);
$ddPrice = $veh['discount_price']['amount'] ?? 0;
}
$handlingFee = (float)($veh['handling_fee']['amount'] ?? 0);
$fobTransportFee = (float)($veh['fob_transport_fee']['amount'] ?? 0);
$portMiscFee = (float)($veh['port_misc_fee']['amount'] ?? 0);
$retrofitFee = (float)($veh['retrofit_fee']['amount'] ?? 0);
$oceanFreight = (float)($veh['ocean_freight']['amount'] ?? 0);
2025-09-22 18:49:18 +08:00
$commission = (float)($veh['commission']['amount'] ?? 0);
$purchaseTax = (float)($veh['purchase_tax']['amount'] ?? 0);
$taxRefund = (float)($veh['tax_refund']['amount'] ?? 0);
2025-09-23 11:54:26 +08:00
// 退税额 是增加利润
2025-10-13 18:54:32 +08:00
return $unitPrice - $ttPrice + $ddPrice - $handlingFee - $fobTransportFee - $portMiscFee - $retrofitFee - $oceanFreight - $commission - $purchaseTax + $taxRefund;
}
/**
* 处理改装项
*/
private function processModificationItems(int $orderItemId, array $modifyList): void
{
if (empty($modifyList)) {
return;
}
foreach ($modifyList as $modify) {
$modify['order_item_id'] = $orderItemId;
StockOrderItemOptions::create($modify);
}
}
/**
* 处理账户项
*/
private function processAccountItems($order, array $accountList): void
{
if (empty($accountList)) {
return;
}
foreach ($accountList as $account) {
$account['order_id'] = $order->id;
if (!empty($account['remit_date'])) {
$account['remit_date'] = strtotime($account['remit_date']);
}
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);
2025-09-23 11:54:26 +08:00
// 拼接 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,
2025-09-23 11:54:26 +08:00
'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);
}
2025-09-02 09:36:37 +08:00
}