Posting a new customer in C# with the Winbooks.Apis.Services


        private static Guid GetId(ICriteria criteria, string type, string code)
        {
            criteria.Add(Condition.Eq("Code", code))
                .SetProjection(Projections.Property("Id"))
                .SetMaxResults(1);
            Newtonsoft.Json.Linq.JArray id = _folder.GetFilterAll(criteria.JsonCriteriaSerialize(), type);
            Guid guid = id == null ? Guid.Empty : Guid.Parse(id[0].ToString());
            return guid;
        }


        private static void PostNewCustomer()
        {
            Guid country_id = GetId(new CountryDAO().CreateCriteria(), "Country", "BE");
            Guid civility_id = GetId(new CivilityDAO().CreateCriteria(), "Civility", "NV");
            Guid language_id = GetId(new LanguageDAO().CreateCriteria(), "Language", "nl");
            Guid currency_id = GetId(new CurrencyDAO().CreateCriteria(), "Currency", "EUR");
            Guid vat_id = GetId(new VatDAO().CreateCriteria(), "Vat", "21");
            Guid customerCategory_id = GetId(new CustomerCategoryDAO().CreateCriteria(), "CustomerCategory", "BIG");
            Guid defaultAccount_Id = GetId(new GLAccountDAO().CreateCriteria(), "GLAccount", "700000");
            Guid centralAccount_Id = GetId(new GLAccountDAO().CreateCriteria(), "GLAccount", "400000");
            Guid payCode_id = GetId(new PayCodeDAO().CreateCriteria(), "PayCode", "7");

            Customer cust = new Customer();
            cust.Code = "WBG3";
            cust.VatApplicable = VatApplicable.Subject;
            cust.Vat_Id = vat_id;
            cust.CustomerCategory_Id = customerCategory_id;
            cust.GLAccount_Id = defaultAccount_Id;
            cust.GLAccount2_Id = centralAccount_Id;
            cust.PayCode_Id = payCode_id;

            cust.Third = new Third();
            cust.Third.Code = "WBG3";
            cust.Third.Name = "WINBOOKS GROUP 3";
            cust.Third.ThirdCivility_Id = civility_id;
            cust.Third.Language_Id = language_id;
            cust.Third.Currency_Id = currency_id;
            cust.Third.VatCountry_Id = country_id;
            cust.Third.VatNumber = "0000000394";
            cust.Third.Addresses = new List<Address>();

            Address adr = new Address();
            adr.Address1 = "330 Lemonierlaan";
            adr.Address2 = "";
            adr.PhoneNo = "+3223456789";
            adr.Country_Id = country_id;
            adr.Zip = "2000";
            adr.Town = "Antwerpen";
            adr.IsDelivering = true;
            adr.IsDeliveringDefault = true;
            adr.IsInvoicing = true;
            adr.IsInvoicingDefault = true;
            adr.IsPosting = true;
            adr.IsPostingDefault = true;

            cust.Third.Addresses.Add(adr);

            _folder.Insert<Customer>(cust, 3, false);

       }


Posting a new customer (with the friendly code properties)

We suggest avoiding the usage of the friendly code properties when you post a list of Customers. You should get the Id of the entities before. 

POST {{url}}/app/Customer/TEST/Folder/{{folder}}

The body of the request for version <= 2.4 of WinBooks on Web will be like this:

{
   "$type": "Winbooks.TORM.OM.Customer, Winbooks.TORM.OM",
   "TotalLevel": 4,
   "Code": "TEST",
   "VatApplicable": 1,
   "IsLocked": false,
   "IsHidden": false,
   "MemoType": 0,
   "Third": {
      "$type": "Winbooks.TORM.OM.Third, Winbooks.TORM.OM",
      "TotalLevel": 3,
      "Code": "TEST",
      "Name": "The Test Company",
      "VatNumber": "0000000196",
      "WebSite": "www.test.be",
      "Addresses": [
         {
            "$type": "Winbooks.TORM.OM.Address, Winbooks.TORM.OM",
            "TotalLevel": 2,
            "Index": 0,
            "Address1": "Vuchtlaan 78",
            "Address2": "",
            "Town": "Antwerpen",
            "Zip": "2000",
            "Name": "",
            "Number": "",
            "Box": "",
            "PhoneNo": "+3233215476",
            "FaxNo": "",
            "MemoType": 0,
            "Memo": "",
            "IsDelivering": true,
            "IsDeliveringDefault": true,
            "IsPosting": true,
            "IsPostingDefault": true,
            "IsInvoicing": true,
            "IsInvoicingDefault": true,
            "CountryCode": "BE"
         }
      ],
      "CivilityCode": "NV",
      "VatCountryCode": "BE",
      "LanguageCode": "en",
      "CurrencyCode": "EUR"
   },
   "VatCode": "21",
   "CategoryCode": "EU",
   "GLAccountDefaultCode": "700000",
   "GLAccountCentralCode": "400000",
   "PayCodeCode": "30"
}

The body for version >= 2.5 of WinBooks On Web looks like this:

{
    "$type": "Winbooks.TORM.OM.Customer, Winbooks.TORM.OM",
    "TotalLevel": 5,
    "Code": "WB6",
    "VatApplicable": 1,
    "Third": {
        "$type": "Winbooks.TORM.OM.Third, Winbooks.TORM.OM",
	    "TotalLevel": 4,
        "Code": "WB6",
        "Name": "Customer WB6",
        "VatNumber": "0000000394",
        "Addresses": [
            {
                "$type": "Winbooks.TORM.OM.Third_Address, Winbooks.TORM.OM",
			    "TotalLevel": 3,
                "IsDelivering": true,
                "IsDeliveringDefault": true,
                "IsPosting": true,
                "IsPostingDefault": true,
                "IsInvoicing": true,
                "IsInvoicingDefault": true,
                "Index": 0,
                "Address": {
                    "$type": "Winbooks.TORM.OM.Address, Winbooks.TORM.OM",
					"TotalLevel": 2,
                    "Address1": "330 Lemonierlaan (WB6)",
                    "Town": "Antwerpen",
                    "Zip": "2000",
                    "PhoneNo": "+3223456789",
					"CountryCode": "BE"
                }
            }
        ],
      "CivilityCode": "NV",
      "VatCountryCode": "BE",
      "LanguageCode": "en",
      "CurrencyCode": "EUR"
    },
   "VatCode": "21",
   "CategoryCode": "EU",
   "GLAccountDefaultCode": "700000",
   "GLAccountCentralCode": "400000",
   "PayCodeCode": "30"
}

The last level (Addresses) has TotalLevel = 2 because we use the friendly code property (CountryCode). If we use the Id's instead, we have to put TotalLevel = 1 for the last level.

Do not use the DefaultAddress entity which does not support the posting.

Posting a new Customer (with the Id's)

It is more efficient to specify the Id's of the related entities, especially when we post several customers using the same country, civility, language, currency, vat, category, default account, central account or payment term (PayCode).

First, we have to retrieve the Id's:

Request

Id

GET {{url}}/app/Country/BE/Folder/{{folder}}4bb898f0-21ba-4ea9-9c35-a74d006cd473
GET {{url}}/app/Civility/NV/Folder/{{folder}}9bc5b8f4-3682-46c6-ac84-a74d006cd9ad
GET {{url}}/app/Language/en/Folder/{{folder}}4b2d0fa7-ecf7-4883-9e04-a89d00bcd216
GET {{url}}/app/Currency/EUR/Folder/{{folder}}8d70e1d6-57cf-4bc1-8402-a74d006cd472

GET {{url}}/app/Vat/21/Folder/{{folder}}
or

GET {{url}}/app/VatAccount/21/Folder/{{folder}}

d939ccde-90ec-45bb-97fe-a74d006cd718
GET {{url}}/app/CustomerCategory/BIG/Folder/{{folder}}c3613c92-d081-4a94-a245-a8b3005d2391
GET {{url}}/app/GLAccount/700000/Folder/{{folder}}c61c5554-0803-4286-9315-a74d006cd5aa
GET {{url}}/app/GLAccount/400000/Folder/{{folder}}023a99d0-f1e5-45c8-b902-a74d006cd5a8
GET {{url}}/app/PayCode/7/Folder/{{folder}}48a7b7ab-0f1b-4420-af2e-a74d006cd9a8


Or really more efficiently via the ExecuteCriteria:

Request

Body

POST {{url}}/app/Countrys/Folder/{{folder}}/ExecuteCriteria{ "EntityType": "Winbooks.TORM.OM.Countrys, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["BE"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }
POST {{url}}/app/Civilitys/Folder/{{folder}}/ExecuteCriteria{ "EntityType": "Winbooks.TORM.OM.ThirdCivility, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["NV"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }
POST {{url}}/app/Languages/Folder/{{folder}}/ExecuteCriteria{ "EntityType": "Winbooks.TORM.OM.Language, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["en"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }
POST {{url}}/app/Currencys/Folder/{{folder}}/ExecuteCriteria

{ "EntityType": "Winbooks.TORM.OM.Currency, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["EUR"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }

POST {{url}}/app/VatAccounts/Folder/{{folder}}/ExecuteCriteria

{ "EntityType": "Winbooks.TORM.OM.Accounting.VatAccount, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["21"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }

POST {{url}}/app/GLAccounts/Folder/{{folder}}/ExecuteCriteria{ "EntityType": "Winbooks.TORM.OM.Accounting.GLAccount, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["700000"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }
POST {{url}}/app/GLAccounts/Folder/{{folder}}/ExecuteCriteria{ "EntityType": "Winbooks.TORM.OM.Accounting.GLAccount, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["400000"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }
POST {{url}}/app/PayCodes/Folder/{{folder}}/ExecuteCriteria

{ "EntityType": "Winbooks.TORM.OM.Accounting.PayCode, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["7"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }


To post the customer, the body will be this one for version <= 2.4:

{
   "$type": "Winbooks.TORM.OM.Customer, Winbooks.TORM.OM",
   "TotalLevel": 3,
   "Vat_Id": "d939ccde-90ec-45bb-97fe-a74d006cd718",
   "CustomerCategory_Id": "c3613c92-d081-4a94-a245-a8b3005d2391",
   "GLAccount_Id": "c61c5554-0803-4286-9315-a74d006cd5aa",
   "GLAccount2_Id": "023a99d0-f1e5-45c8-b902-a74d006cd5a8",
   "PayCode_Id": "bee3ad14-c3b0-4284-80e7-a74d006cd9a8",
   "Code": "TEST2",
   "VatApplicable": 1,
   "IsLocked": false,
   "IsHidden": false,
   "MemoType": 0,
   "Third": {
      "$type": "Winbooks.TORM.OM.Third, Winbooks.TORM.OM",
      "TotalLevel": 2,
      "ThirdCivility_Id": "9bc5b8f4-3682-46c6-ac84-a74d006cd9ad",
      "VatCountry_Id": "4bb898f0-21ba-4ea9-9c35-a74d006cd473",
      "Language_Id": "4b2d0fa7-ecf7-4883-9e04-a89d00bcd216",
      "Currency_Id": "8d70e1d6-57cf-4bc1-8402-a74d006cd472",
      "Code": "TEST2",
      "Name": "The Test Company 2",
      "VatNumber": "0000000295",
      "WebSite": "www.test2.be",
      "Addresses": [
         {
            "$type": "Winbooks.TORM.OM.Address, Winbooks.TORM.OM",
            "TotalLevel": 1,
            "Country_Id": "4bb898f0-21ba-4ea9-9c35-a74d006cd473",
            "Index": 0,
            "Address1": "Vuchtlaan 79",
            "Address2": "",
            "Town": "Antwerpen",
            "Zip": "2000",
            "Name": "",
            "Number": "",
            "Box": "",
            "PhoneNo": "+3233215476",
            "FaxNo": "",
            "MemoType": 0,
            "Memo": "",
            "IsDelivering": true,
            "IsDeliveringDefault": true,
            "IsPosting": true,
            "IsPostingDefault": true,
            "IsInvoicing": true,
            "IsInvoicingDefault": true
         }
      ]
   }
}


And this one for version >= 2.5:

{
    "$type": "Winbooks.TORM.OM.Customer, Winbooks.TORM.OM",
	"TotalLevel": 4,
    "Code": "WB5",
    "Vat_Id": "1d625e47-4871-450b-91c8-9f9400b4ca36",
    "GLAccount_Id": "5633a32d-c312-4e19-9e6b-9f9400b4c691",
    "GLAccount2_Id": "d2647e20-8267-4703-90c0-9f9400b4c67a",
    "PayCode_Id": "f6a39f52-a79d-4758-933a-9f9400b4d059",
    "VatApplicable": 1,
    "Code": "WB5",
    "Third": {
        "$type": "Winbooks.TORM.OM.Third, Winbooks.TORM.OM",
    	"TotalLevel": 3,
        "Code": "WB5",
        "ThirdCivility_Id": "5f4964b8-00f2-464f-a01d-9f9400b4d0ce",
        "VatCountry_Id": "8f8a337e-b483-4c30-b6ed-9f9400b4c553",
        "Language_Id": "5dc36906-5d31-4ff9-b20c-9f9400b4c5df",
        "Currency_Id": "2b790e84-f9f5-4ff6-b775-9f9400b4c54e",
        "Name": "Customer WB5",
        "VatNumber": "0000000394",
        "Code": "WB5",
        "Addresses": [
            {
                "$type": "Winbooks.TORM.OM.Third_Address, Winbooks.TORM.OM",
				"TotalLevel": 2,
                "IsDelivering": true,
                "IsDeliveringDefault": true,
                "IsPosting": true,
                "IsPostingDefault": true,
                "IsInvoicing": true,
                "IsInvoicingDefault": true,
                "Index": 0,
                "Address": {
                    "$type": "Winbooks.TORM.OM.Address, Winbooks.TORM.OM",
					"TotalLevel": 1,
                    "Country_Id": "8f8a337e-b483-4c30-b6ed-9f9400b4c553",
                    "Address1": "330 Lemonierlaan (WB5)",
                    "Town": "Antwerpen",
                    "Zip": "2000",
                    "PhoneNo": "+3223456789"
                }
            }
        ]
    }
}

Check if a Customer exists

The best is to use the ExecuteCriteria.

POST {{url}}/app/Customers/Folder/{{folder}}/ExecuteCriteria

The body of the request:


{ "EntityType": "Winbooks.TORM.OM.Customer, Winbooks.TORM.OM",
"Conditions": [ {"Operator": 0, "PropertyName": "Code", "Values": ["TEST2"] } ],
"ProjectionsList": [ {"PropertyName": "Id", "Operator": 22} ],
"MaxResult": 1 }


The result:

[
"c961989a-afb3-4c2e-aba0-a8b3008e9942"
]


In case of the customer code does not exist, it returns an empty result.

Update a Customer

You can update the Customer and the Third by the same way you create them but you have to remove the address else it will create a new address. The address has to be updated separately.

PUT {{url}}/app/Customer/TEST2/Folder/{{folder}}

The body to update the Customer and the Third is:

{
   "$type": "Winbooks.TORM.OM.Customer, Winbooks.TORM.OM",
   "TotalLevel": 3,
   "Vat_Id": "d939ccde-90ec-45bb-97fe-a74d006cd718",
   "CustomerCategory_Id": "c3613c92-d081-4a94-a245-a8b3005d2391",
   "GLAccount_Id": "c61c5554-0803-4286-9315-a74d006cd5aa",
   "GLAccount2_Id": "023a99d0-f1e5-45c8-b902-a74d006cd5a8",
   "PayCode_Id": "bee3ad14-c3b0-4284-80e7-a74d006cd9a8",
   "Code": "TEST2",
   "VatApplicable": 1,
   "IsLocked": false,
   "IsHidden": false,
   "MemoType": 9,
   "Memo": "This is just a comment",
   "Third": {
      "$type": "Winbooks.TORM.OM.Third, Winbooks.TORM.OM",
      "TotalLevel": 2,
      "ThirdCivility_Id": "9bc5b8f4-3682-46c6-ac84-a74d006cd9ad",
      "VatCountry_Id": "4bb898f0-21ba-4ea9-9c35-a74d006cd473",
      "Language_Id": "4b2d0fa7-ecf7-4883-9e04-a89d00bcd216",
      "Currency_Id": "8d70e1d6-57cf-4bc1-8402-a74d006cd472",
      "Code": "TEST2",
      "Name": "The Test Company 2",
      "VatNumber": "0000000295",
      "WebSite": "www.test2.be"
   }
}

The TotalLevel of the last level (Third) = 2 else it won't update.

Update the Address

Find the Address Id

First, we have to retrieve the Id of the Address of the Third TEST2.

For version <= 2.4 of WinBooks on Web

This is the .NET query to do that (with the Winbooks.Apis.Service.dll):

ICriteria criteria = new AddressDAO().CreateCriteria()
   .CreateAlias("third", typeof(Third), Third.Names.Id, Address.Names.Third_Id)
   .Add(Condition.Eq("third." + Third.Names.Code, "BEBOP"))
   .Add(Condition.Eq(Address.Names.IsInvoicingDefault, true))
   .SetProjection(Projections.Property(Address.Names.Id));
Newtonsoft.Json.Linq.JArray addressid = _folder.GetFilterAll(criteria.JsonCriteriaSerialize(), "Address");
if (addressid == null)
Tools.Log(" no address for BEBOP");
else
Tools.Log(" address_id = " + addressid[0].ToString());

The corresponding http request is this one:

POST {{url}}/app/Addresss/Folder/{{folder}}/ExecuteCriteria

with the following JSON body:

{
   "EntityType": "Winbooks.TORM.OM.Address, Winbooks.TORM.OM",
   "Alias": "this",
   "Association": {
      "third": {
         "AliasName": "third",
         "Type": "Winbooks.TORM.OM.Third, Winbooks.TORM.OM",
         "JoinType": 1,
         "LeftProperty": "Id",
         "RightProperty": "Third_Id"
      }
   },
   "Conditions": [
      {
         "Operator": 0,
         "PropertyName": "third.Code",
         "OtherPropertyName": "",
         "Values": [
            "BEBOP"
         ],
      }
   ],
   "ProjectionsList": [
      {
         "PropertyName": "Id",
         "Operator": 22
      }
   ],
}


For version >= 2.5 of WinBooks on Web

This is the .NET query to do that (with the Winbooks.Apis.Service.dll):

ICriteria criteria = new Third_AddressDAO().CreateCriteria()
.CreateAlias("address", typeof(Address), Address.Names.Id, Third_Address.Names.Address_Id)
.CreateAlias("third", typeof(Third), Third.Names.Id, Third_Address.Names.Third_Id)
.Add(Condition.Eq("third." + Third.Names.Code, "BEBOP"))
.Add(Condition.Eq(Third_Address.Names.IsInvoicingDefault, true))
.SetProjection(Projections.Property("address." + Address.Names.Id));
Newtonsoft.Json.Linq.JArray addressid = _folder.GetFilterAll(criteria.JsonCriteriaSerialize(), "Third_Address");
if (addressid == null)
Tools.Log(" no address for BEBOP");
else
Tools.Log(" address_id = " + addressid[0].ToString());

The corresponding http request is this one:

POST {{url}}/app/Third_Addresss/Folder/{{folder}}/ExecuteCriteria

with the following JSON body:

{
    "EntityType": "Winbooks.TORM.OM.Third_Address, Winbooks.TORM.OM",
    "IsCustomization": false,
    "Alias": "this",
    "Association": {
        "address": {
            "AliasName": "address",
            "Type": "Winbooks.TORM.OM.Address, Winbooks.TORM.OM",
            "JoinType": 1,
            "LeftProperty": "Id",
            "RightProperty": "Address_Id"
        },
        "third": {
            "AliasName": "third",
            "Type": "Winbooks.TORM.OM.Third, Winbooks.TORM.OM",
            "JoinType": 1,
            "LeftProperty": "Id",
            "RightProperty": "Third_Id"
        }
    },
    "Conditions": [
        {
            "Operator": 0,
            "PropertyName": "third.Code",
            "OtherPropertyName": "",
            "Values": [
                "BEBOP"
            ]
        },
        {
            "Operator": 0,
            "PropertyName": "IsInvoicingDefault",
            "OtherPropertyName": "",
            "Values": [
                true
            ]
        }
    ],
   "Havings": [],
    "ProjectionsList": [
        {
            "PropertyName": "address.Id",
            "Operator": 22
        }
    ],
    "Orders": [],
    "Params": {},
    "FirstResult": -1,
    "MaxResult": -1,
    "ResultState": 0,
    "TimeOutSeconds": 0
}


The result is an array of Id's:

[
    "adb4d306-63c9-4d5d-a31a-a8b3008e9946"
]

Update the Address

You have to use the PUT verb in order to update the Address based on the Id which is inside the body. Pay attention at the TotalLevel = 2.

PUT {{url}}/app/Address/Folder/{{folder}}

The body is:

{
     "$type": "Winbooks.TORM.OM.Address, Winbooks.TORM.OM",
     "TotalLevel": 2,
     "Country_Id": "4bb898f0-21ba-4ea9-9c35-a74d006cd473",
     "Index": 0,
     "Address1": "Vuchtlaan 2",
     "Address2": "",
     "Town": "Gent",
     "Zip": "9000",
     "Name": "",
     "Number": "",
     "Box": "",
     "PhoneNo": "",
     "FaxNo": "",
     "MemoType": 0,
     "Memo": "",
     "IsDelivering": true,
     "IsDeliveringDefault": true,
     "IsPosting": true,
     "IsPostingDefault": true,
     "IsInvoicing": true,
     "IsInvoicingDefault": true,
"Id": "adb4d306-63c9-4d5d-a31a-a8b3008e9946"
}
  • No labels