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 QI_DYNAMICS = %w(com_counter 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.qi_table_order { :com_counter => {:name => "ID", :reorder => false}, :d_number => {:name => "N° BC", :reorder => false}, :p_commercial => {:name => "Commercial", :reorder => false}, :created_at => {:name => "Date", :reorder => true}, :wish_date => {:name => "Semaine de livraison", :reorder => true, :sort_name => "price_line_blocks.wish_date"}, :wish_date_2 => {:name => "Date de livraison souhaitée ", :reorder => true, :sort_name => "price_line_blocks.wish_date"}, :p_customer => {:name => "Client", :reorder => false}, :tot_amount_ht => {:name => "Total HT", :reorder => true, :sort_name => "cc_tot_amount_ht"}, :tot_amount_ttc => {:name => "Total TTC", :reorder => true, :sort_name => "cc_tot_amount_ttc"}, :state => {:name => "Statut", :reorder => false}, :actions => {:name => "Actions", :reorder => false}, } #, :sort_name => "code" end def self.valid_sort r = [] self.qi_table_order.each do |key, value| if value.instance_of? Hash if value[:reorder] == false elsif value[:reorder] == true if value[:sort_name] r << value[:sort_name] else r << key.to_s end end end end return r end #["created_at", "price_line_blocks.wish_date", "cc_tot_amount_ht","cc_tot_amount_ttc"] 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 if !self.com_counter_index if last = PCustomerSheet.where(:p_commercial_id => self.p_commercial_id).order("com_counter_index DESC, id DESC").first and last_index = last.com_counter_index index = last_index.to_i + 1 else index = 1 end self.com_counter_index = index end end def ca_com_counter if self.p_commercial self.p_commercial.code.to_s.gsub(/\d+/,"")+(self.created_at ? self.created_at.strftime("%y") : Date.today.strftime("%y")).to_s+('%04d' % self.com_counter_index) else "" 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" ''+self.demande_type.to_s+'' elsif self.state == "offre" 'Offre' elsif self.state == "commande" 'Commande' elsif self.state == "livraison-en-cours" 'Livr. en cours' elsif self.state == "livrée" 'livrée' elsif self.state =="facturée" 'Facturée' elsif self.state == "annulée" 'Annulée' elsif self.state == "refusée" 'Refusée' elsif self.state == "remboursée" 'Remboursée' 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_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