@tomei/finance 0.3.85 → 0.3.87
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.
- package/dist/account/account.js +3 -1
- package/dist/account/account.js.map +1 -1
- package/dist/finance-company/finance-company.js +7 -3
- package/dist/finance-company/finance-company.js.map +1 -1
- package/dist/ledger-transaction/ledger-transaction.js +4 -4
- package/dist/ledger-transaction/ledger-transaction.js.map +1 -1
- package/dist/payment/payment.d.ts +1 -0
- package/dist/payment/payment.js +43 -27
- package/dist/payment/payment.js.map +1 -1
- package/dist/payment-method-type/payment-method-type.d.ts +1 -0
- package/dist/payment-method-type/payment-method-type.js +34 -18
- package/dist/payment-method-type/payment-method-type.js.map +1 -1
- package/dist/payment-paid-with/payment-paid-with.d.ts +2 -1
- package/dist/payment-paid-with/payment-paid-with.js +4 -1
- package/dist/payment-paid-with/payment-paid-with.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/account/account.ts +3 -1
- package/src/finance-company/finance-company.ts +1519 -1510
- package/src/ledger-transaction/ledger-transaction.ts +4 -4
- package/src/payment/payment.ts +39 -47
- package/src/payment-method-type/payment-method-type.ts +32 -21
- package/src/payment-paid-with/payment-paid-with.ts +9 -1
|
@@ -1,1510 +1,1519 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import * as cuid from 'cuid';
|
|
3
|
-
import {
|
|
4
|
-
HashTable,
|
|
5
|
-
LoginUserBase,
|
|
6
|
-
ObjectBase,
|
|
7
|
-
RecordNotFoundError,
|
|
8
|
-
} from '@tomei/general';
|
|
9
|
-
import Account from '../account/account';
|
|
10
|
-
import JournalEntry from '../journal-entry/journal-entry';
|
|
11
|
-
import FinanceCustomerBase from '../customer/customer';
|
|
12
|
-
import Document from '../document/document';
|
|
13
|
-
import { IAccountSystem } from '../interfaces';
|
|
14
|
-
import { FinanceCompanyRepository } from './finance-company.repository';
|
|
15
|
-
import { FinanceCustomerRepository } from '../customer/finance-customer.repository';
|
|
16
|
-
import { LedgerTransactionRepository } from '../ledger-transaction/ledger-transaction.repository';
|
|
17
|
-
import {
|
|
18
|
-
DocType,
|
|
19
|
-
PaymentStatus,
|
|
20
|
-
PaymentType,
|
|
21
|
-
TransactionTypeOptions,
|
|
22
|
-
} from '../enum';
|
|
23
|
-
import PaymentMethodType from '../payment-method-type/payment-method-type';
|
|
24
|
-
import { PaymentRepository } from '../payment/payment.repository';
|
|
25
|
-
import { PaymentItemRepository } from '../payment-item/payment-item.repository';
|
|
26
|
-
import Payment from '../payment/payment';
|
|
27
|
-
import { DocumentRepository } from '../document/document.repository';
|
|
28
|
-
import { DocumentItemRepository } from '../document/document-item.repository';
|
|
29
|
-
import { PaymentMethodRepository } from '../payment-method/payment-method.repository';
|
|
30
|
-
import { PaymentMethodTypeRepository } from '../payment-method-type/payment-method-type.repository';
|
|
31
|
-
import PaymentMethod from '../payment-method/payment-method';
|
|
32
|
-
import { AccountRepository } from '../account/account.repository';
|
|
33
|
-
import { PaymentPaidWithRepository } from '../payment-paid-with/payment-paid-with.repository';
|
|
34
|
-
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
36
|
-
const getConfig = require('../config');
|
|
37
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
38
|
-
const config = getConfig();
|
|
39
|
-
|
|
40
|
-
export default class FinanceCompany extends ObjectBase {
|
|
41
|
-
private _CompanyId = 'New';
|
|
42
|
-
private _CompSystemCode = '';
|
|
43
|
-
private _CompSystemRefId = '';
|
|
44
|
-
private _AccSystemCode = '';
|
|
45
|
-
private static _htFinanceCompanyIds = new HashTable();
|
|
46
|
-
private static _htFinanceCompanies = new HashTable();
|
|
47
|
-
private static _financeCompanyRepository = new FinanceCompanyRepository();
|
|
48
|
-
private static _PaymentRepository = new PaymentRepository();
|
|
49
|
-
private static _PaymentItemRepository = new PaymentItemRepository();
|
|
50
|
-
private static _PaymentPaidWithRepository = new PaymentPaidWithRepository();
|
|
51
|
-
private static _PaymentMethodRepository = new PaymentMethodRepository();
|
|
52
|
-
private static _PaymentMethodTypeRepository =
|
|
53
|
-
new PaymentMethodTypeRepository();
|
|
54
|
-
private static _DocumentRepository = new DocumentRepository();
|
|
55
|
-
private static _DocumentItemRepository = new DocumentItemRepository();
|
|
56
|
-
private static _FinanceCustomerRepository = new FinanceCustomerRepository();
|
|
57
|
-
private static _LedgerTransactionRepository =
|
|
58
|
-
new LedgerTransactionRepository();
|
|
59
|
-
|
|
60
|
-
private static _AccountRepository = new AccountRepository();
|
|
61
|
-
|
|
62
|
-
private _AccountingSystem: IAccountSystem;
|
|
63
|
-
|
|
64
|
-
private _DbTransaction: any;
|
|
65
|
-
private _PaymentMethods = [];
|
|
66
|
-
|
|
67
|
-
get CompSystemCode(): string {
|
|
68
|
-
return this._CompSystemCode;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private set CompSystemCode(code: string) {
|
|
72
|
-
this._CompSystemCode = code;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
get CompSystemRefId() {
|
|
76
|
-
return this._CompSystemRefId;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
set CompSystemRefId(id: string) {
|
|
80
|
-
this._CompSystemRefId = id;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
get AccSystemCode() {
|
|
84
|
-
return this._AccSystemCode;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private set AccSystemCode(code: string) {
|
|
88
|
-
this._AccSystemCode = code;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
get CompanyId() {
|
|
92
|
-
return this._CompanyId;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
get ObjectId() {
|
|
96
|
-
return this._CompanyId;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private set ObjectId(id: string) {
|
|
100
|
-
this._CompanyId = id;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
get ObjectName() {
|
|
104
|
-
return `${this.CompSystemCode}-${this.CompSystemRefId}-${this.AccSystemCode}`;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
get TableName() {
|
|
108
|
-
return 'finance_Company';
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
get AccountingSystem(): IAccountSystem {
|
|
112
|
-
return this._AccountingSystem;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
set AccountingSystem(system: IAccountSystem) {
|
|
116
|
-
this._AccountingSystem = system;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
constructor(
|
|
120
|
-
compSystemCode: string,
|
|
121
|
-
compSystemRefId: string,
|
|
122
|
-
accSystemCode: string,
|
|
123
|
-
) {
|
|
124
|
-
super();
|
|
125
|
-
this.CompSystemCode = compSystemCode;
|
|
126
|
-
this.CompSystemRefId = compSystemRefId;
|
|
127
|
-
this.AccSystemCode = accSystemCode;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
static async getFinanceCompanyId(
|
|
131
|
-
compSystemCode: string,
|
|
132
|
-
compSystemRefId: string,
|
|
133
|
-
accSystemCode: string,
|
|
134
|
-
): Promise<string> {
|
|
135
|
-
let sCompanyId = '';
|
|
136
|
-
/*Assemble the hashtable key*/
|
|
137
|
-
const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
|
|
138
|
-
/*Check if the FinanceCompany has previously being loaded*/
|
|
139
|
-
if (!FinanceCompany._htFinanceCompanyIds.get(sKey)) {
|
|
140
|
-
/*Instantiate a new FinanceCompany*/
|
|
141
|
-
const financeCompany = new FinanceCompany(
|
|
142
|
-
compSystemCode,
|
|
143
|
-
compSystemRefId,
|
|
144
|
-
accSystemCode,
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
/*Retrieve the finance company from finance_Company table using compSystemCode,
|
|
148
|
-
* CompSystemRefId and accSystemCode */
|
|
149
|
-
const company = await FinanceCompany._financeCompanyRepository.findOne({
|
|
150
|
-
where: {
|
|
151
|
-
CompSystemCode: compSystemCode,
|
|
152
|
-
CompSystemRefId: compSystemRefId,
|
|
153
|
-
AccSystemCode: accSystemCode,
|
|
154
|
-
},
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
/*Retrieve and store the companyId from the result*/
|
|
158
|
-
financeCompany.ObjectId = company.CompanyId;
|
|
159
|
-
sCompanyId = financeCompany.ObjectId;
|
|
160
|
-
|
|
161
|
-
/*Add the details into the hashtable*/
|
|
162
|
-
FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
|
|
163
|
-
FinanceCompany._htFinanceCompanies.add(sCompanyId, financeCompany);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (typeof FinanceCompany._htFinanceCompanyIds.get(sKey) === 'string') {
|
|
167
|
-
sCompanyId = FinanceCompany._htFinanceCompanyIds.get(sKey);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return sCompanyId;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
static async getFinanceCompany(companyId: string): Promise<FinanceCompany> {
|
|
174
|
-
/*Check if the finance company is previously be loaded*/
|
|
175
|
-
if (!FinanceCompany._htFinanceCompanies.get(companyId)) {
|
|
176
|
-
/*Retrieve the finance company from finance_Company table using compSystemCode,
|
|
177
|
-
* CompSystemRefId and accSystemCode */
|
|
178
|
-
const company = await FinanceCompany._financeCompanyRepository.findOne({
|
|
179
|
-
where: { CompanyId: companyId },
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
if (!company) {
|
|
183
|
-
throw Error('No finance company found. Please create first.');
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/*Using the result returned, instantiate a new FinanceCompany*/
|
|
187
|
-
const compSystemCode = company.CompSystemCode;
|
|
188
|
-
const compSystemRefId = company.CompSystemRefId;
|
|
189
|
-
const accSystemCode = company.AccSystemCode;
|
|
190
|
-
|
|
191
|
-
const financeCompany = new FinanceCompany(
|
|
192
|
-
compSystemCode,
|
|
193
|
-
compSystemRefId,
|
|
194
|
-
accSystemCode,
|
|
195
|
-
);
|
|
196
|
-
|
|
197
|
-
/*Retrieve and store the CompanyId from the result*/
|
|
198
|
-
financeCompany.ObjectId = company.CompanyId;
|
|
199
|
-
financeCompany._CompanyId = company.CompanyId;
|
|
200
|
-
|
|
201
|
-
/*Add the details into the hashtable*/
|
|
202
|
-
const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
|
|
203
|
-
FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
|
|
204
|
-
FinanceCompany._htFinanceCompanies.add(
|
|
205
|
-
financeCompany.ObjectId,
|
|
206
|
-
financeCompany,
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
// tslint:disable-next-line:no-console
|
|
210
|
-
console.log('return from hash table');
|
|
211
|
-
return FinanceCompany._htFinanceCompanies.get(companyId);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
static async createFinanceCompany(
|
|
215
|
-
dbTransaction: any,
|
|
216
|
-
loginUser: LoginUserBase,
|
|
217
|
-
companyId: string,
|
|
218
|
-
compSystemCode: string,
|
|
219
|
-
compSystemRefId: string,
|
|
220
|
-
accSystemCode: string,
|
|
221
|
-
) {
|
|
222
|
-
/*Instantiate a new FinanceCompany*/
|
|
223
|
-
const financeCompany = new FinanceCompany(
|
|
224
|
-
compSystemCode,
|
|
225
|
-
compSystemRefId,
|
|
226
|
-
accSystemCode,
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
/*Validating if the finance company already exists in finance_Company*/
|
|
230
|
-
const company = await FinanceCompany._financeCompanyRepository.findOne({
|
|
231
|
-
where: {
|
|
232
|
-
CompSystemCode: compSystemCode,
|
|
233
|
-
CompSystemRefId: compSystemRefId,
|
|
234
|
-
AccSystemCode: accSystemCode,
|
|
235
|
-
},
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
if (company) {
|
|
239
|
-
throw Error(
|
|
240
|
-
'There is already another Finance Company with the compSystemCode, CompSystemRefId and accSystemCode specified.',
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/*Generating the companyId*/
|
|
245
|
-
financeCompany.ObjectId = companyId;
|
|
246
|
-
|
|
247
|
-
/*Save the FinanceCompany to the finance_Company table*/
|
|
248
|
-
await FinanceCompany._financeCompanyRepository.create(
|
|
249
|
-
{
|
|
250
|
-
CompanyId: financeCompany.CompanyId,
|
|
251
|
-
CompSystemCode: financeCompany.CompSystemCode,
|
|
252
|
-
CompSystemRefId: financeCompany.CompSystemRefId,
|
|
253
|
-
AccSystemCode: financeCompany.AccSystemCode,
|
|
254
|
-
},
|
|
255
|
-
{
|
|
256
|
-
transaction: dbTransaction,
|
|
257
|
-
},
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
/*Add the details to hashtable*/
|
|
261
|
-
const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
|
|
262
|
-
FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
|
|
263
|
-
FinanceCompany._htFinanceCompanies.add(
|
|
264
|
-
financeCompany.ObjectId,
|
|
265
|
-
financeCompany,
|
|
266
|
-
);
|
|
267
|
-
|
|
268
|
-
// tslint:disable-next-line:no-console
|
|
269
|
-
console.log('return from hash table');
|
|
270
|
-
return FinanceCompany._htFinanceCompanies.get(companyId);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
async createCustomer(
|
|
274
|
-
dbTransaction: any,
|
|
275
|
-
custSystemCode: string,
|
|
276
|
-
custSystemRefId: string,
|
|
277
|
-
customer: FinanceCustomerBase,
|
|
278
|
-
) {
|
|
279
|
-
try {
|
|
280
|
-
if (!custSystemCode || !custSystemRefId) {
|
|
281
|
-
throw new Error(
|
|
282
|
-
'CustSystemCode and CustomerRefId are required fields.',
|
|
283
|
-
);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
const financeCustomerData =
|
|
287
|
-
await FinanceCompany._FinanceCustomerRepository.findOne({
|
|
288
|
-
where: {
|
|
289
|
-
CompanyId: this._CompanyId,
|
|
290
|
-
CustSystemCode: custSystemCode,
|
|
291
|
-
CustSystemRefId: custSystemRefId,
|
|
292
|
-
},
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
if (financeCustomerData) {
|
|
296
|
-
throw new Error('Customer already created previously.');
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// Retrieve the type of accounting system, API Key, and API secret based on SysCode from the Config.ts
|
|
300
|
-
const customerId = await this.AccountingSystem.createCustomer({
|
|
301
|
-
customer,
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
customer.CompanyId = this._CompanyId;
|
|
305
|
-
|
|
306
|
-
const newCustomer = await customer.save(
|
|
307
|
-
customerId,
|
|
308
|
-
custSystemCode,
|
|
309
|
-
custSystemRefId,
|
|
310
|
-
dbTransaction,
|
|
311
|
-
);
|
|
312
|
-
|
|
313
|
-
const payload = {
|
|
314
|
-
Action: 'Create',
|
|
315
|
-
Activity: 'Finance Customer Created',
|
|
316
|
-
Description: `Customer (ID: ${newCustomer.CustomerId}) has been created for ${this.AccSystemCode}`,
|
|
317
|
-
EntityType: 'finance_Customer',
|
|
318
|
-
EntityValueBefore: JSON.stringify({}),
|
|
319
|
-
EntityValueAfter: JSON.stringify(newCustomer),
|
|
320
|
-
PerformedById: 'test',
|
|
321
|
-
PerformedAt: new Date(),
|
|
322
|
-
EntityId: newCustomer.CustomerId,
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
await axios.post(
|
|
326
|
-
`${process.env.COMMON_API_URL}/activity-histories`,
|
|
327
|
-
payload,
|
|
328
|
-
);
|
|
329
|
-
|
|
330
|
-
return customer;
|
|
331
|
-
} catch (error) {
|
|
332
|
-
throw error;
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
async postJournal(dbTransaction: any, journalEntry: JournalEntry) {
|
|
337
|
-
const debitTransactions = await journalEntry.DebitTransactions;
|
|
338
|
-
const creditTransactions = await journalEntry.CreditTransactions;
|
|
339
|
-
try {
|
|
340
|
-
if (creditTransactions.length < 1 || debitTransactions?.length < 1) {
|
|
341
|
-
throw new Error(
|
|
342
|
-
'There should be at least 1 debit ledger transaction and 1 credit ledger transaction in the journal entry',
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const totalCreditAmount = creditTransactions.reduce(
|
|
347
|
-
(accumulator, currentValue) => accumulator + currentValue.CreditAmount,
|
|
348
|
-
0,
|
|
349
|
-
);
|
|
350
|
-
const totalDebitAmount = debitTransactions.reduce(
|
|
351
|
-
(accumulator, currentValue) => accumulator + currentValue.DebitAmount,
|
|
352
|
-
0,
|
|
353
|
-
);
|
|
354
|
-
|
|
355
|
-
if (totalCreditAmount !== totalDebitAmount) {
|
|
356
|
-
throw new Error(
|
|
357
|
-
'Credit ledger transaction and debit ledger transaction should the same amount',
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
const newJournalEntry = await journalEntry.save('test', dbTransaction);
|
|
362
|
-
|
|
363
|
-
for (const ledgerTransaction of debitTransactions) {
|
|
364
|
-
ledgerTransaction.TransactionId = cuid();
|
|
365
|
-
ledgerTransaction.JournalEntryId = newJournalEntry.JournalEntryId;
|
|
366
|
-
|
|
367
|
-
await ledgerTransaction.save(dbTransaction);
|
|
368
|
-
// const dt = await ledgerTransaction.newLedgerTransaction(
|
|
369
|
-
// TransactionTypeOptions.DEBIT,
|
|
370
|
-
// newJournalEntry.JournalEntryId,
|
|
371
|
-
// );
|
|
372
|
-
// await dt.save();
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
for (const ledgerTransaction of creditTransactions) {
|
|
376
|
-
ledgerTransaction.TransactionId = cuid();
|
|
377
|
-
ledgerTransaction.JournalEntryId = newJournalEntry.JournalEntryId;
|
|
378
|
-
|
|
379
|
-
await ledgerTransaction.save(dbTransaction);
|
|
380
|
-
// const ct = await ledgerTransaction.newLedgerTransaction(
|
|
381
|
-
// TransactionTypeOptions.CREDIT,
|
|
382
|
-
// newJournalEntry.JournalEntryId,
|
|
383
|
-
// );
|
|
384
|
-
// await ct.save();
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
await this.AccountingSystem.postJournalEntry(newJournalEntry);
|
|
388
|
-
|
|
389
|
-
const payload = {
|
|
390
|
-
Action: 'Create',
|
|
391
|
-
Activity: 'Post Journal Entry',
|
|
392
|
-
Description: `Journal Entry (ID: ${newJournalEntry.JournalEntryId}) has been created`,
|
|
393
|
-
EntityType: 'JournalEntry',
|
|
394
|
-
EntityValueBefore: JSON.stringify({}),
|
|
395
|
-
EntityValueAfter: JSON.stringify(newJournalEntry),
|
|
396
|
-
PerformedById: 'test',
|
|
397
|
-
PerformedAt: new Date(),
|
|
398
|
-
EntityId: newJournalEntry.JournalEntryId,
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
await axios.post(
|
|
402
|
-
`${process.env.COMMON_API_URL}/activity-histories`,
|
|
403
|
-
payload,
|
|
404
|
-
);
|
|
405
|
-
} catch (error) {
|
|
406
|
-
throw error;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
async createAccount(dbTransaction: any, account: Account) {
|
|
411
|
-
try {
|
|
412
|
-
if (!account.AccountType) {
|
|
413
|
-
throw new Error('AccountType is required.');
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
let createAccountPayload: any = {
|
|
417
|
-
Name: account.Name,
|
|
418
|
-
AcctNum: account.AccountNo,
|
|
419
|
-
AccountType: account.AccountType,
|
|
420
|
-
AccountSubType: account.AccountSubtype,
|
|
421
|
-
};
|
|
422
|
-
|
|
423
|
-
if (account.isParentAccountExists()) {
|
|
424
|
-
createAccountPayload = {
|
|
425
|
-
...createAccountPayload,
|
|
426
|
-
CurrencyRef: 'MYR',
|
|
427
|
-
ParentRef: account.ParentAccountNo,
|
|
428
|
-
SubAccount: true,
|
|
429
|
-
};
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
const accSystemAccountId = await this.AccountingSystem.createAccount(
|
|
433
|
-
createAccountPayload,
|
|
434
|
-
);
|
|
435
|
-
account.PostedToAccSystemYN = 'Y';
|
|
436
|
-
const newAccount = await account.save(
|
|
437
|
-
this.CompanyId,
|
|
438
|
-
accSystemAccountId,
|
|
439
|
-
'test',
|
|
440
|
-
dbTransaction,
|
|
441
|
-
);
|
|
442
|
-
|
|
443
|
-
const payload = {
|
|
444
|
-
Action: 'Create',
|
|
445
|
-
Activity: 'Account Created',
|
|
446
|
-
Description: `Account (ID: ${newAccount.AccountNo}) has been created`,
|
|
447
|
-
EntityType: 'Account',
|
|
448
|
-
EntityValueBefore: JSON.stringify({}),
|
|
449
|
-
EntityValueAfter: JSON.stringify(newAccount),
|
|
450
|
-
PerformedById: 'test',
|
|
451
|
-
PerformedAt: new Date(),
|
|
452
|
-
EntityId: newAccount.AccountNo,
|
|
453
|
-
};
|
|
454
|
-
|
|
455
|
-
await axios.post(
|
|
456
|
-
`${process.env.COMMON_API_URL}/activity-histories`,
|
|
457
|
-
payload,
|
|
458
|
-
);
|
|
459
|
-
|
|
460
|
-
return account;
|
|
461
|
-
} catch (error) {
|
|
462
|
-
throw error;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
/**
|
|
467
|
-
* Issue an invoice
|
|
468
|
-
*
|
|
469
|
-
* @param dbTransaction - The database transaction to be used
|
|
470
|
-
* @param loginUser - The user issuing the invoice
|
|
471
|
-
* @param invoice - The document containing the invoice details
|
|
472
|
-
* @param customer - The customer to be issued the invoice
|
|
473
|
-
* @param dtAccountNo - The account number of the Account Receivable (AR) account to debit. If not provided, will debit the customer's default AR account
|
|
474
|
-
*
|
|
475
|
-
* @returns {Document} - Document object representing the full details of the invoice issued
|
|
476
|
-
*/
|
|
477
|
-
async issueInvoice(
|
|
478
|
-
/*todo: loginUser & customer is NOT supposed to be optional */
|
|
479
|
-
dbTransaction: any,
|
|
480
|
-
invoice: Document,
|
|
481
|
-
loginUser?: LoginUserBase,
|
|
482
|
-
customer?: FinanceCustomerBase,
|
|
483
|
-
dtAccountNo?: string,
|
|
484
|
-
): Promise<Document> {
|
|
485
|
-
try {
|
|
486
|
-
/*Check if the invoice number already exists (should be unique)*/
|
|
487
|
-
const duplicateInvoice = await FinanceCompany._DocumentRepository.findOne(
|
|
488
|
-
{
|
|
489
|
-
where: {
|
|
490
|
-
DocNo: invoice.DocNo,
|
|
491
|
-
},
|
|
492
|
-
transaction: dbTransaction,
|
|
493
|
-
},
|
|
494
|
-
);
|
|
495
|
-
|
|
496
|
-
if (duplicateInvoice) {
|
|
497
|
-
throw new Error('Invoice number already exists');
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
const documentItems = await invoice.DocumentItems;
|
|
501
|
-
|
|
502
|
-
/*Check if the document has at least 1 document item*/
|
|
503
|
-
if (!documentItems.length) {
|
|
504
|
-
throw new Error('Document must have at least 1 document item');
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
/*Check if each document item has CtAccountNo provided*/
|
|
508
|
-
for (const invoiceItem of documentItems) {
|
|
509
|
-
if (!invoiceItem.CtAccountNo) {
|
|
510
|
-
throw new Error(
|
|
511
|
-
'Each document item should have CtAccountNo provided',
|
|
512
|
-
);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
/*Set up the document type*/
|
|
517
|
-
invoice.DocType = DocType.INVOICE;
|
|
518
|
-
// invoice.DocNo = cuid();
|
|
519
|
-
|
|
520
|
-
/*Saving the document and document items to the database*/
|
|
521
|
-
await FinanceCompany._DocumentRepository.create(
|
|
522
|
-
{
|
|
523
|
-
DocNo: invoice.DocNo,
|
|
524
|
-
DocType: invoice.DocType,
|
|
525
|
-
DocDate: invoice.DocDate,
|
|
526
|
-
CompanyId: invoice.CompanyId,
|
|
527
|
-
Currency: invoice.Currency,
|
|
528
|
-
Amount: invoice.Amount,
|
|
529
|
-
Description: invoice.Description,
|
|
530
|
-
Status: invoice.Status,
|
|
531
|
-
IssuedById: invoice.IssuedById,
|
|
532
|
-
IssuedToId: invoice.IssuedToId,
|
|
533
|
-
IssuedToType: invoice.IssuedToType,
|
|
534
|
-
RelatedObjectId: invoice.RelatedObjectId,
|
|
535
|
-
RelatedObjectType: invoice.RelatedObjectType,
|
|
536
|
-
CreatedById: invoice.CreatedById,
|
|
537
|
-
CreatedAt: new Date(),
|
|
538
|
-
UpdatedById: invoice.UpdatedById,
|
|
539
|
-
UpdatedAt: new Date(),
|
|
540
|
-
DocPDFFileMediaId: invoice.DocPDFFileMediaId,
|
|
541
|
-
DocHTMLFileMediaId: invoice.DocHTMLFileMediaId,
|
|
542
|
-
AccSystemRefId: invoice.AccSystemRefId,
|
|
543
|
-
PostedToAccSystemYN: invoice.PostedToAccSystemYN,
|
|
544
|
-
PostedById: invoice.PostedById,
|
|
545
|
-
PostedDateTime: new Date(),
|
|
546
|
-
UseAccSystemDocYN: invoice.UseAccSystemDocYN,
|
|
547
|
-
},
|
|
548
|
-
{
|
|
549
|
-
transaction: dbTransaction,
|
|
550
|
-
},
|
|
551
|
-
);
|
|
552
|
-
|
|
553
|
-
for (const documentItem of documentItems) {
|
|
554
|
-
await FinanceCompany._DocumentItemRepository.create(
|
|
555
|
-
{
|
|
556
|
-
DocumentItemId: cuid(),
|
|
557
|
-
DocNo: invoice.DocNo,
|
|
558
|
-
Name: documentItem.Name,
|
|
559
|
-
NameBM: documentItem.NameBM,
|
|
560
|
-
Description: documentItem.Description,
|
|
561
|
-
ItemId: documentItem.ItemId,
|
|
562
|
-
ItemType: documentItem.ItemType,
|
|
563
|
-
ItemSKU: documentItem.ItemSKU,
|
|
564
|
-
ItemSerialNo: documentItem.ItemSerialNo,
|
|
565
|
-
Currency: documentItem.Currency,
|
|
566
|
-
UnitPrice: documentItem.UnitPrice,
|
|
567
|
-
Quantity: documentItem.Quantity,
|
|
568
|
-
QuantityUOM: documentItem.QuantityUOM,
|
|
569
|
-
Amount: documentItem.Amount,
|
|
570
|
-
TaxCode: documentItem.TaxCode,
|
|
571
|
-
TaxAmount: documentItem.TaxAmount,
|
|
572
|
-
TaxRate: documentItem.TaxRate,
|
|
573
|
-
TaxInclusiveYN: documentItem.TaxInclusiveYN,
|
|
574
|
-
DtAccountNo: documentItem.DtAccountNo
|
|
575
|
-
? documentItem.DtAccountNo
|
|
576
|
-
: null,
|
|
577
|
-
CtAccountNo: documentItem.CtAccountNo
|
|
578
|
-
? documentItem.CtAccountNo
|
|
579
|
-
: null,
|
|
580
|
-
},
|
|
581
|
-
{
|
|
582
|
-
transaction: dbTransaction,
|
|
583
|
-
},
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
/*Generating the invoice*/
|
|
588
|
-
if (invoice.UseAccSystemDocYN === 'Y') {
|
|
589
|
-
/*todo: Posting to accounting system to generate invoice*/
|
|
590
|
-
await this.AccountingSystem.createInvoice(invoice);
|
|
591
|
-
} else {
|
|
592
|
-
/*todo: check config file to see which invoice template is to be used for specific project*/
|
|
593
|
-
|
|
594
|
-
/*Generating invoice based on template*/
|
|
595
|
-
invoice.generateInvoice(invoice.IssuedById, customer);
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
const transactionDate = new Date();
|
|
599
|
-
const htCreditAccountAmount = new HashTable();
|
|
600
|
-
const htCreditAccountCurrency = new HashTable();
|
|
601
|
-
|
|
602
|
-
documentItems.forEach((invoiceItem) => {
|
|
603
|
-
if (!htCreditAccountAmount.exists(invoiceItem.CtAccountNo)) {
|
|
604
|
-
//add the credit account to the hash table
|
|
605
|
-
htCreditAccountAmount.add(
|
|
606
|
-
invoiceItem.CtAccountNo,
|
|
607
|
-
invoiceItem.Amount,
|
|
608
|
-
);
|
|
609
|
-
|
|
610
|
-
htCreditAccountCurrency.add(
|
|
611
|
-
invoiceItem.CtAccountNo,
|
|
612
|
-
invoiceItem.Currency,
|
|
613
|
-
);
|
|
614
|
-
} else {
|
|
615
|
-
//update the credit account amount
|
|
616
|
-
const d = htCreditAccountAmount.get(invoiceItem.CtAccountNo);
|
|
617
|
-
htCreditAccountAmount.add(
|
|
618
|
-
invoiceItem.CtAccountNo,
|
|
619
|
-
d + invoiceItem.Amount,
|
|
620
|
-
);
|
|
621
|
-
}
|
|
622
|
-
});
|
|
623
|
-
|
|
624
|
-
const savedItems = htCreditAccountAmount.list();
|
|
625
|
-
|
|
626
|
-
for (let i = 0; i < savedItems.length; i++) {
|
|
627
|
-
const journalEntry = new JournalEntry(dbTransaction);
|
|
628
|
-
//Temporary fix to successfully save journal entry in PostJournal
|
|
629
|
-
journalEntry.init({
|
|
630
|
-
CompanyId: this.CompanyId,
|
|
631
|
-
Name: 'Issue Invoice ' + invoice.DocNo,
|
|
632
|
-
});
|
|
633
|
-
const account = await Account.initAccount(
|
|
634
|
-
dbTransaction,
|
|
635
|
-
savedItems[i].value[0],
|
|
636
|
-
);
|
|
637
|
-
const creditAmount = savedItems[i].value[1];
|
|
638
|
-
const currency = htCreditAccountCurrency.get(savedItems[i].value[0]);
|
|
639
|
-
|
|
640
|
-
const dt = await journalEntry.newLedgerTransaction(
|
|
641
|
-
TransactionTypeOptions.DEBIT,
|
|
642
|
-
);
|
|
643
|
-
|
|
644
|
-
if (dtAccountNo) {
|
|
645
|
-
/*Transacting using AR account provided*/
|
|
646
|
-
dt.AccountNo = dtAccountNo;
|
|
647
|
-
} else {
|
|
648
|
-
/*Transacting based on default customer AR account*/
|
|
649
|
-
dt.AccountNo = (await customer.AccountReceivable).AccountNo;
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
dt.Currency = currency ? currency : 'MYR';
|
|
653
|
-
dt.DebitAmount = creditAmount ? creditAmount : 0.0;
|
|
654
|
-
dt.Date = transactionDate;
|
|
655
|
-
dt.Description = account.Name;
|
|
656
|
-
dt.Name = account.Name;
|
|
657
|
-
dt.RelatedDocNo = invoice.DocNo;
|
|
658
|
-
|
|
659
|
-
const ct = await journalEntry.newLedgerTransaction(
|
|
660
|
-
TransactionTypeOptions.CREDIT,
|
|
661
|
-
);
|
|
662
|
-
ct.AccountNo = savedItems[i].
|
|
663
|
-
ct.Currency = currency ? currency : 'MYR';
|
|
664
|
-
ct.CreditAmount = creditAmount ? creditAmount : 0.0;
|
|
665
|
-
ct.Date = transactionDate;
|
|
666
|
-
ct.Description = account.Name;
|
|
667
|
-
ct.Name = account.Name;
|
|
668
|
-
ct.RelatedDocNo = invoice.DocNo;
|
|
669
|
-
|
|
670
|
-
await this.postJournal(dbTransaction, journalEntry);
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
return invoice;
|
|
674
|
-
} catch (err) {
|
|
675
|
-
// tslint:disable-next-line:no-console
|
|
676
|
-
console.log('Issue invoice err: ', err);
|
|
677
|
-
throw err;
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
/**
|
|
682
|
-
* Issue a debit note
|
|
683
|
-
*
|
|
684
|
-
* @param dbTransaction - The database transaction to be used
|
|
685
|
-
* @param loginUser - The user issuing the invoice
|
|
686
|
-
* @param invoice - The document containing the invoice details
|
|
687
|
-
* @param customer - The customer to be issued the invoice
|
|
688
|
-
* @param dtAccountNo - The account number of the Account Receivable (AR) account to debit. If not provided, will debit the customer's default AR account
|
|
689
|
-
*
|
|
690
|
-
* @returns {Document} - Document object representing the full details of the invoice issued
|
|
691
|
-
*/
|
|
692
|
-
async issueDebitNote(
|
|
693
|
-
dbTransaction: any,
|
|
694
|
-
loginUser: LoginUserBase,
|
|
695
|
-
invoice: Document,
|
|
696
|
-
customer: FinanceCustomerBase,
|
|
697
|
-
dtAccountNo?: string,
|
|
698
|
-
): Promise<Document> {
|
|
699
|
-
try {
|
|
700
|
-
/*Check if the invoice number already exists (should be unique)*/
|
|
701
|
-
const duplicateInvoice = await FinanceCompany._DocumentRepository.findOne(
|
|
702
|
-
{
|
|
703
|
-
where: {
|
|
704
|
-
DocNo: invoice.DocNo,
|
|
705
|
-
},
|
|
706
|
-
transaction: dbTransaction,
|
|
707
|
-
},
|
|
708
|
-
);
|
|
709
|
-
|
|
710
|
-
if (duplicateInvoice) {
|
|
711
|
-
throw new Error('Invoice number already exists');
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
const documentItems = await invoice.DocumentItems;
|
|
715
|
-
|
|
716
|
-
/*Check if the document has at least 1 document item*/
|
|
717
|
-
if (!documentItems.length) {
|
|
718
|
-
throw new Error('Document must have at least 1 document item');
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
/*Check if each document item has CtAccountNo provided*/
|
|
722
|
-
for (const invoiceItem of documentItems) {
|
|
723
|
-
if (!invoiceItem.CtAccountNo) {
|
|
724
|
-
throw new Error(
|
|
725
|
-
'Each document item should have CtAccountNo provided',
|
|
726
|
-
);
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
/*Set up the document type*/
|
|
731
|
-
invoice.DocType = DocType.DEBIT_NOTE;
|
|
732
|
-
// invoice.DocNo = cuid();
|
|
733
|
-
|
|
734
|
-
/*Saving the document and document items to the database*/
|
|
735
|
-
await FinanceCompany._DocumentRepository.create(
|
|
736
|
-
{
|
|
737
|
-
DocNo: invoice.DocNo,
|
|
738
|
-
DocType: invoice.DocType,
|
|
739
|
-
DocDate: invoice.DocDate,
|
|
740
|
-
CompanyId: invoice.CompanyId,
|
|
741
|
-
Currency: invoice.Currency,
|
|
742
|
-
Amount: invoice.Amount,
|
|
743
|
-
Description: invoice.Description,
|
|
744
|
-
Status: invoice.Status,
|
|
745
|
-
IssuedById: loginUser.ObjectId,
|
|
746
|
-
IssuedToId: customer.ObjectId,
|
|
747
|
-
IssuedToType: invoice.IssuedToType,
|
|
748
|
-
RelatedObjectId: invoice.RelatedObjectId,
|
|
749
|
-
RelatedObjectType: invoice.RelatedObjectType,
|
|
750
|
-
CreatedById: loginUser.ObjectId,
|
|
751
|
-
CreatedAt: new Date(),
|
|
752
|
-
UpdatedById: loginUser.ObjectId,
|
|
753
|
-
UpdatedAt: new Date(),
|
|
754
|
-
DocPDFFileMediaId: invoice.DocPDFFileMediaId,
|
|
755
|
-
DocHTMLFileMediaId: invoice.DocHTMLFileMediaId,
|
|
756
|
-
AccSystemRefId: invoice.AccSystemRefId,
|
|
757
|
-
PostedToAccSystemYN: invoice.PostedToAccSystemYN,
|
|
758
|
-
PostedById: invoice.PostedById,
|
|
759
|
-
PostedDateTime: new Date(),
|
|
760
|
-
UseAccSystemDocYN: invoice.UseAccSystemDocYN,
|
|
761
|
-
},
|
|
762
|
-
{
|
|
763
|
-
transaction: dbTransaction,
|
|
764
|
-
},
|
|
765
|
-
);
|
|
766
|
-
|
|
767
|
-
documentItems.forEach(async (documentItem) => {
|
|
768
|
-
await FinanceCompany._DocumentItemRepository.create(
|
|
769
|
-
{
|
|
770
|
-
DocumentItemId: cuid(),
|
|
771
|
-
DocNo: documentItem.DocNo,
|
|
772
|
-
Name: documentItem.Name,
|
|
773
|
-
NameBM: documentItem.NameBM,
|
|
774
|
-
Description: documentItem.Description,
|
|
775
|
-
ItemId: documentItem.ItemId,
|
|
776
|
-
ItemType: documentItem.ItemType,
|
|
777
|
-
ItemSKU: documentItem.ItemSKU,
|
|
778
|
-
ItemSerialNo: documentItem.ItemSerialNo,
|
|
779
|
-
Currency: documentItem.Currency,
|
|
780
|
-
UnitPrice: documentItem.UnitPrice,
|
|
781
|
-
Quantity: documentItem.Quantity,
|
|
782
|
-
QuantityUOM: documentItem.QuantityUOM,
|
|
783
|
-
Amount: documentItem.Amount,
|
|
784
|
-
TaxCode: documentItem.TaxCode,
|
|
785
|
-
TaxAmount: documentItem.TaxAmount,
|
|
786
|
-
TaxRate: documentItem.TaxRate,
|
|
787
|
-
TaxInclusiveYN: documentItem.TaxInclusiveYN,
|
|
788
|
-
DtAccountNo: documentItem.DtAccountNo,
|
|
789
|
-
CtAccountNo: documentItem.CtAccountNo,
|
|
790
|
-
},
|
|
791
|
-
{
|
|
792
|
-
transaction: dbTransaction,
|
|
793
|
-
},
|
|
794
|
-
);
|
|
795
|
-
});
|
|
796
|
-
|
|
797
|
-
/*Generating the invoice*/
|
|
798
|
-
if (invoice.UseAccSystemDocYN === 'Y') {
|
|
799
|
-
/*todo: Posting to accounting system to generate invoice*/
|
|
800
|
-
await this.AccountingSystem.createInvoice(invoice);
|
|
801
|
-
} else {
|
|
802
|
-
/*todo: check config file to see which invoice template is to be used for specific project*/
|
|
803
|
-
|
|
804
|
-
/*Generating invoice based on template*/
|
|
805
|
-
invoice.generateInvoice(loginUser.IDNo, customer);
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
const transactionDate = new Date();
|
|
809
|
-
const htCreditAccountAmount = new HashTable();
|
|
810
|
-
const htCreditAccountCurrency = new HashTable();
|
|
811
|
-
|
|
812
|
-
documentItems.forEach((invoiceItem) => {
|
|
813
|
-
if (!htCreditAccountAmount.exists(invoiceItem.CtAccountNo)) {
|
|
814
|
-
//add the credit account to the hash table
|
|
815
|
-
htCreditAccountAmount.add(
|
|
816
|
-
invoiceItem.CtAccountNo,
|
|
817
|
-
invoiceItem.Amount,
|
|
818
|
-
);
|
|
819
|
-
|
|
820
|
-
htCreditAccountCurrency.add(
|
|
821
|
-
invoiceItem.CtAccountNo,
|
|
822
|
-
invoiceItem.Currency,
|
|
823
|
-
);
|
|
824
|
-
} else {
|
|
825
|
-
//update the credit account amount
|
|
826
|
-
const d = htCreditAccountAmount.get(invoiceItem.CtAccountNo);
|
|
827
|
-
htCreditAccountAmount.add(
|
|
828
|
-
invoiceItem.CtAccountNo,
|
|
829
|
-
d + invoiceItem.Amount,
|
|
830
|
-
);
|
|
831
|
-
}
|
|
832
|
-
});
|
|
833
|
-
|
|
834
|
-
const savedItems = htCreditAccountAmount.list();
|
|
835
|
-
|
|
836
|
-
for (let i = 0; i < savedItems.length; i++) {
|
|
837
|
-
const journalEntry = new JournalEntry(dbTransaction);
|
|
838
|
-
//Temporary fix to successfully save journal entry in PostJournal
|
|
839
|
-
journalEntry.init({
|
|
840
|
-
CompanyId: this.CompanyId,
|
|
841
|
-
Name: 'issue Invoice ' + invoice.DocNo,
|
|
842
|
-
});
|
|
843
|
-
const account = await Account.initAccount(
|
|
844
|
-
dbTransaction,
|
|
845
|
-
savedItems[i].value[0],
|
|
846
|
-
);
|
|
847
|
-
const creditAmount = savedItems[i].value[1];
|
|
848
|
-
const currency = htCreditAccountCurrency.get(savedItems[i].value[0]);
|
|
849
|
-
|
|
850
|
-
const dt = await journalEntry.newLedgerTransaction(
|
|
851
|
-
TransactionTypeOptions.DEBIT,
|
|
852
|
-
);
|
|
853
|
-
|
|
854
|
-
if (dtAccountNo) {
|
|
855
|
-
/*Transacting using AR account provided*/
|
|
856
|
-
dt.AccountNo = dtAccountNo;
|
|
857
|
-
} else {
|
|
858
|
-
/*Transacting based on default customer AR account*/
|
|
859
|
-
dt.AccountNo = (await customer.AccountReceivable).AccountNo;
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
dt.Currency = currency ? currency : 'MYR';
|
|
863
|
-
dt.DebitAmount = creditAmount ? creditAmount : 0.0;
|
|
864
|
-
dt.Date = transactionDate;
|
|
865
|
-
dt.Description = account.Name;
|
|
866
|
-
dt.Name = account.Name;
|
|
867
|
-
dt.RelatedDocNo = invoice.DocNo;
|
|
868
|
-
|
|
869
|
-
const ct = await journalEntry.newLedgerTransaction(
|
|
870
|
-
TransactionTypeOptions.CREDIT,
|
|
871
|
-
);
|
|
872
|
-
ct.AccountNo = savedItems[i].key;
|
|
873
|
-
ct.Currency = currency ? currency : 'MYR';
|
|
874
|
-
ct.CreditAmount = creditAmount ? creditAmount : 0.0;
|
|
875
|
-
ct.Date = transactionDate;
|
|
876
|
-
ct.Description = account.Name;
|
|
877
|
-
ct.Name = account.Name;
|
|
878
|
-
ct.RelatedDocNo = invoice.DocNo;
|
|
879
|
-
|
|
880
|
-
await this.postJournal(dbTransaction, journalEntry);
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
return invoice;
|
|
884
|
-
} catch (err) {
|
|
885
|
-
// tslint:disable-next-line:no-console
|
|
886
|
-
console.log('Issue debit note err: ', err);
|
|
887
|
-
throw err;
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
/**
|
|
892
|
-
* Issue a credit note
|
|
893
|
-
*
|
|
894
|
-
* @param dbTransaction - The database transaction to be used
|
|
895
|
-
* @param loginUser - The user issuing the credit note
|
|
896
|
-
* @param creditNote - The document containing the credit note details
|
|
897
|
-
* @param customer - The customer to be issued the credit note
|
|
898
|
-
* @param ctAccountNo - The account number of the Account Payable (AP) account to debit. If not provided, will debit the customer's default AR account
|
|
899
|
-
*
|
|
900
|
-
* @returns {Document} - Document object representing the full details of the invoice issued
|
|
901
|
-
*/
|
|
902
|
-
async issueCreditNote(
|
|
903
|
-
dbTransaction: any,
|
|
904
|
-
loginUser: LoginUserBase,
|
|
905
|
-
creditNote: Document,
|
|
906
|
-
customer: FinanceCustomerBase,
|
|
907
|
-
ctAccountNo?: string,
|
|
908
|
-
): Promise<Document> {
|
|
909
|
-
try {
|
|
910
|
-
/*Check if the invoice number already exists (should be unique)*/
|
|
911
|
-
const duplicateCreditNote =
|
|
912
|
-
await FinanceCompany._DocumentRepository.findOne({
|
|
913
|
-
where: {
|
|
914
|
-
DocNo: creditNote.DocNo,
|
|
915
|
-
},
|
|
916
|
-
transaction: dbTransaction,
|
|
917
|
-
});
|
|
918
|
-
|
|
919
|
-
if (duplicateCreditNote) {
|
|
920
|
-
throw new Error('Invoice number already exists');
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
const documentItems = await creditNote.DocumentItems;
|
|
924
|
-
|
|
925
|
-
/*Check if the document has at least 1 document item*/
|
|
926
|
-
if (!documentItems.length) {
|
|
927
|
-
throw new Error('Document must have at least 1 document item');
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
/*Check if each document item has CtAccountNo provided*/
|
|
931
|
-
for (const invoiceItem of documentItems) {
|
|
932
|
-
if (!invoiceItem.CtAccountNo) {
|
|
933
|
-
throw new Error(
|
|
934
|
-
'Each document item should have CtAccountNo provided',
|
|
935
|
-
);
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
/*Set up the document type*/
|
|
940
|
-
creditNote.DocType = DocType.DEBIT_NOTE;
|
|
941
|
-
|
|
942
|
-
/*Saving the document and document items to the database*/
|
|
943
|
-
await FinanceCompany._DocumentRepository.create(
|
|
944
|
-
{
|
|
945
|
-
DocNo: creditNote.DocNo,
|
|
946
|
-
DocType: creditNote.DocType,
|
|
947
|
-
DocDate: creditNote.DocDate,
|
|
948
|
-
CompanyId: creditNote.CompanyId,
|
|
949
|
-
Currency: creditNote.Currency,
|
|
950
|
-
Amount: creditNote.Amount,
|
|
951
|
-
Description: creditNote.Description,
|
|
952
|
-
Status: creditNote.Status,
|
|
953
|
-
IssuedById: loginUser.ObjectId,
|
|
954
|
-
IssuedToId: customer.ObjectId,
|
|
955
|
-
IssuedToType: creditNote.IssuedToType,
|
|
956
|
-
RelatedObjectId: creditNote.RelatedObjectId,
|
|
957
|
-
RelatedObjectType: creditNote.RelatedObjectType,
|
|
958
|
-
CreatedById: loginUser.ObjectId,
|
|
959
|
-
CreatedAt: new Date(),
|
|
960
|
-
UpdatedById: loginUser.ObjectId,
|
|
961
|
-
UpdatedAt: new Date(),
|
|
962
|
-
DocPDFFileMediaId: creditNote.DocPDFFileMediaId,
|
|
963
|
-
DocHTMLFileMediaId: creditNote.DocHTMLFileMediaId,
|
|
964
|
-
AccSystemRefId: creditNote.AccSystemRefId,
|
|
965
|
-
PostedToAccSystemYN: creditNote.PostedToAccSystemYN,
|
|
966
|
-
PostedById: creditNote.PostedById,
|
|
967
|
-
PostedDateTime: new Date(),
|
|
968
|
-
UseAccSystemDocYN: creditNote.UseAccSystemDocYN,
|
|
969
|
-
},
|
|
970
|
-
{
|
|
971
|
-
transaction: dbTransaction,
|
|
972
|
-
},
|
|
973
|
-
);
|
|
974
|
-
|
|
975
|
-
documentItems.forEach(async (documentItem) => {
|
|
976
|
-
await FinanceCompany._DocumentItemRepository.create(
|
|
977
|
-
{
|
|
978
|
-
DocumentItemId: documentItem.DocumentItemId,
|
|
979
|
-
DocNo: documentItem.DocNo,
|
|
980
|
-
Name: documentItem.Name,
|
|
981
|
-
NameBM: documentItem.NameBM,
|
|
982
|
-
Description: documentItem.Description,
|
|
983
|
-
ItemId: documentItem.ItemId,
|
|
984
|
-
ItemType: documentItem.ItemType,
|
|
985
|
-
ItemSKU: documentItem.ItemSKU,
|
|
986
|
-
ItemSerialNo: documentItem.ItemSerialNo,
|
|
987
|
-
Currency: documentItem.Currency,
|
|
988
|
-
UnitPrice: documentItem.UnitPrice,
|
|
989
|
-
Quantity: documentItem.Quantity,
|
|
990
|
-
QuantityUOM: documentItem.QuantityUOM,
|
|
991
|
-
Amount: documentItem.Amount,
|
|
992
|
-
TaxCode: documentItem.TaxCode,
|
|
993
|
-
TaxAmount: documentItem.TaxAmount,
|
|
994
|
-
TaxRate: documentItem.TaxRate,
|
|
995
|
-
TaxInclusiveYN: documentItem.TaxInclusiveYN,
|
|
996
|
-
DtAccountNo: documentItem.DtAccountNo,
|
|
997
|
-
CtAccountNo: documentItem.CtAccountNo,
|
|
998
|
-
},
|
|
999
|
-
{
|
|
1000
|
-
transaction: dbTransaction,
|
|
1001
|
-
},
|
|
1002
|
-
);
|
|
1003
|
-
});
|
|
1004
|
-
|
|
1005
|
-
/*Generating the credit note*/
|
|
1006
|
-
if (creditNote.UseAccSystemDocYN === 'Y') {
|
|
1007
|
-
/*todo: Posting to accounting system to generate creditNote*/
|
|
1008
|
-
await this.AccountingSystem.createCreditNote(creditNote);
|
|
1009
|
-
} else {
|
|
1010
|
-
/*todo: check config file to see which invoice template is to be used for specific project*/
|
|
1011
|
-
|
|
1012
|
-
/*Generating credit note based on template*/
|
|
1013
|
-
creditNote.generateCreditNote(loginUser.IDNo, customer);
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
const journalEntry = new JournalEntry(dbTransaction);
|
|
1017
|
-
//Temporary fix to successfully save journal entry in PostJournal
|
|
1018
|
-
journalEntry.init({
|
|
1019
|
-
CompanyId: this.CompanyId,
|
|
1020
|
-
Name: 'Issue Debit Note ' + creditNote.DocNo,
|
|
1021
|
-
});
|
|
1022
|
-
|
|
1023
|
-
const transactionDate = new Date();
|
|
1024
|
-
|
|
1025
|
-
const creditTransaction = await journalEntry.newLedgerTransaction(
|
|
1026
|
-
TransactionTypeOptions.CREDIT,
|
|
1027
|
-
);
|
|
1028
|
-
|
|
1029
|
-
if (ctAccountNo) {
|
|
1030
|
-
/*Transacting using AR account provided*/
|
|
1031
|
-
creditTransaction.AccountNo = ctAccountNo;
|
|
1032
|
-
} else {
|
|
1033
|
-
/*Transacting based on default customer AR account*/
|
|
1034
|
-
creditTransaction.AccountNo = customer.CustSystemCode + '-AP';
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
|
-
creditTransaction.Currency = creditNote.Currency;
|
|
1038
|
-
creditTransaction.CreditAmount = creditNote.Amount;
|
|
1039
|
-
creditTransaction.Date = transactionDate;
|
|
1040
|
-
creditTransaction.Description = creditNote.DocNo;
|
|
1041
|
-
creditTransaction.Name = creditNote.DocNo;
|
|
1042
|
-
|
|
1043
|
-
for (const invoiceItem of documentItems) {
|
|
1044
|
-
const itemLedger = await journalEntry.newLedgerTransaction(
|
|
1045
|
-
TransactionTypeOptions.DEBIT,
|
|
1046
|
-
);
|
|
1047
|
-
itemLedger.AccountNo = invoiceItem.CtAccountNo;
|
|
1048
|
-
itemLedger.Currency = invoiceItem.Currency;
|
|
1049
|
-
itemLedger.DebitAmount = invoiceItem.Amount;
|
|
1050
|
-
itemLedger.Date = transactionDate;
|
|
1051
|
-
itemLedger.Description = invoiceItem.Description;
|
|
1052
|
-
itemLedger.Name = invoiceItem.Name;
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
this.postJournal(dbTransaction, journalEntry);
|
|
1056
|
-
|
|
1057
|
-
const payload = {
|
|
1058
|
-
Action: 'Create',
|
|
1059
|
-
Activity: 'Issuing a Credit Note Transaction',
|
|
1060
|
-
Description: `Credit Transaction (ID: ${creditTransaction.TransactionId}) has been created`,
|
|
1061
|
-
EntityType: 'CreditTransaction',
|
|
1062
|
-
EntityValueBefore: JSON.stringify({}),
|
|
1063
|
-
EntityValueAfter: JSON.stringify(creditTransaction),
|
|
1064
|
-
PerformedById: 'test',
|
|
1065
|
-
PerformedAt: transactionDate,
|
|
1066
|
-
EntityId: creditTransaction.TransactionId,
|
|
1067
|
-
};
|
|
1068
|
-
|
|
1069
|
-
await axios.post(
|
|
1070
|
-
`${process.env.COMMON_API_URL}/activity-histories`,
|
|
1071
|
-
payload,
|
|
1072
|
-
);
|
|
1073
|
-
|
|
1074
|
-
return creditNote;
|
|
1075
|
-
} catch (err) {
|
|
1076
|
-
// tslint:disable-next-line:no-console
|
|
1077
|
-
console.log('Issue credit note err: ', err);
|
|
1078
|
-
throw err;
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
/**
|
|
1083
|
-
* Register a payment collected
|
|
1084
|
-
*
|
|
1085
|
-
* @param dbTransaction - The database transaction to be used
|
|
1086
|
-
* @param loginUser - The user collecting the payment
|
|
1087
|
-
* @param payment - The payment object containing payment details
|
|
1088
|
-
* @param customer - The customer making the payment
|
|
1089
|
-
* @param ctAccountNo - The account number of the customer's Account Receivable account to transact, else the default customer account payable receivable will be used
|
|
1090
|
-
*
|
|
1091
|
-
* @returns {Payment} - Payment object representing the full detals of the payment collection recorded
|
|
1092
|
-
*/
|
|
1093
|
-
async collectPayment(
|
|
1094
|
-
dbTransaction: any,
|
|
1095
|
-
loginUser: LoginUserBase,
|
|
1096
|
-
payment: Payment,
|
|
1097
|
-
customer: FinanceCustomerBase,
|
|
1098
|
-
ctAccountNo?: string,
|
|
1099
|
-
): Promise<Payment> {
|
|
1100
|
-
try {
|
|
1101
|
-
/*validation 1: Make sure that the payment has at least 1 payment item*/
|
|
1102
|
-
const paymentItems = await payment.PaymentItems;
|
|
1103
|
-
if (paymentItems.length < 1) {
|
|
1104
|
-
throw new Error(
|
|
1105
|
-
'Atleast one payment item is required to identify what payment is being paid for.',
|
|
1106
|
-
);
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
/*validation 2: Make sure that the payment has at least 1 payment method*/
|
|
1110
|
-
const paymentPaidWithItems = await payment.PaymentPaidWith;
|
|
1111
|
-
if (paymentPaidWithItems.length < 1) {
|
|
1112
|
-
throw new Error(
|
|
1113
|
-
'Atleast one payment paid with item is required to identify how the payment was made.',
|
|
1114
|
-
);
|
|
1115
|
-
}
|
|
1116
|
-
|
|
1117
|
-
payment.PaymentId = cuid();
|
|
1118
|
-
payment.PaymentType = PaymentType.PAYMENT_RECEIVED;
|
|
1119
|
-
payment.ReceivedBy = loginUser.ObjectId;
|
|
1120
|
-
|
|
1121
|
-
await FinanceCompany._PaymentRepository.create(
|
|
1122
|
-
{
|
|
1123
|
-
PaymentId: payment.PaymentId,
|
|
1124
|
-
PaymentType: payment.PaymentType,
|
|
1125
|
-
PaymentDate: payment.PaymentDate,
|
|
1126
|
-
Description: payment.Description,
|
|
1127
|
-
Currency: payment.Currency,
|
|
1128
|
-
Amount: payment.Amount,
|
|
1129
|
-
Status: PaymentStatus.SUCCESSFUL,
|
|
1130
|
-
PostedToAccSystemYN: 'N',
|
|
1131
|
-
ReceivedBy: payment.ReceivedBy,
|
|
1132
|
-
UpdatedAt: new Date(),
|
|
1133
|
-
UpdatedBy: loginUser.ObjectId,
|
|
1134
|
-
CreatedAt: new Date(),
|
|
1135
|
-
CreatedBy: loginUser.ObjectId,
|
|
1136
|
-
},
|
|
1137
|
-
{ transaction: dbTransaction },
|
|
1138
|
-
);
|
|
1139
|
-
|
|
1140
|
-
for (const paymentItem of paymentItems) {
|
|
1141
|
-
await FinanceCompany._PaymentItemRepository.create(
|
|
1142
|
-
{
|
|
1143
|
-
PaymentId: payment.PaymentId,
|
|
1144
|
-
PayForObjectId: paymentItem.PayForObjectId,
|
|
1145
|
-
PayForObjectType: paymentItem.PayForObjectType,
|
|
1146
|
-
Currency: paymentItem.Currency,
|
|
1147
|
-
Amount: paymentItem.Amount,
|
|
1148
|
-
Name: paymentItem.Name,
|
|
1149
|
-
Description: paymentItem.Description,
|
|
1150
|
-
},
|
|
1151
|
-
{ transaction: dbTransaction },
|
|
1152
|
-
);
|
|
1153
|
-
}
|
|
1154
|
-
|
|
1155
|
-
for (const paymentPaidWithItem of paymentPaidWithItems) {
|
|
1156
|
-
await FinanceCompany._PaymentPaidWithRepository.create(
|
|
1157
|
-
{
|
|
1158
|
-
PaymentId: payment.PaymentId,
|
|
1159
|
-
MethodTypeId: paymentPaidWithItem.MethodTypeId,
|
|
1160
|
-
Currency: paymentPaidWithItem.Currency,
|
|
1161
|
-
Amount: paymentPaidWithItem.Amount,
|
|
1162
|
-
Status: paymentPaidWithItem.Status,
|
|
1163
|
-
TransactionId: paymentPaidWithItem.TransactionId,
|
|
1164
|
-
RefBank: paymentPaidWithItem.RefBank,
|
|
1165
|
-
RefName: paymentPaidWithItem.RefName,
|
|
1166
|
-
RefNo: paymentPaidWithItem.RefNo,
|
|
1167
|
-
RefOther1: paymentPaidWithItem.RefOther1,
|
|
1168
|
-
RefOther2: paymentPaidWithItem.RefOther2,
|
|
1169
|
-
RefOther3: paymentPaidWithItem.RefOther3,
|
|
1170
|
-
RefOther4: paymentPaidWithItem.RefOther4,
|
|
1171
|
-
RefOther5: paymentPaidWithItem.RefOther5,
|
|
1172
|
-
Remarks: paymentPaidWithItem.Remarks,
|
|
1173
|
-
PaymentMediaId: paymentPaidWithItem.PaymentMediaId,
|
|
1174
|
-
},
|
|
1175
|
-
{ transaction: dbTransaction },
|
|
1176
|
-
);
|
|
1177
|
-
}
|
|
1178
|
-
|
|
1179
|
-
/*Registering the Journal Entries for the transaction*/
|
|
1180
|
-
const transactionDate = new Date();
|
|
1181
|
-
|
|
1182
|
-
for (const paymentPaidWith of paymentPaidWithItems) {
|
|
1183
|
-
let paymentMethodType: PaymentMethodType;
|
|
1184
|
-
|
|
1185
|
-
try {
|
|
1186
|
-
paymentMethodType =
|
|
1187
|
-
dbTransaction,
|
|
1188
|
-
paymentPaidWith.MethodTypeId,
|
|
1189
|
-
);
|
|
1190
|
-
} catch (error) {
|
|
1191
|
-
if (error instanceof RecordNotFoundError) {
|
|
1192
|
-
throw new Error('Invalid Payment Method Type Id');
|
|
1193
|
-
} else {
|
|
1194
|
-
throw error;
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
const journalEntry = new JournalEntry(dbTransaction);
|
|
1198
|
-
|
|
1199
|
-
journalEntry.init({
|
|
1200
|
-
CompanyId: this.CompanyId,
|
|
1201
|
-
Name: 'Collect-payments for ' + payment.PaymentId,
|
|
1202
|
-
});
|
|
1203
|
-
|
|
1204
|
-
const debitLT = await journalEntry.newLedgerTransaction(
|
|
1205
|
-
TransactionTypeOptions.DEBIT,
|
|
1206
|
-
);
|
|
1207
|
-
debitLT.AccountNo = paymentMethodType.AccountNo;
|
|
1208
|
-
debitLT.Currency = payment.Currency;
|
|
1209
|
-
debitLT.DebitAmount = payment.Amount;
|
|
1210
|
-
debitLT.Date = transactionDate;
|
|
1211
|
-
debitLT.Description = customer.FullName;
|
|
1212
|
-
debitLT.Name = customer.FullName;
|
|
1213
|
-
|
|
1214
|
-
const creditLT = await journalEntry.newLedgerTransaction(
|
|
1215
|
-
TransactionTypeOptions.CREDIT,
|
|
1216
|
-
);
|
|
1217
|
-
|
|
1218
|
-
if (ctAccountNo) {
|
|
1219
|
-
creditLT.AccountNo = ctAccountNo;
|
|
1220
|
-
} else {
|
|
1221
|
-
creditLT.AccountNo = (await customer.AccountReceivable).AccountNo;
|
|
1222
|
-
}
|
|
1223
|
-
creditLT.Currency = payment.Currency;
|
|
1224
|
-
creditLT.CreditAmount = payment.Amount;
|
|
1225
|
-
creditLT.Date = transactionDate;
|
|
1226
|
-
creditLT.Description = paymentMethodType.Name;
|
|
1227
|
-
creditLT.Name = paymentMethodType.Name;
|
|
1228
|
-
|
|
1229
|
-
await this.postJournal(dbTransaction, journalEntry);
|
|
1230
|
-
}
|
|
1231
|
-
|
|
1232
|
-
/*todo: saving a record into the activity history table*/
|
|
1233
|
-
|
|
1234
|
-
return payment;
|
|
1235
|
-
} catch (error) {
|
|
1236
|
-
throw error;
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1239
|
-
|
|
1240
|
-
/**
|
|
1241
|
-
* Method to make payment to a customer.
|
|
1242
|
-
*
|
|
1243
|
-
* @param dbTransaction - The database transaction to be used
|
|
1244
|
-
* @param loginUser - The user logging in and the user who collected the payment.
|
|
1245
|
-
* @param payment - The payment object containing payment details
|
|
1246
|
-
* @param customer - The customer who is receiving the payment.
|
|
1247
|
-
* @param ctAccountNo - The account number of the customer's Account Payable account to transact, else the default customer account payable will be used.
|
|
1248
|
-
*
|
|
1249
|
-
* @returns {Payment} - Payment object representing the full details of the payment collection recorded
|
|
1250
|
-
*/
|
|
1251
|
-
async makePayment(
|
|
1252
|
-
dbTransaction: any,
|
|
1253
|
-
loginUser: LoginUserBase,
|
|
1254
|
-
payment: Payment,
|
|
1255
|
-
customer: FinanceCustomerBase,
|
|
1256
|
-
dtAccountNo?: string,
|
|
1257
|
-
): Promise<Payment> {
|
|
1258
|
-
let paymentMethodType: PaymentMethodType;
|
|
1259
|
-
try {
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
{
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
debitLT
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
}
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import * as cuid from 'cuid';
|
|
3
|
+
import {
|
|
4
|
+
HashTable,
|
|
5
|
+
LoginUserBase,
|
|
6
|
+
ObjectBase,
|
|
7
|
+
RecordNotFoundError,
|
|
8
|
+
} from '@tomei/general';
|
|
9
|
+
import Account from '../account/account';
|
|
10
|
+
import JournalEntry from '../journal-entry/journal-entry';
|
|
11
|
+
import FinanceCustomerBase from '../customer/customer';
|
|
12
|
+
import Document from '../document/document';
|
|
13
|
+
import { IAccountSystem } from '../interfaces';
|
|
14
|
+
import { FinanceCompanyRepository } from './finance-company.repository';
|
|
15
|
+
import { FinanceCustomerRepository } from '../customer/finance-customer.repository';
|
|
16
|
+
import { LedgerTransactionRepository } from '../ledger-transaction/ledger-transaction.repository';
|
|
17
|
+
import {
|
|
18
|
+
DocType,
|
|
19
|
+
PaymentStatus,
|
|
20
|
+
PaymentType,
|
|
21
|
+
TransactionTypeOptions,
|
|
22
|
+
} from '../enum';
|
|
23
|
+
import PaymentMethodType from '../payment-method-type/payment-method-type';
|
|
24
|
+
import { PaymentRepository } from '../payment/payment.repository';
|
|
25
|
+
import { PaymentItemRepository } from '../payment-item/payment-item.repository';
|
|
26
|
+
import Payment from '../payment/payment';
|
|
27
|
+
import { DocumentRepository } from '../document/document.repository';
|
|
28
|
+
import { DocumentItemRepository } from '../document/document-item.repository';
|
|
29
|
+
import { PaymentMethodRepository } from '../payment-method/payment-method.repository';
|
|
30
|
+
import { PaymentMethodTypeRepository } from '../payment-method-type/payment-method-type.repository';
|
|
31
|
+
import PaymentMethod from '../payment-method/payment-method';
|
|
32
|
+
import { AccountRepository } from '../account/account.repository';
|
|
33
|
+
import { PaymentPaidWithRepository } from '../payment-paid-with/payment-paid-with.repository';
|
|
34
|
+
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
36
|
+
const getConfig = require('../config');
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
38
|
+
const config = getConfig();
|
|
39
|
+
|
|
40
|
+
export default class FinanceCompany extends ObjectBase {
|
|
41
|
+
private _CompanyId = 'New';
|
|
42
|
+
private _CompSystemCode = '';
|
|
43
|
+
private _CompSystemRefId = '';
|
|
44
|
+
private _AccSystemCode = '';
|
|
45
|
+
private static _htFinanceCompanyIds = new HashTable();
|
|
46
|
+
private static _htFinanceCompanies = new HashTable();
|
|
47
|
+
private static _financeCompanyRepository = new FinanceCompanyRepository();
|
|
48
|
+
private static _PaymentRepository = new PaymentRepository();
|
|
49
|
+
private static _PaymentItemRepository = new PaymentItemRepository();
|
|
50
|
+
private static _PaymentPaidWithRepository = new PaymentPaidWithRepository();
|
|
51
|
+
private static _PaymentMethodRepository = new PaymentMethodRepository();
|
|
52
|
+
private static _PaymentMethodTypeRepository =
|
|
53
|
+
new PaymentMethodTypeRepository();
|
|
54
|
+
private static _DocumentRepository = new DocumentRepository();
|
|
55
|
+
private static _DocumentItemRepository = new DocumentItemRepository();
|
|
56
|
+
private static _FinanceCustomerRepository = new FinanceCustomerRepository();
|
|
57
|
+
private static _LedgerTransactionRepository =
|
|
58
|
+
new LedgerTransactionRepository();
|
|
59
|
+
|
|
60
|
+
private static _AccountRepository = new AccountRepository();
|
|
61
|
+
|
|
62
|
+
private _AccountingSystem: IAccountSystem;
|
|
63
|
+
|
|
64
|
+
private _DbTransaction: any;
|
|
65
|
+
private _PaymentMethods = [];
|
|
66
|
+
|
|
67
|
+
get CompSystemCode(): string {
|
|
68
|
+
return this._CompSystemCode;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private set CompSystemCode(code: string) {
|
|
72
|
+
this._CompSystemCode = code;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get CompSystemRefId() {
|
|
76
|
+
return this._CompSystemRefId;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
set CompSystemRefId(id: string) {
|
|
80
|
+
this._CompSystemRefId = id;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
get AccSystemCode() {
|
|
84
|
+
return this._AccSystemCode;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private set AccSystemCode(code: string) {
|
|
88
|
+
this._AccSystemCode = code;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get CompanyId() {
|
|
92
|
+
return this._CompanyId;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get ObjectId() {
|
|
96
|
+
return this._CompanyId;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private set ObjectId(id: string) {
|
|
100
|
+
this._CompanyId = id;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get ObjectName() {
|
|
104
|
+
return `${this.CompSystemCode}-${this.CompSystemRefId}-${this.AccSystemCode}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
get TableName() {
|
|
108
|
+
return 'finance_Company';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
get AccountingSystem(): IAccountSystem {
|
|
112
|
+
return this._AccountingSystem;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
set AccountingSystem(system: IAccountSystem) {
|
|
116
|
+
this._AccountingSystem = system;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
constructor(
|
|
120
|
+
compSystemCode: string,
|
|
121
|
+
compSystemRefId: string,
|
|
122
|
+
accSystemCode: string,
|
|
123
|
+
) {
|
|
124
|
+
super();
|
|
125
|
+
this.CompSystemCode = compSystemCode;
|
|
126
|
+
this.CompSystemRefId = compSystemRefId;
|
|
127
|
+
this.AccSystemCode = accSystemCode;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static async getFinanceCompanyId(
|
|
131
|
+
compSystemCode: string,
|
|
132
|
+
compSystemRefId: string,
|
|
133
|
+
accSystemCode: string,
|
|
134
|
+
): Promise<string> {
|
|
135
|
+
let sCompanyId = '';
|
|
136
|
+
/*Assemble the hashtable key*/
|
|
137
|
+
const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
|
|
138
|
+
/*Check if the FinanceCompany has previously being loaded*/
|
|
139
|
+
if (!FinanceCompany._htFinanceCompanyIds.get(sKey)) {
|
|
140
|
+
/*Instantiate a new FinanceCompany*/
|
|
141
|
+
const financeCompany = new FinanceCompany(
|
|
142
|
+
compSystemCode,
|
|
143
|
+
compSystemRefId,
|
|
144
|
+
accSystemCode,
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
/*Retrieve the finance company from finance_Company table using compSystemCode,
|
|
148
|
+
* CompSystemRefId and accSystemCode */
|
|
149
|
+
const company = await FinanceCompany._financeCompanyRepository.findOne({
|
|
150
|
+
where: {
|
|
151
|
+
CompSystemCode: compSystemCode,
|
|
152
|
+
CompSystemRefId: compSystemRefId,
|
|
153
|
+
AccSystemCode: accSystemCode,
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
/*Retrieve and store the companyId from the result*/
|
|
158
|
+
financeCompany.ObjectId = company.CompanyId;
|
|
159
|
+
sCompanyId = financeCompany.ObjectId;
|
|
160
|
+
|
|
161
|
+
/*Add the details into the hashtable*/
|
|
162
|
+
FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
|
|
163
|
+
FinanceCompany._htFinanceCompanies.add(sCompanyId, financeCompany);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (typeof FinanceCompany._htFinanceCompanyIds.get(sKey) === 'string') {
|
|
167
|
+
sCompanyId = FinanceCompany._htFinanceCompanyIds.get(sKey);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return sCompanyId;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
static async getFinanceCompany(companyId: string): Promise<FinanceCompany> {
|
|
174
|
+
/*Check if the finance company is previously be loaded*/
|
|
175
|
+
if (!FinanceCompany._htFinanceCompanies.get(companyId)) {
|
|
176
|
+
/*Retrieve the finance company from finance_Company table using compSystemCode,
|
|
177
|
+
* CompSystemRefId and accSystemCode */
|
|
178
|
+
const company = await FinanceCompany._financeCompanyRepository.findOne({
|
|
179
|
+
where: { CompanyId: companyId },
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
if (!company) {
|
|
183
|
+
throw Error('No finance company found. Please create first.');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/*Using the result returned, instantiate a new FinanceCompany*/
|
|
187
|
+
const compSystemCode = company.CompSystemCode;
|
|
188
|
+
const compSystemRefId = company.CompSystemRefId;
|
|
189
|
+
const accSystemCode = company.AccSystemCode;
|
|
190
|
+
|
|
191
|
+
const financeCompany = new FinanceCompany(
|
|
192
|
+
compSystemCode,
|
|
193
|
+
compSystemRefId,
|
|
194
|
+
accSystemCode,
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
/*Retrieve and store the CompanyId from the result*/
|
|
198
|
+
financeCompany.ObjectId = company.CompanyId;
|
|
199
|
+
financeCompany._CompanyId = company.CompanyId;
|
|
200
|
+
|
|
201
|
+
/*Add the details into the hashtable*/
|
|
202
|
+
const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
|
|
203
|
+
FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
|
|
204
|
+
FinanceCompany._htFinanceCompanies.add(
|
|
205
|
+
financeCompany.ObjectId,
|
|
206
|
+
financeCompany,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
// tslint:disable-next-line:no-console
|
|
210
|
+
console.log('return from hash table');
|
|
211
|
+
return FinanceCompany._htFinanceCompanies.get(companyId);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
static async createFinanceCompany(
|
|
215
|
+
dbTransaction: any,
|
|
216
|
+
loginUser: LoginUserBase,
|
|
217
|
+
companyId: string,
|
|
218
|
+
compSystemCode: string,
|
|
219
|
+
compSystemRefId: string,
|
|
220
|
+
accSystemCode: string,
|
|
221
|
+
) {
|
|
222
|
+
/*Instantiate a new FinanceCompany*/
|
|
223
|
+
const financeCompany = new FinanceCompany(
|
|
224
|
+
compSystemCode,
|
|
225
|
+
compSystemRefId,
|
|
226
|
+
accSystemCode,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
/*Validating if the finance company already exists in finance_Company*/
|
|
230
|
+
const company = await FinanceCompany._financeCompanyRepository.findOne({
|
|
231
|
+
where: {
|
|
232
|
+
CompSystemCode: compSystemCode,
|
|
233
|
+
CompSystemRefId: compSystemRefId,
|
|
234
|
+
AccSystemCode: accSystemCode,
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
if (company) {
|
|
239
|
+
throw Error(
|
|
240
|
+
'There is already another Finance Company with the compSystemCode, CompSystemRefId and accSystemCode specified.',
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/*Generating the companyId*/
|
|
245
|
+
financeCompany.ObjectId = companyId;
|
|
246
|
+
|
|
247
|
+
/*Save the FinanceCompany to the finance_Company table*/
|
|
248
|
+
await FinanceCompany._financeCompanyRepository.create(
|
|
249
|
+
{
|
|
250
|
+
CompanyId: financeCompany.CompanyId,
|
|
251
|
+
CompSystemCode: financeCompany.CompSystemCode,
|
|
252
|
+
CompSystemRefId: financeCompany.CompSystemRefId,
|
|
253
|
+
AccSystemCode: financeCompany.AccSystemCode,
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
transaction: dbTransaction,
|
|
257
|
+
},
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
/*Add the details to hashtable*/
|
|
261
|
+
const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
|
|
262
|
+
FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
|
|
263
|
+
FinanceCompany._htFinanceCompanies.add(
|
|
264
|
+
financeCompany.ObjectId,
|
|
265
|
+
financeCompany,
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
// tslint:disable-next-line:no-console
|
|
269
|
+
console.log('return from hash table');
|
|
270
|
+
return FinanceCompany._htFinanceCompanies.get(companyId);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async createCustomer(
|
|
274
|
+
dbTransaction: any,
|
|
275
|
+
custSystemCode: string,
|
|
276
|
+
custSystemRefId: string,
|
|
277
|
+
customer: FinanceCustomerBase,
|
|
278
|
+
) {
|
|
279
|
+
try {
|
|
280
|
+
if (!custSystemCode || !custSystemRefId) {
|
|
281
|
+
throw new Error(
|
|
282
|
+
'CustSystemCode and CustomerRefId are required fields.',
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const financeCustomerData =
|
|
287
|
+
await FinanceCompany._FinanceCustomerRepository.findOne({
|
|
288
|
+
where: {
|
|
289
|
+
CompanyId: this._CompanyId,
|
|
290
|
+
CustSystemCode: custSystemCode,
|
|
291
|
+
CustSystemRefId: custSystemRefId,
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
if (financeCustomerData) {
|
|
296
|
+
throw new Error('Customer already created previously.');
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Retrieve the type of accounting system, API Key, and API secret based on SysCode from the Config.ts
|
|
300
|
+
const customerId = await this.AccountingSystem.createCustomer({
|
|
301
|
+
customer,
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
customer.CompanyId = this._CompanyId;
|
|
305
|
+
|
|
306
|
+
const newCustomer = await customer.save(
|
|
307
|
+
customerId,
|
|
308
|
+
custSystemCode,
|
|
309
|
+
custSystemRefId,
|
|
310
|
+
dbTransaction,
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
const payload = {
|
|
314
|
+
Action: 'Create',
|
|
315
|
+
Activity: 'Finance Customer Created',
|
|
316
|
+
Description: `Customer (ID: ${newCustomer.CustomerId}) has been created for ${this.AccSystemCode}`,
|
|
317
|
+
EntityType: 'finance_Customer',
|
|
318
|
+
EntityValueBefore: JSON.stringify({}),
|
|
319
|
+
EntityValueAfter: JSON.stringify(newCustomer),
|
|
320
|
+
PerformedById: 'test',
|
|
321
|
+
PerformedAt: new Date(),
|
|
322
|
+
EntityId: newCustomer.CustomerId,
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
await axios.post(
|
|
326
|
+
`${process.env.COMMON_API_URL}/activity-histories`,
|
|
327
|
+
payload,
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
return customer;
|
|
331
|
+
} catch (error) {
|
|
332
|
+
throw error;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async postJournal(dbTransaction: any, journalEntry: JournalEntry) {
|
|
337
|
+
const debitTransactions = await journalEntry.DebitTransactions;
|
|
338
|
+
const creditTransactions = await journalEntry.CreditTransactions;
|
|
339
|
+
try {
|
|
340
|
+
if (creditTransactions.length < 1 || debitTransactions?.length < 1) {
|
|
341
|
+
throw new Error(
|
|
342
|
+
'There should be at least 1 debit ledger transaction and 1 credit ledger transaction in the journal entry',
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const totalCreditAmount = creditTransactions.reduce(
|
|
347
|
+
(accumulator, currentValue) => accumulator + currentValue.CreditAmount,
|
|
348
|
+
0,
|
|
349
|
+
);
|
|
350
|
+
const totalDebitAmount = debitTransactions.reduce(
|
|
351
|
+
(accumulator, currentValue) => accumulator + currentValue.DebitAmount,
|
|
352
|
+
0,
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
if (totalCreditAmount !== totalDebitAmount) {
|
|
356
|
+
throw new Error(
|
|
357
|
+
'Credit ledger transaction and debit ledger transaction should the same amount',
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const newJournalEntry = await journalEntry.save('test', dbTransaction);
|
|
362
|
+
|
|
363
|
+
for (const ledgerTransaction of debitTransactions) {
|
|
364
|
+
ledgerTransaction.TransactionId = cuid();
|
|
365
|
+
ledgerTransaction.JournalEntryId = newJournalEntry.JournalEntryId;
|
|
366
|
+
|
|
367
|
+
await ledgerTransaction.save(dbTransaction);
|
|
368
|
+
// const dt = await ledgerTransaction.newLedgerTransaction(
|
|
369
|
+
// TransactionTypeOptions.DEBIT,
|
|
370
|
+
// newJournalEntry.JournalEntryId,
|
|
371
|
+
// );
|
|
372
|
+
// await dt.save();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
for (const ledgerTransaction of creditTransactions) {
|
|
376
|
+
ledgerTransaction.TransactionId = cuid();
|
|
377
|
+
ledgerTransaction.JournalEntryId = newJournalEntry.JournalEntryId;
|
|
378
|
+
|
|
379
|
+
await ledgerTransaction.save(dbTransaction);
|
|
380
|
+
// const ct = await ledgerTransaction.newLedgerTransaction(
|
|
381
|
+
// TransactionTypeOptions.CREDIT,
|
|
382
|
+
// newJournalEntry.JournalEntryId,
|
|
383
|
+
// );
|
|
384
|
+
// await ct.save();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
await this.AccountingSystem.postJournalEntry(newJournalEntry);
|
|
388
|
+
|
|
389
|
+
const payload = {
|
|
390
|
+
Action: 'Create',
|
|
391
|
+
Activity: 'Post Journal Entry',
|
|
392
|
+
Description: `Journal Entry (ID: ${newJournalEntry.JournalEntryId}) has been created`,
|
|
393
|
+
EntityType: 'JournalEntry',
|
|
394
|
+
EntityValueBefore: JSON.stringify({}),
|
|
395
|
+
EntityValueAfter: JSON.stringify(newJournalEntry),
|
|
396
|
+
PerformedById: 'test',
|
|
397
|
+
PerformedAt: new Date(),
|
|
398
|
+
EntityId: newJournalEntry.JournalEntryId,
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
await axios.post(
|
|
402
|
+
`${process.env.COMMON_API_URL}/activity-histories`,
|
|
403
|
+
payload,
|
|
404
|
+
);
|
|
405
|
+
} catch (error) {
|
|
406
|
+
throw error;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async createAccount(dbTransaction: any, account: Account) {
|
|
411
|
+
try {
|
|
412
|
+
if (!account.AccountType) {
|
|
413
|
+
throw new Error('AccountType is required.');
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
let createAccountPayload: any = {
|
|
417
|
+
Name: account.Name,
|
|
418
|
+
AcctNum: account.AccountNo,
|
|
419
|
+
AccountType: account.AccountType,
|
|
420
|
+
AccountSubType: account.AccountSubtype,
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
if (account.isParentAccountExists()) {
|
|
424
|
+
createAccountPayload = {
|
|
425
|
+
...createAccountPayload,
|
|
426
|
+
CurrencyRef: 'MYR',
|
|
427
|
+
ParentRef: account.ParentAccountNo,
|
|
428
|
+
SubAccount: true,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const accSystemAccountId = await this.AccountingSystem.createAccount(
|
|
433
|
+
createAccountPayload,
|
|
434
|
+
);
|
|
435
|
+
account.PostedToAccSystemYN = 'Y';
|
|
436
|
+
const newAccount = await account.save(
|
|
437
|
+
this.CompanyId,
|
|
438
|
+
accSystemAccountId,
|
|
439
|
+
'test',
|
|
440
|
+
dbTransaction,
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
const payload = {
|
|
444
|
+
Action: 'Create',
|
|
445
|
+
Activity: 'Account Created',
|
|
446
|
+
Description: `Account (ID: ${newAccount.AccountNo}) has been created`,
|
|
447
|
+
EntityType: 'Account',
|
|
448
|
+
EntityValueBefore: JSON.stringify({}),
|
|
449
|
+
EntityValueAfter: JSON.stringify(newAccount),
|
|
450
|
+
PerformedById: 'test',
|
|
451
|
+
PerformedAt: new Date(),
|
|
452
|
+
EntityId: newAccount.AccountNo,
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
await axios.post(
|
|
456
|
+
`${process.env.COMMON_API_URL}/activity-histories`,
|
|
457
|
+
payload,
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
return account;
|
|
461
|
+
} catch (error) {
|
|
462
|
+
throw error;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Issue an invoice
|
|
468
|
+
*
|
|
469
|
+
* @param dbTransaction - The database transaction to be used
|
|
470
|
+
* @param loginUser - The user issuing the invoice
|
|
471
|
+
* @param invoice - The document containing the invoice details
|
|
472
|
+
* @param customer - The customer to be issued the invoice
|
|
473
|
+
* @param dtAccountNo - The account number of the Account Receivable (AR) account to debit. If not provided, will debit the customer's default AR account
|
|
474
|
+
*
|
|
475
|
+
* @returns {Document} - Document object representing the full details of the invoice issued
|
|
476
|
+
*/
|
|
477
|
+
async issueInvoice(
|
|
478
|
+
/*todo: loginUser & customer is NOT supposed to be optional */
|
|
479
|
+
dbTransaction: any,
|
|
480
|
+
invoice: Document,
|
|
481
|
+
loginUser?: LoginUserBase,
|
|
482
|
+
customer?: FinanceCustomerBase,
|
|
483
|
+
dtAccountNo?: string,
|
|
484
|
+
): Promise<Document> {
|
|
485
|
+
try {
|
|
486
|
+
/*Check if the invoice number already exists (should be unique)*/
|
|
487
|
+
const duplicateInvoice = await FinanceCompany._DocumentRepository.findOne(
|
|
488
|
+
{
|
|
489
|
+
where: {
|
|
490
|
+
DocNo: invoice.DocNo,
|
|
491
|
+
},
|
|
492
|
+
transaction: dbTransaction,
|
|
493
|
+
},
|
|
494
|
+
);
|
|
495
|
+
|
|
496
|
+
if (duplicateInvoice) {
|
|
497
|
+
throw new Error('Invoice number already exists');
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const documentItems = await invoice.DocumentItems;
|
|
501
|
+
|
|
502
|
+
/*Check if the document has at least 1 document item*/
|
|
503
|
+
if (!documentItems.length) {
|
|
504
|
+
throw new Error('Document must have at least 1 document item');
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/*Check if each document item has CtAccountNo provided*/
|
|
508
|
+
for (const invoiceItem of documentItems) {
|
|
509
|
+
if (!invoiceItem.CtAccountNo) {
|
|
510
|
+
throw new Error(
|
|
511
|
+
'Each document item should have CtAccountNo provided',
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/*Set up the document type*/
|
|
517
|
+
invoice.DocType = DocType.INVOICE;
|
|
518
|
+
// invoice.DocNo = cuid();
|
|
519
|
+
|
|
520
|
+
/*Saving the document and document items to the database*/
|
|
521
|
+
await FinanceCompany._DocumentRepository.create(
|
|
522
|
+
{
|
|
523
|
+
DocNo: invoice.DocNo,
|
|
524
|
+
DocType: invoice.DocType,
|
|
525
|
+
DocDate: invoice.DocDate,
|
|
526
|
+
CompanyId: invoice.CompanyId,
|
|
527
|
+
Currency: invoice.Currency,
|
|
528
|
+
Amount: invoice.Amount,
|
|
529
|
+
Description: invoice.Description,
|
|
530
|
+
Status: invoice.Status,
|
|
531
|
+
IssuedById: invoice.IssuedById,
|
|
532
|
+
IssuedToId: invoice.IssuedToId,
|
|
533
|
+
IssuedToType: invoice.IssuedToType,
|
|
534
|
+
RelatedObjectId: invoice.RelatedObjectId,
|
|
535
|
+
RelatedObjectType: invoice.RelatedObjectType,
|
|
536
|
+
CreatedById: invoice.CreatedById,
|
|
537
|
+
CreatedAt: new Date(),
|
|
538
|
+
UpdatedById: invoice.UpdatedById,
|
|
539
|
+
UpdatedAt: new Date(),
|
|
540
|
+
DocPDFFileMediaId: invoice.DocPDFFileMediaId,
|
|
541
|
+
DocHTMLFileMediaId: invoice.DocHTMLFileMediaId,
|
|
542
|
+
AccSystemRefId: invoice.AccSystemRefId,
|
|
543
|
+
PostedToAccSystemYN: invoice.PostedToAccSystemYN,
|
|
544
|
+
PostedById: invoice.PostedById,
|
|
545
|
+
PostedDateTime: new Date(),
|
|
546
|
+
UseAccSystemDocYN: invoice.UseAccSystemDocYN,
|
|
547
|
+
},
|
|
548
|
+
{
|
|
549
|
+
transaction: dbTransaction,
|
|
550
|
+
},
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
for (const documentItem of documentItems) {
|
|
554
|
+
await FinanceCompany._DocumentItemRepository.create(
|
|
555
|
+
{
|
|
556
|
+
DocumentItemId: cuid(),
|
|
557
|
+
DocNo: invoice.DocNo,
|
|
558
|
+
Name: documentItem.Name,
|
|
559
|
+
NameBM: documentItem.NameBM,
|
|
560
|
+
Description: documentItem.Description,
|
|
561
|
+
ItemId: documentItem.ItemId,
|
|
562
|
+
ItemType: documentItem.ItemType,
|
|
563
|
+
ItemSKU: documentItem.ItemSKU,
|
|
564
|
+
ItemSerialNo: documentItem.ItemSerialNo,
|
|
565
|
+
Currency: documentItem.Currency,
|
|
566
|
+
UnitPrice: documentItem.UnitPrice,
|
|
567
|
+
Quantity: documentItem.Quantity,
|
|
568
|
+
QuantityUOM: documentItem.QuantityUOM,
|
|
569
|
+
Amount: documentItem.Amount,
|
|
570
|
+
TaxCode: documentItem.TaxCode,
|
|
571
|
+
TaxAmount: documentItem.TaxAmount,
|
|
572
|
+
TaxRate: documentItem.TaxRate,
|
|
573
|
+
TaxInclusiveYN: documentItem.TaxInclusiveYN,
|
|
574
|
+
DtAccountNo: documentItem.DtAccountNo
|
|
575
|
+
? documentItem.DtAccountNo
|
|
576
|
+
: null,
|
|
577
|
+
CtAccountNo: documentItem.CtAccountNo
|
|
578
|
+
? documentItem.CtAccountNo
|
|
579
|
+
: null,
|
|
580
|
+
},
|
|
581
|
+
{
|
|
582
|
+
transaction: dbTransaction,
|
|
583
|
+
},
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/*Generating the invoice*/
|
|
588
|
+
if (invoice.UseAccSystemDocYN === 'Y') {
|
|
589
|
+
/*todo: Posting to accounting system to generate invoice*/
|
|
590
|
+
await this.AccountingSystem.createInvoice(invoice);
|
|
591
|
+
} else {
|
|
592
|
+
/*todo: check config file to see which invoice template is to be used for specific project*/
|
|
593
|
+
|
|
594
|
+
/*Generating invoice based on template*/
|
|
595
|
+
invoice.generateInvoice(invoice.IssuedById, customer);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const transactionDate = new Date();
|
|
599
|
+
const htCreditAccountAmount = new HashTable();
|
|
600
|
+
const htCreditAccountCurrency = new HashTable();
|
|
601
|
+
|
|
602
|
+
documentItems.forEach((invoiceItem) => {
|
|
603
|
+
if (!htCreditAccountAmount.exists(invoiceItem.CtAccountNo)) {
|
|
604
|
+
//add the credit account to the hash table
|
|
605
|
+
htCreditAccountAmount.add(
|
|
606
|
+
invoiceItem.CtAccountNo,
|
|
607
|
+
invoiceItem.Amount,
|
|
608
|
+
);
|
|
609
|
+
|
|
610
|
+
htCreditAccountCurrency.add(
|
|
611
|
+
invoiceItem.CtAccountNo,
|
|
612
|
+
invoiceItem.Currency,
|
|
613
|
+
);
|
|
614
|
+
} else {
|
|
615
|
+
//update the credit account amount
|
|
616
|
+
const d = htCreditAccountAmount.get(invoiceItem.CtAccountNo);
|
|
617
|
+
htCreditAccountAmount.add(
|
|
618
|
+
invoiceItem.CtAccountNo,
|
|
619
|
+
d + invoiceItem.Amount,
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
const savedItems = htCreditAccountAmount.list();
|
|
625
|
+
|
|
626
|
+
for (let i = 0; i < savedItems.length; i++) {
|
|
627
|
+
const journalEntry = new JournalEntry(dbTransaction);
|
|
628
|
+
//Temporary fix to successfully save journal entry in PostJournal
|
|
629
|
+
journalEntry.init({
|
|
630
|
+
CompanyId: this.CompanyId,
|
|
631
|
+
Name: 'Issue Invoice ' + invoice.DocNo,
|
|
632
|
+
});
|
|
633
|
+
const account = await Account.initAccount(
|
|
634
|
+
dbTransaction,
|
|
635
|
+
savedItems[i].value[0],
|
|
636
|
+
);
|
|
637
|
+
const creditAmount = savedItems[i].value[1];
|
|
638
|
+
const currency = htCreditAccountCurrency.get(savedItems[i].value[0]);
|
|
639
|
+
|
|
640
|
+
const dt = await journalEntry.newLedgerTransaction(
|
|
641
|
+
TransactionTypeOptions.DEBIT,
|
|
642
|
+
);
|
|
643
|
+
|
|
644
|
+
if (dtAccountNo) {
|
|
645
|
+
/*Transacting using AR account provided*/
|
|
646
|
+
dt.AccountNo = dtAccountNo;
|
|
647
|
+
} else {
|
|
648
|
+
/*Transacting based on default customer AR account*/
|
|
649
|
+
dt.AccountNo = (await customer.AccountReceivable).AccountNo;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
dt.Currency = currency ? currency : 'MYR';
|
|
653
|
+
dt.DebitAmount = creditAmount ? creditAmount : 0.0;
|
|
654
|
+
dt.Date = transactionDate;
|
|
655
|
+
dt.Description = account.Name;
|
|
656
|
+
dt.Name = account.Name;
|
|
657
|
+
dt.RelatedDocNo = invoice.DocNo;
|
|
658
|
+
|
|
659
|
+
const ct = await journalEntry.newLedgerTransaction(
|
|
660
|
+
TransactionTypeOptions.CREDIT,
|
|
661
|
+
);
|
|
662
|
+
ct.AccountNo = savedItems[i].value[0];
|
|
663
|
+
ct.Currency = currency ? currency : 'MYR';
|
|
664
|
+
ct.CreditAmount = creditAmount ? creditAmount : 0.0;
|
|
665
|
+
ct.Date = transactionDate;
|
|
666
|
+
ct.Description = account.Name;
|
|
667
|
+
ct.Name = account.Name;
|
|
668
|
+
ct.RelatedDocNo = invoice.DocNo;
|
|
669
|
+
|
|
670
|
+
await this.postJournal(dbTransaction, journalEntry);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
return invoice;
|
|
674
|
+
} catch (err) {
|
|
675
|
+
// tslint:disable-next-line:no-console
|
|
676
|
+
console.log('Issue invoice err: ', err);
|
|
677
|
+
throw err;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Issue a debit note
|
|
683
|
+
*
|
|
684
|
+
* @param dbTransaction - The database transaction to be used
|
|
685
|
+
* @param loginUser - The user issuing the invoice
|
|
686
|
+
* @param invoice - The document containing the invoice details
|
|
687
|
+
* @param customer - The customer to be issued the invoice
|
|
688
|
+
* @param dtAccountNo - The account number of the Account Receivable (AR) account to debit. If not provided, will debit the customer's default AR account
|
|
689
|
+
*
|
|
690
|
+
* @returns {Document} - Document object representing the full details of the invoice issued
|
|
691
|
+
*/
|
|
692
|
+
async issueDebitNote(
|
|
693
|
+
dbTransaction: any,
|
|
694
|
+
loginUser: LoginUserBase,
|
|
695
|
+
invoice: Document,
|
|
696
|
+
customer: FinanceCustomerBase,
|
|
697
|
+
dtAccountNo?: string,
|
|
698
|
+
): Promise<Document> {
|
|
699
|
+
try {
|
|
700
|
+
/*Check if the invoice number already exists (should be unique)*/
|
|
701
|
+
const duplicateInvoice = await FinanceCompany._DocumentRepository.findOne(
|
|
702
|
+
{
|
|
703
|
+
where: {
|
|
704
|
+
DocNo: invoice.DocNo,
|
|
705
|
+
},
|
|
706
|
+
transaction: dbTransaction,
|
|
707
|
+
},
|
|
708
|
+
);
|
|
709
|
+
|
|
710
|
+
if (duplicateInvoice) {
|
|
711
|
+
throw new Error('Invoice number already exists');
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
const documentItems = await invoice.DocumentItems;
|
|
715
|
+
|
|
716
|
+
/*Check if the document has at least 1 document item*/
|
|
717
|
+
if (!documentItems.length) {
|
|
718
|
+
throw new Error('Document must have at least 1 document item');
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/*Check if each document item has CtAccountNo provided*/
|
|
722
|
+
for (const invoiceItem of documentItems) {
|
|
723
|
+
if (!invoiceItem.CtAccountNo) {
|
|
724
|
+
throw new Error(
|
|
725
|
+
'Each document item should have CtAccountNo provided',
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/*Set up the document type*/
|
|
731
|
+
invoice.DocType = DocType.DEBIT_NOTE;
|
|
732
|
+
// invoice.DocNo = cuid();
|
|
733
|
+
|
|
734
|
+
/*Saving the document and document items to the database*/
|
|
735
|
+
await FinanceCompany._DocumentRepository.create(
|
|
736
|
+
{
|
|
737
|
+
DocNo: invoice.DocNo,
|
|
738
|
+
DocType: invoice.DocType,
|
|
739
|
+
DocDate: invoice.DocDate,
|
|
740
|
+
CompanyId: invoice.CompanyId,
|
|
741
|
+
Currency: invoice.Currency,
|
|
742
|
+
Amount: invoice.Amount,
|
|
743
|
+
Description: invoice.Description,
|
|
744
|
+
Status: invoice.Status,
|
|
745
|
+
IssuedById: loginUser.ObjectId,
|
|
746
|
+
IssuedToId: customer.ObjectId,
|
|
747
|
+
IssuedToType: invoice.IssuedToType,
|
|
748
|
+
RelatedObjectId: invoice.RelatedObjectId,
|
|
749
|
+
RelatedObjectType: invoice.RelatedObjectType,
|
|
750
|
+
CreatedById: loginUser.ObjectId,
|
|
751
|
+
CreatedAt: new Date(),
|
|
752
|
+
UpdatedById: loginUser.ObjectId,
|
|
753
|
+
UpdatedAt: new Date(),
|
|
754
|
+
DocPDFFileMediaId: invoice.DocPDFFileMediaId,
|
|
755
|
+
DocHTMLFileMediaId: invoice.DocHTMLFileMediaId,
|
|
756
|
+
AccSystemRefId: invoice.AccSystemRefId,
|
|
757
|
+
PostedToAccSystemYN: invoice.PostedToAccSystemYN,
|
|
758
|
+
PostedById: invoice.PostedById,
|
|
759
|
+
PostedDateTime: new Date(),
|
|
760
|
+
UseAccSystemDocYN: invoice.UseAccSystemDocYN,
|
|
761
|
+
},
|
|
762
|
+
{
|
|
763
|
+
transaction: dbTransaction,
|
|
764
|
+
},
|
|
765
|
+
);
|
|
766
|
+
|
|
767
|
+
documentItems.forEach(async (documentItem) => {
|
|
768
|
+
await FinanceCompany._DocumentItemRepository.create(
|
|
769
|
+
{
|
|
770
|
+
DocumentItemId: cuid(),
|
|
771
|
+
DocNo: documentItem.DocNo,
|
|
772
|
+
Name: documentItem.Name,
|
|
773
|
+
NameBM: documentItem.NameBM,
|
|
774
|
+
Description: documentItem.Description,
|
|
775
|
+
ItemId: documentItem.ItemId,
|
|
776
|
+
ItemType: documentItem.ItemType,
|
|
777
|
+
ItemSKU: documentItem.ItemSKU,
|
|
778
|
+
ItemSerialNo: documentItem.ItemSerialNo,
|
|
779
|
+
Currency: documentItem.Currency,
|
|
780
|
+
UnitPrice: documentItem.UnitPrice,
|
|
781
|
+
Quantity: documentItem.Quantity,
|
|
782
|
+
QuantityUOM: documentItem.QuantityUOM,
|
|
783
|
+
Amount: documentItem.Amount,
|
|
784
|
+
TaxCode: documentItem.TaxCode,
|
|
785
|
+
TaxAmount: documentItem.TaxAmount,
|
|
786
|
+
TaxRate: documentItem.TaxRate,
|
|
787
|
+
TaxInclusiveYN: documentItem.TaxInclusiveYN,
|
|
788
|
+
DtAccountNo: documentItem.DtAccountNo,
|
|
789
|
+
CtAccountNo: documentItem.CtAccountNo,
|
|
790
|
+
},
|
|
791
|
+
{
|
|
792
|
+
transaction: dbTransaction,
|
|
793
|
+
},
|
|
794
|
+
);
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
/*Generating the invoice*/
|
|
798
|
+
if (invoice.UseAccSystemDocYN === 'Y') {
|
|
799
|
+
/*todo: Posting to accounting system to generate invoice*/
|
|
800
|
+
await this.AccountingSystem.createInvoice(invoice);
|
|
801
|
+
} else {
|
|
802
|
+
/*todo: check config file to see which invoice template is to be used for specific project*/
|
|
803
|
+
|
|
804
|
+
/*Generating invoice based on template*/
|
|
805
|
+
invoice.generateInvoice(loginUser.IDNo, customer);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
const transactionDate = new Date();
|
|
809
|
+
const htCreditAccountAmount = new HashTable();
|
|
810
|
+
const htCreditAccountCurrency = new HashTable();
|
|
811
|
+
|
|
812
|
+
documentItems.forEach((invoiceItem) => {
|
|
813
|
+
if (!htCreditAccountAmount.exists(invoiceItem.CtAccountNo)) {
|
|
814
|
+
//add the credit account to the hash table
|
|
815
|
+
htCreditAccountAmount.add(
|
|
816
|
+
invoiceItem.CtAccountNo,
|
|
817
|
+
invoiceItem.Amount,
|
|
818
|
+
);
|
|
819
|
+
|
|
820
|
+
htCreditAccountCurrency.add(
|
|
821
|
+
invoiceItem.CtAccountNo,
|
|
822
|
+
invoiceItem.Currency,
|
|
823
|
+
);
|
|
824
|
+
} else {
|
|
825
|
+
//update the credit account amount
|
|
826
|
+
const d = htCreditAccountAmount.get(invoiceItem.CtAccountNo);
|
|
827
|
+
htCreditAccountAmount.add(
|
|
828
|
+
invoiceItem.CtAccountNo,
|
|
829
|
+
d + invoiceItem.Amount,
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
});
|
|
833
|
+
|
|
834
|
+
const savedItems = htCreditAccountAmount.list();
|
|
835
|
+
|
|
836
|
+
for (let i = 0; i < savedItems.length; i++) {
|
|
837
|
+
const journalEntry = new JournalEntry(dbTransaction);
|
|
838
|
+
//Temporary fix to successfully save journal entry in PostJournal
|
|
839
|
+
journalEntry.init({
|
|
840
|
+
CompanyId: this.CompanyId,
|
|
841
|
+
Name: 'issue Invoice ' + invoice.DocNo,
|
|
842
|
+
});
|
|
843
|
+
const account = await Account.initAccount(
|
|
844
|
+
dbTransaction,
|
|
845
|
+
savedItems[i].value[0],
|
|
846
|
+
);
|
|
847
|
+
const creditAmount = savedItems[i].value[1];
|
|
848
|
+
const currency = htCreditAccountCurrency.get(savedItems[i].value[0]);
|
|
849
|
+
|
|
850
|
+
const dt = await journalEntry.newLedgerTransaction(
|
|
851
|
+
TransactionTypeOptions.DEBIT,
|
|
852
|
+
);
|
|
853
|
+
|
|
854
|
+
if (dtAccountNo) {
|
|
855
|
+
/*Transacting using AR account provided*/
|
|
856
|
+
dt.AccountNo = dtAccountNo;
|
|
857
|
+
} else {
|
|
858
|
+
/*Transacting based on default customer AR account*/
|
|
859
|
+
dt.AccountNo = (await customer.AccountReceivable).AccountNo;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
dt.Currency = currency ? currency : 'MYR';
|
|
863
|
+
dt.DebitAmount = creditAmount ? creditAmount : 0.0;
|
|
864
|
+
dt.Date = transactionDate;
|
|
865
|
+
dt.Description = account.Name;
|
|
866
|
+
dt.Name = account.Name;
|
|
867
|
+
dt.RelatedDocNo = invoice.DocNo;
|
|
868
|
+
|
|
869
|
+
const ct = await journalEntry.newLedgerTransaction(
|
|
870
|
+
TransactionTypeOptions.CREDIT,
|
|
871
|
+
);
|
|
872
|
+
ct.AccountNo = savedItems[i].key;
|
|
873
|
+
ct.Currency = currency ? currency : 'MYR';
|
|
874
|
+
ct.CreditAmount = creditAmount ? creditAmount : 0.0;
|
|
875
|
+
ct.Date = transactionDate;
|
|
876
|
+
ct.Description = account.Name;
|
|
877
|
+
ct.Name = account.Name;
|
|
878
|
+
ct.RelatedDocNo = invoice.DocNo;
|
|
879
|
+
|
|
880
|
+
await this.postJournal(dbTransaction, journalEntry);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
return invoice;
|
|
884
|
+
} catch (err) {
|
|
885
|
+
// tslint:disable-next-line:no-console
|
|
886
|
+
console.log('Issue debit note err: ', err);
|
|
887
|
+
throw err;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Issue a credit note
|
|
893
|
+
*
|
|
894
|
+
* @param dbTransaction - The database transaction to be used
|
|
895
|
+
* @param loginUser - The user issuing the credit note
|
|
896
|
+
* @param creditNote - The document containing the credit note details
|
|
897
|
+
* @param customer - The customer to be issued the credit note
|
|
898
|
+
* @param ctAccountNo - The account number of the Account Payable (AP) account to debit. If not provided, will debit the customer's default AR account
|
|
899
|
+
*
|
|
900
|
+
* @returns {Document} - Document object representing the full details of the invoice issued
|
|
901
|
+
*/
|
|
902
|
+
async issueCreditNote(
|
|
903
|
+
dbTransaction: any,
|
|
904
|
+
loginUser: LoginUserBase,
|
|
905
|
+
creditNote: Document,
|
|
906
|
+
customer: FinanceCustomerBase,
|
|
907
|
+
ctAccountNo?: string,
|
|
908
|
+
): Promise<Document> {
|
|
909
|
+
try {
|
|
910
|
+
/*Check if the invoice number already exists (should be unique)*/
|
|
911
|
+
const duplicateCreditNote =
|
|
912
|
+
await FinanceCompany._DocumentRepository.findOne({
|
|
913
|
+
where: {
|
|
914
|
+
DocNo: creditNote.DocNo,
|
|
915
|
+
},
|
|
916
|
+
transaction: dbTransaction,
|
|
917
|
+
});
|
|
918
|
+
|
|
919
|
+
if (duplicateCreditNote) {
|
|
920
|
+
throw new Error('Invoice number already exists');
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
const documentItems = await creditNote.DocumentItems;
|
|
924
|
+
|
|
925
|
+
/*Check if the document has at least 1 document item*/
|
|
926
|
+
if (!documentItems.length) {
|
|
927
|
+
throw new Error('Document must have at least 1 document item');
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/*Check if each document item has CtAccountNo provided*/
|
|
931
|
+
for (const invoiceItem of documentItems) {
|
|
932
|
+
if (!invoiceItem.CtAccountNo) {
|
|
933
|
+
throw new Error(
|
|
934
|
+
'Each document item should have CtAccountNo provided',
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/*Set up the document type*/
|
|
940
|
+
creditNote.DocType = DocType.DEBIT_NOTE;
|
|
941
|
+
|
|
942
|
+
/*Saving the document and document items to the database*/
|
|
943
|
+
await FinanceCompany._DocumentRepository.create(
|
|
944
|
+
{
|
|
945
|
+
DocNo: creditNote.DocNo,
|
|
946
|
+
DocType: creditNote.DocType,
|
|
947
|
+
DocDate: creditNote.DocDate,
|
|
948
|
+
CompanyId: creditNote.CompanyId,
|
|
949
|
+
Currency: creditNote.Currency,
|
|
950
|
+
Amount: creditNote.Amount,
|
|
951
|
+
Description: creditNote.Description,
|
|
952
|
+
Status: creditNote.Status,
|
|
953
|
+
IssuedById: loginUser.ObjectId,
|
|
954
|
+
IssuedToId: customer.ObjectId,
|
|
955
|
+
IssuedToType: creditNote.IssuedToType,
|
|
956
|
+
RelatedObjectId: creditNote.RelatedObjectId,
|
|
957
|
+
RelatedObjectType: creditNote.RelatedObjectType,
|
|
958
|
+
CreatedById: loginUser.ObjectId,
|
|
959
|
+
CreatedAt: new Date(),
|
|
960
|
+
UpdatedById: loginUser.ObjectId,
|
|
961
|
+
UpdatedAt: new Date(),
|
|
962
|
+
DocPDFFileMediaId: creditNote.DocPDFFileMediaId,
|
|
963
|
+
DocHTMLFileMediaId: creditNote.DocHTMLFileMediaId,
|
|
964
|
+
AccSystemRefId: creditNote.AccSystemRefId,
|
|
965
|
+
PostedToAccSystemYN: creditNote.PostedToAccSystemYN,
|
|
966
|
+
PostedById: creditNote.PostedById,
|
|
967
|
+
PostedDateTime: new Date(),
|
|
968
|
+
UseAccSystemDocYN: creditNote.UseAccSystemDocYN,
|
|
969
|
+
},
|
|
970
|
+
{
|
|
971
|
+
transaction: dbTransaction,
|
|
972
|
+
},
|
|
973
|
+
);
|
|
974
|
+
|
|
975
|
+
documentItems.forEach(async (documentItem) => {
|
|
976
|
+
await FinanceCompany._DocumentItemRepository.create(
|
|
977
|
+
{
|
|
978
|
+
DocumentItemId: documentItem.DocumentItemId,
|
|
979
|
+
DocNo: documentItem.DocNo,
|
|
980
|
+
Name: documentItem.Name,
|
|
981
|
+
NameBM: documentItem.NameBM,
|
|
982
|
+
Description: documentItem.Description,
|
|
983
|
+
ItemId: documentItem.ItemId,
|
|
984
|
+
ItemType: documentItem.ItemType,
|
|
985
|
+
ItemSKU: documentItem.ItemSKU,
|
|
986
|
+
ItemSerialNo: documentItem.ItemSerialNo,
|
|
987
|
+
Currency: documentItem.Currency,
|
|
988
|
+
UnitPrice: documentItem.UnitPrice,
|
|
989
|
+
Quantity: documentItem.Quantity,
|
|
990
|
+
QuantityUOM: documentItem.QuantityUOM,
|
|
991
|
+
Amount: documentItem.Amount,
|
|
992
|
+
TaxCode: documentItem.TaxCode,
|
|
993
|
+
TaxAmount: documentItem.TaxAmount,
|
|
994
|
+
TaxRate: documentItem.TaxRate,
|
|
995
|
+
TaxInclusiveYN: documentItem.TaxInclusiveYN,
|
|
996
|
+
DtAccountNo: documentItem.DtAccountNo,
|
|
997
|
+
CtAccountNo: documentItem.CtAccountNo,
|
|
998
|
+
},
|
|
999
|
+
{
|
|
1000
|
+
transaction: dbTransaction,
|
|
1001
|
+
},
|
|
1002
|
+
);
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
/*Generating the credit note*/
|
|
1006
|
+
if (creditNote.UseAccSystemDocYN === 'Y') {
|
|
1007
|
+
/*todo: Posting to accounting system to generate creditNote*/
|
|
1008
|
+
await this.AccountingSystem.createCreditNote(creditNote);
|
|
1009
|
+
} else {
|
|
1010
|
+
/*todo: check config file to see which invoice template is to be used for specific project*/
|
|
1011
|
+
|
|
1012
|
+
/*Generating credit note based on template*/
|
|
1013
|
+
creditNote.generateCreditNote(loginUser.IDNo, customer);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
const journalEntry = new JournalEntry(dbTransaction);
|
|
1017
|
+
//Temporary fix to successfully save journal entry in PostJournal
|
|
1018
|
+
journalEntry.init({
|
|
1019
|
+
CompanyId: this.CompanyId,
|
|
1020
|
+
Name: 'Issue Debit Note ' + creditNote.DocNo,
|
|
1021
|
+
});
|
|
1022
|
+
|
|
1023
|
+
const transactionDate = new Date();
|
|
1024
|
+
|
|
1025
|
+
const creditTransaction = await journalEntry.newLedgerTransaction(
|
|
1026
|
+
TransactionTypeOptions.CREDIT,
|
|
1027
|
+
);
|
|
1028
|
+
|
|
1029
|
+
if (ctAccountNo) {
|
|
1030
|
+
/*Transacting using AR account provided*/
|
|
1031
|
+
creditTransaction.AccountNo = ctAccountNo;
|
|
1032
|
+
} else {
|
|
1033
|
+
/*Transacting based on default customer AR account*/
|
|
1034
|
+
creditTransaction.AccountNo = customer.CustSystemCode + '-AP';
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
creditTransaction.Currency = creditNote.Currency;
|
|
1038
|
+
creditTransaction.CreditAmount = creditNote.Amount;
|
|
1039
|
+
creditTransaction.Date = transactionDate;
|
|
1040
|
+
creditTransaction.Description = creditNote.DocNo;
|
|
1041
|
+
creditTransaction.Name = creditNote.DocNo;
|
|
1042
|
+
|
|
1043
|
+
for (const invoiceItem of documentItems) {
|
|
1044
|
+
const itemLedger = await journalEntry.newLedgerTransaction(
|
|
1045
|
+
TransactionTypeOptions.DEBIT,
|
|
1046
|
+
);
|
|
1047
|
+
itemLedger.AccountNo = invoiceItem.CtAccountNo;
|
|
1048
|
+
itemLedger.Currency = invoiceItem.Currency;
|
|
1049
|
+
itemLedger.DebitAmount = invoiceItem.Amount;
|
|
1050
|
+
itemLedger.Date = transactionDate;
|
|
1051
|
+
itemLedger.Description = invoiceItem.Description;
|
|
1052
|
+
itemLedger.Name = invoiceItem.Name;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
this.postJournal(dbTransaction, journalEntry);
|
|
1056
|
+
|
|
1057
|
+
const payload = {
|
|
1058
|
+
Action: 'Create',
|
|
1059
|
+
Activity: 'Issuing a Credit Note Transaction',
|
|
1060
|
+
Description: `Credit Transaction (ID: ${creditTransaction.TransactionId}) has been created`,
|
|
1061
|
+
EntityType: 'CreditTransaction',
|
|
1062
|
+
EntityValueBefore: JSON.stringify({}),
|
|
1063
|
+
EntityValueAfter: JSON.stringify(creditTransaction),
|
|
1064
|
+
PerformedById: 'test',
|
|
1065
|
+
PerformedAt: transactionDate,
|
|
1066
|
+
EntityId: creditTransaction.TransactionId,
|
|
1067
|
+
};
|
|
1068
|
+
|
|
1069
|
+
await axios.post(
|
|
1070
|
+
`${process.env.COMMON_API_URL}/activity-histories`,
|
|
1071
|
+
payload,
|
|
1072
|
+
);
|
|
1073
|
+
|
|
1074
|
+
return creditNote;
|
|
1075
|
+
} catch (err) {
|
|
1076
|
+
// tslint:disable-next-line:no-console
|
|
1077
|
+
console.log('Issue credit note err: ', err);
|
|
1078
|
+
throw err;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
/**
|
|
1083
|
+
* Register a payment collected
|
|
1084
|
+
*
|
|
1085
|
+
* @param dbTransaction - The database transaction to be used
|
|
1086
|
+
* @param loginUser - The user collecting the payment
|
|
1087
|
+
* @param payment - The payment object containing payment details
|
|
1088
|
+
* @param customer - The customer making the payment
|
|
1089
|
+
* @param ctAccountNo - The account number of the customer's Account Receivable account to transact, else the default customer account payable receivable will be used
|
|
1090
|
+
*
|
|
1091
|
+
* @returns {Payment} - Payment object representing the full detals of the payment collection recorded
|
|
1092
|
+
*/
|
|
1093
|
+
async collectPayment(
|
|
1094
|
+
dbTransaction: any,
|
|
1095
|
+
loginUser: LoginUserBase,
|
|
1096
|
+
payment: Payment,
|
|
1097
|
+
customer: FinanceCustomerBase,
|
|
1098
|
+
ctAccountNo?: string,
|
|
1099
|
+
): Promise<Payment> {
|
|
1100
|
+
try {
|
|
1101
|
+
/*validation 1: Make sure that the payment has at least 1 payment item*/
|
|
1102
|
+
const paymentItems = await payment.PaymentItems;
|
|
1103
|
+
if (paymentItems.length < 1) {
|
|
1104
|
+
throw new Error(
|
|
1105
|
+
'Atleast one payment item is required to identify what payment is being paid for.',
|
|
1106
|
+
);
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/*validation 2: Make sure that the payment has at least 1 payment method*/
|
|
1110
|
+
const paymentPaidWithItems = await payment.PaymentPaidWith;
|
|
1111
|
+
if (paymentPaidWithItems.length < 1) {
|
|
1112
|
+
throw new Error(
|
|
1113
|
+
'Atleast one payment paid with item is required to identify how the payment was made.',
|
|
1114
|
+
);
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
payment.PaymentId = cuid();
|
|
1118
|
+
payment.PaymentType = PaymentType.PAYMENT_RECEIVED;
|
|
1119
|
+
payment.ReceivedBy = loginUser.ObjectId;
|
|
1120
|
+
|
|
1121
|
+
await FinanceCompany._PaymentRepository.create(
|
|
1122
|
+
{
|
|
1123
|
+
PaymentId: payment.PaymentId,
|
|
1124
|
+
PaymentType: payment.PaymentType,
|
|
1125
|
+
PaymentDate: payment.PaymentDate,
|
|
1126
|
+
Description: payment.Description,
|
|
1127
|
+
Currency: payment.Currency,
|
|
1128
|
+
Amount: payment.Amount,
|
|
1129
|
+
Status: PaymentStatus.SUCCESSFUL,
|
|
1130
|
+
PostedToAccSystemYN: 'N',
|
|
1131
|
+
ReceivedBy: payment.ReceivedBy,
|
|
1132
|
+
UpdatedAt: new Date(),
|
|
1133
|
+
UpdatedBy: loginUser.ObjectId,
|
|
1134
|
+
CreatedAt: new Date(),
|
|
1135
|
+
CreatedBy: loginUser.ObjectId,
|
|
1136
|
+
},
|
|
1137
|
+
{ transaction: dbTransaction },
|
|
1138
|
+
);
|
|
1139
|
+
|
|
1140
|
+
for (const paymentItem of paymentItems) {
|
|
1141
|
+
await FinanceCompany._PaymentItemRepository.create(
|
|
1142
|
+
{
|
|
1143
|
+
PaymentId: payment.PaymentId,
|
|
1144
|
+
PayForObjectId: paymentItem.PayForObjectId,
|
|
1145
|
+
PayForObjectType: paymentItem.PayForObjectType,
|
|
1146
|
+
Currency: paymentItem.Currency,
|
|
1147
|
+
Amount: paymentItem.Amount,
|
|
1148
|
+
Name: paymentItem.Name,
|
|
1149
|
+
Description: paymentItem.Description,
|
|
1150
|
+
},
|
|
1151
|
+
{ transaction: dbTransaction },
|
|
1152
|
+
);
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
for (const paymentPaidWithItem of paymentPaidWithItems) {
|
|
1156
|
+
await FinanceCompany._PaymentPaidWithRepository.create(
|
|
1157
|
+
{
|
|
1158
|
+
PaymentId: payment.PaymentId,
|
|
1159
|
+
MethodTypeId: paymentPaidWithItem.MethodTypeId,
|
|
1160
|
+
Currency: paymentPaidWithItem.Currency,
|
|
1161
|
+
Amount: paymentPaidWithItem.Amount,
|
|
1162
|
+
Status: paymentPaidWithItem.Status,
|
|
1163
|
+
TransactionId: paymentPaidWithItem.TransactionId,
|
|
1164
|
+
RefBank: paymentPaidWithItem.RefBank,
|
|
1165
|
+
RefName: paymentPaidWithItem.RefName,
|
|
1166
|
+
RefNo: paymentPaidWithItem.RefNo,
|
|
1167
|
+
RefOther1: paymentPaidWithItem.RefOther1,
|
|
1168
|
+
RefOther2: paymentPaidWithItem.RefOther2,
|
|
1169
|
+
RefOther3: paymentPaidWithItem.RefOther3,
|
|
1170
|
+
RefOther4: paymentPaidWithItem.RefOther4,
|
|
1171
|
+
RefOther5: paymentPaidWithItem.RefOther5,
|
|
1172
|
+
Remarks: paymentPaidWithItem.Remarks,
|
|
1173
|
+
PaymentMediaId: paymentPaidWithItem.PaymentMediaId,
|
|
1174
|
+
},
|
|
1175
|
+
{ transaction: dbTransaction },
|
|
1176
|
+
);
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
/*Registering the Journal Entries for the transaction*/
|
|
1180
|
+
const transactionDate = new Date();
|
|
1181
|
+
|
|
1182
|
+
for (const paymentPaidWith of paymentPaidWithItems) {
|
|
1183
|
+
let paymentMethodType: PaymentMethodType;
|
|
1184
|
+
|
|
1185
|
+
try {
|
|
1186
|
+
paymentMethodType = await PaymentMethodType.initMethodType(
|
|
1187
|
+
dbTransaction,
|
|
1188
|
+
paymentPaidWith.MethodTypeId,
|
|
1189
|
+
);
|
|
1190
|
+
} catch (error) {
|
|
1191
|
+
if (error instanceof RecordNotFoundError) {
|
|
1192
|
+
throw new Error('Invalid Payment Method Type Id');
|
|
1193
|
+
} else {
|
|
1194
|
+
throw error;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
const journalEntry = new JournalEntry(dbTransaction);
|
|
1198
|
+
|
|
1199
|
+
journalEntry.init({
|
|
1200
|
+
CompanyId: this.CompanyId,
|
|
1201
|
+
Name: 'Collect-payments for ' + payment.PaymentId,
|
|
1202
|
+
});
|
|
1203
|
+
|
|
1204
|
+
const debitLT = await journalEntry.newLedgerTransaction(
|
|
1205
|
+
TransactionTypeOptions.DEBIT,
|
|
1206
|
+
);
|
|
1207
|
+
debitLT.AccountNo = paymentMethodType.AccountNo;
|
|
1208
|
+
debitLT.Currency = payment.Currency;
|
|
1209
|
+
debitLT.DebitAmount = payment.Amount;
|
|
1210
|
+
debitLT.Date = transactionDate;
|
|
1211
|
+
debitLT.Description = customer.FullName;
|
|
1212
|
+
debitLT.Name = customer.FullName;
|
|
1213
|
+
|
|
1214
|
+
const creditLT = await journalEntry.newLedgerTransaction(
|
|
1215
|
+
TransactionTypeOptions.CREDIT,
|
|
1216
|
+
);
|
|
1217
|
+
|
|
1218
|
+
if (ctAccountNo) {
|
|
1219
|
+
creditLT.AccountNo = ctAccountNo;
|
|
1220
|
+
} else {
|
|
1221
|
+
creditLT.AccountNo = (await customer.AccountReceivable).AccountNo;
|
|
1222
|
+
}
|
|
1223
|
+
creditLT.Currency = payment.Currency;
|
|
1224
|
+
creditLT.CreditAmount = payment.Amount;
|
|
1225
|
+
creditLT.Date = transactionDate;
|
|
1226
|
+
creditLT.Description = paymentMethodType.Name;
|
|
1227
|
+
creditLT.Name = paymentMethodType.Name;
|
|
1228
|
+
|
|
1229
|
+
await this.postJournal(dbTransaction, journalEntry);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/*todo: saving a record into the activity history table*/
|
|
1233
|
+
|
|
1234
|
+
return payment;
|
|
1235
|
+
} catch (error) {
|
|
1236
|
+
throw error;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* Method to make payment to a customer.
|
|
1242
|
+
*
|
|
1243
|
+
* @param dbTransaction - The database transaction to be used
|
|
1244
|
+
* @param loginUser - The user logging in and the user who collected the payment.
|
|
1245
|
+
* @param payment - The payment object containing payment details
|
|
1246
|
+
* @param customer - The customer who is receiving the payment.
|
|
1247
|
+
* @param ctAccountNo - The account number of the customer's Account Payable account to transact, else the default customer account payable will be used.
|
|
1248
|
+
*
|
|
1249
|
+
* @returns {Payment} - Payment object representing the full details of the payment collection recorded
|
|
1250
|
+
*/
|
|
1251
|
+
async makePayment(
|
|
1252
|
+
dbTransaction: any,
|
|
1253
|
+
loginUser: LoginUserBase,
|
|
1254
|
+
payment: Payment,
|
|
1255
|
+
customer: FinanceCustomerBase,
|
|
1256
|
+
dtAccountNo?: string,
|
|
1257
|
+
): Promise<Payment> {
|
|
1258
|
+
let paymentMethodType: PaymentMethodType;
|
|
1259
|
+
try {
|
|
1260
|
+
const PaymentPaidWith = await payment.PaymentPaidWith;
|
|
1261
|
+
if (PaymentPaidWith.length < 1) {
|
|
1262
|
+
throw new Error(
|
|
1263
|
+
'Atleast one payment paid with item is required to identify how the payment was made.',
|
|
1264
|
+
);
|
|
1265
|
+
}
|
|
1266
|
+
paymentMethodType = await PaymentMethodType.initMethodType(
|
|
1267
|
+
dbTransaction,
|
|
1268
|
+
PaymentPaidWith[0].MethodTypeId,
|
|
1269
|
+
);
|
|
1270
|
+
|
|
1271
|
+
const paymentItems = await payment.PaymentItems;
|
|
1272
|
+
if (paymentItems.length < 1) {
|
|
1273
|
+
throw new Error(
|
|
1274
|
+
'Atleast one payment item is required to identify what payment is being paid for',
|
|
1275
|
+
);
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
payment.PaymentId = cuid();
|
|
1279
|
+
payment.PaymentType = PaymentType.PAYOUT;
|
|
1280
|
+
payment.ReceivedBy = loginUser.ObjectId;
|
|
1281
|
+
|
|
1282
|
+
await FinanceCompany._PaymentRepository.create(
|
|
1283
|
+
{
|
|
1284
|
+
PaymentId: payment.PaymentId,
|
|
1285
|
+
PaymentType: payment.PaymentType,
|
|
1286
|
+
PaymentDate: payment.PaymentDate,
|
|
1287
|
+
Description: payment.Description,
|
|
1288
|
+
Currency: payment.Currency,
|
|
1289
|
+
ReceivedBy: payment.ReceivedBy,
|
|
1290
|
+
Amount: payment.Amount,
|
|
1291
|
+
Status: PaymentStatus.SUCCESSFUL,
|
|
1292
|
+
PostedToAccSystemYN: 'N',
|
|
1293
|
+
UpdatedAt: new Date(),
|
|
1294
|
+
UpdatedBy: loginUser.ObjectId,
|
|
1295
|
+
CreatedAt: new Date(),
|
|
1296
|
+
CreatedBy: loginUser.ObjectId,
|
|
1297
|
+
},
|
|
1298
|
+
{ transaction: dbTransaction },
|
|
1299
|
+
);
|
|
1300
|
+
|
|
1301
|
+
paymentItems.forEach(async (paymentItem) => {
|
|
1302
|
+
await FinanceCompany._PaymentItemRepository.create(
|
|
1303
|
+
{
|
|
1304
|
+
PaymentId: payment.PaymentId,
|
|
1305
|
+
PayForObjectId: paymentItem.PayForObjectId,
|
|
1306
|
+
PayForObjectType: paymentItem.PayForObjectType,
|
|
1307
|
+
Currency: paymentItem.Currency,
|
|
1308
|
+
Amount: paymentItem.Amount,
|
|
1309
|
+
Name: paymentItem.Name,
|
|
1310
|
+
Description: paymentItem.Description,
|
|
1311
|
+
},
|
|
1312
|
+
{ transaction: dbTransaction },
|
|
1313
|
+
);
|
|
1314
|
+
});
|
|
1315
|
+
|
|
1316
|
+
const journalEntry = new JournalEntry(dbTransaction);
|
|
1317
|
+
//Temporary fix to successfully save journal entry in PostJournal
|
|
1318
|
+
journalEntry.init({
|
|
1319
|
+
CompanyId: this.CompanyId,
|
|
1320
|
+
Name: 'Make Payment for ' + payment.PaymentId,
|
|
1321
|
+
});
|
|
1322
|
+
const transactionDate = new Date();
|
|
1323
|
+
|
|
1324
|
+
const creditLT = await journalEntry.newLedgerTransaction(
|
|
1325
|
+
TransactionTypeOptions.CREDIT,
|
|
1326
|
+
);
|
|
1327
|
+
creditLT.AccountNo = paymentMethodType.AccountNo;
|
|
1328
|
+
creditLT.Currency = payment.Currency;
|
|
1329
|
+
creditLT.CreditAmount = payment.Amount;
|
|
1330
|
+
creditLT.Date = transactionDate;
|
|
1331
|
+
creditLT.Description = customer.FullName;
|
|
1332
|
+
creditLT.Name = customer.FullName;
|
|
1333
|
+
|
|
1334
|
+
const debitLT = await journalEntry.newLedgerTransaction(
|
|
1335
|
+
TransactionTypeOptions.DEBIT,
|
|
1336
|
+
);
|
|
1337
|
+
if (dtAccountNo) {
|
|
1338
|
+
debitLT.AccountNo = dtAccountNo;
|
|
1339
|
+
} else {
|
|
1340
|
+
debitLT.AccountNo = (await customer.AccountPayable).AccountNo;
|
|
1341
|
+
}
|
|
1342
|
+
debitLT.Currency = payment.Currency;
|
|
1343
|
+
debitLT.DebitAmount = payment.Amount;
|
|
1344
|
+
debitLT.Date = transactionDate;
|
|
1345
|
+
debitLT.Description = paymentMethodType.Name;
|
|
1346
|
+
debitLT.Name = paymentMethodType.Name;
|
|
1347
|
+
|
|
1348
|
+
await this.postJournal(dbTransaction, journalEntry);
|
|
1349
|
+
|
|
1350
|
+
/*todo: saving a record into the activity history table*/
|
|
1351
|
+
|
|
1352
|
+
return payment;
|
|
1353
|
+
} catch (error) {
|
|
1354
|
+
if (error instanceof RecordNotFoundError) {
|
|
1355
|
+
throw new Error('Invalid PaymentMethodType id');
|
|
1356
|
+
} else {
|
|
1357
|
+
throw error;
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
get PaymentMethods(): Promise<PaymentMethod[]> {
|
|
1363
|
+
return new Promise((resolve, reject) => {
|
|
1364
|
+
if (this.CompanyId !== 'New') {
|
|
1365
|
+
FinanceCompany._PaymentMethodRepository
|
|
1366
|
+
.findAll({
|
|
1367
|
+
where: {
|
|
1368
|
+
CompanyId: this.CompanyId,
|
|
1369
|
+
},
|
|
1370
|
+
transaction: this._DbTransaction,
|
|
1371
|
+
})
|
|
1372
|
+
.then((paymentMethod) => {
|
|
1373
|
+
const paymentMethodObjects = paymentMethod.map(
|
|
1374
|
+
(paymentMethodData) => {
|
|
1375
|
+
return new Promise((resolve, reject) => {
|
|
1376
|
+
FinanceCompany._PaymentMethodTypeRepository
|
|
1377
|
+
.findAll({
|
|
1378
|
+
where: {
|
|
1379
|
+
MethodId: paymentMethodData.MethodId,
|
|
1380
|
+
},
|
|
1381
|
+
transaction: this._DbTransaction,
|
|
1382
|
+
raw: true,
|
|
1383
|
+
})
|
|
1384
|
+
.then((paymentMethodTypes) => {
|
|
1385
|
+
const paymentMethodObjects = {
|
|
1386
|
+
...paymentMethodData.get({ plain: true }),
|
|
1387
|
+
Types: paymentMethodTypes,
|
|
1388
|
+
};
|
|
1389
|
+
resolve(paymentMethodObjects);
|
|
1390
|
+
})
|
|
1391
|
+
.catch((err) => {
|
|
1392
|
+
reject(err);
|
|
1393
|
+
});
|
|
1394
|
+
}).then((paymentMethods) => paymentMethods);
|
|
1395
|
+
},
|
|
1396
|
+
);
|
|
1397
|
+
return Promise.all(paymentMethodObjects);
|
|
1398
|
+
})
|
|
1399
|
+
.then((paymentMethodObjects) => {
|
|
1400
|
+
this._PaymentMethods = paymentMethodObjects;
|
|
1401
|
+
resolve(this._PaymentMethods);
|
|
1402
|
+
})
|
|
1403
|
+
.catch((err) => {
|
|
1404
|
+
reject(err);
|
|
1405
|
+
});
|
|
1406
|
+
} else {
|
|
1407
|
+
resolve(this._PaymentMethods);
|
|
1408
|
+
}
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
/**
|
|
1413
|
+
* Method to load / reload the payment methods based on the configuration file
|
|
1414
|
+
*/
|
|
1415
|
+
async LoadPaymentMethods(): Promise<void> {
|
|
1416
|
+
/*Retrieve the payment method from the config file */
|
|
1417
|
+
const { companyId, paymentMethods } = config.financeCompanies['TXG-FS'];
|
|
1418
|
+
const paymentMethod = new PaymentMethod();
|
|
1419
|
+
for (const method in paymentMethods) {
|
|
1420
|
+
const paymentMethodData =
|
|
1421
|
+
await FinanceCompany._PaymentMethodRepository.findOne({
|
|
1422
|
+
where: {
|
|
1423
|
+
MethodId: paymentMethods[method].id,
|
|
1424
|
+
Name: paymentMethods[method].name,
|
|
1425
|
+
},
|
|
1426
|
+
});
|
|
1427
|
+
|
|
1428
|
+
if (!paymentMethodData) {
|
|
1429
|
+
const newPaymentMethod =
|
|
1430
|
+
await FinanceCompany._PaymentMethodRepository.create({
|
|
1431
|
+
MethodId: paymentMethods[method].id,
|
|
1432
|
+
Name: paymentMethods[method].name,
|
|
1433
|
+
CompanyId: companyId,
|
|
1434
|
+
});
|
|
1435
|
+
|
|
1436
|
+
this._PaymentMethods.push(newPaymentMethod);
|
|
1437
|
+
}
|
|
1438
|
+
this._PaymentMethods.push(paymentMethodData);
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
this._PaymentMethods.forEach(async (item) => {
|
|
1442
|
+
const p = item?.get({ plain: true });
|
|
1443
|
+
|
|
1444
|
+
for (const method in paymentMethods) {
|
|
1445
|
+
if (!p) {
|
|
1446
|
+
continue;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
if (p.MethodId === paymentMethods[method]?.id) {
|
|
1450
|
+
const paymentMethodTypeData =
|
|
1451
|
+
await FinanceCompany._PaymentMethodTypeRepository.findOne({
|
|
1452
|
+
where: {
|
|
1453
|
+
MethodId: p.MethodId,
|
|
1454
|
+
},
|
|
1455
|
+
});
|
|
1456
|
+
|
|
1457
|
+
if (!paymentMethodTypeData) {
|
|
1458
|
+
const configPaymentMethodTypes = paymentMethods[method]?.types;
|
|
1459
|
+
|
|
1460
|
+
for (const methodType in configPaymentMethodTypes) {
|
|
1461
|
+
// TODO: Create a seeder for payment method account
|
|
1462
|
+
/*validate whether account data already exists*/
|
|
1463
|
+
const accountData =
|
|
1464
|
+
await FinanceCompany._AccountRepository.findOne({
|
|
1465
|
+
where: {
|
|
1466
|
+
AccountNo: configPaymentMethodTypes[methodType].accountNo,
|
|
1467
|
+
},
|
|
1468
|
+
});
|
|
1469
|
+
|
|
1470
|
+
/*generating account data if not exist */
|
|
1471
|
+
if (!accountData) {
|
|
1472
|
+
const accountPayload = {
|
|
1473
|
+
CompanyId: companyId,
|
|
1474
|
+
Name: configPaymentMethodTypes[methodType].name,
|
|
1475
|
+
AccountType: 'PaymentMethod',
|
|
1476
|
+
CreatedAt: new Date(),
|
|
1477
|
+
CreatedById: 'System',
|
|
1478
|
+
AccSystemRefId: 'REF',
|
|
1479
|
+
PostedToAccSystemYN: 'N',
|
|
1480
|
+
};
|
|
1481
|
+
|
|
1482
|
+
try {
|
|
1483
|
+
await FinanceCompany._AccountRepository.create({
|
|
1484
|
+
AccountNo: configPaymentMethodTypes[methodType].accountNo,
|
|
1485
|
+
...accountPayload,
|
|
1486
|
+
});
|
|
1487
|
+
|
|
1488
|
+
await FinanceCompany._AccountRepository.create({
|
|
1489
|
+
AccountNo:
|
|
1490
|
+
configPaymentMethodTypes[methodType]
|
|
1491
|
+
.processingFeeAccountNo,
|
|
1492
|
+
...accountPayload,
|
|
1493
|
+
});
|
|
1494
|
+
} catch (err) {}
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
const paymentMethodTypePayload = {
|
|
1498
|
+
MethodId: p.MethodId,
|
|
1499
|
+
MethodTypeId: configPaymentMethodTypes[methodType].id,
|
|
1500
|
+
Name: configPaymentMethodTypes[methodType].name,
|
|
1501
|
+
AccountNo: configPaymentMethodTypes[methodType].accountNo,
|
|
1502
|
+
ProcessingFeeRate:
|
|
1503
|
+
configPaymentMethodTypes[methodType].processingFeeRate,
|
|
1504
|
+
ProcessingFeeAccountNo:
|
|
1505
|
+
configPaymentMethodTypes[methodType].processingFeeAccountNo,
|
|
1506
|
+
};
|
|
1507
|
+
|
|
1508
|
+
try {
|
|
1509
|
+
await paymentMethod.newPaymentMethodType(
|
|
1510
|
+
paymentMethodTypePayload,
|
|
1511
|
+
);
|
|
1512
|
+
} catch (err) {}
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
}
|