1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
17 use SL::DB::PaymentTerm;
18 use SL::DB::Pricegroup;
23 __PACKAGE__->run_before(
25 $::auth->assert('customer_vendor_edit');
29 __PACKAGE__->run_before(
33 'save_and_ap_transaction',
34 'save_and_ar_transaction',
45 __PACKAGE__->run_before(
46 '_load_customer_vendor',
54 __PACKAGE__->run_before(
55 '_create_customer_vendor',
66 'customer_vendor/form',
67 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
68 %{$self->{template_args}}
77 'customer_vendor/form',
78 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
79 %{$self->{template_args}}
87 if ( $self->is_vendor() ) {
88 if ( $self->{cv}->vendornumber ) {
89 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
93 if ( $self->{cv}->customernumber ) {
94 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
98 foreach my $entry (@{$cvs_by_nr}) {
99 if( $entry->id != $self->{cv}->id ) {
101 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
103 $::form->error($msg);
107 $self->{cv}->save(cascade => 1);
109 $self->{contact}->cp_cv_id($self->{cv}->id);
110 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
111 $self->{contact}->save();
114 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
115 $self->{note}->trans_id($self->{cv}->id);
116 $self->{note}->save();
117 $self->{note_followup}->save();
119 $self->{note} = SL::DB::Note->new();
120 $self->{note_followup} = SL::DB::FollowUp->new();
123 $self->{shipto}->trans_id($self->{cv}->id);
124 if( $self->{shipto}->shiptoname ne '' ) {
125 $self->{shipto}->save();
136 $self->action_edit();
139 sub action_save_and_close {
144 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
145 $::form->redirect($msg);
149 my ($self, $script) = @_;
151 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
152 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
156 my $callback = $::form->escape($::form->{callback}, 1);
157 my $name = $::form->escape($self->{cv}->name, 1);
158 my $db = $self->is_vendor() ? 'vendor' : 'customer';
164 $db .'_id=' . $self->{cv}->id .'&'.
165 $db .'='. $name .'&'.
166 'type='. $::form->{type} .'&'.
167 'callback='. $callback;
169 print $::form->redirect_header($url);
172 sub action_save_and_ar_transaction {
175 $main::auth->assert('general_ledger');
177 $self->_transaction('ar.pl');
180 sub action_save_and_ap_transaction {
183 $main::auth->assert('general_ledger');
185 $self->_transaction('ap.pl');
188 sub action_save_and_invoice {
191 if ( $self->is_vendor() ) {
192 $::auth->assert('vendor_invoice_edit');
194 $::auth->assert('invoice_edit');
197 $::form->{type} = 'invoice';
198 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
201 sub action_save_and_order {
204 if ( $self->is_vendor() ) {
205 $::auth->assert('purchase_order_edit');
207 $::auth->assert('sales_order_edit');
210 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
211 $self->_transaction('oe.pl');
214 sub action_save_and_rfq {
217 $::auth->assert('request_quotation_edit');
219 $::form->{type} = 'request_quotation';
220 $self->_transaction('oe.pl');
223 sub action_save_and_quotation {
226 $::auth->assert('sales_quotation_edit');
228 $::form->{type} = 'sales_quotation';
229 $self->_transaction('oe.pl');
235 if( !$self->is_orphaned() ) {
236 SL::Helper::Flash::flash('error', $::locale->text('blaabla'));
238 $self->action_edit();
241 $self->{cv}->delete();
245 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
246 $::form->redirect($msg);
252 sub action_delete_contact {
255 if ( !$self->{contact}->cp_id ) {
256 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
258 if ( $self->{contact}->used ) {
259 $self->{contact}->detach();
260 $self->{contact}->save();
261 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
263 $self->{contact}->delete();
264 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
267 $self->{contact} = SL::DB::Contact->new();
270 $self->action_edit();
273 sub action_delete_shipto {
276 if ( !$self->{shipto}->shipto_id ) {
277 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
279 if ( $self->{shipto}->used ) {
280 $self->{shipto}->detach();
281 $self->{shipto}->save();
282 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
284 $self->{shipto}->delete();
285 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
288 $self->{shipto} = SL::DB::Shipto->new();
291 $self->action_edit();
294 sub action_get_delivery {
297 my $dbh = $::form->get_standard_dbh();
299 my ($arap, $db, $qty_sign);
300 if ( $self->is_vendor() ) {
303 $qty_sign = ' * -1 AS qty';
311 my $where = ' WHERE 1=1 ';
314 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
315 $where .= "AND ${arap}.shipto_id = ?";
316 push(@values, $::form->{shipto_id});
319 if ( $::form->{delivery_from} ) {
320 $where .= "AND ${arap}.transdate >= ?";
321 push(@values, conv_date($::form->{delivery_from}));
324 if ( $::form->{delivery_to} ) {
325 $where .= "AND ${arap}.transdate <= ?";
326 push(@values, conv_date($::form->{delivery_to}));
345 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
348 ON ${arap}.id = i.trans_id
354 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
357 ORDER BY ${arap}.transdate DESC LIMIT 15";
359 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
361 $self->render('customer_vendor/get_delivery', { layout => 0 });
364 sub action_ajaj_get_shipto {
370 my $name = 'shipto'. $_;
371 $name => $self->{shipto}->$name;
373 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
377 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
380 sub action_ajaj_get_contact {
388 my $name = 'cp_'. $_;
390 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
391 $name => $self->{contact}->$name->to_lxoffice;
394 $name => $self->{contact}->$name;
398 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
399 satphone satfax project street zipcode city privatphone privatemail birthday
404 $data->{contact_cvars} = {
407 if ( $_->config->type eq 'number' ) {
408 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
411 $_->config->name => $_->value;
416 @{$self->{contact}->cvars_by_config}
421 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
424 sub action_ajaj_customer_autocomplete {
425 my ($self, %params) = @_;
427 my $limit = $::form->{limit} || 20;
428 my $type = $::form->{type} || {};
429 my $query = { ilike => '%'. $::form->{term} .'%' };
434 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
437 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
438 my $value_col = $::form->{column} || 'name';
444 value => $_->can($value_col)->($_),
445 label => $_->displayable_name,
447 customernumber => $_->customernumber,
455 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
459 return $::form->{db} eq 'vendor';
463 return $::form->{db} eq 'customer';
469 if ( defined($self->{_is_orphaned}) ) {
470 return $self->{_is_orphaned};
473 my $arap = $self->is_vendor ? 'ap' : 'ar';
476 my $cv = $self->is_vendor ? 'vendor' : 'customer';
480 FROM '. $arap .' AS a
481 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
488 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
492 if ( $self->is_vendor ) {
495 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
499 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
501 return $self->{_is_orphaned} = !$dummy;
504 sub _instantiate_args {
507 my $curr_employee = SL::DB::Manager::Employee->current;
509 foreach ( 'cv.creditlimit', 'cv.discount' ) {
510 my ($namespace, $varname) = split('.', $_, 2);
511 $::form->{$namespace}->{$varname} = $::form->parse_amount(\%::myconfig, $::form->{$namespace}->{$varname});
513 $::form->{cv}->{discount} /= 100;
515 if ( $::form->{cv}->{id} ) {
516 if ( $self->is_vendor() ) {
517 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
520 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
524 if ( $self->is_vendor() ) {
525 $self->{cv} = SL::DB::Vendor->new();
528 $self->{cv} = SL::DB::Customer->new();
531 $self->{cv}->assign_attributes(%{$::form->{cv}});
533 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
534 my $value = $::form->{cv_cvars}->{$cvar->config->name};
536 if ( $cvar->config->type eq 'number' ) {
537 $value = $::form->parse_amount(\%::myconfig, $value);
540 $cvar->value($value);
543 # foreach my $cvar_key (keys(%{$::form->{cv_cvars}})) {
544 # my $cvar_value = $::form->{cv_cvars}->{$cvar_key};
545 # my $cvar = $self->{cv}->cvar_by_name($cvar_key);
546 # $cvar->value($cvar_value);
549 if ( $::form->{note}->{id} ) {
550 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
553 $self->{note} = SL::DB::Note->new();
555 $self->{note}->assign_attributes(%{$::form->{note}});
556 $self->{note}->created_by($curr_employee->id);
557 $self->{note}->trans_module('ct');
558 # if ( $self->{note}->trans_id != $self->{cv}->id ) {
559 # die($::locale->text('Error'));
563 $self->{note_followup} = SL::DB::FollowUp->new();
564 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
565 $self->{note_followup}->note($self->{note});
566 $self->{note_followup}->created_by($curr_employee->id);
568 if ( $::form->{shipto}->{shipto_id} ) {
569 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
572 $self->{shipto} = SL::DB::Shipto->new();
574 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
575 $self->{shipto}->module('CT');
576 # if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
577 # die($::locale->text('Error'));
580 if ( $::form->{contact}->{cp_id} ) {
581 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
584 $self->{contact} = SL::DB::Contact->new();
586 $self->{contact}->assign_attributes(%{$::form->{contact}});
587 # if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
588 # die($::locale->text('Error'));
591 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
592 my $value = $::form->{contact_cvars}->{$cvar->config->name};
594 if ( $cvar->config->type eq 'number' ) {
595 $value = $::form->parse_amount(\%::myconfig, $value);
598 $cvar->value($value);
602 sub _load_customer_vendor {
605 if ( $self->is_vendor() ) {
606 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
609 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
612 $self->{note} = SL::DB::Note->new();
613 $self->{note_followup} = SL::DB::FollowUp->new();
615 if ( $::form->{shipto_id} ) {
616 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
618 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
619 die($::locale->text('Error'));
623 $self->{shipto} = SL::DB::Shipto->new();
626 if ( $::form->{contact_id} ) {
627 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
629 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
630 die($::locale->text('Error'));
634 $self->{contact} = SL::DB::Contact->new();
638 sub _create_customer_vendor {
641 if ( $self->is_vendor() ) {
642 $self->{cv} = SL::DB::Vendor->new();
645 $self->{cv} = SL::DB::Customer->new();
648 $self->{note} = SL::DB::Note->new();
650 $self->{note_followup} = SL::DB::FollowUp->new();
652 $self->{shipto} = SL::DB::Shipto->new();
654 $self->{contact} = SL::DB::Contact->new();
660 my $dbh = $::form->get_standard_dbh();
664 $self->{all_business} = SL::DB::Manager::Business->get_all();
666 $self->{all_employees} = SL::DB::Manager::Employee->get_all();
669 'SELECT DISTINCT(greeting)
671 WHERE greeting IS NOT NULL AND greeting != \'\'
673 SELECT DISTINCT(greeting)
675 WHERE greeting IS NOT NULL AND greeting != \'\'
677 $self->{all_greetings} = [
680 selectall_hashref_query($::form, $dbh, $query)
685 'SELECT DISTINCT(cp_title) AS title
687 WHERE cp_title IS NOT NULL AND cp_title != \'\'
689 $self->{all_titles} = [
692 selectall_hashref_query($::form, $dbh, $query)
699 my $curr = selectall_hashref_query($::form, $dbh, $query)->[0]->{curr};
700 my @currencies = grep(
707 $self->{all_currencies} = \@currencies;
709 $self->{all_languages} = SL::DB::Manager::Language->get_all();
711 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
715 #TODO: ALL_SALESMAN_CUSTOMERS
717 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
719 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
722 'SELECT DISTINCT(cp_abteilung) AS department
724 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
725 ORDER BY cp_abteilung';
726 $self->{all_departments} = [
728 { $_->{department}; }
729 selectall_hashref_query($::form, $dbh, $query)
733 $self->{contacts} = $self->{cv}->contacts;
734 $self->{contacts} ||= [];
736 $self->{shiptos} = $self->{cv}->shipto;
737 $self->{shiptos} ||= [];
739 $self->{template_args} = {};
741 $self->{cv}->discount($self->{cv}->discount * 100);
744 $::request->{layout}->add_javascripts('autocomplete_customer.js');