Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into refactor-2-advanced-logic-others-extensions
- Loading branch information
Showing
26 changed files
with
918 additions
and
504 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
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
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
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
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
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,2 @@ | ||
export type { ActorInfo, Address, Invoice, InvoiceItem, PaymentTerms, Tax } from './types'; | ||
export * from './utils'; |
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,65 @@ | ||
export interface Invoice { | ||
meta: { | ||
format: 'rnf_invoice'; | ||
version: string; | ||
}; | ||
buyerInfo?: ActorInfo; | ||
creationDate: string; | ||
invoiceItems: InvoiceItem[]; | ||
invoiceNumber: string; | ||
miscellaneous?: unknown; | ||
note?: string; | ||
paymentTerms?: PaymentTerms; | ||
purchaseOrderId?: string; | ||
sellerInfo?: ActorInfo; | ||
terms?: string; | ||
} | ||
|
||
export interface InvoiceItem { | ||
currency: string; | ||
deliveryDate?: string; | ||
deliveryPeriod?: string; | ||
discount?: string; | ||
name: string; | ||
quantity: number; | ||
reference?: string; | ||
/** | ||
* @deprecated since 0.0.3. Use tax instead | ||
*/ | ||
taxPercent?: number; | ||
tax: Tax; | ||
unitPrice: string; | ||
} | ||
|
||
export interface Tax { | ||
type: 'percentage' | 'fixed'; | ||
amount: string; | ||
} | ||
|
||
export interface ActorInfo { | ||
address?: Address; | ||
businessName?: string; | ||
email?: string; | ||
firstName?: string; | ||
lastName?: string; | ||
miscellaneous?: unknown; | ||
phone?: string; | ||
taxRegistration?: string; | ||
} | ||
|
||
export interface PaymentTerms { | ||
dueDate?: string; | ||
lateFeesFix?: string; | ||
lateFeesPercent?: number; | ||
miscellaneous?: unknown; | ||
} | ||
|
||
export interface Address { | ||
'country-name'?: string; | ||
'extended-address'?: string; | ||
locality?: string; | ||
'post-office-box'?: string; | ||
'postal-code'?: string; | ||
region?: string; | ||
'street-address'?: string; | ||
} |
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,41 @@ | ||
import { BigNumber } from 'ethers'; | ||
import { Invoice, InvoiceItem } from './types'; | ||
|
||
export const getInvoiceTotal = (invoice: Invoice): BigNumber => { | ||
return invoice.invoiceItems.reduce( | ||
(acc, item) => acc.add(getInvoiceLineTotal(item)), | ||
BigNumber.from(0), | ||
); | ||
}; | ||
|
||
export const getInvoiceLineTotal = (item: InvoiceItem): BigNumber => { | ||
// Every amount in currency is a big number with the good number of decimals for this currency | ||
// Tax percent is not an amount in currency. To allow a big number multiplication, we convert | ||
// it temporarily with preciselyOne (allows 6 decimals, ie 0.123456%) | ||
const preciselyOne = 1000000; | ||
|
||
// Support for rnf_version < 0.0.3 | ||
const tax = item.taxPercent | ||
? { type: 'percentage', amount: String(item.taxPercent) } | ||
: item.tax || { type: 'percentage', amount: '0' }; | ||
|
||
const taxPercent = tax.amount && tax.type === 'percentage' ? Number(tax.amount) + 100 : 100; | ||
const taxFixed = | ||
tax.amount && tax.type === 'fixed' ? BigNumber.from(tax.amount) : BigNumber.from(0); | ||
const discount = item.discount ? BigNumber.from(item.discount) : BigNumber.from(0); | ||
|
||
return ( | ||
BigNumber.from(item.unitPrice) | ||
// account for floating quantities | ||
.mul(Number(item.quantity * preciselyOne).toFixed(0)) | ||
.div(preciselyOne) | ||
.sub(discount) | ||
// Artificially offset the decimal to let the multiplication work | ||
.mul(Number(taxPercent * preciselyOne).toFixed(0)) | ||
// Remove the decimal offset | ||
.div(preciselyOne) | ||
// Remove the percentage multiplier | ||
.div(100) | ||
.add(taxFixed) | ||
); | ||
}; |
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.