Feel free to copy the files and use it on your on project!
This example illustrate the creation of an internal document of type “inventory transfers”, that allows the user to specify the both warehouses: origin warehouse
and destiny warehouse.
The example will create the product if it is not created yet at the Drive FX. The code below assume that the record of customer is already created.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
/* * Drive FX credentials */ $backendUrl = 'https://sis05.drivefx.net/xxxxxxx/PHCWS'; $appId = 'appid'; $userCode = 'userCode'; $password = 'password'; $company = ''; $tokenLifeTime = 'Never'; /* * Constants */ define('API_URL', 'http://api.drivefx.net/v3'); define('generateAccessToken', '/generateAccessToken'); //generateAccessToken endpoit define('createInternalDocument', '/createInternalDocument'); //createInternalDocument endpoit $ch = curl_init(); //#1 - First we need to get an access token to make the requests to API echo "> Requesting Token "; $accessToken = requestAccessToken($ch); if($accessToken == null){ exit(1); }else{ echo "Access Token Generated Successfully! "; } /* * Once we acquire access token, we can now use all the API as wish */ //#2 - Get Invoices of Type "FATURA" (check DriveFX app for 'Tipo de Documentos de Faturacao' to know your Document type number for request) echo "> Requesting create Internal Document "; $internalDocument = '{ "requestOptions": { "option":1, "requestedFields":[ "no", "obranome" ], "reportName":"Encomenda de Cliente Minimal" }, "internalDocument":{ "docType": 7, "customerNumber": 112314, "documentObservations": "Transferencia de armazem via API" }, "products":[ { "reference":"234567", "designation":"Camel", "unitCode":"M", "unitPrice":15.55, "discount1":20, "discount2":0, "quantity":1, "tax":"tax", "taxIncluded":true, "taxPercentage":23, "taxRegion":"PT", "warehouse" : 1, "targetWarehouse" : 2 } ] }'; $creationMessage = createInternalDocument($ch, $accessToken, $internalDocument); print_r($creationMessage); echo " "; /* A - Access Token */ function requestAccessToken($ch){ global $backendUrl, $appId, $userCode, $password, $company, $tokenLifeTime; //#1 - Specify endpoint of service $url = API_URL . generateAccessToken; //#2 - Specify request Params $jsonObj = '{ "credentials": { "backendUrl": "' .$backendUrl. '", "appId": "' .$appId. '", "userCode": "' .$userCode. '", "password": "' .$password. '", "company": "' .$company. '", "tokenLifeTime": "' .$tokenLifeTime. '" } }'; $response=doAPIRequestToken($ch, $url,$jsonObj); if($response['code'] == 100){ echo "Error on getAccessToken "; return null; } return $response['token']; } /* B - Create Internal Document */ function createInternalDocument($ch, $token, $internalDocumentObj){ //#1 - Specify endpoint of service $url = API_URL . createInternalDocument; //#2 - Specify request Params $jsonObj = $internalDocumentObj; $response=doAPIRequest($ch, $url,$jsonObj); if($response['code'] == 100){ echo "Error on creating Internal Document "; return null; } return $response['message']; } /* * Generic API request */ function doAPIRequestToken($ch, $url, $params){ // Build Http query using params curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type: application/json")); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); $response = curl_exec($ch); // send response as JSON return json_decode($response, true); } function doAPIRequest($ch, $url, $params){ global $accessToken; // Build Http query using params curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-type: application/json", "Authorization: " .$accessToken)); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); $response = curl_exec($ch); // send response as JSON return json_decode($response, true); } |
Has the basic classes and fields needed to sucessfully create a document:
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json.Serialization; using System.IO; namespace CreateDocumentExample { class Program { static void Main(string[] args) { // Required token in order to call the API. Can be created by calling the generateAcessToken with the user credentials string authorizationToken = ""; // Creating the required objects for the request Customer customer = Customer.New(0, "Test", "Address1", "1234-567", "City", "PT", "test@test.com"); RequestOptions requestOptions = RequestOptions.New(1, new String[] { "fno", "ftstamp", "etotal", "ettiva" }); Document document = Document.New(1, "Gary", "Invoice Address 1", "2578-152", "Invoice Locality", DateTime.Now, "Document Observations"); Products[] products = new Products[] { Products.New("ZST001", "M", 15.55, 3), Products.New("F001", "M", 9, 1) }; // Adding the created objects to CreateDocument CreateDocument createDocument = CreateDocument.New(customer, requestOptions, document, products); callCreateDocumentEndpoint(authorizationToken, createDocument).Wait(); } protected static async Task callCreateDocumentEndpoint(String authorizationToken, CreateDocument createDocument) { try { HttpClient client_ = new HttpClient(); client_.DefaultRequestHeaders.Accept.Clear(); client_.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); // Setting the Authorization header with the authoriztion token that must be fetched with the generateAccessToken endpoint client_.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authorizationToken); HttpResponseMessage response = new HttpResponseMessage(); response.EnsureSuccessStatusCode(); // Converting the CreateDocument object to string response = await client_.PostAsJsonAsync("https://api.drivefx.net/v3/createDocument", createDocument); if (response.IsSuccessStatusCode) { string data = await response.Content.ReadAsStringAsync(); Console.Write(data); } } catch (Exception e) { Console.Write("Error running DoTasks: " + e.InnerException.ToString()); } Console.Read(); } } public class CreateDocument { public Customer customer { get; set; } public RequestOptions requestOptions { get; set; } public Document document { get; set; } public Products[] products { get; set; } public static CreateDocument New(Customer customer, RequestOptions requestOptions, Document document, Products[] products) { CreateDocument document_ = new CreateDocument(); document_.customer = customer; document_.requestOptions = requestOptions; document_.document = document; document_.products = products; return document_; } } public class Customer { public Int32 number { get; set; } public String name { get; set; } public String address { get; set; } public String postalCode { get; set; } public String city { get; set; } public String country { get; set; } public String email { get; set; } public static Customer New(Int32 number, String name, String address, String postalCode, String city, String country, String email) { Customer cust_ = new Customer(); cust_.number = number; cust_.name = name; cust_.address = address; cust_.postalCode = postalCode; cust_.city = city; cust_.country = country; cust_.email = email; return cust_; } } public class RequestOptions { public Int32 option { get; set; } public String[] requestedFields { get; set; } public static RequestOptions New(Int32 option, String[] requestedFields) { RequestOptions req_ = new RequestOptions(); req_.option = option; req_.requestedFields = requestedFields; return req_; } } public class Document { public Int32 docType { get; set; } public String salesmanName { get; set; } public String invoicingAddress1 { get; set; } public String invoicingPostalCode { get; set; } public String invoicingLocality { get; set; } public DateTime dueDate { get; set; } public String documentObservations { get; set; } public static Document New(Int32 docType, String salesmanName, String invoicingAddress1, String invoicingPostalCode, String invoicingLocality, DateTime dueDate, String documentObservations = "") { Document doc_ = new Document(); doc_.docType = docType; doc_.salesmanName = salesmanName; doc_.invoicingAddress1 = invoicingAddress1; doc_.invoicingPostalCode = invoicingPostalCode; doc_.invoicingLocality = invoicingLocality; doc_.dueDate = dueDate; doc_.documentObservations = documentObservations; return doc_; } } public class Products { public String reference { get; set; } public String unitCode { get; set; } public Double unitPrice { get; set; } public Int32 quantity { get; set; } public Int32 discount1 { get; set; } public Int32 discount2 { get; set; } public Boolean taxIncluded { get; set; } public Int32 taxPercentage { get; set; } public String taxRegion { get; set; } public static Products New(String reference, String unitCode, Double unitPrice, Int32 quantity, Int32 discount1 = 0, Int32 discount2 = 0, Boolean taxIncluded = true, Int32 taxPercentage = 23, String taxRegion = "PT") { Products prd_ = new Products(); prd_.reference = reference; prd_.unitCode = unitCode; prd_.unitPrice = unitPrice; prd_.quantity = quantity; prd_.discount1 = discount1; prd_.discount2 = discount2; prd_.taxIncluded = taxIncluded; prd_.taxPercentage = taxPercentage; prd_.taxRegion = taxRegion; return prd_; } } } |
This form was created with PHP languange and uses two endpoints of Drive FX API, Generate Access Token and Create Document.
The first endpoint (Generate Access Token) will be called when the form is initialized, in order to generate the access token that will be used every time you want to call an endpoint.
After the token is correctly generated, the form can be filled and when you submit the form it will be called
the endpoint Create Document to create an invoice with that data.
This example was created with PHP languange and uses four endpoints of Drive FX API:
The first endpoint will be called when the example is initialized, in order to generate the access token that will be used every time you want to call an endpoint.
Then, the second endpoint will create an instance in “Artigos e Serviços” and we can change the values as we like.
At the third endpoint, it will saves the values changed before and provides the information at Drive FX.
Finally, the fourth endpoint allows us to search for an entity created before.
This example was created with PHP languange and uses exactly the same four endpoints of Drive FX API of the previous example:
The first endpoint will be called when the example is initialized, in order to generate the access token that will be used every time you want to call an endpoint.
Then, the second endpoint will create an instance in “Clientes” and we can change the values as we like.
At the third endpoint, it will saves the values changed before and provides the information at Drive FX.
Finally, the fourth endpoint allows us to search for an entity created before.
This example was created with PHP languange and uses four endpoints of Drive FX API:
The first endpoint will be called when the example is initialized, in order to generate the access token that will be used every time you want to call an endpoint.
Then, the second endpoint will search through the entities in “Taxas Iva” and “Miseimp” and associate the codes of tabiva to the miseimpstamp.
At the third endpoint, we will create an instance that allows us to fetch the information in an Excel file that we created and then create a new ware or service.
Finally, the fourth endpoint it will saves the values changed before and provides the information at Drive FX.