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 = ''
474 AND ". ($arap eq 'ar' ? 'oe.customer_id IS NOT NULL' : 'oe_vendor_id IS NOT NULL') ." )
477 ORDER BY ${arap}.transdate DESC LIMIT 15";
479 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
481 $self->render('customer_vendor/get_delivery', { layout => 0 });
484 sub action_ajaj_get_shipto {
490 my $name = 'shipto'. $_;
491 $name => $self->{shipto}->$name;
493 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
497 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
500 sub action_ajaj_get_contact {
508 my $name = 'cp_'. $_;
510 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
511 $name => $self->{contact}->$name->to_lxoffice;
513 $name => $self->{contact}->$name;
517 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
518 satphone satfax project street zipcode city privatphone privatemail birthday
523 $data->{contact_cvars} = {
526 if ( $_->config->type eq 'number' ) {
527 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
529 $_->config->name => $_->value;
534 @{$self->{contact}->cvars_by_config}
539 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
542 sub action_ajaj_customer_autocomplete {
543 my ($self, %params) = @_;
545 my $limit = $::form->{limit} || 20;
546 my $type = $::form->{type} || {};
547 my $query = { ilike => '%'. $::form->{term} .'%' };
552 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
555 push @filter, (or => [ obsolete => undef, obsolete => 0 ]) if !$::form->{obsolete};
557 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
558 my $value_col = $::form->{column} || 'name';
564 value => $_->can($value_col)->($_),
565 label => $_->displayable_name,
567 customernumber => $_->customernumber,
575 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
579 return $::form->{db} eq 'vendor';
583 return $::form->{db} eq 'customer';
589 if ( defined($self->{_is_orphaned}) ) {
590 return $self->{_is_orphaned};
593 my $arap = $self->is_vendor ? 'ap' : 'ar';
596 my $cv = $self->is_vendor ? 'vendor' : 'customer';
600 FROM '. $arap .' AS a
601 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
608 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
612 if ( $self->is_vendor ) {
615 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
619 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
621 return $self->{_is_orphaned} = !$dummy;
624 sub _instantiate_args {
627 my $curr_employee = SL::DB::Manager::Employee->current;
629 if ( $::form->{cv}->{id} ) {
630 if ( $self->is_vendor() ) {
631 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
633 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
636 if ( $self->is_vendor() ) {
637 $self->{cv} = SL::DB::Vendor->new();
639 $self->{cv} = SL::DB::Customer->new();
642 $self->{cv}->assign_attributes(%{$::form->{cv}});
644 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
645 $self->{cv}->taxincluded_checked(undef);
648 $self->{cv}->hourly_rate($::instance_conf->get_customer_hourly_rate) if $self->is_customer && !$self->{cv}->hourly_rate;
650 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
651 my $value = $::form->{cv_cvars}->{$cvar->config->name};
653 if ( $cvar->config->type eq 'number' ) {
654 $value = $::form->parse_amount(\%::myconfig, $value);
657 $cvar->value($value);
660 if ( $::form->{note}->{id} ) {
661 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
662 $self->{note_followup} = $self->{note}->follow_up;
663 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
665 $self->{note} = SL::DB::Note->new();
666 $self->{note_followup} = SL::DB::FollowUp->new();
667 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
670 $self->{note}->assign_attributes(%{$::form->{note}});
671 $self->{note}->created_by($curr_employee->id);
672 $self->{note}->trans_module('ct');
674 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
675 $self->{note_followup}->note($self->{note});
676 $self->{note_followup}->created_by($curr_employee->id);
678 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
679 $self->{note_followup_link}->trans_info($self->{cv}->name);
681 if ( $::form->{shipto}->{shipto_id} ) {
682 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
684 $self->{shipto} = SL::DB::Shipto->new();
686 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
687 $self->{shipto}->module('CT');
689 if ( $::form->{contact}->{cp_id} ) {
690 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
692 $self->{contact} = SL::DB::Contact->new();
694 $self->{contact}->assign_attributes(%{$::form->{contact}});
696 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
697 my $value = $::form->{contact_cvars}->{$cvar->config->name};
699 if ( $cvar->config->type eq 'number' ) {
700 $value = $::form->parse_amount(\%::myconfig, $value);
703 $cvar->value($value);
707 sub _load_customer_vendor {
710 if ( $self->is_vendor() ) {
711 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
713 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
716 if ( $::form->{note_id} ) {
717 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
718 $self->{note_followup} = $self->{note}->follow_up;
719 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
721 $self->{note} = SL::DB::Note->new();
722 $self->{note_followup} = SL::DB::FollowUp->new();
723 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
726 if ( $::form->{shipto_id} ) {
727 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
729 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
730 die($::locale->text('Error'));
733 $self->{shipto} = SL::DB::Shipto->new();
736 if ( $::form->{contact_id} ) {
737 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
739 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
740 die($::locale->text('Error'));
743 $self->{contact} = SL::DB::Contact->new();
747 sub _create_customer_vendor {
750 if ( $self->is_vendor() ) {
751 $self->{cv} = SL::DB::Vendor->new();
753 $self->{cv} = SL::DB::Customer->new();
755 $self->{cv}->currency_id($::instance_conf->get_currency_id());
757 $self->{note} = SL::DB::Note->new();
759 $self->{note_followup} = SL::DB::FollowUp->new();
761 $self->{shipto} = SL::DB::Shipto->new();
763 $self->{contact} = SL::DB::Contact->new();
769 my $dbh = $::form->get_standard_dbh();
773 $self->{all_business} = SL::DB::Manager::Business->get_all();
775 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
778 'SELECT DISTINCT(greeting)
780 WHERE greeting IS NOT NULL AND greeting != \'\'
782 SELECT DISTINCT(greeting)
784 WHERE greeting IS NOT NULL AND greeting != \'\'
786 $self->{all_greetings} = [
789 selectall_hashref_query($::form, $dbh, $query)
794 'SELECT DISTINCT(cp_title) AS title
796 WHERE cp_title IS NOT NULL AND cp_title != \'\'
798 $self->{all_titles} = [
801 selectall_hashref_query($::form, $dbh, $query)
805 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
807 $self->{all_languages} = SL::DB::Manager::Language->get_all();
809 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
811 if ( $::instance_conf->get_vertreter() ) {
819 selectall_hashref_query($::form, $dbh, $query)
823 if ( $business_ids->[0] ) {
824 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
826 $self->{all_salesman_customers} = [];
829 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
832 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
834 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
836 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
839 'SELECT DISTINCT(cp_abteilung) AS department
841 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
842 ORDER BY cp_abteilung';
843 $self->{all_departments} = [
845 { $_->{department}; }
846 selectall_hashref_query($::form, $dbh, $query)
850 $self->{contacts} = $self->{cv}->contacts;
851 $self->{contacts} ||= [];
853 $self->{shiptos} = $self->{cv}->shipto;
854 $self->{shiptos} ||= [];
856 $self->{notes} = SL::DB::Manager::Note->get_all(
858 trans_id => $self->{cv}->id,
859 trans_module => 'ct',
861 with_objects => ['follow_up'],
864 $self->{template_args} ||= {};
866 $::request->{layout}->add_javascripts('autocomplete_customer.js');
867 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
873 # check if feature is enabled (select normalize_vc_names from defaults)
874 return unless ($::instance_conf->get_normalize_vc_names);
876 return unless $self->{cv};
877 my $name = $self->{cv}->name;
881 $self->{cv}->name($name);
884 sub home_address_for_google_maps {
887 my $address = $::instance_conf->get_address // '';
888 $address =~ s{^\s+|\s+$|\r+}{}g;
889 $address =~ s{\n+}{,}g;
890 $address =~ s{\s+}{ }g;