1<?php
2/*The supplier transaction uses the SuppTrans class to hold the information about the invoice
3the SuppTrans class contains an array of GRNs objects - containing details of GRNs for invoicing and also
4an array of GLCodes objects - only used if the AP - GL link is effective */
5
6include('includes/DefineSuppTransClass.php');
7/* Session started in header.php for password checking and authorisation level check */
8include('includes/session.php');
9$Title = _('Enter Supplier Invoice Against Goods Received');
10include('includes/header.php');
11
12echo '<p class="page_title_text">
13		<img src="'.$RootPath.'/css/'.$Theme.'/images/magnifier.png" title="' . _('Dispatch') .
14		'" alt="" />' . ' ' . $Title . '
15	</p>';
16
17$Complete=false;
18if (!isset($_SESSION['SuppTrans'])){
19	prnMsg(_('To enter a supplier transactions the supplier must first be selected from the supplier selection screen') . ', ' . _('then the link to enter a supplier invoice must be clicked on'),'info');
20	echo '<br />
21			<a href="' . $RootPath . '/SelectSupplier.php">' . _('Select A Supplier to Enter a Transaction For') . '</a>';
22	include('includes/footer.php');
23	exit;
24	/*It all stops here if there aint no supplier selected and invoice initiated ie $_SESSION['SuppTrans'] started off*/
25}
26
27/*If the user hit the Add to Invoice button then process this first before showing  all GRNs on the invoice
28otherwise it wouldn't show the latest additions*/
29if (isset($_POST['AddPOToTrans']) AND $_POST['AddPOToTrans']!=''){
30	foreach($_SESSION['SuppTransTmp']->GRNs as $GRNTmp) { //loop around temp GRNs array
31		if ($_POST['AddPOToTrans']==$GRNTmp->PONo) {
32			$_SESSION['SuppTrans']->Copy_GRN_To_Trans($GRNTmp); //copy from  temp GRNs array to entered GRNs array
33			$_SESSION['SuppTransTmp']->Remove_GRN_From_Trans($GRNTmp->GRNNo); //remove from temp GRNs array
34		}
35	}
36}
37
38if (isset($_POST['AddGRNToTrans'])){ /*adding a GRN to the invoice */
39	foreach($_SESSION['SuppTransTmp']->GRNs as $GRNTmp) {
40		if (isset($_POST['GRNNo_' . $GRNTmp->GRNNo])) {
41			$_POST['GRNNo_' . $GRNTmp->GRNNo] = true;
42		} else {
43			$_POST['GRNNo_' . $GRNTmp->GRNNo] = false;
44		}
45		$Selected = $_POST['GRNNo_' . $GRNTmp->GRNNo];
46		if ($Selected==True) {
47			$_SESSION['SuppTrans']->Copy_GRN_To_Trans($GRNTmp);
48			$_SESSION['SuppTransTmp']->Remove_GRN_From_Trans($GRNTmp->GRNNo);
49		}
50	}
51}
52
53if (isset($_POST['ModifyGRN'])){
54
55	for ($i=0;isset($_POST['GRNNo'.$i]);$i++) { //loop through all the possible form variables where a GRNNo is in the POST variable name
56
57		$InputError=False;
58		$Hold=False;
59		if (filter_number_format($_POST['This_QuantityInv'. $i]) >= ($_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->QtyRecd - $_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->Prev_QuantityInv )){
60			$Complete = True;
61		} else {
62			$Complete = False;
63		}
64
65		if (filter_number_format($_POST['This_QuantityInv'+$i])+$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->Prev_QuantityInv-$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->QtyRecd > 0){
66			prnMsg(_('The quantity being invoiced is more than the outstanding quantity that was delivered. It is not possible to enter an invoice for a quantity more than was received into stock'),'warn');
67			$InputError = True;
68		}
69		if (!is_numeric(filter_number_format($_POST['ChgPrice' . $i])) AND filter_number_format($_POST['ChgPrice' . $i])<0){
70			$InputError = True;
71			prnMsg(_('The price charged in the suppliers currency is either not numeric or negative') . '. ' . _('The goods received cannot be invoiced at this price'),'error');
72		} elseif ($_SESSION['Check_Price_Charged_vs_Order_Price'] == True AND $_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->OrderPrice != 0) {
73			if (filter_number_format($_POST['ChgPrice' . $i])/$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->OrderPrice > (1+ ($_SESSION['OverChargeProportion'] / 100))){
74				prnMsg(_('The price being invoiced is more than the purchase order price by more than') . ' ' . $_SESSION['OverChargeProportion'] . '%. ' .
75				_('The system is set up to prohibit this so will put this invoice on hold until it is authorised'),'warn');
76				$Hold=True;
77			}
78		}
79
80		if ($InputError==False){
81			$_SESSION['SuppTrans']->Modify_GRN_To_Trans($_POST['GRNNo'.$i],
82														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->PODetailItem,
83														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->ItemCode,
84														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->ItemDescription,
85														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->QtyRecd,
86														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->Prev_QuantityInv,
87														filter_number_format($_POST['This_QuantityInv' . $i]),
88														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->OrderPrice,
89														filter_number_format($_POST['ChgPrice' . $i]),
90														$Complete,
91														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->StdCostUnit,
92														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->ShiptRef,
93														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->JobRef,
94														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->GLCode,
95														$Hold,
96														$_SESSION['SuppTrans']->GRNs[$_POST['GRNNo'.$i]]->SupplierRef);
97		}
98	}
99}
100
101if (isset($_GET['Delete'])){
102	$_SESSION['SuppTransTmp']->Copy_GRN_To_Trans($_SESSION['SuppTrans']->GRNs[$_GET['Delete']]);
103	$_SESSION['SuppTrans']->Remove_GRN_From_Trans($_GET['Delete']);
104}
105
106
107/*Show all the selected GRNs so far from the SESSION['SuppTrans']->GRNs array */
108
109echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') .'" method="post">
110	<table class="selection">
111		<thead>
112		<tr>
113			<th colspan="10"><h3>', _('Invoiced Goods Received Selected'), '</h3></th>
114		</tr>
115		<tr>
116			<th>' . _('Sequence') . ' #</th>
117			<th>' . _("Supplier's Ref") . '</th>
118			<th>' . _('Item Code') . '</th>
119			<th>' . _('Description') . '</th>
120			<th>' . _('Quantity Yet To Inv') . '</th>
121			<th>' . _('Quantity Inv') . '</th>
122			<th>' . _('Order Price') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
123			<th>' . _('Inv Price') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
124			<th>' . _('Order Value') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
125			<th>&nbsp;</th>
126		</tr>
127		</thead>
128		<tbody>';
129
130$TotalValueCharged=0;
131
132$i=0;
133foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN){
134	if ($EnteredGRN->ChgPrice > 1) {
135		$DisplayPrice = locale_number_format($EnteredGRN->OrderPrice,$_SESSION['SuppTrans']->CurrDecimalPlaces);
136	} else {
137		$DisplayPrice = locale_number_format($EnteredGRN->OrderPrice,4);
138	}
139
140	echo '<tr>
141			<td class="number">', $EnteredGRN->GRNNo, '</td>
142			<td class="text">', $EnteredGRN->SupplierRef, '</td>
143			<td class="number">', $EnteredGRN->ItemCode, '</td>
144			<td class="text">', $EnteredGRN->ItemDescription, '</td>
145			<td class="number">', locale_number_format($EnteredGRN->QtyRecd - $EnteredGRN->Prev_QuantityInv,'Variable'), '</td>
146			<td class="number"><input class="number" maxlength="10" name="This_QuantityInv', $i, '" size="11" type="text" value="', locale_number_format($EnteredGRN->This_QuantityInv, 'Variable'), '" /></td>
147			<td class="number">', $DisplayPrice, '</td>
148			<td class="number"><input class="number" maxlength="10" name="ChgPrice', $i, '" size="11" type="text" value="', locale_number_format($EnteredGRN->ChgPrice, $_SESSION['SuppTrans']->CurrDecimalPlaces), '" /></td>
149			<td class="number">', locale_number_format($EnteredGRN->ChgPrice * $EnteredGRN->This_QuantityInv, $_SESSION['SuppTrans']->CurrDecimalPlaces), '</td>
150			<td class="text"><a href="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '?Delete=', $EnteredGRN->GRNNo, '">', _('Delete'), '</a></td>
151		</tr>
152		<input type="hidden" name="GRNNo' . $i . '" . value="' . $EnteredGRN->GRNNo . '" />';
153	$i++;
154}
155
156echo '</tbody>
157	</table>
158	<div class="centre">
159		<p>
160			<input type="submit" name="ModifyGRN" value="' . _('Update Amounts Invoiced') . '" />
161		</p>
162	</div>
163	<br />
164	<div class="centre">
165		<a href="' . $RootPath . '/SupplierInvoice.php">' . _('Back to Invoice Entry') . '</a>
166	</div>
167	<br />';
168
169
170/* Now get all the outstanding GRNs for this supplier from the database*/
171
172$SQL = "SELECT grnbatch,
173				grnno,
174				purchorderdetails.orderno,
175				purchorderdetails.unitprice,
176				grns.itemcode,
177				grns.deliverydate,
178				grns.itemdescription,
179				grns.qtyrecd,
180				grns.quantityinv,
181				grns.stdcostunit,
182				grns.supplierref,
183				purchorderdetails.glcode,
184				purchorderdetails.shiptref,
185				purchorderdetails.jobref,
186				purchorderdetails.podetailitem,
187				purchorderdetails.assetid,
188				stockmaster.decimalplaces
189		FROM grns INNER JOIN purchorderdetails
190			ON  grns.podetailitem=purchorderdetails.podetailitem
191		LEFT JOIN stockmaster ON grns.itemcode=stockmaster.stockid
192		WHERE grns.supplierid ='" . $_SESSION['SuppTrans']->SupplierID . "'
193		AND grns.qtyrecd - grns.quantityinv > 0
194		ORDER BY grns.grnno";
195$GRNResults = DB_query($SQL);
196
197if (DB_num_rows($GRNResults)==0){
198	prnMsg(_('There are no outstanding goods received from') . ' ' . $_SESSION['SuppTrans']->SupplierName . ' ' . _('that have not been invoiced by them') . '<br />' . _('The goods must first be received using the link below to select purchase orders to receive'),'warn');
199	echo '<div class="centre"><p><a href="' . $RootPath . '/PO_SelectOSPurchOrder.php?SupplierID=' . $_SESSION['SuppTrans']->SupplierID .'">' . _('Select Purchase Orders to Receive')  . '</a></p></div>';
200	include('includes/footer.php');
201	exit;
202}
203
204/*Set up a table to show the GRNs outstanding for selection */
205echo '<div>
206	<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
207
208if (!isset( $_SESSION['SuppTransTmp'])){
209	$_SESSION['SuppTransTmp'] = new SuppTrans;
210	while ($myrow=DB_fetch_array($GRNResults)){
211
212		$GRNAlreadyOnInvoice = False;
213
214		foreach ($_SESSION['SuppTrans']->GRNs as $EnteredGRN){
215			if ($EnteredGRN->GRNNo == $myrow['grnno']) {
216				$GRNAlreadyOnInvoice = True;
217			}
218		}
219		if ($myrow['decimalplaces']==''){
220			$myrow['decimalplaces']=2;
221		}
222		if ($GRNAlreadyOnInvoice == False){
223			$_SESSION['SuppTransTmp']->Add_GRN_To_Trans($myrow['grnno'],
224														$myrow['podetailitem'],
225														$myrow['itemcode'],
226														$myrow['itemdescription'],
227														$myrow['qtyrecd'],
228														$myrow['quantityinv'],
229														$myrow['qtyrecd'] - $myrow['quantityinv'],
230														$myrow['unitprice'],
231														$myrow['unitprice'],
232														$Complete,
233														$myrow['stdcostunit'],
234														$myrow['shiptref'],
235														$myrow['jobref'],
236														$myrow['glcode'],
237														$myrow['orderno'],
238														$myrow['assetid'],
239														0,
240														$myrow['decimalplaces'],
241														$myrow['grnbatch'],
242														$myrow['supplierref']);
243		}
244	}
245}
246
247if (!isset($_GET['Modify'])){
248	if (count( $_SESSION['SuppTransTmp']->GRNs)>0){   /*if there are any outstanding GRNs then */
249		echo '<table class="selection">
250				<tr>
251					<th><h3>' . _('Goods Received Yet to be Invoiced From') . ' ' . $_SESSION['SuppTrans']->SupplierName . '</h3></th>
252				</tr>
253				</table>
254				<table>
255					<thead>
256					<tr>
257						<th class="ascending">' . _('Sequence') . ' #</th>
258						<th class="ascending">' . _('GRN Number') . '</th>
259						<th class="ascending">' . _('Supplier\'s Ref') . '</th>
260						<th class="ascending">' . _('Order') . '</th>
261						<th class="ascending">' . _('Item Code') . '</th>
262						<th class="ascending">' . _('Description') . '</th>
263						<th class="ascending">' . _('Total Qty Received') . '</th>
264						<th class="ascending">' . _('Qty Already Invoiced') . '</th>
265						<th class="ascending">' . _('Qty Yet To Invoice') . '</th>
266						<th class="ascending">' . _('Order Price in') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
267						<th class="ascending">' . _('Line Value in') . ' ' . $_SESSION['SuppTrans']->CurrCode . '</th>
268						<th class="ascending">' . _('Select'), '</th>
269					</tr>
270					</thead>
271					<tbody>';
272		$i = 0;
273		$POs = array();
274		foreach($_SESSION['SuppTransTmp']->GRNs as $GRNTmp) {
275			$_SESSION['SuppTransTmp']->GRNs[$GRNTmp->GRNNo]->This_QuantityInv = $GRNTmp->QtyRecd - $GRNTmp->Prev_QuantityInv;
276			if (isset($POs[$GRNTmp->PONo]) and $POs[$GRNTmp->PONo] != $GRNTmp->PONo) {
277				$POs[$GRNTmp->PONo] = $GRNTmp->PONo;
278				echo '<tr>
279						<td><input type="submit" name="AddPOToTrans" value="' . $GRNTmp->PONo . '" /></td>
280						<td colspan="3">' . _('Add Whole PO to Invoice') . '</td>
281							</tr>';
282			}
283			echo '<tr>
284				<td class="number">', $GRNTmp->GRNNo, '</td>
285				<td class="number">', $GRNTmp->GRNBatchNo, '</td>
286				<td class="text">', $GRNTmp->SupplierRef, '</td>
287				<td class="number">', $GRNTmp->PONo, '</td>
288				<td class="number">', $GRNTmp->ItemCode, '</td>
289				<td class="text">', $GRNTmp->ItemDescription, '</td>
290				<td class="number">', locale_number_format($GRNTmp->QtyRecd, $GRNTmp->DecimalPlaces), '</td>
291				<td class="number">', locale_number_format($GRNTmp->Prev_QuantityInv, $GRNTmp->DecimalPlaces), '</td>
292				<td class="number">', locale_number_format(($GRNTmp->QtyRecd - $GRNTmp->Prev_QuantityInv), $GRNTmp->DecimalPlaces), '</td>
293				<td class="number">', locale_number_format($GRNTmp->OrderPrice, $_SESSION['SuppTrans']->CurrDecimalPlaces), '</td>
294				<td class="number">', locale_number_format($GRNTmp->OrderPrice * ($GRNTmp->QtyRecd - $GRNTmp->Prev_QuantityInv), $_SESSION['SuppTrans']->CurrDecimalPlaces), '</td>
295				<td class="centre"><input';
296			if(isset($_POST['SelectAll'])) {
297				echo ' checked';
298			}
299			echo ' name=" GRNNo_', $GRNTmp->GRNNo, '" type="checkbox" /></td>
300				</tr>';
301		}
302		echo '</tbody>
303			</table>
304			<br />
305			<div class="centre">
306				<input type="submit" name="SelectAll" value="' . _('Select All') . '" />
307				<input type="submit" name="DeSelectAll" value="' . _('Deselect All') . '" />
308				<br />
309				<input type="submit" name="AddGRNToTrans" value="' . _('Add to Invoice') . '" />
310			</div>';
311	}
312}
313
314echo '</div>
315	</form>';
316include('includes/footer.php');
317?>
318