3 include_once($path_to_root . "/includes/banking.inc");
4 include_once($path_to_root . "/includes/db/inventory_db.inc");
5 include_once($path_to_root . "/sales/includes/db/sales_order_db.inc");
6 include_once($path_to_root . "/sales/includes/db/sales_credit_db.inc");
7 include_once($path_to_root . "/sales/includes/db/sales_invoice_db.inc");
8 include_once($path_to_root . "/sales/includes/db/sales_delivery_db.inc");
9 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
10 include_once($path_to_root . "/sales/includes/db/custalloc_db.inc");
11 include_once($path_to_root . "/sales/includes/db/cust_trans_db.inc");
12 include_once($path_to_root . "/sales/includes/db/cust_trans_details_db.inc");
13 include_once($path_to_root . "/sales/includes/db/payment_db.inc");
14 include_once($path_to_root . "/sales/includes/db/branches_db.inc");
15 include_once($path_to_root . "/sales/includes/db/customers_db.inc");
17 //----------------------------------------------------------------------------------------
18 // $price in customer's currency
19 // $quantity is used as is (if it's neg it's neg, if it's pos it's pos)
20 // $std_cost is in home currency
21 // $show_or_hide 1 show this item in invoice/credit views, 0 to hide it (used for write-off items)
22 // $type is 10 (invoice) or 11 (credit)
24 function add_stock_move_customer($type, $stock_id, $trans_id, $location, $date_, $reference,
25 $quantity, $std_cost, $show_or_hide=1, $price=0, $discount_percent=0)
27 return add_stock_move($type, $stock_id, $trans_id, $location, $date_, $reference,
28 $quantity, $std_cost, 0, $show_or_hide, $price, $discount_percent,
29 "The customer stock movement record cannot be inserted");
32 //----------------------------------------------------------------------------------------
33 // add a debtor-related gl transaction
34 // $date_ is display date (non-sql)
35 // $amount is in CUSTOMER'S currency
37 function add_gl_trans_customer($type, $type_no, $date_, $account, $dimension, $dimension2,
38 $amount, $customer_id, $err_msg="", $rate=0)
41 $err_msg = "The customer GL transaction could not be inserted";
43 return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, "", $amount,
44 get_customer_currency($customer_id),
45 payment_person_types::customer(), $customer_id, $err_msg, $rate);
48 //----------------------------------------------------------------------------------------
50 function get_price ($stock_id, $currency, $sales_type_id, $factor=null, $date=null)
57 $myrow = get_sales_type($sales_type_id);
58 $factor = $myrow['factor'];
61 $sql = "SELECT ".TB_PREF."prices.price
62 FROM ".TB_PREF."prices
63 WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "' "
64 ." AND ".TB_PREF."prices.sales_type_id = " . $sales_type_id
65 ." AND ".TB_PREF."prices.curr_abrev = '$currency'";
67 $msg = "There was a problem retrieving the pricing information for the part $stock_id for customer";
68 $result = db_query($sql, $msg);
70 if (db_num_rows($result) != 0)
72 $myrow = db_fetch_row($result);
75 if ($factor == 0) return 0; // auto price calculations off
77 $base_id = get_base_sales_type();
78 $home_curr = get_company_currency();
80 // get all prices which we can use to guess the price.
81 // alternative is make up to 2 additional sql queries
82 $sql = "SELECT ".TB_PREF."prices.price,".TB_PREF."prices.curr_abrev,
83 ".TB_PREF."prices.sales_type_id
84 FROM ".TB_PREF."prices
85 WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "' "
86 ." AND (".TB_PREF."prices.sales_type_id = " . $sales_type_id
87 ." OR ".TB_PREF."prices.sales_type_id = " . $base_id.")"
88 ." AND (".TB_PREF."prices.curr_abrev = '$currency'"
89 ." OR ".TB_PREF."prices.curr_abrev = '$home_curr')";
91 $result = db_query($sql, $msg);
94 while($myrow = db_fetch($result))
96 $prices[$myrow['sales_type_id']][$myrow['curr_abrev']] = $myrow['price'];
99 $rate = round(get_exchange_rate_from_home_currency($currency, $date),
103 if (isset($prices[$sales_type_id][$home_curr]))
105 $price = $prices[$sales_type_id][$home_curr] / $rate;
107 if (isset($prices[$base_id][$currency]))
109 $price =$prices[$base_id][$currency] * $factor;
111 if (isset($prices[$base_id][$home_curr]))
113 $price =$prices[$base_id][$home_curr] * $factor / $rate;
116 return round($price, user_price_dec());
119 //-----------------------------------------------------------------------------
121 function set_document_parent($cart)
123 $inv_no = key($cart->trans_no);
125 if (count($cart->src_docs) == 1) {
127 // if this child document has only one parent - update child link
128 $del_no = reset(array_keys($cart->src_docs));
130 $sql = 'UPDATE '.TB_PREF.'debtor_trans SET trans_link = ' . $del_no .
131 ' WHERE type='.$cart->trans_type.' AND trans_no='. $inv_no ;
132 db_query($sql, 'Child document link cannot be updated');
135 if ($cart->trans_type != 10)
138 // the rest is batch invoice specific
140 foreach ($cart->line_items as $line) {
141 if ($line->quantity != $line->qty_dispatched) {
142 return 1; // this is partial invoice
146 $sql = 'UPDATE '.TB_PREF.'debtor_trans SET trans_link = ' . $inv_no .
147 ' WHERE type='.get_parent_type($cart->trans_type).' AND (';
149 $deliveries = array_keys($cart->src_docs);
151 foreach ($deliveries as $key=>$del)
152 $deliveries[$key] = 'trans_no='.$del;
154 $sql .= implode(' OR ', $deliveries) . ')';
155 db_query($sql, 'Delivery links cannot be updated');
157 return 0; // batch or complete invoice
160 //--------------------------------------------------------------------------------------------------
161 function get_parent_type($type)
163 $parent_types = array( 11=>10, 10=>13, 13=>30 );
164 return isset($parent_types[$type]) ? $parent_types[$type] : 0;
167 //--------------------------------------------------------------------------------------------------
168 function update_parent_line($doc_type, $line_id, $qty_dispatched)
170 $doc_type = get_parent_type($doc_type);
172 // echo "update line: $line_id, $doc_type, $qty_dispatched";
177 $sql = "UPDATE ".TB_PREF."sales_order_details
178 SET qty_sent = qty_sent + $qty_dispatched
181 $sql = "UPDATE ".TB_PREF."debtor_trans_details
182 SET qty_done = qty_done + $qty_dispatched
185 db_query($sql, "The parent document detail record could not be updated");
189 //--------------------------------------------------------------------------------------------------
190 // find inventory location for given transaction
192 function get_location(&$cart)
194 $sql = "SELECT ".TB_PREF."locations.* FROM ".TB_PREF."stock_moves,"
195 .TB_PREF."locations".
196 " WHERE type=".$cart->trans_type.
197 " AND trans_no=".key($cart->trans_no).
199 " AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code";
200 $result = db_query($sql, 'Retreiving inventory location');
203 if (db_num_rows($result)) {
204 return db_fetch($result);
208 //--------------------------------------------------------------------------------------------------
209 // Generic read debtor transaction into cart
211 // $trans_no - array of trans nums; special case trans_no==0 - new doc
213 function read_sales_trans($doc_type, $trans_no, &$cart)
215 if (!is_array($trans_no) && $trans_no)
216 $trans_no = array($trans_no);
218 $cart->trans_type = $doc_type;
219 if (!$trans_no) { // new document
220 $cart->trans_no = $trans_no;
222 // read header data from first document
223 $myrow = get_customer_trans($trans_no[0],$doc_type);
224 if (count($trans_no)>1)
225 $cart->trans_no = get_customer_trans_version($doc_type, $trans_no);
227 $cart->trans_no = array($trans_no[0]=>$myrow["version"]);
229 $cart->set_sales_type($myrow["tpe"], $myrow["sales_type"], $myrow["tax_included"],0);
231 $cart->set_customer($myrow["debtor_no"], $myrow["DebtorName"],
232 $myrow["curr_code"], $myrow["discount"]);
234 $cart->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
235 $myrow["tax_group_name"], $myrow["phone"], $myrow["email"]);
237 $cart->reference = $myrow["reference"];
238 $cart->order_no = $myrow["order_"];
239 $cart->trans_link = $myrow["trans_link"];
240 $cart->due_date = sql2date($myrow["due_date"]);
241 $cart->document_date = sql2date($myrow["tran_date"]);
243 $cart->Comments = '';
244 foreach ( $trans_no as $trans ) {
245 $coms = get_comments($doc_type,$trans);
246 while($row=db_fetch($coms)) {
247 $text = $row['memo_'];
249 if ($cart->Comments!='')
250 $cart->Comments .= "\n";
251 $cart->Comments .= $text;
256 // FIX this should be calculated sum() for multiply parents
258 $cart->set_delivery($myrow["ship_via"], $myrow["br_name"],
259 $myrow["br_address"], $myrow["ov_freight"]);
262 $myrow = get_location($cart); // find location from movement
265 $cart->set_location($myrow['loc_code'], $myrow['location_name']);
268 $result = get_customer_trans_details($doc_type,$trans_no);
269 if (db_num_rows($result) > 0) {
270 for($line_no=0; $myrow = db_fetch($result); $line_no++) {
271 $cart->add_to_cart($line_no,$myrow["stock_id"],$myrow["quantity"],
272 $myrow["unit_price"], $myrow["discount_percent"],
273 $myrow["qty_done"], $myrow["standard_cost"],
274 $myrow["StockDescription"],$myrow["id"], $myrow["debtor_trans_no"]);
281 //----------------------------------------------------------------------------------------