spree_suppliers 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +22 -0
- data/LICENSE +26 -0
- data/README.md +80 -0
- data/Rakefile +31 -0
- data/Versionfile +9 -0
- data/app/assets/javascripts/admin/spree_suppliers.js +1 -0
- data/app/assets/javascripts/invoice_items.js.coffee +3 -0
- data/app/assets/javascripts/store/spree_suppliers.js +1 -0
- data/app/assets/javascripts/supplier_invoices.js.coffee +3 -0
- data/app/assets/javascripts/suppliers.js.coffee +3 -0
- data/app/assets/stylesheets/admin/spree_suppliers.css +3 -0
- data/app/assets/stylesheets/invoice_items.css.scss +3 -0
- data/app/assets/stylesheets/store/spree_suppliers.css +3 -0
- data/app/assets/stylesheets/supplier_invoices.css.scss +3 -0
- data/app/assets/stylesheets/suppliers.css.scss +4 -0
- data/app/controllers/admin/supplier_taxons_controller.rb +61 -0
- data/app/controllers/admin/suppliers_controller.rb +127 -0
- data/app/controllers/admin/vendor_overview_controller.rb +18 -0
- data/app/controllers/invoice_items_controller.rb +2 -0
- data/app/controllers/supplier_invoices_controller.rb +2 -0
- data/app/controllers/suppliers_controller.rb +15 -0
- data/app/models/invoice_item.rb +4 -0
- data/app/models/supplier.rb +8 -0
- data/app/models/supplier_invoice.rb +4 -0
- data/app/overrides/admin.rb +5 -0
- data/app/views/admin/orders/show.html.erb +34 -0
- data/app/views/admin/orders/user.html.erb +19 -0
- data/app/views/admin/products/_form.html.erb +114 -0
- data/app/views/admin/products/_suppliers_form.html.erb +4 -0
- data/app/views/admin/shared/_order_tabs_decide.html.erb +5 -0
- data/app/views/admin/shared/_vendor_order_tabs.html.erb +39 -0
- data/app/views/admin/supplier_taxons/_form.html.erb +23 -0
- data/app/views/admin/supplier_taxons/_taxon_table.html.erb +23 -0
- data/app/views/admin/supplier_taxons/available.js.erb +33 -0
- data/app/views/admin/supplier_taxons/edit.html.erb +11 -0
- data/app/views/admin/supplier_taxons/remove.html.erb +1 -0
- data/app/views/admin/supplier_taxons/select.js.erb +2 -0
- data/app/views/admin/supplier_taxons/selected.html.erb +18 -0
- data/app/views/admin/suppliers/_edit_form.html.erb +75 -0
- data/app/views/admin/suppliers/_edit_hours_form.html.erb +18 -0
- data/app/views/admin/suppliers/_form.html.erb +60 -0
- data/app/views/admin/suppliers/_hours_form.html.erb +11 -0
- data/app/views/admin/suppliers/_image_form.html.erb +11 -0
- data/app/views/admin/suppliers/_supplier.html.erb +24 -0
- data/app/views/admin/suppliers/_supplier_tabs.html.erb +12 -0
- data/app/views/admin/suppliers/_suppliers_table.html.erb +25 -0
- data/app/views/admin/suppliers/_upload_image.html.erb +14 -0
- data/app/views/admin/suppliers/available.js.erb +34 -0
- data/app/views/admin/suppliers/edit.html.erb +13 -0
- data/app/views/admin/suppliers/index.html.erb +97 -0
- data/app/views/admin/suppliers/line_items.html.erb +20 -0
- data/app/views/admin/suppliers/new.html.erb +13 -0
- data/app/views/admin/suppliers/remove.html.erb +1 -0
- data/app/views/admin/suppliers/select.html.erb +1 -0
- data/app/views/admin/suppliers/selected.html.erb +40 -0
- data/app/views/admin/suppliers/show.html.erb +2 -0
- data/app/views/admin/vendor_overview/index.html.erb +1 -0
- data/app/views/shared/_supplier_invoices.html.erb +47 -0
- data/app/views/suppliers/_supplier.html.erb +7 -0
- data/app/views/suppliers/_supplier_list.erb +13 -0
- data/app/views/suppliers/index.html.erb +13 -0
- data/app/views/suppliers/show.html.erb +11 -0
- data/app/views/suppliers/supplier_list.erb +11 -0
- data/config/routes.rb +37 -0
- data/db/migrate/20120131160039_create_suppliers.rb +8 -0
- data/db/migrate/20120131160855_add_fields_to_suppliers.rb +16 -0
- data/db/migrate/20120131161247_create_supplier_invoices.rb +12 -0
- data/db/migrate/20120131161610_create_invoice_items.rb +12 -0
- data/db/migrate/20120201160616_create_suppliers_taxons.rb +16 -0
- data/db/migrate/20120201170705_add_supplier_id_to_products.rb +5 -0
- data/db/seeds.rb +143 -0
- data/db/seeds/images/apple.gif +0 -0
- data/db/seeds/images/apple.jpg +0 -0
- data/db/seeds/images/banana.jpg +0 -0
- data/db/seeds/images/blueberry.jpg +0 -0
- data/db/seeds/images/cherries.jpg +0 -0
- data/db/seeds/images/chicken.jpg +0 -0
- data/db/seeds/images/cow.jpg +0 -0
- data/db/seeds/images/flower.jpg +0 -0
- data/db/seeds/images/goat.jpg +0 -0
- data/db/seeds/images/grapes.jpg +0 -0
- data/db/seeds/images/greenapple.jpg +0 -0
- data/db/seeds/images/greenbeans.jpg +0 -0
- data/db/seeds/images/indie_candy.jpg +0 -0
- data/db/seeds/images/orange.jpg +0 -0
- data/db/seeds/images/squash.jpg +0 -0
- data/db/seeds/images/stock_user.jpg +0 -0
- data/db/seeds/images/strawberry.jpg +0 -0
- data/db/seeds/images/watermelon.jpg +0 -0
- data/lib/generators/spree_suppliers/install/install_generator.rb +31 -0
- data/lib/spree_suppliers.rb +192 -0
- data/lib/spree_suppliers/engine.rb +24 -0
- data/lib/spree_suppliers_hooks.rb +19 -0
- data/script/rails +7 -0
- data/spec/spec_helper.rb +31 -0
- data/spree_suppliers.gemspec +24 -0
- data/test/fixtures/.gitkeep +0 -0
- data/test/fixtures/invoice_items.yml +13 -0
- data/test/fixtures/supplier_invoices.yml +13 -0
- data/test/fixtures/suppliers.yml +11 -0
- data/test/functional/.gitkeep +0 -0
- data/test/functional/invoice_items_controller_test.rb +7 -0
- data/test/functional/supplier_invoices_controller_test.rb +7 -0
- data/test/functional/suppliers_controller_test.rb +7 -0
- data/test/integration/.gitkeep +0 -0
- data/test/performance/browsing_test.rb +12 -0
- data/test/test_helper.rb +13 -0
- data/test/unit/helpers/invoice_items_helper_test.rb +4 -0
- data/test/unit/helpers/supplier_invoices_helper_test.rb +4 -0
- data/test/unit/helpers/suppliers_helper_test.rb +4 -0
- data/test/unit/invoice_item_test.rb +7 -0
- data/test/unit/supplier_invoice_test.rb +7 -0
- data/test/unit/supplier_test.rb +7 -0
- metadata +156 -37
@@ -0,0 +1,13 @@
|
|
1
|
+
<li class="suppliers">
|
2
|
+
<div class="vendor-image">
|
3
|
+
<%= link_to supplier.images.first, supplier_path(supplier) %>
|
4
|
+
</div>
|
5
|
+
<div class="article-content">
|
6
|
+
<% if supplier.title %>
|
7
|
+
<h4><%= link_to supplier.title, supplier_path(supplier) %> </h4>
|
8
|
+
<% else %>
|
9
|
+
<h4><%= link_to supplier.name, supplier_path(supplier) %> </h4>
|
10
|
+
<% end %>
|
11
|
+
</p>
|
12
|
+
</div>
|
13
|
+
</li>
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<div class="search-sidebar">
|
2
|
+
<%= form_for @search do |f| %>
|
3
|
+
<p>
|
4
|
+
<%= f.label :search_vendors %>
|
5
|
+
<%= f.text_field :name_or_title_contains %>
|
6
|
+
</p>
|
7
|
+
<p class = "button"><%= f.submit "Search" %></p>
|
8
|
+
<% end %>
|
9
|
+
</div>
|
10
|
+
<div class="search-list">
|
11
|
+
<%= render :partial => "suppliers/supplier_list", :collection => @suppliers, :as => :supplier %>
|
12
|
+
</div>
|
13
|
+
<div class="clear"></div>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<li>
|
2
|
+
<div class="vendor-image">
|
3
|
+
<%= link_to supplier.images.first supplier_path(supplier) %>
|
4
|
+
</div>
|
5
|
+
<div class="article-content">
|
6
|
+
<h3><%= link_to supplier.title, supplier_path(supplier) %> </h3>
|
7
|
+
<p class="supplier_about">
|
8
|
+
<%= supplier.notes%> <br />
|
9
|
+
<span class="phone"><%= supplier.phone %></span></p>
|
10
|
+
</div>
|
11
|
+
</li>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Rails.application.routes.draw do
|
2
|
+
# Add your extension routes here
|
3
|
+
resources :suppliers
|
4
|
+
resources :supplier_invoices
|
5
|
+
resources :invoice_items
|
6
|
+
|
7
|
+
namespace :admin do
|
8
|
+
|
9
|
+
resources :suppliers do
|
10
|
+
get 'feature'
|
11
|
+
resources :taxons, :controller => "supplier_taxons" do
|
12
|
+
member do
|
13
|
+
get :select
|
14
|
+
delete :remove
|
15
|
+
end
|
16
|
+
collection do
|
17
|
+
post :available
|
18
|
+
post :batch_select
|
19
|
+
get :selected
|
20
|
+
post :update_taxons
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
resources :products do
|
26
|
+
resources :suppliers, :member => {:select => :post, :remove => :post}, :collection => {:available => :post, :selected => :get}
|
27
|
+
member do
|
28
|
+
get :publish
|
29
|
+
get :unpublish
|
30
|
+
end
|
31
|
+
end
|
32
|
+
resources :orders do
|
33
|
+
resources :suppliers, :collection => {:line_items => :get}
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class AddFieldsToSuppliers < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
add_column :suppliers, :name, :string
|
4
|
+
add_column :suppliers, :title, :string
|
5
|
+
add_column :suppliers, :phone, :string
|
6
|
+
add_column :suppliers, :fax, :string
|
7
|
+
add_column :suppliers, :email, :string
|
8
|
+
add_column :suppliers, :website, :string
|
9
|
+
add_column :suppliers, :notes, :text
|
10
|
+
add_column :suppliers, :featured, :boolean
|
11
|
+
add_column :suppliers, :twitter, :string
|
12
|
+
add_column :suppliers, :facebook, :string
|
13
|
+
add_column :suppliers, :description, :text
|
14
|
+
add_column :suppliers, :user_id, :integer
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateSuppliersTaxons < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :suppliers_taxons, :id => false do |t|
|
4
|
+
t.integer :supplier_id
|
5
|
+
t.integer :taxon_id
|
6
|
+
end
|
7
|
+
add_index :suppliers_taxons, :supplier_id
|
8
|
+
add_index :suppliers_taxons, :taxon_id
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
remove_index :suppliers_taxons, :column => :taxon_id
|
13
|
+
remove_index :suppliers_taxons, :column => :supplier_id
|
14
|
+
drop_table :suppliers_taxons
|
15
|
+
end
|
16
|
+
end
|
data/db/seeds.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
# Loads seed data out of default dir
|
2
|
+
SpreeCore::Engine.load_seed if defined?(SpreeCore)
|
3
|
+
|
4
|
+
puts "Create payment method"
|
5
|
+
ii = PaymentMethod::Check.create :name => "test", :active => true, :environment => "development"
|
6
|
+
|
7
|
+
puts "Create shipping method"
|
8
|
+
|
9
|
+
s = ShippingMethod.new :name => "Shipping", :zone => Zone.find_by_name("North America")
|
10
|
+
s.calculator = Calculator::FlatRate.new :calculable => s.id, :calculable_type => "ShippingMethod"
|
11
|
+
s.save
|
12
|
+
|
13
|
+
puts "Create Users"
|
14
|
+
|
15
|
+
@admin = User.create :email => "spree@example.com", :password => "password", :password_confirmation => "password", :first_name => "Spree", :last_name => "Cat", :description => "Sup."
|
16
|
+
@admin.roles = [Role.first]
|
17
|
+
@admin.save
|
18
|
+
@user = User.create :email => "test@example.com", :password => "password", :password_confirmation => "password"
|
19
|
+
@user2 = User.create :email => "test2@example.com", :password => "password", :password_confirmation => "password"
|
20
|
+
|
21
|
+
puts"Create Taxons"
|
22
|
+
|
23
|
+
t = Taxonomy.create :name => "Producers"
|
24
|
+
s = Taxonomy.create :name => "Products"
|
25
|
+
|
26
|
+
supplier = t.taxons.create :name => "Suppliers", :parent_id => t.id
|
27
|
+
toys = t.taxons.create :name => "Toys", :parent_id => t.id
|
28
|
+
candy = t.taxons.create :name => "Candy", :parent_id => s.id
|
29
|
+
t.taxons.create :name => "Clothing", :parent_id => s.id
|
30
|
+
t.taxons.create :name => "Recipes", :parent_id => t.id
|
31
|
+
|
32
|
+
s.taxons.create :name => "Potent Potables", :parent_id => t.id
|
33
|
+
s.taxons.create :name => "Childrens", :parent_id => s.id
|
34
|
+
s.taxons.create :name => "Baked Goods", :parent_id => s.id
|
35
|
+
|
36
|
+
|
37
|
+
puts"Create Suppliers"
|
38
|
+
|
39
|
+
supplier1 = Supplier.create(:name => "Farmer Bob", :featured => true, :notes => "Farmer Bob is a specialist in growing watermelon, squash, and tomatoes", :title => "Farmer Bob's Market")
|
40
|
+
supplier1.taxons = [supplier]
|
41
|
+
supplier1.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "chicken.jpg")))
|
42
|
+
supplier1.save
|
43
|
+
|
44
|
+
supplier2 = Supplier.create(:name => "Maury", :title => "Moonshine Maury", :featured => true)
|
45
|
+
supplier2.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "goat.jpg")))
|
46
|
+
supplier2.taxons = [supplier]
|
47
|
+
supplier2.save
|
48
|
+
|
49
|
+
supplier3 = Supplier.create(:name => "Fresh", :title => "Freshfully Farms", :featured => true)
|
50
|
+
supplier3.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "watermelon.jpg")))
|
51
|
+
supplier3.taxons = [supplier]
|
52
|
+
supplier3.save
|
53
|
+
|
54
|
+
supplier4 = Supplier.create(:name => "Betty", :title => "Betty's Bees", :featured => true)
|
55
|
+
supplier4.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "flower.jpg")))
|
56
|
+
supplier4.taxons = [supplier]
|
57
|
+
supplier4.save
|
58
|
+
|
59
|
+
supplier5 = Supplier.create(:name => "Larry", :title => "Larry's Lemonade", :description => "I am a supplier")
|
60
|
+
supplier5.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "cow.jpg")))
|
61
|
+
supplier5.taxons = [supplier]
|
62
|
+
supplier5.save
|
63
|
+
|
64
|
+
supplier6 = Supplier.create(:name => "Gary", :title => "Gardener Gary")
|
65
|
+
supplier6.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "banana.jpg")))
|
66
|
+
supplier6.taxons = [supplier]
|
67
|
+
supplier6.save
|
68
|
+
|
69
|
+
puts "Creating Sample Products"
|
70
|
+
|
71
|
+
a = Product.create :name => "Grapes", :price => "4.99", :permalink => "grapes", :supplier_id => supplier1.id, :available_on => 3.days.ago
|
72
|
+
a.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "grapes.jpg")))
|
73
|
+
a.taxons = [candy]
|
74
|
+
a.save
|
75
|
+
|
76
|
+
c = Product.create :name => "Banana", :price => "6.99", :permalink => "banana", :supplier_id => supplier3.id, :available_on => 3.days.ago
|
77
|
+
c.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "banana.jpg")))
|
78
|
+
c.taxons = [candy]
|
79
|
+
c.save
|
80
|
+
|
81
|
+
d = Product.create :name => "Corn", :price => "4.99", :permalink => "corn", :supplier_id => supplier4.id, :available_on => 3.days.ago
|
82
|
+
d.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "cow.jpg")))
|
83
|
+
d.taxons = [candy]
|
84
|
+
d.save
|
85
|
+
|
86
|
+
e = Product.create :name => "Blueberries", :price => "4.99", :permalink => "blueberries", :supplier_id => supplier4.id, :available_on => 3.days.ago
|
87
|
+
e.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "blueberry.jpg")))
|
88
|
+
e.taxons = [candy]
|
89
|
+
e.save
|
90
|
+
|
91
|
+
f = Product.create :name => "Strawberries", :price => "3.99", :permalink => "strawberries", :supplier_id => supplier5.id, :available_on => 3.days.ago
|
92
|
+
f.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "strawberry.jpg")))
|
93
|
+
f.taxons = [candy]
|
94
|
+
f.save
|
95
|
+
|
96
|
+
g = Product.create :name => "Oranges", :price => "2.99", :permalink => "oranges", :supplier_id => supplier6.id, :available_on => 3.days.ago
|
97
|
+
g.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "orange.jpg")))
|
98
|
+
g.taxons = [candy]
|
99
|
+
g.save
|
100
|
+
|
101
|
+
h =Product.create :name => "Cranberry", :price => "0.99", :permalink => "cranberry", :supplier_id => supplier1.id, :available_on => 3.days.ago
|
102
|
+
h.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "cherries.jpg")))
|
103
|
+
h.taxons = [candy]
|
104
|
+
h.save
|
105
|
+
|
106
|
+
i = Product.create :name => "Squash", :price => "2.99", :permalink => "squash", :supplier_id => supplier2.id, :available_on => 3.days.ago
|
107
|
+
i.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "squash.jpg")))
|
108
|
+
i.taxons = [toys]
|
109
|
+
i.save
|
110
|
+
|
111
|
+
j = Product.create :name => "Green Beans", :price => "1.99", :permalink => "greenbeans", :supplier_id => supplier3.id, :available_on => 3.days.ago
|
112
|
+
j.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "greenbeans.jpg")))
|
113
|
+
j.taxons = [toys]
|
114
|
+
j.save
|
115
|
+
|
116
|
+
g = Product.create :name => "Floranges", :price => "2.99", :permalink => "floranges", :supplier_id => supplier6.id, :available_on => 3.days.ago
|
117
|
+
g.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "orange.jpg")))
|
118
|
+
g.taxons = [candy]
|
119
|
+
g.save
|
120
|
+
|
121
|
+
h =Product.create :name => "Handberry", :price => "0.99", :permalink => "handberry", :supplier_id => supplier1.id, :available_on => 3.days.ago
|
122
|
+
h.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "cherries.jpg")))
|
123
|
+
h.taxons = [candy]
|
124
|
+
h.save
|
125
|
+
|
126
|
+
i = Product.create :name => "Tosh.0", :price => "2.99", :permalink => "tosh", :supplier_id => supplier2.id, :available_on => 3.days.ago
|
127
|
+
i.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "squash.jpg")))
|
128
|
+
i.taxons = [toys]
|
129
|
+
i.save
|
130
|
+
|
131
|
+
j = Product.create :name => "Kidney Beans", :price => "1.99", :permalink => "beans", :supplier_id => supplier3.id, :available_on => 3.days.ago
|
132
|
+
j.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "greenbeans.jpg")))
|
133
|
+
j.taxons = [toys]
|
134
|
+
j.save
|
135
|
+
|
136
|
+
j = Product.create :name => "Liver Beans", :price => "1.99", :permalink => "liver beans", :supplier_id => supplier3.id, :available_on => 3.days.ago
|
137
|
+
j.images.create(:attachment => File.open(File.join(Rails.root, "db", "seeds", "images", "greenbeans.jpg")))
|
138
|
+
j.taxons = [toys]
|
139
|
+
j.save
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
puts "Finished Seed Data"
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module SpreeSuppliers
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
def add_javascripts
|
8
|
+
append_file "app/assets/javascripts/store/all.js", "//= require store/spree_suppliers\n"
|
9
|
+
append_file "app/assets/javascripts/admin/all.js", "//= require admin/spree_suppliers\n"
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_stylesheets
|
13
|
+
inject_into_file "app/assets/stylesheets/store/all.css", " *= require store/spree_suppliers\n", :before => /\*\//, :verbose => true
|
14
|
+
inject_into_file "app/assets/stylesheets/admin/all.css", " *= require admin/spree_suppliers\n", :before => /\*\//, :verbose => true
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_migrations
|
18
|
+
run 'bundle exec rake railties:install:migrations FROM=spree_suppliers'
|
19
|
+
end
|
20
|
+
|
21
|
+
def run_migrations
|
22
|
+
res = ask "Would you like to run the migrations now? [Y/n]"
|
23
|
+
if res == "" || res.downcase == "y"
|
24
|
+
run 'bundle exec rake db:migrate'
|
25
|
+
else
|
26
|
+
puts "Skiping rake db:migrate, don't forget to run it!"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'spree_core'
|
2
|
+
require 'spree_suppliers/engine'
|
3
|
+
|
4
|
+
module SpreeSuppliers
|
5
|
+
class Engine < Rails::Engine
|
6
|
+
config.autoload_paths += %W(#{config.root}/lib)
|
7
|
+
def self.activate
|
8
|
+
|
9
|
+
LineItem.class_eval do
|
10
|
+
has_many :invoice_items
|
11
|
+
end
|
12
|
+
|
13
|
+
Image.class_eval do
|
14
|
+
belongs_to :supplier
|
15
|
+
end
|
16
|
+
|
17
|
+
Admin::OrdersController.class_eval do
|
18
|
+
def show
|
19
|
+
load_order
|
20
|
+
# optional fee that admin can charge to sell suppliers products for them
|
21
|
+
@fee = 0.10
|
22
|
+
if current_user.has_role?("vendor")
|
23
|
+
@invoices = @order.supplier_invoices
|
24
|
+
@invoices.select! {|s| s.supplier_id == current_user.supplier.id}
|
25
|
+
else
|
26
|
+
@invoices = @order.supplier_invoices
|
27
|
+
end
|
28
|
+
respond_with(@order)
|
29
|
+
end
|
30
|
+
|
31
|
+
def index
|
32
|
+
params[:search] ||= {}
|
33
|
+
params[:search][:completed_at_is_not_null] ||= '1' if Spree::Config[:show_only_complete_orders_by_default]
|
34
|
+
@show_only_completed = params[:search][:completed_at_is_not_null].present?
|
35
|
+
params[:search][:meta_sort] ||= @show_only_completed ? 'completed_at.desc' : 'created_at.desc'
|
36
|
+
|
37
|
+
@search = Order.metasearch(params[:search])
|
38
|
+
|
39
|
+
if !params[:search][:created_at_greater_than].blank?
|
40
|
+
params[:search][:created_at_greater_than] = Time.zone.parse(params[:search][:created_at_greater_than]).beginning_of_day rescue ""
|
41
|
+
end
|
42
|
+
|
43
|
+
if !params[:search][:created_at_less_than].blank?
|
44
|
+
params[:search][:created_at_less_than] = Time.zone.parse(params[:search][:created_at_less_than]).end_of_day rescue ""
|
45
|
+
end
|
46
|
+
|
47
|
+
if @show_only_completed
|
48
|
+
params[:search][:completed_at_greater_than] = params[:search].delete(:created_at_greater_than)
|
49
|
+
params[:search][:completed_at_less_than] = params[:search].delete(:created_at_less_than)
|
50
|
+
end
|
51
|
+
|
52
|
+
@orders = Order.metasearch(params[:search]).includes([:user, :shipments, :payments]).page(params[:page]).per(Spree::Config[:orders_per_page])
|
53
|
+
|
54
|
+
if current_user.has_role?("vendor")
|
55
|
+
@orders.select! {|o| o.supplier_invoices.select {|s| s.supplier_id == current_user.supplier.id}.size > 0}
|
56
|
+
end
|
57
|
+
respond_with(@orders)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
Order.class_eval do
|
62
|
+
has_many :supplier_invoices
|
63
|
+
def generate_invoices(order)
|
64
|
+
@order = order
|
65
|
+
@order_products = @order.line_items
|
66
|
+
@suppliers = @order_products.collect{|item| item.product.supplier_id}.uniq
|
67
|
+
@invoices = @suppliers.count
|
68
|
+
|
69
|
+
for i in 0..@invoices - 1
|
70
|
+
@supplier_products = @order_products.select{|x| x.product.supplier_id == @suppliers[i]}
|
71
|
+
@product_count = @supplier_products.count
|
72
|
+
invoice = SupplierInvoice.create(:order_id => @order.id, :supplier_id => @suppliers[i], :item_count => @product_count)
|
73
|
+
|
74
|
+
@supplier_products.each do |item|
|
75
|
+
invoice.invoice_items.create(:product_id => item.product.id, :quantity => item.quantity, :line_item_id => item.id)
|
76
|
+
end
|
77
|
+
|
78
|
+
item_total = "0.00".to_d
|
79
|
+
invoice.invoice_items.each do |i|
|
80
|
+
item_total = (i.line_item.variant.price * i.quantity) + item_total
|
81
|
+
end
|
82
|
+
invoice.update_attributes(:invoice_total => item_total)
|
83
|
+
@invoice = invoice
|
84
|
+
#SupplierMailer.invoice_email(@invoice).deliver
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def finalize!
|
89
|
+
update_attribute(:completed_at, Time.now)
|
90
|
+
InventoryUnit.assign_opening_inventory(self)
|
91
|
+
# lock any optional adjustments (coupon promotions, etc.)
|
92
|
+
adjustments.optional.each { |adjustment| adjustment.update_attribute('locked', true) }
|
93
|
+
# generate the invoices for each supplier
|
94
|
+
generate_invoices(self)
|
95
|
+
#OrderMailer.confirm_email(self).deliver
|
96
|
+
|
97
|
+
self.state_events.create({
|
98
|
+
:previous_state => 'cart',
|
99
|
+
:next_state => 'complete',
|
100
|
+
:name => 'order' ,
|
101
|
+
:user_id => (User.respond_to?(:current) && User.current.try(:id)) || self.user_id
|
102
|
+
})
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
Admin::ProductsController.class_eval do
|
108
|
+
before_filter :load
|
109
|
+
before_filter :load_index, :only => [:index]
|
110
|
+
before_filter :edit_before, :only => [:edit]
|
111
|
+
create.before :create_before
|
112
|
+
create.fails :reset
|
113
|
+
update.before :update_taxons
|
114
|
+
|
115
|
+
def load
|
116
|
+
@suppliers = Supplier.find(:all, :order => "name")
|
117
|
+
@options = Taxon.all
|
118
|
+
end
|
119
|
+
|
120
|
+
def load_index
|
121
|
+
if current_user.roles.member?(Role.find_by_name("vendor"))
|
122
|
+
@collection.select! {|c| c.supplier_id == current_user.supplier.id}
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
#indicate that we want to create a new product
|
127
|
+
def new
|
128
|
+
@object = Product.new()
|
129
|
+
@status = true
|
130
|
+
@suppliers = Supplier.all
|
131
|
+
end
|
132
|
+
|
133
|
+
def edit_before
|
134
|
+
@suppliers = Supplier.all
|
135
|
+
@status = false
|
136
|
+
end
|
137
|
+
|
138
|
+
def taxon_push object
|
139
|
+
object.taxons = []
|
140
|
+
Taxon.all.map {|m| object.taxons.push(Taxon.find_by_id(params[m.name])) if params.member?(m.name)}
|
141
|
+
return object
|
142
|
+
end
|
143
|
+
|
144
|
+
def reset
|
145
|
+
@status = true
|
146
|
+
end
|
147
|
+
|
148
|
+
def update_taxons
|
149
|
+
@object = taxon_push(@object)
|
150
|
+
end
|
151
|
+
|
152
|
+
def create_before
|
153
|
+
if current_user.has_role?("vendor")
|
154
|
+
@object = current_user.supplier.products.build(params[:product])
|
155
|
+
else
|
156
|
+
@object = Product.new(params[:product])
|
157
|
+
end
|
158
|
+
@object = taxon_push(@object)
|
159
|
+
end
|
160
|
+
|
161
|
+
def publish
|
162
|
+
p = Product.find_by_name(params[:id])
|
163
|
+
p.available_on = Date.today
|
164
|
+
p.save
|
165
|
+
redirect_to edit_admin_product_path(p)
|
166
|
+
end
|
167
|
+
|
168
|
+
def unpublish
|
169
|
+
p = Product.find_by_name(params[:id])
|
170
|
+
p.available_on = nil
|
171
|
+
p.save
|
172
|
+
redirect_to edit_admin_product_path(p)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
Product.class_eval do
|
177
|
+
belongs_to :supplier
|
178
|
+
end
|
179
|
+
|
180
|
+
Taxon.class_eval do
|
181
|
+
has_and_belongs_to_many :suppliers
|
182
|
+
end
|
183
|
+
|
184
|
+
User.class_eval do
|
185
|
+
belongs_to :supplier
|
186
|
+
end
|
187
|
+
|
188
|
+
end
|
189
|
+
config.to_prepare &method(:activate).to_proc
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|