Class: PLCommerce::Nip

Inherits:
Object
  • Object
show all
Defined in:
lib/pl-commerce/nip.rb

Constant Summary collapse

CONTROL_CODE_POSITION =
9
WAGES =
[6,5,7,2,3,4,5,6,7]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nipL, opts = {:lenient => false}) ⇒ Nip

constructor

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
# File 'lib/pl-commerce/nip.rb', line 13

def initialize(nipL, opts = {:lenient => false})
  raise ArgumentError, 'nip argument must be either of type String or Bignum' unless nipL.instance_of? String or nipL.instance_of? Bignum
  @lenient = opts[:lenient];
  #puts nip
  @nip = strip_NIP(nipL);
  if not @lenient
    pre_validate_NIP(@nip);
  end
end

Class Method Details

.valid?(nip, lenient = false) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/pl-commerce/nip.rb', line 82

def self.valid?(nip, lenient = false)
  return Nip.new(nip, lenient).valid?;
end

Instance Method Details

#get_ir_codeObject Also known as: ir_code

gets the code of Inland Revenue



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pl-commerce/nip.rb', line 50

def get_ir_code
  begin
    pre_validate_NIP(@nip, true, false, false)
    #3 characters is minimum to get IR code
    if @nip.length < 3
      return '?';
    end
  rescue Exception => e
    if @lenient
      return '?';
    end
    
    raise e;
  end

  return @nip[0, 3].to_i;
end

#get_ir_code_descObject Also known as: ir_code_desc



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pl-commerce/nip.rb', line 68

def get_ir_code_desc
  begin
    pre_validate_NIP(@nip, true, false, false)
  rescue Exception => e
    if @lenient
      return '?';
    end
    
    raise e;
  end
  
  return PLCommerce::IRCodes.get_ir_code_dsc(get_ir_code);
end

#to_sObject



86
87
88
# File 'lib/pl-commerce/nip.rb', line 86

def to_s
  return @nip;
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pl-commerce/nip.rb', line 23

def valid?
  #if lenient and validation produces problems
  #it means that this number is not correct
  begin
    pre_validate_NIP(@nip)
  rescue Exception => e
    if @lenient
      return false;
    end
    
    raise e;
  end

  sum = 0;
  for i in 0..(@nip.length-2)
    sum += @nip[i, 1].to_i * WAGES[i].to_i;
  end
  
  iControlCode = sum % 11;
  iControlCode = iControlCode % 10;
  
  iControlNIPValue = @nip[CONTROL_CODE_POSITION, 1].to_i;
  
  return (iControlCode == iControlNIPValue);
end