Untitled

mail@pastecode.io avatar
unknown
ruby
a month ago
2.8 kB
4
Indexable
Never
require_relative '../helpers/time_helper'
require_relative '../enums/storage_namespace_enum'
require_relative '../../lib/auth_helpers'

class WhSerialNumbersDAO < WHDataSource::BaseSqlDAO
  include TimeHelper
  SCHEMA = {
    :id => { :type => 'integer', :allow_nil => false, :less_than => 9223372036854775808 },
    :display_id => { :type => 'string', :allow_nil => false, :max_length => 255 },
    :grn_id => { :type => 'integer', :allow_nil => true, :less_than => 9223372036854775808 },
    :created_at => { :type => 'date', :allow_nil => true },
    :updated_at => { :type => 'date', :allow_nil => true },
    :product_detail_id => { :type => 'integer', :allow_nil => true, :less_than => 9223372036854775808 },
    :parent_id => { :type => 'integer', :allow_nil => true, :less_than => 9223372036854775808 }
  }

  def get_records_by_grn_id(grn_id)
    sql_stream.select(:grn_id => grn_id)
  end

  def get_records_by_grn_ids(id_list = [])
    sql_stream.select(:grn_id => id_list)
  end

  def get_records_by_display_id(display_id, filter = {})
    query = "SELECT * FROM wh_serial_numbers WHERE display_id = #{sql_stream.escape_user_input(display_id)}"
    query += "AND #{get_conditions(filter)}" if filter.present?
    query += ' order by id'
    sql_stream.select_by_sql(query)
  end

  def get_records_by_display_ids(display_ids)
    sql_stream.select(:display_id => display_ids)
  end
  def test(wsn_id)
    create_display_id(wsn_id)
  end

  def create_display_id(wsn_id, prefix = nil, _suffix = nil)
    str = wsn_id.to_s.rjust(10, '0')
    # WSN Generation Logic WMS = "_" (underscore) and FBE = "-" hyphen
    display_id = ((str.to_i)/26).to_s(36).upcase.rjust(6, '0') + "#{WarehouseService.settings.wsn_delimiter}" +('A'..'Z').to_a[(str.to_i)%26]
    update_wsn_display(wsn_id, display_id)
  end

  def update_wsn_display(wsn_id, display_id)
    query = "UPDATE wh_serial_numbers SET display_id = '#{display_id}' where id = #{wsn_id}"
    sql_stream.execute_by_sql(query)
  end
  def get_record_by_parent_id(parent_id)
    sql_stream.select(:parent_id => parent_id).last
  end

  def get_records_by_ids(id_list = [])
    sql_stream.select(:id => id_list)
  end

  def update_by_id(id, update_hash)
    sql_stream.update(update_hash, :id => id)
  end

  private

  def get_conditions(opts)
    opts.collect do |k, v|
      if v.is_a? Integer
        "#{sql_stream.escape_user_input(k, true)} = #{sql_stream.escape_user_input(v)}"
      elsif v.is_a? Array
        "#{sql_stream.escape_user_input(k, true)} IN (#{v.empty? ? '\'\'' :sql_stream.escape_user_input(v).join(', ')})"
      else
        "#{sql_stream.escape_user_input(k, true)} = #{sql_stream.escape_user_input(v)}"
      end
    end.join(' AND ')
  end

  def get_time
    now_to_datetime
  end

  def table_name
    StorageNamespaceEnum::WH_SERIAL_NUMBERS
  end
end
Leave a Comment