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->redirect_to(action => 'edit', id => $self->{cv}->id);
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 $self->action_edit();
247 $self->{cv}->delete();
249 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
250 SL::DB::History->new(
251 trans_id => $self->{cv}->id,
252 snumbers => $snumbers,
253 employee_id => SL::DB::Manager::Employee->current->id,
254 addition => 'DELETED',
257 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
258 $::form->redirect($msg);
264 sub action_delete_contact {
267 if ( !$self->{contact}->cp_id ) {
268 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
270 if ( $self->{contact}->used ) {
271 $self->{contact}->detach();
272 $self->{contact}->save();
273 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
275 $self->{contact}->delete();
276 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
279 $self->{contact} = SL::DB::Contact->new();
282 $self->action_edit();
285 sub action_delete_shipto {
288 if ( !$self->{shipto}->shipto_id ) {
289 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
291 if ( $self->{shipto}->used ) {
292 $self->{shipto}->detach();
293 $self->{shipto}->save();
294 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
296 $self->{shipto}->delete();
297 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
300 $self->{shipto} = SL::DB::Shipto->new();
303 $self->action_edit();
310 my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer');
312 if ( $::form->{callback} ) {
313 $url .= '&callback='. $::from->escape($::form->{callback});
316 print $::form->redirect_header($url);
320 sub action_search_contact {
323 my $url = 'ct.pl?action=search_contact&db=customer';
325 if ( $::form->{callback} ) {
326 $url .= '&callback='. $::from->escape($::form->{callback});
329 print $::form->redirect_header($url);
333 sub action_get_delivery {
336 my $dbh = $::form->get_standard_dbh();
338 my ($arap, $db, $qty_sign);
339 if ( $self->is_vendor() ) {
342 $qty_sign = ' * -1 AS qty';
350 my $where = ' WHERE 1=1 ';
353 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
354 $where .= "AND ${arap}.shipto_id = ?";
355 push(@values, $::form->{shipto_id});
358 if ( $::form->{delivery_from} ) {
359 $where .= "AND ${arap}.transdate >= ?";
360 push(@values, conv_date($::form->{delivery_from}));
363 if ( $::form->{delivery_to} ) {
364 $where .= "AND ${arap}.transdate <= ?";
365 push(@values, conv_date($::form->{delivery_to}));
384 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
387 ON ${arap}.id = i.trans_id
393 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
396 ORDER BY ${arap}.transdate DESC LIMIT 15";
398 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
400 $self->render('customer_vendor/get_delivery', { layout => 0 });
403 sub action_ajaj_get_shipto {
409 my $name = 'shipto'. $_;
410 $name => $self->{shipto}->$name;
412 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
416 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
419 sub action_ajaj_get_contact {
427 my $name = 'cp_'. $_;
429 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
430 $name => $self->{contact}->$name->to_lxoffice;
433 $name => $self->{contact}->$name;
437 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
438 satphone satfax project street zipcode city privatphone privatemail birthday
443 $data->{contact_cvars} = {
446 if ( $_->config->type eq 'number' ) {
447 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
450 $_->config->name => $_->value;
455 @{$self->{contact}->cvars_by_config}
460 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
463 sub action_ajaj_customer_autocomplete {
464 my ($self, %params) = @_;
466 my $limit = $::form->{limit} || 20;
467 my $type = $::form->{type} || {};
468 my $query = { ilike => '%'. $::form->{term} .'%' };
473 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
476 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
477 my $value_col = $::form->{column} || 'name';
483 value => $_->can($value_col)->($_),
484 label => $_->displayable_name,
486 customernumber => $_->customernumber,
494 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
498 return $::form->{db} eq 'vendor';
502 return $::form->{db} eq 'customer';
508 if ( defined($self->{_is_orphaned}) ) {
509 return $self->{_is_orphaned};
512 my $arap = $self->is_vendor ? 'ap' : 'ar';
515 my $cv = $self->is_vendor ? 'vendor' : 'customer';
519 FROM '. $arap .' AS a
520 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
527 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
531 if ( $self->is_vendor ) {
534 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
538 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
540 return $self->{_is_orphaned} = !$dummy;
543 sub _instantiate_args {
546 my $curr_employee = SL::DB::Manager::Employee->current;
548 if ( $::form->{cv}->{id} ) {
549 if ( $self->is_vendor() ) {
550 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
553 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
557 if ( $self->is_vendor() ) {
558 $self->{cv} = SL::DB::Vendor->new();
561 $self->{cv} = SL::DB::Customer->new();
564 $self->{cv}->assign_attributes(%{$::form->{cv}});
566 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
567 my $value = $::form->{cv_cvars}->{$cvar->config->name};
569 if ( $cvar->config->type eq 'number' ) {
570 $value = $::form->parse_amount(\%::myconfig, $value);
573 $cvar->value($value);
576 # foreach my $cvar_key (keys(%{$::form->{cv_cvars}})) {
577 # my $cvar_value = $::form->{cv_cvars}->{$cvar_key};
578 # my $cvar = $self->{cv}->cvar_by_name($cvar_key);
579 # $cvar->value($cvar_value);
582 if ( $::form->{note}->{id} ) {
583 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
586 $self->{note} = SL::DB::Note->new();
588 $self->{note}->assign_attributes(%{$::form->{note}});
589 $self->{note}->created_by($curr_employee->id);
590 $self->{note}->trans_module('ct');
591 # if ( $self->{note}->trans_id != $self->{cv}->id ) {
592 # die($::locale->text('Error'));
596 $self->{note_followup} = SL::DB::FollowUp->new();
597 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
598 $self->{note_followup}->note($self->{note});
599 $self->{note_followup}->created_by($curr_employee->id);
601 if ( $::form->{shipto}->{shipto_id} ) {
602 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
605 $self->{shipto} = SL::DB::Shipto->new();
607 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
608 $self->{shipto}->module('CT');
609 # if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
610 # die($::locale->text('Error'));
613 if ( $::form->{contact}->{cp_id} ) {
614 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
617 $self->{contact} = SL::DB::Contact->new();
619 $self->{contact}->assign_attributes(%{$::form->{contact}});
620 # if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
621 # die($::locale->text('Error'));
624 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
625 my $value = $::form->{contact_cvars}->{$cvar->config->name};
627 if ( $cvar->config->type eq 'number' ) {
628 $value = $::form->parse_amount(\%::myconfig, $value);
631 $cvar->value($value);
635 sub _load_customer_vendor {
638 if ( $self->is_vendor() ) {
639 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
642 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
645 $self->{note} = SL::DB::Note->new();
646 $self->{note_followup} = SL::DB::FollowUp->new();
648 if ( $::form->{shipto_id} ) {
649 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
651 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
652 die($::locale->text('Error'));
656 $self->{shipto} = SL::DB::Shipto->new();
659 if ( $::form->{contact_id} ) {
660 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
662 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
663 die($::locale->text('Error'));
667 $self->{contact} = SL::DB::Contact->new();
671 sub _create_customer_vendor {
674 if ( $self->is_vendor() ) {
675 $self->{cv} = SL::DB::Vendor->new();
678 $self->{cv} = SL::DB::Customer->new();
681 $self->{note} = SL::DB::Note->new();
683 $self->{note_followup} = SL::DB::FollowUp->new();
685 $self->{shipto} = SL::DB::Shipto->new();
687 $self->{contact} = SL::DB::Contact->new();
693 my $dbh = $::form->get_standard_dbh();
697 $self->{all_business} = SL::DB::Manager::Business->get_all();
699 $self->{all_employees} = SL::DB::Manager::Employee->get_all();
702 'SELECT DISTINCT(greeting)
704 WHERE greeting IS NOT NULL AND greeting != \'\'
706 SELECT DISTINCT(greeting)
708 WHERE greeting IS NOT NULL AND greeting != \'\'
710 $self->{all_greetings} = [
713 selectall_hashref_query($::form, $dbh, $query)
718 'SELECT DISTINCT(cp_title) AS title
720 WHERE cp_title IS NOT NULL AND cp_title != \'\'
722 $self->{all_titles} = [
725 selectall_hashref_query($::form, $dbh, $query)
729 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
731 $self->{all_languages} = SL::DB::Manager::Language->get_all();
733 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
737 #TODO: ALL_SALESMAN_CUSTOMERS
739 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
741 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
744 'SELECT DISTINCT(cp_abteilung) AS department
746 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
747 ORDER BY cp_abteilung';
748 $self->{all_departments} = [
750 { $_->{department}; }
751 selectall_hashref_query($::form, $dbh, $query)
755 $self->{contacts} = $self->{cv}->contacts;
756 $self->{contacts} ||= [];
758 $self->{shiptos} = $self->{cv}->shipto;
759 $self->{shiptos} ||= [];
761 $self->{template_args} = {};
763 $::request->{layout}->add_javascripts('autocomplete_customer.js');