-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
352 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/server/src/services/Sales/Receipts/GetSaleReceiptMailState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Inject, Service } from 'typedi'; | ||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable'; | ||
import HasTenancyService from '@/services/Tenancy/TenancyService'; | ||
import { SaleReceiptMailNotification } from './SaleReceiptMailNotification'; | ||
import { GetSaleReceiptMailStateTransformer } from './GetSaleReceiptMailStateTransformer'; | ||
|
||
@Service() | ||
export class GetSaleReceiptMailState { | ||
@Inject() | ||
private tenancy: HasTenancyService; | ||
|
||
@Inject() | ||
private transformer: TransformerInjectable; | ||
|
||
@Inject() | ||
private receiptMail: SaleReceiptMailNotification; | ||
|
||
/** | ||
* Retrieves the sale receipt mail state of the given sale receipt. | ||
* @param {number} tenantId | ||
* @param {number} saleReceiptId | ||
*/ | ||
public async getMailState(tenantId: number, saleReceiptId: number) { | ||
const { SaleReceipt } = this.tenancy.models(tenantId); | ||
|
||
const saleReceipt = await SaleReceipt.query() | ||
.findById(saleReceiptId) | ||
.withGraphFetched('entries.item') | ||
.withGraphFetched('customer') | ||
.throwIfNotFound(); | ||
|
||
const mailOptions = await this.receiptMail.getMailOptions( | ||
tenantId, | ||
saleReceiptId | ||
); | ||
return this.transformer.transform( | ||
tenantId, | ||
saleReceipt, | ||
new GetSaleReceiptMailStateTransformer(), | ||
{ | ||
mailOptions, | ||
} | ||
); | ||
} | ||
} |
194 changes: 194 additions & 0 deletions
194
packages/server/src/services/Sales/Receipts/GetSaleReceiptMailStateTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { Transformer } from '@/lib/Transformer/Transformer'; | ||
import { ItemEntryTransformer } from '../Invoices/ItemEntryTransformer'; | ||
|
||
export class GetSaleReceiptMailStateTransformer extends Transformer { | ||
/** | ||
* Exclude these attributes from user object. | ||
* @returns {Array} | ||
*/ | ||
public excludeAttributes = (): string[] => { | ||
return ['*']; | ||
}; | ||
|
||
/** | ||
* Included attributes. | ||
* @returns {Array} | ||
*/ | ||
public includeAttributes = (): string[] => { | ||
return [ | ||
'companyName', | ||
'companyLogoUri', | ||
'primaryColor', | ||
'customerName', | ||
'total', | ||
'totalFormatted', | ||
'subtotal', | ||
'subtotalFormatted', | ||
'receiptDate', | ||
'receiptDateFormatted', | ||
'closedAtDate', | ||
'closedAtDateFormatted', | ||
'receiptNumber', | ||
'entries', | ||
]; | ||
}; | ||
|
||
/** | ||
* Retrieves the customer name of the invoice. | ||
* @returns {string} | ||
*/ | ||
protected customerName = (receipt) => { | ||
return receipt.customer.displayName; | ||
}; | ||
|
||
/** | ||
* Retrieves the company name. | ||
* @returns {string} | ||
*/ | ||
protected companyName = () => { | ||
return this.context.organization.name; | ||
}; | ||
|
||
/** | ||
* Retrieves the company logo uri. | ||
* @returns {string | null} | ||
*/ | ||
protected companyLogoUri = (receipt) => { | ||
return receipt.pdfTemplate?.companyLogoUri; | ||
}; | ||
|
||
/** | ||
* Retrieves the primary color. | ||
* @returns {string} | ||
*/ | ||
protected primaryColor = (receipt) => { | ||
return receipt.pdfTemplate?.attributes?.primaryColor; | ||
}; | ||
|
||
/** | ||
* | ||
* @param receipt | ||
* @returns | ||
*/ | ||
protected total = (receipt) => { | ||
return receipt.amount; | ||
}; | ||
|
||
/** | ||
* | ||
* @param receipt | ||
* @returns | ||
*/ | ||
protected totalFormatted = (receipt) => { | ||
return this.formatMoney(receipt.amount, { | ||
currencyCode: receipt.currencyCode, | ||
}); | ||
}; | ||
|
||
/** | ||
* | ||
* @param receipt | ||
* @returns | ||
*/ | ||
protected subtotal = (receipt) => { | ||
return receipt.amount; | ||
}; | ||
|
||
/** | ||
* | ||
* @param receipt | ||
* @returns | ||
*/ | ||
protected subtotalFormatted = (receipt) => { | ||
return this.formatMoney(receipt.amount, { | ||
currencyCode: receipt.currencyCode, | ||
}); | ||
}; | ||
|
||
/** | ||
* | ||
* @param receipt | ||
* @returns | ||
*/ | ||
protected receiptDate = (receipt): string => { | ||
return receipt.receiptDate; | ||
}; | ||
|
||
/** | ||
* | ||
* @param {ISaleReceipt} invoice | ||
* @returns {string} | ||
*/ | ||
protected receiptDateFormatted = (receipt): string => { | ||
return this.formatDate(receipt.receiptDate); | ||
}; | ||
|
||
/** | ||
* | ||
* @param receipt | ||
* @returns | ||
*/ | ||
protected closedAtDate = (receipt): string => { | ||
return receipt.closedAt; | ||
}; | ||
|
||
/** | ||
* Retrieve formatted estimate closed at date. | ||
* @param {ISaleReceipt} invoice | ||
* @returns {String} | ||
*/ | ||
protected closedAtDateFormatted = (receipt): string => { | ||
return this.formatDate(receipt.closedAt); | ||
}; | ||
|
||
/** | ||
* | ||
* @param invoice | ||
* @returns | ||
*/ | ||
protected entries = (receipt) => { | ||
return this.item( | ||
receipt.entries, | ||
new GetSaleReceiptEntryMailStateTransformer(), | ||
{ | ||
currencyCode: receipt.currencyCode, | ||
} | ||
); | ||
}; | ||
|
||
/** | ||
* Merges the mail options with the invoice object. | ||
*/ | ||
public transform = (object: any) => { | ||
return { | ||
...this.options.mailOptions, | ||
...object, | ||
}; | ||
}; | ||
} | ||
|
||
class GetSaleReceiptEntryMailStateTransformer extends ItemEntryTransformer { | ||
/** | ||
* Exclude these attributes from user object. | ||
* @returns {Array} | ||
*/ | ||
public excludeAttributes = (): string[] => { | ||
return ['*']; | ||
}; | ||
|
||
public name = (entry) => { | ||
return entry.item.name; | ||
}; | ||
|
||
public includeAttributes = (): string[] => { | ||
return [ | ||
'name', | ||
'quantity', | ||
'quantityFormatted', | ||
'rate', | ||
'rateFormatted', | ||
'total', | ||
'totalFormatted', | ||
]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.