Merge pull request #1 from shackbarth/keith1
[xtuple] / foundation-database / public / functions / indentedwo.sql
1
2 CREATE OR REPLACE FUNCTION indentedwo(integer, boolean, boolean, boolean) RETURNS SETOF wodata AS $$
3 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
4 -- See www.xtuple.com/CPAL for the full text of the software license.
5 DECLARE
6    pwoid ALIAS FOR $1;
7    pshowops ALIAS FOR $2;
8    pshowmatl ALIAS FOR $3; 
9    pshowindent ALIAS FOR $4;   
10   _row wodata%ROWTYPE;
11   _subrow wodata%ROWTYPE;  
12   _opx wodata%ROWTYPE;
13   _x RECORD;
14   _level INTEGER;
15    
16 BEGIN 
17     --The wodata_id_type column is used to indicate the source of the wodata_id
18     --there are three different tables used wo, womatl and womatlvar
19     --wodata_id_type = 1 = wo_id
20     --wodata_id_type = 2 = womatl_id
21     --wodata_id_type = 3 = wooper_id
22     --initialise values
23     _level := 0;   
24     --get top level works orders
25     FOR _x IN
26        SELECT wo_id,wo_number,wo_subnumber,wo_status,wo_startdate,
27          wo_duedate,wo_adhoc,wo_itemsite_id,qtyAvailable(itemsite_id) AS availableqoh,
28          wo_qtyord,wo_qtyrcv,wo_prodnotes, item_number,
29          item_descrip1, item_descrip2, item_listprice, uom_name
30        FROM wo, itemsite, item, uom     
31        WHERE ((wo_id = pwoid)
32          AND (itemsite_id = wo_itemsite_id)
33          AND (itemsite_item_id=item_id)
34          AND (item_inv_uom_id=uom_id))      
35        ORDER BY wo_number, wo_subnumber
36     LOOP
37         _row.wodata_id := _x.wo_id;
38         _row.wodata_id_type := 1;            
39         _row.wodata_number := _x.wo_number;
40         _row.wodata_subnumber := _x.wo_subnumber;
41         _row.wodata_itemnumber := _x.item_number;
42         _row.wodata_descrip := _x.item_descrip1 || '-' || _x.item_descrip2;
43         _row.wodata_status := _x.wo_status;
44         _row.wodata_startdate := _x.wo_startdate;
45         _row.wodata_duedate := _x.wo_duedate;
46         _row.wodata_adhoc := _x.wo_adhoc;     
47         _row.wodata_itemsite_id := _x.wo_itemsite_id;
48         _row.wodata_custprice := _x.item_listprice;
49         _row.wodata_listprice := _x.item_listprice;
50         _row.wodata_qoh := _x.availableqoh;
51         _row.wodata_short := noneg(_x.wo_qtyord - _x.wo_qtyrcv);
52         _row.wodata_qtyrcv := _x.wo_qtyrcv;   
53         _row.wodata_qtyordreq := _x.wo_qtyord;   
54         _row.wodata_qtyuom := _x.uom_name;    
55         _row.wodata_scrap := 0;        
56         _row.wodata_notes := _x.wo_prodnotes;     
57         _row.wodata_level := _level;                
58         RETURN NEXT _row;
59         IF (pshowmatl AND NOT pshowops) THEN
60           --expand materials      
61           FOR _subrow IN
62              SELECT * FROM indentedwomatl(pwoid, _level)
63           LOOP                                                  
64             RETURN NEXT _subrow;
65           END LOOP;
66         END IF;
67         
68         IF ((pshowmatl OR pshowindent) AND NOT pshowops) THEN
69           --expand next level down               
70           FOR _subrow IN
71            SELECT * FROM indentedwo(_x.wo_id, NULL, _level + 1, pshowmatl, pshowindent) 
72           LOOP                                           
73             RETURN NEXT _subrow; 
74           END LOOP;
75         END IF;
76           
77         IF (pshowops) THEN
78          --expand materials not on operations   
79          IF (pshowmatl) THEN   
80            FOR _subrow IN
81              SELECT * FROM indentedwomatl(pwoid, -1, _level)
82            LOOP                                                  
83              RETURN NEXT _subrow;
84            END LOOP;
85          END IF;
86
87          IF (pshowmatl OR pshowindent) THEN
88            --expand next level down             
89            FOR _subrow IN
90              SELECT * FROM indentedwo(_x.wo_id, -1, _level + 1,  pshowmatl, pshowindent) 
91            LOOP                                           
92              RETURN NEXT _subrow; 
93            END LOOP;
94          END IF;
95
96          --expand opeartions
97          FOR _opx IN
98            SELECT * FROM xtmfg.indentedwoops(pwoid,_level)
99          LOOP
100            RETURN NEXT _opx;
101
102            IF (pshowmatl) THEN  
103               --expand materials on operations      
104               FOR _subrow IN
105                  SELECT * FROM indentedwomatl(pwoid, _opx.wodata_id, _level + 1)
106               LOOP                                                  
107                 RETURN NEXT _subrow;
108               END LOOP;
109            END IF;
110
111            IF (pshowmatl OR pshowindent) THEN
112               --expand next level down             
113               FOR _subrow IN
114                 SELECT * FROM indentedwo(_x.wo_id, _opx.wodata_id, _level + 2,  pshowmatl, pshowindent) 
115               LOOP                                           
116                 RETURN NEXT _subrow; 
117               END LOOP;
118            END IF; 
119          END LOOP; 
120        END IF;                           
121   END LOOP;                     
122   RETURN;
123 END;
124 $$ LANGUAGE 'plpgsql';
125
126 CREATE OR REPLACE FUNCTION indentedwo(integer, integer, integer, boolean, boolean) RETURNS SETOF wodata AS $$
127 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple. 
128 -- See www.xtuple.com/CPAL for the full text of the software license.
129 DECLARE
130    pwoid ALIAS FOR $1;   
131    pwooperid ALIAS FOR $2;
132    plevel ALIAS FOR $3;
133    pshowmatl ALIAS FOR $4; 
134    pshowindent ALIAS FOR $5;  
135   _row wodata%ROWTYPE;   
136   _opx wodata%ROWTYPE; 
137   _x RECORD;
138   _subx RECORD;
139   _index INTEGER;
140   _level INTEGER;
141   _qry TEXT;
142    
143 BEGIN 
144     --The wodata id column is used to indicate the source of the id
145     --there are three different tables used wo, womatl and womatlvar
146     --wodata_id_type = 1 = wo_id
147     --wodata_id_type = 2 = womatl_id
148     --wodata_id_type = 3 = wooper_id
149     _level := (plevel + 1);    
150     --find all WO with the ordid of the next level up
151     _qry := 'SELECT wo_id,wo_number,wo_subnumber,wo_status,wo_startdate,wo_duedate,
152          wo_adhoc,wo_itemsite_id,qtyAvailable(itemsite_id) AS availableqoh,wo_qtyord,wo_qtyrcv, wo_prodnotes,
153          item_number,item_descrip1, item_descrip2, item_listprice, uom_name,
154          womatl_qtyiss, womatl_scrap, womatl_wooper_id
155        FROM itemsite,  wo, item, uom, womatl 
156        WHERE ((wo_ordid = ' || pwoid || ')
157          AND (wo_ordtype = ''W'')
158          AND (itemsite_item_id=item_id)
159          AND (item_inv_uom_id=uom_id)   
160          AND (wo_womatl_id=womatl_id)   
161          AND (wo_itemsite_id = itemsite_id) ';
162
163     IF (pwooperid IS NOT NULL) THEN
164       _qry := _qry || ' AND (womatl_wooper_id=' || pwooperid || ') ';
165     END IF;               
166
167     _qry := _qry || ') ORDER BY wo_number, wo_subnumber';
168  /* if (pwooperid IS NOT NULL) THEN
169     raise exception 'stop %',_qry;
170   END IF;*/
171     FOR _x IN
172       EXECUTE _qry
173     LOOP 
174         _row.wodata_id := _x.wo_id;
175         _row.wodata_id_type := 1;                     
176         _row.wodata_number := _x.wo_number;
177         _row.wodata_subnumber := _x.wo_subnumber;
178         _row.wodata_itemnumber := _x.item_number;
179         _row.wodata_descrip := _x.item_descrip1 || '-' || _x.item_descrip2;
180         _row.wodata_status := _x.wo_status;
181         _row.wodata_startdate := _x.wo_startdate;
182         _row.wodata_duedate := _x.wo_duedate;
183         _row.wodata_adhoc := _x.wo_adhoc;      
184         _row.wodata_itemsite_id := _x.wo_itemsite_id;        
185         _row.wodata_custprice := _x.item_listprice;
186         _row.wodata_listprice := _x.item_listprice;
187         _row.wodata_qoh := _x.availableqoh;
188         _row.wodata_short := noneg(_x.wo_qtyord - _x.wo_qtyrcv);
189         _row.wodata_qtyiss := _x.womatl_qtyiss;  
190         _row.wodata_qtyrcv := _x.wo_qtyrcv;   
191         _row.wodata_qtyordreq := _x.wo_qtyord; 
192         _row.wodata_scrap := _x.womatl_scrap;  
193         _row.wodata_notes := _x.wo_prodnotes;       
194         _row.wodata_level := plevel;                
195         RETURN NEXT _row;  
196         --if indentation require expand next level
197         IF (pshowindent AND pwooperid IS NULL) THEN
198           IF (pshowmatl AND pshowindent) THEN    
199             --get materials for this level
200             FOR _subx IN
201               SELECT * FROM indentedwomatl(_x.wo_id, plevel) 
202             LOOP                                            
203               RETURN NEXT _subx;
204             END LOOP;
205           END IF;
206
207           IF (pshowindent) THEN  
208             --expand lower levels 
209             FOR _subx IN
210               SELECT * FROM indentedwo(_x.wo_id, NULL, _level, pshowmatl, pshowindent )
211             LOOP                                           
212               RETURN NEXT _subx; 
213             END LOOP; 
214           END IF;    
215             
216         ELSIF (pshowindent) THEN --Handle operations
217           --expand materials not on operations   
218           IF (pshowmatl) THEN   
219             FOR _subx IN
220               SELECT * FROM indentedwomatl(_x.wo_id, -1, plevel)
221             LOOP                                                  
222               RETURN NEXT _subx;
223             END LOOP;
224           END IF;
225
226           --expand next level down not on operations
227           FOR _subx IN
228             SELECT * FROM indentedwo(_x.wo_id, -1, _level,  pshowmatl, pshowindent) 
229           LOOP                                           
230             RETURN NEXT _subx; 
231           END LOOP;
232           
233           --expand operations
234           FOR _opx IN
235             SELECT * FROM xtmfg.indentedwoops(_x.wo_id,plevel)
236           LOOP
237             RETURN NEXT _opx;
238
239             IF (pshowmatl) THEN  
240               --expand materials on operations      
241               FOR _subx IN
242                  SELECT * FROM indentedwomatl(_x.wo_id, _opx.wodata_id, _level)
243               LOOP                                                  
244                 RETURN NEXT _subx;
245                 --      raise exception 'stop %',_opx.wodata_id;
246               END LOOP;
247             END IF;
248               
249             --expand next level down   
250             FOR _subx IN
251               SELECT * FROM indentedwo(_x.wo_id, _opx.wodata_id, _level + 2,  pshowmatl, pshowindent) 
252             LOOP                                        
253               RETURN NEXT _subx; 
254             END LOOP;
255               
256           END LOOP;
257         END IF;                       
258       END LOOP;                         
259   RETURN;
260 END;
261 $$ LANGUAGE 'plpgsql';
262