UNPKG

flexbiz-server

Version:

Flexible Server

13 lines (12 loc) 5.39 kB
'use strict';const dvcs=global.getModel("dvcs"),dmvt=global.getModel("dmvt"),dmdt=global.getModel("dmdt"),dmbp=global.getModel("dmbp"),customer=global.getModel("customer"),congdoansx=global.getModel("congdoansx"),dinhmucsx=global.getModel("dinhmucsx"),sale_contract=global.getModel("sale_contract"),model=global.getModel("lenhsx"),Sosanxuat=global.getModel("sosanxuat"),trangthai=global.getModel("trangthai"),controller=require("../../controllers/controller"),PostBook=require("../../libs/post-book"), async=require("async"),lenhcapphat=global.getModel("lenhcapphat"),ma_ct="LENHSX"; 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$$.post=function($obj$$,$fn$$){$fn$$||($fn$$=function(){});async.series({post_sosanxuat:function($callback$jscomp$0$$){let $details$$= [];($obj$$.details||[]).forEach($detail$$=>{let $_detail$$={...$detail$$};delete $_detail$$.sl_ht;($_detail$$.cong_doan_sx||[]).forEach($congdoan$$=>{$details$$.push({...$_detail$$,...$congdoan$$,id_lenhsx:$obj$$._id.toString()})})});(new PostBook($obj$$,$details$$,Sosanxuat,function($detail$$,$callback$$){$callback$$($detail$$)})).run(function($e$$,$rs$$){$callback$jscomp$0$$($e$$,$rs$$)})}},function($e$$,$rs$$){$fn$$($e$$,$rs$$)})};$contr_router$$.view=function($user$$,$items$$,$fn$$){let $id_app$$= $user$$.current_id_app;async.series({dv:function($callback$$){$items$$.joinModel2($id_app$$,dvcs,[{where:$item$$=>({_id:$item$$.ma_dvcs}),fields:["ten_dvcs"]}],function(){$callback$$()})},cust:function($callback$$){$items$$.filter($i$$=>$i$$.ma_kh).joinModel2($id_app$$,customer,[{where:$item$$=>({ma_kh:$item$$.ma_kh}),fields:["ten_kh"]}],function(){$callback$$()})},dt:function($callback$$){$items$$.filter($i$$=>$i$$.ma_dt).joinModel2($id_app$$,dmdt,[{where:$item$$=>({ma_dt:$item$$.ma_dt}),fields:["ma_dt"]}], function(){$callback$$()})},trangthai:function($callback$$){$items$$.joinModel2(null,trangthai,[{where:$item$$=>({ma_trang_thai:$item$$.trang_thai,ma_ct:$item$$.ma_ct}),fields:["ten_trang_thai","color"]}],function(){$callback$$()})},t_tien:function($callback$$){$items$$.forEach($r$$=>{$r$$.t_sl=($r$$.details||[]).map($d$$=>$d$$.sl_sx||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0)});$callback$$()},details_vt:function($callback$$){async.each($items$$,function($r$$,$callback1$$){($r$$.details||[]).joinModel2($id_app$$, dmvt,[{where:{ma_sp:"ma_vt"},fields:[{ten_sp:"ten_vt"},{tk_vt:"tk_vt"}]}],function(){$callback1$$()})},function(){$callback$$()})},sale_contract:function($callback$$){$items$$.filter($i$$=>$i$$.id_contract).joinModel2($id_app$$,sale_contract,{where:{id_contract:"_id"},fields:{so_ct_contract:"so_ct"}},function(){$callback$$()})},capphat:function($callback$$){$items$$.joinModel2($id_app$$,lenhcapphat,{where:{_id:"id_lenhsx"},fields:{id_lenhcapphat:"_id"}},function(){$callback$$()})},dinhmucsx:function($callback$$){async.each($items$$, function($r$$,$callback1$$){($r$$.details||[]).filter($i$$=>$i$$.id_dinhmucsx).joinModel2($id_app$$,dinhmucsx,{where:{id_dinhmucsx:"_id"},fields:{so_ct_dinhmucsx:"so_ct"}},function(){$callback1$$()})},function(){$callback$$()})},congdoansx:function($callback$jscomp$2$$){async.each($items$$,function($r$$,$callback$jscomp$1$$){const $id_app$$=$r$$.id_app,$id_lenhsx$$=$r$$._id.toString();async.mapSeries($r$$.details||[],($detail$$,$callback$jscomp$0$$)=>{const $cong_doan_sx$$=$detail$$.cong_doan_sx|| [],$ma_sp$$=$detail$$.ma_sp;async.series({cd:$callback$$=>{$cong_doan_sx$$.joinModel2(null,trangthai,{where:$item$$=>({ma_trang_thai:$item$$.trang_thai,ma_ct:"CONGDOANSX"}),fields:["ten_trang_thai","color"]},function(){$callback$$()})},tt:$callback$$=>{$cong_doan_sx$$.joinModel2($id_app$$,congdoansx,{where:{ma_cong_doan:"ma_cong_doan"},fields:["ten_cong_doan"]},function(){$callback$$()})},bp:$callback$$=>{$cong_doan_sx$$.joinModel2($id_app$$,dmbp,{where:{ma_bp:"ma_bp"},fields:{ten_bp:"ten_bp"}},function(){$callback$$()})}, ht:$callback$$=>{const $tinh_sl_ht$$=$cong_doan_sx$$.map($cong_doan$$=>new Promise($resolve$$=>{Sosanxuat.find({id_app:$id_app$$,id_lenhsx:$id_lenhsx$$,ma_sp:$ma_sp$$,ma_cong_doan:$cong_doan$$.ma_cong_doan}).lean().exec(($e$$,$rs$$)=>{$e$$&&console.error($e$$);$cong_doan$$.sl_ht=$rs$$.map($s$$=>$s$$.sl_ht||0).reduce(($a$$,$b$$)=>$a$$+$b$$,0);$resolve$$()})}));Promise.all($tinh_sl_ht$$).then(()=>{$detail$$.sl_ht=$cong_doan_sx$$.filter($c$$=>$c$$.cong_doan_cuoi).map($c$$=>$c$$.sl_ht).reduce(($a$$,$b$$)=> $a$$+$b$$,0);$callback$$()})}},()=>{$callback$jscomp$0$$()})},()=>{$callback$jscomp$1$$()})},function(){$callback$jscomp$2$$()})}},function(){$fn$$(null,$items$$)})}};