payre3p_app/app/models/contact.rb
Nicolas Bally d4c5bb38ab Suite
2022-03-19 11:13:03 +01:00

160 lines
3.7 KiB
Ruby

class Contact < ActiveRecord::Base
belongs_to :admin
has_many :contact_actions
validates :name, :presence => true
#validates :email, :presence => true, :if => :email_needed?
validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :if => :email_needed?
#validates :message, :presence => true
#validates :phone, :presence => true
has_many :contact_files
validates :address, :presence => true, :if => :address_needed?
validates :cp, :presence => true, :if => :address_needed?
validates :city, :presence => true, :if => :address_needed?
has_many :contact_products
has_many :d_products, :through => :contact_products
accepts_nested_attributes_for :contact_products
def devis_content
r = ""
if self.contact_products.count > 0
r+= "<table class='table'>"
r+= "<tr>"
r+= "
<td></td>
<td>Code</td>
<td>Produit</td>
<td>Conditionnement</td>
<td>Quantité</td>
<td>Unité</td>
"
r+= "</tr>"
self.contact_products.all.each do |cp|
r+= "<tr>"
r+= "<td>"
r+= ActionController::Base.helpers.image_tag("https://payreenergies.fr/"+cp.d_product.icon.file.url, :style => "height:80px;").to_s
r+= "</td>"
r+= "<td>"
r+=cp.code.to_s
r+= "</td>"
r+= "<td>"
r+=cp.d_product.name.to_s
r+= "</td>"
r+= "<td>"
r+=cp.ship_type.to_s
r+= "</td>"
r+= "<td>"
r+=cp.qte.to_s
r+= "</td>"
r+= "<td>"
r+=cp.d_product.unit.to_s
r+= "</td>"
r+= "</tr>"
end
r+= "</table>"
end
return r
end
def email_needed?
true if self.email?
end
def address_needed?
true if self.raison_id == 4
end
before_validation do
if (true or ((raison_id == 4 )) and !self.phone?)
#errors.add(:email, "Vous devez indiquer votre numéro de téléphone et/ou votre email")
errors.add(:phone, "Vous devez indiquer votre numéro de téléphone")
end
self.contact_status = "En cours" if !self.contact_status?
end
def syncro
require 'net/https'
require 'open-uri'
@params = {
:name => self.name,
:firstname => self.firstname,
:corporate => self.corporate,
:address => self.address,
:address2 => self.address2,
:cp => self.cp,
:city => self.city,
:email => self.email,
:phone => self.phone,
:message => self.message,
:provenance_id => self.provenance_id,
:devis_content => self.devis_content
}
if Rails.env.development?
api_url ="http://localhost:3030/admin/contacts/api"
else
api_url = "https://archivespayre.quartz.xyz/admin/contacts/api"
end
@c = Curl::Easy.new(api_url) do |curl|
curl.verbose = true
end
@c.http_post(
Curl::PostField.content(:name, @params[:name]),
Curl::PostField.content(:firstname, @params[:firstname]),
Curl::PostField.content(:corporate, @params[:corporate]),
Curl::PostField.content(:address, @params[:address]),
Curl::PostField.content(:address2, @params[:address2]),
Curl::PostField.content(:cp, @params[:cp]),
Curl::PostField.content(:city, @params[:city]),
Curl::PostField.content(:email, @params[:email]),
Curl::PostField.content(:phone, @params[:phone]),
Curl::PostField.content(:message, @params[:message]),
Curl::PostField.content(:provenance_id, @params[:provenance_id]),
Curl::PostField.content(:devis_content, @params[:devis_content])
)
@debug = @c.body_str
end
end