1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
6 use List::MoreUtils qw(any);
10 use SL::Helper::Flash;
11 use SL::Locale::String;
12 use SL::Util qw(trim);
15 use SL::Controller::Helper::GetModels;
16 use SL::Controller::Helper::ReportGenerator;
17 use SL::Controller::Helper::ParseFilter;
22 use SL::DB::ContactDepartment;
23 use SL::DB::ContactTitle;
29 use SL::DB::PaymentTerm;
30 use SL::DB::Pricegroup;
34 use SL::DB::FollowUpLink;
38 use SL::DB::PurchaseInvoice;
43 use Rose::Object::MakeMethods::Generic (
44 scalar => [ qw(user_has_edit_rights) ],
45 'scalar --get_set_init' => [ qw(customer_models vendor_models zugferd_settings) ],
49 __PACKAGE__->run_before(
53 'save_and_ap_transaction',
54 'save_and_ar_transaction',
66 __PACKAGE__->run_before(
67 '_load_customer_vendor',
78 # make sure this comes after _load_customer_vendor
79 __PACKAGE__->run_before('_check_auth');
81 __PACKAGE__->run_before(
82 '_create_customer_vendor',
88 __PACKAGE__->run_before('normalize_name');
96 if ($self->{cv}->is_customer) {
97 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate);
98 $self->{cv}->salesman_id(SL::DB::Manager::Employee->current->id) if !$::auth->assert('customer_vendor_all_edit', 1);
102 'customer_vendor/form',
103 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
104 %{$self->{template_args}}
111 $self->_pre_render();
113 'customer_vendor/form',
114 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
115 %{$self->{template_args}}
122 if ($::request->type eq 'json') {
127 $cv_hash = $self->{cv}->as_tree;
128 $cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
131 $self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
138 my @errors = $self->{cv}->validate;
140 flash('error', @errors);
141 $self->_pre_render();
143 'customer_vendor/form',
144 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
145 %{$self->{template_args}}
147 $::dispatcher->end_request;
150 $self->{cv}->greeting(trim $self->{cv}->greeting);
151 my $save_greeting = $self->{cv}->greeting
152 && $::instance_conf->get_vc_greetings_use_textfield
153 && SL::DB::Manager::Greeting->get_all_count(where => [description => $self->{cv}->greeting]) == 0;
155 $self->{contact}->cp_title(trim($self->{contact}->cp_title));
156 my $save_contact_title = $self->{contact}->cp_title
157 && $::instance_conf->get_contact_titles_use_textfield
158 && SL::DB::Manager::ContactTitle->get_all_count(where => [description => $self->{contact}->cp_title]) == 0;
160 $self->{contact}->cp_abteilung(trim($self->{contact}->cp_abteilung));
161 my $save_contact_department = $self->{contact}->cp_abteilung
162 && $::instance_conf->get_contact_departments_use_textfield
163 && SL::DB::Manager::ContactDepartment->get_all_count(where => [description => $self->{contact}->cp_abteilung]) == 0;
165 my $db = $self->{cv}->db;
167 $db->with_transaction(sub {
169 if ( $self->is_vendor() ) {
170 if ( $self->{cv}->vendornumber ) {
171 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
174 if ( $self->{cv}->customernumber ) {
175 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
179 foreach my $entry (@{$cvs_by_nr}) {
180 if( $entry->id != $self->{cv}->id ) {
182 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
184 $::form->error($msg);
188 $self->{cv}->save(cascade => 1);
190 SL::DB::Greeting->new(description => $self->{cv}->greeting)->save if $save_greeting;
192 $self->{contact}->cp_cv_id($self->{cv}->id);
193 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
194 SL::DB::ContactTitle ->new(description => $self->{contact}->cp_title) ->save if $save_contact_title;
195 SL::DB::ContactDepartment->new(description => $self->{contact}->cp_abteilung)->save if $save_contact_department;
197 $self->{contact}->save(cascade => 1);
200 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
202 if ( !$self->{note_followup}->follow_up_date ) {
203 $::form->error($::locale->text('Date missing!'));
206 $self->{note}->trans_id($self->{cv}->id);
207 $self->{note}->save();
209 $self->{note_followup}->save();
211 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
212 $self->{note_followup_link}->trans_id($self->{cv}->id);
213 $self->{note_followup_link}->save();
215 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
218 $self->{shipto}->trans_id($self->{cv}->id);
219 if(any { $self->{shipto}->$_ ne '' } qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax shiptoemail)) {
220 $self->{shipto}->save(cascade => 1);
223 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
224 SL::DB::History->new(
225 trans_id => $self->{cv}->id,
226 snumbers => $snumbers,
227 employee_id => SL::DB::Manager::Employee->current->id,
231 if ( $::form->{delete_notes} ) {
232 foreach my $note_id (@{ $::form->{delete_notes} }) {
233 my $note = SL::DB::Note->new(id => $note_id)->load();
234 if ( $note->follow_up ) {
235 if ( $note->follow_up->follow_up_link ) {
236 $note->follow_up->follow_up_link->delete(cascade => 'delete');
238 $note->follow_up->delete(cascade => 'delete');
240 $note->delete(cascade => 'delete');
245 }) || die($db->error);
254 my @redirect_params = (
256 id => $self->{cv}->id,
257 db => ($self->is_vendor() ? 'vendor' : 'customer'),
260 if ( $self->{contact}->cp_id ) {
261 push(@redirect_params, contact_id => $self->{contact}->cp_id);
264 if ( $self->{shipto}->shipto_id ) {
265 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
268 $self->redirect_to(@redirect_params);
271 sub action_save_and_close {
276 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
277 $::form->redirect($msg);
281 my ($self, $script) = @_;
283 $::auth->assert('gl_transactions | ap_transactions | ar_transactions'.
284 '| invoice_edit | vendor_invoice_edit | ' .
285 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
289 my $name = $::form->escape($self->{cv}->name, 1);
290 my $db = $self->is_vendor() ? 'vendor' : 'customer';
293 if ($::instance_conf->get_feature_experimental_order && 'oe.pl' eq $script) {
294 $script = 'controller.pl';
295 $action = 'Order/' . $action;
298 my $url = $self->url_for(
299 controller => $script,
302 $db .'_id' => $self->{cv}->id,
304 type => $::form->{type},
305 callback => $::form->{callback},
308 print $::form->redirect_header($url);
311 sub action_save_and_ar_transaction {
314 $main::auth->assert('ar_transactions');
316 $self->_transaction('ar.pl');
319 sub action_save_and_ap_transaction {
322 $main::auth->assert('ap_transactions');
324 $self->_transaction('ap.pl');
327 sub action_save_and_invoice {
330 if ( $self->is_vendor() ) {
331 $::auth->assert('vendor_invoice_edit');
333 $::auth->assert('invoice_edit');
336 $::form->{type} = 'invoice';
337 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
340 sub action_save_and_order {
343 if ( $self->is_vendor() ) {
344 $::auth->assert('purchase_order_edit');
346 $::auth->assert('sales_order_edit');
349 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
350 $self->_transaction('oe.pl');
353 sub action_save_and_rfq {
356 $::auth->assert('request_quotation_edit');
358 $::form->{type} = 'request_quotation';
359 $self->_transaction('oe.pl');
362 sub action_save_and_quotation {
365 $::auth->assert('sales_quotation_edit');
367 $::form->{type} = 'sales_quotation';
368 $self->_transaction('oe.pl');
374 my $db = $self->{cv}->db;
376 if( !$self->is_orphaned() ) {
377 $self->action_edit();
380 $db->with_transaction(sub {
381 $self->{cv}->delete(cascade => 1);
383 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
384 SL::DB::History->new(
385 trans_id => $self->{cv}->id,
386 snumbers => $snumbers,
387 employee_id => SL::DB::Manager::Employee->current->id,
388 addition => 'DELETED',
390 }) || die($db->error);
392 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
393 $::form->redirect($msg);
399 sub action_delete_contact {
402 my $db = $self->{contact}->db;
404 if ( !$self->{contact}->cp_id ) {
405 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
408 $db->with_transaction(sub {
409 if ( $self->{contact}->used ) {
410 $self->{contact}->detach();
411 $self->{contact}->save();
412 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
414 $self->{contact}->delete(cascade => 1);
415 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
419 }) || die($db->error);
421 $self->{contact} = $self->_new_contact_object;
424 $self->action_edit();
427 sub action_delete_shipto {
430 my $db = $self->{shipto}->db;
432 if ( !$self->{shipto}->shipto_id ) {
433 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
436 $db->with_transaction(sub {
437 if ( $self->{shipto}->used ) {
438 $self->{shipto}->detach();
439 $self->{shipto}->save(cascade => 1);
440 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
442 $self->{shipto}->delete(cascade => 1);
443 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
447 }) || die($db->error);
449 $self->{shipto} = SL::DB::Shipto->new();
452 $self->action_edit();
460 controller => 'ct.pl',
462 db => $self->is_vendor() ? 'vendor' : 'customer',
465 if ( $::form->{callback} ) {
466 push(@url_params, callback => $::form->{callback});
469 $self->redirect_to(@url_params);
473 sub action_search_contact {
476 my $url = 'ct.pl?action=search_contact&db=customer';
478 if ( $::form->{callback} ) {
479 $url .= '&callback='. $::form->escape($::form->{callback});
482 print $::form->redirect_header($url);
485 sub action_get_delivery {
488 $::auth->assert('sales_all_edit') if $self->is_customer();
489 $::auth->assert('purchase_all_edit') if $self->is_vendor();
491 my $dbh = $::form->get_standard_dbh();
493 my ($arap, $db, $qty_sign);
494 if ( $self->is_vendor() ) {
497 $qty_sign = ' * -1 AS qty';
504 my $where = ' WHERE 1=1';
507 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
508 $where .= " AND ${arap}.shipto_id = ?";
509 push(@values, $::form->{shipto_id});
511 $where .= " AND ${arap}.${db}_id = ?";
512 push(@values, $::form->{id});
515 if ( $::form->{delivery_from} ) {
516 $where .= " AND ${arap}.transdate >= ?";
517 push(@values, conv_date($::form->{delivery_from}));
520 if ( $::form->{delivery_to} ) {
521 $where .= " AND ${arap}.transdate <= ?";
522 push(@values, conv_date($::form->{delivery_to}));
541 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
544 ON ${arap}.id = i.trans_id
550 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
551 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
554 ORDER BY ${arap}.transdate DESC LIMIT 15";
556 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
558 $self->render('customer_vendor/get_delivery', { layout => 0 });
561 sub action_ajaj_get_shipto {
568 my $name = 'shipto'. $_;
569 $name => $self->{shipto}->$name;
571 qw(_id name department_1 department_2 street zipcode city gln country contact phone fax email)
575 $data->{shipto_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{shipto}->cvars_by_config);
577 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
580 sub action_ajaj_get_contact {
588 my $name = 'cp_'. $_;
590 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
591 $name => $self->{contact}->$name->to_lxoffice;
593 $name => $self->{contact}->$name;
597 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
598 satphone satfax project street zipcode city privatphone privatemail birthday main
603 $data->{contact_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{contact}->cvars_by_config);
605 # avoid two or more main_cp
606 my $has_main_cp = grep { $_->cp_main == 1 } @{ $self->{cv}->contacts };
607 $data->{contact}->{disable_cp_main} = 1 if ($has_main_cp && !$data->{contact}->{cp_main});
609 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
612 sub action_ajaj_autocomplete {
613 my ($self, %params) = @_;
615 my ($model, $manager, $number, $matches);
617 # first see if this is customer or vendor picking
618 if ($::form->{type} eq 'customer') {
619 $model = $self->customer_models;
620 $manager = 'SL::DB::Manager::Customer';
621 $number = 'customernumber';
622 } elsif ($::form->{type} eq 'vendor') {
623 $model = $self->vendor_models;
624 $manager = 'SL::DB::Manager::Vendor';
625 $number = 'vendornumber';
627 die "unknown type $::form->{type}";
630 # if someone types something, and hits enter, assume he entered the full name.
631 # if something matches, treat that as the sole match
632 # unfortunately get_models can't do more than one per package atm, so we do it
633 # the oldfashioned way.
634 if ($::form->{prefer_exact}) {
636 if (1 == scalar @{ $exact_matches = $manager->get_all(
640 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
641 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
646 $matches = $exact_matches;
650 $matches //= $model->get;
654 value => $_->displayable_name,
655 label => $_->displayable_name,
657 $number => $_->$number,
659 type => $::form->{type},
660 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
664 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
667 sub action_test_page {
668 $_[0]->render('customer_vendor/test_page');
671 sub action_ajax_list_prices {
672 my ($self, %params) = @_;
674 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
675 my @columns = qw(partnumber description price);
676 my @visible = qw(partnumber description price);
677 my @sortable = qw(partnumber description price);
680 partnumber => { text => $::locale->text('Part Number'), sub => sub { $_[0]->parts->partnumber } },
681 description => { text => $::locale->text('Part Description'), sub => sub { $_[0]->parts->description } },
682 price => { text => $::locale->text('Price'), sub => sub { $::form->format_amount(\%::myconfig, $_[0]->price, 2) }, align => 'right' },
685 $::form->{sort_by} ||= 'partnumber';
686 $::form->{sort_dir} //= 1;
688 for my $col (@sortable) {
689 $column_defs{$col}{link} = $self->url_for(
690 action => 'ajax_list_prices',
691 callback => $::form->{callback},
693 id => $self->{cv}->id,
695 sort_dir => ($::form->{sort_by} eq $col ? 1 - $::form->{sort_dir} : $::form->{sort_dir})
699 map { $column_defs{$_}{visible} = 1 } @visible;
702 $pricegroup = $self->{cv}->pricegroup->pricegroup if $self->{cv}->pricegroup;
704 $report->set_columns(%column_defs);
705 $report->set_column_order(@columns);
706 $report->set_options(allow_pdf_export => 0, allow_csv_export => 0);
707 $report->set_sort_indicator($::form->{sort_by}, $::form->{sort_dir});
708 $report->set_export_options(@{ $params{report_generator_export_options} || [] });
709 $report->set_options(
710 %{ $params{report_generator_options} || {} },
711 output_format => 'HTML',
712 top_info_text => $::locale->text('Pricegroup') . ': ' . $pricegroup,
713 title => $::locale->text('Price List'),
716 my $sort_param = $::form->{sort_by} eq 'price' ? 'price' :
717 $::form->{sort_by} eq 'description' ? 'parts.description' :
719 $sort_param .= ' ' . ($::form->{sort_dir} ? 'ASC' : 'DESC');
720 my $prices = SL::DB::Manager::Price->get_all(where => [ pricegroup_id => $self->{cv}->pricegroup_id ],
721 sort_by => $sort_param,
722 with_objects => 'parts');
724 $self->report_generator_list_objects(report => $report, objects => $prices, layout => 0, header => 0);
728 return $::form->{db} eq 'vendor';
732 return $::form->{db} eq 'customer';
738 if ( defined($self->{_is_orphaned}) ) {
739 return $self->{_is_orphaned};
742 my $arap = $self->is_vendor ? 'ap' : 'ar';
745 my $cv = $self->is_vendor ? 'vendor' : 'customer';
749 FROM '. $arap .' AS a
750 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
757 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
763 FROM delivery_orders a
764 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
768 if ( $self->is_vendor ) {
771 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
775 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
777 return $self->{_is_orphaned} = !$dummy;
780 sub _copy_form_to_cvars {
781 my ($self, %params) = @_;
783 foreach my $cvar (@{ $params{target}->cvars_by_config }) {
784 my $value = $params{source}->{$cvar->config->name};
785 $value = $::form->parse_amount(\%::myconfig, $value) if $cvar->config->type eq 'number';
787 $cvar->value($value);
791 sub _instantiate_args {
794 my $curr_employee = SL::DB::Manager::Employee->current;
796 if ( $::form->{cv}->{id} ) {
797 if ( $self->is_vendor() ) {
798 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
800 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
803 $self->{cv} = $self->_new_customer_vendor_object;
805 $self->{cv}->assign_attributes(%{$::form->{cv}});
807 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
808 $self->{cv}->taxincluded_checked(undef);
811 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
813 if ( $::form->{note}->{id} ) {
814 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
815 $self->{note_followup} = $self->{note}->follow_up;
816 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
818 $self->{note} = SL::DB::Note->new();
819 $self->{note_followup} = SL::DB::FollowUp->new();
820 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
823 $self->{note}->assign_attributes(%{$::form->{note}});
824 $self->{note}->created_by($curr_employee->id);
825 $self->{note}->trans_module('ct');
827 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
828 $self->{note_followup}->note($self->{note});
829 $self->{note_followup}->created_by($curr_employee->id);
831 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
832 $self->{note_followup_link}->trans_info($self->{cv}->name);
834 if ( $::form->{shipto}->{shipto_id} ) {
835 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
837 $self->{shipto} = SL::DB::Shipto->new();
839 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
840 $self->{shipto}->module('CT');
842 if ( $::form->{contact}->{cp_id} ) {
843 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
845 $self->{contact} = $self->_new_contact_object;
847 $self->{contact}->assign_attributes(%{$::form->{contact}});
849 $self->_copy_form_to_cvars(target => $self->{cv}, source => $::form->{cv_cvars});
850 $self->_copy_form_to_cvars(target => $self->{contact}, source => $::form->{contact_cvars});
851 $self->_copy_form_to_cvars(target => $self->{shipto}, source => $::form->{shipto_cvars});
854 sub _load_customer_vendor {
857 if ( $self->is_vendor() ) {
858 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
860 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
863 if ( $::form->{note_id} ) {
864 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
865 $self->{note_followup} = $self->{note}->follow_up;
866 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
868 $self->{note} = SL::DB::Note->new();
869 $self->{note_followup} = SL::DB::FollowUp->new();
870 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
873 if ( $::form->{shipto_id} ) {
874 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
876 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
877 die($::locale->text('Error'));
880 $self->{shipto} = SL::DB::Shipto->new();
883 if ( $::form->{contact_id} ) {
884 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
886 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
887 die($::locale->text('Error'));
890 $self->{contact} = $self->_new_contact_object;
894 sub _may_access_action {
895 my ($self, $action) = @_;
897 my $is_new = !$self->{cv} || !$self->{cv}->id;
898 my $is_own_customer = !$is_new
899 && $self->{cv}->is_customer
900 && (SL::DB::Manager::Employee->current->id == $self->{cv}->salesman_id);
901 my $has_edit_rights = $::auth->assert('customer_vendor_all_edit', 1);
902 $has_edit_rights ||= $::auth->assert('customer_vendor_edit', 1) && ($is_new || $is_own_customer);
903 my $needs_edit_rights = $action =~ m{^(?:add|save|delete|update)};
905 $self->user_has_edit_rights($has_edit_rights);
907 return 1 if $has_edit_rights;
908 return 0 if $needs_edit_rights;
913 my ($self, $action) = @_;
915 if (!$self->_may_access_action($action)) {
916 $::auth->deny_access;
920 sub _create_customer_vendor {
923 $self->{cv} = $self->_new_customer_vendor_object;
924 $self->{cv}->currency_id($::instance_conf->get_currency_id());
926 $self->{note} = SL::DB::Note->new();
928 $self->{note_followup} = SL::DB::FollowUp->new();
930 $self->{shipto} = SL::DB::Shipto->new();
932 $self->{contact} = $self->_new_contact_object;
938 my $dbh = $::form->get_standard_dbh();
942 $self->{all_business} = SL::DB::Manager::Business->get_all();
944 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
946 $self->{all_greetings} = SL::DB::Manager::Greeting->get_all_sorted();
947 if ($self->{cv}->id && $self->{cv}->greeting && !grep {$self->{cv}->greeting eq $_->description} @{$self->{all_greetings}}) {
948 unshift @{$self->{all_greetings}}, (SL::DB::Greeting->new(description => $self->{cv}->greeting));
951 $self->{all_contact_titles} = SL::DB::Manager::ContactTitle->get_all_sorted();
952 foreach my $contact (@{ $self->{cv}->contacts }) {
953 if ($contact->cp_title && !grep {$contact->cp_title eq $_->description} @{$self->{all_contact_titles}}) {
954 unshift @{$self->{all_contact_titles}}, (SL::DB::ContactTitle->new(description => $contact->cp_title));
958 $self->{all_contact_departments} = SL::DB::Manager::ContactDepartment->get_all_sorted();
959 foreach my $contact (@{ $self->{cv}->contacts }) {
960 if ($contact->cp_abteilung && !grep {$contact->cp_abteilung eq $_->description} @{$self->{all_contact_departments}}) {
961 unshift @{$self->{all_contact_departments}}, (SL::DB::ContactDepartment->new(description => $contact->cp_abteilung));
965 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
967 $self->{all_languages} = SL::DB::Manager::Language->get_all();
969 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
971 if ( $::instance_conf->get_vertreter() ) {
979 selectall_hashref_query($::form, $dbh, $query)
983 if ( $business_ids->[0] ) {
984 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
986 $self->{all_salesman_customers} = [];
989 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
992 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->{cv}->payment_id,
995 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
997 if ($self->{cv}->is_customer) {
998 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
1001 $self->{contacts} = $self->{cv}->contacts;
1002 $self->{contacts} ||= [];
1004 $self->{shiptos} = $self->{cv}->shipto;
1005 $self->{shiptos} ||= [];
1007 $self->{notes} = SL::DB::Manager::Note->get_all(
1009 trans_id => $self->{cv}->id,
1010 trans_module => 'ct',
1012 with_objects => ['follow_up'],
1015 if ( $self->is_vendor()) {
1016 $self->{open_items} = SL::DB::Manager::PurchaseInvoice->get_all_count(
1018 vendor_id => $self->{cv}->id,
1019 paid => {lt_sql => 'amount'},
1023 $self->{open_items} = SL::DB::Manager::Invoice->get_all_count(
1025 customer_id => $self->{cv}->id,
1026 paid => {lt_sql => 'amount'},
1031 if ( $self->is_vendor() ) {
1032 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
1034 vendor_id => $self->{cv}->id,
1039 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
1041 customer_id => $self->{cv}->id,
1047 if ($self->{cv}->number && $::instance_conf->get_webdav) {
1048 my $webdav = SL::Webdav->new(
1049 type => $self->is_customer ? 'customer'
1050 : $self->is_vendor ? 'vendor'
1052 number => $self->{cv}->number,
1054 my @all_objects = $webdav->get_all_objects;
1055 @{ $self->{template_args}->{WEBDAV} } = map { { name => $_->filename,
1057 link => File::Spec->catfile($_->full_filedescriptor),
1061 $self->{template_args} ||= {};
1063 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
1064 $::request->{layout}->add_javascripts('kivi.File.js');
1065 $::request->{layout}->add_javascripts('kivi.CustomerVendorTurnover.js');
1067 $self->_setup_form_action_bar;
1070 sub _setup_form_action_bar {
1073 my $no_rights = $self->user_has_edit_rights ? undef
1074 : $self->{cv}->is_customer ? t8("You don't have the rights to edit this customer.")
1075 : t8("You don't have the rights to edit this vendor.");
1077 for my $bar ($::request->layout->get('actionbar')) {
1082 submit => [ '#form', { action => "CustomerVendor/save" } ],
1083 checks => [ 'check_taxzone_and_ustid' ],
1084 accesskey => 'enter',
1085 disabled => $no_rights,
1088 t8('Save and Close'),
1089 submit => [ '#form', { action => "CustomerVendor/save_and_close" } ],
1090 checks => [ 'check_taxzone_and_ustid' ],
1091 disabled => $no_rights,
1093 ], # end of combobox "Save"
1096 action => [ t8('Workflow') ],
1098 t8('Save and AP Transaction'),
1099 submit => [ '#form', { action => "CustomerVendor/save_and_ap_transaction" } ],
1100 checks => [ 'check_taxzone_and_ustid' ],
1101 disabled => $no_rights,
1102 ]) x !!$self->is_vendor,
1104 t8('Save and AR Transaction'),
1105 submit => [ '#form', { action => "CustomerVendor/save_and_ar_transaction" } ],
1106 checks => [ 'check_taxzone_and_ustid' ],
1107 disabled => $no_rights,
1108 ]) x !$self->is_vendor,
1110 t8('Save and Invoice'),
1111 submit => [ '#form', { action => "CustomerVendor/save_and_invoice" } ],
1112 checks => [ 'check_taxzone_and_ustid' ],
1113 disabled => $no_rights,
1116 t8('Save and Order'),
1117 submit => [ '#form', { action => "CustomerVendor/save_and_order" } ],
1118 checks => [ 'check_taxzone_and_ustid' ],
1119 disabled => $no_rights,
1123 submit => [ '#form', { action => "CustomerVendor/save_and_rfq" } ],
1124 checks => [ 'check_taxzone_and_ustid' ],
1125 disabled => $no_rights,
1126 ]) x !!$self->is_vendor,
1128 t8('Save and Quotation'),
1129 submit => [ '#form', { action => "CustomerVendor/save_and_quotation" } ],
1130 checks => [ 'check_taxzone_and_ustid' ],
1131 disabled => $no_rights,
1132 ]) x !$self->is_vendor,
1133 ], # end of combobox "Workflow"
1137 submit => [ '#form', { action => "CustomerVendor/delete" } ],
1138 confirm => t8('Do you really want to delete this object?'),
1139 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.')
1140 : !$self->is_orphaned ? t8('This object has already been used.')
1148 call => [ 'kivi.CustomerVendor.showHistoryWindow', $self->{cv}->id ],
1149 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.') : undef,
1155 sub _prepare_cvar_configs_for_ajaj {
1156 my ($self, $cvars) = @_;
1161 my $result = { type => $cvar->config->type };
1163 if ($cvar->config->type eq 'number') {
1164 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
1166 } elsif ($result->{type} eq 'date') {
1167 $result->{value} = $cvar->value ? $cvar->value->to_kivitendo : undef;
1169 } elsif ($result->{type} =~ m{customer|vendor|part}) {
1170 my $object = $cvar->value;
1171 my $method = $result->{type} eq 'part' ? 'description' : 'name';
1173 $result->{id} = int($cvar->number_value) || undef;
1174 $result->{value} = $object ? $object->$method // '' : '';
1177 $result->{value} = $cvar->value;
1180 ( $cvar->config->name => $result )
1182 } grep { $_->is_valid } @{ $cvars }
1186 sub normalize_name {
1189 # check if feature is enabled (select normalize_vc_names from defaults)
1190 return unless ($::instance_conf->get_normalize_vc_names);
1192 return unless $self->{cv};
1193 my $name = $self->{cv}->name;
1197 $self->{cv}->name($name);
1200 sub home_address_for_google_maps {
1203 my $address = $::instance_conf->get_address // '';
1204 $address =~ s{^\s+|\s+$|\r+}{}g;
1205 $address =~ s{\n+}{,}g;
1206 $address =~ s{\s+}{ }g;
1211 sub init_customer_models {
1214 SL::Controller::Helper::GetModels->new(
1215 controller => $self,
1216 model => 'Customer',
1219 by => 'customernumber',
1222 customernumber => t8('Customer Number'),
1227 sub init_vendor_models {
1230 SL::Controller::Helper::GetModels->new(
1231 controller => $self,
1235 by => 'vendornumber',
1238 vendornumber => t8('Vendor Number'),
1243 sub init_zugferd_settings {
1245 [ -1, t8('Use settings from client configuration') ],
1246 @SL::ZUGFeRD::customer_settings,
1250 sub _new_customer_vendor_object {
1253 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
1257 custom_variables => [],
1261 sub _new_contact_object {
1264 return SL::DB::Contact->new(custom_variables => []);