-
-
Save patrickespake/91c59636d967281eaca12c441b1d8f57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :calc_ceps do | |
desc "Calcular o redespacho dos correios do cliente específico" | |
task all_ceps: :environment do | |
customer = Customer.find_by(api_key:"c0414dadf7d5db207f1188e8e6c53ea3302dc1bb") | |
current_admin_user = AdminUser.find_by(email:"[email protected]") | |
##binding.pry | |
book = Spreadsheet::Workbook.new | |
# Define the sheet name | |
@product = Product.find_by_name("PAC") | |
##binding.pry | |
sheet = book.create_worksheet name: @product.name | |
#IntegrationMailer.mail_default_variables(current_admin_user, customer, product) | |
##binding.pry | |
ceps_redespacho = { | |
# "SPCapital" => "6000000 a 9999999", "SPInterior" => "12000000 a 19999999", | |
# "RJCapital"=> " 20000000 a 23799999" , "RJInterior"=> "26601000 a 28999999", | |
"ESCapital "=> "29000000 a 29099999", "ESInterior" => "29100000 a 29999999", | |
"BACapital" => "40000000 a 44470999", "BAInterior" => "44471000 a 48999999", | |
"MGCapital" => "30000000 a 34999999", "MGInterior"=> "35000000 a 39999999", | |
"SECapital" => "49000000 a 49099999", "SEInterior"=> "49100000 a 49999999", | |
"PEInterior"=> "55000000 a 56999999", "PECapital" => "50000000 a 54999999", | |
"PBInterior" => "58100000 a 58999999", "PBCapital" => "58000000 a 58099999", | |
"ALCapital" => "57000000 a 57099999","ALInterior" => "57100000 a 57999999", | |
"RNCapital" => "59000000 a 59099999", "RNInterior" => "59100000 a 59999999", | |
"PICapital" => "64000000 a 64099999", "PIInterior" => "64100000 a 64999999", | |
"PACapital" => "66000000 a 67999999", "PAInterior" => "68000000 a 68899999", | |
"CECapital" => "60000000 a 61900999","CEInterior" => "61901000 a 63999999", | |
"MACapital" => "65000000 a 65099999","MA Interior" => "65100000 a 65999999", | |
"APCapital" => "68900000 a 68914999","APInterior" => "68915000 a 68999999", | |
"AMCapital" => "69000000 a 69099999", "AMInterior" => "69100000 a 69299999", | |
"ACCapital" => "69900000 a 69920999", "ACInterior" => "69921000 a 69999999", | |
"GOCapital" => "72800000 a 74894999", "GOInterior" => "74895000 a 76799999", | |
"RRCapital" => "69300000 a 69339999","RRInterior" => "69340000 a 69389999", | |
"DFCapital" => "70000000 a 70999999", "DFInterior" => "71000000 a 73699999", | |
"TOCapital" => "77000000 a 77270999","TOInterior" => "77300000 a 77995999", | |
"MTCapital" => "78000000 a 78109999", "MTInterior" => "78110000 a 78899999", | |
"ROCapital" => "78900000 a 78930999","ROInterior=>" => "78931000 a 78999999", | |
"MSCapital" => "79000000 a 79129999", "MSInterior" => "79130000 a 79999999", | |
"PRCapital" => "80000000 a 83800999","PRInterior" => "83801000 a 87999999", | |
"SCCapital" => "88000000 a 88469999","SCInterior" => "88470000 a 89999999", | |
"RSCapital" => "90000000 a 94900999","RSInterior" => "94901000 a 99999999" } | |
# Define the sheet columns | |
###binding.pry | |
sheet.row(0).replace ["País", "Estado", "Cidade", "CEPinicial", "CEPfinal", "Peso Inicial", "Peso Final", " Mensagem de Prazo","Valor","Prazo inicial(dias)", "Prazo final(dias)"] | |
# Initialize variables | |
@cliente_chave = "c0414dadf7d5db207f1188e8e6c53ea3302dc1bb" | |
def calculate_price(weight, zipcode_start ) | |
response = Calculation.calculate_cep!(zipcode_start, weight, @cliente_chave) | |
if response[:products].count < 3 | |
binding.pry | |
end | |
name = response[:products].find{|v| v.keys[0].include?("PAC") }.try(:first).try(:first) | |
@product = Product.find_by(name: name) | |
price = response[:products].find{|v| v.keys[0].include?(name) }.try(:first).try(:last).try(:first)[1] rescue nil | |
end | |
current_row = 1 | |
# Create the sheet data | |
weight = 500 | |
old_weight = 0 | |
ceps_redespacho.each do |cidade , cep | | |
# Adjusts necessary data | |
##binding.pry | |
zipcode_start = cep.split("a").first.to_i | |
zipcode_end = cep.split("a").last.to_i | |
zipcode_start = "0#{zipcode_start}" if zipcode_start.to_s.size == 7 | |
zipcode_end = "0#{zipcode_end}" if zipcode_end.to_s.size == 7 | |
weight_limit = @product.weight_limit | |
old_weight = 0 | |
weight = 1000 | |
while weight <= weight_limit | |
# Add row data | |
binding.pry | |
sheet.row(current_row).replace([ | |
"BRA",# País | |
cidade[0..1], | |
cidade, | |
zipcode_start, #Cep inicial | |
zipcode_end, #Cep final | |
old_weight, #peso inicial | |
weight, #peso final | |
"até 7 dias úteis, após a postagem.", | |
price = calculate_price(weight,zipcode_start), | |
"#{@product.deadline_min.to_s}", | |
"#{@product.deadline_max.to_s}" # | |
]) | |
current_row = current_row + 1 | |
if current_row == 40 || current_row == 400 | |
binding.pry | |
end | |
old_weight = weight | |
weight += 1000 | |
end | |
end | |
# Create file | |
file_path = "#{Rails.root}/tmp/#{Time.now.to_s}.xls" | |
book.write file_path | |
puts "End calc:all_ceps" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment