File size: 17,062 Bytes
1e480b5
 
ec62259
 
 
 
 
 
 
 
1e480b5
 
 
 
 
 
 
 
 
ec62259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e480b5
 
ec62259
1e480b5
ec62259
 
1e480b5
 
 
 
 
 
 
 
 
 
 
 
ec62259
1e480b5
 
 
 
ec62259
1e480b5
 
 
 
ec62259
1e480b5
 
 
 
 
ec62259
1e480b5
 
ec62259
1e480b5
 
 
 
 
 
 
 
 
 
 
 
 
 
ec62259
1e480b5
ec62259
 
1e480b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec62259
 
 
 
 
 
 
1e480b5
ec62259
1e480b5
ec62259
 
1e480b5
 
ec62259
 
 
1e480b5
 
 
 
 
 
 
 
ec62259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e480b5
 
 
 
ec62259
1e480b5
ec62259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e480b5
 
 
 
ec62259
1e480b5
ec62259
1e480b5
 
 
 
 
385d733
1e480b5
 
 
 
 
 
 
 
 
 
 
 
 
ec62259
 
 
 
 
 
1e480b5
 
 
 
 
 
 
 
 
ec62259
1e480b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
const Order = require('../models/Order');
const Expense = require('../models/Expense');
const PlatformSale = require('../models/PlatformSale');
const Setting = require('../models/Setting');
const {
  getBusinessDate,
  getBusinessDayRange,
  getBusinessDateRange,
  getCurrentBusinessDayRange,
} = require('../utils/businessDate');

// ── Helpers ──────────────────────────────────────────────────

const buildOrderFilter = (from, to, extra = {}) => ({
  status: 'completed',
  createdAt: { $gte: from, $lte: to },
  ...extra,
});

// Default financial report settings
const DEFAULT_FINANCIAL_SETTINGS = {
  chefSharePercent: 30,
  directorsSharePercent: 70,
  directorCount: 3,
  directorAdjustments: [],
};

// ── Settings Endpoints ────────────────────────────────────────

// GET /api/reports/settings
exports.getSettings = async (req, res) => {
  const doc = await Setting.findOne({ key: 'financial_settings' });
  res.json(doc ? doc.value : DEFAULT_FINANCIAL_SETTINGS);
};

// PUT /api/reports/settings
exports.updateSettings = async (req, res) => {
  const updatedValue = { ...DEFAULT_FINANCIAL_SETTINGS, ...req.body };
  const doc = await Setting.findOneAndUpdate(
    { key: 'financial_settings' },
    { value: updatedValue },
    { upsert: true, new: true, setDefaultsOnInsert: true }
  );
  res.json(doc.value);
};

// ── Daily Sales Summary ───────────────────────────────────────

// GET /api/reports/daily?date=2026-07-17
exports.dailySummary = async (req, res) => {
  const { date } = req.query;
  const { from, to, businessDate } = getBusinessDayRange(date);

  const orders = await Order.find(buildOrderFilter(from, to));

  const totalSales = orders.reduce((s, o) => s + o.netTotal, 0);
  const orderCount = orders.length;
  const avgOrderValue = orderCount ? totalSales / orderCount : 0;

  const byType = orders.reduce((acc, o) => {
    acc[o.orderType] = (acc[o.orderType] || 0) + o.netTotal;
    return acc;
  }, {});

  res.json({ date: businessDate, totalSales, orderCount, avgOrderValue, byType });
};

// ── Date-Range Report ─────────────────────────────────────────

// GET /api/reports/range?from=2026-07-01&to=2026-07-31
exports.rangeReport = async (req, res) => {
  const { from, to } = req.query;
  if (!from || !to) return res.status(400).json({ message: 'from and to dates required' });

  const { from: fromDate, to: toDate } = getBusinessDateRange(from, to);

  const orders = await Order.find(buildOrderFilter(fromDate, toDate));
  const totalSales = orders.reduce((s, o) => s + o.netTotal, 0);
  const orderCount = orders.length;

  // Group by business day (orders 00:00-05:00 belong to previous day)
  const byDay = {};
  for (const o of orders) {
    const day = getBusinessDate(o.createdAt);
    if (!byDay[day]) byDay[day] = { sales: 0, count: 0 };
    byDay[day].sales += o.netTotal;
    byDay[day].count += 1;
  }

  res.json({ from, to, totalSales, orderCount, byDay });
};

// ── Top-Selling Items ─────────────────────────────────────────

// GET /api/reports/top-items?from=...&to=...&limit=10
exports.topItems = async (req, res) => {
  const { from, to, limit = 10 } = req.query;
  const matchStage = { status: 'completed' };

  if (from || to) {
    const { from: fromDate, to: toDate } = getBusinessDateRange(from, to);
    matchStage.createdAt = { $gte: fromDate, $lte: toDate };
  }

  const results = await Order.aggregate([
    { $match: matchStage },
    { $unwind: '$items' },
    {
      $group: {
        _id: { menuItem: '$items.menuItem', variantLabel: '$items.variantLabel' },
        name: { $first: '$items.menuItemName' },
        variantLabel: { $first: '$items.variantLabel' },
        totalQty: { $sum: '$items.qty' },
        totalRevenue: { $sum: { $multiply: ['$items.qty', '$items.priceAtSale'] } },
      },
    },
    { $sort: { totalQty: -1 } },
    { $limit: Number(limit) },
  ]);

  res.json(results);
};

// ── Profit & Loss ─────────────────────────────────────────────

// GET /api/reports/profit-loss?from=...&to=...
exports.profitLoss = async (req, res) => {
  const { from, to } = req.query;
  if (!from || !to) return res.status(400).json({ message: 'from and to dates required' });

  const { from: fromDate, to: toDate } = getBusinessDateRange(from, to);

  // Calendar dates for expenses
  const [fy, fm, fd] = from.split('-').map(Number);
  const expenseFrom = new Date(fy, fm - 1, fd, 0, 0, 0, 0);
  const [ty, tm, td] = to.split('-').map(Number);
  const expenseTo = new Date(ty, tm - 1, td, 23, 59, 59, 999);

  const [orders, expenses, platformSales] = await Promise.all([
    Order.find(buildOrderFilter(fromDate, toDate)),
    Expense.find({ date: { $gte: expenseFrom, $lte: expenseTo } }),
    PlatformSale.find({ date: { $gte: expenseFrom, $lte: expenseTo } }),
  ]);

  const posSales = orders.reduce((s, o) => s + o.netTotal, 0);
  const platformTotal = platformSales.reduce((s, p) => s + p.amount, 0);
  const totalSales = posSales + platformTotal;
  const totalExpenses = expenses.reduce((s, e) => s + e.amount, 0);
  const netProfit = totalSales - totalExpenses;

  const expensesByCategory = expenses.reduce((acc, e) => {
    acc[e.category] = (acc[e.category] || 0) + e.amount;
    return acc;
  }, {});

  res.json({
    from,
    to,
    posSales,
    platformSales: platformTotal,
    totalSales,
    totalExpenses,
    netProfit,
    expensesByCategory,
  });
};

// ── Monthly Accounting Sheet (Exact Spreadsheet Format) ────────

// GET /api/reports/monthly-sheet?month=2026-07
exports.monthlySheet = async (req, res) => {
  const monthParam = req.query.month || getBusinessDate().substring(0, 7); // "YYYY-MM"
  const [yearStr, monthStr] = monthParam.split('-');
  const year = Number(yearStr);
  const monthNum = Number(monthStr);

  if (isNaN(year) || isNaN(monthNum) || monthNum < 1 || monthNum > 12) {
    return res.status(400).json({ message: 'Invalid month format, expected YYYY-MM' });
  }

  // Days in month
  const totalDays = new Date(year, monthNum, 0).getDate();

  // 1. Business date range for the whole month
  // Month starts on Day 1 at 05:00 AM
  const monthStart = new Date(year, monthNum - 1, 1, 5, 0, 0, 0);
  // Month ends on Day (totalDays + 1) at 04:59:59.999 AM
  const monthEnd = new Date(year, monthNum - 1, totalDays + 1, 4, 59, 59, 999);

  // Calendar date range for expenses and platform sales
  const calendarStart = new Date(year, monthNum - 1, 1, 0, 0, 0, 0);
  const calendarEnd = new Date(year, monthNum, 0, 23, 59, 59, 999);

  // 2. Fetch all data in parallel
  const [orders, expenses, platformSales, settingsDoc] = await Promise.all([
    Order.find(buildOrderFilter(monthStart, monthEnd)),
    Expense.find({ date: { $gte: calendarStart, $lte: calendarEnd } }).populate('createdBy', 'name'),
    PlatformSale.find({ date: { $gte: calendarStart, $lte: calendarEnd } }).populate('createdBy', 'name'),
    Setting.findOne({ key: 'financial_settings' }),
  ]);

  const settings = settingsDoc ? settingsDoc.value : DEFAULT_FINANCIAL_SETTINGS;

  // 3. Aggregate daily POS sales by business date
  const dailySalesMap = {};
  for (const o of orders) {
    const bDate = getBusinessDate(o.createdAt);
    dailySalesMap[bDate] = (dailySalesMap[bDate] || 0) + o.netTotal;
  }

  // 4. Aggregate daily expenses (excluding salaries) and separate salary expenses
  const dailyExpensesMap = {};
  const salaryExpenses = [];

  for (const e of expenses) {
    if (e.category === 'Salary') {
      salaryExpenses.push({
        _id: e._id,
        title: e.title,
        amount: e.amount,
        date: e.date,
        note: e.note,
        addedBy: e.createdBy?.name || '',
      });
    } else {
      const dayNum = new Date(e.date).getDate();
      const bDate = `${year}-${String(monthNum).padStart(2, '0')}-${String(dayNum).padStart(2, '0')}`;
      dailyExpensesMap[bDate] = (dailyExpensesMap[bDate] || 0) + e.amount;
    }
  }

  // 5. Build daily rows with running cumulative totals
  let runningSale = 0;
  let runningProfit = 0;

  const rows = [];
  for (let d = 1; d <= totalDays; d++) {
    const dayPadded = String(d).padStart(2, '0');
    const monthPadded = String(monthNum).padStart(2, '0');
    const dateKey = `${year}-${monthPadded}-${dayPadded}`;
    const displayDate = `${dayPadded}/${monthPadded}`;

    const exp = dailyExpensesMap[dateKey] || 0;
    const sale = dailySalesMap[dateKey] || 0;
    const profitLoss = sale - exp;

    runningSale += sale;
    runningProfit += profitLoss;

    rows.push({
      day: d,
      date: displayDate,
      fullDate: dateKey,
      expenses: exp,
      sale,
      profitLoss,
      totalSale: runningSale,
      totalProfit: runningProfit,
    });
  }

  // 6. Platform Sales (e.g. Foodpanda)
  const foodpandaTotal = platformSales.reduce((s, p) => s + p.amount, 0);

  // 7. Subtotal (S. Total)
  const totalDailyExpenses = rows.reduce((s, r) => s + r.expenses, 0);
  const totalDailySales = rows.reduce((s, r) => s + r.sale, 0);
  const subtotalSale = totalDailySales + foodpandaTotal;
  const subtotalExpenses = totalDailyExpenses;
  const subtotalProfit = subtotalSale - subtotalExpenses;

  // 8. Salaries Total
  const salariesTotal = salaryExpenses.reduce((s, e) => s + e.amount, 0);

  // 9. Grand Total (G. Total)
  const grandTotalExpenses = subtotalExpenses + salariesTotal;
  const grandTotalSale = subtotalSale;
  const netProfit = grandTotalSale - grandTotalExpenses;

  // 10. Profit Sharing Calculations
  const chefSharePercent = Number(settings.chefSharePercent) || 30;
  const directorsSharePercent = Number(settings.directorsSharePercent) || 70;
  const directorCount = Number(settings.directorCount) || 3;

  const chefShare = Math.round((netProfit * chefSharePercent) / 100);
  const directorsShare = netProfit - chefShare;
  const eachDirectorShare = directorCount > 0 ? Math.round(directorsShare / directorCount) : 0;

  res.json({
    month: monthParam,
    year,
    monthNum,
    monthName: new Date(year, monthNum - 1, 1).toLocaleString('en-US', { month: 'long', year: 'numeric' }),
    rows,
    platformSales: {
      platform: 'Foodpanda',
      entries: platformSales,
      total: foodpandaTotal,
    },
    subtotal: {
      expenses: subtotalExpenses,
      sale: subtotalSale,
      profit: subtotalProfit,
    },
    salaries: {
      entries: salaryExpenses,
      total: salariesTotal,
    },
    grandTotal: {
      expenses: grandTotalExpenses,
      sale: grandTotalSale,
      netProfit,
    },
    profitDistribution: {
      chefSharePercent,
      directorsSharePercent,
      directorCount,
      chefShare,
      directorsShare,
      eachDirectorShare,
      directorAdjustments: settings.directorAdjustments || [],
    },
  });
};

// ── CSV Export ────────────────────────────────────────────────

// GET /api/reports/export/csv?type=orders|expenses|monthly-sheet&from=...&to=...&month=...
exports.exportCSV = async (req, res) => {
  const { type = 'orders', from, to, month } = req.query;

  if (type === 'monthly-sheet') {
    const monthParam = month || getBusinessDate().substring(0, 7);
    const [yStr, mStr] = monthParam.split('-');
    const year = Number(yStr);
    const monthNum = Number(mStr);
    const totalDays = new Date(year, monthNum, 0).getDate();

    const monthStart = new Date(year, monthNum - 1, 1, 5, 0, 0, 0);
    const monthEnd = new Date(year, monthNum - 1, totalDays + 1, 4, 59, 59, 999);
    const calendarStart = new Date(year, monthNum - 1, 1, 0, 0, 0, 0);
    const calendarEnd = new Date(year, monthNum, 0, 23, 59, 59, 999);

    const [orders, expenses, platformSales, settingsDoc] = await Promise.all([
      Order.find(buildOrderFilter(monthStart, monthEnd)),
      Expense.find({ date: { $gte: calendarStart, $lte: calendarEnd } }),
      PlatformSale.find({ date: { $gte: calendarStart, $lte: calendarEnd } }),
      Setting.findOne({ key: 'financial_settings' }),
    ]);

    const settings = settingsDoc ? settingsDoc.value : DEFAULT_FINANCIAL_SETTINGS;
    const dailySalesMap = {};
    for (const o of orders) {
      const bDate = getBusinessDate(o.createdAt);
      dailySalesMap[bDate] = (dailySalesMap[bDate] || 0) + o.netTotal;
    }

    const dailyExpensesMap = {};
    let salariesTotal = 0;
    for (const e of expenses) {
      if (e.category === 'Salary') {
        salariesTotal += e.amount;
      } else {
        const dayNum = new Date(e.date).getDate();
        const bDate = `${year}-${String(monthNum).padStart(2, '0')}-${String(dayNum).padStart(2, '0')}`;
        dailyExpensesMap[bDate] = (dailyExpensesMap[bDate] || 0) + e.amount;
      }
    }

    const fpTotal = platformSales.reduce((s, p) => s + p.amount, 0);

    const rows = [
      [`Monthly Report - ${monthParam}`],
      ['Date', 'Expenses', 'Sale', 'Profit/Loss', 'Total Sale', 'Total Profit'],
    ];

    let rSale = 0;
    let rProf = 0;
    let totExp = 0;
    let totSale = 0;

    for (let d = 1; d <= totalDays; d++) {
      const dayP = String(d).padStart(2, '0');
      const monP = String(monthNum).padStart(2, '0');
      const dKey = `${year}-${monP}-${dayP}`;
      const exp = dailyExpensesMap[dKey] || 0;
      const sale = dailySalesMap[dKey] || 0;
      const pl = sale - exp;
      rSale += sale;
      rProf += pl;
      totExp += exp;
      totSale += sale;
      rows.push([`${dayP}/${monP}`, exp, sale, pl, rSale, rProf]);
    }

    // Platform Sales
    rows.push(['Foodpanda', '', fpTotal, fpTotal, totSale + fpTotal, rProf + fpTotal]);
    // Subtotal
    rows.push(['S. Total', totExp, totSale + fpTotal, (totSale + fpTotal) - totExp, '', '']);
    // Salaries
    rows.push(['Salaries', salariesTotal, '', '', '', '']);
    // Grand Total
    const gNet = (totSale + fpTotal) - (totExp + salariesTotal);
    rows.push(['G. Total', totExp + salariesTotal, totSale + fpTotal, gNet, '', '']);

    const chefShare = Math.round((gNet * (settings.chefSharePercent || 30)) / 100);
    const dirShare = gNet - chefShare;
    const eachDir = Math.round(dirShare / (settings.directorCount || 3));

    rows.push([]);
    rows.push(['Share of Chef (30%)', chefShare]);
    rows.push(['Directors Share (70%)', dirShare]);
    rows.push(['Each Director Share', eachDir]);

    res.setHeader('Content-Type', 'text/csv');
    res.setHeader('Content-Disposition', `attachment; filename="monthly_sheet_${monthParam}.csv"`);
    return res.send(rows.map((r) => r.join(',')).join('\n'));
  }

  const { from: fromDate, to: toDate } = getBusinessDateRange(from, to);

  if (type === 'orders') {
    const orders = await Order.find(buildOrderFilter(fromDate, toDate)).populate('cashier', 'name');
    const rows = [
      ['Business Date', 'Created At', 'Order Type', 'Items', 'Subtotal', 'Discount', 'Delivery Fee', 'Net Total', 'Cash Received', 'Change', 'Status', 'Cashier'],
      ...orders.map((o) => [
        getBusinessDate(o.createdAt),
        o.createdAt.toISOString(),
        o.orderType,
        o.items.map((i) => `${i.menuItemName} (${i.variantLabel}) x${i.qty}`).join(' | '),
        o.subtotal,
        o.discount.amount,
        o.deliveryFee || 0,
        o.netTotal,
        o.cashReceived,
        o.change,
        o.status,
        o.cashier?.name || '',
      ]),
    ];
    res.setHeader('Content-Type', 'text/csv');
    res.setHeader('Content-Disposition', `attachment; filename="orders_${from}_${to}.csv"`);
    return res.send(rows.map((r) => r.join(',')).join('\n'));
  }

  if (type === 'expenses') {
    const [fy, fm, fd] = from ? from.split('-').map(Number) : [1970, 1, 1];
    const [ty, tm, td] = to ? to.split('-').map(Number) : [new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()];
    const expStart = new Date(fy, fm - 1, fd, 0, 0, 0, 0);
    const expEnd = new Date(ty, tm - 1, td, 23, 59, 59, 999);

    const expenses = await Expense.find({ date: { $gte: expStart, $lte: expEnd } }).populate('createdBy', 'name');
    const rows = [
      ['Date', 'Title', 'Category', 'Amount', 'Note', 'Created By'],
      ...expenses.map((e) => [e.date.toISOString().split('T')[0], e.title, e.category, e.amount, e.note, e.createdBy?.name || '']),
    ];
    res.setHeader('Content-Type', 'text/csv');
    res.setHeader('Content-Disposition', `attachment; filename="expenses_${from}_${to}.csv"`);
    return res.send(rows.map((r) => r.join(',')).join('\n'));
  }

  res.status(400).json({ message: 'type must be orders, expenses, or monthly-sheet' });
};