1<?php
2/* Import debtors by csv file */
3
4include ('includes/session.php');
5$Title = _('Import Debtors And branches');
6include ('includes/header.php');
7include ('includes/SQL_CommonFunctions.inc');
8
9if (isset($_POST['FormID'])) {
10	if (!isset($_POST['AutoDebtorNo'])) {
11		$_POST['AutoDebtorNo'] = 0;
12	} else {
13		$_POST['AutoDebtorNo'] = 1;
14	}
15	if ($_POST['AutoDebtorNo'] == 1) {
16		$_POST['UpdateIfExists'] = 0;
17	} else {
18		if (!isset($_POST['UpdateIfExists'])) {
19			$_POST['UpdateIfExists'] = 0;
20		} else {
21			$_POST['UpdateIfExists'] = 1;
22		}
23	}
24} else {
25	$_POST['AutoDebtorNo'] = $_SESSION['AutoDebtorNo'];
26	$_POST['UpdateIfExists'] = 0;
27}
28
29// If this script is called with a file object, then the file contents are imported
30// If this script is called with the gettemplate flag, then a template file is served
31// Otherwise, a file upload form is displayed
32$FieldHeadings = array('debtorno', //0
33'name', //1
34'address1', //2
35'address2', //3
36'address3', //4
37'address4', //5
38'address5', //6
39'address6', //7
40'currcode', //8
41'salestype', //9
42'clientsince', //10
43'holdreason', //11
44'paymentterms', //12
45'discount', //13
46'pymtdiscount', //14
47'lastpaid', //15
48'lastpaiddate', //16
49'creditlimit', //17
50'invaddrbranch', //18
51'discountcode', //19
52'Languageid', //20
53'ediinvoices', //21
54'ediorders', //22
55'edireference', //23
56'editransport', //24
57'ediaddress', //25
58'ediserveruser', //26
59'ediserverpwd', //27
60'taxref', //28
61'customerpoline', //29
62'typeid', //30
63'lat', //31
64'lng', //32
65'estdeliverydays', //33
66'area', //34
67'salesman', //35
68'fwddate', //36
69'phoneno', //37
70'faxno', //38
71'contactname', //39
72'email', //40
73'defaultlocation', //41
74'taxgroupid', //42
75'defaultshipvia', //43
76'deliverblind', //44
77'disabletrans', //45
78'brpostaddr1', //46
79'brpostaddr2', //47
80'brpostaddr3', //48
81'brpostaddr4', //49
82'brpostaddr5', //50
83'brpostaddr6', //51
84'specialinstructions', //52
85'custbranchcode', //53
86);
87
88if (isset($_FILES['userfile']) and $_FILES['userfile']['name']) { //start file processing
89	//initialize
90	$FieldTarget = count($FieldHeadings);
91	$InputError = 0;
92
93	//check file info
94	$FileName = $_FILES['userfile']['name'];
95	$TempName = $_FILES['userfile']['tmp_name'];
96	$FileSize = $_FILES['userfile']['size'];
97	//get file handle
98	$FileHandle = fopen($TempName, 'r');
99	//get the header row
100	$headRow = fgetcsv($FileHandle, 10000, ",");
101	//check for correct number of fields
102	if (count($headRow) != count($FieldHeadings)) {
103		prnMsg(_('File contains ' . count($headRow) . ' columns, expected ' . count($FieldHeadings) . '. Try downloading a new template.'), 'error');
104		fclose($FileHandle);
105		include ('includes/footer.php');
106		exit;
107	}
108
109	//test header row field name and sequence
110	$head = 0;
111	foreach ($headRow as $headField) {
112		if (mb_strtoupper($headField) != mb_strtoupper($FieldHeadings[$head])) {
113			prnMsg(_('File contains incorrect headers (' . mb_strtoupper($headField) . ' != ' . mb_strtoupper($header[$head]) . '. Try downloading a new template.'), 'error');
114			fclose($FileHandle);
115			include ('includes/footer.php');
116			exit;
117		}
118		$head++;
119	}
120
121	//start database transaction
122	DB_Txn_Begin();
123
124	//loop through file rows
125	$row = 1;
126	$UpdatedNum = 0;
127	$InsertNum = 0;
128	while (($filerow = fgetcsv($FileHandle, 10000, ",")) !== false) {
129
130		//check for correct number of fields
131		$fieldCount = count($filerow);
132		if ($fieldCount != $FieldTarget) {
133			prnMsg(_($FieldTarget . ' fields required, ' . $fieldCount . ' fields received'), 'error');
134			fclose($FileHandle);
135			include ('includes/footer.php');
136			exit;
137		}
138
139		// cleanup the data (csv files often import with empty strings and such)
140		foreach ($filerow as & $value) {
141			$value = trim($value);
142		}
143
144		$_POST['DebtorNo'] = $filerow[0];
145		$_POST['CustName'] = $filerow[1];
146		$_POST['Address1'] = $filerow[2];
147		$_POST['Address2'] = $filerow[3];
148		$_POST['Address3'] = $filerow[4];
149		$_POST['Address4'] = $filerow[5];
150		$_POST['Address5'] = $filerow[6];
151		$_POST['Address6'] = $filerow[7];
152		$_POST['CurrCode'] = $filerow[8];
153		$_POST['SalesType'] = $filerow[9];
154		$_POST['ClientSince'] = $filerow[10];
155		$_POST['HoldReason'] = $filerow[11];
156		$_POST['PaymentTerms'] = $filerow[12];
157		$_POST['Discount'] = $filerow[13];
158		$_POST['PymtDiscount'] = $filerow[14];
159		$_POST['lastpaid'] = $filerow[15];
160		$_POST['lastpaiddate'] = $filerow[16];
161		$_POST['CreditLimit'] = $filerow[17];
162		$_POST['InvAddrBranch'] = $filerow[18];
163		$_POST['DiscountCode'] = $filerow[19];
164		$_POST['LanguageID'] = $filerow[20];
165		$_POST['EDIInvoices'] = $filerow[21];
166		$_POST['EDIOrders'] = $filerow[22];
167		$_POST['EDIReference'] = $filerow[23];
168		$_POST['EDITransport'] = $filerow[24];
169		$_POST['EDIAddress'] = $filerow[25];
170		$_POST['EDIServerUser'] = $filerow[26];
171		$_POST['EDIServerPwd'] = $filerow[27];
172		$_POST['TaxRef'] = $filerow[28];
173		$_POST['CustomerPOLine'] = $filerow[29];
174		$_POST['typeid'] = $filerow[30];
175
176		if ($_POST['AutoDebtorNo'] == 1) {
177			$_POST['DebtorNo'] = GetNextTransNo(500);
178		} else {
179			$_POST['DebtorNo'] = mb_strtoupper($_POST['DebtorNo']);
180		}
181
182		//$_POST['DebtorNo']=$_POST['DebtorNo'];
183		$_POST['BranchCode'] = $_POST['DebtorNo'];
184		$_POST['BrName'] = $_POST['CustName'];
185		$_POST['BrAddress1'] = $_POST['Address1'];
186		$_POST['BrAddress2'] = $_POST['Address2'];
187		$_POST['BrAddress3'] = $_POST['Address3'];
188		$_POST['BrAddress4'] = $_POST['Address4'];
189		$_POST['BrAddress5'] = $_POST['Address5'];
190		$_POST['BrAddress6'] = $_POST['Address6'];
191		$Latitude = $filerow[31];
192		$Longitude = $filerow[32];
193		$_POST['EstDeliveryDays'] = $filerow[33];
194		$_POST['Area'] = $filerow[34];
195		$_POST['Salesman'] = $filerow[35];
196		$_POST['FwdDate'] = $filerow[36];
197		$_POST['PhoneNo'] = $filerow[37];
198		$_POST['FaxNo'] = $filerow[38];
199		$_POST['ContactName'] = $filerow[39];
200		$_POST['Email'] = $filerow[40];
201		$_POST['DefaultLocation'] = $filerow[41];
202		$_POST['TaxGroup'] = $filerow[42];
203		$_POST['DefaultShipVia'] = $filerow[43];
204		$_POST['DeliverBlind'] = $filerow[44];
205		$_POST['DisableTrans'] = $filerow[45];
206		$_POST['BrPostAddr1'] = $filerow[46];
207		$_POST['BrPostAddr2'] = $filerow[47];
208		$_POST['BrPostAddr3'] = $filerow[48];
209		$_POST['BrPostAddr4'] = $filerow[49];
210		$_POST['BrPostAddr5'] = $filerow[50];
211		$_POST['CustBranchCode'] = $filerow[51];
212		$_POST['SpecialInstructions'] = $filerow[52];
213
214		$i = 0;
215		if ($_POST['AutoDebtorNo'] == 0 and mb_strlen($_POST['DebtorNo']) == 0) {
216			$InputError = 1;
217			prnMsg(_('The debtor code cannot be empty'), 'error');
218			$Errors[$i] = 'DebtorNo';
219			$i++;
220		} elseif ($_POST['AutoDebtorNo'] == 0 and (ContainsIllegalCharacters($_POST['DebtorNo']) or mb_strpos($_POST['DebtorNo'], ' '))) {
221			$InputError = 1;
222			prnMsg(_('The customer code cannot contain any of the following characters') . " . - ' &amp; + \" " . _('or a space'), 'error');
223			$Errors[$i] = 'DebtorNo';
224			$i++;
225		}
226		if (mb_strlen($_POST['CustName']) > 40 or mb_strlen($_POST['CustName']) == 0) {
227			$InputError = 1;
228			prnMsg(_('The customer name must be entered and be forty characters or less long'), 'error');
229			$Errors[$i] = 'CustName';
230			$i++;
231		} elseif (mb_strlen($_POST['Address1']) > 40) {
232			$InputError = 1;
233			prnMsg(_('The Line 1 of the address must be forty characters or less long'), 'error');
234			$Errors[$i] = 'Address1';
235			$i++;
236		} elseif (mb_strlen($_POST['Address2']) > 40) {
237			$InputError = 1;
238			prnMsg(_('The Line 2 of the address must be forty characters or less long'), 'error');
239			$Errors[$i] = 'Address2';
240			$i++;
241		} elseif (mb_strlen($_POST['Address3']) > 40) {
242			$InputError = 1;
243			prnMsg(_('The Line 3 of the address must be forty characters or less long'), 'error');
244			$Errors[$i] = 'Address3';
245			$i++;
246		} elseif (mb_strlen($_POST['Address4']) > 50) {
247			$InputError = 1;
248			prnMsg(_('The Line 4 of the address must be fifty characters or less long'), 'error');
249			$Errors[$i] = 'Address4';
250			$i++;
251		} elseif (mb_strlen($_POST['Address5']) > 20) {
252			$InputError = 1;
253			prnMsg(_('The Line 5 of the address must be twenty characters or less long'), 'error');
254			$Errors[$i] = 'Address5';
255			$i++;
256		} elseif (!is_numeric(filter_number_format($_POST['CreditLimit']))) {
257			$InputError = 1;
258			prnMsg(_('The credit limit must be numeric'), 'error');
259			$Errors[$i] = 'CreditLimit';
260			$i++;
261		} elseif (!is_numeric(filter_number_format($_POST['PymtDiscount']))) {
262			$InputError = 1;
263			prnMsg(_('The payment discount must be numeric'), 'error');
264			$Errors[$i] = 'PymtDiscount';
265			$i++;
266		} elseif (!Is_Date($_POST['ClientSince'])) {
267			$InputError = 1;
268			prnMsg(_('The customer since field must be a date in the format') . ' ' . $_SESSION['DefaultDateFormat'], 'error');
269			$Errors[$i] = 'ClientSince';
270			$i++;
271		} elseif (!is_numeric(filter_number_format($_POST['Discount']))) {
272			$InputError = 1;
273			prnMsg(_('The discount percentage must be numeric'), 'error');
274			$Errors[$i] = 'Discount';
275			$i++;
276		} elseif (filter_number_format($_POST['CreditLimit']) < 0) {
277			$InputError = 1;
278			prnMsg(_('The credit limit must be a positive number'), 'error');
279			$Errors[$i] = 'CreditLimit';
280			$i++;
281		} elseif ((filter_number_format($_POST['PymtDiscount']) > 10) or (filter_number_format($_POST['PymtDiscount']) < 0)) {
282			$InputError = 1;
283			prnMsg(_('The payment discount is expected to be less than 10% and greater than or equal to 0'), 'error');
284			$Errors[$i] = 'PymtDiscount';
285			$i++;
286		} elseif ((filter_number_format($_POST['Discount']) > 100) or (filter_number_format($_POST['Discount']) < 0)) {
287			$InputError = 1;
288			prnMsg(_('The discount is expected to be less than 100% and greater than or equal to 0'), 'error');
289			$Errors[$i] = 'Discount';
290			$i++;
291		}
292
293		if (ContainsIllegalCharacters($_POST['EDIReference']) or mb_strstr($_POST['EDIReference'], ' ')) {
294			$InputError = 1;
295			prnMsg(_('The customers EDI reference code cannot contain any of the following characters') . ' - \' &amp; + \" ' . _('or a space'), 'warn');
296		}
297		if (mb_strlen($_POST['EDIReference']) < 4 and ($_POST['EDIInvoices'] == 1 or $_POST['EDIOrders'] == 1)) {
298			$InputError = 1;
299			prnMsg(_('The customers EDI reference code must be set when EDI Invoices or EDI orders are activated'), 'warn');
300			$Errors[$i] = 'EDIReference';
301			$i++;
302		}
303		if (mb_strlen($_POST['EDIAddress']) < 4 and $_POST['EDIInvoices'] == 1) {
304			$InputError = 1;
305			prnMsg(_('The customers EDI email address or FTP server address must be entered if EDI Invoices are to be sent'), 'warn');
306			$Errors[$i] = 'EDIAddress';
307			$i++;
308		}
309
310		if ($InputError != 1) {
311			$SQL = "SELECT 1 FROM debtorsmaster WHERE debtorno='" . $_POST['DebtorNo'] . "' LIMIT 1";
312			$Result = DB_query($SQL);
313			$DebtorExists = (DB_num_rows($Result) > 0);
314			if ($DebtorExists and $_POST['UpdateIfExists'] != 1) {
315				$UpdatedNum++;
316			} else {
317
318				$SQL_ClientSince = FormatDateForSQL($_POST['ClientSince']);
319
320				if ($DebtorExists) { //update
321					$UpdatedNum++;
322					$SQL = "SELECT 1
323							  FROM debtortrans
324							where debtorno = '" . $_POST['DebtorNo'] . "' LIMIT 1";
325					$Result = DB_query($SQL);
326
327					$curr = false;
328					if (DB_num_rows($Result) == 0) {
329						$curr = true;
330					} else {
331						$CurrSQL = "SELECT currcode
332							FROM debtorsmaster
333							where debtorno = '" . $_POST['DebtorNo'] . "'";
334						$CurrResult = DB_query($CurrSQL);
335						$CurrRow = DB_fetch_array($CurrResult);
336						$OldCurrency = $CurrRow[0];
337						if ($OldCurrency != $_POST['CurrCode']) {
338							prnMsg(_('The currency code cannot be updated as there are already transactions for this customer'), 'info');
339						}
340					}
341
342					$SQL = "UPDATE debtorsmaster SET
343							name='" . $_POST['CustName'] . "',
344							address1='" . $_POST['Address1'] . "',
345							address2='" . $_POST['Address2'] . "',
346							address3='" . $_POST['Address3'] . "',
347							address4='" . $_POST['Address4'] . "',
348							address5='" . $_POST['Address5'] . "',
349							address6='" . $_POST['Address6'] . "',";
350
351					if ($curr) $SQL.= "currcode='" . $_POST['CurrCode'] . "',";
352
353					$SQL.= "clientsince='" . $SQL_ClientSince . "',
354							holdreason='" . $_POST['HoldReason'] . "',
355							paymentterms='" . $_POST['PaymentTerms'] . "',
356							discount='" . filter_number_format($_POST['Discount']) / 100 . "',
357							discountcode='" . $_POST['DiscountCode'] . "',
358							pymtdiscount='" . filter_number_format($_POST['PymtDiscount']) / 100 . "',
359							creditlimit='" . filter_number_format($_POST['CreditLimit']) . "',
360							salestype = '" . $_POST['SalesType'] . "',
361							invaddrbranch='" . $_POST['AddrInvBranch'] . "',
362							taxref='" . $_POST['TaxRef'] . "',
363							customerpoline='" . $_POST['CustomerPOLine'] . "',
364							typeid='" . $_POST['typeid'] . "',
365							language_id='" . $_POST['LanguageID'] . "'
366						  WHERE debtorno = '" . $_POST['DebtorNo'] . "'";
367
368					$ErrMsg = _('The customer could not be updated because');
369					$Result = DB_query($SQL, $ErrMsg);
370
371				} else { //insert
372					$InsertNum++;
373					$SQL = "INSERT INTO debtorsmaster (
374							debtorno,
375							name,
376							address1,
377							address2,
378							address3,
379							address4,
380							address5,
381							address6,
382							currcode,
383							clientsince,
384							holdreason,
385							paymentterms,
386							discount,
387							discountcode,
388							pymtdiscount,
389							creditlimit,
390							salestype,
391							invaddrbranch,
392							taxref,
393							customerpoline,
394							typeid,
395							language_id)
396						VALUES ('" . $_POST['DebtorNo'] . "',
397							'" . $_POST['CustName'] . "',
398							'" . $_POST['Address1'] . "',
399							'" . $_POST['Address2'] . "',
400							'" . $_POST['Address3'] . "',
401							'" . $_POST['Address4'] . "',
402							'" . $_POST['Address5'] . "',
403							'" . $_POST['Address6'] . "',
404							'" . $_POST['CurrCode'] . "',
405							'" . $SQL_ClientSince . "',
406							'" . $_POST['HoldReason'] . "',
407							'" . $_POST['PaymentTerms'] . "',
408							'" . filter_number_format($_POST['Discount']) / 100 . "',
409							'" . $_POST['DiscountCode'] . "',
410							'" . filter_number_format($_POST['PymtDiscount']) / 100 . "',
411							'" . filter_number_format($_POST['CreditLimit']) . "',
412							'" . $_POST['SalesType'] . "',
413							'" . $_POST['InvAddrBranch'] . "',
414							'" . $_POST['TaxRef'] . "',
415							'" . $_POST['CustomerPOLine'] . "',
416							'" . $_POST['typeid'] . "',
417							'" . $_POST['LanguageID'] . "')";
418
419					$ErrMsg = _('This customer could not be added because');
420					$Result = DB_query($SQL, $ErrMsg);
421				}
422			}
423
424		} else {
425
426			break;
427		}
428
429		$i = 0;
430
431		if (ContainsIllegalCharacters($_POST['BranchCode']) or mb_strstr($_POST['BranchCode'], ' ')) {
432			$InputError = 1;
433			prnMsg(_('The Branch code cannot contain any of the following characters') . " -  &amp; \' &lt; &gt;", 'error');
434			$Errors[$i] = 'BranchCode';
435			$i++;
436		}
437		if (mb_strlen($_POST['BranchCode']) == 0) {
438			$InputError = 1;
439			prnMsg(_('The Branch code must be at least one character long'), 'error');
440			$Errors[$i] = 'BranchCode';
441			$i++;
442		}
443		if (!is_numeric($_POST['FwdDate'])) {
444			$InputError = 1;
445			prnMsg(_('The date after which invoices are charged to the following month is expected to be a number and a recognised number has not been entered'), 'error');
446			$Errors[$i] = 'FwdDate';
447			$i++;
448		}
449		if ($_POST['FwdDate'] > 30) {
450			$InputError = 1;
451			prnMsg(_('The date (in the month) after which invoices are charged to the following month should be a number less than 31'), 'error');
452			$Errors[$i] = 'FwdDate';
453			$i++;
454		}
455		if (!is_numeric(filter_number_format($_POST['EstDeliveryDays']))) {
456			$InputError = 1;
457			prnMsg(_('The estimated delivery days is expected to be a number and a recognised number has not been entered'), 'error');
458			$Errors[$i] = 'EstDeliveryDays';
459			$i++;
460		}
461		if (filter_number_format($_POST['EstDeliveryDays']) > 60) {
462			$InputError = 1;
463			prnMsg(_('The estimated delivery days should be a number of days less than 60') . '. ' . _('A package can be delivered by seafreight anywhere in the world normally in less than 60 days'), 'error');
464			$Errors[$i] = 'EstDeliveryDays';
465			$i++;
466		}
467
468		if ($InputError != 1) {
469			if (DB_error_no() == 0) {
470
471				$SQL = "SELECT 1
472				     FROM custbranch
473           			 WHERE debtorno='" . $_POST['DebtorNo'] . "' AND
474				           branchcode='" . $_POST['BranchCode'] . "' LIMIT 1";
475				$Result = DB_query($SQL);
476				$BranchExists = (DB_num_rows($Result) > 0);
477				if ($BranchExists and $_POST['UpdateIfExists'] != 1) {
478					//do nothing
479
480				} else {
481
482					if (!isset($_POST['EstDeliveryDays'])) {
483						$_POST['EstDeliveryDays'] = 1;
484					}
485					if (!isset($Latitude)) {
486						$Latitude = 0.0;
487						$Longitude = 0.0;
488					}
489					if ($BranchExists) {
490						$SQL = "UPDATE custbranch SET brname = '" . $_POST['BrName'] . "',
491									braddress1 = '" . $_POST['BrAddress1'] . "',
492									braddress2 = '" . $_POST['BrAddress2'] . "',
493									braddress3 = '" . $_POST['BrAddress3'] . "',
494									braddress4 = '" . $_POST['BrAddress4'] . "',
495									braddress5 = '" . $_POST['BrAddress5'] . "',
496									braddress6 = '" . $_POST['BrAddress6'] . "',
497									lat = '" . $Latitude . "',
498									lng = '" . $Longitude . "',
499									specialinstructions = '" . $_POST['SpecialInstructions'] . "',
500									phoneno='" . $_POST['PhoneNo'] . "',
501									faxno='" . $_POST['FaxNo'] . "',
502									fwddate= '" . $_POST['FwdDate'] . "',
503									contactname='" . $_POST['ContactName'] . "',
504									salesman= '" . $_POST['Salesman'] . "',
505									area='" . $_POST['Area'] . "',
506									estdeliverydays ='" . filter_number_format($_POST['EstDeliveryDays']) . "',
507									email='" . $_POST['Email'] . "',
508									taxgroupid='" . $_POST['TaxGroup'] . "',
509									defaultlocation='" . $_POST['DefaultLocation'] . "',
510									brpostaddr1 = '" . $_POST['BrPostAddr1'] . "',
511									brpostaddr2 = '" . $_POST['BrPostAddr2'] . "',
512									brpostaddr3 = '" . $_POST['BrPostAddr3'] . "',
513									brpostaddr4 = '" . $_POST['BrPostAddr4'] . "',
514									brpostaddr5 = '" . $_POST['BrPostAddr5'] . "',
515									disabletrans='" . $_POST['DisableTrans'] . "',
516									defaultshipvia='" . $_POST['DefaultShipVia'] . "',
517									custbranchcode='" . $_POST['CustBranchCode'] . "',
518									deliverblind='" . $_POST['DeliverBlind'] . "'
519								WHERE branchcode = '" . $_POST['BranchCode'] . "' AND debtorno='" . $_POST['DebtorNo'] . "'";
520
521					} else {
522
523						$SQL = "INSERT INTO custbranch (branchcode,
524										debtorno,
525										brname,
526										braddress1,
527										braddress2,
528										braddress3,
529										braddress4,
530										braddress5,
531										braddress6,
532										lat,
533										lng,
534										specialinstructions,
535										estdeliverydays,
536										fwddate,
537										salesman,
538										phoneno,
539										faxno,
540										contactname,
541										area,
542										email,
543										taxgroupid,
544										defaultlocation,
545										brpostaddr1,
546										brpostaddr2,
547										brpostaddr3,
548										brpostaddr4,
549										brpostaddr5,
550										disabletrans,
551										defaultshipvia,
552										custbranchcode,
553										deliverblind)
554								VALUES ('" . $_POST['BranchCode'] . "',
555									'" . $_POST['DebtorNo'] . "',
556									'" . $_POST['BrName'] . "',
557									'" . $_POST['BrAddress1'] . "',
558									'" . $_POST['BrAddress2'] . "',
559									'" . $_POST['BrAddress3'] . "',
560									'" . $_POST['BrAddress4'] . "',
561									'" . $_POST['BrAddress5'] . "',
562									'" . $_POST['BrAddress6'] . "',
563									'" . $Latitude . "',
564									'" . $Longitude . "',
565									'" . $_POST['SpecialInstructions'] . "',
566									'" . filter_number_format($_POST['EstDeliveryDays']) . "',
567									'" . $_POST['FwdDate'] . "',
568									'" . $_POST['Salesman'] . "',
569									'" . $_POST['PhoneNo'] . "',
570									'" . $_POST['FaxNo'] . "',
571									'" . $_POST['ContactName'] . "',
572									'" . $_POST['Area'] . "',
573									'" . $_POST['Email'] . "',
574									'" . $_POST['TaxGroup'] . "',
575									'" . $_POST['DefaultLocation'] . "',
576									'" . $_POST['BrPostAddr1'] . "',
577									'" . $_POST['BrPostAddr2'] . "',
578									'" . $_POST['BrPostAddr3'] . "',
579									'" . $_POST['BrPostAddr4'] . "',
580									'" . $_POST['BrPostAddr5'] . "',
581									'" . $_POST['DisableTrans'] . "',
582									'" . $_POST['DefaultShipVia'] . "',
583									'" . $_POST['CustBranchCode'] . "',
584									'" . $_POST['DeliverBlind'] . "')";
585					}
586
587					//run the SQL from either of the above possibilites
588					$ErrMsg = _('The branch record could not be inserted or updated because');
589					$Result = DB_query($SQL, $ErrMsg);
590
591				}
592			} else { //item insert failed so set some useful error info
593				$InputError = 1;
594				prnMsg(_($Result), 'error');
595			}
596
597		}
598
599		if ($InputError == 1) { //this row failed so exit loop
600			break;
601		}
602
603		$row++;
604	}
605
606	if ($InputError == 1) { //exited loop with errors so rollback
607		prnMsg(_('Failed on row ' . $row . '. Batch import has been rolled back.'), 'error');
608		DB_Txn_Rollback();
609	} else { //all good so commit data transaction
610		DB_Txn_Commit();
611		prnMsg(_('Batch Import of') . ' ' . $FileName . ' ' . _('has been completed. All transactions committed to the database.'), 'success');
612		if ($_POST['UpdateIfExists'] == 1) {
613			prnMsg(_('Updated:') . ' ' . $UpdatedNum . ' ' . _('Insert') . ':' . $InsertNum);
614		} else {
615			prnMsg(_('Exist:') . ' ' . $UpdatedNum . ' ' . _('Insert') . ':' . $InsertNum);
616		}
617	}
618
619	fclose($FileHandle);
620
621} elseif (isset($_POST['gettemplate']) || isset($_GET['gettemplate'])) { //download an import template
622	echo '<br /><br /><br />"' . implode('","', $FieldHeadings) . '"<br /><br /><br />';
623
624} else { //show file upload form
625	prnMsg(_('Please ensure that your csv file is encoded in UTF-8, otherwise the input data will not store correctly in database'), 'warn');
626
627	echo '
628		<br />
629		<a href="Z_ImportDebtors.php?gettemplate=1">Get Import Template</a>
630		<br />
631		<br />';
632	echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post" enctype="multipart/form-data">';
633	echo '<div class="centre">';
634	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
635
636	echo '<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />' . _('Upload file') . ': <input name="userfile" type="file" />
637			<input type="submit" value="' . _('Send File') . '" />';
638	echo '<br/>', _('Create Debtor Codes Automatically'), ':<input type="checkbox" name="AutoDebtorNo" ';
639	if ($_POST['AutoDebtorNo'] == 1) echo 'checked="checked"';
640	echo '>';
641	echo '<br/>', _('Update if DebtorNo exists'), ':<input type="checkbox" name="UpdateIfExists">';
642	echo '</div>
643		</form>';
644
645}
646
647include ('includes/footer.php');
648?>
649