1<?php
2
3//$PageSecurity = 15;
4include('includes/session.php');
5$Title = _('Upgrade webERP 3.04 - 3.05');
6include('includes/header.php');
7
8
9prnMsg(_('This script will run perform any modifications to the database required to allow the additional functionality in version 3.05 scripts'),'info');
10
11echo '<p><form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
12echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
13echo '<input type="submit" name="DoUpgrade" value="' . _('Perform Upgrade') . '" />';
14echo '</form>';
15
16if ($_POST['DoUpgrade'] == _('Perform Upgrade')){
17
18	if ($DBType=='postgres'){
19
20		$SQLScriptFile = file('./sql/pg/upgrade3.04-3.05.psql');
21
22	} elseif ($DBType ='mysql') { //its a mysql db
23
24		$SQLScriptFile = file('./sql/mysql/upgrade3.04-3.05.sql');
25	}
26
27	$ScriptFileEntries = sizeof($SQLScriptFile);
28	$ErrMsg = _('The script to upgrade the database failed because');
29	$SQL ='';
30	$InAFunction = false;
31
32	for ($i=0; $i<=$ScriptFileEntries; $i++) {
33
34		$SQLScriptFile[$i] = trim($SQLScriptFile[$i]);
35
36		if (mb_substr($SQLScriptFile[$i], 0, 2) != '--'
37			AND mb_substr($SQLScriptFile[$i], 0, 3) != 'USE'
38			AND mb_strstr($SQLScriptFile[$i],'/*')==FALSE
39			AND mb_strlen($SQLScriptFile[$i])>1){
40
41			$SQL .= ' ' . $SQLScriptFile[$i];
42
43			//check if this line kicks off a function definition - pg chokes otherwise
44			if (mb_substr($SQLScriptFile[$i],0,15) == 'CREATE FUNCTION'){
45				$InAFunction = true;
46			}
47			//check if this line completes a function definition - pg chokes otherwise
48			if (mb_substr($SQLScriptFile[$i],0,8) == 'LANGUAGE'){
49				$InAFunction = false;
50			}
51			if (mb_strpos($SQLScriptFile[$i],';')>0 AND ! $InAFunction){
52				$SQL = mb_substr($SQL,0,mb_strlen($SQL)-1);
53				$result = DB_query($SQL, $ErrMsg);
54				$SQL='';
55			}
56
57		} //end if its a valid sql line not a comment
58	} //end of for loop around the lines of the sql script
59
60
61	/*Now run the data conversions required. */
62
63	prnMsg(_('Upgrade script to put cost information against GRN records from purchorderdetails records .... please wait'),'info');
64
65	$TestAlreadyDoneResult = DB_query('SELECT * FROM grns WHERE stdcostunit<>0');
66	if (DB_num_rows($TestAlreadyDoneResult)>0){
67		prnMsg(_('The upgrade script appears to have been run already successfully - there is no need to re-run it'),'info');
68		include('includes/footer.php');
69		exit;
70	}
71
72
73	$UpdateGRNCosts = DB_query('UPDATE grns INNER JOIN purchorderdetails ON grns.podetailitem=purchorderdetails.podetailitem SET grns.stdcostunit = purchorderdetails.stdcostunit');
74
75
76	prnMsg(_('The GRN records have been updated with cost information from purchorderdetails successfully'),'success');
77} /*Dont do upgrade */
78
79include('includes/footer.php');
80?>
81