Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
37825 amit 1
package com.smartdukaan.cron.migrations;
2
 
3
import com.spice.profitmandi.dao.entity.warehouse.WarehousePurchaseOrder;
4
import org.apache.logging.log4j.LogManager;
5
import org.apache.logging.log4j.Logger;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.stereotype.Component;
8
 
9
import java.util.ArrayList;
10
import java.util.LinkedHashSet;
11
import java.util.List;
12
import java.util.Set;
13
 
14
/**
15
 * Bills internal movement transactions one at a time through MovementBiller. Like InternalGrnTask it carries no
16
 * transaction of its own, so each transaction bills (or fails) on its own and a self invocation never skips the proxy.
17
 */
18
@Component
19
public class MovementBillingTask {
20
 
21
    private static final Logger LOGGER = LogManager.getLogger(MovementBillingTask.class);
22
 
23
    @Autowired
24
    private MovementBiller movementBiller;
25
 
26
    public void billMovements(List<String> poNumbers, String operatorEmail,
27
                              int logisticsProviderId, boolean live) {
28
        Set<Integer> transactions = new LinkedHashSet<>();
37828 amit 29
        for (WarehousePurchaseOrder po : movementBiller.purchaseOrders(poNumbers)) {
37825 amit 30
            if (po.getTransactionId() == null) {
31
                LOGGER.warn("SKIPPED  PO {} raised no internal order (no transaction)", po.getPoNumber());
32
            } else {
33
                transactions.add(po.getTransactionId());
34
            }
35
        }
36
        LOGGER.info("=== Movement billing {} : {} transaction(s), operator {}, logistics provider {} ===",
37
                live ? "LIVE RUN" : "DRY RUN (no writes)", transactions.size(), operatorEmail, logisticsProviderId);
38
 
39
        List<String> invoices = new ArrayList<>();
40
        int ready = 0;
41
        int blocked = 0;
42
        int failed = 0;
43
        for (Integer transactionId : transactions) {
44
            try {
45
                MovementBiller.Plan plan = movementBiller.describe(transactionId);
46
                this.log(plan);
47
                if (!plan.problems.isEmpty()) {
48
                    blocked++;
49
                    continue;
50
                }
51
                if (!live) {
52
                    ready++;
53
                    continue;
54
                }
55
                invoices.add(movementBiller.bill(transactionId, operatorEmail, logisticsProviderId));
56
                ready++;
57
            } catch (Exception e) {
58
                failed++;
59
                LOGGER.error("FAILED   txn {} - {}", transactionId, e.getMessage(), e);
60
            }
61
        }
62
        LOGGER.info("=== Movement billing {} complete : {} {}, {} blocked, {} failed ===", live ? "LIVE RUN" : "DRY RUN",
63
                ready, live ? "billed" : "ready to bill", blocked, failed);
64
        if (!invoices.isEmpty()) {
65
            LOGGER.info("Invoices for --autoGrnInternal: --invoices={}", String.join(",", invoices));
66
        }
67
    }
68
 
69
    private void log(MovementBiller.Plan plan) {
70
        LOGGER.info("txn {} PO {} : {} -> {} : {} line(s), {} unit(s){}", plan.transactionId, plan.poNumber,
71
                plan.sourceWarehouseId, plan.destinationWarehouseId, plan.lines.size(),
72
                plan.lines.stream().mapToInt(x -> x.qty).sum(), plan.problems.isEmpty() ? "" : " - BLOCKED");
73
        for (MovementBiller.Line line : plan.lines) {
74
            LOGGER.info("   order {} {} x{} from {}{}{}", line.orderId, line.item, line.qty, line.fromWarehouseId,
75
                    line.repoint ? " (re-pointed from Dummy)" : "",
76
                    line.serialized ? " IMEIs " + String.join(",", line.imeis) : "");
77
        }
78
        for (String problem : plan.problems) {
79
            LOGGER.warn("   PROBLEM {}", problem);
80
        }
81
    }
82
}