|
@@ -29,6 +29,7 @@ class AfterSaleService extends CrudService {
|
|
|
const { shop, customer } = orderDetail;
|
|
|
const apply_time = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
const obj = { order_detail, customer, shop, goods, ...others, apply_time, status: '0' };
|
|
|
+ console.log(obj);
|
|
|
await this.model.create(obj);
|
|
|
}
|
|
|
|
|
@@ -86,6 +87,7 @@ class AfterSaleService extends CrudService {
|
|
|
const reason = _.get(data, 'desc');
|
|
|
const order_no = _.get(orderDetail, 'order.pay.pay_no');
|
|
|
if (!order_no) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单号');
|
|
|
+
|
|
|
// 查查优惠中,是否有这个商品:将这个商品的每一个优惠券的优惠部分都加一起,然后用原价-优惠价=实付价
|
|
|
const discount_detail = _.get(orderDetail, 'total_detail.discount_detail');
|
|
|
if (discount_detail) {
|
|
@@ -115,9 +117,11 @@ class AfterSaleService extends CrudService {
|
|
|
const out_refund_no = `${order_no}-r${num}`;
|
|
|
const obj = { reason, money: refundMoney, order_no, out_refund_no };
|
|
|
// 退款请求
|
|
|
- const res = await this.ctx.service.trade.pay.refund(obj);
|
|
|
- if (res.errcode && res.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, res.errmsg);
|
|
|
- tran.update('AfterSale', afterSale_id, { status: '-1' });
|
|
|
+ if (refundMoney > 0) {
|
|
|
+ const res = await this.ctx.service.trade.pay.refund(obj);
|
|
|
+ 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' });
|
|
|
await tran.run();
|
|
|
|
|
|
// 检查优惠券是否都退了
|
|
@@ -200,6 +204,34 @@ class AfterSaleService extends CrudService {
|
|
|
const obj = { payTotal, goodsTotal, freightTotal, discountTotal };
|
|
|
return obj;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 退单
|
|
|
+ * @param {Object} body 参数体
|
|
|
+ * @param body.order_detail 订单详情id
|
|
|
+ * @param body.desc 退单理由
|
|
|
+ */
|
|
|
+ async orderCancel({ order_detail, desc }) {
|
|
|
+ // 查询要退的订单
|
|
|
+ const orderDetail = await this.orderDetailModel.findById(order_detail);
|
|
|
+ if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
|
|
|
+ const { goods, customer } = orderDetail;
|
|
|
+ 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 };
|
|
|
+ await this.create(obj);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
async fetch(filter) {
|
|
|
assert(filter);
|