3 /* Definition of the cart class
4 this class can hold all the information for:
12 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
13 include_once($path_to_root . "/taxes/tax_calc.inc");
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
21 var $line_items; //array of objects of class line_details
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)
28 var $sales_type; // set to the customer's sales type
29 var $sales_type_name; // set to customer's sales type name
32 var $customer_currency; // set to the customer's currency
33 var $default_discount; // set to the customer's discount %
40 var $delivery_address;
48 var $order_no; // the original order number
52 var $freight_cost = 0;
56 var $tax_group_array = null; // saves db queries
57 var $price_factor; // ditto for price calculations
59 var $pos; // user assigned POS
60 var $cash; // cash transaction
63 var $cash_discount; // not used as of FA 2.1
66 //-------------------------------------------------------------------------
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)
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 );
82 //-------------------------------------------------------------------------
83 // Reading document into cart
85 function read($type, $trans_no = 0, $view=false ) {
87 if (!is_array($trans_no)) $trans_no = array($trans_no);
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;
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"];
108 if (!$view && ($type!=11 || $this->trans_link!=0)) {
109 $src_type = get_parent_type($type);
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));
118 // calculate & save: qtys on other docs and free qtys on src doc
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];
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
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;
137 $this->trans_type = $type;
139 // set new sales document defaults here
140 if (get_global_customer() != reserved_words::get_all())
141 $this->customer_id = get_global_customer();
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
150 $dim = get_company_pref('use_dimension');
153 if ($this->customer_id == '')
154 $this->dimension_id = 0;
157 $cust = get_customer($this->customer_id);
158 $this->dimension_id = $cust['dimension_id'];
162 if ($this->customer_id == '')
163 $this->dimension2_id = 0;
165 $this->dimension2_id = $cust['dimension2_id'];
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
178 $this->cash = date_diff($this->due_date, Today(), 'd')<2;
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'];
187 add_days($this->document_date, sys_prefs::default_delivery_required_by());
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';
203 $type = $this->trans_type;
204 $ref = $this->reference;
205 $date = $this->document_date;
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;
213 $this->order_no= $this->trans_type==13 ? key($src->trans_no) : $src->order_no;
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);
225 switch($this->trans_type) {
227 return write_sales_invoice($this);
229 return write_credit_note($this, $policy);
231 return write_sales_delivery($this, $policy);
233 if ($this->trans_no==0) // new document
234 return add_sales_order($this);
236 return update_sales_order($this);
240 function set_customer($customer_id, $customer_name, $currency, $discount, $cdiscount=0)
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;
249 function set_branch($branch_id, $tax_group_id, $tax_group_name, $phone='', $email='')
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);
258 function set_sales_type($sales_type, $sales_name, $tax_included=0, $factor=0)
260 $this->sales_type = $sales_type;
261 $this->sales_type_name = $sales_name;
262 $this->tax_included = $tax_included;
263 $this->price_factor = $factor;
266 function set_location($id, $name)
268 $this->Location = $id;
269 $this->location_name = $name;
272 function set_delivery($shipper, $destination, $address, $freight_cost=null)
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;
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)
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);
288 // shouldn't come here under normal circumstances
289 display_db_error("unexpected - adding an invalid item or null quantity", "", true);
294 function update_cart_item($line_no, $qty, $price, $disc)
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;
302 function update_add_cart_item_qty($line_no, $qty)
304 $this->line_items[$line_no]->quantity += $qty;
307 function remove_from_cart($line_no)
309 unset($this->line_items[$line_no]);
312 function clear_items()
314 unset($this->line_items);
315 $this->line_items = array();
316 $this->sales_type = "";
318 $this->customer_id = $this->order_no = 0;
321 function count_items()
324 foreach ($this->line_items as $line) {
325 if ($line->quantity!=$line->qty_done) $counter++;
330 function get_items_total()
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),
342 function get_items_total_dispatch()
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)),
354 function has_items_dispatch()
356 foreach ($this->line_items as $ln_itm) {
357 if ($ln_itm->qty_dispatched > 0)
363 function any_already_delivered()
365 /* Checks if there have been any line item processed */
367 foreach ($this->line_items as $stock_item) {
368 if ($stock_item->qty_done !=0) {
377 function some_already_delivered($line_no)
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) {
387 function get_taxes($shipping_cost=null)
391 if($shipping_cost==null)
392 $shipping_cost = $this->freight_cost;
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());
400 $taxes = get_tax_for_items($items, $prices, $shipping_cost,
401 $this->tax_group_id, $this->tax_included, $this->tax_group_array);
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;
413 function get_tax_free_shipping()
416 if ($this->tax_included==0)
417 return $this->freight_cost;
419 return ($this->freight_cost - $this->get_shipping_tax());
422 function get_shipping_tax()
425 $tax_items = get_shipping_tax_as_array();
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'];
435 if($this->tax_included)
436 return round($this->freight_cost*$tax_rate/($tax_rate+100), user_price_dec());
438 return round($this->freight_cost*$tax_rate/100, user_price_dec());
441 } /* end of class defintion */
447 var $item_description;
452 var $src_no; // number of src doc for this line
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
462 function line_details ($stock_id, $qty, $prc, $disc_percent,
463 $qty_done, $standard_cost, $description, $id=0, $src_no=0 )
465 /* Constructor function to add a new LineDetail object with passed params */
468 $this->src_no = $src_no;
469 $item_row = get_item($stock_id);
471 if ($item_row == null)
472 display_db_error("invalid item added to order : $stock_id", "");
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"];
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"];
484 $this->stock_id = $stock_id;
485 $this->quantity = $qty;
486 $this->qty_dispatched = $qty;
488 $this->discount_percent = $disc_percent;
489 $this->qty_done = $qty_done;
490 $this->standard_cost = $standard_cost;
493 // get unit price as stated on document
494 function line_price()