1 package SL::Controller::CsvImport::Helper::Consistency;
9 use SL::DB::Pricegroup;
11 use SL::DB::Department;
13 use SL::Helper::Csv::Error;
15 use parent qw(Exporter);
16 our @EXPORT = qw(check_currency check_taxzone check_pricegroup check_project check_department check_customer_vendor handle_salesman handle_employee);
23 my ($self, $entry, %params) = @_;
25 my $object = $entry->{object};
27 # Check whether or not currency ID is valid.
28 if ($object->currency_id && ! _currencies_by($self)->{id}->{ $object->currency_id }) {
29 push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
33 # Map name to ID if given.
34 if (!$object->currency_id && $entry->{raw_data}->{currency}) {
35 my $currency = _currencies_by($self)->{name}->{ $entry->{raw_data}->{currency} };
37 push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
41 $object->currency_id($currency->id);
43 # register currency_id for method copying later
44 $self->clone_methods->{currency_id} = 1;
47 # Set default currency if none was given and take_default is true.
48 $object->currency_id(_default_currency_id($self)) if !$object->currency_id and $params{take_default};
50 $entry->{raw_data}->{currency_id} = $object->currency_id;
56 my ($self, $entry, %params) = @_;
58 my $object = $entry->{object};
60 # Check whether the CSV contains the parameters taxzone_id or taxzone, and
61 # check them for validity.
62 # If one of them was given, but is invalid, return an error
64 # If neither was given:
65 # a) if param take_default was set, use the taxzone_id from the profile
66 # (customer/vendor import)
67 # b) if param take_default was not set, do nothing, return without error, and
68 # taxzone_id may be set later by other means (order import uses cv settings)
71 # if $object->taxzone_id is defined (from CSV line), check if it is valid
72 if ($object->taxzone_id && ! _taxzones_by($self)->{id}->{ $object->taxzone_id }) {
73 push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone');
77 # if there was no taxzone_id in CSV, but a taxzone entry, check if it is a
78 # valid taxzone and set the id
79 if (!$object->taxzone_id && $entry->{raw_data}->{taxzone}) {
80 my $taxzone = _taxzones_by($self)->{description}->{ $entry->{raw_data}->{taxzone} };
82 push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone');
86 $object->taxzone_id($taxzone->id);
89 # The take_default option should only be used for the customer/vendor case,
90 # as the default for imported orders is the taxzone according to the customer
92 # if neither taxzone_id nor taxzone were defined, use the default taxzone as
93 # defined from the import settings (a default/fallback taxzone that is to be
94 # used will always be selected)
96 if (!$object->taxzone_id && $params{take_default}) {
97 # my $default_id = $self->settings->{'default_taxzone'};
98 my $default_id = $self->controller->profile->get('default_taxzone');
99 $default_id ||= _default_taxzone_id($self);
100 $object->taxzone_id($default_id);
101 # check if default taxzone_id is valid just to be sure
102 if (! _taxzones_by($self)->{id}->{ $object->taxzone_id }) {
103 push @{ $entry->{errors} }, $::locale->text('Error with default taxzone');
108 # for the order import at this stage $object->taxzone_id may still not be
109 # defined, in this case the customer/vendor taxzone will be used later
110 if ( defined $object->taxzone_id ) {
111 $entry->{info_data}->{taxzone} = _taxzones_by($self)->{id}->{ $object->taxzone_id }->description;
117 sub check_pricegroup {
118 my ($self, $entry) = @_;
120 my $object = $entry->{object};
122 # Check whether or not pricegroup ID is valid.
123 if ($object->pricegroup_id && !_pricegroups_by($self)->{id}->{ $object->pricegroup_id }) {
124 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price group');
128 # Map pricegroup to ID if given.
129 if (!$object->pricegroup_id && $entry->{raw_data}->{pricegroup}) {
130 my $pricegroup = _pricegroups_by($self)->{pricegroup}->{ $entry->{raw_data}->{pricegroup} };
132 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price group');
136 $object->pricegroup_id($pricegroup->id);
143 my ($self, $entry, %params) = @_;
145 my $id_column = ($params{global} ? 'global' : '') . 'project_id';
146 my $number_column = ($params{global} ? 'global' : '') . 'projectnumber';
147 my $description_column = ($params{global} ? 'global' : '') . 'project';
149 my $object = $entry->{object};
151 # Check whether or not project ID is valid.
152 if ($object->$id_column) {
153 if (! _projects_by($self)->{id}->{ $object->$id_column }) {
154 push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
157 $entry->{info_data}->{$number_column} = _projects_by($self)->{id}->{ $object->$id_column }->description;
162 # Map number to ID if given.
163 if (!$object->$id_column && $entry->{raw_data}->{$number_column}) {
164 $proj = _projects_by($self)->{projectnumber}->{ $entry->{raw_data}->{$number_column} };
166 push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
170 $object->$id_column($proj->id);
173 # Map description to ID if given.
174 if (!$object->$id_column && $entry->{raw_data}->{$description_column}) {
175 $proj = _projects_by($self)->{description}->{ $entry->{raw_data}->{$description_column} };
177 push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
181 $object->$id_column($proj->id);
185 $entry->{info_data}->{"$description_column"} = $proj->description;
186 $entry->{info_data}->{"$number_column"} = $proj->projectnumber;
192 sub check_department {
193 my ($self, $entry) = @_;
195 my $object = $entry->{object};
197 # Check whether or not department ID was assigned and is valid.
198 if ($object->department_id) {
199 if (!_departments_by($self)->{id}->{ $object->department_id }) {
200 push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
203 # add department description as well, more feedback for user
204 $entry->{info_data}->{department} = _departments_by($self)->{id}->{ $object->department_id }->description;
208 # Map department description to ID if given.
209 if (!$object->department_id && $entry->{raw_data}->{department}) {
210 $entry->{info_data}->{department} = $entry->{raw_data}->{department};
211 my $dep = _departments_by($self)->{description}->{ $entry->{raw_data}->{department} };
213 push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
216 $entry->{info_data}->{department} = $dep->description;
217 $object->department_id($dep->id);
223 # ToDo: salesman by name
224 sub handle_salesman {
225 my ($self, $entry) = @_;
227 my $object = $entry->{object};
229 $vc_obj = SL::DB::Customer->new(id => $object->customer_id)->load if $object->can('customer') && $object->customer_id;
230 $vc_obj = SL::DB::Vendor->new(id => $object->vendor_id)->load if (!$vc_obj && $object->can('vendor') && $object->vendor_id);
232 # salesman from customer/vendor or login if not given
233 if (!$object->salesman) {
234 if ($vc_obj && $vc_obj->salesman_id) {
235 $object->salesman(SL::DB::Manager::Employee->find_by(id => $vc_obj->salesman_id));
237 $object->salesman(SL::DB::Manager::Employee->current);
242 # ToDo: employee by name
243 sub handle_employee {
244 my ($self, $entry) = @_;
246 my $object = $entry->{object};
248 # employee from front end if not given
249 if (!$object->employee_id) {
250 $object->employee_id($self->controller->{employee_id});
252 # employee from login if not given
253 if (!$object->employee_id) {
254 $object->employee_id(SL::DB::Manager::Employee->current->id) if SL::DB::Manager::Employee->current;
267 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_currencies($self) } } ) } qw(id name) };
270 sub _all_currencies {
273 return SL::DB::Manager::Currency->get_all;
276 sub _default_currency_id {
279 return SL::DB::Default->get->currency_id;
282 sub _default_taxzone_id {
285 return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ])->[0]->id;
288 sub _departments_by {
291 my $all_departments = SL::DB::Manager::Department->get_all;
292 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_departments } } ) } qw(id description) };
295 sub _pricegroups_by {
298 my $all_pricegroups = SL::DB::Manager::Pricegroup->get_all;
299 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_pricegroups } } ) } qw(id pricegroup) };
305 my $all_projects = SL::DB::Manager::Project->get_all;
306 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_projects } } ) } qw(id projectnumber description) };
312 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_taxzones($self) } } ) } qw(id description) };
318 return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]);