Skip to content

Commit

Permalink
Add revert sales invoice (#175)
Browse files Browse the repository at this point in the history
* npm updated

* update

* allow a sales invoice to be reverted

* formatted
  • Loading branch information
davidpodhola authored Jul 17, 2024
1 parent b403626 commit e971fd7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
16 changes: 16 additions & 0 deletions apps/api/src/app/resolvers/sales.invoice.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ export class SalesInvoiceResolver {
);
}

@Mutation(() => SalesInvoice)
async revertSalesInvoice(
@Args('id', { type: () => Int }) id: number,
@CurrentUser() user,
): Promise<SalesInvoiceModel> {
const invoice = await this.salesInvoiceService.loadEntityById(
this.entityManager,
id,
);
return await this.salesInvoiceService.revert(
this.entityManager,
invoice,
user,
);
}

@Mutation(() => SalesInvoice)
async publishSalesInvoice(
@Args('args') objData: SalesInvoicePublishArgs,
Expand Down
26 changes: 19 additions & 7 deletions apps/api/src/model/lib/sales.invoice.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,16 @@ export class SalesInvoiceService extends BaseEntityService<
return invoice;
}

async revert(
manager: EntityManager,
invoice: SalesInvoiceModel,
currentUser: UserModel,
): Promise<SalesInvoiceModel> {
invoice.isDraft = true;
await this.persist(manager, invoice, currentUser);
return invoice;
}

async fixPrint(manager: EntityManager) {
console.log('Fix print started');
const invoices = await manager
Expand Down Expand Up @@ -581,17 +591,19 @@ export class SalesInvoiceService extends BaseEntityService<
notDraftInvoicesWithoutDocumentNumber: Array<SalesInvoiceModel>,
) {
for (const invoice of notDraftInvoicesWithoutDocumentNumber) {
if (invoice.documentNo || invoice.isDraft) {
if (invoice.isDraft) {
throw new Error(
'Call with non draft invoices without document number only!',
);
}
invoice.documentNo =
await this.documentNumberingServiceModel.getNextDocumentNumber(
manager,
invoice.constructor,
await invoice.organization,
);
if (!invoice.documentNo) {
invoice.documentNo =
await this.documentNumberingServiceModel.getNextDocumentNumber(
manager,
invoice.constructor,
await invoice.organization,
);
}
}
}

Expand Down

0 comments on commit e971fd7

Please sign in to comment.