Skip to content

Commit 8a563fa

Browse files
authored
Merge pull request #39 from DavidAmunga/fix/paybill-datetime-parsing
2 parents 7c7ad16 + 9497a3a commit 8a563fa

File tree

4 files changed

+39
-50
lines changed

4 files changed

+39
-50
lines changed

.changeset/fresh-lies-post.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mpesa2csv": patch
3+
---
4+
5+
fix: datetime parsing for paybill statements

src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/pdfService.ts

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,15 @@ export class PdfService {
247247

248248
const receiptNo = receiptMatch[1].trim();
249249

250-
const timeMatch = block.match(/\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}/);
251-
const completionTime = timeMatch ? timeMatch[0].trim() : "";
250+
const dateMatch = block.match(/\d{4}-\d{2}-\d{2}/);
251+
const timeMatch = block.match(/\d{2}:\d{2}:\d{2}/);
252+
253+
let completionTime = "";
254+
if (dateMatch && timeMatch) {
255+
completionTime = `${dateMatch[0]} ${timeMatch[0]}`;
256+
} else if (dateMatch) {
257+
completionTime = dateMatch[0];
258+
}
252259

253260
const statusPattern =
254261
/\b(COMPLETED|FAILED|PENDING|Completed|Failed|Pending)\b/gi;
@@ -261,45 +268,15 @@ export class PdfService {
261268

262269
const status = lastStatusMatch ? lastStatusMatch[0].trim() : "Unknown";
263270

264-
let details = "";
265-
if (timeMatch) {
266-
const timeIndex = block.indexOf(timeMatch[0]) + timeMatch[0].length;
267-
268-
const amountPattern =
269-
/\d+,?\d*\.\d{2}\s+\d+,?\d*\.\d{2}\s+\d+,?\d*\.\d{2}/;
270-
const amountMatch = block.match(amountPattern);
271-
272-
if (amountMatch) {
273-
const amountStartIndex = block.indexOf(amountMatch[0]);
274-
const amountEndIndex = amountStartIndex + amountMatch[0].length;
275-
276-
const basicDetails = block
277-
.substring(timeIndex, amountStartIndex)
278-
.trim()
279-
.replace(/\b(COMPLETED|FAILED|PENDING)\b/gi, "")
280-
.trim();
281-
282-
const additionalDetails = block
283-
.substring(amountEndIndex)
284-
.trim()
285-
.replace(/\b(COMPLETED|FAILED|PENDING)\b/gi, "")
286-
.trim();
287-
288-
if (basicDetails && additionalDetails) {
289-
details = `${basicDetails}\n${additionalDetails}`;
290-
} else if (basicDetails) {
291-
details = basicDetails;
292-
} else if (additionalDetails) {
293-
details = additionalDetails;
294-
}
295-
} else {
296-
details = block
297-
.substring(timeIndex)
298-
.trim()
299-
.replace(/\b(COMPLETED|FAILED|PENDING)\b/gi, "")
300-
.trim();
301-
}
302-
}
271+
// Extract details by removing receipt number, date/time, status, and amounts
272+
let details = block
273+
.replace(new RegExp(receiptNo, "g"), "")
274+
.replace(/\d{4}-\d{2}-\d{2}/g, "")
275+
.replace(/\d{2}:\d{2}:\d{2}/g, "")
276+
.replace(/\b(COMPLETED|FAILED|PENDING)\b/gi, "")
277+
.replace(/[-]?[\d,]+\.\d{2}/g, "")
278+
.replace(/\s+/g, " ")
279+
.trim();
303280

304281
if (!details.trim()) {
305282
details = this.extractDetailsFromBlock(
@@ -363,17 +340,18 @@ export class PdfService {
363340

364341
if (
365342
details.toLowerCase().includes("business payment from") ||
343+
details.toLowerCase().includes("merchant payment from") ||
366344
details.toLowerCase().includes("received") ||
367-
(details.toLowerCase().includes("customer transfer") &&
368-
parsedAmounts[0] > 0)
345+
details.toLowerCase().includes("customer transfer") ||
346+
details.toLowerCase().includes("b2c payment")
369347
) {
370348
paidIn = parsedAmounts[0];
371349
if (parsedAmounts.length > 2) {
372350
withdrawn =
373351
parsedAmounts[1] === balance ? null : parsedAmounts[1];
374352
}
375-
} else {
376-
withdrawn = parsedAmounts[0];
353+
} else {
354+
withdrawn = parsedAmounts[0];
377355
if (parsedAmounts.length > 2) {
378356
paidIn = parsedAmounts[1] === balance ? null : parsedAmounts[1];
379357
}
@@ -384,7 +362,10 @@ export class PdfService {
384362

385363
if (
386364
details.toLowerCase().includes("business payment from") ||
387-
details.toLowerCase().includes("received")
365+
details.toLowerCase().includes("merchant payment from") ||
366+
details.toLowerCase().includes("received") ||
367+
details.toLowerCase().includes("customer transfer") ||
368+
details.toLowerCase().includes("b2c payment")
388369
) {
389370
paidIn = firstAmount;
390371
} else {
@@ -414,6 +395,7 @@ export class PdfService {
414395

415396
if (
416397
details.toLowerCase().includes("business payment from") ||
398+
details.toLowerCase().includes("merchant payment from") ||
417399
details.toLowerCase().includes("funds received") ||
418400
details.toLowerCase().includes("customer transfer") ||
419401
details.toLowerCase().includes("b2c payment")
@@ -426,10 +408,12 @@ export class PdfService {
426408
}
427409
}
428410

411+
let finalDetails = details.replace(/Disclaimer:[\s\S]*$/, "").trim();
412+
429413
const transaction: Transaction = {
430414
receiptNo,
431415
completionTime,
432-
details,
416+
details: finalDetails,
433417
transactionStatus: status,
434418
paidIn,
435419
withdrawn,

src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export enum ExportFormat {
2929
}
3030

3131
export enum SortOrder {
32-
DESC="desc",
33-
ASC="asc",
32+
DESC = "desc",
33+
ASC = "asc",
3434
}
3535

3636
export interface ExportOptions {

0 commit comments

Comments
 (0)