1<?php
2/* Selection of customer - from where all customer related maintenance, transactions and inquiries start */
3
4include('includes/session.php');
5$Title = _('Search Customers');
6$ViewTopic = 'AccountsReceivable';
7$BookMark = 'SelectCustomer';
8include('includes/header.php');
9
10include('includes/SQL_CommonFunctions.inc');
11
12if(isset($_GET['Select'])) {
13	$_SESSION['CustomerID'] = $_GET['Select'];
14}
15
16if(!isset($_SESSION['CustomerID'])) {// initialise if not already done
17	$_SESSION['CustomerID'] = '';
18}
19
20if(isset($_GET['Area'])) {
21	$_POST['Area'] = $_GET['Area'];
22	$_POST['Search'] = 'Search';
23	$_POST['Keywords'] = '';
24	$_POST['CustCode'] = '';
25	$_POST['CustPhone'] = '';
26	$_POST['CustAdd'] = '';
27	$_POST['CustType'] = '';
28}
29
30if(!isset($_SESSION['CustomerType'])) {// initialise if not already done
31	$_SESSION['CustomerType'] = '';
32}
33
34if(isset($_POST['JustSelectedACustomer'])) {
35	if(isset ($_POST['SubmitCustomerSelection'])) {
36	foreach ($_POST['SubmitCustomerSelection'] as $CustomerID => $BranchCode)
37		$_SESSION['CustomerID'] = $CustomerID;
38		$_SESSION['BranchCode'] = $BranchCode;
39	} else {
40		prnMsg(_('Unable to identify the selected customer'), 'error');
41	}
42}
43
44$msg = '';
45
46if(isset($_POST['Go1']) OR isset($_POST['Go2'])) {
47	$_POST['PageOffset'] = (isset($_POST['Go1']) ? $_POST['PageOffset1'] : $_POST['PageOffset2']);
48	$_POST['Go'] = '';
49}
50
51if(!isset($_POST['PageOffset'])) {
52	$_POST['PageOffset'] = 1;
53} else {
54	if($_POST['PageOffset'] == 0) {
55		$_POST['PageOffset'] = 1;
56	}
57}
58
59if(isset($_POST['Search']) OR isset($_POST['CSV']) OR isset($_POST['Go']) OR isset($_POST['Next']) OR isset($_POST['Previous'])) {
60	unset($_POST['JustSelectedACustomer']);
61	if(isset($_POST['Search'])) {
62		$_POST['PageOffset'] = 1;
63	}
64
65	if(($_POST['Keywords'] == '') AND ($_POST['CustCode'] == '') AND ($_POST['CustPhone'] == '') AND ($_POST['CustType'] == 'ALL') AND ($_POST['Area'] == 'ALL') AND ($_POST['CustAdd'] == '')) {
66		// no criteria set then default to all customers
67		$SQL = "SELECT debtorsmaster.debtorno,
68					debtorsmaster.name,
69					debtorsmaster.address1,
70					debtorsmaster.address2,
71					debtorsmaster.address3,
72					debtorsmaster.address4,
73					custbranch.branchcode,
74					custbranch.brname,
75					custbranch.contactname,
76					debtortype.typename,
77					custbranch.phoneno,
78					custbranch.faxno,
79					custbranch.email
80				FROM debtorsmaster LEFT JOIN custbranch
81				ON debtorsmaster.debtorno = custbranch.debtorno
82				INNER JOIN debtortype
83				ON debtorsmaster.typeid = debtortype.typeid";
84	} else {
85		$SearchKeywords = mb_strtoupper(trim(str_replace(' ', '%', $_POST['Keywords'])));
86		$_POST['CustCode'] = mb_strtoupper(trim($_POST['CustCode']));
87		$_POST['CustPhone'] = trim($_POST['CustPhone']);
88		$_POST['CustAdd'] = trim($_POST['CustAdd']);
89		$SQL = "SELECT debtorsmaster.debtorno,
90						debtorsmaster.name,
91						debtorsmaster.address1,
92						debtorsmaster.address2,
93						debtorsmaster.address3,
94						debtorsmaster.address4,
95						custbranch.branchcode,
96						custbranch.brname,
97						custbranch.contactname,
98						debtortype.typename,
99						custbranch.phoneno,
100						custbranch.faxno,
101						custbranch.email
102					FROM debtorsmaster INNER JOIN debtortype
103						ON debtorsmaster.typeid = debtortype.typeid
104					LEFT JOIN custbranch
105						ON debtorsmaster.debtorno = custbranch.debtorno
106					WHERE debtorsmaster.name " . LIKE . " '%" . $SearchKeywords . "%'
107					AND debtorsmaster.debtorno " . LIKE . " '%" . $_POST['CustCode'] . "%'
108					AND (custbranch.phoneno " . LIKE . " '%" . $_POST['CustPhone'] . "%' OR custbranch.phoneno IS NULL)
109					AND (debtorsmaster.address1 " . LIKE . " '%" . $_POST['CustAdd'] . "%'
110						OR debtorsmaster.address2 " . LIKE . " '%" . $_POST['CustAdd'] . "%'
111						OR debtorsmaster.address3 " . LIKE . " '%" . $_POST['CustAdd'] . "%'
112						OR debtorsmaster.address4 " . LIKE . " '%" . $_POST['CustAdd'] . "%')";// If there is no custbranch set, the phoneno in custbranch will be null, so we add IS NULL condition otherwise those debtors without custbranches setting will be no searchable and it will make a inconsistence with customer receipt interface.
113
114		if(mb_strlen($_POST['CustType']) > 0 AND $_POST['CustType'] != 'ALL') {
115			$SQL .= " AND debtortype.typename = '" . $_POST['CustType'] . "'";
116		}
117
118		if(mb_strlen($_POST['Area']) > 0 AND $_POST['Area'] != 'ALL') {
119			$SQL .= " AND custbranch.area = '" . $_POST['Area'] . "'";
120		}
121
122	}// one of keywords OR custcode OR custphone was more than a zero length string
123
124	if($_SESSION['SalesmanLogin'] != '') {
125		$SQL .= " AND custbranch.salesman='" . $_SESSION['SalesmanLogin'] . "'";
126	}
127
128	$SQL .= " ORDER BY debtorsmaster.name";
129	$ErrMsg = _('The searched customer records requested cannot be retrieved because');
130
131	$result = DB_query($SQL, $ErrMsg);
132	if(DB_num_rows($result) == 1) {
133		$myrow = DB_fetch_array($result);
134		$_SESSION['CustomerID'] = $myrow['debtorno'];
135		$_SESSION['BranchCode'] = $myrow['branchcode'];
136		unset($result);
137		unset($_POST['Search']);
138	} elseif(DB_num_rows($result) == 0) {
139		prnMsg(_('No customer records contain the selected text') . ' - ' . _('please alter your search criteria AND try again'), 'info');
140		echo '<br />';
141	}
142}// end of if search
143
144$TableHead =
145	'<table cellpadding="4" width="90%" class="selection">
146		<thead>
147			<tr>
148				<th style="width:33%">' .
149					'<img alt="" src="' . $RootPath . '/css/' . $Theme . '/images/reports.png" title="' . _('Inquiries and Reports') . '" />' .
150					_('Customer Inquiries') . '</th>
151				<th style="width:33%">' .
152					'<img alt="" src="' . $RootPath . '/css/' . $Theme . '/images/transactions.png" title="' . _('Transactions') . '" />' .
153					_('Customer Transactions') . '</th>
154				<th style="width:33%">' .
155					'<img alt="" src="' . $RootPath . '/css/' . $Theme . '/images/maintenance.png" title="' . _('Maintenance') . '" />' .
156					_('Customer Maintenance') . '</th>
157			</tr>
158		</thead>
159		<tbody>';
160
161if($_SESSION['CustomerID'] != '' AND !isset($_POST['Search']) AND !isset($_POST['CSV'])) {
162	// A customer is selected
163	if(!isset($_SESSION['BranchCode'])) {
164		// !isset($_SESSION['BranchCode'])
165		$SQL = "SELECT debtorsmaster.name,
166					custbranch.phoneno,
167					custbranch.brname
168			FROM debtorsmaster INNER JOIN custbranch
169			ON debtorsmaster.debtorno=custbranch.debtorno
170			WHERE custbranch.debtorno='" . $_SESSION['CustomerID'] . "'";
171
172	} else {
173		// isset($_SESSION['BranchCode'])
174		$SQL = "SELECT debtorsmaster.name,
175					custbranch.phoneno,
176					custbranch.brname
177			FROM debtorsmaster INNER JOIN custbranch
178			ON debtorsmaster.debtorno=custbranch.debtorno
179			WHERE custbranch.debtorno='" . $_SESSION['CustomerID'] . "'
180			AND custbranch.branchcode='" . $_SESSION['BranchCode'] . "'";
181	}
182	$ErrMsg = _('The customer name requested cannot be retrieved because');
183	$result = DB_query($SQL, $ErrMsg);
184	if($myrow = DB_fetch_array($result)) {
185		$CustomerName = htmlspecialchars($myrow['name'], ENT_QUOTES, 'UTF-8', false);
186		$PhoneNo = $myrow['phoneno'];
187		$BranchName = $myrow['brname'];
188	}// $myrow = DB_fetch_array($result)
189	unset($result);
190
191	echo '<p class="page_title_text"><img alt="" src="', $RootPath, '/css/', $Theme,
192		'/images/customer.png" title="',// Icon image.
193		_('Customer'), '" /> ',// Icon title.
194		_('Customer'), ': ', $_SESSION['CustomerID'], ' - ', $CustomerName, ' - ', $PhoneNo, '</p>',// Page title.
195		'<div class="page_help_text">', _('Select a menu option to operate using this customer.'), '</div>',// Page help text.
196		'<br />',
197		$TableHead,
198			'<tr>
199				<td valign="top" class="select">';
200	// Customer inquiries options:
201	echo '<a href="', $RootPath, '/CustomerInquiry.php?CustomerID=', urlencode($_SESSION['CustomerID']), '">' . _('Customer Transaction Inquiries') . '</a><br />';
202	echo '<a href="', $RootPath, '/CustomerAccount.php?CustomerID=', urlencode($_SESSION['CustomerID']), '">' . _('Customer Account statement on screen') . '</a><br />';
203	echo '<a href="', $RootPath, '/Customers.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '&amp;Modify=No">' . _('View Customer Details') . '</a><br />';
204	echo '<a href="', $RootPath, '/PrintCustStatements.php?FromCust=', urlencode($_SESSION['CustomerID']), '&amp;ToCust=', urlencode($_SESSION['CustomerID']), '&amp;EmailOrPrint=print&amp;PrintPDF=Yes">' . _('Print Customer Statement') . '</a><br />';
205	echo '<a title="' . _('One of the customer\'s contacts must have an email address and be flagged as the address to send the customer statement to for this function to work') . '" href="', $RootPath, '/PrintCustStatements.php?FromCust=', urlencode($_SESSION['CustomerID']), '&amp;ToCust=', urlencode($_SESSION['CustomerID']), '&amp;EmailOrPrint=email&amp;PrintPDF=Yes">' . _('Email Customer Statement') . '</a><br />';
206	echo '<a href="', $RootPath, '/SelectCompletedOrder.php?SelectedCustomer=', urlencode($_SESSION['CustomerID']), '">' . _('Order Inquiries') . '</a><br />';
207	echo '<a href="', $RootPath, '/CustomerPurchases.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">' . _('Show purchases from this customer') . '</a><br />';
208	wikiLink('Customer', $_SESSION['CustomerID']);
209	echo '</td><td valign="top" class="select">';
210	// Customer transactions options:
211	echo '<a href="', $RootPath, '/SelectSalesOrder.php?SelectedCustomer=', urlencode($_SESSION['CustomerID']), '">' . _('Modify Outstanding Sales Orders') . '</a><br />';
212	echo '<a title="' . _('This allows the deposits received from the customer to be matched against invoices') . '" href="', $RootPath, '/CustomerAllocations.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">' . _('Allocate Receipts or Credit Notes') . '</a><br />';
213	if(isset($_SESSION['CustomerID']) AND isset($_SESSION['BranchCode'])) {
214	echo '<a href="', $RootPath, '/CounterSales.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '&amp;BranchNo=' . $_SESSION['BranchCode'] . '">' . _('Create a Counter Sale for this Customer') . '</a><br />';
215	}
216	echo '</td><td valign="top" class="select">';
217	// Customer maintenance options:
218	echo '<a href="', $RootPath, '/Customers.php">' . _('Add a New Customer') . '</a><br />';
219	echo '<a href="', $RootPath, '/Customers.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">' . _('Modify Customer Details') . '</a><br />';
220	echo '<a href="', $RootPath, '/CustomerBranches.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">' . _('Add/Edit/Delete Customer Branches') . '</a><br />';
221	echo '<a href="', $RootPath, '/SelectProduct.php">' . _('Special Customer Prices') . '</a><br />';
222	echo '<a href="', $RootPath, '/CustEDISetup.php">' . _('Customer EDI Configuration') . '</a><br />';
223	echo '<a href="', $RootPath, '/CustLoginSetup.php">' . _('Customer Login Configuration'), '</a><br />';
224	echo '<a href="', $RootPath, '/AddCustomerContacts.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">', _('Add a customer contact'), '</a><br />';
225	echo '<a href="', $RootPath, '/AddCustomerNotes.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">', _('Add a note on this customer'), '</a>';
226	echo '</td>
227			</tr>
228		<tbody>
229		</table>';
230} else {
231	// Customer is not selected yet
232	echo '<p class="page_title_text"><img alt="" src="', $RootPath, '/css/', $Theme,
233		'/images/customer.png" title="', // Icon image.
234		_('Customers'), '" /> ', // Icon title.
235		_('Customers'), '</p>',// Page title.
236		'<br />',
237		$TableHead,
238		'<tr>',
239			'<td class="select"></td>',// Customer inquiries options.
240			'<td class="select"></td>',// Customer transactions options.
241			'<td class="select">';
242	if(!isset($_SESSION['SalesmanLogin']) OR $_SESSION['SalesmanLogin'] == '') {
243		echo '<a href="', $RootPath, '/Customers.php">', _('Add a New Customer'), '</a><br />';
244	}
245	echo '</td>',// Item maintenance options.
246		'</tr><tbody></table>';
247}
248
249// Search for customers:
250echo '<form action="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '" method="post">',
251	'<input type="hidden" name="FormID" value="', $_SESSION['FormID'], '" />';
252if(mb_strlen($msg) > 1) {
253	prnMsg($msg, 'info');
254}
255echo '<p class="page_title_text"><img alt="" src="', $RootPath, '/css/', $Theme,
256	'/images/magnifier.png" title="',// Icon image.
257	_('Search'), '" /> ',// Icon title.
258	_('Search for Customers'), '</p>';// Page title.
259
260echo '<table cellpadding="3" class="selection">';
261
262echo '<tr>
263		<td colspan="2">', _('Enter a partial Name'), ':</td>
264		<td><input type="text" maxlength="25" name="Keywords" title="', _('If there is an entry in this field then customers with the text entered in their name will be returned') , '"  size="20" ',
265			( isset($_POST['Keywords']) ? 'value="' . $_POST['Keywords'] . '" ' : '' ), '/></td>';
266
267echo '<td><b>', _('OR'), '</b></td><td>', _('Enter a partial Code'), ':</td>
268		<td><input maxlength="18" name="CustCode" pattern="[\w-]*" size="15" type="text" title="', _('If there is an entry in this field then customers with the text entered in their customer code will be returned') , '" ', (isset($_POST['CustCode']) ? 'value="' . $_POST['CustCode'] . '" ' : '' ), '/></td>
269	</tr>';
270
271echo '<tr>
272		<td><b>', _('OR'), '</b></td><td>', _('Enter a partial Phone Number'), ':</td>
273		<td><input maxlength="18" name="CustPhone" pattern="[0-9\-\s()+]*" size="15" type="tel" ',
274			( isset($_POST['CustPhone']) ? 'value="' . $_POST['CustPhone'] . '" ' : '' ), '/></td>';
275
276echo '<td><b>', _('OR'), '</b></td><td>', _('Enter part of the Address'), ':</td>
277		<td><input maxlength="25" name="CustAdd" size="20" type="text" ',
278			(isset($_POST['CustAdd']) ? 'value="' . $_POST['CustAdd'] . '" ' : '' ), '/></td>
279	</tr>';
280
281echo '<tr>
282		<td><b>', _('OR'), '</b></td><td>', _('Choose a Type'), ':</td>
283		<td>';
284if(isset($_POST['CustType'])) {
285	// Show Customer Type drop down list
286	$result2 = DB_query("SELECT typeid, typename FROM debtortype ORDER BY typename");
287	// Error if no customer types setup
288	if(DB_num_rows($result2) == 0) {
289		$DataError = 1;
290		echo '<a href="CustomerTypes.php" target="_parent">' . _('Setup Types') . '</a>';
291		echo '<tr><td colspan="2">' . prnMsg(_('No Customer types defined'), 'error') . '</td></tr>';
292	} else {
293		// If OK show select box with option selected
294		echo '<select name="CustType">
295				<option value="ALL">' . _('Any') . '</option>';
296		while ($myrow = DB_fetch_array($result2)) {
297			if($_POST['CustType'] == $myrow['typename']) {
298				echo '<option selected="selected" value="' . $myrow['typename'] . '">' . $myrow['typename'] . '</option>';
299			}// $_POST['CustType'] == $myrow['typename']
300			else {
301				echo '<option value="' . $myrow['typename'] . '">' . $myrow['typename'] . '</option>';
302			}
303		}// end while loop
304		DB_data_seek($result2, 0);
305		echo '</select></td>';
306	}
307} else {// CustType is not set
308	// No option selected="selected" yet, so show Customer Type drop down list
309	$result2 = DB_query("SELECT typeid, typename FROM debtortype ORDER BY typename");
310	// Error if no customer types setup
311	if(DB_num_rows($result2) == 0) {
312		$DataError = 1;
313		echo '<a href="CustomerTypes.php" target="_parent">' . _('Setup Types') . '</a>';
314		echo '<tr><td colspan="2">' . prnMsg(_('No Customer types defined'), 'error') . '</td></tr>';
315	} else {
316		// if OK show select box with available options to choose
317		echo '<select name="CustType">
318				<option value="ALL">' . _('Any') . '</option>';
319		while ($myrow = DB_fetch_array($result2)) {
320			echo '<option value="' . $myrow['typename'] . '">' . $myrow['typename'] . '</option>';
321		}// end while loop
322		DB_data_seek($result2, 0);
323		echo '</select></td>';
324	}
325}
326
327/* Option to select a sales area */
328echo '<td><b>', _('OR'), '</b></td>
329		<td>' . _('Choose an Area') . ':</td><td>';
330$result2 = DB_query("SELECT areacode, areadescription FROM areas");
331// Error if no sales areas setup
332if(DB_num_rows($result2) == 0) {
333	$DataError = 1;
334	echo '<a href="Areas.php" target="_parent">' . _('Setup Areas') . '</a>';
335	echo '<tr><td colspan="2">' . prnMsg(_('No Sales Areas defined'), 'error') . '</td></tr>';
336} else {
337	// if OK show select box with available options to choose
338	echo '<select name="Area">';
339	echo '<option value="ALL">' . _('Any') . '</option>';
340	while ($myrow = DB_fetch_array($result2)) {
341		if(isset($_POST['Area']) AND $_POST['Area'] == $myrow['areacode']) {
342			echo '<option selected="selected" value="' . $myrow['areacode'] . '">' . $myrow['areadescription'] . '</option>';
343		} else {
344			echo '<option value="' . $myrow['areacode'] . '">' . $myrow['areadescription'] . '</option>';
345		}
346	}// end while loop
347	DB_data_seek($result2, 0);
348	echo '</select></td></tr>';
349}
350
351echo '</table><br />';
352echo '<div class="centre">
353		<input name="Search" type="submit" value="', _('Search Now'), '" />
354		<input name="CSV" type="submit" value="', _('CSV Format'), '" />
355	</div>';
356// End search for customers.
357
358
359if(isset($_SESSION['SalesmanLogin']) AND $_SESSION['SalesmanLogin'] != '') {
360	prnMsg(_('Your account enables you to see only customers allocated to you'), 'warn', _('Note: Sales-person Login'));
361}
362
363if(isset($result)) {
364	unset($_SESSION['CustomerID']);
365	$ListCount = DB_num_rows($result);
366	$ListPageMax = ceil($ListCount / $_SESSION['DisplayRecordsMax']);
367	if(!isset($_POST['CSV'])) {
368		if(isset($_POST['Next'])) {
369			if($_POST['PageOffset'] < $ListPageMax) {
370				$_POST['PageOffset'] = $_POST['PageOffset'] + 1;
371			}
372		}
373		if(isset($_POST['Previous'])) {
374			if($_POST['PageOffset'] > 1) {
375				$_POST['PageOffset'] = $_POST['PageOffset'] - 1;
376			}
377		}
378		echo '<input type="hidden" name="PageOffset" value="' . $_POST['PageOffset'] . '" />';
379		if($ListPageMax > 1) {
380			echo '<br /><div class="centre">&nbsp;&nbsp;' . $_POST['PageOffset'] . ' ' . _('of') . ' ' . $ListPageMax . ' ' . _('pages') . '. ' . _('Go to Page') . ': ';
381			echo '<select name="PageOffset1">';
382			$ListPage = 1;
383			while ($ListPage <= $ListPageMax) {
384				if($ListPage == $_POST['PageOffset']) {
385					echo '<option value="' . $ListPage . '" selected="selected">' . $ListPage . '</option>';
386				} else {
387					echo '<option value="' . $ListPage . '">' . $ListPage . '</option>';
388				}
389				$ListPage++;
390			}
391			echo '</select>
392				<input type="submit" name="Go1" value="' . _('Go') . '" />
393				<input type="submit" name="Previous" value="' . _('Previous') . '" />
394				<input type="submit" name="Next" value="' . _('Next') . '" />';
395			echo '</div>';
396		}
397		echo '<table cellpadding="2" class="selection">
398				<thead>
399					<tr>
400						<th class="ascending">' . _('Code') . '</th>
401						<th class="ascending">' . _('Customer Name') . '</th>
402						<th class="ascending">' . _('Branch') . '</th>
403						<th class="ascending">' . _('Contact') . '</th>
404						<th class="ascending">' . _('Type') . '</th>
405						<th class="ascending">' . _('Phone') . '</th>
406						<th class="ascending">' . _('Fax') . '</th>
407						<th class="ascending">' . _('Email') . '</th>
408					</tr>
409				</thead>';
410		$RowIndex = 0;
411	}// end if NOT producing a CSV file
412	if(DB_num_rows($result) <> 0) {
413		if(isset($_POST['CSV'])) {// producing a CSV file of customers
414			$FileName = $_SESSION['reports_dir'] . '/Customer_Listing_' . date('Y-m-d') . '.csv';
415			echo '<br /><p class="page_title_text"><a href="' . $FileName . '">' . _('Click to view the csv Search Result') . '</p>';
416			$fp = fopen($FileName, 'w');
417			while ($myrow2 = DB_fetch_array($result)) {
418				fwrite($fp, $myrow2['debtorno'] . ',' . str_replace(',', '', $myrow2['name']) . ',' . str_replace(',', '', $myrow2['address1']) . ',' . str_replace(',', '', $myrow2['address2']) . ',' . str_replace(',', '', $myrow2['address3']) . ',' . str_replace(',', '', $myrow2['address4']) . ',' . str_replace(',', '', $myrow2['contactname']) . ',' . str_replace(',', '', $myrow2['typename']) . ',' . $myrow2['phoneno'] . ',' . $myrow2['faxno'] . ',' . $myrow2['email'] . "\n");
419			}// end loop through customers returned
420		}// end if producing a CSV
421		if(!isset($_POST['CSV'])) {
422			DB_data_seek($result, ($_POST['PageOffset'] - 1) * $_SESSION['DisplayRecordsMax']);
423		}
424		$i = 0;// counter for input controls
425		echo '<tbody>';
426		while (($myrow = DB_fetch_array($result)) AND ($RowIndex <> $_SESSION['DisplayRecordsMax'])) {
427			echo '<tr class="striped_row">
428				<td><button type="submit" name="SubmitCustomerSelection[', htmlspecialchars($myrow['debtorno'], ENT_QUOTES, 'UTF-8', false), ']" value="', htmlspecialchars($myrow['branchcode'], ENT_QUOTES, 'UTF-8', false), '" >', $myrow['debtorno'], ' ', $myrow['branchcode'], '</button></td>
429				<td class="text">', htmlspecialchars($myrow['name'], ENT_QUOTES, 'UTF-8', false), '</td>
430				<td class="text">', htmlspecialchars($myrow['brname'], ENT_QUOTES, 'UTF-8', false), '</td>
431				<td class="text">', $myrow['contactname'], '</td>
432				<td class="text">', $myrow['typename'], '</td>
433				<td class="text">', $myrow['phoneno'], '</td>
434				<td class="text">', $myrow['faxno'], '</td>
435				<td><a href="mailto://'.$myrow['email'].'">' . $myrow['email']. '</a></td>
436			</tr>';
437			$i++;
438			$RowIndex++;
439			// end of page full new headings if
440		}// end loop through customers
441		echo '</tbody>';
442		echo '</table>';
443		echo '<input type="hidden" name="JustSelectedACustomer" value="Yes" />';
444	}// end if there are customers to show
445}// end if results to show
446
447if(!isset($_POST['CSV'])) {
448	if(isset($ListPageMax) AND $ListPageMax > 1) {
449		echo '<br /><div class="centre">&nbsp;&nbsp;' . $_POST['PageOffset'] . ' ' . _('of') . ' ' . $ListPageMax . ' ' . _('pages') . '. ' . _('Go to Page') . ': ';
450		echo '<select name="PageOffset2">';
451		$ListPage = 1;
452		while ($ListPage <= $ListPageMax) {
453			if($ListPage == $_POST['PageOffset']) {
454				echo '<option value="' . $ListPage . '" selected="selected">' . $ListPage . '</option>';
455			}// $ListPage == $_POST['PageOffset']
456			else {
457				echo '<option value="' . $ListPage . '">' . $ListPage . '</option>';
458			}
459			$ListPage++;
460		}// $ListPage <= $ListPageMax
461		echo '</select>
462			<input type="submit" name="Go2" value="' . _('Go') . '" />
463			<input type="submit" name="Previous" value="' . _('Previous') . '" />
464			<input type="submit" name="Next" value="' . _('Next') . '" />';
465		echo '</div>';
466	}// end if results to show
467}
468
469echo '</form>';
470
471// Only display the geocode map if the integration is turned on, AND there is a latitude/longitude to display
472if(isset($_SESSION['CustomerID']) AND $_SESSION['CustomerID'] != '') {
473
474	if($_SESSION['geocode_integration'] == 1) {
475
476		$SQL = "SELECT * FROM geocode_param WHERE 1";
477		$ErrMsg = _('An error occurred in retrieving the information');
478		$result = DB_query($SQL, $ErrMsg);
479		if(DB_num_rows($result) == 0) {
480			prnMsg( _('You must first setup the geocode parameters') . ' ' . '<a href="' . $RootPath . '/GeocodeSetup.php">' . _('here') . '</a>', 'error');
481			include('includes/footer.php');
482			exit;
483		}
484		$myrow = DB_fetch_array($result);
485		$API_key = $myrow['geocode_key'];
486		$center_long = $myrow['center_long'];
487		$center_lat = $myrow['center_lat'];
488		$map_height = $myrow['map_height'];
489		$map_width = $myrow['map_width'];
490		$map_host = $myrow['map_host'];
491		if($map_host == '') {$map_host = 'maps.googleapis.com';}// If $map_host is empty, use a default map host.
492
493		$SQL = "SELECT
494					debtorsmaster.debtorno,
495					debtorsmaster.name,
496					custbranch.branchcode,
497					custbranch.brname,
498					custbranch.lat,
499					custbranch.lng,
500					custbranch.braddress1,
501					custbranch.braddress2,
502					custbranch.braddress3,
503					custbranch.braddress4
504				FROM debtorsmaster
505				LEFT JOIN custbranch
506					ON debtorsmaster.debtorno = custbranch.debtorno
507				WHERE debtorsmaster.debtorno = '" . $_SESSION['CustomerID'] . "'
508					AND custbranch.branchcode = '" . $_SESSION['BranchCode'] . "'
509				ORDER BY debtorsmaster.debtorno";
510		$ErrMsg = _('An error occurred in retrieving the information');
511		$result2 = DB_query($SQL, $ErrMsg);
512		$myrow2 = DB_fetch_array($result2);
513		$Lat = $myrow2['lat'];
514		$Lng = $myrow2['lng'];
515
516		if($Lat == 0 and $myrow2["braddress1"] != '' and $_SESSION['BranchCode'] != '') {
517			$delay = 0;
518			$base_url = "https://" . $map_host . "/maps/api/geocode/xml?address=";
519
520			$geocode_pending = true;
521			while ($geocode_pending) {
522				$address = urlencode($myrow2["braddress1"] . "," . $myrow2["braddress2"] . "," . $myrow2["braddress3"] . "," . $myrow2["braddress4"]);
523				$id = $myrow2["branchcode"];
524				$debtorno =$myrow2["debtorno"];
525				$request_url = $base_url . $address . '&key=' . $API_key . '&sensor=true';
526
527				$buffer = file_get_contents($request_url)/* or die("url not loading")*/;
528				$xml = simplexml_load_string($buffer);
529				// echo $xml->asXML();
530
531				$status = $xml->status;
532				if(strcmp($status, "OK") == 0) {
533					$geocode_pending = false;
534
535					$Lat = $xml->result->geometry->location->lat;
536					$Lng = $xml->result->geometry->location->lng;
537
538					$query = sprintf("UPDATE custbranch " .
539							" SET lat = '%s', lng = '%s' " .
540							" WHERE branchcode = '%s' " .
541						" AND debtorno = '%s' LIMIT 1;",
542							($Lat),
543							($Lng),
544							($id),
545							($debtorno));
546					$update_result = DB_query($query);
547
548					if($update_result == 1) {
549						prnMsg( _('GeoCode has been updated for CustomerID') . ': ' . $id . ' - ' . _('Latitude') . ': ' . $Lat . ' ' . _('Longitude') . ': ' . $Lng ,'info');
550					}
551				} else {
552					$geocode_pending = false;
553					prnMsg(_('Unable to update GeoCode for CustomerID') . ': ' . $id . ' - ' . _('Received status') . ': ' . $status , 'error');
554				}
555				usleep($delay);
556			}
557		}
558
559		echo '<br />';
560		if($Lat == 0) {
561			echo '<div class="centre">' . _('Mapping is enabled, but no Mapping data to display for this Customer.') . '</div>';
562		} else {
563			echo '<table cellpadding="4">
564				<thead>
565					<tr>
566						<th style="width:auto">', _('Customer Mapping'), '</th>
567					</tr>
568					<tr>
569						<th style="width:auto">', _('Mapping is enabled, Map will display below.'), '</th>
570					</tr>
571				</thead>
572				<tbody>
573					<tr>
574						<td><div class="centre" id="map" style="height:', $map_height . 'px; margin: 0 auto; width:', $map_width, 'px;"></div></td>
575					</tr>
576				</tbody>
577				</table>';
578
579		// Reference: Google Maps JavaScript API V3, https://developers.google.com/maps/documentation/javascript/reference.
580	    echo '
581<script type="text/javascript">
582var map;
583function initMap() {
584
585	var myLatLng = {lat: ', $Lat, ', lng: ', $Lng, '};', /* Fills with customer's coordinates. */'
586
587	var map = new google.maps.Map(document.getElementById(\'map\'), {', /* Creates the map with the road map view. */'
588		center: myLatLng,
589		mapTypeId: google.maps.MapTypeId.ROADMAP,
590		zoom: 14
591	});
592
593	var contentString =', /* Fills the content to be displayed in the InfoWindow. */'
594		\'<div style="overflow: auto;">\' +
595		\'<div><b>', $BranchName, '</b></div>\' +
596		\'<div>', $myrow2['braddress1'], '</div>\' +
597		\'<div>', $myrow2['braddress2'], '</div>\' +
598		\'<div>', $myrow2['braddress3'], '</div>\' +
599		\'<div>', $myrow2['braddress4'], '</div>\' +
600		\'</div>\';
601
602	var infowindow = new google.maps.InfoWindow({', /* Creates an info window to display the content of 'contentString'. */'
603		content: contentString,
604		maxWidth: 250
605	});
606
607	var marker = new google.maps.Marker({', /* Creates a marker to identify a location on the map. */'
608		position: myLatLng,
609		map: map,
610		title: \'', $CustomerName, '\'
611	});
612
613	marker.addListener(\'click\', function() {', /* Creates the event clicking the marker to display the InfoWindow. */'
614		infowindow.open(map, marker);
615	});
616}
617</script>
618<script src="https://', $map_host , '/maps/api/js?key=', $API_key, '&callback=initMap"></script>';
619		}
620
621	}// end if Geocode integration is turned on
622
623	// Extended Customer Info only if selected in Configuration
624	if($_SESSION['Extended_CustomerInfo'] == 1) {
625		if($_SESSION['CustomerID'] != '') {
626			$SQL = "SELECT debtortype.typeid,
627							debtortype.typename
628						FROM debtorsmaster INNER JOIN debtortype
629					ON debtorsmaster.typeid = debtortype.typeid
630					WHERE debtorsmaster.debtorno = '" . $_SESSION['CustomerID'] . "'";
631			$ErrMsg = _('An error occurred in retrieving the information');
632			$result = DB_query($SQL, $ErrMsg);
633			$myrow = DB_fetch_array($result);
634			$CustomerType = $myrow['typeid'];
635			$CustomerTypeName = $myrow['typename'];
636			// Customer Data
637			echo '<br />';
638			// Select some basic data about the Customer
639			$SQL = "SELECT debtorsmaster.clientsince,
640						(TO_DAYS(date(now())) - TO_DAYS(date(debtorsmaster.clientsince))) as customersincedays,
641						(TO_DAYS(date(now())) - TO_DAYS(date(debtorsmaster.lastpaiddate))) as lastpaiddays,
642						debtorsmaster.paymentterms,
643						debtorsmaster.lastpaid,
644						debtorsmaster.lastpaiddate,
645						currencies.decimalplaces AS currdecimalplaces
646					FROM debtorsmaster INNER JOIN currencies
647					ON debtorsmaster.currcode=currencies.currabrev
648					WHERE debtorsmaster.debtorno ='" . $_SESSION['CustomerID'] . "'";
649			$DataResult = DB_query($SQL);
650			$myrow = DB_fetch_array($DataResult);
651			// Select some more data about the customer
652			$SQL = "SELECT sum(ovamount+ovgst) as total
653					FROM debtortrans
654					WHERE debtorno = '" . $_SESSION['CustomerID'] . "'
655					AND type !=12";
656			$Total1Result = DB_query($SQL);
657			$row = DB_fetch_array($Total1Result);
658			echo '<table cellpadding="4" style="width: 45%;">
659				<tr>
660					<th colspan="3" style="width:auto">', _('Customer Data'), '</th>
661				</tr>
662				<tr>
663					<td class="select" valign="top">';
664			/* Customer Data */
665			if($myrow['lastpaiddate'] == 0) {
666				echo _('No receipts from this customer.'), '</td>
667					<td class="select">&nbsp;</td>
668					<td class="select">&nbsp;</td>
669				</tr>';
670			} else {
671				echo _('Last Paid Date'), ':</td>
672					<td class="select"><b>' . ConvertSQLDate($myrow['lastpaiddate']), '</b></td>
673					<td class="select">', $myrow['lastpaiddays'], ' ', _('days'), '</td>
674				</tr>';
675			}
676			echo '<tr>
677					<td class="select">', _('Last Paid Amount (inc tax)'), ':</td>
678					<td class="select"><b>', locale_number_format($myrow['lastpaid'], $myrow['currdecimalplaces']), '</b></td>
679					<td class="select">&nbsp;</td>
680				</tr>';
681			echo '<tr>
682					<td class="select">', _('Customer since'), ':</td>
683					<td class="select"><b>', ConvertSQLDate($myrow['clientsince']), '</b></td>
684					<td class="select">', $myrow['customersincedays'], ' ', _('days'), '</td>
685				</tr>';
686			if($row['total'] == 0) {
687				echo '<tr>
688						<td class="select"><b>', _('No Spend from this Customer.'), '</b></td>
689						<td class="select">&nbsp;</td>
690						<td class="select">&nbsp;</td>
691					</tr>';
692			} else {
693				echo '<tr>
694						<td class="select">' . _('Total Spend from this Customer (inc tax)') . ':</td>
695						<td class="select"><b>' . locale_number_format($row['total'], $myrow['currdecimalplaces']) . '</b></td>
696						<td class="select"></td>
697						</tr>';
698			}
699			echo '<tr>
700					<td class="select">', _('Customer Type'), ':</td>
701					<td class="select"><b>', $CustomerTypeName, '</b></td>
702					<td class="select">&nbsp;</td>
703				</tr>';
704			echo '</table>';
705		}// end if $_SESSION['CustomerID'] != ''
706
707		// Customer Contacts
708		$SQL = "SELECT * FROM custcontacts
709				WHERE debtorno='" . $_SESSION['CustomerID'] . "'
710				ORDER BY contid";
711		$result = DB_query($SQL);
712
713		if(DB_num_rows($result) <> 0) {
714			echo '<br /><div class="centre"><img src="' . $RootPath . '/css/' . $Theme . '/images/group_add.png" title="' . _('Customer Contacts') . '" alt="" />' . ' ' . _('Customer Contacts') . '</div>';
715			echo '<br /><table width="45%">
716 					<thead>
717						<tr>
718							<th class="ascending">' . _('Name') . '</th>
719							<th class="ascending">' . _('Role') . '</th>
720							<th class="ascending">' . _('Phone Number') . '</th>
721							<th class="ascending">' . _('Email') . '</th>
722							<th class="text">' . _('Statement') . '</th>
723							<th class="text">', _('Notes'), '</th>
724							<th class="noprint">', _('Edit'), '</th>
725							<th class="noprint">' . _('Delete') . '</th>
726						</tr>
727					</thead>
728					<tfoot>
729						<tr>
730							<th colspan="7"><a href="AddCustomerContacts.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">', _('Add New Contact'), '</a></th>
731						</tr>
732					</tfoot>
733					<tbody>';
734
735			while ($myrow = DB_fetch_array($result)) {
736				echo '<tr class="striped_row">
737					<td>' , $myrow[2] , '</td>
738					<td>' , $myrow[3] , '</td>
739					<td>' , $myrow[4] , '</td>
740					<td><a href="mailto:' , $myrow[6] , '">' , $myrow[6] . '</a></td>
741					<td>' , ($myrow[7]==0) ? _('No') : _('Yes'), '</td>
742					<td>' , $myrow[5] , '</td>
743					<td><a href="AddCustomerContacts.php?Id=' , $myrow[0] , '&amp;DebtorNo=' , $myrow[1] , '">' , _('Edit') , '</a></td>
744					<td><a href="AddCustomerContacts.php?Id=' , $myrow[0] , '&amp;DebtorNo=' , $myrow[1] , '&amp;delete=1">' , _('Delete') , '</a></td>
745					</tr>';
746			}// END WHILE LIST LOOP
747
748			// Customer Branch Contacts if selected
749			if(isset ($_SESSION['BranchCode']) AND $_SESSION['BranchCode'] != '') {
750				$SQL = "SELECT
751							branchcode,
752							brname,
753							contactname,
754							phoneno,
755							email
756						FROM custbranch
757						WHERE debtorno='" . $_SESSION['CustomerID'] . "'
758							AND branchcode='" . $_SESSION['BranchCode'] . "'";
759				$result2 = DB_query($SQL);
760				$BranchContact = DB_fetch_row($result2);
761
762				echo '<tr class="striped_row">
763						<td>' . $BranchContact[2] . '</td>
764						<td>' . _('Branch Contact') . ' ' . $BranchContact[0] . '</td>
765						<td>' . $BranchContact[3] . '</td>
766						<td><a href="mailto:' . $BranchContact[4] . '">' . $BranchContact[4] . '</a></td>
767						<td colspan="3"></td>
768					</tr>';
769			}
770			echo '</tbody>
771			</table>';
772		}// end if there are contact rows returned
773		else {
774			if($_SESSION['CustomerID'] != '') {
775				echo '<br /><div class="centre"><img src="' . $RootPath . '/css/' . $Theme . '/images/group_add.png" title="' . _('Customer Contacts') . '" alt="" /><a href="AddCustomerContacts.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">' . ' ' . _('Add New Contact') . '</a></div>';
776			}
777		}
778		// Customer Notes
779		$SQL = "SELECT
780					noteid,
781					debtorno,
782					href,
783					note,
784					date,
785					priority
786				FROM custnotes
787				WHERE debtorno='" . $_SESSION['CustomerID'] . "'
788				ORDER BY date DESC";
789		$result = DB_query($SQL);
790		if(DB_num_rows($result) <> 0) {
791			echo '<br /><div class="centre"><img src="' . $RootPath . '/css/' . $Theme . '/images/note_add.png" title="' . _('Customer Notes') . '" alt="" />' . ' ' . _('Customer Notes') . '</div><br />';
792			echo '<table style="width: 45%;">
793				<thead>
794					<tr>
795					<th class="ascending">' . _('Date') . '</th>
796					<th>' . _('Note') . '</th>
797					<th>' . _('Hyperlink') . '</th>
798					<th class="ascending">' . _('Priority') . '</th>
799					<th>' . _('Edit') . '</th>
800					<th>' . _('Delete') . '</th>
801					<th> <a href="AddCustomerNotes.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">' . ' ' . _('Add New Note') . '</a> </th>
802					</tr>
803				</thead>
804				<tbody>';
805
806			while ($myrow = DB_fetch_array($result)) {
807				echo '<tr class="striped_row">
808					<td>' . ConvertSQLDate($myrow['date']) . '</td>
809					<td>' . $myrow['note'] . '</td>
810					<td><a href="' . $myrow['href'] . '">' . $myrow['href'] . '</a></td>
811					<td>' . $myrow['priority'] . '</td>
812					<td><a href="AddCustomerNotes.php?Id=' . $myrow['noteid'] . '&amp;DebtorNo=' . $myrow['debtorno'] . '">' . _('Edit') . '</a></td>
813					<td><a href="AddCustomerNotes.php?Id=' . $myrow['noteid'] . '&amp;DebtorNo=' . $myrow['debtorno'] . '&amp;delete=1">' . _('Delete') . '</a></td>
814					</tr>';
815			}// END WHILE LIST LOOP
816			echo '</tbody></table>';
817		}// end if there are customer notes to display
818		else {
819			if($_SESSION['CustomerID'] != '') {
820				echo '<br /><div class="centre"><img src="' . $RootPath . '/css/' . $Theme . '/images/note_add.png" title="' . _('Customer Notes') . '" alt="" /><a href="AddCustomerNotes.php?DebtorNo=', urlencode($_SESSION['CustomerID']), '">' . ' ' . _('Add New Note for this Customer') . '</a></div>';
821			}
822		}
823		// Custome Type Notes
824		$SQL = "SELECT * FROM debtortypenotes
825				WHERE typeid='" . $CustomerType . "'
826				ORDER BY date DESC";
827		$result = DB_query($SQL);
828		if(DB_num_rows($result) <> 0) {
829			echo '<br /><div class="centre"><img src="' . $RootPath . '/css/' . $Theme . '/images/folder_add.png" title="' . _('Customer Type (Group) Notes') . '" alt="" />' . ' ' . _('Customer Type (Group) Notes for:' . '<b> ' . $CustomerTypeName . '</b>') . '</div><br />';
830			echo '<table style="width: 45%;">
831				<thead>
832					<tr>
833				 	<th class="ascending">' . _('Date') . '</th>
834					<th>' . _('Note') . '</th>
835					<th>' . _('File Link / Reference / URL') . '</th>
836					<th class="ascending">' . _('Priority') . '</th>
837					<th>' . _('Edit') . '</th>
838					<th>' . _('Delete') . '</th>
839					<th><a href="AddCustomerTypeNotes.php?DebtorType=' . $CustomerType . '">' . _('Add New Group Note') . '</a></th>
840					</tr>
841				</thead>
842				<tbody>';
843
844			while ($myrow = DB_fetch_array($result)) {
845				echo '<tr class="striped_row">
846					<td>' . $myrow[4] . '</td>
847					<td>' . $myrow[3] . '</td>
848					<td>' . $myrow[2] . '</td>
849					<td>' . $myrow[5] . '</td>
850					<td><a href="AddCustomerTypeNotes.php?Id=' . $myrow[0] . '&amp;DebtorType=' . $myrow[1] . '">' . _('Edit') . '</a></td>
851					<td><a href="AddCustomerTypeNotes.php?Id=' . $myrow[0] . '&amp;DebtorType=' . $myrow[1] . '&amp;delete=1">' . _('Delete') . '</a></td>
852					</tr>';
853			}// END WHILE LIST LOOP
854			echo '</tbody></table>';
855		}// end if there are customer group notes to display
856		else {
857			if($_SESSION['CustomerID'] != '') {
858				echo '<br /><div class="centre"><img src="' . $RootPath . '/css/' . $Theme . '/images/folder_add.png" title="' . _('Customer Group Notes') . '" alt="" /><a href="AddCustomerTypeNotes.php?DebtorType=' . $CustomerType . '">' . ' ' . _('Add New Group Note') . '</a></div><br />';
859			}
860		}
861	}// end if Extended_CustomerInfo is turned on
862}// end if isset($_SESSION['CustomerID']) AND $_SESSION['CustomerID'] != ''
863include('includes/footer.php');
864?>
865