Corrected 'factor' parameter checking (fixes 0000012)
[fa-stable.git] / sales / includes / sales_db.inc
1 <?php
2
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");
16
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)
23
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)
26 {
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");
30 }
31
32 //----------------------------------------------------------------------------------------
33 // add a debtor-related gl transaction
34 // $date_ is display date (non-sql)
35 // $amount is in CUSTOMER'S currency
36
37 function add_gl_trans_customer($type, $type_no, $date_, $account, $dimension, $dimension2,
38         $amount, $customer_id, $err_msg="")
39 {
40         if ($err_msg == "")
41                 $err_msg = "The customer GL transaction could not be inserted";
42
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);
46 }
47
48 //----------------------------------------------------------------------------------------
49
50 function get_price ($stock_id, $currency, $sales_type_id, $factor=null, $date=null)
51 {
52         if ($date == null)
53             $date = Today();
54
55         if ($factor === null) 
56         {
57                 $myrow = get_sales_type(get_post('sales_type_id'));
58                 $factor = $myrow['factor'];
59         }
60             
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'";
66
67         $msg = "There was a problem retrieving the pricing information for the part $stock_id for customer";
68         $result = db_query($sql, $msg);
69
70         if (db_num_rows($result) != 0) 
71         {
72                 $myrow = db_fetch_row($result);
73                 return $myrow[0];
74         }
75         if ($factor == 0) return 0; // auto price calculations off
76
77         $base_id = get_base_sales_type();
78         $home_curr = get_company_currency();
79
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')";
90
91         $result = db_query($sql, $msg);
92
93         $prices = array();
94         while($myrow = db_fetch($result)) 
95         {
96             $prices[$myrow['sales_type_id']][$myrow['curr_abrev']] = $myrow['price'];
97         }
98         
99         $rate = round(get_exchange_rate_from_home_currency($currency, $date),
100             user_exrate_dec());
101         $price = 0.00;
102         
103         if (isset($prices[$sales_type_id][$home_curr])) 
104         {
105             $price = $prices[$sales_type_id][$home_curr] / $rate;
106         }
107         if (isset($prices[$base_id][$currency])) 
108         {
109             $price =$prices[$base_id][$currency] * $factor;
110         }
111         if (isset($prices[$base_id][$home_curr])) 
112         {
113             $price =$prices[$base_id][$home_curr] * $factor / $rate;
114         }
115         
116         return round($price, user_price_dec());
117 }
118
119 //-----------------------------------------------------------------------------
120
121 function set_document_parent($cart)
122 {
123         $inv_no = key($cart->trans_no);
124
125         if (count($cart->src_docs) == 1) {
126
127         // if this child document has only one parent - update child link
128         $del_no = key($cart->src_docs);
129
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');
133
134         }
135         if ($cart->trans_type != 10)
136                 return 0;
137
138         // the rest is batch invoice specific
139
140         foreach ($cart->line_items as $line) {
141                 if ($line->quantity != $line->qty_dispatched) {
142                         return 1;       // this is partial invoice
143                 }
144         }
145
146         $sql = 'UPDATE '.TB_PREF.'debtor_trans SET trans_link = ' . $inv_no .
147         ' WHERE type='.get_parent_type($cart->trans_type).' AND (';
148
149         $deliveries = array_keys($cart->src_docs);
150
151         foreach ($deliveries as $key=>$del)
152                 $deliveries[$key] = 'trans_no='.$del;
153
154         $sql .= implode(' OR ', $deliveries) . ')';
155         db_query($sql, 'Delivery links cannot be updated');
156
157         return 0; // batch or complete invoice
158 }
159
160 //--------------------------------------------------------------------------------------------------
161 function get_parent_type($type)
162 {
163         $parent_types = array( 11=>10, 10=>13, 13=>30 );
164         return isset($parent_types[$type]) ?  $parent_types[$type] : 0;
165 }
166
167 //--------------------------------------------------------------------------------------------------
168 function update_parent_line($doc_type, $line_id, $qty_dispatched)
169 {
170         $doc_type = get_parent_type($doc_type);
171
172         //echo "update line: $line_id, $doc_type, $qty_dispatch";
173         if ($doc_type==0)
174                 return false;
175         else {
176                 if ($doc_type==30)
177                         $sql = "UPDATE ".TB_PREF."sales_order_details
178                                 SET qty_sent = qty_sent + $qty_dispatched
179                                 WHERE id=$line_id";
180                 else
181                         $sql = "UPDATE ".TB_PREF."debtor_trans_details
182                                 SET qty_done = qty_done + $qty_dispatched
183                                 WHERE id=$line_id";
184         }
185         db_query($sql, "The parent document detail record could not be updated");
186         return true;
187 }
188
189 //--------------------------------------------------------------------------------------------------
190 // find inventory location for given transaction
191 //
192 function get_location(&$cart)
193 {
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).
198                 " AND qty!=0 ".
199                 " AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code";
200         $result = db_query($sql, 'Retreiving inventory location');
201
202
203         if (db_num_rows($result)) {
204                 return db_fetch($result);
205         }
206         return null;
207 }
208 //--------------------------------------------------------------------------------------------------
209 // Generic read debtor transaction into cart
210 //
211 //      $trans_no - array of trans nums; special case trans_no==0 - new doc
212 //
213 function read_sales_trans($doc_type, $trans_no, &$cart)
214 {
215         if (!is_array($trans_no) && $trans_no)
216                         $trans_no = array($trans_no);
217
218         $cart->trans_type = $doc_type;
219         if (!$trans_no) { // new document
220                 $cart->trans_no = $trans_no;
221         } else {
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);
226                 else
227                         $cart->trans_no = array($trans_no[0]=>$myrow["version"]);
228
229                 $cart->set_sales_type($myrow["tpe"], $myrow["sales_type"], $myrow["tax_included"],0);
230
231                 $cart->set_customer($myrow["debtor_no"], $myrow["DebtorName"],
232                         $myrow["curr_code"], $myrow["discount"]);
233
234                 $cart->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
235                         $myrow["tax_group_name"],       $myrow["phone"], $myrow["email"]);
236
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"]);
242
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_'];
248                                 if ($text!='') {
249                                         if ($cart->Comments!='')
250                                                 $cart->Comments .= "\n";
251                                         $cart->Comments .= $text;
252                                 }
253                         }
254                 }
255
256                 // FIX this should be calculated sum() for multiply parents
257
258                 $cart->set_delivery($myrow["ship_via"], $myrow["br_name"],
259                 $myrow["br_address"], $myrow["ov_freight"]);
260
261                 $location = 0;
262                 $myrow = get_location($cart); // find location from movement
263
264                 if($myrow!=null) {
265                         $cart->set_location($myrow['loc_code'], $myrow['location_name']);
266                 }
267
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"]);
275                         }
276                 }
277         } // !newdoc
278
279         return true;
280 }
281 //----------------------------------------------------------------------------------------
282 ?>