app/models/plugins/ecommerce/order.rb in camaleon_ecommerce-0.0.4 vs app/models/plugins/ecommerce/order.rb in camaleon_ecommerce-1.1

- old
+ new

@@ -4,62 +4,78 @@ Email: owenperedo@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License (GPLv3) for more details. =end -class Plugins::Ecommerce::Order < CamaleonCms::TermTaxonomy - default_scope { where(taxonomy: :ecommerce_order) } - has_one :details, class_name: "Plugins::Ecommerce::OrderDetail", foreign_key: :order_id, dependent: :destroy - has_many :products, foreign_key: :objectid, through: :term_relationships, :source => :objects - belongs_to :customer, class_name: "CamaleonCms::User", foreign_key: :user_id +class Plugins::Ecommerce::Order < Plugins::Ecommerce::Cart + self.table_name = 'plugins_ecommerce_orders' + has_many :metas, ->{ where(object_class: 'Plugins::Ecommerce::Cart')}, :class_name => "CamaleonCms::Meta", foreign_key: :objectid, dependent: :delete_all + default_scope { where(kind: 'order') } + # status: bank_pending => pending of verification for bank transfer orders + # paid => paid by some method + # canceled => canceled order + # shipped => shipped status + # accepted => received status - scope :not_closed, -> { where('status != ?', :closed) } + def payment_method + Plugins::Ecommerce::PaymentMethod.find_by_id(get_meta('payment_method_id', self.payment_method_id)) + end - def add_product(object) - post_id = defined?(object.id) ? object.id : object.to_i - term_relationships.where(objectid: post_id).first_or_create if post_id > 0 + def paid? + status == 'paid' end - def remove_product(object) - post_id = defined?(object.id) ? object.id : object.to_i - term_relationships.where(objectid: post_id).destroy_all if post_id > 0 + def accepted? + status == 'accepted' end - def payment_method - Plugins::Ecommerce::PaymentMethod.find_by_id get_meta("payment")[:payment_id] + def accepted! + update_columns({status: 'accepted', accepted_at: Time.current}) end - def payment - payment = get_meta("payment") - get_meta("pay_#{payment[:type]}".to_sym) + def shipped? + status == 'shipped' end - def shipping_method - Plugins::Ecommerce::ShippingMethod.find_by_id get_meta("payment", {})[:shipping_method] + def shipped!(code) + update_columns({status: 'shipped', shipped_at: Time.current}) + set_meta('consignment_number', code) end def canceled? status == 'canceled' end - def unpaid? - status == 'unpaid' + def canceled! + update_columns({status: 'canceled', shipped_at: Time.current}) end - def closed? - status == 'closed' + def received? + status == 'received' end - def paid? - payment.present? + def bank_pending? + status == 'bank_pending' end + def bank_confirmed! + update_columns({status: 'paid', updated_at: Time.current}) + end - # set user in filter - def self.set_user(user) - user_id = defined?(user.id) ? user.id : user.to_i - self.where(user_id: user_id) + def payment_data + get_meta('payment_data', {}) end - + # return the date of the current status + def action_date + case object.status + when 'paid' + object.created_at + when 'canceled' + object.closed_at + when 'shipped' + object.shipped_at + when 'accepted' + object.accepted_at + end + end end -#Cart = Plugins::Ecommerce::Cart