kpiplus
Version:
KPI+
1 lines • 2.52 kB
JavaScript
var Vsocai=require("../models/vsocai"),Socai=require("../models/socai"),dmtk=require("../models/account"),underscore=require("underscore"),async=require("async"),checkAcc=function(t,n,e,i){dmtk.findOne({id_app:t,tk:e}).lean().exec((t,c)=>t||!c?i(t||`Tài khoản ${e} không tồn tại`):c.tk_cp_yn&&!n.ma_phi?i(`Tài khoản ${e} yêu cầu nhập doanh thu/chi phí`):c.tk_bp_yn&&!n.ma_bp?i(`Tài khoản ${e} yêu cầu nhập bộ phận`):c.tk_dt_yn&&!n.ma_dt?i(`Tài khoản ${e} yêu cầu nhập vụ việc, dự án`):c.tk_hd_yn&&!n.ma_hd?i(`Tài khoản ${e} yêu cầu nhập hợp đồng`):c.tk_nv_yn&&!n.ma_nv?i(`Tài khoản ${e} yêu cầu nhập nhân viên`):void i())},checkData=function(t,n,e){async.parallel({tk_no:e=>{checkAcc(t,n,n.tk_no,t=>{e(t)})},tk_co:e=>{checkAcc(t,n,n.tk_co,t=>{e(t)})}},(t,n)=>{e(t)})},post=function(t,n,e){n&&(this.master=t.toObject(),underscore.isObject(n)?this.details=n:this.details=n.toObject(),this.prepare=e,this.prepare||(this.prepare=function(t,n){n(t)}))};post.prototype.run=function(t){if(t||(t=function(){}),!this.details)return t(null,null);var n=this.master,e=this.details,i=this.prepare;if(!n.ngay_ct)return t("ngay_ct is not null");Vsocai.remove({id_ct:n._id},function(c){if(c)return t("Don't remove vsocai \n"+c);Socai.remove({id_ct:n._id},function(c){if(c)return t("Don't remove socai \n"+c);let o=0;async.map(e,function(t,e){let c=o;i(t,function(t){if(!t.tk_no||!t.tk_co)return e(null);checkData(n.id_app,t,i=>{if(i)return e(i);if(t.tien_nt||(t.tien_nt=0),t.tien||(t.tien=0),0===t.tien_nt&&0===t.tien)return e(null);t.nh_dk=c,o++;let s=new Socai;s.set("id_ct",n._id),t.ma_kh_no||(t.ma_kh_no=t.ma_kh),t.ma_kh_co||(t.ma_kh_co=t.ma_kh);for(let e in Socai.schema.paths)"id_ct"!=e&&"_id"!=e&&(v=t[e],v?s.set(e,v):(v=n[e],v&&s.set(e,v)));s.save(function(t,i){if(t)return e("Can't post socai \n"+t);let o=i.toObject(),s=new Vsocai,_=new Vsocai;s.set("id_ct",n._id),_.set("id_ct",n._id),s.set("nh_dk",c),_.set("nh_dk",c),v=o.tk_no,v&&(s.set("tk",v),_.set("tk_du",v)),v=o.tk_co,v&&(s.set("tk_du",v),_.set("tk",v)),v=o.tien,v&&(s.set("ps_no",v),_.set("ps_co",v)),v=o.tien_nt,v&&(s.set("ps_no_nt",v),_.set("ps_co_nt",v)),v=o.ma_kh_no,v&&s.set("ma_kh",v),v=o.ma_kh_co,v&&_.set("ma_kh",v);for(let t in Vsocai.schema.paths)"id_ct"!=t&&"_id"!=t&&(v=o[t],v?(s.set(t,v),_.set(t,v)):(v=n[t],v&&(s.set(t,v),_.set(t,v))));Vsocai.create([s,_],function(t,n,i){if(t)return e("Can't post vsocai \n"+t);e(null,{no:n,co:i})})})})})},function(n,e){t(n,e)})})})},module.exports=post;