Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
37827 amit 1
package com.smartdukaan.cron.migrations;
2
 
3
import com.spice.profitmandi.dao.entity.catalog.Item;
4
import com.spice.profitmandi.dao.model.InternalMovementAllocationModel;
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.List;
11
import java.util.Map;
12
 
13
/**
14
 * Raises an internal movement PO from an upload file through MovementPoCreator. Without --live it only prints the POs
15
 * the file would be raised as - one per cost, margin-scheme items apart - with every line's price and origin. Like
16
 * MovementBillingTask it carries no transaction, so the PO commits before billing reads it.
17
 */
18
@Component
19
public class MovementPoTask {
20
 
21
    private static final Logger LOGGER = LogManager.getLogger(MovementPoTask.class);
22
 
23
    @Autowired
24
    private MovementPoCreator movementPoCreator;
25
 
26
    @Autowired
27
    private MovementBillingTask movementBillingTask;
28
 
29
    public void createMovementPo(String file, int warehouseId, int supplierId, String operatorEmail, boolean live,
30
                                 boolean bill, int logisticsProviderId) throws Exception {
31
        Map<Integer, Integer> quantities = MovementPoCreator.readFile(file);
32
        LOGGER.info("=== Movement PO {} : {} : {} item(s), {} unit(s) -> warehouse {}, supplier {}, operator {} ===",
33
                live ? "LIVE RUN" : "DRY RUN (no writes)", file, quantities.size(),
34
                quantities.values().stream().mapToInt(Integer::intValue).sum(), warehouseId, supplierId, operatorEmail);
35
 
36
        MovementPoCreator.Preview preview = movementPoCreator.preview(warehouseId, supplierId, quantities);
37
        this.log(preview);
38
        if (!preview.problems.isEmpty()) {
39
            LOGGER.warn("=== Movement PO BLOCKED : {} problem(s), nothing raised ===", preview.problems.size());
40
            return;
41
        }
42
        if (!live) {
43
            LOGGER.info("=== Movement PO DRY RUN complete : would raise {} PO(s). Add --live to raise them ===",
44
                    preview.purchaseOrders.size());
45
            return;
46
        }
47
 
48
        List<String> poNumbers = movementPoCreator.create(warehouseId, supplierId, quantities, operatorEmail);
49
        LOGGER.info("=== Movement PO LIVE RUN complete : raised {} ===", String.join(",", poNumbers));
50
        LOGGER.info("Next: --autoBillMovement --pos={} --email={} --provider={}", String.join(",", poNumbers),
51
                operatorEmail, logisticsProviderId);
52
        if (bill) {
53
            movementBillingTask.billMovements(poNumbers, operatorEmail, logisticsProviderId, false);
54
        }
55
    }
56
 
37832 amit 57
    /** Closes movement POs one by one, each in its own transaction, the way the PO screen's Close does. */
58
    public void closeMovementPos(List<String> poNumbers, String closedBy, boolean live) {
59
        LOGGER.info("=== Close movement PO {} : {} PO(s), by {} ===", live ? "LIVE RUN" : "DRY RUN (no writes)",
60
                poNumbers.size(), closedBy);
61
        int failed = 0;
62
        for (String poNumber : poNumbers) {
63
            try {
64
                LOGGER.info("   {}", movementPoCreator.close(poNumber, closedBy, live));
65
            } catch (Exception e) {
66
                failed++;
67
                LOGGER.error("   FAILED   {} - {}", poNumber, e.getMessage(), e);
68
            }
69
        }
70
        LOGGER.info("=== Close movement PO {} complete : {} ok, {} failed ===", live ? "LIVE RUN" : "DRY RUN",
71
                poNumbers.size() - failed, failed);
72
    }
73
 
37827 amit 74
    private void log(MovementPoCreator.Preview preview) {
75
        for (String problem : preview.problems) {
76
            LOGGER.warn("   PROBLEM {}", problem);
77
        }
78
        int poCount = preview.purchaseOrders.size();
79
        int totalUnits = 0;
80
        double totalValue = 0;
81
        for (int i = 0; i < poCount; i++) {
82
            List<InternalMovementAllocationModel> lines = preview.purchaseOrders.get(i);
83
            int units = 0;
84
            double value = 0;
37831 amit 85
            Integer vendorWarehouseId = lines.isEmpty() ? null : lines.get(0).getVendorWarehouseId();
86
            LOGGER.info("PO {} of {} from {} vendor warehouse {} : {} line(s)", i + 1, poCount, preview.supplierName,
87
                    vendorWarehouseId == null ? "NONE (stock still to arrive - parks on the Dummy)" : vendorWarehouseId,
88
                    lines.size());
37827 amit 89
            for (InternalMovementAllocationModel line : lines) {
90
                Item item = preview.items.get(line.getItemId());
91
                double lineValue = (double) line.getTransferPrice() * line.getQty();
92
                LOGGER.info(String.format("   %-6d %-55.55s x%-5d @ %10.2f = %12.2f  %s (%s)", line.getItemId(),
93
                        item == null ? "?" : item.getItemDescription(), line.getQty(), line.getTransferPrice(),
94
                        lineValue, line.getOriginVendorName(), line.getOriginSource()));
95
                units += line.getQty();
96
                value += lineValue;
97
            }
98
            LOGGER.info(String.format("   PO %d total : %d unit(s), %.2f", i + 1, units, value));
99
            totalUnits += units;
100
            totalValue += value;
101
        }
102
        if (poCount > 0) {
103
            LOGGER.info(String.format("ALL %d PO(s) : %d unit(s), %.2f", poCount, totalUnits, totalValue));
104
        }
105
    }
106
}