Subversion Repositories SmartDukaan

Rev

Rev 37613 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.migrations;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Receives stock into the destination warehouse for internal transfer invoices that were billed but never
 * GRNed. The receiving itself is InternalGrnReceiver, one invoice at a time.
 *
 * This driver carries no transaction of its own. It must not, and it must not hold the receiving logic
 * either: a transactional method called from within its own bean is a self invocation, which bypasses the
 * Spring proxy and runs with no transaction at all.
 */
@Component
public class InternalGrnTask {

    private static final Logger LOGGER = LogManager.getLogger(InternalGrnTask.class);

    @Autowired
    private InternalGrnReceiver internalGrnReceiver;

    public void grnInternalInvoices(List<String> invoiceNumbers, String operatorEmail, boolean dryRun) {
        LOGGER.info("=== Internal GRN {} : {} invoice(s), operator {} ===",
                dryRun ? "DRY RUN (no writes)" : "LIVE RUN", invoiceNumbers.size(), operatorEmail);
        int received = 0;
        int skipped = 0;
        int failed = 0;
        for (String invoiceNumber : invoiceNumbers) {
            try {
                if (internalGrnReceiver.grnInvoice(invoiceNumber.trim(), operatorEmail, dryRun)) {
                    received++;
                } else {
                    skipped++;
                }
            } catch (Exception e) {
                failed++;
                LOGGER.error("FAILED  {} - {}", invoiceNumber, e.getMessage(), e);
            }
        }
        LOGGER.info("=== Internal GRN {} complete : {} {}, {} skipped, {} failed ===",
                dryRun ? "DRY RUN" : "LIVE RUN", received, dryRun ? "would be received" : "received",
                skipped, failed);
    }
}