aboutsummaryrefslogtreecommitdiff
path: root/src/lib/generate.ts
blob: 8f90425b5a321e088a1a3b23285a2ddc1a2b8d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use server';

import { Invoice } from '@/models/invoice';
import PDFDocument from 'pdfkit';
import blobStream from 'blob-stream';

const MARGIN = 30;
const T_CELL_PADDING = 30;
const T_PADDING = 4;
const T_ROW_HEIGHT = 20;

const createTableHeader = (
  doc: PDFKit.PDFDocument,
  params: {
    itemText: string;
    quantityText: string;
    rateText: string;
    amountText: string;
  },
) => {
  doc.rect(doc.x, doc.y, doc.page.width - MARGIN * 2, T_ROW_HEIGHT).fill();
  doc.fontSize(10).fillColor('white');
  doc.text(params.itemText, MARGIN + T_PADDING, doc.y + 6);
  doc.moveUp();
  doc.text(
    params.quantityText,
    doc.page.width -
      MARGIN -
      doc.widthOfString(params.quantityText) -
      (doc.widthOfString(params.amountText) + T_CELL_PADDING) -
      (doc.widthOfString(params.rateText) + T_CELL_PADDING) -
      T_PADDING,
    doc.y,
  );
  doc.moveUp();
  doc.text(
    params.rateText,
    doc.page.width -
      MARGIN -
      (doc.widthOfString(params.amountText) + T_CELL_PADDING) -
      doc.widthOfString(params.rateText) -
      T_PADDING,
    doc.y,
  );
  doc.moveUp();
  doc.text(
    params.amountText,
    doc.page.width - MARGIN - doc.widthOfString(params.amountText) - T_PADDING,
    doc.y,
  );
  doc.fontSize(10).fillColor('black');
  doc.text('', MARGIN + T_PADDING, doc.y + T_ROW_HEIGHT / 2);
};

export async function generateInvoice(invoice: Invoice): Promise<Blob> {
  const doc = new PDFDocument({ size: 'A4', margin: MARGIN });
  const stream = doc.pipe(blobStream());

  // Invoice from
  doc.fontSize(10);
  doc.text(invoice.from);

  // Invlice number and date
  doc.moveUp();
  doc.fontSize(12);

  const invNumber = '# ' + invoice.invoice_number;
  doc.text(invNumber, doc.page.width - MARGIN - doc.widthOfString(invNumber));

  if (invoice.date) {
    doc.fontSize(10);
    const invDate = invoice.date.toLocaleDateString();
    doc.text(invDate, doc.page.width - MARGIN - doc.widthOfString(invDate));
  }

  // Separator
  doc.moveTo(MARGIN, doc.y + 5);
  doc.lineTo(doc.page.width - MARGIN, doc.y + 5).stroke();
  doc.moveDown();

  // Invoice title
  doc.fontSize(24);
  doc.text('INVOICE', MARGIN, doc.y + 3);
  doc.moveDown();

  // Bill to and details
  if (invoice.bill_to) {
    doc.fontSize(8);
    doc.text('BILL TO');
    doc.text(invoice.bill_to);
    doc.moveUp(2);
    doc.text('', MARGIN + doc.widthOfString('BILL TO') * 3, doc.y);
  }

  if (invoice.po_number) {
    doc.fontSize(8);
    doc.text('DETAILS');
    doc.text('Po number: ' + invoice.po_number);
    doc.text('', MARGIN, doc.y + 10);
  } else {
    doc.moveDown(2);
    doc.text('', MARGIN, doc.y + 10);
  }

  const params = {
    itemText: 'ITEM',
    quantityText: 'QUANTITY',
    rateText: 'RATE',
    amountText: 'AMOUNT',
  };

  createTableHeader(doc, params);

  const amountX = doc.page.width - MARGIN - T_PADDING;
  const rateX =
    amountX -
    doc.widthOfString(params.amountText) -
    T_CELL_PADDING -
    doc.widthOfString(params.rateText) / 2;
  const quantityX =
    rateX -
    doc.widthOfString(params.rateText) / 2 -
    T_CELL_PADDING -
    doc.widthOfString(params.quantityText) / 2;

  invoice.line_items.forEach((item) => {
    const amount = (item.quantity * item.rate).toLocaleString(undefined, {
      style: 'currency',
      currency: invoice.currency,
    });
    const quantity = item.quantity.toString();
    const rate = item.rate.toLocaleString(undefined, {
      style: 'currency',
      currency: invoice.currency,
    });
    doc.text(item.description);
    doc.moveUp();
    doc.text(amount, amountX - doc.widthOfString(amount), doc.y);
    doc.moveUp();
    doc.text(rate, rateX - doc.widthOfString(rate) / 2, doc.y);
    doc.moveUp();
    doc.text(quantity, quantityX - doc.widthOfString(quantity) / 2, doc.y);
    doc.text('', MARGIN + T_PADDING, doc.y + T_ROW_HEIGHT / 2);
  });

  const total = invoice.line_items.reduce(
    (acc, cur) => acc + (cur.quantity ?? 0) * (cur.rate ?? 0),
    0,
  );
  const totalText =
    'Total: ' +
    total.toLocaleString(undefined, {
      style: 'currency',
      currency: invoice.currency,
    });

  doc.moveDown(2);
  doc.fontSize(12);
  doc.text(
    totalText,
    doc.page.width - MARGIN - T_PADDING - doc.widthOfString(totalText),
  );

  doc.end();
  return new Promise((resolve, reject) => {
    stream.on('finish', () => {
      const blob = stream.toBlob();
      resolve(blob);
    });
    stream.on('error', reject);
  });
}