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 $default_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
58 //-------------------------------------------------------------------------
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)
64 function Cart($type, $trans_no=0, $view=false ) {
65 /*Constructor function initialises a new shopping cart */
66 $this->line_items = array();
67 $this->default_sales_type = "";
68 $this->trans_type = 30;
69 $this->read($type, $trans_no, $view );
74 function read($type, $trans_no=0, $view=false ) {
76 if (!is_array($trans_no)) $trans_no = array($trans_no);
79 if ($type==30) { // sales order
80 read_sales_order($trans_no[0], $this);
81 if ($view) { // prepare for DN/IV entry
82 for($line_no=0; $line_no<count($this->line_items); $line_no++) {
83 $line= &$this->line_items[$line_no];
84 $line->qty_dispatched = $line->quantity-$line->qty_done;
87 } else { // derivative transaction
88 read_sales_trans($type, $trans_no, $this);
89 if ($this->order_no) { // free hand credit notes have no order_no
90 $sodata = get_sales_order_header($this->order_no);
91 $this->cust_ref = $sodata["customer_ref"];
92 //set_customer($customer_name, $customer_id, $currency, $discount)
93 //* $this->customer_name = $sodata["name"];
94 //* $this->customer_currency = $sodata["curr_code"];
95 //* $this->Comments = $sodata["comments"];
96 $this->delivery_address = $sodata["delivery_address"];
97 // $this->location_name = $sodata["location_name"];
98 // $this->document_date = sql2date($myrow["ord_date"]);
100 if (!$view && ($type!=11 || $this->trans_link!=0)) {
101 $src_type = get_parent_type($type);
103 if ($src_type==30) { // get src data from sales_orders
104 $this->src_docs = array( $sodata['order_no']=>$sodata['version']);
105 $srcdetails = get_sales_order_details($this->order_no);
106 } else { // get src_data from debtor_trans
107 $this->src_docs = get_customer_trans_version($src_type, get_parent_trans($type,$trans_no[0]));
108 $srcdetails = get_customer_trans_details($src_type,array_keys($this->src_docs));
110 // calculate & save: qtys on other docs and free qtys on src doc
112 for($line_no=0; $srcline = db_fetch($srcdetails); $line_no++) {
113 $sign = 1; // $type==13 ? 1 : -1; // this is strange debtor_trans atavism
114 $line = &$this->line_items[$line_no];
116 $line->qty_old = $line->qty_dispatched = $line->quantity;
117 $line->quantity += $sign*($srcline['quantity']-$srcline['qty_done']); // add free qty on src doc
120 for($line_no=0; $line_no<count($this->line_items); $line_no++) {
121 $line= &$this->line_items[$line_no];
122 $line->qty_dispatched = $line->quantity;
127 $this->trans_type = $type;
129 // set new sales document defaults here
130 $this->customer_id='';
131 $this->document_date = Today();
132 if (!is_date_in_fiscalyear($this->document_date))
133 $this->document_date = end_fiscalyear();
134 $this->reference = references::get_next($this->trans_type);
138 //-------------------------------------------------------------------------
139 // Writing new/modified sales document to database.
140 // Makes parent documents for direct delivery/invoice by recurent call.
142 function write($policy=0) {
143 if (count($this->src_docs) == 0 && ($this->trans_type == 10 || $this->trans_type == 13)) {
144 // direct document -> first add parent
145 $src = $this; // make local copy of this cart
146 $src->trans_type = get_parent_type($src->trans_type);
147 $src->reference = 'auto';
150 $type = $this->trans_type;
151 $ref = $this->reference;
153 $this->read($src->trans_type, key($src->trans_no), true);
155 $this->reference = $ref;
156 $this->trans_type = $type;
157 $this->src_docs= $this->trans_no;
159 $this->order_no= $this->trans_type==13 ? key($src->trans_no) : $src->order_no;
161 switch($this->trans_type) {
163 return write_sales_invoice($this);
165 return write_credit_note($this, $policy);
167 return write_sales_delivery($this, $policy);
169 if ($this->trans_no==0) // new document
170 return add_sales_order($this);
172 return update_sales_order($this);
176 function set_customer($customer_id, $customer_name, $currency, $discount)
178 $this->customer_name = $customer_name;
179 $this->customer_id = $customer_id;
180 $this->default_discount = $discount;
181 $this->customer_currency = $currency;
184 function set_branch($branch_id, $tax_group_id, $tax_group_name, $phone='', $email='')
186 $this->Branch = $branch_id;
187 $this->phone = $phone;
188 $this->email = $email;
189 $this->tax_group_id = $tax_group_id;
190 $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
193 function set_sales_type($sales_type, $sales_name, $tax_included=0)
195 $this->default_sales_type = $sales_type;
196 $this->sales_type_name = $sales_name;
197 $this->tax_included = $tax_included;
200 function set_location($id, $name)
202 $this->Location = $id;
203 $this->location_name = $name;
206 function set_delivery($shipper, $destination, $address, $freight_cost=null)
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;
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)
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);
222 // shouldn't come here under normal circumstances
223 display_db_error("unexpected - adding an invalid item or null quantity", "", true);
228 function update_cart_item($line_no, $qty, $price, $disc)
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;
237 function update_add_cart_item_qty($line_no, $qty)
239 $this->line_items[$line_no]->quantity += $qty;
242 function remove_from_cart($line_no)
244 unset($this->line_items[$line_no]);
247 function clear_items()
249 unset($this->line_items);
250 $this->line_items = array();
251 $this->default_sales_type = "";
253 $this->customer_id = $this->order_no = 0;
256 function count_items()
259 foreach ($this->line_items as $line) {
260 if ($line->quantity!=$line->qty_done) $counter++;
265 function get_items_total()
269 foreach ($this->line_items as $ln_itm) {
270 $price = $ln_itm->line_price();
271 $total += ($ln_itm->quantity * $price * (1 - $ln_itm->discount_percent));
276 function get_items_total_dispatch()
280 foreach ($this->line_items as $ln_itm) {
281 $price = $ln_itm->line_price();
282 $total += ($ln_itm->qty_dispatched * $price * (1 - $ln_itm->discount_percent));
287 function has_items_dispatch()
289 foreach ($this->line_items as $ln_itm) {
290 if ($ln_itm->qty_dispatched > 0)
296 function any_already_delivered()
298 /* Checks if there have been any line item processed */
300 foreach ($this->line_items as $stock_item) {
301 if ($stock_item->qty_done !=0) {
310 function some_already_delivered($line_no)
312 /* Checks if there have been deliveries of a specific line item */
313 if (isset($this->line_items[$line_no]) &&
314 $this->line_items[$line_no]->qty_done != 0) {
320 function get_taxes($shipping_cost=null)
324 if($shipping_cost==null)
325 $shipping_cost = $this->freight_cost;
327 foreach ($this->line_items as $ln_itm) {
328 $items[] = $ln_itm->stock_id;
329 $prices[] = ($ln_itm->qty_dispatched *
330 $ln_itm->line_price()* (1 - $ln_itm->discount_percent));
333 $taxes = get_tax_for_items($items, $prices, $shipping_cost,
334 $this->tax_group_id, $this->tax_included, $this->tax_group_array);
340 function get_tax_free_shipping() {
342 if ($this->tax_included==0)
343 return $this->freight_cost;
345 return ($this->freight_cost - $this->get_shipping_tax());
348 function get_shipping_tax() {
350 $tax_items = get_shipping_tax_as_array();
352 if ($tax_items != null) {
353 foreach ($tax_items as $item_tax) {
354 $index = $item_tax['tax_type_id'];
355 if (isset($this->tax_group_array[$index])) {
356 $tax_rate += $item_tax['rate'];
360 if($this->tax_included)
361 return $this->freight_cost*$tax_rate/($tax_rate+100);
363 return $this->freight_cost*$tax_rate/100;
366 } /* end of class defintion */
372 var $item_description;
377 var $src_no; // number of src doc for this line
380 var $discount_percent;
381 var $qty_done; // quantity processed on child documents
382 var $qty_dispatched; // quantity selected to process
383 var $qty_old=0; // quantity dispatched before edition
386 function line_details ($stock_id, $qty, $prc, $disc_percent,
387 $qty_done, $standard_cost, $description, $id=0, $src_no=0 )
389 /* Constructor function to add a new LineDetail object with passed params */
392 $this->src_no = $src_no;
393 $item_row = get_item($stock_id);
395 if ($item_row == null)
396 display_db_error("invalid item added to order : $stock_id", "");
398 $this->mb_flag = $item_row["mb_flag"];
399 $this->units = $item_row["units"];
400 if ($description == null)
401 $this->item_description = $item_row["description"];
403 $this->item_description = $description;
404 //$this->standard_cost = $item_row["material_cost"] + $item_row["labour_cost"] + $item_row["overhead_cost"];
405 $this->tax_type = $item_row["tax_type_id"];
406 $this->tax_type_name = $item_row["tax_type_name"];
408 $this->stock_id = $stock_id;
409 $this->quantity = $qty;
410 $this->qty_dispatched = $qty;
412 $this->discount_percent = $disc_percent;
413 $this->qty_done = $qty_done;
414 $this->standard_cost = $standard_cost;
417 // get unit price as stated on document
418 function line_price()