You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1322 lines
47 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

var app = getApp();
var utils = require('utils.js');
/**
*
*/
var cy_addItem = function (product) {
var tempSpec = []; // 规格
var makeList = []; // 做法
var ids = "";
var idsName = "";
var isContain = false;
var price=0;
var member
// 遍历商品规格
if (product.specList) {
for (var spec of product.specList) {
if (spec.checked || product.specList.length == 1) {
tempSpec.push(spec);
ids = spec.specId;
idsName = spec.specName;
break;
}
}
}
// 遍历商品的做法
if (product.makeList) {
for (var make of product.makeList) {
for (var detail of make.list) {
if (detail.checked) {
makeList.push(detail);
ids = ids + "|" + detail.makeId;
idsName = idsName + "、" + detail.showName;
}
}
}
}
//
for (var item of app.orderTicket.orderList) {
var tempIds = item.ids; // 规格id
if (tempIds == ids) {
item.count = item.count + 1;
isContain = true;
}
}
//
if (!isContain) {
product.ids = ids;
var tempProduct = JSON.parse(JSON.stringify(product));
tempProduct.ids = ids;
tempProduct.idsName = idsName;
tempProduct.showName = idsName;
tempProduct.isSuit = 1;
tempProduct.count = 1;
tempProduct.specList = tempSpec;
tempProduct.makeList = makeList;
app.orderTicket.orderList.push(tempProduct);
}
}
/**
*
*/
var cy_subItem = function (product) {
var tempIds = product.ids;
// 遍历商品规格
if (product.specList) {
for (var spec of product.specList) {
if (spec.checked || product.specList.length == 1) {
tempIds = spec.specId;
break;
}
}
}
// 遍历商品的做法
if (product.makeList) {
for (var make of product.makeList) {
for (var detail of make.list) {
if (detail.checked) {
tempIds = tempIds + "|" + detail.makeId;
}
}
}
}
for (var i = 0; i < app.orderTicket.orderList.length; i++) {
var item = app.orderTicket.orderList[i];
var ids = item.ids; // 规格id
if (tempIds == ids) {
item.count = item.count - 1;
if (item.count == 0) {
app.orderTicket.orderList.remove(i);
break;
}
}
}
}
/**
*
*/
var cy_getProductList = function (list) {
for (var product of list) {
product.sumCount = 0;
for (var item of app.orderTicket.orderList) {
if (item.isSuit == 3) {
continue;
}
if (item.productId == product.productId) {
product.sumCount = product.sumCount + item.count;
}
}
}
}
/**
* 多规格商品数量
*/
var cy_getProductCount = function (product) {
var ids = "";
for (var i = 0; i < product.specList.length; i++) {
var spec = product.specList[i];
if (spec.checked) {
ids = spec.specId;
}
}
for (var i = 0; i < product.makeList.length; i++) {
var makeListTemp = product.makeList[i];
for (var j = 0; j < makeListTemp.list.length; j++) {
var make = makeListTemp.list[j];
if (make.checked) {
ids = ids + "|" + make.makeId;
}
}
}
var productCount = 0;
for (var item of app.orderTicket.orderList) {
if (item.ids == ids) {
if (item.isSuit != 3) {
productCount = item.count;
}
}
}
return productCount;
}
/**
* 默认规格和做法ids
*/
var cy_getIds = function (product) {
var ids = "";
if (product.specList) { // 选择
for (var i = 0; i < product.specList.length; i++) {
var spec = product.specList[i];
spec.checked = false;
// if (i == 0) {
// spec.checked = true;
// }
if (spec.stock > 0) {
spec.checked = true;
}
if (spec.checked) {
ids = spec.specId;
break;
}
}
}
if (product.makeList) {
for (var make of product.makeList) {
for (var i = 0; i < make.list.length; i++) {
var detail = make.list[i];
if (detail.checked) {
ids = ids + "|" + detail.makeId
}
}
}
}
return ids;
}
/**
* 默认规格做法名称idsName
*/
var cy_getIdsName = function (product) {
var idsName = "";
if (product.specList) {
// idsName = product.specList[0].specName;
for (var i = 0; i < product.specList.length; i++) {
var spec = product.specList[i];
spec.checked = false;
// if (i == 0) {
// spec.checked = true;
// }
if (spec.stock > 0) {
spec.checked = true;
idsName = spec.specName;
break;
}
}
}
if (product.makeList) {
for (var make of product.makeList) {
for (var i = 0; i < make.list.length; i++) {
var detail = make.list[i];
var showName = detail.makeName + (detail.addPrice == 0 ? "" : ("+" + detail.addPrice + "元"));
detail.showName = showName;
}
}
}
return idsName;
}
/**
* 商品销售订单 (小程序订单的组合)
*/
var getOrder = function () {
var date = app.utils.getFormatTime(new Date(), 1);
var order = {};
order.id = getDaySeo(1); // 主单id
order.memberId = ""; // 会员id
order.openId = wx.getStorageSync("openId"); // 会员openId
order.sourceType = 0; // 订单来源
order.sourceType = ""; // 用户头像
order.memberName = ""; // 会员姓名
order.mobile = app.orderTicket.phone; // 手机号
order.storeId = app.orderTicket.storeId; // 门店id
order.storeNo = app.orderTicket.storeNo; // 门店编号
order.storeName = app.orderTicket.storeName; // 门店名称
order.saleDate = date; // 销售时间
order.tableNo = app.orderTicket.tableNo ? app.orderTicket.tableNo : ""; // 餐桌号
order.tableName = app.orderTicket.tableName == "选择餐桌" ? "" : app.orderTicket.tableName; // 桌台名称
order.people = app.orderTicket.people; // 人数
order.busMode = wx.getStorageSync('mode') || app.orderTicket.busMode; // 营业模式(0堂食1外带2预定3外卖)
order.reserveTime = app.orderTicket.reserveTime; // 预定时间
order.deliverFee = 0; // 配送费 默认为零下面根据营业模式busMode = 3 外卖)进行计算
order.packageFee = 0; // 打包费 默认为零下面根据营业模式busMode = 1 或者 busMode = 3进行计算
order.amount = 0; // 消费金额 = 商品的总金额 + 做法的总金额
order.discount = 1; // 优惠率
order.discountTotal = 0; // 优惠总金额
order.receivable = 0; // 应收金额 = 商品消费金额 - 优惠金额 (不含餐盒费和配送费)
order.maling = 0; // 抹零金额
order.paid = 0; // 实收金额 = 应收金额 (不含餐盒费和配送费)
order.noOrg = ""; // 原单号
order.backCause = ""; // 退单原因
order.weeker = app.utils.getWeeker(); // 周几(星期二)
order.ext2 = app.orderTicket.ext2; // 订单备注
// order.ticket_deliver ={} ; //配送信息
order.ticket_info = []; // 订单优惠明细 同一类商品优惠的合计
order.order_product = []; // 订单产品明细
if (order.busMode == 0) { // 堂食模式
order.packageFee = 0; // 餐盒费
order.deliverFee = 0; // 配送费
}
if (order.busMode == 1) { // 打包外带
order.deliverFee = 0;
if (app.orderTicket.boxFee) {
if (app.globalData.userIsFee == 1 || app.globalData.takeOutBoxFeeFlag == 1) {
order.packageFee = app.orderTicket.boxFee; // 全部的餐盒费
}
}
}
if (order.busMode == 2) { // 预订
}
if (order.busMode == 3) { // 外卖
order.deliverFee = app.orderTicket.deliverFee; // 配送费根据门店后台配送获取
order.packageFee = app.orderTicket.boxFee; // 餐盒非
order.ticket_deliver = app.orderTicket.ticket_deliver; // 配送费
order.ticket_deliver.id = order.id;
}
var tempInfo = null;
var memberTempInfo = [];
var last = false;
var couponTotal = 0;
var memberPriceTemInfoItem = {};
var discountMemberMoney = 0;
var giftProductTempInfo = "";
var promotionDiscountTemInfo = [];
for (var i = 0; i < app.orderTicket.orderList.length; i++) {
if (i == app.orderTicket.orderList.length - 1) {
last = true;
}
var product = app.orderTicket.orderList[i];
var tempProduct = getOrderItem(product, last, app.orderTicket.isMember);
tempProduct.parentId = order.id; //父记录id
tempProduct.saleDate = date; //销售时间
tempProduct.lineNo = i; // 行号
order.order_product.push(tempProduct);
console.log("----------------------1", order);
if (app.orderTicket.coupon) { // 使用优惠券
// 电子代金券 = 80, 电子折扣券 = 81, 电子兑换券 = 82, 电子商品券 = 83, 会员价优惠 = 8
//兑换券不在这里处理
if (app.orderTicket.coupon.cardType != 'GIFT') {
if (tempProduct.isSuit != 3) { // 套菜明细不参与优惠券的优惠券分摊
useCoupon(tempProduct, last, couponTotal);
couponTotal += parseFloat(tempProduct.discountTotal); // 优惠的合计金额
console.log("couponTotal" + couponTotal);
// 统计优惠券的优惠
for (var product_info of tempProduct.product_info) {
for (var ticket_info of order.ticket_info) {
if (ticket_info.type == product_info.type) {
tempInfo = ticket_info;
}
}
if (tempInfo == null) {
tempInfo = {};
tempInfo.id = getDaySeo(3);
tempInfo.type = product_info.type;
tempInfo.info = product_info.info;
tempInfo.discountMoney = product_info.discountMoney;
} else {
tempInfo.discountMoney = couponTotal;
// if (tempInfo.type >= 80) {
tempInfo.info = "优惠券优惠" + couponTotal + "元";
// }
}
}
}
}
}
/**
* 会员价优惠(折扣券禁止使用有会员优惠)
*/
var discountFlag = true;
if (app.orderTicket.coupon) {
if (app.orderTicket.coupon.cardType == "DISCOUNT") {
discountFlag = false;
}
}
if (discountFlag && tempProduct.isSuit != 3) {
if (app.orderTicket.isMember == 1) {
//判断促销是否支持折上折
if (tempProduct.isRepeatDiscount == 1 | !tempProduct.discountRule) {
if (app.globalData.memberPriceEnabled == 1 && tempProduct.giftProduct != "1") {
var isMemberPrice = 0;
if(app.globalData.cardInfo){
isMemberPrice = app.globalData.cardInfo.memberPriceFlag;
}
if(isMemberPrice == 1){
useMemberPrice(tempProduct);
}
var productInfoLength = tempProduct.product_info.length;
for (var product_info of tempProduct.product_info) {
if (product_info.type == 3) {
discountMemberMoney += parseFloat(product_info.discountMoney);
memberPriceTemInfoItem.id = getDaySeo(3);
memberPriceTemInfoItem.type = product_info.type;
}
}
}
}
}
}
// 计算套餐的做法加价
// if (tempProduct.isSuit == 3 && tempProduct.giftProduct != '1') {
// order.amount += parseFloat(tempProduct.addPriceTotal);
// order.paid += parseFloat(tempProduct.addPriceTotal);
// order.receivable += parseFloat(tempProduct.addPriceTotal);
// }
// if (tempProduct.isSuit ==2 && tempProduct.giftProduct != '1') {
// order.amount += parseFloat(tempProduct.amount)-parseFloat(tempProduct.price);
// order.paid += parseFloat(tempProduct.amount);
// order.receivable += parseFloat(tempProduct.amount);
// }
//兑换券
if (tempProduct.giftProduct == '1') {
giftProductTempInfo = giftCoupon(order, tempProduct, giftProductTempInfo);
}
//计算餐饮促销活动
if (tempProduct.isSuit != 3 && tempProduct.isSuit != 2) {
var tempProductInfo = promotionCompute(tempProduct);
if (tempProductInfo) {
if (promotionDiscountTemInfo.length > 0) {
var promotionSize = promotionDiscountTemInfo.length;
for (var i; i < promotionSize; i++) {
var item = promotionDiscountTemInfo[i];
if (item.type == tempProductInfo.type) {
item.discountMoney = parseFloat(item.discountMoney) + parseFloat(tempProductInfo.discountMoney);
if (item.type == 1) {
item.info = "促销折扣优惠 " + item.discountMoney + " 元 ";
}
if (item.type == 9) {
item.info = "促销立减优惠 " + item.discountMoney + " 元 ";
}
} else {
promotionDiscountTemInfo.push(tempProductInfo);
}
}
} else {
promotionDiscountTemInfo.push(tempProductInfo);
}
}
}
if (tempProduct.isSuit != 3 && tempProduct.giftProduct != '1') {
order.discountTotal += parseFloat(tempProduct.discountTotal);
order.amount += parseFloat(tempProduct.amountTotal);
order.paid += parseFloat(tempProduct.receivableTotal);
order.receivable += parseFloat(tempProduct.receivableTotal);
//重新定义商品的折后价
tempProduct.discountPrice = (parseFloat(tempProduct.price) - (parseFloat(tempProduct.discountTotal) / tempProduct.count)).toFixed(2);
//重新定义商品的优惠率
tempProduct.discount = tempProduct.discountTotal / tempProduct.amountTotal;
}
}
// 订单优惠信息
if (tempInfo) {
order.ticket_info.push(tempInfo);
// order.discountTotal=parseFloat(order.discountTotal) +parseFloat(tempInfo.discountMoney);
}
//促销优惠信息
if (promotionDiscountTemInfo.length > 0) {
for (var item of promotionDiscountTemInfo) {
order.ticket_info.push(item);
}
}
if (giftProductTempInfo) {
order.ticket_info.push(giftProductTempInfo);
}
if (discountMemberMoney > 0) {
// order.discountTotal=parseFloat(order.discountTotal) + parseFloat(discountMemberMoney);
memberPriceTemInfoItem.discountMoney = discountMemberMoney;
memberPriceTemInfoItem.info = "会员优惠" + discountMemberMoney + "元";
order.ticket_info.push(memberPriceTemInfoItem);
}
if (order.receivable + order.discountTotal != 0) {
order.discount = (order.discountTotal / (order.receivable + order.discountTotal) * 100).toFixed(2);
}
order.discountTotal = order.discountTotal.toFixed(2);
order.amount = parseFloat(order.amount.toFixed(2))
order.paid = parseFloat(order.paid.toFixed(2))
order.receivable = order.receivable.toFixed(2)
if(wx.getStorageSync("user")){
order.memberName =wx.getStorageSync("user").nickName;
}
if(wx.getStorageSync("cardInfo")){
order.memberId=wx.getStorageSync("cardInfo").memberId;
order.mobile=wx.getStorageSync("cardInfo").mobile;
order.memberName =wx.getStorageSync("cardInfo").name;
}
return order;
}
/**
* 商品销售订单 (小程序订单的组合)
*/
var getMemberCreateOrder = function () {
var date = app.utils.getFormatTime(new Date(), 1);
var order = {};
order.id = getDaySeo(1); // 主单id
order.memberId = ""; // 会员id
order.openId = wx.getStorageSync("openId"); // 会员openId
order.sourceType = 0; // 订单来源
order.sourceType = ""; // 用户头像
order.memberName = ""; // 会员姓名
order.mobile = app.orderTicket.phone; // 手机号
order.storeId = app.orderTicket.storeId; // 门店id
order.storeNo = app.orderTicket.storeNo; // 门店编号
order.storeName = app.orderTicket.storeName; // 门店名称
order.saleDate = date; // 销售时间
order.tableNo = app.orderTicket.tableNo ? app.orderTicket.tableNo : ""; // 餐桌号
order.tableName = app.orderTicket.tableName == "选择餐桌" ? "" : app.orderTicket.tableName; // 桌台名称
order.people = app.orderTicket.people; // 人数
order.busMode = app.orderTicket.busMode // 营业模式(0堂食1外带2预定3外卖)
order.reserveTime = app.orderTicket.reserveTime; // 预定时间
order.deliverFee = 0; // 配送费 默认为零下面根据营业模式busMode = 3 外卖)进行计算
order.packageFee = 0; // 打包费 默认为零下面根据营业模式busMode = 1 或者 busMode = 3进行计算
order.amount = 0; // 消费金额 = 商品的总金额 + 做法的总金额
order.discount = 1; // 优惠率
order.discountTotal = 0; // 优惠总金额
order.receivable = 0; // 应收金额 = 商品消费金额 - 优惠金额 (不含餐盒费和配送费)
order.maling = 0; // 抹零金额
order.paid = 0; // 实收金额 = 应收金额 (不含餐盒费和配送费)
order.noOrg = ""; // 原单号
order.backCause = ""; // 退单原因
order.weeker = app.utils.getWeeker(); // 周几(星期二)
order.ext2 = app.orderTicket.ext2; // 订单备注
// order.ticket_deliver ={} ; //配送信息
order.ticket_info = []; // 订单优惠明细 同一类商品优惠的合计
order.order_product = []; // 订单产品明细
if (order.busMode == 0) { // 堂食模式
order.packageFee = 0; // 餐盒费
order.deliverFee = 0; // 配送费
}
if (order.busMode == 1) { // 打包外带
order.deliverFee = 0;
if (app.orderTicket.boxFee) {
if (app.globalData.userIsFee == 1 || app.globalData.takeOutBoxFeeFlag == 1) {
order.packageFee = app.orderTicket.boxFee; // 全部的餐盒费
}
}
}
if (order.busMode == 3) { // 外卖
order.deliverFee = app.orderTicket.deliverFee; // 配送费根据门店后台配送获取
order.packageFee = app.orderTicket.boxFee; // 餐盒非
order.ticket_deliver = app.orderTicket.ticket_deliver; // 配送费
order.ticket_deliver.id = order.id;
}
var tempInfo = null;
var memberTempInfo = [];
var last = false;
var couponTotal = 0;
var memberPriceTemInfoItem = {};
var discountMemberMoney = 0;
var giftProductTempInfo = "";
var promotionDiscountTemInfo = [];
for (var i = 0; i < app.orderTicket.orderList.length; i++) {
if (i == app.orderTicket.orderList.length - 1) {
last = true;
}
var product = app.orderTicket.orderList[i];
var tempProduct = getOrderItem(product, last, app.orderTicket.isMember);
tempProduct.parentId = order.id; //父记录id
tempProduct.saleDate = date; //销售时间
tempProduct.lineNo = i; // 行号
order.order_product.push(tempProduct);
if (app.orderTicket.coupon) { // 使用优惠券
// 电子代金券 = 80, 电子折扣券 = 81, 电子兑换券 = 82, 电子商品券 = 83, 会员价优惠 = 8
//兑换券不在这里处理
if (app.orderTicket.coupon.cardType != 'GIFT') {
if (tempProduct.isSuit != 3) { // 套菜明细不参与优惠券的优惠券分摊
useCoupon(tempProduct, last, couponTotal);
couponTotal += parseFloat(tempProduct.discountTotal); // 优惠的合计金额
// 统计优惠券的优惠
for (var product_info of tempProduct.product_info) {
for (var ticket_info of order.ticket_info) {
if (ticket_info.type == product_info.type) {
tempInfo = ticket_info;
}
}
if (tempInfo == null) {
tempInfo = {};
tempInfo.id = getDaySeo(3);
tempInfo.type = product_info.type;
tempInfo.info = product_info.info;
tempInfo.discountMoney = product_info.discountMoney;
} else {
tempInfo.discountMoney = couponTotal;
tempInfo.info = "优惠券优惠" + couponTotal + "元";
}
}
}
}
}
/**
* 会员价优惠(折扣券禁止使用有会员优惠)
*/
var discountFlag = true;
if (app.orderTicket.coupon) {
if (app.orderTicket.coupon.cardType == "DISCOUNT") {
discountFlag = false;
}
}
if (discountFlag && tempProduct.isSuit != 3) {
//判断促销是否支持折上折
if (tempProduct.isRepeatDiscount == 1 | !tempProduct.discountRule) {
/**
* 根据会员信息判断是否使用会员价
*/
console.log("会员卡信息", app.globalData.cardInfo);
var isMemberPrice = 0;
if(app.globalData.cardInfo){
isMemberPrice = app.globalData.cardInfo.memberPriceFlag;
}
if(isMemberPrice == 1){
useMemberPrice(tempProduct);
}
for (var product_info of tempProduct.product_info) {
if (product_info.type == 3) {
discountMemberMoney += parseFloat(product_info.discountMoney);
memberPriceTemInfoItem.id = getDaySeo(3);
memberPriceTemInfoItem.type = product_info.type;
}
}
}
}
//兑换券
if (tempProduct.giftProduct == '1') {
giftProductTempInfo = giftCoupon(order, tempProduct, giftProductTempInfo);
}
//计算餐饮促销活动
if (tempProduct.isSuit != 3 && tempProduct.isSuit != 2) {
var tempProductInfo = promotionCompute(tempProduct);
if (tempProductInfo) {
if (promotionDiscountTemInfo.length > 0) {
var promotionSize = promotionDiscountTemInfo.length;
for (var i; i < promotionSize; i++) {
var item = promotionDiscountTemInfo[i];
if (item.type == tempProductInfo.type) {
item.discountMoney = parseFloat(item.discountMoney) + parseFloat(tempProductInfo.discountMoney);
if (item.type == 1) {
item.info = "促销折扣优惠 " + item.discountMoney + " 元 ";
}
if (item.type == 9) {
item.info = "促销立减优惠 " + item.discountMoney + " 元 ";
}
} else {
promotionDiscountTemInfo.push(tempProductInfo);
}
}
} else {
promotionDiscountTemInfo.push(tempProductInfo);
}
}
}
if (tempProduct.isSuit != 3 && tempProduct.giftProduct != '1') {
order.discountTotal += parseFloat(tempProduct.discountTotal);
order.amount += parseFloat(tempProduct.amountTotal);
order.paid += parseFloat(tempProduct.receivableTotal);
order.receivable += parseFloat(tempProduct.receivableTotal);
//重新定义商品的折后价
tempProduct.discountPrice = (parseFloat(tempProduct.price) - (parseFloat(tempProduct.discountTotal) / tempProduct.count)).toFixed(2);
//重新定义商品的优惠率
tempProduct.discount = tempProduct.discountTotal / tempProduct.amountTotal;
}
}
// 订单优惠信息
if (tempInfo) {
order.ticket_info.push(tempInfo);
}
//促销优惠信息
if (promotionDiscountTemInfo.length > 0) {
for (var item of promotionDiscountTemInfo) {
order.ticket_info.push(item);
}
}
if (giftProductTempInfo) {
order.ticket_info.push(giftProductTempInfo);
}
console.log(discountMemberMoney);
if (discountMemberMoney > 0) {
memberPriceTemInfoItem.discountMoney = discountMemberMoney;
memberPriceTemInfoItem.info = "会员优惠" + discountMemberMoney + "元";
order.ticket_info.push(memberPriceTemInfoItem);
}
if (order.receivable + order.discountTotal != 0) {
order.discount = (order.discountTotal / (order.receivable + order.discountTotal) * 100).toFixed(2);
}
order.discountTotal = order.discountTotal.toFixed(2);
order.amount = parseFloat(order.amount.toFixed(2))
order.paid = parseFloat(order.paid.toFixed(2))
order.receivable = order.receivable.toFixed(2)
if(wx.getStorageSync("user")){
order.memberName =wx.getStorageSync("user").nickName;
}
if(wx.getStorageSync("cardInfo")){
order.memberId=wx.getStorageSync("cardInfo").memberId;
order.mobile=wx.getStorageSync("cardInfo").mobile;
order.memberName =wx.getStorageSync("cardInfo").name;
}
return order;
}
//计算餐饮促销活动
function promotionCompute(tempProduct) {
//此商品是促销商品
var tempProductInfo = "";
if (tempProduct.promotionPrice > 0) {
//促销价优惠金额
var discountPromotion = parseFloat(((parseFloat(tempProduct.price) - parseFloat(tempProduct.promotionPrice)) * tempProduct.count).toFixed(2));
//设置折后价
tempProduct.discountPrice = parseFloat((parseFloat(tempProduct.discountPrice) - parseFloat(discountPromotion)).toFixed(2));
//设置总优惠
tempProduct.discountTotal = parseFloat((parseFloat(tempProduct.discountTotal) + discountPromotion).toFixed(2));
if (tempProduct.discountTotal > tempProduct.amountTotal) {
tempProduct.discountTotal = tempProduct.amountTotal;
}
//如果优惠券优惠会员价优惠 等优惠后的总实收 小于促销优惠让促销优惠等于现在的实收
// var promotionDiscount = ((tempProduct.price - tempProduct.promotionPrice) * tempProduct.count).toFixed(2);
if (discountPromotion > tempProduct.receivableTotal) {
discountPromotion = tempProduct.receivableTotal;
}
tempProduct.receivableTotal = parseFloat((tempProduct.receivableTotal - discountPromotion).toFixed(2));
tempProduct.receivable =parseFloat(( tempProduct.receivable - discountPromotion).toFixed(2));
tempProductInfo = {};
var discountRule = JSON.parse(tempProduct.discountRule);
tempProductInfo.id = getDaySeo(3);
tempProductInfo.orderItemId = tempProduct.id;
tempProductInfo.discountMoney = discountPromotion;
if (discountRule.discountType == 0) {
tempProductInfo.type = 1;
tempProductInfo.info = "促销折扣优惠 " + discountPromotion + " 元 ";
tempProduct.product_info.push(tempProductInfo);
} else if (discountRule.discountType == 1) {
tempProductInfo.type = 9;
tempProductInfo.info = "促销立减优惠 " + discountPromotion + " 元 ";
tempProduct.product_info.push(tempProductInfo);
}
}
// var receivable = (parseFloat(tempProduct.receivable) - parseFloat(tempProduct.discountTotal));
// if (!receivable > 0) {
// tempProduct.receivable = 0;
// }
// var receivableTotal = (parseFloat(tempProduct.receivableTotal) - parseFloat(tempProduct.discountTotal));
// if (!receivableTotal > 0) {
// tempProduct.receivableTotal = 0;
// }
return tempProductInfo;
}
//计算商品促销价
function promotionComputePrice(product) {
var promotionDiscount = 0; //优惠金额
if (product.discountRule) {
var discountRule = JSON.parse(product.discountRule);
var discount = discountRule.discount;
if (discount > 0) {
if (discountRule.discountType == 0) {
promotionDiscount = product.price * (100 - discount) * product.count / 100;
product.promotionPrice = (product.price - promotionDiscount).toFixed(2);
}
if (discountRule.discountType == 1) {
promotionDiscount = discount * product.count;
product.promotionPrice = product.price - discount;
}
}
}
return promotionDiscount;
}
// 使用优惠券
var useCoupon = function (tempProduct, isLast, couponTotal) {
var coupon = app.orderTicket.coupon;
var cardType = coupon.cardType;
if (cardType == "CASH") { // 代金券
cashCoupon(coupon, tempProduct, isLast, couponTotal);
}
if (cardType == "DISCOUNT") { // 折扣券
discountCoupon(coupon, tempProduct);
}
}
/**
* 代金券 (做法不参与优惠计算)
* 1.代金券优惠计算方法:进行整单分摊计算, 单个商品的总价占整单总计的比例乘以优惠金额,计算出来本单的优惠金额,
* 2.最后一个商品的优惠等于优惠总金额减去前面所有商品的和
* 3.coupon 优惠券信息
* 4.tempProduct 单品信息
*/
var cashCoupon = function (coupon, tempProduct, isLast, couponTotal) {
console.log("==================", tempProduct);
//重新找到订单的金额不包括餐盒费配送 主要用于促销之后分摊不干净问题
var receivable = 0;
for (var item of app.orderTicket.orderList) {
if (item.isSuit != 3) {
if (item.promotionTotalPrice && item.promotionTotalPrice > 0) { //在上个页面计算的促销总价包括加价等
receivable += item.promotionTotalPrice;
} else {
receivable += item.amount;
}
}
}
// 计算单品所占的比例,
// var scale = parseFloat(tempProduct.receivableTotal) / parseFloat(app.orderTicket.receivable);
var scale = parseFloat(tempProduct.receivableTotal) / parseFloat(receivable);
// 优惠金额
var discountMoney = parseFloat(coupon.amount) * scale;
// 商品优惠后价格 前面封装
tempProduct.discountPrice = (tempProduct.discountPrice - (discountMoney / tempProduct.count)).toFixed(2);
// 优惠率
tempProduct.discount = 0;
if (tempProduct.receivableTotal > 0) {
tempProduct.discount = (discountMoney / tempProduct.receivableTotal).toFixed(2);
}
// 优惠券
var tempProductInfo = {};
tempProductInfo.id = getDaySeo(3); //
tempProductInfo.orderItemId = tempProduct.id;
tempProductInfo.type = 2;
tempProductInfo.discountMoney = discountMoney.toFixed(2);
if (isLast) { //
tempProductInfo.discountMoney = (parseFloat(coupon.amount) - couponTotal).toFixed(2);
// 优惠总金额
tempProduct.discountTotal = (parseFloat(tempProduct.discountTotal) + parseFloat(tempProductInfo.discountMoney)).toFixed(2);
tempProduct.receivableTotal = parseFloat((tempProduct.receivableTotal - tempProductInfo.discountMoney).toFixed(2));
tempProduct.receivable = parseFloat((tempProduct.receivable - tempProductInfo.discountMoney).toFixed(2));
} else {
couponTotal += discountMoney;
// 优惠总金额
tempProduct.discountTotal = (parseFloat(tempProduct.discountTotal) + parseFloat(discountMoney)).toFixed(2);
tempProduct.receivableTotal = parseFloat((tempProduct.receivableTotal - tempProduct.discountTotal).toFixed(2));
tempProduct.receivable = parseFloat((tempProduct.receivable - tempProduct.discountTotal).toFixed(2));
}
tempProductInfo.info = "优惠券优惠" + tempProductInfo.discountMoney + "元";
tempProduct.product_info.push(tempProductInfo);
}
/**
* 折扣券 (做法不参与优惠券)
*
*/
var discountCoupon = function (coupon, tempProduct) {
console.log(tempProduct);
// 计算优惠金额
var discountMoney = (1 - parseFloat(coupon.amount / 10)) * parseFloat(tempProduct.receivableTotal);
console.error("-------------------discountMoney", discountMoney)
coupon.reduceCost = parseInt(discountMoney.toFixed(2) * 100);
// 单品优惠后的价格
tempProduct.discountPrice = (tempProduct.discountPrice - (discountMoney / tempProduct.count)).toFixed(2);
// 优惠的总金额
tempProduct.discountTotal = discountMoney.toFixed(2);
tempProduct.discount = 0;
if (tempProduct.amountTotal > 0) {
tempProduct.discount = (discountMoney / tempProduct.amountTotal).toFixed(2);
}
tempProduct.receivableTotal = parseFloat((tempProduct.amountTotal - discountMoney).toFixed(2));
tempProduct.receivable = parseFloat((tempProduct.receivable - discountMoney).toFixed(2));
var tempProductInfo = {};
tempProductInfo.id = getDaySeo(3); //
tempProductInfo.orderItemId = tempProduct.id;
tempProductInfo.type = 2;
tempProductInfo.discountMoney = discountMoney.toFixed(2);
tempProductInfo.info = "优惠券优惠" + tempProductInfo.discountMoney + "元";
console.log("---------------", tempProductInfo);
tempProduct.product_info.push(tempProductInfo);
}
/**
* 兑换券
*/
var giftCoupon = function (order, tempProduct, giftProductTempInfo) {
order.amount += parseFloat(tempProduct.amountTotal);
var giftProductTempInfoItem = {};
giftProductTempInfoItem.id = getDaySeo(3);
giftProductTempInfoItem.type = 7;
giftProductTempInfoItem.orderItemId = tempProduct.id;
var discountAmount = tempProduct.price * tempProduct.count;
giftProductTempInfoItem.info = "商品兑换券优惠" + discountAmount + "元";
giftProductTempInfoItem.discountMoney = discountAmount;
giftProductTempInfoItem.count = tempProduct.count;
order.discountTotal = parseFloat(order.discountTotal) + parseFloat(discountAmount);
tempProduct.product_info.push(giftProductTempInfoItem);
if (giftProductTempInfo) {
giftProductTempInfo.discountMoney = parseFloat(giftProductTempInfo.discountMoney) + parseFloat(discountAmount);
giftProductTempInfo.info = "商品兑换券总优惠 " + giftProductTempInfo.discountMoney + " 元 ";
} else {
if (discountAmount >= 0) {
giftProductTempInfo = {};
giftProductTempInfo.id = getDaySeo(3);
giftProductTempInfo.type = 7;
giftProductTempInfo.info = "商品兑换券总优惠 " + discountAmount + " 元 ";
giftProductTempInfo.discountMoney = discountAmount;
}
}
return giftProductTempInfo;
}
// 计算会员价
var useMemberPrice = function (tempProduct) {
var tempProductInfo = {};
// 会员优惠价格
var disPrice = 0;
//判断是否是套餐
if (tempProduct.isSuit == 2) {
console.error(" tempProduct ",tempProduct)
if(tempProduct.addPriceTotalConsumer){
disPrice = parseFloat(tempProduct.addPriceOldPrice) - parseFloat(tempProduct.memberPrice)-parseFloat(tempProduct.addPriceTotalConsumer);
}else{
disPrice = parseFloat(tempProduct.addPriceOldPrice) - parseFloat(tempProduct.memberPrice);
}
} else {
disPrice = parseFloat(tempProduct.price) - parseFloat(tempProduct.memberPrice);
}
if (disPrice > 0) {
// 会员优惠后的价格
// if (tempProduct.promotionPrice > 0) {
// tempProduct.discountPrice = tempProduct.promotionPrice - disPrice;
// } else tempProduct.discountPrice = tempProduct.price - disPrice;
if ((tempProduct.discountPrice - disPrice) < 0) {
disPrice = tempProduct.discountPrice;
}
tempProduct.discountPrice = tempProduct.discountPrice - disPrice;
// 计算优惠金额
var discountMoney = (disPrice * tempProduct.count).toFixed(2);
//计算前优惠总金额
var oldDiscountTotal = tempProduct.discountTotal;
// 优惠总金额
tempProduct.discountTotal = parseFloat(tempProduct.discountTotal) + parseFloat(discountMoney);
//商品总价
var amountTotal = tempProduct.amountTotal;
if (tempProduct.discountTotal >= amountTotal) {
var newDiscountTotal = tempProduct.discountTotal;
tempProduct.discountTotal = amountTotal;
tempProduct.discount = 0;
// 优惠率
if (tempProduct.receivableTotal > 0) {
tempProduct.discount = (discountMoney / tempProduct.receivableTotal).toFixed(2);
}
if (oldDiscountTotal != amountTotal) {
discountMoney = parseFloat(newDiscountTotal) - parseFloat(oldDiscountTotal);
if (discountMoney > 0) {
tempProductInfo.id = getDaySeo(3);
tempProductInfo.type = 3;
tempProductInfo.orderItemId = tempProduct.id;
tempProductInfo.discountMoney = discountMoney;
tempProductInfo.info = "会员价优惠 " + discountMoney + " 元 ";
tempProduct.product_info.push(tempProductInfo);
}
// tempProduct.discount = (discountMoney / tempProduct.receivableTotal).toFixed(2);
} else {
tempProduct.discount = 0;
}
tempProduct.receivable = 0;
tempProduct.receivableTotal = 0;
} else {
tempProduct.discount = 0;
// 优惠率
if (tempProduct.receivableTotal > 0) {
tempProduct.discount = (discountMoney / tempProduct.receivableTotal).toFixed(2);
}
//
tempProduct.receivableTotal = parseFloat((tempProduct.receivableTotal - discountMoney).toFixed(2));
tempProduct.receivable = parseFloat((tempProduct.receivable - discountMoney).toFixed(2));
if (discountMoney > 0) {
tempProductInfo.id = getDaySeo(3);
tempProductInfo.type = 3;
tempProductInfo.orderItemId = tempProduct.id;
tempProductInfo.discountMoney = discountMoney;
tempProductInfo.info = "会员价优惠 " + discountMoney + " 元 ";
tempProduct.product_info.push(tempProductInfo);
}
}
}
}
// 订单明细信息
/**
* product 商品信息
* last 是否是最后一个
* isMember 是否是会员
*/
var getOrderItem = function (product, last, isMember) {
var productSpec = product.specList[0];
var item = {};
item.id = getDaySeo(2);
item.giftProduct = product.giftProduct;
// item.parentId = ""; // 父记录ID
item.productId = product.productId; // 菜品id
item.productNo = product.productNo; // 菜品编号
item.productName = product.productName; //菜品名称
item.productUnitId = product.productUnitId; // 菜品单位
item.productUnitName = product.productUnitName; // 菜品名称
item.productImageUrl = product.linkUrl; // 菜品图片URL
item.typePath = product.typePath; // 分类券路径
item.seriesId = product.seriesId; // 顶级分类ID
item.seriesName = product.seriesName; // 顶级分类名称
item.typeId = product.typeId; // 类别ID
item.typeName = product.typeName; // 类别名
item.specId = productSpec.specId; // 规格ID
item.specName = productSpec.specName; // 规格名称
item.count = product.count; // 数量
item.rcount = 0; // 退菜数量
item.price = 0; // 销售价格
item.discountPrice = 0; // 折后价格
item.priceOrg = 0; // 菜品原价
item.isSuit = product.isSuit; // 是否套菜(1普通菜 2 主菜 3 明细菜)
item.suitId = ""; // 套菜ID
// item.saleDate = ""; // 销售时间
item.amount = 0; // 消费金额
item.discountTotal = 0; // 优惠额
item.discount = 0; // 优惠率
item.receivable = 0; //应收金额
item.addPriceTotal = 0; //加价金额
item.discountAddTotal = 0; // 加价优惠金额
item.amountAddTotal = 0; // 加价消费金额
item.amountTotal = 0; // 消费总额(消费金额 + 加价总额)
item.receivableTotal = 0; // 应收总额(应收金额 + 加价应收总额)
item.packageFee = 0; // 餐盒费
// item.lineNo = 0; // 行号
item.product_info = []; // 商品优惠明细
item.product_make = []; // 商品做法明细
item.memberPrice = productSpec.memberPrice;
item.isRepeatDiscount = product.isRepeatDiscount; //
item.addPriceTotalConsumer=product.addPriceTotalConsumer //套餐的总加价
//如果是套餐的商品给 设置价格方便计算会员价
if(item.isSuit==2){
item.addPriceOldPrice=product.addPriceOldPrice; //套餐价
}
if (product.giftProduct == '1') { //兑换的商品
item.price = product.price; // 销售价格
item.priceOrg = product.price; // 菜品原价
item.discount = product.discount; // 优惠率
item.amountTotal = product.amountTotal; // 消费总额(消费金额 + 加价总额)
}
if (app.orderTicket.busMode == 0) { // 堂食
item.packageFee = 0;
}
if (app.orderTicket.busMode == 1 && app.globalData.userIsFee == 1) { // 外带
item.packageFee = product.count * product.boxPrice;
}
if (app.orderTicket.busMode == 2) { // 预订
}
if (app.orderTicket.busMode == 3) { // 外卖和配送费
item.packageFee = product.count * product.boxPrice;
}
if (item.isSuit == 3) { // 套餐明细
item.suitId=product.suitId;
item.suitProductId=product.suitProductId;
var product_info = product.product_info[0];
if(product_info){
product_info.id = getDaySeo(3);
product_info.orderItemId = item.id;
item.product_info = product.product_info;
}
}
item.discountAddTotal = 0;
if (product.makeList) {
for (var make of product.makeList) {
var tempMake = getOrderItemMake(make);
tempMake.orderItemId = item.id;
item.product_make.push(tempMake);
if (product.isSuit != 3) {
item.addPriceTotal += make.count * make.addPrice;
item.amountAddTotal += make.count * make.addPrice;
}
}
}
/**计算套餐的做法加价 */
if (product.isSuit == 2) { // 套餐主菜
item.addPriceTotal = product.addPriceTotal;
item.amountAddTotal = product.addPriceTotal;
}
//如果有促销价
if (productSpec.promotionPrice > 0) {
item.price = productSpec.price;
item.discountPrice = productSpec.price;
//主要计算会员价还有券的时候判断是否存在存在不参与会员价跟券的优惠
item.promotionPrice = productSpec.promotionPrice;
item.discountRule = productSpec.discountRule;
} else {
//没有促销价 判断是不是套餐主菜
if(product.isSuit == 2){
item.price =item.addPriceOldPrice;
item.discountPrice=item.addPriceOldPrice;
}else{
item.price = productSpec.price;
}
if (product.giftProduct != "1" && product.isSuit != 2) {
item.discountPrice = productSpec.price;
}
}
if (isMember == 0) { // 微信支付
if( product.isSuit==2){
item.priceOrg = product.addPriceOldPrice;
}else{
item.priceOrg = productSpec.price;
}
}
if (isMember == 1) { // 会员卡支付
if( product.isSuit==2){
item.priceOrg = product.addPriceOldPrice;
}else{
item.priceOrg = productSpec.price;
}
}
if( product.isSuit==2){
item.amount = productSpec.price * item.count;
item.amountTotal = productSpec.price * item.count + item.amountAddTotal;
item.receivable = productSpec.price * item.count;
item.receivableTotal = item.receivable + item.addPriceTotal;
}else{
item.amount = item.price * item.count;
item.amountTotal = item.price * item.count + item.amountAddTotal;
item.receivable = item.discountPrice * item.count;
item.receivableTotal = item.receivable + item.addPriceTotal;
}
item.discountTotal = (item.price - item.discountPrice) * item.count;
if (product.isSuit == 3) {
var product_info = product.product_info[0];
if(product_info){
var discountPrice = item.price - product_info.discountMoney / product.count;
item.amount = parseFloat((item.price * item.count).toFixed(2));
item.discountPrice = parseFloat(discountPrice.toFixed(2));
item.receivable = parseFloat((item.discountPrice * item.count).toFixed(2));
item.amountTotal = parseFloat((item.price * item.count + item.amountAddTotal).toFixed(2));
item.receivableTotal = parseFloat((item.receivable + item.addPriceTotal).toFixed(2));
item.discountTotal = product_info.discountMoney;
}
}
return item;
}
/**
* 做法明细
*/
var getOrderItemMake = function (make) {
var order_item_make = {};
order_item_make.id = getDaySeo(4); // String 前台系统ID
order_item_make.orderItemId = ""; // String 商品明细ID
order_item_make.makeId = make.makeId; // String 做法ID
order_item_make.makeName = make.makeName; // String 做法名称
order_item_make.addPrice = make.addPrice; // Double 做法加价
order_item_make.discountPrice = make.addPrice; // Double 折后单价
order_item_make.count = make.count; // Double 做法数量
order_item_make.rcount = 0; // Double 做法退数量
order_item_make.addTotal = make.addPrice * make.count; // Double 加价总额
order_item_make.discountAddTotal = make.addPrice * make.count; // Double 折后总额
order_item_make.discount = 0; // Double 折后率
order_item_make.qtyFlag = make.qtyFlag; // Integer 做法管理数量标识(0否1是)
return order_item_make;
}
/**
* 优惠明细
*/
var getOrderItemDiscount = function (product, type) {
var product_info = {};
product_info.id = getDaySeo(3); // 前台系统ID
product_info.orderItemId = ""; // 产品明细ID
product_info.type = type; // Integer 优惠类型 0赠送 1折扣 2优惠券 3会员卡折扣 4满减 5满送 6议价 7兑换 8会员价 9立减
product_info.info = ""; // info String 优惠说明
product_info.discountMoney = product.discountTotal; // 优惠金额
return product_info;
}
/**
* 获取当前订单号
* orderNum:1 主单id
* orderNum:2 商品id
* orderNum:3 优惠明细id
* orderNum:4 做法明细id
*/
var getDaySeo = function (orderNum) {
var store = wx.getStorageSync("store");
return orderNum + store.storeNo + new Date().getTime() + app.globalData.globalNumber++;
}
/**
* 订单上传信息
*
*/
var getMemberOrder = function () {
var memberOrder = {};
memberOrder.id = getDaySeo(1); // 前台系统ID
memberOrder.payNo = ""; // 付款单号
memberOrder.payTypeNo = "02"; // 付款方式编号
memberOrder.payType = "会员卡支付"; //会员卡支付
memberOrder.paid = ""; // 实收金额
memberOrder.rchange = 0; // 找零金额
memberOrder.money = ""; // 已收金额
memberOrder.overAmount = 0; // 溢出金额
memberOrder.voucherNo = ""; // 凭证号
memberOrder.payDate = ""; //
memberOrder.cardno = ""; // 付款卡号
// memberOrder.cardYe = ""; // 充值卡支付前余额
// memberOrder.cardJf = ""; // 充值卡支付前积分
memberOrder.incomeFlag = 1; // 是否实收(0否1是)
// memberOrder.otherRateType = ""; // 第三方扣费类型(0不扣费1按次数2按比例)
// memberOrder.otherRateValue = ""; // 第三方扣费值
// memberOrder.otherRate = ""; // 第三方扣费
// memberOrder.payChannel = ""; // 支付渠道(-1无)
memberOrder.memo = "微信小程序点餐会员卡支付上传订单信息";
return memberOrder;
}
var giftProductAddOrderProduct = function (gift, list, coupon) {
var reduceCost = 0;
if (gift && gift.length > 0) {
for (var giftItem of gift) {
var productItem = {};
productItem.productName = giftItem.productName;
productItem.linkUrl = giftItem.linkUrl;
productItem.giftProduct = 1;
productItem.count = giftItem.num;
productItem.amount = giftItem.price;
reduceCost += giftItem.price * giftItem.num;
productItem.receivable = 0.0;
productItem.productId = giftItem.productId;
productItem.productNo = giftItem.productNo;
productItem.productUnitId = giftItem.unitId;
productItem.productUnitName = giftItem.unitName;
productItem.typeName = giftItem.typeName;
productItem.specId = giftItem.specId;
productItem.specName = giftItem.specName;
productItem.price = giftItem.price;
productItem.discountPrice = 0;
productItem.discountTotal = giftItem.price * giftItem.num;
productItem.priceOrg = giftItem.price;
productItem.amountTotal = giftItem.price * giftItem.num;
productItem.discount = 100;
productItem.receivableTotal = 0;
productItem.isSuit = parseFloat(giftItem.suitFlag) + 1;
productItem.boxPrice = giftItem.boxPrice;
var specList = [];
var specListItem = {};
specListItem.specId = giftItem.specId;
specListItem.price = giftItem.price;
specListItem.specName = giftItem.specName;
specList.push(specListItem);
productItem.specList = specList;
list.push(productItem);
}
app.orderTicket.orderList = list;
coupon.reduceCost = reduceCost * 100;
return list;
}
}
var checkPromotionProduct = function () {
//判断删掉商品后时候还有促销商品 没有去掉标识
var promotionFlag = false;
for (var item of app.orderTicket.orderList) {
if (item.discountRule) {
promotionFlag = true;
}
}
if (promotionFlag) {
app.orderTicket.promotionFlag = 1;
} else app.orderTicket.promotionFlag = 0;
}
module.exports = {
getDaySeo: getDaySeo,
cy_addItem: cy_addItem,
cy_subItem: cy_subItem,
cy_getIds: cy_getIds,
cy_getIdsName: cy_getIdsName,
cy_getProductCount: cy_getProductCount,
cy_getProductList: cy_getProductList,
getOrder: getOrder,
getMemberOrder: getMemberOrder,
giftProductAddOrderProduct: giftProductAddOrderProduct,
promotionComputePrice: promotionComputePrice,
checkPromotionProduct: checkPromotionProduct,
getMemberCreateOrder: getMemberCreateOrder
};