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 if ( $_->config->type eq 'number' ) {
549 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
551 $_->config->name => $_->value;
556 @{$self->{contact}->cvars_by_config}
561 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
564 sub action_ajaj_autocomplete {
565 my ($self, %params) = @_;
567 my ($model, $manager, $number, $matches);
569 # first see if this is customer or vendor picking
570 if ($::form->{type} eq 'customer') {
571 $model = $self->customer_models;
572 $manager = 'SL::DB::Manager::Customer';
573 $number = 'customernumber';
574 } elsif ($::form->{type} eq 'vendor') {
575 $model = $self->vendor_models;
576 $manager = 'SL::DB::Manager::Vendor';
577 $number = 'vendornumber';
579 die "unknown type $::form->{type}";
582 # if someone types something, and hits enter, assume he entered the full name.
583 # if something matches, treat that as sole match
584 # unfortunately get_models can't do more than one per package atm, so we d it
585 # the oldfashioned way.
586 if ($::form->{prefer_exact}) {
588 if (1 == scalar @{ $exact_matches = $manager->get_all(
592 name => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
593 $number => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
598 $matches = $exact_matches;
602 $matches //= $model->get;
607 label => $_->displayable_name,
609 $number => $_->$number,
611 type => $::form->{type},
612 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
616 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
619 sub action_test_page {
620 $::request->{layout}->add_javascripts('autocomplete_customer.js');
621 $_[0]->render('customer_vendor/test_page');
625 return $::form->{db} eq 'vendor';
629 return $::form->{db} eq 'customer';
635 if ( defined($self->{_is_orphaned}) ) {
636 return $self->{_is_orphaned};
639 my $arap = $self->is_vendor ? 'ap' : 'ar';
642 my $cv = $self->is_vendor ? 'vendor' : 'customer';
646 FROM '. $arap .' AS a
647 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
654 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
658 if ( $self->is_vendor ) {
661 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
665 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
667 return $self->{_is_orphaned} = !$dummy;
670 sub _instantiate_args {
673 my $curr_employee = SL::DB::Manager::Employee->current;
675 if ( $::form->{cv}->{id} ) {
676 if ( $self->is_vendor() ) {
677 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
679 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
682 $self->{cv} = $self->_new_customer_vendor_object;
684 $self->{cv}->assign_attributes(%{$::form->{cv}});
686 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
687 $self->{cv}->taxincluded_checked(undef);
690 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
692 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
693 my $value = $::form->{cv_cvars}->{$cvar->config->name};
695 if ( $cvar->config->type eq 'number' ) {
696 $value = $::form->parse_amount(\%::myconfig, $value);
699 $cvar->value($value);
702 if ( $::form->{note}->{id} ) {
703 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
704 $self->{note_followup} = $self->{note}->follow_up;
705 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
707 $self->{note} = SL::DB::Note->new();
708 $self->{note_followup} = SL::DB::FollowUp->new();
709 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
712 $self->{note}->assign_attributes(%{$::form->{note}});
713 $self->{note}->created_by($curr_employee->id);
714 $self->{note}->trans_module('ct');
716 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
717 $self->{note_followup}->note($self->{note});
718 $self->{note_followup}->created_by($curr_employee->id);
720 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
721 $self->{note_followup_link}->trans_info($self->{cv}->name);
723 if ( $::form->{shipto}->{shipto_id} ) {
724 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
726 $self->{shipto} = SL::DB::Shipto->new();
728 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
729 $self->{shipto}->module('CT');
731 if ( $::form->{contact}->{cp_id} ) {
732 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
734 $self->{contact} = $self->_new_contact_object;
736 $self->{contact}->assign_attributes(%{$::form->{contact}});
738 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
739 my $value = $::form->{contact_cvars}->{$cvar->config->name};
741 if ( $cvar->config->type eq 'number' ) {
742 $value = $::form->parse_amount(\%::myconfig, $value);
745 $cvar->value($value);
749 sub _load_customer_vendor {
752 if ( $self->is_vendor() ) {
753 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
755 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
758 if ( $::form->{note_id} ) {
759 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
760 $self->{note_followup} = $self->{note}->follow_up;
761 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
763 $self->{note} = SL::DB::Note->new();
764 $self->{note_followup} = SL::DB::FollowUp->new();
765 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
768 if ( $::form->{shipto_id} ) {
769 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
771 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
772 die($::locale->text('Error'));
775 $self->{shipto} = SL::DB::Shipto->new();
778 if ( $::form->{contact_id} ) {
779 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
781 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
782 die($::locale->text('Error'));
785 $self->{contact} = $self->_new_contact_object;
789 sub _create_customer_vendor {
792 $self->{cv} = $self->_new_customer_vendor_object;
793 $self->{cv}->currency_id($::instance_conf->get_currency_id());
795 $self->{note} = SL::DB::Note->new();
797 $self->{note_followup} = SL::DB::FollowUp->new();
799 $self->{shipto} = SL::DB::Shipto->new();
801 $self->{contact} = $self->_new_contact_object;
807 my $dbh = $::form->get_standard_dbh();
811 $self->{all_business} = SL::DB::Manager::Business->get_all();
813 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
816 'SELECT DISTINCT(greeting)
818 WHERE greeting IS NOT NULL AND greeting != \'\'
820 SELECT DISTINCT(greeting)
822 WHERE greeting IS NOT NULL AND greeting != \'\'
824 $self->{all_greetings} = [
827 selectall_hashref_query($::form, $dbh, $query)
832 'SELECT DISTINCT(cp_title) AS title
834 WHERE cp_title IS NOT NULL AND cp_title != \'\'
836 $self->{all_titles} = [
839 selectall_hashref_query($::form, $dbh, $query)
843 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
845 $self->{all_languages} = SL::DB::Manager::Language->get_all();
847 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
849 if ( $::instance_conf->get_vertreter() ) {
857 selectall_hashref_query($::form, $dbh, $query)
861 if ( $business_ids->[0] ) {
862 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
864 $self->{all_salesman_customers} = [];
867 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
870 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
872 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
874 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
877 'SELECT DISTINCT(cp_abteilung) AS department
879 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
880 ORDER BY cp_abteilung';
881 $self->{all_departments} = [
883 { $_->{department}; }
884 selectall_hashref_query($::form, $dbh, $query)
888 $self->{contacts} = $self->{cv}->contacts;
889 $self->{contacts} ||= [];
891 $self->{shiptos} = $self->{cv}->shipto;
892 $self->{shiptos} ||= [];
894 $self->{notes} = SL::DB::Manager::Note->get_all(
896 trans_id => $self->{cv}->id,
897 trans_module => 'ct',
899 with_objects => ['follow_up'],
902 $self->{template_args} ||= {};
904 $::request->{layout}->add_javascripts('autocomplete_customer.js');
905 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
911 # check if feature is enabled (select normalize_vc_names from defaults)
912 return unless ($::instance_conf->get_normalize_vc_names);
914 return unless $self->{cv};
915 my $name = $self->{cv}->name;
919 $self->{cv}->name($name);
922 sub home_address_for_google_maps {
925 my $address = $::instance_conf->get_address // '';
926 $address =~ s{^\s+|\s+$|\r+}{}g;
927 $address =~ s{\n+}{,}g;
928 $address =~ s{\s+}{ }g;
933 sub init_customer_models {
936 SL::Controller::Helper::GetModels->new(
949 sub init_vendor_models {
952 SL::Controller::Helper::GetModels->new(
965 sub _new_customer_vendor_object {
968 my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
972 custom_variables => [],
976 sub _new_contact_object {
979 return SL::DB::Contact->new(custom_variables => []);