flexbiz-server 12.4.20 → 12.4.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -14,26 +14,26 @@ sip_password:0,sip_websocket:0,voiip_access_token:0,voiip_webhook:0,wallet_addre
|
|
|
14
14
|
!1);let $condition$$={};$unique$$=$unique$$.filter($key$$=>$model$$.schema.paths[$key$$]);for(let $i$$=0;$i$$<$unique$$.length;$i$$++){let $key$$=$unique$$[$i$$],$v$$=$obj$$[$key$$];$v$$!=void 0&&$model$$.schema.paths[$key$$]&&($v$$!==null?$model$$.schema.paths[$key$$].instance.toLowerCase()=="number"?$condition$$[$key$$]=Number($v$$):$model$$.schema.paths[$key$$].instance.toLowerCase()=="date"?$condition$$[$key$$]=new Date($v$$):$model$$.schema.paths[$key$$].instance.toLowerCase()=="string"?$condition$$[$key$$]=
|
|
15
15
|
$v$$.toString():$condition$$[$key$$]=$v$$:$condition$$[$key$$]=$v$$)}if($condition$$.id_app&&Object.values($condition$$).length<2||Object.values($condition$$).length<1)return $fn$$("D\u1eef li\u1ec7u kh\u00f4ng t\u1ed3n t\u1ea1i \u0111\u1ee7 c\u00e1c gi\u00e1 tr\u1ecb: "+$unique$$.join(","));$obj$$.id_app&&($condition$$.id_app=$obj$$.id_app);$replaceIfExists$$?$model$$.deleteMany($condition$$,$e$$=>{setImmediate(()=>{if($e$$)return $fn$$($e$$);$fn$$(null,!1)})}):$model$$.findOne($condition$$,{_id:1},
|
|
16
16
|
($error$$,$result$$)=>{setImmediate(()=>{if($error$$)return $fn$$($error$$);if(!$result$$)return $fn$$(null,!1);console.warn("object exists",$condition$$,"unique fields",$unique$$);$fn$$(null,!0)})})},postData=function($obj$$,$ctrl$$,$mainCallback$$,$options$$={}){const $post$$=$ctrl$$.dynamicPost||$ctrl$$.post;if(!$post$$)return $mainCallback$$();const $postOptions$$={...$ctrl$$.options,...$options$$};let $obj_created$$=$obj$$.toObject?utils.convertObjectIdsToStrings($obj$$.toObject()):{...$obj$$};
|
|
17
|
-
Object.keys($obj_created$$).forEach($key$$=>{let $p$$=$obj_created$$[$key$$];$p$$&&($p$$.toObject&&($obj_created$$[$key$$]=utils.convertObjectIdsToStrings($p$$.toObject())),_.isArray($p$$)&&($obj_created$$[$key$$]=$p$$.map($el$$=>$el$$?$el$$.toObject?utils.convertObjectIdsToStrings($el$$.toObject()):$el$$:$el$$)))});$ctrl$$.beforePost||($ctrl$$.beforePost=function($obj_created$$,$callback$$){$callback$$(null,null)});$ctrl$$.beforePost($obj_created$$,function($e$$){
|
|
18
|
-
|
|
19
|
-
$
|
|
20
|
-
|
|
21
|
-
$
|
|
22
|
-
$
|
|
23
|
-
$
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
$
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
17
|
+
Object.keys($obj_created$$).forEach($key$$=>{let $p$$=$obj_created$$[$key$$];$p$$&&($p$$.toObject&&($obj_created$$[$key$$]=utils.convertObjectIdsToStrings($p$$.toObject())),_.isArray($p$$)&&($obj_created$$[$key$$]=$p$$.map($el$$=>$el$$?$el$$.toObject?utils.convertObjectIdsToStrings($el$$.toObject()):$el$$:$el$$)))});$ctrl$$.beforePost||($ctrl$$.beforePost=function($obj_created$$,$callback$$){$callback$$(null,null)});$ctrl$$.beforePost($obj_created$$,function($e$$){if($e$$)return $mainCallback$$($e$$);
|
|
18
|
+
deletePost($obj_created$$.user_updated,$obj_created$$,async function($e$$,$_obj$$,$oldBooks$$){if($e$$)return $mainCallback$$($e$$);let $oldDanamicBook$$={};const $callback$$=async($e$$,$rs$$)=>{if($e$$){for(let $key$$ in $oldBooks$$)$oldBooks$$[$key$$].length>0&&await global.getModel($key$$.toLowerCase()).insertMany($oldBooks$$[$key$$]);if($oldDanamicBook$$)for(let $key$$ in $oldDanamicBook$$)!$oldBooks$$[$key$$]&&$oldDanamicBook$$[$key$$].length>0&&await global.getModel($key$$.toLowerCase()).insertMany($oldDanamicBook$$[$key$$])}$mainCallback$$($e$$,
|
|
19
|
+
$rs$$)};if($ctrl$$.dynamicDeletePost)try{$oldDanamicBook$$=await $ctrl$$.dynamicDeletePost($obj_created$$)}catch($e$$){return $callback$$($e$$.message||$e$$.error||$e$$)}setImmediate(async()=>{if($obj_created$$.trang_thai||$obj_created$$.trang_thai==0)$obj_created$$.trang_thai=$obj_created$$.trang_thai.toString();const $listinfo_code$$=$obj_created$$.listinfo_code;let $trang_thai$$;if($obj_created$$.trang_thai&&($listinfo_code$$&&($trang_thai$$=await trangthai.findOne({ma_ct:($listinfo_code$$||"").toUpperCase(),
|
|
20
|
+
ma_trang_thai:$obj_created$$.trang_thai})),$trang_thai$$||=await trangthai.findOne({ma_ct:($obj_created$$.ma_ct||"").toUpperCase(),ma_trang_thai:$obj_created$$.trang_thai}),!$trang_thai$$||!$trang_thai$$.post))return console.log("[controllerUtils][postData] tr\u1ea1ng th\u00e1i kh\u00f4ng t\u1ed3n t\u1ea1i ho\u1eb7c kh\u00f4ng y\u00eau c\u1ea7u post d\u1eef li\u1ec7u",$listinfo_code$$,$obj_created$$.trang_thai),$callback$$();setImmediate(()=>{console.log("[controllerUtils][postData] run posting ...",
|
|
21
|
+
$ctrl$$.name,$obj_created$$.so_ct,$obj_created$$.trang_thai,$obj_created$$.listinfo_code,$e$$);$post$$($obj_created$$,function($e$$,$rs$$){setImmediate(()=>{if($e$$){let $msg_error$$=$e$$.message||$e$$.error||$e$$.toString();if($msg_error$$.indexOf("E11000 duplicate")>=0&&($msg_error$$.indexOf("id_ct_1_nh_dk_1")>=0||$msg_error$$.indexOf("id_ct_1_line_1")>=0))return $callback$$(null,$rs$$);console.error("[controllerUtils] [postData] error post data",$obj_created$$._id,$msg_error$$);deletePost($obj_created$$.user_updated,
|
|
22
|
+
$obj_created$$,async function($e_delete_post$$){if($e_delete_post$$)return console.error("error delete post",$e_delete_post$$),$callback$$($e_delete_post$$);if($ctrl$$.dynamicDeletePost)try{return await $ctrl$$.dynamicDeletePost($obj_created$$),$callback$$($e$$.message||$e$$.error||$e$$)}catch($e$$){return console.error("[dynamicDeletePost]",$e$$.message||$e$$.error||$e$$),$callback$$($e$$)}else $callback$$($e$$)})}else $callback$$(null,$rs$$)})},{...$postOptions$$})})})})})},pushNotification=($ctrl$$,
|
|
23
|
+
$_obj$$,$eventName$$="new",$old_obj$$=null,$justFireEvents$$=!1,$options$$={title:"",data:{}})=>{let $obj$$;$obj$$=$_obj$$.toObject?utils.convertObjectIdsToStrings($_obj$$.toObject()):{...$_obj$$};$obj$$._id&&($obj$$._id=$obj$$._id.toString());setImmediate(async()=>{var $channel_id_func_old_trang_thai$$=`${$ctrl$$.name.toLowerCase()}-${$eventName$$}`;$_obj$$.id_app&&($channel_id_func_old_trang_thai$$=`${$channel_id_func_old_trang_thai$$}-${$_obj$$.id_app}`);let $message$$=JSON.stringify($obj$$);try{await global.clientRedis.publish($channel_id_func_old_trang_thai$$,
|
|
24
|
+
$message$$)}catch($e$$){console.error("[controllerUtiles][pushNotification] fire event",$eventName$$,$e$$)}if($_obj$$.status){$options$$||={};var $exclude_token$$=$options$$.exclude_token,$trang_thai$$=($obj$$.trang_thai||($obj$$.progress==0?"0":$obj$$.progress)||"").toString();$old_obj$$&&!$justFireEvents$$&&($channel_id_func_old_trang_thai$$=($old_obj$$.trang_thai||($old_obj$$.progress==0?"0":$old_obj$$.progress)||"").toString(),$channel_id_func_old_trang_thai$$===$trang_thai$$&&($justFireEvents$$=
|
|
25
|
+
!0));var $listinfos_notifications_setting_users_receive_notification_v_options$$;($channel_id_func_old_trang_thai$$=$obj$$.listinfo_code)&&($listinfos_notifications_setting_users_receive_notification_v_options$$=await OptionsModel.find({id_app:$obj$$.id_app,id_func:$channel_id_func_old_trang_thai$$,option:{$exists:!0}},{option:1}).lean());$channel_id_func_old_trang_thai$$&&$listinfos_notifications_setting_users_receive_notification_v_options$$.length!=0||($listinfos_notifications_setting_users_receive_notification_v_options$$=
|
|
26
|
+
(await global.getModel("listinfo").find({api_code:$ctrl$$.name},{code:1}).lean()).map($a$$=>$a$$.code),$listinfos_notifications_setting_users_receive_notification_v_options$$.push($ctrl$$.name),$listinfos_notifications_setting_users_receive_notification_v_options$$=[...(new Set($listinfos_notifications_setting_users_receive_notification_v_options$$))],$channel_id_func_old_trang_thai$$={$in:$listinfos_notifications_setting_users_receive_notification_v_options$$},$listinfos_notifications_setting_users_receive_notification_v_options$$=
|
|
27
|
+
await OptionsModel.find({id_app:$obj$$.id_app,id_func:$channel_id_func_old_trang_thai$$,option:{$exists:!0}},{option:1}).lean());$listinfos_notifications_setting_users_receive_notification_v_options$$=$listinfos_notifications_setting_users_receive_notification_v_options$$.filter($v$$=>$v$$.option&&$v$$.option.users_receive_notification).map($v$$=>$v$$.option.users_receive_notification).reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);$listinfos_notifications_setting_users_receive_notification_v_options$$.length===
|
|
28
|
+
0&&($listinfos_notifications_setting_users_receive_notification_v_options$$=await Promise.all((($ctrl$$.options||{}).users_receive_notification_default||[]).map($n$$=>new Promise($resolve$$=>{setImmediate(async()=>{_.isFunction($n$$)?$resolve$$(await $n$$($obj$$)):$resolve$$($n$$)})}))));$listinfos_notifications_setting_users_receive_notification_v_options$$&&($listinfos_notifications_setting_users_receive_notification_v_options$$=$listinfos_notifications_setting_users_receive_notification_v_options$$.filter($u$$=>
|
|
29
|
+
!$u$$.trang_thai||$u$$.trang_thai.toString()===$trang_thai$$||$u$$.trang_thai.toString()==="delete"),$listinfos_notifications_setting_users_receive_notification_v_options$$=$listinfos_notifications_setting_users_receive_notification_v_options$$.filter($func_string_n$$=>{if(!$func_string_n$$.condition||!$func_string_n$$.condition.trim())return!0;$func_string_n$$=$func_string_n$$.condition;$func_string_n$$.indexOf("return ")<0&&($func_string_n$$=`return ${$func_string_n$$}`);try{return evalute($func_string_n$$,
|
|
30
|
+
{...$obj$$,master:$obj$$,data:$obj$$})}catch($e$$){return console.error("[controllerUtiles][pushNotification]",$e$$,$func_string_n$$),!1}}),$listinfos_notifications_setting_users_receive_notification_v_options$$.forEach(async $p$$=>{setImmediate(async()=>{let $_justFireEvents$$=$justFireEvents$$;$p$$.just_fire_events&&($_justFireEvents$$=$p$$.just_fire_events);let $notificationData$$={_id:$obj$$._id,trang_thai:$trang_thai$$,id_app:$obj$$.id_app,code:$obj$$.ma_ct||$ctrl$$.name,id_link:$obj$$.id_link||
|
|
31
|
+
$obj$$.id_product,code_link:$obj$$.code_link||$obj$$.colection_name_product,event:$eventName$$,action:$eventName$$.toUpperCase(),data:$options$$.data,key:$options$$.key,not_send_email:$p$$.not_send_email,justFireEvents:$_justFireEvents$$};if($justFireEvents$$||$eventName$$.toLowerCase()==="delete")$notificationData$$.title="",$notificationData$$.body="";else{try{$notificationData$$.body=$p$$.body?compileHtml($p$$.body,$obj$$):""}catch($e$$){$notificationData$$.body=$p$$.body,console.error("[controllerUtiles][pushNotification]",
|
|
32
|
+
$ctrl$$.name,"compileHtml body",$e$$,$obj$$)}try{$notificationData$$.title=stripHtmlTags(compileHtml($options$$.title||$p$$.title||$p$$.content||"",$obj$$))}catch($e$$){$notificationData$$.title=$options$$.title||$p$$.title||$p$$.content||"",console.error("[controllerUtiles][pushNotification]",$ctrl$$.name,"compileHtml title",$e$$,$obj$$)}}if($p$$.email_content)try{$notificationData$$.email_content=compileHtml($p$$.email_content,$obj$$)}catch($e$$){$notificationData$$.email_content=$p$$.email_content,
|
|
33
|
+
console.error("[controllerUtiles][pushNotification]",$ctrl$$.name,"compileHtml email_content",$e$$,$obj$$)}$notificationData$$.title||$notificationData$$.body||($exclude_token$$=null);let $users_will_receive$$=$p$$.users;$users_will_receive$$||=[];$options$$.users_will_receive&&$options$$.users_will_receive.length>0&&($users_will_receive$$=$users_will_receive$$.concat($options$$.users_will_receive));$p$$.send_phu_trach&&$obj$$.phu_trach&&($users_will_receive$$=$users_will_receive$$.concat([$obj$$.phu_trach]));
|
|
34
|
+
if($p$$.send_admin){var $admins_of_user_sandbox_us$$=(await Participant.find({id_app:$obj$$.id_app,admin:!0}).lean()).map($p$$=>$p$$.email);$users_will_receive$$=$users_will_receive$$.concat($admins_of_user_sandbox_us$$)}$p$$.send_attends&&$obj$$.attends&&($users_will_receive$$=$users_will_receive$$.concat($obj$$.attends));$p$$.group_id&&($admins_of_user_sandbox_us$$=(await Participant.find({id_app:$obj$$.id_app,group_id:$p$$.group_id}).lean()).map($u$$=>$u$$.email),$users_will_receive$$=$users_will_receive$$.concat($admins_of_user_sandbox_us$$));
|
|
35
|
+
$p$$.send_to_customer&&($obj$$.ma_kh||$obj$$.id_kh)&&($admins_of_user_sandbox_us$$=await mongoose.models.customer.getInfo($obj$$.id_app,{$or:[{ma_kh:$obj$$.ma_kh},{_id:$obj$$.id_kh}]},"of_user"))&&($users_will_receive$$=$users_will_receive$$.concat([$admins_of_user_sandbox_us$$]));$p$$.send_to_user_created?$users_will_receive$$=$users_will_receive$$.concat([$obj$$.user_created]):$p$$.send_to_user_created===!1&&($users_will_receive$$=$users_will_receive$$.filter($u$$=>$u$$!==$obj$$.user_created));
|
|
36
|
+
if($p$$.handle_receivers){$admins_of_user_sandbox_us$$={obj:$obj$$,users_will_receive:$users_will_receive$$,utils};try{let $str_func$$=$p$$.handle_receivers;if($str_func$$.indexOf("async ")<0){$str_func$$.indexOf("return ")<0&&($str_func$$=`return ${$str_func$$}`);let $cust_users_will_receive$$=await evalute(`return (async ()=>{
|
|
37
37
|
try{
|
|
38
38
|
${$str_func$$}
|
|
39
39
|
}catch(e){
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
const {onAfterCommit}=require("./sessionContext"),_=require("lodash"),moment=require("moment"),processingQueue=new Map,debouncedProcessors=new Map,DEBOUNCE_WAIT_MS=1E4,_runActualCalculation=async $id_app$$=>{const $sokho$$=global.getModel("sokho"),$sokhotc$$=global.getModel("sokhotc");var $dateSet_minDate_query$$=processingQueue.get($id_app$$);if($dateSet_minDate_query$$&&$dateSet_minDate_query$$.size!==0){processingQueue.delete($id_app$$);debouncedProcessors.delete($id_app$$);var $allDates_maxDate$$=
|
|
2
2
|
Array.from($dateSet_minDate_query$$).map($d$$=>moment($d$$,"YYYY-MM-DD"));$dateSet_minDate_query$$=moment.min($allDates_maxDate$$).startOf("day").toDate();$allDates_maxDate$$=moment.max($allDates_maxDate$$).endOf("day").toDate();console.log(`[sokhotc] \ud83d\udd25 B\u1eaft \u0111\u1ea7u ch\u1ea1y debounced cho ${$id_app$$}. D\u1ea3i ng\u00e0y: ${moment($dateSet_minDate_query$$).format("YYYY-MM-DD")} T\u1edaI ${moment($allDates_maxDate$$).format("YYYY-MM-DD")}`);try{$dateSet_minDate_query$$={id_app:$id_app$$,
|
|
3
|
-
ngay_ct:{$gte:$dateSet_minDate_query$$,$lte:$allDates_maxDate$$}};await $sokhotc$$.deleteMany($dateSet_minDate_query$$);let $data_sokho$$=await $sokho$$.find($dateSet_minDate_query$$).lean();if($data_sokho$$&&$data_sokho$$.length!==0){$
|
|
4
|
-
|
|
3
|
+
ngay_ct:{$gte:$dateSet_minDate_query$$,$lte:$allDates_maxDate$$}};await $sokhotc$$.deleteMany($dateSet_minDate_query$$);let $data_sokho$$=await $sokho$$.find($dateSet_minDate_query$$).lean();if($data_sokho$$&&$data_sokho$$.length!==0){var $data_sokho_tc$$=await $data_sokho$$.asyncGroupBy("id_app ma_dvcs ma_ct ngay_ct ma_vt ma_dvt ma_lo han_sd ma_tt1 ma_tt2 ma_tt3 ma_kho ma_nt ty_gia ma_sp ma_cp ma_cong_doan".split(" "),"sl_nhap sl_xuat sl_nhap_qd sl_xuat_qd tien_hang_nt tien_hang tien_ck_nt tien_ck tien_phi_nt tien_phi tien_cp_nt tien_cp tien_hang_nk tien_hang_nk_nt tien_thue_nk tien_thue_nk_nt tien_nhap_nt tien_nhap tien_xuat tien_xuat_nt tien tien_nt tien_hang_ct_nt tien_hang_ct tien_ct tien_ct_nt tien_thue_nt tien_thue sl_sp".split(" ")),
|
|
4
|
+
$rs$$=await $sokhotc$$.insertMany($data_sokho_tc$$);console.log(`[sokhotc] \u2705 Ho\u00e0n th\u00e0nh. S\u1ed1 b\u1ea3n ghi sokho \u0111\u00e3 x\u1eed l\u00fd ${$data_sokho$$.length}, \u0111\u00e3 t\u1ea1o ${$rs$$.length} b\u1ea3n ghi sokhotc cho ${$id_app$$}.`)}else console.log(`[sokhotc] Kh\u00f4ng t\u00ecm th\u1ea5y d\u1eef li\u1ec7u sokho cho ${$id_app$$} trong d\u1ea3i ng\u00e0y.`)}catch($e$$){console.error(`[sokhotc] \u274c L\u1ed7i khi ch\u1ea1y debounced cho ${$id_app$$}:`,$e$$)}}else console.log(`[sokhotc] Debounced run cho ${$id_app$$} kh\u00f4ng c\u00f3 ng\u00e0y n\u00e0o trong h\u00e0ng \u0111\u1ee3i.`)},
|
|
5
5
|
createSokhoTC=async function($id_app$$,$tu_ngay$$,$den_ngay$$){onAfterCommit(()=>{try{processingQueue.has($id_app$$)||processingQueue.set($id_app$$,new Set);const $dateSet$$=processingQueue.get($id_app$$),$m_tu_ngay$$=moment($tu_ngay$$),$m_den_ngay$$=moment($den_ngay$$||$tu_ngay$$);let $currentDate$$=$m_tu_ngay$$.clone().startOf("day");for(;$currentDate$$.isSameOrBefore($m_den_ngay$$,"day");)$dateSet$$.add($currentDate$$.format("YYYY-MM-DD")),$currentDate$$.add(1,"day");debouncedProcessors.has($id_app$$)||
|
|
6
6
|
(console.log(`[sokhotc] \u23f3 T\u1ea1o debouncer m\u1edbi cho ${$id_app$$} (delay ${DEBOUNCE_WAIT_MS}ms)`),debouncedProcessors.set($id_app$$,_.debounce(()=>{_runActualCalculation($id_app$$)},DEBOUNCE_WAIT_MS,{leading:!1,trailing:!0})));debouncedProcessors.get($id_app$$)()}catch($e$$){console.error(`[sokhotc] L\u1ed7i khi l\u00ean l\u1ecbch (scheduling) cho ${$id_app$$}:`,$e$$)}},"createSokhoTC");return Promise.resolve()};exports.createSokhoTC=createSokhoTC;
|
|
7
7
|
const processingQueueSocai=new Map,debouncedProcessorsSocai=new Map,DEBOUNCE_WAIT_MS_SOCAI=1E4,_runActualCalculationSocai=async $id_app$$=>{const $socai$$=global.getModel("socai"),$socaitc$$=global.getModel("socaitc"),$socaitt$$=global.getModel("socaitt");var $dateSet$jscomp$2_minDate$jscomp$1_query$$=processingQueueSocai.get($id_app$$);if($dateSet$jscomp$2_minDate$jscomp$1_query$$&&$dateSet$jscomp$2_minDate$jscomp$1_query$$.size!==0){processingQueueSocai.delete($id_app$$);debouncedProcessorsSocai.delete($id_app$$);
|
|
8
8
|
var $allDates$$=Array.from($dateSet$jscomp$2_minDate$jscomp$1_query$$).map($d$$=>moment($d$$,"YYYY-MM-DD"));$dateSet$jscomp$2_minDate$jscomp$1_query$$=moment.min($allDates$$).startOf("day").toDate();var $maxDate$$=moment.max($allDates$$).endOf("day").toDate();$allDates$$=`${moment($dateSet$jscomp$2_minDate$jscomp$1_query$$).format("YYYY-MM-DD")} T\u1edaI ${moment($maxDate$$).format("YYYY-MM-DD")}`;console.log(`[socai] \ud83d\udd25 B\u1eaft \u0111\u1ea7u ch\u1ea1y debounced cho ${$id_app$$}. D\u1ea3i ng\u00e0y: ${$allDates$$}`);
|
|
9
9
|
try{$dateSet$jscomp$2_minDate$jscomp$1_query$$={id_app:$id_app$$,ngay_ct:{$gte:$dateSet$jscomp$2_minDate$jscomp$1_query$$,$lte:$maxDate$$}};await Promise.all([$socaitc$$.deleteMany($dateSet$jscomp$2_minDate$jscomp$1_query$$),$socaitt$$.deleteMany($dateSet$jscomp$2_minDate$jscomp$1_query$$)]);let $raw_data_socai$$=await $socai$$.find($dateSet$jscomp$2_minDate$jscomp$1_query$$).lean();if($raw_data_socai$$&&$raw_data_socai$$.length!==0){await $raw_data_socai$$.asyncJoinModel2($id_app$$,"account",{where:{tk_no:"tk"},
|
|
10
10
|
fields:{tk_cn_no:"tk_cn"}});await $raw_data_socai$$.asyncJoinModel2($id_app$$,"account",{where:{tk_co:"tk"},fields:{tk_cn_co:"tk_cn"}});for(let $item$$ of $raw_data_socai$$)$item$$.tk_cn_no||($item$$.ma_kh_no=""),$item$$.tk_cn_co||($item$$.ma_kh_co="");var $data_socaitc$$=await $raw_data_socai$$.asyncGroupBy("id_app ma_dvcs ma_ct ma_gd ngay_ct tk_no tk_co ma_kh_no ma_kh_co ma_kho ma_nt ty_gia ma_bp id_lenhsx ma_phi ma_hd ma_dt ma_nv ma_sp ma_cp ma_kenh ma_cong_doan".split(" "),["tien","tien_nt"]),
|
|
11
|
-
$data_socaitt$$=await $raw_data_socai$$.asyncGroupBy("id_app ma_dvcs ma_ct ma_gd ngay_ct tk_no tk_co ma_nt ty_gia ma_bp ma_phi ma_hd ma_dt ma_nv".split(" "),["tien","tien_nt"]),[$rs_tc$$,$rs_tt$$]=await Promise.all([$data_socaitc$$.length>0?$socaitc$$.insertMany($data_socaitc$$):[],$data_socaitt$$.length>0?$socaitt$$.insertMany($data_socaitt$$):[]]);console.log(`[socai] \u2705 Ho\u00e0n th\u00e0nh ${$id_app$$}.
|
|
11
|
+
$data_socaitt$$=await $raw_data_socai$$.asyncGroupBy("id_app ma_dvcs ma_ct ma_gd ngay_ct tk_no tk_co ma_nt ty_gia ma_bp ma_phi ma_hd ma_dt ma_nv".split(" "),["tien","tien_nt"]),[$rs_tc$$,$rs_tt$$]=await Promise.all([$data_socaitc$$.length>0?$socaitc$$.insertMany($data_socaitc$$):[],$data_socaitt$$.length>0?$socaitt$$.insertMany($data_socaitt$$):[]]);console.log(`[socai] \u2705 Ho\u00e0n th\u00e0nh ${$id_app$$}. \u0110\u00e3 x\u1eed l\u00fd ${$raw_data_socai$$.length} b\u1ea3n ghi socai, \u0110\u00e3 t\u1ea1o: ${$rs_tc$$.length} (socaitc), ${$rs_tt$$.length} (socaitt).`)}else console.log(`[socai] Kh\u00f4ng t\u00ecm th\u1ea5y d\u1eef li\u1ec7u socai cho ${$id_app$$} trong d\u1ea3i ng\u00e0y.`)}catch($e$$){console.error(`[socai] \u274c L\u1ed7i khi ch\u1ea1y debounced cho ${$id_app$$} (${$allDates$$}):`,
|
|
12
12
|
$e$$)}}else console.log(`[socai] Debounced run cho ${$id_app$$} kh\u00f4ng c\u00f3 ng\u00e0y n\u00e0o.`)},createSocaiTC=async function($id_app$$,$tu_ngay$$,$den_ngay$$){onAfterCommit(()=>{try{processingQueueSocai.has($id_app$$)||processingQueueSocai.set($id_app$$,new Set);const $dateSet$$=processingQueueSocai.get($id_app$$),$m_tu_ngay$$=moment($tu_ngay$$),$m_den_ngay$$=moment($den_ngay$$||$tu_ngay$$);let $currentDate$$=$m_tu_ngay$$.clone().startOf("day");for(;$currentDate$$.isSameOrBefore($m_den_ngay$$,
|
|
13
13
|
"day");)$dateSet$$.add($currentDate$$.format("YYYY-MM-DD")),$currentDate$$.add(1,"day");debouncedProcessorsSocai.has($id_app$$)||(console.log(`[socai] \u23f3 T\u1ea1o debouncer m\u1edbi cho ${$id_app$$} (delay ${DEBOUNCE_WAIT_MS_SOCAI}ms)`),debouncedProcessorsSocai.set($id_app$$,_.debounce(()=>{_runActualCalculationSocai($id_app$$)},DEBOUNCE_WAIT_MS_SOCAI,{leading:!1,trailing:!0})));debouncedProcessorsSocai.get($id_app$$)()}catch($e$$){console.error(`[socai] L\u1ed7i khi l\u00ean l\u1ecbch (scheduling) cho ${$id_app$$}:`,
|
|
14
14
|
$e$$)}},"createSokhoTC");return Promise.resolve()};exports.createSocaiTC=createSocaiTC;
|
package/server/libs/tinhgiatb.js
CHANGED
|
@@ -3,12 +3,12 @@ module.exports=async function($condition$$,$fn$$){if($condition$$&&$condition$$.
|
|
|
3
3
|
$condition$$.id_app,$app_query_dmvt$$=await global.getModel("app").findOne({_id:$id_app$$},{options:1}).lean();if(!$app_query_dmvt$$)return $fn$$("C\u00f4ng ty n\u00e0y kh\u00f4ng t\u1ed3n t\u1ea1i");var $f_tien$$=($app_query_dmvt$$.options||{}).f_tien||0;$app_query_dmvt$$={id_app:$id_app$$,gia_xuat:"1"};$condition$$.ma_nvt&&($app_query_dmvt$$.ma_nvt=$condition$$.ma_nvt);$condition$$.ma_ncc&&($app_query_dmvt$$.ma_ncc=$condition$$.ma_ncc);$condition$$.ma_vt&&($app_query_dmvt$$.ma_vt=$condition$$.ma_vt);
|
|
4
4
|
console.log(`\ud83d\udd25 [tinhgiatb] t\u00ednh gi\u00e1 trung b\u00ecnh, kho:${$condition$$.ma_kho}, sessionID=${getCurrentSession()?._debugId}`);dmvt.find($app_query_dmvt$$).lean().then(function($dmvts$$){async.mapLimit($dmvts$$,100,function($vt$$,$callback$$){setImmediate(()=>{let $query$$={id_app:$id_app$$,tu_ngay:$tu_ngay$$,den_ngay:$den_ngay$$,ma_vt:$vt$$.ma_vt,ma_kho:$ma_kho$$};tinhgiatb1vt($query$$,function($error$$,$gia$$){if($error$$)return console.error("[tinhgiatb] Kh\u00f4ng th\u1ec3 t\u00ednh gi\u00e1 trung b\u00ecnh cho v\u1eadt t\u01b0",
|
|
5
5
|
$query$$.ma_vt,$error$$),$callback$$($error$$);$gia$$.id_app=$id_app$$;$gia$$.ma_kho=$ma_kho$$;$gia$$.status=!0;$callback$$(null,$gia$$)})})},function($error$$,$bang_gia$$){if($error$$)return $fn$$($error$$);$error$$=[];for(let $t$$=$condition$$.tu_thang;$t$$<=$condition$$.den_thang;$t$$++)$error$$.push($t$$);async.map($error$$,async function($t$$){var $gias_query_delete$$={id_app:$id_app$$,ma_vt:{$in:$bang_gia$$.map($g$$=>$g$$.ma_vt)},nam:$condition$$.nam,thang:$t$$};$ma_kho$$&&($gias_query_delete$$.ma_kho=
|
|
6
|
-
$ma_kho$$);await giatb.deleteMany($gias_query_delete$$);$gias_query_delete$$=$bang_gia$$.map($gia$$=>({...$gia$$,thang:$t$$,nam:$condition$$.nam}));return await giatb.create($gias_query_delete$$)},function($error$$){if($error$$)return $fn$$($error$$);console.log(
|
|
6
|
+
$ma_kho$$);await giatb.deleteMany($gias_query_delete$$);$gias_query_delete$$=$bang_gia$$.map($gia$$=>({...$gia$$,thang:$t$$,nam:$condition$$.nam}));return await giatb.create($gias_query_delete$$)},function($error$$){if($error$$)return $fn$$($error$$);console.log(`[tinhgiatb] L\u1ea5y c\u00e1c ch\u1ee9ng t\u1eeb c\u1ea7n c\u1eadp nh\u1eadt s\u1ed5 s\u00e1ch ma_kho=${$condition$$.ma_kho}`);let $vouchers_x$$={},$vouchers_n$$={},$ma_vts$$=$bang_gia$$.map($v$$=>$v$$.ma_vt);async.parallel({xuat:function($callback$$){setImmediate(async()=>
|
|
7
7
|
{var $cac_ma_ct_query_sokho_x$$={id_app:$id_app$$,ngay_ct:{$gte:$tu_ngay$$,$lte:$den_ngay$$},nxt:2,ma_vt:{$in:$ma_vts$$},px_gia_dd:!1};$ma_kho$$&&($cac_ma_ct_query_sokho_x$$.ma_kho=$ma_kho$$);const $sks$$=await sokho.find($cac_ma_ct_query_sokho_x$$).lean();if($sks$$.length===0)return $callback$$(null,$vouchers_x$$);$cac_ma_ct_query_sokho_x$$=[...(new Set($sks$$.map($sk$$=>$sk$$.ma_ct)))];async.map($cac_ma_ct_query_sokho_x$$,async function($ma_ct$$){var $ctrl_ids$$=global.controllers[$ma_ct$$.toUpperCase()],
|
|
8
|
-
$ct_vouchers$$=$ctrl_ids$$?$ctrl_ids$$.getProperty("model"):mongoose.models[$ma_ct$$.toLowerCase()];if($ct_vouchers$$){$ctrl_ids$$=[...(new Set($sks$$.filter($sk$$=>$sk$$.ma_ct===$ma_ct$$)))];$ct_vouchers$$=await $ct_vouchers$$.find({_id:{$in:$ctrl_ids$$}});for(let $v$$ of $ct_vouchers$$)$vouchers_x$$[$v$$._id.toString()]=$v$$;for(let $id_ct$$ of $ctrl_ids$$.filter($id$$=>!$vouchers_x$$[$id$$]))console.log("\u26a0\ufe0f [tinhgiatb] Khong tim thay phieu xuat. chuong trinh da xoa phieu nay khoi s\u1ed5 s\u00e1ch, id_ct=",
|
|
8
|
+
$ct_vouchers$$=$ctrl_ids$$?$ctrl_ids$$.getProperty("model"):mongoose.models[$ma_ct$$.toLowerCase()];if($ct_vouchers$$){$ctrl_ids$$=[...(new Set($sks$$.filter($sk$$=>$sk$$.ma_ct===$ma_ct$$).map($sk$$=>$sk$$.id_ct)))];$ct_vouchers$$=await $ct_vouchers$$.find({_id:{$in:$ctrl_ids$$}});for(let $v$$ of $ct_vouchers$$)$vouchers_x$$[$v$$._id.toString()]=$v$$;for(let $id_ct$$ of $ctrl_ids$$.filter($id$$=>!$vouchers_x$$[$id$$]))console.log("\u26a0\ufe0f [tinhgiatb] Khong tim thay phieu xuat. chuong trinh da xoa phieu nay khoi s\u1ed5 s\u00e1ch, id_ct=",
|
|
9
9
|
$id_ct$$,"ma_ct=",$ma_ct$$),await sokho.deleteMany({id_ct:$id_ct$$}),await socai.deleteMany({id_ct:$id_ct$$})}else console.log("\u26a0\ufe0f [tinhgiatb] Khong tim model cho ma_ct=",$ma_ct$$);return null},function($error$$){if($error$$)return console.error("\u274c [tinhgiatb] co loi lay phieu xuat",$error$$),$callback$$($error$$);$callback$$(null,$vouchers_x$$)})})},nhap:function($callback$$){setImmediate(async()=>{var $ds_ma_ct_query_sokho_n$$={id_app:$id_app$$,ngay_ct:{$gte:$tu_ngay$$,$lte:$den_ngay$$},
|
|
10
10
|
nxt:1,ma_vt:{$in:$ma_vts$$},pn_gia_tb:!0,ma_ct:{$ne:"PXC"}};$ma_kho$$&&($ds_ma_ct_query_sokho_n$$.ma_kho=$ma_kho$$);const $sks$$=await sokho.find($ds_ma_ct_query_sokho_n$$).lean();if($sks$$.length===0)return $callback$$(null,$vouchers_n$$);$ds_ma_ct_query_sokho_n$$=[...(new Set($sks$$.map($sk$$=>$sk$$.ma_ct)))];async.map($ds_ma_ct_query_sokho_n$$,async function($ma_ct$$){var $ctrl$jscomp$1_ids$$=global.controllers[$ma_ct$$.toUpperCase()],$ct$jscomp$1_vouchers$$=$ctrl$jscomp$1_ids$$?$ctrl$jscomp$1_ids$$.getProperty("model"):
|
|
11
|
-
mongoose.models[$ma_ct$$.toLowerCase()];if($ct$jscomp$1_vouchers$$){$ctrl$jscomp$1_ids$$=[...(new Set($sks$$.map($sk$$=>$sk$$.id_ct)))];$ct$jscomp$1_vouchers$$=await $ct$jscomp$1_vouchers$$.find({_id:{$in:$ctrl$jscomp$1_ids$$}});for(let $v$$ of $ct$jscomp$1_vouchers$$)$vouchers_n$$[$v$$._id.toString()]=$v$$;for(let $id_ct$$ of $ctrl$jscomp$1_ids$$.filter($id$$=>!$vouchers_n$$[$id$$]))console.warn("\u26a0\ufe0f [tinhgiatb] Khong tim thay phieu nh\u1eadp. chuong trinh da xoa phieu nay khoi s\u1ed5 s\u00e1ch, id_ct=",
|
|
11
|
+
mongoose.models[$ma_ct$$.toLowerCase()];if($ct$jscomp$1_vouchers$$){$ctrl$jscomp$1_ids$$=[...(new Set($sks$$.filter($sk$$=>$sk$$.ma_ct===$ma_ct$$).map($sk$$=>$sk$$.id_ct)))];$ct$jscomp$1_vouchers$$=await $ct$jscomp$1_vouchers$$.find({_id:{$in:$ctrl$jscomp$1_ids$$}});for(let $v$$ of $ct$jscomp$1_vouchers$$)$vouchers_n$$[$v$$._id.toString()]=$v$$;for(let $id_ct$$ of $ctrl$jscomp$1_ids$$.filter($id$$=>!$vouchers_n$$[$id$$]))console.warn("\u26a0\ufe0f [tinhgiatb] Khong tim thay phieu nh\u1eadp. chuong trinh da xoa phieu nay khoi s\u1ed5 s\u00e1ch, id_ct=",
|
|
12
12
|
$id_ct$$,"ma_ct=",$ma_ct$$),await sokho.deleteMany({id_ct:$id_ct$$}),await socai.deleteMany({id_ct:$id_ct$$})}else console.log("\u26a0\ufe0f [tinhgiatb] Khong tim model cho ma_ct=",$ma_ct$$);return null},$error$$=>{if($error$$)return console.error("\u274c [tinhgiatb] co loi lay phieu nhap",$error$$),$callback$$($error$$);$callback$$(null,$vouchers_n$$)})})},nhap_dc:$callback$$=>{if($ma_kho$$){let $query_sokho_pnc$$={id_app:$id_app$$,ma_ct:"PNC",ngay_ct:{$gte:$tu_ngay$$,$lte:$den_ngay$$},$or:[{ma_kho_x:$ma_kho$$},
|
|
13
13
|
{"details.ma_kho_x":$ma_kho$$}],"details.px_gia_dd":!1};global.getModel("pnc").find($query_sokho_pnc$$).then($pnc$$=>{$pnc$$.forEach($p$$=>{$vouchers_n$$[$p$$._id.toString()]=$p$$});$callback$$()}).catch($e$$=>{console.error("[tinhgiatb] co loi lay phieu nhap dieu chuyen",$e$$);return $callback$$($e$$)})}else $callback$$()}},function($error$$){if($error$$)return $fn$$($error$$);async.waterfall([function($callback$$){console.log("[tinhgiatb] C\u1eadp nh\u1eadt gi\u00e1 xu\u1ea5t kho...");async.mapLimit(_.values($vouchers_x$$),
|
|
14
14
|
100,($voucher_x$$,$callback$$)=>{setImmediate(async()=>{try{const $details$$=$voucher_x$$.details.filter($d$$=>(!$condition$$.ma_vt||$condition$$.ma_vt===$d$$.ma_vt)&&!$d$$.px_gia_dd&&(!$ma_kho$$||$ma_kho$$===($d$$.ma_kho||$d$$.ma_kho_x||$voucher_x$$.ma_kho||$voucher_x$$.ma_kho_x)));await Promise.all($details$$.map($d$$=>(async()=>{var $gia$$=_.find($bang_gia$$,$vt$$=>$d$$.ma_vt===$vt$$.ma_vt);$gia$$=$gia$$?$gia$$.gia:0;let $he_so_qd$$=1,$qd$$=await dmqddvt.findOne({id_app:$voucher_x$$.id_app,ma_vt:$d$$.ma_vt,
|