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);
14 use SL::Controller::Helper::GetModels;
15 use SL::Controller::Helper::ReportGenerator;
16 use SL::Controller::Helper::ParseFilter;
21 use SL::DB::ContactDepartment;
22 use SL::DB::ContactTitle;
28 use SL::DB::PaymentTerm;
29 use SL::DB::Pricegroup;
33 use SL::DB::FollowUpLink;
37 use SL::DB::PurchaseInvoice;
42 use Rose::Object::MakeMethods::Generic (
43 'scalar --get_set_init' => [ qw(customer_models vendor_models) ],
47 __PACKAGE__->run_before(
49 $::auth->assert('customer_vendor_edit');
51 except => [ qw(ajaj_autocomplete) ],
53 __PACKAGE__->run_before(
57 'save_and_ap_transaction',
58 'save_and_ar_transaction',
70 __PACKAGE__->run_before(
71 '_load_customer_vendor',
82 # make sure this comes after _load_customer_vendor
83 __PACKAGE__->run_before(
84 '_check_customer_vendor_all_edit',
91 'save_and_ap_transaction',
92 'save_and_ar_transaction',
104 __PACKAGE__->run_before(
105 '_create_customer_vendor',
111 __PACKAGE__->run_before('normalize_name');
117 $self->_pre_render();
118 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer;
121 'customer_vendor/form',
122 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
123 %{$self->{template_args}}
130 $self->_pre_render();
132 'customer_vendor/form',
133 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
134 %{$self->{template_args}}
141 if ($::request->type eq 'json') {
146 $cv_hash = $self->{cv}->as_tree;
147 $cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
150 $self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
157 my @errors = $self->{cv}->validate;
159 flash('error', @errors);
160 $self->_pre_render();
162 'customer_vendor/form',
163 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
164 %{$self->{template_args}}
166 $::dispatcher->end_request;
169 $self->{cv}->greeting(trim $self->{cv}->greeting);
170 my $save_greeting = $self->{cv}->greeting
171 && $::instance_conf->get_vc_greetings_use_textfield
172 && SL::DB::Manager::Greeting->get_all_count(where => [description => $self->{cv}->greeting]) == 0;
174 $self->{contact}->cp_title(trim($self->{contact}->cp_title));
175 my $save_contact_title = $self->{contact}->cp_title
176 && $::instance_conf->get_contact_titles_use_textfield
177 && SL::DB::Manager::ContactTitle->get_all_count(where => [description => $self->{contact}->cp_title]) == 0;
179 $self->{contact}->cp_abteilung(trim($self->{contact}->cp_abteilung));
180 my $save_contact_department = $self->{contact}->cp_abteilung
181 && $::instance_conf->get_contact_departments_use_textfield
182 && SL::DB::Manager::ContactDepartment->get_all_count(where => [description => $self->{contact}->cp_abteilung]) == 0;
184 my $db = $self->{cv}->db;
186 $db->with_transaction(sub {
188 if ( $self->is_vendor() ) {
189 if ( $self->{cv}->vendornumber ) {
190 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
193 if ( $self->{cv}->customernumber ) {
194 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
198 foreach my $entry (@{$cvs_by_nr}) {
199 if( $entry->id != $self->{cv}->id ) {
201 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
203 $::form->error($msg);
207 $self->{cv}->save(cascade => 1);
209 SL::DB::Greeting->new(description => $self->{cv}->greeting)->save if $save_greeting;
211 $self->{contact}->cp_cv_id($self->{cv}->id);
212 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
213 SL::DB::ContactTitle ->new(description => $self->{contact}->cp_title) ->save if $save_contact_title;
214 SL::DB::ContactDepartment->new(description => $self->{contact}->cp_abteilung)->save if $save_contact_department;
216 $self->{contact}->save(cascade => 1);
219 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
221 if ( !$self->{note_followup}->follow_up_date ) {
222 $::form->error($::locale->text('Date missing!'));
225 $self->{note}->trans_id($self->{cv}->id);
226 $self->{note}->save();
228 $self->{note_followup}->save();
230 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
231 $self->{note_followup_link}->trans_id($self->{cv}->id);
232 $self->{note_followup_link}->save();
234 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
237 $self->{shipto}->trans_id($self->{cv}->id);
238 if(any { $self->{shipto}->$_ ne '' } qw(shiptoname shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact shiptophone shiptofax shiptoemail)) {
239 $self->{shipto}->save(cascade => 1);
242 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
243 SL::DB::History->new(
244 trans_id => $self->{cv}->id,
245 snumbers => $snumbers,
246 employee_id => SL::DB::Manager::Employee->current->id,
250 if ( $::form->{delete_notes} ) {
251 foreach my $note_id (@{ $::form->{delete_notes} }) {
252 my $note = SL::DB::Note->new(id => $note_id)->load();
253 if ( $note->follow_up ) {
254 if ( $note->follow_up->follow_up_link ) {
255 $note->follow_up->follow_up_link->delete(cascade => 'delete');
257 $note->follow_up->delete(cascade => 'delete');
259 $note->delete(cascade => 'delete');
264 }) || die($db->error);
273 my @redirect_params = (
275 id => $self->{cv}->id,
276 db => ($self->is_vendor() ? 'vendor' : 'customer'),
279 if ( $self->{contact}->cp_id ) {
280 push(@redirect_params, contact_id => $self->{contact}->cp_id);
283 if ( $self->{shipto}->shipto_id ) {
284 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
287 $self->redirect_to(@redirect_params);
290 sub action_save_and_close {
295 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
296 $::form->redirect($msg);
300 my ($self, $script) = @_;
302 $::auth->assert('gl_transactions | ap_transactions | ar_transactions'.
303 '| invoice_edit | vendor_invoice_edit | ' .
304 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
308 my $name = $::form->escape($self->{cv}->name, 1);
309 my $db = $self->is_vendor() ? 'vendor' : 'customer';
312 if ($::instance_conf->get_feature_experimental_order && 'oe.pl' eq $script) {
313 $script = 'controller.pl';
314 $action = 'Order/' . $action;
317 my $url = $self->url_for(
318 controller => $script,
321 $db .'_id' => $self->{cv}->id,
323 type => $::form->{type},
324 callback => $::form->{callback},
327 print $::form->redirect_header($url);
330 sub action_save_and_ar_transaction {
333 $main::auth->assert('ar_transactions');
335 $self->_transaction('ar.pl');
338 sub action_save_and_ap_transaction {
341 $main::auth->assert('ap_transactions');
343 $self->_transaction('ap.pl');
346 sub action_save_and_invoice {
349 if ( $self->is_vendor() ) {
350 $::auth->assert('vendor_invoice_edit');
352 $::auth->assert('invoice_edit');
355 $::form->{type} = 'invoice';
356 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
359 sub action_save_and_order {
362 if ( $self->is_vendor() ) {
363 $::auth->assert('purchase_order_edit');
365 $::auth->assert('sales_order_edit');
368 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
369 $self->_transaction('oe.pl');
372 sub action_save_and_rfq {
375 $::auth->assert('request_quotation_edit');
377 $::form->{type} = 'request_quotation';
378 $self->_transaction('oe.pl');
381 sub action_save_and_quotation {
384 $::auth->assert('sales_quotation_edit');
386 $::form->{type} = 'sales_quotation';
387 $self->_transaction('oe.pl');
393 my $db = $self->{cv}->db;
395 if( !$self->is_orphaned() ) {
396 $self->action_edit();
399 $db->with_transaction(sub {
400 $self->{cv}->delete(cascade => 1);
402 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
403 SL::DB::History->new(
404 trans_id => $self->{cv}->id,
405 snumbers => $snumbers,
406 employee_id => SL::DB::Manager::Employee->current->id,
407 addition => 'DELETED',
409 }) || die($db->error);
411 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
412 $::form->redirect($msg);
418 sub action_delete_contact {
421 my $db = $self->{contact}->db;
423 if ( !$self->{contact}->cp_id ) {
424 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
427 $db->with_transaction(sub {
428 if ( $self->{contact}->used ) {
429 $self->{contact}->detach();
430 $self->{contact}->save();
431 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
433 $self->{contact}->delete(cascade => 1);
434 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
438 }) || die($db->error);
440 $self->{contact} = $self->_new_contact_object;
443 $self->action_edit();
446 sub action_delete_shipto {
449 my $db = $self->{shipto}->db;
451 if ( !$self->{shipto}->shipto_id ) {
452 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
455 $db->with_transaction(sub {
456 if ( $self->{shipto}->used ) {
457 $self->{shipto}->detach();
458 $self->{shipto}->save(cascade => 1);
459 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
461 $self->{shipto}->delete(cascade => 1);
462 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
466 }) || die($db->error);
468 $self->{shipto} = SL::DB::Shipto->new();
471 $self->action_edit();
479 controller => 'ct.pl',
481 db => $self->is_vendor() ? 'vendor' : 'customer',
484 if ( $::form->{callback} ) {
485 push(@url_params, callback => $::form->{callback});
488 $self->redirect_to(@url_params);
492 sub action_search_contact {
495 my $url = 'ct.pl?action=search_contact&db=customer';
497 if ( $::form->{callback} ) {
498 $url .= '&callback='. $::form->escape($::form->{callback});
501 print $::form->redirect_header($url);
504 sub action_get_delivery {
507 $::auth->assert('sales_all_edit') if $self->is_customer();
508 $::auth->assert('purchase_all_edit') if $self->is_vendor();
510 my $dbh = $::form->get_standard_dbh();
512 my ($arap, $db, $qty_sign);
513 if ( $self->is_vendor() ) {
516 $qty_sign = ' * -1 AS qty';
523 my $where = ' WHERE 1=1';
526 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
527 $where .= " AND ${arap}.shipto_id = ?";
528 push(@values, $::form->{shipto_id});
530 $where .= " AND ${arap}.${db}_id = ?";
531 push(@values, $::form->{id});
534 if ( $::form->{delivery_from} ) {
535 $where .= " AND ${arap}.transdate >= ?";
536 push(@values, conv_date($::form->{delivery_from}));
539 if ( $::form->{delivery_to} ) {
540 $where .= " AND ${arap}.transdate <= ?";
541 push(@values, conv_date($::form->{delivery_to}));
560 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
563 ON ${arap}.id = i.trans_id
569 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
570 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
573 ORDER BY ${arap}.transdate DESC LIMIT 15";
575 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
577 $self->render('customer_vendor/get_delivery', { layout => 0 });
580 sub action_ajaj_get_shipto {
587 my $name = 'shipto'. $_;
588 $name => $self->{shipto}->$name;
590 qw(_id name department_1 department_2 street zipcode city gln country contact phone fax email)
594 $data->{shipto_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{shipto}->cvars_by_config);
596 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
599 sub action_ajaj_get_contact {
607 my $name = 'cp_'. $_;
609 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
610 $name => $self->{contact}->$name->to_lxoffice;
612 $name => $self->{contact}->$name;
616 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
617 satphone satfax project street zipcode city privatphone privatemail birthday main
622 $data->{contact_cvars} = $self->_prepare_cvar_configs_for_ajaj($self->{contact}->cvars_by_config);
624 # avoid two or more main_cp
625 my $has_main_cp = grep { $_->cp_main == 1 } @{ $self->{cv}->contacts };
626 $data->{contact}->{disable_cp_main} = 1 if ($has_main_cp && !$data->{contact}->{cp_main});
628 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
631 sub action_ajaj_autocomplete {
632 my ($self, %params) = @_;
634 my ($model, $manager, $number, $matches);
636 # first see if this is customer or vendor picking
637 if ($::form->{type} eq 'customer') {
638 $model = $self->customer_models;
639 $manager = 'SL::DB::Manager::Customer';
640 $number = 'customernumber';
641 } elsif ($::form->{type} eq 'vendor') {
642 $model = $self->vendor_models;
643 $manager = 'SL::DB::Manager::Vendor';
644 $number = 'vendornumber';
646 die "unknown type $::form->{type}";
649 # if someone types something, and hits enter, assume he entered the full name.
650 # if something matches, treat that as the sole match
651 # unfortunately get_models can't do more than one per package atm, so we do it
652 # the oldfashioned way.
653 if ($::form->{prefer_exact}) {
655 if (1 == scalar @{ $exact_matches = $manager->get_all(
658 (salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
660 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
661 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
666 $matches = $exact_matches;
670 $matches //= $model->get;
674 value => $_->displayable_name,
675 label => $_->displayable_name,
677 $number => $_->$number,
679 type => $::form->{type},
680 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
684 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
687 sub action_test_page {
688 $_[0]->render('customer_vendor/test_page');
691 sub action_ajax_list_prices {
692 my ($self, %params) = @_;
694 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
695 my @columns = qw(partnumber description price);
696 my @visible = qw(partnumber description price);
697 my @sortable = qw(partnumber description price);
700 partnumber => { text => $::locale->text('Part Number'), sub => sub { $_[0]->parts->partnumber } },
701 description => { text => $::locale->text('Part Description'), sub => sub { $_[0]->parts->description } },
702 price => { text => $::locale->text('Price'), sub => sub { $::form->format_amount(\%::myconfig, $_[0]->price, 2) }, align => 'right' },
705 $::form->{sort_by} ||= 'partnumber';
706 $::form->{sort_dir} //= 1;
708 for my $col (@sortable) {
709 $column_defs{$col}{link} = $self->url_for(
710 action => 'ajax_list_prices',
711 callback => $::form->{callback},
713 id => $self->{cv}->id,
715 sort_dir => ($::form->{sort_by} eq $col ? 1 - $::form->{sort_dir} : $::form->{sort_dir})
719 map { $column_defs{$_}{visible} = 1 } @visible;
722 $pricegroup = $self->{cv}->pricegroup->pricegroup if $self->{cv}->pricegroup;
724 $report->set_columns(%column_defs);
725 $report->set_column_order(@columns);
726 $report->set_options(allow_pdf_export => 0, allow_csv_export => 0);
727 $report->set_sort_indicator($::form->{sort_by}, $::form->{sort_dir});
728 $report->set_export_options(@{ $params{report_generator_export_options} || [] });
729 $report->set_options(
730 %{ $params{report_generator_options} || {} },
731 output_format => 'HTML',
732 top_info_text => $::locale->text('Pricegroup') . ': ' . $pricegroup,
733 title => $::locale->text('Price List'),
736 my $sort_param = $::form->{sort_by} eq 'price' ? 'price' :
737 $::form->{sort_by} eq 'description' ? 'parts.description' :
739 $sort_param .= ' ' . ($::form->{sort_dir} ? 'ASC' : 'DESC');
740 my $prices = SL::DB::Manager::Price->get_all(where => [ pricegroup_id => $self->{cv}->pricegroup_id ],
741 sort_by => $sort_param,
742 with_objects => 'parts');
744 $self->report_generator_list_objects(report => $report, objects => $prices, layout => 0, header => 0);
748 return $::form->{db} eq 'vendor';
752 return $::form->{db} eq 'customer';
758 if ( defined($self->{_is_orphaned}) ) {
759 return $self->{_is_orphaned};
762 my $arap = $self->is_vendor ? 'ap' : 'ar';
765 my $cv = $self->is_vendor ? 'vendor' : 'customer';
769 FROM '. $arap .' AS a
770 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
777 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
783 FROM delivery_orders a
784 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
788 if ( $self->is_vendor ) {
791 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
795 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
797 return $self->{_is_orphaned} = !$dummy;
800 sub _copy_form_to_cvars {
801 my ($self, %params) = @_;
803 foreach my $cvar (@{ $params{target}->cvars_by_config }) {
804 my $value = $params{source}->{$cvar->config->name};
805 $value = $::form->parse_amount(\%::myconfig, $value) if $cvar->config->type eq 'number';
807 $cvar->value($value);
811 sub _instantiate_args {
814 my $curr_employee = SL::DB::Manager::Employee->current;
816 if ( $::form->{cv}->{id} ) {
817 if ( $self->is_vendor() ) {
818 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
820 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
823 $self->{cv} = $self->_new_customer_vendor_object;
825 $self->{cv}->assign_attributes(%{$::form->{cv}});
827 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
828 $self->{cv}->taxincluded_checked(undef);
831 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
833 if ( $::form->{note}->{id} ) {
834 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
835 $self->{note_followup} = $self->{note}->follow_up;
836 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
838 $self->{note} = SL::DB::Note->new();
839 $self->{note_followup} = SL::DB::FollowUp->new();
840 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
843 $self->{note}->assign_attributes(%{$::form->{note}});
844 $self->{note}->created_by($curr_employee->id);
845 $self->{note}->trans_module('ct');
847 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
848 $self->{note_followup}->note($self->{note});
849 $self->{note_followup}->created_by($curr_employee->id);
851 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
852 $self->{note_followup_link}->trans_info($self->{cv}->name);
854 if ( $::form->{shipto}->{shipto_id} ) {
855 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
857 $self->{shipto} = SL::DB::Shipto->new();
859 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
860 $self->{shipto}->module('CT');
862 if ( $::form->{contact}->{cp_id} ) {
863 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
865 $self->{contact} = $self->_new_contact_object;
867 $self->{contact}->assign_attributes(%{$::form->{contact}});
869 $self->_copy_form_to_cvars(target => $self->{cv}, source => $::form->{cv_cvars});
870 $self->_copy_form_to_cvars(target => $self->{contact}, source => $::form->{contact_cvars});
871 $self->_copy_form_to_cvars(target => $self->{shipto}, source => $::form->{shipto_cvars});
874 sub _load_customer_vendor {
877 if ( $self->is_vendor() ) {
878 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
880 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
883 if ( $::form->{note_id} ) {
884 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
885 $self->{note_followup} = $self->{note}->follow_up;
886 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
888 $self->{note} = SL::DB::Note->new();
889 $self->{note_followup} = SL::DB::FollowUp->new();
890 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
893 if ( $::form->{shipto_id} ) {
894 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
896 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
897 die($::locale->text('Error'));
900 $self->{shipto} = SL::DB::Shipto->new();
903 if ( $::form->{contact_id} ) {
904 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
906 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
907 die($::locale->text('Error'));
910 $self->{contact} = $self->_new_contact_object;
914 sub _check_customer_vendor_all_edit {
917 unless ($::auth->assert('customer_vendor_all_edit', 1)) {
918 die($::locale->text("You don't have the rights to edit this customer.") . "\n")
919 if $self->{cv}->is_customer and
920 SL::DB::Manager::Employee->current->id != $self->{cv}->salesman_id;
924 sub _create_customer_vendor {
927 $self->{cv} = $self->_new_customer_vendor_object;
928 $self->{cv}->currency_id($::instance_conf->get_currency_id());
930 $self->{note} = SL::DB::Note->new();
932 $self->{note_followup} = SL::DB::FollowUp->new();
934 $self->{shipto} = SL::DB::Shipto->new();
936 $self->{contact} = $self->_new_contact_object;
942 my $dbh = $::form->get_standard_dbh();
946 $self->{all_business} = SL::DB::Manager::Business->get_all();
948 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
950 $self->{all_greetings} = SL::DB::Manager::Greeting->get_all_sorted();
951 if ($self->{cv}->id && $self->{cv}->greeting && !grep {$self->{cv}->greeting eq $_->description} @{$self->{all_greetings}}) {
952 unshift @{$self->{all_greetings}}, (SL::DB::Greeting->new(description => $self->{cv}->greeting));
955 $self->{all_contact_titles} = SL::DB::Manager::ContactTitle->get_all_sorted();
956 foreach my $contact (@{ $self->{cv}->contacts }) {
957 if ($contact->cp_title && !grep {$contact->cp_title eq $_->description} @{$self->{all_contact_titles}}) {
958 unshift @{$self->{all_contact_titles}}, (SL::DB::ContactTitle->new(description => $contact->cp_title));
962 $self->{all_contact_departments} = SL::DB::Manager::ContactDepartment->get_all_sorted();
963 foreach my $contact (@{ $self->{cv}->contacts }) {
964 if ($contact->cp_abteilung && !grep {$contact->cp_abteilung eq $_->description} @{$self->{all_contact_departments}}) {
965 unshift @{$self->{all_contact_departments}}, (SL::DB::ContactDepartment->new(description => $contact->cp_abteilung));
969 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
971 $self->{all_languages} = SL::DB::Manager::Language->get_all();
973 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
975 if ( $::instance_conf->get_vertreter() ) {
983 selectall_hashref_query($::form, $dbh, $query)
987 if ( $business_ids->[0] ) {
988 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
990 $self->{all_salesman_customers} = [];
993 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
996 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->{cv}->payment_id,
999 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
1001 if ($self->{cv}->is_customer) {
1002 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all_sorted(query => [ or => [ id => $self->{cv}->pricegroup_id, obsolete => 0 ] ]);
1005 $self->{contacts} = $self->{cv}->contacts;
1006 $self->{contacts} ||= [];
1008 $self->{shiptos} = $self->{cv}->shipto;
1009 $self->{shiptos} ||= [];
1011 $self->{notes} = SL::DB::Manager::Note->get_all(
1013 trans_id => $self->{cv}->id,
1014 trans_module => 'ct',
1016 with_objects => ['follow_up'],
1019 if ( $self->is_vendor()) {
1020 $self->{open_items} = SL::DB::Manager::PurchaseInvoice->get_all_count(
1022 vendor_id => $self->{cv}->id,
1023 paid => {lt_sql => 'amount'},
1027 $self->{open_items} = SL::DB::Manager::Invoice->get_all_count(
1029 customer_id => $self->{cv}->id,
1030 paid => {lt_sql => 'amount'},
1035 if ( $self->is_vendor() ) {
1036 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
1038 vendor_id => $self->{cv}->id,
1043 $self->{open_orders} = SL::DB::Manager::Order->get_all_count(
1045 customer_id => $self->{cv}->id,
1051 if ($self->{cv}->number && $::instance_conf->get_webdav) {
1052 my $webdav = SL::Webdav->new(
1053 type => $self->is_customer ? 'customer'
1054 : $self->is_vendor ? 'vendor'
1056 number => $self->{cv}->number,
1058 my @all_objects = $webdav->get_all_objects;
1059 @{ $self->{template_args}->{WEBDAV} } = map { { name => $_->filename,
1061 link => File::Spec->catfile($_->full_filedescriptor),
1065 $self->{template_args} ||= {};
1067 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
1068 $::request->{layout}->add_javascripts('kivi.File.js');
1069 $::request->{layout}->add_javascripts('kivi.CustomerVendorTurnover.js');
1071 $self->_setup_form_action_bar;
1074 sub _setup_form_action_bar {
1077 for my $bar ($::request->layout->get('actionbar')) {
1082 submit => [ '#form', { action => "CustomerVendor/save" } ],
1083 checks => [ 'check_taxzone_and_ustid' ],
1084 accesskey => 'enter',
1087 t8('Save and Close'),
1088 submit => [ '#form', { action => "CustomerVendor/save_and_close" } ],
1089 checks => [ 'check_taxzone_and_ustid' ],
1091 ], # end of combobox "Save"
1094 action => [ t8('Workflow') ],
1096 t8('Save and AP Transaction'),
1097 submit => [ '#form', { action => "CustomerVendor/save_and_ap_transaction" } ],
1098 checks => [ 'check_taxzone_and_ustid' ],
1099 ]) x !!$self->is_vendor,
1101 t8('Save and AR Transaction'),
1102 submit => [ '#form', { action => "CustomerVendor/save_and_ar_transaction" } ],
1103 checks => [ 'check_taxzone_and_ustid' ],
1104 ]) x !$self->is_vendor,
1106 t8('Save and Invoice'),
1107 submit => [ '#form', { action => "CustomerVendor/save_and_invoice" } ],
1108 checks => [ 'check_taxzone_and_ustid' ],
1111 t8('Save and Order'),
1112 submit => [ '#form', { action => "CustomerVendor/save_and_order" } ],
1113 checks => [ 'check_taxzone_and_ustid' ],
1117 submit => [ '#form', { action => "CustomerVendor/save_and_rfq" } ],
1118 checks => [ 'check_taxzone_and_ustid' ],
1119 ]) x !!$self->is_vendor,
1121 t8('Save and Quotation'),
1122 submit => [ '#form', { action => "CustomerVendor/save_and_quotation" } ],
1123 checks => [ 'check_taxzone_and_ustid' ],
1124 ]) x !$self->is_vendor,
1125 ], # end of combobox "Workflow"
1129 submit => [ '#form', { action => "CustomerVendor/delete" } ],
1130 confirm => t8('Do you really want to delete this object?'),
1131 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.')
1132 : !$self->is_orphaned ? t8('This object has already been used.')
1140 call => [ 'kivi.CustomerVendor.showHistoryWindow', $self->{cv}->id ],
1141 disabled => !$self->{cv}->id ? t8('This object has not been saved yet.') : undef,
1147 sub _prepare_cvar_configs_for_ajaj {
1148 my ($self, $cvars) = @_;
1153 my $result = { type => $cvar->config->type };
1155 if ($cvar->config->type eq 'number') {
1156 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
1158 } elsif ($result->{type} eq 'date') {
1159 $result->{value} = $cvar->value ? $cvar->value->to_kivitendo : undef;
1161 } elsif ($result->{type} =~ m{customer|vendor|part}) {
1162 my $object = $cvar->value;
1163 my $method = $result->{type} eq 'part' ? 'description' : 'name';
1165 $result->{id} = int($cvar->number_value) || undef;
1166 $result->{value} = $object ? $object->$method // '' : '';
1169 $result->{value} = $cvar->value;
1172 ( $cvar->config->name => $result )
1174 } grep { $_->is_valid } @{ $cvars }
1178 sub normalize_name {
1181 # check if feature is enabled (select normalize_vc_names from defaults)
1182 return unless ($::instance_conf->get_normalize_vc_names);
1184 return unless $self->{cv};
1185 my $name = $self->{cv}->name;
1189 $self->{cv}->name($name);
1192 sub home_address_for_google_maps {
1195 my $address = $::instance_conf->get_address // '';
1196 $address =~ s{^\s+|\s+$|\r+}{}g;
1197 $address =~ s{\n+}{,}g;
1198 $address =~ s{\s+}{ }g;
1203 sub init_customer_models {
1206 SL::Controller::Helper::GetModels->new(
1207 controller => $self,
1208 model => 'Customer',
1211 by => 'customernumber',
1214 customernumber => t8('Customer Number'),
1217 ( salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('customer_vendor_all_edit', 1),
1222 sub init_vendor_models {
1225 SL::Controller::Helper::GetModels->new(
1226 controller => $self,
1230 by => 'vendornumber',
1233 vendornumber => t8('Vendor Number'),
1238 sub _new_customer_vendor_object {
1241 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
1245 custom_variables => [],
1249 sub _new_contact_object {
1252 return SL::DB::Contact->new(custom_variables => []);