Huge sales module changes toward delivery and invoicing separation. Includes some...
[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 iv)  a delivery note
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 $trans_type; // invoice, order, delivery note ...
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         var $order_no; // the original order number     
38         var $trans_no;// transaction 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($type = 'order')
53         {
54                 /*Constructor function initialises a new shopping cart */
55                 $this->line_items = array();
56                 $this->default_sales_type = "";
57                 $this->trans_type = $type;
58         }
59
60         function add_to_cart($stock_id, $qty, $price, $disc, $qty_done=0, $standard_cost=0, $description=null, $id=0)
61         {
62                 if (isset($stock_id) && $stock_id != "" && isset($qty)/* && $qty > 0*/)
63                 {
64                         $this->line_items[] = new line_details($stock_id, $qty, $price, $disc, 
65                                 $qty_done,  $standard_cost, $description, $id);
66                         return 1;
67                 }
68                 else
69                 {
70                         // shouldn't come here under normal circumstances
71                         display_db_error("unexpected - adding an invalid item or null quantity", "", true);
72                 }
73                 return 0;
74         }
75
76         function update_cart_item($line_no, $qty, $price, $disc)
77         {
78
79                 if ($qty > 0)
80                 {
81                         $this->line_items[$line_no]->quantity = $qty;
82                 }
83                 $this->line_items[$line_no]->price = $price;
84                 $this->line_items[$line_no]->discount_percent = $disc;
85         }
86         function update_add_cart_item_qty($line_no, $qty)
87         {
88                 $this->line_items[$line_no]->quantity += $qty;
89         }
90
91         function remove_from_cart($line_no)
92         {
93                         unset($this->line_items[$line_no]);
94         }
95
96         function clear_items()
97         {
98         unset($this->line_items);
99                 $this->line_items = array();
100                 $this->default_sales_type = "";
101                 $this->trans_no = 0;
102                 $this->customer_id = $this->order_no = 0;
103         }
104
105         function count_items()
106         {
107           $counter=0;
108           foreach($this->line_items as $line) {
109                 if($line->quantity>$line->qty_done) $counter++;
110           }
111                 return $counter;
112         }
113
114         function get_items_total_dispatch($tax_group_id=null)
115         {
116                 $total = 0;
117
118                 // preload the taxgroup !
119                 if ($tax_group_id != null)
120                         $tax_group_array = get_tax_group_items_as_array($tax_group_id);
121                 else
122                         $tax_group_array = null;
123
124                 foreach ($this->line_items as $ln_itm)
125                 {
126                         $total += ($ln_itm->qty_dispatched * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));
127                 }
128                 return $total;
129         }
130
131         function has_items_dispatch()
132         {
133                 foreach ($this->line_items as $ln_itm)
134                 {
135                         if ($ln_itm->qty_dispatched > 0)
136                                 return true;
137                 }
138                 return false;
139         }
140
141         function get_items_total($tax_group_id=null)
142         {
143                 $total = 0;
144
145                 // preload the taxgroup !
146                 if ($tax_group_id != null)
147                         $tax_group_array = get_tax_group_items_as_array($tax_group_id);
148                 else
149                         $tax_group_array = null;
150
151                 foreach ($this->line_items as $ln_itm) {
152                         $total += ($ln_itm->quantity * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));
153                 }
154                 return $total;
155         }
156
157         function any_already_delivered()
158         {
159                 /* Checks if there have been deliveries of line items */
160
161                 foreach ($this->line_items as $stock_item)
162                 {
163                         if ($stock_item->qty_done !=0)
164                         {
165                                 return 1;
166                         }
167                 }
168
169                 return 0;
170
171         }
172
173         function some_already_delivered($line_no)
174         {
175                 /* Checks if there have been deliveries of a specific line item */
176                 if (isset($this->line_items[$line_no]) && 
177                         $this->line_items[$line_no]->qty_done != 0)
178                 {
179                         return 1;
180                 }
181                 return 0;
182         }
183
184     function get_taxes($tax_group_id=null, $shipping_cost=0)
185     {
186         $items = array();
187         $prices = array();
188
189         if ($tax_group_id == null)
190                 $tax_group_id = $this->tax_group_id;
191
192                 // preload the taxgroup !
193                 $tax_group_array = get_tax_group_items_as_array($tax_group_id);
194
195         foreach ($this->line_items as $ln_itm)
196         {
197                         $items[] = $ln_itm->stock_id;
198                         $prices[] = ($ln_itm->qty_dispatched * $ln_itm->taxfree_price($tax_group_id, $tax_group_array) * (1 - $ln_itm->discount_percent));
199         }
200
201
202         $taxes = get_tax_for_items($items, $prices, $shipping_cost, $tax_group_id, $tax_group_array);
203
204         return $taxes;
205     }
206
207 } /* end of class defintion */
208
209 class line_details
210 {
211         var $line_no;
212         var $id;
213         var $stock_id;
214         var $item_description;
215         var $units;
216         var $mb_flag;
217         var $tax_type;
218         var $tax_type_name;
219
220         var $quantity;
221         var $price;
222         var $discount_percent;
223         var $qty_done;  // quantity processed so far
224         var $qty_dispatched; // quantity selected to process 
225         var $standard_cost;
226
227         function line_details ($stock_id, $qty, $prc, $disc_percent,  
228                 $qty_done, $standard_cost, $description, $id=0)
229         {
230         /* Constructor function to add a new LineDetail object with passed params */
231
232                 $this->id = $id;
233                 $item_row = get_item($stock_id);
234
235                 if ($item_row == null)
236                         display_db_error("invalid item added to order : $stock_id", "");
237
238                 $this->mb_flag = $item_row["mb_flag"];
239                 $this->units = $item_row["units"];
240                 if ($description == null)
241                         $this->item_description = $item_row["description"];
242                 else
243                         $this->item_description = $description;
244                 //$this->standard_cost = $item_row["material_cost"] + $item_row["labour_cost"] + $item_row["overhead_cost"];
245                 $this->tax_type = $item_row["tax_type_id"];
246                 $this->tax_type_name = $item_row["tax_type_name"];
247
248                 $this->stock_id = $stock_id;
249                 $this->quantity = $qty;
250                 $this->price = $prc;
251                 $this->discount_percent = $disc_percent;
252                 $this->qty_done = $qty_done;
253                 $this->standard_cost = $standard_cost;
254         }
255
256         function full_price()
257         {
258                 return $this->price;
259         }
260
261         function taxfree_price($tax_group_id, $tax_group_array=null)
262         {
263                 if ($tax_group_id==null)
264                         return $this->price;
265                 return get_tax_free_price_for_item($this->stock_id, $this->price, $tax_group_id, $tax_group_array);
266         }
267 }
268
269 ?>