"use strict"; const common_vendor = require("../../common/vendor.js"); const common_http = require("../../common/http.js"); const common_constants = require("../../common/constants.js"); const common_assets = require("../../common/assets.js"); function todayString() { const d = /* @__PURE__ */ new Date(); const m = (d.getMonth() + 1).toString().padStart(2, "0"); const day = d.getDate().toString().padStart(2, "0"); return `${d.getFullYear()}-${m}-${day}`; } const _sfc_main = { data() { return { biz: "sale", saleType: "out", purchaseType: "in", order: { orderTime: todayString(), customerId: null, supplierId: null, remark: "" }, customerName: "", customerPriceLevel: "零售价", _lastCustomerId: null, _priceCache: {}, supplierName: "", items: [], activeCategory: "sale_income", counterpartyType: "customer", trxAmount: 0, selectedAccountId: null, selectedAccountName: "", // 收款/付款输入 payments: { cash: 0, bank: 0, wechat: 0 }, showMore: false, SEG_ICONS: { sale: { out: "/static/icons/icons8-shopping-cart-100.png", return: "/static/icons/icons8-return-purchase-50.png", collect: "/static/icons/icons8-profit-50.png" }, purchase: { in: "/static/icons/icons8-purchase-order-100.png", return: "/static/icons/icons8-return-purchase-50.png", pay: "/static/icons/icons8-dollar-ethereum-exchange-50.png" } } }; }, computed: { totalQuantity() { return this.items.reduce((s, it) => s + Number(it.quantity || 0), 0); }, totalAmount() { return this.items.reduce((s, it) => s + Number(it.quantity || 0) * Number(it.unitPrice || 0), 0); }, customerLabel() { return this.customerName || "零售客户"; }, supplierLabel() { return this.supplierName || "零散供应商"; }, incomeCategories() { return this._incomeCategories || common_constants.INCOME_CATEGORIES; }, expenseCategories() { return this._expenseCategories || common_constants.EXPENSE_CATEGORIES; }, accountLabel() { return this.selectedAccountName || "现金"; }, counterpartyLabel() { return this.counterpartyType === "customer" ? this.customerName || "—" : this.supplierName || "—"; }, // 收款/付款合计 payTotal() { const p = this.payments || { cash: 0, bank: 0, wechat: 0 }; return Number(p.cash || 0) + Number(p.bank || 0) + Number(p.wechat || 0); } }, onLoad(query) { try { const preset = common_vendor.index.getStorageSync("ORDER_DEFAULT_PARAMS") || {}; const biz = query && query.biz || preset.biz; const type = query && query.type || preset.type; if (biz === "sale" || biz === "purchase" || biz === "income" || biz === "expense") { this.biz = biz; } if (this.biz === "sale" && (type === "out" || type === "return" || type === "collect")) { this.saleType = type; } if (this.biz === "purchase" && (type === "in" || type === "return" || type === "pay")) { this.purchaseType = type; } try { common_vendor.index.removeStorageSync("ORDER_DEFAULT_PARAMS"); } catch (_) { } } catch (e) { } this.fetchCategories(); }, onShow() { if (this.biz === "sale") { if (this.order.customerId && this.order.customerId !== this._lastCustomerId) { this.loadCustomerLevel(this.order.customerId).then(() => { this._lastCustomerId = this.order.customerId; for (const it of this.items) { if (it && (it._autoPrice || !it.unitPrice)) this.autoPriceItem(it); } }); } for (const it of this.items) { if (it && !it.unitPrice) this.autoPriceItem(it); } } }, methods: { fixMojibake(s) { try { if (!s) return s; const bad = /[ÂÃæåé¼½¢]/.test(s); if (!bad) return s; return decodeURIComponent(escape(s)); } catch (_) { return s; } }, normalizeCats(list) { if (!Array.isArray(list)) return []; return list.map((it) => ({ key: it && it.key || "", label: this.fixMojibake(it && it.label || "") })).filter((it) => it.key && it.label); }, async fetchCategories() { try { const res = await common_http.get("/api/finance/categories"); if (res && Array.isArray(res.incomeCategories)) this._incomeCategories = this.normalizeCats(res.incomeCategories); if (res && Array.isArray(res.expenseCategories)) this._expenseCategories = this.normalizeCats(res.expenseCategories); this.ensureActiveCategory(); } catch (_) { this.ensureActiveCategory(); } }, ensureActiveCategory() { const list = this.biz === "income" ? this.incomeCategories || [] : this.expenseCategories || []; if (!list.length) return; const exists = list.some((it) => it && it.key === this.activeCategory); if (!exists) this.activeCategory = list[0].key; }, async loadCustomerLevel(customerId) { try { const d = await common_http.get(`/api/customers/${customerId}`); this.customerPriceLevel = d && d.priceLevel ? d.priceLevel : "零售价"; } catch (e) { this.customerPriceLevel = "零售价"; } }, priceFieldForLevel() { const lvl = this.customerPriceLevel || "零售价"; if (lvl === "批发价") return "wholesalePrice"; if (lvl === "大单报价") return "bigClientPrice"; return "retailPrice"; }, async autoPriceItem(it) { if (this.biz !== "sale") return; if (!it || !it.productId) return; const pid = it.productId; let detail = this._priceCache[pid]; if (!detail) { try { detail = await common_http.get(`/api/products/${pid}`); this._priceCache[pid] = detail; } catch (e) { return; } } const field = this.priceFieldForLevel(); let price = Number(detail && detail[field] != null ? detail[field] : 0); if (!price && field !== "retailPrice") { price = Number(detail && detail.retailPrice != null ? detail.retailPrice : 0); } it.unitPrice = price; it._autoPrice = true; this.recalc(); }, onPriceInput(it) { if (it) { it._autoPrice = false; this.recalc(); } }, switchBiz(type) { this.biz = type; this.ensureActiveCategory(); }, onDateChange(e) { this.order.orderTime = e.detail.value; }, chooseCustomer() { common_vendor.index.navigateTo({ url: "/pages/customer/select" }); }, chooseSupplier() { common_vendor.index.navigateTo({ url: "/pages/supplier/select" }); }, chooseProduct() { common_vendor.index.navigateTo({ url: "/pages/product/select" }); }, chooseAccount() { common_vendor.index.navigateTo({ url: "/pages/account/select?mode=pick" }); }, chooseCounterparty() { if (!(this.biz === "income" || this.biz === "expense")) return; if (this.counterpartyType === "customer") { common_vendor.index.navigateTo({ url: "/pages/customer/select" }); } else { common_vendor.index.navigateTo({ url: "/pages/supplier/select" }); } }, setCounterparty(t) { this.counterpartyType = t; this.ensureActiveCategory(); }, recalc() { this.$forceUpdate(); }, recalcPay() { this.$forceUpdate(); }, async submit() { const isSaleOrPurchase = this.biz === "sale" || this.biz === "purchase"; const isCollectOrPay = this.biz === "sale" && this.saleType === "collect" || this.biz === "purchase" && this.purchaseType === "pay"; const saleTypeValue = this.biz === "sale" ? "sale." + this.saleType : "purchase." + this.purchaseType; if (isSaleOrPurchase && !isCollectOrPay) { if (!this.items.length) { common_vendor.index.showToast({ title: "请先选择商品", icon: "none" }); return; } const invalid = this.items.find((it) => !it.productId || Number(it.quantity || 0) <= 0); if (invalid) { common_vendor.index.showToast({ title: "数量需大于0", icon: "none" }); return; } } const payload = isSaleOrPurchase ? isCollectOrPay ? [ { method: "cash", amount: Number(this.payments.cash || 0) }, { method: "bank", amount: Number(this.payments.bank || 0) }, { method: "wechat", amount: Number(this.payments.wechat || 0) } ].filter((p) => p.amount > 0) : { type: saleTypeValue, orderTime: this.order.orderTime, customerId: this.order.customerId, supplierId: this.order.supplierId, items: this.items.map((it) => ({ productId: it.productId, quantity: Number(it.quantity || 0), unitPrice: Number(it.unitPrice || 0) })), amount: this.totalAmount } : { type: this.biz, category: this.activeCategory, counterpartyType: this.counterpartyType, counterpartyId: this.counterpartyType === "customer" ? this.order.customerId || null : this.order.supplierId || null, accountId: this.selectedAccountId || null, amount: Number(this.trxAmount || 0), txTime: this.order.orderTime, remark: this.order.remark }; try { const url = isSaleOrPurchase ? isCollectOrPay ? `/api/payments/${this.biz}` : "/api/orders" : "/api/other-transactions"; await common_http.post(url, payload); common_vendor.index.showToast({ title: "已保存", icon: "success" }); setTimeout(() => { common_vendor.index.navigateBack(); }, 600); } catch (e) { common_vendor.index.showToast({ title: e && e.message || "保存失败", icon: "none" }); } }, saveAndReset() { this.items = []; this.trxAmount = 0; this.order.remark = ""; this.payments = { cash: 0, bank: 0, wechat: 0 }; } } }; function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return common_vendor.e({ a: $data.biz === "sale" ? 1 : "", b: common_vendor.o(($event) => $options.switchBiz("sale")), c: $data.biz === "purchase" ? 1 : "", d: common_vendor.o(($event) => $options.switchBiz("purchase")), e: $data.biz === "income" ? 1 : "", f: common_vendor.o(($event) => $options.switchBiz("income")), g: $data.biz === "expense" ? 1 : "", h: common_vendor.o(($event) => $options.switchBiz("expense")), i: $data.biz === "sale" }, $data.biz === "sale" ? { j: $data.SEG_ICONS.sale.out, k: common_vendor.n($data.saleType === "out" && "active"), l: common_vendor.o(($event) => $data.saleType = "out"), m: $data.SEG_ICONS.sale.return, n: common_vendor.n($data.saleType === "return" && "active"), o: common_vendor.o(($event) => $data.saleType = "return"), p: $data.SEG_ICONS.sale.collect, q: common_vendor.n($data.saleType === "collect" && "active"), r: common_vendor.o(($event) => $data.saleType = "collect") } : $data.biz === "purchase" ? { t: $data.SEG_ICONS.purchase.in, v: common_vendor.n($data.purchaseType === "in" && "active"), w: common_vendor.o(($event) => $data.purchaseType = "in"), x: $data.SEG_ICONS.purchase.return, y: common_vendor.n($data.purchaseType === "return" && "active"), z: common_vendor.o(($event) => $data.purchaseType = "return"), A: $data.SEG_ICONS.purchase.pay, B: common_vendor.n($data.purchaseType === "pay" && "active"), C: common_vendor.o(($event) => $data.purchaseType = "pay") } : {}, { s: $data.biz === "purchase", D: common_vendor.t($data.order.orderTime), E: $data.order.orderTime, F: common_vendor.o((...args) => $options.onDateChange && $options.onDateChange(...args)), G: $data.biz === "sale" }, $data.biz === "sale" ? { H: common_vendor.t($options.customerLabel), I: common_vendor.o((...args) => $options.chooseCustomer && $options.chooseCustomer(...args)) } : $data.biz === "purchase" ? { K: common_vendor.t($options.supplierLabel), L: common_vendor.o((...args) => $options.chooseSupplier && $options.chooseSupplier(...args)) } : {}, { J: $data.biz === "purchase", M: $data.biz === "sale" && $data.saleType === "collect" || $data.biz === "purchase" && $data.purchaseType === "pay" }, $data.biz === "sale" && $data.saleType === "collect" || $data.biz === "purchase" && $data.purchaseType === "pay" ? common_vendor.e({ N: $data.biz === "sale" }, $data.biz === "sale" ? { O: common_vendor.t($options.customerLabel), P: common_vendor.o((...args) => $options.chooseCustomer && $options.chooseCustomer(...args)) } : { Q: common_vendor.t($options.supplierLabel), R: common_vendor.o((...args) => $options.chooseSupplier && $options.chooseSupplier(...args)) }, { S: common_vendor.o([common_vendor.m(($event) => $data.payments.cash = $event.detail.value, { number: true }), ($event) => $options.recalcPay()]), T: $data.payments.cash, U: common_vendor.o([common_vendor.m(($event) => $data.payments.bank = $event.detail.value, { number: true }), ($event) => $options.recalcPay()]), V: $data.payments.bank, W: common_vendor.o([common_vendor.m(($event) => $data.payments.wechat = $event.detail.value, { number: true }), ($event) => $options.recalcPay()]), X: $data.payments.wechat, Y: common_vendor.t($data.showMore ? "收起" : ""), Z: common_vendor.o(($event) => $data.showMore = !$data.showMore), aa: common_vendor.t($options.payTotal.toFixed(2)), ab: $data.order.remark, ac: common_vendor.o(($event) => $data.order.remark = $event.detail.value), ad: common_vendor.t($data.order.orderTime), ae: $data.order.orderTime, af: common_vendor.o((...args) => $options.onDateChange && $options.onDateChange(...args)) }) : $data.biz === "sale" || $data.biz === "purchase" ? { ah: common_vendor.t($data.biz === "sale" ? "客户" : "供应商"), ai: common_vendor.t($data.biz === "sale" ? $options.customerLabel : $options.supplierLabel), aj: common_vendor.o(($event) => $data.biz === "sale" ? $options.chooseCustomer() : $options.chooseSupplier()), ak: common_vendor.t($data.order.orderTime), al: $data.order.orderTime, am: common_vendor.o((...args) => $options.onDateChange && $options.onDateChange(...args)), an: common_assets._imports_0, ao: common_vendor.o((...args) => $options.chooseProduct && $options.chooseProduct(...args)), ap: common_vendor.t($options.totalQuantity), aq: common_vendor.t($options.totalAmount.toFixed(2)), ar: common_vendor.o((...args) => $options.chooseProduct && $options.chooseProduct(...args)) } : { as: $data.counterpartyType === "customer" ? 1 : "", at: common_vendor.o(($event) => $options.setCounterparty("customer")), av: $data.counterpartyType === "supplier" ? 1 : "", aw: common_vendor.o(($event) => $options.setCounterparty("supplier")), ax: common_vendor.f($data.biz === "income" ? $options.incomeCategories : $options.expenseCategories, (c, k0, i0) => { return { a: common_vendor.t(c.label), b: c.key, c: $data.activeCategory === c.key ? 1 : "", d: common_vendor.o(($event) => $data.activeCategory = c.key, c.key) }; }), ay: common_vendor.t($options.counterpartyLabel), az: common_vendor.o((...args) => $options.chooseCounterparty && $options.chooseCounterparty(...args)), aA: common_vendor.t($options.accountLabel), aB: common_vendor.o((...args) => $options.chooseAccount && $options.chooseAccount(...args)), aC: $data.trxAmount, aD: common_vendor.o(common_vendor.m(($event) => $data.trxAmount = $event.detail.value, { number: true })), aE: $data.order.remark, aF: common_vendor.o(($event) => $data.order.remark = $event.detail.value) }, { ag: $data.biz === "sale" || $data.biz === "purchase", aG: !$data.items.length }, !$data.items.length ? {} : { aH: common_vendor.f($data.items, (it, idx, i0) => { return { a: common_vendor.t(it.productName), b: common_vendor.o([common_vendor.m(($event) => it.quantity = $event.detail.value, { number: true }), ($event) => $options.recalc()], idx), c: it.quantity, d: common_vendor.o([common_vendor.m(($event) => it.unitPrice = $event.detail.value, { number: true }), ($event) => $options.onPriceInput(it)], idx), e: it.unitPrice, f: common_vendor.t((Number(it.quantity) * Number(it.unitPrice)).toFixed(2)), g: idx }; }) }, { aI: common_vendor.o((...args) => $options.saveAndReset && $options.saveAndReset(...args)), aJ: common_vendor.o((...args) => $options.submit && $options.submit(...args)) }); } const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]); wx.createPage(MiniProgramPage); //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/order/create.js.map