1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00

Merge remote-tracking branch 'origin/product_store' into product_store-inte

This commit is contained in:
vincent 2022-08-09 11:45:28 +02:00
commit ba0a604e40
7 changed files with 74 additions and 4 deletions

View File

@ -53,6 +53,7 @@ class API::ProductsController < API::ApiController
:low_stock_alert, :low_stock_threshold,
machine_ids: [],
product_files_attributes: %i[id attachment _destroy],
product_images_attributes: %i[id attachment is_main _destroy])
product_images_attributes: %i[id attachment is_main _destroy],
product_stock_movements_attributes: %i[id quantity reason stock_type _destroy])
end
end

View File

@ -1,3 +1,5 @@
import { TDateISO } from '../typings/date-iso';
export enum StockType {
internal = 'internal',
external = 'external'
@ -38,5 +40,14 @@ export interface Product {
attachment_url?: string,
_destroy?: boolean,
is_main?: boolean
}>
}>,
product_stock_movements_attributes: Array<{
id?: number,
quantity?: number,
reason?: string,
stock_type?: string,
remaining_stock?: number,
date?: TDateISO,
_destroy?: boolean
}>,
}

View File

@ -12,7 +12,10 @@ class Product < ApplicationRecord
has_many :product_images, as: :viewable, dependent: :destroy
accepts_nested_attributes_for :product_images, allow_destroy: true, reject_if: :all_blank
validates_numericality_of :amount, greater_than: 0, allow_nil: true
has_many :product_stock_movements, dependent: :destroy
accepts_nested_attributes_for :product_stock_movements, allow_destroy: true, reject_if: :all_blank
validates :amount, numericality: { greater_than: 0, allow_nil: true }
scope :active, -> { where(is_active: true) }
end

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
# ProductStockMovement is a model for record the movements of product's stock
class ProductStockMovement < ApplicationRecord
belongs_to :product
ALL_STOCK_TYPES = %w[internal external].freeze
enum stock_type: ALL_STOCK_TYPES.zip(ALL_STOCK_TYPES).to_h
ALL_REASONS = %w[incoming_stock returned_by_customer cancelled_by_customer sold missing_from_inventory damaged].freeze
enum reason: ALL_REASONS.zip(ALL_REASONS).to_h
validates :stock_type, presence: true
validates :stock_type, inclusion: { in: ALL_STOCK_TYPES }
validates :reason, presence: true
validates :reason, inclusion: { in: ALL_REASONS }
end

View File

@ -14,3 +14,11 @@ json.product_images_attributes product.product_images do |f|
json.attachment_url f.attachment_url
json.is_main f.is_main
end
json.product_stock_movements_attributes product.product_stock_movements do |s|
json.id s.id
json.quantity s.quantity
json.reason s.reason
json.stock_type s.stock_type
json.remaining_stock s.remaining_stock
json.date s.date
end

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
class CreateProductStockMovements < ActiveRecord::Migration[5.2]
def change
create_table :product_stock_movements do |t|
t.belongs_to :product, foreign_key: true
t.integer :quantity
t.string :reason
t.string :stock_type
t.integer :remaining_stock
t.datetime :date
t.timestamps
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_08_03_091913) do
ActiveRecord::Schema.define(version: 2022_08_05_083431) do
# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
@ -597,6 +597,18 @@ ActiveRecord::Schema.define(version: 2022_08_03_091913) do
t.index ["parent_id"], name: "index_product_categories_on_parent_id"
end
create_table "product_stock_movements", force: :cascade do |t|
t.bigint "product_id"
t.integer "quantity"
t.string "reason"
t.string "stock_type"
t.integer "remaining_stock"
t.datetime "date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["product_id"], name: "index_product_stock_movements_on_product_id"
end
create_table "products", force: :cascade do |t|
t.string "name"
t.string "slug"
@ -1135,6 +1147,7 @@ ActiveRecord::Schema.define(version: 2022_08_03_091913) do
add_foreign_key "prepaid_packs", "groups"
add_foreign_key "prices", "groups"
add_foreign_key "prices", "plans"
add_foreign_key "product_stock_movements", "products"
add_foreign_key "products", "product_categories"
add_foreign_key "project_steps", "projects"
add_foreign_key "project_users", "projects"