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::Controller::Helper::GetModels;
13 use SL::Controller::Helper::ReportGenerator;
14 use SL::Controller::Helper::ParseFilter;
23 use SL::DB::PaymentTerm;
24 use SL::DB::Pricegroup;
28 use SL::DB::FollowUpLink;
32 use SL::DB::PurchaseInvoice;
37 use Rose::Object::MakeMethods::Generic (
38 'scalar --get_set_init' => [ qw(customer_models vendor_models) ],
42 __PACKAGE__->run_before(
44 $::auth->assert('customer_vendor_edit');
46 except => [ qw(ajaj_autocomplete) ],
48 __PACKAGE__->run_before(
52 'save_and_ap_transaction',
53 'save_and_ar_transaction',
65 __PACKAGE__->run_before(
66 '_load_customer_vendor',
77 # make sure this comes after _load_customer_vendor
78 __PACKAGE__->run_before(
79 '_check_customer_vendor_all_edit',
86 'save_and_ap_transaction',
87 'save_and_ar_transaction',
99 __PACKAGE__->run_before(
100 '_create_customer_vendor',
106 __PACKAGE__->run_before('normalize_name');
112 $self->_pre_render();
113 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer;
116 'customer_vendor/form',
117 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
118 %{$self->{template_args}}
125 $self->_pre_render();
127 'customer_vendor/form',
128 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
129 %{$self->{template_args}}
136 if ($::request->type eq 'json') {
141 $cv_hash = $self->{cv}->as_tree;
142 $cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
145 $self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
152 my @errors = $self->{cv}->validate;
154 flash('error', @errors);
155 $self->_pre_render();
157 'customer_vendor/form',
158 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
159 %{$self->{template_args}}
161 $::dispatcher->end_request;
164 my $db = $self->{cv}->db;
166 $db->with_transaction(sub {
168 if ( $self->is_vendor() ) {
169 if ( $self->{cv}->vendornumber ) {
170 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
173 if ( $self->{cv}->customernumber ) {
174 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
178 foreach my $entry (@{$cvs_by_nr}) {
179 if( $entry->id != $self->{cv}->id ) {
181 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
183 $::form->error($msg);
187 $self->{cv}->save(cascade => 1);
189 $self->{contact}->cp_cv_id($self->{cv}->id);
190 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
191 $self->{contact}->save(cascade => 1);
194 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
196 if ( !$self->{note_followup}->follow_up_date ) {
197 $::form->error($::locale->text('Date missing!'));
200 $self->{note}->trans_id($self->{cv}->id);
201 $self->{note}->save();
203 $self->{note_followup}->save();
205 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
206 $self->{note_followup_link}->trans_id($self->{cv}->id);
207 $self->{note_followup_link}->save();
209 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
212 $self->{shipto}->trans_id($self->{cv}->id);
213 if(any { $self->{shipto}->$_ ne '' } qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax shiptoemail)) {
214 $self->{shipto}->save(cascade => 1);
217 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
218 SL::DB::History->new(
219 trans_id => $self->{cv}->id,
220 snumbers => $snumbers,
221 employee_id => SL::DB::Manager::Employee->current->id,
225 if ( $::form->{delete_notes} ) {
226 foreach my $note_id (@{ $::form->{delete_notes} }) {
227 my $note = SL::DB::Note->new(id => $note_id)->load();
228 if ( $note->follow_up ) {
229 if ( $note->follow_up->follow_up_link ) {
230 $note->follow_up->follow_up_link->delete(cascade => 'delete');
232 $note->follow_up->delete(cascade => 'delete');
234 $note->delete(cascade => 'delete');
239 }) || die($db->error);
248 my @redirect_params = (
250 id => $self->{cv}->id,
251 db => ($self->is_vendor() ? 'vendor' : 'customer'),
254 if ( $self->{contact}->cp_id ) {
255 push(@redirect_params, contact_id => $self->{contact}->cp_id);
258 if ( $self->{shipto}->shipto_id ) {
259 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
262 $self->redirect_to(@redirect_params);
265 sub action_save_and_close {
270 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
271 $::form->redirect($msg);
275 my ($self, $script) = @_;
277 $::auth->assert('gl_transactions | ap_transactions | ar_transactions'.
278 '| invoice_edit | vendor_invoice_edit | ' .
279 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
283 my $name = $::form->escape($self->{cv}->name, 1);
284 my $db = $self->is_vendor() ? 'vendor' : 'customer';
287 if ($::instance_conf->get_feature_experimental_order && 'oe.pl' eq $script) {
288 $script = 'controller.pl';
289 $action = 'Order/' . $action;
292 my $url = $self->url_for(
293 controller => $script,
296 $db .'_id' => $self->{cv}->id,
298 type => $::form->{type},
299 callback => $::form->{callback},
302 print $::form->redirect_header($url);
305 sub action_save_and_ar_transaction {
308 $main::auth->assert('ar_transactions');
310 $self->_transaction('ar.pl');
313 sub action_save_and_ap_transaction {
316 $main::auth->assert('ap_transactions');
318 $self->_transaction('ap.pl');
321 sub action_save_and_invoice {
324 if ( $self->is_vendor() ) {
325 $::auth->assert('vendor_invoice_edit');
327 $::auth->assert('invoice_edit');
330 $::form->{type} = 'invoice';
331 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
334 sub action_save_and_order {
337 if ( $self->is_vendor() ) {
338 $::auth->assert('purchase_order_edit');
340 $::auth->assert('sales_order_edit');
343 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
344 $self->_transaction('oe.pl');
347 sub action_save_and_rfq {
350 $::auth->assert('request_quotation_edit');
352 $::form->{type} = 'request_quotation';
353 $self->_transaction('oe.pl');
356 sub action_save_and_quotation {
359 $::auth->assert('sales_quotation_edit');
361 $::form->{type} = 'sales_quotation';
362 $self->_transaction('oe.pl');
368 my $db = $self->{cv}->db;
370 if( !$self->is_orphaned() ) {
371 $self->action_edit();
374 $db->with_transaction(sub {
375 $self->{cv}->delete(cascade => 1);
377 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
378 SL::DB::History->new(
379 trans_id => $self->{cv}->id,
380 snumbers => $snumbers,
381 employee_id => SL::DB::Manager::Employee->current->id,
382 addition => 'DELETED',
384 }) || die($db->error);
386 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
387 $::form->redirect($msg);
393 sub action_delete_contact {
396 my $db = $self->{contact}->db;
398 if ( !$self->{contact}->cp_id ) {
399 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
402 $db->with_transaction(sub {
403 if ( $self->{contact}->used ) {
404 $self->{contact}->detach();
405 $self->{contact}->save();
406 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
408 $self->{contact}->delete(cascade => 1);
409 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
413 }) || die($db->error);
415 $self->{contact} = $self->_new_contact_object;
418 $self->action_edit();
421 sub action_delete_shipto {
424 my $db = $self->{shipto}->db;
426 if ( !$self->{shipto}->shipto_id ) {
427 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
430 $db->with_transaction(sub {
431 if ( $self->{shipto}->used ) {
432 $self->{shipto}->detach();
433 $self->{shipto}->save(cascade => 1);
434 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
436 $self->{shipto}->delete(cascade => 1);
437 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
441 }) || die($db->error);
443 $self->{shipto} = SL::DB::Shipto->new();
446 $self->action_edit();
454 controller => 'ct.pl',
456 db => $self->is_vendor() ? 'vendor' : 'customer',
459 if ( $::form->{callback} ) {
460 push(@url_params, callback => $::form->{callback});
463 $self->redirect_to(@url_params);
467 sub action_search_contact {
470 my $url = 'ct.pl?action=search_contact&db=customer';
472 if ( $::form->{callback} ) {
473 $url .= '&callback='. $::form->escape($::form->{callback});
476 print $::form->redirect_header($url);
479 sub action_get_delivery {
482 $::auth->assert('sales_all_edit') if $self->is_customer();
483 $::auth->assert('purchase_all_edit') if $self->is_vendor();
485 my $dbh = $::form->get_standard_dbh();
487 my ($arap, $db, $qty_sign);
488 if ( $self->is_vendor() ) {
491 $qty_sign = ' * -1 AS qty';
498 my $where = ' WHERE 1=1';
501 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
502 $where .= " AND ${arap}.shipto_id = ?";
503 push(@values, $::form->{shipto_id});
505 $where .= " AND ${arap}.${db}_id = ?";
506 push(@values, $::form->{id});
509 if ( $::form->{delivery_from} ) {
510 $where .= " AND ${arap}.transdate >= ?";
511 push(@values, conv_date($::form->{delivery_from}));
514 if ( $::form->{delivery_to} ) {
515 $where .= " AND ${arap}.transdate <= ?";
516 push(@values, conv_date($::form->{delivery_to}));
535 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
538 ON ${arap}.id = i.trans_id
544 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
545 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
548 ORDER BY ${arap}.transdate DESC LIMIT 15";
550 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
552 $self->render('customer_vendor/get_delivery', { layout => 0 });
555 sub action_ajaj_get_shipto {
562 my $name = 'shipto'. $_;
563 $name => $self->{shipto}->$name;
565 qw(_id name department_1 department_2 street zipcode city gln country contact phone fax email)
569 $data->{shipto_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{shipto}->cvars_by_config);
571 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
574 sub action_ajaj_get_contact {
582 my $name = 'cp_'. $_;
584 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
585 $name => $self->{contact}->$name->to_lxoffice;
587 $name => $self->{contact}->$name;
591 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
592 satphone satfax project street zipcode city privatphone privatemail birthday main
597 $data->{contact_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{contact}->cvars_by_config);
599 # avoid two or more main_cp
600 my $has_main_cp = grep { $_->cp_main == 1 } @{ $self->{cv}->contacts };
601 $data->{contact}->{disable_cp_main} = 1 if ($has_main_cp && !$data->{contact}->{cp_main});
603 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
606 sub action_ajaj_autocomplete {
607 my ($self, %params) = @_;
609 my ($model, $manager, $number, $matches);
611 # first see if this is customer or vendor picking
612 if ($::form->{type} eq 'customer') {
613 $model = $self->customer_models;
614 $manager = 'SL::DB::Manager::Customer';
615 $number = 'customernumber';
616 } elsif ($::form->{type} eq 'vendor') {
617 $model = $self->vendor_models;
618 $manager = 'SL::DB::Manager::Vendor';
619 $number = 'vendornumber';
621 die "unknown type $::form->{type}";
624 # if someone types something, and hits enter, assume he entered the full name.
625 # if something matches, treat that as the sole match
626 # unfortunately get_models can't do more than one per package atm, so we do it
627 # the oldfashioned way.
628 if ($::form->{prefer_exact}) {
630 if (1 == scalar @{ $exact_matches = $manager->get_all(
633 (salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
635 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
636 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
641 $matches = $exact_matches;
645 $matches //= $model->get;
649 value => $_->displayable_name,
650 label => $_->displayable_name,
652 $number => $_->$number,
654 type => $::form->{type},
655 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
659 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
662 sub action_test_page {
663 $_[0]->render('customer_vendor/test_page');
666 sub action_ajax_list_prices {
667 my ($self, %params) = @_;
669 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
670 my @columns = qw(partnumber description price);
671 my @visible = qw(partnumber description price);
672 my @sortable = qw(partnumber description price);
675 partnumber => { text => $::locale->text('Part Number'), sub => sub { $_[0]->parts->partnumber } },
676 description => { text => $::locale->text('Part Description'), sub => sub { $_[0]->parts->description } },
677 price => { text => $::locale->text('Price'), sub => sub { $::form->format_amount(\%::myconfig, $_[0]->price, 2) }, align => 'right' },
680 $::form->{sort_by} ||= 'partnumber';
681 $::form->{sort_dir} //= 1;
683 for my $col (@sortable) {
684 $column_defs{$col}{link} = $self->url_for(
685 action => 'ajax_list_prices',
686 callback => $::form->{callback},
688 id => $self->{cv}->id,
690 sort_dir => ($::form->{sort_by} eq $col ? 1 - $::form->{sort_dir} : $::form->{sort_dir})
694 map { $column_defs{$_}{visible} = 1 } @visible;
697 $pricegroup = $self->{cv}->pricegroup->pricegroup if $self->{cv}->pricegroup;
699 $report->set_columns(%column_defs);
700 $report->set_column_order(@columns);
701 $report->set_options(allow_pdf_export => 0, allow_csv_export => 0);
702 $report->set_sort_indicator($::form->{sort_by}, $::form->{sort_dir});
703 $report->set_export_options(@{ $params{report_generator_export_options} || [] });
704 $report->set_options(
705 %{ $params{report_generator_options} || {} },
706 output_format => 'HTML',
707 top_info_text => $::locale->text('Pricegroup') . ': ' . $pricegroup,
708 title => $::locale->text('Price List'),
711 my $sort_param = $::form->{sort_by} eq 'price' ? 'price' :
712 $::form->{sort_by} eq 'description' ? 'parts.description' :
714 $sort_param .= ' ' . ($::form->{sort_dir} ? 'ASC' : 'DESC');
715 my $prices = SL::DB::Manager::Price->get_all(where => [ pricegroup_id => $self->{cv}->pricegroup_id ],
716 sort_by => $sort_param,
717 with_objects => 'parts');
719 $self->report_generator_list_objects(report => $report, objects => $prices, layout => 0, header => 0);
723 return $::form->{db} eq 'vendor';
727 return $::form->{db} eq 'customer';
733 if ( defined($self->{_is_orphaned}) ) {
734 return $self->{_is_orphaned};
737 my $arap = $self->is_vendor ? 'ap' : 'ar';
740 my $cv = $self->is_vendor ? 'vendor' : 'customer';
744 FROM '. $arap .' AS a
745 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
752 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
758 FROM delivery_orders a
759 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
763 if ( $self->is_vendor ) {
766 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
770 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
772 return $self->{_is_orphaned} = !$dummy;
775 sub _copy_form_to_cvars {
776 my ($self, %params) = @_;
778 foreach my $cvar (@{ $params{target}->cvars_by_config }) {
779 my $value = $params{source}->{$cvar->config->name};
780 $value = $::form->parse_amount(\%::myconfig, $value) if $cvar->config->type eq 'number';
782 $cvar->value($value);
786 sub _instantiate_args {
789 my $curr_employee = SL::DB::Manager::Employee->current;
791 if ( $::form->{cv}->{id} ) {
792 if ( $self->is_vendor() ) {
793 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
795 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
798 $self->{cv} = $self->_new_customer_vendor_object;
800 $self->{cv}->assign_attributes(%{$::form->{cv}});
802 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
803 $self->{cv}->taxincluded_checked(undef);
806 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
808 if ( $::form->{note}->{id} ) {
809 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
810 $self->{note_followup} = $self->{note}->follow_up;
811 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
813 $self->{note} = SL::DB::Note->new();
814 $self->{note_followup} = SL::DB::FollowUp->new();
815 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
818 $self->{note}->assign_attributes(%{$::form->{note}});
819 $self->{note}->created_by($curr_employee->id);
820 $self->{note}->trans_module('ct');
822 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
823 $self->{note_followup}->note($self->{note});
824 $self->{note_followup}->created_by($curr_employee->id);
826 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
827 $self->{note_followup_link}->trans_info($self->{cv}->name);
829 if ( $::form->{shipto}->{shipto_id} ) {
830 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
832 $self->{shipto} = SL::DB::Shipto->new();
834 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
835 $self->{shipto}->module('CT');
837 if ( $::form->{contact}->{cp_id} ) {
838 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
840 $self->{contact} = $self->_new_contact_object;
842 $self->{contact}->assign_attributes(%{$::form->{contact}});
844 $self->_copy_form_to_cvars(target => $self->{cv}, source => $::form->{cv_cvars});
845 $self->_copy_form_to_cvars(target => $self->{contact}, source => $::form->{contact_cvars});
846 $self->_copy_form_to_cvars(target => $self->{shipto}, source => $::form->{shipto_cvars});
849 sub _load_customer_vendor {
852 if ( $self->is_vendor() ) {
853 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
855 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
858 if ( $::form->{note_id} ) {
859 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
860 $self->{note_followup} = $self->{note}->follow_up;
861 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
863 $self->{note} = SL::DB::Note->new();
864 $self->{note_followup} = SL::DB::FollowUp->new();
865 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
868 if ( $::form->{shipto_id} ) {
869 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
871 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
872 die($::locale->text('Error'));
875 $self->{shipto} = SL::DB::Shipto->new();
878 if ( $::form->{contact_id} ) {
879 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
881 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
882 die($::locale->text('Error'));
885 $self->{contact} = $self->_new_contact_object;
889 sub _check_customer_vendor_all_edit {
892 unless ($::auth->assert('customer_vendor_all_edit', 1)) {
893 die($::locale->text("You don't have the rights to edit this customer.") . "\n")
894 if $self->{cv}->is_customer and
895 SL::DB::Manager::Employee->current->id != $self->{cv}->salesman_id;
899 sub _create_customer_vendor {
902 $self->{cv} = $self->_new_customer_vendor_object;
903 $self->{cv}->currency_id($::instance_conf->get_currency_id());
905 $self->{note} = SL::DB::Note->new();
907 $self->{note_followup} = SL::DB::FollowUp->new();
909 $self->{shipto} = SL::DB::Shipto->new();
911 $self->{contact} = $self->_new_contact_object;
917 my $dbh = $::form->get_standard_dbh();
921 $self->{all_business} = SL::DB::Manager::Business->get_all();
923 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
926 'SELECT DISTINCT(greeting)
928 WHERE greeting IS NOT NULL AND greeting != \'\'
930 SELECT DISTINCT(greeting)
932 WHERE greeting IS NOT NULL AND greeting != \'\'
934 $self->{all_greetings} = [
937 selectall_hashref_query($::form, $dbh, $query)
942 'SELECT DISTINCT(cp_title) AS title
944 WHERE cp_title IS NOT NULL AND cp_title != \'\'
946 $self->{all_titles} = [
949 selectall_hashref_query($::form, $dbh, $query)
953 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
955 $self->{all_languages} = SL::DB::Manager::Language->get_all();
957 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
959 if ( $::instance_conf->get_vertreter() ) {
967 selectall_hashref_query($::form, $dbh, $query)
971 if ( $business_ids->[0] ) {
972 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
974 $self->{all_salesman_customers} = [];
977 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
980 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->{cv}->payment_id,
983 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
985 if ($self->{cv}->is_customer) {
986 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
990 'SELECT DISTINCT(cp_abteilung) AS department
992 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
993 ORDER BY cp_abteilung';
994 $self->{all_departments} = [
996 { $_->{department}; }
997 selectall_hashref_query($::form, $dbh, $query)
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,
1046 $self->{template_args} ||= {};
1048 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
1049 $::request->{layout}->add_javascripts('kivi.File.js');
1050 $::request->{layout}->add_javascripts('kivi.CustomerVendorTurnover.js');
1052 $self->_setup_form_action_bar;
1055 sub _setup_form_action_bar {
1058 for my $bar ($::request->layout->get('actionbar')) {
1063 submit => [ '#form', { action => "CustomerVendor/save" } ],
1064 checks => [ 'check_taxzone_and_ustid' ],
1065 accesskey => 'enter',
1068 t8('Save and Close'),
1069 submit => [ '#form', { action => "CustomerVendor/save_and_close" } ],
1070 checks => [ 'check_taxzone_and_ustid' ],
1072 ], # end of combobox "Save"
1075 action => [ t8('Workflow') ],
1077 t8('Save and AP Transaction'),
1078 submit => [ '#form', { action => "CustomerVendor/save_and_ap_transaction" } ],
1079 checks => [ 'check_taxzone_and_ustid' ],
1080 ]) x !!$self->is_vendor,
1082 t8('Save and AR Transaction'),
1083 submit => [ '#form', { action => "CustomerVendor/save_and_ar_transaction" } ],
1084 checks => [ 'check_taxzone_and_ustid' ],
1085 ]) x !$self->is_vendor,
1087 t8('Save and Invoice'),
1088 submit => [ '#form', { action => "CustomerVendor/save_and_invoice" } ],
1089 checks => [ 'check_taxzone_and_ustid' ],
1092 t8('Save and Order'),
1093 submit => [ '#form', { action => "CustomerVendor/save_and_order" } ],
1094 checks => [ 'check_taxzone_and_ustid' ],
1098 submit => [ '#form', { action => "CustomerVendor/save_and_rfq" } ],
1099 checks => [ 'check_taxzone_and_ustid' ],
1100 ]) x !!$self->is_vendor,
1102 t8('Save and Quotation'),
1103 submit => [ '#form', { action => "CustomerVendor/save_and_quotation" } ],
1104 checks => [ 'check_taxzone_and_ustid' ],
1105 ]) x !$self->is_vendor,
1106 ], # end of combobox "Workflow"
1110 submit => [ '#form', { action => "CustomerVendor/delete" } ],
1111 confirm => t8('Do you really want to delete this object?'),
1112 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.')
1113 : !$self->is_orphaned ? t8('This object has already been used.')
1121 call => [ 'kivi.CustomerVendor.showHistoryWindow', $self->{cv}->id ],
1122 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.') : undef,
1128 sub _prepare_cvar_configs_for_ajaj {
1129 my ($self, $cvars) = @_;
1134 my $result = { type => $cvar->config->type };
1136 if ($cvar->config->type eq 'number') {
1137 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
1139 } elsif ($result->{type} eq 'date') {
1140 $result->{value} = $cvar->value ? $cvar->value->to_kivitendo : undef;
1142 } elsif ($result->{type} =~ m{customer|vendor|part}) {
1143 my $object = $cvar->value;
1144 my $method = $result->{type} eq 'part' ? 'description' : 'name';
1146 $result->{id} = int($cvar->number_value) || undef;
1147 $result->{value} = $object ? $object->$method // '' : '';
1150 $result->{value} = $cvar->value;
1153 ( $cvar->config->name => $result )
1155 } grep { $_->is_valid } @{ $cvars }
1159 sub normalize_name {
1162 # check if feature is enabled (select normalize_vc_names from defaults)
1163 return unless ($::instance_conf->get_normalize_vc_names);
1165 return unless $self->{cv};
1166 my $name = $self->{cv}->name;
1170 $self->{cv}->name($name);
1173 sub home_address_for_google_maps {
1176 my $address = $::instance_conf->get_address // '';
1177 $address =~ s{^\s+|\s+$|\r+}{}g;
1178 $address =~ s{\n+}{,}g;
1179 $address =~ s{\s+}{ }g;
1184 sub init_customer_models {
1187 SL::Controller::Helper::GetModels->new(
1188 controller => $self,
1189 model => 'Customer',
1192 by => 'customernumber',
1195 customernumber => t8('Customer Number'),
1198 ( salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
1203 sub init_vendor_models {
1206 SL::Controller::Helper::GetModels->new(
1207 controller => $self,
1211 by => 'vendornumber',
1214 vendornumber => t8('Vendor Number'),
1219 sub _new_customer_vendor_object {
1222 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
1226 custom_variables => [],
1230 sub _new_contact_object {
1233 return SL::DB::Contact->new(custom_variables => []);