sum values in a child entity - linq

I have an entity with a parent class and two child classes, like this:
Tax Report --> Parcel --> Tax Collector
Tax Report contains a collection of Parcel, and Parcel contains a collection of Tax Collector.
My Tax Report object has 2 Parcel objects associated with it, and each Parcel object has 2 Tax Collector objects associated with it. The Tax Collector object has a property called Taxes Due that I am interested in. Here is an example of the data:
TaxReport: 1
Parcel: 0001
Tax Collector: Dallas County, $100 in taxes due
Tax Collector: Collin County, $50 in taxes due
Parcel: 0002
Tax Collector: Collin County, $1000 in taxes due
Tax Collector: City of Irving, $100 in taxes due
I want to query my objects to sum the taxes due for all parcels, grouped by Tax Collector. Here is what I want my result to look like:
Dallas County, $100
Collin County, $1050
City of Irving, $100
I have tried something along these lines:
var taxsummary = from tr in taxreport.Parcel.Select(y=>y.TaxCollector).Select(n=>n.TaxesDue).Sum()
But I haven't had any luck. My results are always projected as a multi-level collection with Taxes Due below each Parcel, not grouped by Tax Collector. Any ideas?
Thanks,
Brad

This does the trick:
var grouped = from parcel in taxReport.Parcels
from taxCollector in parcel.TaxCollectors
group taxCollector by new
{
Name = taxCollector.Name // Or whatever makes it Unique...
};
foreach(var collectorGroup in grouped)
{
Console.WriteLine(collectorGroup.Key.Name + " : " + collectorGroup .Sum(p => p.TaxesDue));
}
If you want something like a Dictionary you can do this:
var grouped = (from parcel in taxReport.Parcels
from taxCollector in parcel.TaxCollectors
group taxCollector by new
{
Name = taxCollector.Name
}
into groupedCollectors
select new
{
Name = groupedCollectors.Key.Name,
TaxesDue = groupedCollectors.Sum(p => p.TaxesDue)
})
.ToDictionary(p =>p.Name, p => p.TaxesDue);

Related

How to recalculate tax on new row total in magento?

I’m making a call to $item->calcRowTotal() on a Mage_Sales_Model_Quote_Item object.
This works great to reset the row total and base row total, but it does not affect the row total including tax (row_total_incl_tax) attribute on the item.
I assume I have to manually do this after I have the new row total but I can’t figure out how to properly calculate the tax and populate the row_total_incl_tax attribute on the item.
Any suggestions would be much appreciated.
Get the tax rate and recalculate row_total_incl_tax. Here's how you can get tax rate.
Assuming your order no. is 101.
$sale = Mage::getModel('sales/sale)->load(101)
$taxModel = Mage::getModel('sales/order_tax')
->load($sale->getId(), 'order_id');
$taxRate = $taxModel->getPercent()
I think you know how to calculate the tax amount based on row total value.
Edit:
Get tax based on product:
$product = Mage::getModel('catalog/product')->load($item-getProductId());
$request = Mage::getSingleton('tax/calculation')
->getRateRequest()
->setProductClassId($product->getTaxClassId());
$taxRate = Mage::getSingleton('tax/calculation')
->getRate($request);
So you know the tax rate, row_total_incl_tax is sum of tax * qty + total amount (or you can calculate anyway you want). May be you need to check if item price is included tax or not, you can do this by Mage::getModel('Tax/Config')->priceIncludesTax()

How Do I Display Different Messages Under Grand Total for Tax?

I am wondering how I would go about adding a message under the grand total on the shopping cart, checkout and on customer emails that say either one of two messages if tax is enabled or not.
For example if someone in the UK or EU buys something, it will say under the grand total “Includes VAT at 20%”. If someone outside of the EU buy something, it will say under the grand total “UK Sales tax has been deducted”.
I have it already set up so the tax changes depending on which country you select, but I'm wondering how to display these messages now.
Many thanks in advance.
Edit app/design/frontend/base/default/template/checkout/cart.phtml
Adapt and add this code :
$quote = Mage::getSingleton('checkout/session')->getQuote();
$billingAddress = $quote->getBillingAddress();
$country = $billingAddress->getCountryId();
if($country == "XX") {
echo "<p>Display this message if user is from XX.</p>";
}

How to assign a product to be taxable in multiple states?

My concern is I am developing a site in magento where client needs an ability to specify taxes per state at category or product level.
To be more specific, there are some products in the store that are taxable in more than one US states like electronic items are taxable in NY, NJ and CA.
Is there any way I can achieve this?
Magento allows for a lot a flexibility in setting tax rates. Go to Sales -> Tax -> Manage Tax Rates and specify as many rates as you'd like there. Rates can apply to whole countries, whole states, and specific zip codes.
In your use case for say - stereo receivers ( assuming there's a unique tax rate per state for this category of electronics ). You could do the following:
Create a product tax class: specialelectronics
Create a tax rate: usa-ny-specialelectronics ; usa ; ny ; * ; rate = 10%
Create a tax rate: usa-nj-specialelectronics ; usa ; nj ; * ; rate = 20%
Create a tax rate: usa-ca-specialelectronics ; usa ; ca ; * ; rate = 50%
Create a tax rule: specialelectronics; retail customer ; specialelectronics ; usa-ny-specialelectronics, usa-nj-specialelectronics, usa-ca-specialelectronics
Note you can only assign a product to one product tax class.

custom options price calculation

How does custom options price calculation work for a fixed and percent price?
I want to change percent calculation functionality from base price to total price.
Currently it's calculating price from base price.
How to change this from base price to total price?
For example:
base price = $10
custom option1 = $10
custom option2 = 10%
default Magento calculation:
Total price = Base price + selected custom option1 + selected custom option.
Total price = $10+$10+ 10% of $10(baseprice) = $21.
But we need this Total price after selecting the custom option:
Total price = $10+$10 = $20.
Total price after select custom option2
Total price = $20 + (10% of Total price)
Total price = $20 + (10% of $20).
Total price = $20 + $2 = $22.
I need to calculate the percentage from the total price, not from the base price. Please advise.
This is done in
Mage_Catalog_Model_Product_Type_Price::_applyOptionsPrice
If this applies only to special products, you might want to create an own product type with a customer price handler.

VAT calculation when using pricing including taxes

The prices are including VAT Tax. The VAT % applied is based on the delivery address. When the delivery country is changed and the VAT % changes, the the amount incl. tax is changed.
For example:
An amount of 0.79 euro is calculated as tax which corresponds with a delivery adress in the Netherlands. 19% Vat is applicable.
Price ex VAT 4.94/1,19=4,15
VAT Amount = 4,15*0,19=0,79
This is working fine.
When a change the delivery date to the country Belgium, where the VAT is 21%
Then the all the amount including tax is increaded. This should not happen as we choose use prices including tax.
The calculation thats is made by Magento is based on the price excel tax when 19%
Dutch VAT was applicable and then applied 21 Belgian VAT:
Price EX VAT: 4,15 = wroing amount has not been recalculated
Belgian VAT Amount = 4,15*,21= 0.87 = wrong amount
The correct calculatio should be:
Price ex VAT 4.94/1,21= 4,08 = correct price excl 21% tax
VAT Amount = 4,08*0,21= 0,86 = correct 21% VAT Amount
Total price = 4,94 incl. VAT
For Belgium i get this price 5,02 and it should be 4,94.
Desired Outcome:
The amount incl tax should stay the same regardless of the VAT% applied.
How can i fix this.
In fact i don't think your reasoning is good.
i explain:
the base price value of the product is it VAT free value.
For a given country, the product is applied a VAT to comply with country VAT related rates.
For wathever country you deliver the product, what changes is VAT but not product base price value , otherwise you'll loose some margin.
Say:
product A VAT Free price is : 100 and you bought it 80 (20 eur margin)
VAT for Netherlands is 19 : Taxed price for Netherlands => 119
VAT for Belgium is 21 : Taxed price for Belgium => 121
Wanting to keep 119 for Belgium would make loose you 2 euros ,since you would have virtually sold it for 98.
If that's really what you want to do : ie , flat price whatever VAT Rate is applide ,Then a "trick" might be to create customer groups based on their country & used tier pricing to reduce product base price in order to maintain VAT rate independent taxed price. (which i wouldn't recommend)

Resources