fix: add user id to sale invoice non-inventory journal entries.

This commit is contained in:
a.bouhuolia
2021-01-02 16:10:11 +02:00
parent edb439b29e
commit d3cbb3074b
5 changed files with 60 additions and 25 deletions

View File

@@ -146,14 +146,15 @@ export default class SaleInvoicesController extends BaseController {
* @param {Function} next
*/
async newSaleInvoice(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const saleInvoiceOTD: ISaleInvoiceDTO = this.matchedBodyData(req);
const { tenantId, user } = req;
const saleInvoiceDTO: ISaleInvoiceDTO = this.matchedBodyData(req);
try {
// Creates a new sale invoice with associated entries.
const storedSaleInvoice = await this.saleInvoiceService.createSaleInvoice(
tenantId,
saleInvoiceOTD
saleInvoiceDTO,
user
);
return res.status(200).send({
id: storedSaleInvoice.id,
@@ -171,7 +172,7 @@ export default class SaleInvoicesController extends BaseController {
* @param {Function} next
*/
async editSaleInvoice(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { tenantId, user } = req;
const { id: saleInvoiceId } = req.params;
const saleInvoiceOTD: ISaleInvoiceDTO = this.matchedBodyData(req);
@@ -180,7 +181,8 @@ export default class SaleInvoicesController extends BaseController {
await this.saleInvoiceService.editSaleInvoice(
tenantId,
saleInvoiceId,
saleInvoiceOTD
saleInvoiceOTD,
user
);
return res.status(200).send({
id: saleInvoiceId,
@@ -198,12 +200,15 @@ export default class SaleInvoicesController extends BaseController {
* @param {NextFunction} next -
*/
async deliverSaleInvoice(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { tenantId, user } = req;
const { id: saleInvoiceId } = req.params;
try {
await this.saleInvoiceService.deliverSaleInvoice(tenantId, saleInvoiceId);
await this.saleInvoiceService.deliverSaleInvoice(
tenantId,
saleInvoiceId,
user
);
return res.status(200).send({
id: saleInvoiceId,
message: 'The given sale invoice has been delivered successfully',
@@ -221,11 +226,11 @@ export default class SaleInvoicesController extends BaseController {
*/
async deleteSaleInvoice(req: Request, res: Response, next: NextFunction) {
const { id: saleInvoiceId } = req.params;
const { tenantId } = req;
const { tenantId, user } = req;
try {
// Deletes the sale invoice with associated entries and journal transaction.
await this.saleInvoiceService.deleteSaleInvoice(tenantId, saleInvoiceId);
await this.saleInvoiceService.deleteSaleInvoice(tenantId, saleInvoiceId, user);
return res.status(200).send({
id: saleInvoiceId,
@@ -388,7 +393,9 @@ export default class SaleInvoicesController extends BaseController {
}
if (error.errorType === 'INVOICE_HAS_ASSOCIATED_PAYMENT_ENTRIES') {
return res.boom.badRequest(null, {
errors: [{ type: 'INVOICE_HAS_ASSOCIATED_PAYMENT_ENTRIES', code: 1100 }],
errors: [
{ type: 'INVOICE_HAS_ASSOCIATED_PAYMENT_ENTRIES', code: 1100 },
],
});
}
}

View File

@@ -538,16 +538,24 @@ export default class JournalCommands {
);
}
/**
*
* @param {ISaleInvoice} saleInvoice
* @param {number} receivableAccountsId
* @param {number} authorizedUserId
*/
saleInvoiceNonInventory(
saleInvoice: ISaleInvoice & {
entries: IItemEntry & { item: IItem };
},
receivableAccountsId: number
receivableAccountsId: number,
authorizedUserId: number,
) {
const commonEntry = {
referenceType: 'SaleInvoice',
referenceId: saleInvoice.id,
date: saleInvoice.invoiceDate,
userId: authorizedUserId,
};
// XXX Debit - Receivable account.

View File

@@ -12,6 +12,8 @@ import {
ISalesInvoicesFilter,
IPaginationMeta,
IFilterMeta,
ISystemUser,
ISystemService,
} from 'interfaces';
import events from 'subscribers/events';
import InventoryService from 'services/Inventory/Inventory';
@@ -163,7 +165,8 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
*/
public async createSaleInvoice(
tenantId: number,
saleInvoiceDTO: ISaleInvoiceCreateDTO
saleInvoiceDTO: ISaleInvoiceCreateDTO,
authorizedUser: ISystemUser
): Promise<ISaleInvoice> {
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
@@ -202,6 +205,7 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
tenantId,
saleInvoice,
saleInvoiceId: saleInvoice.id,
authorizedUser,
});
this.logger.info('[sale_invoice] successfully inserted.', {
tenantId,
@@ -222,7 +226,8 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
public async editSaleInvoice(
tenantId: number,
saleInvoiceId: number,
saleInvoiceDTO: any
saleInvoiceDTO: any,
authorizedUser: ISystemUser
): Promise<ISaleInvoice> {
const { SaleInvoice } = this.tenancy.models(tenantId);
@@ -280,6 +285,7 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
oldSaleInvoice,
tenantId,
saleInvoiceId,
authorizedUser,
});
return saleInvoice;
}
@@ -292,7 +298,8 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
*/
public async deliverSaleInvoice(
tenantId: number,
saleInvoiceId: number
saleInvoiceId: number,
authorizedUser: ISystemUser
): Promise<void> {
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
@@ -345,7 +352,8 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
*/
public async deleteSaleInvoice(
tenantId: number,
saleInvoiceId: number
saleInvoiceId: number,
authorizedUser: ISystemUser
): Promise<void> {
const { ItemEntry } = this.tenancy.models(tenantId);
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
@@ -383,6 +391,7 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
tenantId,
oldSaleInvoice,
saleInvoiceId,
authorizedUser,
});
}
@@ -400,7 +409,7 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
tenantId: number,
saleInvoiceId: number,
saleInvoiceDate: Date,
override?: boolean
override?: boolean,
): Promise<void> {
// Gets the next inventory lot number.
const lotNumber = this.inventoryService.getNextLotNumber(tenantId);
@@ -454,6 +463,7 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
public async recordNonInventoryJournalEntries(
tenantId: number,
saleInvoiceId: number,
authorizedUserId: number,
override: boolean = false
): Promise<void> {
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
@@ -471,8 +481,12 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
saleInvoiceId,
'entries.item'
);
await this.writeNonInventoryInvoiceEntries(tenantId, saleInvoice, override);
await this.writeNonInventoryInvoiceEntries(
tenantId,
saleInvoice,
authorizedUserId,
override
);
}
/**
@@ -514,7 +528,8 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
*/
public async getSaleInvoice(
tenantId: number,
saleInvoiceId: number
saleInvoiceId: number,
authorizedUser: ISystemUser
): Promise<ISaleInvoice> {
const { SaleInvoice } = this.tenancy.models(tenantId);

View File

@@ -184,12 +184,12 @@ export default class SaleInvoicesCost {
/**
* Writes the sale invoice journal entries.
* @param {SaleInvoice} saleInvoice -
*/
async writeNonInventoryInvoiceEntries(
tenantId: number,
saleInvoice: ISaleInvoice,
override: boolean
authorizedUserId: number,
override: boolean = false,
) {
const { accountRepository } = this.tenancy.repositories(tenantId);
const { AccountTransaction } = this.tenancy.models(tenantId);
@@ -210,7 +210,11 @@ export default class SaleInvoicesCost {
journal.fromTransactions(oldTransactions);
journal.removeEntries();
}
journalCommands.saleInvoiceNonInventory(saleInvoice, receivableAccount.id);
journalCommands.saleInvoiceNonInventory(
saleInvoice,
receivableAccount.id,
authorizedUserId,
);
await Promise.all([
journal.deleteEntries(),

View File

@@ -99,10 +99,11 @@ export default class SaleInvoiceSubscriber {
*/
@On(events.saleInvoice.onCreated)
@On(events.saleInvoice.onEdited)
public async handleWritingNonInventoryEntries({ tenantId, saleInvoice }) {
public async handleWritingNonInventoryEntries({ tenantId, saleInvoice, authorizedUser }) {
await this.saleInvoicesService.recordNonInventoryJournalEntries(
tenantId,
saleInvoice.id
saleInvoice.id,
authorizedUser.id,
);
}