ladder_calculate.js
9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var regeneratorRuntime = require('../../../utils/runtime.js');
module.exports = {
//主要的作用,就是把阶梯促销多余的商品进行拆分,
//如果没有达成阶梯促销的要求,所有的商品都打回原价购买
fir_set_arr: function (c_item, th) {
//寻找一下
function get_num2(ite) {
var vh = c_item.goods.findIndex(function (e) {
return e.goods_id == ite.goods_id
})
if (vh > -1) return vh;
return 0;
}
//组合活动,以及组合活动从表的商品
let ladder_map = c_item.ladder_map;
let goods = c_item.goods; //一个门店中所有的商品
let tfeel = 0, cut_price = 0, offline_price = 0, offline_num = 0;
//多个活动要拿来循环一下
for (let i in ladder_map) {
let title = ""; //提示语;
//其中的一个活动
let act_item = ladder_map[i];
let ladder_list = act_item.ladder_list;
let all_num = 0; //商品数量之和
let all_price0 = 0; //商品数量之和
let aprice = 0; //达到活动条件的购买价格
let no_in_arr = []; //剩余的未加入组合购
let del_g=[];
for (var li in goods) {
if (goods[li].prom_type != 10) continue;
if (goods[li].prom_id != act_item.id) continue;
var item = goods[li];
item.goods_price = item.goods_price;
item.offline_price = item.offline_price;
item.cart_num = item.goods_num;
all_num += item.cart_num;
for (var j = 0; j < item.cart_num; j++) {
no_in_arr.push({
price: item.goods_price,
offline_price: item.offline_price,
goods_id: item.goods_id,
shop_price: item.shop_price,
item:item
})
}
del_g.push(item);
}
function sortData(a, b) {
return a.price - b.price
}
no_in_arr.sort(sortData);
//开始阶梯计算价格
var lev = 0;
for (let j in ladder_list) {
if (no_in_arr.length == 0) break;
let item_j =ladder_list[j];
var end = no_in_arr.pop();
var new_price = parseInt(item_j.discount_field) == 1 ? end.shop_price : end.price;
var account=new_price * parseFloat(item_j.discount) / 10;
aprice +=account;
//-- 单个文件新增 --
let new_g = JSON.parse(JSON.stringify(end.item));
new_g.goods_num = 1;
//如果有平摊到单品的时候,account要显示
if(!th.data.ispt_goods) new_g.account = account.toFixed(2);
new_g.discount = item_j.discount;
new_g.goods_price =new_price;
new_g.ladder_list_id =item_j.id;
goods.push(new_g);
lev++;
}
let goods_map = {};
//算一下剩余的商品
if (no_in_arr.length) {
for (let ii in no_in_arr) {
let item = no_in_arr[ii];
if (item.offline_price) {
offline_price += item.price - item.offline_price;
offline_num += 1;
}
if (goods_map[item.goods_id]) {
var num = goods_map[item.goods_id];
goods_map[item.goods_id] = ++num;
} else {
goods_map[item.goods_id] = 1;
}
}
}
if (Object.keys(goods_map).length) {
//这里就开始拆分提交订单时的列表
for (let j in goods_map) {
//map中存的就是商品的数量
let num = goods_map[j];
//找到相应的商品
let idx = goods.findIndex(function (ele) {
return ele.goods_id == j;
})
//goods[idx].goods_num -= num;
let new_g = JSON.parse(JSON.stringify(goods[idx]));
new_g.goods_num = num;
new_g.prom_type = 0;
new_g.prom_id = 0;
goods.push(new_g);
//goods.splice(idx, 1);
}
}
//-- 最后剔除不要参与的数据 --
for(var h in del_g){
var it=del_g[h];
let idx = goods.findIndex(function (ele) {
return ele.goods_id == it.goods_id
})
goods.splice(idx, 1);
}
//-- 只统计是参与活动的商品 --
for (var ij in goods) {
var iter = goods[ij];
if (iter.prom_type != 10 || iter.prom_id != act_item.id) {
continue;
}
all_price0 += iter.goods_num * iter.goods_price;
}
if(!c_item.ladder_prom_goods) c_item.ladder_prom_goods={};
if(!c_item.ladder_prom_goods[act_item.id]) c_item.ladder_prom_goods[act_item.id]={};
//-- 活动的条件已经满足 --
c_item.ladder_prom_goods[act_item.id].is_has_ladder = 1;
c_item.is_has_ladder = 1;
c_item.ladder_prom_goods[act_item.id].actual_price = aprice;
c_item.ladder_prom_goods[act_item.id].cut_price = all_price0 - aprice;
tfeel += aprice;
cut_price += all_price0 - aprice;
}
c_item.ladder_all_price = tfeel;
c_item.ladder_cut_price = cut_price;
if (c_item.is_offline == 1) {
c_item.offline_price += offline_price;
c_item.offline_num += offline_num;
} else {
c_item.is_offline == 1;
c_item.offline_price = offline_price;
c_item.offline_num = offline_num;
}
},
//筛选组合购,纯粹的数组按活动id分组
find_split: function (arr) {
//过滤只有团购的商品
let oarr = arr.goods.filter(function (ele) {
return ele.prom_type == 10
})
if (!oarr || oarr.length == 0) return null;
//看一下有多少个不同的团购
let map = {},
dest = [];
for (let i = 0; i < oarr.length; i++) {
let ai = oarr[i];
let index = map[ai.prom_id]
if (!index) {
dest.push({
prom_id: ai.prom_id,
act: ai.act,
data: [ai]
});
map[ai.prom_id] = dest.length; //存储下标
} else {
let dj = dest[index - 1];
dj.data.push(ai);
}
}
return dest;
},
//-- 组装阶梯购的价格 --
calc_split_price: async function (c_arr, th) {
//-- 循环处理 --
for (var k in c_arr) {
var cart_item = c_arr[k]; //就是每一单的意思
var ord_goods = c_arr[k].goods; //就是每一单的从表的意思
var ladder_map = cart_item.ladder_prom_goods; //一单中所有的组合购的集合
var item_map = {};
for (let i in ladder_map) {
let item = ladder_map[i];
let prom_id = i;
//如果这个活动还没有达到要求,继续下一个计算
if (!item.is_has_ladder) continue;
//过滤出相应组合购活动的商品
let glist = ord_goods.filter(function (ele) {
return ele.prom_type == 10 && ele.prom_id == i;
})
var all_good_price = 0;
var post_gd = [];
for (let j in glist) {
let item = glist[j];
all_good_price += item.goods_price * item.goods_num;
var gitem = {
goods_id: item.goods_id,
goods_num: item.goods_num,
goods_price: item.goods_price
}
post_gd.push(gitem);
}
//要进行优惠的计算
if (all_good_price - item.actual_price) {
if (cart_item.prom_pt_json) {
cart_item.prom_pt_json.push({
"ladder_prom_id": prom_id,
"dis": (all_good_price - item.actual_price).toFixed(2),
"ispt": 0
})
} else {
cart_item.prom_pt_json = [{
"ladder_prom_id": prom_id,
"dis": (all_good_price - item.actual_price).toFixed(2),
"ispt": 0
}];
}
}
}
}
}
}