CREATE OR REPLACE VIEW public.vw_utility_shipment_fee_allocation AS
 WITH physical_shipment_lots AS (
         SELECT l.lot_shipment_in AS shipment_id,
            l.id AS lot_id,
            l.lot_type,
            l.line AS purchase_line_id,
            l.sale_line AS sale_line_id,
            qh.quantity AS shipment_quantity,
            l.lot_unit_line AS shipment_unit_id
           FROM (lot_lot l
             LEFT JOIN lot_qt_hist qh ON (((qh.lot = l.id) AND (qh.quantity_type = l.lot_state))))
          WHERE (((l.lot_type)::text = 'physic'::text) AND (l.lot_shipment_in IS NOT NULL))
        ), virtual_shipment_lots AS (
         SELECT q.lot_shipment_in AS shipment_id,
            vp.id AS lot_id,
            vp.lot_type,
            vp.line AS purchase_line_id,
            COALESCE(vp.sale_line, ps.sale_line) AS sale_line_id,
            q.lot_quantity AS shipment_quantity,
            q.lot_unit AS shipment_unit_id
           FROM ((lot_qt q
             JOIN lot_lot vp ON (((vp.id = q.lot_p) AND ((vp.lot_type)::text = 'virtual'::text))))
             LEFT JOIN lot_lot ps ON ((ps.id = q.lot_s)))
          WHERE ((q.lot_shipment_in IS NOT NULL) AND (NOT (EXISTS ( SELECT 1
                   FROM physical_shipment_lots pl
                  WHERE ((pl.shipment_id = q.lot_shipment_in) AND (pl.purchase_line_id = vp.line) AND ((pl.sale_line_id = COALESCE(vp.sale_line, ps.sale_line)) OR (COALESCE(vp.sale_line, ps.sale_line) IS NULL)))))))
        ), shipment_lots AS (
         SELECT physical_shipment_lots.shipment_id,
            physical_shipment_lots.lot_id,
            physical_shipment_lots.lot_type,
            physical_shipment_lots.purchase_line_id,
            physical_shipment_lots.sale_line_id,
            physical_shipment_lots.shipment_quantity,
            physical_shipment_lots.shipment_unit_id
           FROM physical_shipment_lots
        UNION ALL
         SELECT virtual_shipment_lots.shipment_id,
            virtual_shipment_lots.lot_id,
            virtual_shipment_lots.lot_type,
            virtual_shipment_lots.purchase_line_id,
            virtual_shipment_lots.sale_line_id,
            virtual_shipment_lots.shipment_quantity,
            virtual_shipment_lots.shipment_unit_id
           FROM virtual_shipment_lots
        ), shipment_context AS (
         SELECT shipment_lots.shipment_id,
            shipment_lots.purchase_line_id,
            shipment_lots.sale_line_id,
            shipment_lots.shipment_unit_id,
            sum(COALESCE(shipment_lots.shipment_quantity, (0)::numeric)) AS shipment_quantity
           FROM shipment_lots
          WHERE (shipment_lots.shipment_id IS NOT NULL)
          GROUP BY shipment_lots.shipment_id, shipment_lots.purchase_line_id, shipment_lots.sale_line_id, shipment_lots.shipment_unit_id
        ), shipment_base AS (
         SELECT shipment_context.shipment_id,
            sum(shipment_context.shipment_quantity) AS shipment_quantity
           FROM shipment_context
          GROUP BY shipment_context.shipment_id
        ), shipment_fee_candidates AS (
         SELECT f_1.id AS fee_id,
            'Shipment'::text AS fee_source,
            f_1.type AS fee_type,
                CASE
                    WHEN ((f_1.type)::text = 'ordered'::text) THEN 1
                    WHEN ((f_1.type)::text = 'scheduled'::text) THEN 2
                    ELSE NULL::integer
                END AS priority,
            sb.shipment_id,
            NULL::integer AS purchase_line_id,
            NULL::integer AS sale_line_id,
            f_1.product AS product_id,
            f_1.supplier AS supplier_id,
            f_1.mode AS packaging,
            f_1.p_r AS pay_or_rec,
            f_1.state,
            f_1.weight_type,
            COALESCE(f_1.quantity, (0)::numeric) AS requested_quantity,
            sb.shipment_quantity,
            f_1.price AS fee_price,
            f_1.currency AS currency_id,
            f_1.unit AS unit_id,
                CASE
                    WHEN (upper((f_1.p_r)::text) = 'REC'::text) THEN 1
                    ELSE '-1'::integer
                END AS sign_multiplier
           FROM (shipment_base sb
             JOIN fee_fee f_1 ON ((f_1.shipment_in = sb.shipment_id)))
          WHERE ((f_1.type)::text = ANY ((ARRAY['ordered'::character varying, 'scheduled'::character varying])::text[]))
        ), shipment_fee_allocated AS (
         SELECT c.fee_id,
            c.fee_source,
            c.fee_type,
            c.priority,
            c.shipment_id,
            c.purchase_line_id,
            c.sale_line_id,
            c.product_id,
            c.supplier_id,
            c.packaging,
            c.pay_or_rec,
            c.state,
            c.weight_type,
            c.requested_quantity,
            c.shipment_quantity,
            c.fee_price,
            c.currency_id,
            c.unit_id,
            c.sign_multiplier,
            LEAST(c.requested_quantity, GREATEST((c.shipment_quantity - COALESCE(sum(c.requested_quantity) OVER (PARTITION BY c.shipment_id, c.product_id, c.supplier_id ORDER BY c.priority, c.fee_id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), (0)::numeric)), (0)::numeric)) AS allocated_quantity
           FROM shipment_fee_candidates c
        ), shipment_fee_coverage AS (
         SELECT shipment_fee_allocated.shipment_id,
            shipment_fee_allocated.product_id,
            shipment_fee_allocated.supplier_id,
            max(shipment_fee_allocated.shipment_quantity) AS shipment_quantity,
            sum(shipment_fee_allocated.allocated_quantity) AS allocated_quantity
           FROM shipment_fee_allocated
          GROUP BY shipment_fee_allocated.shipment_id, shipment_fee_allocated.product_id, shipment_fee_allocated.supplier_id
        ), contract_budgeted_fee_candidates AS (
         SELECT f_1.id AS fee_id,
            'Purchase Contract'::text AS fee_source,
            f_1.type AS fee_type,
            3 AS priority,
            sc.shipment_id,
            sc.purchase_line_id,
            NULL::integer AS sale_line_id,
            f_1.product AS product_id,
            f_1.supplier AS supplier_id,
            f_1.mode AS packaging,
            f_1.p_r AS pay_or_rec,
            f_1.state,
            f_1.weight_type,
            sc.shipment_quantity,
            f_1.price AS fee_price,
            f_1.currency AS currency_id,
            COALESCE(f_1.unit, sc.shipment_unit_id) AS unit_id,
                CASE
                    WHEN (upper((f_1.p_r)::text) = 'REC'::text) THEN '-1'::integer
                    ELSE 1
                END AS sign_multiplier
           FROM (shipment_context sc
             JOIN fee_fee f_1 ON ((f_1.line = sc.purchase_line_id)))
          WHERE (((f_1.type)::text = 'budgeted'::text) AND (sc.purchase_line_id IS NOT NULL))
        UNION ALL
         SELECT f_1.id AS fee_id,
            'Sale Contract'::text AS fee_source,
            f_1.type AS fee_type,
            3 AS priority,
            sc.shipment_id,
            NULL::integer AS purchase_line_id,
            sc.sale_line_id,
            f_1.product AS product_id,
            f_1.supplier AS supplier_id,
            f_1.mode AS packaging,
            f_1.p_r AS pay_or_rec,
            f_1.state,
            f_1.weight_type,
            sc.shipment_quantity,
            f_1.price AS fee_price,
            f_1.currency AS currency_id,
            COALESCE(f_1.unit, sc.shipment_unit_id) AS unit_id,
                CASE
                    WHEN (upper((f_1.p_r)::text) = 'REC'::text) THEN 1
                    ELSE '-1'::integer
                END AS sign_multiplier
           FROM (shipment_context sc
             JOIN fee_fee f_1 ON ((f_1.sale_line = sc.sale_line_id)))
          WHERE (((f_1.type)::text = 'budgeted'::text) AND (sc.sale_line_id IS NOT NULL))
        ), contract_budgeted_allocated AS (
         SELECT c.fee_id,
            c.fee_source,
            c.fee_type,
            c.priority,
            c.shipment_id,
            c.purchase_line_id,
            c.sale_line_id,
            c.product_id,
            c.supplier_id,
            c.packaging,
            c.pay_or_rec,
            c.state,
            c.weight_type,
            c.shipment_quantity,
            c.fee_price,
            c.currency_id,
            c.unit_id,
            c.sign_multiplier,
            GREATEST((c.shipment_quantity - COALESCE(fc.allocated_quantity, (0)::numeric)), (0)::numeric) AS allocated_quantity
           FROM (contract_budgeted_fee_candidates c
             LEFT JOIN shipment_fee_coverage fc ON (((fc.shipment_id = c.shipment_id) AND (fc.product_id = c.product_id) AND (fc.supplier_id = c.supplier_id))))
        ), final_fees AS (
         SELECT shipment_fee_allocated.fee_id,
            shipment_fee_allocated.fee_source,
            shipment_fee_allocated.fee_type,
            shipment_fee_allocated.priority,
            shipment_fee_allocated.shipment_id,
            shipment_fee_allocated.purchase_line_id,
            shipment_fee_allocated.sale_line_id,
            shipment_fee_allocated.product_id,
            shipment_fee_allocated.supplier_id,
            shipment_fee_allocated.packaging,
            shipment_fee_allocated.pay_or_rec,
            shipment_fee_allocated.state,
            shipment_fee_allocated.weight_type,
            shipment_fee_allocated.allocated_quantity AS fee_quantity,
            shipment_fee_allocated.fee_price,
            shipment_fee_allocated.currency_id,
            shipment_fee_allocated.unit_id,
            shipment_fee_allocated.sign_multiplier
           FROM shipment_fee_allocated
          WHERE (shipment_fee_allocated.allocated_quantity > (0)::numeric)
        UNION ALL
         SELECT contract_budgeted_allocated.fee_id,
            contract_budgeted_allocated.fee_source,
            contract_budgeted_allocated.fee_type,
            contract_budgeted_allocated.priority,
            contract_budgeted_allocated.shipment_id,
            contract_budgeted_allocated.purchase_line_id,
            contract_budgeted_allocated.sale_line_id,
            contract_budgeted_allocated.product_id,
            contract_budgeted_allocated.supplier_id,
            contract_budgeted_allocated.packaging,
            contract_budgeted_allocated.pay_or_rec,
            contract_budgeted_allocated.state,
            contract_budgeted_allocated.weight_type,
            contract_budgeted_allocated.allocated_quantity AS fee_quantity,
            contract_budgeted_allocated.fee_price,
            contract_budgeted_allocated.currency_id,
            contract_budgeted_allocated.unit_id,
            contract_budgeted_allocated.sign_multiplier
           FROM contract_budgeted_allocated
          WHERE (contract_budgeted_allocated.allocated_quantity > (0)::numeric)
        )
 SELECT f.shipment_id AS "intShipmentId",
    f.fee_id AS "intFeeId",
    f.fee_source AS "strFeeSource",
    f.fee_type AS "strFeeType",
    f.priority AS "intPriority",
    f.purchase_line_id AS "intPurchaseLineId",
    f.sale_line_id AS "intSaleLineId",
    f.product_id AS "intProductId",
    p.code AS "strFee",
    f.supplier_id AS "intSupplierId",
    sup.name AS "strSupplier",
    f.packaging AS "strPackaging",
    f.pay_or_rec AS "strPayOrRec",
    f.state AS "strState",
        CASE
            WHEN (upper((f.weight_type)::text) = 'BRUT'::text) THEN 'Gross'::text
            ELSE 'Net'::text
        END AS "strWeighingType",
    f.fee_quantity AS "dblQuantity",
    (f.fee_price * (f.sign_multiplier)::numeric) AS "dblPrice",
    cur.name AS "strCurrency",
    COALESCE(uom.name, 'Mt'::character varying) AS "strUnit",
    ((f.fee_quantity * f.fee_price) * (f.sign_multiplier)::numeric) AS "dblAmount",
        CASE
            WHEN (upper((p.code)::text) ~~ '%FREIGHT%'::text) THEN 'Freight'::text
            WHEN (upper((p.code)::text) ~~ '%PROFIT SHARING%'::text) THEN 'ProfitSharing'::text
            ELSE 'OtherCosts'::text
        END AS "strCostGroup"
   FROM ((((final_fees f
     JOIN product_product p ON ((p.id = f.product_id)))
     JOIN party_party sup ON ((sup.id = f.supplier_id)))
     LEFT JOIN currency_currency cur ON ((cur.id = f.currency_id)))
     LEFT JOIN product_uom uom ON ((uom.id = f.unit_id)))
  ORDER BY f.shipment_id, f.product_id, f.supplier_id, f.priority, f.fee_id;;
