|
|
|
@ -4,6 +4,8 @@
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "M5LCD.h"
|
|
|
|
|
|
|
|
|
|
HTTPClient *client = new HTTPClient();
|
|
|
|
|
|
|
|
|
|
DolibarrClient::DolibarrClient(struct DolibarrConfig dolibarr_config) : dolibarr(dolibarr_config) {
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
|
Serial.println(" --- Dolibarr configuration --- ");
|
|
|
|
@ -14,7 +16,6 @@ DolibarrClient::DolibarrClient(struct DolibarrConfig dolibarr_config) : dolibarr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HTTPClient *DolibarrClient::build_url(const String& url) const {
|
|
|
|
|
auto *client = new HTTPClient();
|
|
|
|
|
String clientUrl = this->dolibarr.url + url;
|
|
|
|
|
client->begin(clientUrl);
|
|
|
|
|
client->addHeader("Content-Type", "application/json");
|
|
|
|
@ -25,40 +26,12 @@ HTTPClient *DolibarrClient::build_url(const String& url) const {
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int DolibarrClient::login() const {
|
|
|
|
|
HTTPClient *client = this->build_url(API_LOGIN_URL);
|
|
|
|
|
int httpResponseCode = client->GET();
|
|
|
|
|
if (httpResponseCode > 0) {
|
|
|
|
|
DynamicJsonDocument doc(384);
|
|
|
|
|
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
|
|
|
|
if (error) {
|
|
|
|
|
Serial.println("ERROR: ");
|
|
|
|
|
Serial.println(error.c_str());
|
|
|
|
|
delete client;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
delete client;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
delete client;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String replace_id(const char *str, const char *id) {
|
|
|
|
|
String url(str);
|
|
|
|
|
url.replace("{id}", id);
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<models::Product> *DolibarrClient::list_products() const {
|
|
|
|
|
HTTPClient *client = this->build_url(API_LIST_PRODUCT_URL);
|
|
|
|
|
if (client->GET() == HTTP_CODE_OK) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
models::Product DolibarrClient::get_product_by_factory_id(const char* factory_id) const {
|
|
|
|
|
HTTPClient *client = this->build_url(replace_id("/products?sortorder=ASC&limit=1&sqlfilters=(t.accountancy_code_sell:like:'{id}')", factory_id).c_str());
|
|
|
|
|
if (client->GET() == HTTP_CODE_OK) {
|
|
|
|
@ -67,7 +40,6 @@ models::Product DolibarrClient::get_product_by_factory_id(const char* factory_id
|
|
|
|
|
if (error) {
|
|
|
|
|
Serial.println("ERROR: ");
|
|
|
|
|
Serial.println(error.c_str());
|
|
|
|
|
delete client;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
for (auto obj : doc.as<JsonArray>()) {
|
|
|
|
@ -80,13 +52,10 @@ models::Product DolibarrClient::get_product_by_factory_id(const char* factory_id
|
|
|
|
|
product.accountancy_code_sell = obj["accountancy_code_sell"].as<std::string>();
|
|
|
|
|
product.accountancy_code_sell_export = obj["accountancy_code_sell_export"].as<std::string>();
|
|
|
|
|
product.fk_default_warehouse = obj["fk_default_warehouse"].as<std::string>();
|
|
|
|
|
delete client;
|
|
|
|
|
return product;
|
|
|
|
|
}
|
|
|
|
|
delete client;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
delete client;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -96,7 +65,6 @@ models::Product *DolibarrClient::get_product_by_id(const char* id_product) const
|
|
|
|
|
DynamicJsonDocument doc(6144);
|
|
|
|
|
DeserializationError error = deserializeJson(doc, client->getString().c_str());
|
|
|
|
|
if (error) {
|
|
|
|
|
delete client;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
auto *product = new models::Product();
|
|
|
|
@ -107,10 +75,8 @@ models::Product *DolibarrClient::get_product_by_id(const char* id_product) const
|
|
|
|
|
product->label = doc["label"].as<std::string>();
|
|
|
|
|
product->accountancy_code_sell = doc["accountancy_code_sell"].as<std::string>();
|
|
|
|
|
product->accountancy_code_sell_export = doc["accountancy_code_sell_export"].as<std::string>();
|
|
|
|
|
delete client;
|
|
|
|
|
return product;
|
|
|
|
|
}
|
|
|
|
|
delete client;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -122,7 +88,6 @@ std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
|
|
|
|
|
if (error) {
|
|
|
|
|
Serial.println("ERROR: ");
|
|
|
|
|
Serial.println(error.c_str());
|
|
|
|
|
delete client;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
auto *warehouses = new std::vector<models::Warehouse>();
|
|
|
|
@ -132,10 +97,8 @@ std::vector<models::Warehouse> *DolibarrClient::list_warehouse() const {
|
|
|
|
|
warehouse.label = obj["label"].as<std::string>();
|
|
|
|
|
warehouses->push_back(warehouse);
|
|
|
|
|
}
|
|
|
|
|
delete client;
|
|
|
|
|
return warehouses;
|
|
|
|
|
}
|
|
|
|
|
delete client;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -151,7 +114,6 @@ int DolibarrClient::create_movement(models::CreateProductStock stock) const {
|
|
|
|
|
serializeJson(doc, result);
|
|
|
|
|
Serial.println(result.c_str());
|
|
|
|
|
if (client->POST(result.c_str()) == HTTP_CODE_OK) {
|
|
|
|
|
delete client;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|