UNPKG

flexbiz-server

Version:

Flexible Server

10 lines (9 loc) 4.25 kB
const PostBook=require("../../libs/post-book"),dmvt=global.getModel("dmvt"),dmdt=global.getModel("dmdt"),congdoansx=global.getModel("congdoansx"),lenhsx=global.getModel("lenhsx"),sokhocapphat=global.getModel("sokhocapphat"),dmkho=global.getModel("dmkho"),sale_contract=global.getModel("sale_contract"),pxk=global.getModel("pxk"),model=global.getModel("lenhcapphat"),trangthai=global.getModel("trangthai"),controller=require("../../controllers/controller"),async=require("async"),ma_ct="LENHCAPPHAT"; 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();$contr_router$$.post=function($obj$$,$fn$$){$fn$$||=function(){};async.series({post_sokhocapphat:function($callback$$){(new PostBook($obj$$,$obj$$.details,sokhocapphat,function($detail$$,$callback$$){$detail$$.nxt=1;$detail$$.ma_kho=$obj$$.ma_kho||$detail$$.ma_kho;$detail$$.he_so_qd=1;$detail$$.sl_nhap=$detail$$.so_luong; $detail$$.sl_nhap_qd=$detail$$.sl_nhap*$detail$$.he_so_qd;$detail$$.id_lenhcapphat=$obj$$._id.toString();$obj$$.ma_gd=="1"&&($detail$$.pn_gia_tb=!1);$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$$($e$$,$rs$$)})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};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$$){const $id_app$$= $user$$.current_id_app;async.parallel({dt:function($callback$$){$items$$.joinModel($id_app$$,dmdt,[{akey:"ma_dt",bkey:"ma_dt",fields:[{name:"ten_dt",value:"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$$()})},kho:function($callback$$){$items$$.joinModel2(null,dmkho,[{where:{ma_kho:"ma_kho"},fields:["ten_kho"]}],function(){$callback$$()})}, tinhtong:function($callback$$){$items$$.forEach($item$$=>{$item$$.t_sl=($item$$.details||[]).csum("so_luong")});$callback$$()},details_vt:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$,dmvt,[{where:"ma_vt",fields:["ten_vt","tk_vt"]}],function(){$callback1$$()})},function(){$callback$$()})},san_pham: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$$()})},sale_contract:function($callback$$){$items$$.joinModel2($id_app$$,sale_contract,{where:{id_contract:"_id"},fields:{so_ct_contract:"so_ct"}},function(){$callback$$()})},lenhsx:function($callback$$){$items$$.joinModel2($id_app$$,lenhsx,{where:{id_lenhsx:"_id"},fields:{so_lenh_sx:"so_ct"}},function(){$callback$$()})},congdoansx: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$$()})}},function(){async.mapLimit($items$$,20,($item$$,$callback$$)=>{pxk.find({id_lenhcapphat:$item$$._id,id_app:$item$$.id_app}).lean().exec(($e$$,$pxks$$)=>{if(!$item$$.details)return $callback$$();$item$$.details.forEach($d$$=>{$d$$.sl_da_xuat=$pxks$$.map($xk$$=>$xk$$.details.filter($_d$$=>$d$$.ma_vt===$_d$$.ma_vt).csum("sl_xuat")).reduce(($a$$,$b$$)=>$a$$+ $b$$,0)});$item$$.t_sl_da_xuat=$pxks$$.map($xk$$=>$xk$$.details.csum("sl_xuat")).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$callback$$()})},()=>{$fn$$(null,$items$$)})})}};