From e12d5951e403eee118d9ff245726994fd12bbf7c Mon Sep 17 00:00:00 2001 From: mzl Date: Mon, 22 Sep 2025 01:55:34 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=A1=C3=A5=C2=A4?= =?UTF-8?q?=E5=A4=87=E8=B4=A7=E5=8D=95=E9=87=91=E9=A2=9D=E8=AE=A1=E7=AE=97?= =?UTF-8?q?&&=E9=87=87=E8=B4=AD=E5=8D=95=E7=BC=96=E5=86=99=C3=A2=C2=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PurchaseOrderItemsController.php | 33 +++ .../controller/PurchaseOrdersController.php | 51 ++++ app/common/logic/PurchaseOrderItemsLogic.php | 17 ++ app/common/logic/PurchaseOrdersLogic.php | 51 ++++ app/common/model/PurchaseOrderItems.php | 49 ++++ app/common/model/PurchaseOrders.php | 49 ++++ .../service/PurchaseOrderItemsService.php | 31 +++ app/common/service/PurchaseOrdersService.php | 84 ++++++ app/common/service/StockOrdersService.php | 242 +++++++++++++++++- app/common/service/VehMoneyValueService.php | 46 ++++ .../validate/PurchaseOrderItemsValidate.php | 29 +++ .../validate/PurchaseOrdersValidate.php | 29 +++ app/controller/IndexController.php | 63 ++--- config/route.php | 5 + 14 files changed, 745 insertions(+), 34 deletions(-) create mode 100644 app/common/controller/PurchaseOrderItemsController.php create mode 100644 app/common/controller/PurchaseOrdersController.php create mode 100644 app/common/logic/PurchaseOrderItemsLogic.php create mode 100644 app/common/logic/PurchaseOrdersLogic.php create mode 100644 app/common/model/PurchaseOrderItems.php create mode 100644 app/common/model/PurchaseOrders.php create mode 100644 app/common/service/PurchaseOrderItemsService.php create mode 100644 app/common/service/PurchaseOrdersService.php create mode 100644 app/common/validate/PurchaseOrderItemsValidate.php create mode 100644 app/common/validate/PurchaseOrdersValidate.php diff --git a/app/common/controller/PurchaseOrderItemsController.php b/app/common/controller/PurchaseOrderItemsController.php new file mode 100644 index 0000000..e602109 --- /dev/null +++ b/app/common/controller/PurchaseOrderItemsController.php @@ -0,0 +1,33 @@ +service = new PurchaseOrderItemsService(); + } + + /** + * 列表页数据格式化 + * + * @param array $data + * @return array + */ + // protected function indexFormat($data) + // { + // if (isset($data['created_at'])) + // $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']); + // if (isset($data['updated_at'])) + // $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']); + // return $data; + // } +} \ No newline at end of file diff --git a/app/common/controller/PurchaseOrdersController.php b/app/common/controller/PurchaseOrdersController.php new file mode 100644 index 0000000..a7b7af7 --- /dev/null +++ b/app/common/controller/PurchaseOrdersController.php @@ -0,0 +1,51 @@ +service = new PurchaseOrdersService(); + } + + /** + * 列表页数据格式化 + * + * @param array $data + * @return array + */ + // protected function indexFormat($data) + // { + // if (isset($data['created_at'])) + // $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']); + // if (isset($data['updated_at'])) + // $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']); + // return $data; + // } + + + /** + * 前置查询处理 + * + * @param Request $request + * @return array + */ + public function preQueryData(Request $request) + { + try { + $data = $this->service->preQueryData($request->all()); + return $this->success($data); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/common/logic/PurchaseOrderItemsLogic.php b/app/common/logic/PurchaseOrderItemsLogic.php new file mode 100644 index 0000000..6c5702d --- /dev/null +++ b/app/common/logic/PurchaseOrderItemsLogic.php @@ -0,0 +1,17 @@ +where('pi_no', '!=', '') + ->select('pi_no') + ->distinct() + ->get(); + } + + /** + * 根据PI号获取备货单 + */ + public function getOrderNoListByPiNo($piNo) + { + return (new StockOrders())->where('pi_no', $piNo) + ->select('order_no', 'biz_date', 'salesperson_snapshot', 'id as stock_orders_id') + ->get(); + } + + /** + * 根据备货单号获取备货单明细 + */ + public function getOrderItemsListByOrderNo($stockOrdersId) + { + return (new StockOrderItems())->where('order_id', $stockOrdersId) + ->select('model_snapshot', 'color_interior_snapshot', 'color_exterior_snapshot', 'unique_id', 'id as stock_order_items_id') + ->get(); + } +} \ No newline at end of file diff --git a/app/common/model/PurchaseOrderItems.php b/app/common/model/PurchaseOrderItems.php new file mode 100644 index 0000000..feff909 --- /dev/null +++ b/app/common/model/PurchaseOrderItems.php @@ -0,0 +1,49 @@ +validator; + } +} \ No newline at end of file diff --git a/app/common/service/PurchaseOrdersService.php b/app/common/service/PurchaseOrdersService.php new file mode 100644 index 0000000..7fc957f --- /dev/null +++ b/app/common/service/PurchaseOrdersService.php @@ -0,0 +1,84 @@ +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 $result; + } + + public function beforeServiceStore(array $data, Request $request) + { + $data['po_no'] = generateStockNo('CG'); + $data['buyer_id'] = $request->user->uid; + return $data; + } + + public function serviceStore(Request $request, array $data = [], string $scene = 'store') + { + $data = $this->preProcess(ucfirst($scene), $data, $request); + $dl = $this->getDataLimitFromRequest($request); + // 创建采购单 + $created = $this->logicCreate($data, $dl); + 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); + } +} \ No newline at end of file diff --git a/app/common/service/StockOrdersService.php b/app/common/service/StockOrdersService.php index d553ed7..3ce89d4 100644 --- a/app/common/service/StockOrdersService.php +++ b/app/common/service/StockOrdersService.php @@ -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); } /** @@ -739,4 +739,238 @@ 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); + 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 + ] + $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); + } } \ No newline at end of file diff --git a/app/common/service/VehMoneyValueService.php b/app/common/service/VehMoneyValueService.php index 361aa55..af45547 100644 --- a/app/common/service/VehMoneyValueService.php +++ b/app/common/service/VehMoneyValueService.php @@ -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); + } } \ No newline at end of file diff --git a/app/common/validate/PurchaseOrderItemsValidate.php b/app/common/validate/PurchaseOrderItemsValidate.php new file mode 100644 index 0000000..e44a977 --- /dev/null +++ b/app/common/validate/PurchaseOrderItemsValidate.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/common/validate/PurchaseOrdersValidate.php b/app/common/validate/PurchaseOrdersValidate.php new file mode 100644 index 0000000..a8b4a5a --- /dev/null +++ b/app/common/validate/PurchaseOrdersValidate.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index b25b5a4..cd5e64f 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -69,42 +69,45 @@ class IndexController */ public function createResource() { - $options = [ - 'controller' => [ - 'module' => 'crm', - 'name' => 'approval_flows' - ], - 'model' => [ - 'module' => 'crm', - 'name' => 'approval_flows' - ], - 'service' => [ - 'module' => 'crm', - 'name' => 'approval_flows' - ], - 'logic' => [ - 'module' => 'crm', - 'name' => 'approval_flows' - ], - 'validate' => [ - 'module' => 'crm', - 'name' => 'approval_flows' - ], - 'table' => 'approval_flows' - ]; + // $options = [ + // 'controller' => [ + // 'module' => 'common', + // 'name' => 'purchase_order_items' + // ], + // 'model' => [ + // 'module' => 'common', + // 'name' => 'purchase_order_items' + // ], + // 'service' => [ + // 'module' => 'common', + // 'name' => 'purchase_order_items' + // ], + // 'logic' => [ + // 'module' => 'common', + // 'name' => 'purchase_order_items' + // ], + // 'validate' => [ + // 'module' => 'common', + // 'name' => 'purchase_order_items' + // ], + // 'table' => 'purchase_order_items' + // ]; - $result = (new Generate($options))->doGenerate(); - return $result; + // $result = (new Generate($options))->doGenerate(); + // return $result; } public function test(Request $request) { - // 菜单迁移 - // $where[] = ['id', '>', 141]; - // $list = (new \app\common\utils\MigrationResources())->do('resources_20250916_bak', 'resources_yuanshi', $where); - $list = (new \app\common\utils\MigrationResources())->getFullResourceTreeOptimized('resources_20250916_bak', []); - var_dump($list); + // $list = (new \app\common\logic\PurchaseOrdersLogic())->getPiList()->toArray(); + + + // $list = (new \app\common\logic\PurchaseOrdersLogic())->getOrderNoListByPiNo('ces1222')->toArray(); + + + // $list = (new \app\common\logic\PurchaseOrdersLogic())->getOrderItemsListByOrderNo(32)->toArray(); + // var_dump($list); } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index 887c683..3292d71 100644 --- a/config/route.php +++ b/config/route.php @@ -351,6 +351,11 @@ Route::group('/admin', function () { Route::any('/user/achieved', [app\admin\targets\controller\StatisticsController::class, 'userAchieved']); }); }); + + + Route::group('/purchase/order', function () { + Route::any('/predata', [app\common\controller\PurchaseOrdersController::class, 'preQueryData']); + }); });