feat: Map the invoice preview data

This commit is contained in:
Ahmed Bouhuolia
2024-09-19 14:32:14 +02:00
parent 16eaacd4bc
commit 0ae7a25c27
8 changed files with 112 additions and 20 deletions

View File

@@ -415,7 +415,8 @@ export default class SaleInvoice extends mixin(TenantModel, [
const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
const {
TransactionPaymentServiceEntry,
} = require('models/TransactionPaymentServiceEntry');
} = require('models/TransactionPaymentServiceEntry');
const { PdfTemplate } = require('models/PdfTemplate');
return {
/**
@@ -593,6 +594,18 @@ export default class SaleInvoice extends mixin(TenantModel, [
query.where('reference_type', 'SaleInvoice');
},
},
/**
* Sale invoice may belongs to pdf branding template.
*/
pdfTemplate: {
relation: Model.BelongsToOneRelation,
modelClass: PdfTemplate,
join: {
from: 'sales_invoices.pdfTemplateId',
to: 'pdf_templates.id',
}
},
};
}

View File

@@ -1,10 +1,9 @@
import moment from 'moment';
import { Inject, Service } from 'typedi';
import { ServiceError } from '@/exceptions';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { PaymentLink } from '@/system/models';
import { Inject, Service } from 'typedi';
import { GeneratePaymentLinkTransformer } from './GeneratePaymentLinkTransformer';
import { GetInvoicePaymentLinkMetaTransformer } from './GetInvoicePaymentLinkTransformer';
import { initalizeTenantServices } from '@/api/middleware/TenantDependencyInjection';
@@ -46,7 +45,7 @@ export class GetInvoicePaymentLinkMetadata {
const invoice = await SaleInvoice.query()
.findById(paymentLink.resourceId)
.withGraphFetched('entries')
.withGraphFetched('entries.item')
.withGraphFetched('customer')
.throwIfNotFound();

View File

@@ -1,3 +1,4 @@
import { ItemEntryTransformer } from './ItemEntryTransformer';
import { SaleInvoiceTransformer } from './SaleInvoiceTransformer';
export class GetInvoicePaymentLinkMetaTransformer extends SaleInvoiceTransformer {
@@ -33,6 +34,9 @@ export class GetInvoicePaymentLinkMetaTransformer extends SaleInvoiceTransformer
'dueDate',
'dueDateFormatted',
'invoiceNo',
'invoiceMessage',
'termsConditions',
'entries',
];
};
@@ -43,4 +47,50 @@ export class GetInvoicePaymentLinkMetaTransformer extends SaleInvoiceTransformer
public companyName() {
return 'Bigcapital Technology, Inc.';
}
/**
* Retrieves the entries of the sale invoice.
* @param {ISaleInvoice} invoice
* @returns {}
*/
protected entries = (invoice) => {
return this.item(
invoice.entries,
new GetInvoicePaymentLinkEntryMetaTransformer(),
{
currencyCode: invoice.currencyCode,
}
);
};
}
class GetInvoicePaymentLinkEntryMetaTransformer extends ItemEntryTransformer {
/**
* Include these attributes to item entry object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return [
'quantity',
'quantityFormatted',
'rate',
'rateFormatted',
'total',
'totalFormatted',
'itemName',
'description',
];
};
itemName(entry) {
return entry.item.name;
}
/**
* Exclude these attributes from payment link object.
* @returns {Array}
*/
public excludeAttributes = (): string[] => {
return ['*'];
};
}