1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
9 use SL::Locale::String;
18 use SL::DB::PaymentTerm;
19 use SL::DB::Pricegroup;
22 use SL::DB::FollowUpLink;
27 __PACKAGE__->run_before(
29 $::auth->assert('customer_vendor_edit');
32 __PACKAGE__->run_before(
36 'save_and_ap_transaction',
37 'save_and_ar_transaction',
49 __PACKAGE__->run_before(
50 '_load_customer_vendor',
58 __PACKAGE__->run_before(
59 '_create_customer_vendor',
65 __PACKAGE__->run_before('normalize_name');
72 $self->{cv}->assign_attributes(hourly_rate => $::instance_conf->get_customer_hourly_rate) if $self->{cv}->is_customer;
74 'customer_vendor/form',
75 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
76 %{$self->{template_args}}
85 'customer_vendor/form',
86 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
87 %{$self->{template_args}}
94 my $db = $self->{cv}->db;
96 $db->do_transaction(sub {
98 if ( $self->is_vendor() ) {
99 if ( $self->{cv}->vendornumber ) {
100 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
103 if ( $self->{cv}->customernumber ) {
104 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
108 foreach my $entry (@{$cvs_by_nr}) {
109 if( $entry->id != $self->{cv}->id ) {
111 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
113 $::form->error($msg);
117 $self->{cv}->save(cascade => 1);
119 $self->{contact}->cp_cv_id($self->{cv}->id);
120 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
121 $self->{contact}->save();
124 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
126 if ( !$self->{note_followup}->follow_up_date ) {
127 $::form->error($::locale->text('Date missing!'));
130 $self->{note}->trans_id($self->{cv}->id);
131 $self->{note}->save();
133 $self->{note_followup}->save();
135 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
136 $self->{note_followup_link}->trans_id($self->{cv}->id);
137 $self->{note_followup_link}->save();
139 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
142 $self->{shipto}->trans_id($self->{cv}->id);
143 if( $self->{shipto}->shiptoname ne '' ) {
144 $self->{shipto}->save();
147 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
148 SL::DB::History->new(
149 trans_id => $self->{cv}->id,
150 snumbers => $snumbers,
151 employee_id => SL::DB::Manager::Employee->current->id,
155 if ( $::form->{delete_notes} ) {
156 foreach my $note_id (@{ $::form->{delete_notes} }) {
157 my $note = SL::DB::Note->new(id => $note_id)->load();
158 if ( $note->follow_up ) {
159 if ( $note->follow_up->follow_up_link ) {
160 $note->follow_up->follow_up_link->delete(cascade => 'delete');
162 $note->follow_up->delete(cascade => 'delete');
164 $note->delete(cascade => 'delete');
167 }) || die($db->error);
174 if (!$self->{cv}->name) {
175 flash('error', t8('Customer missing!'));
176 $self->_pre_render();
178 'customer_vendor/form',
179 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
180 %{$self->{template_args}}
186 my @redirect_params = (
188 id => $self->{cv}->id,
189 db => ($self->is_vendor() ? 'vendor' : 'customer'),
192 if ( $self->{contact}->cp_id ) {
193 push(@redirect_params, contact_id => $self->{contact}->cp_id);
196 if ( $self->{shipto}->shipto_id ) {
197 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
200 $self->redirect_to(@redirect_params);
204 sub action_save_and_close {
209 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
210 $::form->redirect($msg);
214 my ($self, $script) = @_;
216 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
217 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
221 my $callback = $::form->escape($::form->{callback}, 1);
222 my $name = $::form->escape($self->{cv}->name, 1);
223 my $db = $self->is_vendor() ? 'vendor' : 'customer';
225 my $url = $self->url_for(
226 controller => $script,
229 $db .'_id' => $self->{cv}->id,
231 type => $::form->{type},
232 callback => $callback,
235 print $::form->redirect_header($url);
238 sub action_save_and_ar_transaction {
241 $main::auth->assert('general_ledger');
243 $self->_transaction('ar.pl');
246 sub action_save_and_ap_transaction {
249 $main::auth->assert('general_ledger');
251 $self->_transaction('ap.pl');
254 sub action_save_and_invoice {
257 if ( $self->is_vendor() ) {
258 $::auth->assert('vendor_invoice_edit');
260 $::auth->assert('invoice_edit');
263 $::form->{type} = 'invoice';
264 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
267 sub action_save_and_order {
270 if ( $self->is_vendor() ) {
271 $::auth->assert('purchase_order_edit');
273 $::auth->assert('sales_order_edit');
276 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
277 $self->_transaction('oe.pl');
280 sub action_save_and_rfq {
283 $::auth->assert('request_quotation_edit');
285 $::form->{type} = 'request_quotation';
286 $self->_transaction('oe.pl');
289 sub action_save_and_quotation {
292 $::auth->assert('sales_quotation_edit');
294 $::form->{type} = 'sales_quotation';
295 $self->_transaction('oe.pl');
301 my $db = $self->{cv}->db;
303 if( !$self->is_orphaned() ) {
304 $self->action_edit();
307 $db->do_transaction(sub {
308 $self->{cv}->delete(cascade => 1);
310 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
311 SL::DB::History->new(
312 trans_id => $self->{cv}->id,
313 snumbers => $snumbers,
314 employee_id => SL::DB::Manager::Employee->current->id,
315 addition => 'DELETED',
317 }) || die($db->error);
319 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
320 $::form->redirect($msg);
326 sub action_delete_contact {
329 my $db = $self->{contact}->db;
331 if ( !$self->{contact}->cp_id ) {
332 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
335 $db->do_transaction(sub {
336 if ( $self->{contact}->used ) {
337 $self->{contact}->detach();
338 $self->{contact}->save();
339 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
341 $self->{contact}->delete(cascade => 1);
342 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
344 }) || die($db->error);
346 $self->{contact} = SL::DB::Contact->new();
349 $self->action_edit();
352 sub action_delete_shipto {
355 my $db = $self->{shipto}->db;
357 if ( !$self->{shipto}->shipto_id ) {
358 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
361 $db->do_transaction(sub {
362 if ( $self->{shipto}->used ) {
363 $self->{shipto}->detach();
364 $self->{shipto}->save(cascade => 1);
365 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
367 $self->{shipto}->delete(cascade => 1);
368 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
370 }) || die($db->error);
372 $self->{shipto} = SL::DB::Shipto->new();
375 $self->action_edit();
383 controller => 'ct.pl',
385 db => $self->is_vendor() ? 'vendor' : 'customer',
388 if ( $::form->{callback} ) {
389 push(@url_params, callback => $::form->{callback});
392 $self->redirect_to(@url_params);
396 sub action_search_contact {
399 my $url = 'ct.pl?action=search_contact&db=customer';
401 if ( $::form->{callback} ) {
402 $url .= '&callback='. $::form->escape($::form->{callback});
405 print $::form->redirect_header($url);
409 sub action_get_delivery {
412 $::auth->assert('sales_all_edit');
414 my $dbh = $::form->get_standard_dbh();
416 my ($arap, $db, $qty_sign);
417 if ( $self->is_vendor() ) {
420 $qty_sign = ' * -1 AS qty';
427 my $where = ' WHERE 1=1';
430 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
431 $where .= " AND ${arap}.shipto_id = ?";
432 push(@values, $::form->{shipto_id});
434 $where .= " AND ${arap}.${db}_id = ?";
435 push(@values, $::form->{id});
438 if ( $::form->{delivery_from} ) {
439 $where .= " AND ${arap}.transdate >= ?";
440 push(@values, conv_date($::form->{delivery_from}));
443 if ( $::form->{delivery_to} ) {
444 $where .= " AND ${arap}.transdate <= ?";
445 push(@values, conv_date($::form->{delivery_to}));
464 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
467 ON ${arap}.id = i.trans_id
473 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
476 ORDER BY ${arap}.transdate DESC LIMIT 15";
478 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
480 $self->render('customer_vendor/get_delivery', { layout => 0 });
483 sub action_ajaj_get_shipto {
489 my $name = 'shipto'. $_;
490 $name => $self->{shipto}->$name;
492 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
496 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
499 sub action_ajaj_get_contact {
507 my $name = 'cp_'. $_;
509 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
510 $name => $self->{contact}->$name->to_lxoffice;
512 $name => $self->{contact}->$name;
516 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
517 satphone satfax project street zipcode city privatphone privatemail birthday
522 $data->{contact_cvars} = {
525 if ( $_->config->type eq 'number' ) {
526 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
528 $_->config->name => $_->value;
533 @{$self->{contact}->cvars_by_config}
538 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
541 sub action_ajaj_customer_autocomplete {
542 my ($self, %params) = @_;
544 my $limit = $::form->{limit} || 20;
545 my $type = $::form->{type} || {};
546 my $query = { ilike => '%'. $::form->{term} .'%' };
551 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
554 push @filter, (or => [ obsolete => undef, obsolete => 0 ]) if !$::form->{obsolete};
556 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
557 my $value_col = $::form->{column} || 'name';
563 value => $_->can($value_col)->($_),
564 label => $_->displayable_name,
566 customernumber => $_->customernumber,
574 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
578 return $::form->{db} eq 'vendor';
582 return $::form->{db} eq 'customer';
588 if ( defined($self->{_is_orphaned}) ) {
589 return $self->{_is_orphaned};
592 my $arap = $self->is_vendor ? 'ap' : 'ar';
595 my $cv = $self->is_vendor ? 'vendor' : 'customer';
599 FROM '. $arap .' AS a
600 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
607 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
611 if ( $self->is_vendor ) {
614 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
618 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
620 return $self->{_is_orphaned} = !$dummy;
623 sub _instantiate_args {
626 my $curr_employee = SL::DB::Manager::Employee->current;
628 if ( $::form->{cv}->{id} ) {
629 if ( $self->is_vendor() ) {
630 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
632 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
635 if ( $self->is_vendor() ) {
636 $self->{cv} = SL::DB::Vendor->new();
638 $self->{cv} = SL::DB::Customer->new();
641 $self->{cv}->assign_attributes(%{$::form->{cv}});
643 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
644 $self->{cv}->taxincluded_checked(undef);
647 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
649 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
650 my $value = $::form->{cv_cvars}->{$cvar->config->name};
652 if ( $cvar->config->type eq 'number' ) {
653 $value = $::form->parse_amount(\%::myconfig, $value);
656 $cvar->value($value);
659 if ( $::form->{note}->{id} ) {
660 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
661 $self->{note_followup} = $self->{note}->follow_up;
662 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
664 $self->{note} = SL::DB::Note->new();
665 $self->{note_followup} = SL::DB::FollowUp->new();
666 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
669 $self->{note}->assign_attributes(%{$::form->{note}});
670 $self->{note}->created_by($curr_employee->id);
671 $self->{note}->trans_module('ct');
673 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
674 $self->{note_followup}->note($self->{note});
675 $self->{note_followup}->created_by($curr_employee->id);
677 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
678 $self->{note_followup_link}->trans_info($self->{cv}->name);
680 if ( $::form->{shipto}->{shipto_id} ) {
681 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
683 $self->{shipto} = SL::DB::Shipto->new();
685 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
686 $self->{shipto}->module('CT');
688 if ( $::form->{contact}->{cp_id} ) {
689 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
691 $self->{contact} = SL::DB::Contact->new();
693 $self->{contact}->assign_attributes(%{$::form->{contact}});
695 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
696 my $value = $::form->{contact_cvars}->{$cvar->config->name};
698 if ( $cvar->config->type eq 'number' ) {
699 $value = $::form->parse_amount(\%::myconfig, $value);
702 $cvar->value($value);
706 sub _load_customer_vendor {
709 if ( $self->is_vendor() ) {
710 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
712 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
715 if ( $::form->{note_id} ) {
716 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
717 $self->{note_followup} = $self->{note}->follow_up;
718 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
720 $self->{note} = SL::DB::Note->new();
721 $self->{note_followup} = SL::DB::FollowUp->new();
722 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
725 if ( $::form->{shipto_id} ) {
726 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
728 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
729 die($::locale->text('Error'));
732 $self->{shipto} = SL::DB::Shipto->new();
735 if ( $::form->{contact_id} ) {
736 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
738 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
739 die($::locale->text('Error'));
742 $self->{contact} = SL::DB::Contact->new();
746 sub _create_customer_vendor {
749 if ( $self->is_vendor() ) {
750 $self->{cv} = SL::DB::Vendor->new();
752 $self->{cv} = SL::DB::Customer->new();
754 $self->{cv}->currency_id($::instance_conf->get_currency_id());
756 $self->{note} = SL::DB::Note->new();
758 $self->{note_followup} = SL::DB::FollowUp->new();
760 $self->{shipto} = SL::DB::Shipto->new();
762 $self->{contact} = SL::DB::Contact->new();
768 my $dbh = $::form->get_standard_dbh();
772 $self->{all_business} = SL::DB::Manager::Business->get_all();
774 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
777 'SELECT DISTINCT(greeting)
779 WHERE greeting IS NOT NULL AND greeting != \'\'
781 SELECT DISTINCT(greeting)
783 WHERE greeting IS NOT NULL AND greeting != \'\'
785 $self->{all_greetings} = [
788 selectall_hashref_query($::form, $dbh, $query)
793 'SELECT DISTINCT(cp_title) AS title
795 WHERE cp_title IS NOT NULL AND cp_title != \'\'
797 $self->{all_titles} = [
800 selectall_hashref_query($::form, $dbh, $query)
804 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
806 $self->{all_languages} = SL::DB::Manager::Language->get_all();
808 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
810 if ( $::instance_conf->get_vertreter() ) {
818 selectall_hashref_query($::form, $dbh, $query)
822 if ( $business_ids->[0] ) {
823 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
825 $self->{all_salesman_customers} = [];
828 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
831 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
833 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
835 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
838 'SELECT DISTINCT(cp_abteilung) AS department
840 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
841 ORDER BY cp_abteilung';
842 $self->{all_departments} = [
844 { $_->{department}; }
845 selectall_hashref_query($::form, $dbh, $query)
849 $self->{contacts} = $self->{cv}->contacts;
850 $self->{contacts} ||= [];
852 $self->{shiptos} = $self->{cv}->shipto;
853 $self->{shiptos} ||= [];
855 $self->{notes} = SL::DB::Manager::Note->get_all(
857 trans_id => $self->{cv}->id,
858 trans_module => 'ct',
860 with_objects => ['follow_up'],
863 $self->{template_args} ||= {};
865 $::request->{layout}->add_javascripts('autocomplete_customer.js');
866 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
872 # check if feature is enabled (select normalize_vc_names from defaults)
873 return unless ($::instance_conf->get_normalize_vc_names);
875 return unless $self->{cv};
876 my $name = $self->{cv}->name;
880 $self->{cv}->name($name);
883 sub home_address_for_google_maps {
886 my $address = $::instance_conf->get_address // '';
887 $address =~ s{^\s+|\s+$|\r+}{}g;
888 $address =~ s{\n+}{,}g;
889 $address =~ s{\s+}{ }g;