Skip to content

Commit 2e140bb

Browse files
committed
Add inventory on store
1 parent ef4d9bf commit 2e140bb

File tree

62 files changed

+331
-69
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+331
-69
lines changed

core/app/models/bouquet/purchase_order.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class PurchaseOrder < ApplicationRecord
1010
after_create do
1111
location = Bouquet::Location.first
1212

13-
arrival = build_arrival(quantity: quantity)
13+
arrival = build_arrival(quantity: quantity, date: Time.current)
1414
arrival.save
1515

16-
stock = arrival.stocks.new(quantity: quantity, location_id: location.id)
16+
stock = arrival.stocks.new(quantity: quantity, date: Time.current, location_id: location.id)
1717
stock.save
1818

19-
storage = stock.storages.new(quantity: quantity)
19+
storage = stock.storages.new(initial_quantity: quantity, date: Time.current)
2020
storage.save
2121
end
2222
end

core/app/models/bouquet/retrieval.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Bouquet::Retrieval
12
module Bouquet
23
class Retrieval < ApplicationRecord
34
belongs_to :storage

core/app/models/bouquet/sales_order.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
# sales_order = Bouquet::SalesOrder.new(quantity: 1, product_id: 1, customer_id: 1)
1+
# sales_order = Bouquet::SalesOrder.new(quantity: 1, customer_id: 1, product_id: 1)
22
# sales_order.save
3-
# sales_order.shipment.deliveries.first.retrievals.first
3+
# Bouquet::SalesOrder.last.shipping
44
module Bouquet
55
class SalesOrder < ApplicationRecord
66
belongs_to :product
77
belongs_to :customer
88
has_one :shipment, dependent: :destroy
99

10+
after_create :shipping
11+
def shipping
12+
shipment = build_shipment
13+
shipment.date = Time.current
14+
shipment.save
15+
16+
shipment.delivering
17+
end
18+
19+
=begin
1020
after_create do
1121
location = Bouquet::Location.first
1222
storage = Bouquet::Storage.first
@@ -20,5 +30,6 @@ class SalesOrder < ApplicationRecord
2030
retrieval = deliverie.retrievals.new(quantity: quantity, storage_id: storage.id)
2131
retrieval.save
2232
end
33+
=end
2334
end
2435
end

core/app/models/bouquet/shipment.rb

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
1-
# Bouquet::Customer.find_by(name: :foo).sales_orders.last.shipment = Bouquet::Shipment.create
1+
# Bouquet::Shipment.last.delivering
22
module Bouquet
33
class Shipment < ApplicationRecord
44
belongs_to :sales_order
55
has_many :deliveries
66
has_many :locations, through: :deliveries
7+
8+
def delivering
9+
ActiveRecord::Base.transaction do
10+
location = Bouquet::Location.first
11+
12+
deliverie = deliveries.new(quantity: quantity, location_id: location.id)
13+
deliverie.date = Time.current
14+
deliverie.save
15+
16+
sales_order.product.assemblies.each do |assembly|
17+
assembly_quantity = assembly.quantity
18+
19+
storages = Bouquet::Storage.with_material(assembly.material_id)
20+
storages.each do |storage|
21+
break if assembly_quantity <= 0
22+
if storage.quantity > 0
23+
retrieval_quantity = if storage.quantity - assembly_quantity < 0
24+
storage.quantity
25+
else
26+
assembly_quantity
27+
end
28+
29+
retrieval = deliverie.retrievals.create(quantity: retrieval_quantity, storage_id: storage.id)
30+
retrieval.date = Time.current
31+
retrieval.save
32+
33+
assembly_quantity = assembly_quantity - retrieval_quantity
34+
end
35+
end
36+
37+
raise ActiveRecord::Rollback if assembly_quantity > 0
38+
end
39+
end
40+
end
741
end
842
end

core/app/models/bouquet/storage.rb

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1+
# Bouquet::Storage.all
2+
# Bouquet::Storage.with_material(1)
3+
# Bouquet::Storage.last.quantity
14
module Bouquet
25
class Storage < ApplicationRecord
36
belongs_to :stock
7+
has_many :retrievals
8+
9+
scope :with_material, ->(id) {
10+
joins({stock: {arrival: :purchase_order}})
11+
.where(bouquet_purchase_orders: {material_id: id})
12+
}
13+
14+
def retrieval_quantity
15+
retrievals.reduce(0) {|sum, retrieval| sum + retrieval.quantity}
16+
end
17+
18+
def quantity
19+
initial_quantity - retrieval_quantity
20+
end
21+
22+
def material_id
23+
stock.arrival.purchase_order.material_id
24+
end
25+
26+
def material_name
27+
stock.arrival.purchase_order.material.name
28+
end
429
end
530
end

core/db/migrate/20160111072002_create_bouquet_storages.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def change
33
create_table :bouquet_storages do |t|
44
t.string :state
55
t.date :date
6-
t.integer :quantity
6+
t.integer :initial_quantity
77
t.references :stock, index: true, foreign_key: true
88

99
t.timestamps

core/spec/dummy/db/schema.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@
139139
create_table "bouquet_storages", force: :cascade do |t|
140140
t.string "state"
141141
t.date "date"
142-
t.integer "quantity"
142+
t.integer "initial_quantity"
143143
t.integer "stock_id"
144-
t.datetime "created_at", null: false
145-
t.datetime "updated_at", null: false
144+
t.datetime "created_at", null: false
145+
t.datetime "updated_at", null: false
146146
t.index ["stock_id"], name: "index_bouquet_storages_on_stock_id"
147147
end
148148

core/spec/dummy/db/seeds.rb

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
# require './db/seeds'
22

33
# 製品加工
4-
product = Bouquet::Product.create_with(price: 1).find_or_create_by(name: :p1)
5-
material = Bouquet::Material.create_with(expiration_days: 1, order_lead_time: 2, order_quantity: 3).find_or_create_by(name: :m1)
6-
assembly = Bouquet::Assembly.create_with(quantity: 1).find_or_create_by(product_id: product.id, material_id: material.id)
4+
gerbera = Bouquet::Material.create_with(expiration_days: 1, order_lead_time: 2, order_quantity: 3).find_or_create_by(name: :Gerbera)
5+
tulip = Bouquet::Material.create_with(expiration_days: 1, order_lead_time: 2, order_quantity: 3).find_or_create_by(name: :Tulip)
6+
cosmos = Bouquet::Material.create_with(expiration_days: 1, order_lead_time: 2, order_quantity: 3).find_or_create_by(name: :Cosmos)
7+
sunflower = Bouquet::Material.create_with(expiration_days: 1, order_lead_time: 2, order_quantity: 3).find_or_create_by(name: :Sunflower)
8+
9+
product = Bouquet::Product.create_with(price: 1).find_or_create_by(name: :Cute)
10+
Bouquet::Assembly.create_with(quantity: 1).find_or_create_by(product_id: product.id, material_id: gerbera.id)
11+
Bouquet::Assembly.create_with(quantity: 2).find_or_create_by(product_id: product.id, material_id: tulip.id)
12+
13+
product = Bouquet::Product.create_with(price: 1).find_or_create_by(name: :Cool)
14+
Bouquet::Assembly.create_with(quantity: 1).find_or_create_by(product_id: product.id, material_id: gerbera.id)
15+
Bouquet::Assembly.create_with(quantity: 2).find_or_create_by(product_id: product.id, material_id: cosmos.id)
16+
17+
product = Bouquet::Product.create_with(price: 1).find_or_create_by(name: :Passion)
18+
Bouquet::Assembly.create_with(quantity: 1).find_or_create_by(product_id: product.id, material_id: gerbera.id)
19+
Bouquet::Assembly.create_with(quantity: 2).find_or_create_by(product_id: product.id, material_id: sunflower.id)
720

821
# 場所
922
location = Bouquet::Location.create_with(capacity: 100).find_or_create_by(name: :l1)
1023

1124
# 仕入れ
1225
supplier = Bouquet::Supplier.create_with(email: '[email protected]').find_or_create_by(name: :s1)
13-
purchase_order = Bouquet::PurchaseOrder.create_with(quantity: 1).find_or_create_by(supplier_id: supplier.id, material_id: material.id)
14-
arrival = Bouquet::Arrival.create_with(quantity: 1).find_or_create_by(purchase_order_id: purchase_order.id)
15-
stock = Bouquet::Stock.create_with(quantity: 1).find_or_create_by(location_id: location.id, arrival_id: arrival.id)
16-
storage = Bouquet::Storage.create_with(quantity: 1).find_or_create_by(stock_id: stock.id)
26+
# purchase_order = Bouquet::PurchaseOrder.create_with(quantity: 1).find_or_create_by(supplier_id: supplier.id, material_id: material.id)
27+
# arrival = Bouquet::Arrival.create_with(quantity: 1).find_or_create_by(purchase_order_id: purchase_order.id)
28+
# stock = Bouquet::Stock.create_with(quantity: 1).find_or_create_by(location_id: location.id, arrival_id: arrival.id)
29+
# storage = Bouquet::Storage.create_with(initial_quantity: 1).find_or_create_by(stock_id: stock.id)
1730

1831
# 販売
1932
customer = Bouquet::Customer.create_with(email: '[email protected]').find_or_create_by(name: :c1)
20-
sales_order = Bouquet::SalesOrder.create_with(quantity: 1).find_or_create_by(customer_id: customer.id, product_id: product.id)
21-
shipment = Bouquet::Shipment.create_with(quantity: 1).find_or_create_by(sales_order_id: sales_order.id)
22-
delivery = Bouquet::Delivery.create_with(quantity: 1).find_or_create_by(location_id: location.id, shipment_id: shipment.id)
23-
retrieval = Bouquet::Retrieval.create_with(quantity: 1).find_or_create_by(storage_id: storage.id, delivery_id: delivery.id)
33+
# sales_order = Bouquet::SalesOrder.create_with(quantity: 1).find_or_create_by(customer_id: customer.id, product_id: product.id)
34+
# shipment = Bouquet::Shipment.create_with(quantity: 1).find_or_create_by(sales_order_id: sales_order.id)
35+
# delivery = Bouquet::Delivery.create_with(quantity: 1).find_or_create_by(location_id: location.id, shipment_id: shipment.id)
36+
# retrieval = Bouquet::Retrieval.create_with(quantity: 1).find_or_create_by(storage_id: storage.id, delivery_id: delivery.id)

gate/app/controllers/bouquet/gate/products_controller.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,21 @@ class Gate::ProductsController < ApplicationController
66

77
# GET /products
88
def index
9-
@products = Bouquet::Product.all
9+
products = Bouquet::Product.all
10+
11+
@products = products.map do |product|
12+
{
13+
id: product.id,
14+
name: product.name,
15+
price: product.price,
16+
assemblies: product.assemblies.map do |assembly|
17+
{
18+
material_name: assembly.material.name,
19+
quantity: assembly.quantity
20+
}
21+
end
22+
}
23+
end
1024

1125
render json: @products
1226
end

gate/app/controllers/bouquet/gate/purchase_orders_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def show
1919
# POST /purchase_orders
2020
def create
2121
@purchase_order = Bouquet::PurchaseOrder.new(purchase_order_params)
22+
@purchase_order.arrival_date = Time.current
2223

2324
if @purchase_order.save
2425
render json: @purchase_order, status: :created #, location: @purchase_order

gate/app/controllers/bouquet/gate/sales_orders_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def show
1919
# POST /sales_orders
2020
def create
2121
@sales_order = Bouquet::SalesOrder.new(sales_order_params)
22+
@sales_order.shipment_date = Time.current
2223

2324
if @sales_order.save
2425
render json: @sales_order, status: :created #, location: @sales_order
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require_dependency "bouquet/gate/application_controller"
2+
3+
module Bouquet
4+
class Gate::StoragesController < ApplicationController
5+
6+
# GET /storages
7+
def index
8+
storages = Bouquet::Storage.all
9+
@storages = storages.map do |storage|
10+
{
11+
state: storage.state,
12+
date: storage.date,
13+
quantity: storage.quantity,
14+
material_id: storage.material_id,
15+
material_name: storage.material_name
16+
}
17+
end
18+
19+
render json: @storages
20+
end
21+
end
22+
end

gate/config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
resources :materials
55
resources :purchase_orders
66
resources :sales_orders
7+
resources :storages, only: :index
78
end
89
end

gate/spec/dummy/db/migrate/20160102160237_create_bouquet_products.bouquet.rb gate/spec/dummy/db/migrate/20160118114717_create_bouquet_products.bouquet.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class CreateBouquetProducts < ActiveRecord::Migration[5.0]
33
def change
44
create_table :bouquet_products do |t|
55
t.string :name
6+
t.integer :price
67

78
t.timestamps
89
end

gate/spec/dummy/db/migrate/20160112135257_create_bouquet_storages.bouquet.rb gate/spec/dummy/db/migrate/20160118114729_create_bouquet_storages.bouquet.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def change
44
create_table :bouquet_storages do |t|
55
t.string :state
66
t.date :date
7-
t.integer :quantity
7+
t.integer :initial_quantity
88
t.references :stock, index: true, foreign_key: true
99

1010
t.timestamps

gate/spec/dummy/db/schema.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20160112135258) do
14+
ActiveRecord::Schema.define(version: 20160118114730) do
1515

1616
create_table "bouquet_arrivals", force: :cascade do |t|
1717
t.string "state"
@@ -71,6 +71,7 @@
7171

7272
create_table "bouquet_products", force: :cascade do |t|
7373
t.string "name"
74+
t.integer "price"
7475
t.datetime "created_at", null: false
7576
t.datetime "updated_at", null: false
7677
end
@@ -138,10 +139,10 @@
138139
create_table "bouquet_storages", force: :cascade do |t|
139140
t.string "state"
140141
t.date "date"
141-
t.integer "quantity"
142+
t.integer "initial_quantity"
142143
t.integer "stock_id"
143-
t.datetime "created_at", null: false
144-
t.datetime "updated_at", null: false
144+
t.datetime "created_at", null: false
145+
t.datetime "updated_at", null: false
145146
t.index ["stock_id"], name: "index_bouquet_storages_on_stock_id"
146147
end
147148

store/app/assets/javascripts/bouquet/store/bundle.js

+16-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

store/app/assets/stylesheets/bouquet/store/bundle.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as types from '../constants/ActionTypes'
2+
import 'isomorphic-fetch'
3+
4+
//const endpoint = 'http://localhost:3000'
5+
const endpoint = ''
6+
7+
export function receiveStorages(storages) {
8+
return {
9+
type: types.STORAGE_RECEIVE,
10+
storages: storages
11+
}
12+
}
13+
14+
export function searchStorages() {
15+
return dispatch => {
16+
return fetch(endpoint + '/api/storages')
17+
.then(response => response.json())
18+
.then(json => dispatch(receiveStorages(json)))
19+
}
20+
}

store/private/static/app/actions/MaterialActions.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as types from '../constants/ActionTypes'
22
import 'isomorphic-fetch'
33

4-
const endpoint = 'http://localhost:3000'
4+
//const endpoint = 'http://localhost:3000'
5+
const endpoint = ''
56

67
export function receiveMaterials(materials) {
78
return {

store/private/static/app/actions/ProductActions.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as types from '../constants/ActionTypes'
22
import 'isomorphic-fetch'
33

4-
const endpoint = 'http://localhost:3000'
4+
//const endpoint = 'http://localhost:3000'
5+
const endpoint = ''
56

67
export function receiveProducts(products) {
78
return {

store/private/static/app/components/layouts/contents/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export Main from './main'
22
export Account from './account'
33
export Product from './product'
44
export Material from './material'
5+
export Inventory from './inventory'
56
export Counter from './counter'
67
export About from './about'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {bindActionCreators} from 'redux'
2+
import {connect} from 'react-redux'
3+
import layout from './layout'
4+
import * as InventoryActions from '../../../../actions/InventoryActions'
5+
6+
function mapStateToProps(state) {
7+
return {
8+
storages: state.inventory.storages
9+
}
10+
}
11+
12+
function mapDispatchToProps(dispatch) {
13+
return bindActionCreators(InventoryActions, dispatch)
14+
}
15+
16+
export default connect(mapStateToProps, mapDispatchToProps)(layout)

0 commit comments

Comments
 (0)