diff --git a/app/controllers/inventory/stock_check_items_controller.rb b/app/controllers/inventory/stock_check_items_controller.rb index aec48a04..18980f29 100644 --- a/app/controllers/inventory/stock_check_items_controller.rb +++ b/app/controllers/inventory/stock_check_items_controller.rb @@ -1,4 +1,4 @@ -class StockCheckItemsController < ApplicationController +class Inventory::StockCheckItemsController < BaseInventoryController before_action :set_stock_check_item, only: [:show, :edit, :update, :destroy] # GET /stock_check_items @@ -28,7 +28,7 @@ class StockCheckItemsController < ApplicationController respond_to do |format| if @stock_check_item.save - format.html { redirect_to @stock_check_item, notice: 'Stock check item was successfully created.' } + format.html { redirect_to inventory_stock_checks_path, notice: 'Stock check item was successfully created.' } format.json { render :show, status: :created, location: @stock_check_item } else format.html { render :new } @@ -69,6 +69,6 @@ class StockCheckItemsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def stock_check_item_params - params.fetch(:stock_check_item, {}) + params.require(:stock_check_item).permit(:item_code, :stock_count) end end diff --git a/app/controllers/inventory/stock_checks_controller.rb b/app/controllers/inventory/stock_checks_controller.rb index 0e36ee62..f161bc25 100644 --- a/app/controllers/inventory/stock_checks_controller.rb +++ b/app/controllers/inventory/stock_checks_controller.rb @@ -1,74 +1,98 @@ class Inventory::StockChecksController < BaseInventoryController - before_action :set_stock_check, only: [:show, :edit, :update, :destroy] + + def index + @check = StockCheck.new + end + + def create + item_list = JSON.parse(params[:stock_item]) + reason = params[:reason] + check = StockCheck.new + @check = check.create(current_user, reason, item_list) + end + + def show + @check = StockCheck.find(params[:id]) + end + + def save_to_journal + check = params[:data] + stockCheck = StockCheck.find(check) + stockCheck.stock_check_items.each do |item| + StockJournal.from_stock_check(item) + end + end + + # before_action :set_stock_check, only: [:show, :edit, :update, :destroy] # GET /stock_checks # GET /stock_checks.json - def index - @stock_checks = StockCheck.all - end - - # GET /stock_checks/1 - # GET /stock_checks/1.json - def show - end - - # GET /stock_checks/new - def new - @stock_check = StockCheck.new - end - - # GET /stock_checks/1/edit - def edit - end - - # POST /stock_checks - # POST /stock_checks.json - def create - @stock_check = StockCheck.new(stock_check_params) - - respond_to do |format| - if @stock_check.save - format.html { redirect_to @stock_check, notice: 'Stock check was successfully created.' } - format.json { render :show, status: :created, location: @stock_check } - else - format.html { render :new } - format.json { render json: @stock_check.errors, status: :unprocessable_entity } - end - end - end - - # PATCH/PUT /stock_checks/1 - # PATCH/PUT /stock_checks/1.json - def update - respond_to do |format| - if @stock_check.update(stock_check_params) - format.html { redirect_to @stock_check, notice: 'Stock check was successfully updated.' } - format.json { render :show, status: :ok, location: @stock_check } - else - format.html { render :edit } - format.json { render json: @stock_check.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /stock_checks/1 - # DELETE /stock_checks/1.json - def destroy - @stock_check.destroy - respond_to do |format| - format.html { redirect_to stock_checks_url, notice: 'Stock check was successfully destroyed.' } - format.json { head :no_content } - end - end - - private - # Use callbacks to share common setup or constraints between actions. - def set_stock_check - @stock_check = StockCheck.find(params[:id]) - end - - # Never trust parameters from the scary internet, only allow the white list through. - def stock_check_params - params.fetch(:stock_check, {}) - end +# def index +# @stock_checks = StockCheck.all +# end +# +# # GET /stock_checks/1 +# # GET /stock_checks/1.json +# def show +# end +# +# # GET /stock_checks/new +# def new +# @stock_check = StockCheck.new +# end +# +# # GET /stock_checks/1/edit +# def edit +# end +# +# # POST /stock_checks +# # POST /stock_checks.json +# def create +# @stock_check = StockCheck.new(stock_check_params) +# +# respond_to do |format| +# if @stock_check.save +# format.html { redirect_to @stock_check, notice: 'Stock check was successfully created.' } +# format.json { render :show, status: :created, location: @stock_check } +# else +# format.html { render :new } +# format.json { render json: @stock_check.errors, status: :unprocessable_entity } +# end +# end +# end +# +# # PATCH/PUT /stock_checks/1 +# # PATCH/PUT /stock_checks/1.json +# def update +# respond_to do |format| +# if @stock_check.update(stock_check_params) +# format.html { redirect_to @stock_check, notice: 'Stock check was successfully updated.' } +# format.json { render :show, status: :ok, location: @stock_check } +# else +# format.html { render :edit } +# format.json { render json: @stock_check.errors, status: :unprocessable_entity } +# end +# end +# end +# +# # DELETE /stock_checks/1 +# # DELETE /stock_checks/1.json +# def destroy +# @stock_check.destroy +# respond_to do |format| +# format.html { redirect_to stock_checks_url, notice: 'Stock check was successfully destroyed.' } +# format.json { head :no_content } +# end +# end +# +# private +# # Use callbacks to share common setup or constraints between actions. +# def set_stock_check +# @stock_check = StockCheck.find(params[:id]) +# end +# +# # Never trust parameters from the scary internet, only allow the white list through. +# def stock_check_params +# params.fetch(:stock_check, {}) +# end end diff --git a/app/jobs/inventory_job.rb b/app/jobs/inventory_job.rb index 0f5b1a51..a47dd362 100644 --- a/app/jobs/inventory_job.rb +++ b/app/jobs/inventory_job.rb @@ -3,6 +3,7 @@ class InventoryJob < ApplicationJob def perform(sale_id) saleObj = Sale.find(sale_id) + InventoryDefinition.calculate_product_count(saleObj) end - + end diff --git a/app/models/inventory_definition.rb b/app/models/inventory_definition.rb index af81c941..91823068 100644 --- a/app/models/inventory_definition.rb +++ b/app/models/inventory_definition.rb @@ -2,7 +2,7 @@ class InventoryDefinition < ApplicationRecord scope :active, -> {where(:is_active => true)} - def calculate_product_count(saleObj) + def self.calculate_product_count(saleObj) saleObj.sale_items.each do |item| found, inventory_definition = find_product_in_inventory(item) if found @@ -20,20 +20,22 @@ class InventoryDefinition < ApplicationRecord end end - def self.check_balance(item,inventory_definition) - stock = StockJournal.where('item_code=?', item.item_code).order('created_at desc').take + def self.check_balance(item,inventory_definition) # item => saleItemOBj + stock = StockJournal.where('item_code=?', item.product_code).order('created_at desc').take unless stock.nil? - modify_balance(item, stock.balance, inventory_definition) + modify_balance(item, stock, inventory_definition) + else + StockJournal.add_to_journal(item, 0, "out of stock", inventory_definition) end end - def self.modify_balance(item, stock_balance, inventory_definition) + def self.modify_balance(item, stock, inventory_definition) #saleitemObj if stock.balance.to_i >= item.qty puts ">> stock is greater than orde qty" - add_to_journal(item, balance, "ok", inventory_definition) + StockJournal.add_to_journal(item, stock.balance, "ok", inventory_definition) else puts " << stock is less than order qty" - add_to_journal(item, balance, "out of stock", inventory_definition) + StockJournal.add_to_journal(item, stock.balance, "out of stock", inventory_definition) end end end diff --git a/app/models/sale.rb b/app/models/sale.rb index 8c89195e..6eec518f 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -118,7 +118,8 @@ class Sale < ApplicationRecord create_saleitem_diningcharges(charges, diningprice, booking.dining_facility.name, dining_time) end - InventoryJob.perform_later(self.id) + InventoryJob.perform_now(self.id) + return true, self.id end diff --git a/app/models/stock_check.rb b/app/models/stock_check.rb index 71eaef79..7a8f8d65 100644 --- a/app/models/stock_check.rb +++ b/app/models/stock_check.rb @@ -1,2 +1,17 @@ class StockCheck < ApplicationRecord + + has_many :stock_check_items + + def create(user, eason, item_list) + self.reason = reason + self.check_by = user.id + self.check_start = Time.now + self.check_end = Time.now + self.save + item_list.each do |item| + stockItem = StockCheckItem.new + stockItem.create(self.id,item) + end + return self + end end diff --git a/app/models/stock_check_item.rb b/app/models/stock_check_item.rb index 2cbb0e22..e02972a6 100644 --- a/app/models/stock_check_item.rb +++ b/app/models/stock_check_item.rb @@ -1,2 +1,36 @@ class StockCheckItem < ApplicationRecord + + belongs_to :stock_check + + def create(stock_id, item) + journal_id, balance = StockCheckItem.find_journal(item['sku']) + remark, different = StockCheckItem.stock_different(item['qty'], balance ) + self.stock_check_id = stock_id + self.item_code = item['sku'] + self.stock_count = item['qty'] + self.stock_journal_id = journal_id + self.stock_balance = balance + self.different = different + self.remark = remark + self.save + end + + def self.find_journal(item_code) + journal = StockJournal.where('item_code=?', item_code).order('created_at desc').take + if journal + return journal.id, journal.balance + else + return nil, 0 + end + end + + def self.stock_different(stock_check_qty, journal_balance) + if stock_check_qty.to_i == journal_balance.to_i + return 'match', stock_check_qty + elsif stock_check_qty.to_i > journal_balance.to_i + return 'missing order item', stock_check_qty.to_i - journal_balance.to_i + elsif stock_check_qty.to_i < journal_balance.to_i + return 'missing stock', stock_check_qty.to_i - journal_balance.to_i + end + end end diff --git a/app/models/stock_journal.rb b/app/models/stock_journal.rb index 106a9bf5..22dceaa5 100644 --- a/app/models/stock_journal.rb +++ b/app/models/stock_journal.rb @@ -3,22 +3,37 @@ class StockJournal < ApplicationRecord SALES_TRANS = "sale" STOCK_CHECK_TRANS = "stock_check" - def add_to_journal(item, balance, stock_message, inventory_definition) + def self.add_to_journal(item, balance, stock_message, inventory_definition) # item => saleObj | balance => Stock journal balance = calculate_balance(balance, item.qty) journal = StockJournal.new - journal.item_code = item.item_code + journal.item_code = item.product_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 + journal.save end def self.calculate_balance(balance, qty) return balance.to_i - qty.to_i end + def self.from_stock_check(item) + definition_id = InventoryDefinition.find_by_item_code(item.item_code) + journal = StockJournal.new + journal.item_code = item.item_code + journal.inventory_definition_id = definition_id.id + journal.debit = 0 + journal.credit = item.stock_count + journal.balance = item.stock_count + journal.remark = StockJournal::STOCK_CHECK_TRANS + journal.trans_ref = item.id + journal.trans_type = StockJournal::STOCK_CHECK_TRANS + journal.save + end + end diff --git a/app/views/inventory/inventory/index.html.erb b/app/views/inventory/inventory/index.html.erb index f275a96d..7aeb4464 100644 --- a/app/views/inventory/inventory/index.html.erb +++ b/app/views/inventory/inventory/index.html.erb @@ -7,7 +7,7 @@ <%if current_login_employee.role == "administrator" || current_login_employee.role == "manager" %> <%end%> - + diff --git a/app/views/inventory/stock_checks/_form.html.erb b/app/views/inventory/stock_checks/_form.html.erb deleted file mode 100644 index b6bb2ae5..00000000 --- a/app/views/inventory/stock_checks/_form.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -<%= simple_form_for(@stock_check) do |f| %> - <%= f.error_notification %> - -
-
- -
- <%= f.button :submit %> -
-<% end %> diff --git a/app/views/inventory/stock_checks/_stock_check.json.jbuilder b/app/views/inventory/stock_checks/_stock_check.json.jbuilder deleted file mode 100644 index 10634ca9..00000000 --- a/app/views/inventory/stock_checks/_stock_check.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.extract! stock_check, :id, :created_at, :updated_at -json.url stock_check_url(stock_check, format: :json) diff --git a/app/views/inventory/stock_checks/create.json.jbuilder b/app/views/inventory/stock_checks/create.json.jbuilder new file mode 100644 index 00000000..38bcb274 --- /dev/null +++ b/app/views/inventory/stock_checks/create.json.jbuilder @@ -0,0 +1 @@ +json.stock_id @check.id diff --git a/app/views/inventory/stock_checks/edit.html.erb b/app/views/inventory/stock_checks/edit.html.erb deleted file mode 100644 index aa821705..00000000 --- a/app/views/inventory/stock_checks/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

Editing Stock Check

- -<%= render 'form', stock_check: @stock_check %> - -<%= link_to 'Show', @stock_check %> | -<%= link_to 'Back', stock_checks_path %> diff --git a/app/views/inventory/stock_checks/index.html.erb b/app/views/inventory/stock_checks/index.html.erb index f5f8eb9a..812d4409 100644 --- a/app/views/inventory/stock_checks/index.html.erb +++ b/app/views/inventory/stock_checks/index.html.erb @@ -2,52 +2,101 @@
- +
- - - - - - -
#ProductBalance
+ +
+
+
+
+
+
+ Product +
+
+
-
+
-
- -
-
- -
- +
- + Qty +
+
+ +
+
+
+
+
+
+
-
+
- +
-
# Product BalanceDifferent
+
+
-
- -
+
+ + diff --git a/app/views/inventory/stock_checks/index.json.jbuilder b/app/views/inventory/stock_checks/index.json.jbuilder deleted file mode 100644 index 23740183..00000000 --- a/app/views/inventory/stock_checks/index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.array! @stock_checks, partial: 'stock_checks/stock_check', as: :stock_check diff --git a/app/views/inventory/stock_checks/new.html.erb b/app/views/inventory/stock_checks/new.html.erb deleted file mode 100644 index 503af621..00000000 --- a/app/views/inventory/stock_checks/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

New Stock Check

- -<%= render 'form', stock_check: @stock_check %> - -<%= link_to 'Back', stock_checks_path %> diff --git a/app/views/inventory/stock_checks/show.html.erb b/app/views/inventory/stock_checks/show.html.erb index 6c306d8d..09ff16b5 100644 --- a/app/views/inventory/stock_checks/show.html.erb +++ b/app/views/inventory/stock_checks/show.html.erb @@ -1,4 +1,74 @@ -

<%= notice %>

+
+ +
+
+
+ Check by +
+
+ <%= @check.check_by %> +
+
+
+
+ Check At +
+
+ <%= @check.created_at %>
+
+
+
+ Reason +
+
+ <%= @check.reason %> +
+
+
+
+ + + + + + + + + + <% + count = 0 + @check.stock_check_items.each do |item| + count += 1 + %> + + + + + + + + + <% end %> +
#ProductStock CountStock BalanceDifferentRemark
<%= count %><%= item.item_code %><%= item.stock_count %><%= item.stock_balance %><%= item.different %><%= item.remark %>
+
+
+
+ + + +
+
-<%= link_to 'Edit', edit_stock_check_path(@stock_check) %> | -<%= link_to 'Back', stock_checks_path %> + diff --git a/app/views/inventory/stock_checks/show.json.jbuilder b/app/views/inventory/stock_checks/show.json.jbuilder deleted file mode 100644 index 1a4ad710..00000000 --- a/app/views/inventory/stock_checks/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.partial! "stock_checks/stock_check", stock_check: @stock_check diff --git a/config/routes.rb b/config/routes.rb index fb384fed..add37c76 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -328,8 +328,11 @@ Rails.application.routes.draw do namespace :inventory do get '/' => 'inventory#index' - resources :stock_check_items - resources :stock_checks + get '/stock_checks' => 'stock_checks#index' + post 'save_stock' => 'stock_checks#create', as:'stock_check_save' + get '/stock_checks/:id' => 'stock_checks#show' + post 'save_to_journal' => 'stock_checks#save_to_journal', as: 'save_to_journal' + # resources :stock_checks resources :stock_journals resources :inventory_definitions end diff --git a/db/migrate/20170824110130_create_stock_check_items.rb b/db/migrate/20170824110130_create_stock_check_items.rb index c8908a1d..578ca893 100644 --- a/db/migrate/20170824110130_create_stock_check_items.rb +++ b/db/migrate/20170824110130_create_stock_check_items.rb @@ -1,7 +1,7 @@ class CreateStockCheckItems < ActiveRecord::Migration[5.1] def change create_table :stock_check_items do |t| - t.integer :stock_check_id, :null => false + t.integer :stock_check_id t.string :item_code, :null => false t.integer :stock_count, :default => 0 t.integer :stock_journal_id diff --git a/dump.rdb b/dump.rdb index 6e3e4bdb..b16f7012 100644 Binary files a/dump.rdb and b/dump.rdb differ