Allow reuse of references previously used on voided documents.
[fa-stable.git] / purchasing / includes / po_class.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 /* Definition of the purch_order class to hold all the information for a purchase order and delivery
13 */
14
15 class purch_order 
16 {
17
18         var $trans_type; // order/grn/invoice (direct)
19         var $line_items;
20         var $curr_code;
21         var $supp_ref;
22         var $delivery_address;
23         var $Comments;
24         var $Location;
25         var $supplier_id;
26         var $supplier_name;
27         var $orig_order_date;
28         var $order_no; /*Only used for modification of existing orders otherwise only established when order committed */
29         var $lines_on_order = 0;
30         var $credit;
31         var $tax_group_id;
32         
33         var $reference;
34         
35         function purch_order()
36         {
37                 /*Constructor function initialises a new purchase order object */
38                 $this->line_items = array();
39                 $this->lines_on_order = $this->order_no = $this->supplier_id = 0;
40         }
41
42         function add_to_order($line_no, $stock_id, $qty, $item_descr, $price, $uom, $req_del_date, $qty_inv, $qty_recd)
43         {
44                 if ($qty != 0 && isset($qty))
45                 {
46                         $this->line_items[$line_no] = new po_line_details($line_no, $stock_id, $item_descr, $qty, $price, $uom, 
47                                 $req_del_date, $qty_inv, $qty_recd);
48                         $this->lines_on_order++;
49                         return 1;
50                 }
51                 return 0;
52         }
53
54         function update_order_item($line_no, $qty, $price, $req_del_date, $description="")
55         {
56                 if ($description != "")
57                         $this->line_items[$line_no]->item_description = $description;
58                 $this->line_items[$line_no]->quantity = $qty;
59                 $this->line_items[$line_no]->price = $price;
60                 $this->line_items[$line_no]->req_del_date = $req_del_date;
61                 $this->line_items[$line_no]->item_description = $description;
62         }
63
64         function remove_from_order($line_no)
65         {
66                 array_splice($this->line_items, $line_no, 1);
67         }
68         
69         function order_has_items() 
70         {
71                 return count($this->line_items) != 0;
72         }
73         
74         function clear_items() 
75         {
76         unset($this->line_items);
77                 $this->line_items = array();
78                 
79                 $this->lines_on_order = 0;  
80                 $this->order_no = 0;
81         }
82
83         
84         function any_already_received()
85         {
86                 /* Checks if there have been deliveries or invoiced entered against any of the line items */
87                 if (count($this->line_items) > 0)
88                 {
89                         foreach ($this->line_items as $ordered_items) 
90                         {
91                                 if ($ordered_items->qty_received != 0 || $ordered_items->qty_inv != 0)
92                                 {
93                                         return 1;
94                                 }
95                         }
96                 }
97                 return 0;
98         }
99
100         function some_already_received($line_no)
101         {
102                 /* Checks if there have been deliveries or amounts invoiced against a specific line item */
103                 if (count($this->line_items) > 0)
104                 {
105                         if ($this->line_items[$line_no]->qty_received != 0 || 
106                                 $this->line_items[$line_no]->qty_inv != 0)
107                         {
108                                 return 1;
109                         }
110                 }
111                 return 0;
112         }
113         /*
114                 Returns order value including all taxes
115         */
116         function get_trans_total() {
117                 
118                 $total = 0;
119                 $dec = user_price_dec();
120
121                 foreach ($this->line_items as $ln_itm) {
122                         $items[] = $ln_itm->stock_id;
123                         $value = round($ln_itm->quantity * $ln_itm->price, $dec);
124                         $prices[] =$value;
125                         $total += $value;
126                 }
127
128                 $taxes = get_tax_for_items($items, $prices, 0, $this->tax_group_id);
129
130                 foreach($taxes as $tax)
131                         $total += round($tax['Value'], $dec);
132
133                 return $total;
134         }
135
136 } /* end of class defintion */
137
138 class po_line_details 
139 {
140
141         var $line_no;
142         var $po_detail_rec;
143         var $grn_item_id;
144         var $stock_id;
145         var $item_description;
146         var $price;
147         var $units;
148         var $req_del_date;
149
150         var $quantity;          // current/entry quantity of PO line
151         var $qty_inv;   // quantity already invoiced against this line
152         var $receive_qty;       // current/entry GRN quantity
153         var $qty_received;      // quantity already received against this line
154
155         var $standard_cost;
156         var $descr_editable;
157
158         function po_line_details($line_no, $stock_item, $item_descr, $qty, $prc, $uom, $req_del_date, 
159                 $qty_inv, $qty_recd, $grn_item_id=0)
160         {
161
162                 /* Constructor function to add a new LineDetail object with passed params */
163                 $this->line_no = $line_no;
164                 $this->stock_id = $stock_item;
165                 $item_row = get_item($stock_item);
166                 if (!$item_row) 
167                         return;
168
169                 $this->descr_editable = $item_row["editable"];
170                 if ($item_descr == null || !$this->descr_editable)
171                         $this->item_description = $item_row["description"];
172                 else
173                         $this->item_description = $item_descr;
174                 $this->quantity = $qty;
175                 $this->req_del_date = $req_del_date;
176                 $this->price = $prc;
177 //              $this->units = $uom;
178                 $this->units = $item_row["units"];
179                 $this->qty_received = $qty_recd;
180                 $this->qty_inv = $qty_inv;
181                 $this->receive_qty = 0; /*initialise these last two only */
182                 $this->standard_cost =0;
183                 $this->grn_item_id = $grn_item_id;
184         }
185 }
186
187 ?>