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');
73 'customer_vendor/form',
74 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
75 %{$self->{template_args}}
84 'customer_vendor/form',
85 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
86 %{$self->{template_args}}
93 my $db = $self->{cv}->db;
95 $db->do_transaction(sub {
97 if ( $self->is_vendor() ) {
98 if ( $self->{cv}->vendornumber ) {
99 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
102 if ( $self->{cv}->customernumber ) {
103 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
107 foreach my $entry (@{$cvs_by_nr}) {
108 if( $entry->id != $self->{cv}->id ) {
110 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
112 $::form->error($msg);
116 $self->{cv}->save(cascade => 1);
118 $self->{contact}->cp_cv_id($self->{cv}->id);
119 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
120 $self->{contact}->save();
123 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
125 if ( !$self->{note_followup}->follow_up_date ) {
126 $::form->error($::locale->text('Date missing!'));
129 $self->{note}->trans_id($self->{cv}->id);
130 $self->{note}->save();
132 $self->{note_followup}->save();
134 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
135 $self->{note_followup_link}->trans_id($self->{cv}->id);
136 $self->{note_followup_link}->save();
138 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
141 $self->{shipto}->trans_id($self->{cv}->id);
142 if( $self->{shipto}->shiptoname ne '' ) {
143 $self->{shipto}->save();
146 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
147 SL::DB::History->new(
148 trans_id => $self->{cv}->id,
149 snumbers => $snumbers,
150 employee_id => SL::DB::Manager::Employee->current->id,
154 if ( $::form->{delete_notes} ) {
155 foreach my $note_id (@{ $::form->{delete_notes} }) {
156 my $note = SL::DB::Note->new(id => $note_id)->load();
157 if ( $note->follow_up ) {
158 if ( $note->follow_up->follow_up_link ) {
159 $note->follow_up->follow_up_link->delete(cascade => 'delete');
161 $note->follow_up->delete(cascade => 'delete');
163 $note->delete(cascade => 'delete');
166 }) || die($db->error);
173 if (!$self->{cv}->name) {
174 flash('error', t8('Customer missing!'));
175 $self->_pre_render();
177 'customer_vendor/form',
178 title => ($self->is_vendor() ? t8('Edit Vendor') : t8('Edit Customer')),
179 %{$self->{template_args}}
185 my @redirect_params = (
187 id => $self->{cv}->id,
188 db => ($self->is_vendor() ? 'vendor' : 'customer'),
191 if ( $self->{contact}->cp_id ) {
192 push(@redirect_params, contact_id => $self->{contact}->cp_id);
195 if ( $self->{shipto}->shipto_id ) {
196 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
199 $self->redirect_to(@redirect_params);
203 sub action_save_and_close {
208 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
209 $::form->redirect($msg);
213 my ($self, $script) = @_;
215 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
216 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
220 my $callback = $::form->escape($::form->{callback}, 1);
221 my $name = $::form->escape($self->{cv}->name, 1);
222 my $db = $self->is_vendor() ? 'vendor' : 'customer';
224 my $url = $self->url_for(
225 controller => $script,
228 $db .'_id' => $self->{cv}->id,
230 type => $::form->{type},
231 callback => $callback,
234 print $::form->redirect_header($url);
237 sub action_save_and_ar_transaction {
240 $main::auth->assert('general_ledger');
242 $self->_transaction('ar.pl');
245 sub action_save_and_ap_transaction {
248 $main::auth->assert('general_ledger');
250 $self->_transaction('ap.pl');
253 sub action_save_and_invoice {
256 if ( $self->is_vendor() ) {
257 $::auth->assert('vendor_invoice_edit');
259 $::auth->assert('invoice_edit');
262 $::form->{type} = 'invoice';
263 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
266 sub action_save_and_order {
269 if ( $self->is_vendor() ) {
270 $::auth->assert('purchase_order_edit');
272 $::auth->assert('sales_order_edit');
275 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
276 $self->_transaction('oe.pl');
279 sub action_save_and_rfq {
282 $::auth->assert('request_quotation_edit');
284 $::form->{type} = 'request_quotation';
285 $self->_transaction('oe.pl');
288 sub action_save_and_quotation {
291 $::auth->assert('sales_quotation_edit');
293 $::form->{type} = 'sales_quotation';
294 $self->_transaction('oe.pl');
300 my $db = $self->{cv}->db;
302 if( !$self->is_orphaned() ) {
303 $self->action_edit();
306 $db->do_transaction(sub {
307 $self->{cv}->delete(cascade => 1);
309 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
310 SL::DB::History->new(
311 trans_id => $self->{cv}->id,
312 snumbers => $snumbers,
313 employee_id => SL::DB::Manager::Employee->current->id,
314 addition => 'DELETED',
316 }) || die($db->error);
318 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
319 $::form->redirect($msg);
325 sub action_delete_contact {
328 my $db = $self->{contact}->db;
330 if ( !$self->{contact}->cp_id ) {
331 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
334 $db->do_transaction(sub {
335 if ( $self->{contact}->used ) {
336 $self->{contact}->detach();
337 $self->{contact}->save();
338 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
340 $self->{contact}->delete(cascade => 1);
341 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
343 }) || die($db->error);
345 $self->{contact} = SL::DB::Contact->new();
348 $self->action_edit();
351 sub action_delete_shipto {
354 my $db = $self->{shipto}->db;
356 if ( !$self->{shipto}->shipto_id ) {
357 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
360 $db->do_transaction(sub {
361 if ( $self->{shipto}->used ) {
362 $self->{shipto}->detach();
363 $self->{shipto}->save(cascade => 1);
364 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
366 $self->{shipto}->delete(cascade => 1);
367 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
369 }) || die($db->error);
371 $self->{shipto} = SL::DB::Shipto->new();
374 $self->action_edit();
382 controller => 'ct.pl',
384 db => $self->is_vendor() ? 'vendor' : 'customer',
387 if ( $::form->{callback} ) {
388 push(@url_params, callback => $::form->{callback});
391 $self->redirect_to(@url_params);
395 sub action_search_contact {
398 my $url = 'ct.pl?action=search_contact&db=customer';
400 if ( $::form->{callback} ) {
401 $url .= '&callback='. $::form->escape($::form->{callback});
404 print $::form->redirect_header($url);
408 sub action_get_delivery {
411 $::auth->assert('sales_all_edit');
413 my $dbh = $::form->get_standard_dbh();
415 my ($arap, $db, $qty_sign);
416 if ( $self->is_vendor() ) {
419 $qty_sign = ' * -1 AS qty';
426 my $where = ' WHERE 1=1';
429 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
430 $where .= " AND ${arap}.shipto_id = ?";
431 push(@values, $::form->{shipto_id});
433 $where .= " AND ${arap}.${db}_id = ?";
434 push(@values, $::form->{id});
437 if ( $::form->{delivery_from} ) {
438 $where .= " AND ${arap}.transdate >= ?";
439 push(@values, conv_date($::form->{delivery_from}));
442 if ( $::form->{delivery_to} ) {
443 $where .= " AND ${arap}.transdate <= ?";
444 push(@values, conv_date($::form->{delivery_to}));
463 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
466 ON ${arap}.id = i.trans_id
472 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
475 ORDER BY ${arap}.transdate DESC LIMIT 15";
477 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
479 $self->render('customer_vendor/get_delivery', { layout => 0 });
482 sub action_ajaj_get_shipto {
488 my $name = 'shipto'. $_;
489 $name => $self->{shipto}->$name;
491 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
495 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
498 sub action_ajaj_get_contact {
506 my $name = 'cp_'. $_;
508 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
509 $name => $self->{contact}->$name->to_lxoffice;
511 $name => $self->{contact}->$name;
515 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
516 satphone satfax project street zipcode city privatphone privatemail birthday
521 $data->{contact_cvars} = {
524 if ( $_->config->type eq 'number' ) {
525 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
527 $_->config->name => $_->value;
532 @{$self->{contact}->cvars_by_config}
537 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
540 sub action_ajaj_customer_autocomplete {
541 my ($self, %params) = @_;
543 my $limit = $::form->{limit} || 20;
544 my $type = $::form->{type} || {};
545 my $query = { ilike => '%'. $::form->{term} .'%' };
550 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
553 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
554 my $value_col = $::form->{column} || 'name';
560 value => $_->can($value_col)->($_),
561 label => $_->displayable_name,
563 customernumber => $_->customernumber,
571 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
575 return $::form->{db} eq 'vendor';
579 return $::form->{db} eq 'customer';
585 if ( defined($self->{_is_orphaned}) ) {
586 return $self->{_is_orphaned};
589 my $arap = $self->is_vendor ? 'ap' : 'ar';
592 my $cv = $self->is_vendor ? 'vendor' : 'customer';
596 FROM '. $arap .' AS a
597 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
604 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
608 if ( $self->is_vendor ) {
611 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
615 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
617 return $self->{_is_orphaned} = !$dummy;
620 sub _instantiate_args {
623 my $curr_employee = SL::DB::Manager::Employee->current;
625 if ( $::form->{cv}->{id} ) {
626 if ( $self->is_vendor() ) {
627 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
629 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
632 if ( $self->is_vendor() ) {
633 $self->{cv} = SL::DB::Vendor->new();
635 $self->{cv} = SL::DB::Customer->new();
638 $self->{cv}->assign_attributes(%{$::form->{cv}});
640 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
641 $self->{cv}->taxincluded_checked(undef);
645 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
646 my $value = $::form->{cv_cvars}->{$cvar->config->name};
648 if ( $cvar->config->type eq 'number' ) {
649 $value = $::form->parse_amount(\%::myconfig, $value);
652 $cvar->value($value);
655 if ( $::form->{note}->{id} ) {
656 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
657 $self->{note_followup} = $self->{note}->follow_up;
658 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
660 $self->{note} = SL::DB::Note->new();
661 $self->{note_followup} = SL::DB::FollowUp->new();
662 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
665 $self->{note}->assign_attributes(%{$::form->{note}});
666 $self->{note}->created_by($curr_employee->id);
667 $self->{note}->trans_module('ct');
669 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
670 $self->{note_followup}->note($self->{note});
671 $self->{note_followup}->created_by($curr_employee->id);
673 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
674 $self->{note_followup_link}->trans_info($self->{cv}->name);
676 if ( $::form->{shipto}->{shipto_id} ) {
677 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
679 $self->{shipto} = SL::DB::Shipto->new();
681 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
682 $self->{shipto}->module('CT');
684 if ( $::form->{contact}->{cp_id} ) {
685 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
687 $self->{contact} = SL::DB::Contact->new();
689 $self->{contact}->assign_attributes(%{$::form->{contact}});
691 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
692 my $value = $::form->{contact_cvars}->{$cvar->config->name};
694 if ( $cvar->config->type eq 'number' ) {
695 $value = $::form->parse_amount(\%::myconfig, $value);
698 $cvar->value($value);
702 sub _load_customer_vendor {
705 if ( $self->is_vendor() ) {
706 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
708 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
711 if ( $::form->{note_id} ) {
712 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
713 $self->{note_followup} = $self->{note}->follow_up;
714 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
716 $self->{note} = SL::DB::Note->new();
717 $self->{note_followup} = SL::DB::FollowUp->new();
718 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
721 if ( $::form->{shipto_id} ) {
722 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
724 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
725 die($::locale->text('Error'));
728 $self->{shipto} = SL::DB::Shipto->new();
731 if ( $::form->{contact_id} ) {
732 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
734 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
735 die($::locale->text('Error'));
738 $self->{contact} = SL::DB::Contact->new();
742 sub _create_customer_vendor {
745 if ( $self->is_vendor() ) {
746 $self->{cv} = SL::DB::Vendor->new();
748 $self->{cv} = SL::DB::Customer->new();
750 $self->{cv}->currency_id($::instance_conf->get_currency_id());
752 $self->{note} = SL::DB::Note->new();
754 $self->{note_followup} = SL::DB::FollowUp->new();
756 $self->{shipto} = SL::DB::Shipto->new();
758 $self->{contact} = SL::DB::Contact->new();
764 my $dbh = $::form->get_standard_dbh();
768 $self->{all_business} = SL::DB::Manager::Business->get_all();
770 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
773 'SELECT DISTINCT(greeting)
775 WHERE greeting IS NOT NULL AND greeting != \'\'
777 SELECT DISTINCT(greeting)
779 WHERE greeting IS NOT NULL AND greeting != \'\'
781 $self->{all_greetings} = [
784 selectall_hashref_query($::form, $dbh, $query)
789 'SELECT DISTINCT(cp_title) AS title
791 WHERE cp_title IS NOT NULL AND cp_title != \'\'
793 $self->{all_titles} = [
796 selectall_hashref_query($::form, $dbh, $query)
800 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
802 $self->{all_languages} = SL::DB::Manager::Language->get_all();
804 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
806 if ( $::instance_conf->get_vertreter() ) {
814 selectall_hashref_query($::form, $dbh, $query)
818 if ( $business_ids->[0] ) {
819 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
821 $self->{all_salesman_customers} = [];
824 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
827 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
829 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
831 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
834 'SELECT DISTINCT(cp_abteilung) AS department
836 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
837 ORDER BY cp_abteilung';
838 $self->{all_departments} = [
840 { $_->{department}; }
841 selectall_hashref_query($::form, $dbh, $query)
845 $self->{contacts} = $self->{cv}->contacts;
846 $self->{contacts} ||= [];
848 $self->{shiptos} = $self->{cv}->shipto;
849 $self->{shiptos} ||= [];
851 $self->{notes} = SL::DB::Manager::Note->get_all(
853 trans_id => $self->{cv}->id,
854 trans_module => 'ct',
856 with_objects => ['follow_up'],
859 $self->{template_args} ||= {};
861 $::request->{layout}->add_javascripts('autocomplete_customer.js');
862 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
868 # check if feature is enabled (select normalize_vc_names from defaults)
869 return unless ($::instance_conf->get_normalize_vc_names);
871 return unless $self->{cv};
872 my $name = $self->{cv}->name;
876 $self->{cv}->name($name);