|
@@ -0,0 +1,107 @@
|
|
|
+'use strict';
|
|
|
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const _ = require('lodash');
|
|
|
+const assert = require('assert');
|
|
|
+const moment = require('moment');
|
|
|
+
|
|
|
+//
|
|
|
+class ShopInBillService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'shopinbill');
|
|
|
+ this.model = this.ctx.model.Shop.ShopInBill;
|
|
|
+ this.shopModel = this.ctx.model.Shop.Shop;
|
|
|
+ this.orderUtil = this.ctx.service.util.orderDetail;
|
|
|
+ this.configModel = this.ctx.model.System.Config;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 正常购物创建店铺流水
|
|
|
+ * @param {object} orderDetail 订单详情数据
|
|
|
+ * @param {Transaction} tran 数据库事务
|
|
|
+ */
|
|
|
+ async createByOrder(orderDetail, tran) {
|
|
|
+ const obj = { shop: _.get(orderDetail, 'shop') };
|
|
|
+ const shopData = await this.shopModel.findById(_.get(orderDetail, 'shop'), { cut: 1 });
|
|
|
+ if (!shopData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到店铺数据');
|
|
|
+ obj.cut = _.get(shopData, 'cut', 0);
|
|
|
+ const real_pay = this.orderUtil.computedRealPay(orderDetail);
|
|
|
+ obj.total = real_pay;
|
|
|
+ obj.receipts = this.ctx.multiply(this.ctx.minus(1, this.ctx.divide(obj.cut, 100)), real_pay);
|
|
|
+ obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ obj.source = '3';
|
|
|
+ obj.source_id = _.get(orderDetail, '_id');
|
|
|
+ const num = await this.model.count({ source: obj.source, source_id: obj.source_id });
|
|
|
+ if (num <= 0) tran.insert('ShopInBill', obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 售后创建流水
|
|
|
+ * @param {object} afterSale 订单详情数据
|
|
|
+ * @param {number} returnMoney 退款金额
|
|
|
+ * @param {Transaction} tran 数据库事务
|
|
|
+ */
|
|
|
+ async createByAfterSale(afterSale, returnMoney, tran) {
|
|
|
+ const query = { source_id: _.get(afterSale, 'order_detail'), source: '3' };
|
|
|
+ const inBill = await this.model.findOne(query);
|
|
|
+ // 该订单没有入账,就不需要退
|
|
|
+ if (!inBill) return;
|
|
|
+ const q2 = { source: '-3', source_id: _.get(afterSale, '_id') };
|
|
|
+ const n2 = await this.model.count(q2);
|
|
|
+ // 有过售后退钱记录,不需要再生成
|
|
|
+ if (n2 > 0) return;
|
|
|
+ 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');
|
|
|
+ const limit = _.get(inBill, 'receipts');
|
|
|
+ if (this.ctx.minus(limit, returnMoney) >= 0) obj.receipts = returnMoney;
|
|
|
+ else obj.receipts = limit;
|
|
|
+ tran.insert('ShopInBill', obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ async computedTotal({ shop }) {
|
|
|
+ const list = await this.model.find({ shop }).lean();
|
|
|
+ const config = await this.configModel.findOne({}, { reward_day: 1 }).lean();
|
|
|
+ const rd = _.get(config, 'reward_day', 14);
|
|
|
+ const min = this.ctx.multiply(rd, this.ctx.multiply(24, 60));
|
|
|
+ let total = 0;
|
|
|
+ for (const i of list) {
|
|
|
+ const { source, receipts: money } = i;
|
|
|
+ const sn = parseInt(source);
|
|
|
+ if (sn >= 0) total = this.ctx.plus(total, money);
|
|
|
+ else total = this.ctx.minus(total, money);
|
|
|
+ }
|
|
|
+ let canGet = 0;
|
|
|
+ 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);
|
|
|
+ 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;
|
|
|
+ let cg = money;
|
|
|
+ const os = outBill.filter(f => f.source_id === source_id);
|
|
|
+ for (const o of os) {
|
|
|
+ cg = this.ctx.minus(cg, o.receipts);
|
|
|
+ }
|
|
|
+ canGet = this.ctx.plus(canGet, cg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ canGet = this.ctx.minus(canGet, outBillMoney);
|
|
|
+ return { total, canGet };
|
|
|
+ }
|
|
|
+
|
|
|
+ async afterQuery(filter, data) {
|
|
|
+ for (const i of data) {
|
|
|
+ const { source } = i;
|
|
|
+ if (parseInt(source) >= 0) i.receipts = `+${i.receipts}`;
|
|
|
+ else i.receipts = `-${i.receipts}`;
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = ShopInBillService;
|