1<?php
2
3
4include('includes/session.php');
5$Title = _('Import Stock Items');
6include('includes/header.php');
7include('xmlrpc/lib/xmlrpc.inc');
8include('api/api_errorcodes.php');
9
10$webERPUser = $_SESSION['UserID'];
11$sql="SELECT password FROM www_users WHERE userid='" . $webERPUser."'";
12$result=DB_query($sql);
13$myrow=DB_fetch_array($result);
14$weberppassword = $myrow[0];
15
16$ServerURL = 'http://'. $_SERVER['HTTP_HOST'] . $RootPath . '/api/api_xml-rpc.php';
17$DebugLevel = 0; //Set to 0,1, or 2 with 2 being the highest level of debug info
18
19
20if (isset($_POST['update'])) {
21	$fp = fopen($_FILES['ImportFile']['tmp_name'], "r");
22   	$buffer = fgets($fp, 4096);
23   	$FieldNames = explode(',', $buffer);
24   	$SuccessStyle='style="color:green; font-weight:bold"';
25   	$FailureStyle='style="color:red; font-weight:bold"';
26   	echo '<table>
27			<tr>
28				<th>' .  _('Part Code')  . '</th>
29				<th>' .  _('Result') . '</th>
30				<th>' .  _('Comments')  . '</th>
31			</tr>';
32   	$successes=0;
33   	$failures=0;
34 	while (!feof ($fp)) {
35    	$buffer = fgets($fp, 4096);
36    	$FieldValues = explode(',', $buffer);
37    	if ($FieldValues[0]!='') {
38    		for ($i=0; $i<sizeof($FieldValues); $i++) {
39    			$ItemDetails[$FieldNames[$i]]=$FieldValues[$i];
40    		}
41			$stockitem = php_xmlrpc_encode($ItemDetails);
42			$user = new xmlrpcval($webERPUser);
43			$password = new xmlrpcval($weberppassword);
44
45			$msg = new xmlrpcmsg("weberp.xmlrpc_InsertStockItem", array($stockitem, $user, $password));
46
47			$client = new xmlrpc_client($ServerURL);
48			$client->setDebug($DebugLevel);
49
50			$response = $client->send($msg);
51			$answer = php_xmlrpc_decode($response->value());
52			if ($answer[0]==0) {
53				echo '<tr '.$SuccessStyle.'><td>' . $ItemDetails['stockid'] . '</td><td>' . 'Success' . '</td></tr>';
54				$successes++;
55			} else {
56				echo '<tr '.$FailureStyle.'><td>' . $ItemDetails['stockid'] . '</td><td>' . 'Failure' . '</td><td>';
57				for ($i=0; $i<sizeof($answer); $i++) {
58					echo 'Error no '.$answer[$i].' - '.$ErrorDescription[$answer[$i]] . '<br />';
59				}
60				echo '</td></tr>';
61				$failures++;
62			}
63    	}
64		unset($ItemDetails);
65	}
66	echo '<tr><td>' . $successes._(' records successfully imported')  . '</td></tr>';
67	echo '<tr><td>' . $failures._(' records failed to import')  . '</td></tr>';
68	echo '</table>';
69	fclose ($fp);
70} else {
71	$sql = "select * from locations";
72	$result = DB_query($sql);
73	if (DB_num_rows($result)==0) {
74		prnMsg( _('No locations have been set up. At least one location should be set up first'), "error");
75	} else {
76		prnMsg( _('Select a csv file containing the details of the parts that you wish to import into webERP. '). '<br />' .
77			 _('The first line must contain the field names that you wish to import. ').
78			 '<a href ="Z_DescribeTable.php?table=stockmaster">' . _('The field names can be found here'). '</a>', 'info');
79		echo '<form id="ItemForm" enctype="multipart/form-data" method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
80        echo '<div class="centre">';
81		echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
82		echo '<table><tr><td>' . _('File to import') . '</td>' .
83			'<td><input type="file" id="ImportFile" name="ImportFile" /></td></tr></table>';
84		echo '<div class="centre"><input type="submit" name="update" value="Process" /></div>';
85		echo '</div>
86              </form>';
87	}
88}
89
90include('includes/footer.php');
91
92?>