Subversion Repositories SmartDukaan

Rev

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

package com.smartdukaan.cron.migrations;

import com.spice.profitmandi.dao.entity.catalog.Item;
import com.spice.profitmandi.dao.model.InternalMovementAllocationModel;
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;
import java.util.Map;

/**
 * Raises an internal movement PO from an upload file through MovementPoCreator. Without --live it only prints the POs
 * the file would be raised as - one per cost, margin-scheme items apart - with every line's price and origin. Like
 * MovementBillingTask it carries no transaction, so the PO commits before billing reads it.
 */
@Component
public class MovementPoTask {

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

    @Autowired
    private MovementPoCreator movementPoCreator;

    @Autowired
    private MovementBillingTask movementBillingTask;

    public void createMovementPo(String file, int warehouseId, int supplierId, String operatorEmail, boolean live,
                                 boolean bill, int logisticsProviderId) throws Exception {
        Map<Integer, Integer> quantities = MovementPoCreator.readFile(file);
        LOGGER.info("=== Movement PO {} : {} : {} item(s), {} unit(s) -> warehouse {}, supplier {}, operator {} ===",
                live ? "LIVE RUN" : "DRY RUN (no writes)", file, quantities.size(),
                quantities.values().stream().mapToInt(Integer::intValue).sum(), warehouseId, supplierId, operatorEmail);

        MovementPoCreator.Preview preview = movementPoCreator.preview(warehouseId, supplierId, quantities);
        this.log(preview);
        if (!preview.problems.isEmpty()) {
            LOGGER.warn("=== Movement PO BLOCKED : {} problem(s), nothing raised ===", preview.problems.size());
            return;
        }
        if (!live) {
            LOGGER.info("=== Movement PO DRY RUN complete : would raise {} PO(s). Add --live to raise them ===",
                    preview.purchaseOrders.size());
            return;
        }

        List<String> poNumbers = movementPoCreator.create(warehouseId, supplierId, quantities, operatorEmail);
        LOGGER.info("=== Movement PO LIVE RUN complete : raised {} ===", String.join(",", poNumbers));
        LOGGER.info("Next: --autoBillMovement --pos={} --email={} --provider={}", String.join(",", poNumbers),
                operatorEmail, logisticsProviderId);
        if (bill) {
            movementBillingTask.billMovements(poNumbers, operatorEmail, logisticsProviderId, false);
        }
    }

    private void log(MovementPoCreator.Preview preview) {
        for (String problem : preview.problems) {
            LOGGER.warn("   PROBLEM {}", problem);
        }
        int poCount = preview.purchaseOrders.size();
        int totalUnits = 0;
        double totalValue = 0;
        for (int i = 0; i < poCount; i++) {
            List<InternalMovementAllocationModel> lines = preview.purchaseOrders.get(i);
            int units = 0;
            double value = 0;
            Integer vendorWarehouseId = lines.isEmpty() ? null : lines.get(0).getVendorWarehouseId();
            LOGGER.info("PO {} of {} from {} vendor warehouse {} : {} line(s)", i + 1, poCount, preview.supplierName,
                    vendorWarehouseId == null ? "NONE (stock still to arrive - parks on the Dummy)" : vendorWarehouseId,
                    lines.size());
            for (InternalMovementAllocationModel line : lines) {
                Item item = preview.items.get(line.getItemId());
                double lineValue = (double) line.getTransferPrice() * line.getQty();
                LOGGER.info(String.format("   %-6d %-55.55s x%-5d @ %10.2f = %12.2f  %s (%s)", line.getItemId(),
                        item == null ? "?" : item.getItemDescription(), line.getQty(), line.getTransferPrice(),
                        lineValue, line.getOriginVendorName(), line.getOriginSource()));
                units += line.getQty();
                value += lineValue;
            }
            LOGGER.info(String.format("   PO %d total : %d unit(s), %.2f", i + 1, units, value));
            totalUnits += units;
            totalValue += value;
        }
        if (poCount > 0) {
            LOGGER.info(String.format("ALL %d PO(s) : %d unit(s), %.2f", poCount, totalUnits, totalValue));
        }
    }
}