Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Adjustments Refactoring

Ryan Bigg
February 26, 2014

Adjustments Refactoring

A talk that goes into the reasons why we refactored Spree's adjustments system.

Ryan Bigg

February 26, 2014
Tweet

More Decks by Ryan Bigg

Other Decks in Programming

Transcript

  1. View Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. View Slide

  7. Adjusting
    Adjustments
    Ryan Bigg

    View Slide

  8. Orders

    View Slide

  9. View Slide

  10. 1. Retain order-wide
    adjustments
    "We screwed up. Have $20 off
    your whole order."

    View Slide

  11. 2. Discounts + Credits
    "Use 10off to get 10%
    off this weekend!"
    "We screwed up. Have $20 off
    your whole order."

    View Slide

  12. 3. Discounts for specific customers
    15% off for .edu customers!
    5% off for members!

    View Slide

  13. 4. Shipping taxation
    Shipping as a service.

    View Slide

  14. 5. Refund specific items
    $3.63 GST included
    10% GST
    "How much tax did this item incur?"

    View Slide

  15. 6. Freeze order adjustments

    View Slide

  16. 7. Refund VAT outside of EU

    View Slide

  17. 8. Allow multiple discounts
    per order
    "Can I combine a free shipping promotion and

    a 10% off promotion?"
    "You can't!"

    View Slide

  18. 9. Robust test suite
    ...........................................
    ...........................................
    ...........................................
    ...........................................
    ...........................................
    ...........................................
    ...........................................

    View Slide

  19. Events
    and
    Notifications

    View Slide

  20. fire_event('spree.cart.add')

    View Slide

  21. 1.Find all Spree::Activator instances
    with that event name

    2. Activate them

    View Slide

  22. #4270

    View Slide

  23. Events
    and
    Notifications

    View Slide

  24. A. Avoid excessive calculations
    B. Avoid multiple adjustments per line item
    C. Not overly complicate things
    Ground Rules

    View Slide

  25. A. Avoid excessive calculations
    B. Avoid multiple adjustments per line item
    C. Not overly complicate things
    Ground Rules

    View Slide

  26. 86,082
    lines

    View Slide

  27. A. Avoid excessive calculations
    B. Avoid multiple adjustments per line item
    C. Not overly complicate things
    Ground Rules

    View Slide

  28. A. Avoid excessive calculations
    B. Avoid multiple adjustments per line item
    Ground Rules

    View Slide

  29. http://www.ohloh.net/p/magento

    View Slide

  30. View Slide

  31. View Slide

  32. Sales Tax
    (it's complicated)

    View Slide

  33. Sales Tax
    (it's super complicated)

    View Slide

  34. New York Sales Tax = ?????

    View Slide

  35. "Clothing and footwear
    under $110 are exempt
    from New York City and
    NY State Sales Tax.
    !
    Purchases above $110
    are subject to a 4.5% NYC
    Sales Tax and a 4% NY
    State Sales Tax."
    http://www.nyc.gov/html/dof/html/business/nys_sales_tax.shtml

    View Slide

  36. New York Sales Tax = 0%

    View Slide

  37. http://www.irishtimes.com/blogs/generationemigration/files/2011/11/australian_flag_3.jpg
    10% Goods and Services Tax

    View Slide

  38. $3.63 GST included
    10% GST

    View Slide

  39. vatlive.com

    View Slide

  40. $6.66 VAT included
    20% VAT

    View Slide

  41. $6.38 VAT included
    19% VAT

    View Slide

  42. View Slide

  43. http://en.wikipedia.org/wiki/Sales_taxes_in_the_United_States

    View Slide

  44. taxcloud.net

    View Slide

  45. View Slide

  46. $0 - $4.68 extra
    0 - 11.725% Sales Tax

    View Slide

  47. "united" states

    View Slide

  48. OR

    View Slide

  49. Tax Categories
    Zones
    Rates

    View Slide

  50. Clothing
    Australia
    10%

    View Slide

  51. Food
    NY
    8.875%

    View Slide

  52. Old
    Adjustments

    View Slide

  53. Old Adjustments
    * Source - Thing doing the adjustment
    * Originator - Maybe same as source, maybe not
    * Adjustable - Thing being adjusted
    * Amount - Self-explanatory

    View Slide

  54. Tax Adjustment
    * Source = Order
    * Originator = TaxRate
    * Adjustable = Order

    View Slide

  55. Order Tax Rate
    create_tax_charge

    View Slide

  56. 1. tax included in price (i.e. GST)
    2. default tax zone is the same as the order's zone
    IF
    THEN
    Purchases in same country as store (price inc.)
    Adjustment
    Line Item
    Line Item
    Line Item
    Adjustment
    Adjustment
    1. Apply adjustments to every single line item

    View Slide

  57. Spree < 2.2 - 10% GST
    $1.82 tax incl.

    View Slide

  58. $1.82 tax incl.
    $1.82 tax incl.
    Spree < 2.2 - 10% GST

    View Slide

  59. 1. tax included in price (i.e. VAT)
    2. default tax zone is NOT the order's zone
    IF
    THEN
    1. apply one big negative adjustment to the entire order (a refund)
    Purchases outside of the country (price inc.)
    Adjustment
    Order

    View Slide

  60. Spree < 2.2 - 10% VAT
    $1.82 tax refund

    View Slide

  61. Spree < 2.2 - 10% VAT
    $1.82 tax refund
    $1.82 tax refund
    1 whole-order adjustment for $3.64

    View Slide

  62. 1. tax is NOT included in price (i.e. sales tax)
    THEN
    1. apply one big adjustment to the whole order
    Purchases in country (price excl.)
    Order Adjustment

    View Slide

  63. Spree < 2.2 - 7% Sales Tax (NY)
    + $1.39 tax

    View Slide

  64. Spree < 2.2 - 7% Sales Tax (NY)
    + $1.39 tax
    + $1.39 tax

    View Slide

  65. Spree < 2.2 - 7% Sales Tax (NY)
    1 whole-order adjustment for $2.78
    + $1.39 tax
    + $1.39 tax

    View Slide

  66. Adjustment
    Line Item
    Line Item
    Line Item
    Adjustment
    Adjustment
    Adjustment
    Order
    Order Adjustment
    Spree < 2.2

    View Slide

  67. Adjustment
    Line Item
    Line Item
    Line Item
    Adjustment
    Adjustment
    Adjustment
    Order
    Order Adjustment
    Spree < 2.2

    View Slide

  68. New
    Adjustments

    View Slide

  69. Adjustment
    Line Item
    Line Item
    Line Item
    Adjustment
    Adjustment
    Adjustment
    Adjustment
    Adjustment
    Line Item
    Line Item
    Line Item
    Spree 2.2+
    +

    View Slide

  70. 5. Refund specific items
    $3.63 GST included
    10% GST
    "How much tax did this item incur?"

    View Slide

  71. line_item.tax_total
    shipment.tax_total
    order.tax_total

    View Slide

  72. Included

    vs.
    Additional

    View Slide

  73. line_item.included_tax_total
    shipment.included_tax_total
    order.included_tax_total
    line_item.additional_tax_total
    shipment.additional_tax_total
    order.additional_tax_total

    View Slide

  74. Spree 2.2+ - 10% GST
    $3.63 tax incl.
    $1.82 tax incl.
    2 $39.98
    $59.97
    $59.97
    1 adjustment for $3.63,
    1 adjustment for $1.82

    View Slide

  75. Spree 2.2+ - 7% Sales Tax
    1 adjustment per line item for $1.39
    + $1.39 tax
    + $1.39 tax

    View Slide

  76. Spree 2.1+ Adjustments
    * Source
    * Originator
    * Adjustable
    * Amount
    * Included? - Counts towards final
    total

    View Slide

  77. * Source
    * Originator
    * Adjustable
    * Amount
    * Included? - Counts towards final
    total
    Spree 2.2+ Adjustments

    View Slide

  78. Shipment
    Adjustments

    View Slide

  79. Shipment Adjustment
    * Source = Shipment
    * Originator = ShippingMethod
    * Adjustable = Order
    Spree 2.1

    View Slide

  80. "Why does the cost
    of a shipment
    need to be
    an adjustment?"

    View Slide

  81. shipment.cost = 10

    View Slide

  82. Shipment
    Taxation

    View Slide

  83. Shipping methods now
    have a tax category
    tax category + zone =
    rate(?)

    View Slide

  84. Order
    create_tax_charge
    Tax Rate
    Line items
    Order
    create_tax_charge
    Tax Rate
    Shipments
    Spree 2.2. Taxation

    View Slide

  85. Shipment Adjustment
    * Source = Shipment
    * Originator = ShippingMethod
    * Adjustable = Order
    Spree 2.1
    Shipment Adjustment
    * Source = TaxRate
    * Adjustable = Shipment
    Spree 2.2

    View Slide

  86. Tax Adjustment
    * Source = TaxRate
    * Adjustable = LineItem
    OR
    * Adjustable = Shipment

    View Slide

  87. Promotion
    Adjustments

    View Slide

  88. Order Promotions
    * Source = Order
    * Originator = PromotionAction
    * Adjustable = Order

    View Slide

  89. Spree < 2.2 - 10% off promotion
    $2 off

    View Slide

  90. $2 off
    $2 off
    Spree < 2.2 - 10% off promotion
    1 Order Adjustment for -$4

    View Slide

  91. $2 off
    $2 off
    Spree 2.2+ - 10% off promotion
    1 adjustment per line item for -$2

    View Slide

  92. Promotion
    Handlers

    View Slide

  93. Promotion Handlers
    Order Promotion
    Cart, Coupon, Free Shipping, or Page
    Promotion::Handler
    New in Spree 2.2!

    View Slide

  94. Promotion Handlers
    Cart
    1. Find all non-code or path promotions
    2. Check if they're eligible for that line item or order
    3. Activate

    View Slide

  95. Promotion Handlers
    Coupon
    1. Find promotion matching coupon code
    2. Validate promotion usage limit & eligiblity
    3. Attempt application of promotion to order
    4. Report on success of application

    View Slide

  96. Promotion Handlers
    Free Shipping
    1. Find all 'Free Shipping' promotion actions
    2. Check eligibility for order
    3. Activate

    View Slide

  97. Promotion Handlers
    Page
    1. Find promotion for specified path
    2. Check eligibility for order
    3. Activate

    View Slide

  98. Picking
    a winner

    View Slide

  99. 10% off!
    adjustment
    LineItem
    $10 off!
    adjustment

    View Slide

  100. $1
    LineItem!
    @ $10
    $10

    View Slide

  101. $15
    LineItem!
    @ $150
    $10

    View Slide

  102. #4270
    Complex promotion rules

    View Slide

  103. In summary…

    View Slide

  104. "Can I combine
    a free shipping promotion
    and
    a 10% off promotion? "
    "You can!"
    ORDER SUMMARY
    Item Total: $1,599.00
    Sales Tax $80.45
    -
    Free Shipping
    Promotion (10% off)
    Order Total: $1,511.51
    -$167.95

    View Slide

  105. PromotionAction::CreateAdjustment
    Commandment #1:
    Order Wide Adjustments

    View Slide

  106. Commandment #2: Discounts + Credits
    Yes Not yet

    View Slide

  107. Commandment #3: Group discounts
    Soon.

    View Slide

  108. Commandment #4: Shipping taxation
    Yes.

    View Slide

  109. Commandment #5: Refunding of items
    Soon.

    View Slide

  110. Commandment #6: Adjustment freezing
    It's always been that way.

    View Slide

  111. Commandment #7: VAT Refunding
    Of course!
    Adjustment
    Adjustment
    Adjustment
    Line Item
    Line Item
    Line Item

    View Slide

  112. Commandment #8: Multiple discounts per order
    One promotion adjustment.
    One credit adjustment.
    One taxation adjustment.

    View Slide

  113. Commandment #9: Robust test suite.
    Definitely.

    View Slide

  114. Commandment A: Avoid excessive calculations.
    Calculations at an all time low.

    View Slide

  115. Commandment B: Avoid multiple adjustments per item.
    Oops.
    One promotion adjustment.
    One credit adjustment.
    One taxation adjustment.

    View Slide

  116. Commandment i: Line item adjustments.
    One promotion adjustment.
    One credit adjustment.
    One taxation adjustment.

    View Slide

  117. Commandment ii: Some form of inheritance.
    Inheritance free zone.

    View Slide

  118. Commandment iii: Take advantage of
    calculators and rules
    Done.

    View Slide

  119. Commandment iv: Events should be scrapped.
    Gone.

    View Slide

  120. Commandment v: Notifications should be scrapped.
    Gone.
    Replaced with Handlers.
    Gone.

    View Slide

  121. We now have an adjustments system which is:
    SIMPLE

    View Slide

  122. We now have an adjustments system which is:
    CONSISTENT
    Adjustment
    LineItem
    Shipment Adjustment

    View Slide

  123. We now have an adjustments system which is:
    FLEXIBLE

    View Slide

  124. View Slide