suite import

This commit is contained in:
Nicolas Bally 2019-07-07 16:47:15 +02:00
parent f9238ea184
commit dd15688d60
13 changed files with 238 additions and 4 deletions

View File

@ -12,14 +12,16 @@ class Admin::PProductStocksController < ApplicationController
@file = "" @file = ""
@headers = [ @headers = [
"ID Sys", "ID produit Sys.",
"Ref produit", "Ref produit",
"Nom", "Nom",
"ID variante",
"Ref variante", "Ref variante",
"Taille", "Taille",
"Couleur", "Couleur",
"Stock Physique", "Stock Physique",
"Stock théorique (prenant en compte les commandes en cours)" "Stock theo",
"Stock const"
] ]
@ -34,6 +36,7 @@ class Admin::PProductStocksController < ApplicationController
line << p_product.code line << p_product.code
line << p_product.name line << p_product.name
line << pps.id
line << pps.code line << pps.code
line << (pps.p_color ? pps.p_color.name : "") line << (pps.p_color ? pps.p_color.name : "")
line << (pps.p_size ? pps.p_size.name : "") line << (pps.p_size ? pps.p_size.name : "")
@ -42,6 +45,8 @@ class Admin::PProductStocksController < ApplicationController
line << pps.stock_th_ok line << pps.stock_th_ok
line << ""
csv << line csv << line
end end
@ -78,7 +83,7 @@ class Admin::PProductStocksController < ApplicationController
end end
def import def import
if true if false
#@file = params[:csv_file] #@file = params[:csv_file]
@ -90,6 +95,20 @@ class Admin::PProductStocksController < ApplicationController
end end
def import_stocks
@file = params[:csv_file] if params[:csv_file]
if @file
csv_text = @file.tempfile.read #.force_encoding('ISO-8859-1').encode('utf-8')
@csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
else
end
end
def import_prix def import_prix
if false if false

View File

@ -0,0 +1,7 @@
class PImportStock < ActiveRecord::Base
has_many :p_import_stock_product_stocks
has_many :p_product_stocks, :through => :p_import_stock_product_stocks
end

View File

@ -0,0 +1,4 @@
class PImportStockProductStock < ActiveRecord::Base
belongs_to :p_import_stock
belongs_to :p_product_stock
end

View File

@ -0,0 +1,112 @@
.qi_header
%h1
Achats
%span
Import stocks
.qi_row
.qi_pannel.qi_plain.padding
-if !@csv and !params[:import_ok]
=form_tag import_stocks_admin_p_product_stocks_path(), multipart: true do
=file_field_tag :csv_file
%br
%br
=submit_tag "Vérifier et importer", :class => "btn btn-primary"
-if @csv
=form_tag import_stocks_admin_p_product_stocks_path(), multipart: true do
=hidden_field_tag "import_ok", true
%table.table.table-striped
%tr
%th
%th Réf variante
%th Id variante
%th Méthode ident.
%th Nom produit
%th Taille
%th Couleur
%th Stock actuel
%th Stock à importer
-@csv.each do |row|
-ps = nil
-if row["Stock const"].to_s != ""
-if row["Ref variante"].to_s != "" and PProductStock.where(:code => row["Ref variante"]).count == 1
-m = "Par réf."
-ps = PProductStock.where(:code => row["Ref variante"]).first
-elsif row["ID variante"].to_s != "" and PProductStock.where(:id => row["ID variante"]).count == 1
-m = "Par id"
-ps = PProductStock.where(:id => row["ID variante"]).first
-else
-m = "Produit introuvable"
%tr
%td
=check_box_tag :"ids[]", ps.id, true if ps
=hidden_field_tag "ps_#{ps.id}", row["Stock const"]
%td=row["Ref variante"]
%td=row["ID variante"]
%td=m
-if ps
%td=ps.p_product.name
%td=ps.p_size.name if ps.p_size
%td=ps.p_color.name if ps.p_color
%td=ps.stock_ok
-else
%td
%td
%td
%td=row["Stock const"]
=submit_tag "Valider et importer", :class => "btn btn-primary"
-if params[:import_ok]
%h2 Import réalisé
-p_import_stock = PImportStock.create()
%table.table.table-striped
%tr
%th Réf variante
%th Id variante
%th Nom produit
%th Taille
%th Couleur
%th Stock actuel
%th Stock importé
-params[:ids].each do |id|
-ps = PProductStock.where(:id => id.to_i).first
-p_import_stock.p_import_stock_product_stocks << PImportStockProductStock.new(:stock => params[:"ps_#{ps.id}"], :past_stock => ps.stock_ok, :p_product_stock => ps)
%tr
%td=ps.code
%td=ps.id
%td=ps.p_product.name
%td=ps.p_size.name if ps.p_size
%td=ps.p_color.name if ps.p_color
%td=ps.stock_ok
%td=params[:"ps_#{ps.id}"]
-ps.stock_ok = params[:"ps_#{ps.id}"]
-ps.save
%br
%br
=link_to "Retour au résumé stock", admin_p_product_stocks_path, :class => "btn btn-default"

View File

@ -9,6 +9,7 @@
.qi_row .qi_row
.qi_pannel.qi_plain.padding .qi_pannel.qi_plain.padding
=link_to "Export stocks", export_admin_p_product_stocks_path, :class => "btn btn-default" =link_to "Export stocks", export_admin_p_product_stocks_path, :class => "btn btn-default"
=link_to "Import stocks", import_stocks_admin_p_product_stocks_path, :class => "btn btn-default"
%br %br
%br %br

View File

@ -135,6 +135,8 @@ Rails.application.routes.draw do
get :export get :export
get :import get :import
get :import_prix get :import_prix
get :import_stocks
post :import_stocks
end end
end end

View File

@ -0,0 +1,9 @@
class CreatePImportStocks < ActiveRecord::Migration
def change
create_table :p_import_stocks do |t|
t.string :name
t.timestamps null: false
end
end
end

View File

@ -0,0 +1,16 @@
class CreatePImportStockProductStocks < ActiveRecord::Migration
def change
create_table :p_import_stock_product_stocks do |t|
t.boolean :ignored, :default => false
t.references :p_import_stock, index: true, foreign_key: true
t.references :p_product_stock, index: true, foreign_key: true
t.decimal :stock, :precision => 12, :scale => 2
t.decimal :stock_th, :precision => 12, :scale => 2
t.decimal :past_stock, :precision => 12, :scale => 2
t.decimal :past_stock_th, :precision => 12, :scale => 2
t.text :csv_line
t.timestamps null: false
end
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190627165947) do ActiveRecord::Schema.define(version: 20190704151954) do
create_table "admin_admin_roles", force: :cascade do |t| create_table "admin_admin_roles", force: :cascade do |t|
t.integer "admin_id", limit: 4 t.integer "admin_id", limit: 4
@ -997,6 +997,28 @@ ActiveRecord::Schema.define(version: 20190627165947) do
t.string "tel", limit: 255 t.string "tel", limit: 255
end end
create_table "p_import_stock_product_stocks", force: :cascade do |t|
t.boolean "ignored", default: false
t.integer "p_import_stock_id", limit: 4
t.integer "p_product_stock_id", limit: 4
t.decimal "stock", precision: 12, scale: 2
t.decimal "stock_th", precision: 12, scale: 2
t.decimal "past_stock", precision: 12, scale: 2
t.decimal "past_stock_th", precision: 12, scale: 2
t.text "csv_line", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "p_import_stock_product_stocks", ["p_import_stock_id"], name: "index_p_import_stock_product_stocks_on_p_import_stock_id", using: :btree
add_index "p_import_stock_product_stocks", ["p_product_stock_id"], name: "index_p_import_stock_product_stocks_on_p_product_stock_id", using: :btree
create_table "p_import_stocks", force: :cascade do |t|
t.string "name", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "p_origines", force: :cascade do |t| create_table "p_origines", force: :cascade do |t|
t.string "name", limit: 255 t.string "name", limit: 255
t.datetime "created_at", null: false t.datetime "created_at", null: false
@ -1634,6 +1656,8 @@ ActiveRecord::Schema.define(version: 20190627165947) do
add_foreign_key "p_customers", "p_price_cats" add_foreign_key "p_customers", "p_price_cats"
add_foreign_key "p_degressifs", "p_price_cats" add_foreign_key "p_degressifs", "p_price_cats"
add_foreign_key "p_degressifs", "p_products" add_foreign_key "p_degressifs", "p_products"
add_foreign_key "p_import_stock_product_stocks", "p_import_stocks"
add_foreign_key "p_import_stock_product_stocks", "p_product_stocks"
add_foreign_key "p_price_cat_p_degressifs", "p_degressifs" add_foreign_key "p_price_cat_p_degressifs", "p_degressifs"
add_foreign_key "p_price_cat_p_degressifs", "p_price_cats" add_foreign_key "p_price_cat_p_degressifs", "p_price_cats"
add_foreign_key "p_product_certifs", "p_certifs" add_foreign_key "p_product_certifs", "p_certifs"

View File

@ -0,0 +1,19 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
ignored: false
p_import_stock_id:
p_product_stock_id:
stock: 9.99
stock_th: 9.99
past_stock: 9.99
past_stock_th: 9.99
two:
ignored: false
p_import_stock_id:
p_product_stock_id:
stock: 9.99
stock_th: 9.99
past_stock: 9.99
past_stock_th: 9.99

7
test/fixtures/p_import_stocks.yml vendored Normal file
View File

@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
two:
name: MyString

View File

@ -0,0 +1,7 @@
require 'test_helper'
class PImportStockProductStockTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class PImportStockTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end