Class: Catalog::AssignCatalogToCustomer
- Inherits:
-
BaseService
- Object
- BaseService
- Catalog::AssignCatalogToCustomer
- Defined in:
- app/services/catalog/assign_catalog_to_customer.rb
Overview
Operation to assign a catalog to a customer
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
- #error_check(customer, target_catalog) ⇒ Object
-
#initialize(options = {}) ⇒ AssignCatalogToCustomer
constructor
A new instance of AssignCatalogToCustomer.
-
#process(customer, target_catalog) ⇒ Object
customer, a customer to convert target_catalog, a target_catalog to use.
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
#initialize(options = {}) ⇒ AssignCatalogToCustomer
Returns a new instance of AssignCatalogToCustomer.
5 6 7 |
# File 'app/services/catalog/assign_catalog_to_customer.rb', line 5 def initialize(={}) @catalog_item_assigner = [:catalog_item_assigner] || Catalog::AssignCatalogItem.new end |
Instance Method Details
#error_check(customer, target_catalog) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/services/catalog/assign_catalog_to_customer.rb', line 63 def error_check(customer, target_catalog) # Don't allow catalog change to different currency if ( customer.catalog.currency != target_catalog.currency && ( customer.orders.not_open_for_change.any? || customer.orders.co_only.any? || customer.invoices.any? ) ) return ["Target catalog is in different currency and customer has orders not open for changes or invoices"] elsif ( customer.catalog.currency != target_catalog.currency && customer.orders.where.not(state: %w(partially_invoiced invoiced)).joins(:payments).merge(Payment.where(category: [Payment::CREDIT_CARD,Payment::ECHECK]).).exists? ) return ["Target catalog is in different currency and customer has orders with unvoided credit card payments. Please void any open payments first."] else return [] end end |
#process(customer, target_catalog) ⇒ Object
customer, a customer to convert
target_catalog, a target_catalog to use
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/services/catalog/assign_catalog_to_customer.rb', line 16 def process(customer, target_catalog) #Do a pre-check, if the customer or catalog can actually change return Result.new(catalog_assigned: false, messages: ['Already assigned to this catalog']) if customer.catalog_id == target_catalog.id errors = error_check(customer, target_catalog) return Result.new(catalog_assigned: false, messages: errors) if errors.present? # Create a map so we can easily merge later on #items = Item.joins(:line_items).merge(customer.all_line_items) = [] begin # target_catalog.transaction do # items.distinct.each do |item| # process_item(target_catalog, item, customer, messages) # end # end currency_changed = customer.catalog.currency != target_catalog.currency # Now assign customer the catalog safely! customer.catalog_id = target_catalog.id # If our catalog has a discount assigned, we reset the customer's discount to catalog if target_catalog.parent_catalog_discount customer.tier2_program_pricing = Coupon::Tier2ProgramPricing.customer_default end # clear out stripe_customer_id and delete stored cards if switching to a different currency if currency_changed customer.stripe_customer_id = nil customer.credit_card_vaults.destroy end customer.save! customer.reload # Switch quick estimators customer.quick_estimators.each{|qe| qe.update_attribute(:catalog_id, customer.catalog_id)} # Switch currency on quotes, rooms, orders customer.quotes.each(&:fix_catalog) customer.orders.where.not(state: %w(partially_invoiced invoiced)).each(&:fix_catalog) customer.room_configurations.each(&:fix_catalog) return Result.new catalog_assigned: true, messages: ["Catalog remapped"] rescue StandardError => exc return Result.new catalog_assigned: false, messages: [exc.to_s]+exc.backtrace end end |