Changed name tax type uniqueness constraint to (name, rate)
[fa-stable.git] / update_db.php
1 <?php
2 //ini_set("display_errors", "on");
3 $path_to_root = ".";
4 include_once($path_to_root. "/admin/db/maintenance_db.inc");
5 include_once($path_to_root. "/includes/db/connect_db.inc");
6 include_once($path_to_root. "/includes/ui/ui_view.inc");
7 include_once($path_to_root. "/includes/ui/ui_input.inc");
8
9 $js = get_js_png_fix();
10 $js .= get_js_set_focus("user");
11 $image = $path_to_root."/themes/default/images/logo_frontaccounting.png";
12 $title = "Update All Company Databases";
13
14 function display_error($msg, $center=true)
15 {
16     echo "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='#CC3300' width='50%'>
17       <tr>
18         <td  " . ($center?"align='center' ":"") . " width='100%' bgcolor='#ffcccc'><font color='#dd2200'>$msg</font></td>
19       </tr>
20     </table></center><br>\n";
21 }
22
23 function display_notification($msg, $center=true)
24 {
25     echo "<center><table border='1' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='#33cc00' width='50%'>
26       <tr>
27         <td " . ($center?"align='center' ":"") . " width='100%' bgcolor='#ccffcc'><font color='#007700'>$msg</font></td>
28       </tr>
29     </table></center><br>\n";
30 }
31
32
33 function db_open($conn)
34 {
35         $db = mysql_connect($conn["host"] ,$conn["dbuser"], $conn["dbpassword"]);
36         if (!$db)
37                 return false;
38         if (!mysql_select_db($conn["dbname"], $db))
39                 return false;
40         return $db;
41 }
42
43 echo "<html dir='ltr' >\n";
44 echo "<head><title>$title</title>\n";
45 echo "<meta http-equiv='Content-type' content='text/html'; charset='iso-8859-1'>\n";
46 echo "<link href='$path_to_root/themes/default/default.css' rel='stylesheet' type='text/css' />\n";
47 echo $js;
48 echo "</head> \n";
49 echo "<body style='background-color:#f9f9f9;' onload='setFocus();'>";
50
51 echo "<br><br><br><br>";
52 echo "<table align='center' width='50%' cellpadding=3 border=1 bordercolor='#cccccc' style='border-collapse: collapse'>\n";
53 echo "<tr><td align='center' valign='bottom'><img src='$image' alt='FrontAccounting' width='250' height='50' onload='fixPNG(this)' border='0' /></td></tr>\n";
54 echo "</table>\n";
55
56 echo "<br><br>";
57 echo "<center><span class='headingtext'>$title</span></center>\n";
58 echo "<br>";
59
60 if (isset($_POST["submit"]))
61 {
62         if (!isset($_FILES['uploadfile']['tmp_name']) || !is_uploaded_file($_FILES['uploadfile']['tmp_name']))
63         {
64                 display_error("You must select an SQL script for update");;
65         }
66         else
67         {
68                 include_once($path_to_root."/config_db.php");
69                 if (!isset($_POST['user']) || !isset($_POST['passwd']) || $_POST['user'] == "")
70                 {
71                         display_error("You must select a user name and an optional password");
72                 }
73                 else
74                 {
75                         foreach($db_connections as $id => $conn)
76                         {
77                                 $conn['dbuser'] = $_POST['user'];
78                                 $conn['dbpassword'] = $_POST['passwd'];
79                                 if (!($db = db_open($conn)))
80                                 {
81                                         display_error("Wrong user name or password - ".mysql_error());
82                                 }
83                                 else
84                                 {
85                                         if (!db_import($_FILES['uploadfile']['tmp_name'], $conn))
86                                                 display_error("Bad SQL file or you have already updated the company: "
87                                                         . $id . " " . $conn['name']." - ".mysql_error());
88                                         else
89                                                 display_notification("Database has been updated for company: "
90                                                         . $id . " " .  $conn['name']);
91                                 }
92                         }
93                 }
94         }
95 }
96 if (!isset($_POST['passwd']))
97         $_POST['passwd'] = "";
98
99 echo "<form enctype='multipart/form-data' method='post' action='".$_SERVER['PHP_SELF']."'>\n";
100
101 echo "<table align='center' width='50%' cellpadding=3 border=1 bordercolor='#cccccc' style='border-collapse: collapse'>\n";
102
103 text_row_ex("Database User", "user", 20);
104 label_row("Password", "<input name='passwd' type='password' value='".$_POST['passwd']."' />");
105 label_row("Upload Script", "<input name='uploadfile' type='file'>");
106 submit_row("submit", "Update");
107
108 echo "</table>\n";;
109
110 echo "<br><br>";
111 echo "<center><span>Choose from Database update scripts in SQL folder. No Datase is updated without a script.</span></center>\n";
112 echo "<br>";
113
114 echo "</form>\n";
115
116 echo "</body></html>\n";
117 ?>