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