flexbiz-server
Version:
Flexible Server
14 lines (13 loc) • 5.96 kB
JavaScript
'use strict';const dvcs=global.getModel("dvcs"),dmvt=global.getModel("dmvt"),dmdt=global.getModel("dmdt"),customer=global.getModel("customer"),lenhsx=global.getModel("lenhsx"),congdoansx=global.getModel("congdoansx"),purchase_contract=global.getModel("purchase_contract"),dnm=global.getModel("dnm"),sale_contract=global.getModel("sale_contract"),model=global.getModel("kehoachmh"),trangthai=global.getModel("trangthai"),Ckvt=require("../../libs/ckvt"),controller=require("../../controllers/controller"),
async=require("async"),ma_ct="KEHOACHMH";
module.exports=function($contr_router$$){$contr_router$$=new controller($contr_router$$,model,ma_ct.toLowerCase(),{isVoucher:!0,check_stock:!1,sort:{ngay_ct:-1,so_ct:-1}});$contr_router$$.route();const $valid$$=function($user$$,$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.creating=function($user$$,$obj$$,$next$$){$obj$$.details||($obj$$.details=[]);$valid$$($user$$,$obj$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$obj$$)})};$contr_router$$.created=function($user$$,
$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.updating=function($user$$,$data$$,$obj$$,$next$$){$valid$$($user$$,$data$$,function($error$$){if($error$$)return $next$$($error$$);$next$$(null,$data$$,$obj$$)})};$contr_router$$.updated=function($user$$,$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.deleting=function($user$$,$obj$$,$next$$){$next$$(null,$obj$$)};$contr_router$$.view=function($user$$,$items$$,$fn$$){let $id_app$$=$user$$.current_id_app;async.series({dv:function($callback$$){$items$$.joinModel($id_app$$,
dvcs,[{akey:"ma_dvcs",bkey:"_id",fields:[{name:"ten_dvcs",value:"ten_dvcs"}]}],function(){$callback$$()})},dt:function($callback$$){$items$$.joinModel2($id_app$$,dmdt,[{where:"ma_dt",fields:["ten_dt"]}],function(){$callback$$()})},trangthai:function($callback$$){$items$$.joinModel2(null,trangthai,[{where:{trang_thai:"ma_trang_thai",ma_ct:"ma_ct"},fields:["ten_trang_thai","color"]}],function(){$callback$$()})},tinhtongcong:function($callback$$){async.each($items$$,function($r$$,$callback1$$){const $details$$=
$r$$.details||[];$r$$.t_sl_nhu_cau=$details$$.csum("sl_nhu_cau");$r$$.t_sl_da_cap_phat=$details$$.csum("sl_da_cap_phat");$r$$.t_sl_ke_hoach=$details$$.csum("sl_ke_hoach");$r$$.t_tien_nt=$details$$.csum("tien_nt");$r$$.t_tien=$details$$.csum("tien");$callback1$$()},function(){$callback$$()})},details_vt:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,dmvt,[{where:{ma_vt:"ma_vt"},fields:{ten_vt:"ten_vt"}}],function(){$callback1$$()})},function(){$callback$$()})},
details_tonkho:$callback$jscomp$0$$=>{async.each($items$$,($r$jscomp$0$$,$callback$$)=>{Promise.all(($r$jscomp$0$$.details||[]).map($detail$$=>(async()=>{const $condition$$={id_app:$r$jscomp$0$$.id_app,ma_vt:$detail$$.ma_vt,ngay:$r$jscomp$0$$.ngay_ct};try{let $rs$$=await Ckvt($condition$$);$detail$$.ton=$rs$$.map($r$$=>$r$$.ton00).reduce(($a$$,$b$$)=>$a$$+$b$$,0)}catch($e$$){console.error($e$$)}})())).then(()=>{$callback$$()})},function(){$callback$jscomp$0$$()})},details_sp:function($callback$$){async.each($items$$,
function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,dmvt,[{where:{ma_sp:"ma_vt"},fields:{ten_sp:"ten_vt"}}],function(){$callback1$$()})},function(){$callback$$()})},details_congdoan:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,congdoansx,[{where:{ma_cong_doan:"ma_cong_doan"},fields:{ten_cong_doan:"ten_cong_doan"}}],function(){$callback1$$()})},function(){$callback$$()})},details_kh:function($callback$$){async.each($items$$,
function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,customer,[{where:{ma_kh:"ma_kh"},fields:{ten_kh:"ten_kh"}}],function(){$callback1$$()})},function(){$callback$$()})},details_ncc:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,customer,[{where:{ma_ncc:"ma_kh"},fields:{ten_ncc:"ten_kh"}}],function(){$callback1$$()})},function(){$callback$$()})},details_lenhsx:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||
[]).joinModel2($id_app$$,lenhsx,[{where:{id_lenhsx:"_id"},fields:[{so_ct_lenhsx:"so_ct"}]}],function(){$callback1$$()})},function(){$callback$$()})},details_contract:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,sale_contract,[{where:{id_contract:"_id"},fields:[{so_ct_contract:"so_ct"}]}],function(){$callback1$$()})},function(){$callback$$()})}},function(){async.mapSeries($items$$,($kehoach$$,$callback$jscomp$1$$)=>{if(!$kehoach$$.details)return $callback$jscomp$1$$();
async.series([$callback$jscomp$0$$=>{purchase_contract.find({id_kehoachmh:$kehoach$$._id}).lean().exec(($e$jscomp$0$$,$contracts$$)=>{async.mapSeries($contracts$$,($contract$$,$callback$$)=>{purchase_contract.tinhSoLuongDaNhap($contract$$,($e$$,$_contract$$)=>{$callback$$(null,$_contract$$)})},($e$$,$_contracts$$)=>{$kehoach$$.details.forEach($d$$=>{$d$$.sl_da_nhap=$_contracts$$.map($_contr$$=>$_contr$$.details.filter($_d$$=>$_d$$.ma_vt===$d$$.ma_vt).csum("sl_da_nhap")).reduce(($a$$,$b$$)=>$a$$+$b$$,
0);$d$$.sl_contract=$_contracts$$.map($_contr$$=>$_contr$$.details.filter($_d$$=>$_d$$.ma_vt===$d$$.ma_vt).csum("so_luong")).reduce(($a$$,$b$$)=>$a$$+$b$$,0)});$callback$jscomp$0$$()})})},$callback$$=>{dnm.find({id_kehoachmh:$kehoach$$._id}).lean().exec(($e$$,$dnms$$)=>{$kehoach$$.details.forEach($d$$=>{$d$$.sl_dnm=$dnms$$.map($_dnm$$=>$_dnm$$.details.filter($_d$$=>$_d$$.ma_vt===$d$$.ma_vt).csum("so_luong")).reduce(($a$$,$b$$)=>$a$$+$b$$,0)});$callback$$()})}],()=>{$kehoach$$.t_sl_contract=$kehoach$$.details.csum("sl_contract");
$kehoach$$.t_sl_da_nhap=$kehoach$$.details.csum("sl_da_nhap");$kehoach$$.t_sl_dnm=$kehoach$$.details.csum("sl_dnm");$callback$jscomp$1$$(null,$kehoach$$)})},()=>{$fn$$(null,$items$$)})})}};