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