/**
* Created by 837 on 2017/8/12.
*/
var ctx = $("#ctx").val();
var product_index = $('#product_index').val();
// 订单促销活动
var promotionsIds = [];
var totalPromotions = [];
var giftPromotionsIds = [];
// 用户总优惠券
var totalCoupons = [];
// 当前商品可用优惠券
var selectCoupons = [];
// 勾选后的优惠金额
var couponPrice = 0;
// 添加订单true,修改订单false
var initFlag = true;
var orderStatus = 0;
var machineTypes = [];
$(function () {
/** 发票信息 start * */
// var invoiceTitle=$("#orderInvoice\\.invoiceTitle").val();
// if(invoiceTitle !='' && invoiceTitle !=null && invoiceTitle !='null'){
var type = $("#orderInvoice\\.type").val();
if (type == '1') {
$("#invoiceTable").show();
$("#addTaxInvoiceTable").hide();
$("#invoice2").attr("checked", true);
var invoiceTitleType = $("#orderInvoice\\.invoiceTitleType").val();
if (invoiceTitleType == 0) {
$('#orderInvoice_companyTitle').attr('disabled', true);
$('#orderInvoice_companyTitle').attr('value', '');
$('#orderInvoice_corporationTaxNum').attr('disabled', true);
$('#orderInvoice_corporationTaxNum').attr('value', '');
$($(":radio[name='normalTitle_r']").get(1)).attr("checked", 'checked');
$('#orderInvoice_normalTitle').attr('name', 'orderInvoice.invoiceTitle');
}
if (invoiceTitleType == 1) {
$('#orderInvoice_normalTitle').attr('disabled', true);
$('#orderInvoice_normalTitle').attr('value', '');
$('#orderInvoice_normalTitle').attr('name', '');
$($(":radio[name='normalTitle_r']").get(0)).attr("checked", 'checked');
$('#orderInvoice_companyTitle').attr('name', 'orderInvoice.invoiceTitle');
}
$("#orderInvoice\\.corporationTaxNum").attr('value', '');
$("#orderInvoice\\.corporationTaxNum").attr('name', '');
} else if (type == '2') {
$("#invoiceTable").hide();
$("#addTaxInvoiceTable").show();
$("#invoice3").attr("checked", true);
$("#orderInvoice_normalTitle").attr('disabled', true);
$("#orderInvoice\\.invoiceTitleType").attr("value", '1');
$('#orderInvoiceInvoiceTitle3').attr('name', 'orderInvoice.invoiceTitle');
$($(":radio[name='normalTitle_r']").get(0)).attr("checked", 'checked');
} else {
$("#invoiceTable").hide();
$("#addTaxInvoiceTable").hide();
$("#invoice1").attr("checked", true);
$("#orderInvoice_normalTitle").attr('disabled', true);
$("#orderInvoice\\.invoiceTitleType").attr("value", '1');
$("#orderInvoice_corporationTaxNum").attr('value', '');
$("#orderInvoice_corporationTaxNum").attr('name', '');
$($(":radio[name='normalTitle_r']").get(0)).attr("checked", 'checked');
}
//单存在输入时候默认为个人发票类型
$("#orderInvoice\\.invoiceTitle").on('change', function () {
$("#orderInvoice\\.type").val("1"); //优化发票修改
});
$(":radio[name='normalTitle_r']").click(function () {
var index = $(":radio[name='normalTitle_r']").index($(this));
$("#orderInvoice\\.corporationTaxNum").attr('value', '');
$("#orderInvoice\\.corporationTaxNum").attr('name', '');
$("#orderInvoiceInvoiceTitle3").attr('value', '');
$("#orderInvoiceInvoiceTitle3").attr('name', '');
if (index == 0) {
//普通发票的 企业抬头被选中了
$("#orderInvoice\\.invoiceTitleType").val("1");
$("#orderInvoice_normalTitle").attr('disabled', true);
$("#orderInvoice_normalTitle").attr('value', '');
$("#orderInvoice_companyTitle").attr('disabled', false);
$("#orderInvoice_corporationTaxNum").attr('disabled', false);
$('#orderInvoice_companyTitle').attr('name', 'orderInvoice.invoiceTitle');
$('#orderInvoice_corporationTaxNum').attr('name', 'orderInvoice.corporationTaxNum');
} else {
//普通发票的 个人抬头被选中了
$("#orderInvoice\\.invoiceTitleType").val("0");
$("#orderInvoice_companyTitle").attr('disabled', true);
$("#orderInvoice_companyTitle").attr('value', '');
$("#orderInvoice_corporationTaxNum").attr('disabled', true);
$("#orderInvoice_corporationTaxNum").attr('value', '');
$("#orderInvoice_normalTitle").attr('disabled', false);
$('#orderInvoice_companyTitle').attr('name', '');
$('#orderInvoice_normalTitle').attr('name', 'orderInvoice.invoiceTitle');
}
});
$(":radio[name='invoice']").click(function () {
var index = $(":radio[name='invoice']").index($(this));
if (index == 1) { //选中第2个时,div显示
$("#invoiceTable").hide();
$("#addTaxInvoiceTable").show();
$("#orderInvoice\\.type").val("2");
$('#orderInvoice_corporationTaxNum').attr('name', '');
$('#orderInvoice\\.corporationTaxNum').attr('name', 'orderInvoice.corporationTaxNum');
$('#orderInvoice_companyTitle').attr('name', '');
$('#orderInvoice_normalTitle').attr('name', '');
$('#orderInvoiceInvoiceTitle3').attr('name', 'orderInvoice.invoiceTitle');
} else if (index == 0) { //当被选中的不是第1个时,div隐藏
$("#invoiceTable").hide();
$("#addTaxInvoiceTable").hide();
$("#orderInvoice\\.invoiceTitle").val("");
$("#orderInvoice\\.type").val("0"); //优化发票修改
} else { //选中增值税发票
$("#invoiceTable").hide();
$("#addTaxInvoiceTable").show();
$("#orderInvoice\\.type").val("2");
$('#orderInvoice_corporationTaxNum').attr('name', '');
$('#orderInvoice\\.corporationTaxNum').attr('name', 'orderInvoice.corporationTaxNum');
$('#orderInvoice_companyTitle').attr('name', '');
$('#orderInvoice_normalTitle').attr('name', '');
$('#orderInvoiceInvoiceTitle3').attr('name', 'orderInvoice.invoiceTitle');
}
});
/** 发票信息 end * */
$("body").on('change', '.shopNote', function () {
var self = $(this);
var shopID = self.attr('data-shopid');
var val = self.val();
self.next('input[name=shopNote]').val(val + '=' + shopID);
});
/** 修改订单初始化 */
var orderId = $("#orderID").val() * 1;
orderStatus = $("#orderStatus").val() * 1;
if (orderId && orderId > 0) {
var userId = $("#inputForm").find("[name*='bpOrderUserinfo.userId']").val();
// 获取用户总优惠券
getUserCoupons(userId);
// 修改订单初始化商品列表
$('#product_index').val(0);
product_index = 0;
var productList = [];
$('.productTable tbody tr:not(.gift)').each(function (index, element) {
var data = JSON.parse($(this).attr("data-value"));
data.shopId = data.shopID ? data.shopID : data.shopId;
data.productId = data.productID ? data.productID : data.productId;
if (data.productPromotion) {
data.cmPromotion = {
id: data.productPromotion.promotionsId,
isNewRecord: data.productPromotion.isNewRecord,
name: data.productPromotion.name,
type: data.productPromotion.type,
mode: data.productPromotion.mode,
touchPrice: data.productPromotion.touchPrice,
reducedPrice: data.productPromotion.reducedPrice,
status: data.productPromotion.status,
discount: data.productPromotion.discount
};
var tempProduct = {
shopId: data.shopId,
productId: data.productId,
price: data.price,
number: data.num,
totalTax: data.totalAddedValueTax
};
data.productId = data.productID;
data.shopId = data.shopID;
var tempId = data.productPromotion.promotionsId;
if (promotionsIds.indexOf(tempId) >= 0) {
totalPromotions.forEach(function (item) {
if (item.id * 1 === tempId) {
for (var i = 0; i < item.promotionProducts.length; i++) {
if (item.promotionProducts[i].productId == data.productId) {
item.promotionProducts.splice(i--, 1);
}
}
item.promotionProducts.push(tempProduct);
}
})
} else {
promotionsIds.push(tempId);
var tempPromotions = JSON.parse(JSON.stringify(data.cmPromotion));
if (tempPromotions.mode * 1 === 1) {
tempPromotions.reducedPrice = data.price - tempPromotions.touchPrice;
}
if (data.cmPromotion.type * 1 === 3) {
tempPromotions.shopId = data.shopId;
}
/*if(data.cmPromotion.mode*1 === 3){
giftPromotionsIds.push(tempId);
}*/
tempPromotions.promotionProducts = [];
tempPromotions.promotionProducts.push(tempProduct);
totalPromotions.push(tempPromotions);
}
}
data.productPromotion = null;
productList.push(data);
});
$('.productTable tbody tr.gift').each(function (index, element) {
var present = JSON.parse($(this).attr("data-value"));
present.shopId = present.shopID ? present.shopID : present.shopId;
present.productId = present.productID ? present.productID : present.productId;
if (present.productType == 1) {
productList.forEach(function (data) {
if (present.productId == data.productId) {
data.presentNum = present.num;
}
});
}
});
$('.productTable').each(function (index, element) {
$(this).prev('.order-rows').remove();
$(this).remove();
});
initFlag = false;
productList.forEach(function (data) {
setProductList(data);
});
setOrderFee(); //设置底部订单金额
initFlag = true;
}
});
/************************添加会所 start**********************************************************/
/**
* 展示会所选择框
* @param iframe
*/
function showClubSelect(iframe) {
top.$.jBox(iframe, {
iframeScrolling: 'yes',
width: $(top.document).width() - 100,
height: $(top.document).height() - 160,
persistent: true,
// showScrolling: false,
title: "选择会所",
bottomText: '请选择一个会所',
buttons: {
"确定": '1',
"关闭": '-1'
},
submit: function (v, h, f) {
//确定
var $jboxFrame = top.$('#jbox-iframe');
var $mainFrame = top.$('#mainFrame');
if ('1' == v && 1 == $jboxFrame.size() && 1 == $mainFrame.size()) {
var items = $jboxFrame[0].contentWindow.getCheckedItems();
// console.log(items);
if (items.length > 0) {
//$jboxFrame[0].contentWindow.setBinding(items);
setSingleSelect(items); //设置选中值
// initStages();//初始化分期信息---分期功能目前已废弃
$('.productTable,.order-rows').remove(); //重新选择会所时清空商品
$('.productTotalFee,.payTotalFee,.payableAmount,.balancePayFee').text('0.00'); //清空金额
$('#surplusBalance,#surplusBalance').text('0.00');
$('.productCount').text('0');
// 设置添加商品显示
$(".addSecondHandProduct").show();
return true;
} else {
top.$.jBox.tip("请选择...");
return false;
}
}
return true;
}
});
}
/**
* 选择会所后设置用户信息
* @param items
*/
function setSingleSelect(items) {
var info = eval('(' + items[0] + ')');
$("#curProvince").val(info.province);
$("#curCity").val(info.city);
$("#curTown").val(info.town);
loadProvince(); //加载地址下拉选项
$("#bpOrderUserinfo\\.address").val(info.address); //设置详细地址
console.log(info);
$("#bpOrderUserinfo\\.clubId").val(info.id); //设置会所ID
$("#bpOrderUserinfo\\.userId").val(info.userID);
$("#bpOrderUserinfo\\.firstClubType").val(info.firstClubType ? info.firstClubType :'');
$("#inputForm").find("[name*='bpOrderUserinfo.province']").select2("val", info.province); //设置省份值
$("#inputForm").find("[name*='bpOrderUserinfo.city']").select2("val", info.city);
$("#inputForm").find("[name*='bpOrderUserinfo.town']").select2("val", info.town);
$("#inputForm").find("[name*='bpOrderUserinfo.name']").val(info.name);
$("#inputForm").find("[name*='bpOrderUserinfo.shouHuoRen']").val(info.linkMan1 ? info.linkMan1 : info.linkMan);
$("#inputForm").find("[name*='bpOrderUserinfo.phonee']").val(info.contractPhone1 ? info.contractPhone1 : info.contractPhone);
$("#inputForm").find("[name*='bpOrderUserinfo.mobile']").val(info.contractMobile1 ? info.contractMobile1 : info.contractMobile);
$("#svipFlagLabel").text(1 == info.svipFlag ? '(超级会员)' : '');
$("#availableBalance").html(info.ableUserMoney);
//设置添加商品可见(会所添加添加运费订单公用此方法但是添加运费订单不需要添加商品)
var ui = document.getElementById("shopSelected");
var ui1 = document.getElementById("shopSelected1");
if (ui) {
ui.style.display = "";
ui1.style.display = "none";
}
// 获取用户总优惠券
getUserCoupons(info.userID);
}
function getUserCoupons(userId) {
totalCoupons = [];
$.get($("#ctx").val() + "/order/clubCouponList?userId=" + userId, function (data) {
if (data.code * 1 === 0) {
totalCoupons = data.couponList;
// 展示可选优惠券列表
getShownCouponList();
}
})
}
/************************添加会所 end**********************************************************/
// 兑换优惠券
$("#redeemCoupons").on("click", function () {
var userId = $("#inputForm").find("[name*='bpOrderUserinfo.userId']").val();
if (userId && userId * 1 > 0) {
var html = '
';
var submit = function (v, h, f) {
if (f.couponsCode == '' || isNaN(f.couponsCode)) {
$.jBox.tip("请输入兑换码!", 'error', {focusId: "couponsCode"});
}
var bool = new RegExp("^[A-Za-z0-9]{16}$").test(f.couponsCode);
if (!bool) {
// 16位数字和字母
$.jBox.tip("请输入正确的兑换码!", 'error', {focusId: "couponsCode"});
} else {
$.post($("#ctx").val() + "/coupon/cmCouponClub/redeem", {
'redemptionCode': f.couponsCode,
'userId': userId
}, function (data) {
if (0 === data.code * 1) {
$.jBox.tip(data.msg, 'info');
// 获取用户总优惠券
getUserCoupons(userId);
} else {
$.jBox.tip(data.msg, 'error');
}
}, "JSON");
}
};
$.jBox(html, {title: "兑换优惠券", buttons: {'立即兑换': true}, submit: submit});
} else {
alertx('请您先选择会所!');
}
});
/************************商品相关 start**********************************************************/
//设置商品(正常)
function editShop(obj) {
var $tr = obj.parents('tr');
var data = $tr.attr("data-value");
data = JSON.parse(data);
var productID = (data.productId == undefined) ? data.productID : data.productId;
var secondHandType = (data.secondHandType == undefined) ? 0 : data.secondHandType;
var discount = (data.discount == undefined) ? 100 : data.discount;
var productCategory = (data.productCategory == undefined) ? 1 : data.productCategory;
var includexTax = (data.includexTax == undefined) ? 2 : data.includexTax;
var invoiceType = (data.invoiceType == undefined) ? 3 : data.invoiceType;
var taxRate = (data.taxRate == undefined) ? 0 : data.taxRate;
var addedValueTax = (data.addedValueTax == undefined) ? 0 : data.addedValueTax;
var totalAddedValueTax = (data.totalAddedValueTax == undefined) ? 0 : data.totalAddedValueTax;
$.jBox.open("iframe:" + ctx + "/bulkpurchase/purchaseProduct/formOrderProduct?productId=" + productID +
"&secondHandType=" +
secondHandType + "&discount=" + discount + "&productCategory=" + productCategory + "&includedTax=" +
includexTax +
"&invoiceType=" + invoiceType + "&taxRate=" + taxRate + "&addedValueTax=" + addedValueTax +
"&totalAddedValueTax" + totalAddedValueTax,
"设置商品", $(top.document).width() - 240, $(top.document).height() - 200, {
top: 36,
persistent: true,
buttons: {
"确定": "ok",
"关闭": true
},
// buttons:{"关闭":true},
bottomText: "设置商品",
submit: function (v, h, f) {
if (v == "ok") {
var $jboxFrame = $('#jbox-iframe');
var items = $jboxFrame[0].contentWindow.getCheckedItems();
if (!items) {
return false;
} else {
console.log(items[0]);
setProductList(items[0], false); //设置商品列表
// initStages();//初始化分期信息
setOrderFee(); //设置底部订单金额
}
return true;
}
},
loaded: function (h) {
//$(".jbox-content", top.document).css("overflow-y","hidden");
}
});
}
// 展示可选优惠券列表
function getShownCouponList() {
selectCoupons = [];
var couponProductIds = [];
var couponProductList = [];
$('.productTable tbody tr:not(.gift)').each(function (index, element) {
var data = JSON.parse($(this).attr("data-value"));
couponProductList.push(data);
couponProductIds.push(data.productId);
});
totalCoupons.forEach(function (coupon) {
var totalAmount = 0;
var couponType = coupon.couponType * 1;
couponProductList.forEach(function (item) {
var productPrice = item.totalFee * 1;
if (couponType === 0) {
if (coupon.productType * 1 === 1) {
//活动券-全商城商品
totalAmount += productPrice;
} else if (coupon.productType * 1 === 2) {
//活动券-指定商品
if (coupon.productIdList.length > 0 && coupon.productIdList.indexOf(item.productId * 1) >= 0) {
totalAmount += productPrice;
}
}
} else if (couponType === 1) {
//品类券
if (coupon.categoryType == item.commodityType) {
totalAmount += productPrice;
}
} else if (couponType === 2 || couponType === 4) {
//用户专享券或新用户券
totalAmount += productPrice;
} else if (couponType === 3) {
//店铺券
if (coupon.shopId == item.shopId) {
totalAmount += productPrice;
}
}
});
if (totalAmount >= coupon.touchPrice) {
selectCoupons.push(coupon);
}
});
setPageCoupon();
}
// 设置页面显示
function setPageCoupon() {
var clubCouponId = $("#clubCouponId").val() * 1;
var html = '';
selectCoupons.forEach(function (item) {
var checked = '';
var redemptionCode = '';
if (clubCouponId === item.clubCouponId) {
checked = 'checked="checked"';
}
if (!isNaN(item.redemptionCode)) {
redemptionCode = '兑换码:' + item.redemptionCode + '';
}
html += '';
});
$("#couponBox").find(".couponContent").html(html);
if (clubCouponId > 0) {
$("#couponBox").find("input[name=clubCouponId][checked=checked]").trigger("change");
}
}
// 勾选优惠券重新计算总价
$("#couponBox").on("change", "input[name=clubCouponId]", function () {
couponPrice = 0;
var clubCouponId = $(this).val() * 1;
selectCoupons.forEach(function (item) {
if (item.clubCouponId * 1 === clubCouponId) {
couponPrice = item.couponAmount;
$("#clubCouponId").val(clubCouponId);
$("#couponAmount").val(couponPrice);
} else if (clubCouponId == 0) {
couponPrice = 0;
$("#clubCouponId").val('');
$("#couponAmount").val(couponPrice);
}
});
// 重新计算总价
setOrderFee();
});
// 设置商品(二手)
function orderSecondHandEditShop(obj) {
var $tr = obj.parents('tr');
var data = $tr.attr("data-value");
data = JSON.parse(data);
var productID = (data.productId == undefined) ? data.productID : data.productId;
$.jBox.open("iframe:" + ctx + "/bulkpurchase/purchaseProduct/orderSecondHandProductForm?productId=" + productID,
"设置商品", $(top.document).width() - 240, $(top.document).height() - 200, {
top: 36,
persistent: true,
buttons: {
"确定": "ok",
"关闭": true
},
bottomText: "设置商品",
submit: function (v, h, f) {
if (v == "ok") {
var $jboxFrame = $('#jbox-iframe');
var items = $jboxFrame[0].contentWindow.getCheckedItems();
if (!items) {
return false;
} else {
console.log(items[0]);
setProductList(items[0], false); //设置商品列表
setOrderFee(); //设置底部订单金额
}
return true;
}
},
loaded: function (h) {
}
});
}
// 修改商品的时候统计下面的订单商品金额
function setShopOrderFee(shopId) {
var totalFee = 0;
$($('#pTable_' + shopId)).find('.totalFee').each(function (index, element) {
totalFee += Number($(this).html());
});
totalPromotions.forEach(function (item) {
if (item.shopId == shopId && item.type == 3 && item.mode == 2) {
var touchPrice = 0;
item.promotionProducts.forEach(function (product) { //计算促销包含税费
touchPrice += (product.number * product.price + product.totalTax * 1);
});
if (touchPrice >= item.touchPrice) {
totalFee -= item.reducedPrice;
}
}
});
$($('#pTable_' + shopId)).prev('div').find('.bmd').html('¥' + totalFee.toFixed(2));
}
//删除商品
function deleteShop(obj) {
var $tr = obj.parents('tr');
var $table = $tr.parents('table');
// 协销赠品
var productId = $tr.attr("id").split("_")[1];
$('.productTable tbody tr.gift_' + productId).each(function (index, element) {
$(this).remove();
});
// 促销
var promotionsId = $tr.find('.tag').attr("data-id");
var thisTouchPrice = 0;
totalPromotions.forEach(function (item) {
if (item.id == promotionsId) {
for (var i = 0; i < item.promotionProducts.length; i++) {
if (item.promotionProducts[i].productId == productId) {
item.promotionProducts.splice(i--, 1);
}
}
item.promotionProducts.forEach(function (p) {
thisTouchPrice += p.number * p.price;
});
if (thisTouchPrice < item.touchPrice) {
$('.productTable tbody tr.promotion_' + promotionsId).each(function (index, element) {
$(this).remove();
});
}
}
});
$tr.remove();
if ($table.find('tr').length < 2) {
//table里面没有商品了 table 也删掉 <2 是因为表头上有一个tr
$table.prev('.order-rows').remove();
$table.remove();
}
var shopId = $table.attr("id").split("_")[1];
setShopOrderFee(shopId);
setOrderFee(); //设置底部订单金额
// initStages();//初始化分期信息
//重新显示添加按钮(针对二手商品)
$(".addSecondHandProduct").show();
}
function resetProductList(setData, ind) {
var productList = [];
var productIdList = [];
var skuIds = [];
//var if (promotionsIds.indexOf(tempId)>=0) {
$('.productTable tbody tr:not(.gift)').each(function (index, element) {
var data = JSON.parse($(this).attr("data-value"));
productList.push(data);
if (data.skuStr) {
var strings = data.skuStr.split("/");
var skuId = strings[0];
skuId = skuId * 1;
skuIds.push(skuId);
} else {
var skuId = "";
if (data.skuList) {
skuId = data.skuList[0].skuId;
} else {
skuId = data.skuId;
}
skuId = skuId * 1;
skuIds.push(skuId);
}
productIdList.push(data.productId);
});
$('.productTable').each(function (index, element) {
$(this).prev('.order-rows').remove();
$(this).remove();
});
promotionsIds = [];
totalPromotions = [];
giftPromotionsIds = [];
if (!setData) { //删除
productList.forEach(function (data) {
setProductList(data, false);
});
} else {
if (ind) {
productList.forEach(function (data) {
setProductList(data, false);
});
}
var skuId;
if (setData.skuStr) {
var strings = setData.skuStr.split("/");
skuId = strings[0];
} else {
skuId = setData.skuList[0].skuId;
}
skuId = skuId * 1;
if (skuIds.indexOf(skuId) >= 0) {
//设置
productList.forEach(function (data) {
if (setData.skuId == data.skuId) {
setProductList(setData, false, ind);
} else {
setProductList(data, false, ind);
}
});
} else {
//新增
productList.forEach(function (data) {
setProductList(data, false, ind);
});
setProductList(setData, false, ind);
}
}
setOrderFee(); //设置底部订单金额
}
function deleteShopProduct(obj) {
console.log('machineTypes',machineTypes)
var skuId = obj.parents('tr').find('.orderProductSkuId').text();
obj.parents('tr').remove();
console.log('skuId======',skuId)
machineTypes = machineTypes.filter(item => item.skuId !== skuId*1);
console.log('machineTypes======',machineTypes)
var $table = obj.parents('tr').parents('table');
if ($table.find('tr').length < 2) {
//table里面没有商品了 table 也删掉 <2 是因为表头上有一个tr
$table.prev('.order-rows').remove();
$table.remove();
}
resetProductList();
if ($('.productTable tbody tr').length === 0) {
setOrderFee();
// 展示可选优惠券列表
getShownCouponList();
}
$(".addSecondHandProduct").show();
}
function editShopProduct(obj) {
var $tr = obj.parents('tr');
var data = $tr.attr("data-value");
data = JSON.parse(data);
var userId = $("#inputForm").find("[name*='bpOrderUserinfo.userId']").val();
var productID = (data.productId == undefined) ? data.productID : data.productId;
var skuId = (data.skuId == undefined) ? data.skuId : data.skuId;
var secondHandType = (data.secondHandType == undefined) ? 0 : data.secondHandType;
var discount = (data.discount == undefined) ? 100 : data.discount;
var productCategory = (data.productCategory == undefined) ? 1 : data.productCategory;
var price = (data.price == undefined) ? 0 : data.price;
var includedTax = (data.includedTax == undefined) ? 2 : data.includedTax;
var invoiceType = (data.invoiceType == undefined) ? 3 : data.invoiceType;
var taxRate = (data.taxRate == undefined) ? 0 : data.taxRate;
var addedValueTax = (data.addedValueTax == undefined) ? 0 : data.addedValueTax;
var totalAddedValueTax = (data.totalAddedValueTax == undefined) ? 0 : data.totalAddedValueTax;
var couponsLogo = (data.couponsLogo == undefined) ? 1 : data.couponsLogo;
var svipReduction = (data.svipReduction == undefined) ? 0 : data.svipReduction;
var svipTaxReduction = (data.svipTaxReduction == undefined) ? 0 : data.svipTaxReduction;
$.jBox.open("iframe:" + ctx + "/bulkpurchase/purchaseProduct/formOrderProduct?productId=" + productID +
"&secondHandType=" + secondHandType +
"&discount=" + discount + "&productCategory=" + productCategory + "&price=" + price + "&includedTax=" +
includedTax + "&userId=" + userId +
"&invoiceType=" + invoiceType + "&taxRate=" + taxRate + "&addedValueTax=" + addedValueTax +
"&totalAddedValueTax=" + totalAddedValueTax + "&couponsLogo=" + couponsLogo + "&svipReduction=" + svipReduction +
"&svipTaxReduction=" + svipTaxReduction,
"设置商品", $(top.document).width() - 240, $(top.document).height() - 200, {
top: 36,
persistent: true,
buttons: {
"确定": "ok",
"关闭": true
},
bottomText: "设置商品",
submit: function (v, h, f) {
if (v == "ok") {
var $jboxFrame = $('#jbox-iframe');
var items = $jboxFrame[0].contentWindow.getCheckedItems();
if (!items) {
return false;
} else {
console.log('设置:' + items[0]);
resetProductList(items[0], skuId); //设置商品列表
}
return true;
}
},
loaded: function (h) {
}
});
}
// 展示商品选择框
function showShopSelect(iframe) {
var userId = $("#inputForm").find("[name*='bpOrderUserinfo.userId']").val();
iframe = iframe + "&userId=" + userId;
top.$.jBox(iframe, {
iframeScrolling: 'yes',
width: $(top.document).width() - 100,
height: $(top.document).height() - 160,
persistent: true,
title: "选择商品",
bottomText: '请从可供选择的项目中选择一个',
buttons: {
"确定": '1',
"关闭": '-1'
},
submit: function (v, h, f) {
//确定
var $jboxFrame = top.$('#jbox-iframe');
var $mainFrame = top.$('#mainFrame');
if ('1' == v && 1 == $jboxFrame.size() && 1 == $mainFrame.size()) {
var items = $jboxFrame[0].contentWindow.getCheckedItems();
if (!items) {
return false;
} else if (items.length > 0) {
console.log('添加:' + JSON.stringify(items[0]));
resetProductList(items[0], 0); //设置商品列表
return true;
} else {
top.$.jBox.tip("请选择...");
return false;
}
}
},
});
}
// 展示优惠券详情
function showCouponDetail(userId, productId) {
var url = ctx + "/coupon/cmCoupon/productDetails?userId=" + userId + "&productId=" + productId;
var title = '';
title = "查看优惠券";
top.$.jBox("iframe:" + url, {
iframeScrolling: 'yes',
width: $(top.document).width() - 600,
height: $(top.document).height() - 250,
persistent: true,
title: title,
buttons: {"确定": '1'}
});
}
// 展示二手商品选择框
function showSencondHandSelect(iframe) {
var userId = $("#inputForm").find("[name*='bpOrderUserinfo.userId']").val();
iframe = iframe + "&userId=" + userId;
top.$.jBox(iframe, {
iframeScrolling: 'yes',
width: $(top.document).width() - 100,
height: $(top.document).height() - 160,
persistent: true,
title: "选择商品",
bottomText: '请从可供选择的项目中选择一个',
buttons: {
"确定": '1',
"关闭": '-1'
},
submit: function (v, h, f) {
//确定
var $jboxFrame = top.$('#jbox-iframe');
var $mainFrame = top.$('#mainFrame');
if ('1' == v && 1 == $jboxFrame.size() && 1 == $mainFrame.size()) {
var items = $jboxFrame[0].contentWindow.getCheckedItems();
if (!items) {
return false;
} else if (items.length > 0) {
console.log($('.productTable'));
$(('.producttable')).remove();
setSeondHandProductList(items[0]); //设置二手商品列表
// 隐藏商品添加入口
$(".addSecondHandProduct").hide();
return true;
} else {
top.$.jBox.tip("请选择...");
return false;
}
}
},
});
}
/**
* 设置商品列表(正常商品)
* @param items
*/
function setProductList(data, flag, ind) {
console.log("设置" + JSON.stringify(data));
var skuStr = "";
var skuIdStr = "";
var price = "";
if (data.skuList) {
skuStr = "" + data.skuList[0].unit + " | ";
skuIdStr = "" + data.skuList[0].skuId + " | ";
price = data.skuList[0].price;
} else {
skuStr = "" + data.unit + " | ";
skuIdStr = "" + data.skuId + " | ";
price = data.price;
}
//未选择sku,默认sku[0],选择了sku,skustr不为空
if (data.skuStr) {
var strings = data.skuStr.split("/");
var skuId = strings[0];
var index = strings[1] * 1;
skuStr = "" + data.skuList[index].unit + " | ";
skuIdStr = "" + skuId + " | ";
//price
price = data.skuList[index].price;
}
var userId = $("#inputForm").find("[name*='bpOrderUserinfo.userId']").val();
var orderStatus = $("#orderStatus").val() ? $("#orderStatus").val() * 1 : 0;
data.productType = 0;
var shopFee = data.shopFee;
var cmPromotion = '';
var ladderPrice = '';
// 如果存在促销活动
if (data.cmPromotion) {
// debugger;
var tempId = data.cmPromotion.id * 1;
data.promotionsId = data.cmPromotion.id;
// 没有折扣,才享受促销优惠
if (data.discount * 1 === 100) {
var tempProduct = {
shopId: data.shopId,
productId: data.productId,
//有skuid后不能直传price,从选中项中取
price: price,
number: data.num,
totalTax: data.totalAddedValueTax
};
if (promotionsIds.indexOf(tempId) >= 0) {
totalPromotions.forEach(function (item) {
if (item.id * 1 === tempId) {
for (var i = 0; i < item.promotionProducts.length; i++) {
if (item.promotionProducts[i].productId == data.productId) {
item.promotionProducts.splice(i--, 1);
}
}
item.promotionProducts.push(tempProduct);
}
})
} else {
promotionsIds.push(tempId);
var tempPromotions = JSON.parse(JSON.stringify(data.cmPromotion));
if (tempPromotions.mode == 1) {
tempPromotions.reducedPrice = data.price - tempPromotions.touchPrice;
}
if (data.cmPromotion.type == 3) {
tempPromotions.shopId = data.shopId;
}
tempPromotions.promotionProducts = [];
tempPromotions.promotionProducts.push(tempProduct);
totalPromotions.push(tempPromotions);
}
if (data.cmPromotion.mode == 1) {
data.totalFee = (data.cmPromotion.touchPrice * data.num + data.totalAddedValueTax * 1);
cmPromotion = '' + data
.cmPromotion.name + '优惠价:' + data.cmPromotion.touchPrice + '';
} else if (data.cmPromotion.mode == 2) {
cmPromotion = '' + data
.cmPromotion.name + '满' + data.cmPromotion.touchPrice + '减' + data.cmPromotion.reducedPrice +
'';
} else if (data.cmPromotion.mode == 3) {
cmPromotion = '' + data
.cmPromotion.name + '满' + data.cmPromotion.touchPrice + '赠送商品' + '';
}
} else {
cmPromotion = '有折扣,不享受促销优惠'
}
} else {
if (data.ladderPriceFlag * 1 === 1) {
ladderPrice = '
阶梯价格';
if (data.ladderPriceList && data.ladderPriceList.length > 0) {
var ladder = '起订量价格
';
for (var i = 0; i < data.ladderPriceList.length; i++) {
ladder += '' + data.ladderPriceList[i]
.buyNum + '' + data.ladderPriceList[i].buyPrice +
'
';
}
ladderPrice = '
阶梯价格' +
ladder + '
'
}
}
}
var cmCouponBtn = '---';
if (data.couponsLogo * 1 === 2) {
cmCouponBtn = '查看';
}
var str =
"" +
"" + data.productId + " | " +
skuIdStr +
"" + data.shopId + " | " +
"" + data.shopName + " | " +
"" + data.name + " | | " +
(data.productCategory == 2 ? "" : ("" + data.shopName + " | ")) +
skuStr +
"" + data.num + (data.presentNum > 0 ? "(" + data.presentNum + ")" : "") + " | " +
//价格
(data.svipPriceFlag == 1 ? "" + data.discountPrice :
(data.discount * 1 === 100 && data.cmPromotion && data.cmPromotion.mode == 1 ? (" | " + price +
"") : (" | " + price))) +
((data.svipPriceFlag == 1 || (data.includedTax != null && data.includedTax != '' && data.includedTax != 2)) ? "" : "") +
" | " +
"" + data.discount + " | " +
(data.discount * 1 === 100 && data.cmPromotion && data.cmPromotion.mode == 1 ? ("" + data
.discountPrice + " | ") : ("" + data.discountPrice +
ladderPrice + " | ")) +
(data.includedTax != '' && (data.includedTax == 0 && data.invoiceType == 3) ? ("--- | ") : data
.includedTax == 2 ? ("0% | ") : ("" + data.taxRate + "% | ")) +
(data.includedTax == 1 || (data.includedTax != '' && data.includedTax == 0 && data.invoiceType == 3) ? (
"--- | ") : data.addedValueTax == null ? ("0 | ") : ("" + data.addedValueTax + " | ")) +
(data.includedTax == 1 || (data.includedTax != '' && data.includedTax == 0 && data.invoiceType == 3) ? (
"--- | ") : data.totalAddedValueTax == null ? ("0 | ") : ("" + data.totalAddedValueTax +
" | ")) +
"" + Number(data.totalFee).toFixed(2) + " | " +
// ""+ Math.round(shopFee*100)/100 +" | "+
"" + (data.buyAgainFlag == 1 ? "是" : "否") + " | " +
((data.productCategory == 2) ? "" : "" + cmPromotion + " | ") +
((data.productCategory == 2) ? "" : "" + cmCouponBtn + " | ") +
"" + (orderStatus != 0 ? "" : (data.productCategory == 2) ?
"设置 删除商品" :
"设置 删除商品"
) +
" | " +
"
";
appendProductList(str, data, cmPromotion, ind);
var firstClubType = $("#inputForm").find("[name*='bpOrderUserinfo.firstClubType']").val();
const hasSku = machineTypes.find(item => item.skuId === data.skuId)
!hasSku && machineTypes.push({ machineType : data.machineType , skuId: data.skuId })
console.log('machineTypes',machineTypes)
if(firstClubType !== '1' && machineTypes.find(item => item.machineType === '3')){
$('#checkedTips').show();
}else{
$('#checkedTips').hide();
}
}
function appendProductList(str, data, cmPromotion, ind) {
// debugger
//把表中存在的删除
var removeBeforeSoon = false; //是这一次移除
var removeEle;
var skuId;
if (data.skuStr) {
var strings = data.skuStr.split("/");
skuId = strings[0];
} else {
if (data.skuList) {
skuId = data.skuList[0].skuId;
} else {
skuId = data.skuId;
}
}
skuId = skuId * 1;
$('.productTable tbody tr:not(.gift)').each(function (index, element) {
if ($(this).find("td:nth-child(2)").html() == skuId) {
removeEle = $(this).parent();
$(this).remove();
removeBeforeSoon = true;
}
});
if (removeBeforeSoon) {
//走到这里 说明是修改
removeEle.append(str);
} else {
//这里就是新加了 //先看有没有这个 当前这个商品的供应商的table
if ($('#pTable_' + data.shopId).length != 0) {
$('#pTable_' + data.shopId + ' tbody').append(str);
} else {
appendShopOrderHead(data, str, cmPromotion);
}
}
setShopOrderFee(data.shopId);
// setOrderFee(); //设置底部订单金额
product_index++;
// 设置赠品
setGiftHtmlList(data, cmPromotion);
// 展示可选优惠券列表
getShownCouponList();
}
function appendShopOrderHead(data, str, cmPromotion) {
var html = '' +
'
' +
'' +
'' +
'' +
'
' +
'
' + data.shopName + (data.cmPromotion && data.cmPromotion.type == 3 ? cmPromotion :
'') + '
' +
'
' +
"" +
'' +
'
' +
'
' + '' +
'' +
'' +
'商品ID | ' +
'skuId | ' +
' | ' +
' | ' +
'商品名称 | | ' +
((data.productCategory == 2) ? '' : '供应商名称 | ') +
'规格 | ' +
'数量(赠品数) | ' +
'单价 | ' +
'折扣 | ' +
'折后单价 | ' +
'税率 | ' +
'单个税费 | ' +
'税费 | ' +
'总价 | ' +
/*'应付供应商 | ' +*/
'能否复购 | ' +
((data.productCategory == 2) ? '' : '促销活动 | ') +
((data.productCategory == 2) ? "" : "优惠券 | ") +
'操作 | ' +
'
' +
'' +
'' + str + '
';
$('#product_index').before(html);
}
function setGiftHtmlList(data, cmPromotion) {
console.log("设置赠品" + JSON.stringify(data));
if (data.presentNum > 0) {
// 已经添加的,重新添加
$('.productTable tbody tr.gift_' + data.skuId).each(function (index, element) {
$(this).remove();
});
// debugger;
var skuStr = "赠品";
if (data.skuStr) {
var strings = data.skuStr.split("/");
var index = strings[1] * 1;
skuStr = data.skuList[index].unit;
} else {
skuStr = data.skuList[0].unit;
}
var present = JSON.parse(JSON.stringify(data));
present.productType = 1;
present.num = data.presentNum;
present.price = 0;
present.discountPrice = 0;
present.orderProductId = '';
present.orderProductID = '';
var giftStr = "" +
"" + present.productId + " | " +
"" + present.skuId + " | " +
"" + present.shopId + " | " +
"" + present.shopName + " | " +
"协商赠品:" + present.name + " | | " +
"" + present.shopName + " | " +
"" + skuStr + " | " +
"" + present.num + " | " +
"0 | " +
"100 | " +
"0 | " +
"0 | " +
"0 | " +
"0 | " +
"0 | " +
/*""+ Math.round(present.shopFee*100)/100 +" | "+*/
"否 | " +
" | " +
" | " +
"
";
product_index++;
appendGiftsList(present, giftStr, cmPromotion);
}
if (data.cmPromotion && data.cmPromotion.mode == 3) {
var thisTouchPrice = 0;
totalPromotions.forEach(function (item) {
if (data.cmPromotion.id == item.id) {
item.promotionProducts.forEach(function (p) {
thisTouchPrice += (p.number * p.price + p.totalTax * 1);
});
}
});
//本单总价>=满赠条件金额
if (thisTouchPrice >= data.cmPromotion.touchPrice) {
if (giftPromotionsIds.indexOf(data.cmPromotion.id) < 0) {
// 已经添加的,重新添加
$('.productTable tbody tr.promotion_' + cmPromotion.id).each(function (index, element) {
$(this).remove();
});
// 记录赠品促销
giftPromotionsIds.push(data.cmPromotion.id);
// 获取赠品
var getGiftPath = $('#ctx').val() + '/product/cmPromotions/getGifts';
$.getJSON(getGiftPath, {
id: data.cmPromotion.id
}, function (res) {
if (res && res.length > 0) {
res.forEach(function (gift) {
gift.productType = 2;
gift.price = 0;
gift.discountPrice = 0;
gift.promotionsId = data.cmPromotion.id;
gift.orderProductId = '';
gift.orderProductID = '';
if ($("#product_promotion_" + gift.productId).length > 0) {
var pageGift = JSON.parse($("#product_promotion_" + gift.productId)
.attr("data-value"));
if (gift.discounts * 1 === 1) {
var b = Math.floor(thisTouchPrice / data.cmPromotion.touchPrice);
gift.num = gift.num * 1 * b + pageGift.num * 1;
} else {
gift.num = gift.num * 1 + pageGift.num * 1;
}
$("#product_promotion_" + gift.productId).remove();
}
if (gift.discounts * 1 === 1) {
var b = Math.floor(thisTouchPrice / data.cmPromotion.touchPrice);
gift.num = gift.num * 1 * b;
} else {
gift.num = gift.num * 1;
}
var giftStr = "" +
"" + gift.productId + " | " +
"" + 0 + " | " +
"" + gift.shopId +
" | " +
"促销赠品:" + gift.name +
" | " + gift.shopName + " | " +
" | " +
"赠品 | " +
"" + gift.num + " | " +
"0 | " +
"100 | " +
"0 | " +
"0 | " +
"0 | " +
"0 | " +
"0 | " +
/*""+ Math.round(gift.shopFee*100)/100 +" | "+*/
"否 | " +
" | " +
" | " +
"
";
product_index++;
appendGiftsList(gift, giftStr, cmPromotion);
});
}
});
}
}
}
// 重新设置底部订单金额
// setOrderFee();
// 重新设置返佣订单选项
showRebateCheck();
}
function appendGiftsList(gift, giftStr, cmPromotion) {
if ($('#pTable_' + gift.shopId).length != 0) {
$('#pTable_' + gift.shopId + ' tbody').append(giftStr);
} else {
appendShopOrderHead(gift, giftStr, cmPromotion);
}
// 重新设置底部订单金额
setOrderFee();
}
/**
* 设置商品列表(二手商品)
* @param items
*/
function setSeondHandProductList(data) {
console.log(data);
data.productType = 0;
var productID = (data.productId == undefined) ? data.productID : data.productId;
//把表中存在的删除
var removeBeforeSoon = false; //是这一次移除
var removeEle;
$('.productTable tbody tr').each(function (index, element) {
if ($(this).find("td:nth-child(1)").html() == productID) {
removeEle = $(this).parent();
$(this).remove();
removeBeforeSoon = true;
}
});
var buyAgain = "否 | ";
var str = '';
var shopFee = data.shopFee;
str +=
"" +
"" + productID + " | " +
"" + data.shopId + " | " +
"" + data.shopName + " | " +
"" + data.name + " | " +
"" + "" + data.shopName + " | " +
"" + data.num + "(" + data.presentNum + ")" + " | " +
"" + data.price + " | " +
"" + data.discount + " | " +
"" + data.discountPrice + " | " +
"" + data.taxRate + " | " +
"" + data.addedValueTax + " | " +
"" + data.totalAddedValueTax + " | " +
"" + data.totalFee + " | " +
/*""+ Math.round(shopFee*100)/100 +" | "+*/
// 以下三个应付不显示,但是内容值参与计算并保存---关闭控制入口移动到财务管理中控制20181201
// ""+ data.shopFee+" | "+
// ""+ data.otherFee +" | "+
// ""+ data.cmFee +" | "+
buyAgain +
"" +
"设置 " +
"删除商品" +
" | " +
"
";
if (removeBeforeSoon) {
removeEle.append(str);
//走到这里 说明是修改
} else {
//这里就是新加了
//先看有没有这个 当前这个商品的供应商的table
if ($('#pTable_' + data.shopId).length != 0) {
$('#pTable_' + data.shopId + ' tbody').append(str);
} else {
var html = '' +
'' +
'' +
'' +
'
' +
'' +
"" +
'' +
'' +
'
' + '' +
'' +
'' +
'商品ID | ' +
' | ' +
' | ' +
'商品名称 | ' +
'供应商名称 | ' +
'数量(赠品数) | ' +
'单价 | ' +
'折扣 | ' +
'折后单价 | ' +
'税率 | ' +
'单个税费 | ' +
'税费 | ' +
'总价 | ' +
/*'应付供应商 | ' +*/
'能否复购 | ' +
'操作 | ' +
'
' +
'' +
'' + str + '
';
$('#product_index').before(html);
}
}
setShopOrderFee(data.shopId);
setOrderFee(); //设置底部订单金额
product_index++;
}
//设置订单金额信息
function setOrderFee() {
//不考虑经理折扣、运费,此时 商品总金额=订单总额=应付总额
var productTotalFee = 0, //商品总金额
productCount = 0, // 商品总数
presentCount = 0, //协商赠送商品数
giftCount = 0, //促销赠品数
orderTotalFee = 0, //订单总额
reducedPrice = 0, //促销满减
payTotalFee = 0, //应付总额
svipFullReduction = 0; //超级会员优惠
var couponPrice = $("#couponAmount").val() ? $("#couponAmount").val() * 1 : 0;
$('.productTable tbody tr:not(.gift)').each(function (index, element) {
var obj = JSON.parse($(this).attr("data-value"));
productTotalFee = ((Number(productTotalFee) * 100 + Number(obj.num * obj.price) * 100 + Number(obj
.totalAddedValueTax) * 100) / 100 + Number(obj.svipTaxReduction)).toFixed(2);
productCount = Number(productCount) + Number(obj.num);
presentCount = Number(presentCount) + Number(obj.presentNum);
orderTotalFee = ((Number(orderTotalFee) * 100 + Number(obj.totalFee) * 100) / 100).toFixed(2);
payTotalFee = ((Number(payTotalFee) * 100 + Number(obj.totalFee) * 100) / 100).toFixed(2);
svipFullReduction = ((Number(svipFullReduction) * 100 + Number(obj.svipReduction) * obj.num * 100) / 100).toFixed(2);
});
$('.productTable tbody tr.promotions').each(function (index, element) {
var obj = JSON.parse($(this).attr("data-value"));
giftCount = Number(giftCount) + Number(obj.num);
});
// 计算促销折扣
totalPromotions.forEach(function (item) {
console.log(totalPromotions);
if (item.mode == 2) {
var touchPrice = 0;
item.promotionProducts.forEach(function (product) {
touchPrice += (product.number * product.price + product.totalTax * 1);
});
if (touchPrice >= item.touchPrice) {
if (item.discount * 1 === 1) {
var b = Math.floor(touchPrice / item.touchPrice);
reducedPrice += (b * item.reducedPrice);
payTotalFee -= reducedPrice;
} else {
reducedPrice += item.reducedPrice;
payTotalFee -= item.reducedPrice;
}
}
}
});
// 优惠券折扣
if (couponPrice > 0) {
orderTotalFee -= couponPrice;
payTotalFee -= couponPrice;
}
$(".productTotalFee").text((productTotalFee * 1).toFixed(2));
$(".promotionFullReduction").text((reducedPrice * 1).toFixed(2));
$(".couponPrice").text((couponPrice * 1).toFixed(2));
$(".productCount").text(productCount);
$(".presentCount").text(presentCount + giftCount);
$(".presentCount1").text(presentCount + giftCount);
$("#presentCount").val(presentCount);
$("#promotionalGiftsCount").val(giftCount);
$(".orderTotalFee").text(orderTotalFee);
$(".payTotalFee").text(payTotalFee);
$(".svipFullReduction").text(svipFullReduction);
$("#svipFullReduction").val(svipFullReduction);
$("#discount").val('0.00');
setProductTotalFee(); //重新设置经理折扣
}
// 修改数量
function changenum(index) {
var $num = $("#num" + index);
var numVal = $num.val();
var $checkItem = $("#check-item" + index);
var orderProductStr = $($checkItem).val();
var obj = JSON.parse(orderProductStr);
if (numVal != null && numVal != "" && typeof (numVal) != "undefined") {
if (parseInt(numVal) + "" != numVal || parseInt(numVal) < 1) {
alertx("数量必须大于0,并且是一个整数");
$num.val(obj["num"]);
return;
}
obj["num"] = numVal;
setLadderPrice(obj, numVal, index);
var result = JSON.stringify(obj);
$($checkItem).val(result);
//重新计算税费
taxRateVal(index);
} else {
$num.val(obj["num"]);
alertx("您输入的数量有误,请重新输入");
}
}
//加减数量
function presentNum(index, type) {
var $num = $("#num" + index);
var numVal = $num.val();
if (numVal != null && numVal != "" && typeof (numVal) != "undefined") {
if (type == 'del') {
numVal = parseInt(numVal) - 1;
} else if (type == 'add') {
numVal = parseInt(numVal) + 1;
}
if (numVal < 1) {
alertx("商品数量不能小于0")
} else {
$num.val(numVal);
//重新设置选中json对象值
var $checkItem = $("#check-item" + index);
var orderProductStr = $($checkItem).val();
var obj = JSON.parse(orderProductStr);
obj["num"] = numVal;
setLadderPrice(obj, numVal, index);
var result = JSON.stringify(obj);
$($checkItem).val(result);
//重新计算税费
taxRateVal(index);
}
}
}
function setLadderPrice(obj, numVal, index) {
if (obj["ladderPriceFlag"] * 1 === 1) {
// debugger;
var ladderList = obj["ladderPriceList"];
var resultLadder = 0;
for (var i = 0; i < ladderList.length; i++) {
if (numVal >= ladderList[i]["buyNum"]) {
resultLadder = i;
}
}
obj["price"] = ladderList[resultLadder]["buyPrice"];
obj["discountPrice"] = ladderList[resultLadder]["buyPrice"];
$("#check-item" + index).parents("tr").find("#price" + index).val(ladderList[resultLadder]["buyPrice"]);
$("#check-item" + index).parents("tr").find("#priceLabel" + index).text(ladderList[resultLadder]["buyPrice"]);
$("#check-item" + index).parents("tr").find("#discountPrice" + index).val(ladderList[resultLadder]["buyPrice"]);
if (numVal < ladderList[0]["buyNum"]) {
var old = $("#oldPrice" + index).val();
$("#check-item" + index).parents("tr").find("#price" + index).val(old);
$("#check-item" + index).parents("tr").find("#priceLabel" + index).text(old);
$("#check-item" + index).parents("tr").find("#discountPrice" + index).val(old);
}
}
}
function showView(index) {
var $num = $("#num" + index);
//重置数量
$num.val(1);
alertx("二手仪器购买数量只能为1");
}
// 商品折扣比例
function discountPriceVal(index) {
var discount = $("#discount" + index).val(); //折扣
discount = Number(discount);
var $checkItem = $("#check-item" + index);
var orderProductStr = $($checkItem).val();
var obj = JSON.parse(orderProductStr);
if (!isNaN(discount)) {
if (discount.length > 8 || discount.length < 0) {
$("#discount" + index).val(obj["discount"]);
alertx("折扣最大为5位小数");
} else if (discount > 100 || discount < 0) {
$("#discount" + index).val(obj["discount"]);
alertx("输入的折扣有误")
} else {
var svipPriceFlag = $("#svipPriceFlag" + index).val();
var price;
if (1 == svipPriceFlag) {
price = Number($("#svipDiscountPrice" + index).text());// 拿到超级会员价
} else {
price = Number($("#price" + index).val()); //拿到单价
}
//折后单价=折扣*当前价格/100
var discountPrice = ((discount * 100000 * price) / (100 * 100000)).toFixed(2);
$("#discountPrice" + index).val(discountPrice);
$("#discount" + index).val(discount);
var $checkItem = eval('(' + $("#check-item" + index).val() + ')');
$checkItem["discount"] = discount + ""; //重新设置折扣
$checkItem["discountPrice"] = discountPrice + ""; //重新设置折扣单价
$("#check-item" + index).val(JSON.stringify($checkItem));
//重新计算税费
taxRateVal(index)
}
} else {
alertx("商品折扣只能输入数字");
$("#discount" + index).val(obj.discount);
}
}
//输入折扣单价重新计算折扣
function countDiscount(index) {
var svipPriceFlag = $("#svipPriceFlag" + index).val(); //是否使用超级会员价
var price;
if (1 == svipPriceFlag) {
price = Number($("#svipDiscountPrice" + index).text());// 拿到超级会员价
} else {
price = Number($("#price" + index).val()); //拿到单价
}
var discountPrice = Number($("#discountPrice" + index).val()); //拿到折后单价
//判断必须小于等于商品单价
if (discountPrice > price) {
alertx("折后单价不能大于当前单价");
$("#discountPrice" + index).val(price); //清楚输入值设置为原始单价
$("#discount" + index).val(100);
return;
}
var discount = ((discountPrice / price).toFixed(7) * 100);
//设置折扣
$("#discount" + index).val(discount);
//重新计算税费和总价
var $checkItem = $("#check-item" + index);
var orderProductStr = $($checkItem).val();
var obj = JSON.parse(orderProductStr);
if (!isNaN(discount)) {
if (discount > 100 || discount < 0) {
$("#discount" + index).val(obj["discount"]);
alertx("输入的折扣有误")
} else {
var $checkItem = eval('(' + $("#check-item" + index).val() + ')');
$checkItem["discount"] = discount + ""; //重新设置折扣
$checkItem["discountPrice"] = discountPrice + ""; //重新设置折扣单价
$("#check-item" + index).val(JSON.stringify($checkItem));
//重新计算税费
taxRateVal(index)
}
}
}
//计算税费
function taxRateVal(index) {
var $checkItem = eval('(' + $("#check-item" + index).val() + ')');
var taxRateLabel = Number($("#taxRateLabel").val());
var taxRate = isNaN(taxRateLabel) ? Number($("#taxRate" + index).val()) : taxRateLabel; //税率
var supplierTaxRate = Number($checkItem["supplierTaxRate"]); //供应商税率
var numVal = $("#num" + index).val(); //商品数量
var presentNum = $("#presentNum" + index).val(); //商品赠品数量-计算税费需要包含赠品
if (presentNum == "" || presentNum == "undefined") {
presentNum = 0;
$("#presentNum" + index).val(0);
}
var totalNumVal = numVal * 1 + presentNum * 1; //统计赠品和购买商品数量
var discountPrice = $("#discountPrice" + index).val(); //折后单价
var $checkItem = eval('(' + $("#check-item" + index).val() + ')');
if ($checkItem["name"] == '' || $checkItem["name"] == null || $checkItem["name"] == 'null' || typeof ($checkItem[
"name"]) == "undefined") {
$checkItem["name"] = $checkItem.purchaseProductName + "";
}
var mode = $("#mode" + index).val(); //是否参与优惠活动
var touchPrice = $("#touchPrice" + index).val(); //优惠价/满减满赠设定价
var reducedPrice = $("#reducedPrice" + index).val(); //减免价格
var name = $("#promotionName" + index).val(); //促销活动名称
var promotionsId = $("#promotionsId" + index).val(); //促销活动id
var discount = $("#discount" + index).val(); //百分比折扣
var productId = $checkItem["productId"]; //商品id 11
var includedTax = $checkItem["includedTax"] == undefined ? 2 : $checkItem["includedTax"]; //是否含税 11
var invoiceType = $checkItem["invoiceType"] == undefined ? 3 : $checkItem["invoiceType"]; //发票类型 11
var ctx = $("#ctx" + index).val(); //ctx
var costCheckFlag = $checkItem["costCheckFlag"] == undefined ? 1 : $checkItem[
"costCheckFlag"]; //成本价选中标志:1固定成本 2比例成 11
var costPrice = $checkItem["costPrice"] == undefined ? 0.0 : $checkItem["costPrice"]; //成本价 11
var shopPercent = $checkItem["shopPercent"] == undefined ? 90 : $checkItem[
"shopPercent"]; //比例成本百分比90% 11
var oldProductFlag = $checkItem["oldProductFlag"] == undefined ? 0 : $checkItem["oldProductFlag"]; //旧商品标志,0否1是
var obj1 = document.getElementById("priceLabel" + index); //选择商品页面商品单价
var obj2 = document.getElementById("price"); //设置商品页面商品单价
var price = obj1 != null ? obj1 : obj2 != null ? obj2 : null;
var svipDiscountPriceObj = document.getElementById("svipDiscountPrice" + index);
var svipReduction = $("#svipReduction" + index).val() == undefined ? 0 : $("#svipReduction" + index).val();
var svipPriceFlag = $("#svipPriceFlag" + index).val() == undefined ? 0 : $("#svipPriceFlag" + index).val();
var svipTaxReduction = $("#svipTaxReduction" + index).val() == undefined ? 0 : $("#svipTaxReduction" + index).val();
if (includedTax != '' && includedTax == 0 && invoiceType == 3) {
//不含税不能开票商品,税率置为0
taxRate = 0;
}
//税率校验
if (isNaN(taxRate) || taxRate < 0 || taxRate > 100) {
alertx("机构税率只能为[0,100]之间的数");
return;
}
if (isNaN(supplierTaxRate) || supplierTaxRate < 0 || supplierTaxRate > 100) {
alertx("供应商税率只能为[0,100]之间的数");
return;
}
// 含税商品以及不含税不能发票商品,不需要计算税费
if (includedTax == '1' || (includedTax == '0' && invoiceType == '3')) {
//计算总价(税费为0)=折后单价*数量
if (discount == 100 && mode == 1) {
var totalFee = parseFloat(((touchPrice * 100000 * numVal) / 100000).toFixed(2));
if (price != null) {
price.style.cssText = "text-decoration: line-through";
}
document.getElementById("discountPrice" + index).style.cssText =
"width: 80px;text-decoration: line-through";
} else {
var totalFee = parseFloat(((discountPrice * 100000 * numVal) / 100000).toFixed(2));
if (price != null) {
price.style.cssText = "";
}
document.getElementById("discountPrice" + index).style.cssText = "width: 80px;";
}
if (discount != 100) {
if (promotionsId != 0) {
$("#promotion" + index).html("");
} else {
$("#promotion" + index).html("");
}
$("#presentNum" + index).attr("readonly", null);
} else {
if (mode == 1) {
$("#promotion" + index).html(
"")
} else if (mode == 2) {
$("#promotion" + index).html(
"" + name + "\n" +
"满" + touchPrice + ",减" + reducedPrice + "");
} else if (mode == 3) {
$("#promotion" + index).html(
"" + name + "\n" +
"满" + touchPrice + ",赠送商品");
}
if (promotionsId != null && promotionsId != '' && promotionsId != 0) {
$("#presentNum" + index).val(0);
$("#presentNum" + index).attr("readonly", "readonly");
} else {
if (productId >= 6060 && productId <= 6069) {
$("#presentNum" + index).attr("readonly", "readonly");
} else {
$("#presentNum" + index).attr("readonly", null);
}
}
}
$("#totalFee" + index).val(totalFee);
$("#supplierTaxRate" + index).val(supplierTaxRate);
$checkItem["taxRate"] = taxRate + ""; //重新设置税率
$checkItem["supplierTaxRate"] = supplierTaxRate + ""; //重新设置供应商税率
$checkItem["num"] = numVal + ""; //重新设置数量
$checkItem["addedValueTax"] = 0; //重新设置单个机构税费
$checkItem["totalAddedValueTax"] = 0; //重新设置机构总税费
$checkItem["singleShouldPayTotalTax"] = 0; //重新设置单个供应商税费
$checkItem["shouldPayTotalTax"] = 0; //重新设置供应商总税费
$checkItem["totalFee"] = totalFee + ""; //重新设置总价
$checkItem["payTotalFee"] = totalFee + ""; //重新设置应付
$checkItem["costPrice"] = costPrice + ""; //重新设置成本
$checkItem["svipReduction"] = svipReduction + ""; //重新设置超级会员优惠
$("#svipReduction0").val(svipReduction);
$checkItem["svipTaxReduction"] = svipTaxReduction + ""; //重新设置超级会员税费优惠
$("#svipTaxReduction").val(svipTaxReduction);
// console.log("data" + index + ":" + JSON.stringify($checkItem));
$("#check-item" + index).val(JSON.stringify($checkItem));
} else {
//不含税可开发票商品,需要计算税费
if (taxRate == 0) {
//机构税率为0,机构税费则为0
$("#taxRate" + index).val(0);
$("#addedValueTax" + index).val(0); //设置单个税费
$("#totalAddedValueTax" + index).val(0); //设置总税费
//计算总价(税费为0)=折后单价*数量
if (discount == 100 && mode == 1) {
//参与单品优惠价
//计算总价
var totalFee = parseFloat(((touchPrice * 100000 * numVal) / 100000).toFixed(2));
//计算成本价
if (costCheckFlag == 2 && shopPercent != null) {
costPrice = parseFloat((((touchPrice * shopPercent) / 100).toFixed(2)));
}
if (price != null) {
price.style.cssText = "text-decoration: line-through";
}
document.getElementById("discountPrice" + index).style.cssText =
"width: 80px;text-decoration: line-through";
} else {
var totalFee = parseFloat(((discountPrice * 100000 * numVal) / 100000).toFixed(2));
//计算成本价
if (costCheckFlag == 2 && shopPercent != null) {
costPrice = parseFloat((((discountPrice * shopPercent) / 100).toFixed(2)));
}
if (price != null) {
price.style.cssText = "";
}
document.getElementById("discountPrice" + index).style.cssText = "width: 80px;";
}
if (discount != 100) {
if (promotionsId != 0) {
$("#promotion" + index).html("有折扣,不享受促销优惠");
} else {
$("#promotion" + index).html("");
}
$("#presentNum" + index).attr("readonly", null);
} else {
if (mode == 1) {
$("#promotion" + index).html(
"" + name + "\n" +
"优惠价:" + touchPrice + "")
} else if (mode == 2) {
$("#promotion" + index).html(
"" + name + "\n" +
"满" + touchPrice + ",减" + reducedPrice + "");
} else if (mode == 3) {
$("#promotion" + index).html(
"" + name + "\n" +
"满" + touchPrice + ",赠送商品");
}
if (promotionsId != null && promotionsId != '' && promotionsId != 0) {
$("#presentNum" + index).val(0);
$("#presentNum" + index).attr("readonly", "readonly");
} else {
if (productId >= 6060 && productId <= 6069) {
$("#presentNum" + index).attr("readonly", "readonly");
} else {
$("#presentNum" + index).attr("readonly", null);
}
}
}
var singleShouldPayTotalTax = 0;
var shouldPayTotalTax = 0;
if (supplierTaxRate > 0) {
//供应商税费(单) = 成本价 * 供应商税率 * 0.01
singleShouldPayTotalTax = (costPrice * supplierTaxRate * 0.01).toFixed(2);
//供应商税费(总) = 供应商税费(单)*数量
shouldPayTotalTax = (singleShouldPayTotalTax * numVal).toFixed(2);
}
$("#totalFee" + index).val(totalFee);
$("#supplierTaxRate" + index).val(supplierTaxRate);
$checkItem["taxRate"] = 0; //重新设置税率
$checkItem["supplierTaxRate"] = supplierTaxRate + ""; //重新设置供应商税率
$checkItem["num"] = numVal + ""; //重新设置数量
$checkItem["addedValueTax"] = 0; //重新设置单个税费
$checkItem["totalAddedValueTax"] = 0; //重新设置机构总税费
$checkItem["singleShouldPayTotalTax"] = singleShouldPayTotalTax + "" //重新设置供应商单个税费
$checkItem["shouldPayTotalTax"] = shouldPayTotalTax + ""; //重新设置供应商总税费
$checkItem["totalFee"] = totalFee + ""; //重新设置总价
$checkItem["payTotalFee"] = totalFee + ""; //重新设置应付
$checkItem["costPrice"] = costPrice + ""; //重新设置成本
$("#taxRateLabel").val(taxRate);
$("#addedValueTaxLabel").val(0)
$("#totalAddedValueTaxLabel").val(0)
if (svipPriceFlag == 1) {
// 折扣价重新计算超级会员优惠
var svipDiscountPrice = $("#svipDiscountPrice" + index).text();
var originalPrice = $("#price" + index).val();
svipReduction = originalPrice - svipDiscountPrice;
}
$checkItem["svipReduction"] = svipReduction + ""; //重新设置超级会员优惠
$("#svipReduction0").val(svipReduction);
$checkItem["svipTaxReduction"] = svipTaxReduction + ""; //重新设置超级会员税费优惠
$("#svipTaxReduction").val(svipTaxReduction);
// console.log("data" + index + ":" + JSON.stringify($checkItem));
$("#check-item" + index).val(JSON.stringify($checkItem));
} else if (taxRate > 100) {
alertx("税率不能大于100%");
$("#taxRate" + index).val($checkItem['taxRate']);
return;
} else {
// var r = /^\+?[1-9][0-9]*$/;
if ((typeof taxRate === 'number') && (taxRate <= 100)) {
//折扣为100且参与单品优惠活动时,根据优惠价计算数据
if (discount == 100 && mode == 1) {
$("#taxRate" + index).val(taxRate);
// if (r.test(taxRate) && taxRate <= 100) {
//单个税费=优惠价*税率【税率 (4舍5入保留2位小数)】
var addedValueTax = (touchPrice * taxRate * 0.01).toFixed(2);
//商品总额=折后单价*数量【[折后单价(真实值) x 数量](4舍5入保留2位小数)】
var totalTax = (touchPrice * numVal).toFixed(2);
//税费=商品总额*税率【税率:(4舍5入保留2位小数)】
var totalAddedValueTax = (totalTax * taxRate * 0.01).toFixed(2);
//计算成本价
if (costCheckFlag == 2 && shopPercent != null) {
costPrice = (((parseFloat(touchPrice) + parseFloat(addedValueTax)) * shopPercent) / 100)
.toFixed(2);
}
//计算总价(存在折扣)【总价=[折后单价 x 数量](4舍5入保留2位小数) + 税费 】
var totalFee = ((parseFloat((touchPrice * numVal).toFixed(2)) * 100 + parseFloat(
totalAddedValueTax) * 100) / 100).toFixed(2);
if (price != null) {
price.style.cssText = "text-decoration: line-through";
}
document.getElementById("discountPrice" + index).style.cssText =
"width: 80px;text-decoration: line-through";
} else {
$("#taxRate" + index).val(taxRate);
// if (r.test(taxRate) && taxRate <= 100) {
//单个税费=折后单价*税率【税率 (4舍5入保留2位小数)】
var addedValueTax = (discountPrice * taxRate * 0.01).toFixed(2);
//商品总额=折后单价*数量【[折后单价(真实值) x 数量](4舍5入保留2位小数)】
var totalTax = (discountPrice * numVal).toFixed(2);
//机构税费=商品总额*税率【税率:(4舍5入保留2位小数)】
var totalAddedValueTax = (totalTax * taxRate * 0.01).toFixed(2);
//计算成本价
if (costCheckFlag == 2 && shopPercent != null) {
costPrice = ((((parseFloat(discountPrice) + parseFloat(addedValueTax))) * shopPercent) /
100).toFixed(2);
}
//计算总价(存在折扣)【总价=[折后单价 x 数量](4舍5入保留2位小数) + 税费 】
var totalFee = ((parseFloat((discountPrice * numVal).toFixed(2)) * 100 + parseFloat(
totalAddedValueTax) * 100) / 100).toFixed(2);
$("#totalFee" + index).val(totalFee);
if (price != null) {
price.style.cssText = "";
}
document.getElementById("discountPrice" + index).style.cssText = "width: 80px;";
}
if (discount != 100) {
$("#promotion" + index).html("有折扣,不享受促销优惠")
$("#presentNum" + index).attr("readonly", null);
} else {
if (mode == 1) {
$("#promotion" + index).html(
"" + name +
"\n" +
"优惠价:" + touchPrice + "")
} else if (mode == 2) {
$("#promotion" + index).html(
"" + name +
"\n" +
"满" + touchPrice + ",减" + reducedPrice + "");
} else if (mode == 3) {
$("#promotion" + index).html(
"" + name +
"\n" +
"满" + touchPrice + ",赠送商品");
}
if (promotionsId != null && promotionsId != '' && promotionsId != 0) {
$("#presentNum" + index).val(0);
$("#presentNum" + index).attr("readonly", "readonly");
} else {
$("#presentNum" + index).attr("readonly", null);
}
}
var singleShouldPayTotalTax = 0;
var shouldPayTotalTax = 0;
//(未知商品/不含税可开发票的旧商品)将机构税率赋值给供应商税率
if (includedTax == '2' || includedTax == '' || (includedTax == '0' && (invoiceType == '1' ||
invoiceType == '2') && oldProductFlag == '1')) {
supplierTaxRate = taxRate;
}
if (supplierTaxRate > 0) {
//供应商税费(单) = 成本价 * 供应商税率 * 0.01
singleShouldPayTotalTax = (costPrice * supplierTaxRate * 0.01).toFixed(3);
//供应商税费(总) = 供应商税费(单)*数量
shouldPayTotalTax = (singleShouldPayTotalTax * numVal).toFixed(2);
}
if (svipPriceFlag == 1) {
// 折扣价重新计算超级会员优惠
var svipDiscountPrice = $("#svipDiscountPrice" + index).text();
var originalPrice = $("#price" + index).val();
svipReduction = ((originalPrice - svipDiscountPrice) + (originalPrice - svipDiscountPrice) * taxRate / 100).toFixed(2);
svipTaxReduction = ((originalPrice - svipDiscountPrice) * taxRate / 100).toFixed(2);
}
$checkItem["svipReduction"] = svipReduction + ""; //重新设置超级会员优惠
$("#svipReduction0").val(svipReduction);
$checkItem["svipTaxReduction"] = svipTaxReduction + ""; //重新设置超级会员税费优惠
$("#svipTaxReduction").val(svipTaxReduction);
$("#addedValueTax" + index).val(addedValueTax);
$("#totalAddedValueTax" + index).val(totalAddedValueTax);
$("#totalFee" + index).val(totalFee);
$("#supplierTaxRate" + index).val(supplierTaxRate);
$checkItem["num"] = numVal + ""; //重新设置数量
$checkItem["taxRate"] = taxRate + ""; //重新设置税率
$checkItem["supplierTaxRate"] = supplierTaxRate + ""; //重新设置供应商税率
$checkItem["addedValueTax"] = addedValueTax + ""; //重新设置单个税费
$checkItem["totalAddedValueTax"] = totalAddedValueTax + ""; //重新设置总税费
$checkItem["singleShouldPayTotalTax"] = singleShouldPayTotalTax + "" //重新设置供应商单个税费
$checkItem["shouldPayTotalTax"] = shouldPayTotalTax + ""; //重新设置供应商总税费
$checkItem["totalFee"] = totalFee + ""; //重新设置总价
$checkItem["payTotalFee"] = totalFee + ""; //重新设置应付
$checkItem["costPrice"] = costPrice + ""; //重新设置成本
$("#taxRateLabel").val(taxRate);
$("#addedValueTaxLabel").val(addedValueTax)
$("#totalAddedValueTaxLabel").val(totalAddedValueTax)
// console.log("data" + index + ":" + JSON.stringify($checkItem));
$("#check-item" + index).val(JSON.stringify($checkItem));
} else {
alertx("税率只能为[0,100]之间的数");
$("#taxRate" + index).val($checkItem['taxRate']);
return;
}
}
}
if (svipPriceFlag == 1) {
if (price != null) {
price.style.cssText = "text-decoration: line-through";
svipDiscountPriceObj.style.cssText = "display:''";
}
}
singeGiveNum(index);
shopFee(index); //优化功能开启刷新应付供应商
otherFee(index);
}
// 添加赠品数
function giveNum(i) {
var $giveNum = $("#presentNum" + i).val();
if ($giveNum == "" || $giveNum == "undefined") {
$giveNum = 0;
$("#presentNum" + i).val(0);
}
var giveNum = Number($giveNum);
var $checkItem = eval('(' + $("#check-item" + i).val() + ')');
if (!isNaN(giveNum)) {
if ((typeof giveNum === 'number') && (giveNum % 1 === 0)) {
$checkItem["presentNum"] = giveNum + ""; //重新设置赠品数
$("#presentNum" + i).val(giveNum);
$("#check-item" + i).val(JSON.stringify($checkItem));
if (0 != giveNum) {
cmFee(i);
}
//重新税费(因为赠品也需要计算税费)
taxRateVal(i);
} else {
alertx("赠品数只能输入整数");
$("#presentNum" + i).val($checkItem.presentNum);
}
} else {
alertx("赠品数只能输入数字");
$("#presentNum" + i).val($checkItem.presentNum);
}
}
// 单纯获取赠品数(不计算税费)
function singeGiveNum(i) {
var $giveNum = $("#presentNum" + i).val();
if ($giveNum == null || $giveNum == "" || $giveNum == "undefined") {
$giveNum = 0;
$("#presentNum" + i).val(0);
}
var giveNum = Number($giveNum);
var $checkItem = eval('(' + $("#check-item" + i).val() + ')');
if (giveNum != 'undefined' && !isNaN(giveNum)) {
if ((typeof giveNum === 'number') && (giveNum % 1 === 0)) {
$checkItem["presentNum"] = giveNum + ""; //重新设置赠品数
$("#presentNum" + i).val(giveNum);
$("#check-item" + i).val(JSON.stringify($checkItem));
if (0 != giveNum) {
cmFee(i);
}
} else {
alertx("赠品数只能输入整数");
$("#presentNum" + i).val($checkItem.presentNum);
}
} else {
alertx("赠品数只能输入数字");
$("#presentNum" + i).val($checkItem.presentNum);
}
}
/**
* @param obj
* jquery控制input只能输入数字和两位小数
*/
function num(obj) {
obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
}
// 添加供应应付商金额
function shopFee(i) {
var $shopFee = $("#shopFee1" + i).val();
var shopFee = Number($shopFee);
var $checkItem = eval('(' + $("#check-item" + i).val() + ')');
if (!isNaN(shopFee)) {
$checkItem["shopFee1"] = shopFee + "";
$("#shopFee1" + i).val(shopFee);
$("#check-item" + i).val(JSON.stringify($checkItem));
cmFee(i);
} else {
alertx("应付供应商只能输入数字");
$("#shopFee1" + i).val($checkItem.shopFee1);
}
}
// 添加复购属性
function buyAgain(i) {
var options = $("#buyAgainFlag" + i + " option:selected");
var buyAgainFlag = options.val();
if (null != buyAgainFlag && undefined != buyAgainFlag) {
var $checkItem = eval('(' + $("#check-item" + i).val() + ')');
$checkItem["buyAgainFlag"] = buyAgainFlag + "";
$("#buyAgainFlag" + i).val(buyAgainFlag);
$("#check-item" + i).val(JSON.stringify($checkItem));
} else {
var $checkItem = eval('(' + $("#check-item" + i).val() + ')');
$checkItem["buyAgainFlag"] = "1";
$("#buyAgainFlag" + i).val(buyAgainFlag);
$("#check-item" + i).val(JSON.stringify($checkItem));
}
}
// 添加第三方应付商金额
function otherFee(i) {
var $otherFee = $("#otherFee1" + i).val();
var otherFee = Number($otherFee);
var $checkItem = eval('(' + $("#check-item" + i).val() + ')');
if (!isNaN(otherFee)) {
$checkItem["otherFee1"] = otherFee + "";
$("#otherFee1" + i).val(otherFee);
$("#check-item" + i).val(JSON.stringify($checkItem));
cmFee(i);
} else {
alertx("应付第三方只能输入数字");
$("#otherFee1" + i).val($checkItem.otherFee1);
}
}
// 添加采美应付金额
function cmFee(i) {
var discountPrice = $("#discountPrice" + i).val(); //折后单价
var shopFee1 = $("#shopFee1" + i).val(); //应付供应商
var otherFee1 = $("#otherFee1" + i).val() //应付第三方
if ((shopFee1 != "" && shopFee1 != null) && (otherFee1 != "" && otherFee1 != null)) {
var shopFee = Number(shopFee1);
var otherFee = Number(otherFee1);
var $checkItem = eval('(' + $("#check-item" + i).val() + ')');
var num = parseInt($checkItem.num);
var presentNum = parseInt($checkItem.presentNum);
var cmFee = ((discountPrice * 100000 - shopFee * 100000 - otherFee * 100000) / 100000).toFixed(6);
$("#cmFee1" + i).val(cmFee);
$checkItem["cmFee1"] = cmFee + "";
//设置总的应付
var _shopFee = (shopFee * (num + presentNum)).toFixed(6); //供应商
var _otherFee = (otherFee * num).toFixed(6); //供应商
var _cmFee = ((discountPrice * num * 100000 - _shopFee * 100000 - _otherFee * 100000) / 100000).toFixed(6);
$checkItem["shopFee"] = _shopFee + "";
$checkItem["otherFee"] = _otherFee + "";
$checkItem["cmFee"] = _cmFee + "";
$("#check-item" + i).val(JSON.stringify($checkItem));
}
}
//经理折扣后计算
function setProductTotalFee() {
var discount = Number($("#discount").val()); //经理折扣金额
if (isNaN(discount)) {
discount = Number($("#discountFee").val());
}
if ($('.productTable').length == 0) {
// 非添加订单页面
$("#discount").val('0.00');
$(".payableAmount").text(0);
return;
}
var trs = $('.productTable tbody').find('tr:not(.gift)');
if (trs.length == 0) { //没有添加商品的情况
$("#discount").val('0.00');
$(".payableAmount").text(0);
alertx("请先添加商品");
return;
}
//经理折扣 -> 改变订单总额 -> 改变分期免息金额
var productTotalFee = Number($(".productTotalFee").html()), //商品总金额
productCount = Number($(".productCount").html()), // 订单商品总数
presentCount = Number($("#presentCount").val()), //赠送商品数
giftCount = Number($("#promotionalGiftsCount").val()), //促销赠品
orderTotalFee = Number($(".orderTotalFee").html()), //订单总额
reducedPrice = 0, //促销满减
payTotalFee = ((orderTotalFee * 100 - discount * 100) / 100).toFixed(2); //应付总额=订单总额-折扣金额-减抵扣余额(待开发)
if (payTotalFee < 0) {
$("#discount").attr('value', '0');
$("#discountFee").attr('value', '0');
alertx("订单金额不能小于0");
setProductTotalFee();
return;
}
//计算运费
var freePostFlag = $("#freePostFlag").val();
var freight = 0;
if (freePostFlag == "1") {
freight = Number($("#freight").val());
}
payTotalFee = (Number(payTotalFee) + freight).toFixed(2);
// 计算促销折扣
totalPromotions.forEach(function (item) {
if (item.mode == 2) {
var touchPrice = 0;
item.promotionProducts.forEach(function (product) {
touchPrice += (product.number * product.price + product.totalTax * 1);
});
if (touchPrice >= item.touchPrice) {
if (item.discount * 1 === 1) {
var b = Math.floor(touchPrice / item.touchPrice);
reducedPrice += (b * item.reducedPrice);
payTotalFee -= reducedPrice;
} else {
reducedPrice += item.reducedPrice;
payTotalFee -= item.reducedPrice;
}
}
}
});
//计算余额抵扣
//计算应付金额
var payableAmount = payTotalFee; //应付订单金额
var balancePayFee = Number($("#balancePayFee").val()); //当前使用金额
var surplusBalance = 0.00; //剩余金额
var availableBalanceStatus = $("#useBalancePayFee").is(':checked'); //账户余额
var availableBalance = Number($("#availableBalance").html());
if (availableBalanceStatus == true) {
if ((availableBalance + balancePayFee - payTotalFee).toFixed(2) > 0) { //余额大于订单总额 全额抵扣
surplusBalance = (Number(availableBalance) + balancePayFee - Number(payTotalFee)).toFixed(2);
balancePayFee = payTotalFee;
payableAmount = 0.00;
availableBalance = surplusBalance;
} else {
payableAmount = (Number(payTotalFee) - Number(availableBalance) - balancePayFee).toFixed(2);
balancePayFee = (Number(availableBalance) + balancePayFee).toFixed(2);
surplusBalance = 0.00;
availableBalance = surplusBalance;
}
} else {
availableBalance = availableBalance + balancePayFee;
surplusBalance = availableBalance;
balancePayFee = 0.00;
}
trs.each(function (index, element) {
var obj = JSON.parse($(this).attr("data-value"));
var _taxRate = Number(obj.taxRate); //税率
var _totalFee = Number(obj.totalFee); //总价
var _shopFee1 = isNaN(Number(obj.shopFee1)) ? Number(obj.singleShopFee) : Number(obj.shopFee1);
var _shopFee = (_shopFee1 * (Number(obj.num) + Number(obj.presentNum))).toFixed(6); //供应商
var _otherFee1 = isNaN(Number(obj.otherFee1)) ? Number(obj.singleOtherFee) : Number(obj.otherFee1);
var _otherFee = (_otherFee1 * Number(obj.num)).toFixed(6); //第三方
//采美
var _cmFee = ((Number(obj.discountPrice) * Number(obj.num) * 100000 - _shopFee * 100000 - _otherFee *
100000) / 100000).toFixed(6);
//订单的单个商品 应付金额= 单个总价 。(四舍五入后,保留两位小数,--此处不平摊经理折扣)
var _payTotalFee = (Number(obj.totalFee)).toFixed(2);
//【旧】订单的单个商品 税费=订单的单个商品应付金额-[订单的单个商品应付金额/(1+税率)](最后保留2位小数+四舍五入)
//var _totalAddedValueTax=((_payTotalFee*100000-((_payTotalFee/((1+_taxRate/100).toFixed(6))).toFixed(6))*100000)/100000).toFixed(2);
var _totalAddedValueTax = obj.totalAddedValueTax;
//差额(单个商品的经理折扣)=总价-应付金额 如果没有特别要求默认保留6位小数(此处结果为0)
var _difAmount = ((_totalFee * 100000 - _payTotalFee * 100000) / 100000).toFixed(6);
//总盈利(经理折扣后)=第三方+采美-差额
var _totalProfit = ((_otherFee * 100000 + _cmFee * 100000 - _difAmount * 100000) / 100000).toFixed(6);
//盈利比=总盈利÷(第三方+采美)
var _profitability = 0;
if (_otherFee != 0 && _cmFee != 0) {
_profitability = (_totalProfit / ((_otherFee * 100000 + _cmFee * 100000) / 100000).toFixed(6))
.toFixed(6);
}
//订单的每种商品 应付第三方(经理折扣后)=盈利比x第三方(经理折扣前)
_otherFee = (_profitability * _otherFee).toFixed(6);
//订单的单个商品 应付采美=应付金额-应付供应商-应付第三方-税费
_cmFee = ((_payTotalFee * 100000 - _shopFee * 100000 - _otherFee * 100000 - _totalAddedValueTax *
100000) / 100000).toFixed(6);
obj["shopFee"] = _shopFee + "";
obj["otherFee"] = _otherFee + "";
obj["cmFee"] = _cmFee + "";
obj["shouldPayFee"] = _payTotalFee + "";
obj["presentNum"] = obj.presentNum + "";
if (obj["name"] == '' || obj["name"] == null || obj["name"] == 'null' || typeof (obj["name"]) ==
"undefined") {
obj["name"] = obj.purchaseProductName + "";
}
$(this).attr("data-value", JSON.stringify(obj));
var index = $(this).attr("data-index");
$('#orderProduct' + index + '\\.enabledStatus').val(JSON.stringify(obj));
});
if (initFlag) {
$(".productTotalFee").html(productTotalFee);
$(".productCount").html(productCount);
$(".presentCount").text(presentCount + giftCount);
$(".orderTotalFee").html(orderTotalFee);
$(".payTotalFee").html((payTotalFee * 1).toFixed(2));
$(".promotionFullReduction").text(reducedPrice);
$(".payableAmount").html((payableAmount * 1).toFixed(2));
$("#balancePayFee").val(balancePayFee);
$(".balancePayFee").html(balancePayFee);
$("#surplusBalance").html(surplusBalance);
$("#availableBalance").html(availableBalance);
$("#productTotalFee").val(productTotalFee);
$("#promotionFullReduction").val(reducedPrice);
$("#productCount").val(productCount);
$("#presentCount").val(presentCount);
$("#promotionalGiftsCount").val(giftCount);
$("#orderTotalFee").val(orderTotalFee);
$("#payTotalFee").val(payTotalFee);
$("#inputForm").find("[name*='bpOrderUserinfo.ableUserMoney']").val(availableBalance);
$("#payableAmount").val(payableAmount);
}
initStagesFee(); //初始化分期相关金额
}
// 展示商品促销活动详情
function showPromotionDetail(iframe, title) {
top.$.jBox(iframe, {
iframeScrolling: 'yes',
width: $(top.document).width() - 800,
height: $(top.document).height() - 200,
persistent: true,
title: title,
buttons: {
"关闭": '-1'
},
submit: function (v, h, f) {
//确定
var $jboxFrame = top.$('#jbox-iframe');
var $mainFrame = top.$('#mainFrame');
if ('1' == v && 1 == $jboxFrame.size() && 1 == $mainFrame.size()) {
var items = $jboxFrame[0].contentWindow.getCheckedItems();
if (!items) {
return false;
} else if (items.length > 0) {
console.log($('.productTable'));
$(('.producttable')).remove();
setProductList(items[0], false); //设置商品列表
// initStages();//初始化分期信息
return true;
} else {
top.$.jBox.tip("请选择...");
return false;
}
}
},
});
}
/**
* 改变返佣标识
*/
function updateRebateFlag() {
var rebateFlag = $("#rebateFlag").val() * 1;
console.log(rebateFlag);
if (1 === rebateFlag) {
// $("#rebateFlag").val(1);
//选中返佣订单后不可用优惠券
// $("#couponBox").find("input[name=clubCouponId]").prop('checked', 'checked');
checkRebateFee(rebateFlag);
$("#couponBox").find("input[name=clubCouponId]").prop('checked', '');
$("#couponBox").find("input[name=clubCouponId]").attr("disabled", true);
$("#couponBox").find("#bsy").prop('checked', 'checked');
$("#freePostFlag").attr("value", "-1");
$("#freeDesc").find(".select2-chosen").text("到付");
$("#useBalancePayFee").prop('checked', '');
setFreight();
$("#freePostFlag").attr("disabled", true);
$("#useBalancePayFee").attr("disabled", true);
$("#couponAmount").val(0);
$("#seen").find("#orderSeen2").prop('checked', 'checked');
setOrderFee();
} else {
// $("#rebateFlag").val(0);
// $("#couponBox").find("input[name=clubCouponId]").prop('checked', '');
checkRebateFee(rebateFlag);
$("#couponBox").find("input[name=clubCouponId]").prop('checked', '');
$("#couponBox").find("input[name=clubCouponId]").attr("disabled", false);
$("#freePostFlag").attr("value", "0");
$("#freeDesc").find(".select2-chosen").text("包邮");
$("#useBalancePayFee").prop('checked', '');
setFreight();
$("#freePostFlag").attr("disabled", false);
$("#useBalancePayFee").attr("disabled", false);
$("#couponAmount").val(0);
$("#seen").find("#orderSeen1").prop('checked', 'checked');
// 重新计算总价
setOrderFee();
}
}
function updateRebateFlags() {
var rebateFlag = $("#rebateCheckBox").is(':checked');
if (rebateFlag) {
$("#rebateFlag").val(1);
//选中返佣订单后不可用优惠券
// $("#couponBox").find("input[name=clubCouponId]").prop('checked', 'checked');
$("#couponBox").find("input[name=clubCouponId]").prop('checked', '');
$("#couponBox").find("input[name=clubCouponId]").attr("disabled", true);
$("#couponBox").find("#bsy").prop('checked', 'checked');
$("#freePostFlag").attr("value", "-1");
$("#freeDesc").find(".select2-chosen").text("到付");
$("#useBalancePayFee").prop('checked', '');
setFreight();
$("#freePostFlag").attr("disabled", true);
$("#useBalancePayFee").attr("disabled", true);
$("#couponAmount").val(0);
$("#seen").find("#orderSeen2").prop('checked', 'checked');
setOrderFee();
} else {
$("#rebateFlag").val(0);
// $("#couponBox").find("input[name=clubCouponId]").prop('checked', '');
$("#couponBox").find("input[name=clubCouponId]").prop('checked', '');
$("#couponBox").find("input[name=clubCouponId]").attr("disabled", false);
$("#freePostFlag").attr("value", "0");
$("#freeDesc").find(".select2-chosen").text("包邮");
$("#useBalancePayFee").prop('checked', '');
setFreight();
$("#freePostFlag").attr("disabled", false);
$("#useBalancePayFee").attr("disabled", false);
$("#couponAmount").val(0);
$("#seen").find("#orderSeen1").prop('checked', 'checked');
// 重新计算总价
setOrderFee();
}
}
function checkRebateFee(rebateFlag) {
if (2 === rebateFlag) {
$("#rebateFee").show();
} else {
$("#rebateFee").hide();
}
}
/**
* 修改返佣选项是否可见
*/
function showRebateCheck() {
var length = $(".productTable").length;
var rebateDiv = document.getElementById("rebateDiv");
if (rebateDiv != null) {
if (length != 0 && length != 1) {
$("#rebateCheckBox").attr("checked", false);
$("#rebateFlag").val(0);
rebateDiv.style.cssText = "display:none";
} else {
rebateDiv.style.cssText = "";
}
}
}
/************************商品相关 end**********************************************************/