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);
13 use SL::Controller::Helper::GetModels;
14 use SL::Controller::Helper::ReportGenerator;
15 use SL::Controller::Helper::ParseFilter;
20 use SL::DB::ContactDepartment;
21 use SL::DB::ContactTitle;
27 use SL::DB::PaymentTerm;
28 use SL::DB::Pricegroup;
32 use SL::DB::FollowUpLink;
36 use SL::DB::PurchaseInvoice;
41 use Rose::Object::MakeMethods::Generic (
42 'scalar --get_set_init' => [ qw(customer_models vendor_models) ],
46 __PACKAGE__->run_before(
48 $::auth->assert('customer_vendor_edit');
50 except => [ qw(ajaj_autocomplete) ],
52 __PACKAGE__->run_before(
56 'save_and_ap_transaction',
57 'save_and_ar_transaction',
69 __PACKAGE__->run_before(
70 '_load_customer_vendor',
81 # make sure this comes after _load_customer_vendor
82 __PACKAGE__->run_before(
83 '_check_customer_vendor_all_edit',
90 'save_and_ap_transaction',
91 'save_and_ar_transaction',
103 __PACKAGE__->run_before(
104 '_create_customer_vendor',
110 __PACKAGE__->run_before('normalize_name');
116 $self->_pre_render();
117 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer;
120 'customer_vendor/form',
121 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
122 %{$self->{template_args}}
129 $self->_pre_render();
131 'customer_vendor/form',
132 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
133 %{$self->{template_args}}
140 if ($::request->type eq 'json') {
145 $cv_hash = $self->{cv}->as_tree;
146 $cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
149 $self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
156 my @errors = $self->{cv}->validate;
158 flash('error', @errors);
159 $self->_pre_render();
161 'customer_vendor/form',
162 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
163 %{$self->{template_args}}
165 $::dispatcher->end_request;
168 $self->{cv}->greeting(trim $self->{cv}->greeting);
169 my $save_greeting = $self->{cv}->greeting
170 && $::instance_conf->get_vc_greetings_use_textfield
171 && SL::DB::Manager::Greeting->get_all_count(where => [description => $self->{cv}->greeting]) == 0;
173 $self->{contact}->cp_title(trim($self->{contact}->cp_title));
174 my $save_contact_title = $self->{contact}->cp_title
175 && $::instance_conf->get_contact_titles_use_textfield
176 && SL::DB::Manager::ContactTitle->get_all_count(where => [description => $self->{contact}->cp_title]) == 0;
178 $self->{contact}->cp_abteilung(trim($self->{contact}->cp_abteilung));
179 my $save_contact_department = $self->{contact}->cp_abteilung
180 && $::instance_conf->get_contact_departments_use_textfield
181 && SL::DB::Manager::ContactDepartment->get_all_count(where => [description => $self->{contact}->cp_abteilung]) == 0;
183 my $db = $self->{cv}->db;
185 $db->with_transaction(sub {
187 if ( $self->is_vendor() ) {
188 if ( $self->{cv}->vendornumber ) {
189 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
192 if ( $self->{cv}->customernumber ) {
193 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
197 foreach my $entry (@{$cvs_by_nr}) {
198 if( $entry->id != $self->{cv}->id ) {
200 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
202 $::form->error($msg);
206 $self->{cv}->save(cascade => 1);
208 SL::DB::Greeting->new(description => $self->{cv}->greeting)->save if $save_greeting;
210 $self->{contact}->cp_cv_id($self->{cv}->id);
211 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
212 SL::DB::ContactTitle ->new(description => $self->{contact}->cp_title) ->save if $save_contact_title;
213 SL::DB::ContactDepartment->new(description => $self->{contact}->cp_abteilung)->save if $save_contact_department;
215 $self->{contact}->save(cascade => 1);
218 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
220 if ( !$self->{note_followup}->follow_up_date ) {
221 $::form->error($::locale->text('Date missing!'));
224 $self->{note}->trans_id($self->{cv}->id);
225 $self->{note}->save();
227 $self->{note_followup}->save();
229 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
230 $self->{note_followup_link}->trans_id($self->{cv}->id);
231 $self->{note_followup_link}->save();
233 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
236 $self->{shipto}->trans_id($self->{cv}->id);
237 if(any { $self->{shipto}->$_ ne '' } qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax shiptoemail)) {
238 $self->{shipto}->save(cascade => 1);
241 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
242 SL::DB::History->new(
243 trans_id => $self->{cv}->id,
244 snumbers => $snumbers,
245 employee_id => SL::DB::Manager::Employee->current->id,
249 if ( $::form->{delete_notes} ) {
250 foreach my $note_id (@{ $::form->{delete_notes} }) {
251 my $note = SL::DB::Note->new(id => $note_id)->load();
252 if ( $note->follow_up ) {
253 if ( $note->follow_up->follow_up_link ) {
254 $note->follow_up->follow_up_link->delete(cascade => 'delete');
256 $note->follow_up->delete(cascade => 'delete');
258 $note->delete(cascade => 'delete');
263 }) || die($db->error);
272 my @redirect_params = (
274 id => $self->{cv}->id,
275 db => ($self->is_vendor() ? 'vendor' : 'customer'),
278 if ( $self->{contact}->cp_id ) {
279 push(@redirect_params, contact_id => $self->{contact}->cp_id);
282 if ( $self->{shipto}->shipto_id ) {
283 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
286 $self->redirect_to(@redirect_params);
289 sub action_save_and_close {
294 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
295 $::form->redirect($msg);
299 my ($self, $script) = @_;
301 $::auth->assert('gl_transactions | ap_transactions | ar_transactions'.
302 '| invoice_edit | vendor_invoice_edit | ' .
303 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
307 my $name = $::form->escape($self->{cv}->name, 1);
308 my $db = $self->is_vendor() ? 'vendor' : 'customer';
311 if ($::instance_conf->get_feature_experimental_order && 'oe.pl' eq $script) {
312 $script = 'controller.pl';
313 $action = 'Order/' . $action;
316 my $url = $self->url_for(
317 controller => $script,
320 $db .'_id' => $self->{cv}->id,
322 type => $::form->{type},
323 callback => $::form->{callback},
326 print $::form->redirect_header($url);
329 sub action_save_and_ar_transaction {
332 $main::auth->assert('ar_transactions');
334 $self->_transaction('ar.pl');
337 sub action_save_and_ap_transaction {
340 $main::auth->assert('ap_transactions');
342 $self->_transaction('ap.pl');
345 sub action_save_and_invoice {
348 if ( $self->is_vendor() ) {
349 $::auth->assert('vendor_invoice_edit');
351 $::auth->assert('invoice_edit');
354 $::form->{type} = 'invoice';
355 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
358 sub action_save_and_order {
361 if ( $self->is_vendor() ) {
362 $::auth->assert('purchase_order_edit');
364 $::auth->assert('sales_order_edit');
367 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
368 $self->_transaction('oe.pl');
371 sub action_save_and_rfq {
374 $::auth->assert('request_quotation_edit');
376 $::form->{type} = 'request_quotation';
377 $self->_transaction('oe.pl');
380 sub action_save_and_quotation {
383 $::auth->assert('sales_quotation_edit');
385 $::form->{type} = 'sales_quotation';
386 $self->_transaction('oe.pl');
392 my $db = $self->{cv}->db;
394 if( !$self->is_orphaned() ) {
395 $self->action_edit();
398 $db->with_transaction(sub {
399 $self->{cv}->delete(cascade => 1);
401 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
402 SL::DB::History->new(
403 trans_id => $self->{cv}->id,
404 snumbers => $snumbers,
405 employee_id => SL::DB::Manager::Employee->current->id,
406 addition => 'DELETED',
408 }) || die($db->error);
410 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
411 $::form->redirect($msg);
417 sub action_delete_contact {
420 my $db = $self->{contact}->db;
422 if ( !$self->{contact}->cp_id ) {
423 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
426 $db->with_transaction(sub {
427 if ( $self->{contact}->used ) {
428 $self->{contact}->detach();
429 $self->{contact}->save();
430 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
432 $self->{contact}->delete(cascade => 1);
433 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
437 }) || die($db->error);
439 $self->{contact} = $self->_new_contact_object;
442 $self->action_edit();
445 sub action_delete_shipto {
448 my $db = $self->{shipto}->db;
450 if ( !$self->{shipto}->shipto_id ) {
451 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
454 $db->with_transaction(sub {
455 if ( $self->{shipto}->used ) {
456 $self->{shipto}->detach();
457 $self->{shipto}->save(cascade => 1);
458 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
460 $self->{shipto}->delete(cascade => 1);
461 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
465 }) || die($db->error);
467 $self->{shipto} = SL::DB::Shipto->new();
470 $self->action_edit();
478 controller => 'ct.pl',
480 db => $self->is_vendor() ? 'vendor' : 'customer',
483 if ( $::form->{callback} ) {
484 push(@url_params, callback => $::form->{callback});
487 $self->redirect_to(@url_params);
491 sub action_search_contact {
494 my $url = 'ct.pl?action=search_contact&db=customer';
496 if ( $::form->{callback} ) {
497 $url .= '&callback='. $::form->escape($::form->{callback});
500 print $::form->redirect_header($url);
503 sub action_get_delivery {
506 $::auth->assert('sales_all_edit') if $self->is_customer();
507 $::auth->assert('purchase_all_edit') if $self->is_vendor();
509 my $dbh = $::form->get_standard_dbh();
511 my ($arap, $db, $qty_sign);
512 if ( $self->is_vendor() ) {
515 $qty_sign = ' * -1 AS qty';
522 my $where = ' WHERE 1=1';
525 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
526 $where .= " AND ${arap}.shipto_id = ?";
527 push(@values, $::form->{shipto_id});
529 $where .= " AND ${arap}.${db}_id = ?";
530 push(@values, $::form->{id});
533 if ( $::form->{delivery_from} ) {
534 $where .= " AND ${arap}.transdate >= ?";
535 push(@values, conv_date($::form->{delivery_from}));
538 if ( $::form->{delivery_to} ) {
539 $where .= " AND ${arap}.transdate <= ?";
540 push(@values, conv_date($::form->{delivery_to}));
559 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
562 ON ${arap}.id = i.trans_id
568 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
569 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
572 ORDER BY ${arap}.transdate DESC LIMIT 15";
574 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
576 $self->render('customer_vendor/get_delivery', { layout => 0 });
579 sub action_ajaj_get_shipto {
586 my $name = 'shipto'. $_;
587 $name => $self->{shipto}->$name;
589 qw(_id name department_1 department_2 street zipcode city gln country contact phone fax email)
593 $data->{shipto_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{shipto}->cvars_by_config);
595 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
598 sub action_ajaj_get_contact {
606 my $name = 'cp_'. $_;
608 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
609 $name => $self->{contact}->$name->to_lxoffice;
611 $name => $self->{contact}->$name;
615 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
616 satphone satfax project street zipcode city privatphone privatemail birthday main
621 $data->{contact_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{contact}->cvars_by_config);
623 # avoid two or more main_cp
624 my $has_main_cp = grep { $_->cp_main == 1 } @{ $self->{cv}->contacts };
625 $data->{contact}->{disable_cp_main} = 1 if ($has_main_cp && !$data->{contact}->{cp_main});
627 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
630 sub action_ajaj_autocomplete {
631 my ($self, %params) = @_;
633 my ($model, $manager, $number, $matches);
635 # first see if this is customer or vendor picking
636 if ($::form->{type} eq 'customer') {
637 $model = $self->customer_models;
638 $manager = 'SL::DB::Manager::Customer';
639 $number = 'customernumber';
640 } elsif ($::form->{type} eq 'vendor') {
641 $model = $self->vendor_models;
642 $manager = 'SL::DB::Manager::Vendor';
643 $number = 'vendornumber';
645 die "unknown type $::form->{type}";
648 # if someone types something, and hits enter, assume he entered the full name.
649 # if something matches, treat that as the sole match
650 # unfortunately get_models can't do more than one per package atm, so we do it
651 # the oldfashioned way.
652 if ($::form->{prefer_exact}) {
654 if (1 == scalar @{ $exact_matches = $manager->get_all(
657 (salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
659 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
660 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
665 $matches = $exact_matches;
669 $matches //= $model->get;
673 value => $_->displayable_name,
674 label => $_->displayable_name,
676 $number => $_->$number,
678 type => $::form->{type},
679 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
683 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
686 sub action_test_page {
687 $_[0]->render('customer_vendor/test_page');
690 sub action_ajax_list_prices {
691 my ($self, %params) = @_;
693 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
694 my @columns = qw(partnumber description price);
695 my @visible = qw(partnumber description price);
696 my @sortable = qw(partnumber description price);
699 partnumber => { text => $::locale->text('Part Number'), sub => sub { $_[0]->parts->partnumber } },
700 description => { text => $::locale->text('Part Description'), sub => sub { $_[0]->parts->description } },
701 price => { text => $::locale->text('Price'), sub => sub { $::form->format_amount(\%::myconfig, $_[0]->price, 2) }, align => 'right' },
704 $::form->{sort_by} ||= 'partnumber';
705 $::form->{sort_dir} //= 1;
707 for my $col (@sortable) {
708 $column_defs{$col}{link} = $self->url_for(
709 action => 'ajax_list_prices',
710 callback => $::form->{callback},
712 id => $self->{cv}->id,
714 sort_dir => ($::form->{sort_by} eq $col ? 1 - $::form->{sort_dir} : $::form->{sort_dir})
718 map { $column_defs{$_}{visible} = 1 } @visible;
721 $pricegroup = $self->{cv}->pricegroup->pricegroup if $self->{cv}->pricegroup;
723 $report->set_columns(%column_defs);
724 $report->set_column_order(@columns);
725 $report->set_options(allow_pdf_export => 0, allow_csv_export => 0);
726 $report->set_sort_indicator($::form->{sort_by}, $::form->{sort_dir});
727 $report->set_export_options(@{ $params{report_generator_export_options} || [] });
728 $report->set_options(
729 %{ $params{report_generator_options} || {} },
730 output_format => 'HTML',
731 top_info_text => $::locale->text('Pricegroup') . ': ' . $pricegroup,
732 title => $::locale->text('Price List'),
735 my $sort_param = $::form->{sort_by} eq 'price' ? 'price' :
736 $::form->{sort_by} eq 'description' ? 'parts.description' :
738 $sort_param .= ' ' . ($::form->{sort_dir} ? 'ASC' : 'DESC');
739 my $prices = SL::DB::Manager::Price->get_all(where => [ pricegroup_id => $self->{cv}->pricegroup_id ],
740 sort_by => $sort_param,
741 with_objects => 'parts');
743 $self->report_generator_list_objects(report => $report, objects => $prices, layout => 0, header => 0);
747 return $::form->{db} eq 'vendor';
751 return $::form->{db} eq 'customer';
757 if ( defined($self->{_is_orphaned}) ) {
758 return $self->{_is_orphaned};
761 my $arap = $self->is_vendor ? 'ap' : 'ar';
764 my $cv = $self->is_vendor ? 'vendor' : 'customer';
768 FROM '. $arap .' AS a
769 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
776 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
782 FROM delivery_orders a
783 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
787 if ( $self->is_vendor ) {
790 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
794 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
796 return $self->{_is_orphaned} = !$dummy;
799 sub _copy_form_to_cvars {
800 my ($self, %params) = @_;
802 foreach my $cvar (@{ $params{target}->cvars_by_config }) {
803 my $value = $params{source}->{$cvar->config->name};
804 $value = $::form->parse_amount(\%::myconfig, $value) if $cvar->config->type eq 'number';
806 $cvar->value($value);
810 sub _instantiate_args {
813 my $curr_employee = SL::DB::Manager::Employee->current;
815 if ( $::form->{cv}->{id} ) {
816 if ( $self->is_vendor() ) {
817 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
819 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
822 $self->{cv} = $self->_new_customer_vendor_object;
824 $self->{cv}->assign_attributes(%{$::form->{cv}});
826 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
827 $self->{cv}->taxincluded_checked(undef);
830 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
832 if ( $::form->{note}->{id} ) {
833 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
834 $self->{note_followup} = $self->{note}->follow_up;
835 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
837 $self->{note} = SL::DB::Note->new();
838 $self->{note_followup} = SL::DB::FollowUp->new();
839 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
842 $self->{note}->assign_attributes(%{$::form->{note}});
843 $self->{note}->created_by($curr_employee->id);
844 $self->{note}->trans_module('ct');
846 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
847 $self->{note_followup}->note($self->{note});
848 $self->{note_followup}->created_by($curr_employee->id);
850 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
851 $self->{note_followup_link}->trans_info($self->{cv}->name);
853 if ( $::form->{shipto}->{shipto_id} ) {
854 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
856 $self->{shipto} = SL::DB::Shipto->new();
858 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
859 $self->{shipto}->module('CT');
861 if ( $::form->{contact}->{cp_id} ) {
862 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
864 $self->{contact} = $self->_new_contact_object;
866 $self->{contact}->assign_attributes(%{$::form->{contact}});
868 $self->_copy_form_to_cvars(target => $self->{cv}, source => $::form->{cv_cvars});
869 $self->_copy_form_to_cvars(target => $self->{contact}, source => $::form->{contact_cvars});
870 $self->_copy_form_to_cvars(target => $self->{shipto}, source => $::form->{shipto_cvars});
873 sub _load_customer_vendor {
876 if ( $self->is_vendor() ) {
877 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
879 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
882 if ( $::form->{note_id} ) {
883 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
884 $self->{note_followup} = $self->{note}->follow_up;
885 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
887 $self->{note} = SL::DB::Note->new();
888 $self->{note_followup} = SL::DB::FollowUp->new();
889 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
892 if ( $::form->{shipto_id} ) {
893 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
895 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
896 die($::locale->text('Error'));
899 $self->{shipto} = SL::DB::Shipto->new();
902 if ( $::form->{contact_id} ) {
903 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
905 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
906 die($::locale->text('Error'));
909 $self->{contact} = $self->_new_contact_object;
913 sub _check_customer_vendor_all_edit {
916 unless ($::auth->assert('customer_vendor_all_edit', 1)) {
917 die($::locale->text("You don't have the rights to edit this customer.") . "\n")
918 if $self->{cv}->is_customer and
919 SL::DB::Manager::Employee->current->id != $self->{cv}->salesman_id;
923 sub _create_customer_vendor {
926 $self->{cv} = $self->_new_customer_vendor_object;
927 $self->{cv}->currency_id($::instance_conf->get_currency_id());
929 $self->{note} = SL::DB::Note->new();
931 $self->{note_followup} = SL::DB::FollowUp->new();
933 $self->{shipto} = SL::DB::Shipto->new();
935 $self->{contact} = $self->_new_contact_object;
941 my $dbh = $::form->get_standard_dbh();
945 $self->{all_business} = SL::DB::Manager::Business->get_all();
947 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
949 $self->{all_greetings} = SL::DB::Manager::Greeting->get_all_sorted();
950 if ($self->{cv}->id && $self->{cv}->greeting && !grep {$self->{cv}->greeting eq $_->description} @{$self->{all_greetings}}) {
951 unshift @{$self->{all_greetings}}, (SL::DB::Greeting->new(description => $self->{cv}->greeting));
954 $self->{all_contact_titles} = SL::DB::Manager::ContactTitle->get_all_sorted();
955 foreach my $contact (@{ $self->{cv}->contacts }) {
956 if ($contact->cp_title && !grep {$contact->cp_title eq $_->description} @{$self->{all_contact_titles}}) {
957 unshift @{$self->{all_contact_titles}}, (SL::DB::ContactTitle->new(description => $contact->cp_title));
961 $self->{all_contact_departments} = SL::DB::Manager::ContactDepartment->get_all_sorted();
962 foreach my $contact (@{ $self->{cv}->contacts }) {
963 if ($contact->cp_abteilung && !grep {$contact->cp_abteilung eq $_->description} @{$self->{all_contact_departments}}) {
964 unshift @{$self->{all_contact_departments}}, (SL::DB::ContactDepartment->new(description => $contact->cp_abteilung));
968 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
970 $self->{all_languages} = SL::DB::Manager::Language->get_all();
972 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
974 if ( $::instance_conf->get_vertreter() ) {
982 selectall_hashref_query($::form, $dbh, $query)
986 if ( $business_ids->[0] ) {
987 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
989 $self->{all_salesman_customers} = [];
992 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
995 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->{cv}->payment_id,
998 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
1000 if ($self->{cv}->is_customer) {
1001 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
1004 $self->{contacts} = $self->{cv}->contacts;
1005 $self->{contacts} ||= [];
1007 $self->{shiptos} = $self->{cv}->shipto;
1008 $self->{shiptos} ||= [];
1010 $self->{notes} = SL::DB::Manager::Note->get_all(
1012 trans_id => $self->{cv}->id,
1013 trans_module => 'ct',
1015 with_objects => ['follow_up'],
1018 if ( $self->is_vendor()) {
1019 $self->{open_items} = SL::DB::Manager::PurchaseInvoice->get_all_count(
1021 vendor_id => $self->{cv}->id,
1022 paid => {lt_sql => 'amount'},
1026 $self->{open_items} = SL::DB::Manager::Invoice->get_all_count(
1028 customer_id => $self->{cv}->id,
1029 paid => {lt_sql => 'amount'},
1034 if ( $self->is_vendor() ) {
1035 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
1037 vendor_id => $self->{cv}->id,
1042 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
1044 customer_id => $self->{cv}->id,
1049 $self->{template_args} ||= {};
1051 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
1052 $::request->{layout}->add_javascripts('kivi.File.js');
1053 $::request->{layout}->add_javascripts('kivi.CustomerVendorTurnover.js');
1055 $self->_setup_form_action_bar;
1058 sub _setup_form_action_bar {
1061 for my $bar ($::request->layout->get('actionbar')) {
1066 submit => [ '#form', { action => "CustomerVendor/save" } ],
1067 checks => [ 'check_taxzone_and_ustid' ],
1068 accesskey => 'enter',
1071 t8('Save and Close'),
1072 submit => [ '#form', { action => "CustomerVendor/save_and_close" } ],
1073 checks => [ 'check_taxzone_and_ustid' ],
1075 ], # end of combobox "Save"
1078 action => [ t8('Workflow') ],
1080 t8('Save and AP Transaction'),
1081 submit => [ '#form', { action => "CustomerVendor/save_and_ap_transaction" } ],
1082 checks => [ 'check_taxzone_and_ustid' ],
1083 ]) x !!$self->is_vendor,
1085 t8('Save and AR Transaction'),
1086 submit => [ '#form', { action => "CustomerVendor/save_and_ar_transaction" } ],
1087 checks => [ 'check_taxzone_and_ustid' ],
1088 ]) x !$self->is_vendor,
1090 t8('Save and Invoice'),
1091 submit => [ '#form', { action => "CustomerVendor/save_and_invoice" } ],
1092 checks => [ 'check_taxzone_and_ustid' ],
1095 t8('Save and Order'),
1096 submit => [ '#form', { action => "CustomerVendor/save_and_order" } ],
1097 checks => [ 'check_taxzone_and_ustid' ],
1101 submit => [ '#form', { action => "CustomerVendor/save_and_rfq" } ],
1102 checks => [ 'check_taxzone_and_ustid' ],
1103 ]) x !!$self->is_vendor,
1105 t8('Save and Quotation'),
1106 submit => [ '#form', { action => "CustomerVendor/save_and_quotation" } ],
1107 checks => [ 'check_taxzone_and_ustid' ],
1108 ]) x !$self->is_vendor,
1109 ], # end of combobox "Workflow"
1113 submit => [ '#form', { action => "CustomerVendor/delete" } ],
1114 confirm => t8('Do you really want to delete this object?'),
1115 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.')
1116 : !$self->is_orphaned ? t8('This object has already been used.')
1124 call => [ 'kivi.CustomerVendor.showHistoryWindow', $self->{cv}->id ],
1125 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.') : undef,
1131 sub _prepare_cvar_configs_for_ajaj {
1132 my ($self, $cvars) = @_;
1137 my $result = { type => $cvar->config->type };
1139 if ($cvar->config->type eq 'number') {
1140 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
1142 } elsif ($result->{type} eq 'date') {
1143 $result->{value} = $cvar->value ? $cvar->value->to_kivitendo : undef;
1145 } elsif ($result->{type} =~ m{customer|vendor|part}) {
1146 my $object = $cvar->value;
1147 my $method = $result->{type} eq 'part' ? 'description' : 'name';
1149 $result->{id} = int($cvar->number_value) || undef;
1150 $result->{value} = $object ? $object->$method // '' : '';
1153 $result->{value} = $cvar->value;
1156 ( $cvar->config->name => $result )
1158 } grep { $_->is_valid } @{ $cvars }
1162 sub normalize_name {
1165 # check if feature is enabled (select normalize_vc_names from defaults)
1166 return unless ($::instance_conf->get_normalize_vc_names);
1168 return unless $self->{cv};
1169 my $name = $self->{cv}->name;
1173 $self->{cv}->name($name);
1176 sub home_address_for_google_maps {
1179 my $address = $::instance_conf->get_address // '';
1180 $address =~ s{^\s+|\s+$|\r+}{}g;
1181 $address =~ s{\n+}{,}g;
1182 $address =~ s{\s+}{ }g;
1187 sub init_customer_models {
1190 SL::Controller::Helper::GetModels->new(
1191 controller => $self,
1192 model => 'Customer',
1195 by => 'customernumber',
1198 customernumber => t8('Customer Number'),
1201 ( salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
1206 sub init_vendor_models {
1209 SL::Controller::Helper::GetModels->new(
1210 controller => $self,
1214 by => 'vendornumber',
1217 vendornumber => t8('Vendor Number'),
1222 sub _new_customer_vendor_object {
1225 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
1229 custom_variables => [],
1233 sub _new_contact_object {
1236 return SL::DB::Contact->new(custom_variables => []);