1<?php
2
3$DirectoryLevelsDeep = 1;
4$PathPrefix = '../';
5
6require($PathPrefix . 'includes/session.php');
7
8// TBD The followiung line needs to be replace when more translations are available
9$ReportLanguage = 'en_US';					// default language file
10define('DBReports','reports');			// name of the databse holding the main report information (ReportID)
11define('DBRptFields','reportfields');	// name of the database holding the report fields
12//define('FPDF_FONTPATH','../fonts/');  FPDF path to fonts directory
13define('DefRptPath',$PathPrefix . 'companies/' . $_SESSION['DatabaseName'] . '/reportwriter/');	// path to default reports
14// Fetch necessary include files - Host application specific (webERP)
15require_once($PathPrefix . 'includes/DateFunctions.inc');
16
17// Include files for ReportMaker.php
18require('languages/'.$ReportLanguage.'/reports.php'); // include translation before defaults.php
19require('admin/defaults.php'); // load default values
20
21$usrMsg = array(); // setup array for return messages
22$GoBackURL = $RootPath.'/index.php'; // set the return path to the index.php page
23
24if (isset($_GET['id'])) { // then entered with form group requested
25	$QueryString = '?'.$_SERVER['QUERY_STRING']; // save the passed parameters
26} else { // script was entered generically
27	$QueryString = ''; // no passed parameters
28}
29
30switch ($_POST['todo']) {
31	case RPT_BTN_CANCEL:
32		header("Location: ".$GoBackURL);
33		exit();
34
35	default: // determine how we entered the script to show correct form list information
36		$OutputString = BuildFormList((int) $_GET['id']); // ['id'] will be null for generic entry
37		$Title=RPT_FORMSELECT;
38		$IncludePage = 'forms/FormsList.html';
39		break;
40
41	case RPT_BTN_CRIT:
42		$ReportID = $_POST['ReportID']; // fetch the report id
43		if ($ReportID=='') { // then no report was selected, error
44			$usrMsg[] = array('message'=>FRM_NORPT, 'level'=>'error');
45			$OutputString = BuildFormList((int) $_GET['id']);
46			$Title=RPT_FORMSELECT;
47			$IncludePage = 'forms/FormsList.html';
48		} else {
49			$Prefs = FetchReportDetails($ReportID);  //fetch the defaults
50			// Update with passed parameters if so
51			// NOTE: The max number of parameters to test is currrently set at the date and 10 form specific.
52			if (isset($_GET['cr0'])) $Prefs['DateListings']['params'] = $_GET['cr0'];
53			for ($i=1; $i<11; $i++) {
54				if (isset($_GET['cr'.$i])) { // then a criteria was passed update the parameter info
55					$Prefs['CritListings'][$i-1]['params'] = $Prefs['CritListings'][$i-1]['params'].':'.$_GET['cr'.$i];
56				}
57			}
58			$Title=RPT_CRITERIA;
59			$IncludePage = 'forms/FormsFilter.html';
60		}
61		break;
62
63	case RPT_BTN_EXPPDF:
64		$ReportID = $_POST['ReportID']; // fetch the report id
65		if ($ReportID=='') { // then no report was selected, error
66			$usrMsg[] = array('message'=>FRM_NORPT, 'level'=>'error');
67			$OutputString = BuildFormList((int) $_GET['id']);
68			$Title=RPT_FORMSELECT;
69			$IncludePage = 'forms/FormsList.html';
70			break;
71		}
72		$Prefs = FetchReportDetails($ReportID);  //fetch the defaults then overwrite with user preferences
73		// read from the filter form, fetch user selections for date information
74		if (isset($_POST['DefDate'])) { // then we entered from criteria form, get user overrides
75			if ($_POST['DefDate']=='b') { // then it's a range selection, save to-from dates, else discard
76				$Prefs['DateListings']['params'] = $_POST['DefDate'].':'.$_POST['DefDateFrom'].':'.$_POST['DefDateTo'];
77			} else {
78				$Prefs['DateListings']['params'] = $_POST['DefDate'];
79			}
80			// Read in the criteria field selection, if any
81			$i=1;
82			while (isset($_POST['defcritsel'.$i])) { // then there is at least one criteria
83				// Build the criteria default string
84				$Prefs['CritListings'][$i-1]['params'] = mb_substr($Prefs['CritListings'][$i-1]['params'],0,1);
85				$Prefs['CritListings'][$i-1]['params'] .= ':'.$_POST['defcritsel'.$i];
86				$Prefs['CritListings'][$i-1]['params'] .= ':'.$_POST['fromvalue'.$i];
87				$Prefs['CritListings'][$i-1]['params'] .= ':'.$_POST['tovalue'.$i];
88				$i++;
89			}
90		} else { // then parameters may have been passed in the URL, load them if necessary
91			// NOTE: The max number of parameters to test is currrently set at the date and 10 form specific.
92			if (isset($_GET['cr0'])) $Prefs['DateListings']['params'] = $_GET['cr0'];
93			for ($i=1; $i<11; $i++) {
94				if (isset($_GET['cr'.$i])) { // then a criteria was passed update the parameter info
95					$Prefs['CritListings'][$i-1]['params'] = $Prefs['CritListings'][$i-1]['params'].':'.$_GET['cr'.$i];
96				}
97			}
98		} // else use default settings, i.e. no overrides
99		// All done with setup, build the form
100		require($PathPrefix . 'includes/tcpdf.php'); // TCPDF class to generate reports
101		require('WriteForm.inc');
102		// build the pdf pages (this function exits the script if successful; otherwise returns with error)
103		$success = BuildPDF($ReportID, $Prefs); // build and output form, should not return from this function
104		// if we are here, there's been an error, report it
105		$usrMsg[] = array('message'=>$success['message'], 'level'=>$success['level']);
106		if (isset($_POST['FormFilter'])) { // then return to the form filter page
107			$Prefs = FetchReportDetails($ReportID);  //fetch the defaults
108			// Update with passed parameters if so
109			// NOTE: The max number of parameters to test is currrently set at the date and 10 form specific.
110			if (isset($_GET['cr0'])) $Prefs['DateListings']['params'] = $_GET['cr0'];
111			for ($i=1; $i<11; $i++) {
112				if (isset($_GET['cr'.$i])) { // then a criteria was passed update the parameter info
113					$Prefs['CritListings'][$i-1]['params'] = $Prefs['CritListings'][$i-1]['params'].':'.$_GET['cr'.$i];
114				}
115			}
116			$Title=RPT_CRITERIA;
117			$IncludePage = 'forms/FormsFilter.html';
118		} else { // return to the form list page
119			$OutputString = BuildFormList((int) $_GET['id']);
120			$Title=RPT_FORMSELECT;
121			$IncludePage = 'forms/FormsList.html';
122		}
123		break;
124} // end switch 'todo'
125
126include ($PathPrefix . 'includes/header.php');
127if ($usrMsg) foreach ($usrMsg as $temp) prnmsg($temp['message'],$temp['level']);
128include ($IncludePage);
129include ( $PathPrefix . 'includes/footer.php');
130// End main body
131
132// Begin functions
133function BuildFormList($GroupID) {
134	global $ReportGroups, $FormGroups;
135
136	$OutputString = '';
137	if ($GroupID=='') { // then fetchthe complete form list for all groups
138		foreach ($ReportGroups as $key=>$GName) {
139			$OutputString .= '<tr style="background-color:#CCCCCC"><td colspan="3" align="center">'.$GName.'</td></tr>';
140			$OutputString .= '<tr><td colspan="3" width="250" valign="top">';
141			$sql= "SELECT id,
142						groupname,
143						reportname
144					FROM ".DBReports."
145					WHERE defaultreport='1'
146					AND reporttype='frm'
147					ORDER BY groupname,
148												reportname";
149			$Result=DB_query($sql,'','',false,true);
150			$FormList = array();
151			while ($Temp = DB_fetch_array($Result)) $FormList[] = $Temp;
152			foreach ($FormGroups as $index=>$value) {
153				$Group=explode(':',$index); // break into main group and form group array
154				if ($Group[0]==$key) { // then it's a part of the group we're showing
155					$WriteOnce = true;
156					foreach ($FormList as $Entry) {
157						if ($Entry['groupname']==$index) { // then it's part of this listing
158							if ($WriteOnce) { $OutputString .= $value.'<br />'; $WriteOnce=false; }
159							$OutputString .= '&nbsp;&nbsp;<input type="radio" name="ReportID" value="'.$Entry['id'].'">'.$Entry['reportname'].'<br />';
160						}
161					}
162				}
163			}
164			$OutputString .= '</td></tr>';
165		}
166	} else { // fetch the forms specific to a group GroupID
167		$sql= "SELECT id,
168					reportname
169				FROM ".DBReports."
170				WHERE defaultreport='1' AND groupname='".$GroupID."'
171				ORDER BY reportname";
172		$Result=DB_query($sql,'','',false,true);
173		$OutputString .= '<tr><td colspan="3" width="250" valign="top">';
174		while ($Forms = DB_fetch_array($Result)) {
175			$OutputString .= '<input type="radio" name="ReportID" value="'.$Forms['id'].'">'.$Forms['reportname'].'<br />';
176		}
177		$OutputString .= '</td></tr>';
178	}
179	return $OutputString;
180}
181
182function FetchReportDetails($ReportID) {
183	$sql= "SELECT reportname,
184					reporttype,
185					groupname,
186					papersize,
187					paperorientation,
188					margintop,
189					marginbottom,
190					marginleft,
191					marginright,
192					table1,
193					table2,
194					table2criteria,
195					table3,
196					table3criteria,
197					table4,
198					table4criteria,
199					table5,
200					table5criteria,
201					table6,
202					table6criteria
203			FROM " . DBReports . "
204			WHERE id = '".$ReportID."'";
205	$Result=DB_query($sql,'','',false,true);
206	$myrow=DB_fetch_assoc($Result);
207	foreach ($myrow as $key=>$value) {
208		$Prefs[$key]=$value;
209	}
210	// Build drop down menus for selectable criteria
211	$Temp = RetrieveFields($ReportID, 'dateselect');
212	$Prefs['DateListings'] = $Temp[0]; // only need the first field
213	$Prefs['GroupListings'] = RetrieveFields($ReportID, 'grouplist');
214	$Prefs['CritListings'] = RetrieveFields($ReportID, 'critlist');
215	return $Prefs;
216}
217
218function RetrieveFields($ReportID, $EntryType) {
219	$FieldListings = array();
220	$sql= "SELECT *	FROM ".DBRptFields."
221			WHERE reportid = '".$ReportID."'
222			AND entrytype = '".$EntryType."'
223			ORDER BY seqnum";
224	$Result=DB_query($sql,'','',false,true);
225	while ($FieldValues = DB_fetch_assoc($Result)) { $FieldListings[] = $FieldValues; }
226	return $FieldListings;
227}
228
229function BuildCriteria($FieldListings) {
230	global $CritChoices;
231	$SeqNum = $FieldListings['seqnum'];
232	$CriteriaString = '<tr><td>'.$FieldListings['displaydesc'].'</td>'; // add the description
233	// retrieve the dropdown based on the params field (dropdown type)
234	$Params = explode(':',$FieldListings['params']);  // the first value is the criteria type
235	$CritBlocks = explode(':',$CritChoices[array_shift($Params)]);
236	if (!isset($Params[0])) {
237		$Params[0] = '-'; // default to no default if this parameter doesn't exist
238	}
239	if (!isset($Params[1])) {
240		$Params[1] = ''; // default to no entry for default from box
241	}
242	if (!isset($Params[2])) {
243		$Params[2] = ''; // default to no entry for default to box
244	}
245	switch (array_shift($CritBlocks)) { // determine how many text boxes to build
246		default:
247		case 0: $EndString = '<td>&nbsp;</td><td>&nbsp;</td>';
248			break;
249		case 1: $EndString = '<td><input name="fromvalue'.$SeqNum.'" type="text"
250				value="'.$Params[1].'" size="21" maxlength="20"></td><td>&nbsp;</td>';
251			break;
252		case 2: $EndString = '<td><input name="fromvalue'.$SeqNum.'" type="text" value="'.$Params[1].'" size="21" maxlength="20"></td>
253				<td><input name="tovalue'.$SeqNum.'" type="text" value="'.$Params[2].'" size="21" maxlength="20"></td>';
254	} // end switch array_shift($CritBlocks)
255	$CriteriaString .= '<td><select name="defcritsel'.$SeqNum.'">';
256	foreach ($CritBlocks as $value) {
257		if ($Params[0]==$value) {
258			$Selected = ' selected';
259		} else {
260			$Selected = '';  // find the default
261		}
262		$CriteriaString .= '<option value="'.$value.'"'.$Selected.'>'.$value.'</option>';
263	}
264	$CriteriaString .= '</select></td>';
265	$CriteriaString .= $EndString.'</tr>';
266	return $CriteriaString;
267}
268
269?>