Pman/templates/transaction.html
[Pman.Base] / Pman / templates / x-fifo-invsell_apply_all.sql
1 CREATE OR REPLACE FUNCTION invsell_apply_all() RETURNS integer
2 AS $BODY$
3 DECLARE
4      v_cnt integer;
5      
6 BEGIN
7
8 -- find all the matches that we can update, and make the changes.
9     SELECT COUNT(invsell_invhist_id) into v_cnt FROM 
10         invsell
11          
12         WHERE
13             ROUND(invsell_current_totalcost,2)  !=  ROUND(invsell_calc_totalcost,2)
14             AND
15             invsell_is_estimate = false   ;
16             
17             
18     
19     IF (v_cnt < 1) THEN
20         RETURN 0;
21     END IF;
22     
23     SELECT invsell_invhist_id INTO v_cnt
24          FROM
25             invsell
26          
27         WHERE
28             ROUND(invsell_current_totalcost,2)  !=  ROUND(invsell_calc_totalcost,2)
29             AND
30             invsell_is_estimate = false
31         ORDER BY
32             invsell_invhist_id ASC 
33         LIMIT 1    ;
34     
35     RAISE NOTICE 'APPLY INVSELL TO %', v_cnt;
36 -- updates invhist and gltrans
37     PERFORM  
38             invsell_apply_invsell(
39                 v_cnt
40                 
41             ) as result;
42         
43      
44     RETURN v_cnt;
45             
46 END;
47 $BODY$
48   LANGUAGE plpgsql VOLATILE
49   COST 100;
50   
51 ALTER FUNCTION  invsell_apply_all()
52   OWNER TO admin;
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66 CREATE OR REPLACE FUNCTION invsell_apply_invsell(integer) RETURNS integer
67 AS $BODY$
68 DECLARE
69     i_invsell_invhist_id ALIAS FOR $1;
70     v_mininvsell_transdate timestamp with time zone;
71     
72      
73 BEGIN
74     --RAISE NOTICE 'invsell_apply_invsell:i_invsell_invhist_id =%', i_invsell_invhist_id;
75 -- find all the matches that we can update, and make the changes.
76
77     --RAISE NOTICE 'invsell_apply';
78 -- updates invhist and gltrans
79     PERFORM  
80             invsell_apply(
81                 invsell_invhist_id,
82                 invhist_id
83             ) as result
84         FROM
85             invhist
86         INNER JOIN
87             invdepend ON invhist_id = invdepend_invhist_id
88         INNER JOIN
89             invsell ON invdepend_parent_id = invsell_invhist_id
90         
91         WHERE
92             invsell_invhist_id = i_invsell_invhist_id;
93     
94     
95 -- updates invhist and itemsite.
96     --RAISE NOTICE 'invsell_apply_order';
97
98     PERFORM
99             invsell_apply_order (
100                 invsell_invhist_id
101             ) as result
102         FROM
103             invsell
104         WHERE
105             invsell_invhist_id = i_invsell_invhist_id;
106     
107     --RAISE NOTICE 'calc min';
108     SELECT min(invsell_transdate)
109             INTO v_mininvsell_transdate
110             FROM invsell
111             WHERE
112                 invsell_itemsite_id = (
113                     SELECT invsell_itemsite_id FROM invsell WHERE  invsell_invhist_id = i_invsell_invhist_id LIMIT 1
114                 );
115                 
116     -- on invsell
117     
118     --RAISE NOTICE 'asset trialbal';
119     PERFORM
120             invsell_apply_trialbal (
121                 costcat_asset_accnt_id,
122                  v_mininvsell_transdate
123             ) as result
124         FROM
125             invsell
126         LEFT JOIN itemsite ON itemsite_id = invsell_itemsite_id
127         LEFT JOIN costcat ON costcat_id = itemsite_costcat_id
128         WHERE
129             invsell_invhist_id = i_invsell_invhist_id;
130     
131     -- on invsell
132     --RAISE NOTICE 'shipasset trialbal';
133     PERFORM
134             invsell_apply_trialbal (
135                 costcat_shipasset_accnt_id,
136                  v_mininvsell_transdate
137             ) as result
138         FROM
139             invsell
140         LEFT JOIN itemsite ON itemsite_id = invsell_itemsite_id
141         LEFT JOIN costcat ON costcat_id = itemsite_costcat_id
142         WHERE
143             invsell_invhist_id = i_invsell_invhist_id;
144     
145     -- on invsell
146     --RAISE NOTICE 'Cogs trialbal';
147     PERFORM
148             invsell_apply_trialbal (
149                 resolvecosaccount(invsell_itemsite_id, cohead_cust_id),
150                  v_mininvsell_transdate
151             ) as result
152              
153         FROM
154             invsell
155         LEFT JOIN cohead ON 
156             cohead_number = CASE strpos(invsell_ordnumber, '-')
157                         WHEN 0 THEN invsell_ordnumber 
158                         ELSE substr(invsell_ordnumber, 1, strpos(invsell_ordnumber, '-') - 1)
159                     END
160           WHERE
161             invsell_invhist_id = i_invsell_invhist_id;
162     RETURN 1;
163             
164 END;
165 $BODY$
166   LANGUAGE plpgsql VOLATILE
167   COST 100;
168   
169 ALTER FUNCTION  invsell_apply_invsell(integer)
170   OWNER TO admin;
171   
172