API v1 order_articles endpoints
This commit is contained in:
parent
127ae83f04
commit
ed9192c47f
14 changed files with 323 additions and 4 deletions
38
app/controllers/api/v1/order_articles_controller.rb
Normal file
38
app/controllers/api/v1/order_articles_controller.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
class Api::V1::OrderArticlesController < Api::V1::BaseController
|
||||
include Concerns::CollectionScope
|
||||
|
||||
before_action ->{ doorkeeper_authorize! 'orders:read', 'orders:write' }
|
||||
|
||||
def index
|
||||
render_collection search_scope
|
||||
end
|
||||
|
||||
def show
|
||||
render json: scope.find(params.require(:id))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def scope
|
||||
OrderArticle.includes(:article_price, article: :supplier)
|
||||
end
|
||||
|
||||
def search_scope
|
||||
merge_ordered_scope(super, params.fetch(:q, {})[:ordered])
|
||||
end
|
||||
|
||||
def merge_ordered_scope(scope, ordered)
|
||||
if ordered.blank?
|
||||
scope
|
||||
elsif ordered == 'member' && current_ordergroup
|
||||
scope.joins(:group_order_articles).merge(current_ordergroup.group_order_articles)
|
||||
elsif ordered == 'all'
|
||||
table = scope.arel_table
|
||||
scope.where(table[:quantity].gt(0).or(table[:tolerance].gt(0)))
|
||||
elsif ordered == 'supplier'
|
||||
scope.ordered
|
||||
else
|
||||
scope.none # as a hint that it's an invalid value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -43,6 +43,12 @@ class Article < ApplicationRecord
|
|||
# @!attribute article_prices
|
||||
# @return [Array<ArticlePrice>] Price history (current price first).
|
||||
has_many :article_prices, -> { order("created_at DESC") }
|
||||
# @!attribute order_articles
|
||||
# @return [Array<OrderArticle>] Order articles for this article.
|
||||
has_many :order_articles
|
||||
# @!attribute order
|
||||
# @return [Array<Order>] Orders this article appears in.
|
||||
has_many :orders, through: :order_articles
|
||||
|
||||
# Replace numeric seperator with database format
|
||||
localize_input_of :price, :tax, :deposit
|
||||
|
|
@ -68,6 +74,14 @@ class Article < ApplicationRecord
|
|||
before_save :update_price_history
|
||||
before_destroy :check_article_in_use
|
||||
|
||||
def self.ransackable_attributes(auth_object = nil)
|
||||
%w(id name supplier_id article_category_id unit note manufacturer origin unit_quantity order_number)
|
||||
end
|
||||
|
||||
def self.ransackable_associations(auth_object = nil)
|
||||
%w(article_category supplier order_articles orders)
|
||||
end
|
||||
|
||||
# Returns true if article has been updated at least 2 days ago
|
||||
def recently_updated
|
||||
updated_at > 2.days.ago
|
||||
|
|
|
|||
|
|
@ -20,6 +20,14 @@ class OrderArticle < ApplicationRecord
|
|||
before_create :init_from_balancing
|
||||
after_destroy :update_ordergroup_prices
|
||||
|
||||
def self.ransackable_attributes(auth_object = nil)
|
||||
%w(id order_id article_id quantity tolerance units_to_order)
|
||||
end
|
||||
|
||||
def self.ransackable_associations(auth_object = nil)
|
||||
%w(order article)
|
||||
end
|
||||
|
||||
# This method returns either the ArticlePrice or the Article
|
||||
# The first will be set, when the the order is finished
|
||||
def price
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ class Ordergroup < Group
|
|||
|
||||
has_many :financial_transactions
|
||||
has_many :group_orders
|
||||
has_many :orders, :through => :group_orders
|
||||
has_many :orders, through: :group_orders
|
||||
has_many :group_order_articles, through: :group_orders
|
||||
|
||||
validates_numericality_of :account_balance, :message => I18n.t('ordergroups.model.invalid_balance')
|
||||
validate :uniqueness_of_name, :uniqueness_of_members
|
||||
|
|
|
|||
|
|
@ -9,6 +9,16 @@ class StockArticle < Article
|
|||
|
||||
before_destroy :check_quantity
|
||||
|
||||
ransack_alias :quantity_available, :quantity # in-line with {StockArticleSerializer}
|
||||
|
||||
def self.ransackable_attributes(auth_object = nil)
|
||||
super(auth_object) - %w(supplier_id) + %w(quantity)
|
||||
end
|
||||
|
||||
def self.ransackable_associations(auth_object = nil)
|
||||
super(auth_object) - %w(supplier)
|
||||
end
|
||||
|
||||
# Update the quantity of items in stock
|
||||
def update_quantity!
|
||||
update_attribute :quantity, stock_changes.collect(&:quantity).sum
|
||||
|
|
|
|||
9
app/serializers/article_serializer.rb
Normal file
9
app/serializers/article_serializer.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
class ArticleSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name
|
||||
attributes :supplier_id, :supplier_name
|
||||
attributes :unit, :unit_quantity, :note, :manufacturer, :origin, :article_category_id
|
||||
|
||||
def supplier_name
|
||||
object.supplier.try(:name)
|
||||
end
|
||||
end
|
||||
10
app/serializers/order_article_serializer.rb
Normal file
10
app/serializers/order_article_serializer.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
class OrderArticleSerializer < ActiveModel::Serializer
|
||||
attributes :id, :order_id, :price
|
||||
attributes :quantity, :tolerance, :units_to_order
|
||||
|
||||
has_one :article
|
||||
|
||||
def price
|
||||
object.price.fc_price.to_f
|
||||
end
|
||||
end
|
||||
7
app/serializers/stock_article_serializer.rb
Normal file
7
app/serializers/stock_article_serializer.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class StockArticleSerializer < ArticleSerializer
|
||||
attribute :quantity_available
|
||||
|
||||
def quantity_available
|
||||
object.quantity
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue