Commit 02d394bb by ivan Lan

Update order about seq & amount

parent 736c4a68
...@@ -51,13 +51,14 @@ class CreateShotengaiProductsAndOrders < ActiveRecord::Migration[5.1] ...@@ -51,13 +51,14 @@ class CreateShotengaiProductsAndOrders < ActiveRecord::Migration[5.1]
def create_order def create_order
create_table :shotengai_orders do |t| create_table :shotengai_orders do |t|
t.integer :seq t.string :seq
t.string :address t.string :address
t.decimal :amount, precision: 9, scale: 2
t.datetime :pay_time t.datetime :pay_time
t.datetime :delivery_time t.datetime :delivery_time
t.datetime :receipt_time t.datetime :receipt_time
t.string :delivery_way t.string :delivery_way
t.integer :delivery_cost t.integer :delivery_cost, default: 0
t.text :merchant_remark t.text :merchant_remark
t.string :mark # merchant mark, like red, blue .. t.string :mark # merchant mark, like red, blue ..
t.text :customer_remark t.text :customer_remark
......
...@@ -4,13 +4,14 @@ module Shotengai ...@@ -4,13 +4,14 @@ module Shotengai
# Table name: shotengai_orders # Table name: shotengai_orders
# #
# id :integer not null, primary key # id :integer not null, primary key
# seq :integer # seq :string(255)
# address :string(255) # address :string(255)
# amount :decimal(9, 2)
# pay_time :datetime # pay_time :datetime
# delivery_time :datetime # delivery_time :datetime
# receipt_time :datetime # receipt_time :datetime
# delivery_way :string(255) # delivery_way :string(255)
# delivery_cost :string(255) # delivery_cost :integer default(0)
# merchant_remark :text(65535) # merchant_remark :text(65535)
# mark :string(255) # mark :string(255)
# customer_remark :text(65535) # customer_remark :text(65535)
...@@ -35,12 +36,14 @@ module Shotengai ...@@ -35,12 +36,14 @@ module Shotengai
default_scope { where.not(status: 'cart') } default_scope { where.not(status: 'cart') }
scope :status_is, ->(status) { where(status.blank?.! && { status: status }) } scope :status_is, ->(status) { where(status.blank?.! && { status: status }) }
after_create :set_seq
include AASM_DLC include AASM_DLC
aasm column: :status do aasm column: :status do
state :unpaid, initial: true state :unpaid, initial: true
state :paid, :delivering, :received, :canceled, :evaluated state :paid, :delivering, :received, :canceled, :evaluated
event :pay, before: [:fill_snapshot, :cut_stock, :set_pay_time] do event :pay, before: [:set_amount, :fill_snapshot, :cut_stock, :set_pay_time] do
transitions from: :unpaid, to: :paid transitions from: :unpaid, to: :paid
end end
...@@ -95,11 +98,22 @@ module Shotengai ...@@ -95,11 +98,22 @@ module Shotengai
update!(receipt_time: Time.now) update!(receipt_time: Time.now)
end end
def total_price def set_amount
self.update!(amount: product_amount + delivery_cost)
end
def set_seq
timestamp = Time.now.strftime("%Y%m%d-%H%M")
no_length = 4
no = ("%0#{no_length}d" % id).last no_length
self.update!(seq: "#{timestamp}-#{no}}")
end
def product_amount
snapshots.sum(&:total_price) snapshots.sum(&:total_price)
end end
def total_original_price def product_original_amount
snapshots.sum(&:total_original_price) snapshots.sum(&:total_original_price)
end end
......
...@@ -3,13 +3,14 @@ ...@@ -3,13 +3,14 @@
# Table name: shotengai_orders # Table name: shotengai_orders
# #
# id :integer not null, primary key # id :integer not null, primary key
# seq :integer # seq :string(255)
# address :string(255) # address :string(255)
# amount :decimal(9, 2)
# pay_time :datetime # pay_time :datetime
# delivery_time :datetime # delivery_time :datetime
# receipt_time :datetime # receipt_time :datetime
# delivery_way :string(255) # delivery_way :string(255)
# delivery_cost :string(255) # delivery_cost :integer default(0)
# merchant_remark :text(65535) # merchant_remark :text(65535)
# mark :string(255) # mark :string(255)
# customer_remark :text(65535) # customer_remark :text(65535)
...@@ -21,6 +22,11 @@ ...@@ -21,6 +22,11 @@
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# #
# Indexes
#
# index_shotengai_orders_on_buyer_id_and_buyer_type (buyer_id,buyer_type)
# index_shotengai_orders_on_type (type)
#
FactoryGirl.define do FactoryGirl.define do
factory :shotengai_order do factory :shotengai_order do
......
...@@ -176,16 +176,24 @@ RSpec.describe 'Shotengai Models' do ...@@ -176,16 +176,24 @@ RSpec.describe 'Shotengai Models' do
it 'Methods' do it 'Methods' do
# total_price # total_price
expect(@order.total_price).to eq(@snapshot_1.total_price + @snapshot_2.total_price) expect(@order.product_amount).to eq(@snapshot_1.total_price + @snapshot_2.total_price)
expect(@order.total_original_price).to eq(@snapshot_1.total_original_price + @snapshot_2.total_original_price) expect(@order.product_original_amount).to eq(@snapshot_1.total_original_price + @snapshot_2.total_original_price)
end end
it 'About state machine' do it 'About state machine' do
expect(@snapshot_1.reload.attributes.values.include?(nil)).to eq(true) expect(@snapshot_1.reload.attributes.values.include?(nil)).to eq(true)
@order.pay! @order.pay!
expect(@order.seq).not_to be_nil
expect(@order.reload.pay_time).not_to be_nil expect(@order.reload.pay_time).not_to be_nil
# set amount
expect(@order.reload.amount).to eq(@order.snapshots.map(&:total_price).reduce(:+) + @order.delivery_cost)
# cannot edit or destroy snapshot unless the order is unpaid
# TODO:
# expect {
# @order.snapshots.first.destroy!
# }.to raise_error(ActiveRecord::RecordInvalid)
# copy snapshot info from series # copy snapshot info from series
expect(@snapshot_1.reload.attributes.values.-([:revised_amount]).include?(nil)).to eq(false) expect(@snapshot_1.reload.attributes.except('revised_amount').values.include?(nil)).to eq(false)
@order.send_out! @order.send_out!
# set delivery_time # set delivery_time
expect(@order.reload.delivery_time).not_to be_nil expect(@order.reload.delivery_time).not_to be_nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment