Cleanup.
[fa-stable.git] / sql / alter2.3.php
index 07e84e44a2f49f0c5cdb09001332c94f6940b9a5..69d3b171d5f6d8be297fb0c67e3407a49c2a9da4 100644 (file)
@@ -26,7 +26,37 @@ 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 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'));
        }
        //
        //      Checking before install
@@ -39,15 +69,16 @@ 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;
        }
-};
-
+}      
 $install = new fa2_3;
 ?>
\ No newline at end of file