<commit_before># == License
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2008-2011 <NAME>, <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

module Backend
  class ParcelsController < Backend::BaseController
    manage_restfully t3e: { nature: 'RECORD.nature.text'.c }, planned_at: 'Time.zone.now'.c

    respond_to :csv, :ods, :xlsx, :pdf, :odt, :docx, :html, :xml, :json

    unroll

    # params:
    #   :q Text search
    #   :s State search
    #   :period Two Dates with _ separator
    #   :recipient_id
    #   :sender_id
    #   :transporter_id
    #   :delivery_mode Choice
    #   :nature Choice
    def self.parcels_conditions
      code = ''
      code = search_conditions(parcels: [:number, :reference_number], entities: [:full_name, :number]) + " ||= []\n"
      code << "unless params[:period].blank? || params[:period].is_a?(Symbol)\n"
      code << "  if params[:period] != 'all'\n"
      code << "    interval = params[:period].split('_')\n"
      code << "    first_date = interval.first\n"
      code << "    last_date = interval.last\n"
      code << "    c[0] << \" AND #{Parcel.table_name}.planned_at BETWEEN ? AND ?\"\n"
      code << "    c << first_date\n"
      code << "    c << last_date\n"
      code << "  end\n "
      code << "end\n "
      code << "if params[:recipient_id].to_i > 0\n"
      code << "  c[0] << \" AND \#{Parcel.table_name}.recipient_id = ?\"\n"
      code << "  c << params[:recipient_id].to_i\n"
      code << "end\n"
      code << "if params[:sender_id].to_i > 0\n"
      code << "  c[0] << \" AND \#{Parcel.table_name}.sender_id = ?\"\n"
      code << "  c << params[:sender_id].to_i\n"
      code << "end\n"
      code << "if params[:transporter_id].to_i > 0\n"
      code << "  c[0] << \" AND \#{Parcel.table_name}.transporter_id = ?\"\n"
      code << "  c << params[:transporter_id].to_i\n"
      code << "end\n"
      code << "if params[:delivery_mode].present? && params[:delivery_mode] != 'all'\n"
      code << "  if Parcel.delivery_mode.values.include?(params[:delivery_mode].to_sym)\n"
      code << "    c[0] << ' AND #{Parcel.table_name}.delivery_mode = ?'\n"
      code << "    c << params[:delivery_mode]\n"
      code << "  end\n"
      code << "end\n"
      code << "if params[:nature].present? && params[:nature] != 'all'\n"
      code << "  if Parcel.nature.values.include?(params[:nature].to_sym)\n"
      code << "    c[0] << ' AND #{Parcel.table_name}.nature = ?'\n"
      code << "    c << params[:nature]\n"
      code << "  end\n"
      code << "end\n"
      code << "c\n"
      code.c
    end

    list(conditions: parcels_conditions, order: { planned_at: :desc }) do |t|
      t.action :invoice, on: :both, method: :post, if: :invoiceable?
      t.action :ship,    on: :both, method: :post, if: :shippable?
      t.action :edit,    on: :both, method: :get, if: :updateable?
      t.action :destroy
      t.column :number, url: true
      t.column :reference_number, hidden: true
      t.column :nature
      t.column :planned_at
      t.column :recipient, url: true
      t.column :sender, url: true
      t.status
      t.column :human_state_name
      t.column :delivery, url: true
      t.column :transporter, url: true, hidden: true
      # t.column :sent_at
      t.column :delivery_mode
      # t.column :net_mass, hidden: true
      t.column :sale, url: true
      t.column :purchase, url: true
    end

    list(:outgoing_items, model: :parcel_items, conditions: { parcel_id: 'params[:id]'.c }) do |t|
      t.column :source_product, url: true
      t.column :product, url: true, hidden: true
      # t.column :product_work_number, through: :product, label_method: :work_number
      t.column :population
      t.column :unit_name, through: :variant
      # t.column :variant, url: true
      t.status
      # t.column :net_mass
      t.column :analysis, url: true
    end

    list(:incoming_items, model: :parcel_items, conditions: { parcel_id: 'params[:id]'.c }) do |t|
      t.column :variant, url: true
      # t.column :source_product, url: true
      t.column :product_name
      t.column :product_work_number
      t.column :population
      t.column :unit_name, through: :variant
      t.status
      # t.column :net_mass
      t.column :product, url: true
      t.column :analysis, url: true
    end

    # Displays the main page with the list of parcels
    def index
      respond_to do |format|
        format.html
        format.xml { render xml: @parcels }
        format.pdf { render pdf: @parcels, with: params[:template] }
      end
    end

    # Displays details of one parcel selected with +params[:id]+
    def show
      return unless @parcel = find_and_check
      respond_with(@parcel, methods: [:all_item_prepared, :status, :items_quantity],<commit_msg>Fixes invalid column name in backend/parcels<commit_after># == License
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2008-2011 <NAME>, <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

module Backend
  class ParcelsController < Backend::BaseController
    manage_restfully t3e: { nature: 'RECORD.nature.text'.c }, planned_at: 'Time.zone.now'.c

    respond_to :csv, :ods, :xlsx, :pdf, :odt, :docx, :html, :xml, :json

    unroll

    # params:
    #   :q Text search
    #   :s State search
    #   :period Two Dates with _ separator
    #   :recipient_id
    #   :sender_id
    #   :transporter_id
    #   :delivery_mode Choice
    #   :nature Choice
    def self.parcels_conditions
      code = ''
      code = search_conditions(parcels: [:number, :reference_number], entities: [:full_name, :number]) + " ||= []\n"
      code << "unless params[:period].blank? || params[:period].is_a?(Symbol)\n"
      code << "  if params[:period] != 'all'\n"
      code << "    interval = params[:period].split('_')\n"
      code << "    first_date = interval.first\n"
      code << "    last_date = interval.last\n"
      code << "    c[0] << \" AND #{Parcel.table_name}.planned_at BETWEEN ? AND ?\"\n"
      code << "    c << first_date\n"
      code << "    c << last_date\n"
      code << "  end\n "
      code << "end\n "
      code << "if params[:recipient_id].to_i > 0\n"
      code << "  c[0] << \" AND \#{Parcel.table_name}.recipient_id = ?\"\n"
      code << "  c << params[:recipient_id].to_i\n"
      code << "end\n"
      code << "if params[:sender_id].to_i > 0\n"
      code << "  c[0] << \" AND \#{Parcel.table_name}.sender_id = ?\"\n"
      code << "  c << params[:sender_id].to_i\n"
      code << "end\n"
      code << "if params[:transporter_id].to_i > 0\n"
      code << "  c[0] << \" AND \#{Parcel.table_name}.transporter_id = ?\"\n"
      code << "  c << params[:transporter_id].to_i\n"
      code << "end\n"
      code << "if params[:delivery_mode].present? && params[:delivery_mode] != 'all'\n"
      code << "  if Parcel.delivery_mode.values.include?(params[:delivery_mode].to_sym)\n"
      code << "    c[0] << ' AND #{Parcel.table_name}.delivery_mode = ?'\n"
      code << "    c << params[:delivery_mode]\n"
      code << "  end\n"
      code << "end\n"
      code << "if params[:nature].present? && params[:nature] != 'all'\n"
      code << "  if Parcel.nature.values.include?(params[:nature].to_sym)\n"
      code << "    c[0] << ' AND #{Parcel.table_name}.nature = ?'\n"
      code << "    c << params[:nature]\n"
      code << "  end\n"
      code << "end\n"
      code << "c\n"
      code.c
    end

    list(conditions: parcels_conditions, order: { planned_at: :desc }) do |t|
      t.action :invoice, on: :both, method: :post, if: :invoiceable?
      t.action :ship,    on: :both, method: :post, if: :shippable?
      t.action :edit,    on: :both, method: :get, if: :updateable?
      t.action :destroy
      t.column :number, url: true
      t.column :reference_number, hidden: true
      t.column :nature
      t.column :planned_at
      t.column :recipient, url: true
      t.column :sender, url: true
      t.status
      t.column :human_state_name
      t.column :delivery, url: true
      t.column :transporter, url: true, hidden: true
      # t.column :sent_at
      t.column :delivery_mode
      # t.column :net_mass, hidden: true
      t.column :sale, url: true
      t.column :purchase, url: true
    end

    list(:outgoing_items, model: :parcel_items, conditions: { parcel_id: 'params[:id]'.c }) do |t|
      t.column :source_product, url: true
      t.column :product, url: true, hidden: true
      # t.column :product_work_number, through: :product, label_method: :work_number
      t.column :population
      t.column :unit_name, through: :variant
      # t.column :variant, url: true
      t.status
      # t.column :net_mass
      t.column :analysis, url: true
    end

    list(:incoming_items, model: :parcel_items, conditions: { parcel_id: 'params[:id]'.c }) do |t|
      t.column :variant, url: true
      # t.column :source_product, url: true
      t.column :product_name
      t.column :product_identification_number
      t.column :population
      t.column :unit_name, through: :variant
      t.status
      # t.column :net_mass
      t.column :product, url: true
      t.column :analysis, url: true
    end

    # Displays the main page with the list of parcels
    def index
      respond_to do |format|
        format.html
        format.xml { render xml: @parcels }
        format.pdf { render pdf: @parcels, with: params[:template] }
      end
    end

    # Displays details of one parcel selected with +params[:id]+
    def show
      return unless @parcel = find_and_check
      respond_with(@parcel, methods: [:all_item_prepared, :status, :items_quantity],
{'fraction_non_alphanumeric': 0.1440481704492821, 'fraction_numerical': 0.0035201482167670217, 'mean_word_length': 2.6658743633276742, 'pattern_counts': {'":': 0, '<': 203, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '124399', 'n_tokens_mistral': 3692, 'n_tokens_neox': 3584, 'n_words': 986}