1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
6 use List::MoreUtils qw(any);
10 use SL::Helper::Flash;
11 use SL::Locale::String;
12 use SL::Util qw(trim);
15 use SL::Controller::Helper::GetModels;
16 use SL::Controller::Helper::ReportGenerator;
17 use SL::Controller::Helper::ParseFilter;
22 use SL::DB::ContactDepartment;
23 use SL::DB::ContactTitle;
29 use SL::DB::PaymentTerm;
30 use SL::DB::Pricegroup;
34 use SL::DB::FollowUpLink;
38 use SL::DB::PurchaseInvoice;
43 use Rose::Object::MakeMethods::Generic (
44 'scalar --get_set_init' => [ qw(customer_models vendor_models zugferd_settings) ],
48 __PACKAGE__->run_before(
50 $::auth->assert('customer_vendor_edit');
52 except => [ qw(ajaj_autocomplete) ],
54 __PACKAGE__->run_before(
58 'save_and_ap_transaction',
59 'save_and_ar_transaction',
71 __PACKAGE__->run_before(
72 '_load_customer_vendor',
83 # make sure this comes after _load_customer_vendor
84 __PACKAGE__->run_before(
85 '_check_customer_vendor_all_edit',
92 'save_and_ap_transaction',
93 'save_and_ar_transaction',
105 __PACKAGE__->run_before(
106 '_create_customer_vendor',
112 __PACKAGE__->run_before('normalize_name');
118 $self->_pre_render();
119 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer;
122 'customer_vendor/form',
123 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
124 %{$self->{template_args}}
131 $self->_pre_render();
133 'customer_vendor/form',
134 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
135 %{$self->{template_args}}
142 if ($::request->type eq 'json') {
147 $cv_hash = $self->{cv}->as_tree;
148 $cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
151 $self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
158 my @errors = $self->{cv}->validate;
160 flash('error', @errors);
161 $self->_pre_render();
163 'customer_vendor/form',
164 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
165 %{$self->{template_args}}
167 $::dispatcher->end_request;
170 $self->{cv}->greeting(trim $self->{cv}->greeting);
171 my $save_greeting = $self->{cv}->greeting
172 && $::instance_conf->get_vc_greetings_use_textfield
173 && SL::DB::Manager::Greeting->get_all_count(where => [description => $self->{cv}->greeting]) == 0;
175 $self->{contact}->cp_title(trim($self->{contact}->cp_title));
176 my $save_contact_title = $self->{contact}->cp_title
177 && $::instance_conf->get_contact_titles_use_textfield
178 && SL::DB::Manager::ContactTitle->get_all_count(where => [description => $self->{contact}->cp_title]) == 0;
180 $self->{contact}->cp_abteilung(trim($self->{contact}->cp_abteilung));
181 my $save_contact_department = $self->{contact}->cp_abteilung
182 && $::instance_conf->get_contact_departments_use_textfield
183 && SL::DB::Manager::ContactDepartment->get_all_count(where => [description => $self->{contact}->cp_abteilung]) == 0;
185 my $db = $self->{cv}->db;
187 $db->with_transaction(sub {
189 if ( $self->is_vendor() ) {
190 if ( $self->{cv}->vendornumber ) {
191 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
194 if ( $self->{cv}->customernumber ) {
195 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
199 foreach my $entry (@{$cvs_by_nr}) {
200 if( $entry->id != $self->{cv}->id ) {
202 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
204 $::form->error($msg);
208 $self->{cv}->save(cascade => 1);
210 SL::DB::Greeting->new(description => $self->{cv}->greeting)->save if $save_greeting;
212 $self->{contact}->cp_cv_id($self->{cv}->id);
213 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
214 SL::DB::ContactTitle ->new(description => $self->{contact}->cp_title) ->save if $save_contact_title;
215 SL::DB::ContactDepartment->new(description => $self->{contact}->cp_abteilung)->save if $save_contact_department;
217 $self->{contact}->save(cascade => 1);
220 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
222 if ( !$self->{note_followup}->follow_up_date ) {
223 $::form->error($::locale->text('Date missing!'));
226 $self->{note}->trans_id($self->{cv}->id);
227 $self->{note}->save();
229 $self->{note_followup}->save();
231 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
232 $self->{note_followup_link}->trans_id($self->{cv}->id);
233 $self->{note_followup_link}->save();
235 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
238 $self->{shipto}->trans_id($self->{cv}->id);
239 if(any { $self->{shipto}->$_ ne '' } qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax shiptoemail)) {
240 $self->{shipto}->save(cascade => 1);
243 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
244 SL::DB::History->new(
245 trans_id => $self->{cv}->id,
246 snumbers => $snumbers,
247 employee_id => SL::DB::Manager::Employee->current->id,
251 if ( $::form->{delete_notes} ) {
252 foreach my $note_id (@{ $::form->{delete_notes} }) {
253 my $note = SL::DB::Note->new(id => $note_id)->load();
254 if ( $note->follow_up ) {
255 if ( $note->follow_up->follow_up_link ) {
256 $note->follow_up->follow_up_link->delete(cascade => 'delete');
258 $note->follow_up->delete(cascade => 'delete');
260 $note->delete(cascade => 'delete');
265 }) || die($db->error);
274 my @redirect_params = (
276 id => $self->{cv}->id,
277 db => ($self->is_vendor() ? 'vendor' : 'customer'),
280 if ( $self->{contact}->cp_id ) {
281 push(@redirect_params, contact_id => $self->{contact}->cp_id);
284 if ( $self->{shipto}->shipto_id ) {
285 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
288 $self->redirect_to(@redirect_params);
291 sub action_save_and_close {
296 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
297 $::form->redirect($msg);
301 my ($self, $script) = @_;
303 $::auth->assert('gl_transactions | ap_transactions | ar_transactions'.
304 '| invoice_edit | vendor_invoice_edit | ' .
305 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
309 my $name = $::form->escape($self->{cv}->name, 1);
310 my $db = $self->is_vendor() ? 'vendor' : 'customer';
313 if ($::instance_conf->get_feature_experimental_order && 'oe.pl' eq $script) {
314 $script = 'controller.pl';
315 $action = 'Order/' . $action;
318 my $url = $self->url_for(
319 controller => $script,
322 $db .'_id' => $self->{cv}->id,
324 type => $::form->{type},
325 callback => $::form->{callback},
328 print $::form->redirect_header($url);
331 sub action_save_and_ar_transaction {
334 $main::auth->assert('ar_transactions');
336 $self->_transaction('ar.pl');
339 sub action_save_and_ap_transaction {
342 $main::auth->assert('ap_transactions');
344 $self->_transaction('ap.pl');
347 sub action_save_and_invoice {
350 if ( $self->is_vendor() ) {
351 $::auth->assert('vendor_invoice_edit');
353 $::auth->assert('invoice_edit');
356 $::form->{type} = 'invoice';
357 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
360 sub action_save_and_order {
363 if ( $self->is_vendor() ) {
364 $::auth->assert('purchase_order_edit');
366 $::auth->assert('sales_order_edit');
369 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
370 $self->_transaction('oe.pl');
373 sub action_save_and_rfq {
376 $::auth->assert('request_quotation_edit');
378 $::form->{type} = 'request_quotation';
379 $self->_transaction('oe.pl');
382 sub action_save_and_quotation {
385 $::auth->assert('sales_quotation_edit');
387 $::form->{type} = 'sales_quotation';
388 $self->_transaction('oe.pl');
394 my $db = $self->{cv}->db;
396 if( !$self->is_orphaned() ) {
397 $self->action_edit();
400 $db->with_transaction(sub {
401 $self->{cv}->delete(cascade => 1);
403 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
404 SL::DB::History->new(
405 trans_id => $self->{cv}->id,
406 snumbers => $snumbers,
407 employee_id => SL::DB::Manager::Employee->current->id,
408 addition => 'DELETED',
410 }) || die($db->error);
412 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
413 $::form->redirect($msg);
419 sub action_delete_contact {
422 my $db = $self->{contact}->db;
424 if ( !$self->{contact}->cp_id ) {
425 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
428 $db->with_transaction(sub {
429 if ( $self->{contact}->used ) {
430 $self->{contact}->detach();
431 $self->{contact}->save();
432 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
434 $self->{contact}->delete(cascade => 1);
435 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
439 }) || die($db->error);
441 $self->{contact} = $self->_new_contact_object;
444 $self->action_edit();
447 sub action_delete_shipto {
450 my $db = $self->{shipto}->db;
452 if ( !$self->{shipto}->shipto_id ) {
453 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
456 $db->with_transaction(sub {
457 if ( $self->{shipto}->used ) {
458 $self->{shipto}->detach();
459 $self->{shipto}->save(cascade => 1);
460 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
462 $self->{shipto}->delete(cascade => 1);
463 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
467 }) || die($db->error);
469 $self->{shipto} = SL::DB::Shipto->new();
472 $self->action_edit();
480 controller => 'ct.pl',
482 db => $self->is_vendor() ? 'vendor' : 'customer',
485 if ( $::form->{callback} ) {
486 push(@url_params, callback => $::form->{callback});
489 $self->redirect_to(@url_params);
493 sub action_search_contact {
496 my $url = 'ct.pl?action=search_contact&db=customer';
498 if ( $::form->{callback} ) {
499 $url .= '&callback='. $::form->escape($::form->{callback});
502 print $::form->redirect_header($url);
505 sub action_get_delivery {
508 $::auth->assert('sales_all_edit') if $self->is_customer();
509 $::auth->assert('purchase_all_edit') if $self->is_vendor();
511 my $dbh = $::form->get_standard_dbh();
513 my ($arap, $db, $qty_sign);
514 if ( $self->is_vendor() ) {
517 $qty_sign = ' * -1 AS qty';
524 my $where = ' WHERE 1=1';
527 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
528 $where .= " AND ${arap}.shipto_id = ?";
529 push(@values, $::form->{shipto_id});
531 $where .= " AND ${arap}.${db}_id = ?";
532 push(@values, $::form->{id});
535 if ( $::form->{delivery_from} ) {
536 $where .= " AND ${arap}.transdate >= ?";
537 push(@values, conv_date($::form->{delivery_from}));
540 if ( $::form->{delivery_to} ) {
541 $where .= " AND ${arap}.transdate <= ?";
542 push(@values, conv_date($::form->{delivery_to}));
561 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
564 ON ${arap}.id = i.trans_id
570 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
571 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
574 ORDER BY ${arap}.transdate DESC LIMIT 15";
576 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
578 $self->render('customer_vendor/get_delivery', { layout => 0 });
581 sub action_ajaj_get_shipto {
588 my $name = 'shipto'. $_;
589 $name => $self->{shipto}->$name;
591 qw(_id name department_1 department_2 street zipcode city gln country contact phone fax email)
595 $data->{shipto_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{shipto}->cvars_by_config);
597 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
600 sub action_ajaj_get_contact {
608 my $name = 'cp_'. $_;
610 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
611 $name => $self->{contact}->$name->to_lxoffice;
613 $name => $self->{contact}->$name;
617 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
618 satphone satfax project street zipcode city privatphone privatemail birthday main
623 $data->{contact_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{contact}->cvars_by_config);
625 # avoid two or more main_cp
626 my $has_main_cp = grep { $_->cp_main == 1 } @{ $self->{cv}->contacts };
627 $data->{contact}->{disable_cp_main} = 1 if ($has_main_cp && !$data->{contact}->{cp_main});
629 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
632 sub action_ajaj_autocomplete {
633 my ($self, %params) = @_;
635 my ($model, $manager, $number, $matches);
637 # first see if this is customer or vendor picking
638 if ($::form->{type} eq 'customer') {
639 $model = $self->customer_models;
640 $manager = 'SL::DB::Manager::Customer';
641 $number = 'customernumber';
642 } elsif ($::form->{type} eq 'vendor') {
643 $model = $self->vendor_models;
644 $manager = 'SL::DB::Manager::Vendor';
645 $number = 'vendornumber';
647 die "unknown type $::form->{type}";
650 # if someone types something, and hits enter, assume he entered the full name.
651 # if something matches, treat that as the sole match
652 # unfortunately get_models can't do more than one per package atm, so we do it
653 # the oldfashioned way.
654 if ($::form->{prefer_exact}) {
656 if (1 == scalar @{ $exact_matches = $manager->get_all(
659 (salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
661 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
662 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
667 $matches = $exact_matches;
671 $matches //= $model->get;
675 value => $_->displayable_name,
676 label => $_->displayable_name,
678 $number => $_->$number,
680 type => $::form->{type},
681 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
685 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
688 sub action_test_page {
689 $_[0]->render('customer_vendor/test_page');
692 sub action_ajax_list_prices {
693 my ($self, %params) = @_;
695 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
696 my @columns = qw(partnumber description price);
697 my @visible = qw(partnumber description price);
698 my @sortable = qw(partnumber description price);
701 partnumber => { text => $::locale->text('Part Number'), sub => sub { $_[0]->parts->partnumber } },
702 description => { text => $::locale->text('Part Description'), sub => sub { $_[0]->parts->description } },
703 price => { text => $::locale->text('Price'), sub => sub { $::form->format_amount(\%::myconfig, $_[0]->price, 2) }, align => 'right' },
706 $::form->{sort_by} ||= 'partnumber';
707 $::form->{sort_dir} //= 1;
709 for my $col (@sortable) {
710 $column_defs{$col}{link} = $self->url_for(
711 action => 'ajax_list_prices',
712 callback => $::form->{callback},
714 id => $self->{cv}->id,
716 sort_dir => ($::form->{sort_by} eq $col ? 1 - $::form->{sort_dir} : $::form->{sort_dir})
720 map { $column_defs{$_}{visible} = 1 } @visible;
723 $pricegroup = $self->{cv}->pricegroup->pricegroup if $self->{cv}->pricegroup;
725 $report->set_columns(%column_defs);
726 $report->set_column_order(@columns);
727 $report->set_options(allow_pdf_export => 0, allow_csv_export => 0);
728 $report->set_sort_indicator($::form->{sort_by}, $::form->{sort_dir});
729 $report->set_export_options(@{ $params{report_generator_export_options} || [] });
730 $report->set_options(
731 %{ $params{report_generator_options} || {} },
732 output_format => 'HTML',
733 top_info_text => $::locale->text('Pricegroup') . ': ' . $pricegroup,
734 title => $::locale->text('Price List'),
737 my $sort_param = $::form->{sort_by} eq 'price' ? 'price' :
738 $::form->{sort_by} eq 'description' ? 'parts.description' :
740 $sort_param .= ' ' . ($::form->{sort_dir} ? 'ASC' : 'DESC');
741 my $prices = SL::DB::Manager::Price->get_all(where => [ pricegroup_id => $self->{cv}->pricegroup_id ],
742 sort_by => $sort_param,
743 with_objects => 'parts');
745 $self->report_generator_list_objects(report => $report, objects => $prices, layout => 0, header => 0);
749 return $::form->{db} eq 'vendor';
753 return $::form->{db} eq 'customer';
759 if ( defined($self->{_is_orphaned}) ) {
760 return $self->{_is_orphaned};
763 my $arap = $self->is_vendor ? 'ap' : 'ar';
766 my $cv = $self->is_vendor ? 'vendor' : 'customer';
770 FROM '. $arap .' AS a
771 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
778 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
784 FROM delivery_orders a
785 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
789 if ( $self->is_vendor ) {
792 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
796 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
798 return $self->{_is_orphaned} = !$dummy;
801 sub _copy_form_to_cvars {
802 my ($self, %params) = @_;
804 foreach my $cvar (@{ $params{target}->cvars_by_config }) {
805 my $value = $params{source}->{$cvar->config->name};
806 $value = $::form->parse_amount(\%::myconfig, $value) if $cvar->config->type eq 'number';
808 $cvar->value($value);
812 sub _instantiate_args {
815 my $curr_employee = SL::DB::Manager::Employee->current;
817 if ( $::form->{cv}->{id} ) {
818 if ( $self->is_vendor() ) {
819 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
821 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
824 $self->{cv} = $self->_new_customer_vendor_object;
826 $self->{cv}->assign_attributes(%{$::form->{cv}});
828 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
829 $self->{cv}->taxincluded_checked(undef);
832 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
834 if ( $::form->{note}->{id} ) {
835 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
836 $self->{note_followup} = $self->{note}->follow_up;
837 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
839 $self->{note} = SL::DB::Note->new();
840 $self->{note_followup} = SL::DB::FollowUp->new();
841 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
844 $self->{note}->assign_attributes(%{$::form->{note}});
845 $self->{note}->created_by($curr_employee->id);
846 $self->{note}->trans_module('ct');
848 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
849 $self->{note_followup}->note($self->{note});
850 $self->{note_followup}->created_by($curr_employee->id);
852 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
853 $self->{note_followup_link}->trans_info($self->{cv}->name);
855 if ( $::form->{shipto}->{shipto_id} ) {
856 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
858 $self->{shipto} = SL::DB::Shipto->new();
860 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
861 $self->{shipto}->module('CT');
863 if ( $::form->{contact}->{cp_id} ) {
864 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
866 $self->{contact} = $self->_new_contact_object;
868 $self->{contact}->assign_attributes(%{$::form->{contact}});
870 $self->_copy_form_to_cvars(target => $self->{cv}, source => $::form->{cv_cvars});
871 $self->_copy_form_to_cvars(target => $self->{contact}, source => $::form->{contact_cvars});
872 $self->_copy_form_to_cvars(target => $self->{shipto}, source => $::form->{shipto_cvars});
875 sub _load_customer_vendor {
878 if ( $self->is_vendor() ) {
879 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
881 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
884 if ( $::form->{note_id} ) {
885 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
886 $self->{note_followup} = $self->{note}->follow_up;
887 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
889 $self->{note} = SL::DB::Note->new();
890 $self->{note_followup} = SL::DB::FollowUp->new();
891 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
894 if ( $::form->{shipto_id} ) {
895 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
897 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
898 die($::locale->text('Error'));
901 $self->{shipto} = SL::DB::Shipto->new();
904 if ( $::form->{contact_id} ) {
905 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
907 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
908 die($::locale->text('Error'));
911 $self->{contact} = $self->_new_contact_object;
915 sub _check_customer_vendor_all_edit {
918 unless ($::auth->assert('customer_vendor_all_edit', 1)) {
919 die($::locale->text("You don't have the rights to edit this customer.") . "\n")
920 if $self->{cv}->is_customer and
921 SL::DB::Manager::Employee->current->id != $self->{cv}->salesman_id;
925 sub _create_customer_vendor {
928 $self->{cv} = $self->_new_customer_vendor_object;
929 $self->{cv}->currency_id($::instance_conf->get_currency_id());
931 $self->{note} = SL::DB::Note->new();
933 $self->{note_followup} = SL::DB::FollowUp->new();
935 $self->{shipto} = SL::DB::Shipto->new();
937 $self->{contact} = $self->_new_contact_object;
943 my $dbh = $::form->get_standard_dbh();
947 $self->{all_business} = SL::DB::Manager::Business->get_all();
949 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
951 $self->{all_greetings} = SL::DB::Manager::Greeting->get_all_sorted();
952 if ($self->{cv}->id && $self->{cv}->greeting && !grep {$self->{cv}->greeting eq $_->description} @{$self->{all_greetings}}) {
953 unshift @{$self->{all_greetings}}, (SL::DB::Greeting->new(description => $self->{cv}->greeting));
956 $self->{all_contact_titles} = SL::DB::Manager::ContactTitle->get_all_sorted();
957 foreach my $contact (@{ $self->{cv}->contacts }) {
958 if ($contact->cp_title && !grep {$contact->cp_title eq $_->description} @{$self->{all_contact_titles}}) {
959 unshift @{$self->{all_contact_titles}}, (SL::DB::ContactTitle->new(description => $contact->cp_title));
963 $self->{all_contact_departments} = SL::DB::Manager::ContactDepartment->get_all_sorted();
964 foreach my $contact (@{ $self->{cv}->contacts }) {
965 if ($contact->cp_abteilung && !grep {$contact->cp_abteilung eq $_->description} @{$self->{all_contact_departments}}) {
966 unshift @{$self->{all_contact_departments}}, (SL::DB::ContactDepartment->new(description => $contact->cp_abteilung));
970 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
972 $self->{all_languages} = SL::DB::Manager::Language->get_all();
974 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
976 if ( $::instance_conf->get_vertreter() ) {
984 selectall_hashref_query($::form, $dbh, $query)
988 if ( $business_ids->[0] ) {
989 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
991 $self->{all_salesman_customers} = [];
994 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
997 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->{cv}->payment_id,
1000 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
1002 if ($self->{cv}->is_customer) {
1003 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
1006 $self->{contacts} = $self->{cv}->contacts;
1007 $self->{contacts} ||= [];
1009 $self->{shiptos} = $self->{cv}->shipto;
1010 $self->{shiptos} ||= [];
1012 $self->{notes} = SL::DB::Manager::Note->get_all(
1014 trans_id => $self->{cv}->id,
1015 trans_module => 'ct',
1017 with_objects => ['follow_up'],
1020 if ( $self->is_vendor()) {
1021 $self->{open_items} = SL::DB::Manager::PurchaseInvoice->get_all_count(
1023 vendor_id => $self->{cv}->id,
1024 paid => {lt_sql => 'amount'},
1028 $self->{open_items} = SL::DB::Manager::Invoice->get_all_count(
1030 customer_id => $self->{cv}->id,
1031 paid => {lt_sql => 'amount'},
1036 if ( $self->is_vendor() ) {
1037 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
1039 vendor_id => $self->{cv}->id,
1044 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
1046 customer_id => $self->{cv}->id,
1052 if ($self->{cv}->number && $::instance_conf->get_webdav) {
1053 my $webdav = SL::Webdav->new(
1054 type => $self->is_customer ? 'customer'
1055 : $self->is_vendor ? 'vendor'
1057 number => $self->{cv}->number,
1059 my @all_objects = $webdav->get_all_objects;
1060 @{ $self->{template_args}->{WEBDAV} } = map { { name => $_->filename,
1062 link => File::Spec->catfile($_->full_filedescriptor),
1066 $self->{template_args} ||= {};
1068 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
1069 $::request->{layout}->add_javascripts('kivi.File.js');
1070 $::request->{layout}->add_javascripts('kivi.CustomerVendorTurnover.js');
1072 $self->_setup_form_action_bar;
1075 sub _setup_form_action_bar {
1078 for my $bar ($::request->layout->get('actionbar')) {
1083 submit => [ '#form', { action => "CustomerVendor/save" } ],
1084 checks => [ 'check_taxzone_and_ustid' ],
1085 accesskey => 'enter',
1088 t8('Save and Close'),
1089 submit => [ '#form', { action => "CustomerVendor/save_and_close" } ],
1090 checks => [ 'check_taxzone_and_ustid' ],
1092 ], # end of combobox "Save"
1095 action => [ t8('Workflow') ],
1097 t8('Save and AP Transaction'),
1098 submit => [ '#form', { action => "CustomerVendor/save_and_ap_transaction" } ],
1099 checks => [ 'check_taxzone_and_ustid' ],
1100 ]) x !!$self->is_vendor,
1102 t8('Save and AR Transaction'),
1103 submit => [ '#form', { action => "CustomerVendor/save_and_ar_transaction" } ],
1104 checks => [ 'check_taxzone_and_ustid' ],
1105 ]) x !$self->is_vendor,
1107 t8('Save and Invoice'),
1108 submit => [ '#form', { action => "CustomerVendor/save_and_invoice" } ],
1109 checks => [ 'check_taxzone_and_ustid' ],
1112 t8('Save and Order'),
1113 submit => [ '#form', { action => "CustomerVendor/save_and_order" } ],
1114 checks => [ 'check_taxzone_and_ustid' ],
1118 submit => [ '#form', { action => "CustomerVendor/save_and_rfq" } ],
1119 checks => [ 'check_taxzone_and_ustid' ],
1120 ]) x !!$self->is_vendor,
1122 t8('Save and Quotation'),
1123 submit => [ '#form', { action => "CustomerVendor/save_and_quotation" } ],
1124 checks => [ 'check_taxzone_and_ustid' ],
1125 ]) x !$self->is_vendor,
1126 ], # end of combobox "Workflow"
1130 submit => [ '#form', { action => "CustomerVendor/delete" } ],
1131 confirm => t8('Do you really want to delete this object?'),
1132 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.')
1133 : !$self->is_orphaned ? t8('This object has already been used.')
1141 call => [ 'kivi.CustomerVendor.showHistoryWindow', $self->{cv}->id ],
1142 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.') : undef,
1148 sub _prepare_cvar_configs_for_ajaj {
1149 my ($self, $cvars) = @_;
1154 my $result = { type => $cvar->config->type };
1156 if ($cvar->config->type eq 'number') {
1157 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
1159 } elsif ($result->{type} eq 'date') {
1160 $result->{value} = $cvar->value ? $cvar->value->to_kivitendo : undef;
1162 } elsif ($result->{type} =~ m{customer|vendor|part}) {
1163 my $object = $cvar->value;
1164 my $method = $result->{type} eq 'part' ? 'description' : 'name';
1166 $result->{id} = int($cvar->number_value) || undef;
1167 $result->{value} = $object ? $object->$method // '' : '';
1170 $result->{value} = $cvar->value;
1173 ( $cvar->config->name => $result )
1175 } grep { $_->is_valid } @{ $cvars }
1179 sub normalize_name {
1182 # check if feature is enabled (select normalize_vc_names from defaults)
1183 return unless ($::instance_conf->get_normalize_vc_names);
1185 return unless $self->{cv};
1186 my $name = $self->{cv}->name;
1190 $self->{cv}->name($name);
1193 sub home_address_for_google_maps {
1196 my $address = $::instance_conf->get_address // '';
1197 $address =~ s{^\s+|\s+$|\r+}{}g;
1198 $address =~ s{\n+}{,}g;
1199 $address =~ s{\s+}{ }g;
1204 sub init_customer_models {
1207 SL::Controller::Helper::GetModels->new(
1208 controller => $self,
1209 model => 'Customer',
1212 by => 'customernumber',
1215 customernumber => t8('Customer Number'),
1218 ( salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
1223 sub init_vendor_models {
1226 SL::Controller::Helper::GetModels->new(
1227 controller => $self,
1231 by => 'vendornumber',
1234 vendornumber => t8('Vendor Number'),
1239 sub init_zugferd_settings {
1241 [ -1, t8('Use settings from client configuration') ],
1242 @SL::ZUGFeRD::customer_settings,
1246 sub _new_customer_vendor_object {
1249 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
1253 custom_variables => [],
1257 sub _new_contact_object {
1260 return SL::DB::Contact->new(custom_variables => []);