flexbiz-server
Version:
Flexible Server
18 lines (17 loc) • 7.86 kB
JavaScript
'use strict';const moment=require("moment"),socainppSchema=new Schema({id_app:{type:String,required:!0,maxlength:1024},ma_dvcs:{type:String,required:!0,maxlength:1024},id_ct:{type:String,required:!0,maxlength:1024,trim:!0},ma_ct:{type:String,required:!0,uppercase:!0,maxlength:32,trim:!0},ma_gd:{type:String,default:"0",maxlength:32,trim:!0},ngay_ct:{type:Date,required:!0},so_ct:{type:String,required:!0,uppercase:!0,maxlength:32,trim:!0},ngay_hd:{type:Date},so_hd:{type:String,uppercase:!0,maxlength:32,
trim:!0},tk_no:{type:String,required:!0,uppercase:!0,maxlength:32,trim:!0},tk_co:{type:String,required:!0,uppercase:!0,maxlength:32,trim:!0},ma_kh_no:{type:String,uppercase:!0,default:"",maxlength:32,trim:!0},ma_kh_co:{type:String,uppercase:!0,default:"",maxlength:32,trim:!0},ma_nt:{type:String,default:"VND",required:!0,uppercase:!0,maxlength:32,trim:!0},ty_gia:{type:Number,default:1},tien_nt:{type:Number,default:0},tien:{type:Number,default:0},dien_giai:{type:String,default:"",maxlength:1024},ong_ba:{type:String,
default:"",maxlength:1024},ma_kho:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_bp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_phi:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_hd:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_dt:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_nv:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_lo:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},han_sd:{type:Date},
ma_tt1:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_tt2:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_tt3:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_ca:{type:String,uppercase:!0,maxlength:32,trim:!0},ma_cp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_sp:{type:String,default:"",uppercase:!0,maxlength:1024,trim:!0},ma_vt:{type:String,default:"",uppercase:!0,maxlength:1024,trim:!0},ma_dvt_sp:{type:String,maxlength:32,trim:!0},ma_tt1_sp:{type:String,
default:"",uppercase:!0,maxlength:32,trim:!0},ma_tt2_sp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_tt3_sp:{type:String,default:"",uppercase:!0,maxlength:32,trim:!0},ma_cum_chi_tiet:{type:String,uppercase:!0,default:"",maxlength:32,trim:!0},ten_cum_chi_tiet:{type:String,default:"",maxlength:1024,trim:!0},ma_cong_doan:{type:String,uppercase:!0,default:"",maxlength:32,trim:!0},id_lenhsx:{type:String,maxlength:1024},id_cs:{type:String,maxlength:1024},id_link:String,ma_ct_link:String,
so_ct_link:String,id_ct_goc:String,ma_ct_goc:String,so_ct_goc:String,ngay_ct_goc:Date,pt_thanh_toan_goc:String,pt_thanh_toan:String,pt_thanh_toan2:String,trang_thai:{type:String,maxlength:32},nh_dk:{type:Number,default:0},id_kbbtpb:{type:String,maxlength:1024},exfields:Schema.Types.Mixed,exfields_detail:Schema.Types.Mixed,flag:{type:String,maxlength:1024},gio:Number,ngay:Number,thang:Number,quy:Number,nam:Number,ma_kenh:String,ma_td1:Schema.Types.Mixed,ma_td2:Schema.Types.Mixed,ma_td3:Schema.Types.Mixed,
ma_td4:Schema.Types.Mixed,ma_td5:Schema.Types.Mixed,ma_td6:Schema.Types.Mixed,ma_td7:Schema.Types.Mixed,ma_td8:Schema.Types.Mixed,ma_td9:Schema.Types.Mixed,status:{type:Boolean,default:!0},date_created:{type:Date,default:Date.now},date_updated:{type:Date,default:Date.now},user_created:{type:String,default:""},user_updated:{type:String,default:""}},{toJSON:{virtuals:!0}});
socainppSchema.pre("save",function($next$$){this.tien="VND"==this.ma_nt?this.tien_nt=Math.roundBy(this.tien_nt,0):Math.roundBy(this.tien,0);let $ngay_ct$$=moment(this.ngay_ct);this.gio=$ngay_ct$$.get("hours");this.ngay=$ngay_ct$$.get("dates");this.quy=$ngay_ct$$.get("quarters");this.thang=$ngay_ct$$.get("months")+1;this.nam=$ngay_ct$$.get("year");$next$$()});const model=mongoose.models.socainpp||mongoose.model("socainpp",socainppSchema);
(global.configs||{}).createIndexes&&(socainppSchema.index({id_app:1}),socainppSchema.index({ma_dvcs:1}),socainppSchema.index({so_ct:1}),socainppSchema.index({ngay_ct:1}),socainppSchema.index({so_ct:1,ngay_ct:1}),socainppSchema.index({gio:1}),socainppSchema.index({ngay:1}),socainppSchema.index({thang:1}),socainppSchema.index({quy:1}),socainppSchema.index({nam:-1}),socainppSchema.index({ma_ct:1}),socainppSchema.index({id_ct:1}),socainppSchema.index({ma_ct_goc:1}),socainppSchema.index({id_ct_goc:1}),
socainppSchema.index({tk_no:1}),socainppSchema.index({tk_co:1}),socainppSchema.index({ma_kh_no:1}),socainppSchema.index({ma_kh_co:1}),socainppSchema.index({ma_nt:1}),socainppSchema.index({ma_bp:1}),socainppSchema.index({ma_phi:1}),socainppSchema.index({ma_hd:1}),socainppSchema.index({ma_dt:1}),socainppSchema.index({ma_nv:1}),socainppSchema.index({ma_sp:1}),socainppSchema.index({ma_vt:1}),socainppSchema.index({ma_kenh:1}),socainppSchema.index({ma_td1:1}),socainppSchema.index({ma_td2:1}),socainppSchema.index({ma_td3:1}),
socainppSchema.index({ma_td4:1}),socainppSchema.index({ma_td5:1}),socainppSchema.index({ma_td6:1}),socainppSchema.index({ma_td7:1}),socainppSchema.index({ma_td8:1}),socainppSchema.index({ma_td9:1}),socainppSchema.index({ma_lo:1}),socainppSchema.index({han_sd:-1}),socainppSchema.index({ma_tt1:1}),socainppSchema.index({ma_tt2:1}),socainppSchema.index({ma_tt3:1}),socainppSchema.index({id_lenhsx:1}),socainppSchema.index({flag:1}),socainppSchema.index({id_link:1}),socainppSchema.index({id_cs:1}),socainppSchema.index({id_app:1,
id_ct:1,so_ct:-1,ngay_ct:-1,ma_kh_no:1,ma_kh_co:1,tk_no:1,tk_co:1}),socainppSchema.index({dien_giai:"text"}),socainppSchema.index({status:1}),socainppSchema.index({user_created:1,visible_to:1,visible_to_users:1}),socainppSchema.index({trang_thai:1}),model.on("index",async function($err$$){$err$$?console.error("socainpp index error",$err$$):console.info("socainpp indexing complete")}));
socainppSchema.post("save",async function($doc$$){if(0<=["SO1","HD2","HD1","PBL"].indexOf($doc$$.ma_ct)){if($doc$$.ma_kh_no){var $dhgn_dhgn$$=await global.getModel("socainpp").findOne({ma_kh:$doc$$.ma_kh_no,id_app:$doc$$.id_app},{ngay_ct:1}).sort({ngay_ct:-1});if($dhgn_dhgn$$){var $so_ngay_da_mua_so_ngay_da_mua$$=moment().diff(moment($dhgn_dhgn$$.ngay_ct),"days");model.updateOne({ma_kh:$doc$$.ma_kh_no,id_app:$doc$$.id_app},{ngay_mua_gan_nhat:$dhgn_dhgn$$.ngay_ct,so_ngay_da_mua:$so_ngay_da_mua_so_ngay_da_mua$$})}}$doc$$.ma_kh_co&&
$doc$$.ma_kh_co!=$doc$$.ma_kh_no&&($dhgn_dhgn$$=await global.getModel("socainpp").findOne({ma_kh:$doc$$.ma_kh_co,id_app:$doc$$.id_app},{ngay_ct:1}).sort({ngay_ct:-1}))&&($so_ngay_da_mua_so_ngay_da_mua$$=moment().diff(moment($dhgn_dhgn$$.ngay_ct),"days"),model.updateOne({ma_kh:$doc$$.ma_kh_co,id_app:$doc$$.id_app},{ngay_mua_gan_nhat:$dhgn_dhgn$$.ngay_ct,so_ngay_da_mua:$so_ngay_da_mua_so_ngay_da_mua$$}))}});
model.getArise=async($query$$,$options$$={fields:void 0})=>{let $condition$$={ngay_ct:{$gte:$query$$.tu_ngay,$lte:$query$$.den_ngay},id_app:$query$$.id_app};$query$$.ma_dvcs&&($condition$$.ma_dvcs=$query$$.ma_dvcs);$query$$.ma_bp&&($condition$$.ma_bp=$query$$.ma_bp);$query$$.ma_hd&&($condition$$.ma_hd=$query$$.ma_hd);$query$$.ma_dt&&($condition$$.ma_dt=$query$$.ma_dt);$query$$.ma_sp&&($condition$$.ma_sp=$query$$.ma_sp);$query$$.ma_nv&&($condition$$.ma_nv=$query$$.ma_nv);$query$$.ma_phi&&($condition$$.ma_phi=
$query$$.ma_phi);$query$$.ma_kho&&($condition$$.ma_kho=$query$$.ma_kho);if($query$$.tk_no&&Array.isArray($query$$.tk_no)){var $tk_co_tk_no$$=$query$$.tk_no.reg();$condition$$.tk_no=$query$$.giam_tru_no?{$not:new RegExp($tk_co_tk_no$$)}:{$regex:$tk_co_tk_no$$,$options:"i"}}$query$$.tk_co&&Array.isArray($query$$.tk_co)&&($tk_co_tk_no$$=$query$$.tk_co.reg(),$condition$$.tk_co=$query$$.giam_tru_co&&$tk_co_tk_no$$?{$not:new RegExp($tk_co_tk_no$$)}:{$regex:$tk_co_tk_no$$,$options:"i"});return await model.find($condition$$,
$options$$.fields).lean()};module.exports=model;