1<?php
2/*	This page can be called with...
3
4	1. A SuppTrans TransNo and Type
5	The page will then show potential allocations for the transaction called with,
6	this page can be called from the supplier enquiry to show the make up and to modify
7	existing allocations
8
9	2. A SupplierID
10	The page will show all outstanding payments or credits yet to be allocated for the supplier selected
11
12	3. No parameters
13	The page will show all outstanding supplier credit notes and payments yet to be
14	allocated
15*/
16
17include('includes/DefineSuppAllocsClass.php');
18
19include('includes/session.php');
20$Title = _('Supplier Payment') . '/' . _('Credit Note Allocations');
21$ViewTopic = 'ARTransactions';// Filename in ManualContents.php's TOC./* RChacon: To do ManualAPInquiries.html from ManualARInquiries.html */
22$BookMark = 'SupplierAllocations';
23include('includes/header.php');
24
25echo '<p class="page_title_text"><img alt="" src="', $RootPath, '/css/', $Theme,
26	'/images/transactions.png" title="', // Icon image.
27	_('Supplier Allocations'), '" /> ', // Icon title.
28	_('Supplier Allocations'), '</p>';// Page title.
29
30include('includes/SQL_CommonFunctions.inc');
31
32if (isset($_POST['UpdateDatabase']) OR isset($_POST['RefreshAllocTotal'])) {
33
34	if (!isset($_SESSION['Alloc'])){
35		prnMsg(
36			_('Allocations can not be processed again') . '. ' .
37				_('If you hit refresh on this page after having just processed an allocation') . ', ' .
38				_('try to use the navigation links provided rather than the back button, to avoid this message in future'),
39			'warn');
40		include('includes/footer.php');
41		exit;
42	}
43
44/*1st off run through and update the array with the amounts allocated
45	This works because the form has an input field called the value of
46	AllocnItm->ID for each record of the array - and PHP sets the value of
47	the form variable on a post*/
48
49	$InputError = 0;
50	$TotalAllocated = 0;
51	$TotalDiffOnExch = 0;
52
53	for ($AllocCounter=0; $AllocCounter < $_POST['TotalNumberOfAllocs']; $AllocCounter++){
54
55		$_POST['Amt' . $AllocCounter] = filter_number_format($_POST['Amt' . $AllocCounter]);
56
57		if (!is_numeric($_POST['Amt' . $AllocCounter])){
58		      $_POST['Amt' . $AllocCounter] = 0;
59		 }
60		 if ($_POST['Amt' . $AllocCounter] < 0){
61			prnMsg(_('The entry for the amount to allocate was negative') . '. ' . _('A positive allocation amount is expected'),'error');
62			$_POST['Amt' . $AllocCounter] = 0;
63		 }
64
65		if (isset($_POST['All' . $AllocCounter]) AND $_POST['All' . $AllocCounter] == True){
66			/* $_POST['YetToAlloc...] is a hidden item on the form not locale_number_formatted */
67			$_POST['Amt' . $AllocCounter] = $_POST['YetToAlloc' . $AllocCounter];
68
69		 }
70
71		  /*Now check to see that the AllocAmt is no greater than the
72		 amount left to be allocated against the transaction under review */
73
74		 if ($_POST['Amt' . $AllocCounter] > $_POST['YetToAlloc' . $AllocCounter]){
75		     $_POST['Amt' . $AllocCounter] = $_POST['YetToAlloc' . $AllocCounter];
76		 }
77
78		 $_SESSION['Alloc']->Allocs[$_POST['AllocID' . $AllocCounter]]->AllocAmt = $_POST['Amt' . $AllocCounter];
79
80		 /*recalcuate the new difference on exchange
81		 (a +positive amount is a gain -ve a loss)*/
82
83		 $_SESSION['Alloc']->Allocs[$_POST['AllocID' . $AllocCounter]]->DiffOnExch = ($_POST['Amt' . $AllocCounter] / $_SESSION['Alloc']->TransExRate) - ($_POST['Amt' . $AllocCounter] / $_SESSION['Alloc']->Allocs[$_POST['AllocID' . $AllocCounter]]->ExRate);
84
85		 $TotalDiffOnExch += $_SESSION['Alloc']->Allocs[$_POST['AllocID' . $AllocCounter]]->DiffOnExch;
86		 $TotalAllocated += round($_POST['Amt' . $AllocCounter],$_SESSION['Alloc']->CurrDecimalPlaces);
87	} /*end of the loop to set the new allocation amounts,
88	recalc diff on exchange and add up total allocations */
89
90	if ($TotalAllocated + $_SESSION['Alloc']->TransAmt > 0.005){
91		echo '<br />';
92		prnMsg(_('These allocations cannot be processed because the amount allocated is more than the amount of the') . ' ' . $_SESSION['Alloc']->TransTypeName  . ' ' . _('being allocated') . '<br />' . _('Total allocated') . ' = ' . locale_number_format($TotalAllocated,$_SESSION['Alloc']->CurrDecimalPlaces) . ' ' . _('and the total amount of the Credit/payment was') . ' ' . locale_number_format(-$_SESSION['Alloc']->TransAmt,$_SESSION['Alloc']->CurrDecimalPlaces) ,'error');
93		echo '<br />';
94		$InputError = 1;
95	}
96
97}
98
99if (isset($_POST['UpdateDatabase'])){
100
101	if ($InputError == 0){ /* ie all the traps were passed */
102
103	/* actions to take having checked that the input is sensible
104	1st set up a transaction on this thread*/
105
106		DB_Txn_Begin();
107
108		foreach ($_SESSION['Alloc']->Allocs as $AllocnItem) {
109
110			  if ($AllocnItem->OrigAlloc >0 AND ($AllocnItem->OrigAlloc != $AllocnItem->AllocAmt)){
111
112			  /*Orignial allocation was not 0 and it has now changed
113			    need to delete the old allocation record */
114
115				$SQL = "DELETE FROM suppallocs WHERE id = '" . $AllocnItem->PrevAllocRecordID . "'";
116
117				$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The existing allocation for') . ' ' . $AllocnItem->TransType .' ' . $AllocnItem->TypeNo . ' ' . _('could not be deleted because');
118				$DbgMsg = _('The following SQL to delete the allocation record was used');
119
120				$Result=DB_query($SQL, $ErrMsg, $DbgMsg, True);
121			 }
122
123			 if ($AllocnItem->OrigAlloc != $AllocnItem->AllocAmt){
124
125			 /*Only when there has been a change to the allocated amount
126			 do we need to insert a new allocation record and update
127			 the transaction with the new alloc amount and diff on exch */
128
129				     if ($AllocnItem->AllocAmt > 0){
130					     $SQL = "INSERT INTO suppallocs (datealloc,
131														amt,
132														transid_allocfrom,
133														transid_allocto)
134										VALUES ('" . FormatDateForSQL(date($_SESSION['DefaultDateFormat'])) . "',
135										     		'" . $AllocnItem->AllocAmt . "',
136												'" . $_SESSION['Alloc']->AllocTrans . "',
137												'" . $AllocnItem->ID . "')";
138
139						  $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' .  _('The supplier allocation record for') . ' ' . $AllocnItem->TransType . ' ' .  $AllocnItem->TypeNo . ' ' ._('could not be inserted because');
140						  $DbgMsg = _('The following SQL to insert the allocation record was used');
141
142					     $Result=DB_query($SQL, $ErrMsg, $DbgMsg, True);
143				     }
144				     $NewAllocTotal = $AllocnItem->PrevAlloc + $AllocnItem->AllocAmt;
145
146				     if (abs($NewAllocTotal-$AllocnItem->TransAmount) < 0.01){
147					     $Settled = 1;
148				     } else {
149					     $Settled = 0;
150				     }
151
152				     $SQL = "UPDATE supptrans SET diffonexch='" . $AllocnItem->DiffOnExch . "',
153												alloc = '" .  $NewAllocTotal . "',
154												settled = '" . $Settled . "'
155							WHERE id = '" . $AllocnItem->ID . "'";
156
157					  $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The debtor transaction record could not be modified for the allocation against it because');
158
159					  $DbgMsg = _('The following SQL to update the debtor transaction record was used');
160
161				     $Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
162
163			 } /*end if the new allocation is different to what it was before */
164
165		}  /*end of the loop through the array of allocations made */
166
167		/*Now update the payment or credit note with the amount allocated
168		and the new diff on exchange */
169
170		if (abs($TotalAllocated + $_SESSION['Alloc']->TransAmt) < 0.01){
171		   $Settled = 1;
172		} else {
173		   $Settled = 0;
174		}
175
176		$SQL = "UPDATE supptrans SET alloc = '" .  -$TotalAllocated . "',
177					diffonexch = '" . -$TotalDiffOnExch . "',
178					settled='" . $Settled . "'
179				WHERE id = '" . $_SESSION['AllocTrans'] . "'";
180
181		$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' .
182					 _('The supplier payment or credit note transaction could not be modified for the new allocation and exchange difference because');
183
184		$DbgMsg = _('The following SQL to update the payment or credit note was used');
185
186		$Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
187
188		/*Almost there ... if there is a change in the total diff on exchange
189		 and if the GLLink to debtors is active - need to post diff on exchange to GL */
190
191		$MovtInDiffOnExch = $_SESSION['Alloc']->PrevDiffOnExch + $TotalDiffOnExch;
192		if ($MovtInDiffOnExch !=0 ){
193
194		   if ($_SESSION['CompanyRecord']['gllink_debtors'] == 1){
195
196		      $PeriodNo = GetPeriod($_SESSION['Alloc']->TransDate);
197
198		      $_SESSION['Alloc']->TransDate = FormatDateForSQL($_SESSION['Alloc']->TransDate);
199
200		      $SQL = "INSERT INTO gltrans (type,
201							typeno,
202							trandate,
203							periodno,
204							account,
205							narrative,
206							amount)
207						VALUES ('" . $_SESSION['Alloc']->TransType . "',
208							'" . $_SESSION['Alloc']->TransNo . "',
209							'" . $_SESSION['Alloc']->TransDate . "',
210							'" . $PeriodNo . "',
211							'" . $_SESSION['CompanyRecord']['purchasesexchangediffact'] . "',
212							'". _('Exchange difference') . "',
213							'" . $MovtInDiffOnExch . "')";
214
215		      $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The GL entry for the difference on exchange arising out of this allocation could not be inserted because');
216		      $DbgMsg = _('The following SQL to insert the GLTrans record was used');
217
218		      $Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
219
220
221		      $SQL = "INSERT INTO gltrans (type,
222							typeno,
223							trandate,
224							periodno,
225							account,
226							narrative,
227							amount)
228						VALUES ('" . $_SESSION['Alloc']->TransType . "',
229							'" . $_SESSION['Alloc']->TransNo . "',
230							'" . $_SESSION['Alloc']->TransDate . "',
231							'" . $PeriodNo . "',
232							'" . $_SESSION['CompanyRecord']['creditorsact'] . "',
233							'" . _('Exchange difference') . "',
234							'" . -$MovtInDiffOnExch . "')";
235
236		      $ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ' : ' .
237		      			 _('The GL entry for the difference on exchange arising out of this allocation could not be inserted because');
238
239		      $DbgMsg = _('The following SQL to insert the GLTrans record was used');
240
241		      $Result = DB_query($SQL, $ErrMsg, $DbgMsg, True);
242
243		   }
244
245		}
246
247	 /* OK Commit the transaction */
248
249		DB_Txn_Commit();
250
251	/*finally delete the session variables holding all the previous data */
252
253		unset($_SESSION['AllocTrans']);
254		unset($_SESSION['Alloc']);
255		unset($_POST['AllocTrans']);
256
257	} /* end of processing required if there were no input errors trapped */
258}
259
260/*The main logic determines whether the page is called with a Supplier code
261a specific transaction or with no parameters ie else
262If with a supplier code show just that supplier's payments and credits for allocating
263If with a specific payment or credit show the invoices and credits available
264for allocating to  */
265
266echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">';
267echo '<div>';
268echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
269
270if (isset($_POST['SupplierID'])){
271 	$_GET['SupplierID'] = $_POST['SupplierID'];
272	echo '<input type="hidden" name="SupplierID" value="' . $_POST['SupplierID'] . '" />';
273}
274
275If (isset($_GET['AllocTrans'])){
276
277	/*page called with a specific transaction ID for allocating
278	SupplierID may also be set but this is the logic to follow
279	the SupplierID logic is only for showing the payments and credits to allocate*/
280
281
282	/*The logic is:
283	- read in the transaction into a session class variable
284	- read in the invoices available for allocating to into a session array of allocs object
285	- Display the supplier name the transaction being allocated amount and trans no
286	- Display the invoices for allocating to with a form entry for each one
287	for the allocated amount to be entered */
288
289
290	$_SESSION['Alloc'] = new Allocation;
291
292	/*The session varibale AllocTrans is set from the passed variable AllocTrans
293	on the first pass */
294
295	$_SESSION['AllocTrans'] = $_GET['AllocTrans'];
296	$_POST['AllocTrans'] = $_GET['AllocTrans'];
297
298
299	$SQL= "SELECT systypes.typename,
300				supptrans.type,
301				supptrans.transno,
302				supptrans.trandate,
303				supptrans.supplierno,
304				suppliers.suppname,
305				supptrans.rate,
306				(supptrans.ovamount+supptrans.ovgst) AS total,
307				supptrans.diffonexch,
308				supptrans.alloc,
309				currencies.decimalplaces
310		    FROM supptrans INNER JOIN systypes
311			ON supptrans.type = systypes.typeid
312			INNER JOIN suppliers
313			ON supptrans.supplierno = suppliers.supplierid
314			INNER JOIN currencies
315			ON suppliers.currcode=currencies.currabrev
316		    WHERE supptrans.id='" . $_SESSION['AllocTrans'] . "'";
317
318	$Result = DB_query($SQL);
319	if (DB_num_rows($Result) != 1){
320		prnMsg(_('There was a problem retrieving the information relating the transaction selected') . '. ' . _('Allocations are unable to proceed'), 'error');
321		if ($debug == 1){
322			echo '<br />' . _('The SQL that was used to retrieve the transaction information was') . ' :<br />'  . $SQL;
323		}
324		exit;
325	}
326
327	$myrow = DB_fetch_array($Result);
328
329	$_SESSION['Alloc']->AllocTrans = $_SESSION['AllocTrans'];
330	$_SESSION['Alloc']->SupplierID = $myrow['supplierno'];
331	$_SESSION['Alloc']->SuppName = $myrow['suppname'];;
332	$_SESSION['Alloc']->TransType = $myrow['type'];
333	$_SESSION['Alloc']->TransTypeName = _($myrow['typename']);
334	$_SESSION['Alloc']->TransNo = $myrow['transno'];
335	$_SESSION['Alloc']->TransExRate = $myrow['rate'];
336	$_SESSION['Alloc']->TransAmt = $myrow['total'];
337	$_SESSION['Alloc']->PrevDiffOnExch = $myrow['diffonexch'];
338	$_SESSION['Alloc']->TransDate = ConvertSQLDate($myrow['trandate']);
339	$_SESSION['Alloc']->CurrDecimalPlaces = $myrow['decimalplaces'];
340
341	/* Now populate the array of possible (and previous actual) allocations for this supplier */
342	/*First get the transactions that have outstanding balances ie Total-Alloc >0 */
343
344	$SQL= "SELECT supptrans.id,
345				typename,
346				transno,
347				trandate,
348				suppreference,
349				rate,
350				ovamount+ovgst AS total,
351				diffonexch,
352				alloc
353			FROM supptrans INNER JOIN systypes
354			ON supptrans.type = systypes.typeid
355			WHERE supptrans.settled=0
356			AND abs(ovamount+ovgst-alloc)>0.009
357			AND supplierno='" . $_SESSION['Alloc']->SupplierID . "'";
358
359	$ErrMsg = _('There was a problem retrieving the transactions available to allocate to');
360
361	$DbgMsg = _('The SQL that was used to retrieve the transaction information was');
362
363	$Result = DB_query($SQL, $ErrMsg, $DbgMsg);
364
365	while ($myrow=DB_fetch_array($Result)){
366		$_SESSION['Alloc']->add_to_AllocsAllocn ($myrow['id'],
367												_($myrow['typename']),
368												$myrow['transno'],
369												ConvertSQLDate($myrow['trandate']),
370												$myrow['suppreference'],
371												0,
372												$myrow['total'],
373												$myrow['rate'],
374												$myrow['diffonexch'],
375												$myrow['diffonexch'],
376												$myrow['alloc'],
377												'NA');
378	}
379
380	/* Now get trans that might have previously been allocated to by this trans
381	NB existing entries where still some of the trans outstanding entered from
382	above logic will be overwritten with the prev alloc detail below */
383
384	$SQL = "SELECT supptrans.id,
385					typename,
386					transno,
387					trandate,
388					suppreference,
389					rate,
390					ovamount+ovgst AS total,
391					diffonexch,
392					supptrans.alloc-suppallocs.amt AS prevallocs,
393					amt,
394					suppallocs.id AS allocid
395			FROM supptrans INNER JOIN systypes
396			ON supptrans.type = systypes.typeid
397			INNER JOIN suppallocs
398			ON supptrans.id=suppallocs.transid_allocto
399			WHERE suppallocs.transid_allocfrom='" . $_SESSION['AllocTrans'] .
400			"' AND supplierno='" . $_SESSION['Alloc']->SupplierID . "'";
401
402	$ErrMsg = _('There was a problem retrieving the previously allocated transactions for modification');
403
404	$DbgMsg = _('The SQL that was used to retrieve the previously allocated transaction information was');
405
406	$Result = DB_query($SQL, $ErrMsg, $DbgMsg);
407
408	while ($myrow = DB_fetch_array($Result)){
409
410		$DiffOnExchThisOne = ($myrow['amt']/$myrow['rate']) - ($myrow['amt']/$_SESSION['Alloc']->TransExRate);
411
412		$_SESSION['Alloc']->add_to_AllocsAllocn ($myrow['id'],
413												_($myrow['typename']),
414												$myrow['transno'],
415												ConvertSQLDate($myrow['trandate']), $myrow['suppreference'], $myrow['amt'],
416												$myrow['total'],
417												$myrow['rate'],
418												$DiffOnExchThisOne,
419												($myrow['diffonexch'] - $DiffOnExchThisOne),
420												$myrow['prevallocs'],
421												$myrow['allocid']);
422	}
423}
424
425if (isset($_POST['AllocTrans'])){
426
427	echo '<input type="hidden" name="AllocTrans" value="' . $_POST['AllocTrans'] . '" />';
428
429	/*Show the transaction being allocated and the potential trans it could be allocated to
430        and those where there is already an existing allocation */
431
432        echo '<div class="centre">
433				<font color="blue">' . _('Allocation of supplier') . ' ' .
434        		 $_SESSION['Alloc']->TransTypeName . ' ' . _('number') . ' ' .
435        		 $_SESSION['Alloc']->TransNo . ' ' . _('from') . ' ' .
436        		 $_SESSION['Alloc']->SupplierID . ' - <b>' .
437        		 $_SESSION['Alloc']->SuppName . '</b>, ' . _('dated') . ' ' .
438        		 $_SESSION['Alloc']->TransDate;
439
440        if ($_SESSION['Alloc']->TransExRate != 1){
441	     	  echo '<br />' . _('Amount in supplier currency'). ' <b>' .
442	     	  		 locale_number_format(-$_SESSION['Alloc']->TransAmt,$_SESSION['Alloc']->CurrDecimalPlaces) . '</b><i> (' .
443	     	  		 _('converted into local currency at an exchange rate of') . ' ' .
444	     	  		 $_SESSION['Alloc']->TransExRate . ')</i><p>';
445
446        } else {
447		     echo '<br />' . _('Transaction total') . ': <b>' . locale_number_format(-$_SESSION['Alloc']->TransAmt,$_SESSION['Alloc']->CurrDecimalPlaces) . '</b></div>';
448        }
449
450    /*Now display the potential and existing allocations put into the array above */
451
452		echo '<table class="selection">
453			<thead>
454				<tr>
455							<th class="ascending">' . _('Type') . '</th>
456				 			<th class="ascending">' . _('Trans') . '<br />' . _('Number') . '</th>
457							<th class="ascending">' . _('Trans')  . '<br />' . _('Date') . '</th>
458							<th class="ascending">' . _('Supp') . '<br />' . _('Ref') . '</th>
459							<th class="ascending">' . _('Total') . '<br />' . _('Amount')  . '</th>
460							<th class="ascending">' . _('Yet to') . '<br />' . _('Allocate') . '</th>
461							<th class="ascending">' . _('This') . '<br />' . _('Allocation') . '</th>
462				</tr>
463			</thead>
464			<tbody>';
465
466		$Counter = 0;
467		$TotalAllocated = 0;
468
469		foreach ($_SESSION['Alloc']->Allocs as $AllocnItem) {
470
471	    $YetToAlloc = ($AllocnItem->TransAmount - $AllocnItem->PrevAlloc);
472
473	    echo '<tr class="striped_row">
474			<td>' . $AllocnItem->TransType . '</td>
475			<td class="number">' . $AllocnItem->TypeNo . '</td>
476			<td>' . $AllocnItem->TransDate . '</td>
477			<td>' . $AllocnItem->SuppRef . '</td>
478			<td class="number">' . locale_number_format($AllocnItem->TransAmount,$_SESSION['Alloc']->CurrDecimalPlaces) . '</td>
479			<td class="number">' . locale_number_format($YetToAlloc,$_SESSION['Alloc']->CurrDecimalPlaces) . '<input type="hidden" name="YetToAlloc' . $Counter . '" value="' . $YetToAlloc . '" /></td>';
480		 if (ABS($AllocnItem->AllocAmt-$YetToAlloc) < 0.01){
481			echo '<td class="number"><input type="checkbox" name="All' .  $Counter . '" checked="checked" />';
482	    } else {
483	    	echo '<td class="number"><input type="checkbox" name="All' .  $Counter . '" />';
484	    }
485		echo '<input type="text" class="number" name="Amt' . $Counter .'" maxlength="12" size="13" value="' . locale_number_format($AllocnItem->AllocAmt,$_SESSION['Alloc']->CurrDecimalPlaces) . '" /><input type="hidden" name="AllocID' . $Counter .'" value="' . $AllocnItem->ID . '" /></td></tr>';
486
487	    $TotalAllocated = $TotalAllocated + $AllocnItem->AllocAmt;
488	    $Counter++;
489   }
490
491   echo '</tbody>
492		<tfoot>
493			<tr>
494			<td colspan="5" class="number"><b><u>' . _('Total Allocated') . ':</u></b></td>
495			<td class="number"><b><u>' .  locale_number_format($TotalAllocated,$_SESSION['Alloc']->CurrDecimalPlaces) . '</u></b></td>
496			</tr>
497			<tr>
498			<td colspan="5" class="number"><b>' . _('Left to allocate') . '</b></td>
499			<td class="number"><b>' . locale_number_format(-$_SESSION['Alloc']->TransAmt - $TotalAllocated,$_SESSION['Alloc']->CurrDecimalPlaces) . '</b></td>
500		</tr>
501		</tfoot>
502		</table>';
503
504   echo '<div class="centre">
505			<input type="hidden" name="TotalNumberOfAllocs" value="' . $Counter . '" />
506			<br />
507			<input type="submit" name="RefreshAllocTotal" value="' . _('Recalculate Total To Allocate') . '" />
508			<input type="submit" name="UpdateDatabase" value="' . _('Process Allocations') . '" />
509			<input type="submit" name="Cancel" value="' . _('Cancel') . '" />
510		</div>';
511
512} elseif(isset($_GET['SupplierID'])){
513
514  /*page called with a supplier code  so show the transactions to allocate
515  specific to the supplier selected */
516
517  echo '<input type="hidden" name="SupplierID" value="' . $_GET['SupplierID'] . '" />';
518
519  /*Clear any previous allocation records */
520
521  unset($_SESSION['Alloc']);
522
523  $sql = "SELECT id,
524		  		transno,
525				typename,
526				type,
527				suppliers.supplierid,
528				suppname,
529				trandate,
530		  		suppreference,
531				supptrans.rate,
532				ovamount+ovgst AS total,
533				alloc,
534				decimalplaces AS currdecimalplaces
535		  	FROM supptrans INNER JOIN suppliers
536		  	ON supptrans.supplierno=suppliers.supplierid
537		  	INNER JOIN systypes
538		  	ON supptrans.type=systypes.typeid
539		  	INNER JOIN currencies
540		  	ON suppliers.currcode=currencies.currabrev
541		  	WHERE suppliers.supplierid='" . $_GET['SupplierID'] ."'
542			AND (supptrans.type=21 OR supptrans.type=22)
543			AND settled=0
544			ORDER BY id";
545
546  $result = DB_query($sql);
547  if (DB_num_rows($result) == 0){
548	prnMsg(_('There are no outstanding payments or credits yet to be allocated for this supplier'),'info');
549	include('includes/footer.php');
550	exit;
551  }
552  echo '<table class="selection">';
553
554  $TableHeader = '<tr>
555					<th>' . _('Trans Type')  . '</th>
556					<th>' . _('Supplier') . '</th>
557					<th>' . _('Number') . '</th>
558					<th>' . _('Date') .  '</th>
559					<th>' . _('Total') . '</th>
560					<th>' . _('To Alloc') . '</th>
561				</tr>';
562
563  echo $TableHeader;
564
565  /* set up table of TransType - Supplier - Trans No - Date - Total - Left to alloc  */
566
567  $RowCounter = 0;
568
569  while ($myrow = DB_fetch_array($result)) {
570
571	printf('<tr class="striped_row">
572			<td>%s</td>
573			<td>%s</td>
574			<td>%s</td>
575			<td>%s</td>
576			<td class="number">%s</td>
577			<td class="number">%s</td>
578			<td><a href="%sAllocTrans=%s">' . _('Allocate')  . '</a></td>
579			</tr>',
580			_($myrow['typename']),
581			$myrow['suppname'],
582			$myrow['transno'],
583			ConvertSQLDate($myrow['trandate']),
584			locale_number_format($myrow['total'],$myrow['currdecimalplaces']),
585			locale_number_format($myrow['total']-$myrow['alloc'], $myrow['currdecimalplaces']),
586			htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
587			$myrow['id']);
588
589  }
590
591} else { /* show all outstanding payments and credits to be allocated */
592
593  /*Clear any previous allocation records */
594
595  unset($_SESSION['Alloc']->Allocs);
596  unset($_SESSION['Alloc']);
597
598  $sql = "SELECT id,
599		  		transno,
600				typename,
601				type,
602				suppliers.supplierid,
603				suppname,
604				trandate,
605		  		suppreference,
606				supptrans.rate,
607				ovamount+ovgst AS total,
608				alloc,
609				decimalplaces AS currdecimalplaces
610		  	FROM supptrans INNER JOIN suppliers
611			ON supptrans.supplierno=suppliers.supplierid
612			INNER JOIN systypes
613			ON supptrans.type=systypes.typeid
614			INNER JOIN currencies
615			ON suppliers.currcode=currencies.currabrev
616			WHERE (supptrans.type=21 OR supptrans.type=22)
617			AND settled=0
618			ORDER BY id";
619
620  $result = DB_query($sql);
621
622  echo '<table class="selection">';
623  $TableHeader = '<tr>
624					<th>' . _('Trans Type') . '</th>
625			  		<th>' . _('Supplier') . '</th>
626			  		<th>' . _('Number') . '</th>
627			  		<th>' . _('Date') . '</th>
628			  		<th>' . _('Total') . '</th>
629			  		<th>' . _('To Alloc') . '</th>
630					<th>' . _('More Info') . '</th>
631				</tr>' ;
632
633  echo $TableHeader;
634
635  /* set up table of Tran Type - Supplier - Trans No - Date - Total - Left to alloc  */
636
637  $RowCounter = 0;
638  while ($myrow = DB_fetch_array($result)) {
639
640	printf('<tr class="striped_row">
641			<td>%s</td>
642			<td>%s</td>
643			<td>%s</td>
644			<td>%s</td>
645			<td class="number">%s</td>
646			<td class="number">%s</td>
647			<td><a href="%sAllocTrans=%s">' . _('Allocate') . '</a></td>
648			</tr>',
649			_($myrow['typename']),
650			$myrow['suppname'],
651			$myrow['transno'],
652			ConvertSQLDate($myrow['trandate']),
653			locale_number_format($myrow['total'],$myrow['currdecimalplaces']),
654			locale_number_format($myrow['total']-$myrow['alloc'],$myrow['currdecimalplaces']),
655			htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
656			$myrow['id']);
657
658
659  }  //END WHILE LIST LOOP
660
661  echo '</table>';
662
663  if (DB_num_rows($result) == 0) {
664	prnMsg(_('There are no allocations to be done'),'info');
665  }
666
667} /* end of else if not a SupplierID or transaction called with the URL */
668
669echo '</div>
670      </form>';
671include('includes/footer.php');
672?>
673