+ include_once("$path_to_root/sales/includes/cart_class.inc");
+ include_once("$path_to_root/purchasing/includes/po_class.inc");
+ $cart = new cart(ST_SALESORDER);
+ $sql = "SELECT order_no, trans_type FROM ".TB_PREF."sales_orders";
+ $orders = db_query($sql);
+ if (!$orders)
+ return false;
+ while ($order = db_fetch($orders)) {
+ read_sales_order($order['order_no'], $cart, $order['trans_type']);
+ $result = db_query("UPDATE ".TB_PREF."sales_orders
+ SET total=".$cart->get_trans_total()
+ ." WHERE order_no=".$order[0]);
+ unset($cart->line_items);
+ }
+ unset($cart);
+ $cart = new purch_order();
+ $sql = "SELECT order_no FROM ".TB_PREF."purch_orders";
+ $orders = db_query($sql);
+ if (!$orders)
+ return false;
+ while ($order_no = db_fetch($orders)) {
+ read_po($order_no[0], $cart);
+ $result = db_query("UPDATE ".TB_PREF."purch_orders SET total=".$cart->get_trans_total());
+ unset($cart->line_items);
+ }
+ return true;
+ }
+
+ //------------------------------------------------------------------------------
+ // Retreive parent document number(s) for given transaction
+ //
+ function get_parent_trans_2_2($trans_type, $trans_no) {
+
+ $sql = 'SELECT trans_link FROM
+ '.TB_PREF.'debtor_trans WHERE trans_no='.db_escape($trans_no)
+ .' AND type='.db_escape($trans_type).' AND trans_link!=0';
+
+ $result = db_query($sql, 'Parent document numbers cannot be retrieved');
+
+ if (db_num_rows($result)) {
+ $link = db_fetch($result);
+ return array($link['trans_link']);
+ }
+ if ($trans_type!=ST_SALESINVOICE) return 0; // this is credit note with no parent invoice
+ // invoice: find batch invoice parent trans.
+ $sql = 'SELECT trans_no FROM
+ '.TB_PREF.'debtor_trans WHERE
+ (trans_link='.db_escape($trans_no).' AND type='. get_parent_type($trans_type) .')';
+
+ $result = db_query($sql, 'Delivery links cannot be retrieved');
+
+ $delivery = array();
+ if(db_num_rows($result)>0) {
+ while($link = db_fetch($result)) {
+ $delivery[] = $link['trans_no'];
+ }
+ }
+ return count($delivery) ? $delivery : 0;
+ }
+
+ /*
+ Reorganizing document relations. Due to the design issue in pre 2.3 db structure
+ there can be sales documents with lines not properly linked to parents. This rare
+ cases will be described in error log.
+ */
+ function update_line_relations()
+ {
+ global $path_to_root, $systypes_array;
+
+ require_once("$path_to_root/includes/sysnames.inc");
+
+ $sql = "SELECT d.type, trans_no, order_ FROM ".TB_PREF."debtor_trans d
+ LEFT JOIN ".TB_PREF."voided v ON d.type=v.type AND d.trans_no=v.id
+ WHERE ISNULL(v.type) AND
+ (d.type=".ST_CUSTDELIVERY
+ ." OR d.type=".ST_SALESINVOICE
+ ." OR d.type=".ST_CUSTCREDIT.")";
+ $result = db_query($sql);
+ if (!$result)
+ return false;
+
+ while ($trans = db_fetch($result)) {
+ $type = $trans['type'];
+ $trans_no = $trans['trans_no'];
+ $invalid = 0;
+ $msg ='';
+
+ $lines = get_customer_trans_details($type, $trans_no);
+ $n = db_num_rows($lines);
+
+ if ($type==ST_CUSTDELIVERY)
+ $src_lines = get_sales_order_details($trans['order_'], ST_SALESORDER);
+ else
+ $src_lines = get_customer_trans_details(get_parent_type($type),
+ $this->get_parent_trans_2_2($type, $trans_no));
+
+ $src_n = db_num_rows($src_lines);
+
+ if (($type == ST_CUSTCREDIT) && ($src_n == 0))
+ continue; // free credit note has no src lines
+
+ $max = $type == ST_CUSTDELIVERY ? $n : max($src_n, $n);
+
+ for($i = 0, $j=0; $i < $max; $i++) {
+ if (!($doc_line = @db_fetch($lines)))
+ break;
+
+ if(!($src_line = @db_fetch($src_lines)))
+ break;
+
+ if ($type == ST_CUSTDELIVERY)
+ $src_line['stock_id'] = $src_line['stk_code']; // SO details has another field name
+
+ if ($src_line['stock_id'] == $doc_line['stock_id']
+ && ($src_line['quantity'] >= $doc_line['quantity'])) {
+
+ $sql = "UPDATE ".TB_PREF."debtor_trans_details SET src_id = {$src_line['id']}
+ WHERE id = {$doc_line['id']}";
+ if (!db_query($sql))
+ return false;
+ $j++;
+ }
+ }
+ if ($j != $n) {
+ error_log("Line level relations error for ".$systypes_array[$type]." #$trans_no.");
+ }
+ }
+ return true;