*** empty log message ***
[fa-stable.git] / sales / includes / cart_class.inc
1 <?php
2
3 /* Definition of the cart class
4 this class can hold all the information for:
5
6 i)   a sales order
7 ii)  an invoice
8 iii) a credit note
9
10 */
11
12 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
13 include_once($path_to_root . "/taxes/tax_calc.inc");
14
15 class cart 
16 {
17
18         var $line_items; /*array of objects of class line_details using the product id as the pointer */
19
20         var $delivery_date;
21         var $default_sales_type; // set to the customer's sales type
22         var $sales_type_name; // set to customer's sales type name
23         var $customer_currency; // set to the customer's currency
24         var $default_discount; // set to the customer's discount %
25         var $direct_invoice;  // direct invoicing
26         var $memo_; // memo_ on direct invoicing
27         
28         var $deliver_to;
29         var $delivery_address;
30         var $phone;
31         
32         var $email;
33         var $cust_ref;
34         var $Comments;
35         var $Location;
36         var $location_name;
37         
38         var $order_no; // the order number 
39
40         var $customer_name;
41         var $customer_id;
42         var $Branch;
43         
44         var $orig_order_date;
45         
46         var $ship_via;
47         var $freight_cost;
48         
49         var $tax_group_id;
50         var $tax_group_name;
51
52         function Cart()
53         {
54                 /*Constructor function initialises a new shopping cart */
55                 $this->line_items = array();
56                 $this->default_sales_type = "";
57                 $this->direct_invoice=false;
58         }
59
60         function add_to_cart($stock_id, $qty, $price, $disc, $qty_invoiced=0, $standard_cost=0, $description=null)
61         {
62
63                 if (isset($stock_id) && $stock_id != "" && isset($qty) && $qty > 0)
64                 {
65
66                         $this->line_items[$stock_id] = new line_details($stock_id, $qty, $price, $disc, 
67                                 $qty_invoiced, $standard_cost, $description);
68                         return 1;
69                 } 
70                 else 
71                 {
72                         // shouldn't come here under normal circumstances
73                         display_db_error("unexpected - adding an invalid item or null quantity", "", true);
74                 }
75                 return 0;
76         }
77
78         function find_cart_item($stock_id) 
79         {
80                 if (isset($this->line_items[$stock_id]) && $this->line_items[$stock_id] != null)
81                         return $this->line_items[$stock_id];
82                 return null;
83         }
84         
85         function update_cart_item($update_item, $qty, $price, $disc)
86         {
87
88                 if ($qty > 0)
89                 {
90                         $this->line_items[$update_item]->quantity = $qty;
91                 }
92                 $this->line_items[$update_item]->price = $price;
93                 $this->line_items[$update_item]->discount_percent = $disc;
94         }
95         
96         function update_add_cart_item_qty($update_item, $qty)
97         {
98                 $this->line_items[$update_item]->quantity += $qty;
99         }       
100
101         function remove_from_cart(&$stock_id)
102         {
103                 if (isset($stock_id))
104                 {
105                         unset($this->line_items[$stock_id]);
106                 }
107         }
108         
109         function clear_items() 
110         {
111         unset($this->line_items);
112                 $this->line_items = array();
113                 $this->default_sales_type = "";
114                 $this->customer_id = $this->order_no = 0;
115         }
116         
117         function count_items() 
118         {
119                 return count($this->line_items);
120         }
121         
122         function get_items_total_dispatch($tax_group_id=null)
123         {
124                 $total = 0;
125                 
126                 // preload the taxgroup !
127                 if ($tax_group_id != null)
128                         $tax_group_array = get_tax_group_items_as_array($tax_group_id);
129                 else            
130                         $tax_group_array = null;
131                 
132                 foreach ($this->line_items as $ln_itm) 
133                 {
134                         $total += ($ln_itm->qty_dispatched * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));                        
135                 }               
136                 return $total;
137         }
138         
139         function has_items_dispatch()
140         {
141                 foreach ($this->line_items as $ln_itm) 
142                 {
143                         if ($ln_itm->qty_dispatched > 0)
144                                 return true;                    
145                 }               
146                 return false;
147         }
148         
149         function get_items_total($tax_group_id=null)
150         {
151                 $total = 0;
152                 
153                 // preload the taxgroup !
154                 if ($tax_group_id != null)
155                         $tax_group_array = get_tax_group_items_as_array($tax_group_id);
156                 else            
157                         $tax_group_array = null;                
158                 
159                 foreach ($this->line_items as $ln_itm) {
160                         $total += ($ln_itm->quantity * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));                                              
161                 }               
162                 return $total;
163         }       
164
165         function any_already_delivered()
166         {
167                 /* Checks if there have been deliveries of line items */
168
169                 foreach ($this->line_items as $stock_item) 
170                 {
171                         if ($stock_item->qty_inv !=0)
172                         {
173                                 return 1;
174                         }
175                 }
176
177                 return 0;
178
179         }
180
181         function some_already_delivered($stock_id)
182         {
183                 /* Checks if there have been deliveries of a specific line item */
184                 if (isset($stock_id) && isset($this->line_items[$stock_id]) && 
185                         $this->line_items[$stock_id]->qty_inv != 0)
186                 {
187                         return 1;
188                 }
189                 return 0;
190         }
191         
192     function get_taxes($tax_group_id=null, $shipping_cost=0)
193     {
194         $items = array();
195         $prices = array();
196         
197         if ($tax_group_id == null)
198                 $tax_group_id = $this->tax_group_id;
199                 
200                 // preload the taxgroup !
201                 $tax_group_array = get_tax_group_items_as_array($tax_group_id);
202         
203         foreach ($this->line_items as $ln_itm) 
204         {
205                 $items[] = $ln_itm->stock_id;
206                 $prices[] = ($ln_itm->qty_dispatched * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));
207         }
208                 
209
210         $taxes = get_tax_for_items($items, $prices, $shipping_cost, $tax_group_id, $tax_group_array);
211         
212         return $taxes;
213     }   
214         
215 } /* end of class defintion */
216
217 class line_details 
218 {
219         var $stock_id;
220         var $item_description;
221         var $units;
222         var $mb_flag;
223         var $tax_type;
224         var $tax_type_name;
225                         
226         var $quantity;
227         var $price;
228         var $discount_percent;
229         var $qty_inv;
230         var $qty_dispatched;
231         var $standard_cost;
232
233         function line_details ($stock_id, $qty, $prc, $disc_percent,  
234                 $qty_invoiced, $standard_cost, $description)
235         {
236         /* Constructor function to add a new LineDetail object with passed params */
237                 $item_row = get_item($stock_id);
238                 
239                 if ($item_row == null)
240                         display_db_error("invalid item added to order : $stock_id", "");
241                 
242                 $this->mb_flag = $item_row["mb_flag"];
243                 $this->units = $item_row["units"];
244                 if ($description == null)
245                         $this->item_description = $item_row["description"];
246                 else
247                         $this->item_description = $description;
248                 //$this->standard_cost = $item_row["material_cost"] + $item_row["labour_cost"] + $item_row["overhead_cost"]; 
249                 $this->tax_type = $item_row["tax_type_id"];
250                 $this->tax_type_name = $item_row["tax_type_name"];
251
252                 $this->stock_id = $stock_id;
253                 $this->quantity = $qty;
254                 $this->price = $prc;
255                 $this->discount_percent = $disc_percent;
256                 $this->qty_inv = $qty_invoiced;
257                 $this->qty_dispatched = $qty - $qty_invoiced;
258                 $this->standard_cost = $standard_cost;
259         }
260         
261         function full_price()
262         {
263                 return $this->price;
264         }
265         
266         function taxfree_price($tax_group_id, $tax_group_array=null)
267         {
268                 if ($tax_group_id==null)
269                         return $this->price;
270                 return get_tax_free_price_for_item($this->stock_id, $this->price, $tax_group_id, $tax_group_array);
271         }
272 }
273
274 ?>