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;
21 use SL::DB::PaymentTerm;
22 use SL::DB::Pricegroup;
25 use SL::DB::FollowUpLink;
29 use Rose::Object::MakeMethods::Generic (
30 'scalar --get_set_init' => [ qw(customer_models vendor_models) ],
34 __PACKAGE__->run_before(
36 $::auth->assert('customer_vendor_edit');
39 __PACKAGE__->run_before(
43 'save_and_ap_transaction',
44 'save_and_ar_transaction',
56 __PACKAGE__->run_before(
57 '_load_customer_vendor',
67 # make sure this comes after _load_customer_vendor
68 __PACKAGE__->run_before(
69 '_check_customer_vendor_all_edit',
76 'save_and_ap_transaction',
77 'save_and_ar_transaction',
89 __PACKAGE__->run_before(
90 '_create_customer_vendor',
96 __PACKAGE__->run_before('normalize_name');
102 $self->_pre_render();
103 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer;
106 'customer_vendor/form',
107 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
108 %{$self->{template_args}}
115 $self->_pre_render();
117 'customer_vendor/form',
118 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
119 %{$self->{template_args}}
126 if ($::request->type eq 'json') {
131 $cv_hash = $self->{cv}->as_tree;
132 $cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
135 $self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
142 my @errors = $self->{cv}->validate;
144 flash('error', @errors);
145 $self->_pre_render();
147 'customer_vendor/form',
148 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
149 %{$self->{template_args}}
151 $::dispatcher->end_request;
154 my $db = $self->{cv}->db;
156 $db->with_transaction(sub {
158 if ( $self->is_vendor() ) {
159 if ( $self->{cv}->vendornumber ) {
160 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
163 if ( $self->{cv}->customernumber ) {
164 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
168 foreach my $entry (@{$cvs_by_nr}) {
169 if( $entry->id != $self->{cv}->id ) {
171 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
173 $::form->error($msg);
177 $self->{cv}->save(cascade => 1);
179 $self->{contact}->cp_cv_id($self->{cv}->id);
180 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
181 $self->{contact}->save(cascade => 1);
184 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
186 if ( !$self->{note_followup}->follow_up_date ) {
187 $::form->error($::locale->text('Date missing!'));
190 $self->{note}->trans_id($self->{cv}->id);
191 $self->{note}->save();
193 $self->{note_followup}->save();
195 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
196 $self->{note_followup_link}->trans_id($self->{cv}->id);
197 $self->{note_followup_link}->save();
199 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
202 $self->{shipto}->trans_id($self->{cv}->id);
203 if(any { $self->{shipto}->$_ ne '' } qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax shiptoemail)) {
204 $self->{shipto}->save(cascade => 1);
207 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
208 SL::DB::History->new(
209 trans_id => $self->{cv}->id,
210 snumbers => $snumbers,
211 employee_id => SL::DB::Manager::Employee->current->id,
215 if ( $::form->{delete_notes} ) {
216 foreach my $note_id (@{ $::form->{delete_notes} }) {
217 my $note = SL::DB::Note->new(id => $note_id)->load();
218 if ( $note->follow_up ) {
219 if ( $note->follow_up->follow_up_link ) {
220 $note->follow_up->follow_up_link->delete(cascade => 'delete');
222 $note->follow_up->delete(cascade => 'delete');
224 $note->delete(cascade => 'delete');
229 }) || die($db->error);
238 my @redirect_params = (
240 id => $self->{cv}->id,
241 db => ($self->is_vendor() ? 'vendor' : 'customer'),
244 if ( $self->{contact}->cp_id ) {
245 push(@redirect_params, contact_id => $self->{contact}->cp_id);
248 if ( $self->{shipto}->shipto_id ) {
249 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
252 $self->redirect_to(@redirect_params);
255 sub action_save_and_close {
260 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
261 $::form->redirect($msg);
265 my ($self, $script) = @_;
267 $::auth->assert('gl_transactions | ap_transactions | ar_transactions'.
268 '| invoice_edit | vendor_invoice_edit | ' .
269 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
273 my $name = $::form->escape($self->{cv}->name, 1);
274 my $db = $self->is_vendor() ? 'vendor' : 'customer';
277 if ($::instance_conf->get_feature_experimental && 'oe.pl' eq $script) {
278 $script = 'controller.pl';
279 $action = 'Order/' . $action;
282 my $url = $self->url_for(
283 controller => $script,
286 $db .'_id' => $self->{cv}->id,
288 type => $::form->{type},
289 callback => $::form->{callback},
292 print $::form->redirect_header($url);
295 sub action_save_and_ar_transaction {
298 $main::auth->assert('ar_transactions');
300 $self->_transaction('ar.pl');
303 sub action_save_and_ap_transaction {
306 $main::auth->assert('ap_transactions');
308 $self->_transaction('ap.pl');
311 sub action_save_and_invoice {
314 if ( $self->is_vendor() ) {
315 $::auth->assert('vendor_invoice_edit');
317 $::auth->assert('invoice_edit');
320 $::form->{type} = 'invoice';
321 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
324 sub action_save_and_order {
327 if ( $self->is_vendor() ) {
328 $::auth->assert('purchase_order_edit');
330 $::auth->assert('sales_order_edit');
333 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
334 $self->_transaction('oe.pl');
337 sub action_save_and_rfq {
340 $::auth->assert('request_quotation_edit');
342 $::form->{type} = 'request_quotation';
343 $self->_transaction('oe.pl');
346 sub action_save_and_quotation {
349 $::auth->assert('sales_quotation_edit');
351 $::form->{type} = 'sales_quotation';
352 $self->_transaction('oe.pl');
358 my $db = $self->{cv}->db;
360 if( !$self->is_orphaned() ) {
361 $self->action_edit();
364 $db->with_transaction(sub {
365 $self->{cv}->delete(cascade => 1);
367 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
368 SL::DB::History->new(
369 trans_id => $self->{cv}->id,
370 snumbers => $snumbers,
371 employee_id => SL::DB::Manager::Employee->current->id,
372 addition => 'DELETED',
374 }) || die($db->error);
376 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
377 $::form->redirect($msg);
383 sub action_delete_contact {
386 my $db = $self->{contact}->db;
388 if ( !$self->{contact}->cp_id ) {
389 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
392 $db->with_transaction(sub {
393 if ( $self->{contact}->used ) {
394 $self->{contact}->detach();
395 $self->{contact}->save();
396 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
398 $self->{contact}->delete(cascade => 1);
399 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
403 }) || die($db->error);
405 $self->{contact} = $self->_new_contact_object;
408 $self->action_edit();
411 sub action_delete_shipto {
414 my $db = $self->{shipto}->db;
416 if ( !$self->{shipto}->shipto_id ) {
417 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
420 $db->with_transaction(sub {
421 if ( $self->{shipto}->used ) {
422 $self->{shipto}->detach();
423 $self->{shipto}->save(cascade => 1);
424 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
426 $self->{shipto}->delete(cascade => 1);
427 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
431 }) || die($db->error);
433 $self->{shipto} = SL::DB::Shipto->new();
436 $self->action_edit();
444 controller => 'ct.pl',
446 db => $self->is_vendor() ? 'vendor' : 'customer',
449 if ( $::form->{callback} ) {
450 push(@url_params, callback => $::form->{callback});
453 $self->redirect_to(@url_params);
457 sub action_search_contact {
460 my $url = 'ct.pl?action=search_contact&db=customer';
462 if ( $::form->{callback} ) {
463 $url .= '&callback='. $::form->escape($::form->{callback});
466 print $::form->redirect_header($url);
470 sub action_get_delivery {
473 $::auth->assert('sales_all_edit');
475 my $dbh = $::form->get_standard_dbh();
477 my ($arap, $db, $qty_sign);
478 if ( $self->is_vendor() ) {
481 $qty_sign = ' * -1 AS qty';
488 my $where = ' WHERE 1=1';
491 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
492 $where .= " AND ${arap}.shipto_id = ?";
493 push(@values, $::form->{shipto_id});
495 $where .= " AND ${arap}.${db}_id = ?";
496 push(@values, $::form->{id});
499 if ( $::form->{delivery_from} ) {
500 $where .= " AND ${arap}.transdate >= ?";
501 push(@values, conv_date($::form->{delivery_from}));
504 if ( $::form->{delivery_to} ) {
505 $where .= " AND ${arap}.transdate <= ?";
506 push(@values, conv_date($::form->{delivery_to}));
525 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
528 ON ${arap}.id = i.trans_id
534 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
535 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
538 ORDER BY ${arap}.transdate DESC LIMIT 15";
540 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
542 $self->render('customer_vendor/get_delivery', { layout => 0 });
545 sub action_ajaj_get_shipto {
552 my $name = 'shipto'. $_;
553 $name => $self->{shipto}->$name;
555 qw(_id name department_1 department_2 street zipcode city gln country contact phone fax email)
559 $data->{shipto_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{shipto}->cvars_by_config);
561 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
564 sub action_ajaj_get_contact {
572 my $name = 'cp_'. $_;
574 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
575 $name => $self->{contact}->$name->to_lxoffice;
577 $name => $self->{contact}->$name;
581 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
582 satphone satfax project street zipcode city privatphone privatemail birthday
587 $data->{contact_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{contact}->cvars_by_config);
589 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
592 sub action_ajaj_autocomplete {
593 my ($self, %params) = @_;
595 my ($model, $manager, $number, $matches);
597 # first see if this is customer or vendor picking
598 if ($::form->{type} eq 'customer') {
599 $model = $self->customer_models;
600 $manager = 'SL::DB::Manager::Customer';
601 $number = 'customernumber';
602 } elsif ($::form->{type} eq 'vendor') {
603 $model = $self->vendor_models;
604 $manager = 'SL::DB::Manager::Vendor';
605 $number = 'vendornumber';
607 die "unknown type $::form->{type}";
610 # if someone types something, and hits enter, assume he entered the full name.
611 # if something matches, treat that as sole match
612 # unfortunately get_models can't do more than one per package atm, so we d it
613 # the oldfashioned way.
614 if ($::form->{prefer_exact}) {
616 if (1 == scalar @{ $exact_matches = $manager->get_all(
619 (salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
621 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
622 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
627 $matches = $exact_matches;
631 $matches //= $model->get;
635 value => $_->displayable_name,
636 label => $_->displayable_name,
638 $number => $_->$number,
640 type => $::form->{type},
641 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
645 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
648 sub action_test_page {
649 $_[0]->render('customer_vendor/test_page');
653 return $::form->{db} eq 'vendor';
657 return $::form->{db} eq 'customer';
663 if ( defined($self->{_is_orphaned}) ) {
664 return $self->{_is_orphaned};
667 my $arap = $self->is_vendor ? 'ap' : 'ar';
670 my $cv = $self->is_vendor ? 'vendor' : 'customer';
674 FROM '. $arap .' AS a
675 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
682 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
688 FROM delivery_orders a
689 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
693 if ( $self->is_vendor ) {
696 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
700 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
702 return $self->{_is_orphaned} = !$dummy;
705 sub _copy_form_to_cvars {
706 my ($self, %params) = @_;
708 foreach my $cvar (@{ $params{target}->cvars_by_config }) {
709 my $value = $params{source}->{$cvar->config->name};
710 $value = $::form->parse_amount(\%::myconfig, $value) if $cvar->config->type eq 'number';
712 $cvar->value($value);
716 sub _instantiate_args {
719 my $curr_employee = SL::DB::Manager::Employee->current;
721 if ( $::form->{cv}->{id} ) {
722 if ( $self->is_vendor() ) {
723 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
725 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
728 $self->{cv} = $self->_new_customer_vendor_object;
730 $self->{cv}->assign_attributes(%{$::form->{cv}});
732 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
733 $self->{cv}->taxincluded_checked(undef);
736 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
738 if ( $::form->{note}->{id} ) {
739 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
740 $self->{note_followup} = $self->{note}->follow_up;
741 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
743 $self->{note} = SL::DB::Note->new();
744 $self->{note_followup} = SL::DB::FollowUp->new();
745 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
748 $self->{note}->assign_attributes(%{$::form->{note}});
749 $self->{note}->created_by($curr_employee->id);
750 $self->{note}->trans_module('ct');
752 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
753 $self->{note_followup}->note($self->{note});
754 $self->{note_followup}->created_by($curr_employee->id);
756 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
757 $self->{note_followup_link}->trans_info($self->{cv}->name);
759 if ( $::form->{shipto}->{shipto_id} ) {
760 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
762 $self->{shipto} = SL::DB::Shipto->new();
764 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
765 $self->{shipto}->module('CT');
767 if ( $::form->{contact}->{cp_id} ) {
768 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
770 $self->{contact} = $self->_new_contact_object;
772 $self->{contact}->assign_attributes(%{$::form->{contact}});
774 $self->_copy_form_to_cvars(target => $self->{cv}, source => $::form->{cv_cvars});
775 $self->_copy_form_to_cvars(target => $self->{contact}, source => $::form->{contact_cvars});
776 $self->_copy_form_to_cvars(target => $self->{shipto}, source => $::form->{shipto_cvars});
779 sub _load_customer_vendor {
782 if ( $self->is_vendor() ) {
783 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
785 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
788 if ( $::form->{note_id} ) {
789 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
790 $self->{note_followup} = $self->{note}->follow_up;
791 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
793 $self->{note} = SL::DB::Note->new();
794 $self->{note_followup} = SL::DB::FollowUp->new();
795 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
798 if ( $::form->{shipto_id} ) {
799 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
801 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
802 die($::locale->text('Error'));
805 $self->{shipto} = SL::DB::Shipto->new();
808 if ( $::form->{contact_id} ) {
809 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
811 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
812 die($::locale->text('Error'));
815 $self->{contact} = $self->_new_contact_object;
819 sub _check_customer_vendor_all_edit {
822 unless ($::auth->assert('customer_vendor_all_edit', 1)) {
823 die($::locale->text("You don't have the rights to edit this customer.") . "\n")
824 if $self->{cv}->is_customer and
825 SL::DB::Manager::Employee->current->id != $self->{cv}->salesman_id;
829 sub _create_customer_vendor {
832 $self->{cv} = $self->_new_customer_vendor_object;
833 $self->{cv}->currency_id($::instance_conf->get_currency_id());
835 $self->{note} = SL::DB::Note->new();
837 $self->{note_followup} = SL::DB::FollowUp->new();
839 $self->{shipto} = SL::DB::Shipto->new();
841 $self->{contact} = $self->_new_contact_object;
847 my $dbh = $::form->get_standard_dbh();
851 $self->{all_business} = SL::DB::Manager::Business->get_all();
853 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
856 'SELECT DISTINCT(greeting)
858 WHERE greeting IS NOT NULL AND greeting != \'\'
860 SELECT DISTINCT(greeting)
862 WHERE greeting IS NOT NULL AND greeting != \'\'
864 $self->{all_greetings} = [
867 selectall_hashref_query($::form, $dbh, $query)
872 'SELECT DISTINCT(cp_title) AS title
874 WHERE cp_title IS NOT NULL AND cp_title != \'\'
876 $self->{all_titles} = [
879 selectall_hashref_query($::form, $dbh, $query)
883 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
885 $self->{all_languages} = SL::DB::Manager::Language->get_all();
887 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
889 if ( $::instance_conf->get_vertreter() ) {
897 selectall_hashref_query($::form, $dbh, $query)
901 if ( $business_ids->[0] ) {
902 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
904 $self->{all_salesman_customers} = [];
907 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
910 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->{cv}->payment_id,
913 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
915 if ($self->{cv}->is_customer) {
916 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
920 'SELECT DISTINCT(cp_abteilung) AS department
922 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
923 ORDER BY cp_abteilung';
924 $self->{all_departments} = [
926 { $_->{department}; }
927 selectall_hashref_query($::form, $dbh, $query)
931 $self->{contacts} = $self->{cv}->contacts;
932 $self->{contacts} ||= [];
934 $self->{shiptos} = $self->{cv}->shipto;
935 $self->{shiptos} ||= [];
937 $self->{notes} = SL::DB::Manager::Note->get_all(
939 trans_id => $self->{cv}->id,
940 trans_module => 'ct',
942 with_objects => ['follow_up'],
945 $self->{template_args} ||= {};
947 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
948 $::request->{layout}->add_javascripts('kivi.File.js');
950 $self->_setup_form_action_bar;
953 sub _setup_form_action_bar {
956 for my $bar ($::request->layout->get('actionbar')) {
961 submit => [ '#form', { action => "CustomerVendor/save" } ],
962 checks => [ 'check_taxzone_and_ustid' ],
963 accesskey => 'enter',
966 t8('Save and Close'),
967 submit => [ '#form', { action => "CustomerVendor/save_and_close" } ],
968 checks => [ 'check_taxzone_and_ustid' ],
970 ], # end of combobox "Save"
973 action => [ t8('Workflow') ],
975 t8('Save and AP Transaction'),
976 submit => [ '#form', { action => "CustomerVendor/save_and_ap_transaction" } ],
977 checks => [ 'check_taxzone_and_ustid' ],
978 ]) x !!$self->is_vendor,
980 t8('Save and AR Transaction'),
981 submit => [ '#form', { action => "CustomerVendor/save_and_ar_transaction" } ],
982 checks => [ 'check_taxzone_and_ustid' ],
983 ]) x !$self->is_vendor,
985 t8('Save and Invoice'),
986 submit => [ '#form', { action => "CustomerVendor/save_and_invoice" } ],
987 checks => [ 'check_taxzone_and_ustid' ],
990 t8('Save and Order'),
991 submit => [ '#form', { action => "CustomerVendor/save_and_order" } ],
992 checks => [ 'check_taxzone_and_ustid' ],
996 submit => [ '#form', { action => "CustomerVendor/save_and_rfq" } ],
997 checks => [ 'check_taxzone_and_ustid' ],
998 ]) x !!$self->is_vendor,
1000 t8('Save and Quotation'),
1001 submit => [ '#form', { action => "CustomerVendor/save_and_quotation" } ],
1002 checks => [ 'check_taxzone_and_ustid' ],
1003 ]) x !$self->is_vendor,
1004 ], # end of combobox "Workflow"
1008 submit => [ '#form', { action => "CustomerVendor/delete" } ],
1009 confirm => t8('Do you really want to delete this object?'),
1010 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.')
1011 : !$self->is_orphaned ? t8('This object has already been used.')
1019 call => [ 'kivi.CustomerVendor.showHistoryWindow', $self->{cv}->id ],
1020 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.') : undef,
1026 sub _prepare_cvar_configs_for_ajaj {
1027 my ($self, $cvars) = @_;
1032 my $result = { type => $cvar->config->type };
1034 if ($cvar->config->type eq 'number') {
1035 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
1037 } elsif ($result->{type} eq 'date') {
1038 $result->{value} = $cvar->value ? $cvar->value->to_kivitendo : undef;
1040 } elsif ($result->{type} =~ m{customer|vendor|part}) {
1041 my $object = $cvar->value;
1042 my $method = $result->{type} eq 'part' ? 'description' : 'name';
1044 $result->{id} = int($cvar->number_value) || undef;
1045 $result->{value} = $object ? $object->$method // '' : '';
1048 $result->{value} = $cvar->value;
1051 ( $cvar->config->name => $result )
1053 } grep { $_->is_valid } @{ $cvars }
1057 sub normalize_name {
1060 # check if feature is enabled (select normalize_vc_names from defaults)
1061 return unless ($::instance_conf->get_normalize_vc_names);
1063 return unless $self->{cv};
1064 my $name = $self->{cv}->name;
1068 $self->{cv}->name($name);
1071 sub home_address_for_google_maps {
1074 my $address = $::instance_conf->get_address // '';
1075 $address =~ s{^\s+|\s+$|\r+}{}g;
1076 $address =~ s{\n+}{,}g;
1077 $address =~ s{\s+}{ }g;
1082 sub init_customer_models {
1085 SL::Controller::Helper::GetModels->new(
1086 controller => $self,
1087 model => 'Customer',
1090 by => 'customernumber',
1093 customernumber => t8('Customer Number'),
1096 ( salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
1101 sub init_vendor_models {
1104 SL::Controller::Helper::GetModels->new(
1105 controller => $self,
1109 by => 'vendornumber',
1112 vendornumber => t8('Vendor Number'),
1117 sub _new_customer_vendor_object {
1120 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
1124 custom_variables => [],
1128 sub _new_contact_object {
1131 return SL::DB::Contact->new(custom_variables => []);