1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
17 use SL::DB::PaymentTerm;
18 use SL::DB::Pricegroup;
25 __PACKAGE__->run_before(
27 $::auth->assert('customer_vendor_edit');
31 __PACKAGE__->run_before(
35 'save_and_ap_transaction',
36 'save_and_ar_transaction',
47 __PACKAGE__->run_before(
48 '_load_customer_vendor',
56 __PACKAGE__->run_before(
57 '_create_customer_vendor',
68 'customer_vendor/form',
69 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
70 %{$self->{template_args}}
79 'customer_vendor/form',
80 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
81 %{$self->{template_args}}
89 if ( $self->is_vendor() ) {
90 if ( $self->{cv}->vendornumber ) {
91 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
95 if ( $self->{cv}->customernumber ) {
96 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
100 foreach my $entry (@{$cvs_by_nr}) {
101 if( $entry->id != $self->{cv}->id ) {
103 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
105 $::form->error($msg);
109 $self->{cv}->save(cascade => 1);
111 $self->{contact}->cp_cv_id($self->{cv}->id);
112 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
113 $self->{contact}->save();
116 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
117 $self->{note}->trans_id($self->{cv}->id);
118 $self->{note}->save();
119 $self->{note_followup}->save();
121 $self->{note} = SL::DB::Note->new();
122 $self->{note_followup} = SL::DB::FollowUp->new();
125 $self->{shipto}->trans_id($self->{cv}->id);
126 if( $self->{shipto}->shiptoname ne '' ) {
127 $self->{shipto}->save();
130 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
131 SL::DB::History->new(
132 trans_id => $self->{cv}->id,
133 snumbers => $snumbers,
134 employee_id => SL::DB::Manager::Employee->current->id,
144 $self->action_edit();
147 sub action_save_and_close {
152 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
153 $::form->redirect($msg);
157 my ($self, $script) = @_;
159 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
160 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
164 my $callback = $::form->escape($::form->{callback}, 1);
165 my $name = $::form->escape($self->{cv}->name, 1);
166 my $db = $self->is_vendor() ? 'vendor' : 'customer';
172 $db .'_id=' . $self->{cv}->id .'&'.
173 $db .'='. $name .'&'.
174 'type='. $::form->{type} .'&'.
175 'callback='. $callback;
177 print $::form->redirect_header($url);
180 sub action_save_and_ar_transaction {
183 $main::auth->assert('general_ledger');
185 $self->_transaction('ar.pl');
188 sub action_save_and_ap_transaction {
191 $main::auth->assert('general_ledger');
193 $self->_transaction('ap.pl');
196 sub action_save_and_invoice {
199 if ( $self->is_vendor() ) {
200 $::auth->assert('vendor_invoice_edit');
202 $::auth->assert('invoice_edit');
205 $::form->{type} = 'invoice';
206 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
209 sub action_save_and_order {
212 if ( $self->is_vendor() ) {
213 $::auth->assert('purchase_order_edit');
215 $::auth->assert('sales_order_edit');
218 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
219 $self->_transaction('oe.pl');
222 sub action_save_and_rfq {
225 $::auth->assert('request_quotation_edit');
227 $::form->{type} = 'request_quotation';
228 $self->_transaction('oe.pl');
231 sub action_save_and_quotation {
234 $::auth->assert('sales_quotation_edit');
236 $::form->{type} = 'sales_quotation';
237 $self->_transaction('oe.pl');
243 if( !$self->is_orphaned() ) {
244 SL::Helper::Flash::flash('error', $::locale->text('blaabla'));
246 $self->action_edit();
249 $self->{cv}->delete();
251 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
252 SL::DB::History->new(
253 trans_id => $self->{cv}->id,
254 snumbers => $snumbers,
255 employee_id => SL::DB::Manager::Employee->current->id,
256 addition => 'DELETED',
259 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
260 $::form->redirect($msg);
266 sub action_delete_contact {
269 if ( !$self->{contact}->cp_id ) {
270 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
272 if ( $self->{contact}->used ) {
273 $self->{contact}->detach();
274 $self->{contact}->save();
275 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
277 $self->{contact}->delete();
278 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
281 $self->{contact} = SL::DB::Contact->new();
284 $self->action_edit();
287 sub action_delete_shipto {
290 if ( !$self->{shipto}->shipto_id ) {
291 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
293 if ( $self->{shipto}->used ) {
294 $self->{shipto}->detach();
295 $self->{shipto}->save();
296 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
298 $self->{shipto}->delete();
299 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
302 $self->{shipto} = SL::DB::Shipto->new();
305 $self->action_edit();
312 my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer');
314 if ( $::form->{callback} ) {
315 $url .= '&callback='. $::from->escape($::form->{callback});
318 print $::form->redirect_header($url);
322 sub action_search_contact {
325 my $url = 'ct.pl?action=search_contact&db=customer';
327 if ( $::form->{callback} ) {
328 $url .= '&callback='. $::from->escape($::form->{callback});
331 print $::form->redirect_header($url);
335 sub action_get_delivery {
338 my $dbh = $::form->get_standard_dbh();
340 my ($arap, $db, $qty_sign);
341 if ( $self->is_vendor() ) {
344 $qty_sign = ' * -1 AS qty';
352 my $where = ' WHERE 1=1 ';
355 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
356 $where .= "AND ${arap}.shipto_id = ?";
357 push(@values, $::form->{shipto_id});
360 if ( $::form->{delivery_from} ) {
361 $where .= "AND ${arap}.transdate >= ?";
362 push(@values, conv_date($::form->{delivery_from}));
365 if ( $::form->{delivery_to} ) {
366 $where .= "AND ${arap}.transdate <= ?";
367 push(@values, conv_date($::form->{delivery_to}));
386 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
389 ON ${arap}.id = i.trans_id
395 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
398 ORDER BY ${arap}.transdate DESC LIMIT 15";
400 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
402 $self->render('customer_vendor/get_delivery', { layout => 0 });
405 sub action_ajaj_get_shipto {
411 my $name = 'shipto'. $_;
412 $name => $self->{shipto}->$name;
414 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
418 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
421 sub action_ajaj_get_contact {
429 my $name = 'cp_'. $_;
431 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
432 $name => $self->{contact}->$name->to_lxoffice;
435 $name => $self->{contact}->$name;
439 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
440 satphone satfax project street zipcode city privatphone privatemail birthday
445 $data->{contact_cvars} = {
448 if ( $_->config->type eq 'number' ) {
449 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
452 $_->config->name => $_->value;
457 @{$self->{contact}->cvars_by_config}
462 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
465 sub action_ajaj_customer_autocomplete {
466 my ($self, %params) = @_;
468 my $limit = $::form->{limit} || 20;
469 my $type = $::form->{type} || {};
470 my $query = { ilike => '%'. $::form->{term} .'%' };
475 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
478 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
479 my $value_col = $::form->{column} || 'name';
485 value => $_->can($value_col)->($_),
486 label => $_->displayable_name,
488 customernumber => $_->customernumber,
496 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
500 return $::form->{db} eq 'vendor';
504 return $::form->{db} eq 'customer';
510 if ( defined($self->{_is_orphaned}) ) {
511 return $self->{_is_orphaned};
514 my $arap = $self->is_vendor ? 'ap' : 'ar';
517 my $cv = $self->is_vendor ? 'vendor' : 'customer';
521 FROM '. $arap .' AS a
522 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
529 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
533 if ( $self->is_vendor ) {
536 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
540 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
542 return $self->{_is_orphaned} = !$dummy;
545 sub _instantiate_args {
548 my $curr_employee = SL::DB::Manager::Employee->current;
550 if ( $::form->{cv}->{id} ) {
551 if ( $self->is_vendor() ) {
552 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
555 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
559 if ( $self->is_vendor() ) {
560 $self->{cv} = SL::DB::Vendor->new();
563 $self->{cv} = SL::DB::Customer->new();
566 $self->{cv}->assign_attributes(%{$::form->{cv}});
568 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
569 my $value = $::form->{cv_cvars}->{$cvar->config->name};
571 if ( $cvar->config->type eq 'number' ) {
572 $value = $::form->parse_amount(\%::myconfig, $value);
575 $cvar->value($value);
578 # foreach my $cvar_key (keys(%{$::form->{cv_cvars}})) {
579 # my $cvar_value = $::form->{cv_cvars}->{$cvar_key};
580 # my $cvar = $self->{cv}->cvar_by_name($cvar_key);
581 # $cvar->value($cvar_value);
584 if ( $::form->{note}->{id} ) {
585 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
588 $self->{note} = SL::DB::Note->new();
590 $self->{note}->assign_attributes(%{$::form->{note}});
591 $self->{note}->created_by($curr_employee->id);
592 $self->{note}->trans_module('ct');
593 # if ( $self->{note}->trans_id != $self->{cv}->id ) {
594 # die($::locale->text('Error'));
598 $self->{note_followup} = SL::DB::FollowUp->new();
599 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
600 $self->{note_followup}->note($self->{note});
601 $self->{note_followup}->created_by($curr_employee->id);
603 if ( $::form->{shipto}->{shipto_id} ) {
604 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
607 $self->{shipto} = SL::DB::Shipto->new();
609 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
610 $self->{shipto}->module('CT');
611 # if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
612 # die($::locale->text('Error'));
615 if ( $::form->{contact}->{cp_id} ) {
616 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
619 $self->{contact} = SL::DB::Contact->new();
621 $self->{contact}->assign_attributes(%{$::form->{contact}});
622 # if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
623 # die($::locale->text('Error'));
626 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
627 my $value = $::form->{contact_cvars}->{$cvar->config->name};
629 if ( $cvar->config->type eq 'number' ) {
630 $value = $::form->parse_amount(\%::myconfig, $value);
633 $cvar->value($value);
637 sub _load_customer_vendor {
640 if ( $self->is_vendor() ) {
641 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
644 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
647 $self->{note} = SL::DB::Note->new();
648 $self->{note_followup} = SL::DB::FollowUp->new();
650 if ( $::form->{shipto_id} ) {
651 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
653 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
654 die($::locale->text('Error'));
658 $self->{shipto} = SL::DB::Shipto->new();
661 if ( $::form->{contact_id} ) {
662 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
664 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
665 die($::locale->text('Error'));
669 $self->{contact} = SL::DB::Contact->new();
673 sub _create_customer_vendor {
676 if ( $self->is_vendor() ) {
677 $self->{cv} = SL::DB::Vendor->new();
680 $self->{cv} = SL::DB::Customer->new();
683 $self->{note} = SL::DB::Note->new();
685 $self->{note_followup} = SL::DB::FollowUp->new();
687 $self->{shipto} = SL::DB::Shipto->new();
689 $self->{contact} = SL::DB::Contact->new();
695 my $dbh = $::form->get_standard_dbh();
699 $self->{all_business} = SL::DB::Manager::Business->get_all();
701 $self->{all_employees} = SL::DB::Manager::Employee->get_all();
704 'SELECT DISTINCT(greeting)
706 WHERE greeting IS NOT NULL AND greeting != \'\'
708 SELECT DISTINCT(greeting)
710 WHERE greeting IS NOT NULL AND greeting != \'\'
712 $self->{all_greetings} = [
715 selectall_hashref_query($::form, $dbh, $query)
720 'SELECT DISTINCT(cp_title) AS title
722 WHERE cp_title IS NOT NULL AND cp_title != \'\'
724 $self->{all_titles} = [
727 selectall_hashref_query($::form, $dbh, $query)
731 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
733 $self->{all_languages} = SL::DB::Manager::Language->get_all();
735 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
739 #TODO: ALL_SALESMAN_CUSTOMERS
741 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
743 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
746 'SELECT DISTINCT(cp_abteilung) AS department
748 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
749 ORDER BY cp_abteilung';
750 $self->{all_departments} = [
752 { $_->{department}; }
753 selectall_hashref_query($::form, $dbh, $query)
757 $self->{contacts} = $self->{cv}->contacts;
758 $self->{contacts} ||= [];
760 $self->{shiptos} = $self->{cv}->shipto;
761 $self->{shiptos} ||= [];
763 $self->{template_args} = {};
765 $::request->{layout}->add_javascripts('autocomplete_customer.js');