UNPKG

flexbiz-server

Version:

Flexible Server

7 lines (6 loc) 2.57 kB
const bangtinhluong=global.getModel("bangtinhluong"),dmnv=global.getModel("dmnv"),dmbp=global.getModel("dmbp"),controller=require("../../controllers/controllerRPT"),async=require("async"); module.exports=function($router$$){new controller($router$$,"sctluong",async($req$$,$callback$$)=>{var $chiTietCacDotDaChi_thang$$=Number($req$$.query.thang);let $nam$$=Number($req$$.query.nam),$id_app$$=$req$$.user.current_id_app;if(!$chiTietCacDotDaChi_thang$$||!$nam$$)return $callback$$("T\u00ednh n\u0103ng n\u00e0y y\u00eau c\u1ea7u hai tham s\u1ed1 thang v\u00e0 nam");if($chiTietCacDotDaChi_thang$$<1||$chiTietCacDotDaChi_thang$$>12||$nam$$<1E3||$nam$$>3E3)return $callback$$("Tham s\u1ed1 thang ho\u1eb7c nam kh\u00f4ng ph\u00f9 h\u1ee3p"); var $dsNhanVien_query$$={thang:$chiTietCacDotDaChi_thang$$,nam:$nam$$,id_app:$id_app$$};$req$$.query.ma_nv&&($dsNhanVien_query$$.ma_nv=$req$$.query.ma_nv);$req$$.query.ma_bp&&($dsNhanVien_query$$.ma_bp=$req$$.query.ma_bp);let $bangLuong$$=await bangtinhluong.find($dsNhanVien_query$$).sort({nam:1,thang:1,ma_nv:1}).lean();$dsNhanVien_query$$=$bangLuong$$.map($i$$=>$i$$.ma_nv);$chiTietCacDotDaChi_thang$$=await global.getModel("pcl").find({id_app:$id_app$$,ky:$chiTietCacDotDaChi_thang$$,nam:$nam$$,"details.ma_nv":{$in:$dsNhanVien_query$$}}).lean(); const $dsDotChi$$=[...(new Set($chiTietCacDotDaChi_thang$$.map($d$$=>$d$$.dot)))].sort(($a$$,$b$$)=>$a$$-$b$$);let $mapChiTiet$$={};$chiTietCacDotDaChi_thang$$.forEach($phieu$$=>{let $fieldName$$=`chi_dot_${$phieu$$.dot}`;$phieu$$.details&&Array.isArray($phieu$$.details)&&$phieu$$.details.forEach($d$$=>{let $amount$$=$d$$.tien_nt||0;$mapChiTiet$$[$d$$.ma_nv]||($mapChiTiet$$[$d$$.ma_nv]={total_da_chi:0});$mapChiTiet$$[$d$$.ma_nv][$fieldName$$]||($mapChiTiet$$[$d$$.ma_nv][$fieldName$$]=0);$mapChiTiet$$[$d$$.ma_nv][$fieldName$$]+= $amount$$;$mapChiTiet$$[$d$$.ma_nv].total_da_chi+=$amount$$})});$bangLuong$$.forEach($row$$=>{$dsDotChi$$.forEach($dot$$=>{$row$$[`chi_dot_${$dot$$}`]=0});let $tongDaChi$$=0;$mapChiTiet$$[$row$$.ma_nv]&&(Object.assign($row$$,$mapChiTiet$$[$row$$.ma_nv]),$tongDaChi$$=$mapChiTiet$$[$row$$.ma_nv].total_da_chi||0);$row$$.luong_con_lai=($row$$.tong_luong||0)-$tongDaChi$$});async.parallel({nv:async()=>{await $bangLuong$$.asyncJoinModel2($req$$.user.current_id_app,dmnv,[{where:{ma_nv:"ma_nv"},fields:["ten_nv", "ttncn_mst"]}]);return null},bp:async()=>{await $bangLuong$$.asyncJoinModel2($req$$.user.current_id_app,dmbp,[{where:{ma_bp:"ma_bp"},fields:["ten_bp"]}]);return null}},()=>{$callback$$(null,{data:$bangLuong$$,columns:$dsDotChi$$})})})};