Subversion Repositories SmartDukaan

Rev

Rev 37831 | Go to most recent revision | Details | 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
 
57
    private void log(MovementPoCreator.Preview preview) {
58
        for (String problem : preview.problems) {
59
            LOGGER.warn("   PROBLEM {}", problem);
60
        }
61
        int poCount = preview.purchaseOrders.size();
62
        int totalUnits = 0;
63
        double totalValue = 0;
64
        for (int i = 0; i < poCount; i++) {
65
            List<InternalMovementAllocationModel> lines = preview.purchaseOrders.get(i);
66
            int units = 0;
67
            double value = 0;
68
            LOGGER.info("PO {} of {} from {} : {} line(s)", i + 1, poCount, preview.supplierName, lines.size());
69
            for (InternalMovementAllocationModel line : lines) {
70
                Item item = preview.items.get(line.getItemId());
71
                double lineValue = (double) line.getTransferPrice() * line.getQty();
72
                LOGGER.info(String.format("   %-6d %-55.55s x%-5d @ %10.2f = %12.2f  %s (%s)", line.getItemId(),
73
                        item == null ? "?" : item.getItemDescription(), line.getQty(), line.getTransferPrice(),
74
                        lineValue, line.getOriginVendorName(), line.getOriginSource()));
75
                units += line.getQty();
76
                value += lineValue;
77
            }
78
            LOGGER.info(String.format("   PO %d total : %d unit(s), %.2f", i + 1, units, value));
79
            totalUnits += units;
80
            totalValue += value;
81
        }
82
        if (poCount > 0) {
83
            LOGGER.info(String.format("ALL %d PO(s) : %d unit(s), %.2f", poCount, totalUnits, totalValue));
84
        }
85
    }
86
}