module Logistics module Core class OperationsController < ApplicationController # include ControllerCommon before_action :set_service, only: %i[assign] def index operations = Operation.includes(:declarant, :declaration_type, :transport_mode, :customs_office, :customs_office_unit, :country_of_origin, :acquisition_mode, :payment_term, :delivery_term, :container_arrangement, :customs_transport_tariff, :esl_transport_tariff, :warehouse, :owner, :offer_request).order('id DESC') data = ApplicationRecord.as_json(operations) response = { success: true, data: data } render json: response end def create service = OperationService.new result, @operation = service.save(model_params) if result response = Mks::Common::MethodResponse.new(true, 'Operation saved successfully!', @operation, nil, nil) else errors = Mks::Common::Util.error_messages @operation, 'Operation' response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil) end render json: response end def update operation = Operation.find(params[:id]) update_attributes(operation, model_params) if operation.save response = Mks::Common::MethodResponse.new(true, 'Operation updated successfully!', nil, nil, nil) else response = Mks::Common::MethodResponse.new(false, nil, nil, ['Operation update failed!'], nil) end render json: response end def fresh operations = Operation.fresh data = ApplicationRecord.as_json(operations) response = Mks::Common::MethodResponse.new(true, nil, data, nil, nil) render json: response end def filter_fresh_by_client operations = Operation.joins(:offer_request).fresh.where('logistics_core_offer_requests.client_id = ?', params[:id]) data = ApplicationRecord.as_json(operations) response = Mks::Common::MethodResponse.new(true, nil, data, nil, nil) render json: response end def assigned operations = Operation.where.not(owner_id: nil) data = ApplicationRecord.as_json(operations) response = Mks::Common::MethodResponse.new(true, nil, data, nil, nil) render json: response end def unassigned operations = Operation.where(owner_id: nil) data = ApplicationRecord.as_json(operations) response = Mks::Common::MethodResponse.new(true, nil, data, nil, nil) render json: response end def assign response = @service.assign_owner(assign_params[:payload]) render json: response end private def update_attributes(op, params) params.each do |k, v| op[k] = v end op end def set_service @service = OperationService.new end def assign_params params.permit(payload: [:owner_id, op_ids: []]) end def model_params params.require(:operation).permit(:operation_number, :contract_number, :is_new_customer, :status, :load_level, :offer_request_id, :declarant_id, :declaration_type_id, :transport_mode_id, :customs_office_id, :customs_office_unit_id, :country_of_origin_id, :t_number, :acquisition_mode_id, :payment_term_id, :delivery_term_id, :tin_number, :container_arrangement_id, :customs_transport_tariff_id, :transfer_tin_no, :warehouse_id, :trade_license_number, :esl_transport_tariff_id, :is_transfer, :investment_license_number, :transfer_name, :transport_modality) end end end end