f653c24a6b038ebf995601f50e7884d8a3575208
[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/sales_points_db.inc");
11 include_once($path_to_root . "/sales/includes/db/custalloc_db.inc");
12 include_once($path_to_root . "/sales/includes/db/cust_trans_db.inc");
13 include_once($path_to_root . "/sales/includes/db/cust_trans_details_db.inc");
14 include_once($path_to_root . "/sales/includes/db/payment_db.inc");
15 include_once($path_to_root . "/sales/includes/db/branches_db.inc");
16 include_once($path_to_root . "/sales/includes/db/customers_db.inc");
17
18 //----------------------------------------------------------------------------------------
19 // $price in customer's currency
20 // $quantity is used as is (if it's neg it's neg, if it's pos it's pos)
21 // $std_cost is in home currency
22 // $show_or_hide 1 show this item in invoice/credit views, 0 to hide it (used for write-off items)
23 // $type is 10 (invoice) or 11 (credit)
24
25 function add_stock_move_customer($type, $stock_id, $trans_id, $location, $date_, $reference,
26         $quantity, $std_cost, $show_or_hide=1, $price=0, $discount_percent=0)
27 {
28         return add_stock_move($type, $stock_id, $trans_id, $location, $date_, $reference,
29                 $quantity, $std_cost, 0, $show_or_hide, $price, $discount_percent,
30                 "The customer stock movement record cannot be inserted");
31 }
32
33 //----------------------------------------------------------------------------------------
34 // add a debtor-related gl transaction
35 // $date_ is display date (non-sql)
36 // $amount is in CUSTOMER'S currency
37
38 function add_gl_trans_customer($type, $type_no, $date_, $account, $dimension, $dimension2,
39         $amount, $customer_id, $err_msg="")
40 {
41         if ($err_msg == "")
42                 $err_msg = "The customer GL transaction could not be inserted";
43
44         return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, "", $amount,
45                 get_customer_currency($customer_id),
46                 payment_person_types::customer(), $customer_id, $err_msg);
47 }
48
49 //----------------------------------------------------------------------------------------
50
51 function get_price ($stock_id, $currency, $sales_type_id, $factor=null, $date=null)
52 {
53         if ($date == null)
54             $date = Today();
55
56         if ($factor === null) 
57         {
58                 $myrow = get_sales_type($sales_type_id);
59                 $factor = $myrow['factor'];
60         }
61             
62         $sql = "SELECT ".TB_PREF."prices.price
63                 FROM ".TB_PREF."prices
64                 WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "' "
65                 ." AND ".TB_PREF."prices.sales_type_id = " . $sales_type_id
66                 ." AND ".TB_PREF."prices.curr_abrev = '$currency'";
67
68         $msg = "There was a problem retrieving the pricing information for the part $stock_id for customer";
69         $result = db_query($sql, $msg);
70
71         if (db_num_rows($result) != 0) 
72         {
73                 $myrow = db_fetch_row($result);
74                 return $myrow[0];
75         }
76         if ($factor == 0) return 0; // auto price calculations off
77
78         $base_id = get_base_sales_type();
79         $home_curr = get_company_currency();
80
81     // get all prices which we can use to guess the price.
82     // alternative is make up to 2 additional sql queries
83         $sql = "SELECT ".TB_PREF."prices.price,".TB_PREF."prices.curr_abrev,
84                 ".TB_PREF."prices.sales_type_id
85                 FROM ".TB_PREF."prices
86                 WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "' "
87                 ." AND (".TB_PREF."prices.sales_type_id = " . $sales_type_id
88                 ." OR ".TB_PREF."prices.sales_type_id = " . $base_id.")"
89                 ." AND (".TB_PREF."prices.curr_abrev = '$currency'"
90                 ." OR ".TB_PREF."prices.curr_abrev = '$home_curr')";
91
92         $result = db_query($sql, $msg);
93
94         $prices = array();
95         while($myrow = db_fetch($result)) 
96         {
97             $prices[$myrow['sales_type_id']][$myrow['curr_abrev']] = $myrow['price'];
98         }
99         
100         $rate = round(get_exchange_rate_from_home_currency($currency, $date),
101             user_exrate_dec());
102         $price = 0.00;
103         
104         if (isset($prices[$sales_type_id][$home_curr])) 
105         {
106             $price = $prices[$sales_type_id][$home_curr] / $rate;
107         }
108         if (isset($prices[$base_id][$currency])) 
109         {
110             $price =$prices[$base_id][$currency] * $factor;
111         }
112         if (isset($prices[$base_id][$home_curr])) 
113         {
114             $price =$prices[$base_id][$home_curr] * $factor / $rate;
115         }
116         
117         return round($price, user_price_dec());
118 }
119
120 //-----------------------------------------------------------------------------
121
122 function set_document_parent($cart)
123 {
124         $inv_no = key($cart->trans_no);
125
126         if (count($cart->src_docs) == 1) {
127
128         // if this child document has only one parent - update child link
129         $del_no = reset(array_keys($cart->src_docs));
130
131         $sql = 'UPDATE '.TB_PREF.'debtor_trans SET trans_link = ' . $del_no .
132                 ' WHERE type='.$cart->trans_type.' AND trans_no='. $inv_no ;
133         db_query($sql, 'Child document link cannot be updated');
134
135         }
136         if ($cart->trans_type != 10)
137                 return 0;
138
139         // the rest is batch invoice specific
140
141         foreach ($cart->line_items as $line) {
142                 if ($line->quantity != $line->qty_dispatched) {
143                         return 1;       // this is partial invoice
144                 }
145         }
146
147         $sql = 'UPDATE '.TB_PREF.'debtor_trans SET trans_link = ' . $inv_no .
148         ' WHERE type='.get_parent_type($cart->trans_type).' AND (';
149
150         $deliveries = array_keys($cart->src_docs);
151
152         foreach ($deliveries as $key=>$del)
153                 $deliveries[$key] = 'trans_no='.$del;
154
155         $sql .= implode(' OR ', $deliveries) . ')';
156         db_query($sql, 'Delivery links cannot be updated');
157
158         return 0; // batch or complete invoice
159 }
160
161 //--------------------------------------------------------------------------------------------------
162 function get_parent_type($type)
163 {
164         $parent_types = array( 11=>10, 10=>13, 13=>30 );
165         return isset($parent_types[$type]) ?  $parent_types[$type] : 0;
166 }
167
168 //--------------------------------------------------------------------------------------------------
169 function update_parent_line($doc_type, $line_id, $qty_dispatched)
170 {
171         $doc_type = get_parent_type($doc_type);
172
173 //      echo "update line: $line_id, $doc_type, $qty_dispatched";
174         if ($doc_type==0)
175                 return false;
176         else {
177                 if ($doc_type==30)
178                         $sql = "UPDATE ".TB_PREF."sales_order_details
179                                 SET qty_sent = qty_sent + $qty_dispatched
180                                 WHERE id=$line_id";
181                 else
182                         $sql = "UPDATE ".TB_PREF."debtor_trans_details
183                                 SET qty_done = qty_done + $qty_dispatched
184                                 WHERE id=$line_id";
185         }
186         db_query($sql, "The parent document detail record could not be updated");
187         return true;
188 }
189
190 //--------------------------------------------------------------------------------------------------
191 // find inventory location for given transaction
192 //
193 function get_location(&$cart)
194 {
195         $sql = "SELECT ".TB_PREF."locations.* FROM ".TB_PREF."stock_moves,"
196                 .TB_PREF."locations".
197                 " WHERE type=".$cart->trans_type.
198                 " AND trans_no=".key($cart->trans_no).
199                 " AND qty!=0 ".
200                 " AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code";
201         $result = db_query($sql, 'Retreiving inventory location');
202
203
204         if (db_num_rows($result)) {
205                 return db_fetch($result);
206         }
207         return null;
208 }
209 //--------------------------------------------------------------------------------------------------
210 // Generic read debtor transaction into cart
211 //
212 //      $trans_no - array of trans nums; special case trans_no==0 - new doc
213 //
214 function read_sales_trans($doc_type, $trans_no, &$cart)
215 {
216         if (!is_array($trans_no) && $trans_no)
217                         $trans_no = array($trans_no);
218
219         $cart->trans_type = $doc_type;
220         if (!$trans_no) { // new document
221                 $cart->trans_no = $trans_no;
222         } else {
223                 // read header data from first document
224                 $myrow = get_customer_trans($trans_no[0],$doc_type);
225                 if (count($trans_no)>1)
226                         $cart->trans_no = get_customer_trans_version($doc_type, $trans_no);
227                 else
228                         $cart->trans_no = array($trans_no[0]=>$myrow["version"]);
229
230                 $cart->set_sales_type($myrow["tpe"], $myrow["sales_type"], $myrow["tax_included"],0);
231
232                 $cart->set_customer($myrow["debtor_no"], $myrow["DebtorName"],
233                         $myrow["curr_code"], $myrow["discount"]);
234
235                 $cart->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
236                         $myrow["tax_group_name"],       $myrow["phone"], $myrow["email"]);
237
238                 $cart->reference = $myrow["reference"];
239                 $cart->order_no = $myrow["order_"];
240                 $cart->trans_link = $myrow["trans_link"];
241                 $cart->due_date = sql2date($myrow["due_date"]);
242                 $cart->document_date = sql2date($myrow["tran_date"]);
243                 $cart->dimension_id = $myrow['dimension_id']; // added 2.1 Joe Hunt 2008-11-12
244                 $cart->dimension2_id = $myrow['dimension2_id'];
245                 $cart->Comments = '';
246                 foreach ( $trans_no as $trans ) {
247                         $coms =  get_comments($doc_type,$trans);
248                         while($row=db_fetch($coms)) {
249                                 $text = $row['memo_'];
250                                 if ($text!='') {
251                                         if ($cart->Comments!='')
252                                                 $cart->Comments .= "\n";
253                                         $cart->Comments .= $text;
254                                 }
255                         }
256                 }
257
258                 // FIX this should be calculated sum() for multiply parents
259
260                 $cart->set_delivery($myrow["ship_via"], $myrow["br_name"],
261                 $myrow["br_address"], $myrow["ov_freight"]);
262
263                 $location = 0;
264                 $myrow = get_location($cart); // find location from movement
265
266                 if($myrow!=null) {
267                         $cart->set_location($myrow['loc_code'], $myrow['location_name']);
268                 }
269
270                 $result = get_customer_trans_details($doc_type,$trans_no);
271                 if (db_num_rows($result) > 0) {
272                         for($line_no=0; $myrow = db_fetch($result); $line_no++) {
273                                 $cart->add_to_cart($line_no,$myrow["stock_id"],$myrow["quantity"],
274                                         $myrow["unit_price"], $myrow["discount_percent"],
275                                         $myrow["qty_done"], $myrow["standard_cost"],
276                                         $myrow["StockDescription"],$myrow["id"], $myrow["debtor_trans_no"]);
277                         }
278                 }
279         } // !newdoc
280
281         return true;
282 }
283 //----------------------------------------------------------------------------------------
284 ?>