Set Sales Order's PriceScheme and recalculate line prices  Topic is solved

Discussions relating to the REST API of Jiwa 7.

Set Sales Order's PriceScheme and recalculate line prices

Postby mike.waugh » Wed Feb 26, 2020 1:44 pm

Hi

How do we set a PriceScheme for a Sales Order via API please? If we set it via API, will the lines item's prices be auto recalculated? if not, is there a way to do so?

Thank you
mike.waugh
Occasional Contributor
Occasional Contributor
 
Posts: 10
Joined: Wed Feb 26, 2020 1:36 pm

Re: Set Sales Order's PriceScheme and recalculate line price

Postby Mike.Sheen » Wed Feb 26, 2020 2:00 pm

mike.waugh wrote:Hi

How do we set a PriceScheme for a Sales Order via API please? If we set it via API, will the lines item's prices be auto recalculated? if not, is there a way to do so?

Thank you


Hi,

It surprises me to say this - but we must have overlooked that and our current DTO's for sales orders don't have the Price Scheme and so the API does not allow you to change it.

I've added DEV-8106 to add this improvement.

As a work-around a plugin can define a custom service which would allow you to change the price scheme for an existing sales order. With a bit more work and you can do the same for creating a sales order.

If you look inside our standard REST API plugin, you'll see the code for the PATCH /SalesOrders/{InvoiceID} performs a read of the sales order, then deserialises a provided DTO, saves, and then serialises and returns the DTO as a response - you can copy-paste that and modify it so you just read the sales order, set the price scheme, save, serialise and return the DTO as a response.

A PATCH on /SalesOrders/{InvoiceID}/PriceScheme/{PriceSchemeID} is how I'd route it.

Is that enough for you to be able to move forward with?

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2440
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 755

Re: Set Sales Order's PriceScheme and recalculate line price

Postby mike.waugh » Fri Feb 28, 2020 1:42 am

Hi Mike Sheen,
Hope you are doing well, Thank you for your reply.

I understand the PATCH method. But I have some question for creating new Sales Order with PriceScheme,
The questions are:
1. As for POST method we have to pass DTO using Json serialization, how can we pass PriceSchemeIdD to SalesOrder DTO can we pass as param or in other way for POST method?
2. Please check the code below to see if that is how the Service method should be written to set the SalesOrder's PriceSchemeId



The steps we have done are:
1. Modify the REST API Custom Routes Plug in and enable it
2. We copy the POST method from the standard API plug in and paste in as a new method like below

[Authenticate]
public JiwaFinancials.Jiwa.JiwaServiceModel.SalesOrders.SalesOrder Post(SalesOrderWithPriceSchemePOSTRequest request)
{
JiwaApplication.Manager manager = this.SessionAs<JiwaAuthUserSession>().Manager;

JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder so = manager.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder>(null);
so.DTO_Deserialise(request);

if (!string.IsNullOrWhiteSpace(request.PriceSchemeId))
{
so.PriceSchemeID=request.PriceSchemeId;
}

if (Helper.Service.IsStateful(this))
manager.ObjectDictionary.Add(so.InvoiceID, so);
else
{
so.Save();
so.Read(so.InvoiceID);
}

// Add the location to the response header
base.Response.AddHeader("Location", String.Format("{0}/{1}", base.Request.AbsoluteUri.Split(new[] { '?' })[0], so.InvoiceID));

return so.DTO_Serialise();
}
mike.waugh
Occasional Contributor
Occasional Contributor
 
Posts: 10
Joined: Wed Feb 26, 2020 1:36 pm

Re: Set Sales Order's PriceScheme and recalculate line price

Postby SBarnes » Fri Feb 28, 2020 7:28 am

Without all the code it's hard to comment but the following should help.

Your SalesOrderWithPriceSchemePOSTRequest should inherit from SalesOrderPOSTRequest and then add any properties you need and pass anything else that way, then they just need to go in the json that goes in the body.

You then need you own configure function if there is not one in the plugin, look at the Rest API Plugin if needs be and you will need code like the following in it


Code: Select all
AppHost.RegisterService<You service class goes here >;();
         
AppHost.Routes.Add(typeof(your request class goes here), "/Salesorders/PatchWithScheme", "POST", "Put a description here", "");


I would also suggest you don't modify an existing plugin from Jiwa, you can actually have you own by copying it and giving it a different name and gutting out the original code, the reason being an upgrade to Jiwa could possibly overwrite your code if Jiwa modify the custom routes plugin.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1617
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Set Sales Order's PriceScheme and recalculate line price

Postby mike.waugh » Fri Feb 28, 2020 12:32 pm

HI Steve

Thanks a lot for the help.

For now, we just work on the existing Custom Routes plugin for simplicity. We will create a new one after things working.

Questions:
1. Please let us know where are we getting wrong here. So we can solve it and get our solution.

Error Messages:
When we send POST request from our application, returned error is:
[A]JiwaFinancials.Jiwa.JiwaServiceModel.JiwaAuthUserSession cannot be cast to [B]JiwaFinancials.Jiwa.JiwaServiceModel.JiwaAuthUserSession.
Type A originates from 'REST API, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\\ProgramData\\Jiwa Financials\\Jiwa 7\\7.2.1\\SYSTEM\\DESKTOP-QTDMRQ9SQLEXPRESS\\Jiwa_Demo72_Test_DevWip\\Plugins\\Admin\\Runtime\\REST API\\REST API.dll'.
Type B originates from 'REST API Custom Routes Example, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\\ProgramData\\Jiwa Financials\\Jiwa 7\\7.2.1\\SYSTEM\\DESKTOP-QTDMRQ9SQLEXPRESS\\Jiwa_Demo72_Test_DevWip\\Plugins\\Admin\\Runtime\\REST API Custom Routes Example\\REST API Custom Routes Example.dll

What we have done:
We have done following steps and code in custom api.

Step 1: Route as per your suggestion.

public void Configure(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, ServiceStack.ServiceStackHost AppHost, Funq.Container Container, JiwaApplication.Manager JiwaApplicationManager)
{
AppHost.RegisterService<CustomServices>();
AppHost.Routes.Add(typeof(SalesOrderWithPriceSchemePOSTRequest), "/SalesOrders/PatchWithScheme", "POST", "To create a new sales order together with a specific Price Scheme.", "");
}

Step 2: Inherrited request as per your suggestion. Please check code below.

[Serializable()]
[ApiResponse(200, "Created OK")]
[ApiResponse(401, "Not authenticated")]
[ApiResponse(403, "Not authorised")]
public class SalesOrderWithPriceSchemePOSTRequest : SalesOrderPOSTRequest
{
public string PriceSchemeId { get; set; }
}

Step 3: Making Service For Post to Save Sales Order

public class CustomServices : Service
{
[Authenticate]
public JiwaFinancials.Jiwa.JiwaServiceModel.SalesOrders.SalesOrder Post(SalesOrderWithPriceSchemePOSTRequest request)
{
JiwaApplication.Manager manager = this.SessionAs<JiwaAuthUserSession>().Manager;

JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder so = manager.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder>(null);
so.DTO_Deserialise(request);

if (!string.IsNullOrWhiteSpace(request.PriceSchemeId))
{
so.PriceSchemeID=request.PriceSchemeId;
}

if (Helper.Service.IsStateful(this))
manager.ObjectDictionary.Add(so.InvoiceID, so);
else
{
so.Save();
so.Read(so.InvoiceID);
}

// Add the location to the response header
base.Response.AddHeader("Location", String.Format("{0}/{1}", base.Request.AbsoluteUri.Split(new[] { '?' })[0], so.InvoiceID));

return so.DTO_Serialise();
}
}
mike.waugh
Occasional Contributor
Occasional Contributor
 
Posts: 10
Joined: Wed Feb 26, 2020 1:36 pm

Re: Set Sales Order's PriceScheme and recalculate line price

Postby mike.waugh » Fri Feb 28, 2020 12:41 pm

Sorry, I forgot to include this part of the code in my last post (to go in before Step 3: Making Service For Post to Save Sales Order)

Copy and paste the JiwaAuthUserSession class from the Standard Api plugin to my plugin
public class JiwaAuthUserSession : AuthUserSession
{
public JiwaApplication.Manager Manager { get; set; }
public long RequestCount { get; set; }
public long ConcurrentRequestCount { get; set; }
public long MaxConcurrentRequestCount { get; set; }
public string IPAddress { get; set; }
}
mike.waugh
Occasional Contributor
Occasional Contributor
 
Posts: 10
Joined: Wed Feb 26, 2020 1:36 pm

Re: Set Sales Order's PriceScheme and recalculate line price  Topic is solved

Postby SBarnes » Fri Feb 28, 2020 12:48 pm

Ok remove the last bit of code you sent, if the plugin then won't compile, there is a tab at the top of the plugin editor that says Plugin Reference, go there and add the Rest API plugin as a reference you are basically ending up with two versions of the class JiwaAuthUserSession by adding the reference you are telling it to look in the Rest API as well where it is declared and no where else.

The current custom route plugin doesn't use the manager that's why the reference isn't there on all the custom plugins I do I add the reference when I start.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1617
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Set Sales Order's PriceScheme and recalculate line price

Postby mike.waugh » Fri Feb 28, 2020 4:21 pm

Hi Steve

Thank you very much for your help. The PriceSchemeId now works properly.

But in Jiwa, all sales order line have Ex Price = 0 so total order is 0 although the PriceScheme was set up to use the Item Default Price. If we reselect the pricescheme in Jiwa Sales order screen and say Yes to Recalculate the prices prompt then the prices takes effect.

What did we miss? Is there a way to recalculate the prices?
mike.waugh
Occasional Contributor
Occasional Contributor
 
Posts: 10
Joined: Wed Feb 26, 2020 1:36 pm

Re: Set Sales Order's PriceScheme and recalculate line price

Postby SBarnes » Fri Feb 28, 2020 5:34 pm

I occasionally go by the name Stuart as well :lol: but you want salesorder.RecalculatePrices
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1617
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Set Sales Order's PriceScheme and recalculate line price

Postby Mike.Sheen » Fri Feb 28, 2020 8:07 pm

Thanks so much, Steve for helping William out here.

Nothing quite like a community spirit helping each other out!
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2440
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 755

Next

Return to REST API

Who is online

Users browsing this forum: No registered users and 0 guests

cron