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 @errors = $self->{cv}->validate;
118 flash('error', @errors);
119 $self->_pre_render();
121 'customer_vendor/form',
122 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
123 %{$self->{template_args}}
128 my $db = $self->{cv}->db;
130 $db->do_transaction(sub {
132 if ( $self->is_vendor() ) {
133 if ( $self->{cv}->vendornumber ) {
134 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
137 if ( $self->{cv}->customernumber ) {
138 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
142 foreach my $entry (@{$cvs_by_nr}) {
143 if( $entry->id != $self->{cv}->id ) {
145 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
147 $::form->error($msg);
151 $self->{cv}->save(cascade => 1);
153 $self->{contact}->cp_cv_id($self->{cv}->id);
154 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
155 $self->{contact}->save(cascade => 1);
158 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
160 if ( !$self->{note_followup}->follow_up_date ) {
161 $::form->error($::locale->text('Date missing!'));
164 $self->{note}->trans_id($self->{cv}->id);
165 $self->{note}->save();
167 $self->{note_followup}->save();
169 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
170 $self->{note_followup_link}->trans_id($self->{cv}->id);
171 $self->{note_followup_link}->save();
173 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
176 $self->{shipto}->trans_id($self->{cv}->id);
177 if( $self->{shipto}->shiptoname ne '' ) {
178 $self->{shipto}->save();
181 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
182 SL::DB::History->new(
183 trans_id => $self->{cv}->id,
184 snumbers => $snumbers,
185 employee_id => SL::DB::Manager::Employee->current->id,
189 if ( $::form->{delete_notes} ) {
190 foreach my $note_id (@{ $::form->{delete_notes} }) {
191 my $note = SL::DB::Note->new(id => $note_id)->load();
192 if ( $note->follow_up ) {
193 if ( $note->follow_up->follow_up_link ) {
194 $note->follow_up->follow_up_link->delete(cascade => 'delete');
196 $note->follow_up->delete(cascade => 'delete');
198 $note->delete(cascade => 'delete');
201 }) || die($db->error);
210 my @redirect_params = (
212 id => $self->{cv}->id,
213 db => ($self->is_vendor() ? 'vendor' : 'customer'),
216 if ( $self->{contact}->cp_id ) {
217 push(@redirect_params, contact_id => $self->{contact}->cp_id);
220 if ( $self->{shipto}->shipto_id ) {
221 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
224 $self->redirect_to(@redirect_params);
227 sub action_save_and_close {
232 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
233 $::form->redirect($msg);
237 my ($self, $script) = @_;
239 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
240 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
244 my $callback = $::form->escape($::form->{callback}, 1);
245 my $name = $::form->escape($self->{cv}->name, 1);
246 my $db = $self->is_vendor() ? 'vendor' : 'customer';
248 my $url = $self->url_for(
249 controller => $script,
252 $db .'_id' => $self->{cv}->id,
254 type => $::form->{type},
255 callback => $callback,
258 print $::form->redirect_header($url);
261 sub action_save_and_ar_transaction {
264 $main::auth->assert('general_ledger');
266 $self->_transaction('ar.pl');
269 sub action_save_and_ap_transaction {
272 $main::auth->assert('general_ledger');
274 $self->_transaction('ap.pl');
277 sub action_save_and_invoice {
280 if ( $self->is_vendor() ) {
281 $::auth->assert('vendor_invoice_edit');
283 $::auth->assert('invoice_edit');
286 $::form->{type} = 'invoice';
287 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
290 sub action_save_and_order {
293 if ( $self->is_vendor() ) {
294 $::auth->assert('purchase_order_edit');
296 $::auth->assert('sales_order_edit');
299 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
300 $self->_transaction('oe.pl');
303 sub action_save_and_rfq {
306 $::auth->assert('request_quotation_edit');
308 $::form->{type} = 'request_quotation';
309 $self->_transaction('oe.pl');
312 sub action_save_and_quotation {
315 $::auth->assert('sales_quotation_edit');
317 $::form->{type} = 'sales_quotation';
318 $self->_transaction('oe.pl');
324 my $db = $self->{cv}->db;
326 if( !$self->is_orphaned() ) {
327 $self->action_edit();
330 $db->do_transaction(sub {
331 $self->{cv}->delete(cascade => 1);
333 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
334 SL::DB::History->new(
335 trans_id => $self->{cv}->id,
336 snumbers => $snumbers,
337 employee_id => SL::DB::Manager::Employee->current->id,
338 addition => 'DELETED',
340 }) || die($db->error);
342 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
343 $::form->redirect($msg);
349 sub action_delete_contact {
352 my $db = $self->{contact}->db;
354 if ( !$self->{contact}->cp_id ) {
355 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
358 $db->do_transaction(sub {
359 if ( $self->{contact}->used ) {
360 $self->{contact}->detach();
361 $self->{contact}->save();
362 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
364 $self->{contact}->delete(cascade => 1);
365 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
367 }) || die($db->error);
369 $self->{contact} = $self->_new_contact_object;
372 $self->action_edit();
375 sub action_delete_shipto {
378 my $db = $self->{shipto}->db;
380 if ( !$self->{shipto}->shipto_id ) {
381 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
384 $db->do_transaction(sub {
385 if ( $self->{shipto}->used ) {
386 $self->{shipto}->detach();
387 $self->{shipto}->save(cascade => 1);
388 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
390 $self->{shipto}->delete(cascade => 1);
391 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
393 }) || die($db->error);
395 $self->{shipto} = SL::DB::Shipto->new();
398 $self->action_edit();
406 controller => 'ct.pl',
408 db => $self->is_vendor() ? 'vendor' : 'customer',
411 if ( $::form->{callback} ) {
412 push(@url_params, callback => $::form->{callback});
415 $self->redirect_to(@url_params);
419 sub action_search_contact {
422 my $url = 'ct.pl?action=search_contact&db=customer';
424 if ( $::form->{callback} ) {
425 $url .= '&callback='. $::form->escape($::form->{callback});
428 print $::form->redirect_header($url);
432 sub action_get_delivery {
435 $::auth->assert('sales_all_edit');
437 my $dbh = $::form->get_standard_dbh();
439 my ($arap, $db, $qty_sign);
440 if ( $self->is_vendor() ) {
443 $qty_sign = ' * -1 AS qty';
450 my $where = ' WHERE 1=1';
453 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
454 $where .= " AND ${arap}.shipto_id = ?";
455 push(@values, $::form->{shipto_id});
457 $where .= " AND ${arap}.${db}_id = ?";
458 push(@values, $::form->{id});
461 if ( $::form->{delivery_from} ) {
462 $where .= " AND ${arap}.transdate >= ?";
463 push(@values, conv_date($::form->{delivery_from}));
466 if ( $::form->{delivery_to} ) {
467 $where .= " AND ${arap}.transdate <= ?";
468 push(@values, conv_date($::form->{delivery_to}));
487 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
490 ON ${arap}.id = i.trans_id
496 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = ''
497 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe.vendor_id IS NOT NULL') ." )
500 ORDER BY ${arap}.transdate DESC LIMIT 15";
502 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
504 $self->render('customer_vendor/get_delivery', { layout => 0 });
507 sub action_ajaj_get_shipto {
513 my $name = 'shipto'. $_;
514 $name => $self->{shipto}->$name;
516 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
520 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
523 sub action_ajaj_get_contact {
531 my $name = 'cp_'. $_;
533 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
534 $name => $self->{contact}->$name->to_lxoffice;
536 $name => $self->{contact}->$name;
540 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
541 satphone satfax project street zipcode city privatphone privatemail birthday
546 $data->{contact_cvars} = {
549 my $result = { type => $cvar->config->type };
551 if ($cvar->config->type eq 'number') {
552 $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
554 } elsif ($result->{type} =~ m{customer|vendor|part}) {
555 my $object = $cvar->value;
556 my $method = $result->{type} eq 'part' ? 'description' : 'name';
558 $result->{id} = int($cvar->number_value) || undef;
559 $result->{value} = $object ? $object->$method // '' : '';
562 $result->{value} = $cvar->value;
565 ( $cvar->config->name => $result )
567 } grep { $_->is_valid } @{ $self->{contact}->cvars_by_config }
570 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
573 sub action_ajaj_autocomplete {
574 my ($self, %params) = @_;
576 my ($model, $manager, $number, $matches);
578 # first see if this is customer or vendor picking
579 if ($::form->{type} eq 'customer') {
580 $model = $self->customer_models;
581 $manager = 'SL::DB::Manager::Customer';
582 $number = 'customernumber';
583 } elsif ($::form->{type} eq 'vendor') {
584 $model = $self->vendor_models;
585 $manager = 'SL::DB::Manager::Vendor';
586 $number = 'vendornumber';
588 die "unknown type $::form->{type}";
591 # if someone types something, and hits enter, assume he entered the full name.
592 # if something matches, treat that as sole match
593 # unfortunately get_models can't do more than one per package atm, so we d it
594 # the oldfashioned way.
595 if ($::form->{prefer_exact}) {
597 if (1 == scalar @{ $exact_matches = $manager->get_all(
601 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
602 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
607 $matches = $exact_matches;
611 $matches //= $model->get;
615 value => $_->displayable_name,
616 label => $_->displayable_name,
618 $number => $_->$number,
620 type => $::form->{type},
621 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
625 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
628 sub action_test_page {
629 $::request->{layout}->add_javascripts('autocomplete_customer.js');
630 $_[0]->render('customer_vendor/test_page');
634 return $::form->{db} eq 'vendor';
638 return $::form->{db} eq 'customer';
644 if ( defined($self->{_is_orphaned}) ) {
645 return $self->{_is_orphaned};
648 my $arap = $self->is_vendor ? 'ap' : 'ar';
651 my $cv = $self->is_vendor ? 'vendor' : 'customer';
655 FROM '. $arap .' AS a
656 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
663 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
669 FROM delivery_orders a
670 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
674 if ( $self->is_vendor ) {
677 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
681 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
683 return $self->{_is_orphaned} = !$dummy;
686 sub _instantiate_args {
689 my $curr_employee = SL::DB::Manager::Employee->current;
691 if ( $::form->{cv}->{id} ) {
692 if ( $self->is_vendor() ) {
693 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
695 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
698 $self->{cv} = $self->_new_customer_vendor_object;
700 $self->{cv}->assign_attributes(%{$::form->{cv}});
702 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
703 $self->{cv}->taxincluded_checked(undef);
706 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
708 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
709 my $value = $::form->{cv_cvars}->{$cvar->config->name};
711 if ( $cvar->config->type eq 'number' ) {
712 $value = $::form->parse_amount(\%::myconfig, $value);
715 $cvar->value($value);
718 if ( $::form->{note}->{id} ) {
719 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
720 $self->{note_followup} = $self->{note}->follow_up;
721 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
723 $self->{note} = SL::DB::Note->new();
724 $self->{note_followup} = SL::DB::FollowUp->new();
725 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
728 $self->{note}->assign_attributes(%{$::form->{note}});
729 $self->{note}->created_by($curr_employee->id);
730 $self->{note}->trans_module('ct');
732 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
733 $self->{note_followup}->note($self->{note});
734 $self->{note_followup}->created_by($curr_employee->id);
736 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
737 $self->{note_followup_link}->trans_info($self->{cv}->name);
739 if ( $::form->{shipto}->{shipto_id} ) {
740 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
742 $self->{shipto} = SL::DB::Shipto->new();
744 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
745 $self->{shipto}->module('CT');
747 if ( $::form->{contact}->{cp_id} ) {
748 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
750 $self->{contact} = $self->_new_contact_object;
752 $self->{contact}->assign_attributes(%{$::form->{contact}});
754 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
755 my $value = $::form->{contact_cvars}->{$cvar->config->name};
757 if ( $cvar->config->type eq 'number' ) {
758 $value = $::form->parse_amount(\%::myconfig, $value);
761 $cvar->value($value);
765 sub _load_customer_vendor {
768 if ( $self->is_vendor() ) {
769 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
771 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
774 if ( $::form->{note_id} ) {
775 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
776 $self->{note_followup} = $self->{note}->follow_up;
777 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
779 $self->{note} = SL::DB::Note->new();
780 $self->{note_followup} = SL::DB::FollowUp->new();
781 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
784 if ( $::form->{shipto_id} ) {
785 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
787 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
788 die($::locale->text('Error'));
791 $self->{shipto} = SL::DB::Shipto->new();
794 if ( $::form->{contact_id} ) {
795 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
797 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
798 die($::locale->text('Error'));
801 $self->{contact} = $self->_new_contact_object;
805 sub _create_customer_vendor {
808 $self->{cv} = $self->_new_customer_vendor_object;
809 $self->{cv}->currency_id($::instance_conf->get_currency_id());
811 $self->{note} = SL::DB::Note->new();
813 $self->{note_followup} = SL::DB::FollowUp->new();
815 $self->{shipto} = SL::DB::Shipto->new();
817 $self->{contact} = $self->_new_contact_object;
823 my $dbh = $::form->get_standard_dbh();
827 $self->{all_business} = SL::DB::Manager::Business->get_all();
829 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
832 'SELECT DISTINCT(greeting)
834 WHERE greeting IS NOT NULL AND greeting != \'\'
836 SELECT DISTINCT(greeting)
838 WHERE greeting IS NOT NULL AND greeting != \'\'
840 $self->{all_greetings} = [
843 selectall_hashref_query($::form, $dbh, $query)
848 'SELECT DISTINCT(cp_title) AS title
850 WHERE cp_title IS NOT NULL AND cp_title != \'\'
852 $self->{all_titles} = [
855 selectall_hashref_query($::form, $dbh, $query)
859 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
861 $self->{all_languages} = SL::DB::Manager::Language->get_all();
863 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
865 if ( $::instance_conf->get_vertreter() ) {
873 selectall_hashref_query($::form, $dbh, $query)
877 if ( $business_ids->[0] ) {
878 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
880 $self->{all_salesman_customers} = [];
883 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
886 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
888 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
890 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
893 'SELECT DISTINCT(cp_abteilung) AS department
895 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
896 ORDER BY cp_abteilung';
897 $self->{all_departments} = [
899 { $_->{department}; }
900 selectall_hashref_query($::form, $dbh, $query)
904 $self->{contacts} = $self->{cv}->contacts;
905 $self->{contacts} ||= [];
907 $self->{shiptos} = $self->{cv}->shipto;
908 $self->{shiptos} ||= [];
910 $self->{notes} = SL::DB::Manager::Note->get_all(
912 trans_id => $self->{cv}->id,
913 trans_module => 'ct',
915 with_objects => ['follow_up'],
918 $self->{template_args} ||= {};
920 $::request->{layout}->add_javascripts('autocomplete_customer.js');
921 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
927 # check if feature is enabled (select normalize_vc_names from defaults)
928 return unless ($::instance_conf->get_normalize_vc_names);
930 return unless $self->{cv};
931 my $name = $self->{cv}->name;
935 $self->{cv}->name($name);
938 sub home_address_for_google_maps {
941 my $address = $::instance_conf->get_address // '';
942 $address =~ s{^\s+|\s+$|\r+}{}g;
943 $address =~ s{\n+}{,}g;
944 $address =~ s{\s+}{ }g;
949 sub init_customer_models {
952 SL::Controller::Helper::GetModels->new(
957 by => 'customernumber',
960 customernumber => t8('Customer Number'),
965 sub init_vendor_models {
968 SL::Controller::Helper::GetModels->new(
973 by => 'vendornumber',
976 vendornumber => t8('Vendor Number'),
981 sub _new_customer_vendor_object {
984 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
988 custom_variables => [],
992 sub _new_contact_object {
995 return SL::DB::Contact->new(custom_variables => []);