1<?php
2
3
4// MRPCalendar.php
5// Maintains the calendar of valid manufacturing dates for MRP
6
7include('includes/session.php');
8$Title = _('MRP Calendar');
9$ViewTopic= 'MRP';
10$BookMark = 'MRP_Calendar';
11include('includes/header.php');
12
13
14if (isset($_POST['ChangeDate'])){
15	$ChangeDate =trim(mb_strtoupper($_POST['ChangeDate']));
16} elseif (isset($_GET['ChangeDate'])){
17	$ChangeDate =trim(mb_strtoupper($_GET['ChangeDate']));
18}
19
20echo '<p class="page_title_text">
21		<img src="'.$RootPath.'/css/'.$Theme.'/images/inventory.png" title="' .
22			_('Inventory') . '" alt="" />' . ' ' . $Title . '
23	</p>';
24
25if (isset($_POST['submit'])) {
26	submit($ChangeDate);
27} elseif (isset($_POST['update'])) {
28	update($ChangeDate);
29} elseif (isset($_POST['ListAll'])) {
30	ShowDays();
31} else {
32	ShowInputForm($ChangeDate);
33}
34
35function submit(&$ChangeDate)  //####SUBMIT_SUBMIT_SUBMIT_SUBMIT_SUBMIT_SUBMIT_SUBMIT_SUBMIT####
36{
37
38	//initialize no input errors
39	$InputError = 0;
40
41	/* actions to take once the user has clicked the submit button
42	ie the page has called itself with some user input */
43
44	//first off validate inputs sensible
45
46	if (!Is_Date($_POST['FromDate'])) {
47		$InputError = 1;
48		prnMsg(_('Invalid From Date'),'error');
49	}
50
51	if (!Is_Date($_POST['ToDate'])) {
52		$InputError = 1;
53		prnMsg(_('Invalid To Date'),'error');
54
55	}
56
57// Use FormatDateForSQL to put the entered dates into right format for sql
58// Use ConvertSQLDate to put sql formatted dates into right format for functions such as
59// DateDiff and DateAdd
60	$FormatFromDate = FormatDateForSQL($_POST['FromDate']);
61	$FormatToDate = FormatDateForSQL($_POST['ToDate']);
62	$ConvertFromDate = ConvertSQLDate($FormatFromDate);
63	$ConvertToDate = ConvertSQLDate($FormatToDate);
64
65	$DateGreater = Date1GreaterThanDate2($_POST['ToDate'],$_POST['FromDate']);
66	$DateDiff = DateDiff($ConvertToDate,$ConvertFromDate,'d'); // Date1 minus Date2
67
68	if ($DateDiff < 1) {
69		$InputError = 1;
70		prnMsg(_('To Date Must Be Greater Than From Date'),'error');
71	}
72
73	 if ($InputError == 1) {
74		ShowInputForm($ChangeDate);
75		return;
76	 }
77
78	$sql = "DROP TABLE IF EXISTS mrpcalendar";
79	$result = DB_query($sql);
80
81	$sql = "CREATE TABLE mrpcalendar (
82				calendardate date NOT NULL,
83				daynumber int(6) NOT NULL,
84				manufacturingflag smallint(6) NOT NULL default '1',
85				INDEX (daynumber),
86				PRIMARY KEY (calendardate)) DEFAULT CHARSET=utf8";
87	$ErrMsg = _('The SQL to create passbom failed with the message');
88	$result = DB_query($sql,$ErrMsg);
89
90	$i = 0;
91
92	/* $DaysTextArray used so can get text of day based on the value get from DayOfWeekFromSQLDate of
93	 the calendar date. See if that text is in the ExcludeDays array note no gettext here hard coded english days from $_POST*/
94	$DaysTextArray = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
95
96	$ExcludeDays = array($_POST['Sunday'],$_POST['Monday'],$_POST['Tuesday'],$_POST['Wednesday'],
97						 $_POST['Thursday'],$_POST['Friday'],$_POST['Saturday']);
98
99	$CalDate = $ConvertFromDate;
100	for ($i = 0; $i <= $DateDiff; $i++) {
101		 $DateAdd = FormatDateForSQL(DateAdd($CalDate,'d',$i));
102
103		 // If the check box for the calendar date's day of week was clicked, set the manufacturing flag to 0
104		 $DayOfWeek = DayOfWeekFromSQLDate($DateAdd);
105		 $ManuFlag = 1;
106		 foreach ($ExcludeDays as $exday) {
107			 if ($exday == $DaysTextArray[$DayOfWeek]) {
108				 $ManuFlag = 0;
109			 }
110		 }
111
112		 $sql = "INSERT INTO mrpcalendar (
113					calendardate,
114					daynumber,
115					manufacturingflag)
116				 VALUES ('" . $DateAdd . "',
117						'1',
118						'" . $ManuFlag . "')";
119		$result = DB_query($sql,$ErrMsg);
120	}
121
122	// Update daynumber. Set it so non-manufacturing days will have the same daynumber as a valid
123	// manufacturing day that precedes it. That way can read the table by the non-manufacturing day,
124	// subtract the leadtime from the daynumber, and find the valid manufacturing day with that daynumber.
125	$DayNumber = 1;
126	$sql = "SELECT * FROM mrpcalendar
127			ORDER BY calendardate";
128	$result = DB_query($sql,$ErrMsg);
129	while ($myrow = DB_fetch_array($result)) {
130		   if ($myrow['manufacturingflag'] == "1") {
131			   $DayNumber++;
132		   }
133		   $CalDate = $myrow['calendardate'];
134		   $sql = "UPDATE mrpcalendar SET daynumber = '" . $DayNumber . "'
135					WHERE calendardate = '" . $CalDate . "'";
136		   $resultupdate = DB_query($sql,$ErrMsg);
137	}
138	prnMsg(_('The MRP Calendar has been created'),'success');
139	ShowInputForm($ChangeDate);
140
141} // End of function submit()
142
143
144function update(&$ChangeDate)  //####UPDATE_UPDATE_UPDATE_UPDATE_UPDATE_UPDATE_UPDATE_####
145{
146// Change manufacturing flag for a date. The value "1" means the date is a manufacturing date.
147// After change the flag, re-calculate the daynumber for all dates.
148
149	$InputError = 0;
150	$CalDate = FormatDateForSQL($ChangeDate);
151	$sql="SELECT COUNT(*) FROM mrpcalendar
152		  WHERE calendardate='$CalDate'
153		  GROUP BY calendardate";
154	$result = DB_query($sql);
155	$myrow = DB_fetch_row($result);
156	if ($myrow[0] < 1  ||  !Is_Date($ChangeDate))  {
157		$InputError = 1;
158		prnMsg(_('Invalid Change Date'),'error');
159	}
160
161	 if ($InputError == 1) {
162		ShowInputForm($ChangeDate);
163		return;
164	 }
165
166	$sql="SELECT mrpcalendar.* FROM mrpcalendar WHERE calendardate='$CalDate'";
167	$result = DB_query($sql);
168	$myrow = DB_fetch_row($result);
169	$newmanufacturingflag = 0;
170	if ($myrow[2] == 0) {
171		$newmanufacturingflag = 1;
172	}
173	$sql = "UPDATE mrpcalendar SET manufacturingflag = '".$newmanufacturingflag."'
174			WHERE calendardate = '".$CalDate."'";
175	$ErrMsg = _('Cannot update the MRP Calendar');
176	$resultupdate = DB_query($sql,$ErrMsg);
177	prnMsg(_('The MRP calendar record for') . ' ' . $ChangeDate  . ' ' . _('has been updated'),'success');
178	unset ($ChangeDate);
179	ShowInputForm($ChangeDate);
180
181	// Have to update daynumber any time change a date from or to a manufacturing date
182	// Update daynumber. Set it so non-manufacturing days will have the same daynumber as a valid
183	// manufacturing day that precedes it. That way can read the table by the non-manufacturing day,
184	// subtract the leadtime from the daynumber, and find the valid manufacturing day with that daynumber.
185	$DayNumber = 1;
186	$sql = "SELECT * FROM mrpcalendar ORDER BY calendardate";
187	$result = DB_query($sql,$ErrMsg);
188	while ($myrow = DB_fetch_array($result)) {
189		   if ($myrow['manufacturingflag'] == '1') {
190			   $DayNumber++;
191		   }
192		   $CalDate = $myrow['calendardate'];
193		   $sql = "UPDATE mrpcalendar SET daynumber = '" . $DayNumber . "'
194					WHERE calendardate = '" . $CalDate . "'";
195		   $resultupdate = DB_query($sql,$ErrMsg);
196	} // End of while
197
198} // End of function update()
199
200
201function ShowDays()  {//####LISTALL_LISTALL_LISTALL_LISTALL_LISTALL_LISTALL_LISTALL_####
202
203// List all records in date range
204	$FromDate = FormatDateForSQL($_POST['FromDate']);
205	$ToDate = FormatDateForSQL($_POST['ToDate']);
206	$sql = "SELECT calendardate,
207				   daynumber,
208				   manufacturingflag,
209				   DAYNAME(calendardate) as dayname
210			FROM mrpcalendar
211			WHERE calendardate >='" . $FromDate . "'
212			AND calendardate <='" . $ToDate . "'";
213
214	$ErrMsg = _('The SQL to find the parts selected failed with the message');
215	$result = DB_query($sql,$ErrMsg);
216
217	echo '<br />
218		<table class="selection">
219		<tr>
220			<th>' . _('Date') . '</th>
221			<th>' . _('Manufacturing Date') . '</th>
222		</tr>';
223	$ctr = 0;
224	while ($myrow = DB_fetch_array($result)) {
225		$flag = _('Yes');
226		if ($myrow['manufacturingflag'] == 0) {
227			$flag = _('No');
228		}
229		printf('<tr>
230					<td>%s</td>
231					<td>%s</td>
232					<td>%s</td>
233				</tr>',
234				ConvertSQLDate($myrow[0]),
235				_($myrow[3]),
236				$flag);
237	} //END WHILE LIST LOOP
238
239	echo '</table>';
240	echo '<br /><br />';
241	unset ($ChangeDate);
242	ShowInputForm($ChangeDate);
243
244} // End of function ShowDays()
245
246
247function ShowInputForm(&$ChangeDate)  {//####DISPLAY_DISPLAY_DISPLAY_DISPLAY_DISPLAY_DISPLAY_#####
248
249// Display form fields. This function is called the first time
250// the page is called, and is also invoked at the end of all of the other functions.
251
252	if (!isset($_POST['FromDate'])) {
253		$_POST['FromDate']=date($_SESSION['DefaultDateFormat']);
254		$_POST['ToDate']=date($_SESSION['DefaultDateFormat']);
255	}
256	echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">
257          <div>
258			<br />
259			<br />';
260	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
261
262	echo '<br /><table class="selection">';
263
264	echo '<tr>
265			<td>' . _('From Date') . ':</td>
266			<td><input type="text" class="date" name="FromDate" required="required" autofocus="autofocus" size="11" maxlength="10" value="' . $_POST['FromDate'] . '" /></td></tr>
267			<tr><td>' . _('To Date') . ':</td>
268			<td><input type="text" class="date" name="ToDate" required="required" size="11" maxlength="10" value="' . $_POST['ToDate'] . '" /></td>
269		</tr>
270		<tr><td></td></tr>
271		<tr><td></td></tr>
272		<tr><td>' . _('Exclude The Following Days') . '</td></tr>
273		 <tr>
274			<td>' . _('Saturday') . ':</td>
275			<td><input type="checkbox" name="Saturday" value="Saturday" /></td>
276		</tr>
277		 <tr>
278			<td>' . _('Sunday') . ':</td>
279			<td><input type="checkbox" name="Sunday" value="Sunday" /></td>
280		</tr>
281		 <tr>
282			<td>' . _('Monday') . ':</td>
283			<td><input type="checkbox" name="Monday" value="Monday" /></td>
284		</tr>
285		 <tr>
286			<td>' . _('Tuesday') . ':</td>
287			<td><input type="checkbox" name="Tuesday" value="Tuesday" /></td>
288		</tr>
289		 <tr>
290			<td>' . _('Wednesday') . ':</td>
291			<td><input type="checkbox" name="Wednesday" value="Wednesday" /></td>
292		</tr>
293		 <tr>
294			<td>' . _('Thursday') . ':</td>
295			<td><input type="checkbox" name="Thursday" value="Thursday" /></td>
296		</tr>
297		 <tr>
298			<td>' . _('Friday') . ':</td>
299			<td><input type="checkbox" name="Friday" value="Friday" /></td>
300		</tr>
301		</table><br />
302		<div class="centre">
303			<input type="submit" name="submit" value="' . _('Create Calendar') . '" />
304			<input type="submit" name="ListAll" value="' . _('List Date Range') . '" />
305		</div>';
306
307	if (!isset($_POST['ChangeDate'])) {
308		$_POST['ChangeDate']=date($_SESSION['DefaultDateFormat']);
309	}
310
311	echo '<br />
312		<table class="selection">
313		<tr>
314			<td>' . _('Change Date Status') . ':</td>
315			<td><input type="text" name="ChangeDate" class="date" size="11" maxlength="10" value="' . $_POST['ChangeDate'] . '" /></td>
316			<td><input type="submit" name="update" value="' . _('Update') . '" /></td>
317		</tr>
318		</table>
319		<br />
320		<br />
321        </div>
322		</form>';
323
324} // End of function ShowInputForm()
325
326include('includes/footer.php');
327?>
328