spina-shop 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +37 -0
- data/app/assets/config/spina_shop_manifest.js +0 -0
- data/app/assets/fonts/montserrat-bold.ttf +0 -0
- data/app/assets/fonts/montserrat-italic.ttf +0 -0
- data/app/assets/fonts/montserrat-medium.ttf +0 -0
- data/app/assets/fonts/montserrat-regular.ttf +0 -0
- data/app/assets/fonts/montserrat-semibold.ttf +0 -0
- data/app/assets/images/spina/shop/delete-big-confirm.png +0 -0
- data/app/assets/images/spina/shop/delete-big.png +0 -0
- data/app/assets/javascripts/spina/shop/admin/app.infinitescroll.js.coffee +23 -0
- data/app/assets/javascripts/spina/shop/admin/customers.coffee +27 -0
- data/app/assets/javascripts/spina/shop/admin/orders.coffee +18 -0
- data/app/assets/javascripts/spina/shop/admin/product_bundles.coffee +15 -0
- data/app/assets/javascripts/spina/shop/admin/product_categories.coffee +15 -0
- data/app/assets/javascripts/spina/shop/admin/products.coffee +88 -0
- data/app/assets/javascripts/spina/shop/admin/shop.js +10 -0
- data/app/assets/stylesheets/spina/shop/admin/_customers.sass +10 -0
- data/app/assets/stylesheets/spina/shop/admin/_forms.sass +191 -0
- data/app/assets/stylesheets/spina/shop/admin/_product_bundles.sass +12 -0
- data/app/assets/stylesheets/spina/shop/admin/_status_dots.sass +16 -0
- data/app/assets/stylesheets/spina/shop/admin/_tables.sass +2 -0
- data/app/assets/stylesheets/spina/shop/admin/_tooltips.sass +31 -0
- data/app/assets/stylesheets/spina/shop/admin/shop.css.sass +11 -0
- data/app/controllers/spina/shop/admin/admin_controller.rb +8 -0
- data/app/controllers/spina/shop/admin/customer_accounts_controller.rb +38 -0
- data/app/controllers/spina/shop/admin/customers_controller.rb +67 -0
- data/app/controllers/spina/shop/admin/discounts_controller.rb +65 -0
- data/app/controllers/spina/shop/admin/gift_cards_controller.rb +65 -0
- data/app/controllers/spina/shop/admin/invoices_controller.rb +12 -0
- data/app/controllers/spina/shop/admin/order_items_controller.rb +36 -0
- data/app/controllers/spina/shop/admin/orders/packing_slips_controller.rb +27 -0
- data/app/controllers/spina/shop/admin/orders/shipping_labels_controller.rb +19 -0
- data/app/controllers/spina/shop/admin/orders_controller.rb +123 -0
- data/app/controllers/spina/shop/admin/product_reviews_controller.rb +17 -0
- data/app/controllers/spina/shop/admin/products/product_bundles_controller.rb +62 -0
- data/app/controllers/spina/shop/admin/products/stock_forecasts_controller.rb +15 -0
- data/app/controllers/spina/shop/admin/products/stock_level_adjustments_controller.rb +33 -0
- data/app/controllers/spina/shop/admin/products_controller.rb +135 -0
- data/app/controllers/spina/shop/admin/reports_controller.rb +33 -0
- data/app/controllers/spina/shop/admin/settings/product_categories_controller.rb +45 -0
- data/app/controllers/spina/shop/admin/settings/product_category_properties_controller.rb +49 -0
- data/app/controllers/spina/shop/admin/settings/sales_categories_controller.rb +14 -0
- data/app/controllers/spina/shop/admin/settings/shop_controller.rb +9 -0
- data/app/controllers/spina/shop/admin/settings/tax_groups_controller.rb +47 -0
- data/app/controllers/spina/shop/admin/variants_controller.rb +8 -0
- data/app/jobs/spina/shop/in_stock_reminder_job.rb +17 -0
- data/app/jobs/spina/shop/invoice_report_job.rb +23 -0
- data/app/jobs/spina/shop/payments_report_job.rb +23 -0
- data/app/mailers/spina/shop/customer_account_mailer.rb +32 -0
- data/app/mailers/spina/shop/export_mailer.rb +22 -0
- data/app/mailers/spina/shop/in_stock_reminder_mailer.rb +22 -0
- data/app/mailers/spina/shop/order_mailer.rb +32 -0
- data/app/models/concerns/spina/shop/preferable.rb +27 -0
- data/app/models/concerns/spina/shop/tax_rateable.rb +11 -0
- data/app/models/spina/shop/address.rb +19 -0
- data/app/models/spina/shop/application_record.rb +5 -0
- data/app/models/spina/shop/bundled_product.rb +6 -0
- data/app/models/spina/shop/campaign.rb +4 -0
- data/app/models/spina/shop/collectable.rb +8 -0
- data/app/models/spina/shop/country.rb +5 -0
- data/app/models/spina/shop/customer.rb +60 -0
- data/app/models/spina/shop/customer_account.rb +15 -0
- data/app/models/spina/shop/customer_group.rb +9 -0
- data/app/models/spina/shop/delivery_option.rb +27 -0
- data/app/models/spina/shop/discount.rb +39 -0
- data/app/models/spina/shop/discount_action.rb +16 -0
- data/app/models/spina/shop/discount_rule.rb +17 -0
- data/app/models/spina/shop/discounts/actions/percent_off.rb +17 -0
- data/app/models/spina/shop/discounts/rules/all_orders.rb +13 -0
- data/app/models/spina/shop/discounts/rules/collection.rb +14 -0
- data/app/models/spina/shop/discounts/rules/order_total.rb +17 -0
- data/app/models/spina/shop/discounts_order.rb +6 -0
- data/app/models/spina/shop/favorite.rb +8 -0
- data/app/models/spina/shop/gift_card.rb +53 -0
- data/app/models/spina/shop/gift_cards_order.rb +6 -0
- data/app/models/spina/shop/in_stock_reminder.rb +7 -0
- data/app/models/spina/shop/invoice.rb +49 -0
- data/app/models/spina/shop/invoice_line.rb +13 -0
- data/app/models/spina/shop/number_sequence.rb +19 -0
- data/app/models/spina/shop/order.rb +222 -0
- data/app/models/spina/shop/order/billing.rb +85 -0
- data/app/models/spina/shop/order/state_machine_transitions.rb +106 -0
- data/app/models/spina/shop/order_attachment.rb +9 -0
- data/app/models/spina/shop/order_item.rb +101 -0
- data/app/models/spina/shop/order_transition.rb +5 -0
- data/app/models/spina/shop/product.rb +227 -0
- data/app/models/spina/shop/product_bundle.rb +51 -0
- data/app/models/spina/shop/product_bundle_part.rb +13 -0
- data/app/models/spina/shop/product_category.rb +18 -0
- data/app/models/spina/shop/product_category_property.rb +14 -0
- data/app/models/spina/shop/product_collection.rb +8 -0
- data/app/models/spina/shop/product_image.rb +18 -0
- data/app/models/spina/shop/product_item.rb +93 -0
- data/app/models/spina/shop/product_part.rb +13 -0
- data/app/models/spina/shop/product_relation.rb +6 -0
- data/app/models/spina/shop/product_review.rb +22 -0
- data/app/models/spina/shop/property_option.rb +16 -0
- data/app/models/spina/shop/sales_category.rb +43 -0
- data/app/models/spina/shop/sales_category_code.rb +20 -0
- data/app/models/spina/shop/shop_review.rb +19 -0
- data/app/models/spina/shop/stock_level_adjustment.rb +17 -0
- data/app/models/spina/shop/tax_group.rb +62 -0
- data/app/models/spina/shop/tax_rate.rb +20 -0
- data/app/models/spina/shop/zone.rb +11 -0
- data/app/pdfs/spina/shop/invoice_pdf.rb +114 -0
- data/app/pdfs/spina/shop/packing_slip_pdf.rb +74 -0
- data/app/presenters/spina/shop/invoice_presenter.rb +30 -0
- data/app/services/spina/shop/allocate_stock.rb +42 -0
- data/app/services/spina/shop/cache_order.rb +47 -0
- data/app/services/spina/shop/change_stock_level.rb +38 -0
- data/app/services/spina/shop/country_importer.rb +14 -0
- data/app/services/spina/shop/customer_generator.rb +32 -0
- data/app/services/spina/shop/deallocate_stock.rb +17 -0
- data/app/services/spina/shop/invoice_generator.rb +72 -0
- data/app/services/spina/shop/invoice_number_generator.rb +10 -0
- data/app/services/spina/shop/invoices_pdfs_exporter.rb +22 -0
- data/app/services/spina/shop/order_number_generator.rb +10 -0
- data/app/services/spina/shop/payments_excel_exporter.rb +41 -0
- data/app/services/spina/shop/store_address.rb +23 -0
- data/app/state_machines/spina/shop/order_state_machine.rb +116 -0
- data/app/uploaders/spina/shop/exports_uploader.rb +9 -0
- data/app/views/layouts/spina/shop/admin/settings/shop.html.haml +16 -0
- data/app/views/layouts/spina/shop/mail.html.erb +1 -0
- data/app/views/spina/admin/hooks/shop/_head.html.haml +2 -0
- data/app/views/spina/admin/hooks/shop/_primary_navigation.html.haml +31 -0
- data/app/views/spina/admin/hooks/shop/_settings_secondary_navigation.html.haml +2 -0
- data/app/views/spina/admin/page_partables/shop/product_bundle_parts/_form.html.haml +6 -0
- data/app/views/spina/admin/page_partables/shop/product_parts/_form.html.haml +6 -0
- data/app/views/spina/admin/structure_partables/shop/product_parts/_form.html.haml +3 -0
- data/app/views/spina/shop/admin/customer_accounts/_form.html.haml +32 -0
- data/app/views/spina/shop/admin/customer_accounts/edit.html.haml +1 -0
- data/app/views/spina/shop/admin/customers/_address_fields.html.haml +46 -0
- data/app/views/spina/shop/admin/customers/_customer.html.haml +11 -0
- data/app/views/spina/shop/admin/customers/_form.html.haml +67 -0
- data/app/views/spina/shop/admin/customers/edit.html.haml +1 -0
- data/app/views/spina/shop/admin/customers/index.html.haml +52 -0
- data/app/views/spina/shop/admin/customers/index.js.erb +4 -0
- data/app/views/spina/shop/admin/customers/new.html.haml +1 -0
- data/app/views/spina/shop/admin/customers/show.html.haml +121 -0
- data/app/views/spina/shop/admin/discounts/_form.html.haml +60 -0
- data/app/views/spina/shop/admin/discounts/edit.html.haml +1 -0
- data/app/views/spina/shop/admin/discounts/fields/_all_orders.html.haml +0 -0
- data/app/views/spina/shop/admin/discounts/fields/_discount_action_fields.html.haml +8 -0
- data/app/views/spina/shop/admin/discounts/fields/_discount_rule_fields.html.haml +0 -0
- data/app/views/spina/shop/admin/discounts/index.html.haml +36 -0
- data/app/views/spina/shop/admin/discounts/new.html.haml +1 -0
- data/app/views/spina/shop/admin/gift_cards/_form.html.haml +28 -0
- data/app/views/spina/shop/admin/gift_cards/_gift_card.html.haml +17 -0
- data/app/views/spina/shop/admin/gift_cards/index.html.haml +37 -0
- data/app/views/spina/shop/admin/gift_cards/index.js.erb +4 -0
- data/app/views/spina/shop/admin/gift_cards/new.html.haml +1 -0
- data/app/views/spina/shop/admin/gift_cards/show.html.haml +46 -0
- data/app/views/spina/shop/admin/order_items/_modal.html.haml +18 -0
- data/app/views/spina/shop/admin/order_items/new.js.erb +2 -0
- data/app/views/spina/shop/admin/orders/_form.html.haml +159 -0
- data/app/views/spina/shop/admin/orders/_order.html.haml +22 -0
- data/app/views/spina/shop/admin/orders/edit.html.haml +1 -0
- data/app/views/spina/shop/admin/orders/index.html.haml +78 -0
- data/app/views/spina/shop/admin/orders/index.js.erb +4 -0
- data/app/views/spina/shop/admin/orders/new.html.haml +1 -0
- data/app/views/spina/shop/admin/orders/show.html.haml +338 -0
- data/app/views/spina/shop/admin/product_reviews/_product_review.html.haml +22 -0
- data/app/views/spina/shop/admin/product_reviews/index.html.haml +29 -0
- data/app/views/spina/shop/admin/product_reviews/index.js.erb +4 -0
- data/app/views/spina/shop/admin/products/_form.html.haml +259 -0
- data/app/views/spina/shop/admin/products/_new_product_modal.html.haml +15 -0
- data/app/views/spina/shop/admin/products/_price_exception_fields.html.haml +11 -0
- data/app/views/spina/shop/admin/products/_product.html.haml +36 -0
- data/app/views/spina/shop/admin/products/edit.html.haml +1 -0
- data/app/views/spina/shop/admin/products/index.html.haml +73 -0
- data/app/views/spina/shop/admin/products/index.js.erb +4 -0
- data/app/views/spina/shop/admin/products/new.html.haml +1 -0
- data/app/views/spina/shop/admin/products/new_by_category.js.erb +1 -0
- data/app/views/spina/shop/admin/products/product_bundles/_bundled_products_fields.html.haml +11 -0
- data/app/views/spina/shop/admin/products/product_bundles/_form.html.haml +64 -0
- data/app/views/spina/shop/admin/products/product_bundles/edit.html.haml +1 -0
- data/app/views/spina/shop/admin/products/product_bundles/index.html.haml +42 -0
- data/app/views/spina/shop/admin/products/product_bundles/new.html.haml +1 -0
- data/app/views/spina/shop/admin/products/properties/_property_fields.html.haml +37 -0
- data/app/views/spina/shop/admin/products/stock_forecasts/_product.html.haml +49 -0
- data/app/views/spina/shop/admin/products/stock_forecasts/show.html.haml +47 -0
- data/app/views/spina/shop/admin/products/stock_forecasts/show.js.erb +4 -0
- data/app/views/spina/shop/admin/products/stock_level_adjustments/_form.html.haml +37 -0
- data/app/views/spina/shop/admin/products/stock_level_adjustments/_history.html.haml +30 -0
- data/app/views/spina/shop/admin/products/stock_level_adjustments/index.js.erb +1 -0
- data/app/views/spina/shop/admin/products/stock_level_adjustments/new.js.erb +1 -0
- data/app/views/spina/shop/admin/reports/index.html.haml +23 -0
- data/app/views/spina/shop/admin/settings/product_categories/_form.html.haml +18 -0
- data/app/views/spina/shop/admin/settings/product_categories/_product_category.html.haml +3 -0
- data/app/views/spina/shop/admin/settings/product_categories/_property_fields.html.haml +77 -0
- data/app/views/spina/shop/admin/settings/product_categories/edit.html.haml +1 -0
- data/app/views/spina/shop/admin/settings/product_categories/index.html.haml +3 -0
- data/app/views/spina/shop/admin/settings/product_categories/show.html.haml +24 -0
- data/app/views/spina/shop/admin/settings/product_category_properties/_property_options_fields.html.haml +11 -0
- data/app/views/spina/shop/admin/settings/product_category_properties/edit_options.html.haml +16 -0
- data/app/views/spina/shop/admin/settings/sales_categories/_sales_category.html.haml +17 -0
- data/app/views/spina/shop/admin/settings/sales_categories/index.html.haml +1 -0
- data/app/views/spina/shop/admin/settings/tax_groups/_form.html.haml +19 -0
- data/app/views/spina/shop/admin/settings/tax_groups/_tax_group.html.haml +21 -0
- data/app/views/spina/shop/admin/settings/tax_groups/index.html.haml +1 -0
- data/app/views/spina/shop/admin/settings/tax_groups/new.js.erb +1 -0
- data/app/views/spina/shop/admin/shared/_locale_switcher.html.haml +9 -0
- data/app/views/spina/shop/customer_account_mailer/forgot_password.html.erb +1 -0
- data/app/views/spina/shop/customer_account_mailer/forgot_password.txt.erb +1 -0
- data/app/views/spina/shop/export_mailer/exported.html.erb +2 -0
- data/app/views/spina/shop/in_stock_reminder_mailer/reminder.html.erb +1 -0
- data/app/views/spina/shop/in_stock_reminder_mailer/reminder.txt.erb +1 -0
- data/app/views/spina/shop/order_mailer/confirmation.html.erb +5 -0
- data/app/views/spina/shop/order_mailer/confirmation.txt.erb +5 -0
- data/app/views/spina/shop/order_mailer/shipped.html.erb +1 -0
- data/app/views/spina/shop/order_mailer/shipped.txt.erb +1 -0
- data/config/initializers/kaminari_monkey_patch.rb +15 -0
- data/config/locales/en.yml +214 -0
- data/config/locales/nl.yml +452 -0
- data/config/routes.rb +92 -0
- data/db/countries.txt +252 -0
- data/db/migrate/10_create_spina_shop_products_without_items.rb +23 -0
- data/db/migrate/1_create_spina_shop_tables.rb +518 -0
- data/db/migrate/20170701135207_create_spina_shop_bundled_products.rb +11 -0
- data/db/migrate/20170717125930_add_stock_enabled_to_spina_shop_products.rb +5 -0
- data/db/migrate/20170718084146_add_editable_sku_to_spina_shop_products.rb +5 -0
- data/db/migrate/20170718084626_add_deletable_to_spina_shop_products.rb +5 -0
- data/db/migrate/20170719082531_rename_column_order_picked_at_in_spina_shop_orders.rb +5 -0
- data/db/migrate/20170719112920_create_spina_shop_order_attachments.rb +15 -0
- data/db/migrate/20170719184125_add_price_exceptions_to_spina_shop_products.rb +5 -0
- data/db/migrate/20170720133304_add_vat_id_to_spina_shop_customers.rb +5 -0
- data/db/migrate/20170731082027_add_price_includes_tax_to_spina_shop_products.rb +5 -0
- data/db/migrate/20170731132620_add_reverse_charge_vat_to_spina_shop_tax_rates.rb +5 -0
- data/db/migrate/20170731150630_add_vat_id_to_spina_shop_invoices.rb +5 -0
- data/db/migrate/20170801093935_add_price_includes_tax_to_spina_shop_delivery_options.rb +5 -0
- data/db/migrate/20170814092924_add_business_to_spina_shop_orders.rb +6 -0
- data/db/migrate/20170815080839_rename_vat_reverse_charge_column_on_tax_rates.rb +5 -0
- data/db/migrate/20170815081018_add_business_to_spina_shop_tax_rates.rb +5 -0
- data/db/migrate/20170817075706_add_archived_to_spina_shop_products.rb +5 -0
- data/db/migrate/20170824175231_add_metadata_to_photos.rb +7 -0
- data/db/migrate/20170829085402_add_business_to_spina_shop_sales_category_codes.rb +5 -0
- data/db/migrate/20170911121034_add_trend_to_spina_shop_products.rb +5 -0
- data/db/migrate/20171106122225_add_editable_fields_to_spina_shop_product_category_properties.rb +6 -0
- data/db/migrate/20171106122537_create_property_options.rb +19 -0
- data/db/migrate/20171118110256_add_promotional_price_to_spina_shop_products.rb +6 -0
- data/db/migrate/2_create_spina_shop_tax_rates.rb +15 -0
- data/db/migrate/3_create_spina_shop_zones.rb +9 -0
- data/db/migrate/4_add_tax_rateable_to_spina_shop_tax_rates.rb +9 -0
- data/db/migrate/5_add_type_to_spina_shop_zones.rb +5 -0
- data/db/migrate/6_add_code_to_spina_shop_zones.rb +5 -0
- data/db/migrate/7_create_spina_shop_product_collections.rb +16 -0
- data/db/migrate/8_create_spina_shop_sales_category_codes.rb +13 -0
- data/db/migrate/9_create_spina_shop_customer_groups.rb +11 -0
- data/lib/generators/spina/shop/install_generator.rb +44 -0
- data/lib/spina/shop.rb +24 -0
- data/lib/spina/shop/admin_helpers.rb +49 -0
- data/lib/spina/shop/engine.rb +27 -0
- data/lib/spina/shop/errors/empty_order.rb +6 -0
- data/lib/spina/shop/errors/order_already_received.rb +6 -0
- data/lib/spina/shop/railtie.rb +31 -0
- data/lib/spina/shop/simple_linear_regression.rb +36 -0
- data/lib/spina/shop/version.rb +5 -0
- data/lib/spina/shop/view_helpers.rb +11 -0
- data/lib/tasks/spina/shop.rake +32 -0
- metadata +627 -0
@@ -0,0 +1,85 @@
|
|
1
|
+
module Spina::Shop
|
2
|
+
class Order < ApplicationRecord
|
3
|
+
|
4
|
+
def order_total
|
5
|
+
order_items.inject(BigDecimal(0)) { |t, i| t + i.total }
|
6
|
+
end
|
7
|
+
|
8
|
+
def delivery_price
|
9
|
+
read_attribute(:delivery_price) || delivery_option.try(:price_for_order, self) || BigDecimal(0)
|
10
|
+
end
|
11
|
+
|
12
|
+
def delivery_tax_rate
|
13
|
+
read_attribute(:delivery_tax_rate) || delivery_option.try(:tax_group).try(:tax_rate_for_order, self) || BigDecimal(0)
|
14
|
+
end
|
15
|
+
|
16
|
+
def delivery_tax_modifier
|
17
|
+
(delivery_tax_rate + BigDecimal(100)) / BigDecimal(100)
|
18
|
+
end
|
19
|
+
|
20
|
+
def gift_card_amount
|
21
|
+
read_attribute(:gift_card_amount) || gift_card.try(:amount_for_order, self) || BigDecimal(0)
|
22
|
+
end
|
23
|
+
|
24
|
+
def billing_first_name
|
25
|
+
first_name
|
26
|
+
end
|
27
|
+
|
28
|
+
def billing_last_name
|
29
|
+
last_name
|
30
|
+
end
|
31
|
+
|
32
|
+
# Total of the order
|
33
|
+
def total
|
34
|
+
order_total + delivery_price + (prices_include_tax ? 0 : tax_amount)
|
35
|
+
end
|
36
|
+
|
37
|
+
def total_excluding_tax
|
38
|
+
total - tax_amount
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_be_paid
|
42
|
+
total - gift_card_amount
|
43
|
+
end
|
44
|
+
|
45
|
+
# Mandatory rounding to 0.05 for cash payments in a store
|
46
|
+
def to_be_paid_round
|
47
|
+
(to_be_paid * 2).round(1, :half_up) / 2
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_be_paid_rounding_difference
|
51
|
+
to_be_paid_round - to_be_paid
|
52
|
+
end
|
53
|
+
|
54
|
+
def nothing_owed?
|
55
|
+
paid? || to_be_paid == BigDecimal(0)
|
56
|
+
end
|
57
|
+
|
58
|
+
def tax_amount
|
59
|
+
tax_amount_by_rates.inject(BigDecimal.new(0)) { |t, r| t + r[1][:tax_amount] }
|
60
|
+
end
|
61
|
+
|
62
|
+
def tax_amount_by_rates
|
63
|
+
if vat_reverse_charge?
|
64
|
+
rates = {"0": {tax_amount: BigDecimal.new(0), total: BigDecimal.new(0)}}
|
65
|
+
else
|
66
|
+
items = [order_items]
|
67
|
+
items << OpenStruct.new(tax_rate: delivery_tax_rate, tax_modifier: delivery_tax_modifier, total: delivery_price) if delivery_option.present?
|
68
|
+
rates = items.flatten.inject({}) do |h, item|
|
69
|
+
rate = h[item.tax_rate] ||= { tax_amount: BigDecimal(0), total: BigDecimal(0) }
|
70
|
+
if prices_include_tax
|
71
|
+
rate[:total] += (item.total / item.tax_modifier).round(2)
|
72
|
+
rate[:tax_amount] += item.total - (item.total / item.tax_modifier).round(2)
|
73
|
+
else
|
74
|
+
rate[:total] += item.total
|
75
|
+
rate[:tax_amount] += (item.total * (BigDecimal(item.tax_rate) / BigDecimal(100))).round(2)
|
76
|
+
end
|
77
|
+
h
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
rates.sort{|x, y| y[0] <=> x[0]}.to_h
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module Spina::Shop
|
2
|
+
class Order < ApplicationRecord
|
3
|
+
include Statesman::Adapters::ActiveRecordQueries
|
4
|
+
|
5
|
+
delegate :can_transition_to?, :allowed_transitions, :history, :in_state?, :last_transition, :transition_to!, :transition_to, :current_state, to: :state_machine
|
6
|
+
|
7
|
+
def received?
|
8
|
+
received_at.present?
|
9
|
+
end
|
10
|
+
|
11
|
+
def failed?
|
12
|
+
failed_at.present?
|
13
|
+
end
|
14
|
+
|
15
|
+
def cancelled?
|
16
|
+
cancelled_at.present?
|
17
|
+
end
|
18
|
+
|
19
|
+
def confirmed?
|
20
|
+
confirming_at.present?
|
21
|
+
end
|
22
|
+
|
23
|
+
def paid?
|
24
|
+
paid_at.present?
|
25
|
+
end
|
26
|
+
|
27
|
+
def order_prepared?
|
28
|
+
order_prepared_at.present?
|
29
|
+
end
|
30
|
+
|
31
|
+
def shipped?
|
32
|
+
shipped_at.present?
|
33
|
+
end
|
34
|
+
|
35
|
+
def delivered?
|
36
|
+
delivered_at.present?
|
37
|
+
end
|
38
|
+
|
39
|
+
def picked_up?
|
40
|
+
picked_up_at.present?
|
41
|
+
end
|
42
|
+
|
43
|
+
def building?
|
44
|
+
current_state == 'building'
|
45
|
+
end
|
46
|
+
|
47
|
+
def confirming?
|
48
|
+
current_state == 'confirming'
|
49
|
+
end
|
50
|
+
|
51
|
+
def status_progress
|
52
|
+
if delivered? || picked_up?
|
53
|
+
100
|
54
|
+
elsif shipped?
|
55
|
+
80
|
56
|
+
elsif order_prepared?
|
57
|
+
60
|
58
|
+
elsif paid?
|
59
|
+
40
|
60
|
+
elsif received?
|
61
|
+
20
|
62
|
+
else
|
63
|
+
0
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def status_css_class
|
68
|
+
case current_state
|
69
|
+
when 'preparing'
|
70
|
+
'primary'
|
71
|
+
when 'paid'
|
72
|
+
'primary'
|
73
|
+
when 'shipped'
|
74
|
+
'success'
|
75
|
+
when 'delivered'
|
76
|
+
'success'
|
77
|
+
when 'picked_up'
|
78
|
+
'success'
|
79
|
+
when 'failed'
|
80
|
+
'danger'
|
81
|
+
when 'cancelled'
|
82
|
+
'danger'
|
83
|
+
else
|
84
|
+
''
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def state_machine
|
89
|
+
@state_machine ||= OrderStateMachine.new(self, transition_class: OrderTransition, association_name: :order_transitions)
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.transition_name
|
93
|
+
:order_transitions
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.transition_class
|
97
|
+
OrderTransition
|
98
|
+
end
|
99
|
+
private_class_method :transition_class
|
100
|
+
|
101
|
+
def self.initial_state
|
102
|
+
:building
|
103
|
+
end
|
104
|
+
private_class_method :initial_state
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Spina::Shop
|
2
|
+
class OrderItem < ApplicationRecord
|
3
|
+
attr_accessor :validate_stock
|
4
|
+
|
5
|
+
belongs_to :order
|
6
|
+
belongs_to :orderable, polymorphic: true
|
7
|
+
|
8
|
+
has_many :stock_level_adjustments, dependent: :nullify # Don't destroy the stock level adjustments automatically if an order is destroyed
|
9
|
+
|
10
|
+
validates :unit_price, :unit_cost_price, :tax_rate, :weight, presence: true, if: -> { order.try(:received?) }
|
11
|
+
validate :item_must_be_in_stock, if: -> { validate_stock }
|
12
|
+
validates :order_id, uniqueness: {scope: [:orderable_id, :orderable_type]}
|
13
|
+
|
14
|
+
scope :ordered, -> { order(:created_at) }
|
15
|
+
|
16
|
+
def unit_price
|
17
|
+
read_attribute(:unit_price) || orderable.price_for_order(order) || BigDecimal(0)
|
18
|
+
end
|
19
|
+
|
20
|
+
def unit_cost_price
|
21
|
+
read_attribute(:unit_cost_price) || orderable.cost_price || BigDecimal(0)
|
22
|
+
end
|
23
|
+
|
24
|
+
def tax_rate
|
25
|
+
read_attribute(:tax_rate) || orderable.tax_group.tax_rate_for_order(order) || BigDecimal(0)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Discount as amount
|
29
|
+
def discount_amount
|
30
|
+
read_attribute(:discount_amount) || order.discount.try(:discount_for_order_item, self) || BigDecimal(0)
|
31
|
+
end
|
32
|
+
|
33
|
+
def weight
|
34
|
+
read_attribute(:weight) || orderable.weight || BigDecimal(0)
|
35
|
+
end
|
36
|
+
|
37
|
+
def description
|
38
|
+
orderable.name
|
39
|
+
end
|
40
|
+
|
41
|
+
def total_without_discount
|
42
|
+
unit_price * quantity
|
43
|
+
end
|
44
|
+
|
45
|
+
def total
|
46
|
+
total_without_discount - discount_amount
|
47
|
+
end
|
48
|
+
|
49
|
+
def tax_modifier
|
50
|
+
(tax_rate + BigDecimal(100)) / BigDecimal(100)
|
51
|
+
end
|
52
|
+
|
53
|
+
def total_weight
|
54
|
+
quantity * weight
|
55
|
+
end
|
56
|
+
|
57
|
+
def in_stock?
|
58
|
+
if is_product_bundle?
|
59
|
+
orderable.bundled_products.all?{|p| product_in_stock?(p.product_id)}
|
60
|
+
else
|
61
|
+
product_in_stock?(orderable_id)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def is_product_bundle?
|
66
|
+
orderable_type == "Spina::Shop::ProductBundle"
|
67
|
+
end
|
68
|
+
|
69
|
+
def allocated_stock(product_id)
|
70
|
+
-stock_level_adjustments.where(product_id: product_id).sum(:adjustment)
|
71
|
+
end
|
72
|
+
|
73
|
+
def unallocated_stock(product_id)
|
74
|
+
multiplier = if is_product_bundle?
|
75
|
+
orderable.bundled_products.find_by(product_id: product_id).try(:quantity).to_i
|
76
|
+
else
|
77
|
+
orderable_id == product_id ? 1 : 0
|
78
|
+
end
|
79
|
+
quantity * multiplier - allocated_stock(product_id)
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def product_in_stock?(product_id)
|
85
|
+
# Add itself to an array of order items
|
86
|
+
# If the record isn't persisted yet it won't show up in order.order_items
|
87
|
+
order_items = order.order_items.to_a | [self]
|
88
|
+
product = Product.find(product_id)
|
89
|
+
return true unless product.stock_enabled
|
90
|
+
product.stock_level >= order_items.inject(BigDecimal(0)) do |total, order_item|
|
91
|
+
order_item.quantity = quantity if order_item == self
|
92
|
+
total + order_item.unallocated_stock(product_id)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def item_must_be_in_stock
|
97
|
+
errors.add(:stock_level, "not sufficient") unless in_stock?
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,227 @@
|
|
1
|
+
module Spina::Shop
|
2
|
+
class Product < ApplicationRecord
|
3
|
+
# Stores the old path when generating a new materialized_path
|
4
|
+
attr_accessor :old_path
|
5
|
+
|
6
|
+
belongs_to :tax_group
|
7
|
+
belongs_to :sales_category
|
8
|
+
belongs_to :product_category, optional: true
|
9
|
+
|
10
|
+
has_many :product_images, dependent: :destroy
|
11
|
+
has_many :product_reviews, dependent: :destroy
|
12
|
+
has_many :product_relations, dependent: :destroy
|
13
|
+
has_many :related_products, through: :product_relations
|
14
|
+
has_many :favorites, dependent: :destroy
|
15
|
+
has_many :collectables, dependent: :destroy
|
16
|
+
has_many :product_collections, through: :collectables
|
17
|
+
|
18
|
+
has_many :order_items, as: :orderable, dependent: :restrict_with_exception
|
19
|
+
has_many :stock_level_adjustments, dependent: :destroy
|
20
|
+
has_many :in_stock_reminders, as: :orderable, dependent: :destroy
|
21
|
+
|
22
|
+
has_many :bundled_products, dependent: :restrict_with_exception
|
23
|
+
|
24
|
+
accepts_nested_attributes_for :product_images, allow_destroy: true
|
25
|
+
accepts_attachments_for :product_images, append: true
|
26
|
+
|
27
|
+
# Generate materialized path
|
28
|
+
before_validation :set_materialized_path
|
29
|
+
|
30
|
+
# Create a 301 redirect if materialized_path changed
|
31
|
+
after_save :rewrite_rule
|
32
|
+
|
33
|
+
validates :name, :base_price, presence: true
|
34
|
+
validates :sku, uniqueness: true, allow_blank: true
|
35
|
+
|
36
|
+
# Globalize translates
|
37
|
+
# Virtual attributes need to be defined because of Rails 5.1 Attributes API
|
38
|
+
[:name, :description, :seo_title, :seo_description, :materialized_path].each do |attr|
|
39
|
+
attribute attr
|
40
|
+
translates attr, fallbacks_for_empty_translations: true
|
41
|
+
end
|
42
|
+
|
43
|
+
# Active product
|
44
|
+
scope :active, -> { where(active: true, archived: false) }
|
45
|
+
|
46
|
+
# Postgres-specific queries for the jsonb column
|
47
|
+
scope :where_any_tags, -> (key, value) do
|
48
|
+
value = [value] unless value.kind_of?(Array)
|
49
|
+
where("spina_shop_products.properties->'#{key}' ?| array['#{value.join('\',\'')}']")
|
50
|
+
end
|
51
|
+
|
52
|
+
scope :where_all_tags, -> (key, value) do
|
53
|
+
value = [value] unless value.kind_of?(Array)
|
54
|
+
where("spina_shop_products.properties->'#{key}' ?& array['#{value.join('\',\'')}']")
|
55
|
+
end
|
56
|
+
|
57
|
+
scope :where_in_range, -> (key, min, max) { where("CAST(coalesce(NULLIF(REPLACE(spina_shop_products.properties->>'#{key}', ',', '.'), ''), '0') AS numeric) BETWEEN ? AND ?", min, max) }
|
58
|
+
|
59
|
+
def to_s
|
60
|
+
name
|
61
|
+
end
|
62
|
+
|
63
|
+
def seo_title
|
64
|
+
read_attribute(:seo_title).presence || name
|
65
|
+
end
|
66
|
+
|
67
|
+
def seo_description
|
68
|
+
read_attribute(:seo_description).presence || description
|
69
|
+
end
|
70
|
+
|
71
|
+
def promotion?
|
72
|
+
promotional_price.present?
|
73
|
+
end
|
74
|
+
|
75
|
+
def price
|
76
|
+
promotional_price.presence || base_price
|
77
|
+
end
|
78
|
+
|
79
|
+
def price_for_order(order)
|
80
|
+
# Return the default price if we don't know anything about the order
|
81
|
+
return price if order.nil?
|
82
|
+
|
83
|
+
# If no conversion is needed, simply return price
|
84
|
+
price = price_for_customer(order.customer)
|
85
|
+
price_includes_tax = price_includes_tax_for_customer(order.customer)
|
86
|
+
return price if price_includes_tax == order.prices_include_tax
|
87
|
+
|
88
|
+
# Price modifier for unit price
|
89
|
+
price_modifier = tax_group.price_modifier_for_order(order)
|
90
|
+
|
91
|
+
# Calculate unit price based on price modifier
|
92
|
+
unit_price = price_includes_tax ? price / price_modifier : price * price_modifier
|
93
|
+
|
94
|
+
# Round to two decimals using bankers' rounding
|
95
|
+
return unit_price.round(2, :half_even)
|
96
|
+
end
|
97
|
+
|
98
|
+
def price_for_customer(customer)
|
99
|
+
return price if customer.nil?
|
100
|
+
price_exception_for_customer(customer).try(:[], 'price').try(:to_d) || price
|
101
|
+
end
|
102
|
+
|
103
|
+
def price_includes_tax_for_customer(customer)
|
104
|
+
return price_includes_tax if customer.nil?
|
105
|
+
price_exception = price_exception_for_customer(customer)
|
106
|
+
if price_exception.present?
|
107
|
+
ActiveRecord::Type::Boolean.new.cast(price_exception['price_includes_tax'])
|
108
|
+
else
|
109
|
+
price_includes_tax
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def in_stock?
|
114
|
+
return true unless stock_enabled?
|
115
|
+
stock_level > 0
|
116
|
+
end
|
117
|
+
|
118
|
+
# All properties are dynamically stored using jsonb
|
119
|
+
def properties
|
120
|
+
decorate_with_methods(read_attribute(:properties))
|
121
|
+
end
|
122
|
+
|
123
|
+
# Get products by filtering their properties
|
124
|
+
# Based on querying the jsonb column using Postgres
|
125
|
+
def self.filtered(filters)
|
126
|
+
products = all
|
127
|
+
|
128
|
+
filters ||= []
|
129
|
+
filters.each do |filter|
|
130
|
+
case filter[:field_type]
|
131
|
+
when "select"
|
132
|
+
products = products.where_any_tags(filter[:property], filter[:value])
|
133
|
+
when "percentage", "decimal", "number"
|
134
|
+
if filter[:value]["min"].present? && filter[:value]["max"].present?
|
135
|
+
products = products.where_in_range(filter[:property], filter[:value]["min"].presence.try(:to_d) || 0, filter[:value]["max"].presence.try(:to_d) || 0)
|
136
|
+
end
|
137
|
+
when "price"
|
138
|
+
min = filter[:value]["min"].presence.try(:to_d) || 0
|
139
|
+
max = filter[:value]["max"].presence.try(:to_d) || 0
|
140
|
+
products = products.where(price: min..max)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
return products
|
145
|
+
end
|
146
|
+
|
147
|
+
def cache_stock_level
|
148
|
+
update_columns(
|
149
|
+
stock_level: stock_level_adjustments.sum(:adjustment),
|
150
|
+
sales_count: stock_level_adjustments.sales.sum(:adjustment) * -1,
|
151
|
+
expiration_date: can_expire? ? earliest_expiration_date : nil
|
152
|
+
)
|
153
|
+
end
|
154
|
+
|
155
|
+
def earliest_expiration_date
|
156
|
+
offset = 0
|
157
|
+
sum = 0
|
158
|
+
adjustment = stock_level_adjustments.ordered.additions.offset(offset).first
|
159
|
+
while sum < stock_level && adjustment.present? do
|
160
|
+
adjustment = stock_level_adjustments.ordered.additions.offset(offset).first
|
161
|
+
offset = offset.next
|
162
|
+
sum = sum + adjustment.try(:adjustment).to_i
|
163
|
+
end
|
164
|
+
|
165
|
+
if adjustment.try(:expiration_year).present?
|
166
|
+
Date.new.change(day: 1, month: adjustment.expiration_month || 1, year: adjustment.expiration_year)
|
167
|
+
else
|
168
|
+
nil
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
private
|
173
|
+
|
174
|
+
def rewrite_rule
|
175
|
+
Spina::RewriteRule.where(old_path: old_path).first_or_create.update_attributes(new_path: materialized_path) if old_path != materialized_path
|
176
|
+
end
|
177
|
+
|
178
|
+
# Get price exception based on Customer / CustomerGroup if available
|
179
|
+
def price_exception_for_customer(customer)
|
180
|
+
price_exceptions.try(:[], 'customer_groups').try(:find) do |h|
|
181
|
+
h["customer_group_id"].to_i == customer.customer_group_id
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
# Get all values for properties defined on the ProductCategory.
|
186
|
+
# Defines singleton methods on the properties attribute for this product.
|
187
|
+
def decorate_with_methods(jsonb)
|
188
|
+
return nil unless product_category.present?
|
189
|
+
product_category.properties.each do |property|
|
190
|
+
jsonb.define_singleton_method(property.name) do
|
191
|
+
case property.field_type
|
192
|
+
when 'select'
|
193
|
+
if jsonb[property.name].is_a? Array
|
194
|
+
jsonb[property.name].map do |option|
|
195
|
+
property.property_options.where(name: option).first
|
196
|
+
end.compact
|
197
|
+
else
|
198
|
+
property.property_options.where(name: jsonb[property.name]).first
|
199
|
+
end
|
200
|
+
else
|
201
|
+
jsonb[property.name]
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
jsonb
|
206
|
+
end
|
207
|
+
|
208
|
+
# Each product has a unique materialized path which can be used in URL's
|
209
|
+
# Actually more like a slug than a path
|
210
|
+
# TODO: rename materialized_path to slug
|
211
|
+
def set_materialized_path
|
212
|
+
self.old_path = materialized_path
|
213
|
+
self.materialized_path = localized_materialized_path
|
214
|
+
self.materialized_path += "-#{self.class.where(materialized_path: materialized_path).count}" if self.class.where(materialized_path: materialized_path).where.not(id: id).count > 0
|
215
|
+
materialized_path
|
216
|
+
end
|
217
|
+
|
218
|
+
def localized_materialized_path
|
219
|
+
if I18n.locale == I18n.default_locale
|
220
|
+
name.try(:parameterize).prepend('/products/')
|
221
|
+
else
|
222
|
+
name.try(:parameterize).prepend("/#{I18n.locale}/products/").gsub(/\/\z/, "")
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
end
|
227
|
+
end
|