| 1 | ############################################################################## | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (C) 2012 Agile Business Group sagl (<http://www.agilebg.com>) | ||||||||||||||||||||||||||||
| 4 | # Copyright (C) 2012 Domsense srl (<http://www.domsense.com>) | ||||||||||||||||||||||||||||
| 5 | # Copyright (C) 2012 Associazione OpenERP Italia | ||||||||||||||||||||||||||||
| 6 | # (<http://www.openerp-italia.org>). | ||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2022 Res Binaria Di Paolo Capaldo (<https://www.resbinaria.com/>) | ||||||||||||||||||||||||||||
| 8 | # All Rights Reserved | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 11 | # it under the terms of the GNU Affero General Public License as published | ||||||||||||||||||||||||||||
| 12 | # by the Free Software Foundation, either version 3 of the License, or | ||||||||||||||||||||||||||||
| 13 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 18 | # GNU Affero General Public License for more details. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # You should have received a copy of the GNU Affero General Public License | ||||||||||||||||||||||||||||
| 21 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||
| 23 | ############################################################################## | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | package CBI::Wrapper::Record; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # ABSTRACT: Row in a CBI file. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use CBI::Wrapper::Field; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use CBI::Wrapper::RecordMapping; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 40 | 0 | my $self = { | |||||||||||||||||||||||||||
| 41 | _raw_record => shift, | ||||||||||||||||||||||||||||
| 42 | _flow_type => shift, # see RecordMapping.pm. | ||||||||||||||||||||||||||||
| 43 | _fields => [], | ||||||||||||||||||||||||||||
| 44 | _code => '', # Record code (see RecordMapping.pm). | ||||||||||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Check row length. | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (length($self->get_raw_record()) == 2 or length($self->get_raw_record()) == 120) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $self->get_raw_record == 2 or length $self->get_raw_record == 120
MC/DC0%0/2
length $self->get_raw_record == 2 or length $self->get_raw_record == 120
length $self->get_raw_record == 2
length $self->get_raw_record == 120
Truth table0%0/3
A or B
A= length $self->get_raw_record == 2B= length $self->get_raw_record == 120
| |||||||||||||||||||||||||||||
| 51 | 0 | $self->set_code($self->get_raw_record()); | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 0 | croak('[TypeError] String (' . $self->get_raw_record() . ') must contain 2 or 120 chars'); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $self->set_code($self->get_raw_record()) if length($self->get_code()) == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $self->set_code(substr($self->get_raw_record(), 1, 2)) if length($self->get_code()) == 120; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $flow_type = defined $self->get_flow_type() ? $self->get_flow_type() : $CBI::Wrapper::RecordMapping::FLOW_TYPE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | $self->set_flow_type($CBI::Wrapper::RecordMapping::RECORD_MAPPING->{$flow_type}); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | $flow_type = $self->get_flow_type(); | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | unless (exists $flow_type->{$self->get_code()}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | croak('[IndexError] Unknown record type ' . $self->get_code()); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Fields creation. | ||||||||||||||||||||||||||||
| 73 | 0 | for my $field_args (@{$flow_type->{$self->get_code()}}) { | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my $new_field = new CBI::Wrapper::Field($field_args); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ($new_field->get_type() eq 'tipo_record') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $new_field->set_content($self->get_code()); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | $self->append_field($new_field); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $self->insert_fields_content($self->get_raw_record()) if length($self->get_raw_record()) == 120; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | return $self; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub set_raw_record { | ||||||||||||||||||||||||||
|
Subroutine
set_raw_record: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($self, $raw_record) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | $self->{_raw_record} = $raw_record; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub get_raw_record { | ||||||||||||||||||||||||||
|
Subroutine
get_raw_record: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | return $self->{_raw_record}; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub set_flow_type { | ||||||||||||||||||||||||||
|
Subroutine
set_flow_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 0 | my ($self, $flow_type) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | $self->{_flow_type} = $flow_type; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub get_flow_type { | ||||||||||||||||||||||||||
|
Subroutine
get_flow_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | return $self->{_flow_type}; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub set_code { | ||||||||||||||||||||||||||
|
Subroutine
set_code: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($self, $code) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $self->{_code} = $code; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub get_code { | ||||||||||||||||||||||||||
|
Subroutine
get_code: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | return $self->{_code}; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub set_fields { | ||||||||||||||||||||||||||
|
Subroutine
set_fields: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 0 | my ($self, $fields) = @_; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | $self->{_fields} = $fields; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub get_fields { | ||||||||||||||||||||||||||
|
Subroutine
get_fields: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | return $self->{_fields}; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Set a string in content. | ||||||||||||||||||||||||||||
| 138 | # The key param can be: | ||||||||||||||||||||||||||||
| 139 | # 1. arrayref with start and end column (es. [1,3]). | ||||||||||||||||||||||||||||
| 140 | # 2. name of the field | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub set_field_content { | ||||||||||||||||||||||||||
|
Subroutine
set_field_content: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 142 | 0 | my ($self, $key, $content) = @_; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (ref $key eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | for my $field (@{$self->get_fields()}) { | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($field->get_from_position() == $key->[0] and $field->get_to_position() == $key->[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$field->get_from_position == $key->[0] and $field->get_to_position == $key->[1]
MC/DC0%0/2
$field->get_from_position == $key->[0] and $field->get_to_position == $key->[1]
$field->get_from_position == $key->[0]
$field->get_to_position == $key->[1]
Truth table0%0/3
A and B
A= $field->get_from_position == $key->[0]B= $field->get_to_position == $key->[1]
| |||||||||||||||||||||||||||||
| 147 | 0 | $field->set_content($content); | |||||||||||||||||||||||||||
| 148 | 0 | return; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 0 | croak('[IndexError] Impossible to find field with position ' . $key->[0] . ', ' . $key->[1]); | |||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||
| 153 | 0 | for my $field (@{$self->get_fields()}) { | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($field->get_name() eq $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | #say STDERR $field->get_name() . ' - ' . $content; | ||||||||||||||||||||||||||||
| 156 | 0 | $field->set_content($content); | |||||||||||||||||||||||||||
| 157 | 0 | return; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | #use Data::Dumper; | ||||||||||||||||||||||||||||
| 163 | #say STDOUT 'SELF'. Dumper($self->{_fields}); | ||||||||||||||||||||||||||||
| 164 | 0 | croak('[IndexError] Impossible to find field with key ' . $key); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # Get a string from a field in the current record. | ||||||||||||||||||||||||||||
| 168 | # The key param can be: | ||||||||||||||||||||||||||||
| 169 | # 1. arrayref with start and end column (es. [1,3]). | ||||||||||||||||||||||||||||
| 170 | # 2. name of the field | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub get_field_content { | ||||||||||||||||||||||||||
|
Subroutine
get_field_content: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | if (ref $key eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | return substr($self->to_string(), $key->[0], $key->[1]); | |||||||||||||||||||||||||||
| 175 | } else { | ||||||||||||||||||||||||||||
| 176 | 0 | for my $field (@{$self->get_fields()}) { | |||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($field->get_name() eq $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | my $ret = $field->get_content(); | |||||||||||||||||||||||||||
| 179 | 0 | $ret =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 180 | 0 | return $ret; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 0 | croak('[IndexError] Impossible to find field with key ' . $key); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub append_field { | ||||||||||||||||||||||||||
|
Subroutine
append_field: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 189 | 0 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Type check | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | unless (Scalar::Util::blessed($field) eq 'CBI::Wrapper::Field') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | croak('[TypeError] You can only append Field objects'); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | for my $f (@{$self->get_fields()}) { | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | if ($f->get_name() eq $field->get_name()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | croak('[IndexError] Field name ' . $field->get_name() . ' already present'); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | my $fields = $self->get_fields(); | |||||||||||||||||||||||||||
| 203 | 0 | push(@$fields, $field); | |||||||||||||||||||||||||||
| 204 | 0 | $self->set_fields($fields); | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | sub insert_fields_content { | ||||||||||||||||||||||||||
|
Subroutine
insert_fields_content: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 208 | 0 | my ($self, $raw_record) = @_; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | for my $field (@{$self->get_fields()}) { | |||||||||||||||||||||||||||
| 211 | 0 | my $from_position = $field->get_from_position() - 1; | |||||||||||||||||||||||||||
| 212 | 0 | my $to_position = $field->get_to_position(); | |||||||||||||||||||||||||||
| 213 | 0 | $to_position -= $from_position; | |||||||||||||||||||||||||||
| 214 | 0 | $field->set_content(substr($raw_record, $from_position, $to_position)); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # Convert to string (row in CBI file). | ||||||||||||||||||||||||||||
| 219 | 0 | ▶ | sub to_string { | ||||||||||||||||||||||||||
|
Subroutine
to_string: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 220 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 221 | 0 | my $c = ''; | |||||||||||||||||||||||||||
| 222 | 0 | for my $field (@{$self->get_fields()}) { | |||||||||||||||||||||||||||
| 223 | 0 | $c .= $field->get_content(); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 0 | return $c; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 1 | 1; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||