|
|
@@ -6,116 +6,106 @@ Page({
|
|
|
disabled: false,
|
|
|
showAll: false,
|
|
|
activeTab: 0,
|
|
|
+ productList: [],
|
|
|
form: [{
|
|
|
- label: "客户",
|
|
|
- error: false,
|
|
|
- errMsg: "",
|
|
|
- type: "text",
|
|
|
- value: "",
|
|
|
- placeholder: "客户名称",
|
|
|
- valueName: "name",
|
|
|
- required: false,
|
|
|
- checking: "base",
|
|
|
- disabled: true
|
|
|
- },
|
|
|
- {
|
|
|
- label: "门店",
|
|
|
- error: false,
|
|
|
- errMsg: "",
|
|
|
- type: "text",
|
|
|
- value: "",
|
|
|
- placeholder: "门店名称",
|
|
|
- valueName: "storename",
|
|
|
- required: false,
|
|
|
- checking: "base",
|
|
|
- disabled: true
|
|
|
- },
|
|
|
- {
|
|
|
- label: "开单日期",
|
|
|
- error: false,
|
|
|
- errMsg: "",
|
|
|
- type: "date",
|
|
|
- value: "",
|
|
|
- placeholder: "请选择开单日期",
|
|
|
- valueName: "billdate",
|
|
|
- required: false,
|
|
|
- checking: "base"
|
|
|
- },
|
|
|
- {
|
|
|
- label: "订单数量",
|
|
|
- error: false,
|
|
|
- errMsg: "",
|
|
|
- type: "number",
|
|
|
- value: "0",
|
|
|
- placeholder: "订单数量",
|
|
|
- valueName: "orderCount",
|
|
|
- required: false,
|
|
|
- checking: "base",
|
|
|
- disabled: true
|
|
|
- },
|
|
|
- {
|
|
|
- label: "订单金额",
|
|
|
- error: false,
|
|
|
- errMsg: "",
|
|
|
- type: "number",
|
|
|
- value: "0",
|
|
|
- placeholder: "订单金额",
|
|
|
- valueName: "amount",
|
|
|
- required: false,
|
|
|
- checking: "base",
|
|
|
- disabled: true
|
|
|
- },
|
|
|
- {
|
|
|
- label: "备注",
|
|
|
- error: false,
|
|
|
- errMsg: "",
|
|
|
- type: "textarea",
|
|
|
- value: "",
|
|
|
- placeholder: "请输入备注",
|
|
|
- valueName: "remarks",
|
|
|
- required: false,
|
|
|
- checking: "base"
|
|
|
- }
|
|
|
+ label: "客户",
|
|
|
+ error: false,
|
|
|
+ errMsg: "",
|
|
|
+ type: "text",
|
|
|
+ value: "",
|
|
|
+ placeholder: "客户名称",
|
|
|
+ valueName: "name",
|
|
|
+ required: false,
|
|
|
+ checking: "base",
|
|
|
+ disabled: true
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: "门店",
|
|
|
+ error: false,
|
|
|
+ errMsg: "",
|
|
|
+ type: "text",
|
|
|
+ value: "",
|
|
|
+ placeholder: "门店名称",
|
|
|
+ valueName: "storename",
|
|
|
+ required: false,
|
|
|
+ checking: "base",
|
|
|
+ disabled: true
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: "开单日期",
|
|
|
+ error: false,
|
|
|
+ errMsg: "",
|
|
|
+ type: "date",
|
|
|
+ value: "",
|
|
|
+ placeholder: "请选择开单日期",
|
|
|
+ valueName: "billdate",
|
|
|
+ required: false,
|
|
|
+ checking: "base"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: "订单数量",
|
|
|
+ error: false,
|
|
|
+ errMsg: "",
|
|
|
+ type: "number",
|
|
|
+ value: "0",
|
|
|
+ placeholder: "订单数量",
|
|
|
+ valueName: "orderCount",
|
|
|
+ required: false,
|
|
|
+ checking: "base",
|
|
|
+ disabled: true
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: "订单金额",
|
|
|
+ error: false,
|
|
|
+ errMsg: "",
|
|
|
+ type: "number",
|
|
|
+ value: "0",
|
|
|
+ placeholder: "订单金额",
|
|
|
+ valueName: "amount",
|
|
|
+ required: false,
|
|
|
+ checking: "base",
|
|
|
+ disabled: true
|
|
|
+ },
|
|
|
+ {
|
|
|
+ label: "备注",
|
|
|
+ error: false,
|
|
|
+ errMsg: "",
|
|
|
+ type: "textarea",
|
|
|
+ value: "",
|
|
|
+ placeholder: "请输入备注",
|
|
|
+ valueName: "remarks",
|
|
|
+ required: false,
|
|
|
+ checking: "base"
|
|
|
+ }
|
|
|
],
|
|
|
"content": {
|
|
|
"sa_custorderid": 0,
|
|
|
- "sa_customersid": "",
|
|
|
- "name": "",
|
|
|
- "storename": "",
|
|
|
- "billdate": "",
|
|
|
- "orderCount": 0,
|
|
|
- "amount": 0,
|
|
|
- "remarks": "",
|
|
|
- "siteid": "MD"
|
|
|
+ "sa_customersid": 0,
|
|
|
}
|
|
|
},
|
|
|
onLoad(options) {
|
|
|
- // 获取客户ID和客户信息
|
|
|
- if (options.sa_customersid) {
|
|
|
+ // 如果传递了客户信息,填充到表单
|
|
|
+ try {
|
|
|
+ const customerInfo = getCurrentPages().find(v => v.__route__ == 'CRM/customer/detail').data.detail;
|
|
|
+ let form = this.data.form;
|
|
|
+ console.log("customerInfo", customerInfo)
|
|
|
+ // 填充客户信息到表单
|
|
|
+ form.forEach(item => {
|
|
|
+ if (item.valueName === 'name' && customerInfo.name) {
|
|
|
+ item.value = customerInfo.name;
|
|
|
+ }
|
|
|
+ if (item.valueName === 'storename' && customerInfo.storename) {
|
|
|
+ item.value = customerInfo.storename;
|
|
|
+ }
|
|
|
+ });
|
|
|
this.setData({
|
|
|
- "content.sa_customersid": options.sa_customersid
|
|
|
+ form,
|
|
|
+ "content.sa_storeid": customerInfo.sa_storeid,
|
|
|
+ "content.storeno": customerInfo.storeno,
|
|
|
+ "content.sa_customersid": customerInfo.sa_customersid,
|
|
|
});
|
|
|
-
|
|
|
- // 如果传递了客户信息,填充到表单
|
|
|
- try {
|
|
|
- const customerInfo = getCurrentPages().find(v => v.__route__ == 'CRM/customer/detail').data.detail;
|
|
|
- let form = this.data.form;
|
|
|
- console.log("customerInfo", customerInfo)
|
|
|
- // 填充客户信息到表单
|
|
|
- form.forEach(item => {
|
|
|
- if (item.valueName === 'name' && customerInfo.name) {
|
|
|
- item.value = customerInfo.name;
|
|
|
- }
|
|
|
- if (item.valueName === 'storename' && customerInfo.storename) {
|
|
|
- item.value = customerInfo.storename;
|
|
|
- }
|
|
|
- });
|
|
|
- this.setData({
|
|
|
- form
|
|
|
- });
|
|
|
- } catch (error) {
|
|
|
- console.error("解析客户信息失败", error);
|
|
|
- }
|
|
|
+ } catch (error) {
|
|
|
+ console.error("解析客户信息失败", error);
|
|
|
}
|
|
|
|
|
|
// 设置默认开单日期为当天
|
|
|
@@ -134,15 +124,23 @@ Page({
|
|
|
});
|
|
|
},
|
|
|
submit() {
|
|
|
+ // 检查是否有商品
|
|
|
+ if (this.data.productList.length === 0) {
|
|
|
+ this.addProduct();
|
|
|
+ wx.showToast({
|
|
|
+ title: '请先添加商品',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
this.setData({
|
|
|
loading: true
|
|
|
});
|
|
|
let formData = this.selectComponent("#Form").submit();
|
|
|
-
|
|
|
+
|
|
|
if (!formData) {
|
|
|
- this.setData({
|
|
|
- loading: false
|
|
|
- });
|
|
|
+ this.setData({ loading: false });
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -151,30 +149,32 @@ Page({
|
|
|
...formData
|
|
|
};
|
|
|
|
|
|
+ // 先创建订单
|
|
|
_Http.basic({
|
|
|
"id": "2026031309441701", // 创建订单的接口ID
|
|
|
content
|
|
|
}).then(res => {
|
|
|
- this.setData({
|
|
|
- loading: false
|
|
|
- });
|
|
|
console.log("保存订单", res);
|
|
|
if (res.code == 1) {
|
|
|
- // 刷新客户详情页面
|
|
|
- const pages = getCurrentPages();
|
|
|
- const detailPage = pages.find(v => v.__route__ == 'CRM/customer/detail');
|
|
|
- if (detailPage) {
|
|
|
- detailPage.partialRenewal(true);
|
|
|
- }
|
|
|
- wx.navigateBack({
|
|
|
- success() {
|
|
|
- wx.showToast({
|
|
|
- title: "开单成功",
|
|
|
- icon: "success"
|
|
|
- });
|
|
|
- }
|
|
|
+ const orderId = res.data;
|
|
|
+ console.log("订单ID", orderId);
|
|
|
+
|
|
|
+ // 显示加载提示
|
|
|
+ wx.showLoading({
|
|
|
+ title: '正在绑定商品...',
|
|
|
+ mask: true
|
|
|
+ });
|
|
|
+
|
|
|
+ // 使用Promise.all处理商品绑定
|
|
|
+ this.bindProductsWithPromise(orderId).then(() => {
|
|
|
+ // 绑定完成,跳转到订单详情
|
|
|
+ this.navigateToOrderDetail(orderId);
|
|
|
+ }).catch(() => {
|
|
|
+ // 绑定失败,跳转到订单详情
|
|
|
+ this.navigateToOrderDetail(orderId);
|
|
|
});
|
|
|
} else {
|
|
|
+ this.setData({ loading: false });
|
|
|
wx.showToast({
|
|
|
title: res.msg || '保存失败',
|
|
|
icon: 'none'
|
|
|
@@ -191,33 +191,268 @@ Page({
|
|
|
});
|
|
|
});
|
|
|
},
|
|
|
- interrupt({
|
|
|
- detail
|
|
|
- }) {
|
|
|
- // 处理中断逻辑,如果需要的话
|
|
|
- },
|
|
|
- /* 表单必填项是否完成 */
|
|
|
- onConfirm({
|
|
|
- detail
|
|
|
- }) {
|
|
|
- this.setData({
|
|
|
- disabled: detail
|
|
|
+ // 使用Promise.all绑定商品
|
|
|
+ bindProductsWithPromise(orderId) {
|
|
|
+ const productList = this.data.productList;
|
|
|
+
|
|
|
+ // 创建商品绑定的Promise数组
|
|
|
+ const bindPromises = productList.map(product => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ _Http.basic({
|
|
|
+ "id": "2026031415462301", // 绑定商品接口ID
|
|
|
+ content: {
|
|
|
+ sa_custorderid: orderId,
|
|
|
+ sa_custorderitemsid: 0,
|
|
|
+ sys_enterprise_itemid:product.sys_enterprise_itemid|| product.itemid,
|
|
|
+ qty: product.qty,
|
|
|
+ oldprice: product.originalPrice || product.price,
|
|
|
+ discountrate: product.discount,
|
|
|
+ price: product.price,
|
|
|
+ amount: product.amount,
|
|
|
+ remarks: product.remarks || ""
|
|
|
+ }
|
|
|
+ }).then(res => {
|
|
|
+ if (res.code != 1) {
|
|
|
+ console.error("绑定商品失败", res);
|
|
|
+ }
|
|
|
+ resolve(res);
|
|
|
+ }).catch(err => {
|
|
|
+ console.error("绑定商品失败", err);
|
|
|
+ resolve(null); // 即使失败也继续处理
|
|
|
+ });
|
|
|
});
|
|
|
+ });
|
|
|
+
|
|
|
+// 处理附件绑定(预留接口)
|
|
|
+const attachmentPromise = this.bindAttachmentsWithPromise(orderId);
|
|
|
+
|
|
|
+// 合并所有Promise
|
|
|
+const allPromises = [...bindPromises, attachmentPromise];
|
|
|
+
|
|
|
+// 使用Promise.all处理所有请求
|
|
|
+return Promise.all(allPromises);
|
|
|
+ },
|
|
|
+// 绑定附件(预留接口)
|
|
|
+bindAttachmentsWithPromise(orderId) {
|
|
|
+ return new Promise((resolve) => {
|
|
|
+ // 这里预留附件绑定的逻辑
|
|
|
+ // 目前暂时直接resolve
|
|
|
+ resolve(null);
|
|
|
+ });
|
|
|
+},
|
|
|
+// 跳转到订单详情
|
|
|
+navigateToOrderDetail(orderId) {
|
|
|
+ // 隐藏加载提示
|
|
|
+ wx.hideLoading();
|
|
|
+ this.setData({ loading: false });
|
|
|
+ wx.redirectTo({
|
|
|
+ url: `/CRM/order/detail?id=${orderId}`,
|
|
|
+ success: () => {
|
|
|
+ // 跳转到订单详情页面
|
|
|
+ wx.showToast({
|
|
|
+ title: "开单成功",
|
|
|
+ icon: "none",
|
|
|
+ duration: 1500,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ getCurrentPages().find(v => v.__route__ == 'CRM/customer/detail').partialRenewal(true);
|
|
|
+},
|
|
|
+interrupt({
|
|
|
+ detail
|
|
|
+}) {
|
|
|
+ // 处理中断逻辑,如果需要的话
|
|
|
+},
|
|
|
+/* 表单必填项是否完成 */
|
|
|
+onConfirm({
|
|
|
+ detail
|
|
|
+}) {
|
|
|
+ this.setData({
|
|
|
+ disabled: detail
|
|
|
+ });
|
|
|
+},
|
|
|
+onChange(e) {
|
|
|
+ this.setData({
|
|
|
+ showAll: e.detail
|
|
|
+ });
|
|
|
+},
|
|
|
+closePage() {
|
|
|
+ wx.navigateBack({
|
|
|
+ delta: 1
|
|
|
+ });
|
|
|
+},
|
|
|
+// tab切换
|
|
|
+onTabChange(e) {
|
|
|
+ this.setData({
|
|
|
+ activeTab: e.detail
|
|
|
+ });
|
|
|
+},
|
|
|
+// 打开添加商品面板
|
|
|
+addProduct() {
|
|
|
+ // 直接跳转到产品选择页面
|
|
|
+ wx.navigateTo({
|
|
|
+ url: `/select/product1/index?params=${JSON.stringify({
|
|
|
+ "id": "2026031312441901",
|
|
|
+ "content": {
|
|
|
+ "pageNumber": 1,
|
|
|
+ "pageSize": 20,
|
|
|
+ "where": {
|
|
|
+ "tablefilter": {
|
|
|
+ "itemname": null,
|
|
|
+ "itemno": null,
|
|
|
+ "model": null,
|
|
|
+ "guid_price": null,
|
|
|
+ "guid_price_cus": null,
|
|
|
+ "packageqty": null
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })}&butText=添加商品`
|
|
|
+ });
|
|
|
+ // 设置全局回调函数
|
|
|
+ getApp().globalData.handleSelect = this.handleSelect.bind(this);
|
|
|
+},
|
|
|
+ // 处理选择商品回调
|
|
|
+ handleSelect(detail) {
|
|
|
+ if (detail && detail.list) {
|
|
|
+ const existingItemIds = this.data.productList.map(item => item.sys_enterprise_itemid);
|
|
|
+
|
|
|
+ // 过滤掉已经存在的商品
|
|
|
+ const newProducts = detail.list.filter(item => !existingItemIds.includes(item.sys_enterprise_itemid)).map(item => {
|
|
|
+ // 处理价格字段,去除格式化符号
|
|
|
+ const priceStr = item.guid_price_cus || item.price || "0";
|
|
|
+ const price = parseFloat(priceStr.toString().replace(/[¥,]/g, '')) || 0;
|
|
|
+ const qty = parseInt(item.qty) || 1;
|
|
|
+
|
|
|
+ return {
|
|
|
+ itemid: item.itemid,
|
|
|
+ sys_enterprise_itemid: item.sys_enterprise_itemid,
|
|
|
+ itemname: item.itemname,
|
|
|
+ itemno: item.itemno,
|
|
|
+ model: item.model,
|
|
|
+ originalPrice: price,
|
|
|
+ price: price,
|
|
|
+ discount: 1,
|
|
|
+ qty: qty,
|
|
|
+ amount: parseFloat((price * qty * 1).toFixed(2)),
|
|
|
+ remarks: item.remarks || ''
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+ if (newProducts.length > 0) {
|
|
|
+ this.setData({
|
|
|
+ productList: [...this.data.productList, ...newProducts]
|
|
|
+ });
|
|
|
+
|
|
|
+ // 更新订单金额
|
|
|
+ this.updateOrderAmount();
|
|
|
+ } else {
|
|
|
+ wx.showToast({
|
|
|
+ title: '所选商品已在列表中',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回页面
|
|
|
+ wx.navigateBack();
|
|
|
+ }
|
|
|
},
|
|
|
- onChange(e) {
|
|
|
+ // 处理字段编辑
|
|
|
+ onFieldBlur(e) {
|
|
|
+ const index = e.currentTarget.dataset.index;
|
|
|
+ const field = e.currentTarget.dataset.field;
|
|
|
+ const value = e.detail.value;
|
|
|
+
|
|
|
+ const productList = [...this.data.productList];
|
|
|
+ const product = productList[index];
|
|
|
+
|
|
|
+ // 处理不同字段的编辑
|
|
|
+ switch (field) {
|
|
|
+ case 'price':
|
|
|
+ product.price = parseFloat(value) || 0;
|
|
|
+ // 重新计算金额,保留两位小数,折扣1=100%
|
|
|
+ product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
|
|
|
+ break;
|
|
|
+ case 'discount':
|
|
|
+ product.discount = parseFloat(value) || 1;
|
|
|
+ // 重新计算金额,保留两位小数,折扣1=100%
|
|
|
+ product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
|
|
|
+ break;
|
|
|
+ case 'qty':
|
|
|
+ let qty = parseInt(value) || 1;
|
|
|
+ // 数量不能为0
|
|
|
+ qty = Math.max(1, qty);
|
|
|
+ product.qty = qty;
|
|
|
+ // 重新计算金额,保留两位小数,折扣1=100%
|
|
|
+ product.amount = parseFloat((product.price * product.qty * product.discount).toFixed(2));
|
|
|
+ break;
|
|
|
+ case 'amount':
|
|
|
+ product.amount = parseFloat(value) || 0;
|
|
|
+ // 重新计算单价(假设数量和折扣不变),保留两位小数,折扣1=100%
|
|
|
+ if (product.qty > 0 && product.discount > 0) {
|
|
|
+ product.price = parseFloat((product.amount / (product.qty * product.discount)).toFixed(2));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 'remarks':
|
|
|
+ product.remarks = value;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
this.setData({
|
|
|
- showAll: e.detail
|
|
|
+ productList
|
|
|
});
|
|
|
+
|
|
|
+ // 更新订单金额
|
|
|
+ this.updateOrderAmount();
|
|
|
},
|
|
|
- closePage() {
|
|
|
- wx.navigateBack({
|
|
|
- delta: 1
|
|
|
+ // 删除商品
|
|
|
+ deleteProduct(e) {
|
|
|
+ wx.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '确定要删除这个商品吗?',
|
|
|
+ confirmText: '确定',
|
|
|
+ cancelText: '取消',
|
|
|
+ success: (res) => {
|
|
|
+ if (res.confirm) {
|
|
|
+ const index = e.currentTarget.dataset.index;
|
|
|
+ const productList = [...this.data.productList];
|
|
|
+ productList.splice(index, 1);
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ productList
|
|
|
+ });
|
|
|
+
|
|
|
+ // 更新订单金额
|
|
|
+ this.updateOrderAmount();
|
|
|
+ }
|
|
|
+ }
|
|
|
});
|
|
|
},
|
|
|
- // tab切换
|
|
|
- onTabChange(e) {
|
|
|
- this.setData({
|
|
|
- activeTab: e.detail
|
|
|
+ // 更新订单金额
|
|
|
+ updateOrderAmount() {
|
|
|
+ // 计算总金额,确保不是NaN,默认0
|
|
|
+ const totalAmount = parseFloat(this.data.productList.reduce((sum, item) => {
|
|
|
+ const amount = parseFloat(item.amount) || 0;
|
|
|
+ return sum + amount;
|
|
|
+ }, 0)) || 0;
|
|
|
+
|
|
|
+ // 计算总数量,所有商品数量的总计
|
|
|
+ const totalQty = this.data.productList.reduce((sum, item) => {
|
|
|
+ const qty = parseInt(item.qty) || 0;
|
|
|
+ return sum + qty;
|
|
|
+ }, 0);
|
|
|
+
|
|
|
+ const form = [...this.data.form];
|
|
|
+ form.forEach(item => {
|
|
|
+ if (item.valueName === 'amount') {
|
|
|
+ item.value = totalAmount.toString();
|
|
|
+ this.setData({ "content.amount": totalAmount });
|
|
|
+ }
|
|
|
+ if (item.valueName === 'orderCount') {
|
|
|
+ item.value = totalQty.toString();
|
|
|
+ this.setData({ "content.orderCount": totalQty });
|
|
|
+ }
|
|
|
});
|
|
|
+ this.setData({ form });
|
|
|
}
|
|
|
});
|