zh_calculate.js
22.7 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
var regeneratorRuntime = require('../../../utils/runtime.js');
module.exports = {
//主要的作用,就是把组合购多余的商品进行拆分,
//如果没有达成组合购的要求,所有的商品都打回原价购买
fir_set_arr: function (c_item, th) {
//组合活动,以及组合活动从表的商品
let zh_prom_goods = c_item.zh_prom_goods;
let goods = c_item.goods; //一个门店中所有的商品
let tfeel = 0,
cut_price = 0,
offline_price = 0,
offline_num = 0;
let txt_ite = c_item.need_list;
//多个活动要拿来循环一下
for (let i in zh_prom_goods) {
let title = ""; //提示语;
//其中的一个活动
let act_item = zh_prom_goods[i];
let act_goos = act_item.gdlist;
let act = act_item.act;
if (!act) continue;
let all_num = 0; //商品数量之和
let all_price0 = 0; //商品数量之和
let aprice = 0; //达到活动条件的购买价格
let need_to_buy = 0;
let all_zhqty = 0; //所有商品要求起购数之和
let no_in_arr = []; //剩余的未加入组合购
let out_arr = []; //超出活动限购的商品放入
let zhqty_bz = []; //超量
//-- 寻找一下 --
function get_num2(ite) {
for (let v2 in goods) {
var vh = goods[v2];
if (vh.goods_id == ite.goods_id) {
return vh;
}
}
return 0;
}
for (let j in act_goos) {
let item = act_goos[j];
let item_j = goods.find(function (ele) {
console.log('ele', ele);
return ele.goods_id == act_goos[j].goods_id;
})
//-- 如果有找到的话 --
if (item_j) {
act_goos[j].num = item_j.goods_num;
act_goos[j].goods_price = item_j.goods_price;
var cart_num = item.cart_num = item_j.goods_num;
var zh_b_num = item_j.promgoodsbuynum;
if (item.buyqty > 0) {
var key = item.goods_id + "" + act.id;
var cbuy = item.buyqty - zh_b_num;
if (th.data.in_zhact_gdmap[key]) {
var cbuy = item.buyqty - zh_b_num - th.data.in_zhact_gdmap[key];
}
//当可买的数量为0
if (cbuy <= 0) {
item.num = 0;
out_arr.push({
price: item.goods_price,
offline_price: item.offline_price,
goods_id: item.goods_id,
num: cart_num
})
} else {
if (cbuy >= cart_num) item.num = cart_num;
else {
item.num = cbuy;
out_arr.push({
price: item.goods_price,
offline_price: item.offline_price,
goods_id: item.goods_id,
num: cart_num - cbuy
})
}
}
}
all_num += item.num;
//当有起购数的控制的时候
if (item.zhqty) {
zhqty_bz.push(item);
all_zhqty += item.zhqty;
if (item.num < item.zhqty) {
need_to_buy += item.zhqty - item.num;
}
if (item.num > item.zhqty) {
for (let i = 0; i < item.num - item.zhqty; i++) {
no_in_arr.push({
price: item.goods_price,
goods_id: item.goods_id,
offline_price: item_j.offline_price
})
}
}
} else {
for (let j = 0; j < item.num; j++) {
no_in_arr.push({
price: item.goods_price,
goods_id: item.goods_id,
offline_price: item_j.offline_price
})
}
}
} else {
if (item.zhqty) {
title = "必买商品未加购,下单立享【" + act.zhprice + "元任选" + act.zhbuyqty + "件】";
need_to_buy += item.zhqty;
}
}
}
//当满足组合的要求:总数要满足,起购数要满足
if (act && all_num >= act.zhbuyqty && !need_to_buy) {
//商品的列表
for (var u in act_goos) {
var ite = act_goos[u];
if (ite.buyqty > 0 && ite.num > 0) {
var key = ite.goods_id + "" + act.id;
if (th.data.in_zhact_gdmap[key]) {
th.data.in_zhact_gdmap[key] += ite.num;
} else {
th.data.in_zhact_gdmap[key] = ite.num;
}
}
}
function sortData(a, b) {
return a.price - b.price
}
if (no_in_arr.length > 0) no_in_arr.sort(sortData);
aprice = act.zhprice;
if (act.zhbuyqty > all_zhqty) {
for (let n = 0; n < act.zhbuyqty - all_zhqty; n++) {
no_in_arr.pop();
}
}
//看一下剩下的数量有没有满足组合购的要求,以及要不要倍增
if (act.is_bz && no_in_arr.length >= act.zhbuyqty) {
var bz_num_ok = 0; //超量倍增是否满足
var zhqty_len = 0; //几个超量倍增
var delete_num=0;
let be = parseInt(no_in_arr.length / act.zhbuyqty); //看一下是几倍
if (act.is_bzyh && zhqty_bz.length > 0) {
if (zhqty_bz.length > 1) {
let zhqty_bz_arr = [];
let zhqty_bz_flag = zhqty_bz.every((item1, i) => {
var bz_num = be * item1['zhqty']; //超量倍增
var num = item1['num'] - item1['zhqty']; //购买数量减去超量
if (bz_num > num) {
zhqty_bz_arr.push(item1);
zhqty_bz.splice(i, 1)
}
return bz_num <= num;
})
if (zhqty_bz_flag) {
for (let i = 0; i < zhqty_bz.length; i++) {
var vv = zhqty_bz[i];
for (let j = 0; j < be * vv['zhqty']; j++) {
let index = no_in_arr.findIndex(i => {
return vv.goods_id === i.goods_id
})
if (index > -1) {
delete_num++
no_in_arr.splice(index, 1)
}
}
}
} else {
let min_bz_num = Math.min.apply(Math, zhqty_bz_arr.map(function (o) {
return o['num']
}));
let new_arr = zhqty_bz_arr.filter(ii => {
return ii['num'] == min_bz_num
})
var vv = new_arr[0];
var bz_num = be * new_arr[0].zhqty; //超量倍增
var num = min_bz_num - new_arr[0].zhqty; //购买数量减去超量
bz_num_ok = bz_num - num;
if (bz_num_ok <= 0) {
//超量倍增满足,超量倍增就等于倍数
bz_num_ok = bz_num;
} else {
//超量倍增不满足,倍数要减去多出得
// be=be-bz_num_ok;
if (num % vv.zhqty == 0) {
be = num / vv.zhqty;
} else {
be = Math.floor(num / vv.zhqty)
}
bz_num_ok = be * vv.zhqty;
}
for (let j = 0; j < bz_num_ok; j++) {
let index = no_in_arr.findIndex(i => {
return vv.goods_id === i.goods_id
})
if (index > -1) {
delete_num++
no_in_arr.splice(index, 1)
}
}
}
} else {
var vv = zhqty_bz[0];
var bz_num = be * vv.zhqty; //超量倍增
var num = vv['num'] - vv.zhqty; //购买数量减去超量
if (num > 0) {
bz_num_ok = bz_num - num;
if (bz_num_ok <= 0) {
//超量倍增满足,超量倍增就等于倍数
bz_num_ok = bz_num;
} else {
//超量倍增不满足,倍数要减去多出得
// be=be-bz_num_ok;
if (num % vv.zhqty == 0) {
be = num / vv.zhqty;
// bz_num_ok = num;
} else {
be = Math.floor(num / vv.zhqty)
// bz_num_ok = num - (be * vv.zhqty);
}
bz_num_ok = be * vv.zhqty;
}
// be=vv.num;
for (let j = 0; j < bz_num_ok; j++) {
let index = no_in_arr.findIndex(i => {
return vv.goods_id === i.goods_id
})
if(index > -1){
delete_num++
no_in_arr.splice(index, 1)
}
}
} else {
zhqty_len = 1;
}
}
}
if (!zhqty_len) {
aprice += be * act.zhprice;
let pop_num = be * act.zhbuyqty - delete_num;
for (var m = 0; m < pop_num; m++) {
no_in_arr.pop();
}
}
}
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 (out_arr.length) {
for (var m in out_arr) {
var item = out_arr[m];
if (item.offline_price) {
offline_price += (item.price - item.offline_price) * item.num;
offline_num += item.num;
}
if (goods_map[item.goods_id]) {
var num = goods_map[item.goods_id];
num += item.num
goods_map[item.goods_id] = num;
} else {
goods_map[item.goods_id] = item.num;
}
}
}
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);
//如果商品的数量已经为空了
if (goods[idx].goods_num <= 0) {
goods.splice(idx, 1);
}
}
}
//-- 只统计是参与活动的商品 --
for (var ij in goods) {
var iter = goods[ij];
if (iter.prom_type != 7 || iter.prom_id != act.id) {
continue;
}
all_price0 += iter.goods_num * iter.goods_price;
}
//-- 活动的条件已经满足 --
c_item.zh_prom_goods[act.id].is_has_zh = 1;
c_item.is_has_zh = 1;
c_item.zh_prom_goods[act.id].actual_price = aprice;
c_item.zh_prom_goods[act.id].cut_price = all_price0 - aprice;
//-- 设置还需要购买多少件,享受活动,前段显示 --
let need_to = c_item.need_list;
let index = this.find_need_to(need_to, act);
if (index > -1) {
need_to.splice(index, 1);
txt_ite = need_to
}
} else {
for (let ii in goods) {
let item = goods[ii];
if (item.prom_type != 7) continue;
if (item.prom_id != act.id) continue;
if (item.offline_price) {
offline_price += item.goods_price - item.offline_price;
offline_num += 1;
}
item.prom_type = 0;
item.prom_id = 0;
}
if (act.zhbuyqty - all_num > need_to_buy) {
need_to_buy = act.zhbuyqty - all_num;
}
//-- 设置还需要购买多少件享受活动,前端显示 --
let need_to = c_item.need_list;
let index = this.find_need_to(need_to, act);
if (title == '') title = "再买" + need_to_buy + "件,下单立享【" + act.zhprice + "元任选" + act.zhbuyqty + "件】";
if (index != -1) {
need_to[index].title = title;
} else {
if (!need_to) need_to = [];
let it = {
id: act.id,
pickup_id: c_item.pickup_id,
title: title
};
need_to.push(it);
}
txt_ite = need_to;
}
tfeel += aprice;
cut_price += all_price0 - aprice;;
}
c_item.zh_all_price = tfeel;
c_item.zh_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 == 7
})
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;
},
find_need_to: function (list, iter) {
if (!list || list.length <= 0) return -1;
for (var i in list) {
var item = list[i];
if (item.id == iter.id) {
return i;
}
}
return -1;
},
//-- 平摊组合购的价格 --
calc_zh_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 zh_prom_goods = cart_item.zh_prom_goods; //一单中所有的组合购的集合
var item_map = {};
for (let i in zh_prom_goods) {
let item = zh_prom_goods[i];
let prom_id = i;
//如果这个活动还没有达到要求,继续下一个计算
if (!item.is_has_zh) continue;
//过滤出相应组合购活动的商品
let glist = ord_goods.filter(function (ele) {
return ele.prom_type == 7 && 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({
"zhprom_id": prom_id,
"dis": (all_good_price - item.actual_price).toFixed(2),
"ispt": 0
})
} else {
cart_item.prom_pt_json = [{
"zhprom_id": prom_id,
"dis": (all_good_price - item.actual_price).toFixed(2),
"ispt": 0
}];
}
}
//-- 如果系统要平摊到单品 --
var pt_data = {
'prom_id': prom_id,
'dis': parseFloat(all_good_price - item.actual_price).toFixed(2),
'goods': post_gd
}
var pt_res = null;
await getApp().request.promisePost("/api/weshop/order/getGoodsSplit", {
is_json: 1,
data: pt_data
}).then(res => {
if (res.data.code == 0) {
pt_res = res.data.data;
}
})
if (pt_res) {
for (var io in glist) {
var goods_id = glist[io].goods_id;
item_map[goods_id] = {};
//平摊赋值
item_map[goods_id].account_fir = th.arr_get_goods(goods_id, pt_res).fisrt_account;
item_map[goods_id].account_yu_fir = th.arr_get_goods(goods_id, pt_res).fisrt_account_yu;
if (!th.data.ispt_goods) {
item_map[goods_id].account = item_map[goods_id].account_fir;
item_map[goods_id].account_yu = item_map[goods_id].account_yu_fir;
}
}
}
}
//--------循环计算商品是不是包邮,是不是使用优惠券,此时循环是商品从表-----------
for (var jj = 0; jj < ord_goods.length; jj++) {
if (ord_goods[jj].is_gift) continue;
if (ord_goods[jj].prom_type == 7 && item_map[ord_goods[jj].goods_id]) {
ord_goods[jj].account_fir = item_map[ord_goods[jj].goods_id].account_fir;
ord_goods[jj].account_yu_fir = item_map[ord_goods[jj].goods_id].account_yu_fir;
ord_goods[jj].account = item_map[ord_goods[jj].goods_id].account;
ord_goods[jj].account_yu = item_map[ord_goods[jj].goods_id].account_yu;
}
}
}
}
}