"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 }; }, 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 biz = query && query.biz; const type = query && query.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; } } 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: { async fetchCategories() { try { const res = await common_http.get("/api/finance/categories"); if (res && Array.isArray(res.incomeCategories)) this._incomeCategories = res.incomeCategories; if (res && Array.isArray(res.expenseCategories)) this._expenseCategories = 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.saleType === "out" ? 1 : "", k: common_vendor.o(($event) => $data.saleType = "out"), l: $data.saleType === "return" ? 1 : "", m: common_vendor.o(($event) => $data.saleType = "return"), n: $data.saleType === "collect" ? 1 : "", o: common_vendor.o(($event) => $data.saleType = "collect") } : $data.biz === "purchase" ? { q: $data.purchaseType === "in" ? 1 : "", r: common_vendor.o(($event) => $data.purchaseType = "in"), s: $data.purchaseType === "return" ? 1 : "", t: common_vendor.o(($event) => $data.purchaseType = "return"), v: $data.purchaseType === "pay" ? 1 : "", w: common_vendor.o(($event) => $data.purchaseType = "pay") } : {}, { p: $data.biz === "purchase", x: common_vendor.t($data.order.orderTime), y: $data.order.orderTime, z: common_vendor.o((...args) => $options.onDateChange && $options.onDateChange(...args)), A: $data.biz === "sale" }, $data.biz === "sale" ? { B: common_vendor.t($options.customerLabel), C: common_vendor.o((...args) => $options.chooseCustomer && $options.chooseCustomer(...args)) } : $data.biz === "purchase" ? { E: common_vendor.t($options.supplierLabel), F: common_vendor.o((...args) => $options.chooseSupplier && $options.chooseSupplier(...args)) } : {}, { D: $data.biz === "purchase", G: $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({ H: $data.biz === "sale" }, $data.biz === "sale" ? { I: common_vendor.t($options.customerLabel), J: common_vendor.o((...args) => $options.chooseCustomer && $options.chooseCustomer(...args)) } : { K: common_vendor.t($options.supplierLabel), L: common_vendor.o((...args) => $options.chooseSupplier && $options.chooseSupplier(...args)) }, { M: common_vendor.o([common_vendor.m(($event) => $data.payments.cash = $event.detail.value, { number: true }), ($event) => $options.recalcPay()]), N: $data.payments.cash, O: common_vendor.o([common_vendor.m(($event) => $data.payments.bank = $event.detail.value, { number: true }), ($event) => $options.recalcPay()]), P: $data.payments.bank, Q: common_vendor.o([common_vendor.m(($event) => $data.payments.wechat = $event.detail.value, { number: true }), ($event) => $options.recalcPay()]), R: $data.payments.wechat, S: common_vendor.t($data.showMore ? "收起" : ""), T: common_vendor.o(($event) => $data.showMore = !$data.showMore), U: common_vendor.t($options.payTotal.toFixed(2)), V: $data.order.remark, W: common_vendor.o(($event) => $data.order.remark = $event.detail.value), X: common_vendor.t($data.order.orderTime), Y: $data.order.orderTime, Z: common_vendor.o((...args) => $options.onDateChange && $options.onDateChange(...args)) }) : $data.biz === "sale" || $data.biz === "purchase" ? { ab: common_vendor.t($options.totalQuantity), ac: common_vendor.t($options.totalAmount.toFixed(2)), ad: common_vendor.o((...args) => $options.chooseProduct && $options.chooseProduct(...args)) } : { ae: $data.counterpartyType === "customer" ? 1 : "", af: common_vendor.o(($event) => $options.setCounterparty("customer")), ag: $data.counterpartyType === "supplier" ? 1 : "", ah: common_vendor.o(($event) => $options.setCounterparty("supplier")), ai: 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) }; }), aj: common_vendor.t($options.counterpartyLabel), ak: common_vendor.o((...args) => $options.chooseCounterparty && $options.chooseCounterparty(...args)), al: common_vendor.t($options.accountLabel), am: common_vendor.o((...args) => $options.chooseAccount && $options.chooseAccount(...args)), an: $data.trxAmount, ao: common_vendor.o(common_vendor.m(($event) => $data.trxAmount = $event.detail.value, { number: true })), ap: $data.order.remark, aq: common_vendor.o(($event) => $data.order.remark = $event.detail.value) }, { aa: $data.biz === "sale" || $data.biz === "purchase", ar: !$data.items.length }, !$data.items.length ? { as: common_assets._imports_0$1 } : { at: 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 }; }) }, { av: common_vendor.o((...args) => $options.saveAndReset && $options.saveAndReset(...args)), aw: 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