2022-07-13 15:06:46 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for Product
|
|
|
|
class ProductService
|
2022-09-09 14:00:53 +02:00
|
|
|
PRODUCTS_PER_PAGE = 12
|
2022-09-09 13:48:20 +02:00
|
|
|
|
2022-08-16 18:53:11 +02:00
|
|
|
def self.list(filters)
|
|
|
|
products = Product.includes(:product_images)
|
|
|
|
if filters[:is_active].present?
|
|
|
|
state = filters[:disabled] == 'false' ? [nil, false] : true
|
|
|
|
products = products.where(is_active: state)
|
|
|
|
end
|
2022-09-09 17:14:22 +02:00
|
|
|
products = products.page(filters[:page]).per(PRODUCTS_PER_PAGE) if filters[:page].present?
|
2022-08-16 18:53:11 +02:00
|
|
|
products
|
2022-07-13 15:06:46 +02:00
|
|
|
end
|
2022-08-04 14:02:19 +02:00
|
|
|
|
2022-09-09 17:14:22 +02:00
|
|
|
def self.pages(filters)
|
|
|
|
products = Product.all
|
|
|
|
if filters[:is_active].present?
|
|
|
|
state = filters[:disabled] == 'false' ? [nil, false] : true
|
|
|
|
products = Product.where(is_active: state)
|
|
|
|
end
|
|
|
|
products.page(1).per(PRODUCTS_PER_PAGE).total_pages
|
2022-09-09 13:48:20 +02:00
|
|
|
end
|
|
|
|
|
2022-08-04 14:02:19 +02:00
|
|
|
# amount params multiplied by hundred
|
|
|
|
def self.amount_multiplied_by_hundred(amount)
|
|
|
|
if amount.present?
|
|
|
|
v = amount.to_f
|
|
|
|
|
|
|
|
return nil if v.zero?
|
|
|
|
|
|
|
|
return v * 100
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
2022-08-25 08:52:17 +02:00
|
|
|
|
2022-08-26 14:08:20 +02:00
|
|
|
def self.update_stock(product, stock_type, reason, quantity, order_item_id = nil)
|
2022-08-25 08:52:17 +02:00
|
|
|
remaining_stock = product.stock[stock_type] + quantity
|
|
|
|
product.product_stock_movements.create(stock_type: stock_type, reason: reason, quantity: quantity, remaining_stock: remaining_stock,
|
2022-08-26 11:06:09 +02:00
|
|
|
date: DateTime.current,
|
|
|
|
order_item_id: order_item_id)
|
2022-08-25 08:52:17 +02:00
|
|
|
product.stock[stock_type] = remaining_stock
|
|
|
|
product.save
|
|
|
|
end
|
2022-07-13 15:06:46 +02:00
|
|
|
end
|