vyapari 0.1.5dev10 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/vyapari/admin/base_controller.rb +0 -9
- data/app/controllers/vyapari/admin/countries_controller.rb +2 -2
- data/app/controllers/vyapari/admin/dashboard_controller.rb +1 -3
- data/app/controllers/vyapari/admin/exchange_rates_controller.rb +2 -2
- data/app/controllers/vyapari/admin/regions_controller.rb +1 -1
- data/app/controllers/vyapari/admin/resource_controller.rb +0 -1
- data/app/controllers/vyapari/admin/users_controller.rb +130 -0
- data/app/controllers/vyapari/application_controller.rb +0 -11
- data/app/models/brand.rb +19 -67
- data/app/models/category.rb +19 -63
- data/app/models/country.rb +7 -31
- data/app/models/exchange_rate.rb +7 -32
- data/app/models/product.rb +20 -75
- data/app/models/region.rb +20 -37
- data/app/models/vyapari/application_record.rb +0 -3
- data/app/views/layouts/kuppayam/_footer.html.erb +1 -1
- data/app/views/layouts/kuppayam/_sidebar.html.erb +44 -45
- data/app/views/vyapari/admin/countries/_index.html.erb +1 -1
- data/app/views/vyapari/admin/countries/index.html.erb +3 -19
- data/app/views/vyapari/admin/exchange_rates/_form.html.erb +4 -3
- data/app/views/vyapari/admin/exchange_rates/_index.html.erb +7 -7
- data/app/views/vyapari/admin/exchange_rates/_row.html.erb +3 -3
- data/app/views/vyapari/admin/exchange_rates/_show.html.erb +0 -1
- data/app/views/vyapari/admin/exchange_rates/index.html.erb +3 -19
- data/app/views/vyapari/admin/regions/_index.html.erb +1 -1
- data/app/views/vyapari/admin/regions/index.html.erb +3 -19
- data/app/views/vyapari/admin/users/_form.html.erb +39 -0
- data/app/views/vyapari/admin/users/_index.html.erb +101 -0
- data/app/views/vyapari/admin/users/_row.html.erb +72 -0
- data/app/views/vyapari/admin/users/_show.html.erb +199 -0
- data/app/views/vyapari/admin/{categories → users}/index.html.erb +24 -8
- data/config/routes.rb +5 -66
- data/db/migrate/20170000000200_create_exchange_rates.rb +2 -3
- data/lib/tasks/vyapari_tasks.rake +4 -0
- data/lib/vyapari/version.rb +1 -1
- metadata +15 -167
- data/app/assets/images/vyapari/sample_stock_bundle.csv +0 -1
- data/app/controllers/vyapari/admin/brands_controller.rb +0 -99
- data/app/controllers/vyapari/admin/categories_controller.rb +0 -115
- data/app/controllers/vyapari/admin/products_controller.rb +0 -79
- data/app/controllers/vyapari/admin/stores_controller.rb +0 -67
- data/app/controllers/vyapari/admin/suppliers_controller.rb +0 -67
- data/app/controllers/vyapari/admin/terminals_controller.rb +0 -150
- data/app/controllers/vyapari/store_manager/base_controller.rb +0 -33
- data/app/controllers/vyapari/store_manager/dashboard_controller.rb +0 -31
- data/app/controllers/vyapari/store_manager/reports/invoices_controller.rb +0 -249
- data/app/controllers/vyapari/store_manager/reports/sales_controller.rb +0 -299
- data/app/controllers/vyapari/store_manager/reports/stock_controller.rb +0 -132
- data/app/controllers/vyapari/store_manager/resource_controller.rb +0 -17
- data/app/controllers/vyapari/store_manager/stock_bundles_controller.rb +0 -206
- data/app/controllers/vyapari/store_manager/stock_entries_controller.rb +0 -161
- data/app/controllers/vyapari/terminal_staff/base_controller.rb +0 -38
- data/app/controllers/vyapari/terminal_staff/dashboard_controller.rb +0 -35
- data/app/controllers/vyapari/terminal_staff/invoices_controller.rb +0 -147
- data/app/controllers/vyapari/terminal_staff/line_items_controller.rb +0 -139
- data/app/controllers/vyapari/terminal_staff/resource_controller.rb +0 -17
- data/app/controllers/vyapari/user_dashboard_controller.rb +0 -31
- data/app/models/bank_account.rb +0 -44
- data/app/models/contact.rb +0 -72
- data/app/models/image/brand_image.rb +0 -3
- data/app/models/image/category_image.rb +0 -3
- data/app/models/image/product_image.rb +0 -3
- data/app/models/invoice.rb +0 -256
- data/app/models/line_item.rb +0 -207
- data/app/models/stock_bundle.rb +0 -249
- data/app/models/stock_entry.rb +0 -275
- data/app/models/store.rb +0 -302
- data/app/models/supplier.rb +0 -79
- data/app/models/terminal.rb +0 -158
- data/app/uploaders/brand_image_uploader.rb +0 -14
- data/app/uploaders/category_image_uploader.rb +0 -14
- data/app/uploaders/product_image_uploader.rb +0 -14
- data/app/uploaders/stock_bundle_uploader.rb +0 -10
- data/app/views/layouts/vyapari/_store_manager_menu.html.erb +0 -126
- data/app/views/layouts/vyapari/_terminal_staff_menu.html.erb +0 -106
- data/app/views/layouts/vyapari/store_manager.html.erb +0 -112
- data/app/views/layouts/vyapari/terminal_staff.html.erb +0 -116
- data/app/views/vyapari/admin/brands/_form.html.erb +0 -23
- data/app/views/vyapari/admin/brands/_index.html.erb +0 -89
- data/app/views/vyapari/admin/brands/_row.html.erb +0 -63
- data/app/views/vyapari/admin/brands/_show.html.erb +0 -104
- data/app/views/vyapari/admin/brands/index.html.erb +0 -48
- data/app/views/vyapari/admin/categories/_form.html.erb +0 -28
- data/app/views/vyapari/admin/categories/_index.html.erb +0 -101
- data/app/views/vyapari/admin/categories/_row.html.erb +0 -69
- data/app/views/vyapari/admin/categories/_show.html.erb +0 -115
- data/app/views/vyapari/admin/products/_form.html.erb +0 -32
- data/app/views/vyapari/admin/products/_index.html.erb +0 -58
- data/app/views/vyapari/admin/products/_row.html.erb +0 -30
- data/app/views/vyapari/admin/products/_show.html.erb +0 -81
- data/app/views/vyapari/admin/products/index.html.erb +0 -48
- data/app/views/vyapari/admin/stores/_form.html.erb +0 -37
- data/app/views/vyapari/admin/stores/_index.html.erb +0 -84
- data/app/views/vyapari/admin/stores/_row.html.erb +0 -55
- data/app/views/vyapari/admin/stores/_show.html.erb +0 -110
- data/app/views/vyapari/admin/stores/index.html.erb +0 -48
- data/app/views/vyapari/admin/suppliers/_form.html.erb +0 -32
- data/app/views/vyapari/admin/suppliers/_index.html.erb +0 -58
- data/app/views/vyapari/admin/suppliers/_row.html.erb +0 -30
- data/app/views/vyapari/admin/suppliers/_show.html.erb +0 -81
- data/app/views/vyapari/admin/suppliers/index.html.erb +0 -48
- data/app/views/vyapari/admin/terminals/_form.html.erb +0 -24
- data/app/views/vyapari/admin/terminals/_index.html.erb +0 -75
- data/app/views/vyapari/admin/terminals/_row.html.erb +0 -49
- data/app/views/vyapari/admin/terminals/_show.html.erb +0 -74
- data/app/views/vyapari/store_manager/dashboard/_sales_counts.html.erb +0 -90
- data/app/views/vyapari/store_manager/dashboard/_stock_counts.html.erb +0 -63
- data/app/views/vyapari/store_manager/dashboard/_terminals.html.erb +0 -23
- data/app/views/vyapari/store_manager/dashboard/index.html.erb +0 -64
- data/app/views/vyapari/store_manager/reports/invoices/_index.html.erb +0 -65
- data/app/views/vyapari/store_manager/reports/invoices/index.html.erb +0 -105
- data/app/views/vyapari/store_manager/reports/sales/_index.html.erb +0 -64
- data/app/views/vyapari/store_manager/reports/sales/index.html.erb +0 -105
- data/app/views/vyapari/store_manager/reports/stock/_index.html.erb +0 -72
- data/app/views/vyapari/store_manager/reports/stock/index.html.erb +0 -32
- data/app/views/vyapari/store_manager/stock_bundles/_form.html.erb +0 -99
- data/app/views/vyapari/store_manager/stock_bundles/_index.html.erb +0 -74
- data/app/views/vyapari/store_manager/stock_bundles/_row.html.erb +0 -44
- data/app/views/vyapari/store_manager/stock_bundles/_show.html.erb +0 -110
- data/app/views/vyapari/store_manager/stock_bundles/create.html.erb +0 -57
- data/app/views/vyapari/store_manager/stock_bundles/index.html.erb +0 -36
- data/app/views/vyapari/store_manager/stock_bundles/update.html.erb +0 -57
- data/app/views/vyapari/store_manager/stock_entries/_form.html.erb +0 -50
- data/app/views/vyapari/store_manager/stock_entries/_index.html.erb +0 -80
- data/app/views/vyapari/store_manager/stock_entries/_row.html.erb +0 -33
- data/app/views/vyapari/store_manager/stock_entries/_show.html.erb +0 -110
- data/app/views/vyapari/store_manager/stock_entries/_stock_bundle.html.erb +0 -61
- data/app/views/vyapari/store_manager/stock_entries/index.html.erb +0 -45
- data/app/views/vyapari/terminal_staff/dashboard/_counts.html.erb +0 -131
- data/app/views/vyapari/terminal_staff/dashboard/_invoices.html.erb +0 -44
- data/app/views/vyapari/terminal_staff/dashboard/_search_form.html.erb +0 -6
- data/app/views/vyapari/terminal_staff/dashboard/_search_results.html.erb +0 -24
- data/app/views/vyapari/terminal_staff/dashboard/index.html.erb +0 -56
- data/app/views/vyapari/terminal_staff/dashboard/search.js.erb +0 -12
- data/app/views/vyapari/terminal_staff/invoices/_draft.html.erb +0 -62
- data/app/views/vyapari/terminal_staff/invoices/_form.html.erb +0 -134
- data/app/views/vyapari/terminal_staff/invoices/_index.html.erb +0 -70
- data/app/views/vyapari/terminal_staff/invoices/_row.html.erb +0 -33
- data/app/views/vyapari/terminal_staff/invoices/_show.html.erb +0 -204
- data/app/views/vyapari/terminal_staff/invoices/index.html.erb +0 -35
- data/app/views/vyapari/terminal_staff/invoices/new.js.erb +0 -18
- data/app/views/vyapari/terminal_staff/invoices/show.html.erb +0 -1
- data/app/views/vyapari/terminal_staff/invoices/show.js.erb +0 -13
- data/app/views/vyapari/terminal_staff/line_items/_form.html.erb +0 -40
- data/app/views/vyapari/terminal_staff/line_items/_index.html.erb +0 -96
- data/app/views/vyapari/terminal_staff/line_items/create.js.erb +0 -34
- data/app/views/vyapari/terminal_staff/line_items/destroy.js.erb +0 -25
- data/app/views/vyapari/user_dashboard/index.html.erb +0 -51
- data/db/import_data/brands.csv +0 -7
- data/db/import_data/categories.csv +0 -12
- data/db/import_data/countries.csv +0 -1
- data/db/import_data/dummy/brands.csv +0 -7
- data/db/import_data/dummy/categories.csv +0 -12
- data/db/import_data/dummy/countries.csv +0 -1
- data/db/import_data/dummy/exchange_rates.csv +0 -5
- data/db/import_data/dummy/products-copy.csv +0 -1
- data/db/import_data/dummy/products.csv +0 -1
- data/db/import_data/dummy/products.xlsx +0 -0
- data/db/import_data/dummy/regions.csv +0 -13
- data/db/import_data/dummy/stores.csv +0 -10
- data/db/import_data/dummy/suppliers.csv +0 -14
- data/db/import_data/dummy/terminals.csv +0 -11
- data/db/import_data/exchange_rates.csv +0 -5
- data/db/import_data/regions.csv +0 -13
- data/db/import_data/stores.csv +0 -3
- data/db/import_data/suppliers.csv +0 -14
- data/db/import_data/terminals.csv +0 -3
- data/db/migrate/20170000000203_create_contacts.rb +0 -22
- data/db/migrate/20170000000204_create_bank_accounts.rb +0 -21
- data/db/migrate/20170000000205_create_suppliers.rb +0 -18
- data/db/migrate/20170000000206_create_stores.rb +0 -21
- data/db/migrate/20170000000207_create_terminals.rb +0 -18
- data/db/migrate/20170000000210_create_brands.rb +0 -14
- data/db/migrate/20170000000211_create_categories.rb +0 -22
- data/db/migrate/20170000000212_create_products.rb +0 -29
- data/db/migrate/20170000000213_create_invoices.rb +0 -61
- data/db/migrate/20170000000215_create_stock_bundles.rb +0 -17
- data/db/migrate/20170000000216_create_stock_entries.rb +0 -20
- data/db/sample_reports/products.xlsx +0 -0
- data/lib/tasks/vyapari/all.rake +0 -73
data/app/models/stock_bundle.rb
DELETED
@@ -1,249 +0,0 @@
|
|
1
|
-
require 'csv'
|
2
|
-
|
3
|
-
class StockBundle < ActiveRecord::Base
|
4
|
-
|
5
|
-
# Constants
|
6
|
-
PENDING = "pending" # Default Status
|
7
|
-
ERRORED = "errored"
|
8
|
-
APPROVED = "approved"
|
9
|
-
|
10
|
-
STATUS = {"Pending" => PENDING, "Approved" => APPROVED, "Errored" => ERRORED}
|
11
|
-
STATUS_REVERSE = {PENDING => "Pending", APPROVED => "Approved", ERRORED => "Errored"}
|
12
|
-
|
13
|
-
# Validations
|
14
|
-
validates :name, :presence=> true, uniqueness: true
|
15
|
-
validates :uploaded_date, presence: true
|
16
|
-
validates :status, :presence=> true, :inclusion => {:in => STATUS_REVERSE.keys, :presence_of => :status, :message => "%{value} is not a valid status" }
|
17
|
-
# validates :file, :presence=> true
|
18
|
-
|
19
|
-
# Associations
|
20
|
-
has_many :stock_entries
|
21
|
-
belongs_to :uploader, class_name: 'User'
|
22
|
-
belongs_to :supplier, optional: true
|
23
|
-
belongs_to :store
|
24
|
-
|
25
|
-
# Uploader
|
26
|
-
mount_uploader :file, File::StockBundleUploader
|
27
|
-
mount_uploader :error_file, File::StockBundleUploader
|
28
|
-
|
29
|
-
# ------------------
|
30
|
-
# Class Methods
|
31
|
-
# ------------------
|
32
|
-
|
33
|
-
# return an active record relation object with the search query in its where clause
|
34
|
-
# Return the ActiveRecord::Relation object
|
35
|
-
# == Examples
|
36
|
-
# >>> obj.search(query)
|
37
|
-
# => ActiveRecord::Relation object
|
38
|
-
scope :search, lambda {|query| joins(:supplier, :store).where("LOWER(suppliers.name) LIKE LOWER('%#{query}%') OR LOWER(stores.name) LIKE LOWER('%#{query}%')")}
|
39
|
-
|
40
|
-
scope :status, lambda { |status| where ("LOWER(stock_bundles.status)='#{status}'") }
|
41
|
-
|
42
|
-
scope :pending, -> { where(status: PENDING) }
|
43
|
-
scope :approved, -> { where(status: APPROVED) }
|
44
|
-
|
45
|
-
# ------------------
|
46
|
-
# Class Methods
|
47
|
-
# ------------------
|
48
|
-
|
49
|
-
def parse_stocks
|
50
|
-
|
51
|
-
# For some reasn it throws error for self.error_details unless we reload
|
52
|
-
self.reload
|
53
|
-
|
54
|
-
path = "#{Rails.root}/public#{self.file.url}"
|
55
|
-
begin
|
56
|
-
csv_table = CSV.table(path, {headers: true, converters: nil, header_converters: :symbol})
|
57
|
-
rescue CSV::MalformedCSVError => e
|
58
|
-
self.error_summary = "The Uploaded File is corrupted."
|
59
|
-
self.error_details = "#{e.class}: #{e.message}"
|
60
|
-
self.save
|
61
|
-
puts self.error_summary.red
|
62
|
-
puts self.error_details.red
|
63
|
-
return false
|
64
|
-
rescue Exception => e
|
65
|
-
self.error_summary = "The Uploaded File format is not supported."
|
66
|
-
self.error_details = "#{e.class}: #{e.message}"
|
67
|
-
puts self.error_summary.red
|
68
|
-
puts self.error_details.red
|
69
|
-
self.save
|
70
|
-
return false
|
71
|
-
end
|
72
|
-
|
73
|
-
headers = csv_table.headers
|
74
|
-
|
75
|
-
StockEntry.where(stock_bundle: self.id).destroy_all
|
76
|
-
|
77
|
-
# We need a collection of all the column headings to pass to error hander to reproduce the errors in same format
|
78
|
-
columns = [:env_sku, :name, :reference_number, :one_liner, :description, :purchased_price, :landed_price, :selling_price, :retail_price, :brand, :category, :quantity]
|
79
|
-
|
80
|
-
# Initializing the Data Error to store errors for each column
|
81
|
-
data_error = Kuppayam::Importer::DataError.new
|
82
|
-
data_error.columns = columns
|
83
|
-
|
84
|
-
csv_table.each_with_index do |row, i|
|
85
|
-
|
86
|
-
row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
|
87
|
-
|
88
|
-
if row[:ean_sku].blank?
|
89
|
-
data_error.add_column_error(:ean_sku, "", "ENV / SKU number is blank", i)
|
90
|
-
next
|
91
|
-
end
|
92
|
-
|
93
|
-
if row[:quantity].blank?
|
94
|
-
data_error.add_column_error(:quantity, "", "Quantity is blank", i)
|
95
|
-
next
|
96
|
-
end
|
97
|
-
|
98
|
-
product = Product.where("ean_sku = ?", row[:ean_sku]).first || Product.new
|
99
|
-
|
100
|
-
product.ean_sku = row[:ean_sku]
|
101
|
-
product.name = row[:name]
|
102
|
-
product.reference_number = row[:reference_number]
|
103
|
-
product.one_liner = row[:one_liner]
|
104
|
-
product.description = row[:description]
|
105
|
-
|
106
|
-
product.purchased_price = row[:purchased_price]
|
107
|
-
product.landed_price = row[:landed_price]
|
108
|
-
product.selling_price = row[:selling_price]
|
109
|
-
product.retail_price = row[:retail_price]
|
110
|
-
|
111
|
-
product.purchased_price = 0.00 if product.purchased_price.blank?
|
112
|
-
product.landed_price = 0.00 if product.landed_price.blank?
|
113
|
-
product.selling_price = 0.00 if product.selling_price.blank?
|
114
|
-
product.retail_price = 0.00 if product.retail_price.blank?
|
115
|
-
|
116
|
-
product.brand = Brand.where("name = ?", row[:brand]).first || product.build_brand(name: row[:brand]) if row[:brand]
|
117
|
-
product.category = Category.where("name = ?", row[:category]).first || product.build_category(name: row[:category]) if row[:category]
|
118
|
-
|
119
|
-
stock_entry = StockEntry.new()
|
120
|
-
|
121
|
-
stock_entry.store = self.store
|
122
|
-
stock_entry.product = product
|
123
|
-
stock_entry.supplier = self.supplier
|
124
|
-
stock_entry.quantity = row[:quantity]
|
125
|
-
stock_entry.stock_bundle = self
|
126
|
-
stock_entry.status = :pending
|
127
|
-
|
128
|
-
if product.valid? && stock_entry.valid?
|
129
|
-
product.save!
|
130
|
-
stock_entry.save!
|
131
|
-
else
|
132
|
-
unless product.valid?
|
133
|
-
columns.each do |col_name|
|
134
|
-
data_error.add_column_error(col_name, row[col_name], product.errors[col_name], i) if product.errors.has_key?(col_name)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
unless stock_entry.valid?
|
138
|
-
columns.each do |col_name|
|
139
|
-
data_error.add_column_error(col_name, row[col_name], stock_entry.errors[col_name], i) if stock_entry.errors.has_key?(col_name)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
end
|
145
|
-
|
146
|
-
if data_error.errors.any?
|
147
|
-
self.error_summary = "There are few errors in some of the rows in the CSV file. Open the error file to know more."
|
148
|
-
# self.error_file = data_error.generate_error_file
|
149
|
-
end
|
150
|
-
|
151
|
-
self.save!
|
152
|
-
self.stock_entries.update_all(status: :active) if data_error.errors.blank?
|
153
|
-
|
154
|
-
return data_error.errors.blank?
|
155
|
-
end
|
156
|
-
|
157
|
-
# ------------------
|
158
|
-
# Instance Methods
|
159
|
-
# ------------------
|
160
|
-
|
161
|
-
def display_name
|
162
|
-
"Stock Bundle - #{self.id} from #{supplier.try(:name)}"
|
163
|
-
end
|
164
|
-
|
165
|
-
def display_status
|
166
|
-
STATUS_REVERSE[self.status]
|
167
|
-
end
|
168
|
-
|
169
|
-
def display_file_name
|
170
|
-
if self.file && self.file.file
|
171
|
-
self.file.file.filename
|
172
|
-
else
|
173
|
-
self.display_name
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
def original_file_path
|
178
|
-
self.file && self.file.file ? self.file.file.path : nil
|
179
|
-
end
|
180
|
-
|
181
|
-
def original_error_file_path
|
182
|
-
self.error_file && self.error_file.file ? self.error_file.file.path : nil
|
183
|
-
end
|
184
|
-
|
185
|
-
# * Return true if the stock_entry is approved, else false.
|
186
|
-
# == Examples
|
187
|
-
# >>> stock_entry.pending?
|
188
|
-
# => true
|
189
|
-
def pending?
|
190
|
-
(status == PENDING)
|
191
|
-
end
|
192
|
-
|
193
|
-
# change the status of the stock_entry to :pending
|
194
|
-
# Return the status
|
195
|
-
# == Examples
|
196
|
-
# >>> stock_entry.mark_as_pending!
|
197
|
-
# => "pending"
|
198
|
-
def mark_as_pending!
|
199
|
-
self.update_attribute(:status, PENDING)
|
200
|
-
self.stock_entries.update_all(status: StockEntry::PENDING)
|
201
|
-
end
|
202
|
-
|
203
|
-
# * Return true if the stock_entry is approved, else false.
|
204
|
-
# == Examples
|
205
|
-
# >>> stock_entry.approved?
|
206
|
-
# => true
|
207
|
-
def approved?
|
208
|
-
(status == APPROVED)
|
209
|
-
end
|
210
|
-
|
211
|
-
# change the status to :approved
|
212
|
-
# Return the status
|
213
|
-
# == Examples
|
214
|
-
# >>> stock_entry.approve!
|
215
|
-
# => "approve"
|
216
|
-
def approve!
|
217
|
-
self.update_attribute(:status, APPROVED)
|
218
|
-
self.stock_entries.update_all(status: StockEntry::ACTIVE)
|
219
|
-
true
|
220
|
-
end
|
221
|
-
|
222
|
-
# * Return true if the stock_entry is errored, else false.
|
223
|
-
# == Examples
|
224
|
-
# >>> stock_entry.mark_as_errored?
|
225
|
-
# => true
|
226
|
-
def mark_as_errored?
|
227
|
-
(status == ERRORED)
|
228
|
-
end
|
229
|
-
|
230
|
-
# change the status to :errored
|
231
|
-
# Return the status
|
232
|
-
# == Examples
|
233
|
-
# >>> stock_entry.mark_as_errored!
|
234
|
-
# => "approve"
|
235
|
-
def mark_as_errored!
|
236
|
-
self.update_attribute(:status, ERRORED)
|
237
|
-
self.stock_entries.update_all(status: StockEntry::PENDING)
|
238
|
-
true
|
239
|
-
end
|
240
|
-
|
241
|
-
def can_be_deleted?
|
242
|
-
true
|
243
|
-
end
|
244
|
-
|
245
|
-
def can_be_edited?
|
246
|
-
true
|
247
|
-
end
|
248
|
-
|
249
|
-
end
|
data/app/models/stock_entry.rb
DELETED
@@ -1,275 +0,0 @@
|
|
1
|
-
class StockEntry < Vyapari::ApplicationRecord
|
2
|
-
|
3
|
-
# Constants
|
4
|
-
ACTIVE = "active"
|
5
|
-
SOLD = "sold"
|
6
|
-
DAMAGED = "damaged"
|
7
|
-
RECEIVED = "received"
|
8
|
-
RETURNED = "returned"
|
9
|
-
RESERVED = "reserved"
|
10
|
-
PENDING = "pending"
|
11
|
-
|
12
|
-
STATUS = {"Active" => ACTIVE, "Sold" => SOLD, "Damaged" => DAMAGED, "Received" => RECEIVED, "Reserved" => RESERVED, "Pending" => PENDING, "Returned" => RETURNED}
|
13
|
-
STATUS_REVERSE = {ACTIVE => "Active", SOLD => "Sold", DAMAGED => "Damaged", RECEIVED => "Received", RESERVED => "Reserved", PENDING => "Pending", RETURNED => "Returned"}
|
14
|
-
|
15
|
-
# Validations
|
16
|
-
validates :store, presence: true
|
17
|
-
validates :product, presence: true
|
18
|
-
validates :quantity, presence: true, numericality: true
|
19
|
-
validates :status, :presence=> true, :inclusion => {:in => STATUS_REVERSE.keys, :presence_of => :status, :message => "%{value} is not a valid status" }
|
20
|
-
|
21
|
-
# Associations
|
22
|
-
belongs_to :store
|
23
|
-
belongs_to :product
|
24
|
-
belongs_to :supplier, optional: true
|
25
|
-
belongs_to :stock_bundle, optional: true
|
26
|
-
belongs_to :invoice, optional: true
|
27
|
-
|
28
|
-
# ------------------
|
29
|
-
# Class Methods
|
30
|
-
# ------------------
|
31
|
-
|
32
|
-
# return an active record relation object with the search query in its where clause
|
33
|
-
# Return the ActiveRecord::Relation object
|
34
|
-
# == Examples
|
35
|
-
# >>> obj.search(query)
|
36
|
-
# => ActiveRecord::Relation object
|
37
|
-
scope :search, lambda {|query| joins("LEFT JOIN `stores` ON `stores`.`id` = `stock_entries`.`store_id` LEFT JOIN `products` ON `products`.`id` = `stock_entries`.`product_id` LEFT JOIN `suppliers` ON `suppliers`.`id` = `stock_entries`.`supplier_id`").where("LOWER(products.name) LIKE LOWER('%#{query}%') OR
|
38
|
-
LOWER(suppliers.name) LIKE LOWER('%#{query}%') OR
|
39
|
-
LOWER(products.ean_sku) LIKE LOWER('%#{query}%') OR
|
40
|
-
LOWER(products.one_liner) LIKE LOWER('%#{query}%') OR
|
41
|
-
LOWER(products.description) LIKE LOWER('%#{query}%') OR
|
42
|
-
LOWER(stores.name) LIKE LOWER('%#{query}%')")}
|
43
|
-
|
44
|
-
|
45
|
-
scope :active, -> { where(status: ACTIVE) }
|
46
|
-
scope :sold, -> { where(status: SOLD) }
|
47
|
-
scope :damaged, -> { where(status: DAMAGED) }
|
48
|
-
scope :returned, -> { where(status: RETURNED) }
|
49
|
-
scope :received, -> { where(status: RECEIVED) }
|
50
|
-
scope :reserved, -> { where(status: RESERVED) }
|
51
|
-
scope :in_stock, -> { where(status: [ACTIVE, RECEIVED, RETURNED]) }
|
52
|
-
|
53
|
-
scope :this_month, lambda { joins(:invoice).where("invoices.invoice_date >= ? AND invoices.invoice_date <= ?", Time.zone.now.beginning_of_month, Time.zone.now.end_of_month) }
|
54
|
-
scope :today, lambda { joins(:invoice).where('DATE(invoices.invoice_date) = ?', Date.current.in_time_zone)}
|
55
|
-
scope :dated, lambda { |d| joins(:invoice).where('DATE(invoices.invoice_date) = ?', d)}
|
56
|
-
|
57
|
-
def self.save_row_data(row)
|
58
|
-
|
59
|
-
row.headers.each{ |cell| row[cell] = row[cell].to_s.strip }
|
60
|
-
|
61
|
-
return if row[:ean_sku].blank?
|
62
|
-
|
63
|
-
# Initializing error hash for displaying all errors altogether
|
64
|
-
error_object = Kuppayam::Importer::ErrorHash.new
|
65
|
-
|
66
|
-
store = Store.find_by_code(row[:stock])
|
67
|
-
unless store
|
68
|
-
summary = "Store doesn't exist."
|
69
|
-
details = "Error! The store with code #{row[:store]} doesn't exists in the database"
|
70
|
-
error_object.errors << { summary: summary, details: details }
|
71
|
-
return
|
72
|
-
end
|
73
|
-
|
74
|
-
product = Product.find_by_ean_sku(row[:ean_sku]) || Product.new
|
75
|
-
|
76
|
-
product.name = row[:name]
|
77
|
-
product.code = row[:code]
|
78
|
-
|
79
|
-
if product.valid?
|
80
|
-
product.save!
|
81
|
-
else
|
82
|
-
summary = "Error while saving product: #{product.name}"
|
83
|
-
details = "Error! #{product.errors.full_messages.to_sentence}"
|
84
|
-
error_object.errors << { summary: summary, details: details }
|
85
|
-
end
|
86
|
-
|
87
|
-
|
88
|
-
stock_entry.store = store
|
89
|
-
stock_entry.product = product
|
90
|
-
stock_entry.quantity = row[:quantity]
|
91
|
-
|
92
|
-
stock_entry.set_stock_entry_type(row[:stock_entry_type])
|
93
|
-
|
94
|
-
stock_entry.region = Region.find_by_code(row[:region]) || Country.find_by_name(row[:country])
|
95
|
-
stock_entry.country = Country.find_by_code(row[:country]) || Country.find_by_name(row[:country])
|
96
|
-
|
97
|
-
if stock_entry.valid?
|
98
|
-
stock_entry.save!
|
99
|
-
else
|
100
|
-
summary = "Error while saving stock_entry: #{stock_entry.name}"
|
101
|
-
details = "Error! #{stock_entry.errors.full_messages.to_sentence}"
|
102
|
-
error_object.errors << { summary: summary, details: details }
|
103
|
-
end
|
104
|
-
return error_object
|
105
|
-
end
|
106
|
-
|
107
|
-
# ------------------
|
108
|
-
# Instance Methods
|
109
|
-
# ------------------
|
110
|
-
|
111
|
-
def display_name
|
112
|
-
"Stock from #{self.supplier.try(:name)}"
|
113
|
-
end
|
114
|
-
|
115
|
-
def display_store_type
|
116
|
-
STATUS[self.status]
|
117
|
-
end
|
118
|
-
|
119
|
-
def display_status
|
120
|
-
STATUS_REVERSE[self.status]
|
121
|
-
end
|
122
|
-
|
123
|
-
def slug
|
124
|
-
if self.product
|
125
|
-
self.product.name.parameterize
|
126
|
-
else
|
127
|
-
self.id
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
def to_param
|
132
|
-
"#{id}-#{slug}"
|
133
|
-
end
|
134
|
-
|
135
|
-
# change the status to :active
|
136
|
-
# Return the status
|
137
|
-
# == Examples
|
138
|
-
# >>> stock_entry.activate!
|
139
|
-
# => "active"
|
140
|
-
def activate!
|
141
|
-
self.update_attribute(:status, ACTIVE)
|
142
|
-
end
|
143
|
-
|
144
|
-
# change the status to :sold
|
145
|
-
# Return the status
|
146
|
-
# == Examples
|
147
|
-
# >>> stock_entry.sell!
|
148
|
-
# => "sold"
|
149
|
-
def sell!
|
150
|
-
self.update_attribute(:status, SOLD)
|
151
|
-
end
|
152
|
-
|
153
|
-
# change the status to :damaged
|
154
|
-
# Return the status
|
155
|
-
# == Examples
|
156
|
-
# >>> stock_entry.mark_as_damaged!
|
157
|
-
# => "damaged"
|
158
|
-
def mark_as_damaged!
|
159
|
-
self.update_attribute(:status, DAMAGED)
|
160
|
-
end
|
161
|
-
|
162
|
-
# change the status to :received
|
163
|
-
# Return the status
|
164
|
-
# == Examples
|
165
|
-
# >>> stock_entry.mark_as_received!
|
166
|
-
# => "received"
|
167
|
-
def mark_as_received!
|
168
|
-
self.update_attribute(:status, RECEIVED)
|
169
|
-
end
|
170
|
-
|
171
|
-
# change the status to :reserved
|
172
|
-
# Return the status
|
173
|
-
# == Examples
|
174
|
-
# >>> stock_entry.reserve!
|
175
|
-
# => "reserved"
|
176
|
-
def reserve!
|
177
|
-
self.update_attribute(:status, RESERVED)
|
178
|
-
end
|
179
|
-
|
180
|
-
# change the status to :returned
|
181
|
-
# Return the status
|
182
|
-
# == Examples
|
183
|
-
# >>> stock_entry.reserve!
|
184
|
-
# => "returned"
|
185
|
-
def return!
|
186
|
-
self.update_attribute(:status, RETURN)
|
187
|
-
end
|
188
|
-
|
189
|
-
# change the status to :pending
|
190
|
-
# Return the status
|
191
|
-
# == Examples
|
192
|
-
# >>> stock_entry.mark_as_pending!
|
193
|
-
# => "pending"
|
194
|
-
def mark_as_pending!
|
195
|
-
self.update_attribute(:status, PENDING)
|
196
|
-
end
|
197
|
-
|
198
|
-
# * Return true if the stock_entry is active, else false.
|
199
|
-
# == Examples
|
200
|
-
# >>> stock_entry.active?
|
201
|
-
# => true
|
202
|
-
def active?
|
203
|
-
(status == ACTIVE)
|
204
|
-
end
|
205
|
-
|
206
|
-
# * Return true if the stock_entry is sold, else false.
|
207
|
-
# == Examples
|
208
|
-
# >>> stock_entry.sold?
|
209
|
-
# => true
|
210
|
-
def sold?
|
211
|
-
(status == SOLD)
|
212
|
-
end
|
213
|
-
|
214
|
-
# * Return true if the stock_entry is damaged, else false.
|
215
|
-
# == Examples
|
216
|
-
# >>> stock_entry.damaged?
|
217
|
-
# => true
|
218
|
-
def damaged?
|
219
|
-
(status == DAMAGED)
|
220
|
-
end
|
221
|
-
|
222
|
-
# * Return true if the stock_entry is received, else false.
|
223
|
-
# == Examples
|
224
|
-
# >>> stock_entry.received?
|
225
|
-
# => true
|
226
|
-
def received?
|
227
|
-
(status == RECEIVED)
|
228
|
-
end
|
229
|
-
|
230
|
-
# * Return true if the stock_entry is returned, else false.
|
231
|
-
# == Examples
|
232
|
-
# >>> stock_entry.returned?
|
233
|
-
# => true
|
234
|
-
def returned?
|
235
|
-
(status == RETURNED)
|
236
|
-
end
|
237
|
-
|
238
|
-
# * Return true if the stock_entry is reserved, else false.
|
239
|
-
# == Examples
|
240
|
-
# >>> stock_entry.reserved?
|
241
|
-
# => true
|
242
|
-
def reserved?
|
243
|
-
(status == RESERVED)
|
244
|
-
end
|
245
|
-
|
246
|
-
# * Return true if the stock_entry is pending, else false.
|
247
|
-
# == Examples
|
248
|
-
# >>> stock_entry.pending?
|
249
|
-
# => true
|
250
|
-
def pending?
|
251
|
-
(status == PENDING)
|
252
|
-
end
|
253
|
-
|
254
|
-
def can_be_edited?
|
255
|
-
true
|
256
|
-
end
|
257
|
-
|
258
|
-
def can_be_deleted?
|
259
|
-
# if self.products.any?
|
260
|
-
# #self.errors.add(:base, DELETE_MESSAGE)
|
261
|
-
# return false
|
262
|
-
# else
|
263
|
-
# return true
|
264
|
-
# end
|
265
|
-
return true
|
266
|
-
end
|
267
|
-
|
268
|
-
def report_heading
|
269
|
-
rh = []
|
270
|
-
rh << self.company.try(:name) if self.company.name
|
271
|
-
rh << self.display_name
|
272
|
-
rh.join(", ")
|
273
|
-
end
|
274
|
-
|
275
|
-
end
|