flexbiz-server 12.6.13 → 12.6.16

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.
Files changed (129) hide show
  1. package/README.md +439 -4
  2. package/package.json +4 -3
  3. package/server/agents/agents.js +1 -0
  4. package/server/agents/ai_memory_manager.js +6 -0
  5. package/server/agents/ceoAgent.js +43 -0
  6. package/server/agents/chatController.js +6 -0
  7. package/server/agents/cskhAgent.js +43 -0
  8. package/server/agents/flowAgent.js +43 -0
  9. package/server/agents/prompts/ceo.js +2 -0
  10. package/server/agents/prompts/flow.js +2 -0
  11. package/server/agents/staffAgent.js +44 -0
  12. package/server/agents/tools/financeTools.js +57 -0
  13. package/server/agents/tools/flowTools.js +36 -0
  14. package/server/agents/tools/kpiTools.js +104 -0
  15. package/server/agents/tools/masterDataTools.js +74 -0
  16. package/server/agents/tools/okrTools.js +21 -0
  17. package/server/agents/tools/ragTools.js +21 -0
  18. package/server/agents/tools/reportTools.js +43 -0
  19. package/server/agents/tools/staffTools.js +13 -0
  20. package/server/agents/tools/stockTools.js +41 -0
  21. package/server/agents/tools/systemTools.js +52 -0
  22. package/server/agents/tools/taskTools.js +23 -0
  23. package/server/agents/tools/utils.js +57 -0
  24. package/server/app.js +7 -7
  25. package/server/controllers/controller.js +31 -29
  26. package/server/controllers/controllerRPT.js +31 -29
  27. package/server/controllers/findHandler.js +22 -21
  28. package/server/controllers/importFromExcelHandler.js +17 -14
  29. package/server/controllers/importHandler.js +26 -41
  30. package/server/controllers/importHandler_old.js +41 -0
  31. package/server/controllers/rptHandler.js +14 -11
  32. package/server/controllers/viewHandler.js +7 -7
  33. package/server/flow/FlowEngine.js +49 -45
  34. package/server/flow/WebhookManager.js +1 -1
  35. package/server/flow/handlers.js +2 -2
  36. package/server/flow/nodes/userform.js +28 -0
  37. package/server/flow/utils.js +12 -4
  38. package/server/global.js +4 -3
  39. package/server/libs/cdpstk.js +1 -1
  40. package/server/libs/ckvt.js +12 -11
  41. package/server/libs/ckvtcapphat.js +5 -5
  42. package/server/libs/ckvtnpp.js +8 -8
  43. package/server/libs/ckvttt.js +6 -5
  44. package/server/libs/ctvt.js +7 -6
  45. package/server/libs/ctvtnpp.js +7 -6
  46. package/server/libs/ctvttt.js +6 -6
  47. package/server/libs/databanle.js +17 -18
  48. package/server/libs/databanle_old.js +18 -0
  49. package/server/libs/databanletheongay.js +25 -29
  50. package/server/libs/dktk.js +3 -3
  51. package/server/libs/dkvt.js +11 -11
  52. package/server/libs/dkvtnpp.js +7 -6
  53. package/server/libs/dkvttt.js +7 -6
  54. package/server/libs/docs.js +72 -36
  55. package/server/libs/dtbanletheosp.js +16 -19
  56. package/server/libs/fifo_dk.js +8 -0
  57. package/server/libs/fifo_group.js +4 -0
  58. package/server/libs/fifo_trace_query.js +12 -0
  59. package/server/libs/getGiaban.js +59 -70
  60. package/server/libs/getGiaban_old.js +70 -0
  61. package/server/libs/joinData.js +3 -3
  62. package/server/libs/joinData_bk.js +44 -0
  63. package/server/libs/kpistatus.js +24 -17
  64. package/server/libs/permission.js +54 -56
  65. package/server/libs/permission_old.js +58 -0
  66. package/server/libs/post-book.js +1 -1
  67. package/server/libs/post-socai.js +2 -1
  68. package/server/libs/post-sokho.js +11 -11
  69. package/server/libs/prototypes.js +13 -12
  70. package/server/libs/queryBuilding.js +11 -10
  71. package/server/libs/redis-cache.js +22 -10
  72. package/server/libs/sctcnkh.js +19 -17
  73. package/server/libs/socaitk.js +1 -1
  74. package/server/libs/thnxt.js +17 -14
  75. package/server/libs/thnxtnpp.js +8 -8
  76. package/server/libs/thnxttt.js +8 -7
  77. package/server/libs/tinhgia1vt_fifo.js +22 -0
  78. package/server/libs/tinhgiafifo.js +28 -0
  79. package/server/libs/utils.js +37 -19
  80. package/server/models/app.js +1 -1
  81. package/server/models/approve.js +8 -8
  82. package/server/models/cacheDataReport.js +2 -2
  83. package/server/models/cacheSessionReport.js +5 -1
  84. package/server/models/cdvt.js +2 -2
  85. package/server/models/checkin.js +2 -2
  86. package/server/models/chitietluongds.js +1 -1
  87. package/server/models/contract.js +1 -1
  88. package/server/models/csluongtheods.js +2 -2
  89. package/server/models/ctds2.js +7 -6
  90. package/server/models/custom_report.js +1 -0
  91. package/server/models/dmdt.js +2 -2
  92. package/server/models/dmkho.js +5 -4
  93. package/server/models/dmnvt.js +5 -4
  94. package/server/models/fifo_dk.js +2 -0
  95. package/server/models/fifo_lot.js +3 -0
  96. package/server/models/fifo_trace.js +4 -0
  97. package/server/models/flowpending.js +2 -0
  98. package/server/models/group.js +3 -5
  99. package/server/models/listinfo.js +61 -33
  100. package/server/models/schedule.js +4 -3
  101. package/server/models/task.js +1 -1
  102. package/server/models/trangthai.js +2 -2
  103. package/server/modules/lists/bg-fifo_dk.js +8 -0
  104. package/server/modules/lists/ls-checkin.js +1 -1
  105. package/server/modules/lists/ls-customer.js +23 -26
  106. package/server/modules/lists/ls-dmbp.js +5 -5
  107. package/server/modules/lists/ls-dmkho.js +7 -1
  108. package/server/modules/lists/ls-dmnhtask.js +1 -1
  109. package/server/modules/lists/ls-dmvt.js +52 -70
  110. package/server/modules/lists/ls-flowpending.js +3 -0
  111. package/server/modules/lists/ls-participant.js +6 -4
  112. package/server/modules/lists/ls-task.js +6 -6
  113. package/server/modules/reports/calc-tinhgiafifo.js +8 -0
  114. package/server/modules/reports/calc-tinhluong.js +17 -16
  115. package/server/modules/reports/rp-baoCaoFifoKy.js +1 -0
  116. package/server/modules/reports/rp-congviectheobp.js +4 -4
  117. package/server/modules/reports/rp-congviectheodt.js +1 -1
  118. package/server/modules/reports/rp-congviectheokh.js +1 -1
  119. package/server/modules/reports/rp-congviectheonv.js +1 -1
  120. package/server/modules/reports/rp-dtbanletheoct.js +22 -23
  121. package/server/modules/reports/rp-dtbanletheokh.js +17 -20
  122. package/server/modules/reports/rp-dtbanletheokho.js +15 -18
  123. package/server/modules/reports/rp-hoahong.js +1 -1
  124. package/server/modules/reports/rp-sctvt.js +1 -1
  125. package/server/modules/reports/rp-sosanhkhvatt2.js +1 -1
  126. package/server/modules/reports/rp-thnxt.js +8 -8
  127. package/server/modules/reports/rp-tongquannvbh.js +1 -1
  128. package/server/modules/systems/sys-app.js +5 -5
  129. package/server/modules/vouchers/vo-so1.js +14 -14
@@ -1,24 +1,30 @@
1
1
  const numeral=require("numeral"),moment=require("moment"),utils=require("./utils"),getApiContext=$id_app$$=>({apis:{asyncGetData:async($token$$,$model$$,$condition$$={})=>{$condition$$.id_app=$id_app$$;return global.getModel($model$$).findOne($condition$$).lean()}},userInfo:{email:"admin",token:""}}),types=[{code:"String",ten:"String"},{code:"Number",ten:"Number"},{code:"Boolean",ten:"Boolean"},{code:"Date",ten:"Date"},{code:"DateTime",ten:"DateTime",type_data:"Date"},{code:"Time",ten:"Time",type_data:"Date"},
2
- {code:"Array",ten:"Array"},{code:"Mixed",ten:"Mixed",type_data:"Json"},{code:"File",ten:"Excel",type_data:"String"},{code:"Pdf",ten:"PDF",type_data:"String"},{code:"Image",ten:"Image",type_data:"String"},{code:"OtherFile",ten:"OtherFile",type_data:"String"}],buildJsonSchema=async function($entries_fields$$,$depth$$=0,$id_app$$,$listInfo$$){if($depth$$>5)return{};const $validFields$$=$entries_fields$$.filter($f$$=>["ten_vt","ten_kh","ten_kho"].indexOf($f$$.name)>=0||$f$$.not_input!="true"&&$f$$.not_input!=
3
- 1&&$f$$.type!="Group"&&$f$$.type!="Button"&&$f$$.type!="Action"&&$f$$.type!="Link"&&$f$$.type!="Files"&&types.find($t$$=>$t$$.code==$f$$.type));await Promise.all(($entries_fields$$||[]).filter($f$$=>$f$$.header.indexOf("return ")>=0).map($field$$=>(async()=>{if($id_app$$&&$listInfo$$){let $body_func$$=$field$$.header;$body_func$$=`return (async ()=>{
2
+ {code:"Array",ten:"Array"},{code:"Mixed",ten:"Mixed",type_data:"Json"},{code:"File",ten:"Excel",type_data:"String"},{code:"Pdf",ten:"PDF",type_data:"String"},{code:"Image",ten:"Image",type_data:"String"},{code:"OtherFile",ten:"OtherFile",type_data:"String"}],buildJsonSchema=async function($entries_fields$$,$depth$$=0,$id_app$$,$listInfo$$){if($depth$$>5)return{};const $validFields$$=$entries_fields$$.filter($f$$=>["ten_vt","ten_kh","ten_kho"].indexOf($f$$.name)>=0||$f$$.type!="Group"&&$f$$.type!=
3
+ "Button"&&$f$$.type!="Action"&&$f$$.type!="Link"&&$f$$.type!="Files"&&types.find($t$$=>$t$$.code==$f$$.type));await Promise.all(($entries_fields$$||[]).filter($f$$=>$f$$.header.indexOf("return ")>=0).map($field$$=>(async()=>{if($id_app$$&&$listInfo$$){let $body_func$$=$field$$.header;$body_func$$=`return (async ()=>{
4
4
  ${$body_func$$}
5
5
  })()`;try{const $func$$=new Function("master","Moment","Numeral","API","utils","context","field","listInfo",$body_func$$),$context$$=getApiContext($id_app$$);let $header$$=await $func$$({},moment,numeral,$context$$.apis,{},$context$$,$field$$,$listInfo$$);$field$$.header=$header$$}catch($e$$){console.error($e$$.message,$field$$.header,$body_func$$),$field$$.header=$field$$.name}}else $field$$.header=$field$$.name})()));$entries_fields$$=await Promise.all($validFields$$.map(async $f$$=>{var $fieldType_typeInfo$$=
6
- types.find($t$$=>$t$$.code==$f$$.type);$fieldType_typeInfo$$=$fieldType_typeInfo$$?$fieldType_typeInfo$$.type_data||$fieldType_typeInfo$$.code:$f$$.type;let $fieldDef$$={type:$fieldType_typeInfo$$,required:$f$$.required==1||$f$$.required=="true"?!0:void 0,maxlength:$f$$.maxlength?Number($f$$.maxlength):void 0,header:$f$$.header||$f$$.name,description:$f$$.api_description||$f$$.header||void 0};if($f$$.ref_model){var $detailCode_ref_model$$=$f$$.ref_model;const $isJson$$=typeof $detailCode_ref_model$$===
7
- "string"&&($detailCode_ref_model$$.trim().startsWith("{")||$detailCode_ref_model$$.trim().startsWith("[")),$hasComma$$=typeof $detailCode_ref_model$$==="string"&&$detailCode_ref_model$$.indexOf(",")>=0;try{$isJson$$?$detailCode_ref_model$$=utils.JSONParser($detailCode_ref_model$$):$hasComma$$&&($detailCode_ref_model$$=$detailCode_ref_model$$.split(",").map($v$$=>({code:$v$$,name:$v$$})))}catch($e$$){Logger.error("[docs] parse ref_model",$detailCode_ref_model$$,$e$$)}Array.isArray($detailCode_ref_model$$)?
8
- $fieldDef$$.options={options:$detailCode_ref_model$$,value_field:$f$$.ref_field||"code",label_field:$f$$.ref_label||$f$$.ref_field||"name"}:$fieldDef$$.ref={ref_model:$detailCode_ref_model$$,value_field:$f$$.ref_field||"code",label_field:$f$$.ref_label||$f$$.ref_field||"name",listinfo_code:$isJson$$||$hasComma$$?void 0:$detailCode_ref_model$$}}Object.keys($fieldDef$$).forEach($key$$=>$fieldDef$$[$key$$]===void 0&&delete $fieldDef$$[$key$$]);if(($detailCode_ref_model$$=$f$$.form||"")&&$detailCode_ref_model$$!==
9
- "0")try{const $subList$$=await global.getModel("listinfo").findOne({code:$detailCode_ref_model$$}).lean();if($subList$$&&$subList$$.fields){let $subSchema$$=await buildJsonSchema($subList$$.fields,$depth$$+1,$id_app$$,$subList$$);$fieldType_typeInfo$$==="Array"||$f$$.type==="Array"?$fieldDef$$.type=[$subSchema$$]:$fieldType_typeInfo$$==="Json"||$fieldType_typeInfo$$==="Mixed"||$f$$.type==="Mixed"?$fieldDef$$.type=$subSchema$$:$fieldDef$$.sub_schema=$subSchema$$}}catch($e$$){console.error($e$$)}return[$f$$.name,
10
- $fieldDef$$]}));return Object.fromEntries($entries_fields$$)},renderSchema=async function($fields$jscomp$1_rows_validFields$$,$id_app$$,$import_yn$$=0,$depth$$=0,$cleanJson_jsonStr$jscomp$1_listInfo$$){let $server_url$$=configs.api_url||configs.domain;if($depth$$>5)return"";$cleanJson_jsonStr$jscomp$1_listInfo$$=await buildJsonSchema($fields$jscomp$1_rows_validFields$$,void 0,$id_app$$,$cleanJson_jsonStr$jscomp$1_listInfo$$);$cleanJson_jsonStr$jscomp$1_listInfo$$=JSON.stringify($cleanJson_jsonStr$jscomp$1_listInfo$$,
11
- null,2).replace(/</g,"\\u003c").replace(/>/g,"\\u003e");const $tableId$$="schema_"+Math.random().toString(36).substr(2,9);$fields$jscomp$1_rows_validFields$$=$fields$jscomp$1_rows_validFields$$.filter($f$$=>["ten_vt","ten_kh","ten_kho"].indexOf($f$$.name)>=0||$f$$.not_input!="true"&&$f$$.not_input!=1&&$f$$.type!="Button"&&$f$$.type!="Link"&&types.find($t$$=>$t$$.code==$f$$.type));$fields$jscomp$1_rows_validFields$$=await Promise.all($fields$jscomp$1_rows_validFields$$.map(async $field$$=>{var $h_type$$=
12
- types.find($t$$=>$t$$.code==$field$$.type),$formattedJson_isJson$$="",$description_detailCode$jscomp$1_ref_model$$=$field$$.ref_model||"";let $ref_field$$=$field$$.ref_field||"code",$ref_label$$=$field$$.ref_label||$field$$.ref_field||"name";if($description_detailCode$jscomp$1_ref_model$$){$formattedJson_isJson$$=typeof $description_detailCode$jscomp$1_ref_model$$==="string"&&($description_detailCode$jscomp$1_ref_model$$.trim().startsWith("{")||$description_detailCode$jscomp$1_ref_model$$.trim().startsWith("["));
13
- const $hasComma$$=typeof $description_detailCode$jscomp$1_ref_model$$==="string"&&$description_detailCode$jscomp$1_ref_model$$.indexOf(",")>=0;if($formattedJson_isJson$$||$hasComma$$){$formattedJson_isJson$$=$description_detailCode$jscomp$1_ref_model$$;try{if($description_detailCode$jscomp$1_ref_model$$.indexOf("{")>=0||$description_detailCode$jscomp$1_ref_model$$.indexOf("[")>=0){var $jsonObj$$=typeof $description_detailCode$jscomp$1_ref_model$$==="string"?utils.JSONParser($description_detailCode$jscomp$1_ref_model$$):
14
- $description_detailCode$jscomp$1_ref_model$$;$formattedJson_isJson$$=JSON.stringify($jsonObj$$,null,2)}else $formattedJson_isJson$$=$description_detailCode$jscomp$1_ref_model$$.split(",").map($v$$=>({code:$v$$,name:$v$$}))}catch($e$$){Logger.error("[docs] parse ref_model",$description_detailCode$jscomp$1_ref_model$$,$e$$)}$formattedJson_isJson$$=`
6
+ types.find($t$$=>$t$$.code==$f$$.type);$fieldType_typeInfo$$=$fieldType_typeInfo$$?$fieldType_typeInfo$$.type_data||$fieldType_typeInfo$$.code:$f$$.type;let $fieldDef$$={type:$fieldType_typeInfo$$,required:$f$$.required==1||$f$$.required=="true"?!0:void 0,maxlength:$f$$.maxlength?Number($f$$.maxlength):void 0,header:$f$$.header||$f$$.name,description:$f$$.api_description||$f$$.header||void 0,hidden:$f$$.not_input===!0||$f$$.not_input==="true"};if($f$$.ref_model){let $ref_model$$=$f$$.ref_model;const $isJson$$=
7
+ typeof $ref_model$$==="string"&&($ref_model$$.trim().startsWith("{")||$ref_model$$.trim().startsWith("[")),$hasComma$$=typeof $ref_model$$==="string"&&$ref_model$$.indexOf(",")>=0;try{$isJson$$?$ref_model$$=utils.JSONParser($ref_model$$):$hasComma$$&&($ref_model$$=$ref_model$$.split(",").map($v$$=>({code:$v$$,name:$v$$})))}catch($e$$){Logger.error("[docs] parse ref_model",$ref_model$$,$e$$)}if(Array.isArray($ref_model$$))$fieldDef$$.options={options:$ref_model$$,value_field:$f$$.ref_field||"code",
8
+ label_field:$f$$.ref_label||$f$$.ref_field||"name"};else{const $listinfo$$=await global.getModel("listinfo").findOne({code:$ref_model$$},{api_code:1,code:1}).lean();if($f$$.ref_condition)try{var $condition$$=utils.JSONParser($f$$.ref_condition)}catch($e$$){}$fieldDef$$.ref={ref_model:$listinfo$$?.model_code||$listinfo$$?.api_code||$ref_model$$,value_field:$f$$.ref_field||"code",label_field:$f$$.ref_label||$f$$.ref_field||"name",condition:$condition$$,listinfo_code:$isJson$$||$hasComma$$?void 0:$ref_model$$}}}Object.keys($fieldDef$$).forEach($key$$=>
9
+ $fieldDef$$[$key$$]===void 0&&delete $fieldDef$$[$key$$]);if(($condition$$=$f$$.form||"")&&$condition$$!=="0")try{const $subList$$=await global.getModel("listinfo").findOne({code:$condition$$}).lean();if($subList$$&&$subList$$.fields){let $subSchema$$=await buildJsonSchema($subList$$.fields,$depth$$+1,$id_app$$,$subList$$);$fieldType_typeInfo$$==="Array"||$f$$.type==="Array"?$fieldDef$$.type=[$subSchema$$]:$fieldType_typeInfo$$==="Json"||$fieldType_typeInfo$$==="Mixed"||$f$$.type==="Mixed"?$fieldDef$$.type=
10
+ $subSchema$$:$fieldDef$$.sub_schema=$subSchema$$}}catch($e$$){console.error($e$$)}return[$f$$.name,$fieldDef$$]}));return Object.fromEntries($entries_fields$$)},renderSchema=async function($fields$jscomp$1_rows_validFields$$,$id_app$$,$import_yn$$=0,$depth$$=0,$cleanJson_jsonStr$jscomp$1_listInfo$$){let $server_url$$=configs.api_url||configs.domain;if($depth$$>5)return"";$cleanJson_jsonStr$jscomp$1_listInfo$$=await buildJsonSchema($fields$jscomp$1_rows_validFields$$,void 0,$id_app$$,$cleanJson_jsonStr$jscomp$1_listInfo$$);
11
+ $cleanJson_jsonStr$jscomp$1_listInfo$$=JSON.stringify($cleanJson_jsonStr$jscomp$1_listInfo$$,null,2).replace(/</g,"\\u003c").replace(/>/g,"\\u003e");const $tableId$$="schema_"+Math.random().toString(36).substr(2,9);$fields$jscomp$1_rows_validFields$$=$fields$jscomp$1_rows_validFields$$.filter($f$$=>["ten_vt","ten_kh","ten_kho"].indexOf($f$$.name)>=0||$f$$.not_input!="true"&&$f$$.not_input!=1&&$f$$.type!="Button"&&$f$$.type!="Link"&&types.find($t$$=>$t$$.code==$f$$.type));$fields$jscomp$1_rows_validFields$$=
12
+ await Promise.all($fields$jscomp$1_rows_validFields$$.map(async $field$$=>{var $h_type$$=types.find($t$$=>$t$$.code==$field$$.type),$isJson$$="",$description_detailCode$jscomp$1_ref_model$$=$field$$.ref_model||"";let $ref_field$$=$field$$.ref_field||"code",$ref_label$$=$field$$.ref_label||$field$$.ref_field||"name";if($field$$.ref_condition)try{var $formattedJson_ref_condition$$=utils.JSONParser($field$$.ref_condition)}catch($e$$){}if($description_detailCode$jscomp$1_ref_model$$){$isJson$$=typeof $description_detailCode$jscomp$1_ref_model$$===
13
+ "string"&&($description_detailCode$jscomp$1_ref_model$$.trim().startsWith("{")||$description_detailCode$jscomp$1_ref_model$$.trim().startsWith("["));const $hasComma$$=typeof $description_detailCode$jscomp$1_ref_model$$==="string"&&$description_detailCode$jscomp$1_ref_model$$.indexOf(",")>=0;if($isJson$$||$hasComma$$){$formattedJson_ref_condition$$=$description_detailCode$jscomp$1_ref_model$$;try{if($description_detailCode$jscomp$1_ref_model$$.indexOf("{")>=0||$description_detailCode$jscomp$1_ref_model$$.indexOf("[")>=
14
+ 0){var $jsonObj$$=typeof $description_detailCode$jscomp$1_ref_model$$==="string"?utils.JSONParser($description_detailCode$jscomp$1_ref_model$$):$description_detailCode$jscomp$1_ref_model$$;$formattedJson_ref_condition$$=JSON.stringify($jsonObj$$,null,2)}else $formattedJson_ref_condition$$=JSON.stringify($description_detailCode$jscomp$1_ref_model$$.split(",").map($v$$=>({code:$v$$,name:$v$$})),null,2)}catch($e$$){Logger.error("[docs] parse ref_model",$description_detailCode$jscomp$1_ref_model$$,$e$$)}$isJson$$=
15
+ `
15
16
  <div style="margin-top:8px">
16
17
  <span style="color:#7f8c8d; font-size:0.85em">Tu\u1ef3 ch\u1ecdn (value field: "${$ref_field$$}", label field: "${$ref_label$$}"):</span>
17
- <pre style="background:#f4f4f4; padding:8px; border-radius:4px; font-size:11px; color:#2c3e50; border:1px solid #ddd; overflow-x:auto; max-width:350px; margin-top:4px;">${$formattedJson_isJson$$}</pre>
18
- </div>`}else $jsonObj$$=`${$server_url$$}/api-docs-input/${$description_detailCode$jscomp$1_ref_model$$}`,$id_app$$&&($jsonObj$$=$jsonObj$$+"?id_app="+$id_app$$),$formattedJson_isJson$$=`
18
+ <pre style="background:#f4f4f4; padding:8px; border-radius:4px; font-size:11px; color:#2c3e50; border:1px solid #ddd; overflow-x:auto; max-width:350px; margin-top:4px;">${$formattedJson_ref_condition$$}</pre>
19
+ </div>`}else $jsonObj$$=`${$server_url$$}/api-docs-input/${$description_detailCode$jscomp$1_ref_model$$}`,$id_app$$&&($jsonObj$$=$jsonObj$$+"?id_app="+$id_app$$),$isJson$$=`
19
20
  <div style="margin-top:8px">
20
21
  <span style="color:#7f8c8d; font-size:0.85em">Tham chi\u1ebfu:</span>
21
- <a href='${$jsonObj$$}' target='_blank' style='color:#3498db; text-decoration:none; font-weight:bold; border-bottom:1px dashed #3498db;'>${$description_detailCode$jscomp$1_ref_model$$} (value field: "${$ref_field$$}", label field: "${$ref_label$$}")</a>
22
+ <a href='${$jsonObj$$}' target='_blank' style='color:#3498db; text-decoration:none; font-weight:bold; border-bottom:1px dashed #3498db;'>
23
+ ${$description_detailCode$jscomp$1_ref_model$$} (value field: "${$ref_field$$}", label field: "${$ref_label$$}")
24
+ </a>
25
+ <div>
26
+ ${$formattedJson_ref_condition$$?'<span style="color:#7f8c8d; font-size:0.85em">\u0110i\u1ec1u ki\u1ec7n:</span><pre style="background:#f4f4f4; padding:8px; border-radius:4px; font-size:11px; color:#2c3e50; border:1px solid #ddd; overflow-x:auto; max-width:350px; margin-top:4px;">'+JSON.stringify($formattedJson_ref_condition$$,null,2)+"</pre>":""}
27
+ </div>
22
28
  </div>`}$description_detailCode$jscomp$1_ref_model$$=$field$$.api_description||$field$$.header;["ten_vt","ten_kh","ten_kho"].indexOf($field$$.name)>=0&&($description_detailCode$jscomp$1_ref_model$$=`${$description_detailCode$jscomp$1_ref_model$$}. N\u1ebfu \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y kh\u00f4ng t\u1ed3n t\u1ea1i trong danh m\u1ee5c th\u00ec h\u1ec7 th\u1ed1ng s\u1ebd s\u1eed d\u1ee5ng th\u00f4ng tin n\u00e0y \u0111\u1ec3 t\u1ea1o`);$h_type$$=`
23
29
  <tr style="background:#fff">
24
30
  <td style="font-weight:bold; color:#e67e22; vertical-align:top; padding:10px;">${$field$$.name}</td>
@@ -28,7 +34,7 @@ $description_detailCode$jscomp$1_ref_model$$;$formattedJson_isJson$$=JSON.string
28
34
  <td style="vertical-align:top; padding:10px;">
29
35
  <div style="font-weight:500">${$description_detailCode$jscomp$1_ref_model$$}</div>
30
36
  ${$field$$.help_text?`<div style='font-size:0.9em;color:#666;font-style:italic;margin-top:4px'>* ${$field$$.help_text}</div>`:""}
31
- ${$formattedJson_isJson$$}
37
+ ${$isJson$$}
32
38
  </td>
33
39
  </tr>`;if(($description_detailCode$jscomp$1_ref_model$$=$field$$.form||"")&&$description_detailCode$jscomp$1_ref_model$$!=="0")try{const $subList$$=await global.getModel("listinfo").findOne({code:$description_detailCode$jscomp$1_ref_model$$}).lean();if($subList$$&&$subList$$.fields){const $subTable$$=await renderSchema($subList$$.fields,$id_app$$,$import_yn$$,$depth$$+1,$subList$$);$h_type$$+=`
34
40
  <tr>
@@ -59,8 +65,8 @@ $description_detailCode$jscomp$1_ref_model$$;$formattedJson_isJson$$=JSON.string
59
65
  </tr>
60
66
  ${$fields$jscomp$1_rows_validFields$$.join("")}
61
67
  </table>
62
- </div>`},apiDocs=async function($code$$,$id_app$$){var $endpointsHtml_server_url$$=configs.api_url||configs.domain;$code$$=await global.getModel("listinfo").findOne({$or:[{code:$code$$},{api_code:$code$$}]}).lean();if(!$code$$)return"Kh\u00f4ng t\u00ecm th\u1ea5y t\u00e0i li\u1ec7u API.";var $authLink_controller$$=global.controllers[($code$$.api_code||$code$$.code).toUpperCase()];if(!$authLink_controller$$)return"API Controller ch\u01b0a s\u1eb5n s\u00e0ng.";let $route_name$$=$authLink_controller$$.route_name.replace(":id_app",
63
- $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/api-docs-auth`;let $idAppNote$$=$route_name$$.indexOf("id_app")>=0?'<li style="color:#8e44ad; margin-top:4px;"><b>id_app:</b> ID c\u1ee7a c\u00f4ng ty/chi nh\u00e1nh \u0111ang thao t\u00e1c (n\u1eb1m trong URL)</li>':"";$endpointsHtml_server_url$$=`
68
+ </div>`},apiDocs=async function($code$jscomp$1_listinfo$$,$id_app$$){var $endpointsHtml_server_url$$=configs.api_url||configs.domain;$code$jscomp$1_listinfo$$=await global.getModel("listinfo").findOne({$or:[{code:$code$jscomp$1_listinfo$$},{api_code:$code$jscomp$1_listinfo$$}]}).lean();if(!$code$jscomp$1_listinfo$$)return"Kh\u00f4ng t\u00ecm th\u1ea5y t\u00e0i li\u1ec7u API.";var $authLink_controller$$=global.controllers[($code$jscomp$1_listinfo$$.api_code||$code$jscomp$1_listinfo$$.code).toUpperCase()];
69
+ if(!$authLink_controller$$)return"API Controller ch\u01b0a s\u1eb5n s\u00e0ng.";let $route_name$$=$authLink_controller$$.route_name.replace(":id_app",$id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/api-docs-auth`;let $idAppNote$$=$route_name$$.indexOf("id_app")>=0?'<li style="color:#8e44ad; margin-top:4px;"><b>id_app:</b> ID c\u1ee7a c\u00f4ng ty/chi nh\u00e1nh \u0111ang thao t\u00e1c (n\u1eb1m trong URL)</li>':"";$endpointsHtml_server_url$$=`
64
70
  <h3>1. Danh s\u00e1ch c\u00e1c Endpoint</h3>
65
71
  <table style="width:100%; border-collapse:collapse; margin-bottom:30px" border="1" bordercolor="#ddd">
66
72
  <tr style="background:#eee">
@@ -80,7 +86,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
80
86
  </ul>
81
87
  <div style="color:#d35400; font-weight:bold; margin-bottom:3px;">Headers ri\u00eang:</div>
82
88
  <ul style="margin:0 0 10px 0; padding-left:20px; background:#fff3e0; padding:5px 20px; border-radius:4px;">
83
- <li><b>listinfo-code:</b> <code>${$code$$.code}</code></li>
89
+ <li><b>listinfo-code:</b> <code>${$code$jscomp$1_listinfo$$.code}</code></li>
84
90
  </ul>
85
91
  <div style="color:#2c3e50; font-weight:bold; margin-bottom:3px;">Body (JSON):</div>
86
92
  <ul style="margin:0; padding-left:20px;">
@@ -114,7 +120,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
114
120
  </td>
115
121
  </tr>
116
122
 
117
- ${$code$$.not_add?"":`
123
+ ${$code$jscomp$1_listinfo$$.not_add?"":`
118
124
  <tr>
119
125
  <td style="padding:10px; font-weight:bold">Th\u00eam m\u1edbi<br/>(Create)</td>
120
126
  <td style="padding:10px; text-align:center"><b>POST</b></td>
@@ -124,7 +130,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
124
130
  <ul style="margin:0 0 10px 0; padding-left:20px;">${$idAppNote$$}</ul>
125
131
  <div style="color:#d35400; font-weight:bold; margin-bottom:3px;">Headers ri\u00eang:</div>
126
132
  <ul style="margin:0 0 10px 0; padding-left:20px; background:#fff3e0; padding:5px 20px; border-radius:4px;">
127
- <li><b>listinfo-code:</b> <code>${$code$$.code}</code></li>
133
+ <li><b>listinfo-code:</b> <code>${$code$jscomp$1_listinfo$$.code}</code></li>
128
134
  </ul>
129
135
  <div style="color:#2c3e50; font-weight:bold; margin-bottom:3px;">Body (JSON):</div>
130
136
  <ul style="margin:0; padding-left:20px;">
@@ -143,7 +149,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
143
149
  <ul style="margin:0 0 10px 0; padding-left:20px;">${$idAppNote$$}</ul>
144
150
  <div style="color:#d35400; font-weight:bold; margin-bottom:3px;">Headers ri\u00eang:</div>
145
151
  <ul style="margin:0 0 10px 0; padding-left:20px; background:#fff3e0; padding:5px 20px; border-radius:4px;">
146
- <li><b>listinfo-code:</b> <code>${$code$$.code}</code></li>
152
+ <li><b>listinfo-code:</b> <code>${$code$jscomp$1_listinfo$$.code}</code></li>
147
153
  </ul>
148
154
  <div style="color:#16a085; font-weight:bold; margin-bottom:3px;">Query Parameters:</div>
149
155
  <ul style="margin:0 0 10px 0; padding-left:20px; background:#e8f8f5; padding:5px 20px; border-radius:4px;">
@@ -157,7 +163,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
157
163
  </td>
158
164
  </tr>`}
159
165
 
160
- ${$code$$.not_update?"":`
166
+ ${$code$jscomp$1_listinfo$$.not_update?"":`
161
167
  <tr>
162
168
  <td style="padding:10px; font-weight:bold">C\u1eadp nh\u1eadt d\u1eef li\u1ec7u<br/>(Update)</td>
163
169
  <td style="padding:10px; text-align:center"><b>PUT</b></td>
@@ -170,7 +176,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
170
176
  </ul>
171
177
  <div style="color:#d35400; font-weight:bold; margin-bottom:3px;">Headers ri\u00eang:</div>
172
178
  <ul style="margin:0 0 10px 0; padding-left:20px; background:#fff3e0; padding:5px 20px; border-radius:4px;">
173
- <li><b>listinfo-code:</b> <code>${$code$$.code}</code></li>
179
+ <li><b>listinfo-code:</b> <code>${$code$jscomp$1_listinfo$$.code}</code></li>
174
180
  </ul>
175
181
  <div style="color:#2c3e50; font-weight:bold; margin-bottom:3px;">Body (JSON):</div>
176
182
  <ul style="margin:0; padding-left:20px;">
@@ -180,7 +186,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
180
186
  </td>
181
187
  </tr>`}
182
188
 
183
- ${$code$$.not_delete?"":`
189
+ ${$code$jscomp$1_listinfo$$.not_delete?"":`
184
190
  <tr>
185
191
  <td style="padding:10px; font-weight:bold; color:#c0392b">Xo\u00e1 d\u1eef li\u1ec7u<br/>(Delete By ID)</td>
186
192
  <td style="padding:10px; text-align:center"><b>DELETE</b></td>
@@ -193,7 +199,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
193
199
  </ul>
194
200
  <div style="color:#d35400; font-weight:bold; margin-bottom:3px;">Headers ri\u00eang:</div>
195
201
  <ul style="margin:0 0 10px 0; padding-left:20px; background:#fff3e0; padding:5px 20px; border-radius:4px;">
196
- <li><b>listinfo-code:</b> <code>${$code$$.code}</code></li>
202
+ <li><b>listinfo-code:</b> <code>${$code$jscomp$1_listinfo$$.code}</code></li>
197
203
  </ul>
198
204
  </div>
199
205
  </td>
@@ -214,12 +220,12 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
214
220
  </td>
215
221
  </tr>`}
216
222
  </table>
217
- `;$id_app$$=await renderSchema($code$$.fields,$id_app$$,void 0,void 0,$code$$);return`
223
+ `;$id_app$$=await renderSchema($code$jscomp$1_listinfo$$.fields,$id_app$$,void 0,void 0,$code$jscomp$1_listinfo$$);return`
218
224
  <!DOCTYPE html>
219
225
  <html lang="vi">
220
226
  <head>
221
227
  <meta charset="utf-8">
222
- <title>T\u00e0i li\u1ec7u API - ${$code$$.title}</title>
228
+ <title>T\u00e0i li\u1ec7u API - ${$code$jscomp$1_listinfo$$.title}</title>
223
229
  <style>
224
230
  body { font-family: 'Segoe UI', Tahoma, sans-serif; line-height: 1.5; color: #333; padding: 30px; max-width: 1200px; margin: 0 auto; background: #fafafa; }
225
231
  h2 { color: #2c3e50; border-bottom: 3px solid #3498db; padding-bottom: 15px; margin-top:0; }
@@ -230,7 +236,7 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
230
236
  </style>
231
237
  </head>
232
238
  <body>
233
- <h2>T\u00c0I LI\u1ec6U API: ${$code$$.title}</h2>
239
+ <h2>T\u00c0I LI\u1ec6U API: ${$code$jscomp$1_listinfo$$.title}</h2>
234
240
 
235
241
  <div class="auth-box">
236
242
  <strong style="font-size:1.1em;">Th\u00f4ng tin Headers d\u00f9ng chung cho T\u1ea4T C\u1ea2 request:</strong>
@@ -264,9 +270,24 @@ $id_app$$||"{id_app}");$authLink_controller$$=`${$endpointsHtml_server_url$$}/ap
264
270
  <td style="padding:10px;"><code style="color:${$typeColor$$}; background:#f9f2f4; padding:2px 4px; border-radius:3px;">${$col$$.datatype||"String"}</code></td>
265
271
  <td style="padding:10px; font-weight:500;">${$col$$.header||""}</td>
266
272
  </tr>`}).join("")}
267
- </table>`:""},apiReport=async function($code$$,$id_app$jscomp$4_schemaHtml$$){var $endpointsHtml$jscomp$1_server_url$$=configs.api_url||configs.domain;$code$$=await global.getModel("reportinfo").findOne({code:$code$$}).lean();if(!$code$$)return"Kh\u00f4ng t\u00ecm th\u1ea5y t\u00e0i li\u1ec7u cho b\u00e1o c\u00e1o n\u00e0y.";var $columnsHtml_listinfo$$;$code$$.form_condition_info&&($columnsHtml_listinfo$$=await global.getModel("listinfo").findOne({code:$code$$.form_condition_info}).lean());$columnsHtml_listinfo$$=
268
- $columnsHtml_listinfo$$||{fields:[]};var $authLink$jscomp$1_controller$$=global.report_controllers[($code$$.api_code||$code$$.code).toUpperCase()];if(!$authLink$jscomp$1_controller$$)return"Report Controller ch\u01b0a s\u1eb5n s\u00e0ng.";let $route_name$$=`${$authLink$jscomp$1_controller$$.base_path}${$authLink$jscomp$1_controller$$.module}`.replace(":id_app",$id_app$jscomp$4_schemaHtml$$||"{id_app}");$authLink$jscomp$1_controller$$=`${$endpointsHtml$jscomp$1_server_url$$}/api-docs-auth`;let $idAppNote$$=
269
- $route_name$$.indexOf("id_app")>=0?'<li style="color:#8e44ad; margin-top:4px;"><b>id_app:</b> ID c\u1ee7a c\u00f4ng ty/chi nh\u00e1nh \u0111ang thao t\u00e1c (n\u1eb1m trong URL)</li>':"";$endpointsHtml$jscomp$1_server_url$$=`
273
+ </table>`:""},apiReport=async function($agentCopyHtml_agentDef_agentDefJson_code$$,$id_app$jscomp$4_schemaHtml$$){var $endpointsHtml$jscomp$1_server_url$$=configs.api_url||configs.domain;let $reportInfo$$=await global.getModel("reportinfo").findOne({code:$agentCopyHtml_agentDef_agentDefJson_code$$}).lean();if(!$reportInfo$$)return"Kh\u00f4ng t\u00ecm th\u1ea5y t\u00e0i li\u1ec7u cho b\u00e1o c\u00e1o n\u00e0y.";var $columnsHtml_listinfo$$;$reportInfo$$.form_condition_info&&($columnsHtml_listinfo$$=
274
+ await global.getModel("listinfo").findOne({code:$reportInfo$$.form_condition_info}).lean());$columnsHtml_listinfo$$=$columnsHtml_listinfo$$||{fields:[]};var $authLink$jscomp$1_controller$$=global.report_controllers[($reportInfo$$.api_code||$reportInfo$$.code).toUpperCase()];if(!$authLink$jscomp$1_controller$$)return"Report Controller ch\u01b0a s\u1eb5n s\u00e0ng.";let $route_name$$=`${$authLink$jscomp$1_controller$$.base_path}${$authLink$jscomp$1_controller$$.module}`.replace(":id_app",$id_app$jscomp$4_schemaHtml$$||
275
+ "{id_app}");$authLink$jscomp$1_controller$$=`${$endpointsHtml$jscomp$1_server_url$$}/api-docs-auth`;let $idAppNote$$=$route_name$$.indexOf("id_app")>=0?'<li style="color:#8e44ad; margin-top:4px;"><b>id_app:</b> ID c\u1ee7a c\u00f4ng ty/chi nh\u00e1nh \u0111ang thao t\u00e1c (n\u1eb1m trong URL)</li>':"";$agentCopyHtml_agentDef_agentDefJson_code$$=await getAgentReportDefinition($agentCopyHtml_agentDef_agentDefJson_code$$,$id_app$jscomp$4_schemaHtml$$);$agentCopyHtml_agentDef_agentDefJson_code$$=JSON.stringify($agentCopyHtml_agentDef_agentDefJson_code$$,
276
+ null,2).replace(/</g,"\\u003c").replace(/>/g,"\\u003e");const $agentTextareaId$$="agent_schema_"+Math.random().toString(36).substr(2,9);$agentCopyHtml_agentDef_agentDefJson_code$$=`
277
+ <div style="background: #f0f7ff; padding: 15px 20px; border-left: 5px solid #2980b9; border-radius: 4px; margin-bottom: 30px; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
278
+ <div>
279
+ <strong style="color: #2980b9; font-size: 1.1em;">\ud83e\udd16 C\u1ea5u tr\u00fac JSON d\u00e0nh cho AI Agent</strong>
280
+ <p style="margin: 5px 0 0 0; color: #555; font-size: 0.9em;">Copy file JSON \u0111\u1ecbnh ngh\u0129a chu\u1ea9n n\u00e0y \u0111\u1ec3 n\u1ea1p v\u00e0o prompt/context cho AI Agent hi\u1ec3u c\u00e1ch g\u1ecdi b\u00e1o c\u00e1o.</p>
281
+ </div>
282
+ <div>
283
+ <textarea id="${$agentTextareaId$$}" style="position:absolute; left:-9999px; top:0;" aria-hidden="true">${$agentCopyHtml_agentDef_agentDefJson_code$$}</textarea>
284
+ <button onclick="var t=document.getElementById('${$agentTextareaId$$}'); t.select(); document.execCommand('copy'); var b=this; var o=b.innerHTML; b.innerHTML='\u2705 \u0110\u00e3 Copy JSON!'; b.style.background='#27ae60'; setTimeout(function(){b.innerHTML=o; b.style.background='#2980b9';},2000);"
285
+ style="background: #2980b9; color: #fff; border: none; padding: 8px 16px; border-radius: 4px; cursor: pointer; font-size: 13px; transition: 0.2s; font-weight: bold; box-shadow: 0 1px 2px rgba(0,0,0,0.2); white-space: nowrap;">
286
+ \ud83d\udccb Copy JSON cho Agent
287
+ </button>
288
+ </div>
289
+ </div>
290
+ `;$endpointsHtml$jscomp$1_server_url$$=`
270
291
  <h3>1. Th\u00f4ng tin Endpoint B\u00e1o c\u00e1o</h3>
271
292
  <table style="width:100%; border-collapse:collapse; margin-bottom:30px" border="1" bordercolor="#ddd">
272
293
  <tr style="background:#eee">
@@ -296,12 +317,12 @@ $route_name$$.indexOf("id_app")>=0?'<li style="color:#8e44ad; margin-top:4px;"><
296
317
  </tr>
297
318
  </table>
298
319
  `;$id_app$jscomp$4_schemaHtml$$=$columnsHtml_listinfo$$.fields&&$columnsHtml_listinfo$$.fields.length>0?await renderSchema($columnsHtml_listinfo$$.fields,$id_app$jscomp$4_schemaHtml$$,void 0,void 0,$columnsHtml_listinfo$$):'<div style="padding: 15px; background: #f9f9f9; color: #666; font-style: italic; border-left: 3px solid #ccc;">B\u00e1o c\u00e1o n\u00e0y kh\u00f4ng y\u00eau c\u1ea7u tham s\u1ed1 l\u1ecdc (G\u1eedi Body l\u00e0 m\u1ed9t Object r\u1ed7ng <code>{}</code>).</div>';$columnsHtml_listinfo$$=
299
- $code$$.columns&&$code$$.columns.length>0?renderReportColumns($code$$.columns):'<div style="padding: 15px; background: #fdfaf0; color: #d35400; font-style: italic; border-left: 3px solid #e67e22;">Ch\u01b0a c\u00f3 c\u1ea5u h\u00ecnh chi ti\u1ebft c\u00e1c c\u1ed9t tr\u1ea3 v\u1ec1 cho b\u00e1o c\u00e1o n\u00e0y.</div>';return`
320
+ $reportInfo$$.columns&&$reportInfo$$.columns.length>0?renderReportColumns($reportInfo$$.columns):'<div style="padding: 15px; background: #fdfaf0; color: #d35400; font-style: italic; border-left: 3px solid #e67e22;">Ch\u01b0a c\u00f3 c\u1ea5u h\u00ecnh chi ti\u1ebft c\u00e1c c\u1ed9t tr\u1ea3 v\u1ec1 cho b\u00e1o c\u00e1o n\u00e0y.</div>';return`
300
321
  <!DOCTYPE html>
301
322
  <html lang="vi">
302
323
  <head>
303
324
  <meta charset="utf-8">
304
- <title>T\u00e0i li\u1ec7u API Report - ${$code$$.title}</title>
325
+ <title>T\u00e0i li\u1ec7u API Report - ${$reportInfo$$.title}</title>
305
326
  <style>
306
327
  body { font-family: 'Segoe UI', Tahoma, sans-serif; line-height: 1.5; color: #333; padding: 30px; max-width: 1200px; margin: 0 auto; background: #fafafa; }
307
328
  h2 { color: #2c3e50; border-bottom: 3px solid #e67e22; padding-bottom: 15px; margin-top:0; }
@@ -312,7 +333,7 @@ $code$$.columns&&$code$$.columns.length>0?renderReportColumns($code$$.columns):'
312
333
  </style>
313
334
  </head>
314
335
  <body>
315
- <h2>API REPORT: ${$code$$.title}</h2>
336
+ <h2>API REPORT: ${$reportInfo$$.title}</h2>
316
337
 
317
338
  <div class="auth-box">
318
339
  <strong style="font-size:1.1em;">Th\u00f4ng tin Headers b\u1eaft bu\u1ed9c cho Request:</strong>
@@ -322,7 +343,7 @@ $code$$.columns&&$code$$.columns.length>0?renderReportColumns($code$$.columns):'
322
343
  </ul>
323
344
  </div>
324
345
 
325
- ${$endpointsHtml$jscomp$1_server_url$$}
346
+ ${$agentCopyHtml_agentDef_agentDefJson_code$$} ${$endpointsHtml$jscomp$1_server_url$$}
326
347
 
327
348
  <h3>2. B\u1ea3n \u0111\u1ed3 D\u1eef ki\u1ec7n L\u1ecdc (Body Request)</h3>
328
349
  <p style="color:#555; font-size:0.95em;">
@@ -574,4 +595,19 @@ $code$$.columns&&$code$$.columns.length>0?renderReportColumns($code$$.columns):'
574
595
  </div>
575
596
  </body>
576
597
  </html>
577
- `};module.exports={apiDocs,apiReport,apiAuth,buildJsonSchema};
598
+ `},getAgentReportDefinition=async function($query$$,$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$){if(!$query$$)return{error:"Vui l\u00f2ng cung c\u1ea5p m\u00e3 ho\u1eb7c t\u00ean b\u00e1o c\u00e1o c\u1ea7n t\u00ecm."};const $cacheKey$$=`reportdef_${$query$$.toLowerCase().trim()}`;if(global.__aiReportDefCache){var $ReportModel_cached_inputConditionsSchema_topScore$$=global.__aiReportDefCache.get($cacheKey$$);if($ReportModel_cached_inputConditionsSchema_topScore$$&&Date.now()-
599
+ $ReportModel_cached_inputConditionsSchema_topScore$$.timestamp<3E5)return $ReportModel_cached_inputConditionsSchema_topScore$$.result}else global.__aiReportDefCache=new Map;$ReportModel_cached_inputConditionsSchema_topScore$$=global.getModel("reportinfo");if(!$ReportModel_cached_inputConditionsSchema_topScore$$)return{error:"Model 'reportinfo' kh\u00f4ng t\u1ed3n t\u1ea1i."};const $keywords$$=($rawQuery$$=>{let $q$$=$rawQuery$$.trim();$q$$=$q$$.replace(/^(xem|ch\u1ea1y|xu\u1ea5t|in|l\u1ea5y|cho\s+(?:t\u00f4i|anh|em|m\u00ecnh)\s*(?:xem|bi\u1ebft)?|t\u00f4i\s+mu\u1ed1n\s+(?:xem)?|t\u1ea1o|m\u1edf)\s+/i,
600
+ "");const $withoutPrefix$$=$q$$.replace(/^b\u00e1o\s*c\u00e1o\s+/i,"").trim(),$withoutTime$$=$withoutPrefix$$.replace(/\s+(th\u00e1ng\s+(?:n\u00e0y|tr\u01b0\u1edbc|nay|\d+)|qu\u00fd\s+\d+|n\u0103m\s+(?:nay|ngo\u00e1i|\d+)|h\u00f4m\s+nay|tu\u1ea7n\s+n\u00e0y).*$/i,"").trim(),$keywords$$=new Set;$withoutTime$$&&$withoutTime$$!==$rawQuery$$&&$keywords$$.add($withoutTime$$);$withoutPrefix$$&&$withoutPrefix$$!==$withoutTime$$&&$keywords$$.add($withoutPrefix$$);$q$$!==$rawQuery$$&&$keywords$$.add($q$$);
601
+ $keywords$$.add($rawQuery$$);return[...$keywords$$].filter($k$$=>$k$$.length>1)})($query$$),$seenCodes$$=new Set;let $allCandidates$$=[];var $addUnique_secondScore$$=($docs$$=[])=>{for(const $r$$ of $docs$$)$seenCodes$$.has($r$$.code)||($seenCodes$$.add($r$$.code),$allCandidates$$.push($r$$))},$byPhrase_byTitle_exactMatch$$=await $ReportModel_cached_inputConditionsSchema_topScore$$.find({$or:[{code:$query$$},{api_code:$query$$}]}).lean();$addUnique_secondScore$$($byPhrase_byTitle_exactMatch$$);if(!$allCandidates$$.length)for(var $byFirst_kw_words$$ of $keywords$$)if($byPhrase_byTitle_exactMatch$$=
602
+ await $ReportModel_cached_inputConditionsSchema_topScore$$.find({$or:[{title:{$regex:$byFirst_kw_words$$,$options:"i"}},{api_code:{$regex:$byFirst_kw_words$$,$options:"i"}}]}).lean(),$addUnique_secondScore$$($byPhrase_byTitle_exactMatch$$),$allCandidates$$.length)break;if(!$allCandidates$$.length)for(var $kw$jscomp$1_reportInfo$$ of $keywords$$)if($kw$jscomp$1_reportInfo$$.includes(" ")){$byFirst_kw_words$$=$kw$jscomp$1_reportInfo$$.split(" ").filter($w$$=>$w$$.length>1);if($byFirst_kw_words$$.length>=
603
+ 2&&($byPhrase_byTitle_exactMatch$$=await $ReportModel_cached_inputConditionsSchema_topScore$$.find({$and:$byFirst_kw_words$$.slice(0,Math.min(3,$byFirst_kw_words$$.length)).map($w$$=>({title:{$regex:$w$$,$options:"i"}}))}).lean(),$addUnique_secondScore$$($byPhrase_byTitle_exactMatch$$),$allCandidates$$.length))break;if(!$allCandidates$$.length&&$byFirst_kw_words$$.length>0&&($byFirst_kw_words$$=await $ReportModel_cached_inputConditionsSchema_topScore$$.find({title:{$regex:$byFirst_kw_words$$[0],$options:"i"}}).lean(),
604
+ $addUnique_secondScore$$($byFirst_kw_words$$),$allCandidates$$.length))break}if(!$allCandidates$$.length)return $id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$={error:"Kh\u00f4ng t\u00ecm th\u1ea5y b\u00e1o c\u00e1o n\u00e0o kh\u1edbp v\u1edbi t\u1eeb kh\u00f3a.",query_used:$query$$,keywords_tried:$keywords$$},global.__aiReportDefCache.set($cacheKey$$,{timestamp:Date.now(),result:$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$}),$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$;
605
+ const $scoreReport$$=$apiCode_report_words$$=>{const $title$$=($apiCode_report_words$$.title||"").toLowerCase();var $code$$=($apiCode_report_words$$.code||"").toLowerCase();$apiCode_report_words$$=($apiCode_report_words$$.api_code||"").toLowerCase();var $kwLower_matchedCount_qLower$$=$query$$.toLowerCase();if($code$$===$kwLower_matchedCount_qLower$$||$apiCode_report_words$$===$kwLower_matchedCount_qLower$$)return 1E3;$code$$=0;for(const $kw$$ of $keywords$$)$kwLower_matchedCount_qLower$$=$kw$$.toLowerCase(),
606
+ $apiCode_report_words$$=$kwLower_matchedCount_qLower$$.split(" ").filter($w$$=>$w$$.length>1),$title$$===$kwLower_matchedCount_qLower$$?$code$$+=200:$title$$.startsWith($kwLower_matchedCount_qLower$$)?$code$$+=80:$title$$.includes($kwLower_matchedCount_qLower$$)&&($code$$+=40),$apiCode_report_words$$.length>1&&($kwLower_matchedCount_qLower$$=$apiCode_report_words$$.filter($w$$=>$title$$.includes($w$$)).length,$code$$+=$kwLower_matchedCount_qLower$$*10,$kwLower_matchedCount_qLower$$===$apiCode_report_words$$.length&&
607
+ ($code$$+=30));return $code$$};$kw$jscomp$1_reportInfo$$=$allCandidates$$.map($r$$=>({report:$r$$,score:$scoreReport$$($r$$)})).sort(($a$$,$b$$)=>$b$$.score-$a$$.score);$ReportModel_cached_inputConditionsSchema_topScore$$=$kw$jscomp$1_reportInfo$$[0].score;$addUnique_secondScore$$=$kw$jscomp$1_reportInfo$$.length>1?$kw$jscomp$1_reportInfo$$[1].score:0;if(!($kw$jscomp$1_reportInfo$$.length===1||$ReportModel_cached_inputConditionsSchema_topScore$$>=1E3||$ReportModel_cached_inputConditionsSchema_topScore$$>=
608
+ 200||$ReportModel_cached_inputConditionsSchema_topScore$$>=80&&$ReportModel_cached_inputConditionsSchema_topScore$$>=$addUnique_secondScore$$*2))return $id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$={status:"multiple_matches",message:`T\u00ecm th\u1ea5y ${$kw$jscomp$1_reportInfo$$.length} b\u00e1o c\u00e1o ph\u00f9 h\u1ee3p. H\u00e3y d\u00f9ng ch\u00ednh x\u00e1c 'code' \u0111\u1ec3 l\u1ea5y c\u1ea5u tr\u00fac chi ti\u1ebft.`,suggestions:$kw$jscomp$1_reportInfo$$.slice(0,5).map(({report:$r$$,
609
+ score:$s$$})=>({code:$r$$.code,api_code:$r$$.api_code||$r$$.code,title:$r$$.title,description:$r$$.description||`B\u00e1o c\u00e1o ${$r$$.title}`,_relevance_score:$s$$}))},global.__aiReportDefCache.set($cacheKey$$,{timestamp:Date.now(),result:$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$}),$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$;$kw$jscomp$1_reportInfo$$=$kw$jscomp$1_reportInfo$$[0].report;$ReportModel_cached_inputConditionsSchema_topScore$$={};
610
+ if($kw$jscomp$1_reportInfo$$.form_condition_info)try{const $listinfo$$=await global.getModel("listinfo").findOne({code:$kw$jscomp$1_reportInfo$$.form_condition_info}).lean();$listinfo$$?.fields?.length>0&&($ReportModel_cached_inputConditionsSchema_topScore$$=await buildJsonSchema($listinfo$$.fields,0,$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$,$listinfo$$),$ReportModel_cached_inputConditionsSchema_topScore$$=JSON.parse(JSON.stringify($ReportModel_cached_inputConditionsSchema_topScore$$)))}catch($e$$){console.error("[AI Agent] L\u1ed7i khi build input schema cho b\u00e1o c\u00e1o:",
611
+ $e$$)}$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$=[];$kw$jscomp$1_reportInfo$$.columns?.length>0&&($id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$=[...$kw$jscomp$1_reportInfo$$.columns].sort(($a$$,$b$$)=>($a$$.stt||0)-($b$$.stt||0)).map($col$$=>({field_name:$col$$.field,data_type:$col$$.datatype||"String",description:$col$$.header||""})));$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$={status:"success",report_info:{code:$kw$jscomp$1_reportInfo$$.code,
612
+ api_code:$kw$jscomp$1_reportInfo$$.api_code||$kw$jscomp$1_reportInfo$$.code,title:$kw$jscomp$1_reportInfo$$.title},input_conditions_schema:$ReportModel_cached_inputConditionsSchema_topScore$$,output_columns_schema:$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$};global.__aiReportDefCache.set($cacheKey$$,{timestamp:Date.now(),result:$id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$});return $id_app$jscomp$5_outputColumnsSchema_result_result$jscomp$1_result$$};
613
+ module.exports={apiDocs,apiReport,apiAuth,buildJsonSchema,getAgentReportDefinition};
@@ -1,19 +1,16 @@
1
- const databanle=require("./databanle"),async=require("async");
2
- module.exports=async function($condition$$){if(!$condition$$.id_app)throw"B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1:id_app";$condition$$.den_ngay||($condition$$.den_ngay=new Date);const $query$$={id_app:$condition$$.id_app};$query$$.ngay_ct={$lte:$condition$$.den_ngay};$condition$$.tu_ngay&&($query$$.ngay_ct.$gte=$condition$$.tu_ngay);$condition$$.ma_dvcs&&($query$$.ma_dvcs=$condition$$.ma_dvcs);$condition$$.ma_nv&&($query$$.ma_nv=$condition$$.ma_nv);$condition$$.user_nv_giao_hang&&
3
- ($query$$.user_nv_giao_hang=$condition$$.user_nv_giao_hang);$condition$$.ma_kho&&($query$$.$or=[{ma_kho:$condition$$.ma_kho},{"details.ma_kho":$condition$$.ma_kho}]);if($condition$$.ma_kh)$query$$.ma_kh=$condition$$.ma_kh;else{var $condition_kh_custs_q$$={};if($condition$$.nh_kh){var $nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh,{status:!0});$condition_kh_custs_q$$.nh_kh={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$}}$condition$$.nh_kh2&&
4
- ($nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh2,{status:!0}),$condition_kh_custs_q$$.nh_kh2={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$});$condition$$.nh_kh3&&($nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh3,{status:!0}),$condition_kh_custs_q$$.nh_kh3={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$});$condition$$.nh_kh4&&($nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=
5
- await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh4,{status:!0}),$condition_kh_custs_q$$.nh_kh4={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$});$condition$$.nh_kh5&&($nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$=await global.getModel("group").getAllChildrenGroup($condition$$.id_app,$condition$$.nh_kh5,{status:!0}),$condition_kh_custs_q$$.nh_kh5={$in:$nh_khs_nh_khs2_nh_khs3_nh_khs4_nh_khs5$$});Object.keys($condition_kh_custs_q$$).length>0&&($condition_kh_custs_q$$.id_app=
6
- $condition$$.id_app,$condition_kh_custs_q$$=await global.getModel("customer").find($condition_kh_custs_q$$,{ma_kh:1}).lean(),$query$$.ma_kh={$in:$condition_kh_custs_q$$.map($c$$=>$c$$.ma_kh)})}$condition$$.ma_ct&&($query$$.ma_ct=$condition$$.ma_ct);$condition$$.user_hoa_hong&&($query$$.$or?$query$$.$and=[{$or:[{user_nv:$condition$$.user_hoa_hong},{user_ctv:$condition$$.user_hoa_hong},{user_gt:$condition$$.user_hoa_hong},{user_ql:$condition$$.user_hoa_hong},{"details.user_nv":$condition$$.user_hoa_hong},
7
- {"details.user_ctv":$condition$$.user_hoa_hong},{"details.user_gt":$condition$$.user_hoa_hong},{"details.user_ql":$condition$$.user_hoa_hong}]}]:$query$$.$or=[{user_nv:$condition$$.user_hoa_hong},{user_ctv:$condition$$.user_hoa_hong},{user_gt:$condition$$.user_hoa_hong},{user_ql:$condition$$.user_hoa_hong},{"details.user_nv":$condition$$.user_hoa_hong},{"details.user_ctv":$condition$$.user_hoa_hong},{"details.user_gt":$condition$$.user_hoa_hong},{"details.user_ql":$condition$$.user_hoa_hong}]);let $ma_vts$$;
8
- if($condition$$.ma_vt)$query$$["details.ma_vt"]=$condition$$.ma_vt,$ma_vts$$=[$condition$$.ma_vt];else if($condition$$.ma_ncc||$condition$$.ma_nvt||$condition$$.ma_danh_muc_cty||$condition$$.ma_danh_muc)$condition_kh_custs_q$$={id_app:$condition$$.id_app},$condition$$.ma_ncc&&($condition_kh_custs_q$$.ma_ncc=$condition$$.ma_ncc),$condition$$.ma_nvt&&($condition_kh_custs_q$$.ma_nvt=$condition$$.ma_nvt),$condition$$.ma_danh_muc_cty&&($condition_kh_custs_q$$.ma_danh_muc_cty=$condition$$.ma_danh_muc_cty),
9
- $condition$$.ma_danh_muc&&($condition_kh_custs_q$$.ma_danh_muc=$condition$$.ma_danh_muc),$ma_vts$$=(await global.getModel("dmvt").find($condition_kh_custs_q$$,{ma_vt:1}).lean()).map($v$$=>$v$$.ma_vt),$query$$["details.ma_vt"]={$in:$ma_vts$$};$condition$$.id_cs&&($query$$["details.id_cs"]=$condition$$.id_cs);$condition$$.id_cs_ck&&($query$$["details.id_cs_ck"]=$condition$$.id_cs_ck);$query$$.show_payment_method=!1;return new Promise(($resolve$$,$reject$$)=>{databanle($query$$,function($error$$,$rs$$){if($error$$)return $reject$$($error$$);
10
- async.mapLimit($rs$$,100,function($r$$,$callback$$){let $details$$=$r$$.details||[];delete $r$$.details;$details$$=$details$$.map($_r_detail$$=>{delete $_r_detail$$._id;$_r_detail$$.nxt=2;$_r_detail$$={...$r$$,...$_r_detail$$};$_r_detail$$.ma_nv||($_r_detail$$.ma_nv=$r$$.ma_nv);$_r_detail$$.ma_dt||($_r_detail$$.ma_dt=$r$$.ma_dt);$_r_detail$$.ma_bp||($_r_detail$$.ma_bp=$r$$.ma_bp);$_r_detail$$.ma_kh||($_r_detail$$.ma_kh=$r$$.ma_kh);return $_r_detail$$});$r$$.details_tl&&($details$$=[...$details$$,
11
- ...$r$$.details_tl.map($detail$$=>{delete $detail$$._id;$detail$$={...$r$$,...$detail$$};$detail$$.ma_nv||($detail$$.ma_nv=$r$$.ma_nv);$detail$$.ma_dt||($detail$$.ma_dt=$r$$.ma_dt);$detail$$.ma_bp||($detail$$.ma_bp=$r$$.ma_bp);$detail$$.tien_hang_nt=0-$detail$$.tien_hang_nt||0;$detail$$.tien_hang=0-($detail$$.tien_hang||0);$detail$$.tien_hang_ct_nt=0-$detail$$.tien_hang_ct_nt||0;$detail$$.tien_hang_ct=0-($detail$$.tien_hang_ct||0);$detail$$.tien_ck=0-($detail$$.tien_ck||0);$detail$$.tien_ck_nt=0-
12
- ($detail$$.tien_ck_nt||0);$detail$$.tien_thue=0-($detail$$.tien_thue||0);$detail$$.tien_thue_nt=0-($detail$$.tien_thue_nt||0);$detail$$.tien_xuat_tmp=0-($detail$$.tien_nhap_nt||0);$detail$$.tien_xuat_nt=0-($detail$$.tien_nhap_nt||0);$detail$$.sl_xuat=0;$detail$$.tien_hoa_hong=0-$detail$$.tien_hoa_hong;$detail$$.tien_hoa_hong_gt=0-$detail$$.tien_hoa_hong_gt;$detail$$.tien_hoa_hong_ctv=0-$detail$$.tien_hoa_hong_ctv;$detail$$.tien_hoa_hong_ql=0-$detail$$.tien_hoa_hong_ql;$detail$$.nxt=1;return $detail$$})],
13
- delete $r$$.details_tl);$callback$$(null,$details$$)},async function($error$$,$report$$){if($error$$)return $reject$$($error$$);$report$$=$report$$.reduce(($a$$,$b$$)=>$a$$.concat($b$$),[]);$ma_vts$$&&($report$$=$report$$.filter($r$$=>$ma_vts$$.indexOf($r$$.ma_vt)>=0));$condition$$.id_cs&&($report$$=$report$$.filter($r$$=>$r$$.id_cs===$condition$$.id_cs));$condition$$.id_cs_ck&&($report$$=$report$$.filter($r$$=>$r$$.id_cs_ck===$condition$$.id_cs_ck));await $report$$.asyncJoinModel2($condition$$.id_app,
14
- "dmqddvt",{where:{ma_dvt:"ma_dvt",ma_vt:"ma_vt"},fields:["tu","mau","ma_dvt_goc"]});$report$$.forEach(function($r$$,$he_so_qd_i$$){$r$$.stt=$he_so_qd_i$$+1;$r$$.id_ct=$r$$._id.toString();$r$$.bold=!1;$r$$.sysorder=5;$r$$.tt=$r$$.tien=($r$$.tien_hang_ct||0)-($r$$.tien_ck||0)+($r$$.tien_thue||0);$r$$.loi_nhuan_tmp=$r$$.tien_xuat_tmp?$r$$.tt-$r$$.tien_xuat_tmp-($r$$.tien_thue||0)-($r$$.tien_cp||0):0;$r$$.loi_nhuan=$r$$.tt-($r$$.tien_xuat||0)-($r$$.tien_thue||0)-($r$$.tien_cp||0);$r$$.t_hoa_hong=$r$$.tien_hoa_hong+
15
- $r$$.tien_hoa_hong_gt+$r$$.tien_hoa_hong_ctv+$r$$.tien_hoa_hong_ql;$r$$.mau?($he_so_qd_i$$=$r$$.tu/$r$$.mau,$r$$.sl_xuat_qd=$he_so_qd_i$$*($r$$.sl_xuat||0),$r$$.sl_nhap_qd=$he_so_qd_i$$*($r$$.sl_nhap||0)):($r$$.sl_xuat_qd=$r$$.sl_xuat||0,$r$$.sl_nhap_qd=$r$$.sl_nhap||0);$r$$.sl_xuat_qd_bc=$r$$.sl_xuat_qd;$r$$.sl_nhap_qd_bc=$r$$.sl_nhap_qd;$r$$.ma_dvt_bc=$r$$.ma_dvt_goc||$r$$.ma_dvt});$condition$$.ma_dvt&&await $report$$.asyncJoinModel2($condition$$.id_app,"dmqddvt",{where:$item$$=>({ma_vt:$item$$.ma_vt,
16
- ma_dvt:$condition$$.ma_dvt}),setFields:($item$$,$he_so_qd$$)=>{$he_so_qd$$&&$he_so_qd$$.tu?($item$$.ma_dvt_bc=$condition$$.ma_dvt,$he_so_qd$$=$he_so_qd$$.mau/$he_so_qd$$.tu,$item$$.sl_xuat_qd_bc=$he_so_qd$$*($item$$.sl_xuat_qd||0),$item$$.sl_nhap_qd_bc=$he_so_qd$$*($item$$.sl_nhap_qd||0)):($item$$.sl_xuat_qd_bc=$item$$.sl_xuat_qd,$item$$.sl_nhap_qd_bc=$item$$.sl_nhap_qd)}});if($condition$$.tinh_tong_cong||$condition$$.chi_hien_tong_cong){let $total_row$$={sysorder:9,bold:!0,dien_giai:"T\u1ed5ng c\u1ed9ng",
17
- ten_vt:"T\u1ed5ng c\u1ed9ng",sl_xuat:0,sl_nhap:0,sl_xuat_qd_bc:0,sl_nhap_qd_bc:0,tien_hang:0,tien_hang_ct:0,tien_ck:0,tien_thue:0,tien_cp:0,tien_xuat:0,tien_xuat_tmp:0,tt:0,tien:0,loi_nhuan:0,loi_nhuan_tmp:0,tien_hoa_hong:0,tien_hoa_hong_gt:0,tien_hoa_hong_ctv:0,tien_hoa_hong_ql:0,t_hoa_hong:0};$report$$.forEach($r$$=>{$total_row$$.sl_xuat+=$r$$.sl_xuat||0;$total_row$$.sl_nhap+=$r$$.sl_nhap||0;$total_row$$.sl_xuat_qd_bc+=$r$$.sl_xuat_qd_bc||0;$total_row$$.sl_nhap_qd_bc+=$r$$.sl_nhap_qd_bc||0;$total_row$$.tien_hang+=
18
- $r$$.tien_hang||0;$total_row$$.tien_hang_ct+=$r$$.tien_hang_ct||0;$total_row$$.tien_ck+=$r$$.tien_ck||0;$total_row$$.tien_thue+=$r$$.tien_thue||0;$total_row$$.tien_cp+=$r$$.tien_cp||0;$total_row$$.tien_xuat+=$r$$.tien_xuat||0;$total_row$$.tien_xuat_tmp+=$r$$.tien_xuat_tmp||0;$total_row$$.tt+=$r$$.tt||0;$total_row$$.tien+=$r$$.tien||0;$total_row$$.loi_nhuan+=$r$$.loi_nhuan||0;$total_row$$.loi_nhuan_tmp+=$r$$.loi_nhuan_tmp||0;$total_row$$.tien_hoa_hong+=$r$$.tien_hoa_hong||0;$total_row$$.tien_hoa_hong_gt+=
19
- $r$$.tien_hoa_hong_gt||0;$total_row$$.tien_hoa_hong_ctv+=$r$$.tien_hoa_hong_ctv||0;$total_row$$.tien_hoa_hong_ql+=$r$$.tien_hoa_hong_ql||0;$total_row$$.t_hoa_hong+=$r$$.t_hoa_hong||0});$condition$$.chi_hien_tong_cong?$report$$=[$total_row$$]:$report$$.push($total_row$$)}$resolve$$($report$$)})})})};
1
+ const databanle=require("./databanle"),FALLBACK_FIELDS=["ma_nv","ma_dt","ma_bp","ma_kh"],NEGATE_FIELDS="tien_hang_nt tien_hang tien_hang_ct_nt tien_hang_ct tien_ck tien_ck_nt tien_thue tien_thue_nt tien_hoa_hong tien_hoa_hong_gt tien_hoa_hong_ctv tien_hoa_hong_ql".split(" "),TOTAL_FIELDS="sl_xuat sl_nhap sl_xuat_qd_bc sl_nhap_qd_bc tien_hang tien_hang_ct tien_ck tien_thue tien_cp tien_xuat tien_xuat_tmp tt tien loi_nhuan loi_nhuan_tmp tien_hoa_hong tien_hoa_hong_gt tien_hoa_hong_ctv tien_hoa_hong_ql t_hoa_hong".split(" ");
2
+ module.exports=async function($condition$$){if(!$condition$$.id_app)throw"B\u00e1o c\u00e1o n\u00e0y y\u00eau c\u1ea7u c\u00e1c tham s\u1ed1: id_app";$condition$$.den_ngay||($condition$$.den_ngay=new Date);const $query$$={id_app:$condition$$.id_app};$query$$.ngay_ct={$lte:$condition$$.den_ngay};$condition$$.tu_ngay&&($query$$.ngay_ct.$gte=$condition$$.tu_ngay);$condition$$.ma_dvcs&&($query$$.ma_dvcs=$condition$$.ma_dvcs);$condition$$.ma_nv&&($query$$.ma_nv=$condition$$.ma_nv);$condition$$.user_nv_giao_hang&&
3
+ ($query$$.user_nv_giao_hang=$condition$$.user_nv_giao_hang);$condition$$.ma_kho&&($query$$.$or=[{ma_kho:$condition$$.ma_kho},{"details.ma_kho":$condition$$.ma_kho}]);$condition$$.ma_ct&&($query$$.ma_ct=$condition$$.ma_ct);$condition$$.id_cs&&($query$$["details.id_cs"]=$condition$$.id_cs);$condition$$.id_cs_ck&&($query$$["details.id_cs_ck"]=$condition$$.id_cs_ck);if($condition$$.ma_kh)$query$$.ma_kh=$condition$$.ma_kh;else{var $activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$=["nh_kh","nh_kh2","nh_kh3",
4
+ "nh_kh4","nh_kh5"].filter($f$$=>$condition$$[$f$$]);if($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.length>0){const $groupModel$$=global.getModel("group"),$nhResults$$=await Promise.all($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.map($f$$=>$groupModel$$.getAllChildrenGroup($condition$$.id_app,$condition$$[$f$$],{status:!0}))),$condition_kh$$={id_app:$condition$$.id_app};$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.forEach(($f$$,$i$$)=>{$condition_kh$$[$f$$]={$in:$nhResults$$[$i$$]}});
5
+ $activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$=await global.getModel("customer").find($condition_kh$$,{ma_kh:1}).lean();$query$$.ma_kh={$in:$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.map($c$$=>$c$$.ma_kh)}}}$condition$$.user_hoa_hong&&($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$=$condition$$.user_hoa_hong,$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$=[{user_nv:$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$},{user_ctv:$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$},{user_gt:$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$},
6
+ {user_ql:$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$},{"details.user_nv":$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$},{"details.user_ctv":$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$},{"details.user_gt":$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$},{"details.user_ql":$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$}],$query$$.$or?$query$$.$and=[{$or:$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$}]:$query$$.$or=$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$);let $ma_vts_set$$=
7
+ null;if($condition$$.ma_vt)$query$$["details.ma_vt"]=$condition$$.ma_vt,$ma_vts_set$$=new Set([$condition$$.ma_vt]);else if($condition$$.ma_ncc||$condition$$.ma_nvt||$condition$$.ma_danh_muc_cty||$condition$$.ma_danh_muc)$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$={id_app:$condition$$.id_app},$condition$$.ma_ncc&&($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.ma_ncc=$condition$$.ma_ncc),$condition$$.ma_nvt&&($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.ma_nvt=$condition$$.ma_nvt),$condition$$.ma_danh_muc_cty&&
8
+ ($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.ma_danh_muc_cty=$condition$$.ma_danh_muc_cty),$condition$$.ma_danh_muc&&($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.ma_danh_muc=$condition$$.ma_danh_muc),$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$=(await global.getModel("dmvt").find($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$,{ma_vt:1}).lean()).map($v$$=>$v$$.ma_vt),$ma_vts_set$$=new Set($activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$),$query$$["details.ma_vt"]={$in:$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$};
9
+ $query$$.show_payment_method=!1;$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$=await new Promise(($resolve$$,$reject$$)=>{databanle($query$$,($error$$,$rs$$)=>$error$$?$reject$$($error$$):$resolve$$($rs$$))});$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$=($ma_vts_set$$||$condition$$.id_cs||$condition$$.id_cs_ck?$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.filter($r$$=>($r$$.details||[]).some($d$$=>(!$ma_vts_set$$||$ma_vts_set$$.has($d$$.ma_vt))&&(!$condition$$.id_cs||$d$$.id_cs===$condition$$.id_cs)&&
10
+ (!$condition$$.id_cs_ck||$d$$.id_cs_ck===$condition$$.id_cs_ck))):$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$).flatMap($mainRows_r$$=>{const {details:$details$$=[],details_tl:$details_tl$$,...$header$$}=$mainRows_r$$;$mainRows_r$$=$details$$.filter($d$$=>(!$ma_vts_set$$||$ma_vts_set$$.has($d$$.ma_vt))&&(!$condition$$.id_cs||$d$$.id_cs===$condition$$.id_cs)&&(!$condition$$.id_cs_ck||$d$$.id_cs_ck===$condition$$.id_cs_ck)).map(({_id:$_id_row$$,...$detail$$})=>{$_id_row$$={...$header$$,...$detail$$,
11
+ nxt:2};for(const $f$$ of FALLBACK_FIELDS)$_id_row$$[$f$$]||($_id_row$$[$f$$]=$header$$[$f$$]);return $_id_row$$});if(!$details_tl$$)return $mainRows_r$$;const $tlRows$$=$details_tl$$.map(({_id:$_id$$,...$detail$$})=>{$_id$$={...$header$$,...$detail$$,nxt:1};for(const $f$$ of FALLBACK_FIELDS)$_id$$[$f$$]||($_id$$[$f$$]=$header$$[$f$$]);for(const $f$$ of NEGATE_FIELDS)$_id$$[$f$$]=-($_id$$[$f$$]||0);$_id$$.tien_xuat_tmp=-($_id$$.tien_nhap_nt||0);$_id$$.tien_xuat_nt=-($_id$$.tien_nhap_nt||0);$_id$$.sl_xuat=
12
+ 0;return $_id$$});return[...$mainRows_r$$,...$tlRows$$]});await $activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.asyncJoinModel2($condition$$.id_app,"dmqddvt",{where:{ma_dvt:"ma_dvt",ma_vt:"ma_vt"},fields:["tu","mau","ma_dvt_goc"]});$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.forEach(($r$$,$he_so_i$$)=>{$r$$.stt=$he_so_i$$+1;$r$$.id_ct=$r$$._id?.toString();$r$$.bold=!1;$r$$.sysorder=5;$r$$.tt=$r$$.tien=($r$$.tien_hang_ct||0)-($r$$.tien_ck||0)+($r$$.tien_thue||0);$r$$.loi_nhuan_tmp=$r$$.tien_xuat_tmp?
13
+ $r$$.tt-$r$$.tien_xuat_tmp-($r$$.tien_thue||0)-($r$$.tien_cp||0):0;$r$$.loi_nhuan=$r$$.tt-($r$$.tien_xuat||0)-($r$$.tien_thue||0)-($r$$.tien_cp||0);$r$$.t_hoa_hong=($r$$.tien_hoa_hong||0)+($r$$.tien_hoa_hong_gt||0)+($r$$.tien_hoa_hong_ctv||0)+($r$$.tien_hoa_hong_ql||0);$r$$.mau?($he_so_i$$=$r$$.tu/$r$$.mau,$r$$.sl_xuat_qd=$he_so_i$$*($r$$.sl_xuat||0),$r$$.sl_nhap_qd=$he_so_i$$*($r$$.sl_nhap||0)):($r$$.sl_xuat_qd=$r$$.sl_xuat||0,$r$$.sl_nhap_qd=$r$$.sl_nhap||0);$r$$.sl_xuat_qd_bc=$r$$.sl_xuat_qd;$r$$.sl_nhap_qd_bc=
14
+ $r$$.sl_nhap_qd;$r$$.ma_dvt_bc=$r$$.ma_dvt_goc||$r$$.ma_dvt});$condition$$.ma_dvt&&await $activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.asyncJoinModel2($condition$$.id_app,"dmqddvt",{where:$item$$=>({ma_vt:$item$$.ma_vt,ma_dvt:$condition$$.ma_dvt}),setFields:($item$$,$he_so$$)=>{$he_so$$?.tu?($he_so$$=$he_so$$.mau/$he_so$$.tu,$item$$.ma_dvt_bc=$condition$$.ma_dvt,$item$$.sl_xuat_qd_bc=$he_so$$*($item$$.sl_xuat_qd||0),$item$$.sl_nhap_qd_bc=$he_so$$*($item$$.sl_nhap_qd||0)):($item$$.sl_xuat_qd_bc=
15
+ $item$$.sl_xuat_qd,$item$$.sl_nhap_qd_bc=$item$$.sl_nhap_qd)}});if($condition$$.tinh_tong_cong||$condition$$.chi_hien_tong_cong){const $total_row$$=$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$.reduce(($acc$$,$r$$)=>{for(const $f$$ of TOTAL_FIELDS)$acc$$[$f$$]+=$r$$[$f$$]||0;return $acc$$},{sysorder:9,bold:!0,dien_giai:"T\u1ed5ng c\u1ed9ng",ten_vt:"T\u1ed5ng c\u1ed9ng",...Object.fromEntries(TOTAL_FIELDS.map($f$$=>[$f$$,0]))});$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$=$condition$$.chi_hien_tong_cong?
16
+ [$total_row$$]:[...$activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$,$total_row$$]}return $activeFields_custs_hhOr_ma_vts_q_report_rs_uHH$$};
@@ -0,0 +1,8 @@
1
+ const FifoDk=global.getModel("fifo_dk"),FifoLot=global.getModel("fifo_lot"),dkvtPromise=$condition$$=>new Promise(($resolve$$,$reject$$)=>{const $result$$=require("./dkvt")($condition$$,($err$$,$res$$)=>$err$$?$reject$$($err$$):$resolve$$($res$$));$result$$&&$result$$.then&&$result$$.then($resolve$$).catch($reject$$)});
2
+ module.exports=async function($condition$$){const {id_app:$id_app$$,ma_vts:$ma_vts$$,tu_ngay:$tu_ngay$$,ma_kho:$ma_kho$$,nam:$nam$$,thang:$thang$$}=$condition$$;if(!$ma_vts$$||$ma_vts$$.length===0)return new Map;$condition$$=await FifoLot.find({id_app:$id_app$$,ma_vt:{$in:$ma_vts$$},sl_con:{$ne:0},ma_kho:$ma_kho$$?$ma_kho$$:null},{ma_vt:1}).lean();const $vtDaCoFifoLot$$=new Set($condition$$.map($l$$=>$l$$.ma_vt));$condition$$=$ma_vts$$.filter($v$$=>!$vtDaCoFifoLot$$.has($v$$));const $result$$=new Map;
3
+ $ma_vts$$.forEach($v$$=>$result$$.set($v$$,[]));if($condition$$.length===0)return $result$$;const $fifoDkRows$$=await FifoDk.find({id_app:$id_app$$,ma_vt:{$in:$condition$$},nam:$nam$$,thang_bat_dau:$thang$$,sl_con:{$gt:0},ma_kho:$ma_kho$$?$ma_kho$$:null}).sort({ma_vt:1,ngay_nhap:1}).lean(),$vtCoFifoDk$$=new Set;for(var $row_vtCanDkvt$$ of $fifoDkRows$$)$vtCoFifoDk$$.add($row_vtCanDkvt$$.ma_vt),$result$$.get($row_vtCanDkvt$$.ma_vt).push({ma_vt:$row_vtCanDkvt$$.ma_vt,sl_con:$row_vtCanDkvt$$.sl_con,
4
+ gia:$row_vtCanDkvt$$.gia,tien_con:$row_vtCanDkvt$$.tien_con||$row_vtCanDkvt$$.sl_con*$row_vtCanDkvt$$.gia,ngay_nhap:$row_vtCanDkvt$$.ngay_nhap,ma_dvt:$row_vtCanDkvt$$.ma_dvt||"",he_so_qd:$row_vtCanDkvt$$.he_so_qd||1,nguon:"fifo_dk"});$fifoDkRows$$.length>0&&FifoDk.updateMany({_id:{$in:$fifoDkRows$$.map($r$$=>$r$$._id)}},{$set:{da_su_dung:!0}}).catch($err$$=>Logger.error("[fifo_dk] L\u1ed7i update da_su_dung:",$err$$.message));$row_vtCanDkvt$$=$condition$$.filter($v$$=>!$vtCoFifoDk$$.has($v$$));if($row_vtCanDkvt$$.length>
5
+ 0){Logger.info(`[fifo_dk] Fallback dkvt cho ${$row_vtCanDkvt$$.length} VT ch\u01b0a c\u00f3 fifo_dk`);try{var $dauKyData_gia_dau$$=await dkvtPromise({id_app:$id_app$$,ma_vt:$row_vtCanDkvt$$,ngay:$tu_ngay$$,ma_kho:$ma_kho$$});const $arrDauKy$$=Array.isArray($dauKyData_gia_dau$$)?$dauKyData_gia_dau$$:$dauKyData_gia_dau$$?[$dauKyData_gia_dau$$]:[];for(var $countFifoDk_item$$ of $arrDauKy$$)if($countFifoDk_item$$?.ma_vt&&$row_vtCanDkvt$$.includes($countFifoDk_item$$.ma_vt)){var $countDkvt_ton_dau$$=$countFifoDk_item$$.ton00||
6
+ 0,$countEmpty_du_dau$$=$countFifoDk_item$$.du00||0;Math.abs($countDkvt_ton_dau$$)<1E-4||($dauKyData_gia_dau$$=$countDkvt_ton_dau$$!==0?$countEmpty_du_dau$$/$countDkvt_ton_dau$$:0,$result$$.get($countFifoDk_item$$.ma_vt).push({ma_vt:$countFifoDk_item$$.ma_vt,sl_con:$countDkvt_ton_dau$$,gia:$dauKyData_gia_dau$$>0?$dauKyData_gia_dau$$:0,tien_con:$countEmpty_du_dau$$,ngay_nhap:$tu_ngay$$,ma_dvt:"",he_so_qd:1,nguon:"dkvt"}),$countDkvt_ton_dau$$<0&&Logger.warn(`[fifo_dk] \u26a0\ufe0f T\u1ed3n \u0111\u1ea7u k\u1ef3 \u00e2m (${$countDkvt_ton_dau$$}) cho VT ${$countFifoDk_item$$.ma_vt} \u2014 ngu\u1ed3n dkvt`))}}catch($err$$){throw Logger.error("[fifo_dk] L\u1ed7i g\u1ecdi dkvt fallback:",
7
+ $err$$.message),$err$$;}}$countEmpty_du_dau$$=$countDkvt_ton_dau$$=$countFifoDk_item$$=0;for(const [$ma_vt$$,$lots$$]of $result$$.entries())$condition$$.includes($ma_vt$$)&&($lots$$.length===0?$countEmpty_du_dau$$++:$lots$$[0].nguon==="fifo_dk"?$countFifoDk_item$$++:$countDkvt_ton_dau$$++);Logger.info(`[fifo_dk] \u0110\u1ea7u k\u1ef3: ${$countFifoDk_item$$} VT t\u1eeb fifo_dk | `+`${$countDkvt_ton_dau$$} VT fallback dkvt | `+`${$countEmpty_du_dau$$} VT t\u1ed3n \u0111\u1ea7u = 0 | `+`${$vtDaCoFifoLot$$.size} VT \u0111\u00e3 c\u00f3 fifo_lot (b\u1ecf qua)`);
8
+ return $result$$};
@@ -0,0 +1,4 @@
1
+ const VALID_GROUP_FIELDS=["ma_kho","ma_lo","ma_tt1","ma_tt2","ma_tt3"];function normalizeGroupBy($groupBy$$){return Array.isArray($groupBy$$)&&$groupBy$$.length!==0?[...(new Set($groupBy$$.filter($f$$=>VALID_GROUP_FIELDS.includes($f$$))))]:[]}function getGroupKeyFromRow($row$$,$groupFields$$){return $groupFields$$.length===0?"":$groupFields$$.map($f$$=>$f$$==="ma_kho"?$row$$.ma_kho||$row$$.ma_kho_x||"":$row$$[$f$$]||"").join("|")}
2
+ function getGroupKeyFromDetail($detail$$,$voucher$$,$groupFields$$){return $groupFields$$.length===0?"":$groupFields$$.map($f$$=>$f$$==="ma_kho"?$detail$$.ma_kho||$detail$$.ma_kho_x||$voucher$$.ma_kho||$voucher$$.ma_kho_x||"":$detail$$[$f$$]||$voucher$$[$f$$]||"").join("|")}
3
+ function buildGroupFilter($groupFields$$,$groupKey$$){if($groupFields$$.length===0)return{};const $values$$=$groupKey$$.split("|"),$filter$$={};$groupFields$$.forEach(($f$$,$i$$)=>{$filter$$[$f$$]=$values$$[$i$$]||null});return $filter$$}
4
+ function groupKeyToDoc($groupFields$$,$groupKey$$){if($groupFields$$.length===0)return{};const $values$$=$groupKey$$.split("|"),$doc$$={};VALID_GROUP_FIELDS.forEach($f$$=>{$doc$$[$f$$]=null});$groupFields$$.forEach(($f$$,$i$$)=>{$doc$$[$f$$]=$values$$[$i$$]||null});return $doc$$}module.exports={VALID_GROUP_FIELDS,normalizeGroupBy,getGroupKeyFromRow,getGroupKeyFromDetail,buildGroupFilter,groupKeyToDoc};
@@ -0,0 +1,12 @@
1
+ const FifoTrace=global.getModel("fifo_trace"),FifoLot=global.getModel("fifo_lot"),dmvt=global.getModel("dmvt");
2
+ async function traceLoToChungTu($byCtXuat_condition$$,$fn$$){try{const {id_app:$id_app$$,id_fifo_lot:$id_fifo_lot$$}=$byCtXuat_condition$$;if(!$id_fifo_lot$$)return $fn$$("Thi\u1ebfu id_fifo_lot");const $traces$$=await FifoTrace.find({id_app:$id_app$$,id_fifo_lot:$id_fifo_lot$$}).sort({ngay_xuat:1,id_ct_xuat:1,detail_index:1}).lean();$byCtXuat_condition$$={};for(const $t$$ of $traces$$){const $key$$=$t$$.id_ct_xuat.toString();$byCtXuat_condition$$[$key$$]||($byCtXuat_condition$$[$key$$]={id_ct_xuat:$t$$.id_ct_xuat,
3
+ ma_ct_xuat:$t$$.ma_ct_xuat,so_ct_xuat:$t$$.so_ct_xuat,ngay_xuat:$t$$.ngay_xuat,ma_vt:$t$$.ma_vt,details:[],tong_sl_xuat:0,tong_tien:0});$byCtXuat_condition$$[$key$$].details.push({detail_index:$t$$.detail_index,sl_xuat_lo:$t$$.sl_xuat_lo,gia_von:$t$$.gia_von,tien_xuat_lo:$t$$.tien_xuat_lo,sl_lo_truoc_xuat:$t$$.sl_lo_truoc_xuat,sl_lo_sau_xuat:$t$$.sl_lo_sau_xuat});$byCtXuat_condition$$[$key$$].tong_sl_xuat+=$t$$.sl_xuat_lo;$byCtXuat_condition$$[$key$$].tong_tien+=$t$$.tien_xuat_lo}return $fn$$(null,
4
+ Object.values($byCtXuat_condition$$))}catch($err$$){return $fn$$($err$$)}}
5
+ async function traceChungTuToLo($byDetail_condition$$,$fn$$){try{const {id_app:$id_app$$,id_ct_xuat:$id_ct_xuat$$,detail_index:$detail_index$$}=$byDetail_condition$$;if(!$id_ct_xuat$$)return $fn$$("Thi\u1ebfu id_ct_xuat");$byDetail_condition$$={id_app:$id_app$$,id_ct_xuat:$id_ct_xuat$$};$detail_index$$!==void 0&&($byDetail_condition$$.detail_index=$detail_index$$);const $traces$$=await FifoTrace.find($byDetail_condition$$).sort({detail_index:1,ngay_nhap:1}).lean();Logger.warn("[traceChungTuToLo] query",
6
+ $byDetail_condition$$,$traces$$);$byDetail_condition$$={};for(const $t$$ of $traces$$){const $idx$$=$t$$.detail_index;$byDetail_condition$$[$idx$$]||($byDetail_condition$$[$idx$$]={detail_index:$idx$$,ma_vt:$t$$.ma_vt,lots:[],tong_sl:0,tong_tien:0,gia_von_tb:0});$byDetail_condition$$[$idx$$].lots.push({id_fifo_lot:$t$$.id_fifo_lot,id_ct_nhap:$t$$.id_ct_nhap,ma_ct_nhap:$t$$.ma_ct_nhap,so_ct_nhap:$t$$.so_ct_nhap,ngay_nhap:$t$$.ngay_nhap,gia_nhap:$t$$.gia_nhap,sl_xuat_lo:$t$$.sl_xuat_lo,gia_von:$t$$.gia_von,
7
+ tien_xuat_lo:$t$$.tien_xuat_lo,sl_lo_truoc:$t$$.sl_lo_truoc_xuat,sl_lo_sau:$t$$.sl_lo_sau_xuat});$byDetail_condition$$[$idx$$].tong_sl+=$t$$.sl_xuat_lo;$byDetail_condition$$[$idx$$].tong_tien+=$t$$.tien_xuat_lo}for(const $d$$ of Object.values($byDetail_condition$$))$d$$.gia_von_tb=$d$$.tong_sl>0?Math.roundBy($d$$.tong_tien/$d$$.tong_sl,0):0;return $fn$$(null,Object.values($byDetail_condition$$).sort(($a$$,$b$$)=>$a$$.detail_index-$b$$.detail_index))}catch($err$$){return $fn$$($err$$)}}
8
+ async function baoCaoFifoKy($condition$$,$fn$$){try{const {id_app:$id_app$$,nam:$nam$$,thang:$thang$$,ma_vt:$ma_vt$$,ma_kho:$ma_kho$$}=$condition$$,$traceQuery$$={id_app:$id_app$$,nam:Number($nam$$),thang:Number($thang$$)};$ma_vt$$&&($traceQuery$$.ma_vt=$ma_vt$$);$ma_kho$$&&($traceQuery$$.ma_kho=$ma_kho$$);const [$traces$$,$fifoLotsCuoiKy$$]=await Promise.all([FifoTrace.find($traceQuery$$).sort({ngay_nhap:1,id_fifo_lot:1,ngay_xuat:1}).lean(),FifoLot.find({id_app:$id_app$$,nam:$nam$$,thang:$thang$$,
9
+ ...($ma_vt$$?{ma_vt:$ma_vt$$}:{}),...($ma_kho$$?{ma_kho:$ma_kho$$}:{ma_kho:null})}).sort({ma_vt:1,ngay_nhap:1}).lean()]),$lotsMap$$=new Map;for(const $lot$$ of $fifoLotsCuoiKy$$)$lotsMap$$.set($lot$$._id.toString(),{id_fifo_lot:$lot$$._id,ma_vt:$lot$$.ma_vt,ma_kho:$lot$$.ma_kho,id_ct_nhap:$lot$$.id_ct,ma_ct_nhap:$lot$$.ma_ct,so_ct_nhap:$lot$$.so_ct,ngay_nhap:$lot$$.ngay_nhap,sl_nhap:$lot$$.sl_nhap,sl_da_xuat:$lot$$.sl_da_xuat,sl_con:$lot$$.sl_con,gia:$lot$$.gia,tien_nhap:$lot$$.tien_nhap,tien_con:$lot$$.tien_con,
10
+ ds_chung_tu_xuat:[]});for(const $t$$ of $traces$$){if(!$t$$.id_fifo_lot)continue;const $key$$=$t$$.id_fifo_lot.toString();$lotsMap$$.has($key$$)||$lotsMap$$.set($key$$,{id_fifo_lot:$t$$.id_fifo_lot,ma_vt:$t$$.ma_vt,ma_kho:$t$$.ma_kho,id_ct_nhap:$t$$.id_ct_nhap,ma_ct_nhap:$t$$.ma_ct_nhap,so_ct_nhap:$t$$.so_ct_nhap,ngay_nhap:$t$$.ngay_nhap,gia:$t$$.gia_nhap,sl_nhap:null,sl_da_xuat:null,sl_con:0,tien_nhap:null,tien_con:0,ds_chung_tu_xuat:[]});$lotsMap$$.get($key$$).ds_chung_tu_xuat.push({id_ct_xuat:$t$$.id_ct_xuat,
11
+ ma_ct_xuat:$t$$.ma_ct_xuat,so_ct_xuat:$t$$.so_ct_xuat,ngay_xuat:$t$$.ngay_xuat,detail_index:$t$$.detail_index,sl_xuat_lo:$t$$.sl_xuat_lo,tien_xuat_lo:$t$$.tien_xuat_lo})}const $result$$=Array.from($lotsMap$$.values()).sort(($a$$,$b$$)=>$a$$.ma_vt!==$b$$.ma_vt?$a$$.ma_vt.localeCompare($b$$.ma_vt):new Date($a$$.ngay_nhap)-new Date($b$$.ngay_nhap));await $result$$.asyncJoinModel2($id_app$$,dmvt,[{where:"ma_vt",fields:"ten_vt"}]);return $fn$$(null,$result$$)}catch($err$$){return $fn$$($err$$)}}
12
+ module.exports={traceLoToChungTu,traceChungTuToLo,baoCaoFifoKy};