Subversion Repositories SmartDukaan

Rev

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