mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat: ability to change customer/vendor opening balance.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { difference, upperFirst, omit } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import { ServiceError } from "exceptions";
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import {
|
||||
@@ -11,6 +12,10 @@ import JournalPoster from '../Accounting/JournalPoster';
|
||||
|
||||
type TContactService = 'customer' | 'vendor';
|
||||
|
||||
const ERRORS = {
|
||||
OPENING_BALANCE_DATE_REQUIRED: 'OPENING_BALANCE_DATE_REQUIRED',
|
||||
};
|
||||
|
||||
@Service()
|
||||
export default class ContactsService {
|
||||
@Inject()
|
||||
@@ -185,4 +190,40 @@ export default class ContactsService {
|
||||
journal.deleteEntries(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Chanages the opening balance of the given contact.
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {ICustomerChangeOpeningBalanceDTO} changeOpeningBalance
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async changeOpeningBalance(
|
||||
tenantId: number,
|
||||
contactId: number,
|
||||
contactService: string,
|
||||
openingBalance: number,
|
||||
openingBalanceAt?: Date|string,
|
||||
): Promise<void> {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
// Retrieve the given contact details or throw not found service error.
|
||||
const contact = await this.getContactByIdOrThrowError(tenantId, contactId, contactService);
|
||||
|
||||
// Should the opening balance date be required.
|
||||
if (!contact.openingBalanceAt && !openingBalanceAt) {
|
||||
throw new ServiceError(ERRORS.OPENING_BALANCE_DATE_REQUIRED);
|
||||
};
|
||||
// Changes the customer the opening balance and opening balance date.
|
||||
await contactRepository.update({
|
||||
openingBalance: openingBalance,
|
||||
|
||||
...(openingBalanceAt) && ({
|
||||
openingBalanceAt: moment(openingBalanceAt).toMySqlDateTime(),
|
||||
}),
|
||||
}, {
|
||||
id: contactId,
|
||||
contactService,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -16,14 +16,13 @@ import {
|
||||
IContactNewDTO,
|
||||
IContactEditDTO,
|
||||
IContact,
|
||||
ISaleInvoice
|
||||
ISaleInvoice,
|
||||
} from 'interfaces';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||
import events from 'subscribers/events';
|
||||
import moment from 'moment';
|
||||
import SaleInvoiceRepository from 'repositories/SaleInvoiceRepository';
|
||||
|
||||
@Service()
|
||||
export default class CustomersService {
|
||||
@@ -298,4 +297,31 @@ export default class CustomersService {
|
||||
throw new ServiceError('some_customers_have_invoices');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the opening balance of the given customer.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {number} openingBalance
|
||||
* @param {string|Date} openingBalanceAt
|
||||
*/
|
||||
public async changeOpeningBalance(
|
||||
tenantId: number,
|
||||
customerId: number,
|
||||
openingBalance: number,
|
||||
openingBalanceAt: Date|string,
|
||||
) {
|
||||
|
||||
await this.contactService.changeOpeningBalance(
|
||||
tenantId,
|
||||
customerId,
|
||||
'customer',
|
||||
openingBalance,
|
||||
openingBalanceAt,
|
||||
);
|
||||
// Triggers `onOpeingBalanceChanged` event.
|
||||
await this.eventDispatcher.dispatch(events.customers.onOpeningBalanceChanged, {
|
||||
tenantId, customerId, openingBalance, openingBalanceAt
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,4 +251,31 @@ export default class VendorsService {
|
||||
filterMeta: dynamicFilter.getResponseMeta(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the opeing balance of the given vendor.
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @param {number} openingBalance
|
||||
* @param {Date|string} openingBalanceAt
|
||||
*/
|
||||
public async changeOpeningBalance(
|
||||
tenantId: number,
|
||||
vendorId: number,
|
||||
openingBalance: number,
|
||||
openingBalanceAt: Date|string,
|
||||
): Promise<void> {
|
||||
|
||||
await this.contactService.changeOpeningBalance(
|
||||
tenantId,
|
||||
vendorId,
|
||||
'vendor',
|
||||
openingBalance,
|
||||
openingBalanceAt,
|
||||
);
|
||||
// Triggers `onOpeingBalanceChanged` event.
|
||||
await this.eventDispatcher.dispatch(events.vendors.onOpeningBalanceChanged, {
|
||||
tenantId, vendorId, openingBalance, openingBalanceAt
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ const ERRORS = {
|
||||
|
||||
ITEMS_HAVE_ASSOCIATED_TRANSACTIONS: 'ITEMS_HAVE_ASSOCIATED_TRANSACTIONS',
|
||||
ITEM_HAS_ASSOCIATED_TRANSACTINS: 'ITEM_HAS_ASSOCIATED_TRANSACTINS'
|
||||
}
|
||||
};
|
||||
|
||||
@Service()
|
||||
export default class ItemsService implements IItemsService {
|
||||
|
||||
Reference in New Issue
Block a user