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',
48 __PACKAGE__->run_before(
49 '_load_customer_vendor',
57 __PACKAGE__->run_before(
58 '_create_customer_vendor',
69 'customer_vendor/form',
70 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
71 %{$self->{template_args}}
80 'customer_vendor/form',
81 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
82 %{$self->{template_args}}
89 my $db = $self->{cv}->db;
91 $db->do_transaction(sub {
93 if ( $self->is_vendor() ) {
94 if ( $self->{cv}->vendornumber ) {
95 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
98 if ( $self->{cv}->customernumber ) {
99 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
103 foreach my $entry (@{$cvs_by_nr}) {
104 if( $entry->id != $self->{cv}->id ) {
106 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
108 $::form->error($msg);
112 $self->{cv}->save(cascade => 1);
114 $self->{contact}->cp_cv_id($self->{cv}->id);
115 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
116 $self->{contact}->save();
119 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
120 $self->{note}->trans_id($self->{cv}->id);
121 $self->{note}->save();
122 $self->{note_followup}->save();
124 $self->{note} = SL::DB::Note->new();
125 $self->{note_followup} = SL::DB::FollowUp->new();
128 $self->{shipto}->trans_id($self->{cv}->id);
129 if( $self->{shipto}->shiptoname ne '' ) {
130 $self->{shipto}->save();
133 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
134 SL::DB::History->new(
135 trans_id => $self->{cv}->id,
136 snumbers => $snumbers,
137 employee_id => SL::DB::Manager::Employee->current->id,
140 }) || die($db->error);
149 $self->redirect_to(action => 'edit', id => $self->{cv}->id, db => $self->is_vendor() ? 'vendor' : 'customer');
152 sub action_save_and_close {
157 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
158 $::form->redirect($msg);
162 my ($self, $script) = @_;
164 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
165 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
169 my $callback = $::form->escape($::form->{callback}, 1);
170 my $name = $::form->escape($self->{cv}->name, 1);
171 my $db = $self->is_vendor() ? 'vendor' : 'customer';
177 $db .'_id=' . $self->{cv}->id .'&'.
178 $db .'='. $name .'&'.
179 'type='. $::form->{type} .'&'.
180 'callback='. $callback;
182 print $::form->redirect_header($url);
185 sub action_save_and_ar_transaction {
188 $main::auth->assert('general_ledger');
190 $self->_transaction('ar.pl');
193 sub action_save_and_ap_transaction {
196 $main::auth->assert('general_ledger');
198 $self->_transaction('ap.pl');
201 sub action_save_and_invoice {
204 if ( $self->is_vendor() ) {
205 $::auth->assert('vendor_invoice_edit');
207 $::auth->assert('invoice_edit');
210 $::form->{type} = 'invoice';
211 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
214 sub action_save_and_order {
217 if ( $self->is_vendor() ) {
218 $::auth->assert('purchase_order_edit');
220 $::auth->assert('sales_order_edit');
223 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
224 $self->_transaction('oe.pl');
227 sub action_save_and_rfq {
230 $::auth->assert('request_quotation_edit');
232 $::form->{type} = 'request_quotation';
233 $self->_transaction('oe.pl');
236 sub action_save_and_quotation {
239 $::auth->assert('sales_quotation_edit');
241 $::form->{type} = 'sales_quotation';
242 $self->_transaction('oe.pl');
248 my $db = $self->{cv}->db;
250 if( !$self->is_orphaned() ) {
251 $self->action_edit();
254 $db->do_transaction(sub {
255 $self->{cv}->delete(cascade => 1);
257 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
258 SL::DB::History->new(
259 trans_id => $self->{cv}->id,
260 snumbers => $snumbers,
261 employee_id => SL::DB::Manager::Employee->current->id,
262 addition => 'DELETED',
264 }) || die($db->error);
266 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
267 $::form->redirect($msg);
273 sub action_delete_contact {
276 my $db = $self->{contact}->db;
278 if ( !$self->{contact}->cp_id ) {
279 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
282 $db->do_transaction(sub {
283 if ( $self->{contact}->used ) {
284 $self->{contact}->detach();
285 $self->{contact}->save();
286 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
288 $self->{contact}->delete(cascade => 1);
289 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
291 }) || die($db->error);
293 $self->{contact} = SL::DB::Contact->new();
296 $self->action_edit();
299 sub action_delete_shipto {
302 my $db = $self->{shipto}->db;
304 if ( !$self->{shipto}->shipto_id ) {
305 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
308 $db->do_transaction(sub {
309 if ( $self->{shipto}->used ) {
310 $self->{shipto}->detach();
311 $self->{shipto}->save(cascade => 1);
312 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
314 $self->{shipto}->delete(cascade => 1);
315 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
317 }) || die($db->error);
319 $self->{shipto} = SL::DB::Shipto->new();
322 $self->action_edit();
329 my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer');
331 if ( $::form->{callback} ) {
332 $url .= '&callback='. $::from->escape($::form->{callback});
335 print $::form->redirect_header($url);
339 sub action_search_contact {
342 my $url = 'ct.pl?action=search_contact&db=customer';
344 if ( $::form->{callback} ) {
345 $url .= '&callback='. $::from->escape($::form->{callback});
348 print $::form->redirect_header($url);
352 sub action_get_delivery {
355 my $dbh = $::form->get_standard_dbh();
357 my ($arap, $db, $qty_sign);
358 if ( $self->is_vendor() ) {
361 $qty_sign = ' * -1 AS qty';
368 my $where = ' WHERE 1=1 ';
371 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
372 $where .= "AND ${arap}.shipto_id = ?";
373 push(@values, $::form->{shipto_id});
376 if ( $::form->{delivery_from} ) {
377 $where .= "AND ${arap}.transdate >= ?";
378 push(@values, conv_date($::form->{delivery_from}));
381 if ( $::form->{delivery_to} ) {
382 $where .= "AND ${arap}.transdate <= ?";
383 push(@values, conv_date($::form->{delivery_to}));
402 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
405 ON ${arap}.id = i.trans_id
411 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
414 ORDER BY ${arap}.transdate DESC LIMIT 15";
416 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
418 $self->render('customer_vendor/get_delivery', { layout => 0 });
421 sub action_ajaj_get_shipto {
427 my $name = 'shipto'. $_;
428 $name => $self->{shipto}->$name;
430 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
434 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
437 sub action_ajaj_get_contact {
445 my $name = 'cp_'. $_;
447 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
448 $name => $self->{contact}->$name->to_lxoffice;
450 $name => $self->{contact}->$name;
454 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
455 satphone satfax project street zipcode city privatphone privatemail birthday
460 $data->{contact_cvars} = {
463 if ( $_->config->type eq 'number' ) {
464 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
466 $_->config->name => $_->value;
471 @{$self->{contact}->cvars_by_config}
476 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
479 sub action_ajaj_customer_autocomplete {
480 my ($self, %params) = @_;
482 my $limit = $::form->{limit} || 20;
483 my $type = $::form->{type} || {};
484 my $query = { ilike => '%'. $::form->{term} .'%' };
489 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
492 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
493 my $value_col = $::form->{column} || 'name';
499 value => $_->can($value_col)->($_),
500 label => $_->displayable_name,
502 customernumber => $_->customernumber,
510 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
514 return $::form->{db} eq 'vendor';
518 return $::form->{db} eq 'customer';
524 if ( defined($self->{_is_orphaned}) ) {
525 return $self->{_is_orphaned};
528 my $arap = $self->is_vendor ? 'ap' : 'ar';
531 my $cv = $self->is_vendor ? 'vendor' : 'customer';
535 FROM '. $arap .' AS a
536 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
543 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
547 if ( $self->is_vendor ) {
550 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
554 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
556 return $self->{_is_orphaned} = !$dummy;
559 sub _instantiate_args {
562 my $curr_employee = SL::DB::Manager::Employee->current;
564 if ( $::form->{cv}->{id} ) {
565 if ( $self->is_vendor() ) {
566 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
568 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
571 if ( $self->is_vendor() ) {
572 $self->{cv} = SL::DB::Vendor->new();
574 $self->{cv} = SL::DB::Customer->new();
577 $self->{cv}->assign_attributes(%{$::form->{cv}});
579 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
580 my $value = $::form->{cv_cvars}->{$cvar->config->name};
582 if ( $cvar->config->type eq 'number' ) {
583 $value = $::form->parse_amount(\%::myconfig, $value);
586 $cvar->value($value);
589 if ( $::form->{note}->{id} ) {
590 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
592 $self->{note} = SL::DB::Note->new();
594 $self->{note}->assign_attributes(%{$::form->{note}});
595 $self->{note}->created_by($curr_employee->id);
596 $self->{note}->trans_module('ct');
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();
606 $self->{shipto} = SL::DB::Shipto->new();
608 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
609 $self->{shipto}->module('CT');
611 if ( $::form->{contact}->{cp_id} ) {
612 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
614 $self->{contact} = SL::DB::Contact->new();
616 $self->{contact}->assign_attributes(%{$::form->{contact}});
618 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
619 my $value = $::form->{contact_cvars}->{$cvar->config->name};
621 if ( $cvar->config->type eq 'number' ) {
622 $value = $::form->parse_amount(\%::myconfig, $value);
625 $cvar->value($value);
629 sub _load_customer_vendor {
632 if ( $self->is_vendor() ) {
633 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
635 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
638 $self->{note} = SL::DB::Note->new();
639 $self->{note_followup} = SL::DB::FollowUp->new();
641 if ( $::form->{shipto_id} ) {
642 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
644 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
645 die($::locale->text('Error'));
648 $self->{shipto} = SL::DB::Shipto->new();
651 if ( $::form->{contact_id} ) {
652 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
654 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
655 die($::locale->text('Error'));
658 $self->{contact} = SL::DB::Contact->new();
662 sub _create_customer_vendor {
665 if ( $self->is_vendor() ) {
666 $self->{cv} = SL::DB::Vendor->new();
668 $self->{cv} = SL::DB::Customer->new();
671 $self->{note} = SL::DB::Note->new();
673 $self->{note_followup} = SL::DB::FollowUp->new();
675 $self->{shipto} = SL::DB::Shipto->new();
677 $self->{contact} = SL::DB::Contact->new();
683 my $dbh = $::form->get_standard_dbh();
687 $self->{all_business} = SL::DB::Manager::Business->get_all();
689 $self->{all_employees} = SL::DB::Manager::Employee->get_all();
692 'SELECT DISTINCT(greeting)
694 WHERE greeting IS NOT NULL AND greeting != \'\'
696 SELECT DISTINCT(greeting)
698 WHERE greeting IS NOT NULL AND greeting != \'\'
700 $self->{all_greetings} = [
703 selectall_hashref_query($::form, $dbh, $query)
708 'SELECT DISTINCT(cp_title) AS title
710 WHERE cp_title IS NOT NULL AND cp_title != \'\'
712 $self->{all_titles} = [
715 selectall_hashref_query($::form, $dbh, $query)
719 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
721 $self->{all_languages} = SL::DB::Manager::Language->get_all();
723 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
727 #TODO: ALL_SALESMAN_CUSTOMERS
729 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
731 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
734 'SELECT DISTINCT(cp_abteilung) AS department
736 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
737 ORDER BY cp_abteilung';
738 $self->{all_departments} = [
740 { $_->{department}; }
741 selectall_hashref_query($::form, $dbh, $query)
745 $self->{contacts} = $self->{cv}->contacts;
746 $self->{contacts} ||= [];
748 $self->{shiptos} = $self->{cv}->shipto;
749 $self->{shiptos} ||= [];
751 $self->{template_args} = {};
753 $::request->{layout}->add_javascripts('autocomplete_customer.js');