lrf 2 gadi atpakaļ
vecāks
revīzija
260823ac4f

+ 3 - 0
app/controller/business/config/.lessonStudent.js

@@ -49,4 +49,7 @@ module.exports = {
   checkCanUse: {
     requestBody: ['!lesson_id', '!school_id', '!student_id'],
   },
+  toRefund: {
+    requestBody: ['!lesson_id', '!student_id'],
+  },
 };

+ 1 - 1
app/model/business/bill.js

@@ -9,7 +9,7 @@ const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-pl
 // 3,直接支付:type为-1 pay_for:tempLessonApply/lessonStudent,有from_id,有pay_id和is_pay
 // 4,余额支付:type为-2 pay_for:tempLessonApply/lessonStudent,有from_id,有pay_id和is_pay
 const bill = {
-  school_id: { type: String, required: false, zh: '学校id', ref: 'School', getProp: ['name'] }, //
+  school_id: { type: String, required: false, zh: '学校id', ref: 'School', getProp: [ 'name' ] }, //
   payer_role: { type: String, required: false, zh: '用户角色' }, //
   payer_id: { type: String, required: false, zh: '用户角色id', refPath: 'payer_role' }, //
   pay_for: { type: String, required: false, zh: '原因' }, //

+ 13 - 2
app/service/business/lessonStudent.js

@@ -3,6 +3,7 @@ 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 LessonStudentService extends CrudService {
@@ -12,6 +13,9 @@ class LessonStudentService extends CrudService {
     this.rscModel = this.ctx.model.Relation.RelationStudentCoach;
     this.lessonCoachModel = this.ctx.model.Business.LessonCoach;
     this.lessonModel = this.ctx.model.Business.Lesson;
+    this.payOrderService = this.ctx.service.business.payOrder;
+    this.payOrderModel = this.ctx.model.Business.PayOrder;
+    this.billModel = this.ctx.model.Business.Bill;
   }
 
   async checkCanUse({ school_id, student_id, lesson_id }) {
@@ -85,6 +89,7 @@ class LessonStudentService extends CrudService {
   async toRefund({ lesson_id, student_id }) {
     const data = await this.model.findOne({ lesson_id, student_id });
     if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据');
+    if (data.is_pay === '-3') throw new BusinessError(ErrorCode.DATA_INVALID, '已经完成退课,无法再次退课');
     if (data.is_try === '1') {
       // 试课 修改退款状态, 而不需要退钱,同时也浪费了试课机会
       data.is_pay = '-3';
@@ -92,8 +97,14 @@ class LessonStudentService extends CrudService {
       return;
     }
     // 正常交钱的课,将 pay_id 的status 修改为 -3, 同时触发修改这条数据,但是没有退款;
-    // 再将金额打到充值表里,
-
+    await this.payOrderService.updateOne({ _id: data.pay_id }, { status: '-3' });
+    const payOrder = await this.payOrderModel.findById(data.pay_id);
+    // 再生成账单记录
+    const obj = _.pick(payOrder, [ 'school_id', 'payer_role', 'payer_id', 'pay_for', 'from_id' ]);
+    obj.type = '2';
+    obj.is_pay = '1';
+    obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
+    await this.billModel.create(obj);
   }
 }
 

+ 25 - 10
app/service/business/payOrder.js

@@ -119,9 +119,11 @@ class PayOrderService extends CrudService {
       tempApply.pay_id = pay_id;
       await tempApply.save();
       // 修改完申请,再创建 lessonStudent
-      const { lesson_id, student_id, school_id } = tempApply;
-      const obj = { lesson_id, student_id, school_id, is_pay, pay_id, config, money };
-      await this.lessonStudentService.create(obj);
+      if (is_pay === '1') {
+        const { lesson_id, student_id, school_id } = tempApply;
+        const obj = { lesson_id, student_id, school_id, is_pay, pay_id, config, money };
+        await this.lessonStudentService.create(obj);
+      }
       await this.makeRecord(data);
     } else if (pay_for === 'Bill' && is_pay !== '0') {
       // 充值记录,找到充值记录,没有就生成
@@ -136,19 +138,32 @@ class PayOrderService extends CrudService {
       }
     }
   }
-  // 检查 消费记录
+  // 检查记录
   async makeRecord(data) {
-    const { config, _id: pay_id } = data;
-    // 消费记录
+    const { config, _id: pay_id, status: is_pay } = data;
+    let billData;
+    // 使用余额的处理
     if (_.get(config, 'useSurplus')) {
       const { bill } = config;
-      // 使用了余额,但是余额记录不是直接生成的,需要检查下costDetail是否为ObjectId
+      // 使用了余额,但是余额记录不是直接生成的,需要检查下bill是否为ObjectId
       // 如果是ObjectId,说明余额记录已经生成.无需操作
       if (_.isObject(bill)) {
-        // 是数据,生成消费记录
-        const cdd = await this.billModel.create({ ...bill, pay_id, time: moment().format('YYYY-MM-DD HH:mm:ss') });
-        if (cdd) data.config.bill = ObjectId(cdd._id).toString();
+        // 是数据,生成账单记录
+        billData = await this.billModel.create({ ...bill, pay_id, is_pay, time: moment().format('YYYY-MM-DD HH:mm:ss') });
+        if (billData) data.config.bill = ObjectId(billData._id).toString();
         await this.model.updateOne({ _id: data._id }, data);
+      } else {
+        // 已经有数据了, 修改账单.并根据is_pay去做相应的处理
+        await this.billModel.updateOne({ _id: data.config.bill }, { is_pay });
+        billData = await this.billModel.findById(data.config.bill);
+      }
+      const { type, payer_role, payer_id, money } = billData;
+      if (type === '-2') {
+        let relation;
+        if (payer_role === 'Student') relation = await this.rssModel.findOne({ student_id: payer_id });
+        else if (payer_role === 'Coach') relation = await this.rcsModel.findOne({ coach_id: payer_id });
+        relation.money = relation.money - money;
+        await relation.save();
       }
     }
   }

+ 1 - 0
app/z_router/business/lessonStudent.js

@@ -7,6 +7,7 @@ const rkey = 'lessonStudent';
 const ckey = 'business.lessonStudent';
 const keyZh = '课程-学员';
 const routes = [
+  { method: 'post', path: `${rkey}/toRefund`, controller: `${ckey}.toRefund`, name: `${ckey}toRefund`, zh: `退课${keyZh}` },
   { method: 'post', path: `${rkey}/checkCanUse`, controller: `${ckey}.checkCanUse`, name: `${ckey}checkCanUse`, zh: `查询是否可以报名${keyZh}` },
   { method: 'post', path: `${rkey}/toComputed`, controller: `${ckey}.toComputed`, name: `${ckey}toComputed`, zh: `计算价格${keyZh}` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },