-
This commit is contained in:
@@ -2,7 +2,8 @@ import { AccountingSumService } from '../services/accountingSumService.js'
|
|||||||
import { sendError } from '../utils/response.js'
|
import { sendError } from '../utils/response.js'
|
||||||
import { GeneralService } from '../share/generalservice.js';
|
import { GeneralService } from '../share/generalservice.js';
|
||||||
import { trim_all_array } from '../utils/trim.js'
|
import { trim_all_array } from '../utils/trim.js'
|
||||||
import { verifyToken, generateToken } from '../utils/token.js'
|
import { verifyToken,
|
||||||
|
generateToken } from '../utils/token.js'
|
||||||
|
|
||||||
export class accountingSum {
|
export class accountingSum {
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ export class accountingSum {
|
|||||||
if (!result) return sendError('ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง', 'Invalid credentials');
|
if (!result) return sendError('ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง', 'Invalid credentials');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// ✅ 1) เตรียม data สำหรับใช้คำนวณ
|
// 1) เตรียม data สำหรับใช้คำนวณ
|
||||||
// ถ้า service คืนมาเป็น { code, message, data: [...] }
|
// ถ้า service คืนมาเป็น { code, message, data: [...] }
|
||||||
const data = Array.isArray(result)
|
const data = Array.isArray(result)
|
||||||
? result
|
? result
|
||||||
@@ -57,7 +58,7 @@ export class accountingSum {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ 2) แยก income / expense
|
// 2) แยก income / expense
|
||||||
const incomeList = data.filter(i => i.acttyp === 'i');
|
const incomeList = data.filter(i => i.acttyp === 'i');
|
||||||
const expenseList = data.filter(e => e.acttyp === 'e');
|
const expenseList = data.filter(e => e.acttyp === 'e');
|
||||||
|
|
||||||
@@ -68,7 +69,7 @@ export class accountingSum {
|
|||||||
const profitRate = totalIncome > 0 ? (netProfit / totalIncome) * 100 : 0;
|
const profitRate = totalIncome > 0 ? (netProfit / totalIncome) * 100 : 0;
|
||||||
const adjustedProfitRate = profitRate + 1.9;
|
const adjustedProfitRate = profitRate + 1.9;
|
||||||
|
|
||||||
// ✅ 3) แนบ summary (เหมือนที่เราทำไปก่อนหน้า)
|
// 3) แนบ summary (เหมือนที่เราทำไปก่อนหน้า)
|
||||||
var summary = {
|
var summary = {
|
||||||
totalIncome: totalIncome.toFixed(2),
|
totalIncome: totalIncome.toFixed(2),
|
||||||
totalExpense: totalExpense.toFixed(2),
|
totalExpense: totalExpense.toFixed(2),
|
||||||
@@ -78,14 +79,14 @@ export class accountingSum {
|
|||||||
period: '30 วัน'
|
period: '30 วัน'
|
||||||
};
|
};
|
||||||
|
|
||||||
// ✅ 4) ดึงสีจาก dtlmst (แนะนำให้เรียกจาก service เพิ่ม)
|
// 4) ดึงสีจาก dtlmst (แนะนำให้เรียกจาก service เพิ่ม)
|
||||||
// ตัวอย่างสมมติ: คุณไป query มาจาก service ก่อนหน้าแล้วได้เป็น object แบบนี้
|
// ตัวอย่างสมมติ: คุณไป query มาจาก service ก่อนหน้าแล้วได้เป็น object แบบนี้
|
||||||
// key = ชื่อหมวด (actcatnam หรือ code), value = color
|
// key = ชื่อหมวด (actcatnam หรือ code), value = color
|
||||||
const categoryColorMap = await this.accountingSumService.getCategoryColorMap(database);
|
const categoryColorMap = await this.accountingSumService.getCategoryColorMap(database);
|
||||||
// ตัวอย่างที่คาดหวังจาก service:
|
// ตัวอย่างที่คาดหวังจาก service:
|
||||||
// { 'ค่าอาหาร': '#FF6384', 'ค่าเดินทาง': '#36A2EB', 'ขายสินค้า': '#4BC0C0', ... }
|
// { 'ค่าอาหาร': '#FF6384', 'ค่าเดินทาง': '#36A2EB', 'ขายสินค้า': '#4BC0C0', ... }
|
||||||
|
|
||||||
// ✅ 5) สรุปยอดตามหมวด แล้วคำนวณ % สำหรับ expense
|
// 5) สรุปยอดตามหมวด แล้วคำนวณ % สำหรับ expense
|
||||||
const expenseAgg = {};
|
const expenseAgg = {};
|
||||||
expenseList.forEach(row => {
|
expenseList.forEach(row => {
|
||||||
const key = row.actcatnam; // หรือใช้รหัส category ถ้ามี เช่น row.actcatcod
|
const key = row.actcatnam; // หรือใช้รหัส category ถ้ามี เช่น row.actcatcod
|
||||||
@@ -122,7 +123,7 @@ export class accountingSum {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// ✅ 6) แนบข้อมูล pie chart เข้า result
|
// 6) แนบข้อมูล pie chart เข้า result
|
||||||
var pie = {
|
var pie = {
|
||||||
expense: expensePie,
|
expense: expensePie,
|
||||||
income: incomePie
|
income: incomePie
|
||||||
|
|||||||
Reference in New Issue
Block a user