File size: 1,868 Bytes
222dbfc
 
2c97468
e66f26b
 
55b9836
222dbfc
 
 
e66f26b
c629ca7
e66f26b
 
 
 
 
 
222dbfc
e66f26b
 
 
d747060
 
222dbfc
e66f26b
222dbfc
0212fa2
 
c2bc2d7
b077308
 
e66f26b
55b9836
 
 
 
c2bc2d7
e66f26b
 
 
 
 
 
0212fa2
 
e66f26b
 
 
 
 
 
d747060
 
e66f26b
 
 
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
import { Request, Response } from 'express';

import { logger } from '../../utils/logger';
import { syncVendorsFromJsonService } from '../../shared/services/vendors.service';
import PWVendors from '../../models/pwVendors';
import PWGlaccounts from '../../models/pwGlaccounts';

export const fetchVendorsData = async (req: Request, res: Response) => {
    try {
        const vendors = await PWVendors.findAll({
          attributes: [['pw_id' , 'id'], 'name'],
          order: [['name', 'ASC']],
        });
    
        const vendorsResponseData = vendors.map((vendor) => ({
          id: vendor.id,
          name: vendor.name,
        }));
    
        res.status(200).json(vendorsResponseData);
      } catch (error) {
        logger.error('Error fetching Vendors');
        logger.error(error);
        res.status(500).json({ error: 'Error fetching Vendors' });
      }
};

export const isVendorHasDefaultBillsplitAccount = async (vendorId: number) => {
    try {
        const vendor = await PWVendors.findOne({ where: { pw_id: vendorId }, attributes: ['default_bill_split'] });

        if (vendor && vendor.default_bill_split) {
          const accNumber = vendor.default_bill_split.toString();
          const GLaccount = await PWGlaccounts.findOne({ where: { account_number: accNumber } });
          if (GLaccount)
          return GLaccount.pw_id;
        }
    
        return null;
      } catch (error) {
        logger.error('Error checking default bill split account:', error);
        return null;
      }

};

export const syncVendors = async (req: Request, res: Response) => {
    try {
        const result = await syncVendorsFromJsonService();
        res.status(200).send(result);
    } catch (error) {
        logger.error('Error syncing Vendors');
        logger.error(error);
        res.status(500).send((error as Error).message);
    }
};