From 83ec17a3df45ef2ba9fd90e0295e0683661fa4ab Mon Sep 17 00:00:00 2001 From: Nweni Date: Mon, 28 Aug 2017 09:37:36 +0630 Subject: [PATCH] inventory development --- .../inventory/inventory_controller.rb | 2 +- .../inventory_definitions_controller.rb | 4 +- .../inventory/stock_checks_controller.rb | 2 +- app/jobs/inventory_job.rb | 8 ++ app/models/inventory_definition.rb | 37 ++++++++++ app/models/sale.rb | 1 + app/models/stock_journal.rb | 22 ++++++ app/views/home/dashboard.html.erb | 2 +- .../inventory/_inventory_list.html.erb | 40 ++++++++++ app/views/inventory/inventory/index.html.erb | 27 ++++--- .../inventory_definitions/_form.html.erb | 7 +- .../inventory_definitions/new.html.erb | 14 +++- .../inventory/stock_checks/index.html.erb | 74 +++++++++++++------ app/views/layouts/inventory.html.erb | 2 +- config/routes.rb | 2 +- ...0824110103_create_inventory_definitions.rb | 2 + 16 files changed, 200 insertions(+), 46 deletions(-) create mode 100644 app/jobs/inventory_job.rb create mode 100644 app/views/inventory/inventory/_inventory_list.html.erb diff --git a/app/controllers/inventory/inventory_controller.rb b/app/controllers/inventory/inventory_controller.rb index 525f0277..673cf943 100644 --- a/app/controllers/inventory/inventory_controller.rb +++ b/app/controllers/inventory/inventory_controller.rb @@ -1,6 +1,6 @@ class Inventory::InventoryController < BaseInventoryController def index - + @products = InventoryDefinition.all.active.order('created_at desc') end end diff --git a/app/controllers/inventory/inventory_definitions_controller.rb b/app/controllers/inventory/inventory_definitions_controller.rb index 8a3f3a40..7a8f17e1 100644 --- a/app/controllers/inventory/inventory_definitions_controller.rb +++ b/app/controllers/inventory/inventory_definitions_controller.rb @@ -28,7 +28,7 @@ class Inventory::InventoryDefinitionsController < BaseInventoryController respond_to do |format| if @inventory_definition.save - format.html { redirect_to @inventory_definition, notice: 'Inventory definition was successfully created.' } + format.html { redirect_to inventory_path, notice: 'Inventory definition was successfully created.' } format.json { render :show, status: :created, location: @inventory_definition } else format.html { render :new } @@ -69,6 +69,6 @@ class Inventory::InventoryDefinitionsController < BaseInventoryController # Never trust parameters from the scary internet, only allow the white list through. def inventory_definition_params - params.fetch(:inventory_definition, {}) + params.require(:inventory_definition).permit(:item_code, :min_order_level, :max_stock_level) end end diff --git a/app/controllers/inventory/stock_checks_controller.rb b/app/controllers/inventory/stock_checks_controller.rb index e8620ca1..0e36ee62 100644 --- a/app/controllers/inventory/stock_checks_controller.rb +++ b/app/controllers/inventory/stock_checks_controller.rb @@ -1,4 +1,4 @@ -class StockChecksController < ApplicationController +class Inventory::StockChecksController < BaseInventoryController before_action :set_stock_check, only: [:show, :edit, :update, :destroy] # GET /stock_checks diff --git a/app/jobs/inventory_job.rb b/app/jobs/inventory_job.rb new file mode 100644 index 00000000..0f5b1a51 --- /dev/null +++ b/app/jobs/inventory_job.rb @@ -0,0 +1,8 @@ +class InventoryJob < ApplicationJob + queue_as :default + + def perform(sale_id) + saleObj = Sale.find(sale_id) + end + +end diff --git a/app/models/inventory_definition.rb b/app/models/inventory_definition.rb index 150ae6a9..af81c941 100644 --- a/app/models/inventory_definition.rb +++ b/app/models/inventory_definition.rb @@ -1,2 +1,39 @@ class InventoryDefinition < ApplicationRecord + + scope :active, -> {where(:is_active => true)} + + def calculate_product_count(saleObj) + saleObj.sale_items.each do |item| + found, inventory_definition = find_product_in_inventory(item) + if found + check_balance(item,inventory_definition) + end + end + end + + def self.find_product_in_inventory(item) + product = InventoryDefinition.find_by_item_code(item.product_code) + if product.nil? + return false, nil + else + return true, product + end + end + + def self.check_balance(item,inventory_definition) + stock = StockJournal.where('item_code=?', item.item_code).order('created_at desc').take + unless stock.nil? + modify_balance(item, stock.balance, inventory_definition) + end + end + + def self.modify_balance(item, stock_balance, inventory_definition) + if stock.balance.to_i >= item.qty + puts ">> stock is greater than orde qty" + add_to_journal(item, balance, "ok", inventory_definition) + else + puts " << stock is less than order qty" + add_to_journal(item, balance, "out of stock", inventory_definition) + end + end end diff --git a/app/models/sale.rb b/app/models/sale.rb index 500e28ef..c8ceb696 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -117,6 +117,7 @@ class Sale < ApplicationRecord create_saleitem_diningcharges(charges, diningprice, booking.dining_facility.name, dining_time) end + InventoryJob.perform_later(self.id) return true, self.id end diff --git a/app/models/stock_journal.rb b/app/models/stock_journal.rb index ee6efe79..106a9bf5 100644 --- a/app/models/stock_journal.rb +++ b/app/models/stock_journal.rb @@ -1,2 +1,24 @@ class StockJournal < ApplicationRecord + + SALES_TRANS = "sale" + STOCK_CHECK_TRANS = "stock_check" + + def add_to_journal(item, balance, stock_message, inventory_definition) + + balance = calculate_balance(balance, item.qty) + + journal = StockJournal.new + journal.item_code = item.item_code + journal.inventory_definition_id = inventory_definition.id + journal.debit = item.qty + journal.balance = balance + journal.remark = stock_message + journal.trans_ref = item.id + journal.trans_type = StockJournal::SALES_TRANS + end + + def self.calculate_balance(balance, qty) + return balance.to_i - qty.to_i + end + end diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 8a922573..83874328 100644 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -58,7 +58,7 @@
-
+
Inventory
diff --git a/app/views/inventory/inventory/_inventory_list.html.erb b/app/views/inventory/inventory/_inventory_list.html.erb new file mode 100644 index 00000000..115c80c5 --- /dev/null +++ b/app/views/inventory/inventory/_inventory_list.html.erb @@ -0,0 +1,40 @@ + +
+

Inventoy Product Lists

+
+
+ +
+
+ + + + + + + + + + <% + count = 0 + @products.each do |item| + count += 1 + %> + + + + + + + + + <% end %> +
#ProductMin OrderMax StockCreated byCreated Time
<%= count %><%= item.item_code rescue ""%><%= item.min_order_level %><%= item.max_stock_level %><%= item.created_by%><%= item.created_at%>
+
+
+ + diff --git a/app/views/inventory/inventory/index.html.erb b/app/views/inventory/inventory/index.html.erb index e8826240..f275a96d 100644 --- a/app/views/inventory/inventory/index.html.erb +++ b/app/views/inventory/inventory/index.html.erb @@ -1,17 +1,22 @@ -# Hello Inventory -
-
+
+
+ <%= render 'inventory_list' %>
-
- - - - +
<%if current_login_employee.role == "administrator" || current_login_employee.role == "manager" %> - + <%end%> - + +
+ + diff --git a/app/views/inventory/inventory_definitions/_form.html.erb b/app/views/inventory/inventory_definitions/_form.html.erb index 4c716bac..36b80822 100644 --- a/app/views/inventory/inventory_definitions/_form.html.erb +++ b/app/views/inventory/inventory_definitions/_form.html.erb @@ -1,10 +1,15 @@ -<%= simple_form_for(@inventory_definition) do |f| %> + +<%= simple_form_for([:inventory,@inventory_definition]) do |f| %> <%= f.error_notification %>
+ <%= f.input :item_code %> + <%= f.input :min_order_level %> + <%= f.input :max_stock_level %>
<%= f.button :submit %>
+ <% end %> diff --git a/app/views/inventory/inventory_definitions/new.html.erb b/app/views/inventory/inventory_definitions/new.html.erb index a67b973a..ce290607 100644 --- a/app/views/inventory/inventory_definitions/new.html.erb +++ b/app/views/inventory/inventory_definitions/new.html.erb @@ -1,5 +1,11 @@ -

New Inventory Definition

-<%= render 'form', inventory_definition: @inventory_definition %> - -<%= link_to 'Back', inventory_definitions_path %> +
+ + <%= render 'form', inventory: @inventory_definition %> +
diff --git a/app/views/inventory/stock_checks/index.html.erb b/app/views/inventory/stock_checks/index.html.erb index 05b802eb..f5f8eb9a 100644 --- a/app/views/inventory/stock_checks/index.html.erb +++ b/app/views/inventory/stock_checks/index.html.erb @@ -1,25 +1,53 @@ -

<%= notice %>

+
+
+
+
+ +
+
+
+
+ + + + + + +
#ProductBalance
+
+
+
+
+
+ +
+
+ +
-

Stock Checks

+
+ +
+
+
- - - - - - - - - <% @stock_checks.each do |stock_check| %> - - - - - - <% end %> - -
<%= link_to 'Show', stock_check %><%= link_to 'Edit', edit_stock_check_path(stock_check) %><%= link_to 'Destroy', stock_check, method: :delete, data: { confirm: 'Are you sure?' } %>
- -
- -<%= link_to 'New Stock Check', new_stock_check_path %> +
+
+
+ + + + + + + +
#ProductBalanceDifferent
+
+
+
+
+ +
+
+
+
diff --git a/app/views/layouts/inventory.html.erb b/app/views/layouts/inventory.html.erb index 5bcf7e0a..d6bbf436 100644 --- a/app/views/layouts/inventory.html.erb +++ b/app/views/layouts/inventory.html.erb @@ -9,7 +9,7 @@ SmartSales : Restaurant <%= csrf_meta_tags %> - <%= stylesheet_link_tag 'inventory', media: 'all', 'data-turbolinks-track': 'reload' %> + <%= stylesheet_link_tag 'origami', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> <%= stylesheet_link_tag 'jquery-confirm', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'jquery-confirm', 'data-turbolinks-track': 'reload' %> diff --git a/config/routes.rb b/config/routes.rb index 3e8caa28..980980cb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -327,7 +327,7 @@ Rails.application.routes.draw do # ----------- Inventory --------------------------- namespace :inventory do - get 'inventory' => 'inventory#index' + get '/' => 'inventory#index' resources :stock_check_items resources :stock_checks resources :stock_journals diff --git a/db/migrate/20170824110103_create_inventory_definitions.rb b/db/migrate/20170824110103_create_inventory_definitions.rb index 4278ce89..c73fd0bd 100644 --- a/db/migrate/20170824110103_create_inventory_definitions.rb +++ b/db/migrate/20170824110103_create_inventory_definitions.rb @@ -4,6 +4,8 @@ class CreateInventoryDefinitions < ActiveRecord::Migration[5.1] t.string :item_code t.integer :min_order_level, :default => 0 t.integer :max_stock_level, :default => 0 + t.integer :created_by + t.boolean :is_active, :default => true t.timestamps end end