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");
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)
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)
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");
31 //----------------------------------------------------------------------------------------
32 // add a debtor-related gl transaction
33 // $date_ is display date (non-sql)
34 // $amount is in CUSTOMER'S currency
36 function add_gl_trans_customer($type, $type_no, $date_, $account, $dimension, $dimension2,
37 $amount, $customer_id, $err_msg="")
40 $err_msg = "The customer GL transaction could not be inserted";
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);
47 //----------------------------------------------------------------------------------------
49 function get_price ($stock_id, $currency, $sales_type_id)
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'";
57 $result = db_query($sql, "There was a problem retrieving the pricing information for the part $stock_id for customer");
59 if (db_num_rows($result) != 0)
61 /*There is a price from one of the above so return that */
62 $myrow = db_fetch_row($result);
72 //-----------------------------------------------------------------------------
74 function set_document_parent($cart)
76 $inv_no = key($cart->trans_no);
78 if (count($cart->src_docs) == 1) {
80 // if this child document has only one parent - update child link
81 $del_no = key($cart->src_docs);
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');
88 if ($cart->trans_type != 10)
91 // the rest is batch invoice specific
93 foreach ($cart->line_items as $line) {
94 if ($line->quantity != $line->qty_dispatched) {
95 return 1; // this is partial invoice
99 $sql = 'UPDATE '.TB_PREF.'debtor_trans SET trans_link = ' . $inv_no .
100 ' WHERE type='.get_parent_type($cart->trans_type).' AND (';
102 $deliveries = array_keys($cart->src_docs);
104 foreach ($deliveries as $key=>$del)
105 $deliveries[$key] = 'trans_no='.$del;
107 $sql .= implode(' OR ', $deliveries) . ')';
108 db_query($sql, 'Delivery links cannot be updated');
110 return 0; // batch or complete invoice
113 //--------------------------------------------------------------------------------------------------
114 function get_parent_type($type)
116 $parent_types = array( 11=>10, 10=>13, 13=>30 );
117 return isset($parent_types[$type]) ? $parent_types[$type] : 0;
120 //--------------------------------------------------------------------------------------------------
121 function update_parent_line($doc_type, $line_id, $qty_dispatched)
123 $doc_type = get_parent_type($doc_type);
125 //echo "update line: $line_id, $doc_type, $qty_dispatch";
130 $sql = "UPDATE ".TB_PREF."sales_order_details
131 SET qty_sent = qty_sent + $qty_dispatched
134 $sql = "UPDATE ".TB_PREF."debtor_trans_details
135 SET qty_done = qty_done + $qty_dispatched
138 db_query($sql, "The parent document detail record could not be updated");
142 //--------------------------------------------------------------------------------------------------
143 // find inventory location for given transaction
145 function get_location(&$cart)
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).
152 " AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code";
153 $result = db_query($sql, 'Retreiving inventory location');
156 if (db_num_rows($result)) {
157 return db_fetch($result);
161 //--------------------------------------------------------------------------------------------------
162 // Generic read debtor transaction into cart
164 // $trans_no - array of trans nums; special case trans_no==0 - new doc
166 function read_sales_trans($doc_type, $trans_no, &$cart)
168 if (!is_array($trans_no) && $trans_no)
169 $trans_no = array($trans_no);
171 $cart->trans_type = $doc_type;
172 if (!$trans_no) { // new document
173 $cart->trans_no = $trans_no;
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);
180 $cart->trans_no = array($trans_no[0]=>$myrow["version"]);
182 $cart->set_sales_type($myrow["tpe"], $myrow["sales_type"], $myrow["tax_included"]);
184 $cart->set_customer($myrow["debtor_no"], $myrow["DebtorName"],
185 $myrow["curr_code"], $myrow["discount"]);
187 $cart->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
188 $myrow["tax_group_name"], $myrow["phone"], $myrow["email"]);
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"]);
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_'];
202 if ($cart->Comments!='')
203 $cart->Comments .= "\n";
204 $cart->Comments .= $text;
209 // FIX this should be calculated sum() for multiply parents
211 $cart->set_delivery($myrow["ship_via"], $myrow["br_name"],
212 $myrow["br_address"], $myrow["ov_freight"]);
215 $myrow = get_location($cart); // find location from movement
218 $cart->set_location($myrow['loc_code'], $myrow['location_name']);
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"]);
234 //----------------------------------------------------------------------------------------