Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tito 314 posts 623 karma points
    Mar 28, 2016 @ 18:08
    Tito
    0

    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:

    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

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Mar 29, 2016 @ 00:00
    Rusty Swayne
    0

    In the global settings, can you check to see if you have the Shipping is taxable checkbox checked?

  • Tito 314 posts 623 karma points
    Mar 29, 2016 @ 09:00
    Tito
    0

    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);
            }
    
  • Tito 314 posts 623 karma points
    Mar 29, 2016 @ 09:03
    Tito
    0

    Something is not right as every time i call this method is adding new shipping quotes...

  • Tito 314 posts 623 karma points
    Mar 29, 2016 @ 09:12
    Tito
    100

    Now it works, this is the final code:

    public ActionResult GetImpuestos(string shippingMethodKey, string paisCOD="ES", string 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);
    
    
                var checkoutManager = this.Basket.GetCheckoutManager();
    
                if (quote != null)
                {
                    checkoutManager.Shipping.ClearShipmentRateQuotes();
                    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);
            }
    
  • Tito 314 posts 623 karma points
    Mar 29, 2016 @ 09:50
    Tito
    0

    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?

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Mar 29, 2016 @ 18:38
    Rusty Swayne
    0

    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.

  • Tito 314 posts 623 karma points
    Mar 30, 2016 @ 09:45
    Tito
    0

    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?

  • Tito 314 posts 623 karma points
    Apr 06, 2016 @ 09:19
    Tito
    0

    @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.

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Apr 07, 2016 @ 14:53
    Rusty Swayne
    0

    Hey Tito,

    Can you confirm you are passing the settings each time you instantiate the checkout manager?

  • Tito 314 posts 623 karma points
    Apr 07, 2016 @ 17:29
    Tito
    0

    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);
                }
    
  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Apr 07, 2016 @ 18:13
    Rusty Swayne
    0

    You have to have a Billing Address for the invoice - is that being added somewhere else?

  • Tito 314 posts 623 karma points
    Apr 08, 2016 @ 09:04
    Tito
    0

    Thanks Rusty, now it works, i have added this lines:

    checkoutManager.Customer.SaveShipToAddress(shipAd);
    checkoutManager.Customer.SaveBillToAddress(shipAd);
    

    I thought that using the PackageBasket would be enought to know the taxes, but it makes sense to use BillingAdress.

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Apr 08, 2016 @ 17:42
    Rusty Swayne
    0

    Great - the billing address is required to create an invoice.

  • 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.

Please Sign in or register to post replies