+ if (!$this->beta) {
+ $n += 3;
+ if (!check_table($pref, 'comments', 'type', array('Key'=>'MUL'))) $patchcnt++;
+ if (!check_table($pref, 'sys_prefs')) $patchcnt++;
+ if (!check_table($pref, 'sales_orders', 'payment_terms')) $patchcnt++;
+ }
+ if (!check_table($pref, 'purch_orders', 'tax_included')) $patchcnt++;
+ return $n == $patchcnt ? true : ($patchcnt ? ($patchcnt.'/'. $n) : 0);
+ }
+ //=========================================================================================
+ // 2.3 specific update functions
+ //
+
+ /*
+ Update order totals
+ */
+ function update_totals($pref)
+ {
+ global $path_to_root;
+
+ 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 {$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 {$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 {$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 {$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($pref)
+ {
+ global $path_to_root, $systypes_array;
+
+ require_once("$path_to_root/includes/sysnames.inc");
+
+ $sql = "SELECT d.type, trans_no, order_ FROM {$pref}debtor_trans d
+ LEFT JOIN {$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