1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
9 use SL::Locale::String;
10 use SL::Controller::Helper::GetModels;
19 use SL::DB::PaymentTerm;
20 use SL::DB::Pricegroup;
23 use SL::DB::FollowUpLink;
27 use Rose::Object::MakeMethods::Generic (
28 'scalar --get_set_init' => [ qw(customer_models vendor_models) ],
32 __PACKAGE__->run_before(
34 $::auth->assert('customer_vendor_edit');
37 __PACKAGE__->run_before(
41 'save_and_ap_transaction',
42 'save_and_ar_transaction',
54 __PACKAGE__->run_before(
55 '_load_customer_vendor',
64 __PACKAGE__->run_before(
65 '_create_customer_vendor',
71 __PACKAGE__->run_before('normalize_name');
78 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer;
80 'customer_vendor/form',
81 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
82 %{$self->{template_args}}
91 'customer_vendor/form',
92 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
93 %{$self->{template_args}}
100 if ($::request->type eq 'json') {
105 $cv_hash = $self->{cv}->as_tree;
106 $cv_hash->{cvars} = $self->{cv}->cvar_as_hashref;
109 $self->render(\ SL::JSON::to_json($cv_hash), { layout => 0, type => 'json', process => 0 });
116 my $db = $self->{cv}->db;
118 $db->do_transaction(sub {
120 if ( $self->is_vendor() ) {
121 if ( $self->{cv}->vendornumber ) {
122 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
125 if ( $self->{cv}->customernumber ) {
126 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
130 foreach my $entry (@{$cvs_by_nr}) {
131 if( $entry->id != $self->{cv}->id ) {
133 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
135 $::form->error($msg);
139 $self->{cv}->save(cascade => 1);
141 $self->{contact}->cp_cv_id($self->{cv}->id);
142 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
143 $self->{contact}->save(cascade => 1);
146 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
148 if ( !$self->{note_followup}->follow_up_date ) {
149 $::form->error($::locale->text('Date missing!'));
152 $self->{note}->trans_id($self->{cv}->id);
153 $self->{note}->save();
155 $self->{note_followup}->save();
157 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
158 $self->{note_followup_link}->trans_id($self->{cv}->id);
159 $self->{note_followup_link}->save();
161 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
164 $self->{shipto}->trans_id($self->{cv}->id);
165 if( $self->{shipto}->shiptoname ne '' ) {
166 $self->{shipto}->save();
169 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
170 SL::DB::History->new(
171 trans_id => $self->{cv}->id,
172 snumbers => $snumbers,
173 employee_id => SL::DB::Manager::Employee->current->id,
177 if ( $::form->{delete_notes} ) {
178 foreach my $note_id (@{ $::form->{delete_notes} }) {
179 my $note = SL::DB::Note->new(id => $note_id)->load();
180 if ( $note->follow_up ) {
181 if ( $note->follow_up->follow_up_link ) {
182 $note->follow_up->follow_up_link->delete(cascade => 'delete');
184 $note->follow_up->delete(cascade => 'delete');
186 $note->delete(cascade => 'delete');
189 }) || die($db->error);
196 if (!$self->{cv}->name) {
197 flash('error', t8('Customer missing!'));
198 $self->_pre_render();
200 'customer_vendor/form',
201 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
202 %{$self->{template_args}}
208 my @redirect_params = (
210 id => $self->{cv}->id,
211 db => ($self->is_vendor() ? 'vendor' : 'customer'),
214 if ( $self->{contact}->cp_id ) {
215 push(@redirect_params, contact_id => $self->{contact}->cp_id);
218 if ( $self->{shipto}->shipto_id ) {
219 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
222 $self->redirect_to(@redirect_params);
226 sub action_save_and_close {
231 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
232 $::form->redirect($msg);
236 my ($self, $script) = @_;
238 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
239 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
243 my $callback = $::form->escape($::form->{callback}, 1);
244 my $name = $::form->escape($self->{cv}->name, 1);
245 my $db = $self->is_vendor() ? 'vendor' : 'customer';
247 my $url = $self->url_for(
248 controller => $script,
251 $db .'_id' => $self->{cv}->id,
253 type => $::form->{type},
254 callback => $callback,
257 print $::form->redirect_header($url);
260 sub action_save_and_ar_transaction {
263 $main::auth->assert('general_ledger');
265 $self->_transaction('ar.pl');
268 sub action_save_and_ap_transaction {
271 $main::auth->assert('general_ledger');
273 $self->_transaction('ap.pl');
276 sub action_save_and_invoice {
279 if ( $self->is_vendor() ) {
280 $::auth->assert('vendor_invoice_edit');
282 $::auth->assert('invoice_edit');
285 $::form->{type} = 'invoice';
286 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
289 sub action_save_and_order {
292 if ( $self->is_vendor() ) {
293 $::auth->assert('purchase_order_edit');
295 $::auth->assert('sales_order_edit');
298 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
299 $self->_transaction('oe.pl');
302 sub action_save_and_rfq {
305 $::auth->assert('request_quotation_edit');
307 $::form->{type} = 'request_quotation';
308 $self->_transaction('oe.pl');
311 sub action_save_and_quotation {
314 $::auth->assert('sales_quotation_edit');
316 $::form->{type} = 'sales_quotation';
317 $self->_transaction('oe.pl');
323 my $db = $self->{cv}->db;
325 if( !$self->is_orphaned() ) {
326 $self->action_edit();
329 $db->do_transaction(sub {
330 $self->{cv}->delete(cascade => 1);
332 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
333 SL::DB::History->new(
334 trans_id => $self->{cv}->id,
335 snumbers => $snumbers,
336 employee_id => SL::DB::Manager::Employee->current->id,
337 addition => 'DELETED',
339 }) || die($db->error);
341 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
342 $::form->redirect($msg);
348 sub action_delete_contact {
351 my $db = $self->{contact}->db;
353 if ( !$self->{contact}->cp_id ) {
354 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
357 $db->do_transaction(sub {
358 if ( $self->{contact}->used ) {
359 $self->{contact}->detach();
360 $self->{contact}->save();
361 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
363 $self->{contact}->delete(cascade => 1);
364 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
366 }) || die($db->error);
368 $self->{contact} = $self->_new_contact_object;
371 $self->action_edit();
374 sub action_delete_shipto {
377 my $db = $self->{shipto}->db;
379 if ( !$self->{shipto}->shipto_id ) {
380 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
383 $db->do_transaction(sub {
384 if ( $self->{shipto}->used ) {
385 $self->{shipto}->detach();
386 $self->{shipto}->save(cascade => 1);
387 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
389 $self->{shipto}->delete(cascade => 1);
390 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
392 }) || die($db->error);
394 $self->{shipto} = SL::DB::Shipto->new();
397 $self->action_edit();
405 controller => 'ct.pl',
407 db => $self->is_vendor() ? 'vendor' : 'customer',
410 if ( $::form->{callback} ) {
411 push(@url_params, callback => $::form->{callback});
414 $self->redirect_to(@url_params);
418 sub action_search_contact {
421 my $url = 'ct.pl?action=search_contact&db=customer';
423 if ( $::form->{callback} ) {
424 $url .= '&callback='. $::form->escape($::form->{callback});
427 print $::form->redirect_header($url);
431 sub action_get_delivery {
434 $::auth->assert('sales_all_edit');
436 my $dbh = $::form->get_standard_dbh();
438 my ($arap, $db, $qty_sign);
439 if ( $self->is_vendor() ) {
442 $qty_sign = ' * -1 AS qty';
449 my $where = ' WHERE 1=1';
452 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
453 $where .= " AND ${arap}.shipto_id = ?";
454 push(@values, $::form->{shipto_id});
456 $where .= " AND ${arap}.${db}_id = ?";
457 push(@values, $::form->{id});
460 if ( $::form->{delivery_from} ) {
461 $where .= " AND ${arap}.transdate >= ?";
462 push(@values, conv_date($::form->{delivery_from}));
465 if ( $::form->{delivery_to} ) {
466 $where .= " AND ${arap}.transdate <= ?";
467 push(@values, conv_date($::form->{delivery_to}));
486 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
489 ON ${arap}.id = i.trans_id
495 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
496 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
499 ORDER BY ${arap}.transdate DESC LIMIT 15";
501 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
503 $self->render('customer_vendor/get_delivery', { layout => 0 });
506 sub action_ajaj_get_shipto {
512 my $name = 'shipto'. $_;
513 $name => $self->{shipto}->$name;
515 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
519 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
522 sub action_ajaj_get_contact {
530 my $name = 'cp_'. $_;
532 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
533 $name => $self->{contact}->$name->to_lxoffice;
535 $name => $self->{contact}->$name;
539 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
540 satphone satfax project street zipcode city privatphone privatemail birthday
545 $data->{contact_cvars} = {
548 my $result = { type => $cvar->config->type };
550 if ($cvar->config->type eq 'number') {
551 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
553 } elsif ($result->{type} =~ m{customer|vendor|part}) {
554 my $object = $cvar->value;
555 my $method = $result->{type} eq 'part' ? 'description' : 'name';
557 $result->{id} = int($cvar->number_value) || undef;
558 $result->{value} = $object ? $object->$method // '' : '';
561 $result->{value} = $cvar->value;
564 ( $cvar->config->name => $result )
566 } grep { $_->is_valid } @{ $self->{contact}->cvars_by_config }
569 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
572 sub action_ajaj_autocomplete {
573 my ($self, %params) = @_;
575 my ($model, $manager, $number, $matches);
577 # first see if this is customer or vendor picking
578 if ($::form->{type} eq 'customer') {
579 $model = $self->customer_models;
580 $manager = 'SL::DB::Manager::Customer';
581 $number = 'customernumber';
582 } elsif ($::form->{type} eq 'vendor') {
583 $model = $self->vendor_models;
584 $manager = 'SL::DB::Manager::Vendor';
585 $number = 'vendornumber';
587 die "unknown type $::form->{type}";
590 # if someone types something, and hits enter, assume he entered the full name.
591 # if something matches, treat that as sole match
592 # unfortunately get_models can't do more than one per package atm, so we d it
593 # the oldfashioned way.
594 if ($::form->{prefer_exact}) {
596 if (1 == scalar @{ $exact_matches = $manager->get_all(
600 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
601 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
606 $matches = $exact_matches;
610 $matches //= $model->get;
614 value => $_->displayable_name,
615 label => $_->displayable_name,
617 $number => $_->$number,
619 type => $::form->{type},
620 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
624 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
627 sub action_test_page {
628 $::request->{layout}->add_javascripts('autocomplete_customer.js');
629 $_[0]->render('customer_vendor/test_page');
633 return $::form->{db} eq 'vendor';
637 return $::form->{db} eq 'customer';
643 if ( defined($self->{_is_orphaned}) ) {
644 return $self->{_is_orphaned};
647 my $arap = $self->is_vendor ? 'ap' : 'ar';
650 my $cv = $self->is_vendor ? 'vendor' : 'customer';
654 FROM '. $arap .' AS a
655 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
662 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
668 FROM delivery_orders a
669 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
673 if ( $self->is_vendor ) {
676 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
680 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
682 return $self->{_is_orphaned} = !$dummy;
685 sub _instantiate_args {
688 my $curr_employee = SL::DB::Manager::Employee->current;
690 if ( $::form->{cv}->{id} ) {
691 if ( $self->is_vendor() ) {
692 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
694 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
697 $self->{cv} = $self->_new_customer_vendor_object;
699 $self->{cv}->assign_attributes(%{$::form->{cv}});
701 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
702 $self->{cv}->taxincluded_checked(undef);
705 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
707 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
708 my $value = $::form->{cv_cvars}->{$cvar->config->name};
710 if ( $cvar->config->type eq 'number' ) {
711 $value = $::form->parse_amount(\%::myconfig, $value);
714 $cvar->value($value);
717 if ( $::form->{note}->{id} ) {
718 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
719 $self->{note_followup} = $self->{note}->follow_up;
720 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
722 $self->{note} = SL::DB::Note->new();
723 $self->{note_followup} = SL::DB::FollowUp->new();
724 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
727 $self->{note}->assign_attributes(%{$::form->{note}});
728 $self->{note}->created_by($curr_employee->id);
729 $self->{note}->trans_module('ct');
731 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
732 $self->{note_followup}->note($self->{note});
733 $self->{note_followup}->created_by($curr_employee->id);
735 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
736 $self->{note_followup_link}->trans_info($self->{cv}->name);
738 if ( $::form->{shipto}->{shipto_id} ) {
739 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
741 $self->{shipto} = SL::DB::Shipto->new();
743 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
744 $self->{shipto}->module('CT');
746 if ( $::form->{contact}->{cp_id} ) {
747 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
749 $self->{contact} = $self->_new_contact_object;
751 $self->{contact}->assign_attributes(%{$::form->{contact}});
753 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
754 my $value = $::form->{contact_cvars}->{$cvar->config->name};
756 if ( $cvar->config->type eq 'number' ) {
757 $value = $::form->parse_amount(\%::myconfig, $value);
760 $cvar->value($value);
764 sub _load_customer_vendor {
767 if ( $self->is_vendor() ) {
768 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
770 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
773 if ( $::form->{note_id} ) {
774 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
775 $self->{note_followup} = $self->{note}->follow_up;
776 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
778 $self->{note} = SL::DB::Note->new();
779 $self->{note_followup} = SL::DB::FollowUp->new();
780 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
783 if ( $::form->{shipto_id} ) {
784 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
786 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
787 die($::locale->text('Error'));
790 $self->{shipto} = SL::DB::Shipto->new();
793 if ( $::form->{contact_id} ) {
794 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
796 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
797 die($::locale->text('Error'));
800 $self->{contact} = $self->_new_contact_object;
804 sub _create_customer_vendor {
807 $self->{cv} = $self->_new_customer_vendor_object;
808 $self->{cv}->currency_id($::instance_conf->get_currency_id());
810 $self->{note} = SL::DB::Note->new();
812 $self->{note_followup} = SL::DB::FollowUp->new();
814 $self->{shipto} = SL::DB::Shipto->new();
816 $self->{contact} = $self->_new_contact_object;
822 my $dbh = $::form->get_standard_dbh();
826 $self->{all_business} = SL::DB::Manager::Business->get_all();
828 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
831 'SELECT DISTINCT(greeting)
833 WHERE greeting IS NOT NULL AND greeting != \'\'
835 SELECT DISTINCT(greeting)
837 WHERE greeting IS NOT NULL AND greeting != \'\'
839 $self->{all_greetings} = [
842 selectall_hashref_query($::form, $dbh, $query)
847 'SELECT DISTINCT(cp_title) AS title
849 WHERE cp_title IS NOT NULL AND cp_title != \'\'
851 $self->{all_titles} = [
854 selectall_hashref_query($::form, $dbh, $query)
858 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
860 $self->{all_languages} = SL::DB::Manager::Language->get_all();
862 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
864 if ( $::instance_conf->get_vertreter() ) {
872 selectall_hashref_query($::form, $dbh, $query)
876 if ( $business_ids->[0] ) {
877 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
879 $self->{all_salesman_customers} = [];
882 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
885 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
887 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
889 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
892 'SELECT DISTINCT(cp_abteilung) AS department
894 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
895 ORDER BY cp_abteilung';
896 $self->{all_departments} = [
898 { $_->{department}; }
899 selectall_hashref_query($::form, $dbh, $query)
903 $self->{contacts} = $self->{cv}->contacts;
904 $self->{contacts} ||= [];
906 $self->{shiptos} = $self->{cv}->shipto;
907 $self->{shiptos} ||= [];
909 $self->{notes} = SL::DB::Manager::Note->get_all(
911 trans_id => $self->{cv}->id,
912 trans_module => 'ct',
914 with_objects => ['follow_up'],
917 $self->{template_args} ||= {};
919 $::request->{layout}->add_javascripts('autocomplete_customer.js');
920 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
926 # check if feature is enabled (select normalize_vc_names from defaults)
927 return unless ($::instance_conf->get_normalize_vc_names);
929 return unless $self->{cv};
930 my $name = $self->{cv}->name;
934 $self->{cv}->name($name);
937 sub home_address_for_google_maps {
940 my $address = $::instance_conf->get_address // '';
941 $address =~ s{^\s+|\s+$|\r+}{}g;
942 $address =~ s{\n+}{,}g;
943 $address =~ s{\s+}{ }g;
948 sub init_customer_models {
951 SL::Controller::Helper::GetModels->new(
956 by => 'customernumber',
959 customernumber => t8('Customer Number'),
964 sub init_vendor_models {
967 SL::Controller::Helper::GetModels->new(
972 by => 'vendornumber',
975 vendornumber => t8('Vendor Number'),
980 sub _new_customer_vendor_object {
983 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
987 custom_variables => [],
991 sub _new_contact_object {
994 return SL::DB::Contact->new(custom_variables => []);