cirandas.net

ref: master

plugins/orders_cycle/models/orders_cycle_plugin/sale.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
class OrdersCyclePlugin::Sale < OrdersPlugin::Sale

  include OrdersCyclePlugin::OrderBase

  has_many :cycles, through: :cycle_sales, source: :cycle

  after_save :change_purchases, if: :cycle
  before_destroy :remove_purchases_items, if: :cycle

  def current_status
    return 'forgotten' if self.forgotten?
    super
  end

  def delivery?
    self.cycle.delivery?
  end
  def forgotten?
    self.draft? and !self.cycle.orders?
  end

  def open?
    super and self.cycle.orders?
  end

  def change_purchases
    return unless self.status_was.present?
    # only on sale confirmation
    if self.ordered_at_was.nil? and self.ordered_at.present?
      self.add_purchases_items
    # only on sale cancellation/unconfirmation
    elsif self.ordered_at_was.present? and self.ordered_at.nil?
      self.remove_purchases_items
    end
  end

  def add_purchases_items
    ApplicationRecord.transaction do
      self.items.each do |item|
        self.add_purchase_item item
      end
    end
  end

  def add_purchase_item item
    return unless supplier_product = item.product.supplier_product
    return unless supplier = supplier_product.profile

    purchase = self.cycle.purchases.for_profile(supplier).first
    purchase ||= OrdersCyclePlugin::Purchase.create! cycle: self.cycle, consumer: self.profile, profile: supplier

    purchased_item = purchase.items.for_product(supplier_product).first
    purchased_item ||= purchase.items.build purchase: purchase, product: supplier_product
    purchased_item.quantity_consumer_ordered ||= 0
    purchased_item.quantity_consumer_ordered += item.status_quantity
    purchased_item.price_consumer_ordered ||= 0
    purchased_item.price_consumer_ordered += item.status_quantity * supplier_product.price
    purchased_item.save!
  end

  def update_purchase_item item
    return unless supplier_product = item.product.supplier_product
    return unless supplier = supplier_product.profile

    previous_value = item.quantity_supplier_accepted_was || item.quantity_consumer_ordered
    if previous_value != item.quantity_supplier_accepted
      qtt_diff = (item.quantity_supplier_accepted || 0) - (item.quantity_supplier_accepted_was || 0)
    elsif item.quantity_consumer_ordered_was != item.quantity_consumer_ordered
      qtt_diff = (item.quantity_consumer_ordered || 0) - (item.quantity_consumer_ordered_was || 0)
    else
      return
    end
    return if qtt_diff.zero?

    purchase = self.cycle.purchases.for_profile(supplier).first
    purchased_item = purchase.items.for_product(supplier_product).first
    purchased_item.quantity_consumer_ordered += qtt_diff
    purchased_item.price_consumer_ordered += qtt_diff * supplier_product.price
    purchased_item.save!
  end

  def remove_purchases_items
    ApplicationRecord.transaction do
      self.items.each do |item|
        self.remove_purchase_item item
      end
    end
  end

  def remove_purchase_item item
    return unless supplier_product = item.product.supplier_product
    return unless purchase = supplier_product.orders_cycles_purchases.for_cycle(self.cycle).first

    purchased_item = purchase.items.for_product(supplier_product).first
    purchased_item.quantity_consumer_ordered -= item.status_quantity
    purchased_item.price_consumer_ordered -= item.status_quantity * supplier_product.price
    purchased_item.save!

    purchased_item.destroy if purchased_item.quantity_consumer_ordered.zero?
    purchase.destroy if purchase.items(true).blank?
  end

  handle_asynchronously :add_purchases_items
  handle_asynchronously :remove_purchases_items

end