Skip to content

Commit

Permalink
🌐 added en-AU
Browse files Browse the repository at this point in the history
  • Loading branch information
guym4c committed Jul 30, 2021
1 parent 028f61f commit c0c1b69
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 38 deletions.
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iwgb/roo-invoice-parser",
"version": "0.1.7",
"version": "0.2.0",
"repository": {
"type": "git",
"url": "git+https://github.com/iwgb/roo-invoice-parser.git"
Expand All @@ -10,6 +10,7 @@
"dependencies": {
"cli-progress": "^3.9.0",
"lodash": "^4.17.20",
"luxon": "^2.0.1",
"pdfjs-dist": "^2.5.207",
"sha.js": "^2.4.11",
"yargs": "^17.0.1"
Expand All @@ -30,14 +31,10 @@
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^1.7.0",
"luxon": "^2.0.1",
"release-it": "^14.10.0",
"rimraf": "^3.0.2",
"typescript": "^4.3.5"
},
"peerDependencies": {
"luxon": "^2.0.1"
},
"scripts": {
"build": "tsc -p tsconfig.json",
"watch": "npm run build -- -w",
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
alias: 't',
type: 'string',
describe: 'Main timezone (not DST) that the work was performed in',
default: 'UTC',
default: undefined,
},
locale: {
alias: 'l',
Expand Down
47 changes: 47 additions & 0 deletions src/market/en-AU.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { DateTime } from 'luxon';
import { InvoiceComponentGetterProps } from '../types';
import {
getDataFromAdjustmentTable, getDataFromShiftTable,
} from '../utils/parse';
import { INVOICE_DATE_FORMAT } from '../constants/invoice';

const SUMMARY_START_FLAG = 'Summary';
const HEADER_END_FLAG = 'Total';
const INVOICE_PERIOD_FLAG = 'Bill for services supplied during:';
const INVOICE_PERIOD_DATE_SEPARATOR = '-';
const INVOICE_ADJUSTMENT_EXCLUDED_LABELS = ['Drop Fees', 'Total'];
const INVOICE_NAME_FLAG = 'Supplier:';
const INVOICE_NAME_LABEL_SEPARATOR = ':';

const getShifts = ({ text, zone, locale }: InvoiceComponentGetterProps) => getDataFromShiftTable(
text,
zone,
locale,
HEADER_END_FLAG,
SUMMARY_START_FLAG,
);

const getPeriod = ({ text, zone }: InvoiceComponentGetterProps) => {
const [start, end] = text[text.indexOf(INVOICE_PERIOD_FLAG) + 1]
.split(INVOICE_PERIOD_DATE_SEPARATOR)
.map((date) => DateTime.fromFormat(date.trim(), INVOICE_DATE_FORMAT, { zone }));

return {
start,
end: end.endOf('day'),
};
};

const getName = ({ text }: InvoiceComponentGetterProps) => text[text.indexOf(INVOICE_NAME_FLAG) + 1]
.split(INVOICE_NAME_LABEL_SEPARATOR)[1]
.trim();

const getAdjustments = ({ text }: InvoiceComponentGetterProps) => getDataFromAdjustmentTable(
text,
SUMMARY_START_FLAG,
INVOICE_ADJUSTMENT_EXCLUDED_LABELS,
);

export default {
getName, getPeriod, getShifts, getAdjustments,
};
2 changes: 1 addition & 1 deletion src/market/en-BE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const getPeriod = ({ text, zone }: InvoiceComponentGetterProps) => {

const getName = ({
text,
}: InvoiceComponentGetterProps) => text[text.findIndex((line) => line === SUPPLIER_FLAG) + 1];
}: InvoiceComponentGetterProps) => text[text.indexOf(SUPPLIER_FLAG) + 1];

const getAdjustments = ({ text }: InvoiceComponentGetterProps) => getDataFromAdjustmentTable(
text,
Expand Down
26 changes: 12 additions & 14 deletions src/market/en-GB.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DateTime } from 'luxon';
import { INVOICE_DATE_FORMAT } from '../constants/invoice';
import { InvoiceComponentGetterProps } from '../types';
import { getDataFromAdjustmentTable, getDataFromShiftTable } from '../utils/parse';
import {
getDataFromAdjustmentTable,
getDataFromShiftTable,
getPeriodFromHeader,
} from '../utils/parse';

const HEADER_END_FLAG = 'Total';
const SUMMARY_START_FLAG = 'Summary';
Expand All @@ -20,17 +22,13 @@ const getShifts = ({ text, zone, locale }: InvoiceComponentGetterProps) => getDa
SUMMARY_START_FLAG,
);

const getPeriod = ({ text, zone }: InvoiceComponentGetterProps) => {
const [start, end] = (text
.find((line) => line.includes(INVOICE_PERIOD_FLAG)) || '')
.split(INVOICE_PERIOD_LABEL_SEPARATOR)[1]
.split(INVOICE_PERIOD_DATE_SEPARATOR)
.map((date) => DateTime.fromFormat(date.trim(), INVOICE_DATE_FORMAT, { zone }));
return {
start,
end: end.endOf('day'),
};
};
const getPeriod = ({ text, zone }: InvoiceComponentGetterProps) => getPeriodFromHeader(
text,
INVOICE_PERIOD_FLAG,
INVOICE_PERIOD_LABEL_SEPARATOR,
INVOICE_PERIOD_DATE_SEPARATOR,
{ zone },
);

const getName = ({ text }: InvoiceComponentGetterProps) => (text
.find((line) => line.includes(INVOICE_NAME_FLAG)) || '')
Expand Down
26 changes: 12 additions & 14 deletions src/market/fr-FR.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DateTime } from 'luxon';
import { INVOICE_DATE_FORMAT } from '../constants/invoice';
import { InvoiceComponentGetterProps } from '../types';
import { getDataFromAdjustmentTable, getDataFromShiftTable } from '../utils/parse';
import {
getDataFromAdjustmentTable,
getDataFromShiftTable,
getPeriodFromHeader,
} from '../utils/parse';

const HEADER_END_FLAG = 'Total';
const SUMMARY_START_FLAG = 'sume';
Expand All @@ -20,17 +22,13 @@ const getShifts = ({ text, zone, locale }: InvoiceComponentGetterProps) => getDa
SUMMARY_START_FLAG,
);

const getPeriod = ({ text, zone, locale }: InvoiceComponentGetterProps) => {
const [start, end] = (text
.find((line) => line.includes(INVOICE_PERIOD_FLAG)) || '')
.split(INVOICE_PERIOD_LABEL_SEPARATOR)[1]
.split(INVOICE_PERIOD_DATE_SEPARATOR)
.map((date) => DateTime.fromFormat(date.trim(), INVOICE_DATE_FORMAT, { zone, locale }));
return {
start,
end: end.endOf('day'),
};
};
const getPeriod = ({ text, zone, locale }: InvoiceComponentGetterProps) => getPeriodFromHeader(
text,
INVOICE_PERIOD_FLAG,
INVOICE_PERIOD_LABEL_SEPARATOR,
INVOICE_PERIOD_DATE_SEPARATOR,
{ zone, locale },
);

const getName = ({ text }: InvoiceComponentGetterProps) => (text
.find((line) => line.includes(INVOICE_NAME_FLAG)) || '')
Expand Down
5 changes: 5 additions & 0 deletions src/market/markets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enGb from './en-GB';
import enBe from './en-BE';
import frFr from './fr-FR';
import enAu from './en-AU';
import {
Adjustment, InvoiceComponentGetterProps, Period, Shift,
} from '../types';
Expand All @@ -15,21 +16,25 @@ export interface InvoiceParser {
const UNITED_KINGDOM = 'en-GB';
const BELGIUM = 'en-BE';
const FRANCE = 'fr-FR';
const AUSTRALIA = 'en-AU';

export interface Markets {
[UNITED_KINGDOM]: InvoiceParser,
[BELGIUM]: InvoiceParser,
[FRANCE]: InvoiceParser,
[AUSTRALIA]: InvoiceParser,
}

export const defaultTimezones = {
[UNITED_KINGDOM]: 'Europe/London',
[BELGIUM]: 'Europe/Brussels',
[FRANCE]: 'Europe/Paris',
[AUSTRALIA]: 'Australia/Sydney',
};

export default {
[UNITED_KINGDOM]: enGb,
[BELGIUM]: enBe,
[FRANCE]: frFr,
[AUSTRALIA]: enAu,
};
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const hashInvoice = (
const parseInvoice = async (
data: PdfData,
locale: keyof Markets,
timezone: string | null = null,
timezone: string | undefined = undefined,
progress: SingleBar | null = null,
): Promise<Invoice> => {
const text = await getPdfText(data);
Expand Down
23 changes: 21 additions & 2 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import chunk from 'lodash/chunk';
import { Info } from 'luxon';
import { DateTime, DateTimeOptions, Info } from 'luxon';
import { Adjustment, Shift } from '../types';
import { getDateTime } from './datetime';
import { INVOICE_DATE_FORMAT } from '../constants/invoice';

export const getDataFromShiftTable = (
text: string[],
Expand Down Expand Up @@ -74,7 +75,7 @@ export const getDataFromAdjustmentTable = (
), 2);

return adjustments
.map(([label, amount]) => ({
.map(([label, amount = '']) => ({
label,
amount: Number.parseFloat(amount.slice(1)),
}))
Expand All @@ -83,3 +84,21 @@ export const getDataFromAdjustmentTable = (
&& !Number.isNaN(amount)
));
};

export const getPeriodFromHeader = (
text: string[],
lineLabel: string,
labelSeparator: string,
dateSeparator: string,
dateTimeOpts: DateTimeOptions,
) => {
const [start, end] = (text
.find((line) => line.includes(lineLabel)) || '')
.split(labelSeparator)[1]
.split(dateSeparator)
.map((date) => DateTime.fromFormat(date.trim(), INVOICE_DATE_FORMAT, dateTimeOpts));
return {
start,
end: end.endOf('day'),
};
};

0 comments on commit c0c1b69

Please sign in to comment.