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;
27 use SL::DB::FollowUpLink;
31 use SL::DB::PurchaseInvoice;
36 use Rose::Object::MakeMethods::Generic (
37 'scalar --get_set_init' => [ qw(customer_models vendor_models) ],
41 __PACKAGE__->run_before(
43 $::auth->assert('customer_vendor_edit');
45 except => [ qw(ajaj_autocomplete) ],
47 __PACKAGE__->run_before(
51 'save_and_ap_transaction',
52 'save_and_ar_transaction',
64 __PACKAGE__->run_before(
65 '_load_customer_vendor',
75 # make sure this comes after _load_customer_vendor
76 __PACKAGE__->run_before(
77 '_check_customer_vendor_all_edit',
84 'save_and_ap_transaction',
85 'save_and_ar_transaction',
97 __PACKAGE__->run_before(
98 '_create_customer_vendor',
104 __PACKAGE__->run_before('normalize_name');
110 $self->_pre_render();
111 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer;
114 'customer_vendor/form',
115 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
116 %{$self->{template_args}}
123 $self->_pre_render();
125 'customer_vendor/form',
126 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
127 %{$self->{template_args}}
134 if ($::request->type eq 'json') {
139 $cv_hash = $self->{cv}->as_tree;
140 $cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
143 $self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
150 my @errors = $self->{cv}->validate;
152 flash('error', @errors);
153 $self->_pre_render();
155 'customer_vendor/form',
156 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
157 %{$self->{template_args}}
159 $::dispatcher->end_request;
162 my $db = $self->{cv}->db;
164 $db->with_transaction(sub {
166 if ( $self->is_vendor() ) {
167 if ( $self->{cv}->vendornumber ) {
168 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
171 if ( $self->{cv}->customernumber ) {
172 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
176 foreach my $entry (@{$cvs_by_nr}) {
177 if( $entry->id != $self->{cv}->id ) {
179 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
181 $::form->error($msg);
185 $self->{cv}->save(cascade => 1);
187 $self->{contact}->cp_cv_id($self->{cv}->id);
188 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
189 $self->{contact}->save(cascade => 1);
192 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
194 if ( !$self->{note_followup}->follow_up_date ) {
195 $::form->error($::locale->text('Date missing!'));
198 $self->{note}->trans_id($self->{cv}->id);
199 $self->{note}->save();
201 $self->{note_followup}->save();
203 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
204 $self->{note_followup_link}->trans_id($self->{cv}->id);
205 $self->{note_followup_link}->save();
207 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
210 $self->{shipto}->trans_id($self->{cv}->id);
211 if(any { $self->{shipto}->$_ ne '' } qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax shiptoemail)) {
212 $self->{shipto}->save(cascade => 1);
215 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
216 SL::DB::History->new(
217 trans_id => $self->{cv}->id,
218 snumbers => $snumbers,
219 employee_id => SL::DB::Manager::Employee->current->id,
223 if ( $::form->{delete_notes} ) {
224 foreach my $note_id (@{ $::form->{delete_notes} }) {
225 my $note = SL::DB::Note->new(id => $note_id)->load();
226 if ( $note->follow_up ) {
227 if ( $note->follow_up->follow_up_link ) {
228 $note->follow_up->follow_up_link->delete(cascade => 'delete');
230 $note->follow_up->delete(cascade => 'delete');
232 $note->delete(cascade => 'delete');
237 }) || die($db->error);
246 my @redirect_params = (
248 id => $self->{cv}->id,
249 db => ($self->is_vendor() ? 'vendor' : 'customer'),
252 if ( $self->{contact}->cp_id ) {
253 push(@redirect_params, contact_id => $self->{contact}->cp_id);
256 if ( $self->{shipto}->shipto_id ) {
257 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
260 $self->redirect_to(@redirect_params);
263 sub action_save_and_close {
268 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
269 $::form->redirect($msg);
273 my ($self, $script) = @_;
275 $::auth->assert('gl_transactions | ap_transactions | ar_transactions'.
276 '| invoice_edit | vendor_invoice_edit | ' .
277 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
281 my $name = $::form->escape($self->{cv}->name, 1);
282 my $db = $self->is_vendor() ? 'vendor' : 'customer';
285 if ($::instance_conf->get_feature_experimental_order && 'oe.pl' eq $script) {
286 $script = 'controller.pl';
287 $action = 'Order/' . $action;
290 my $url = $self->url_for(
291 controller => $script,
294 $db .'_id' => $self->{cv}->id,
296 type => $::form->{type},
297 callback => $::form->{callback},
300 print $::form->redirect_header($url);
303 sub action_save_and_ar_transaction {
306 $main::auth->assert('ar_transactions');
308 $self->_transaction('ar.pl');
311 sub action_save_and_ap_transaction {
314 $main::auth->assert('ap_transactions');
316 $self->_transaction('ap.pl');
319 sub action_save_and_invoice {
322 if ( $self->is_vendor() ) {
323 $::auth->assert('vendor_invoice_edit');
325 $::auth->assert('invoice_edit');
328 $::form->{type} = 'invoice';
329 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
332 sub action_save_and_order {
335 if ( $self->is_vendor() ) {
336 $::auth->assert('purchase_order_edit');
338 $::auth->assert('sales_order_edit');
341 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
342 $self->_transaction('oe.pl');
345 sub action_save_and_rfq {
348 $::auth->assert('request_quotation_edit');
350 $::form->{type} = 'request_quotation';
351 $self->_transaction('oe.pl');
354 sub action_save_and_quotation {
357 $::auth->assert('sales_quotation_edit');
359 $::form->{type} = 'sales_quotation';
360 $self->_transaction('oe.pl');
366 my $db = $self->{cv}->db;
368 if( !$self->is_orphaned() ) {
369 $self->action_edit();
372 $db->with_transaction(sub {
373 $self->{cv}->delete(cascade => 1);
375 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
376 SL::DB::History->new(
377 trans_id => $self->{cv}->id,
378 snumbers => $snumbers,
379 employee_id => SL::DB::Manager::Employee->current->id,
380 addition => 'DELETED',
382 }) || die($db->error);
384 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
385 $::form->redirect($msg);
391 sub action_delete_contact {
394 my $db = $self->{contact}->db;
396 if ( !$self->{contact}->cp_id ) {
397 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
400 $db->with_transaction(sub {
401 if ( $self->{contact}->used ) {
402 $self->{contact}->detach();
403 $self->{contact}->save();
404 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
406 $self->{contact}->delete(cascade => 1);
407 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
411 }) || die($db->error);
413 $self->{contact} = $self->_new_contact_object;
416 $self->action_edit();
419 sub action_delete_shipto {
422 my $db = $self->{shipto}->db;
424 if ( !$self->{shipto}->shipto_id ) {
425 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
428 $db->with_transaction(sub {
429 if ( $self->{shipto}->used ) {
430 $self->{shipto}->detach();
431 $self->{shipto}->save(cascade => 1);
432 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
434 $self->{shipto}->delete(cascade => 1);
435 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
439 }) || die($db->error);
441 $self->{shipto} = SL::DB::Shipto->new();
444 $self->action_edit();
452 controller => 'ct.pl',
454 db => $self->is_vendor() ? 'vendor' : 'customer',
457 if ( $::form->{callback} ) {
458 push(@url_params, callback => $::form->{callback});
461 $self->redirect_to(@url_params);
465 sub action_search_contact {
468 my $url = 'ct.pl?action=search_contact&db=customer';
470 if ( $::form->{callback} ) {
471 $url .= '&callback='. $::form->escape($::form->{callback});
474 print $::form->redirect_header($url);
477 sub action_get_delivery {
480 $::auth->assert('sales_all_edit');
482 my $dbh = $::form->get_standard_dbh();
484 my ($arap, $db, $qty_sign);
485 if ( $self->is_vendor() ) {
488 $qty_sign = ' * -1 AS qty';
495 my $where = ' WHERE 1=1';
498 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
499 $where .= " AND ${arap}.shipto_id = ?";
500 push(@values, $::form->{shipto_id});
502 $where .= " AND ${arap}.${db}_id = ?";
503 push(@values, $::form->{id});
506 if ( $::form->{delivery_from} ) {
507 $where .= " AND ${arap}.transdate >= ?";
508 push(@values, conv_date($::form->{delivery_from}));
511 if ( $::form->{delivery_to} ) {
512 $where .= " AND ${arap}.transdate <= ?";
513 push(@values, conv_date($::form->{delivery_to}));
532 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
535 ON ${arap}.id = i.trans_id
541 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
542 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
545 ORDER BY ${arap}.transdate DESC LIMIT 15";
547 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
549 $self->render('customer_vendor/get_delivery', { layout => 0 });
552 sub action_ajaj_get_shipto {
559 my $name = 'shipto'. $_;
560 $name => $self->{shipto}->$name;
562 qw(_id name department_1 department_2 street zipcode city gln country contact phone fax email)
566 $data->{shipto_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{shipto}->cvars_by_config);
568 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
571 sub action_ajaj_get_contact {
579 my $name = 'cp_'. $_;
581 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
582 $name => $self->{contact}->$name->to_lxoffice;
584 $name => $self->{contact}->$name;
588 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
589 satphone satfax project street zipcode city privatphone privatemail birthday main
594 $data->{contact_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{contact}->cvars_by_config);
596 # avoid two or more main_cp
597 my $has_main_cp = grep { $_->cp_main == 1 } @{ $self->{cv}->contacts };
598 $data->{contact}->{disable_cp_main} = 1 if ($has_main_cp && !$data->{contact}->{cp_main});
600 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
603 sub action_ajaj_autocomplete {
604 my ($self, %params) = @_;
606 my ($model, $manager, $number, $matches);
608 # first see if this is customer or vendor picking
609 if ($::form->{type} eq 'customer') {
610 $model = $self->customer_models;
611 $manager = 'SL::DB::Manager::Customer';
612 $number = 'customernumber';
613 } elsif ($::form->{type} eq 'vendor') {
614 $model = $self->vendor_models;
615 $manager = 'SL::DB::Manager::Vendor';
616 $number = 'vendornumber';
618 die "unknown type $::form->{type}";
621 # if someone types something, and hits enter, assume he entered the full name.
622 # if something matches, treat that as sole match
623 # unfortunately get_models can't do more than one per package atm, so we d it
624 # the oldfashioned way.
625 if ($::form->{prefer_exact}) {
627 if (1 == scalar @{ $exact_matches = $manager->get_all(
630 (salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
632 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
633 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
638 $matches = $exact_matches;
642 $matches //= $model->get;
646 value => $_->displayable_name,
647 label => $_->displayable_name,
649 $number => $_->$number,
651 type => $::form->{type},
652 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
656 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
659 sub action_test_page {
660 $_[0]->render('customer_vendor/test_page');
664 return $::form->{db} eq 'vendor';
668 return $::form->{db} eq 'customer';
674 if ( defined($self->{_is_orphaned}) ) {
675 return $self->{_is_orphaned};
678 my $arap = $self->is_vendor ? 'ap' : 'ar';
681 my $cv = $self->is_vendor ? 'vendor' : 'customer';
685 FROM '. $arap .' AS a
686 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
693 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
699 FROM delivery_orders a
700 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
704 if ( $self->is_vendor ) {
707 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
711 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
713 return $self->{_is_orphaned} = !$dummy;
716 sub _copy_form_to_cvars {
717 my ($self, %params) = @_;
719 foreach my $cvar (@{ $params{target}->cvars_by_config }) {
720 my $value = $params{source}->{$cvar->config->name};
721 $value = $::form->parse_amount(\%::myconfig, $value) if $cvar->config->type eq 'number';
723 $cvar->value($value);
727 sub _instantiate_args {
730 my $curr_employee = SL::DB::Manager::Employee->current;
732 if ( $::form->{cv}->{id} ) {
733 if ( $self->is_vendor() ) {
734 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
736 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
739 $self->{cv} = $self->_new_customer_vendor_object;
741 $self->{cv}->assign_attributes(%{$::form->{cv}});
743 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
744 $self->{cv}->taxincluded_checked(undef);
747 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
749 if ( $::form->{note}->{id} ) {
750 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
751 $self->{note_followup} = $self->{note}->follow_up;
752 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
754 $self->{note} = SL::DB::Note->new();
755 $self->{note_followup} = SL::DB::FollowUp->new();
756 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
759 $self->{note}->assign_attributes(%{$::form->{note}});
760 $self->{note}->created_by($curr_employee->id);
761 $self->{note}->trans_module('ct');
763 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
764 $self->{note_followup}->note($self->{note});
765 $self->{note_followup}->created_by($curr_employee->id);
767 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
768 $self->{note_followup_link}->trans_info($self->{cv}->name);
770 if ( $::form->{shipto}->{shipto_id} ) {
771 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
773 $self->{shipto} = SL::DB::Shipto->new();
775 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
776 $self->{shipto}->module('CT');
778 if ( $::form->{contact}->{cp_id} ) {
779 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
781 $self->{contact} = $self->_new_contact_object;
783 $self->{contact}->assign_attributes(%{$::form->{contact}});
785 $self->_copy_form_to_cvars(target => $self->{cv}, source => $::form->{cv_cvars});
786 $self->_copy_form_to_cvars(target => $self->{contact}, source => $::form->{contact_cvars});
787 $self->_copy_form_to_cvars(target => $self->{shipto}, source => $::form->{shipto_cvars});
790 sub _load_customer_vendor {
793 if ( $self->is_vendor() ) {
794 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
796 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
799 if ( $::form->{note_id} ) {
800 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
801 $self->{note_followup} = $self->{note}->follow_up;
802 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
804 $self->{note} = SL::DB::Note->new();
805 $self->{note_followup} = SL::DB::FollowUp->new();
806 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
809 if ( $::form->{shipto_id} ) {
810 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
812 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
813 die($::locale->text('Error'));
816 $self->{shipto} = SL::DB::Shipto->new();
819 if ( $::form->{contact_id} ) {
820 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
822 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
823 die($::locale->text('Error'));
826 $self->{contact} = $self->_new_contact_object;
830 sub _check_customer_vendor_all_edit {
833 unless ($::auth->assert('customer_vendor_all_edit', 1)) {
834 die($::locale->text("You don't have the rights to edit this customer.") . "\n")
835 if $self->{cv}->is_customer and
836 SL::DB::Manager::Employee->current->id != $self->{cv}->salesman_id;
840 sub _create_customer_vendor {
843 $self->{cv} = $self->_new_customer_vendor_object;
844 $self->{cv}->currency_id($::instance_conf->get_currency_id());
846 $self->{note} = SL::DB::Note->new();
848 $self->{note_followup} = SL::DB::FollowUp->new();
850 $self->{shipto} = SL::DB::Shipto->new();
852 $self->{contact} = $self->_new_contact_object;
858 my $dbh = $::form->get_standard_dbh();
862 $self->{all_business} = SL::DB::Manager::Business->get_all();
864 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
867 'SELECT DISTINCT(greeting)
869 WHERE greeting IS NOT NULL AND greeting != \'\'
871 SELECT DISTINCT(greeting)
873 WHERE greeting IS NOT NULL AND greeting != \'\'
875 $self->{all_greetings} = [
878 selectall_hashref_query($::form, $dbh, $query)
883 'SELECT DISTINCT(cp_title) AS title
885 WHERE cp_title IS NOT NULL AND cp_title != \'\'
887 $self->{all_titles} = [
890 selectall_hashref_query($::form, $dbh, $query)
894 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
896 $self->{all_languages} = SL::DB::Manager::Language->get_all();
898 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
900 if ( $::instance_conf->get_vertreter() ) {
908 selectall_hashref_query($::form, $dbh, $query)
912 if ( $business_ids->[0] ) {
913 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
915 $self->{all_salesman_customers} = [];
918 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
921 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->{cv}->payment_id,
924 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
926 if ($self->{cv}->is_customer) {
927 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
931 'SELECT DISTINCT(cp_abteilung) AS department
933 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
934 ORDER BY cp_abteilung';
935 $self->{all_departments} = [
937 { $_->{department}; }
938 selectall_hashref_query($::form, $dbh, $query)
942 $self->{contacts} = $self->{cv}->contacts;
943 $self->{contacts} ||= [];
945 $self->{shiptos} = $self->{cv}->shipto;
946 $self->{shiptos} ||= [];
948 $self->{notes} = SL::DB::Manager::Note->get_all(
950 trans_id => $self->{cv}->id,
951 trans_module => 'ct',
953 with_objects => ['follow_up'],
956 if ( $self->is_vendor()) {
957 $self->{open_items} = SL::DB::Manager::PurchaseInvoice->get_all_count(
959 vendor_id => $self->{cv}->id,
960 paid => {lt_sql => 'amount'},
964 $self->{open_items} = SL::DB::Manager::Invoice->get_all_count(
966 customer_id => $self->{cv}->id,
967 paid => {lt_sql => 'amount'},
972 if ( $self->is_vendor() ) {
973 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
975 vendor_id => $self->{cv}->id,
980 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
982 customer_id => $self->{cv}->id,
987 $self->{template_args} ||= {};
989 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
990 $::request->{layout}->add_javascripts('kivi.File.js');
991 $::request->{layout}->add_javascripts('kivi.CustomerVendorTurnover.js');
993 $self->_setup_form_action_bar;
996 sub _setup_form_action_bar {
999 for my $bar ($::request->layout->get('actionbar')) {
1004 submit => [ '#form', { action => "CustomerVendor/save" } ],
1005 checks => [ 'check_taxzone_and_ustid' ],
1006 accesskey => 'enter',
1009 t8('Save and Close'),
1010 submit => [ '#form', { action => "CustomerVendor/save_and_close" } ],
1011 checks => [ 'check_taxzone_and_ustid' ],
1013 ], # end of combobox "Save"
1016 action => [ t8('Workflow') ],
1018 t8('Save and AP Transaction'),
1019 submit => [ '#form', { action => "CustomerVendor/save_and_ap_transaction" } ],
1020 checks => [ 'check_taxzone_and_ustid' ],
1021 ]) x !!$self->is_vendor,
1023 t8('Save and AR Transaction'),
1024 submit => [ '#form', { action => "CustomerVendor/save_and_ar_transaction" } ],
1025 checks => [ 'check_taxzone_and_ustid' ],
1026 ]) x !$self->is_vendor,
1028 t8('Save and Invoice'),
1029 submit => [ '#form', { action => "CustomerVendor/save_and_invoice" } ],
1030 checks => [ 'check_taxzone_and_ustid' ],
1033 t8('Save and Order'),
1034 submit => [ '#form', { action => "CustomerVendor/save_and_order" } ],
1035 checks => [ 'check_taxzone_and_ustid' ],
1039 submit => [ '#form', { action => "CustomerVendor/save_and_rfq" } ],
1040 checks => [ 'check_taxzone_and_ustid' ],
1041 ]) x !!$self->is_vendor,
1043 t8('Save and Quotation'),
1044 submit => [ '#form', { action => "CustomerVendor/save_and_quotation" } ],
1045 checks => [ 'check_taxzone_and_ustid' ],
1046 ]) x !$self->is_vendor,
1047 ], # end of combobox "Workflow"
1051 submit => [ '#form', { action => "CustomerVendor/delete" } ],
1052 confirm => t8('Do you really want to delete this object?'),
1053 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.')
1054 : !$self->is_orphaned ? t8('This object has already been used.')
1062 call => [ 'kivi.CustomerVendor.showHistoryWindow', $self->{cv}->id ],
1063 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.') : undef,
1069 sub _prepare_cvar_configs_for_ajaj {
1070 my ($self, $cvars) = @_;
1075 my $result = { type => $cvar->config->type };
1077 if ($cvar->config->type eq 'number') {
1078 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
1080 } elsif ($result->{type} eq 'date') {
1081 $result->{value} = $cvar->value ? $cvar->value->to_kivitendo : undef;
1083 } elsif ($result->{type} =~ m{customer|vendor|part}) {
1084 my $object = $cvar->value;
1085 my $method = $result->{type} eq 'part' ? 'description' : 'name';
1087 $result->{id} = int($cvar->number_value) || undef;
1088 $result->{value} = $object ? $object->$method // '' : '';
1091 $result->{value} = $cvar->value;
1094 ( $cvar->config->name => $result )
1096 } grep { $_->is_valid } @{ $cvars }
1100 sub normalize_name {
1103 # check if feature is enabled (select normalize_vc_names from defaults)
1104 return unless ($::instance_conf->get_normalize_vc_names);
1106 return unless $self->{cv};
1107 my $name = $self->{cv}->name;
1111 $self->{cv}->name($name);
1114 sub home_address_for_google_maps {
1117 my $address = $::instance_conf->get_address // '';
1118 $address =~ s{^\s+|\s+$|\r+}{}g;
1119 $address =~ s{\n+}{,}g;
1120 $address =~ s{\s+}{ }g;
1125 sub init_customer_models {
1128 SL::Controller::Helper::GetModels->new(
1129 controller => $self,
1130 model => 'Customer',
1133 by => 'customernumber',
1136 customernumber => t8('Customer Number'),
1139 ( salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
1144 sub init_vendor_models {
1147 SL::Controller::Helper::GetModels->new(
1148 controller => $self,
1152 by => 'vendornumber',
1155 vendornumber => t8('Vendor Number'),
1160 sub _new_customer_vendor_object {
1163 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
1167 custom_variables => [],
1171 sub _new_contact_object {
1174 return SL::DB::Contact->new(custom_variables => []);