File size: 5,500 Bytes
149698e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe203ef
149698e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe203ef
149698e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
173
174
175
176
177
178
179
import PDFDocument from 'pdfkit';
import type { InteracTransaction } from '@icc/shared';

const ICC_BLUE = '#1773cf';
const GRAY_TEXT = '#4B5563';
const LIGHT_GRAY = '#F3F4F6';

function formatCurrencyPdf(amount: number): string {
  return new Intl.NumberFormat('fr-CA', {
    style: 'currency',
    currency: 'CAD',
    minimumFractionDigits: 2,
  }).format(amount);
}

function formatDatePdf(dateStr: string): string {
  try {
    return new Intl.DateTimeFormat('fr-CA', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
    }).format(new Date(dateStr));
  } catch {
    return dateStr;
  }
}

const STATUS_LABELS: Record<string, string> = {
  deposited: 'Déposé ✓',
  pending: 'En attente',
  expired: 'Expiré',
  cancelled: 'Annulé',
};

export function generateReceipt(transaction: InteracTransaction): Promise<Buffer> {
  return new Promise((resolve, reject) => {
    const doc = new PDFDocument({
      size: 'A4',
      margin: 50,
      info: {
        Title: `Reçu - ${transaction.reference || 'N/A'}`,
        Author: 'ICC Interac Manager',
      },
    });

    const chunks: Buffer[] = [];
    doc.on('data', (chunk: Buffer) => chunks.push(chunk));
    doc.on('end', () => resolve(Buffer.concat(chunks)));
    doc.on('error', reject);

    const pageWidth = doc.page.width - 100;
    const startX = 50;
    let y = 50;

    // Header background
    doc.rect(startX, y, pageWidth, 80).fill(ICC_BLUE);

    // Header text
    doc.fillColor('#FFFFFF')
      .font('Helvetica-Bold')
      .fontSize(22)
      .text('ICC AMÉRIQUES', startX, y + 15, { width: pageWidth, align: 'center' });

    doc.fontSize(12)
      .font('Helvetica')
      .text('Reçu de virement Interac', startX, y + 45, { width: pageWidth, align: 'center' });

    y += 100;

    // Separator line
    doc.moveTo(startX, y).lineTo(startX + pageWidth, y).strokeColor(ICC_BLUE).lineWidth(2).stroke();
    y += 20;

    // Transaction details
    const fields: [string, string][] = [
      ['Date', formatDatePdf(transaction.date)],
      ['Expéditeur', transaction.sender],
      ['Montant', formatCurrencyPdf(transaction.amount)],
      ['Devise', transaction.currency || 'CAD'],
      ['Référence', transaction.reference || 'N/A'],
      ['Message', transaction.message || '—'],
      ['Succursale', (transaction as any).branch || 'Montreal'],
      ['Statut', STATUS_LABELS[transaction.status] || transaction.status],
    ];

    for (const [label, value] of fields) {
      // Alternating row background
      if (fields.indexOf([label, value] as any) % 2 === 0) {
        doc.rect(startX, y - 5, pageWidth, 30).fill(LIGHT_GRAY);
      }

      doc.fillColor(GRAY_TEXT)
        .font('Helvetica-Bold')
        .fontSize(11)
        .text(`${label}:`, startX + 15, y, { width: 150 });

      doc.fillColor('#111827')
        .font('Helvetica')
        .fontSize(11)
        .text(value, startX + 170, y, { width: pageWidth - 185 });

      y += 30;
    }

    y += 10;

    // Footer separator
    doc.moveTo(startX, y).lineTo(startX + pageWidth, y).strokeColor('#E5E7EB').lineWidth(1).stroke();
    y += 15;

    // Footer
    doc.fillColor(GRAY_TEXT)
      .font('Helvetica')
      .fontSize(9)
      .text('Reçu généré automatiquement par ICC Interac Manager', startX, y, { width: pageWidth, align: 'center' });

    y += 15;
    doc.text(`Date de génération: ${new Date().toISOString().split('T')[0]}`, startX, y, { width: pageWidth, align: 'center' });

    doc.end();
  });
}

export async function generateBatchReceipts(txns: InteracTransaction[]): Promise<Buffer> {
  const doc = new PDFDocument({ size: 'A4', margin: 50 });
  const chunks: Buffer[] = [];

  return new Promise((resolve, reject) => {
    doc.on('data', (chunk: Buffer) => chunks.push(chunk));
    doc.on('end', () => resolve(Buffer.concat(chunks)));
    doc.on('error', reject);

    for (let i = 0; i < txns.length; i++) {
      if (i > 0) doc.addPage();

      const tx = txns[i];
      const pageWidth = doc.page.width - 100;
      const startX = 50;
      let y = 50;

      // Header
      doc.rect(startX, y, pageWidth, 60).fill(ICC_BLUE);
      doc.fillColor('#FFFFFF').font('Helvetica-Bold').fontSize(18)
        .text('ICC AMÉRIQUES', startX, y + 10, { width: pageWidth, align: 'center' });
      doc.fontSize(10).font('Helvetica')
        .text('Reçu de virement Interac', startX, y + 35, { width: pageWidth, align: 'center' });

      y += 75;

      const fields: [string, string][] = [
        ['Date', formatDatePdf(tx.date)],
        ['Expéditeur', tx.sender],
        ['Montant', formatCurrencyPdf(tx.amount)],
        ['Référence', tx.reference || 'N/A'],
        ['Message', tx.message || '—'],
        ['Succursale', (tx as any).branch || 'Montreal'],
        ['Statut', STATUS_LABELS[tx.status] || tx.status],
      ];

      for (const [label, value] of fields) {
        doc.fillColor(GRAY_TEXT).font('Helvetica-Bold').fontSize(10)
          .text(`${label}:`, startX + 15, y, { width: 130 });
        doc.fillColor('#111827').font('Helvetica').fontSize(10)
          .text(value, startX + 150, y, { width: pageWidth - 165 });
        y += 25;
      }

      y += 10;
      doc.fillColor(GRAY_TEXT).font('Helvetica').fontSize(8)
        .text(`Reçu ${i + 1}/${txns.length} — Généré le ${new Date().toISOString().split('T')[0]}`,
          startX, y, { width: pageWidth, align: 'center' });
    }

    doc.end();
  });
}