Added debtor_trans_details.src_id field
[fa-stable.git] / sql / alter2.3.php
index 07e84e44a2f49f0c5cdb09001332c94f6940b9a5..a429430b1be8cc63fea14ea3118e04980699258c 100644 (file)
@@ -26,7 +26,57 @@ class fa2_3 {
        //
        function install($pref, $force) 
        {
-               return true;
+               $sql = "SELECT debtor_no, payment_terms FROM {$pref}debtors_master";
+               
+               $result = db_query($sql);
+               if (!$result) {
+                       display_error("Cannot read customers"
+                       .':<br>'. db_error_msg($db));
+                       return false;
+               }
+               // update all sales orders and transactions with customer std payment terms
+               while($cust = db_fetch($result)) {
+                       $sql = "UPDATE {$pref}debtor_trans SET "
+                               ."payment_terms = '" .$cust['payment_terms']
+                               ."' WHERE debtor_no='".$cust['debtor_no']."'";
+                       if (db_query($sql)==false) {
+                               display_error("Cannot update cust trans payment"
+                               .':<br>'. db_error_msg($db));
+                               return false;
+                       }
+                       $sql = "UPDATE {$pref}sales_orders SET "
+                               ."payment_terms = '" .$cust['payment_terms']
+                               ."' WHERE debtor_no='".$cust['debtor_no']."'";
+                       if (db_query($sql)==false) {
+                               display_error("Cannot update sales order payment"
+                               .':<br>'. db_error_msg($db));
+                               return false;
+                       }
+               }
+               //remove obsolete and temporary columns.
+               // this have to be done here as db_import rearranges alter query order
+               $dropcol = array(
+                       'crm_persons' => array('tmp_id','tmp_class'),
+                       'debtors_master' => array('email'),
+                       'cust_branch' => array('phone', 'phone2', 'fax', 'email'),
+                       'suppliers' => array('phone', 'phone2', 'fax', 'email')
+               );
+
+               foreach($dropcol as $table => $columns)
+                       foreach($columns as $col) {
+                       if (db_query("ALTER TABLE `{$pref}{$table}` DROP `$col`")==false) {
+                               display_error("Cannot drop {$table}.{$col} column:<br>".db_error_msg($db));
+                               return false;
+                       }
+               }
+               if (!update_totals_2_3($pref)) {
+                       display_error("Cannot update order totals");
+                       return false;
+               }
+               // remove old preferences table after upgrade script has been executed
+               $sql = "DROP TABLE IF EXISTS `{$pref}company`";
+
+               return db_query($sql) && update_company_prefs(array('version_id'=>'2.3'), $pref);
        }
        //
        //      Checking before install
@@ -39,15 +89,45 @@ class fa2_3 {
        //      Test if patch was applied before.
        //
        function installed($pref) {
-               $n = 1; // number of patches to be installed
+               $n = 3; // number of patches to be installed
                $patchcnt = 0;
 
                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++;
 
                $n -= $patchcnt;
                return $n == 0 ? true : $patchcnt;
        }
-};
+}
+/*
+       Update order totals
+*/
+function update_totals_2_3($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 FROM {$pref}sales_orders";
+       $orders = db_query($sql);
+       while ($order_no = db_fetch($orders)) {
+               read_sales_order($order_no[0], $cart, ST_SALESORDER);
+               update_sales_order($cart);
+               unset($cart->line_items);
+       }
+       unset($cart);
+       $cart = new purch_order();
+       $sql = "SELECT order_no FROM {$pref}purch_orders";
+       $orders = db_query($sql);
+       while ($order_no = db_fetch($orders)) {
+               read_po($order_no[0], $cart);
+               update_po($cart);
+               unset($cart->line_items);
+       }
+}
 
 $install = new fa2_3;
+
 ?>
\ No newline at end of file