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';
276 my $url = $self->url_for(
277 controller => $script,
280 $db .'_id' => $self->{cv}->id,
282 type => $::form->{type},
283 callback => $::form->{callback},
286 print $::form->redirect_header($url);
289 sub action_save_and_ar_transaction {
292 $main::auth->assert('ar_transactions');
294 $self->_transaction('ar.pl');
297 sub action_save_and_ap_transaction {
300 $main::auth->assert('ap_transactions');
302 $self->_transaction('ap.pl');
305 sub action_save_and_invoice {
308 if ( $self->is_vendor() ) {
309 $::auth->assert('vendor_invoice_edit');
311 $::auth->assert('invoice_edit');
314 $::form->{type} = 'invoice';
315 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
318 sub action_save_and_order {
321 if ( $self->is_vendor() ) {
322 $::auth->assert('purchase_order_edit');
324 $::auth->assert('sales_order_edit');
327 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
328 $self->_transaction('oe.pl');
331 sub action_save_and_rfq {
334 $::auth->assert('request_quotation_edit');
336 $::form->{type} = 'request_quotation';
337 $self->_transaction('oe.pl');
340 sub action_save_and_quotation {
343 $::auth->assert('sales_quotation_edit');
345 $::form->{type} = 'sales_quotation';
346 $self->_transaction('oe.pl');
352 my $db = $self->{cv}->db;
354 if( !$self->is_orphaned() ) {
355 $self->action_edit();
358 $db->with_transaction(sub {
359 $self->{cv}->delete(cascade => 1);
361 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
362 SL::DB::History->new(
363 trans_id => $self->{cv}->id,
364 snumbers => $snumbers,
365 employee_id => SL::DB::Manager::Employee->current->id,
366 addition => 'DELETED',
368 }) || die($db->error);
370 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
371 $::form->redirect($msg);
377 sub action_delete_contact {
380 my $db = $self->{contact}->db;
382 if ( !$self->{contact}->cp_id ) {
383 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
386 $db->with_transaction(sub {
387 if ( $self->{contact}->used ) {
388 $self->{contact}->detach();
389 $self->{contact}->save();
390 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
392 $self->{contact}->delete(cascade => 1);
393 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
397 }) || die($db->error);
399 $self->{contact} = $self->_new_contact_object;
402 $self->action_edit();
405 sub action_delete_shipto {
408 my $db = $self->{shipto}->db;
410 if ( !$self->{shipto}->shipto_id ) {
411 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
414 $db->with_transaction(sub {
415 if ( $self->{shipto}->used ) {
416 $self->{shipto}->detach();
417 $self->{shipto}->save(cascade => 1);
418 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
420 $self->{shipto}->delete(cascade => 1);
421 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
425 }) || die($db->error);
427 $self->{shipto} = SL::DB::Shipto->new();
430 $self->action_edit();
438 controller => 'ct.pl',
440 db => $self->is_vendor() ? 'vendor' : 'customer',
443 if ( $::form->{callback} ) {
444 push(@url_params, callback => $::form->{callback});
447 $self->redirect_to(@url_params);
451 sub action_search_contact {
454 my $url = 'ct.pl?action=search_contact&db=customer';
456 if ( $::form->{callback} ) {
457 $url .= '&callback='. $::form->escape($::form->{callback});
460 print $::form->redirect_header($url);
464 sub action_get_delivery {
467 $::auth->assert('sales_all_edit');
469 my $dbh = $::form->get_standard_dbh();
471 my ($arap, $db, $qty_sign);
472 if ( $self->is_vendor() ) {
475 $qty_sign = ' * -1 AS qty';
482 my $where = ' WHERE 1=1';
485 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
486 $where .= " AND ${arap}.shipto_id = ?";
487 push(@values, $::form->{shipto_id});
489 $where .= " AND ${arap}.${db}_id = ?";
490 push(@values, $::form->{id});
493 if ( $::form->{delivery_from} ) {
494 $where .= " AND ${arap}.transdate >= ?";
495 push(@values, conv_date($::form->{delivery_from}));
498 if ( $::form->{delivery_to} ) {
499 $where .= " AND ${arap}.transdate <= ?";
500 push(@values, conv_date($::form->{delivery_to}));
519 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
522 ON ${arap}.id = i.trans_id
528 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
529 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
532 ORDER BY ${arap}.transdate DESC LIMIT 15";
534 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
536 $self->render('customer_vendor/get_delivery', { layout => 0 });
539 sub action_ajaj_get_shipto {
546 my $name = 'shipto'. $_;
547 $name => $self->{shipto}->$name;
549 qw(_id name department_1 department_2 street zipcode city gln country contact phone fax email)
553 $data->{shipto_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{shipto}->cvars_by_config);
555 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
558 sub action_ajaj_get_contact {
566 my $name = 'cp_'. $_;
568 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
569 $name => $self->{contact}->$name->to_lxoffice;
571 $name => $self->{contact}->$name;
575 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
576 satphone satfax project street zipcode city privatphone privatemail birthday
581 $data->{contact_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{contact}->cvars_by_config);
583 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
586 sub action_ajaj_autocomplete {
587 my ($self, %params) = @_;
589 my ($model, $manager, $number, $matches);
591 # first see if this is customer or vendor picking
592 if ($::form->{type} eq 'customer') {
593 $model = $self->customer_models;
594 $manager = 'SL::DB::Manager::Customer';
595 $number = 'customernumber';
596 } elsif ($::form->{type} eq 'vendor') {
597 $model = $self->vendor_models;
598 $manager = 'SL::DB::Manager::Vendor';
599 $number = 'vendornumber';
601 die "unknown type $::form->{type}";
604 # if someone types something, and hits enter, assume he entered the full name.
605 # if something matches, treat that as sole match
606 # unfortunately get_models can't do more than one per package atm, so we d it
607 # the oldfashioned way.
608 if ($::form->{prefer_exact}) {
610 if (1 == scalar @{ $exact_matches = $manager->get_all(
613 (salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
615 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
616 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
621 $matches = $exact_matches;
625 $matches //= $model->get;
629 value => $_->displayable_name,
630 label => $_->displayable_name,
632 $number => $_->$number,
634 type => $::form->{type},
635 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
639 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
642 sub action_test_page {
643 $_[0]->render('customer_vendor/test_page');
647 return $::form->{db} eq 'vendor';
651 return $::form->{db} eq 'customer';
657 if ( defined($self->{_is_orphaned}) ) {
658 return $self->{_is_orphaned};
661 my $arap = $self->is_vendor ? 'ap' : 'ar';
664 my $cv = $self->is_vendor ? 'vendor' : 'customer';
668 FROM '. $arap .' AS a
669 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
676 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
682 FROM delivery_orders a
683 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
687 if ( $self->is_vendor ) {
690 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
694 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
696 return $self->{_is_orphaned} = !$dummy;
699 sub _copy_form_to_cvars {
700 my ($self, %params) = @_;
702 foreach my $cvar (@{ $params{target}->cvars_by_config }) {
703 my $value = $params{source}->{$cvar->config->name};
704 $value = $::form->parse_amount(\%::myconfig, $value) if $cvar->config->type eq 'number';
706 $cvar->value($value);
710 sub _instantiate_args {
713 my $curr_employee = SL::DB::Manager::Employee->current;
715 if ( $::form->{cv}->{id} ) {
716 if ( $self->is_vendor() ) {
717 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
719 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
722 $self->{cv} = $self->_new_customer_vendor_object;
724 $self->{cv}->assign_attributes(%{$::form->{cv}});
726 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
727 $self->{cv}->taxincluded_checked(undef);
730 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
732 if ( $::form->{note}->{id} ) {
733 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
734 $self->{note_followup} = $self->{note}->follow_up;
735 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
737 $self->{note} = SL::DB::Note->new();
738 $self->{note_followup} = SL::DB::FollowUp->new();
739 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
742 $self->{note}->assign_attributes(%{$::form->{note}});
743 $self->{note}->created_by($curr_employee->id);
744 $self->{note}->trans_module('ct');
746 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
747 $self->{note_followup}->note($self->{note});
748 $self->{note_followup}->created_by($curr_employee->id);
750 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
751 $self->{note_followup_link}->trans_info($self->{cv}->name);
753 if ( $::form->{shipto}->{shipto_id} ) {
754 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
756 $self->{shipto} = SL::DB::Shipto->new();
758 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
759 $self->{shipto}->module('CT');
761 if ( $::form->{contact}->{cp_id} ) {
762 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
764 $self->{contact} = $self->_new_contact_object;
766 $self->{contact}->assign_attributes(%{$::form->{contact}});
768 $self->_copy_form_to_cvars(target => $self->{cv}, source => $::form->{cv_cvars});
769 $self->_copy_form_to_cvars(target => $self->{contact}, source => $::form->{contact_cvars});
770 $self->_copy_form_to_cvars(target => $self->{shipto}, source => $::form->{shipto_cvars});
773 sub _load_customer_vendor {
776 if ( $self->is_vendor() ) {
777 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
779 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
782 if ( $::form->{note_id} ) {
783 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
784 $self->{note_followup} = $self->{note}->follow_up;
785 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
787 $self->{note} = SL::DB::Note->new();
788 $self->{note_followup} = SL::DB::FollowUp->new();
789 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
792 if ( $::form->{shipto_id} ) {
793 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
795 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
796 die($::locale->text('Error'));
799 $self->{shipto} = SL::DB::Shipto->new();
802 if ( $::form->{contact_id} ) {
803 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
805 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
806 die($::locale->text('Error'));
809 $self->{contact} = $self->_new_contact_object;
813 sub _check_customer_vendor_all_edit {
816 unless ($::auth->assert('customer_vendor_all_edit', 1)) {
817 die($::locale->text("You don't have the rights to edit this customer.") . "\n")
818 if $self->{cv}->is_customer and
819 SL::DB::Manager::Employee->current->id != $self->{cv}->salesman_id;
823 sub _create_customer_vendor {
826 $self->{cv} = $self->_new_customer_vendor_object;
827 $self->{cv}->currency_id($::instance_conf->get_currency_id());
829 $self->{note} = SL::DB::Note->new();
831 $self->{note_followup} = SL::DB::FollowUp->new();
833 $self->{shipto} = SL::DB::Shipto->new();
835 $self->{contact} = $self->_new_contact_object;
841 my $dbh = $::form->get_standard_dbh();
845 $self->{all_business} = SL::DB::Manager::Business->get_all();
847 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
850 'SELECT DISTINCT(greeting)
852 WHERE greeting IS NOT NULL AND greeting != \'\'
854 SELECT DISTINCT(greeting)
856 WHERE greeting IS NOT NULL AND greeting != \'\'
858 $self->{all_greetings} = [
861 selectall_hashref_query($::form, $dbh, $query)
866 'SELECT DISTINCT(cp_title) AS title
868 WHERE cp_title IS NOT NULL AND cp_title != \'\'
870 $self->{all_titles} = [
873 selectall_hashref_query($::form, $dbh, $query)
877 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
879 $self->{all_languages} = SL::DB::Manager::Language->get_all();
881 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
883 if ( $::instance_conf->get_vertreter() ) {
891 selectall_hashref_query($::form, $dbh, $query)
895 if ( $business_ids->[0] ) {
896 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
898 $self->{all_salesman_customers} = [];
901 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
904 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->{cv}->payment_id,
907 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
909 if ($self->{cv}->is_customer) {
910 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
914 'SELECT DISTINCT(cp_abteilung) AS department
916 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
917 ORDER BY cp_abteilung';
918 $self->{all_departments} = [
920 { $_->{department}; }
921 selectall_hashref_query($::form, $dbh, $query)
925 $self->{contacts} = $self->{cv}->contacts;
926 $self->{contacts} ||= [];
928 $self->{shiptos} = $self->{cv}->shipto;
929 $self->{shiptos} ||= [];
931 $self->{notes} = SL::DB::Manager::Note->get_all(
933 trans_id => $self->{cv}->id,
934 trans_module => 'ct',
936 with_objects => ['follow_up'],
939 $self->{template_args} ||= {};
941 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
942 $::request->{layout}->add_javascripts('kivi.File.js');
944 $self->_setup_form_action_bar;
947 sub _setup_form_action_bar {
950 for my $bar ($::request->layout->get('actionbar')) {
955 submit => [ '#form', { action => "CustomerVendor/save" } ],
956 checks => [ 'check_taxzone_and_ustid' ],
957 accesskey => 'enter',
960 t8('Save and Close'),
961 submit => [ '#form', { action => "CustomerVendor/save_and_close" } ],
962 checks => [ 'check_taxzone_and_ustid' ],
964 ], # end of combobox "Save"
967 action => [ t8('Workflow') ],
969 t8('Save and AP Transaction'),
970 submit => [ '#form', { action => "CustomerVendor/save_and_ap_transaction" } ],
971 checks => [ 'check_taxzone_and_ustid' ],
972 ]) x !!$self->is_vendor,
974 t8('Save and AR Transaction'),
975 submit => [ '#form', { action => "CustomerVendor/save_and_ar_transaction" } ],
976 checks => [ 'check_taxzone_and_ustid' ],
977 ]) x !$self->is_vendor,
979 t8('Save and Invoice'),
980 submit => [ '#form', { action => "CustomerVendor/save_and_invoice" } ],
981 checks => [ 'check_taxzone_and_ustid' ],
984 t8('Save and Order'),
985 submit => [ '#form', { action => "CustomerVendor/save_and_order" } ],
986 checks => [ 'check_taxzone_and_ustid' ],
990 submit => [ '#form', { action => "CustomerVendor/save_and_rfq" } ],
991 checks => [ 'check_taxzone_and_ustid' ],
992 ]) x !!$self->is_vendor,
994 t8('Save and Quotation'),
995 submit => [ '#form', { action => "CustomerVendor/save_and_quotation" } ],
996 checks => [ 'check_taxzone_and_ustid' ],
997 ]) x !$self->is_vendor,
998 ], # end of combobox "Workflow"
1002 submit => [ '#form', { action => "CustomerVendor/delete" } ],
1003 confirm => t8('Do you really want to delete this object?'),
1004 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.')
1005 : !$self->is_orphaned ? t8('This object has already been used.')
1013 call => [ 'kivi.CustomerVendor.showHistoryWindow', $self->{cv}->id ],
1014 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.') : undef,
1020 sub _prepare_cvar_configs_for_ajaj {
1021 my ($self, $cvars) = @_;
1026 my $result = { type => $cvar->config->type };
1028 if ($cvar->config->type eq 'number') {
1029 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
1031 } elsif ($result->{type} eq 'date') {
1032 $result->{value} = $cvar->value ? $cvar->value->to_kivitendo : undef;
1034 } elsif ($result->{type} =~ m{customer|vendor|part}) {
1035 my $object = $cvar->value;
1036 my $method = $result->{type} eq 'part' ? 'description' : 'name';
1038 $result->{id} = int($cvar->number_value) || undef;
1039 $result->{value} = $object ? $object->$method // '' : '';
1042 $result->{value} = $cvar->value;
1045 ( $cvar->config->name => $result )
1047 } grep { $_->is_valid } @{ $cvars }
1051 sub normalize_name {
1054 # check if feature is enabled (select normalize_vc_names from defaults)
1055 return unless ($::instance_conf->get_normalize_vc_names);
1057 return unless $self->{cv};
1058 my $name = $self->{cv}->name;
1062 $self->{cv}->name($name);
1065 sub home_address_for_google_maps {
1068 my $address = $::instance_conf->get_address // '';
1069 $address =~ s{^\s+|\s+$|\r+}{}g;
1070 $address =~ s{\n+}{,}g;
1071 $address =~ s{\s+}{ }g;
1076 sub init_customer_models {
1079 SL::Controller::Helper::GetModels->new(
1080 controller => $self,
1081 model => 'Customer',
1084 by => 'customernumber',
1087 customernumber => t8('Customer Number'),
1090 ( salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
1095 sub init_vendor_models {
1098 SL::Controller::Helper::GetModels->new(
1099 controller => $self,
1103 by => 'vendornumber',
1106 vendornumber => t8('Vendor Number'),
1111 sub _new_customer_vendor_object {
1114 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
1118 custom_variables => [],
1122 sub _new_contact_object {
1125 return SL::DB::Contact->new(custom_variables => []);