7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/spree_suppliers.rb', line 7
def self.activate
LineItem.class_eval do
has_many :invoice_items
end
Image.class_eval do
belongs_to :supplier
end
Admin::OrdersController.class_eval do
def show
load_order
@fee = 0.10
if current_user.has_role?("vendor")
@invoices = @order.supplier_invoices
@invoices.select! {|s| s.supplier_id == current_user.supplier.id}
else
@invoices = @order.supplier_invoices
end
respond_with(@order)
end
def index
params[:search] ||= {}
params[:search][:completed_at_is_not_null] ||= '1' if Spree::Config[:show_only_complete_orders_by_default]
@show_only_completed = params[:search][:completed_at_is_not_null].present?
params[:search][:meta_sort] ||= @show_only_completed ? 'completed_at.desc' : 'created_at.desc'
@search = Order.metasearch(params[:search])
if !params[:search][:created_at_greater_than].blank?
params[:search][:created_at_greater_than] = Time.zone.parse(params[:search][:created_at_greater_than]).beginning_of_day rescue ""
end
if !params[:search][:created_at_less_than].blank?
params[:search][:created_at_less_than] = Time.zone.parse(params[:search][:created_at_less_than]).end_of_day rescue ""
end
if @show_only_completed
params[:search][:completed_at_greater_than] = params[:search].delete(:created_at_greater_than)
params[:search][:completed_at_less_than] = params[:search].delete(:created_at_less_than)
end
@orders = Order.metasearch(params[:search]).includes([:user, :shipments, :payments]).page(params[:page]).per(Spree::Config[:orders_per_page])
if current_user.has_role?("vendor")
@orders.select! {|o| o.supplier_invoices.select {|s| s.supplier_id == current_user.supplier.id}.size > 0}
end
respond_with(@orders)
end
end
Order.class_eval do
has_many :supplier_invoices
def generate_invoices(order)
@order = order
@order_products = @order.line_items
@suppliers = @order_products.collect{|item| item.product.supplier_id}.uniq
@invoices = @suppliers.count
for i in 0..@invoices - 1
@supplier_products = @order_products.select{|x| x.product.supplier_id == @suppliers[i]}
@product_count = @supplier_products.count
invoice = SupplierInvoice.create(:order_id => @order.id, :supplier_id => @suppliers[i], :item_count => @product_count)
@supplier_products.each do |item|
invoice.invoice_items.create(:product_id => item.product.id, :quantity => item.quantity, :line_item_id => item.id)
end
item_total = "0.00".to_d
invoice.invoice_items.each do |i|
item_total = (i.line_item.variant.price * i.quantity) + item_total
end
invoice.update_attributes(:invoice_total => item_total)
@invoice = invoice
end
end
def finalize!
update_attribute(:completed_at, Time.now)
InventoryUnit.assign_opening_inventory(self)
adjustments.optional.each { |adjustment| adjustment.update_attribute('locked', true) }
generate_invoices(self)
self.state_events.create({
:previous_state => 'cart',
:next_state => 'complete',
:name => 'order' ,
:user_id => (User.respond_to?(:current) && User.current.try(:id)) || self.user_id
})
end
end
Admin::ProductsController.class_eval do
before_filter :load
before_filter :load_index, :only => [:index]
before_filter :edit_before, :only => [:edit]
create.before :create_before
create.fails :reset
update.before :update_taxons
def load
@suppliers = Supplier.find(:all, :order => "name")
@options = Taxon.all
end
def load_index
if current_user.roles.member?(Role.find_by_name("vendor"))
@collection.select! {|c| c.supplier_id == current_user.supplier.id}
end
end
def new
@object = Product.new()
@status = true
@suppliers = Supplier.all
end
def edit_before
@suppliers = Supplier.all
@status = false
end
def taxon_push object
object.taxons = []
Taxon.all.map {|m| object.taxons.push(Taxon.find_by_id(params[m.name])) if params.member?(m.name)}
return object
end
def reset
@status = true
end
def update_taxons
@object = taxon_push(@object)
end
def create_before
if current_user.has_role?("vendor")
@object = current_user.supplier.products.build(params[:product])
else
@object = Product.new(params[:product])
end
@object = taxon_push(@object)
end
def publish
p = Product.find_by_name(params[:id])
p.available_on = Date.today
p.save
redirect_to edit_admin_product_path(p)
end
def unpublish
p = Product.find_by_name(params[:id])
p.available_on = nil
p.save
redirect_to edit_admin_product_path(p)
end
end
Product.class_eval do
belongs_to :supplier
end
Taxon.class_eval do
has_and_belongs_to_many :suppliers
end
User.class_eval do
has_one :supplier
end
end
|