1 package SL::Controller::CsvImport::Helper::Consistency;
10 use SL::DB::Department;
12 use SL::Helper::Csv::Error;
14 use parent qw(Exporter);
15 our @EXPORT = qw(check_currency check_taxzone check_project check_department check_customer_vendor handle_salesman handle_employee);
22 my ($self, $entry, %params) = @_;
24 my $object = $entry->{object};
26 # Check whether or not currency ID is valid.
27 if ($object->currency_id && ! _currencies_by($self)->{id}->{ $object->currency_id }) {
28 push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
32 # Map name to ID if given.
33 if (!$object->currency_id && $entry->{raw_data}->{currency}) {
34 my $currency = _currencies_by($self)->{name}->{ $entry->{raw_data}->{currency} };
36 push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
40 $object->currency_id($currency->id);
42 # register currency_id for method copying later
43 $self->clone_methods->{currency_id} = 1;
46 # Set default currency if none was given and take_default is true.
47 $object->currency_id(_default_currency_id($self)) if !$object->currency_id and $params{take_default};
49 $entry->{raw_data}->{currency_id} = $object->currency_id;
55 my ($self, $entry, %params) = @_;
57 my $object = $entry->{object};
59 # Check whether the CSV contains the parameters taxzone_id or taxzone, and
60 # check them for validity.
61 # If one of them was given, but is invalid, return an error
63 # If neither was given:
64 # a) if param take_default was set, use the taxzone_id from the profile
65 # (customer/vendor import)
66 # b) if param take_default was not set, do nothing, return without error, and
67 # taxzone_id may be set later by other means (order import uses cv settings)
70 # if $object->taxzone_id is defined (from CSV line), check if it is valid
71 if ($object->taxzone_id && ! _taxzones_by($self)->{id}->{ $object->taxzone_id }) {
72 push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone');
76 # if there was no taxzone_id in CSV, but a taxzone entry, check if it is a
77 # valid taxzone and set the id
78 if (!$object->taxzone_id && $entry->{raw_data}->{taxzone}) {
79 my $taxzone = _taxzones_by($self)->{description}->{ $entry->{raw_data}->{taxzone} };
81 push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone');
85 $object->taxzone_id($taxzone->id);
88 # The take_default option should only be used for the customer/vendor case,
89 # as the default for imported orders is the taxzone according to the customer
91 # if neither taxzone_id nor taxzone were defined, use the default taxzone as
92 # defined from the import settings (a default/fallback taxzone that is to be
93 # used will always be selected)
95 if (!$object->taxzone_id && $params{take_default}) {
96 # my $default_id = $self->settings->{'default_taxzone'};
97 my $default_id = $self->controller->profile->get('default_taxzone');
98 $default_id ||= _default_taxzone_id($self);
99 $object->taxzone_id($default_id);
100 # check if default taxzone_id is valid just to be sure
101 if (! _taxzones_by($self)->{id}->{ $object->taxzone_id }) {
102 push @{ $entry->{errors} }, $::locale->text('Error with default taxzone');
107 # for the order import at this stage $object->taxzone_id may still not be
108 # defined, in this case the customer/vendor taxzone will be used later
109 if ( defined $object->taxzone_id ) {
110 $entry->{info_data}->{taxzone} = _taxzones_by($self)->{id}->{ $object->taxzone_id }->description;
117 my ($self, $entry, %params) = @_;
119 my $id_column = ($params{global} ? 'global' : '') . 'project_id';
120 my $number_column = ($params{global} ? 'global' : '') . 'projectnumber';
121 my $description_column = ($params{global} ? 'global' : '') . 'project';
123 my $object = $entry->{object};
125 # Check whether or not project ID is valid.
126 if ($object->$id_column) {
127 if (! _projects_by($self)->{id}->{ $object->$id_column }) {
128 push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
131 $entry->{info_data}->{$number_column} = _projects_by($self)->{id}->{ $object->$id_column }->description;
136 # Map number to ID if given.
137 if (!$object->$id_column && $entry->{raw_data}->{$number_column}) {
138 $proj = _projects_by($self)->{projectnumber}->{ $entry->{raw_data}->{$number_column} };
140 push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
144 $object->$id_column($proj->id);
147 # Map description to ID if given.
148 if (!$object->$id_column && $entry->{raw_data}->{$description_column}) {
149 $proj = _projects_by($self)->{description}->{ $entry->{raw_data}->{$description_column} };
151 push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
155 $object->$id_column($proj->id);
159 $entry->{info_data}->{"$description_column"} = $proj->description;
160 $entry->{info_data}->{"$number_column"} = $proj->projectnumber;
166 sub check_department {
167 my ($self, $entry) = @_;
169 my $object = $entry->{object};
171 # Check whether or not department ID was assigned and is valid.
172 if ($object->department_id) {
173 if (!_departments_by($self)->{id}->{ $object->department_id }) {
174 push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
177 # add department description as well, more feedback for user
178 $entry->{info_data}->{department} = _departments_by($self)->{id}->{ $object->department_id }->description;
182 # Map department description to ID if given.
183 if (!$object->department_id && $entry->{raw_data}->{department}) {
184 $entry->{info_data}->{department} = $entry->{raw_data}->{department};
185 my $dep = _departments_by($self)->{description}->{ $entry->{raw_data}->{department} };
187 push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
190 $entry->{info_data}->{department} = $dep->description;
191 $object->department_id($dep->id);
197 # ToDo: salesman by name
198 sub handle_salesman {
199 my ($self, $entry) = @_;
201 my $object = $entry->{object};
203 $vc_obj = SL::DB::Customer->new(id => $object->customer_id)->load if $object->can('customer') && $object->customer_id;
204 $vc_obj = SL::DB::Vendor->new(id => $object->vendor_id)->load if (!$vc_obj && $object->can('vendor') && $object->vendor_id);
206 # salesman from customer/vendor or login if not given
207 if (!$object->salesman) {
208 if ($vc_obj && $vc_obj->salesman_id) {
209 $object->salesman(SL::DB::Manager::Employee->find_by(id => $vc_obj->salesman_id));
211 $object->salesman(SL::DB::Manager::Employee->find_by(login => $::myconfig{login}));
216 # ToDo: employee by name
217 sub handle_employee {
218 my ($self, $entry) = @_;
220 my $object = $entry->{object};
222 # employee from front end if not given
223 if (!$object->employee_id) {
224 $object->employee_id($self->controller->{employee_id});
226 # employee from login if not given
227 if (!$object->employee_id) {
228 $object->employee_id(SL::DB::Manager::Employee->current->id) if SL::DB::Manager::Employee->current;
241 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_currencies($self) } } ) } qw(id name) };
244 sub _all_currencies {
247 return SL::DB::Manager::Currency->get_all;
250 sub _default_currency_id {
253 return SL::DB::Default->get->currency_id;
256 sub _default_taxzone_id {
259 return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ])->[0]->id;
262 sub _departments_by {
265 my $all_departments = SL::DB::Manager::Department->get_all;
266 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_departments } } ) } qw(id description) };
272 my $all_projects = SL::DB::Manager::Project->get_all;
273 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_projects } } ) } qw(id projectnumber description) };
279 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_taxzones($self) } } ) } qw(id description) };
285 return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]);