Vai al contenuto principale

RESTful API - Esempio crea prodotto

Esempio di codice da utilizzare per l'estrazione dei prodotti da un database e crearli in Contabilità in cloud

Scritto da Angela Spina

Nel codice di esempio, che si trova anche nei documenti in allegato, si effettua l'estrazione di una serie di prodotti da un ipotetico database e li crea in Reviso.

Per creare un prodotto è stata usata la RESTful API:

POST /products

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reviso.Example
{
public class RevisoProduct
{
public string productNumber { get; set; } //"Unique alphanumeric
product number."
public string name { get; set; } //"Descriptive name of the
product."
public string description { get; set; } //"Free text description of product."
public decimal costPrice { get; set; } //"The cost of the goods. If you have the inventory module enabled, this is read-only and will just be ignored."
public decimal recommendedPrice { get; set; } //"Recommended retail price of the goods."
public decimal salesPrice { get; set; } //"This is the unit net price that will appear on invoice lines when a product is added to an invoice line."
public bool barred { get; set; } //"If this value is true, then the product can no longer be sold, and trying to book an invoice with this product will not be possible."
public RevisoProduct_ProductGroup productGroup { get; set; } //"A reference to the product group this product is contained within."
}
public class RevisoProduct_ProductGroup
{
public int productGroupNumber { get; set; }
}
}
private RevisoExportLog CreateProduct() 
{
.....
.....
int i = 0;

//estrae i prodotti da creare su Reviso
XPQuery queryArticolo = new XPQuery(uow);
var articoli = queryArticolo.ToList();

foreach (Articolo item in articoli)
{
//crea il nuovo prodotto su Reviso
RevisoProduct articolo = new RevisoProduct();

if (item.Codice == null || item.Codice.Length == 0)
{
i++;
articolo.productNumber = string.Concat("AUTOMATICO",
i.ToString());
}
else if (item.Codice.Length > 25)
articolo.productNumber = item.Codice.Substring(0, 25);
else
articolo.productNumber = item.Codice;
if (item.Descrizione != null && item.Descrizione.Length > 0)
articolo.name = item.Descrizione;
else
articolo.name = articolo.productNumber;
articolo.description = articolo.name;
articolo.recommendedPrice = Decimal.Round(item.Prezzo, 2);
articolo.salesPrice = articolo.recommendedPrice;
articolo.barred = false;

RevisoProduct_ProductGroup productGroup = new RevisoProduct_ProductGroup();
productGroup.productGroupNumber = GetProductGroup(item.Iva);
articolo.productGroup = productGroup;

var requestProduct = new RestRequest("products",
Method.POST);
requestProduct.AddHeader("Content-Type",
"application/json");
requestProduct.AddHeader("X-AppSecretToken",
Properties.Resources.RevisoAppSecretToken);
requestProduct.AddHeader("X-AgreementGrantToken", token);

requestProduct.AddJsonBody(articolo);

IRestResponse restResponseProduct = restClient2.Execute(requestProduct);

if (restResponseProduct != null &&
restResponseProduct.StatusCode == HttpStatusCode.Created &&
restResponseProduct.ErrorException == null)
log.AddEntry(LogEntryType.Articolo, true, articolo.name, "Creazione completata con successo!");
else
{
if (restResponseProduct != null &&
restResponseProduct.ErrorException != null)
log.AddEntry(LogEntryType.Articolo, false,
articolo.name, String.Concat("Creazione non completata. Errore: ",
restResponseProduct.ErrorException));
else
log.AddEntry(LogEntryType.Articolo, false,
articolo.name, String.Concat("Creazione non completata. Messaggio del
server: ", restResponseProduct.Content.Replace(System.Environment.NewLine, " ")));
}
}
....
....
}
private int GetProductGroup(Iva iva)
{
int iProductGroup = 0;

if (iva == null || iva.Id == null)
iProductGroup = = 99;
//Globals.REVISO_DEFAULT_PRODUCTGROUP
else
{
VATMap ivaMappata = MyVAT.Find(x => x.Id == iva.Id);
if (ivaMappata != null)
iProductGroup =
ivaMappata.NumeroGruppoProdottoReviso;
if (iProductGroup == 0)
iProductGroup = = 99; //Globals.REVISO_DEFAULT_PRODUCTGROUP
}

return iProductGroup;
}
Hai ricevuto la risposta alla tua domanda?