1<?php
2
3
4include('includes/session.php');
5include('includes/CurrenciesArray.php'); // To get the currency name from the currency code.
6
7if (isset($_POST['Edit']) or isset($_GET['Edit']) or isset($_GET['DebtorNo'])) {
8	$ViewTopic = 'AccountsReceivable';
9	$BookMark = 'AmendCustomer';
10} else {
11	$ViewTopic = 'AccountsReceivable';
12	$BookMark = 'NewCustomer';
13}
14
15$Title = _('Customer Maintenance');
16/* webERP manual links before header.php */
17$ViewTopic= 'AccountsReceivable';
18$BookMark = 'NewCustomer';
19include('includes/header.php');
20include('includes/SQL_CommonFunctions.inc');
21include('includes/CountriesArray.php');
22
23echo '<p class="page_title_text">
24		<img src="'.$RootPath.'/css/'.$Theme.'/images/customer.png" title="' . _('Customer') .
25	'" alt="" />' . ' ' . _('Customer Maintenance') . '
26	</p>';
27
28if (isset($Errors)) {
29	unset($Errors);
30}
31$Errors = array();
32
33if (isset($_POST['submit'])) {
34
35	//initialise no input errors assumed initially before we test
36	$InputError = 0;
37	$i=1;
38
39	/* actions to take once the user has clicked the submit button
40	ie the page has called itself with some user input */
41
42	//first off validate inputs sensible
43
44	$_POST['DebtorNo'] = mb_strtoupper($_POST['DebtorNo']);
45
46	$sql="SELECT COUNT(debtorno) FROM debtorsmaster WHERE debtorno='".$_POST['DebtorNo']."'";
47	$result=DB_query($sql);
48	$myrow=DB_fetch_row($result);
49	if ($myrow[0]>0 AND isset($_POST['New'])) {
50		$InputError = 1;
51		prnMsg( _('The customer number already exists in the database'),'error');
52		$Errors[$i] = 'DebtorNo';
53		$i++;
54	}elseif (mb_strlen($_POST['CustName']) > 40 OR mb_strlen($_POST['CustName'])==0) {
55		$InputError = 1;
56		prnMsg( _('The customer name must be entered and be forty characters or less long'),'error');
57		$Errors[$i] = 'CustName';
58		$i++;
59	} elseif ($_SESSION['AutoDebtorNo']==0 AND mb_strlen($_POST['DebtorNo']) ==0) {
60		$InputError = 1;
61		prnMsg( _('The debtor code cannot be empty'),'error');
62		$Errors[$i] = 'DebtorNo';
63		$i++;
64	} elseif ($_SESSION['AutoDebtorNo']==0 AND (ContainsIllegalCharacters($_POST['DebtorNo']) OR mb_strpos($_POST['DebtorNo'], ' '))) {
65		$InputError = 1;
66		prnMsg( _('The customer code cannot contain any of the following characters') . " . - ' &amp; + \" " . _('or a space'),'error');
67		$Errors[$i] = 'DebtorNo';
68		$i++;
69	} elseif (mb_strlen($_POST['Address1']) >40) {
70		$InputError = 1;
71		prnMsg( _('The Line 1 of the address must be forty characters or less long'),'error');
72		$Errors[$i] = 'Address1';
73		$i++;
74	} elseif (mb_strlen($_POST['Address2']) >40) {
75		$InputError = 1;
76		prnMsg( _('The Line 2 of the address must be forty characters or less long'),'error');
77		$Errors[$i] = 'Address2';
78		$i++;
79	} elseif (mb_strlen($_POST['Address3']) >40) {
80		$InputError = 1;
81		prnMsg( _('The Line 3 of the address must be forty characters or less long'),'error');
82		$Errors[$i] = 'Address3';
83		$i++;
84	} elseif (mb_strlen($_POST['Address4']) >50) {
85		$InputError = 1;
86		prnMsg( _('The Line 4 of the address must be fifty characters or less long'),'error');
87		$Errors[$i] = 'Address4';
88		$i++;
89	} elseif (mb_strlen($_POST['Address5']) >20) {
90		$InputError = 1;
91		prnMsg( _('The Line 5 of the address must be twenty characters or less long'),'error');
92		$Errors[$i] = 'Address5';
93		$i++;
94	} elseif (!is_numeric(filter_number_format($_POST['CreditLimit']))) {
95		$InputError = 1;
96		prnMsg( _('The credit limit must be numeric'),'error');
97		$Errors[$i] = 'CreditLimit';
98		$i++;
99	} elseif (!is_numeric(filter_number_format($_POST['PymtDiscount']))) {
100		$InputError = 1;
101		prnMsg( _('The payment discount must be numeric'),'error');
102		$Errors[$i] = 'PymtDiscount';
103		$i++;
104	} elseif (!Is_Date($_POST['ClientSince'])) {
105		$InputError = 1;
106		prnMsg( _('The customer since field must be a date in the format') . ' ' . $_SESSION['DefaultDateFormat'],'error');
107		$Errors[$i] = 'ClientSince';
108		$i++;
109	} elseif (!is_numeric(filter_number_format($_POST['Discount']))) {
110		$InputError = 1;
111		prnMsg( _('The discount percentage must be numeric'),'error');
112		$Errors[$i] = 'Discount';
113		$i++;
114	} elseif (filter_number_format($_POST['CreditLimit']) <0) {
115		$InputError = 1;
116		prnMsg( _('The credit limit must be a positive number'),'error');
117		$Errors[$i] = 'CreditLimit';
118		$i++;
119	} elseif ((filter_number_format($_POST['PymtDiscount'])> 10) OR (filter_number_format($_POST['PymtDiscount']) <0)) {
120		$InputError = 1;
121		prnMsg( _('The payment discount is expected to be less than 10% and greater than or equal to 0'),'error');
122		$Errors[$i] = 'PymtDiscount';
123		$i++;
124	} elseif ((filter_number_format($_POST['Discount'])> 100) OR (filter_number_format($_POST['Discount']) <0)) {
125		$InputError = 1;
126		prnMsg( _('The discount is expected to be less than 100% and greater than or equal to 0'),'error');
127		$Errors[$i] = 'Discount';
128		$i++;
129	}
130
131	if ($InputError !=1){
132
133		$SQL_ClientSince = FormatDateForSQL($_POST['ClientSince']);
134
135		if (!isset($_POST['New'])) {
136
137			$sql = "SELECT count(id)
138					  FROM debtortrans
139					where debtorno = '" . $_POST['DebtorNo'] . "'";
140			$result = DB_query($sql);
141			$myrow = DB_fetch_array($result);
142
143			if ($myrow[0] == 0) {
144			  $sql = "UPDATE debtorsmaster SET	name='" . $_POST['CustName'] . "',
145												address1='" . $_POST['Address1'] . "',
146												address2='" . $_POST['Address2'] . "',
147												address3='" . $_POST['Address3'] ."',
148												address4='" . $_POST['Address4'] . "',
149												address5='" . $_POST['Address5'] . "',
150												address6='" . $_POST['Address6'] . "',
151												currcode='" . $_POST['CurrCode'] . "',
152												clientsince='" . $SQL_ClientSince. "',
153												holdreason='" . $_POST['HoldReason'] . "',
154												paymentterms='" . $_POST['PaymentTerms'] . "',
155												discount='" . filter_number_format($_POST['Discount'])/100 . "',
156												discountcode='" . $_POST['DiscountCode'] . "',
157												pymtdiscount='" . filter_number_format($_POST['PymtDiscount'])/100 . "',
158												creditlimit='" . filter_number_format($_POST['CreditLimit']) . "',
159												salestype = '" . $_POST['SalesType'] . "',
160												invaddrbranch='" . $_POST['AddrInvBranch'] . "',
161												taxref='" . $_POST['TaxRef'] . "',
162												customerpoline='" . $_POST['CustomerPOLine'] . "',
163												typeid='" . $_POST['typeid'] . "',
164												language_id='" . $_POST['LanguageID'] . "'
165					  WHERE debtorno = '" . $_POST['DebtorNo'] . "'";
166			} else {
167
168			  $CurrSQL = "SELECT currcode
169					  		FROM debtorsmaster
170							where debtorno = '" . $_POST['DebtorNo'] . "'";
171			  $CurrResult = DB_query($CurrSQL);
172			  $CurrRow = DB_fetch_array($CurrResult);
173			  $OldCurrency = $CurrRow[0];
174
175			  $sql = "UPDATE debtorsmaster SET	name='" . $_POST['CustName'] . "',
176												address1='" . $_POST['Address1'] . "',
177												address2='" . $_POST['Address2'] . "',
178												address3='" . $_POST['Address3'] ."',
179												address4='" . $_POST['Address4'] . "',
180												address5='" . $_POST['Address5'] . "',
181												address6='" . $_POST['Address6'] . "',
182												clientsince='" . $SQL_ClientSince . "',
183												holdreason='" . $_POST['HoldReason'] . "',
184												paymentterms='" . $_POST['PaymentTerms'] . "',
185												discount='" . filter_number_format($_POST['Discount'])/100 . "',
186												discountcode='" . $_POST['DiscountCode'] . "',
187												pymtdiscount='" . filter_number_format($_POST['PymtDiscount'])/100 . "',
188												creditlimit='" . filter_number_format($_POST['CreditLimit']) . "',
189												salestype = '" . $_POST['SalesType'] . "',
190												invaddrbranch='" . $_POST['AddrInvBranch'] . "',
191												taxref='" . $_POST['TaxRef'] . "',
192												customerpoline='" . $_POST['CustomerPOLine'] . "',
193												typeid='" . $_POST['typeid'] . "',
194												language_id='" . $_POST['LanguageID'] . "'
195						WHERE debtorno = '" . $_POST['DebtorNo'] . "'";
196
197			  if ($OldCurrency != $_POST['CurrCode']) {
198			  	prnMsg( _('The currency code cannot be updated as there are already transactions for this customer'),'info');
199			  }
200			}
201
202			$ErrMsg = _('The customer could not be updated because');
203			$result = DB_query($sql,$ErrMsg);
204			prnMsg( _('Customer updated'),'success');
205			echo '<br />';
206
207		} else { //it is a new customer
208			/* set the DebtorNo if $AutoDebtorNo in config.php has been set to
209			something greater 0 */
210			if ($_SESSION['AutoDebtorNo'] > 0) {
211				/* system assigned, sequential, numeric */
212				if ($_SESSION['AutoDebtorNo']== 1) {
213					$_POST['DebtorNo'] = GetNextTransNo(500);
214				}
215			}
216
217			$sql = "INSERT INTO debtorsmaster (
218							debtorno,
219							name,
220							address1,
221							address2,
222							address3,
223							address4,
224							address5,
225							address6,
226							currcode,
227							clientsince,
228							holdreason,
229							paymentterms,
230							discount,
231							discountcode,
232							pymtdiscount,
233							creditlimit,
234							salestype,
235							invaddrbranch,
236							taxref,
237							customerpoline,
238							typeid,
239							language_id)
240				VALUES ('" . $_POST['DebtorNo'] ."',
241						'" . $_POST['CustName'] ."',
242						'" . $_POST['Address1'] ."',
243						'" . $_POST['Address2'] ."',
244						'" . $_POST['Address3'] . "',
245						'" . $_POST['Address4'] . "',
246						'" . $_POST['Address5'] . "',
247						'" . $_POST['Address6'] . "',
248						'" . $_POST['CurrCode'] . "',
249						'" . $SQL_ClientSince . "',
250						'" . $_POST['HoldReason'] . "',
251						'" . $_POST['PaymentTerms'] . "',
252						'" . filter_number_format($_POST['Discount'])/100 . "',
253						'" . $_POST['DiscountCode'] . "',
254						'" . filter_number_format($_POST['PymtDiscount'])/100 . "',
255						'" . filter_number_format($_POST['CreditLimit']) . "',
256						'" . $_POST['SalesType'] . "',
257						'" . $_POST['AddrInvBranch'] . "',
258						'" . $_POST['TaxRef'] . "',
259						'" . $_POST['CustomerPOLine'] . "',
260						'" . $_POST['typeid'] . "',
261						'" . $_POST['LanguageID'] . "')";
262
263			$ErrMsg = _('This customer could not be added because');
264			$result = DB_query($sql,$ErrMsg);
265
266			$BranchCode = mb_substr($_POST['DebtorNo'],0,4);
267
268			echo '<meta http-equiv="Refresh" content="0; url=' . $RootPath .'/CustomerBranches.php?DebtorNo=' . $_POST['DebtorNo'] . '">';
269
270			echo '<div class="centre">' . _('You should automatically be forwarded to the entry of a new Customer Branch page') .
271			'. ' . _('If this does not happen') .' (' . _('if the browser does not support META Refresh') . ') ' .
272			'<a href="' . $RootPath . '/CustomerBranches.php?DebtorNo=' . $_POST['DebtorNo']  . '"></a></div>';
273
274			include('includes/footer.php');
275			exit;
276		}
277	} else {
278		prnMsg( _('Validation failed') . '. ' . _('No updates or deletes took place'),'error');
279	}
280
281} elseif (isset($_POST['delete'])) {
282
283//the link to delete a selected record was clicked instead of the submit button
284
285	$CancelDelete = 0;
286
287// PREVENT DELETES IF DEPENDENT RECORDS IN 'DebtorTrans'
288
289	$sql= "SELECT COUNT(*) FROM debtortrans WHERE debtorno='" . $_POST['DebtorNo'] . "'";
290	$result = DB_query($sql);
291	$myrow = DB_fetch_row($result);
292	if ($myrow[0]>0) {
293		$CancelDelete = 1;
294		prnMsg( _('This customer cannot be deleted because there are transactions that refer to it'),'warn');
295		echo '<br /> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('transactions against this customer');
296
297	} else {
298		$sql= "SELECT COUNT(*) FROM salesorders WHERE debtorno='" . $_POST['DebtorNo'] . "'";
299		$result = DB_query($sql);
300		$myrow = DB_fetch_row($result);
301		if ($myrow[0]>0) {
302			$CancelDelete = 1;
303			prnMsg( _('Cannot delete the customer record because orders have been created against it'),'warn');
304			echo '<br /> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('orders against this customer');
305		} else {
306			$sql= "SELECT COUNT(*) FROM salesanalysis WHERE cust='" . $_POST['DebtorNo'] . "'";
307			$result = DB_query($sql);
308			$myrow = DB_fetch_row($result);
309			if ($myrow[0]>0) {
310				$CancelDelete = 1;
311				prnMsg( _('Cannot delete this customer record because sales analysis records exist for it'),'warn');
312				echo '<br /> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('sales analysis records against this customer');
313			} else {
314
315				// Check if there are any users that refer to this CUSTOMER code
316				$SQL= "SELECT COUNT(*) FROM www_users WHERE www_users.customerid = '" . $_POST['DebtorNo'] . "'";
317
318				$result = DB_query($SQL);
319				$myrow = DB_fetch_row($result);
320
321				if ($myrow[0]>0) {
322					prnMsg(_('Cannot delete this customer because users exist that refer to it') . '. ' . _('Purge old users first'),'warn');
323					echo '<br />' . _('There are') . ' ' . $myrow[0] . ' '._('users referring to this Branch/customer');
324				} else {
325						// Check if there are any contract that refer to this branch code
326					$SQL = "SELECT COUNT(*) FROM contracts WHERE contracts.debtorno = '" . $_POST['DebtorNo'] . "'";
327
328					$result = DB_query($SQL);
329					$myrow = DB_fetch_row($result);
330
331					if ($myrow[0]>0) {
332						prnMsg(_('Cannot delete this customer because contracts have been created that refer to it') . '. ' . _('Purge old contracts first'),'warn');
333						echo '<br />' . _('There are') . ' ' . $myrow[0] . ' '._('contracts referring to this customer');
334					}
335				}
336			}
337		}
338
339	}
340	if ($CancelDelete==0) { //ie not cancelled the delete as a result of above tests
341		$SQL="DELETE FROM custbranch WHERE debtorno='" . $_POST['DebtorNo'] . "'";
342		$result = DB_query($SQL,$ErrMsg);
343		$sql="DELETE FROM custcontacts WHERE debtorno='" . $_POST['DebtorNo'] . "'";
344		$result = DB_query($sql);
345		$sql="DELETE FROM debtorsmaster WHERE debtorno='" . $_POST['DebtorNo'] . "'";
346		$result = DB_query($sql);
347		prnMsg( _('Customer') . ' ' . $_POST['DebtorNo'] . ' ' . _('has been deleted - together with all the associated branches and contacts'),'success');
348		include('includes/footer.php');
349		unset($_SESSION['CustomerID']);
350		exit;
351	} //end if Delete Customer
352}
353
354if(isset($_POST['Reset'])){
355	unset($_POST['CustName']);
356	unset($_POST['Address1']);
357	unset($_POST['Address2']);
358	unset($_POST['Address3']);
359	unset($_POST['Address4']);
360	unset($_POST['Address5']);
361	unset($_POST['Address6']);
362	unset($_POST['HoldReason']);
363	unset($_POST['PaymentTerms']);
364	unset($_POST['Discount']);
365	unset($_POST['DiscountCode']);
366	unset($_POST['PymtDiscount']);
367	unset($_POST['CreditLimit']);
368// Leave Sales Type set so as to faciltate fast customer setup
369//	unset($_POST['SalesType']);
370	unset($_POST['DebtorNo']);
371	unset($_POST['InvAddrBranch']);
372	unset($_POST['TaxRef']);
373	unset($_POST['CustomerPOLine']);
374	unset($_POST['LanguageID']);
375// Leave Type ID set so as to faciltate fast customer setup
376//	unset($_POST['typeid']);
377}
378
379/*DebtorNo could be set from a post or a get when passed as a parameter to this page */
380
381if (isset($_POST['DebtorNo'])){
382	$DebtorNo = $_POST['DebtorNo'];
383} elseif (isset($_GET['DebtorNo'])){
384	$DebtorNo = $_GET['DebtorNo'];
385}
386if (isset($_POST['ID'])){
387	$ID = $_POST['ID'];
388} elseif (isset($_GET['ID'])){
389	$ID = $_GET['ID'];
390} else {
391	$ID='';
392}
393if (isset($_POST['Edit'])){
394	$Edit = $_POST['Edit'];
395} elseif (isset($_GET['Edit'])){
396	$Edit = $_GET['Edit'];
397} else {
398	$Edit='';
399}
400
401if (isset($_POST['Add'])){
402	$Add = $_POST['Add'];
403} elseif (isset($_GET['Add'])){
404	$Add = $_GET['Add'];
405}
406
407if(isset($_POST['AddContact']) AND (isset($_POST['AddContact'])!='')){
408	echo '<meta http-equiv="Refresh" content="0; url=' . $RootPath . '/AddCustomerContacts.php?DebtorNo=' .$DebtorNo.'">';
409}
410
411if (!isset($DebtorNo)) {
412
413/*If the page was called without $_POST['DebtorNo'] passed to page then assume a new customer is to be entered show a form with a Debtor Code field other wise the form showing the fields with the existing entries against the customer will show for editing with only a hidden DebtorNo field*/
414
415/* First check that all the necessary items have been setup */
416
417	$SetupErrors=0; //Count errors
418	$sql="SELECT COUNT(typeabbrev)
419				FROM salestypes";
420	$result=DB_query($sql);
421	$myrow=DB_fetch_row($result);
422	if ($myrow[0]==0) {
423		prnMsg( _('In order to create a new customer you must first set up at least one sales type/price list') . '<br />' .
424			_('Click').' ' . '<a target="_blank" href="' . $RootPath . '/SalesTypes.php">' . _('here').' ' . '</a>' . _('to set up your price lists'),'warning') . '<br />';
425		$SetupErrors += 1;
426	}
427	$sql="SELECT COUNT(typeid)
428				FROM debtortype";
429	$result=DB_query($sql);
430	$myrow=DB_fetch_row($result);
431	if ($myrow[0]==0) {
432		prnMsg( _('In order to create a new customer you must first set up at least one customer type') . '<br />' .
433			_('Click').' ' . '<a target="_blank" href="' . $RootPath . '/CustomerTypes.php">' . _('here').' ' . '</a>' . _('to set up your customer types'),'warning');
434		$SetupErrors += 1;
435	}
436
437	if ($SetupErrors>0) {
438		echo '<br /><div class="centre"><a href="'.htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') .'" >' . _('Click here to continue') . '</a></div>';
439		include('includes/footer.php');
440		exit;
441	}
442	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">
443		<div>
444		<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
445		<input type="hidden" name="New" value="Yes" />';
446
447	$DataError =0;
448
449	echo '<table class="selection" cellspacing="4">
450			<tr>
451				<td valign="top">
452					<table class="selection">';
453
454	/* if $AutoDebtorNo in config.php has not been set or if it has been set to a number less than one,
455	then provide an input box for the DebtorNo to manually assigned */
456	if ($_SESSION['AutoDebtorNo']==0)  {
457		echo '<tr>
458				<td>' . _('Customer Code') . ':</td>
459				<td><input type="text" data-type="no-illegal-chars" tabindex="1"  name="DebtorNo"  required="required" autofocus="autofocus" title ="'._('Up to 10 characters for the customer code. The following characters are prohibited:') . ' \' &quot; + . &amp; \\ &gt; &lt;" placeholder="'._('alpha-numeric').'" size="11" maxlength="10" /></td></tr>';
460	}
461
462	echo '<tr>
463			<td>' . _('Customer Name') . ':</td>
464			<td><input tabindex="2" type="text" name="CustName" required="required" size="42" maxlength="40" /></td>
465		</tr>
466		<tr>
467			<td>' . _('Address Line 1 (Street)') . ':</td>
468			<td><input tabindex="3" type="text" name="Address1" required="required" size="42" maxlength="40" /></td>
469		</tr>
470		<tr>
471			<td>' . _('Address Line 2 (Street)') . ':</td>
472			<td><input tabindex="4" type="text" name="Address2" size="42" maxlength="40" /></td>
473		</tr>
474		<tr>
475			<td>' . _('Address Line 3 (Suburb/City)') . ':</td>
476			<td><input tabindex="5" type="text" name="Address3" size="42" maxlength="40" /></td>
477		</tr>
478		<tr>
479			<td>' . _('Address Line 4 (State/Province)') . ':</td>
480			<td><input tabindex="6" type="text" name="Address4" size="42" maxlength="40" /></td>
481		</tr>
482		<tr>
483			<td>' . _('Address Line 5 (Postal Code)') . ':</td>
484			<td><input tabindex="7" type="text" name="Address5" size="22" maxlength="20" /></td>
485		</tr>';
486
487	if (!isset($_POST['Address6'])) {
488		 $_POST['Address6'] = $CountriesArray[$_SESSION['CountryOfOperation']];
489	}
490	echo '<tr>
491			<td>' . _('Country') . ':</td>
492			<td><select name="Address6">';
493	foreach ($CountriesArray as $CountryEntry => $CountryName){
494		if (isset($_POST['Address6']) AND (strtoupper($_POST['Address6']) == strtoupper($CountryName))){
495			echo '<option selected="selected" value="' . $CountryName . '">' . $CountryName  . '</option>';
496		} else {
497			echo '<option value="' . $CountryName . '">' . $CountryName  . '</option>';
498		}
499	}
500	echo '</select></td>
501		</tr>';
502
503// Show Sales Type drop down list
504	$result=DB_query("SELECT typeabbrev, sales_type FROM salestypes ORDER BY sales_type");
505	if (DB_num_rows($result)==0){
506		$DataError =1;
507		echo '<tr>
508				<td colspan="2">' . prnMsg(_('No sales types/price lists defined'),'error') . '<br /><a href="SalesTypes.php?" target="_parent">' . _('Setup Types') . '</a></td>
509			</tr>';
510	} else {
511        echo '<tr>
512				<td>' . _('Sales Type') . '/' . _('Price List') . ':</td>
513			   <td><select tabindex="9" name="SalesType" required="required">';
514
515		while ($myrow = DB_fetch_array($result)) {
516		   echo '<option value="'. $myrow['typeabbrev'] . '">' . $myrow['sales_type'] . '</option>';
517		} //end while loopre
518		DB_data_seek($result,0);
519        echo '</select></td>
520			</tr>';
521	}
522
523// Show Customer Type drop down list
524	$result=DB_query("SELECT typeid, typename FROM debtortype ORDER BY typename");
525	if (DB_num_rows($result)==0){
526	   $DataError =1;
527	   echo '<a href="SalesTypes.php?" target="_parent">' . _('Setup Types') . '</a>';
528	   echo '<tr>
529				<td colspan="2">' . prnMsg(_('No Customer types/price lists defined'),'error') . '</td>
530			</tr>';
531	} else {
532		echo '<tr>
533				<td>' . _('Customer Type') . ':</td>
534				<td><select tabindex="9" name="typeid" required="required">';
535
536		while ($myrow = DB_fetch_array($result)) {
537			echo '<option value="'. $myrow['typeid'] . '">' . $myrow['typename'] . '</option>';
538		} //end while loop
539		DB_data_seek($result,0);
540		echo '</select></td>
541			</tr>';
542	}
543
544	$DateString = Date($_SESSION['DefaultDateFormat']);
545	echo '<tr>
546			<td>' . _('Customer Since') . ' (' . $_SESSION['DefaultDateFormat'] . '):</td>
547			<td><input tabindex="10" type="text" class="date" name="ClientSince" value="' . $DateString . '" size="11" maxlength="10" /></td>
548		</tr>';
549
550	echo '</table></td>
551			<td><table class="selection">
552				<tr>
553					<td>' . _('Discount Percent') . ':</td>
554					<td><input tabindex="11" type="text" class="number" name="Discount" value="0" size="5" maxlength="4" /></td>
555				</tr>
556				<tr>
557					<td>' . _('Discount Code') . ':</td>
558					<td><input tabindex="12" type="text" name="DiscountCode" size="3" maxlength="2" /></td>
559				</tr>
560				<tr>
561					<td>' . _('Payment Discount Percent') . ':</td>
562					<td><input tabindex="13" type="text" class ="number" name="PymtDiscount" value="0" size="5" maxlength="4" /></td>
563				</tr>
564				<tr>
565					<td>' . _('Credit Limit') . ':</td>
566					<td><input tabindex="14" type="text" class="integer" name="CreditLimit" required="required" value="' . locale_number_format($_SESSION['DefaultCreditLimit'],0) . '" size="16" maxlength="14" /></td>
567				</tr>
568				<tr>
569					<td>' . _('Tax Reference') . ':</td>
570					<td><input tabindex="15" type="text" name="TaxRef" size="22" maxlength="20" /></td>
571				</tr>';
572
573	$result=DB_query("SELECT terms, termsindicator FROM paymentterms");
574	if (DB_num_rows($result)==0){
575		$DataError =1;
576		echo '<tr><td colspan="2">' . prnMsg(_('There are no payment terms currently defined - go to the setup tab of the main menu and set at least one up first'),'error') . '</td></tr>';
577	} else {
578
579		echo '<tr>
580				<td>' . _('Payment Terms') . ':</td>
581				<td><select tabindex="15" name="PaymentTerms" required="required">';
582
583		while ($myrow = DB_fetch_array($result)) {
584			echo '<option value="'. $myrow['termsindicator'] . '">' . $myrow['terms'] . '</option>';
585		} //end while loop
586		DB_data_seek($result,0);
587
588		echo '</select></td></tr>';
589	}
590	echo '<tr>
591			<td>' . _('Credit Status') . ':</td>
592			<td><select tabindex="16" name="HoldReason" required="required">';
593
594	$result=DB_query("SELECT reasoncode, reasondescription FROM holdreasons");
595	if (DB_num_rows($result)==0){
596		$DataError =1;
597		echo '<tr>
598				<td colspan="2">' . prnMsg(_('There are no credit statuses currently defined - go to the setup tab of the main menu and set at least one up first'),'error') . '</td>
599			</tr>';
600	} else {
601		while ($myrow = DB_fetch_array($result)) {
602			echo '<option value="'. $myrow['reasoncode'] . '">' . $myrow['reasondescription'] . '</option>';
603		} //end while loop
604		DB_data_seek($result,0);
605		echo '</select></td></tr>';
606	}
607
608	$result=DB_query("SELECT currency, currabrev FROM currencies");
609	if (DB_num_rows($result)==0){
610		$DataError =1;
611		echo '<tr>
612				<td colspan="2">' . prnMsg(_('There are no currencies currently defined - go to the setup tab of the main menu and set at least one up first'),'error') . '</td>
613			</tr>';
614	} else {
615		if (!isset($_POST['CurrCode'])){
616			$CurrResult = DB_query("SELECT currencydefault FROM companies WHERE coycode=1");
617			$myrow = DB_fetch_row($CurrResult);
618			$_POST['CurrCode'] = $myrow[0];
619		}
620		echo '<tr>
621				<td>' . _('Customer Currency') . ':</td>
622				<td><select tabindex="17" name="CurrCode" required="required">';
623		while ($myrow = DB_fetch_array($result)) {
624			if ($_POST['CurrCode']==$myrow['currabrev']){
625				echo '<option selected="selected" value="'. $myrow['currabrev'] . '">' . $myrow['currency'] . '</option>';
626			} else {
627				echo '<option value="'. $myrow['currabrev'] . '">' . $myrow['currency'] . '</option>';
628			}
629		} //end while loop
630		DB_data_seek($result,0);
631
632		echo '</select></td>
633			</tr>';
634	}
635
636	echo '<tr>
637			<td>' . _('Language') . ':</td>
638			<td><select name="LanguageID" required="required">';
639
640	if (!isset($_POST['LanguageID']) OR $_POST['LanguageID']==''){
641		$_POST['LanguageID']=$_SESSION['Language'];
642	}
643
644	foreach ($LanguagesArray as $LanguageCode => $LanguageName){
645		if ($_POST['LanguageID'] == $LanguageCode){
646			echo '<option selected="selected" value="' . $LanguageCode . '">' . $LanguageName['LanguageName']  . '</option>';
647		} else {
648			echo '<option value="' . $LanguageCode . '">' . $LanguageName['LanguageName']  . '</option>';
649		}
650	}
651	echo '</select></td>
652			</tr>';
653
654	echo '<tr>
655			<td>' . _('Customer PO Line on SO') . ':</td>
656			<td><select tabindex="18" name="CustomerPOLine" required="required">
657				<option selected="selected" value="0">' . _('No') . '</option>
658				<option value="1">' . _('Yes') . '</option>
659				</select>
660			</td>
661		</tr>
662		<tr>
663			<td>' . _('Invoice Addressing') . ':</td>
664			<td><select tabindex="19" name="AddrInvBranch" required="required">
665				<option selected="selected" value="0">' . _('Address to HO') . '</option>
666				<option value="1">' . _('Address to Branch') . '</option>
667				</select>
668			</td>
669		</tr>
670		</table></td>
671		</tr>
672		</table>';
673	if ($DataError ==0){
674		echo '<br />
675			<div class="centre">
676				<input tabindex="20" type="submit" name="submit" value="' . _('Add New Customer') . '" />&nbsp;<input tabindex="21" type="submit" value="' . _('Reset') . '" />
677			</div>';
678
679	}
680    echo '</div>';
681	echo '</form>';
682
683} else {
684
685//DebtorNo exists - either passed when calling the form or from the form itself
686
687	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
688    echo '<div>';
689	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
690	echo '<table class="selection">
691			<tr><td valign="top">';
692
693	if (!isset($_POST['New'])) {
694		$sql = "SELECT debtorno,
695						name,
696						address1,
697						address2,
698						address3,
699						address4,
700						address5,
701						address6,
702						currcode,
703						salestype,
704						clientsince,
705						holdreason,
706						paymentterms,
707						discount,
708						discountcode,
709						pymtdiscount,
710						creditlimit,
711						invaddrbranch,
712						taxref,
713						customerpoline,
714						typeid,
715						language_id
716				FROM debtorsmaster
717				WHERE debtorno = '" . $DebtorNo . "'";
718
719		$ErrMsg = _('The customer details could not be retrieved because');
720		$result = DB_query($sql,$ErrMsg);
721
722		$myrow = DB_fetch_array($result);
723		/* if $AutoDebtorNo in config.php has not been set or if it has been set to a number less than one,
724		then display the DebtorNo */
725		if ($_SESSION['AutoDebtorNo']== 0 )  {
726			echo '<table class="selection"><tr>
727					<td>' . _('Customer Code') . ':</td>
728					<td>' . $DebtorNo. '</td>
729				</tr></table><br />';
730		}
731		$_POST['CustName'] = $myrow['name'];
732		$_POST['Address1']  = $myrow['address1'];
733		$_POST['Address2']  = $myrow['address2'];
734		$_POST['Address3']  = $myrow['address3'];
735		$_POST['Address4']  = $myrow['address4'];
736		$_POST['Address5']  = $myrow['address5'];
737		$_POST['Address6']  = $myrow['address6'];
738		$_POST['SalesType'] = $myrow['salestype'];
739		$_POST['CurrCode']  = $myrow['currcode'];
740		$_POST['ClientSince'] = ConvertSQLDate($myrow['clientsince']);
741		$_POST['HoldReason']  = $myrow['holdreason'];
742		$_POST['PaymentTerms']  = $myrow['paymentterms'];
743		$_POST['Discount']  = locale_number_format($myrow['discount'] * 100,2);
744		$_POST['DiscountCode']  = $myrow['discountcode'];
745		$_POST['PymtDiscount']  = locale_number_format($myrow['pymtdiscount'] * 100,2);
746		$_POST['CreditLimit']	= locale_number_format($myrow['creditlimit'],0);
747		$_POST['InvAddrBranch'] = $myrow['invaddrbranch'];
748		$_POST['TaxRef'] = $myrow['taxref'];
749		$_POST['CustomerPOLine'] = $myrow['customerpoline'];
750		$_POST['typeid'] = $myrow['typeid'];
751		$_POST['LanguageID'] = $myrow['language_id'];
752
753		echo '<input type="hidden" name="DebtorNo" value="' . $DebtorNo . '" />';
754        echo '<table class="selection">';
755
756	} else {
757	// its a new customer being added
758		echo '<input type="hidden" name="New" value="Yes" />';
759        echo '<table class="selection">';
760
761		/* if $AutoDebtorNo in config.php has not been set or if it has been set to a number less than one,
762		then provide an input box for the DebtorNo to manually assigned */
763		if ($_SESSION['AutoDebtorNo']== 0 )  {
764			echo '<tr>
765					<td>' . _('Customer Code') . ':</td>
766					<td><input ' . (in_array('DebtorNo',$Errors) ?  'class="inputerror"' : '' ) .' type="text" name="DebtorNo" required="required" data-type="no-illegal-chars" title="' . _('The customer code can be up to 10 alpha-numeric characters long or underscore') . '" value="' . $DebtorNo . '" size="12" maxlength="10" /></td></tr>';
767		}
768	}
769	if (isset($_GET['Modify'])) {
770		echo '<tr>
771				<td>' . _('Customer Name') . ':</td>
772				<td>' . $_POST['CustName'] . '</td>
773			</tr>
774			<tr>
775				<td>' . _('Address Line 1 (Street)') . ':</td>
776				<td>' . $_POST['Address1'] . '</td>
777			</tr>
778			<tr>
779				<td>' . _('Address Line 2 (Street)') . ':</td>
780				<td>' . $_POST['Address2'] . '</td>
781			</tr>
782			<tr>
783				<td>' . _('Address Line 3 (Suburb/City)') . ':</td>
784				<td>' . $_POST['Address3'] . '</td>
785			</tr>
786			<tr>
787				<td>' . _('Address Line 4 (State/Province)') . ':</td>
788				<td>' . $_POST['Address4'] . '</td>
789			</tr>
790			<tr>
791				<td>' . _('Address Line 5 (Postal Code)') . ':</td>
792				<td>' . $_POST['Address5'] . '</td>
793			</tr>
794			<tr>
795				<td>' . _('Country') . ':</td>
796				<td>' . $_POST['Address6'] . '</td>
797			</tr>';
798	} else {
799		echo '<tr>
800				<td>' . _('Customer Name') . ':</td>
801				<td><input ' . (in_array('CustName',$Errors) ?  'class="inputerror"' : '' ) .' type="text" name="CustName" required="required" autofocus="autofocus" value="' . $_POST['CustName'] . '" size="42" maxlength="40" /></td>
802			</tr>
803			<tr>
804				<td>' . _('Address Line 1 (Street)') . ':</td>
805				<td><input ' . (in_array('Address1',$Errors) ?  'class="inputerror"' : '' ) .' type="text" name="Address1" required="required" size="42" maxlength="40" value="' . $_POST['Address1'] . '" /></td>
806			</tr>
807			<tr>
808				<td>' . _('Address Line 2 (Street)') . ':</td>
809				<td><input ' . (in_array('Address2',$Errors) ?  'class="inputerror"' : '' ) .' type="text" name="Address2" size="42" maxlength="40" value="' . $_POST['Address2'] . '" /></td>
810			</tr>
811			<tr>
812				<td>' . _('Address Line 3 (Suburb/City)') . ':</td>
813				<td><input ' . (in_array('Address3',$Errors) ?  'class="inputerror"' : '' ) .' type="text" name="Address3" size="42" maxlength="40" value="' . $_POST['Address3'] . '" /></td>
814			</tr>
815			<tr>
816				<td>' . _('Address Line 4 (State/Province)') . ':</td>
817				<td><input ' . (in_array('Address4',$Errors) ?  'class="inputerror"' : '' ) .' type="text" name="Address4" size="42" maxlength="40" value="' . $_POST['Address4'] . '" /></td>
818			</tr>
819			<tr>
820				<td>' . _('Address Line 5 (Postal Code)') . ':</td>
821				<td><input ' . (in_array('Address5',$Errors) ?  'class="inputerror"' : '' ) .' type="text" name="Address5" size="42" maxlength="40" value="' . $_POST['Address5'] . '" /></td>
822			</tr>';
823		echo '<tr>
824				<td>' . _('Country') . ':</td>
825				<td><select name="Address6">';
826		foreach ($CountriesArray as $CountryEntry => $CountryName){
827			if (isset($_POST['Address6']) AND (strtoupper($_POST['Address6']) == strtoupper($CountryName))){
828				echo '<option selected="selected" value="' . $CountryName . '">' . $CountryName  . '</option>';
829			}elseif (!isset($_POST['Address6']) AND $CountryName == "") {
830				echo '<option selected="selected" value="' . $CountryName . '">' . $CountryName  . '</option>';
831			} else {
832				echo '<option value="' . $CountryName . '">' . $CountryName  . '</option>';
833			}
834		}
835		echo '</select></td>
836			</tr>';
837
838	}
839// Select sales types for drop down list
840	if (isset($_GET['Modify'])) {
841		$result=DB_query("SELECT sales_type FROM salestypes WHERE typeabbrev='".$_POST['SalesType']."'");
842		$myrow=DB_fetch_array($result);
843		echo '<tr>
844				<td>' . _('Sales Type') . ':</td>
845				<td>' . $myrow['sales_type'] . '</td></tr>';
846	} else {
847		$result=DB_query("SELECT typeabbrev, sales_type FROM salestypes");
848		echo '<tr>
849				<td>' . _('Sales Type') . '/' . _('Price List') . ':</td>
850				<td><select name="SalesType" required="required">';
851		while ($myrow = DB_fetch_array($result)) {
852			if ($_POST['SalesType']==$myrow['typeabbrev']){
853				echo '<option selected="selected" value="' . $myrow['typeabbrev'] . '">' . $myrow['sales_type'] . '</option>';
854			} else {
855				echo '<option value="'. $myrow['typeabbrev'] . '">' . $myrow['sales_type'] . '</option>';
856			}
857		} //end while loop
858		DB_data_seek($result,0);
859        echo '</select></td></tr>';
860	}
861
862// Select Customer types for drop down list for SELECT/UPDATE
863	if (isset($_GET['Modify'])) {
864		$result=DB_query("SELECT typename FROM debtortype WHERE typeid='".$_POST['typeid']."'");
865		$myrow=DB_fetch_array($result);
866		echo '<tr>
867				<td>' . _('Customer Type') . ':</td>
868				<td>' . $myrow['typename'] . '</td>
869			</tr>';
870	} else {
871		$result=DB_query("SELECT typeid, typename FROM debtortype ORDER BY typename");
872		echo '<tr>
873				<td>' . _('Customer Type') . ':</td>
874				<td><select name="typeid" required="required">';
875		while ($myrow = DB_fetch_array($result)) {
876				if ($_POST['typeid']==$myrow['typeid']){
877					echo '<option selected="selected" value="' . $myrow['typeid'] . '">' . $myrow['typename'] . '</option>';
878				} else {
879					echo '<option value="'. $myrow['typeid'] . '">' . $myrow['typename'] . '</option>';
880				}
881		} //end while loop
882		DB_data_seek($result,0);
883	}
884
885	if (isset($_GET['Modify'])) {
886		echo '</select></td></tr>
887			<tr><td>' . _('Customer Since') . ' (' . $_SESSION['DefaultDateFormat'] . '):</td>
888				<td>' . $_POST['ClientSince'] . '</td></tr>';
889
890		echo '</table></td>
891				<td><table class="selection">';
892
893		echo '<tr>
894				<td>' . _('Discount Percent') . ':</td>
895				<td>' . $_POST['Discount'] . '</td>
896			</tr>
897			<tr>
898				<td>' . _('Discount Code') . ':</td>
899				<td>' . $_POST['DiscountCode'] . '</td>
900			</tr>
901			<tr>
902				<td>' . _('Payment Discount Percent') . ':</td>
903				<td>' . $_POST['PymtDiscount'] . '</td>
904			</tr>
905			<tr>
906				<td>' . _('Credit Limit') . ':</td>
907				<td>' . $_POST['CreditLimit'] . '</td>
908			</tr>
909			<tr>
910				<td>' . _('Tax Reference') . ':</td>
911				<td>' . $_POST['TaxRef'] . '</td>
912			</tr>';
913	} else {
914		echo '</select></td>
915			</tr>
916			<tr>
917				<td>' . _('Customer Since') . ' (' . $_SESSION['DefaultDateFormat'] . '):</td>
918				<td><input ' . (in_array('ClientSince',$Errors) ?  'class="inputerror"' : '' ) .' type="text" class="date" name="ClientSince" size="11" maxlength="10" value="' . $_POST['ClientSince'] . '" /></td>
919			</tr>
920			</table></td>
921			<td><table class="selection">';
922
923		echo '<tr>
924				<td>' . _('Discount Percent') . ':</td>
925				<td><input type="text" name="Discount" class="number" size="5" maxlength="4" value="' . $_POST['Discount'] . '" /></td>
926			</tr>
927			<tr>
928				<td>' . _('Discount Code') . ':</td>
929				<td><input ' . (in_array('DiscountCode',$Errors) ?  'class="inputerror"' : '' ) .' type="text" name="DiscountCode" size="3" maxlength="2" value="' . $_POST['DiscountCode'] . '" /></td>
930			</tr>
931			<tr>
932				<td>' . _('Payment Discount Percent') . ':</td>
933				<td><input ' . (in_array('PymtDiscount',$Errors) ?  'class="inputerror"' : '' ) .' type="text" class="number" name="PymtDiscount" size="5" maxlength="4" value="' . $_POST['PymtDiscount'] . '" /></td>
934			</tr>
935			<tr>
936				<td>' . _('Credit Limit') . ':</td>
937				<td><input ' . (in_array('CreditLimit',$Errors) ?  'class="inputerror"' : '' ) .' type="text" class="integer" name="CreditLimit" required="required" size="16" maxlength="14" value="' . $_POST['CreditLimit'] . '" /></td>
938			</tr>
939			<tr>
940				<td>' . _('Tax Reference') . ':</td>
941				<td><input type="text" name="TaxRef" size="22" maxlength="20"  value="' . $_POST['TaxRef'] . '" /></td>
942			</tr>';
943	}
944
945	if (isset($_GET['Modify'])) {
946		$result=DB_query("SELECT terms FROM paymentterms WHERE termsindicator='".$_POST['PaymentTerms']."'");
947		$myrow=DB_fetch_array($result);
948		echo '<tr>
949				<td>' . _('Payment Terms') . ':</td>
950				<td>' . $myrow['terms'] . '</td>
951			</tr>';
952	} else {
953		$result=DB_query("SELECT terms, termsindicator FROM paymentterms");
954		echo '<tr>
955				<td>' . _('Payment Terms') . ':</td>
956				<td><select name="PaymentTerms" required="required">';
957		while ($myrow = DB_fetch_array($result)) {
958			if ($_POST['PaymentTerms']==$myrow['termsindicator']){
959				echo '<option selected="selected" value="'. $myrow['termsindicator'] . '">' . $myrow['terms'] . '</option>';
960			} else {
961				echo '<option value="'. $myrow['termsindicator'] . '">' . $myrow['terms'] . '</option>';
962			}
963		} //end while loop
964		DB_data_seek($result,0);
965		echo '</select></td>
966			</tr>';
967	}
968
969	if (isset($_GET['Modify'])) {
970		$result=DB_query("SELECT reasondescription FROM holdreasons WHERE reasoncode='".$_POST['HoldReason']."'");
971		$myrow=DB_fetch_array($result);
972		echo '<tr>
973				<td>' . _('Credit Status') . ':</td>
974				<td>' . $myrow['reasondescription'] . '</td>
975			</tr>';
976	} else {
977		$result=DB_query("SELECT reasoncode, reasondescription FROM holdreasons");
978		echo '<tr>
979				<td>' . _('Credit Status') . ':</td>
980				<td><select name="HoldReason" required="required">';
981		while ($myrow = DB_fetch_array($result)) {
982			if ($_POST['HoldReason']==$myrow['reasoncode']){
983				echo '<option selected="selected" value="'. $myrow['reasoncode'] . '">' . $myrow['reasondescription'] . '</option>';
984			} else {
985				echo '<option value="'. $myrow['reasoncode'] . '">' . $myrow['reasondescription'] . '</option>';
986			}
987		} //end while loop
988		DB_data_seek($result,0);
989		echo '</select></td>
990			</tr>';
991	}
992
993	if (isset($_GET['Modify'])) {
994		echo '<tr>
995				<td>' . _('Customer Currency') . ':</td>
996				<td>' . $CurrencyName[$_POST['CurrCode']] . '</td></tr>';
997	} else {
998		$result=DB_query("SELECT currency, currabrev FROM currencies");
999		echo '<tr>
1000				<td>' . _('Customer Currency') . ':</td>
1001				<td><select name="CurrCode" required="required">';
1002		while ($myrow = DB_fetch_array($result)) {
1003			echo '<option';
1004			if ($_POST['CurrCode']==$myrow['currabrev']){
1005				echo ' selected="selected"';
1006			}
1007			echo ' value="'. $myrow['currabrev'] . '">' . $CurrencyName[$myrow['currabrev']] . '</option>';
1008		} //end while loop
1009		DB_data_seek($result,0);
1010		echo '</select></td>
1011			</tr>';
1012	}
1013
1014    if (!isset($_POST['LanguageID']) OR $_POST['LanguageID']==''){
1015        $_POST['LanguageID']=$_SESSION['Language'];
1016    }
1017
1018	if (isset($_GET['Modify'])) {
1019        echo '<tr>
1020                <td>' . _('Language') . ':</td>';
1021        foreach ($LanguagesArray as $LanguageCode => $LanguageName){
1022            if ($_POST['LanguageID'] == $LanguageCode){
1023                echo '<td>' . $LanguageName['LanguageName'];
1024            }
1025        }
1026        echo '</td>
1027        </tr>';
1028	} else {
1029        echo '<tr>
1030                <td>' . _('Language') . ':</td>
1031                <td><select name="LanguageID" required="required">';
1032        foreach ($LanguagesArray as $LanguageCode => $LanguageName){
1033            if ($_POST['LanguageID'] == $LanguageCode){
1034                echo '<option selected="selected" value="' . $LanguageCode . '">' . $LanguageName['LanguageName']  . '</option>';
1035            } else {
1036                echo '<option value="' . $LanguageCode . '">' . $LanguageName['LanguageName']  . '</option>';
1037            }
1038        }
1039        echo '</select></td>
1040        </tr>';
1041    }
1042	echo '<tr>
1043			<td>' . _('Require Customer PO Line on SO') . ':</td>';
1044	if (isset($_GET['Modify'])) {
1045		if ($_POST['CustomerPOLine']==0){
1046			echo '<td>' . _('No') . '</td>';
1047		} else {
1048			echo '<td>' . _('Yes') . '</td>';
1049		}
1050	} else {
1051		echo '<td><select name="CustomerPOLine">';
1052		if ($_POST['CustomerPOLine']==0){
1053			echo '<option selected="selected" value="0">' . _('No') . '</option>';
1054			echo '<option value="1">' . _('Yes') . '</option>';
1055		} else {
1056			echo '<option value="0">' . _('No') . '</option>';
1057			echo '<option selected="selected" value="1">' . _('Yes') . '</option>';
1058		}
1059		echo '</select></td>';
1060	}
1061	echo '</tr>';
1062
1063	if (isset($_GET['Modify'])) {
1064		if ($_POST['InvAddrBranch']==0){
1065			echo '<tr>
1066					<td>' . _('Invoice Addressing') . ':</td>
1067					<td>' . _('Address to HO') . '</td>
1068				</tr>';
1069		} else {
1070			echo '<tr>
1071					<td>' . _('Invoice Addressing') . ':</td>
1072					<td>' . _('Address to Branch') . '</td>
1073				</tr>';
1074		}
1075	} else {
1076		echo '<tr>
1077				<td>' . _('Invoice Addressing') . ':</td>
1078				<td><select name="AddrInvBranch">';
1079		if ($_POST['InvAddrBranch']==0){
1080			echo '<option selected="selected" value="0">' . _('Address to HO') . '</option>';
1081			echo '<option value="1">' . _('Address to Branch') . '</option>';
1082		} else {
1083			echo '<option value="0">' . _('Address to HO') . '</option>';
1084			echo '<option selected="selected" value="1">' . _('Address to Branch') . '</option>';
1085		}
1086	}
1087
1088	echo '</select></td>
1089		</tr>
1090		</table></td>
1091		</tr>
1092		<tr><td colspan="2">';
1093
1094	if (isset($_GET['delete'])) { //User hit delete link on customer contacts
1095		/*Process this first before showing remaining contacts */
1096		$resultupcc = DB_query("DELETE FROM custcontacts
1097								WHERE debtorno='".$DebtorNo."'
1098								AND contid='".$ID."'");
1099		prnMsg(_('Contact Deleted'),'success');
1100	}
1101
1102  	$sql = "SELECT contid,
1103					debtorno,
1104					contactname,
1105					role,
1106					phoneno,
1107					notes,
1108					email
1109			FROM custcontacts
1110			WHERE debtorno='".$DebtorNo."'
1111			ORDER BY contid";
1112	$result = DB_query($sql);
1113
1114	echo '<table class="selection">';
1115	if (isset($_GET['Modify'])) {
1116		echo '<tr>
1117				<th>' . _('Name') . '</th>
1118				<th>' . _('Role') . '</th>
1119				<th>' . _('Phone Number') . '</th>
1120				<th>' . _('Email') . '</th>
1121				<th>' . _('Notes') . '</th>
1122			</tr>';
1123	} else {
1124		echo '<tr>
1125				<th>' . _('Name') . '</th>
1126				<th>' . _('Role') . '</th>
1127				<th>' . _('Phone Number') . '</th>
1128				<th>' . _('Email') . '</th>
1129				<th>' . _('Notes') . '</th>
1130				<th>' . _('Edit') . '</th>
1131				<th colspan="2"><input type="submit" name="AddContact" value="' . _('Add Contact') . '" /></th>
1132			</tr>';
1133	}
1134
1135	while ($myrow = DB_fetch_array($result)) {
1136
1137		if (isset($_GET['Modify'])) {
1138			printf('<tr class="striped_row">
1139					<td>%s</td>
1140					<td>%s</td>
1141					<td>%s</td>
1142					<td><a href="mailto:%s">%s</a></td>
1143					<td>%s</td>
1144					</tr>',
1145					$myrow['contactname'],
1146					$myrow['role'],
1147					$myrow['phoneno'],
1148					$myrow['email'],
1149					$myrow['email'],
1150					$myrow['notes']);
1151		} else {
1152			printf('<tr class="striped_row">
1153					<td>%s</td>
1154					<td>%s</td>
1155					<td>%s</td>
1156					<td><a href="mailto:%s">%s</a></td>
1157					<td>%s</td>
1158					<td><a href="AddCustomerContacts.php?Id=%s&amp;DebtorNo=%s">' .  _('Edit'). '</a></td>
1159					<td><a href="%sID=%s&amp;DebtorNo=%s&amp;delete=1" onclick="return confirm(\'' . _('Are you sure you wish to delete this customer contact?') . '\');">' .  _('Delete'). '</a></td>
1160					</tr>',
1161					$myrow['contactname'],
1162					$myrow['role'],
1163					$myrow['phoneno'],
1164					$myrow['email'],
1165					$myrow['email'],
1166					$myrow['notes'],
1167					$myrow['contid'],
1168					$myrow['debtorno'],
1169					htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
1170					$myrow['contid'],
1171					$myrow['debtorno']);
1172		}
1173	}//END WHILE LIST LOOP
1174	echo '</table>';
1175
1176	echo'</td></tr></table>';
1177
1178	if (isset($_POST['New']) AND $_POST['New']) {
1179		echo '<div class="centre">
1180				<input type="submit" name="submit" value="' . _('Add New Customer') . '" />&nbsp;
1181				<input type="submit" name="Reset" value="' . _('Reset') . '" />
1182			</div>';
1183	} elseif (!isset($_GET['Modify'])){
1184		echo '<br />
1185			<div class="centre">
1186				<input type="submit" name="submit" value="' . _('Update Customer') . '" />&nbsp;
1187				<input type="submit" name="delete" value="' . _('Delete Customer') . '" onclick="return confirm(\'' . _('Are You Sure?') . '\');" />
1188            </div>';
1189	}
1190
1191	echo '</div>
1192          </form>';
1193} // end of main ifs
1194
1195include('includes/footer.php');
1196?>
1197