Kaynağa Gözat

Merge branch 'dev'

lrf 2 yıl önce
ebeveyn
işleme
222d947434

+ 7 - 2
app/service/shop/shopInBill.js

@@ -52,7 +52,7 @@ class ShopInBillService extends CrudService {
     const obj = { shop: _.get(afterSale, 'shop') };
     obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
     obj.source = '-3';
-    obj.source_id = _.get(afterSale, '_id');
+    obj.source_id = _.get(afterSale, 'order_detail');
     const limit = _.get(inBill, 'receipts');
     if (this.ctx.minus(limit, returnMoney) >= 0) obj.receipts = returnMoney;
     else obj.receipts = limit;
@@ -75,17 +75,22 @@ class ShopInBillService extends CrudService {
     const inBill = list.filter(f => parseInt(f.source) >= 0);
     const outBill = list.filter(f => parseInt(f.source) < 0);
     const getOutBill = list.filter(f => f.source === '-1');
-    const outBillMoney = getOutBill.reduce((p, n) => this.ctx.plus(p, n.money), 0);
+    const outBillMoney = getOutBill.reduce((p, n) => this.ctx.plus(p, n.receipts), 0);
+    const canGetIds = [];
+    const shouldOutIds = [];
     for (const i of inBill) {
       const cAt = _.get(i, 'time');
       const m = moment().diff(cAt, 'm');
       if (m >= min) {
         // 已经过了提款时间,那就再找下,是否有该记录相应的退款
         const { receipts: money, source_id } = i;
+        canGetIds.push(source_id);
         let cg = money;
         const os = outBill.filter(f => f.source_id === source_id);
+        console.log(`${source_id} 退款数: ${os.length}`);
         for (const o of os) {
           cg = this.ctx.minus(cg, o.receipts);
+          shouldOutIds.push(o.source_id);
         }
         canGet = this.ctx.plus(canGet, cg);
       }

+ 1 - 0
app/service/trade/afterSale.js

@@ -225,6 +225,7 @@ class AfterSaleService extends CrudService {
     await this.ctx.service.user.point.refundOrderPoint(order_detail, tran);
     // 退商铺的入账流水金额
     await this.ctx.service.shop.shopInBill.createByAfterSale(data, returnMoney, tran);
+    await this.ctx.service.user.cashBack.refund(data, tran);
     return refundInfo;
   }
   /**

+ 6 - 4
app/service/trade/order.js

@@ -186,7 +186,9 @@ class OrderService extends CrudService {
           this.tran.update('GoodsSpec', _id, { num: newNum });
           if (cart_id) {
             const num = await this.cartModel.count({ _id: cart_id });
-            if (num > 0) { this.tran.remove('Cart', cart_id); }
+            if (num > 0) {
+              this.tran.remove('Cart', cart_id);
+            }
           }
           // 活动相关:
           // 买赠:不一定非要处理赠品,赠品可能不在库中;
@@ -210,7 +212,6 @@ class OrderService extends CrudService {
           }
         }
       }
-
     }
   }
 
@@ -264,8 +265,9 @@ class OrderService extends CrudService {
     const couponList = await this.ctx.service.user.userCoupon.toMakeOrder_getList(specsData);
     pageData.couponList = couponList;
     // 返现部分:添加推荐人信息
-    const inviter = data.find(f => ObjectId.isValid(f.inviter));
-    pageData.inviter = inviter;
+    const inviter = _.get(_.head(data), 'inviter');
+    // const inviter = data.find((f) => ObjectId.isValid(f.inviter));
+    if (inviter) pageData.inviter = inviter;
     // 活动部分
     // 将加价购拿出来,给前端,需要特殊处理;
     pageData.actList = actList.filter(f => f.platform_act_type === '4' && f.activity);

+ 24 - 8
app/service/user/cashBack.js

@@ -22,8 +22,8 @@ class CashBackService extends CrudService {
     const min = this.ctx.multiply(rd, this.ctx.multiply(24, 60));
     let total = 0;
     for (const i of res) {
-      const { source, money } = i;
-      const sn = parseInt(source);
+      const { status, money } = i;
+      const sn = parseInt(status);
       if (sn >= 0) total = this.ctx.plus(total, money);
       else total = this.ctx.minus(total, money);
     }
@@ -31,10 +31,10 @@ class CashBackService extends CrudService {
     // 上面总价包含了提现金额
     // 下面计算的金额是可以提现的金额 提现总金额 - 提现金额
     let canGet = 0;
-    const inBill = res.filter(f => parseInt(f.source) >= 0);
-    const outBill = res.filter(f => parseInt(f.source) < 0);
-    const getOutBill = res.filter(f => f.source === '-1');
-    const outBillMoney = getOutBill.reduce((p, n) => this.ctx.plus(p, n.money), 0);
+    const inBill = res.filter(f => parseInt(f.status) >= 0);
+    const outBill = res.filter(f => parseInt(f.status) < 0);
+    // const getOutBill = res.filter(f => f.status === '-1');
+    // const outBillMoney = getOutBill.reduce((p, n) => this.ctx.plus(p, n.money), 0);
     for (const i of inBill) {
       const cAt = _.get(i, 'time');
       const m = moment().diff(cAt, 'm');
@@ -49,7 +49,7 @@ class CashBackService extends CrudService {
         canGet = this.ctx.plus(canGet, cg);
       }
     }
-    canGet = this.ctx.minus(canGet, outBillMoney);
+    // canGet = this.ctx.minus(canGet, outBillMoney);
     return { total, canGet };
   }
   /**
@@ -78,11 +78,13 @@ class CashBackService extends CrudService {
       const type = _.get(cb_config, 'back_type');
       if (!type) continue;
       const money = _.get(cb_config, 'money');
+      // 返现/比例小于等于 0则不需要进行
+      if (this.ctx.minus(money, 0) <= 0) continue;
       if (type === 'fixed') rmoney = this.ctx.plus(rmoney, money);
       else if (type === 'percent') {
         const specId = _.get(gs, '_id');
         const realPay = _.get(moneyDetail, `${specId}.${priceKey}`);
-        const grm = this.ctx.multiply(realPay, this.ctx.divide(money, 10));
+        const grm = this.ctx.multiply(realPay, this.ctx.divide(money, 100));
         rmoney = this.ctx.plus(rmoney, grm);
       }
     }
@@ -94,6 +96,20 @@ class CashBackService extends CrudService {
     if (num > 0) return;
     tran.insert('CashBack', obj);
   }
+  /**
+   * 退货退流水
+   * @param {Object} afterSale 修改前的售后数据
+   * @param {Transaction} tran 数据库事务
+   */
+  async refund(afterSale, tran) {
+    const source_id = _.get(afterSale, 'order_detail');
+    const inBill = await this.model.findOne({ source_id }).lean();
+    if (!inBill) return;
+    const outBill = _.pick(inBill, [ 'inviter', 'money', 'source', 'source_id' ]);
+    outBill.time = moment().format('YYYY-MM-DD HH:mm:ss');
+    outBill.status = '-1';
+    tran.insert('CashBack', outBill);
+  }
 }
 
 module.exports = CashBackService;

+ 1 - 1
app/service/user/userCoupon.js

@@ -294,7 +294,7 @@ class UserCouponService extends CrudService {
       // 有最低消费的限制,就需要看看下满足条件的商品够不够这个金额
       // 消费下限 大于 满足优惠券使用的商品的消费总额,则不能使用这个券,消费的钱不够
       // 且这个地方应该报错
-      if (this.ctx.minus(limit, goodsTotal) <= 0) throw new BusinessError(ErrorCode.DATA_INVALID, `消费券:${coupon.name}不满足使用条件`);
+      if (this.ctx.minus(limit, goodsTotal) > 0) throw new BusinessError(ErrorCode.DATA_INVALID, `消费券:${coupon.name}不满足使用条件`);
     }
     // 减免设置转换成 实际减免总金额:
     // 满减则直接就是 min(设置的满x减y的y);