From: <dai...@us...> - 2016-10-07 09:21:47
|
Revision: 7643 http://sourceforge.net/p/web-erp/reponame/7643 Author: daintree Date: 2016-10-07 09:21:45 +0000 (Fri, 07 Oct 2016) Log Message: ----------- Eatong: Format the ManualAPITutorial.html for easier reading Modified Paths: -------------- trunk/doc/Change.log trunk/doc/Manual/ManualAPITutorial.html trunk/doc/Manual/ManualContributors.html trunk/doc/Manual/style/manual.css trunk/sql/mysql/upgrade4.13-4.13.1.sql Modified: trunk/doc/Change.log =================================================================== --- trunk/doc/Change.log 2016-10-06 02:13:21 UTC (rev 7642) +++ trunk/doc/Change.log 2016-10-07 09:21:45 UTC (rev 7643) @@ -1,5 +1,8 @@ webERP Change Log +05/10/16 Eatong: Format the ManualAPITutorial.html for easier reading. +05/10/16 Eatong: Add CSS rule for <pre> for easier reading. +05/10/16 Eatong: Align field length of salesanalysis.salesperson to salesman.salesmancode. 05/10/16 RChacon: Add cross.svg, next.svg, previous.svg, and tick.svg images in Scalable Vector Graphics (SVG) format for general use (any size). 04/10/16 RChacon: In class.pdf.php, fix the function addJpegFromFile() use of the functionality Image() of TCPDF class. 04/10/16 RChacon: In class.pdf.php, functions Rectangle() and RoundRectangle() use the functionalities Rect() and RoundedRectXY() of TCPDF class. @@ -10,7 +13,7 @@ 24/10/16 Simon Kelly: Fixed placing POs for sales orders using array form variable 24/09/16 waynemcdougall: Fixed missing date in Sales Price history 24/09/16 Exson: Make Justify feature workable in addTextWrap in class.pdf.php. -24/09/16 Exson: Fixed the AddTextWrap missing characters errors when there is space and make it more reliable. +24/09/16 Exson: Fixed the AddTextWrap missing characters errors when there is space and make it more reliable. 21/09/16 RChacon: In SuppWhereAlloc.php, accepts the payment multiple creditors. In CustWhereAlloc.php, accepts the receipt of multiple debtors. 18/09/16 RChacon: Add style to describe how button image should be displayed. Clean up Xenos css. 18/09/16 Exson: Add multiple items issue for non-controlled items feature to Work Orders in WorkOrderIssue.php. @@ -22,7 +25,7 @@ 04/09/16 Exson: Fixed the undefined noise in WorkOrderStatus.php. 04/09/16 Exson: Fixed the bug that work order location will be wrong when user select location which is not user's default location. 04/09/16 Dave Parrish fixed accumulated No of orders bug in SalesByTypePeriodInquiry.php. -31/08/16 Exson: Addd new feature assign cash from one tab to another. +31/08/16 Exson: Addd new feature assign cash from one tab to another. 24/08/16 Exson: Fixed the latin1 charset mixed bug in supplierdiscounts table; 24/9/16 Exson: Fixed the bug that days of payment terms in the following month over 31 days can not be handled correctly in DateFunctions.inc. 20/08/16 RChacon: In ConfirmDispatch_Invoice.php, fix table html code. Modified: trunk/doc/Manual/ManualAPITutorial.html =================================================================== --- trunk/doc/Manual/ManualAPITutorial.html 2016-10-06 02:13:21 UTC (rev 7642) +++ trunk/doc/Manual/ManualAPITutorial.html 2016-10-07 09:21:45 UTC (rev 7643) @@ -1,447 +1,430 @@ -<a id="APITutorial"><h1>Application Programming Interface</h1></a> - -<h2> API - Getting Started</h2> -<p> -webERP comes with a simple to use and flexible API that allows client programs to access webERP in a safe and secure manner. If you wish to write an application that accesses webERP, either to post transactions, or to extract information, then you should use the API, rather than try to access the webERP database directly, as the API makes sure that the integrity of the data is maintained. The API is an "<i>Application Program Interface</i>", that is intended to expose functionality to external programs. There are currently a number of low level functions it exposes to enable external applications to retrieve data and to update or insert data. However the API was structured in a manner that allows other protocols to be used very easily. All that would need to be done to use the SOAP protocol for instance would be to create an api_soap.php file with the same functions as the api_xmlrpc.php file. -</p> -The API uses the XML-RPC protocol to communicate between the client and the server. This was chosen because it is lightweight, simple, and easy to use. The API uses the XML-RPC for PHP - also called the phpxmlrpc class from Useful Inc originally developed by Edd Dumbill. This is an external library that is bundled with webERP code ready to run. Whilst the standard PHP XML-RPC extension could have been used, the extension is often not installed by default, so would add another dependency to webERP and complexity for the setup and installation of webERP. -</p> -<p>XML-RPC is a protocl to use XML to make RPC - remote procedure calls.</p> -<p> -Simply put the XML-RPC call is XML that contains the method of the remote procedure call together with any parameters and their data types and is sent over http as a POST to the XML-RPC server - the server returns an XML payload containing the results of the call. The parameters sent to the methods can contain arrays and associative arrays of data.</p> -<p> -The clever thing about XML-RPC is that it is the simplest protocol around for doing web-services. The newer and MUCH more complex SOAP - Simple Object Access Protocol - is quite involved and complicated. is founded on the KISS principle.</p> -<p> -In fact the XML-RPC "Server" in is just the script http://www.yourdomain.com//api/api_xml-rpc.php</p> -<p>There is no daemon background process running continuously to field calls to the "server" it is just a script that is http posted to by the XML-RPC call sending the XML encoded method to be run together with the necessary parameters to the API - the server script runs the API php functions exposed by the xml-rpc methods and returns the XML-RPC response as an XML payload. The phpxmlrpc class does the packaging converting the PHP variables and arrays to the XML required for the XML-RPC call and also has the functions to convert the XML response into something useable in PHP without having to write the XML parsing routines.</p> -<p>There is one hardcoded parameter that needs to be set in the api before you start to use it. The database name - the company database - to use with the api is defined in the file api/api_php.php - the variable -<blockquote><i>$api_DatabaseName="demo";</i></blockquote> -<p>should be set before attempting to use the api.</p> -<p> -It is worthwhile reading a how-to on XML-RPC with PHP which explains in more detail what is going on as a primer for the concepts.</p> -<p> -The beauty of XML-RPC is that the client calling the XML-RPC server and performing native functions can be called from any language (with XML-RPC bindings). I have used Vala, Genie and Python. Python particularly has been very straight forward as it has an xmlrpclib bundled with it. Of course a PHP client is also possible and is demonstrated below.</p> -<p> -The API help is actually produced by an xml-rpc call to the API using the system.listMethods method (this is a phpxmlrpc method - not a API method). Aother system xml-rpc method of phpxmlrpc class is used to return the details of each method"s parameters required. So the help file not only documents each of the API methods it is itself and illustration of how the API can be used!!</p> -<p>In the narrative below, the word "<i>Server</i>" is used to refer to the host webERP installation - in fact the <i>"server"</i> this is the script webERP/xml-rpc/api_xmlrpc.php</p> -<p>The API is configured by default to use the company database weberpdemo, and this is hard coded in the script api/api_php.php on line 6: -<blockquote><i> -$api_DatabaseName="weberpdemo"; -</i></blockquote> -<p>This database should be changed manually before the API can be used to the company database that you wish the API to access. Note that the API can only work on one company in a webERP installation. This is a limitation of the design.</p> -<p>Below is a simple example of how to use the API.</p> -<p>It is a simple <i>client</i> (a consumer of the API) application where the stock quantity for the item DVD-TOPGUN is retrieved from the webERP installation using the API. -<p> -<hr> -<blockquote><i> -echo "Test API"; -<br /> -<br />//the xmlrpc class can output some funny warnings so make sure notices are turned off error_reporting (E_ALL & ~E_NOTICE); -<br /> -<br />/* you need to include the phpxmlrpc class - see link above - copy the whole directory structure of the class over to your client application from the /xmlrpc directory */ -<br /> -<br />include ("xmlrpc/lib/xmlrpc.inc"); -<br /> -<br />//if your install is on a server at http://www.yourdomain.com/ -<br /> -<br />$ServerURL = "http://www.yourdomain.com//api/api_xml-rpc.php"; -<br /> -<br />$DebugLevel = 0; //Set to 0,1, or 2 with 2 being the highest level of debug info -<br /> -<br />$Parameters = array(); -<br /> -<br />/* The trap for me was that each parameter needs to be run through xmlrpcval() - to create the necessary xml required for the rpc call if one of the parameters required is an array then it needs to be processing into xml for the rpc call through php_xmlrpc_encode()*/ -<br /> -<br />$Parameters["StockID"] = new xmlrpcval("DVD-TOPGUN"); //the stockid of the item we wish to know the balance for -<br /> -<br />//assuming the demo username and password will work ! -<br /> -<br />$Parameters["Username"] = new xmlrpcval("admin"); -<br /> -<br />$Parameters["Password"] = new xmlrpcval(""); -<br /> -<br />$Msg = new xmlrpcmsg(".xmlrpc_GetStockBalance", $Parameters); -<br /> -<br />$Client = new xmlrpc_client($ServerURL); -<br /> -<br />$Client->setDebug($DebugLevel); -<br /> -<br />$Response = $Client->send($Msg); -<br /> -<br />$Answer = php_xmlrpc_decode($Response->value()); -<br /> -<br />if ($Answer[0]!=0){ //then the API returned some errors need to figure out what went wrong -<br /> -<br /><blockquote>//need to figure out how to return all the error descriptions associated with the codes</blockquote> -<br /> -<br />} else { //all went well the returned data is in $answer[1] -<br /> -<br /><blockquote>//answer will be an array of the locations and quantity on hand for DVD_TOPGUN so we need to run through the array to print out -<br /> -<br />for ($i=0; $i < sizeof($Answer[1]);$i++) { -<br /> -<br /><blockquote>echo "" . $Answer[1][$i]["loccode"] . " has " . $Answer[1][$i]["quantity"] . " on hand";</blockquote> -<br /> -<br />} -<br /></blockquote> -<br />} -</blockquote></i> -<hr /> -<p> -To create invoices in you need to use the following methods:</p> -<p> -InsertOrderHeader InsertOrderLine - potentially multiple times for all the lines on the order then InvoiceSalesOrder - to invoice sales orders directly assuming the entire order is delivered - it cannot deal with controlled stock items though. However, it does process invoices in much the same way as standard with updates to the stock quantities dispatched, GL entries and records required to record taxes and sales analysis records.</p> -<p> -To create a credit note just a sinlge API call is required:</p> -<p> -CreateCreditNote - to create a credit note from some base header data and an array of line items (as an associative array. In the same way as the InvoiceSalesOrder function this does all the same processing as a standard credit note from the interface in .</p> -<p> -There are some example scripts on the wiki showing how a number of the API XML-RPC functions are called - these scripts should be put on a web-server outside a installation - all you need to do is edit the config.inc file to give the system your username and password and the URL of your installation you wish to connect to. As always playing with the examples helps to figure out how it all works.</p> -<br /> -<h2>An Example Client</h2> -<p>For this example we will build a small PHP application that will first interrogate webERP for a full list of available stock locations, build them into an HTML drop down list and then allow a user to input a stock item code and return the quantity of stock of that item at the selected location. We will use PHP for simplicity but any language that has an xmlrpc library (just about every language) can be used to write a client.</p> -<p>Firstly we need the xmlrpc library, so we copy the xmlrpc sub-directory from webERP to this new project.</p> -<p>The basic code will look like this, and be saved into a file called index.php:</p> -<blockquote><i> -<html> - <head> - <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - </head> - <body> - <form action="index.html" method="post"> - Stock Code:<input type="text" name="StockID" /><br /> - Location:<select name="location"> - <?php // Here will go the available stock locations from webERP?> - </select><br /> - <input type="submit" name="submit" value="Submit" /> - </form> - </body> -</html> -</blockquote></i> -<p>As its name suggests, the xmlrpc function calls are made by sending an XML file with the function name and the parameters to the server, and receive an XML file back from the server.</p> -<p>To assist with this, the phpxmlrpc library that webERP uses (and we will use as well for our client) contains methods to encode our function call as XML, and to decode the XML that we receive back.</p> -<p>First off we need to include the xmlrpc library in our file, so immediately above the HTML, we need the following:</p> -<blockquote><i> -<?php - include "xmlrpc/lib/xmlrpc.inc"; - $xmlrpc_internalencoding="UTF-8"; - include "xmlrpc/lib/xmlrpcs.inc"; -?> -</blockquote></i> -<p>To populate the drop down box with the stock locations in it the API function called webERP.xmlrpc_GetLocationList() is used. This function takes two parameters, a valid userid for the webERP instance, and the password for that user. Using the demo credentials is admin/weberp. Also, the file api_php.php must have the $api_databasename set to whatever the name of the database is on your target webERP installation.</p> -<p>The function to get the inventory locations will look like this, and be at the bottom of the file, within a PHP code section (ie <?php ?>).</p> -<blockquote><i> -<br /> function GetLocations() { -<br /> -<br /> //Encode the user/password combination -<br /> $UserID = php_xmlrpc_encode("admin"); -<br /> $Password = php_xmlrpc_encode("webERP"); -<br /> -<br /> //Create a client object to use for xmlrpc call -<br /> $Client = new xmlrpc_client("http://localhost/webERP/api/api_xml-rpc.php"); -<br /> -<br /> //Create a message object, containing the parameters and the function name -<br /> $Message = new xmlrpcmsg("webERP.xmlrpc_GetLocationList", array($UserID, $Password)); -</blockquote></i> -<p>The most common error just return no authorisation with no information about why this has happened. Most webERP API/XML-RPC calls returns an array containing two elements, the first - $Response[0] containing an integer code, and the second the result of the call, if one is expected. If the integer code is zero, this indicates success. Any other code indicates an error. These are the errors and the codes that are returned to represent them: -<blockquote><i> -<br />NoAuthorisation - 1 -<br />IncorrectDebtorNumberLength - 1000 -<br />DebtorNoAlreadyExists - 1001 -<br />IncorrectDebtorNameLength - 1002 -<br />InvalidAddressLine - 1003 -<br />CurrencyCodeNotSetup - 1004 -<br />SalesTypeNotSetup - 1005 -<br />InvalidClientSinceDate - 1006 -<br />HoldReasonNotSetup - 1007 -<br />PaymentTermsNotSetup - 1008 -<br />InvalidDiscount - 1009 -<br />InvalidPaymentDiscount - 1010 -<br />InvalidLastPaid - 1011 -<br />InvalidLastPaidDate - 1012 -<br />InvalidCreditLimit - 1013 -<br />InvalidInvAddrBranch - 1014 -<br />InvalidDiscountCode - 1015 -<br />InvalidEDIInvoices - 1016 -<br />InvalidEDIOrders - 1017 -<br />InvalidEDIReference - 1018 -<br />InvalidEDITransport - 1019 -<br />InvalidEDIAddress - 1020 -<br />InvalidEDIServerUser - 1021 -<br />InvalidEDIServerPassword - 1022 -<br />InvalidTaxRef - 1023 -<br />InvalidCustomerPOLine - 1024 -<br />DatabaseUpdateFailed - 1025 -<br />NoDebtorNumber - 1026 -<br />DebtorDoesntExist - 1027 -<br />IncorrectBranchNumberLength - 1028 -<br />BranchNoAlreadyExists - 1029 -<br />IncorrectBranchNameLength - 1030 -<br />InvalidEstDeliveryDays - 1031 -<br />AreaCodeNotSetup - 1032 -<br />SalesmanCodeNotSetup - 1033 -<br />InvalidFwdDate - 1034 -<br />InvalidPhoneNumber - 1035 -<br />InvalidFaxNumber - 1036 -<br />InvalidContactName - 1037 -<br />InvalidEmailAddress - 1038 -<br />LocationCodeNotSetup - 1039 -<br />TaxGroupIdNotSetup - 1040 -<br />ShipperNotSetup - 1041 -<br />InvalidDeliverBlind - 1042 -<br />InvalidDisableTrans - 1043 -<br />InvalidSpecialInstructions - 1044 -<br />InvalidCustBranchCode - 1045 -<br />BranchNoDoesntExist - 1046 -<br />StockCodeDoesntExist - 1047 -<br />StockCategoryDoesntExist - 1048 -<br />IncorrectStockDescriptionLength - 1049 -<br />IncorrectUnitsLength - 1050 -<br />IncorrectMBFlag - 1051 -<br />InvalidCurCostDate - 1052 -<br />InvalidActualCost - 1053 -<br />InvalidLowestLevel - 1054 -<br />InvalidDiscontinued - 1055 -<br />InvalidEOQ - 1056 -<br />InvalidVolume - 1057 -<br />InvalidKgs - 1058 -<br />IncorrectBarCodeLength - 1059 -<br />IncorrectDiscountCategory - 1060 -<br />TaxCategoriesDoesntExist - 1061 -<br />InvalidSerialised - 1062 -<br />IncorrectAppendFile - 1063 -<br />InvalidPerishable - 1064 -<br />InvalidDecmalPlaces - 1065 -<br />IncorrectLongStockDescriptionLength - 1066 -<br />StockCodeAlreadyExists - 1067 -<br />TransactionNumberAlreadyExists - 1068 -<br />InvalidTranDate - 1069 -<br />InvalidSettled - 1070 -<br />IncorrectReference - 1071 -<br />IncorrectTpe - 1072 -<br />InvalidOrderNumbers - 1073 -<br />InvalidExchangeRate - 1074 -<br />InvalidOVAmount - 1075 -<br />InvalidOVGst - 1076 -<br />InvalidOVFreight - 1077 -<br />InvalidDiffOnExchange - 1078 -<br />InvalidAllocation - 1079 -<br />IncorrectInvoiceText - 1080 -<br />InvalidShipVia - 1081 -<br />InvalidEdiSent - 1082 -<br />InvalidConsignment - 1083 -<br />InvalidLastCost - 1084 -<br />InvalidMaterialCost - 1085 -<br />InvalidLabourCost - 1086 -<br />InvalidOverheadCost - 1087 -<br />InvalidCustomerRef - 1088 -<br />InvalidBuyerName - 1089 -<br />InvalidComments - 1090 -<br />InvalidOrderDate - 1091 -<br />InvalidDeliverTo - 1092 -<br />InvalidFreightCost - 1094 -<br />InvalidDeliveryDate - 1095 -<br />InvalidQuotationFlag - 1096 -<br />OrderHeaderNotSetup - 1097 -<br />InvalidUnitPrice - 1098 -<br />InvalidQuantity - 1099 -<br />InvalidDiscountPercent - 1100 -<br />InvalidNarrative - 1101 -<br />InvalidItemDueDate - 1102 -<br />InvalidPOLine - 1103 -<br />GLAccountCodeAlreadyExists - 1104 -<br />IncorrectAccountNameLength - 1105 -<br />AccountGroupDoesntExist - 1106 -<br />GLAccountSectionAlreadyExists - 1107 -<br />IncorrectSectionNameLength - 1108 -<br />GLAccountGroupAlreadyExists - 1109 -<br />GLAccountSectionDoesntExist - 1110 -<br />InvalidPandL - 1111 -<br />InvalidSequenceInTB - 1112 -<br />GLAccountGroupDoesntExist - 1113 -<br />InvalidLatitude - 1114 -<br />InvalidLongitude - 1115 -<br />CustomerTypeNotSetup - 1116 -<br />NoPricesSetup - 1117 -<br />InvalidInvoicedQuantity - 1118 -<br />InvalidActualDispatchDate - 1119 -<br />InvalidCompletedFlag - 1120 -<br />InvalidCategoryID - 1121 -<br />InvalidCategoryDescription - 1122 -<br />InvalidStockType - 1123 -<br />GLAccountCodeDoesntExists - 1124 -<br />StockCategoryAlreadyExists - 1125 -<br />SupplierNoAlreadyExists - 1126 -<br />IncorrectSupplierNameLength - 1127 -<br />InvalidSupplierSinceDate - 1128 -<br />InvalidBankAccount - 1129 -<br />InvalidBankReference - 1130 -<br />InvalidBankPartics - 1131 -<br />InvalidRemittanceFlag - 1132 -<br />FactorCompanyNotSetup - 1133 -<br />SupplierNoDoesntExists - 1134 -<br />InvalidSuppliersUOM - 1135 -<br />InvalidConversionFactor - 1136 -<br />InvalidSupplierDescription - 1137 -<br />InvalidLeadTime - 1138 -<br />InvalidPreferredFlag - 1139 -<br />StockSupplierLineDoesntExist - 1140 -<br />InvalidRequiredByDate - 1141 -<br />InvalidStartDate - 1142 -<br />InvalidCostIssued - 1143 -<br />InvalidQuantityRequired - 1144 -<br />InvalidQuantityReceived - 1145 -<br />InvalidStandardCost - 1146 -<br />IncorrectSerialNumber - 1147 -<br />WorkOrderDoesntExist - 1148 -<br />InvalidIssuedQuantity - 1149 -<br />InvalidTransactionDate - 1150 -<br />InvalidReceivedQuantity - 1151 -<br />ItemNotControlled - 1152 -<br />ItemSerialised - 1153 -<br />BatchNumberDoesntExist - 1154 -<br />BatchIsEmpty - 1155 -<br />NoSuchArea - 1156 -<br />NoSuchSalesMan - 1157 -<br />NoCompanyRecord - 1158 -<br />NoReadOrder - 1159 -<br />NoReadOrderLines - 1160 -<br />NoTaxProvince - 1161 -<br />TaxRatesFailed - 1162 -<br />NoReadCustomerBranch - 1163 -<br />NoReadItem - 1164 -<br />MustBeReceiptOrCreditNote - 1165 -<br />NoTransactionToAllocate - 1166 -</blockquote></i> - -<p>As you can see error code 1 indicates "NoAuthorisation" which will be the error returned if the user name or password is incorrect and also if the name of the database to be used in the api call is not specified correctly in the file webERP/api/api_php.php in the variable:</p> -<blockquote><i> -$api_DatabaseName="weberpdemo"; -</blockquote></i> -<p>To catch the errors we create a session variable to hold any error messages that happen, so that we can show the to the user. So the initialisation code at the top of index.php becomes:</p> - -<blockquote><i><?php -<br />include "xmlrpc/lib/xmlrpc.inc"; -<br />$xmlrpc_internalencoding="UTF-8"; -<br />include "xmlrpc/lib/xmlrpcs.inc"; -<br />$_SESSION["Errors"] = array(); -?> -</i></blockquote> -<p>and then at the bottom of the output we have a loop to output these errors:</p> -<blockquote><i> -<br />foreach ($_SESSION["Errors"] as $Error) { -<br />echo $Error; -<br />} -</i></blockquote> -<br />Now we just need to capture that error. We need to put this code at the bottom of the GetLocations() function so that it now reads: -<blockquote><i> -<br />if ($ReturnValue[0] == 0) { -<br /> return $ReturnValue[1]; -<br />} elseif ($ReturnValue[0] == 1) { -<br /> $_SESSION["Errors"][] = "Incorrect login/password credentials used"; -<br />} -</i></blockquote> -<br />Now run the index.php script again in your browser and you should get output similar to this: -<br /> -<br />We just need to put this code at the bottom of our other functions, and then they will all be able to catch this error. -<br /> -<br />Now if we put the proper password back in index.php should work as before. -<br /> -<br />Now try entering a stock code that you know doesn"t exist and see what happens. I entered a part code called "wrong" and this is what I see. -<br /> -<br />This is not very helpful output so we need catch this error. A quick look here shows that error code 1047 is "StockCodeDoesntExist" and this should be returned if the code we entered is wrong. So we need to capture error 1047 in the GetStockQuantity() function. The code at the end of this function now becomes: -<blockquote><i> -<br />} elseif ($ReturnValue[0] == 1) { -<br />$_SESSION["Errors"][] = "Incorrect login/password credentials used"; -<br />} elseif ($ReturnValue[0] == 1047) { -<br />$_SESSION["Errors"][] = "The stock code you entered does not exist"; -<br />} -</blockquote></i> -<br />So now the function is checking that the user/password is correct and also checking that the stock code is correct and providing useful feedback in the case of any problems. We could go on and check for other errors but this should be enough for now. -<br /> -<br />To do this we need a function much like the one we used to extract the array of location codes. Here is the full code: -<blockquote><i> -<br /> function LocationName($LocationCode) { -<br /> -<br />//Encode the data items -<br /> $UserID = php_xmlrpc_encode("admin"); -<br /> $Password = php_xmlrpc_encode("webERP"); -<br /> $Code = php_xmlrpc_encode($LocationCode); -<br /> -<br /> //Create a client object to use for xmlrpc call and set its debug level to zero -<br /> $Client = new xmlrpc_client("http://localhost/webERP/api/api_xml-rpc.php"); -<br /> $Client->setDebug(0); -<br /> -<br /> //Create a message object, containing the parameters and the function name -<br /> $Message = new xmlrpcmsg("webERP.xmlrpc_GetLocationDetails", array($Code, $UserID, $Password)); -<br /> -<br /> //Use the client object to send the message object to the server, returning the response -<br /> $Response = $Client->send($Message); -<br /> -<br /> //Decode the response and return the array -<br /> $ReturnValue = php_xmlrpc_decode($Response->value()); -<br /> if ($ReturnValue[0] == 0) { -<br /> return $ReturnValue[1]["locationname"]; -<br /> } -<br /> } -</blockquote></i> -<br /> -<p>The first section encodes the parameters as XML. The first two parameters are always the userid/password combination, and for this function call we need a third parameter, which is the code of the location that we require the name of. The second section is identical to the previous function and creates an instance of the XML-RPC client class. The third section then creates an instance of the message class, with the first parameter being the full name of the API function being called, in this case webERP.xmlrpc_GetLocationDetails, and then the second parameter is an array of the encoded parameters, (location code, userid, password). This message is then sent to the server, and the response decoded into an array called $ReturnValue.</p> -<p>As last time the first element of the array signifies whether the function was successful (a zero), or any other integer for an error code. The second element is an associative array of details for that location. The key of each element is the field name for that value. In our case we just want the location name, so we return the element ["locationname"]. If it was the telephone number we were interested in we would just return the ["tel"] element.</p> -<p>Changing the line in the HTML where we fill the drop down box to:</p> -<blockquote><i> -echo <option value=" . $LocationCode . "> . LocationName($LocationCode) . "</option>"; -</blockquote></i> -<p>The full name of the location appears in the drop down the list, but the value returned by the form is still just the code.</p> -<p>All that is left to complete our client, is to type a stock code in the text box, submit the form and return the amount of stock for that code at the chosen location. First we need to insert some PHP code in the HTML to handle the form being sent:</p> - -<blockquote><i> -<br /> if (isset($_POST["submit"])) { -<br /> echo "The quantity of " . $_POST["StockID"] . " at " . $_POST["location"] . " is : " . GetStockQuantity($_POST["StockID"], $_POST["location"]); -<br /> } -</blockquote></i> -<p>As you can see this calls another PHP function - GetStockQuantity() - that retrieves the stock quantity for the required item at the required location. Looking at the API function reference in the manual the API function we require is webERP.xmlrpc_GetStockBalance. However this time there is a small addition we require as this function returns an array containing the stock balances at all the locations for the given stock item.</p> -<p>The full code for the PHP function is:</p> -<blockquote><i> -<br />function GetStockQuantity($StockID, $LocationCode) { -<br /> //Encode the data items -<br /> $UserID = php_xmlrpc_encode("admin"); -<br /> $Password = php_xmlrpc_encode("webERP"); -<br /> $StockCode = php_xmlrpc_encode($StockID); -<br /> -<br /> //Create a client object to use for xmlrpc call and set its debug level to zero -<br /> $Client = new xmlrpc_client("http://localhost/webERP/api/api_xml-rpc.php"); -<br /> $Client->setDebug(0); -<br /> //Create a message object, containing the parameters and the function name -<br /> $Message = new xmlrpcmsg("webERP.xmlrpc_GetStockBalance", array($StockCode, $UserID, $Password)); -<br /> //Use the client object to send the message object to the server, returning the response -<br /> $Response = $Client->send($Message); -<br /> //Decode the response and return the array -<br /> $ReturnValue = php_xmlrpc_decode($Response->value()); -<br /> if ($ReturnValue[0] == 0) { -<br /> $Items = $ReturnValue[1]; -<br /> for ($i=0; $i<sizeOf($Items); $i++) { -<br /> if ($Items[$i]["loccode"]==$LocationCode) { -<br /> return $Items[$i]["quantity"]; -<br /> } -<br /> } -<br /> } -<br /> } -</blockquote></i> -<br /> -<p>I wont go through this in details as it is mostly the same as the previous functions. The key section is the last:</p> -<blockquote><i> -<br /> $ReturnValue = php_xmlrpc_decode($Response->value()); -<br /> if ($ReturnValue[0] == 0) { -<br /> $Items = $ReturnValue[1]; -<br /> for ($i=0; $i<sizeOf($Items); $i++) { -<br /> if ($Items[$i]["loccode"]==$LocationCode) { -<br /> return $Items[$i]["quantity"]; -<br /> } -<br /> } -<br /> } -</blockquote></i> -<br />Here the RPC returns an array of locations with the stock quantities for each location, and we filter out the location we need. -<br /> +<a id="APITutorial"><h1>Application Programming Interface</h1></a> + +<h2> API - Getting Started</h2> +<p> +webERP comes with a simple to use and flexible API that allows client programs to access webERP in a safe and secure manner. If you wish to write an application that accesses webERP, either to post transactions, or to extract information, then you should use the API, rather than try to access the webERP database directly, as the API makes sure that the integrity of the data is maintained. The API is an "<i>Application Program Interface</i>", that is intended to expose functionality to external programs. There are currently a number of low level functions it exposes to enable external applications to retrieve data and to update or insert data. However the API was structured in a manner that allows other protocols to be used very easily. All that would need to be done to use the SOAP protocol for instance would be to create an api_soap.php file with the same functions as the api_xmlrpc.php file. +</p> +<p> +The API uses the XML-RPC protocol to communicate between the client and the server. This was chosen because it is lightweight, simple, and easy to use. The API uses the XML-RPC for PHP - also called the phpxmlrpc class from Useful Inc originally developed by Edd Dumbill. This is an external library that is bundled with webERP code ready to run. Whilst the standard PHP XML-RPC extension could have been used, the extension is often not installed by default, so would add another dependency to webERP and complexity for the setup and installation of webERP. +</p> +<p>XML-RPC is a protocl to use XML to make RPC - remote procedure calls.</p> +<p> +Simply put the XML-RPC call is XML that contains the method of the remote procedure call together with any parameters and their data types and is sent over http as a POST to the XML-RPC server - the server returns an XML payload containing the results of the call. The parameters sent to the methods can contain arrays and associative arrays of data.</p> +<p> +The clever thing about XML-RPC is that it is the simplest protocol around for doing web-services. The newer and MUCH more complex SOAP - Simple Object Access Protocol - is quite involved and complicated. is founded on the KISS principle.</p> +<p> +In fact the XML-RPC "Server" in is just the script http://www.yourdomain.com//api/api_xml-rpc.php</p> +<p>There is no daemon background process running continuously to field calls to the "server" it is just a script that is http posted to by the XML-RPC call sending the XML encoded method to be run together with the necessary parameters to the API - the server script runs the API php functions exposed by the xml-rpc methods and returns the XML-RPC response as an XML payload. The phpxmlrpc class does the packaging converting the PHP variables and arrays to the XML required for the XML-RPC call and also has the functions to convert the XML response into something useable in PHP without having to write the XML parsing routines.</p> +<p>There is one hardcoded parameter that needs to be set in the api before you start to use it. The database name - the company database - to use with the api is defined in the file api/api_php.php - the variable +<blockquote><pre>$api_DatabaseName="demo";</pre></blockquote> +<p>should be set before attempting to use the api.</p> +<p> +It is worthwhile reading a how-to on XML-RPC with PHP which explains in more detail what is going on as a primer for the concepts.</p> +<p> +The beauty of XML-RPC is that the client calling the XML-RPC server and performing native functions can be called from any language (with XML-RPC bindings). I have used Vala, Genie and Python. Python particularly has been very straight forward as it has an xmlrpclib bundled with it. Of course a PHP client is also possible and is demonstrated below.</p> +<p> +The API help is actually produced by an xml-rpc call to the API using the system.listMethods method (this is a phpxmlrpc method - not a API method). Aother system xml-rpc method of phpxmlrpc class is used to return the details of each method"s parameters required. So the help file not only documents each of the API methods it is itself and illustration of how the API can be used!!</p> +<p>In the narrative below, the word "<i>Server</i>" is used to refer to the host webERP installation - in fact the <i>"server"</i> this is the script webERP/xml-rpc/api_xmlrpc.php</p> +<p>The API is configured by default to use the company database weberpdemo, and this is hard coded in the script api/api_php.php on line 6: +<blockquote><pre><pre> +$api_DatabaseName="weberpdemo"; +</pre></pre></blockquote> +<p>This database should be changed manually before the API can be used to the company database that you wish the API to access. Note that the API can only work on one company in a webERP installation. This is a limitation of the design.</p> +<p>Below is a simple example of how to use the API.</p> +<p>It is a simple <i>client</i> (a consumer of the API) application where the stock quantity for the item DVD-TOPGUN is retrieved from the webERP installation using the API. +<p> +<hr> +<blockquote><pre> +echo "Test API"; +//the xmlrpc class can output some funny warnings so make sure notices are turned off error_reporting (E_ALL & ~E_NOTICE); +/* you need to include the phpxmlrpc class - see link above - copy the whole directory structure of the class over to your client application from the /xmlrpc directory */ + +include ("xmlrpc/lib/xmlrpc.inc"); + +//if your install is on a server at http://www.yourdomain.com/ + +$ServerURL = "http://www.yourdomain.com//api/api_xml-rpc.php"; +$DebugLevel = 0; //Set to 0,1, or 2 with 2 being the highest level of debug info +$Parameters = array(); + +/* The trap for me was that each parameter needs to be run through xmlrpcval() - to create the necessary xml required for the rpc call if one of the parameters required is an array then it needs to be processing into xml for the rpc call through php_xmlrpc_encode()*/ +$Parameters["StockID"] = new xmlrpcval("DVD-TOPGUN"); //the stockid of the item we wish to know the balance for + +//assuming the demo username and password will work ! +$Parameters["Username"] = new xmlrpcval("admin"); +$Parameters["Password"] = new xmlrpcval(""); + +$Msg = new xmlrpcmsg(".xmlrpc_GetStockBalance", $Parameters); + +$Client = new xmlrpc_client($ServerURL); +$Client->setDebug($DebugLevel); +$Response = $Client->send($Msg); +$Answer = php_xmlrpc_decode($Response->value()); +if ($Answer[0]!=0){ //then the API returned some errors need to figure out what went wrong + //need to figure out how to return all the error descriptions associated with the codes +} else { //all went well the returned data is in $answer[1] + //answer will be an array of the locations and quantity on hand for DVD_TOPGUN so we need to run through the array to print out + for ($i=0; $i < sizeof($Answer[1]);$i++) { + echo "" . $Answer[1][$i]["loccode"] . " has " . $Answer[1][$i]["quantity"] . " on hand"; + } +} +</pre></blockquote> +<hr /> +<p> +To create invoices in you need to use the following methods:</p> +<p> +InsertOrderHeader InsertOrderLine - potentially multiple times for all the lines on the order then InvoiceSalesOrder - to invoice sales orders directly assuming the entire order is delivered - it cannot deal with controlled stock items though. However, it does process invoices in much the same way as standard with updates to the stock quantities dispatched, GL entries and records required to record taxes and sales analysis records.</p> +<p> +To create a credit note just a sinlge API call is required:</p> +<p> +CreateCreditNote - to create a credit note from some base header data and an array of line items (as an associative array. In the same way as the InvoiceSalesOrder function this does all the same processing as a standard credit note from the interface in .</p> +<p> +There are some example scripts on the wiki showing how a number of the API XML-RPC functions are called - these scripts should be put on a web-server outside a installation - all you need to do is edit the config.inc file to give the system your username and password and the URL of your installation you wish to connect to. As always playing with the examples helps to figure out how it all works.</p> +<br /> +<h2>An Example Client</h2> +<p>For this example we will build a small PHP application that will first interrogate webERP for a full list of available stock locations, build them into an HTML drop down list and then allow a user to input a stock item code and return the quantity of stock of that item at the selected location. We will use PHP for simplicity but any language that has an xmlrpc library (just about every language) can be used to write a client.</p> +<p>Firstly we need the xmlrpc library, so we copy the xmlrpc sub-directory from webERP to this new project.</p> +<p>The basic code will look like this, and be saved into a file called index.php:</p> +<blockquote><pre> +<html> + <head> + <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> + </head> + <body> + <form action="index.html" method="post"> + Stock Code:<input type="text" name="StockID" /><br /> + Location:<select name="location"> + <?php // Here will go the available stock locations from webERP?> + </select><br /> + <input type="submit" name="submit" value="Submit" /> + </form> + </body> +</html> +</pre></pre></blockquote> +<p>As its name suggests, the xmlrpc function calls are made by sending an XML file with the function name and the parameters to the server, and receive an XML file back from the server.</p> +<p>To assist with this, the phpxmlrpc library that webERP uses (and we will use as well for our client) contains methods to encode our function call as XML, and to decode the XML that we receive back.</p> +<p>First off we need to include the xmlrpc library in our file, so immediately above the HTML, we need the following:</p> +<blockquote><pre> +<?php + include "xmlrpc/lib/xmlrpc.inc"; + $xmlrpc_internalencoding="UTF-8"; + include "xmlrpc/lib/xmlrpcs.inc"; +?> +</pre></blockquote> +<p>To populate the drop down box with the stock locations in it the API function called webERP.xmlrpc_GetLocationList() is used. This function takes two parameters, a valid userid for the webERP instance, and the password for that user. Using the demo credentials is admin/weberp. Also, the file api_php.php must have the $api_databasename set to whatever the name of the database is on your target webERP installation.</p> +<p>The function to get the inventory locations will look like this, and be at the bottom of the file, within a PHP code section (ie <?php ?>).</p> +<blockquote><pre> +function GetLocations() { + + //Encode the user/password combination + $UserID = php_xmlrpc_encode("admin"); + $Password = php_xmlrpc_encode("webERP"); + + //Create a client object to use for xmlrpc call + $Client = new xmlrpc_client("http://localhost/webERP/api/api_xml-rpc.php"); + + //Create a message object, containing the parameters and the function name + $Message = new xmlrpcmsg("webERP.xmlrpc_GetLocationList", array($UserID, $Password)); +</pre></blockquote> +<p>The most common error just return no authorisation with no information about why this has happened. Most webERP API/XML-RPC calls returns an array containing two elements, the first - $Response[0] containing an integer code, and the second the result of the call, if one is expected. If the integer code is zero, this indicates success. Any other code indicates an error. These are the errors and the codes that are returned to represent them: +<blockquote><pre> +NoAuthorisation - 1 +IncorrectDebtorNumberLength - 1000 +DebtorNoAlreadyExists - 1001 +IncorrectDebtorNameLength - 1002 +InvalidAddressLine - 1003 +CurrencyCodeNotSetup - 1004 +SalesTypeNotSetup - 1005 +InvalidClientSinceDate - 1006 +HoldReasonNotSetup - 1007 +PaymentTermsNotSetup - 1008 +InvalidDiscount - 1009 +InvalidPaymentDiscount - 1010 +InvalidLastPaid - 1011 +InvalidLastPaidDate - 1012 +InvalidCreditLimit - 1013 +InvalidInvAddrBranch - 1014 +InvalidDiscountCode - 1015 +InvalidEDIInvoices - 1016 +InvalidEDIOrders - 1017 +InvalidEDIReference - 1018 +InvalidEDITransport - 1019 +InvalidEDIAddress - 1020 +InvalidEDIServerUser - 1021 +InvalidEDIServerPassword - 1022 +InvalidTaxRef - 1023 +InvalidCustomerPOLine - 1024 +DatabaseUpdateFailed - 1025 +NoDebtorNumber - 1026 +DebtorDoesntExist - 1027 +IncorrectBranchNumberLength - 1028 +BranchNoAlreadyExists - 1029 +IncorrectBranchNameLength - 1030 +InvalidEstDeliveryDays - 1031 +AreaCodeNotSetup - 1032 +SalesmanCodeNotSetup - 1033 +InvalidFwdDate - 1034 +InvalidPhoneNumber - 1035 +InvalidFaxNumber - 1036 +InvalidContactName - 1037 +InvalidEmailAddress - 1038 +LocationCodeNotSetup - 1039 +TaxGroupIdNotSetup - 1040 +ShipperNotSetup - 1041 +InvalidDeliverBlind - 1042 +InvalidDisableTrans - 1043 +InvalidSpecialInstructions - 1044 +InvalidCustBranchCode - 1045 +BranchNoDoesntExist - 1046 +StockCodeDoesntExist - 1047 +StockCategoryDoesntExist - 1048 +IncorrectStockDescriptionLength - 1049 +IncorrectUnitsLength - 1050 +IncorrectMBFlag - 1051 +InvalidCurCostDate - 1052 +InvalidActualCost - 1053 +InvalidLowestLevel - 1054 +InvalidDiscontinued - 1055 +InvalidEOQ - 1056 +InvalidVolume - 1057 +InvalidKgs - 1058 +IncorrectBarCodeLength - 1059 +IncorrectDiscountCategory - 1060 +TaxCategoriesDoesntExist - 1061 +InvalidSerialised - 1062 +IncorrectAppendFile - 1063 +InvalidPerishable - 1064 +InvalidDecmalPlaces - 1065 +IncorrectLongStockDescriptionLength - 1066 +StockCodeAlreadyExists - 1067 +TransactionNumberAlreadyExists - 1068 +InvalidTranDate - 1069 +InvalidSettled - 1070 +IncorrectReference - 1071 +IncorrectTpe - 1072 +InvalidOrderNumbers - 1073 +InvalidExchangeRate - 1074 +InvalidOVAmount - 1075 +InvalidOVGst - 1076 +InvalidOVFreight - 1077 +InvalidDiffOnExchange - 1078 +InvalidAllocation - 1079 +IncorrectInvoiceText - 1080 +InvalidShipVia - 1081 +InvalidEdiSent - 1082 +InvalidConsignment - 1083 +InvalidLastCost - 1084 +InvalidMaterialCost - 1085 +InvalidLabourCost - 1086 +InvalidOverheadCost - 1087 +InvalidCustomerRef - 1088 +InvalidBuyerName - 1089 +InvalidComments - 1090 +InvalidOrderDate - 1091 +InvalidDeliverTo - 1092 +InvalidFreightCost - 1094 +InvalidDeliveryDate - 1095 +InvalidQuotationFlag - 1096 +OrderHeaderNotSetup - 1097 +InvalidUnitPrice - 1098 +InvalidQuantity - 1099 +InvalidDiscountPercent - 1100 +InvalidNarrative - 1101 +InvalidItemDueDate - 1102 +InvalidPOLine - 1103 +GLAccountCodeAlreadyExists - 1104 +IncorrectAccountNameLength - 1105 +AccountGroupDoesntExist - 1106 +GLAccountSectionAlreadyExists - 1107 +IncorrectSectionNameLength - 1108 +GLAccountGroupAlreadyExists - 1109 +GLAccountSectionDoesntExist - 1110 +InvalidPandL - 1111 +InvalidSequenceInTB - 1112 +GLAccountGroupDoesntExist - 1113 +InvalidLatitude - 1114 +InvalidLongitude - 1115 +CustomerTypeNotSetup - 1116 +NoPricesSetup - 1117 +InvalidInvoicedQuantity - 1118 +InvalidActualDispatchDate - 1119 +InvalidCompletedFlag - 1120 +InvalidCategoryID - 1121 +InvalidCategoryDescription - 1122 +InvalidStockType - 1123 +GLAccountCodeDoesntExists - 1124 +StockCategoryAlreadyExists - 1125 +SupplierNoAlreadyExists - 1126 +IncorrectSupplierNameLength - 1127 +InvalidSupplierSinceDate - 1128 +InvalidBankAccount - 1129 +InvalidBankReference - 1130 +InvalidBankPartics - 1131 +InvalidRemittanceFlag - 1132 +FactorCompanyNotSetup - 1133 +SupplierNoDoesntExists - 1134 +InvalidSuppliersUOM - 1135 +InvalidConversionFactor - 1136 +InvalidSupplierDescription - 1137 +InvalidLeadTime - 1138 +InvalidPreferredFlag - 1139 +StockSupplierLineDoesntExist - 1140 +InvalidRequiredByDate - 1141 +InvalidStartDate - 1142 +InvalidCostIssued - 1143 +InvalidQuantityRequired - 1144 +InvalidQuantityReceived - 1145 +InvalidStandardCost - 1146 +IncorrectSerialNumber - 1147 +WorkOrderDoesntExist - 1148 +InvalidIssuedQuantity - 1149 +InvalidTransactionDate - 1150 +InvalidReceivedQuantity - 1151 +ItemNotControlled - 1152 +ItemSerialised - 1153 +BatchNumberDoesntExist - 1154 +BatchIsEmpty - 1155 +NoSuchArea - 1156 +NoSuchSalesMan - 1157 +NoCompanyRecord - 1158 +NoReadOrder - 1159 +NoReadOrderLines - 1160 +NoTaxProvince - 1161 +TaxRatesFailed - 1162 +NoReadCustomerBranch - 1163 +NoReadItem - 1164 +MustBeReceiptOrCreditNote - 1165 +NoTransactionToAllocate - 1166 +</pre></blockquote> + +<p>As you can see error code 1 indicates "NoAuthorisation" which will be the error returned if the user name or password is incorrect and also if the name of the database to be used in the api call is not specified correctly in the file webERP/api/api_php.php in the variable:</p> +<blockquote><pre> +$api_DatabaseName="weberpdemo"; +</pre></blockquote> +<p>To catch the errors we create a session variable to hold any error messages that happen, so that we can show the to the user. So the initialisation code at the top of index.php becomes:</p> + +<blockquote><pre><?php +include "xmlrpc/lib/xmlrpc.inc"; +$xmlrpc_internalencoding="UTF-8"; +include "xmlrpc/lib/xmlrpcs.inc"; +$_SESSION["Errors"] = array(); +?> +</pre></blockquote> +<p>and then at the bottom of the output we have a loop to output these errors:</p> +<blockquote><pre> +foreach ($_SESSION["Errors"] as $Error) { + echo $Error; +} +</pre></blockquote> +<br />Now we just need to capture that error. We need to put this code at the bottom of the GetLocations() function so that it now reads: +<blockquote><pre> +if ($ReturnValue[0] == 0) { + return $ReturnValue[1]; +} elseif ($ReturnValue[0] == 1) { + $_SESSION["Errors"][] = "Incorrect login/password credentials used"; +} +</pre></blockquote> +<br />Now run the index.php script again in your browser and you should get output similar to this: +<br /> +<br />We just need to put this code at the bottom of our other functions, and then they will all be able to catch this error. +<br /> +<br />Now if we put the proper password back in index.php should work as before. +<br /> +<br />Now try entering a stock code that you know doesn"t exist and see what happens. I entered a part code called "wrong" and this is what I see. +<br /> +<br />This is not very helpful output so we need catch this error. A quick look here shows that error code 1047 is "StockCodeDoesntExist" and this should be returned if the code we entered is wrong. So we need to capture error 1047 in the GetStockQuantity() function. The code at the end of this function now becomes: +<blockquote><pre> +} elseif ($ReturnValue[0] == 1) { + $_SESSION["Errors"][] = "Incorrect login/password credentials used"; +} elseif ($ReturnValue[0] == 1047) { + $_SESSION["Errors"][] = "The stock code you entered does not exist"; +} +</pre></blockquote> +<br />So now the function is checking that the user/password is correct and also checking that the stock code is correct and providing useful feedback in the case of any problems. We could go on and check for other errors but this should be enough for now. +<br /> +<br />To do this we need a function much like the one we used to extract the array of location codes. Here is the full code: +<blockquote><pre> + function LocationName($LocationCode) { + +//Encode the data items + $UserID = php_xmlrpc_encode("admin"); + $Password = php_xmlrpc_encode("webERP"); + $Code = php_xmlrpc_encode($LocationCode); + + //Create a client object to use for xmlrpc call and set its debug level to zero + $Client = new xmlrpc_client("http://localhost/webERP/api/api_xml-rpc.php"); + $Client->setDebug(0); + + //Create a message object, containing the parameters and the function name + $Message = new xmlrpcmsg("webERP.xmlrpc_GetLocationDetails", array($Code, $UserID, $Password)); + + //Use the client object to send the message object to the server, returning the response + $Response = $Client->send($Message); + + //Decode the response and return the array + $ReturnValue = php_xmlrpc_decode($Response->value()); + if ($ReturnValue[0] == 0) { + return $ReturnValue[1]["locationname"]; + } + } +</pre></blockquote> +<br /> +<p>The first section encodes the parameters as XML. The first two parameters are always the userid/password combination, and for this function call we need a third parameter, which is the code of the location that we require the name of. The second section is identical to the previous function and creates an instance of the XML-RPC client class. The third section then creates an instance of the message class, with the first parameter being the full name of the API function being called, in this case webERP.xmlrpc_GetLocationDetails, and then the second parameter is an array of the encoded parameters, (location code, userid, password). This message is then sent to the server, and the response decoded into an array called $ReturnValue.</p> +<p>As last time the first element of the array signifies whether the function was successful (a zero), or any other integer for an error code. The second element is an associative array of details for that location. The key of each element is the field name for that value. In our case we just want the location name, so we return the element ["locationname"]. If it was the telephone number we were interested in we would just return the ["tel"] element.</p> +<p>Changing the line in the HTML where we fill the drop down box to:</p> +<blockquote><pre> +echo <option value=" . $LocationCode . "> . LocationName($LocationCode) . "</option>"; +</pre></blockquote> +<p>The full name of the location appears in the drop down the list, but the value returned by the form is still just the code.</p> +<p>All that is left to complete our client, is to type a stock code in the text box, submit the form and return the amount of stock for that code at the chosen location. First we need to insert some PHP code in the HTML to handle the form being sent:</p> + +<blockquote><pre> +if (isset($_POST["submit"])) { + echo "The quantity of " . $_POST["StockID"] . " at " . $_POST["location"] . " is : " . GetStockQuantity($_POST["StockID"], $_POST["location"]); +} +</pre></blockquote> +<p>As you can see this calls another PHP function - GetStockQuantity() - that retrieves the stock quantity for the required item at the required location. Looking at the API function reference in the manual the API function we require is webERP.xmlrpc_GetStockBalance. However this time there is a small addition we require as this function returns an array containing the stock balances at all the locations for the given stock item.</p> +<p>The full code for the PHP function is:</p> +<blockquote><pre> +function GetStockQuantity($StockID, $LocationCode) { + //Encode the data items + $UserID = php_xmlrpc_encode("admin"); + $Password = php_xmlrpc_encode("webERP"); + $StockCode = php_xmlrpc_encode($StockID); + + //Create a client object to use for xmlrpc call and set its debug level to zero + $Client = new xmlrpc_client("http://localhost/webERP/api/api_xml-rpc.php"); + $Client->setDebug(0); + //Create a message object, containing the parameters and the function name + $Message = new xmlrpcmsg("webERP.xmlrpc_GetStockBalance", array($StockCode, $UserID, $Password)); + //Use the client object to send the message object to the server, returning the response + $Response = $Client->send($Message); + //Decode the response and return the array + $ReturnValue = php_xmlrpc_decode($Response->value()); + if ($ReturnValue[0] == 0) { + $Items = $ReturnValue[1]; + for ($i=0; $i < sizeOf($Items); $i++) { + if ($Items[$i]["loccode"]==$LocationCode) { + return $Items[$i]["quantity"]; + } + } + } +} +</pre></blockquote> +<br /> +<p>I wont go through this in details as it is mostly the same as the previous functions. The key section is the last:</p> +<blockquote><pre> + $ReturnValue = php_xmlrpc_decode($Response->value()); + if ($ReturnValue[0] == 0) { + $Items = $ReturnValue[1]; + for ($i=0; $i < sizeOf($Items); $i++) { + if ($Items[$i]["loccode"]==$LocationCode) { + return $Items[$i]["quantity"]; + } + } + } +</pre></blockquote> +<br />Here the RPC returns an array of locations with the stock quantities for each location, and we filter out the location we need. +<br /> <br />Showing that we have returned the correct numbers. \ No newline at end of file Modified: trunk/doc/Manual/ManualContributors.html =================================================================== --- trunk/doc/Manual/ManualContributors.html 2016-10-06 02:13:21 UTC (rev 7642) +++ trunk/doc/Manual/ManualContributors.html 2016-10-07 09:21:45 UTC (rev 7643) @@ -14,6 +14,7 @@ <p>Jan Bakke</p> <p>Chris Bice</p> <p>Danie Brink</p> + <p>Aingel Carbonell</p> <p>Daffyd Crosby</p> <p>Rafael E. Chacón</p> <p>Phil Daintree</p> Modified: trunk/doc/Manual/style/manual.css =================================================================== --- trunk/doc/Manual/style/manual.css 2016-10-06 02:13:21 UTC (rev 7642) +++ trunk/doc/Manual/style/manual.css 2016-10-07 09:21:45 UTC (rev 7643) @@ -122,3 +122,6 @@ margin-left:2%; margin-right:2%; } +pre { + font-size: 1.2em; +} \ No newline at end of file Modified: trunk/sql/mysql/upgrade4.13-4.13.1.sql =================================================================== --- trunk/sql/mysql/upgrade4.13-4.13.1.sql 2016-10-06 02:13:21 UTC (rev 7642) +++ trunk/sql/mysql/upgrade4.13-4.13.1.sql 2016-10-07 09:21:45 UTC (rev 7643) @@ -11,3 +11,5 @@ INSERT INTO scripts VALUES ('PcAssignCashTabToTab.php',12,'Assign cash from one tab to another'); ALTER table workorders ADD remark text DEFAULT NULL; ALTER table workorders ADD reference varchar(40) NOT NULL DEFAULT ''; + +ALTER TABLE `salesanalysis` CHANGE `salesperson` `salesperson` VARCHAR(4) DEFAULT '' NOT NULL; |