Class: PricesController
- Inherits:
-
AdminController
- Object
- ActionController::Base
- ApplicationController
- BaseController
- AdminController
- PricesController
- Defined in:
- app/controllers/prices_controller.rb
Overview
License
Ekylibre - Simple ERP Copyright (C) 2008-2011 Brice Texier, Thibaud Merigon
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Instance Attribute Summary
Attributes inherited from AdminController
Instance Method Summary (collapse)
- - (Object) create
- - (Object) destroy
- - (Object) edit
- - (Object) export
- - (Object) find
- - (Object) import
-
- (Object) index
Displays the main page with the list of prices.
- - (Object) new
- - (Object) update
Methods inherited from AdminController
Methods inherited from BaseController
#has_notifications?, #human_action_name, human_action_name, #notify, #notify_error, #notify_error_now, #notify_now, #notify_success, #notify_success_now, #notify_warning, #notify_warning_now
Instance Method Details
- (Object) create
41 42 43 44 45 46 47 |
# File 'app/controllers/prices_controller.rb', line 41 def create @mode = (params[:mode]||"sales").to_sym @price = Price.new(params[:price]) @price.entity_id = params[:price][:entity_id]||Entity.of_company.id return if save_and_redirect(@price) render_restfully_form end |
- (Object) destroy
49 50 51 52 53 |
# File 'app/controllers/prices_controller.rb', line 49 def destroy return unless @price = find_and_check @price.destroy redirect_to_current end |
- (Object) edit
75 76 77 78 79 80 |
# File 'app/controllers/prices_controller.rb', line 75 def edit return unless @price = find_and_check @mode = "purchases" if @price.entity_id != Entity.of_company.id t3e @price.attributes, :product=>@price.product.name render_restfully_form end |
- (Object) export
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 |
# File 'app/controllers/prices_controller.rb', line 108 def export @products = Product.availables @entity_categories = EntityCategory csv = ["",""] csv2 = ["Code Produit", "Nom"] @entity_categories.each do |category| csv += [category.code, category.name, ""] csv2 += ["HT","TTC","TVA"] end csv_string = Ekylibre::CSV.generate_line(csv) csv_string += Ekylibre::CSV.generate_line(csv2) csv_string += Ekylibre::CSV.generate do |csv| @products.each do |product| line = [] line << [product.code, product.name] @entity_categories.each do |category| price = Price.find(:first, :conditions=>{:active=>true, :product_id=>product.id, :category_id=>EntityCategory.find_by_code(category.code).id}) #raise Exception.new price.inspect if price.nil? line << ["","",""] else line << [price.pretax_amount.to_s.gsub(/\./,","), price.amount.to_s.gsub(/\./,","), price.tax.amount] end end csv << line.flatten end end send_data csv_string, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=Tarifs.csv" end |
- (Object) find
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/controllers/prices_controller.rb', line 55 def find if params[:product_id] and params[:entity_id] return unless product = find_and_check(:product, params[:product_id]) return unless entity = find_and_check(:entity, params[:entity_id]) @price = product.prices.find(:first, :conditions=>{:entity_id=>entity.id, :active=>true}, :order=>"by_default DESC") @price ||= Price.new(:category_id=>entity.category_id) respond_to do |format| format.html { render :partial=>"amount_form" } format.json { render :json=>@price.to_json } format.xml { render :xml=>@price.to_xml } end elsif !params[:purchase_line_price_id].blank? return unless @price = find_and_check(:price, params[:purchase_line_price_id]) @product = @price.product if @price elsif params[:purchase_line_product_id] return unless @product = find_and_check(:product, params[:purchase_line_product_id]) @price = @product.prices.find_by_active_and_by_default_and_entity_id(true, true, params[:entity_id]||Entity.of_company.id) if @product end end |
- (Object) import
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'app/controllers/prices_controller.rb', line 147 def import if request.post? if params[:csv_file].nil? notify_warning(:you_must_select_a_file_to_import) redirect_to :action=>:import else file = params[:csv_file][:path] name = "MES_TARIFS.csv" @entity_categories = [] @available_prices = [] @unavailable_prices = [] i = 0 File.open("#{Rails.root.to_s}/#{name}", "w") { |f| f.write(file.read)} Ekylibre::CSV.foreach("#{Rails.root.to_s}/#{name}") do |row| if i == 0 x = 2 while !row[x].nil? entity_category = EntityCategory.find_by_code(row[x]) entity_category = EntityCategory.create!(:code=>row[x], :name=>row[x+1]) if entity_category.nil? @entity_categories << entity_category x += 3 end end if i > 1 puts i.to_s+"hhhhhhhhhhhhhhh" x = 2 @product = Product.find_by_code(row[0]) for category in @entity_categories blank = true tax = Tax.find(:first, :conditions=>{:amount=>row[x+2].to_s.gsub(/\,/,".").to_f}) tax_id = tax.nil? ? nil : tax.id @price = Price.find(:first, :conditions=>{:product_id=>@product.id, :category_id=>category.id, :active=>true} ) if @price.nil? and (!row[x].nil? or !row[x+1].nil? or !row[x+2].nil?) @price = Price.new(:pretax_amount=>row[x].to_s.gsub(/\,/,".").to_f, :tax_id=>tax_id, :amount=>row[x+1].to_s.gsub(/\,/,".").to_f, :product_id=>@product.id, :category_id=>category.id, :entity_id=>Entity.of_company.id, :currency=>Entity.of_company.currency) blank = false elsif !@price.nil? blank = false @price.pretax_amount = row[x].to_s.gsub(/\,/,".").to_f @price.amount = row[x+1].to_s.gsub(/\,/,".").to_f @price.tax_id = tax_id end if blank == false if @price.valid? @available_prices << @price else @unavailable_prices << [i+1, @price.errors.] end end x += 3 end end for price in @available_prices puts price.inspect+" id" if price.id.nil? if i==14 end #raise Exception.new @unavailable_prices.inspect if i == 12 i += 1 end if @unavailable_prices.empty? for price in @available_prices if price.id.nil? puts price.inspect Price.create!(price.attributes) else price.update_attributes(price.attributes) end notify_now(:prices_import_succeeded) end end end end end |
- (Object) index
Displays the main page with the list of prices
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/controllers/prices_controller.rb', line 92 def index @modes = ['all', 'clients', 'suppliers'] @suppliers = Entity.where(:supplier=>true) session[:entity_id] = 0 if request.post? mode = params[:price][:mode] if mode == "suppliers" session[:entity_id] = params[:price][:supply].to_i elsif mode == "clients" session[:entity_id] = Entity.of_company.id else session[:entity_id] = 0 end end end |
- (Object) new
34 35 36 37 38 39 |
# File 'app/controllers/prices_controller.rb', line 34 def new @mode = (params[:mode]||"sales").to_sym @price = Price.new(:product_id=>params[:product_id], :currency=>params[:currency]||Entity.of_company.currency, :category_id=>params[:entity_category_id]||session[:current_entity_category_id]||0) @price.entity_id = params[:entity_id] if params[:entity_id] render_restfully_form end |
- (Object) update
82 83 84 85 86 87 88 89 |
# File 'app/controllers/prices_controller.rb', line 82 def update return unless @price = find_and_check @mode = "purchases" if @price.entity_id != Entity.of_company.id @price.amount = 0 return if save_and_redirect(@price, :attributes=>params[:price]) t3e @price.attributes, :product=>@price.product.name render_restfully_form end |