Fix #7123 - getting abra ready to test
[Pman.Xtuple] / pgsql / x-fifo-invfifo-update-from-invdetail-all.sql
1
2 -- run a whole itemsite
3 -- at present looks like the best way to update
4
5
6
7
8 -- alternative approaches.
9 -- loop each incomming
10 --> PO 's value as is.
11 --> transfers ... push active location to the one 
12 -->  loop through find all sales of it.. (or transfer outs..)
13 --> 
14
15 --> STACK?
16  --> 
17
18
19
20
21 CREATE OR REPLACE FUNCTION invfifo_update_from_invdetail_all_force(integer)
22     RETURNS  NUMERIC
23 AS $BODY$
24 DECLARE    
25     i_itemsite_id  ALIAS FOR $1;
26     v_ret NUMERIC;
27 BEGIN   
28     DELETE FROM invfifopos WHERE invfifopos_itemsite_id = i_itemsite_id ;
29     
30     SELECT invfifo_update_from_invdetail_all(i_itemsite_id) INTO v_ret;
31     
32    RETURN v_ret;
33    END;
34 $BODY$
35   LANGUAGE plpgsql VOLATILE
36   COST 100;
37   
38 ALTER FUNCTION   invfifo_update_from_invdetail_all_force(integer)
39   OWNER TO admin;
40
41
42 DROP FUNCTION IF EXISTS invfifo_update_from_invdetail_all(integer);
43
44 CREATE OR REPLACE FUNCTION invfifo_update_from_invdetail_all(integer)
45     RETURNS  NUMERIC
46 AS $BODY$
47 DECLARE    
48     i_itemsite_id  ALIAS FOR $1;
49     v_last_invdetail_id INTEGER;
50     v_last_checked_id INTEGER;
51     v_ret NUMERIC;
52  BEGIN   
53     -- do we need to do this...
54     
55     -- it basically check to see if the last run resulted in '0.0' adjustment.
56     -- if so, no need to run again..
57     
58     SELECT
59             COALESCE(max(invdetail_id),0)
60         INTO
61             v_last_invdetail_id
62         FROM
63             invdetailview
64         where
65             invhist_itemsite_id = i_itemsite_id;
66             
67             
68     SELECT
69             COALESCE(invfifopos_last_invdetail_id,0 )
70         INTO
71             v_last_checked_id
72         FROM
73             invfifopos
74         where
75             invfifopos_itemsite_id= i_itemsite_id
76             AND
77             invfifopos_lastadjustment = 0.0;
78     
79     IF v_last_checked_id = v_last_invdetail_id OR v_last_invdetail_id = 0 THEN
80         RAISE NOTICE 'skipping  - max tx matches last run';
81     
82         RETURN 0;
83     END IF;
84     -- the transfer out must appear before the transfer in..
85     -- the other way to handle this is to NOOP the transfer out
86     -- and 
87     SELECT SUM(ABS(invfifo_update_from_invdetail(invdetail_id, false)))
88         INTO v_ret
89     FROM (
90         SELECT
91                 invdetail_id
92             FROM
93                 invdetailview
94             WHERE
95                 invhist_itemsite_id = i_itemsite_id
96                -- AND
97                -- invfifo_void = 0
98             ORDER BY
99                 -- real recieved stock first..
100             
101                -- CASE WHEN invdetail_qty >0  and (invhist_transtype = 'RP' OR invhist_transtype = 'AD' OR invhist_transtype = 'RS' ) THEN 1
102                -- ELSE -1 END DESC,
103                 invhist_transdate ASC,
104                 invdetail_id ASC
105             
106         ) x;
107     
108     
109     --- finall balancing occurs by running RL a few times??? with update on??
110      PERFORM  invfifopos_update(
111             i_itemsite_id,
112             COALESCE( (SELECT max(invdetail_id) FROM invdetailview where invhist_itemsite_id =  i_itemsite_id), 0),
113             v_ret
114     );
115   
116     RETURN v_ret;
117    END;
118 $BODY$
119   LANGUAGE plpgsql VOLATILE
120   COST 100;
121   
122 ALTER FUNCTION   invfifo_update_from_invdetail_all(integer)
123   OWNER TO admin;