Erply has some awesome api’s available to access backend data. I particularly was interested in loading a bunch of suppliers through a spreadsheet saved in CSV format. I would like a quick import button but this works great as well!
I made a quick script to load in the file. Done 😀
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | <?php /** * Used for importing Suppliers into Erply from a CSV spreadsheet. * * The format should look something like this: * * Code | First & Last Name | GroupID number | Notes * * Example: * 100 Dieula Etienne 7 ED * */ ini_set('max_execution_time', 180); session_start(); // include ERPLY API class include('EAPI.class.php'); // Initialise class $api = new EAPI(); // Configuration settings $api->url = "https://s3.erply.com/api/"; $api->clientCode = "49102"; $api->username = "<insert username>"; $api->password = "<insert password>"; //////////////////////////////////// // Supplier Groups //////////////////////////////////// function getSupplierGroups() { $supplierGroups = $api->sendRequest("getSupplierGroups", array()); $supplierGroupsOutput = json_decode($supplierGroups, true); print "<pre>"; print_r($supplierGroupsOutput); print "</pre>"; } //////////////////////////////////// // Load CSV //////////////////////////////////// function loadCSV() { $csv = array(); $lines = file('suppliers.csv', FILE_IGNORE_NEW_LINES); foreach ($lines as $key => $value) { $csv[$key] = str_getcsv($value); } echo '<pre>'; print_r($csv); echo '</pre>'; return $csv; } //////////////////////////////////// // Get Suppliers //////////////////////////////////// // Get client groups from API // No input parameters are needed $result = $api->sendRequest("getSuppliers", array("recordsOnPage" => 100)); // Default output format is JSON, so we'll decode it into a PHP array $suppliers = json_decode($result, true); //print_r($suppliers); print "<pre>"; foreach (loadCSV() as $key => $row) { $newCode = $row[0]; list($newFirst, $newLast) = explode(" ", $row[1]); $newGroupID = $row[2]; $newNote = $row[3]; print "$newFirst | $newLast | $newCode | $newGroupID | $newNote <br />"; $isFound = FALSE; $supplierId; foreach ($suppliers["records"] as $key => $value) { //print "Value code: " . strcmp($value["code"],$newCode) . " | " . $value["code"] . " | " . $newCode . " | <br />"; if(isset($value["code"]) && strcmp($value["code"],$newCode) == 0) { $isFound = TRUE; $supplierId = $value["supplierID"]; print "Found Code: $newCode for $newFirst with supplier id of $supplierId <br />"; break; } } $saveSupplier; if ($isFound) { //Update supplier $saveSupplier = array( "supplierID" => $supplierId, "firstName" => $newFirst, "lastName" => $newLast, "groupID" => $newGroupID, "notes" => $newNote, "code" => $newCode ); print "Supplier will be *updated* with code: $newCode for $newFirst <br />\n"; } else { //New Supplier $saveSupplier = array( "firstName" => $newFirst, "lastName" => $newLast, "groupID" => $newGroupID, "notes" => $newNote, "code" => $newCode ); print "New Supplier will be added with code: $newCode for $newFirst <br />\n"; } $result = $api->sendRequest("saveSupplier", $saveSupplier); $suppliers = json_decode($result, true); print_r($suppliers); break; } print "</pre>"; ?> |
The script works great but I do have an issue with names that have special characters for example: “Marléne Dessalines”. The “é” get’s cut off. I haven’t spent anytime to see if it’s a problem with CurL or if it’s an issue with the server accepting it.