From dfbecba2a25febc3d31752512bbbf69bc97dcc11 Mon Sep 17 00:00:00 2001 From: Jean-Marc Tremeaux Date: Tue, 9 May 2023 11:29:32 +0200 Subject: [PATCH] Complete supplier invoice api --- CHANGELOG.md | 2 + README.md | 2 +- .../api/dolibarr/model/PaymentTerm.java | 8 + .../api/dolibarr/model/ProductType.java | 8 + .../ClientInvoiceValidateModel.java | 9 ++ .../SupplierInvoiceLineModel.java | 119 ++++++++++++++ .../supplierInvoice/SupplierInvoiceModel.java | 145 ++++++++++++++++++ .../SupplierInvoiceValidateModel.java | 9 ++ .../service/ClientInvoiceService.java | 4 +- .../service/SupplierInvoiceService.java | 66 ++++++++ conf/dependencies.yml | 2 +- 11 files changed, 370 insertions(+), 4 deletions(-) create mode 100755 app/helpers/api/dolibarr/model/PaymentTerm.java create mode 100755 app/helpers/api/dolibarr/model/ProductType.java create mode 100755 app/helpers/api/dolibarr/model/clientInvoice/ClientInvoiceValidateModel.java create mode 100755 app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceLineModel.java create mode 100755 app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceModel.java create mode 100755 app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceValidateModel.java diff --git a/CHANGELOG.md b/CHANGELOG.md index b8338b1..4e978de 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ ## Change Log +### Version 1.8.2 (2023-05-09) +- Complete supplier invoice API ### Version 1.8.1 (2022-02-23) - Fix client invoice payment ### Version 1.8.0 (2021-04-06) diff --git a/README.md b/README.md index 2c1e5e3..2033f19 100755 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ This plugin adds [Dolibarr](https://www.dolibarr.org/) support to Play! Framewor ``` require: - - dolibarr -> dolibarr 1.8.1 + - dolibarr -> dolibarr 1.8.2 repositories: - sismicsNexusRaw: diff --git a/app/helpers/api/dolibarr/model/PaymentTerm.java b/app/helpers/api/dolibarr/model/PaymentTerm.java new file mode 100755 index 0000000..1bd443b --- /dev/null +++ b/app/helpers/api/dolibarr/model/PaymentTerm.java @@ -0,0 +1,8 @@ +package helpers.api.dolibarr.model; + +/** + * @author jtremeaux + */ +public class PaymentTerm { + public static final Integer AFTER_30_DAYS = 2; +} diff --git a/app/helpers/api/dolibarr/model/ProductType.java b/app/helpers/api/dolibarr/model/ProductType.java new file mode 100755 index 0000000..a89f965 --- /dev/null +++ b/app/helpers/api/dolibarr/model/ProductType.java @@ -0,0 +1,8 @@ +package helpers.api.dolibarr.model; + +/** + * @author jtremeaux + */ +public class ProductType { + public static final Integer SERVICE = 1; +} diff --git a/app/helpers/api/dolibarr/model/clientInvoice/ClientInvoiceValidateModel.java b/app/helpers/api/dolibarr/model/clientInvoice/ClientInvoiceValidateModel.java new file mode 100755 index 0000000..f93f2fc --- /dev/null +++ b/app/helpers/api/dolibarr/model/clientInvoice/ClientInvoiceValidateModel.java @@ -0,0 +1,9 @@ +package helpers.api.dolibarr.model.clientInvoice; + +/** + * @author jtremeaux + */ +public class ClientInvoiceValidateModel { + public Integer idwarehouse; + public Integer notrigger; +} diff --git a/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceLineModel.java b/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceLineModel.java new file mode 100755 index 0000000..e814724 --- /dev/null +++ b/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceLineModel.java @@ -0,0 +1,119 @@ +package helpers.api.dolibarr.model.supplierInvoice; + +import java.math.BigDecimal; + +/** + * @author jtremeaux + */ +public class SupplierInvoiceLineModel { + public String ref; + public String product_ref; + public String ref_supplier; + public String product_desc; + public BigDecimal pu_ht; + public BigDecimal subprice; + public BigDecimal pu_ttc; + public Long fk_facture_fourn; + public String label; + public String description; + public String date_start; + public String date_end; + public String situation_percent; + public Long fk_prev_id; + public String vat_src_code; + public BigDecimal tva_tx; + public BigDecimal localtax1_tx; + public BigDecimal localtax2_tx; + public BigDecimal qty; + public BigDecimal remise_percent; + public BigDecimal total_ht; + public BigDecimal total_ttc; + public BigDecimal total_tva; + public BigDecimal total_localtax1; + public BigDecimal total_localtax2; + public Long fk_product; + public Integer product_type; + public String product_label; + public String info_bits; + public Long fk_remise_except; + public Long fk_parent_line; + public String special_code; + public String rang; + public String localtax1_type; + public Long localtax2_type; + public BigDecimal multicurrency_subprice; + public BigDecimal multicurrency_total_ht; + public BigDecimal multicurrency_total_tva; + public BigDecimal multicurrency_total_ttc; + public Long id; + public Long fk_unit; + public String date_debut_prevue; + public String date_debut_reel; + public String date_fin_prevue; + public String date_fin_reel; + public String weight; + public String weight_units; + public String width; + public String width_units; + public String height; + public String height_units; + public String length; + public String length_units; + public String surface; + public String surface_units; + public String volume; + public String volume_units; + public String multilangs; + public String desc; + public String product; + public String product_barcode; + public Long fk_product_type; + public String duree; + public String entity; + public String import_key; + public String contacts_ids; + public String linked_objects; + public String linkedObjectsIds; + public String canvas; + public String origin; + public String origin_id; + public String ref_ext; + public String statut; + public String status; + public String state_id; + public String region_id; + public String demand_reason_id; + public String transport_mode_id; + public String last_main_doc; + public Long fk_bank; + public Long fk_account; + public String lines; + public String date_creation; + public String date_validation; + public String date_modification; + public String date_cloture; + public String user_author; + public String user_creation; + public String user_creation_id; + public String user_valid; + public String user_validation; + public String user_validation_id; + public String user_closing_id; + public String user_modification; + public String user_modification_id; + public String specimen; + public String libelle; + public String code_ventilation; + public Long fk_accounting_account; + + public SupplierInvoiceLineModel() { + } + + public SupplierInvoiceLineModel(Integer product_type, BigDecimal pu_ht, BigDecimal tva_tx, BigDecimal qty, String description) { + this.product_type = product_type; + this.pu_ht = pu_ht; + this.tva_tx = tva_tx; + this.qty = qty; + this.description = description; + } +} diff --git a/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceModel.java b/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceModel.java new file mode 100755 index 0000000..88c6e22 --- /dev/null +++ b/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceModel.java @@ -0,0 +1,145 @@ +package helpers.api.dolibarr.model.supplierInvoice; + +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * @author jtremeaux + */ +public class SupplierInvoiceModel { + public String ref; + public String ref_supplier; + public String label; + public Integer socid; + public Long type; + public Long statut; + public Long status; + public String close_code; + public String close_note; + public String paye; + public String datec; + public String tms; + public String date; + public String date_echeance; + public String amount; + public String remise; + public String tva; + public String localtax1; + public String localtax2; + public BigDecimal total_ht; + public BigDecimal total_tva; + public String total_localtax1; + public String total_localtax2; + public BigDecimal total_ttc; + public String note_private; + public String note_public; + public String propalid; + public Integer cond_reglement_id; + public String cond_reglement_code; + public String cond_reglement_label; + public String cond_reglement_doc; + public Integer fk_account; + public Integer mode_reglement_id; + public String mode_reglement_code; + public Long transport_mode_id; +// "extraparams": [], + public List lines = new ArrayList<>(); + public String fournisseur; + public String fk_multicurrency; + public String multicurrency_code; + public BigDecimal multicurrency_tx; + public BigDecimal multicurrency_total_ht; + public BigDecimal multicurrency_total_tva; + public BigDecimal multicurrency_total_ttc; + public Long fk_facture_source; + public String fac_rec; + public String totalpaid; + public String totaldeposits; + public String totalcreditnotes; + public String sumpayed; + public String sumpayed_multicurrency; + public String sumdeposit; + public String sumdeposit_multicurrency; + public String sumcreditnote; + public String sumcreditnote_multicurrency; + public String remaintopay; + public Long id; + public Long entity; + public String import_key; +// public String array_options": [], + public String array_languages; + public String contacts_ids; + public String linked_objects; + public String linkedObjectsIds; + public String canvas; + public String fk_project; + public String contact_id; + public String user; + public String origin; + public String origin_id; + public String ref_ext; + public String country_id; + public String country_code; + public String state_id; + public String region_id; + public String demand_reason_id; + public String shipping_method_id; + public String model_pdf; + public String last_main_doc; + public String fk_bank; + public String name; + public String lastname; + public String firstname; + public String civility_id; + public String date_creation; + public String date_validation; + public String date_modification; + public String date_cloture; + public String user_author; + public String user_creation; + public String user_creation_id; + public String user_valid; + public String user_validation; + public String user_validation_id; + public String user_closing_id; + public String user_modification; + public String user_modification_id; + public Long specimen; + public Long fk_incoterms; + public String label_incoterms; + public String location_incoterms; + public String fk_soc; + public Long datep; + public String libelle; + public Long paid; + public Long fk_statut; + public Long fk_user_author; + public Long fk_user_valid; + public String fk_fac_rec_source; + public String socnom; + + public static final String DATE_SHORT_FORMAT = "yyyy-MM-dd"; + + public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormat.forPattern(DATE_SHORT_FORMAT); + + public SupplierInvoiceModel() { + } + + public SupplierInvoiceModel(String ref_supplier, Integer socid, Date date, Date dateEcheance, Integer cond_reglement_id, Integer mode_reglement_id, Integer fk_account) { + this.ref = "auto"; + this.ref_supplier = ref_supplier; + this.socid = socid; + this.date = DATE_TIME_FORMATTER.print(new DateTime(date).withZone(DateTimeZone.UTC)); + this.date_echeance = DATE_TIME_FORMATTER.print(new DateTime(dateEcheance).withZone(DateTimeZone.UTC)); + this.cond_reglement_id = cond_reglement_id; + this.mode_reglement_id = mode_reglement_id; + this.fk_account = fk_account; + } +} diff --git a/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceValidateModel.java b/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceValidateModel.java new file mode 100755 index 0000000..f2b04d0 --- /dev/null +++ b/app/helpers/api/dolibarr/model/supplierInvoice/SupplierInvoiceValidateModel.java @@ -0,0 +1,9 @@ +package helpers.api.dolibarr.model.supplierInvoice; + +/** + * @author jtremeaux + */ +public class SupplierInvoiceValidateModel { + public Integer idwarehouse; + public Integer notrigger; +} diff --git a/app/helpers/api/dolibarr/service/ClientInvoiceService.java b/app/helpers/api/dolibarr/service/ClientInvoiceService.java index 99bdea8..f6c8609 100755 --- a/app/helpers/api/dolibarr/service/ClientInvoiceService.java +++ b/app/helpers/api/dolibarr/service/ClientInvoiceService.java @@ -6,7 +6,7 @@ import helpers.api.dolibarr.DolibarrClient; import helpers.api.dolibarr.model.clientInvoice.ClientInvoiceModel; import helpers.api.dolibarr.model.clientInvoice.ClientInvoicePaymentModel; -import helpers.api.dolibarr.model.clientInvoice.InvoiceValidateModel; +import helpers.api.dolibarr.model.clientInvoice.ClientInvoiceValidateModel; import org.apache.commons.io.IOUtils; import java.io.InputStreamReader; @@ -72,7 +72,7 @@ public Integer createClientInvoice(ClientInvoiceModel clientInvoiceModel) { * @param invoiceId The invoice ID * @param invoiceValidateModel The invoice validation model */ - public String validateInvoice(Integer invoiceId, InvoiceValidateModel invoiceValidateModel) { + public String validateInvoice(Integer invoiceId, ClientInvoiceValidateModel invoiceValidateModel) { HttpRequest request = dolibarrClient.authRequest(HttpRequest.newBuilder() .uri(URI.create(dolibarrClient.getUrl("/invoices/" + invoiceId + "/validate")))) .header("Content-Type", "application/json") diff --git a/app/helpers/api/dolibarr/service/SupplierInvoiceService.java b/app/helpers/api/dolibarr/service/SupplierInvoiceService.java index 729fa5b..5ca4f5d 100755 --- a/app/helpers/api/dolibarr/service/SupplierInvoiceService.java +++ b/app/helpers/api/dolibarr/service/SupplierInvoiceService.java @@ -1,15 +1,23 @@ package helpers.api.dolibarr.service; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; import com.sismics.sapparot.string.StringUtil; import helpers.api.dolibarr.DolibarrClient; +import helpers.api.dolibarr.model.supplierInvoice.SupplierInvoiceModel; +import helpers.api.dolibarr.model.supplierInvoice.SupplierInvoiceValidateModel; import helpers.api.dolibarr.model.supplierInvoice.SupplierinvoicesAddPaymentModel; import org.apache.commons.io.IOUtils; +import java.io.InputStreamReader; +import java.lang.reflect.Type; import java.net.URI; import java.net.http.HttpRequest; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Date; +import java.util.List; /** * @author jtremeaux @@ -21,6 +29,64 @@ public SupplierInvoiceService(DolibarrClient dolibarrClient) { this.dolibarrClient = dolibarrClient; } + /** + * Get the list of supplier invoices. + * + * @return The list of supplier invoices + */ + public List listSupplierInvoice() { + HttpRequest request = dolibarrClient.authRequest(HttpRequest.newBuilder() + .uri(URI.create(dolibarrClient.getUrl("/supplierinvoices")))) + .GET() + .build(); + return dolibarrClient.execute(request, + (response) -> { + Type listType = new TypeToken>(){}.getType(); + return dolibarrClient.getGson().fromJson(new JsonReader(new InputStreamReader(response.body())), listType); + }, + (response) -> { + throw new RuntimeException("Error getting supplier invoice list, response was: " + StringUtil.toString(response.body())); + }); + } + + /** + * Create a supplier invoice. + * + * @param supplierInvoiceModel The supplier invoice model + * @return The payment ID + */ + public Integer createSupplierInvoice(SupplierInvoiceModel supplierInvoiceModel) { + HttpRequest request = dolibarrClient.authRequest(HttpRequest.newBuilder() + .uri(URI.create(dolibarrClient.getUrl("/supplierinvoices")))) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(dolibarrClient.getGson().toJson(supplierInvoiceModel))) + .build(); + return dolibarrClient.execute(request, + (response) -> Integer.parseInt(IOUtils.toString(response.body(), StandardCharsets.UTF_8.name())), + (response) -> { + throw new RuntimeException("Error creating supplier invoice, response was: " + StringUtil.toString(response.body())); + }); + } + + /** + * Validate a supplier invoice. + * + * @param invoiceId The invoice ID + * @param supplierInvoiceValidateModel The validation model + */ + public String validateInvoice(Integer invoiceId, SupplierInvoiceValidateModel supplierInvoiceValidateModel) { + HttpRequest request = dolibarrClient.authRequest(HttpRequest.newBuilder() + .uri(URI.create(dolibarrClient.getUrl("/supplierinvoices/" + invoiceId + "/validate")))) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(supplierInvoiceValidateModel))) + .build(); + return dolibarrClient.execute(request, + (response) -> null, + (response) -> { + throw new RuntimeException("Error validating invoice, response was: " + StringUtil.toString(response.body())); + }); + } + /** * Pay a supplier invoice. * diff --git a/conf/dependencies.yml b/conf/dependencies.yml index 887fbaa..d8aea0c 100755 --- a/conf/dependencies.yml +++ b/conf/dependencies.yml @@ -1,4 +1,4 @@ -self: play -> dolibarr 1.8.1 +self: play -> dolibarr 1.8.2 configurations: - provided: