I have set Merchello to include taxes on invoice (not produce).
My tax method is fixed for Spain and without taxes for outside Spain.
Well, i need to show the taxes in my checkout process, before generating the invoice.
So i guess i should have a method that receives the country code and returns the taxes amount. In documentation i see how to get taxes from the tax context:
var shipAd = new Address()
{
AddressType = AddressType.Shipping,
CountryCode = paisCOD
};
var checkout = this.Basket.GetCheckoutManager();
var invoice = checkout.Payment.PrepareInvoice();
var taxationContext = MerchelloContext.Current.Gateways.Taxation;
var tax = taxationContext.CalculateTaxesForInvoice(invoice, shipAd);
It seems to work, but it doesnt take shipping costs into account
Yes @Rusty, it is checked. And it calculates ok afterwards when invoicing and the payment. Now i have calculated the shipping quote and saved it to the invoice and is showing taxes ok. I dont know if i have done it correctly, i have to pass the shipping costs selected to get the quote (i think would be better passing the value instead of the rate) this is my code:
public ActionResult GetImpuestos(decimal gastosEnvio, string paisCOD="ES")
{
var shipAd = new Address()
{
AddressType = AddressType.Shipping,
CountryCode = paisCOD
};
var shipment = this.Basket.PackageBasket(shipAd).FirstOrDefault();
var shipmentRateQuotes = shipment.ShipmentRateQuotes().ToArray();
var quote = shipmentRateQuotes.FirstOrDefault(s => s.Rate == gastosEnvio);
var checkoutManager = this.Basket.GetCheckoutManager();
if(quote!=null)
checkoutManager.Shipping.SaveShipmentRateQuote(quote);
var invoice = checkoutManager.Payment.PrepareInvoice();
var taxationContext = MerchelloContext.Current.Gateways.Taxation;
var tax = taxationContext.CalculateTaxesForInvoice(invoice, shipAd);
decimal impuestos = tax.TaxAmount;
return Json(impuestos);
}
You should be able to define regions in the Merchello.config. These are then used by the shipping and tax providers (like US and Canada - but I've tested with Spain as well).
Once you have defined your regions, you will need to associate a region code with your address - from there you can use the tax provider for overrides per region.
Thanks Rusty, i will check it. As an improving idea for future versions, could this regions be outside of merchello.config to allow clean updates? like a regions_ES.config, so we could share it?
@Rusty my site is on production now and have a problem with a customer calculating taxes this way.
It gets a null invoice. I have checked IsReadyToInvoice() and its false so it makes sense. What may be the issue here? how could i troubleshoot this?
I have tried adding this settings to the checkout manager:
var settings = new CheckoutContextSettings()
{
ResetCustomerManagerDataOnVersionChange = false,
RaiseCustomerEvents = false,
EmptyBasketOnPaymentSuccess = true
};
i'm thinking is something with this customer basket, other baskets works ok.
Yes @Rusty i am passing the settings each time. But note that at the point of this error is before confirming the sale. I only do it to calculate taxes to show on the checkout process. So it is the first time in the process i generate the invoice. Although it gets null, if i confirm the order it goes ok, so i think it is something to this "GetImpuestos" method:
public ActionResult GetImpuestos(string shippingMethodKey, string paisCOD="ES", string region="", decimal gastosEnvio = 0)
{
decimal impuestos = 0;
var settings = new CheckoutContextSettings()
{
ResetCustomerManagerDataOnVersionChange = false,
RaiseCustomerEvents = false,
EmptyBasketOnPaymentSuccess = true
};
var checkoutManager = this.Basket.GetCheckoutManager(settings);
if (string.IsNullOrEmpty(paisCOD))
paisCOD = "ES";
//this is a default address only to calculate taxes for this country/region
var shipAd = new Address()
{
AddressType = AddressType.Shipping,
CountryCode = paisCOD,
Region = region
};
var shipment = this.Basket.PackageBasket(shipAd).FirstOrDefault();
var shipmentRateQuotes = shipment.ShipmentRateQuotes().ToArray();
var quote = shipmentRateQuotes.FirstOrDefault(s => s.ShipMethod.Key.ToString() == shippingMethodKey);
if (quote != null)
{
checkoutManager.Shipping.ClearShipmentRateQuotes();
checkoutManager.Shipping.SaveShipmentRateQuote(quote);
}
//This is where i get false and i dont know why...
if (checkoutManager.Payment.IsReadyToInvoice())
{
var invoice = checkoutManager.Payment.PrepareInvoice();
var taxationContext = MerchelloContext.Current.Gateways.Taxation;
var tax = taxationContext.CalculateTaxesForInvoice(invoice, shipAd);
impuestos = tax.TaxAmount;
}
else
{
//Temporal workaround, as i know for spain is 0.21
decimal amount = gastosEnvio + this.Basket.TotalBasketPrice;
impuestos = decimal.Round(amount * (decimal)0.21, 2);
}
return Json(impuestos);
}
Calculate taxes from invoice on checkout process
I have set Merchello to include taxes on invoice (not produce). My tax method is fixed for Spain and without taxes for outside Spain. Well, i need to show the taxes in my checkout process, before generating the invoice. So i guess i should have a method that receives the country code and returns the taxes amount. In documentation i see how to get taxes from the tax context:
It seems to work, but it doesnt take shipping costs into account
In the global settings, can you check to see if you have the Shipping is taxable checkbox checked?
Yes @Rusty, it is checked. And it calculates ok afterwards when invoicing and the payment. Now i have calculated the shipping quote and saved it to the invoice and is showing taxes ok. I dont know if i have done it correctly, i have to pass the shipping costs selected to get the quote (i think would be better passing the value instead of the rate) this is my code:
Something is not right as every time i call this method is adding new shipping quotes...
Now it works, this is the final code:
Take into account that in sale preparation you have to clear shipment rate quotes before saving it again.
Is there a way to set a fixed tax rate to a region inside a country?
You should be able to define regions in the Merchello.config. These are then used by the shipping and tax providers (like US and Canada - but I've tested with Spain as well).
Once you have defined your regions, you will need to associate a region code with your address - from there you can use the tax provider for overrides per region.
Thanks Rusty, i will check it. As an improving idea for future versions, could this regions be outside of merchello.config to allow clean updates? like a regions_ES.config, so we could share it?
@Rusty my site is on production now and have a problem with a customer calculating taxes this way. It gets a null invoice. I have checked IsReadyToInvoice() and its false so it makes sense. What may be the issue here? how could i troubleshoot this? I have tried adding this settings to the checkout manager:
i'm thinking is something with this customer basket, other baskets works ok.
Hey Tito,
Can you confirm you are passing the settings each time you instantiate the checkout manager?
Yes @Rusty i am passing the settings each time. But note that at the point of this error is before confirming the sale. I only do it to calculate taxes to show on the checkout process. So it is the first time in the process i generate the invoice. Although it gets null, if i confirm the order it goes ok, so i think it is something to this "GetImpuestos" method:
You have to have a Billing Address for the invoice - is that being added somewhere else?
Thanks Rusty, now it works, i have added this lines:
I thought that using the PackageBasket would be enought to know the taxes, but it makes sense to use BillingAdress.
Great - the billing address is required to create an invoice.
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.