lockaz_app/app/models/p_customer_sheet.rb
2020-11-09 15:38:09 +01:00

287 lines
6.7 KiB
Ruby

class PCustomerSheet < ApplicationRecord
has_many :p_documents, :as => :element
belongs_to :p_customer
belongs_to :p_payment_type
validates :p_customer_id, :presence => true
belongs_to :p_commercial
belongs_to :p_customer
belongs_to :particular_bill, :class_name => "Particular"#, :dependent => :destroy
belongs_to :particular_send, :class_name => "Particular"#, :dependent => :destroy
has_one :price_line_block, :as => :price_lineable
accepts_nested_attributes_for :price_line_block
has_many :price_documents, :as => :ref_element
acts_as_sorting :fields => {
:p_commercial => {:name => "Commercial", :reorder => false},
:created_at => {:name => "Date", :reorder => true, :sort_name => "p_customer_sheets.created_at"},
:admin_id => {:name => "Déposée par", :reorder => true},
:demande_type => {:name => "Type de demande", :reorder => true},
:p_customer => {:name => "Client", :reorder => false},
:tot_amount_ht => {:name => "Total HT", :reorder => true, :sort_name => "cc_tot_amount_ht", :sort_name => "p_customer_sheets.cc_tot_amount_ht"},
:tot_amount_ttc => {:name => "Total TTC", :reorder => true, :sort_name => "cc_tot_amount_ttc", :sort_name => "p_customer_sheets.cc_tot_amount_ttc"},
:state => {:name => "Statut", :reorder => false},
:actions => {:name => "Actions", :reorder => false},
}
QI_DYNAMICS = %w(accounting_zone_id accounting_zone_name tot_amount_ht tot_amount_ttc tot_amount_tva payment_days payment_delais payment_month_end payment_end_at )
eval(QI_DYNAMICS_CORE)
def personalised_archive
self.price_line_block.archive_now
end
def personalised_unarchive
self.price_line_block.unarchive_now
end
def self.valid_sort
["created_at", "price_line_blocks.wish_date"]
end
before_validation do
if self.price_line_block
self.p_customer = self.price_line_block.p_customer
self.p_commercial_id = self.price_line_block.p_commercial_id
self.particular_bill_id = self.price_line_block.particular_bill_id
self.particular_send_id = self.price_line_block.particular_send_id
self.ref_particular_bill_id = self.price_line_block.ref_particular_bill_id
self.ref_particular_send_id = self.price_line_block.ref_particular_send_id
end
end
def ca_accounting_zone_id
self.p_customer.accounting_zone.id if self.p_customer and self.p_customer.accounting_zone
end
def ca_accounting_zone_name
self.p_customer.accounting_zone.name if self.p_customer and self.p_customer.accounting_zone
end
def ca_tot_amount_ht
self.price_line_block.tot_amount_ht
end
def ca_tot_amount_ttc
self.price_line_block.tot_amount_ttc
end
def ca_tot_amount_tva
self.price_line_block.tot_amount_tva
end
def ca_label
end
def ca_header
end
def ca_footer
end
def ca_payment_days
end
def ca_payment_delais
end
def ca_payment_month_end
end
def ca_payment_end_at
end
def ca_particular_bill_id
end
def ca_particular_send_id
end
def ca_to_paid_ht
end
def ca_to_paid_ttc
end
def ca_to_paid_tva
end
def oldvalidation
#Validation des paiements
if false
if self.comptant
if self.acompte
errors.add(:acompte, "N'est pas compatible avec les paiements comptants")
end
if self.acompte_percent?
errors.add(:acompte_percent, "N'est pas compatible avec les paiements comptants")
end
if self.payment_delais?
errors.add(:payment_delais, "N'est pas compatible avec les paiements comptants")
end
if self.payment_fin_de_mois
errors.add(:payment_fin_de_mois, "N'est pas compatible avec les paiements comptants")
end
end
if self.acompte
if !self.acompte_percent?
errors.add(:acompte_percent, "Doit être remplis pour demander un acompte")
end
end
if !self.comptant
if !self.payment_delais?
errors.add(:payment_delais, "Délais de paiement nécessaire si pas de paiement comptant")
end
end
end
### fin de validation des paiements
end
def state_html
if self.state == "brouillon"
'<span class="badge badge-pill badge-warning">Demande de commande</span>'
elsif self.state == "offre"
'<span class="badge badge-warning">Offre</span>'
elsif self.state == "commande"
'<span class="badge badge-success">Commande</span>'
elsif self.state == "livraison-en-cours"
'<span class="badge badge-success" style="background:#036516;">Livr. en cours</span>'
elsif self.state == "livrée"
'<span class="badge badge-info">livrée</span>'
elsif self.state =="facturée"
'<span class="badge badge-primary">Facturée</span>'
elsif self.state == "annulée"
'<span class="badge badge-dark">Annulée</span>'
elsif self.state == "refusée"
'<span class="badge badge-danger">Refusée</span>'
elsif self.state == "remboursée"
'<span class="badge badge-danger" style="background:#865F7C;">Remboursée</span>'
end
end
after_create do
self.state = "brouillon"
self.save
end
def generate_doc(label, state)
#self.archive_now if !self.archived
price_document = self.price_documents.new(:price_document_type => PriceDocumentType.find_by_label(label), :date => Date.today)
price_document.p_customer = self.p_customer
price_document.price_line_block = self.price_line_block.dup
self.price_line_block.price_lines.each do |pl|
price_document.price_line_block.price_lines << pl.dup
end
if price_document.save
#price_document.archive_now
self.state = state
self.save
end
end
def generate_d
generate_doc("Devis", "offre")
end
def generate_fp
if self.p_documents.create(:p_document_type => PDocumentType.find_by_label("Facture proforma"))
self.state = "offre"
self.save
end
end
def generate_bc
generate_doc("Bon de commande", "commande")
end
def generate_c
generate_doc("Contrat", "commande")
end
def generate_bl()
generate_doc("Bon de livraison", "en livraison")
end
def generate_f()
generate_doc("Facture", "facturée")
end
def document_date(document=nil)
self.created_at
end
after_save do
self.price_line_block.save
end
STATES = ["brouillon", "offre", "commande", "livrée","facturée", "annulée", "refusée"]
end