1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
17 use SL::DB::PaymentTerm;
18 use SL::DB::Pricegroup;
21 use SL::DB::FollowUpLink;
26 __PACKAGE__->run_before(
28 $::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',
70 'customer_vendor/form',
71 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
72 %{$self->{template_args}}
81 'customer_vendor/form',
82 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
83 %{$self->{template_args}}
90 my $db = $self->{cv}->db;
92 $db->do_transaction(sub {
94 if ( $self->is_vendor() ) {
95 if ( $self->{cv}->vendornumber ) {
96 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
99 if ( $self->{cv}->customernumber ) {
100 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
104 foreach my $entry (@{$cvs_by_nr}) {
105 if( $entry->id != $self->{cv}->id ) {
107 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
109 $::form->error($msg);
113 $self->{cv}->save(cascade => 1);
115 $self->{contact}->cp_cv_id($self->{cv}->id);
116 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
117 $self->{contact}->save();
120 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
122 if ( !$self->{note_followup}->follow_up_date ) {
123 $::form->error($::locale->text('Date missing!'));
126 $self->{note}->trans_id($self->{cv}->id);
127 $self->{note}->save();
129 $self->{note_followup}->save();
131 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
132 $self->{note_followup_link}->trans_id($self->{cv}->id);
133 $self->{note_followup_link}->save();
135 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
138 $self->{shipto}->trans_id($self->{cv}->id);
139 if( $self->{shipto}->shiptoname ne '' ) {
140 $self->{shipto}->save();
143 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
144 SL::DB::History->new(
145 trans_id => $self->{cv}->id,
146 snumbers => $snumbers,
147 employee_id => SL::DB::Manager::Employee->current->id,
150 }) || die($db->error);
159 my @redirect_params = (
161 id => $self->{cv}->id,
162 db => ($self->is_vendor() ? 'vendor' : 'customer'),
165 if ( $self->{contact}->cp_id ) {
166 push(@redirect_params, contact_id => $self->{contact}->cp_id);
169 if ( $self->{shipto}->shipto_id ) {
170 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
173 if ( $self->{note}->id ) {
174 push(@redirect_params, note_id => $self->{note}->id);
177 $self->redirect_to(@redirect_params);
180 sub action_save_and_close {
185 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
186 $::form->redirect($msg);
190 my ($self, $script) = @_;
192 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
193 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
197 my $callback = $::form->escape($::form->{callback}, 1);
198 my $name = $::form->escape($self->{cv}->name, 1);
199 my $db = $self->is_vendor() ? 'vendor' : 'customer';
201 my $url = $self->url_for(
202 controller => $script,
205 $db .'_id' => $self->{cv}->id,
207 type => $::form->{type},
208 callback => $callback,
211 print $::form->redirect_header($url);
214 sub action_save_and_ar_transaction {
217 $main::auth->assert('general_ledger');
219 $self->_transaction('ar.pl');
222 sub action_save_and_ap_transaction {
225 $main::auth->assert('general_ledger');
227 $self->_transaction('ap.pl');
230 sub action_save_and_invoice {
233 if ( $self->is_vendor() ) {
234 $::auth->assert('vendor_invoice_edit');
236 $::auth->assert('invoice_edit');
239 $::form->{type} = 'invoice';
240 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
243 sub action_save_and_order {
246 if ( $self->is_vendor() ) {
247 $::auth->assert('purchase_order_edit');
249 $::auth->assert('sales_order_edit');
252 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
253 $self->_transaction('oe.pl');
256 sub action_save_and_rfq {
259 $::auth->assert('request_quotation_edit');
261 $::form->{type} = 'request_quotation';
262 $self->_transaction('oe.pl');
265 sub action_save_and_quotation {
268 $::auth->assert('sales_quotation_edit');
270 $::form->{type} = 'sales_quotation';
271 $self->_transaction('oe.pl');
277 my $db = $self->{cv}->db;
279 if( !$self->is_orphaned() ) {
280 $self->action_edit();
283 $db->do_transaction(sub {
284 $self->{cv}->delete(cascade => 1);
286 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
287 SL::DB::History->new(
288 trans_id => $self->{cv}->id,
289 snumbers => $snumbers,
290 employee_id => SL::DB::Manager::Employee->current->id,
291 addition => 'DELETED',
293 }) || die($db->error);
295 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
296 $::form->redirect($msg);
302 sub action_delete_contact {
305 my $db = $self->{contact}->db;
307 if ( !$self->{contact}->cp_id ) {
308 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
311 $db->do_transaction(sub {
312 if ( $self->{contact}->used ) {
313 $self->{contact}->detach();
314 $self->{contact}->save();
315 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
317 $self->{contact}->delete(cascade => 1);
318 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
320 }) || die($db->error);
322 $self->{contact} = SL::DB::Contact->new();
325 $self->action_edit();
328 sub action_delete_shipto {
331 my $db = $self->{shipto}->db;
333 if ( !$self->{shipto}->shipto_id ) {
334 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
337 $db->do_transaction(sub {
338 if ( $self->{shipto}->used ) {
339 $self->{shipto}->detach();
340 $self->{shipto}->save(cascade => 1);
341 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
343 $self->{shipto}->delete(cascade => 1);
344 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
346 }) || die($db->error);
348 $self->{shipto} = SL::DB::Shipto->new();
351 $self->action_edit();
359 controller => 'ct.pl',
361 db => $self->is_vendor() ? 'vendor' : 'customer',
364 if ( $::form->{callback} ) {
365 push(@url_params, callback => $::form->{callback});
368 $self->redirect_to(@url_params);
372 sub action_search_contact {
375 my $url = 'ct.pl?action=search_contact&db=customer';
377 if ( $::form->{callback} ) {
378 $url .= '&callback='. $::from->escape($::form->{callback});
381 print $::form->redirect_header($url);
385 sub action_get_delivery {
388 my $dbh = $::form->get_standard_dbh();
390 my ($arap, $db, $qty_sign);
391 if ( $self->is_vendor() ) {
394 $qty_sign = ' * -1 AS qty';
401 my $where = ' WHERE 1=1 ';
404 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
405 $where .= "AND ${arap}.shipto_id = ?";
406 push(@values, $::form->{shipto_id});
409 if ( $::form->{delivery_from} ) {
410 $where .= "AND ${arap}.transdate >= ?";
411 push(@values, conv_date($::form->{delivery_from}));
414 if ( $::form->{delivery_to} ) {
415 $where .= "AND ${arap}.transdate <= ?";
416 push(@values, conv_date($::form->{delivery_to}));
435 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
438 ON ${arap}.id = i.trans_id
444 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
447 ORDER BY ${arap}.transdate DESC LIMIT 15";
449 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
451 $self->render('customer_vendor/get_delivery', { layout => 0 });
454 sub action_ajaj_get_shipto {
460 my $name = 'shipto'. $_;
461 $name => $self->{shipto}->$name;
463 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
467 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
470 sub action_ajaj_get_contact {
478 my $name = 'cp_'. $_;
480 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
481 $name => $self->{contact}->$name->to_lxoffice;
483 $name => $self->{contact}->$name;
487 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
488 satphone satfax project street zipcode city privatphone privatemail birthday
493 $data->{contact_cvars} = {
496 if ( $_->config->type eq 'number' ) {
497 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
499 $_->config->name => $_->value;
504 @{$self->{contact}->cvars_by_config}
509 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
512 sub action_ajaj_customer_autocomplete {
513 my ($self, %params) = @_;
515 my $limit = $::form->{limit} || 20;
516 my $type = $::form->{type} || {};
517 my $query = { ilike => '%'. $::form->{term} .'%' };
522 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
525 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
526 my $value_col = $::form->{column} || 'name';
532 value => $_->can($value_col)->($_),
533 label => $_->displayable_name,
535 customernumber => $_->customernumber,
543 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
547 return $::form->{db} eq 'vendor';
551 return $::form->{db} eq 'customer';
557 if ( defined($self->{_is_orphaned}) ) {
558 return $self->{_is_orphaned};
561 my $arap = $self->is_vendor ? 'ap' : 'ar';
564 my $cv = $self->is_vendor ? 'vendor' : 'customer';
568 FROM '. $arap .' AS a
569 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
576 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
580 if ( $self->is_vendor ) {
583 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
587 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
589 return $self->{_is_orphaned} = !$dummy;
592 sub _instantiate_args {
595 my $curr_employee = SL::DB::Manager::Employee->current;
597 if ( $::form->{cv}->{id} ) {
598 if ( $self->is_vendor() ) {
599 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
601 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
604 if ( $self->is_vendor() ) {
605 $self->{cv} = SL::DB::Vendor->new();
607 $self->{cv} = SL::DB::Customer->new();
610 $self->{cv}->assign_attributes(%{$::form->{cv}});
612 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
613 $self->{cv}->taxincluded_checked(undef);
617 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
618 my $value = $::form->{cv_cvars}->{$cvar->config->name};
620 if ( $cvar->config->type eq 'number' ) {
621 $value = $::form->parse_amount(\%::myconfig, $value);
624 $cvar->value($value);
627 if ( $::form->{note}->{id} ) {
628 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
630 $self->{note_followup_link} = SL::DB::Manager::FollowUpLink->get_all(
632 'follow_up.note_id' => $self->{note}->id,
633 trans_id => $self->{cv}->id,
634 trans_type => ($self->is_vendor() ? 'vendor' : 'customer'),
636 with_objects => ['follow_up'],
639 $self->{note_followup} = $self->{note_followup_link}->follow_up;
641 $self->{note} = SL::DB::Note->new();
642 $self->{note_followup} = SL::DB::FollowUp->new();
643 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
646 $self->{note}->assign_attributes(%{$::form->{note}});
647 $self->{note}->created_by($curr_employee->id);
648 $self->{note}->trans_module('ct');
650 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
651 $self->{note_followup}->note($self->{note});
652 $self->{note_followup}->created_by($curr_employee->id);
654 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
655 $self->{note_followup_link}->trans_info($self->{cv}->name);
657 if ( $::form->{shipto}->{shipto_id} ) {
658 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
660 $self->{shipto} = SL::DB::Shipto->new();
662 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
663 $self->{shipto}->module('CT');
665 if ( $::form->{contact}->{cp_id} ) {
666 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
668 $self->{contact} = SL::DB::Contact->new();
670 $self->{contact}->assign_attributes(%{$::form->{contact}});
672 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
673 my $value = $::form->{contact_cvars}->{$cvar->config->name};
675 if ( $cvar->config->type eq 'number' ) {
676 $value = $::form->parse_amount(\%::myconfig, $value);
679 $cvar->value($value);
683 sub _load_customer_vendor {
686 if ( $self->is_vendor() ) {
687 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
689 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
692 if ( $::form->{note_id} ) {
693 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
695 $self->{note_followup_link} = SL::DB::Manager::FollowUpLink->get_all(
697 'follow_up.note_id' => $self->{note}->id,
698 trans_id => $self->{cv}->id,
699 trans_type => ($self->is_vendor() ? 'vendor' : 'customer'),
701 with_objects => ['follow_up'],
704 $self->{note_followup} = $self->{note_followup_link}->follow_up;
706 $self->{note} = SL::DB::Note->new();
707 $self->{note_followup} = SL::DB::FollowUp->new();
708 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
711 if ( $::form->{shipto_id} ) {
712 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
714 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
715 die($::locale->text('Error'));
718 $self->{shipto} = SL::DB::Shipto->new();
721 if ( $::form->{contact_id} ) {
722 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
724 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
725 die($::locale->text('Error'));
728 $self->{contact} = SL::DB::Contact->new();
732 sub _create_customer_vendor {
735 if ( $self->is_vendor() ) {
736 $self->{cv} = SL::DB::Vendor->new();
738 $self->{cv} = SL::DB::Customer->new();
740 $self->{cv}->currency_id($::instance_conf->get_currency_id());
742 $self->{note} = SL::DB::Note->new();
744 $self->{note_followup} = SL::DB::FollowUp->new();
746 $self->{shipto} = SL::DB::Shipto->new();
748 $self->{contact} = SL::DB::Contact->new();
754 my $dbh = $::form->get_standard_dbh();
758 $self->{all_business} = SL::DB::Manager::Business->get_all();
760 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
763 'SELECT DISTINCT(greeting)
765 WHERE greeting IS NOT NULL AND greeting != \'\'
767 SELECT DISTINCT(greeting)
769 WHERE greeting IS NOT NULL AND greeting != \'\'
771 $self->{all_greetings} = [
774 selectall_hashref_query($::form, $dbh, $query)
779 'SELECT DISTINCT(cp_title) AS title
781 WHERE cp_title IS NOT NULL AND cp_title != \'\'
783 $self->{all_titles} = [
786 selectall_hashref_query($::form, $dbh, $query)
790 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
792 $self->{all_languages} = SL::DB::Manager::Language->get_all();
794 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
796 if ( $::instance_conf->get_vertreter() ) {
804 selectall_hashref_query($::form, $dbh, $query)
808 if ( $business_ids->[0] ) {
809 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
811 $self->{all_salesman_customers} = [];
814 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
817 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
819 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
822 'SELECT DISTINCT(cp_abteilung) AS department
824 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
825 ORDER BY cp_abteilung';
826 $self->{all_departments} = [
828 { $_->{department}; }
829 selectall_hashref_query($::form, $dbh, $query)
833 $self->{contacts} = $self->{cv}->contacts;
834 $self->{contacts} ||= [];
836 $self->{shiptos} = $self->{cv}->shipto;
837 $self->{shiptos} ||= [];
839 $self->{template_args} ||= {};
841 $::request->{layout}->add_javascripts('autocomplete_customer.js');
842 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');