import hocAuth from "@/hocs/auth"; import { CommonComponent } from "@/types/typings"; import Taro from "@tarojs/taro"; import { business } from "@/services"; import { useEffect, useState } from "react"; import { View } from "@tarojs/components"; import purchaseOrder from "@/constant/purchaseOrder"; import { ActionSheet, Button, Dialog, Input, Popup, SafeArea, TextArea, Toast, } from "@nutui/nutui-react-taro"; import { BasicInfoSection, CompanyInfoSection, CostDifferenceSection, CostSummarySection, DealerInfoSection, EmptyBoxInfoSection, MarketPriceSection, MaterialCostSection, PackageInfoSection, PackagingCostSection, ProductionAdvanceSection, PurchaseCostInfoSection, RebateCalcSection, State, TaxProvisionSection, TaxSubsidySection, WorkerAdvanceSection, } from "@/components"; import { buildUrl, formatCurrency, PurchaseOrderCalculator } from "@/utils"; import ProductionLossSection from "../../../components/purchase/section/ProductionLossSection"; import DeliveryFormSection from "../../../components/purchase/section/DeliveryFormSection"; import classNames from "classnames"; const sections = { // 市场报价 marketPrice: { component: MarketPriceSection, title: "市场报价", }, // 我方入账公司 supplierInfo: { component: CompanyInfoSection, title: "我方入账公司", }, // 下游经销商信息 dealerInfo: { component: DealerInfoSection, title: "下游经销商", }, // 基础信息 basicInfo: { component: BasicInfoSection, title: "基础信息", }, // // 瓜农信息 // farmerInfo: { // component: SupplierInfoSection, // title: "瓜农信息", // }, // 采购成本 purchaseCostInfo: { component: PurchaseCostInfoSection, title: "采购成本复核", }, // 包装纸箱费 packageInfo: { component: PackageInfoSection, title: "包装纸箱费复核", }, // 空箱费用 emptyBoxInfo: { component: EmptyBoxInfoSection, title: "空箱费用复核", }, // 人工费用复核 foremanAdvance: { component: WorkerAdvanceSection, title: "人工费用复核", }, // 辅料费用复核 materialCost: { component: MaterialCostSection, title: "辅料费复核", }, // 产地费用复核 originAdvance: { component: ProductionAdvanceSection, title: "产地费用复核", }, // 其他费用复核 otherCost: { component: PackagingCostSection, title: "其他费用复核", }, // 产地损耗复核 productionLoss: { component: ProductionLossSection, title: "产地损耗复核", }, // 公司返点复核 taxSubsidy: { component: TaxSubsidySection, title: "公司返点复核", }, // 计提税金复核 taxProvision: { component: TaxProvisionSection, title: "计提税金复核", }, // 调诚信志远分红 costDifference: { component: CostDifferenceSection, title: "调诚信志远分红", }, // 成本合计 costSummary: { component: CostSummarySection, title: "成本合计", }, // 个人返点复核 rebateCalc: { component: RebateCalcSection, title: "个人返点复核", }, // 发货单复核 deliveryForm: { component: DeliveryFormSection, title: "发货单复核", }, }; export default hocAuth(function Page(props: CommonComponent) { const { router, isInitialized, setIsInitialized, userRoleVO, setLoading } = props; const orderId = router.params .orderId as BusinessAPI.PurchaseOrderVO["orderId"]; // 费用项目列表 const [costList, setCostList] = useState([]); // 获取费用项目列表 useEffect(() => { const fetchCost = async () => { try { const { data } = await business.cost.listCost({ costListQry: { status: true, }, }); setCostList(data.data || []); } catch (error) { console.error("获取费用项目列表失败:", error); } }; fetchCost(); }, []); const [purchaseOrderVO, setPurchaseOrderVO] = useState(); console.log("purchaseOrderVO", purchaseOrderVO); // 驳回原因弹窗相关状态 const [rejectVisible, setRejectVisible] = useState(false); const [rejectReason, setRejectReason] = useState(""); // 驳回操作 const handleReject = () => { setRejectVisible(true); }; // 确认驳回 const confirmReject = async () => { if (!purchaseOrderVO) { Toast.show("toast", { icon: "fail", title: "提示", content: "请稍后再试", }); return; } if (!rejectReason.trim()) { Toast.show("toast", { icon: "fail", title: "提示", content: "请输入驳回原因", }); return; } try { // 调用驳回API const { data: { success, errMessage }, } = await business.purchaseOrder.rejectApprovePurchaseOrder({ orderId: purchaseOrderVO?.orderId, rejectReason: rejectReason, }); if (!success) { Toast.show("toast", { icon: "fail", title: "提示", content: errMessage || "驳回失败", }); return; } Toast.show("toast", { icon: "success", title: "提示", content: "驳回成功,正在跳转我的审核...", }); // 关闭弹窗 setRejectVisible(false); setRejectReason(""); setTimeout(() => { Taro.redirectTo({ url: buildUrl("/pages/purchase/audit/pending"), }); }, 1000); // 返回采购单页面 } catch (error) { Toast.show("toast", { icon: "fail", title: "提示", content: error.message || "驳回失败", }); } }; const [originPrincipal, setOriginPrincipal] = useState(""); // 暂存和提交审核的Dialog状态 const [saveDialogVisible, setSaveDialogVisible] = useState(false); const [submitDialogVisible, setSubmitDialogVisible] = useState(false); // 控制更多操作的ActionSheet显示状态 const [moreActionVisible, setMoreActionVisible] = useState(false); const [dealerRebateCustomerVOList, setDealerRebateCustomerVOList] = useState(); const initDealer = async (dealerId: BusinessAPI.DealerVO["dealerId"]) => { const { data: { data: dealerRebateCustomerVOList }, } = await business.dealerRebateCustomer.listDealerRebateCustomer({ dealerRebateCustomerListQry: { dealerId: dealerId, status: true, }, }); setDealerRebateCustomerVOList(dealerRebateCustomerVOList); }; // 暂存操作 const handleSave = () => { setSaveDialogVisible(true); }; // 确认暂存 const confirmSave = async () => { // 关闭对话框 setSaveDialogVisible(false); // 这里应该调用暂存API console.log("暂存数据:", purchaseOrderVO); const { data } = await business.purchaseOrder.approvePurchaseOrder({ ...purchaseOrderVO!, draft: true, }); if (data.success) { Toast.show("toast", { icon: "success", title: "提示", content: "暂存成功", }); // 返回采购单页面 Taro.redirectTo({ url: "/pages/purchase/audit/pending" }); } }; // 提交老板审核操作 const handleSubmit = () => { setSubmitDialogVisible(true); }; // 确认提交老板审核 const confirmSubmit = async () => { // 关闭对话框 setSubmitDialogVisible(false); // 表单校验 const errorMsg = validateForm(); if (errorMsg) { Toast.show("toast", { icon: "fail", title: "校验失败", content: errorMsg, }); return; } // 这里应该调用提交审核API console.log("提交老板审核:", purchaseOrderVO); const { data } = await business.purchaseOrder.approvePurchaseOrder({ ...purchaseOrderVO!, draft: false, }); if (data.success) { Toast.show("toast", { icon: "success", title: "提示", content: "提交审核成功", }); // 跳转到提交结果页面 Taro.redirectTo({ url: buildUrl("/pages/purchase/audit/result", { orderId: purchaseOrderVO!.orderId, }), }); } }; // 表单校验 const validateForm = () => { // 校验销售方 if (!purchaseOrderVO?.orderCompany?.companyId) { return "请选择销售方"; } // 校验经销商 if (!purchaseOrderVO?.orderDealer?.dealerId) { return "请选择经销商"; } // 校验本车次号 if (!purchaseOrderVO?.orderVehicle?.vehicleNo) { return "请输入本车次号"; } // 校验运费类型 if (!purchaseOrderVO?.orderVehicle?.priceType) { return "请选择运费类型"; } // 校验市场报价的报价方式 if (!purchaseOrderVO?.pricingMethod) { return "请选择市场报价的报价方式"; } // 校验市场报价的销售单价 purchaseOrderVO.orderSupplierList.forEach( (supplier: BusinessAPI.OrderSupplier) => { if (!supplier.salePrice || supplier.salePrice <= 0) { return "请填写市场报价的销售单价"; } }, ); return null; }; const init = async (orderId: BusinessAPI.PurchaseOrderVO["orderId"]) => { const { data } = await business.purchaseOrder.showPurchaseOrder({ purchaseOrderShowQry: { orderId, }, }); if (data.success) { setPurchaseOrderVO(data.data); await initDealer(data.data?.orderDealer?.dealerId!); } }; useEffect(() => { if (orderId && !isInitialized) { setLoading(true); init(orderId).then(() => { setIsInitialized(true); setLoading(false); }); } }, []); if (!purchaseOrderVO || !dealerRebateCustomerVOList || !costList) { return; } const calculator = new PurchaseOrderCalculator(purchaseOrderVO, true); const personalProfit = calculator.getPersonalProfit(); return ( <> {/* 顶部导航 */} {purchaseOrderVO?.orderDealer?.shortName || "-"} 第 {purchaseOrderVO?.orderVehicle?.vehicleNo || "-"}车 {purchaseOrderVO?.orderVehicle.origin || "-"} 至{" "} {purchaseOrderVO?.orderVehicle.destination || "-"} {/* 展示产地负责人*/} 产地负责人 setOriginPrincipal(value)} onBlur={() => { // 更新采购订单中的产地负责人 setPurchaseOrderVO((prev) => ({ ...prev!, originPrincipal: originPrincipal, })); }} className="w-full bg-transparent" /> {/* 循环渲染各部分内容 */} {Object.keys(sections).map((sectionKey) => { const section = sections[sectionKey]; const orderDealer = purchaseOrderVO.orderDealer; if ( !orderDealer?.enableCompanyRebate && sectionKey === "taxSubsidy" ) { return null; } if ( !orderDealer?.enableAccrualTax && sectionKey === "taxProvision" ) { return null; } if (!orderDealer?.enableShare && sectionKey === "costDifference") { return null; } // 如果没有返点人这个模块,则不渲染 if ( (!dealerRebateCustomerVOList || dealerRebateCustomerVOList.length === 0) && sectionKey === "rebateCalc" ) { return null; } if ( (!purchaseOrderVO.orderPackageList || purchaseOrderVO?.orderPackageList.length === 0) && sectionKey === "emptyBoxInfo" ) { return null; } return ( <> {section.title} ); })} {/* 个人利润 */} 个人利润 0, "text-red-500": personalProfit < 0, })} > ¥ {formatCurrency(personalProfit) || "-"} {/* 按钮操作 */} {purchaseOrderVO.state === "WAITING_AUDIT" && purchaseOrderVO.auditState === "PENDING_QUOTE_APPROVAL" && ( <> )} {purchaseOrderVO.state === "WAITING_AUDIT" && purchaseOrderVO.auditState === "PENDING_BOSS_APPROVAL" && ( <> )} {/* 暂存确认对话框 */} setSaveDialogVisible(false)} onConfirm={confirmSave} /> {/* 提交审核确认对话框 */} setSubmitDialogVisible(false)} onConfirm={confirmSubmit} /> {/* 更多操作ActionSheet */} setMoreActionVisible(false)} onSelect={(item) => { setMoreActionVisible(false); if (item.name === "暂存") { // 暂存操作 handleSave(); } else if (item.name === "驳回") { // 驳回操作 handleReject(); } }} cancelText="取消" /> {/* 驳回原因弹窗 */} { setRejectVisible(false); setRejectReason(""); }} closeable destroyOnClose title={"驳回"} description={"此采购单将退回到产地录入员"} > 请输入驳回原因