Subversion Repositories SmartDukaan

Rev

Rev 37614 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
37613 amit 1
package com.smartdukaan.cron.migrations;
2
 
3
import org.apache.logging.log4j.LogManager;
4
import org.apache.logging.log4j.Logger;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.stereotype.Component;
7
 
8
import java.util.List;
9
 
10
/**
11
 * Receives stock into the destination warehouse for internal transfer invoices that were billed but never
37614 amit 12
 * GRNed. The receiving itself is InternalGrnReceiver, one invoice at a time.
37613 amit 13
 *
37614 amit 14
 * This driver carries no transaction of its own. It must not, and it must not hold the receiving logic
15
 * either: a transactional method called from within its own bean is a self invocation, which bypasses the
16
 * Spring proxy and runs with no transaction at all.
37613 amit 17
 */
18
@Component
19
public class InternalGrnTask {
20
 
21
    private static final Logger LOGGER = LogManager.getLogger(InternalGrnTask.class);
22
 
23
    @Autowired
37614 amit 24
    private InternalGrnReceiver internalGrnReceiver;
37613 amit 25
 
37617 amit 26
    @Autowired
27
    private InternalGrnReverser internalGrnReverser;
28
 
29
    /** Undoes receipts so their invoices can be received again; same one-at-a-time isolation as receiving. */
30
    public void reverseInternalInvoices(List<String> invoiceNumbers, boolean dryRun) {
31
        LOGGER.info("=== Internal GRN REVERSAL {} : {} invoice(s) ===",
32
                dryRun ? "DRY RUN (no writes)" : "LIVE RUN", invoiceNumbers.size());
33
        int reversed = 0;
34
        int skipped = 0;
35
        int failed = 0;
36
        for (String invoiceNumber : invoiceNumbers) {
37
            try {
38
                if (internalGrnReverser.reverseInvoice(invoiceNumber.trim(), dryRun)) {
39
                    reversed++;
40
                } else {
41
                    skipped++;
42
                }
43
            } catch (Exception e) {
44
                failed++;
45
                LOGGER.error("FAILED   {} - {}", invoiceNumber, e.getMessage(), e);
46
            }
47
        }
48
        LOGGER.info("=== Internal GRN REVERSAL {} complete : {} {}, {} skipped, {} failed ===",
49
                dryRun ? "DRY RUN" : "LIVE RUN", reversed, dryRun ? "would be reversed" : "reversed",
50
                skipped, failed);
51
    }
52
 
37613 amit 53
    public void grnInternalInvoices(List<String> invoiceNumbers, String operatorEmail, boolean dryRun) {
54
        LOGGER.info("=== Internal GRN {} : {} invoice(s), operator {} ===",
55
                dryRun ? "DRY RUN (no writes)" : "LIVE RUN", invoiceNumbers.size(), operatorEmail);
56
        int received = 0;
57
        int skipped = 0;
58
        int failed = 0;
59
        for (String invoiceNumber : invoiceNumbers) {
60
            try {
37614 amit 61
                if (internalGrnReceiver.grnInvoice(invoiceNumber.trim(), operatorEmail, dryRun)) {
37613 amit 62
                    received++;
63
                } else {
64
                    skipped++;
65
                }
66
            } catch (Exception e) {
67
                failed++;
68
                LOGGER.error("FAILED  {} - {}", invoiceNumber, e.getMessage(), e);
69
            }
70
        }
71
        LOGGER.info("=== Internal GRN {} complete : {} {}, {} skipped, {} failed ===",
72
                dryRun ? "DRY RUN" : "LIVE RUN", received, dryRun ? "would be received" : "received",
73
                skipped, failed);
74
    }
75
}