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