9a730136fb4ed09806b210a394f71cf2fe264f2d
[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         var $trans_type; // invoice, order, delivery note ...
18         var $trans_no = array();// array (num1=>ver1,..) or 0 for new
19         var $so_type = 0;               // for sales order: simple=0 template=1
20
21         var $line_items;  //array of objects of class line_details
22
23         var $src_docs = array();        // array of arrays(num1=>ver1,...) or 0 for no src
24         var $src_date;                  // src document date (for info only)
25
26         var $document_date;
27         var $due_date;
28         var $sales_type; // set to the customer's sales type
29         var $sales_type_name; // set to customer's sales type name
30         var $tax_included;
31
32         var $customer_currency; // set to the customer's currency
33         var $default_discount; // set to the customer's discount %
34         var $customer_name;
35         var $customer_id;
36         var $Branch;
37         var $email;
38
39         var $deliver_to;
40         var $delivery_address;
41         var $phone;
42
43         var $cust_ref;
44         var $reference;
45         var $Comments;
46         var $Location;
47         var $location_name;
48         var $order_no;          // the original order number
49         var $trans_link = 0;
50
51         var $ship_via;
52         var $freight_cost = 0;
53
54         var $tax_group_id;
55         var $tax_group_name;
56         var $tax_group_array = null; // saves db queries
57
58         //-------------------------------------------------------------------------
59         //
60         //  $trans_no==0 => open new/direct document
61         //  $trans_no!=0 && $view==false => read for view
62         //  $trans_no!=0 && $view==true => read for edit (qty update from parent doc)
63         //
64         function Cart($type, $trans_no=0, $view=false ) {
65                 /*Constructor function initialises a new shopping cart */
66                 $this->line_items = array();
67                 $this->sales_type = "";
68                 $this->trans_type = 30;
69                 $this->read($type, $trans_no, $view );
70
71         }
72
73         //-------------------------------------------------------------------------
74         // Reading document into cart
75         //
76         function read($type, $trans_no = 0, $view=false ) {
77
78                 if (!is_array($trans_no)) $trans_no = array($trans_no);
79
80                 if ($trans_no[0]) {
81                         if ($type == 30) { // sales order
82                                 read_sales_order($trans_no[0], $this);
83                                 if ($view) {    // prepare for DN/IV entry
84                                         for($line_no = 0; $line_no < count($this->line_items); $line_no++) {
85                                                 $line = &$this->line_items[$line_no];
86                                                 $line->qty_dispatched = $line->quantity - $line->qty_done;
87                                         }
88                                 }
89                                 } else {        // derivative transaction
90                                         read_sales_trans($type, $trans_no, $this);
91                                         if ($this->order_no) { // free hand credit notes have no order_no
92                                                 $sodata = get_sales_order_header($this->order_no);
93                                                 $this->cust_ref = $sodata["customer_ref"];
94                                         // currently currency is hard linked to debtor account
95                                         //      $this->customer_currency = $sodata["curr_code"];
96                                                 $this->delivery_to = $sodata["deliver_to"];
97                                                 $this->delivery_address = $sodata["delivery_address"];
98                                         }
99                                         if (!$view && ($type!=11 || $this->trans_link!=0)) {
100                                                 $src_type = get_parent_type($type);
101                                                 $src_details = 0;
102                                                 if ($src_type == 30) { // get src data from sales_orders
103                                                         $this->src_docs = array( $sodata['order_no']=>$sodata['version']);
104                                                         $srcdetails = get_sales_order_details($this->order_no);
105                                                 } else {        // get src_data from debtor_trans
106                                                         $this->src_docs = get_customer_trans_version($src_type, get_parent_trans($type,$trans_no[0]));
107                                                         $srcdetails = get_customer_trans_details($src_type,array_keys($this->src_docs));
108                                                 }
109                                                 // calculate & save: qtys on other docs and free qtys on src doc
110                                                 $line_no = 0;
111                                                 for($line_no = 0; $srcline = db_fetch($srcdetails); $line_no++) {
112                                                         $sign = 1; // $type==13 ?  1 : -1; // this is strange debtor_trans atavism
113                                                         $line = &$this->line_items[$line_no];
114
115                                                         $line->qty_old = $line->qty_dispatched = $line->quantity;
116                                                         $line->quantity += $sign * ($srcline['quantity'] - $srcline['qty_done']); // add free qty on src doc
117                                                 }
118                                         } else {
119                                                 for($line_no = 0; $line_no < count($this->line_items); $line_no++) {
120                                                         $line = &$this->line_items[$line_no];
121                                                         $line->qty_dispatched = $line->quantity;
122                                                 }
123                                         }
124                                 }
125                         } else {
126                                 $this->trans_type = $type;
127                                 $this->trans_no = 0;
128                                 // set new sales document defaults here
129                                 $this->customer_id = '';
130                                 $this->document_date = Today();
131                                 if (!is_date_in_fiscalyear($this->document_date))
132                                         $this->document_date = end_fiscalyear();
133                                 $this->reference = references::get_next($this->trans_type);
134                         }
135         }
136
137         //-------------------------------------------------------------------------
138         // Writing new/modified sales document to database.
139         // Makes parent documents for direct delivery/invoice by recurent call.
140         // $policy - 0 or 1:  writeoff/return for IV, back order/cancel for DN
141         function write($policy=0) {
142                 if (count($this->src_docs) == 0 && ($this->trans_type == 10 || $this->trans_type == 13)) {
143                         // this is direct document - first add parent
144                         $src = (PHP_VERSION<5) ? $this : clone( $this ); // make local copy of this cart
145                         $src->trans_type = get_parent_type($src->trans_type);
146                         $src->reference = 'auto';
147
148                         $src->write(1);
149
150                         $type = $this->trans_type;
151                         $ref = $this->reference;
152                         // re-read document
153                         $this->read($src->trans_type, key($src->trans_no), true);
154                         $this->reference = $ref;
155                         $this->trans_type = $type;
156                         $this->src_docs= $this->trans_no;
157                         $this->trans_no = 0;
158                         $this->order_no= $this->trans_type==13 ? key($src->trans_no) : $src->order_no;
159                 }
160                 switch($this->trans_type) {
161                         case 10:
162                                 return write_sales_invoice($this);
163                         case 11:
164                                 return write_credit_note($this, $policy);
165                         case 13:
166                                 return write_sales_delivery($this, $policy);
167                         case 30:
168                                 if ($this->trans_no==0) // new document
169                                         return add_sales_order($this);
170                                 else
171                                         return update_sales_order($this);
172                 }
173         }
174
175         function set_customer($customer_id, $customer_name, $currency, $discount)
176         {
177                 $this->customer_name = $customer_name;
178                 $this->customer_id = $customer_id;
179                 $this->default_discount = $discount;
180                 $this->customer_currency = $currency;
181         }
182
183         function set_branch($branch_id, $tax_group_id, $tax_group_name, $phone='', $email='')
184         {
185                 $this->Branch = $branch_id;
186                 $this->phone = $phone;
187                 $this->email = $email;
188                 $this->tax_group_id = $tax_group_id;
189                 $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
190         }
191
192         function set_sales_type($sales_type, $sales_name, $tax_included=0)
193         {
194             $old_tax_inc = $this->tax_included;
195             $this->sales_type = $sales_type;
196             $this->sales_type_name = $sales_name;
197             $this->tax_included = $tax_included;
198         }
199
200         function set_location($id, $name)
201         {
202                 $this->Location = $id;
203                 $this->location_name = $name;
204         }
205
206         function set_delivery($shipper, $destination, $address, $freight_cost=null)
207         {
208                 $this->ship_via = $shipper;
209                 $this->deliver_to = $destination;
210                 $this->delivery_address = $address;
211                 if (isset($freight_cost))
212                         $this->freight_cost = $freight_cost;
213         }
214
215         function add_to_cart($line_no,$stock_id, $qty, $price, $disc, $qty_done=0, $standard_cost=0, $description=null, $id=0, $src_no=0)
216         {
217                 if (isset($stock_id) && $stock_id != "" && isset($qty)/* && $qty > 0*/) {
218                         $this->line_items[$line_no] = new line_details($stock_id, $qty, $price, $disc,
219                                 $qty_done,  $standard_cost, $description, $id, $src_no);
220                         return 1;
221                 } else {
222                         // shouldn't come here under normal circumstances
223                         display_db_error("unexpected - adding an invalid item or null quantity", "", true);
224                 }
225                 return 0;
226         }
227
228         function update_cart_item($line_no, $qty, $price, $disc)
229         {
230
231                 $this->line_items[$line_no]->quantity = $qty;
232                 $this->line_items[$line_no]->qty_dispatched = $qty;
233                 $this->line_items[$line_no]->price = $price;
234                 $this->line_items[$line_no]->discount_percent = $disc;
235         }
236
237         function update_add_cart_item_qty($line_no, $qty)
238         {
239                 $this->line_items[$line_no]->quantity += $qty;
240         }
241
242         function remove_from_cart($line_no)
243         {
244                 unset($this->line_items[$line_no]);
245         }
246
247         function clear_items()
248         {
249                 unset($this->line_items);
250                 $this->line_items = array();
251                 $this->sales_type = "";
252                 $this->trans_no = 0;
253                 $this->customer_id = $this->order_no = 0;
254         }
255
256         function count_items()
257         {
258                 $counter=0;
259                 foreach ($this->line_items as $line) {
260                         if ($line->quantity!=$line->qty_done) $counter++;
261                 }
262                 return $counter;
263         }
264
265         function get_items_total()
266         {
267                 $total = 0;
268
269                 foreach ($this->line_items as $ln_itm) {
270                         $price = $ln_itm->line_price();
271                         $total += round($ln_itm->quantity * $price * (1 - $ln_itm->discount_percent), 
272                            user_price_dec());
273                 }
274                 return $total;
275         }
276
277         function get_items_total_dispatch()
278         {
279                 $total = 0;
280
281                 foreach ($this->line_items as $ln_itm) {
282                         $price = $ln_itm->line_price();
283                         $total += round(($ln_itm->qty_dispatched * $price * (1 - $ln_itm->discount_percent)), 
284                            user_price_dec());
285                 }
286                 return $total;
287         }
288
289         function has_items_dispatch()
290         {
291                 foreach ($this->line_items as $ln_itm) {
292                         if ($ln_itm->qty_dispatched > 0)
293                                 return true;
294                 }
295                 return false;
296         }
297
298         function any_already_delivered()
299         {
300                 /* Checks if there have been any line item processed */
301
302                 foreach ($this->line_items as $stock_item) {
303                         if ($stock_item->qty_done !=0) {
304                                 return 1;
305                         }
306                 }
307
308                 return 0;
309
310         }
311
312         function some_already_delivered($line_no)
313         {
314                 /* Checks if there have been deliveries of a specific line item */
315                 if (isset($this->line_items[$line_no]) &&
316                         $this->line_items[$line_no]->qty_done != 0) {
317                         return 1;
318                 }
319                 return 0;
320         }
321
322         function get_taxes($shipping_cost=null)
323         {
324                 $items = array();
325                 $prices = array();
326                 if($shipping_cost==null)
327                         $shipping_cost = $this->freight_cost;
328
329                 foreach ($this->line_items as $ln_itm) {
330                         $items[] = $ln_itm->stock_id;
331                         $prices[] = round(($ln_itm->qty_dispatched *
332                                 $ln_itm->line_price()* (1 - $ln_itm->discount_percent)),  user_price_dec());
333                 }
334
335                 $taxes = get_tax_for_items($items, $prices, $shipping_cost,
336                   $this->tax_group_id, $this->tax_included,  $this->tax_group_array);
337
338                 return $taxes;
339         }
340
341
342         function get_tax_free_shipping()
343         {
344
345                 if ($this->tax_included==0)
346                         return $this->freight_cost;
347                 else
348                         return ($this->freight_cost - $this->get_shipping_tax());
349         }
350
351         function get_shipping_tax()
352         {
353
354                 $tax_items = get_shipping_tax_as_array();
355                 $tax_rate = 0;
356                 if ($tax_items != null) {
357                         foreach ($tax_items as $item_tax) {
358                                 $index = $item_tax['tax_type_id'];
359                                 if (isset($this->tax_group_array[$index])) {
360                                         $tax_rate += $item_tax['rate'];
361                                 }
362                         }
363                 }
364                 if($this->tax_included)
365                         return round($this->freight_cost*$tax_rate/($tax_rate+100),  user_price_dec());
366                 else
367                         return round($this->freight_cost*$tax_rate/100,  user_price_dec());
368         }
369
370 } /* end of class defintion */
371
372 class line_details
373 {
374         var $id;
375         var $stock_id;
376         var $item_description;
377         var $units;
378         var $mb_flag;
379         var $tax_type;
380         var $tax_type_name;
381         var $src_no;    // number of src doc for this line
382         var $quantity;
383         var $price;
384         var $discount_percent;
385         var $qty_done;  // quantity processed on child documents
386         var $qty_dispatched; // quantity selected to process
387         var $qty_old=0; // quantity dispatched before edition
388         var $standard_cost;
389
390         function line_details ($stock_id, $qty, $prc, $disc_percent,
391                 $qty_done, $standard_cost, $description, $id=0, $src_no=0 )
392         {
393         /* Constructor function to add a new LineDetail object with passed params */
394
395                 $this->id = $id;
396                 $this->src_no = $src_no;
397                 $item_row = get_item($stock_id);
398
399                 if ($item_row == null)
400                         display_db_error("invalid item added to order : $stock_id", "");
401
402                 $this->mb_flag = $item_row["mb_flag"];
403                 $this->units = $item_row["units"];
404                 if ($description == null)
405                         $this->item_description = $item_row["description"];
406                 else
407                         $this->item_description = $description;
408                 //$this->standard_cost = $item_row["material_cost"] + $item_row["labour_cost"] + $item_row["overhead_cost"];
409                 $this->tax_type = $item_row["tax_type_id"];
410                 $this->tax_type_name = $item_row["tax_type_name"];
411
412                 $this->stock_id = $stock_id;
413                 $this->quantity = $qty;
414                 $this->qty_dispatched = $qty;
415                 $this->price = $prc;
416                 $this->discount_percent = $disc_percent;
417                 $this->qty_done = $qty_done;
418                 $this->standard_cost = $standard_cost;
419         }
420
421         // get unit price as stated on document
422         function line_price()
423         {
424                 return $this->price;
425         }
426 }
427
428 ?>