UNPKG

flexbiz-server

Version:

Flexible Server

13 lines (12 loc) 5.92 kB
const dvcs=global.getModel("dvcs"),account=global.getModel("account"),sotinhkh=global.getModel("sotinhkh"),nguonvon=global.getModel("dmnguonvon"),bophan=global.getModel("dmbp"),dmkho=global.getModel("dmkho"),dmphi=global.getModel("dmphi"),contract=global.getModel("contract"),loaits=global.getModel("dmloaits"),tanggiamts=global.getModel("dmtanggiamts"),model=global.getModel("qts"),controller=require("../../controllers/controller"),async=require("async"),underscore=require("underscore"),ma_ct="QTS"; module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,sort:{ngay_ct:-1,so_ct:-1}});$contr_router$$.route();$contr_router$$.creating=function($user$$,$obj$$,$next$$){$obj$$.details||($obj$$.details=[]);model.find({so_the_ts:$obj$$.so_the_ts,id_app:$user$$.current_id_app}).limit(1).exec(function($error$$,$ts$$){if($error$$)return $next$$($error$$);if($ts$$&&$ts$$.length>0)return $next$$("S\u1ed1 th\u1ebb t\u00e0i s\u1ea3n "+$obj$$.so_the_ts+ " \u0111\u00e3 t\u1ed3n t\u1ea1i");$next$$(null,$obj$$)})};$contr_router$$.updating=async function($_kh_ngay_kh_moi_nhat_user$$,$data$$,$obj$$,$next$$){$data$$.details.forEach($d$$=>{delete $d$$.ten_nguon_von});if($data$$.so_the_ts&&$obj$$.so_the_ts!=$data$$.so_the_ts&&await model.findOne({so_the_ts:$data$$.so_the_ts,id_app:$_kh_ngay_kh_moi_nhat_user$$.current_id_app}))return $next$$("S\u1ed1 th\u1ebb t\u00e0i s\u1ea3n "+$data$$.so_the_ts+" \u0111\u00e3 t\u1ed3n t\u1ea1i");$_kh_ngay_kh_moi_nhat_user$$= await sotinhkh.find({id_ts:$obj$$._id}).sort({ngay_cuoi_thang:-1}).limit(1);if($_kh_ngay_kh_moi_nhat_user$$=$_kh_ngay_kh_moi_nhat_user$$.length===1?$_kh_ngay_kh_moi_nhat_user$$[0]:null){$_kh_ngay_kh_moi_nhat_user$$=new Date($_kh_ngay_kh_moi_nhat_user$$.ngay_cuoi_thang);if($data$$.ngay_thoi_kh){var $new_ngay_giam_new_ngay_thoi_kh$$=new Date($data$$.ngay_thoi_kh);if(!underscore.isEqual($new_ngay_giam_new_ngay_thoi_kh$$,$obj$$.ngay_thoi_kh)&&$new_ngay_giam_new_ngay_thoi_kh$$.getTime()<$_kh_ngay_kh_moi_nhat_user$$.getTime())return $next$$("Ng\u00e0y th\u00f4i kh\u1ea5u hao ph\u1ea3i l\u1edbn h\u01a1n ng\u00e0y tr\u00edch kh\u1ea5u hao g\u1ea7n nh\u1ea5t.")}if($data$$.ngay_giam&& ($new_ngay_giam_new_ngay_thoi_kh$$=new Date($data$$.ngay_giam),!underscore.isEqual($new_ngay_giam_new_ngay_thoi_kh$$,$obj$$.ngay_giam)&&$new_ngay_giam_new_ngay_thoi_kh$$.getTime()<$_kh_ngay_kh_moi_nhat_user$$.getTime()))return $next$$("Ng\u00e0y gi\u1ea3m t\u00e0i s\u1ea3n ph\u1ea3i l\u1edbn h\u01a1n ng\u00e0y tr\u00edch kh\u1ea5u hao g\u1ea7n nh\u1ea5t.");if($data$$.so_the_ts&&$data$$.so_the_ts!==$obj$$.so_the_ts||$data$$.details&&!underscore.isEqual($data$$.details,JSON.parse(JSON.stringify($obj$$.details)))|| $data$$.pp_tinh_kh&&$data$$.pp_tinh_kh!==$obj$$.pp_tinh_kh||$data$$.ngay_kh&&!underscore.isEqual(new Date($data$$.ngay_kh),$obj$$.ngay_kh)||$data$$.so_ky_kh&&$data$$.so_ky_kh!==$obj$$.so_ky_kh)return $next$$("Kh\u00f4ng th\u1ec3 c\u1eadp nh\u1eadt t\u00e0i s\u1ea3n n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c tr\u00edch kh\u1ea5u hao.")}$next$$(null,$data$$,$obj$$)};$contr_router$$.deleting=function($user$$,$obj$$,$next$$){sotinhkh.find({id_ts:$obj$$._id}).limit(1).exec(function($error$$,$ts$$){if($error$$)return $next$$($error$$); if($ts$$&&$ts$$.length>0)return $next$$("Kh\u00f4ng th\u1ec3 x\u00f3a t\u00e0i s\u1ea3n n\u00e0y do \u0111\u00e3 \u0111\u01b0\u1ee3c tr\u00edch kh\u1ea5u hao.");$next$$(null,$obj$$)})};$contr_router$$.view=function($user$$,$items$$,$fn$$){let $id_app$$=$user$$.current_id_app;async.parallel({dv:function($callback$$){$items$$.filter($item$$=>$item$$.ma_dvcs).joinModel($id_app$$,dvcs,[{akey:"ma_dvcs",bkey:"_id",fields:[{name:"ten_dvcs",value:"ten_dvcs"}]}],function(){$callback$$()})},tk:function($callback$$){$items$$.filter($item$$=> $item$$.tk_ts).joinModel($id_app$$,account,[{akey:"tk_ts",bkey:"tk",fields:[{name:"ten_tk_ts",value:"ten_tk"}]},{akey:"tk_kh",bkey:"tk",fields:[{name:"ten_tk_kh",value:"ten_tk"}]},{akey:"tk_cp",bkey:"tk",fields:[{name:"ten_tk_cp",value:"ten_tk"}]}],function(){$callback$$()})},bp:function($callback$$){$items$$.filter($item$$=>$item$$.ma_bp).joinModel($id_app$$,bophan,[{akey:"ma_bp",bkey:"ma_bp",fields:[{name:"ten_bp",value:"ten_bp"}]}],function(){$callback$$()})},phi:function($callback$$){$items$$.filter($item$$=> $item$$.ma_phi).joinModel2($id_app$$,dmphi,{where:"ma_phi",fields:"ten_phi"},function(){$callback$$()})},hd:function($callback$$){$items$$.filter($item$$=>$item$$.ma_hd).joinModel2($id_app$$,contract,{where:{ma_hd:"so_hd"},fields:"ten_hd"},function(){$callback$$()})},kho:function($callback$$){$items$$.filter($item$$=>$item$$.ma_kho).joinModel2($id_app$$,dmkho,{where:{ma_kho:"ma_kho"},fields:"ten_kho"},function(){$callback$$()})},loaits:function($callback$$){$items$$.filter($item$$=>$item$$.ma_loai_ts).joinModel($id_app$$, loaits,[{akey:"ma_loai_ts",bkey:"ma_loai_ts",fields:[{name:"ten_loai_ts",value:"ten_loai_ts"}]}],function(){$callback$$()})},tanggiamts:function($callback$$){$items$$.filter($item$$=>$item$$.ma_tang_giam_ts).joinModel($id_app$$,tanggiamts,[{akey:"ma_tang_giam_ts",bkey:"ma_tang_giam_ts",fields:[{name:"ten_tang_giam_ts",value:"ten_tang_giam_ts"}]}],function(){$callback$$()})},t_nguon_von:function($callback$$){$items$$.forEach(function($r$$){$r$$.details&&($r$$.t_nguyen_gia=$r$$.details.csum("nguyen_gia"), $r$$.t_gia_tri_da_kh=$r$$.details.csum("gia_tri_da_kh"),$r$$.t_gia_tri_con_lai=$r$$.details.csum("gia_tri_con_lai"),$r$$.t_gia_tri_kh_ky=$r$$.details.csum("gia_tri_kh_ky"))});$callback$$()},details_nguon_von:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||[]).filter($item$$=>$item$$.ma_nguon_von).joinModel($id_app$$,nguonvon,[{akey:"ma_nguon_von",bkey:"ma_nguon_von",fields:[{name:"ten_nguon_von",value:"ten_nguon_von"}]}],function(){$callback1$$()})},function(){$callback$$()})}}, function(){$fn$$(null,$items$$)})}};