Merge branch 'develop'

This commit is contained in:
mzl
2025-10-21 19:55:36 +08:00
parent 951f2a6536
commit 1058bd9cbd
19 changed files with 552 additions and 2311 deletions
+201 -149
View File
@@ -15,6 +15,7 @@ use support\Db;
use support\Model;
use app\common\model\ApprovalInstances;
use app\common\model\ApprovalRecords;
use app\common\model\StockOrdersFiles; // 添加文件附件模型
use support\exception\BusinessException;
class StockOrdersService
@@ -83,6 +84,7 @@ class StockOrdersService
$config['with'][] = 'accounts';
$config['with'][] = 'moneyValues';
$config['with'][] = 'salesperson';
$config['with'][] = 'files'; // 添加文件附件关联
break;
case 'store':
break;
@@ -328,10 +330,16 @@ class StockOrdersService
$data = $this->preProcess('Store', $data, $request);
$dl = $this->getDataLimitFromRequest($request);
$created = $this->logicCreate($data, $dl);
// 添加金额
$created->addMoney('cif_total', $data['cif_total']['currency'], $data['cif_total']['amount'], $data['cif_total']['rate'], '合计到岸价');
// 处理车辆和账户信息
$this->processOrderItems($created, $data);
// 处理文件附件
$this->processOrderFiles($created, $data);
return $this->postProcess('Store', $created);
});
}
@@ -410,6 +418,8 @@ class StockOrdersService
$order = $this->logicShow($config);
// 处理车辆和账户信息
$this->processOrderItems($order, $data, true);
// 处理文件附件
$this->processOrderFiles($order, $data, true);
return $this->postProcess('Update', $order);
});
}
@@ -434,6 +444,62 @@ class StockOrdersService
$this->processAccountItems($order, $data['account_list'] ?? []);
}
/**
* 处理订单文件附件
* @param StockOrders $order 订单对象
* @param array $data 数据
* @param bool $isUpdate 是否为更新操作
*/
private function processOrderFiles($order, array $data, bool $isUpdate = false): void
{
// 如果是更新操作,先删除原有的文件附件
if ($isUpdate) {
$this->clearExistingOrderFiles($order->id);
}
// 处理文件附件项
$this->processFileItems($order, $data);
}
/**
* (更新时)清除已存在的订单文件附件
* @param int $orderId 订单ID
*/
private function clearExistingOrderFiles(int $orderId): void
{
StockOrdersFiles::where('order_id', $orderId)->delete();
}
/**
* 处理文件附件项
*/
private function processFileItems($order, array $data): void
{
// 定义支持的文件类型
$fileTypes = [
'contract' => StockOrdersFiles::CONTRACT,
'transaction_slip' => StockOrdersFiles::TRANSACTION_SLIP,
'pi_doc' => StockOrdersFiles::PI_DOC
];
// 遍历每种文件类型
foreach ($fileTypes as $field => $fileCode) {
// 检查数据中是否存在该字段且为数组
if (!empty($data[$field]) && is_array($data[$field])) {
// 处理该字段下的每个文件
foreach ($data[$field] as $fileUrl) {
$fileData = [
'order_id' => $order->id,
'file_code' => $fileCode,
'file_url' => $fileUrl,
'file_name' => StockOrdersFiles::FILE_CODE[$fileCode],
];
StockOrdersFiles::create($fileData);
}
}
}
}
/**
* (更新时)清除已存在的订单项
* @param int $orderId 订单ID
@@ -473,8 +539,9 @@ class StockOrdersService
*/
private function processVehicleItems($order, array $vehList): void
{
$cifTotalAmount = 0.00;
$profitTotalAmount = 0.00;
$profitCurrency = '';
$profitRate = 0;
foreach ($vehList as $veh) {
$veh['order_id'] = $order->id;
$veh['unique_id'] = $this->generateVehicleUniqueId($veh);
@@ -483,23 +550,18 @@ class StockOrdersService
// 处理金额信息
$this->processVehiclePrices($item, $veh);
// 计算利润
// $profit = $this->calculateProfit($veh, $item->mileage, $order);
// $item->addMoney('profit', $order->settlement_currency, $profit, $order->contract_exchange_rate, '利润');
// 累计到岸价
$cifTotalAmount += $veh['total_price']['amount'] ?? 0;
// $cifTotalAmount += $veh['total_price']['amount'] ?? 0;
$profitCurrency === '' ? $profitCurrency = $veh['profit']['currency'] ?? '' : '';
$profitRate === 0 ? $profitRate = $veh['profit']['rate'] ?? 0 : '';
$profitTotalAmount += $veh['profit']['amount'] ?? 0;
// 处理改装项
$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, '总利润');
$order->addMoney('profit_total', $profitCurrency, $profitTotalAmount, $profitRate, '总利润');
}
/**
@@ -507,13 +569,9 @@ class StockOrdersService
*/
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'] ?? []);
$keysToRemove = ['vin', 'remark', 'modify_list', 'order_id'];
$array = array_diff_key($veh, array_flip($keysToRemove));
$uniqueIdStr = json_encode($array);
return md5($uniqueIdStr);
}
@@ -523,9 +581,29 @@ class StockOrdersService
*/
private function processVehiclePrices($item, array $veh): void
{
// unit_price 单价/成交价/收款价
// total_price 总价
// handling_fee 手续费
// fob_transport_fee FOB运输费
// port_misc_fee 港杂
// retrofit_fee 改装费
// ocean_freight 海运费
// commission 佣金
// purchase_tax 购置税
// tax_refund 预估退税
// clearance_price 报关价
// profit 利润
// official_price 官方指导价
// discount_price 优惠价
// listing_price 二手挂牌价
// reduction_price 二手减价/优惠价
// retention_price 留款/挂账
// car_price 车价
// accessory_price 配件费
// 基础费用
$priceFields = [
'unit_price' => '单价',
'unit_price' => '收款价',
'total_price' => '总价',
'handling_fee' => '手续费',
'fob_transport_fee' => 'FOB运输费',
@@ -536,7 +614,10 @@ class StockOrdersService
'purchase_tax' => '购置税',
'tax_refund' => '预估退税',
'clearance_price' => '报关价',
'profit' => '利润'
'profit' => '利润',
'retention_price' => '挂账',
'car_price' => '车价',
'accessory_price' => '配件费'
];
foreach ($priceFields as $field => $description) {
@@ -564,7 +645,7 @@ class StockOrdersService
$veh['reduction_price']['currency'],
(float)$veh['reduction_price']['amount'],
$veh['reduction_price']['rate'],
'二手减价'
'二手优惠价'
);
} else {
// 新车
@@ -585,111 +666,6 @@ class StockOrdersService
}
}
/**
* 计算利润
*/
private function calculateProfit(array $veh, int $mileage, $order): float
{
$contractExchangeRate = $order->contract_exchange_rate ?: 1;
// 提取单价并进行汇率转换(如果需要)
$unitPriceRate = $veh['unit_price']['rate'] ?: 1;
if ($unitPriceRate != $contractExchangeRate) {
$unitPrice = (float)($veh['unit_price']['amount'] ?? 0) / $contractExchangeRate;
} else {
$unitPrice = (float)($veh['unit_price']['amount'] ?? 0);
}
// 处理主要价格字段,根据是否需要汇率转换来计算
if ($mileage > 0) {
// 二手车
$listingPriceRate = $veh['listing_price']['rate'] ?: 1;
if ($listingPriceRate != $contractExchangeRate) {
$ttPrice = ($veh['listing_price']['amount'] ?? 0) / $contractExchangeRate;
} else {
$ttPrice = $veh['listing_price']['amount'] ?? 0;
}
$reductionPriceRate = $veh['reduction_price']['rate'] ?: 1;
if ($reductionPriceRate != $contractExchangeRate) {
$ddPrice = ($veh['reduction_price']['amount'] ?? 0) / $contractExchangeRate;
} else {
$ddPrice = $veh['reduction_price']['amount'] ?? 0;
}
} else {
// 新车
$officialPriceRate = $veh['official_price']['rate'] ?: 1;
if ($officialPriceRate != $contractExchangeRate) {
$ttPrice = ($veh['official_price']['amount'] ?? 0) / $contractExchangeRate;
} else {
$ttPrice = $veh['official_price']['amount'] ?? 0;
}
$discountPriceRate = $veh['discount_price']['rate'] ?: 1;
if ($discountPriceRate != $contractExchangeRate) {
$ddPrice = ($veh['discount_price']['amount'] ?? 0) / $contractExchangeRate;
} else {
$ddPrice = $veh['discount_price']['amount'] ?? 0;
}
}
// 处理其他费用字段,根据是否需要汇率转换来计算
$handlingFeeRate = $veh['handling_fee']['rate'] ?: 1;
if ($handlingFeeRate != $contractExchangeRate) {
$handlingFee = (float)($veh['handling_fee']['amount'] ?? 0) / $contractExchangeRate;
} else {
$handlingFee = (float)($veh['handling_fee']['amount'] ?? 0);
}
$fobTransportFeeRate = $veh['fob_transport_fee']['rate'] ?: 1;
if ($fobTransportFeeRate != $contractExchangeRate) {
$fobTransportFee = (float)($veh['fob_transport_fee']['amount'] ?? 0) / $contractExchangeRate;
} else {
$fobTransportFee = (float)($veh['fob_transport_fee']['amount'] ?? 0);
}
$portMiscFeeRate = $veh['port_misc_fee']['rate'] ?: 1;
if ($portMiscFeeRate != $contractExchangeRate) {
$portMiscFee = (float)($veh['port_misc_fee']['amount'] ?? 0) / $contractExchangeRate;
} else {
$portMiscFee = (float)($veh['port_misc_fee']['amount'] ?? 0);
}
$retrofitFeeRate = $veh['retrofit_fee']['rate'] ?: 1;
if ($retrofitFeeRate != $contractExchangeRate) {
$retrofitFee = (float)($veh['retrofit_fee']['amount'] ?? 0) / $contractExchangeRate;
} else {
$retrofitFee = (float)($veh['retrofit_fee']['amount'] ?? 0);
}
$oceanFreightRate = $veh['ocean_freight']['rate'] ?: 1;
if ($oceanFreightRate != $contractExchangeRate) {
$oceanFreight = (float)($veh['ocean_freight']['amount'] ?? 0) / $contractExchangeRate;
} else {
$oceanFreight = (float)($veh['ocean_freight']['amount'] ?? 0);
}
$commissionRate = $veh['commission']['rate'] ?: 1;
if ($commissionRate != $contractExchangeRate) {
$commission = (float)($veh['commission']['amount'] ?? 0) / $contractExchangeRate;
} else {
$commission = (float)($veh['commission']['amount'] ?? 0);
}
$purchaseTaxRate = $veh['purchase_tax']['rate'] ?: 1;
if ($purchaseTaxRate != $contractExchangeRate) {
$purchaseTax = (float)($veh['purchase_tax']['amount'] ?? 0) / $contractExchangeRate;
} else {
$purchaseTax = (float)($veh['purchase_tax']['amount'] ?? 0);
}
// 预估退税不需要参与汇率转换计算,前端已经计算过了
$taxRefund = (float)($veh['tax_refund']['amount'] ?? 0);
// 退税额 是增加利润
return $unitPrice - $ttPrice + $ddPrice - $handlingFee - $fobTransportFee - $portMiscFee - $retrofitFee - $oceanFreight - $commission - $purchaseTax + $taxRefund;
}
/**
* 处理改装项
*/
@@ -738,6 +714,9 @@ class StockOrdersService
// 提取账户列表信息
$accountList = $this->prepareAccountList($item);
// 提取文件附件列表信息
$fileList = $this->prepareFileList($item);
// 构建返回数据
$veh = [
// 基本信息
@@ -756,8 +735,8 @@ class StockOrdersService
// 订单项
'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'] ?? [],
'cif_total' => $moneyValues['cif_total'] ?? [],
// 运输和交付信息
'declarant_name' => $item->declarant_name,
@@ -776,7 +755,10 @@ class StockOrdersService
'acc_index' => count($accountList) + 1,
'currency_symbol' => $item->settlement_currency,
'sign_imgs' => $this->queryAppro($item->id),
'contract_url' => $this->getContractUrl($item->contract ?? null)
'contract_url' => $this->getContractUrl($item->contract ?? null),
// 文件附件信息
'file_list' => $fileList
];
return $veh;
@@ -798,7 +780,7 @@ class StockOrdersService
// 拼接 remark 值
if (!empty($v->remark)) {
if (!empty($vehList[$v->unique_id]['remark'])) {
$vehList[$v->unique_id]['remark'] .= "; " . $v->remark;
$vehList[$v->unique_id]['remark'] .= "<br>" . $v->remark;
} else {
$vehList[$v->unique_id]['remark'] = $v->remark;
}
@@ -811,12 +793,10 @@ class StockOrdersService
$moneyValues = $this->processItemMoneyValues($v);
// 处理改装信息
$modificationText = $this->getModificationText($v->options);
// $modificationText = $this->getModificationText($v->options);
// 计算单项利润
// $itemProfit = $this->calculateProfit($moneyValues, $v->mileage, $item);
// var_dump($moneyValues);
$itemProfit = empty($moneyValues['profit']['amount']) ? 0 : $moneyValues['profit']['amount'];
// 利润
// $itemProfit = empty($moneyValues['profit']['amount']) ? 0 : $moneyValues['profit']['amount'];
// 构建车辆信息
$vehList[$v->unique_id] = [
@@ -825,17 +805,17 @@ class StockOrdersService
'color_interior_snapshot' => $v->color_interior_snapshot,
'color_exterior_snapshot' => $v->color_exterior_snapshot,
'num' => 1,
'modify' => $modificationText,
// 'modify' => $modificationText,
'mileage' => $v->mileage,
'remark' => $v->remark,
] + $moneyValues;
// var_dump($vehList[$v->unique_id]);
// 添加利润信息
$vehList[$v->unique_id]['profit'] = [
'currency' => $item->settlement_currency,
'amount' => $itemProfit,
'currency_info' => $currencyInfo['currency_symbol'] . number_format($itemProfit, 2, '.', ',')
];
// $vehList[$v->unique_id]['profit'] = [
// 'currency' => $item->settlement_currency,
// 'amount' => $itemProfit,
// 'currency_info' => $currencyInfo['currency_symbol'] . number_format($itemProfit, 2, '.', ',')
// ];
}
return ['list' => $vehList];
@@ -877,16 +857,38 @@ class StockOrdersService
{
$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['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, '.', ',');
// // 更新利润显示
// if (isset($vehicleData['profit']['amount'])) {
// $profitAmount = $vehicleData['profit']['amount'] * $vehicleData['num'];
// $vehicleData['profit']['currency_info'] = $currencyInfo['currency_symbol'] . number_format($profitAmount, 2, '.', ',');
// }
// var_dump($vehicleData);
$fieldList = [
'fob_transport_fee',
'car_price',
'ocean_freight',
'commission',
'retention_price',
'port_misc_fee',
'total_price',
'clearance_price',
'profit'
];
foreach ($vehicleData as $field => $value) {
if (in_array($field, $fieldList)) {
$amount = $vehicleData[$field]['amount'] * $vehicleData['num'];
$currencySymbol = SysCurrency::where('currency_code', $vehicleData[$field]['currency'])->value('currency_symbol');
$vehicleData[$field]['currency_info'] = $currencySymbol . number_format($amount, 2, '.', ',');
}
}
}
@@ -927,6 +929,45 @@ class StockOrdersService
return $accountList;
}
/**
* 准备文件附件列表
*/
private function prepareFileList($item): array
{
$fileList = [];
// 按file_code分组文件
$groupedFiles = [];
foreach ($item->files as $file) {
if (!isset($groupedFiles[$file->file_code])) {
$groupedFiles[$file->file_code] = [];
}
$groupedFiles[$file->file_code][] = [
'id' => $file->id,
'file_url' => $file->file_url,
'file_full_url' => $this->getFileUrl($file->file_url),
];
}
// 定义支持的文件类型
$fileTypes = [
'contract' => StockOrdersFiles::CONTRACT,
'transaction_slip' => StockOrdersFiles::TRANSACTION_SLIP,
'pi_doc' => StockOrdersFiles::PI_DOC
];
// 按前端需要的字段组织数据
foreach ($fileTypes as $field => $fileCode) {
if (isset($groupedFiles[$fileCode])) {
$fileList[$field] = $groupedFiles[$fileCode];
} else {
$fileList[$field] = [];
}
}
return $fileList;
}
/**
* 获取货币信息
*/
@@ -967,4 +1008,15 @@ class StockOrdersService
}
return (new \app\common\service\OssService)->generatePrivateUrl($contract);
}
/**
* 获取文件URL
*/
private function getFileUrl(?string $fileUrl): string
{
if (empty($fileUrl)) {
return '';
}
return (new \app\common\service\OssService)->generatePrivateUrl($fileUrl);
}
}