Commit ab27e7eb authored by Jean-Noël Rouchon's avatar Jean-Noël Rouchon

Ajout d'une fonction d'import d'article depuis un fichier csv

parent bb214c00
......@@ -49,6 +49,8 @@ gem "bootstrap-switch-rails"
gem "chartkick"
gem 'groupdate'
gem "smarter_csv"
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
......
......@@ -179,6 +179,7 @@ GEM
rdoc (~> 4.0)
simple_markdown (0.3.3)
rails (>= 4.2.0)
smarter_csv (1.1.5)
spring (2.0.1)
activesupport (>= 4.2)
sprockets (3.5.2)
......@@ -240,6 +241,7 @@ DEPENDENCIES
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
simple_markdown
smarter_csv
spring
sqlite3
therubyracer
......@@ -249,4 +251,4 @@ DEPENDENCIES
web-console (~> 2.0)
BUNDLED WITH
1.14.3
1.16.1
module ArticlesHelper
def import_articles supplier_name
fournisseur = Customer.where(name: supplier_name).first
lignes = SmarterCSV.process(Rails.root.join('tmp','import_articles.csv'), {:col_sep => ";"})
categories = []
articles = []
category_id = nil
tva = nil
lignes.each do |ligne|
if ligne[:prix].nil? and ligne[:la_mare].nil? and !ligne[:référence].include?("CONCEPT ONE PRODUCTION")
category = ligne[:référence].gsub("(TVA)","").gsub("(NP)","").rstrip
tva = ligne[:référence].include?("(TVA)") ? 1 : 2
categories << {title: category, vat: tva}
c = Category.where(title: category).first
if c
category_id = c.id
else
c = Category.create(title: category, purchase_vat_id: tva, sale_vat_id: tva)
category_id = c.id
end
else
if ligne[:prix] and ligne[:désignation] and ligne[:référence]
prix = ligne[:prix].gsub("€","").to_f
stock1 = ligne[:la_mare].to_i
stock2 = ligne[:saint_louis].to_i
stock3 = ligne[:saint_pierre].to_i
stock_total = stock1 + stock2 + stock3
a = Article.where(ref: ligne[:référence]).first
if prix>0.0 and stock_total>0
a = Article.new unless a
a.title = ligne[:désignation]
a.ref = ligne[:référence]
a.purchase_price = prix
a.cost_price = prix
a.public_price = (prix*1.3).round(2)
a.category_id = category_id
a.supplier_id = fournisseur.id
a.warranty = 24
a.can_sell = true
a.can_purchase = true
a.supplier_stock = stock_total
a.purchase_vat_id = tva
a.sale_vat_id = tva
a.save
elsif a
a.destroy
end
end
end
end
return 1
end
end
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