File size: 2,944 Bytes
0b68098
0159174
0b68098
 
 
 
0159174
0b68098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0159174
0b68098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0159174
0b68098
 
 
 
 
0159174
0b68098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0159174
0b68098
 
 
0159174
0b68098
 
 
 
 
 
 
 
 
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
import Income from "../models/IncomeModel.js";
import { resolveBranchForWrite, resolveScopedBranch } from "../utils/branchContext.js";

export const addIncome = async (req, res) => {
   try {
      const tenantId = req.user.tenant._id;
      const branch = await resolveBranchForWrite(req);
      const title = req.body.title?.trim();
      const amount = Number(req.body.amount);
      const category = req.body.category?.trim() || "general";
      const paymentMethod = req.body.paymentMethod?.trim() || "cash";
      const notes = req.body.notes?.trim() || "";
      const date = req.body.date ? new Date(req.body.date) : new Date();

      if (!title) {
         return res.status(400).json({ message: "مطلوب عنوان الدخل" });
      }
      if (!amount || amount <= 0) {
         return res.status(400).json({ message: "يجب أن يكون مبلغ الدخل أكبر من الصفر" });
      }

      const income = await Income.create({
         tenant: tenantId,
         branch: branch._id,
         title,
         category,
         amount,
         paymentMethod,
         notes,
         date,
         createdBy: req.user._id,
      });

      res.status(201).json(income);
   } catch (error) {
      res.status(500).json({ message: error.message });
   }
};

export const getIncomes = async (req, res) => {
   try {
      const tenantId = req.user.tenant._id;
      const { branchFilter } = await resolveScopedBranch(req);
      const page = parseInt(req.query.page) || 1;
      const limit = parseInt(req.query.limit) || 5;
      const skip = (page - 1) * limit;
      const search = req.query.search || "";

      const query = { tenant: tenantId, ...branchFilter };
      if (search) {
         query.title = { $regex: search, $options: "i" };
      }

      const [items, total] = await Promise.all([
         Income.find(query).sort({ date: -1 }).skip(skip).limit(limit),
         Income.countDocuments(query),
      ]);

      res.status(200).json({
         data: items,
         pagination: {
            total,
            page,
            limit,
            totalPages: Math.ceil(total / limit),
         },
      });
   } catch (error) {
      res.status(500).json({ message: error.message });
   }
};

export const deleteIncome = async (req, res) => {
   try {
      const incomeId = req.params.id;
      const { branchFilter } = await resolveScopedBranch(req);
      const income = await Income.findOneAndDelete({
         _id: incomeId,
         tenant: req.user.tenant._id,
         ...branchFilter,
      });
      if (!income) {
         return res.status(404).json({ message: "سجل الإيراد غير موجود" });
      }
      res.status(200).json({ message: "تم حذف سجل الإيراد بنجاح" });
   } catch (error) {
      res.status(500).json({ message: error.message });
   }
};