Added confirmation js dialogs for deleting and restoring backup file.
[fa-stable.git] / sql / alter2.2.php
index 1d6cb8fc307d4681f137980c7cb0cc9d8075eea6..21fa6822c7f5492f927e7a05739811b0e65c9c9d 100644 (file)
@@ -81,7 +81,27 @@ class fa2_2 {
                        }
                  }
                }
-*/             
+*/
+       
+       if (!($ret = db_query("SELECT MAX(`order_no`) FROM `{$pref}sales_orders`")) ||
+               !db_num_rows($ret))
+       {
+               display_error(_('Cannot query max sales order number.'));
+               return false;
+       } 
+       $row = db_fetch($ret);
+       $max_order = $row[0];
+       $next_ref = $max_order+1;
+       $sql = "UPDATE `{$pref}sys_types` 
+               SET `type_no`='$max_order', 
+                       `next_reference`='$next_ref'
+               WHERE `type_id`=30";
+       if(!db_query($sql))
+       {
+               display_error(_('Cannot store next sales order reference.'));
+               return false;
+       }
+
        return convert_roles($pref);
        }
        //
@@ -96,13 +116,22 @@ class fa2_2 {
        //      Test if patch was applied before.
        //
        function installed($pref) {
-               if (check_table($pref, 'company', 'login_tout')) return false;
-               if (check_table($pref, 'stock_category', 'dflt_dim2')) return false;
-               if (check_table($pref, 'users', 'sticky_doc_date')) return false;
-               if (check_table($pref, 'audit_trail')) return false;
-               if (check_table($pref, 'stock_master','no_sale')) return false;
-               if (check_table($pref, 'users', 'role_id')) return false;
-                       return true;
+               $n = 14; // number of features to be installed
+               if (check_table($pref, 'company', 'custom1_name')) $n--;
+               if (!check_table($pref, 'company', 'profit_loss_year_act')) $n--;
+               if (!check_table($pref, 'company', 'login_tout')) $n--;
+               if (!check_table($pref, 'stock_category', 'dflt_no_sale')) $n--;
+               if (!check_table($pref, 'users', 'sticky_doc_date')) $n--;
+               if (!check_table($pref, 'users', 'startup_tab')) $n--;
+               if (!check_table($pref, 'cust_branch', 'inactive')) $n--;
+               if (!check_table($pref, 'chart_class', 'ctype')) $n--;
+               if (!check_table($pref, 'audit_trail')) $n--;
+               if (!check_table($pref, 'currencies', 'auto_update')) $n--;
+               if (!check_table($pref, 'stock_master','no_sale')) $n--;
+               if (!check_table($pref, 'suppliers', 'supp_ref')) $n--;
+               if (!check_table($pref, 'users', 'role_id')) $n--;
+               if (!check_table($pref, 'sales_orders', 'reference')) $n--;
+               return $n == 0 ? true : 14 - $n;
        }
 };
 
@@ -155,14 +184,14 @@ function convert_roles($pref)
                20 => array('SA_CREATECOMPANY', 'SA_CREATELANGUAGE', 'SA_CREATEMODULES',
                        'SA_SOFTWAREUPGRADE', 'SA_SECROLES')
                );
-               
+               $new_ids = array();
                foreach ($security_groups as $role_id => $areas) {
                        $area_set = array();
                        $sections = array();
                        foreach ($areas as $a) {
                         if (isset($trans_sec[$a]))
                                foreach ($trans_sec[$a] as $id) {
-                                if ($security_areas[$id][0]==0)
+                                if ($security_areas[$id][0] != 0)
 //                                     error_log('invalid area id: '.$a.':'.$id);
                                        $area_set[] = $security_areas[$id][0];
                                        $sections[$security_areas[$id][0]&~0xff] = 1;
@@ -171,11 +200,20 @@ function convert_roles($pref)
                        $sections  = array_keys($sections);
                        sort($sections); sort($area_set);
                        import_security_role($pref, $security_headings[$role_id], $sections, $area_set);
-                       $new = db_insert_id();
-                       $sql = "UPDATE {$pref}users set role_id=$new WHERE role_id=$role_id";
-                       $ret = db_query($sql, 'cannot update users roles');
-                       if(!$ret) return false;
+                       $new_ids[$role_id] = db_insert_id();
+               }
+               $result = get_users(true);
+               $users = array();
+               while($row = db_fetch($result)) { // complete old user ids and roles
+                       $users[$row['role_id']][] = $row['id'];
                }
+               foreach($users as $old_id => $uids)
+                       foreach( $uids as $id) {
+                               $sql = "UPDATE {$pref}users set role_id=".$new_ids[$old_id].
+                                       " WHERE id=$id";
+                               $ret = db_query($sql, 'cannot update users roles');
+                               if(!$ret) return false;
+                       }
                return true;
 }