mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
fix: payment receive and made.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { omit } from 'lodash';
|
||||
import {
|
||||
ISaleInvoice,
|
||||
IPaymentReceivePageEntry,
|
||||
IPaymentReceive,
|
||||
ISystemUser,
|
||||
} from 'interfaces';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import { ERRORS } from './constants';
|
||||
|
||||
/**
|
||||
* Payment receives edit/new pages service.
|
||||
*/
|
||||
@Service()
|
||||
export default class PaymentReceivesPages {
|
||||
@Inject()
|
||||
tenancy: TenancyService;
|
||||
|
||||
@Inject('logger')
|
||||
logger: any;
|
||||
|
||||
/**
|
||||
* Retrive page invoices entries from the given sale invoices models.
|
||||
* @param {ISaleInvoice[]} invoices - Invoices.
|
||||
* @return {IPaymentReceivePageEntry}
|
||||
*/
|
||||
private invoiceToPageEntry(invoice: ISaleInvoice): IPaymentReceivePageEntry {
|
||||
return {
|
||||
entryType: 'invoice',
|
||||
invoiceId: invoice.id,
|
||||
dueAmount: invoice.dueAmount,
|
||||
amount: invoice.balance,
|
||||
invoiceNo: invoice.invoiceNo,
|
||||
totalPaymentAmount: invoice.paymentAmount,
|
||||
paymentAmount: invoice.paymentAmount,
|
||||
date: invoice.invoiceDate,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve payment receive new page receivable entries.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} vendorId - Vendor id.
|
||||
* @return {IPaymentReceivePageEntry[]}
|
||||
*/
|
||||
public async getNewPageEntries(tenantId: number, customerId: number) {
|
||||
const { SaleInvoice } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieve due invoices.
|
||||
const entries = await SaleInvoice.query()
|
||||
.modify('delivered')
|
||||
.modify('dueInvoices')
|
||||
.where('customer_id', customerId)
|
||||
.orderBy('invoice_date', 'ASC');
|
||||
|
||||
return entries.map(this.invoiceToPageEntry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the payment receive details of the given id.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {Integer} paymentReceiveId - Payment receive id.
|
||||
*/
|
||||
public async getPaymentReceiveEditPage(
|
||||
tenantId: number,
|
||||
paymentReceiveId: number,
|
||||
): Promise<{
|
||||
paymentReceive: Omit<IPaymentReceive, 'entries'>;
|
||||
entries: IPaymentReceivePageEntry[];
|
||||
}> {
|
||||
const { PaymentReceive, SaleInvoice } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieve payment receive.
|
||||
const paymentReceive = await PaymentReceive.query()
|
||||
.findById(paymentReceiveId)
|
||||
.withGraphFetched('entries.invoice');
|
||||
|
||||
// Throw not found the payment receive.
|
||||
if (!paymentReceive) {
|
||||
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
|
||||
}
|
||||
const paymentEntries = paymentReceive.entries.map((entry) => ({
|
||||
...this.invoiceToPageEntry(entry.invoice),
|
||||
dueAmount: entry.invoice.dueAmount + entry.paymentAmount,
|
||||
paymentAmount: entry.paymentAmount,
|
||||
}));
|
||||
// Retrieves all receivable bills that associated to the payment receive transaction.
|
||||
const restReceivableInvoices = await SaleInvoice.query()
|
||||
.modify('delivered')
|
||||
.modify('dueInvoices')
|
||||
.where('customer_id', paymentReceive.customerId)
|
||||
.whereNotIn(
|
||||
'id',
|
||||
paymentReceive.entries.map((entry) => entry.invoiceId)
|
||||
)
|
||||
.orderBy('invoice_date', 'ASC');
|
||||
|
||||
const restReceivableEntries = restReceivableInvoices.map(
|
||||
this.invoiceToPageEntry
|
||||
);
|
||||
const entries = [...paymentEntries, ...restReceivableEntries];
|
||||
|
||||
return {
|
||||
paymentReceive: omit(paymentReceive, ['entries']),
|
||||
entries,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,6 @@ import {
|
||||
IPaymentReceivesFilter,
|
||||
ISaleInvoice,
|
||||
ISystemUser,
|
||||
IPaymentReceivePageEntry,
|
||||
} from 'interfaces';
|
||||
import AccountsService from 'services/Accounts/AccountsService';
|
||||
import JournalPoster from 'services/Accounting/JournalPoster';
|
||||
@@ -31,21 +30,9 @@ import CustomersService from 'services/Contacts/CustomersService';
|
||||
import ItemsEntriesService from 'services/Items/ItemsEntriesService';
|
||||
import JournalCommands from 'services/Accounting/JournalCommands';
|
||||
import { ACCOUNT_PARENT_TYPE } from 'data/AccountTypes';
|
||||
import AutoIncrementOrdersService from './AutoIncrementOrdersService';
|
||||
import AutoIncrementOrdersService from '../AutoIncrementOrdersService';
|
||||
import { ERRORS } from './constants';
|
||||
|
||||
const ERRORS = {
|
||||
PAYMENT_RECEIVE_NO_EXISTS: 'PAYMENT_RECEIVE_NO_EXISTS',
|
||||
PAYMENT_RECEIVE_NOT_EXISTS: 'PAYMENT_RECEIVE_NOT_EXISTS',
|
||||
DEPOSIT_ACCOUNT_NOT_FOUND: 'DEPOSIT_ACCOUNT_NOT_FOUND',
|
||||
DEPOSIT_ACCOUNT_INVALID_TYPE: 'DEPOSIT_ACCOUNT_INVALID_TYPE',
|
||||
INVALID_PAYMENT_AMOUNT: 'INVALID_PAYMENT_AMOUNT',
|
||||
INVOICES_IDS_NOT_FOUND: 'INVOICES_IDS_NOT_FOUND',
|
||||
ENTRIES_IDS_NOT_EXISTS: 'ENTRIES_IDS_NOT_EXISTS',
|
||||
INVOICES_NOT_DELIVERED_YET: 'INVOICES_NOT_DELIVERED_YET',
|
||||
PAYMENT_RECEIVE_NO_IS_REQUIRED: 'PAYMENT_RECEIVE_NO_IS_REQUIRED',
|
||||
PAYMENT_RECEIVE_NO_REQUIRED: 'PAYMENT_RECEIVE_NO_REQUIRED',
|
||||
PAYMENT_CUSTOMER_SHOULD_NOT_UPDATE: 'PAYMENT_CUSTOMER_SHOULD_NOT_UPDATE',
|
||||
};
|
||||
/**
|
||||
* Payment receive service.
|
||||
* @service
|
||||
@@ -532,7 +519,7 @@ export default class PaymentReceiveService {
|
||||
* @param {Integer} paymentReceiveId - Payment receive id.
|
||||
* @param {IPaymentReceive} paymentReceive - Payment receive object.
|
||||
*/
|
||||
async deletePaymentReceive(
|
||||
public async deletePaymentReceive(
|
||||
tenantId: number,
|
||||
paymentReceiveId: number,
|
||||
authorizedUser: ISystemUser
|
||||
@@ -572,7 +559,7 @@ export default class PaymentReceiveService {
|
||||
* @param {number} paymentReceiveId - Payment receive id.
|
||||
* @return {Promise<IPaymentReceive>}
|
||||
*/
|
||||
async getPaymentReceive(
|
||||
public async getPaymentReceive(
|
||||
tenantId: number,
|
||||
paymentReceiveId: number
|
||||
): Promise<IPaymentReceive> {
|
||||
@@ -591,76 +578,6 @@ export default class PaymentReceiveService {
|
||||
return paymentReceive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrive edit page invoices entries from the given sale invoices models.
|
||||
* @param {ISaleInvoice[]} invoices - Invoices.
|
||||
* @return {IPaymentReceiveEditPageEntry}
|
||||
*/
|
||||
public invoiceToPageEntry(
|
||||
invoice: ISaleInvoice
|
||||
): IPaymentReceiveEditPageEntry {
|
||||
return {
|
||||
entryType: 'invoice',
|
||||
invoiceId: invoice.id,
|
||||
dueAmount: invoice.dueAmount + invoice.paymentAmount,
|
||||
amount: invoice.balance,
|
||||
invoiceNo: invoice.invoiceNo,
|
||||
totalPaymentAmount: invoice.paymentAmount,
|
||||
paymentAmount: invoice.paymentAmount,
|
||||
date: invoice.invoiceDate,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the payment receive details of the given id.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {Integer} paymentReceiveId - Payment receive id.
|
||||
*/
|
||||
public async getPaymentReceiveEditPage(
|
||||
tenantId: number,
|
||||
paymentReceiveId: number,
|
||||
authorizedUser: ISystemUser
|
||||
): Promise<{
|
||||
paymentReceive: Omit<IPaymentReceive, 'entries'>;
|
||||
entries: IPaymentReceivePageEntry[];
|
||||
}> {
|
||||
const { PaymentReceive, SaleInvoice } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieve payment receive.
|
||||
const paymentReceive = await PaymentReceive.query()
|
||||
.findById(paymentReceiveId)
|
||||
.withGraphFetched('entries.invoice');
|
||||
|
||||
// Throw not found the payment receive.
|
||||
if (!paymentReceive) {
|
||||
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
|
||||
}
|
||||
const paymentEntries = paymentReceive.entries.map((entry) => ({
|
||||
...this.invoiceToPageEntry(entry.invoice),
|
||||
paymentAmount: entry.paymentAmount,
|
||||
}));
|
||||
|
||||
// Retrieves all receivable bills that associated to the payment receive transaction.
|
||||
const restReceivableInvoices = await SaleInvoice.query()
|
||||
.modify('dueInvoices')
|
||||
.where('customer_id', paymentReceive.customerId)
|
||||
.whereNotIn(
|
||||
'id',
|
||||
paymentReceive.entries.map((entry) => entry.invoiceId)
|
||||
)
|
||||
.orderBy('invoice_date', 'ASC');
|
||||
|
||||
const restReceivableEntries = restReceivableInvoices.map(
|
||||
this.invoiceToPageEntry
|
||||
);
|
||||
const entries = [...paymentEntries, ...restReceivableEntries];
|
||||
|
||||
return {
|
||||
paymentReceive: omit(paymentReceive, ['entries']),
|
||||
entries,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve sale invoices that assocaited to the given payment receive.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
@@ -680,7 +597,6 @@ export default class PaymentReceiveService {
|
||||
const paymentReceiveInvoicesIds = paymentReceive.entries.map(
|
||||
(entry) => entry.invoiceId
|
||||
);
|
||||
|
||||
const saleInvoices = await SaleInvoice.query().whereIn(
|
||||
'id',
|
||||
paymentReceiveInvoicesIds
|
||||
@@ -726,22 +642,6 @@ export default class PaymentReceiveService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the payment receive details with associated invoices.
|
||||
* @param {Integer} paymentReceiveId
|
||||
*/
|
||||
async getPaymentReceiveWithInvoices(
|
||||
tenantId: number,
|
||||
paymentReceiveId: number
|
||||
) {
|
||||
const { PaymentReceive } = this.tenancy.models(tenantId);
|
||||
|
||||
return PaymentReceive.query()
|
||||
.where('id', paymentReceiveId)
|
||||
.withGraphFetched('invoices')
|
||||
.first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Records payment receive journal transactions.
|
||||
*
|
||||
@@ -871,22 +771,4 @@ export default class PaymentReceiveService {
|
||||
});
|
||||
await Promise.all([...opers]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve payment receive new page receivable entries.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} vendorId - Vendor id.
|
||||
* @return {IPaymentReceivePageEntry[]}
|
||||
*/
|
||||
async getNewPageEntries(tenantId: number, customerId: number) {
|
||||
const { SaleInvoice } = this.tenancy.models(tenantId);
|
||||
|
||||
// Retrieve due invoices.
|
||||
const entries = await SaleInvoice.query()
|
||||
.modify('dueInvoices')
|
||||
.where('customer_id', customerId)
|
||||
.orderBy('invoice_date', 'ASC');
|
||||
|
||||
return entries.map(this.invoiceToPageEntry);
|
||||
}
|
||||
}
|
||||
13
server/src/services/Sales/PaymentReceives/constants.ts
Normal file
13
server/src/services/Sales/PaymentReceives/constants.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export const ERRORS = {
|
||||
PAYMENT_RECEIVE_NO_EXISTS: 'PAYMENT_RECEIVE_NO_EXISTS',
|
||||
PAYMENT_RECEIVE_NOT_EXISTS: 'PAYMENT_RECEIVE_NOT_EXISTS',
|
||||
DEPOSIT_ACCOUNT_NOT_FOUND: 'DEPOSIT_ACCOUNT_NOT_FOUND',
|
||||
DEPOSIT_ACCOUNT_INVALID_TYPE: 'DEPOSIT_ACCOUNT_INVALID_TYPE',
|
||||
INVALID_PAYMENT_AMOUNT: 'INVALID_PAYMENT_AMOUNT',
|
||||
INVOICES_IDS_NOT_FOUND: 'INVOICES_IDS_NOT_FOUND',
|
||||
ENTRIES_IDS_NOT_EXISTS: 'ENTRIES_IDS_NOT_EXISTS',
|
||||
INVOICES_NOT_DELIVERED_YET: 'INVOICES_NOT_DELIVERED_YET',
|
||||
PAYMENT_RECEIVE_NO_IS_REQUIRED: 'PAYMENT_RECEIVE_NO_IS_REQUIRED',
|
||||
PAYMENT_RECEIVE_NO_REQUIRED: 'PAYMENT_RECEIVE_NO_REQUIRED',
|
||||
PAYMENT_CUSTOMER_SHOULD_NOT_UPDATE: 'PAYMENT_CUSTOMER_SHOULD_NOT_UPDATE',
|
||||
};
|
||||
@@ -656,6 +656,7 @@ export default class SaleInvoicesService {
|
||||
|
||||
const salesInvoices = await SaleInvoice.query().onBuild((query) => {
|
||||
query.modify('dueInvoices');
|
||||
query.modify('delivered');
|
||||
|
||||
if (customerId) {
|
||||
query.where('customer_id', customerId);
|
||||
|
||||
Reference in New Issue
Block a user