moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.62k stars 440 forks source link

Documentation needs details on tax calculation #786

Closed oucil closed 6 months ago

oucil commented 7 months ago

Since this is a Money library, it's only logical that it will also be used in tax calculations (i.e. multiplying by a decimal/float). I fully understand and support the concept of "no floats" in it's construction and do not expect any changes there.

That said, there is no reference to how to properly calculate tax using decimals in the documentation with reference to tax, or at least it's not obvious, and it wasn't until I spent a while searching that I found an answer like this https://github.com/moneyphp/money/issues/650

The problem is that those answers essentially disappear from the issues list once they're closed, and so you have to manually go searching through history. There should be a documented method of how to calculate tax, by using double quotes to surround the decimal number to cast to string. That info may be there, but you need to actually list a use case using the word "tax" to it's easier to find and such a common use case.

Just a helpful request for others in the same situation as I was, it would save time for both us, and for you answering questions with "No float, it has been explained before". ;)

begench993 commented 7 months ago

Converting a float value to a string !important $product['tax'] is INTEGER example in turkey tax is %20

$tax=(string)floatval(1+(intval($product['tax'])/100));

including TAX

// (Net Amount) x [1 + (Tax Rate/100)]
// 1000 USD + 20% TAX is:
// 1000 x (1 + 20/100) = 1000 x (1 + 0.20) = 1000 x 1.20 = 1200 USD

$total=$price->multiply($tax);

excluding TAX

// (Gross Amount) / [1 + (Tax Rate/100)]
// 1200 USD - 20% TAX is:
// 1200 / (1 + 20/100) = 1200 / (1 + 0,20) = 1200 / 1,20 = 1000 USD 

$price=$total->divide($tax);

calculating TAX

// 1200 - 1000 = 200 USD

$tax_total=$total->subtract($price);

tax base

// tax_total / ( tax / 100 )
// 200 / (20/100) = 200 / (0,20) = 1000 USD 

$tax=(string)floatval(intval($product['tax'])/100);
$price=$tax_total->divide($tax)

Hope I help someone

frederikbosch commented 6 months ago

Tax is outside the scope of this library.

oucil commented 6 months ago

Nobody asked for the library to support tax in scope. All I suggested was a quick example for one of the primary use cases to save yourselves having to answer issues about how to multiply by a decimal. It's just not easy finding an explicit answer as to how to do that properly, I didn't find it until digging through your closed issues. If you had even a single example that uses the word tax, I think it would save people a lot of time.