module Logistics module Core class ClientContract < ApplicationRecord belongs_to :offer_request has_and_belongs_to_many :contract_article_cpies, :join_table => 'client_contract_articles' validates :offer_request, :contract_date, presence: true enum status:[:draft,:submitted,:approved,:rejected,:accepted,:declined,:active] validates :effective_date,presence: true,allow_blank: false validates :expiry_date,presence: true,allow_blank: false class << self def get_drafted_contracts draft=ClientContract.draft draft.empty? ? nil : get_contracts(draft) end def get_approved_contracts approved=ClientContract.approved approved.empty? ? nil : get_contracts(approved) end def get_declined_contracts declined=ClientContract.declined declined.empty? ? nil : get_contracts(declined) end def get_submitted_contracts submitted=ClientContract.submitted submitted.empty? ? nil : get_contracts(submitted) end def get_accepted_contracts accepted=ClientContract.accepted accepted.empty? ? nil : get_contracts(accepted) end def get_active_contracts active=ClientContract.active active.empty? ? nil : get_contracts(active) end def get_rejected_contracts rejected=ClientContract.rejected rejected.empty? ? nil : get_contracts(rejected) end def get_contracts(contracts) contract_details=[] contracts.each do|contract| contract_details.push({'id'=>contract.id, 'title'=>contract.title, 'client'=>contract.offer_request.operation.client ? contract.offer_request.operation.client.name : nil, 'contract_type'=>contract.offer_request.is_bulk ? 'Bulk purchase' : 'One time', 'effective_date'=>contract.effective_date, 'expiry_date'=>contract.expiry_date, 'contract_date'=>contract.contract_date, 'offer_request_id'=>contract.offer_request_id, 'request_no'=>contract.offer_request ? contract.offer_request.request_no : nil, 'contract_duration'=>contract.effective_date.to_formatted_s(:long)+'-'+contract.expiry_date.to_formatted_s(:long), # 'article'=>article(contract.id), 'transaction_type'=>contract.offer_request.operation.transaction_type ? contract.offer_request.operation.transaction_type.name : nil, # 'contract_remark'=>get_contract_remark(contract.id) }) end contract_details.empty? ? nil : contract_details end end =begin def self.get_contract_per_client(client_id) bls = Mks::BillOfLoading.where(client_id: client_id) contracts = Mks::ClientContract.where(bill_of_loading_id: bls) response = convert_to_json(contracts) return response end def self.convert_to_json(contracts) client_contracts = [] contracts.each { |contract| client_contracts.push({id: contract.id, title: contract.title, ref_no: contract.ref_no, contract_date: contract.contract_date}) } end =end end end end