Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inbound shipment % margin/markup #509

Open
josh-griffin opened this issue Nov 26, 2021 · 6 comments
Open

Inbound shipment % margin/markup #509

josh-griffin opened this issue Nov 26, 2021 · 6 comments

Comments

@josh-griffin
Copy link
Contributor

Currently in mSupply there is a %margin field. I don't know too much about it and it's quite a pickle to figure out - need to invest more time into it.

It seems as though it might be a non-persisted field and it is just the % difference between cost and sell prices, where adjust the margin adjusts the sell price also

@josh-griffin josh-griffin added this to the Alpha milestone Nov 26, 2021
@mark-prins
Copy link
Contributor

mark-prins commented Nov 30, 2021

I didn't know - have done a little digging and come up with something helpful from the docs. Turns out that there is a preference!

Note: If you did wish to apply a different margin to the whole invoice, click the cancel button, and edit the margin for that supplier by choosing Supplier >Edit supplier and change the margin. If you wish to apply a different margin to just some lines, you can do this as you enter the lines, as long as Editing margins is enabled in the Preferences.

though the preference isn't working - there's a bug in the implementation.
there is however, a default margin on the supplier and we should probably bring that through

Calculations made in tl_calc_margin_and_sell_price - this is the key bit:

	If ([trans_line]cost_price#0)
		r_margin:=(([trans_line]sell_price-[trans_line]cost_price)/[trans_line]cost_price)*100
		r_margin:=Round(r_margin; $i_rounding_factor)
	Else 
		r_margin:=0
		[trans_line]cost_price:=[trans_line]sell_price  // If the cost price is 0 then sell the cost price same as sell price
	End if 

The margin isn't persisted though, as you say.
Initial value comes from either [name] or [item] depending on preferences. Sound like another 🐰 hole

@mark-prins
Copy link
Contributor

Updated the API issue with a comment about margins. We'd need the default value somehow

@josh-griffin
Copy link
Contributor Author

Ah thanks for looking into this. I wonder what the priority is of name or item. "if" there is a supplier margin, use that, otherwise use the item? Otherwise use nothing? Seems just a defaultMargin field on the InvoiceNode or something would work well - the backend can just decide what is meant to be used

@mark-prins
Copy link
Contributor

mark-prins commented Dec 1, 2021

There's a pref for that! 😁

If Item margin overrides supplier margin on Supplier Invoices (<>ipref_inv_entry ?? 27) is set to true, and the item margin is > 0 then the item margin is used. Otherwise the name margin is used. Unless that's 0 then the item margin is used.

Although, if the default price is set, then the margin is 0. That comes from [item_store_join].. specific to store and item. Could return that along with the item.

When loading a supplier invoice, the margin is calculated. If the cost & sell price are > 0 then this:
margin:=Round(((([trans_line]sell_price/[trans_line]cost_price)-1)*100); 2)
( if either cost or sell is 0 then margin = 0 )

The defaultMargin on InvoiceNode wouldn't be specific to an item - I thought that the only way would be to send a mutation to create the InvoiceLineNode and have the API fuss around with prefs and defaults. But I don't like the idea of creating InvoiceLineNodes at that point 😢 It would be a big change to the approach, makes batching requests harder, complicates the cancel action (have to delete the line again) and could be slower

To handle it client-side, we'd need the margin on item and supplier, the default price on item.. and the pref value. I'd much rather have the API handle it all - but I'm not seeing an easy way to do that. I wonder if we can ignore/remove that pref? Prefer supplier margin, but take item if no supplier margin?

@josh-griffin
Copy link
Contributor Author

Ah..! Getting clearer 😁

Yup I think your suggestion is good:

  • Ignore the prefs for now (though, I would like to start adding them sooner rather than later, they're a huge cross cutting concern so the more we put it off the more time it will consume)
    • ItemNode.defaultPrice, ItemNode.margin, NameNode.margin
  • Then we just make some fn getMargin(item, name) => margin and then just add a pref parameter at some point (Which might just be some big json blob of prefs after logging in, or something?)

@josh-griffin
Copy link
Contributor Author

As a recap since it's been so long:

  • Get item.defaultPrice added to API
  • Get item.margin added to API
  • Get name.margin added to api
  • When adding an inbound line, set the initial cost price to the default price and the initial sell price to cost * margin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants