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