|
@@ -18,8 +18,6 @@ class AfterSaleService extends CrudService {
|
|
async create({ order_detail, goods_id, ...others }) {
|
|
async create({ order_detail, goods_id, ...others }) {
|
|
const orderDetail = await this.orderDetailModel.findById(order_detail);
|
|
const orderDetail = await this.orderDetailModel.findById(order_detail);
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
|
|
- // 查看订单是否签收
|
|
|
|
-
|
|
|
|
// 查看该商品是否已经申请售后
|
|
// 查看该商品是否已经申请售后
|
|
const hasData = await this.model.count({ order_detail, 'goods._id': goods_id, type: [ '0', '1', '2', '3' ] });
|
|
const hasData = await this.model.count({ order_detail, 'goods._id': goods_id, type: [ '0', '1', '2', '3' ] });
|
|
if (hasData > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已有正在处理中的售后申请.请勿重复申请');
|
|
if (hasData > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已有正在处理中的售后申请.请勿重复申请');
|
|
@@ -29,7 +27,6 @@ class AfterSaleService extends CrudService {
|
|
const { shop, customer } = orderDetail;
|
|
const { shop, customer } = orderDetail;
|
|
const apply_time = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
const apply_time = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
const obj = { order_detail, customer, shop, goods, ...others, apply_time, status: '0' };
|
|
const obj = { order_detail, customer, shop, goods, ...others, apply_time, status: '0' };
|
|
- console.log(obj);
|
|
|
|
await this.model.create(obj);
|
|
await this.model.create(obj);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -51,7 +48,7 @@ class AfterSaleService extends CrudService {
|
|
await this.tran.run();
|
|
await this.tran.run();
|
|
const type = _.get(entity, 'type');
|
|
const type = _.get(entity, 'type');
|
|
const status = _.get(update, 'status');
|
|
const status = _.get(update, 'status');
|
|
- // 同意退款,则直接进行退款,然后再将状态修改为已退款
|
|
|
|
|
|
+ // 同意退款/退货,则直接进行退款,然后再将状态修改为已退款
|
|
if (type !== '2' && status === '1') {
|
|
if (type !== '2' && status === '1') {
|
|
await this.toRefund({ afterSale_id: entity._id, goods_id: _.get(entity, 'goods._id') }, this.tran);
|
|
await this.toRefund({ afterSale_id: entity._id, goods_id: _.get(entity, 'goods._id') }, this.tran);
|
|
}
|
|
}
|
|
@@ -80,24 +77,17 @@ class AfterSaleService extends CrudService {
|
|
const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
|
|
const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
|
|
const orderDetail = await this.orderDetailModel.findById(data.order_detail).populate(populate);
|
|
const orderDetail = await this.orderDetailModel.findById(data.order_detail).populate(populate);
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到售后信息的订单');
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到售后信息的订单');
|
|
- // 计算商品原价,运费
|
|
|
|
- let money = this.ctx.multiply(_.get(data, 'goods.sell_money'), _.get(data, 'goods.buy_num'));
|
|
|
|
- const freight = this.ctx.multiply(_.get(data, 'goods.buy_num'), _.get(data, 'goods.freight'));
|
|
|
|
- money = this.ctx.plus(money, freight);
|
|
|
|
const reason = _.get(data, 'desc');
|
|
const reason = _.get(data, 'desc');
|
|
const order_no = _.get(orderDetail, 'order.pay.pay_no');
|
|
const order_no = _.get(orderDetail, 'order.pay.pay_no');
|
|
if (!order_no) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单号');
|
|
if (!order_no) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单号');
|
|
-
|
|
|
|
- // 查查优惠中,是否有这个商品:将这个商品的每一个优惠券的优惠部分都加一起,然后用原价-优惠价=实付价
|
|
|
|
- const discount_detail = _.get(orderDetail, 'total_detail.discount_detail');
|
|
|
|
- if (discount_detail) {
|
|
|
|
- let discountMoney = 0;
|
|
|
|
- for (const uc_id in discount_detail) {
|
|
|
|
- const detail = discount_detail[uc_id];
|
|
|
|
- discountMoney = this.ctx.plus(discountMoney, detail[goods_id]);
|
|
|
|
- }
|
|
|
|
- money = this.ctx.minus(money, discountMoney);
|
|
|
|
- }
|
|
|
|
|
|
+ // 用工具函数,获取退货商品的实际支付价格(常规/团购)
|
|
|
|
+ const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
|
|
|
|
+ const goodsMoneyDetail = _.get(moneyDetail, goods_id, {});
|
|
|
|
+ const type = _.get(orderDetail, 'type', '0');
|
|
|
|
+ let priceKey;
|
|
|
|
+ if (type === '1') priceKey = 'ggrp';
|
|
|
|
+ else priceKey = 'grp';
|
|
|
|
+ const money = _.get(goodsMoneyDetail, priceKey, 0);
|
|
|
|
|
|
// 取出商品输入的价格
|
|
// 取出商品输入的价格
|
|
const needRefund = _.get(data, 'money');
|
|
const needRefund = _.get(data, 'money');
|
|
@@ -109,7 +99,6 @@ class AfterSaleService extends CrudService {
|
|
// 部分退,部分退是不退优惠券的
|
|
// 部分退,部分退是不退优惠券的
|
|
refundMoney = needRefund;
|
|
refundMoney = needRefund;
|
|
}
|
|
}
|
|
-
|
|
|
|
// 找下当前这个订单有多少次售后记录
|
|
// 找下当前这个订单有多少次售后记录
|
|
let num = await this.model.count({ order_detail: data.order_detail });
|
|
let num = await this.model.count({ order_detail: data.order_detail });
|
|
num += 2;
|
|
num += 2;
|
|
@@ -122,8 +111,15 @@ class AfterSaleService extends CrudService {
|
|
if (res.errcode && res.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, res.errmsg);
|
|
if (res.errcode && res.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, res.errmsg);
|
|
}
|
|
}
|
|
if (data.status === '1') tran.update('AfterSale', afterSale_id, { status: '-1' });
|
|
if (data.status === '1') tran.update('AfterSale', afterSale_id, { status: '-1' });
|
|
- await tran.run();
|
|
|
|
-
|
|
|
|
|
|
+ // #region 团购部分
|
|
|
|
+ const status = _.get(orderDetail, 'status');
|
|
|
|
+ // 团购单,且未收货的单子,才需要走退团逻辑,否则不需要走退团逻辑
|
|
|
|
+ if (type === '1' && status !== '3') {
|
|
|
|
+ // 团购单,走团购退货逻辑补充
|
|
|
|
+ const { group, customer } = orderDetail;
|
|
|
|
+ await this.ctx.service.group.group.refund({ group, customer }, tran);
|
|
|
|
+ }
|
|
|
|
+ // #endregion
|
|
// 检查优惠券是否都退了
|
|
// 检查优惠券是否都退了
|
|
// 查看支付订单-优惠明细中,该优惠券影响的商品是否都有退款/退货成功的记录,且退款成功的记录为退全款的.退部分是不退优惠券的
|
|
// 查看支付订单-优惠明细中,该优惠券影响的商品是否都有退款/退货成功的记录,且退款成功的记录为退全款的.退部分是不退优惠券的
|
|
// 如果有,就说明该优惠券可以退了,没有影响任何一单了
|
|
// 如果有,就说明该优惠券可以退了,没有影响任何一单了
|
|
@@ -214,23 +210,32 @@ class AfterSaleService extends CrudService {
|
|
// 查询要退的订单
|
|
// 查询要退的订单
|
|
const orderDetail = await this.orderDetailModel.findById(order_detail);
|
|
const orderDetail = await this.orderDetailModel.findById(order_detail);
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
|
|
- const { goods, customer } = orderDetail;
|
|
|
|
|
|
+ const { customer } = orderDetail;
|
|
const basic = { order_detail, customer, type: '1', desc };
|
|
const basic = { order_detail, customer, type: '1', desc };
|
|
- const discount_detail = _.get(orderDetail, 'total_detail.discount_detail', {});
|
|
|
|
- // 组织数据
|
|
|
|
- for (const g of goods) {
|
|
|
|
- let money = this.ctx.multiply(g.buy_num, g.sell_money);
|
|
|
|
- let dmt = 0;
|
|
|
|
- for (const dd in discount_detail) {
|
|
|
|
- const detail = _.get(discount_detail, dd, {});
|
|
|
|
- const dm = _.get(detail, g._id);
|
|
|
|
- dmt = this.ctx.plus(dmt, dm);
|
|
|
|
- }
|
|
|
|
- money = this.ctx.minus(money, dmt);
|
|
|
|
- if (money <= 0) money = 0;
|
|
|
|
- const obj = { ...basic, goods_id: g._id, money };
|
|
|
|
|
|
+ const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
|
|
|
|
+ let priceKey;
|
|
|
|
+ if (_.get(orderDetail, 'type') === '1') priceKey = 'ggrp';
|
|
|
|
+ else priceKey = 'grp';
|
|
|
|
+ for (const goods_id in moneyDetail) {
|
|
|
|
+ const d = _.get(moneyDetail, goods_id, {});
|
|
|
|
+ const money = _.get(d, priceKey, 0);
|
|
|
|
+ const obj = { ...basic, goods_id, money };
|
|
await this.create(obj);
|
|
await this.create(obj);
|
|
}
|
|
}
|
|
|
|
+ // 组织数据
|
|
|
|
+ // for (const g of goods) {
|
|
|
|
+ // let money = this.ctx.multiply(g.buy_num, g.sell_money);
|
|
|
|
+ // let dmt = 0;
|
|
|
|
+ // for (const dd in discount_detail) {
|
|
|
|
+ // const detail = _.get(discount_detail, dd, {});
|
|
|
|
+ // const dm = _.get(detail, g._id);
|
|
|
|
+ // dmt = this.ctx.plus(dmt, dm);
|
|
|
|
+ // }
|
|
|
|
+ // money = this.ctx.minus(money, dmt);
|
|
|
|
+ // if (money <= 0) money = 0;
|
|
|
|
+ // const obj = { ...basic, goods_id: g._id, money };
|
|
|
|
+ // await this.create(obj);
|
|
|
|
+ // }
|
|
}
|
|
}
|
|
|
|
|
|
async fetch(filter) {
|
|
async fetch(filter) {
|