Subversion Repositories SmartDukaan

Rev

Rev 37758 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.dao.model;

import javax.persistence.*;

/**
 * Aging-stock breakdown per item, scoped to a specific vendor and grouped by
 * warehouse. Used by the PO stock-cover approval flow to show "does THIS vendor
 * already have aging stock of this item sitting in any of our warehouses?" so
 * the approver can see whether the same vendor's inventory can be redistributed
 * rather than buying fresh from them.
 *
 * <p><b>Vendor-scoped by design:</b> aging held by a DIFFERENT vendor at a
 * different warehouse is not our accountability against this PO — Green Fortune
 * doesn't own NSSPL's pile, so we don't surface NSSPL's aging when raising a
 * Green Fortune PO. Rule matches the same-vendor decision documented in the
 * project plan.
 *
 * <p>Same trace path as {@link ItemAgingStockByVendorModel}
 * ({@code inventoryItem.purchaseId -> purchase.poId -> purchaseorder.supplierId}).
 */
@Entity
@NamedNativeQueries({
        @NamedNativeQuery(
                name = "PoStockApproval.AgingBreakdown",
                query = "SELECT inv.itemId                          AS item_id," +
                        "       inv.currentWarehouseId              AS warehouse_id," +
                        "       po.supplierId                       AS vendor_id," +
                        "       COALESCE(SUM(inv.currentQuantity),0) AS aging_qty" +
                        " FROM warehouse.inventoryItem  inv" +
                        "   JOIN warehouse.purchase      pr ON pr.id = inv.purchaseId" +
                        "   JOIN warehouse.purchaseorder po ON po.id = pr.purchaseOrder_id" +
                        " WHERE inv.itemId IN (:itemIds)" +
                        "   AND po.supplierId = :vendorId" +
                        "   AND inv.currentQuantity > 0" +
                        "   AND pr.receivedOn <= :agingCutoff" +
                        " GROUP BY inv.itemId, inv.currentWarehouseId, po.supplierId" +
                        " HAVING SUM(inv.currentQuantity) > 0",
                resultSetMapping = "PoStockApprovalAgingBreakdown")
})
@SqlResultSetMappings({
        @SqlResultSetMapping(
                name = "PoStockApprovalAgingBreakdown",
                classes = {
                        @ConstructorResult(
                                targetClass = ItemAgingBreakdownModel.class,
                                columns = {
                                        @ColumnResult(name = "item_id", type = Integer.class),
                                        @ColumnResult(name = "warehouse_id", type = Integer.class),
                                        @ColumnResult(name = "vendor_id", type = Integer.class),
                                        @ColumnResult(name = "aging_qty", type = Long.class)
                                }
                        )
                }
        )
})
public class ItemAgingBreakdownModel {

    private Integer itemId;
    private Integer warehouseId;
    private Integer vendorId;
    private Long agingQty;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    public ItemAgingBreakdownModel() {
    }

    public ItemAgingBreakdownModel(Integer itemId, Integer warehouseId, Integer vendorId, Long agingQty) {
        this.itemId = itemId;
        this.warehouseId = warehouseId;
        this.vendorId = vendorId;
        this.agingQty = agingQty;
    }

    public Integer getItemId() {
        return itemId;
    }

    public Integer getWarehouseId() {
        return warehouseId;
    }

    public Integer getVendorId() {
        return vendorId;
    }

    public Long getAgingQty() {
        return agingQty;
    }
}