1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
17 use SL::DB::PaymentTerm;
18 use SL::DB::Pricegroup;
24 __PACKAGE__->run_before(
26 $::auth->assert('customer_vendor_edit');
30 __PACKAGE__->run_before(
34 'save_and_ap_transaction',
35 'save_and_ar_transaction',
46 __PACKAGE__->run_before(
47 '_load_customer_vendor',
55 __PACKAGE__->run_before(
56 '_create_customer_vendor',
67 'customer_vendor/form',
68 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
69 %{$self->{template_args}}
78 'customer_vendor/form',
79 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
80 %{$self->{template_args}}
88 if ( $self->is_vendor() ) {
89 if ( $self->{cv}->vendornumber ) {
90 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
94 if ( $self->{cv}->customernumber ) {
95 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
99 foreach my $entry (@{$cvs_by_nr}) {
100 if( $entry->id != $self->{cv}->id ) {
102 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
104 $::form->error($msg);
108 $self->{cv}->save(cascade => 1);
110 $self->{contact}->cp_cv_id($self->{cv}->id);
111 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
112 $self->{contact}->save();
115 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
116 $self->{note}->trans_id($self->{cv}->id);
117 $self->{note}->save();
118 $self->{note_followup}->save();
120 $self->{note} = SL::DB::Note->new();
121 $self->{note_followup} = SL::DB::FollowUp->new();
124 $self->{shipto}->trans_id($self->{cv}->id);
125 if( $self->{shipto}->shiptoname ne '' ) {
126 $self->{shipto}->save();
129 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
130 SL::DB::History->new(
131 trans_id => $self->{cv}->id,
132 snumbers => $snumbers,
133 employee_id => SL::DB::Manager::Employee->current->id,
143 $self->action_edit();
146 sub action_save_and_close {
151 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
152 $::form->redirect($msg);
156 my ($self, $script) = @_;
158 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
159 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
163 my $callback = $::form->escape($::form->{callback}, 1);
164 my $name = $::form->escape($self->{cv}->name, 1);
165 my $db = $self->is_vendor() ? 'vendor' : 'customer';
171 $db .'_id=' . $self->{cv}->id .'&'.
172 $db .'='. $name .'&'.
173 'type='. $::form->{type} .'&'.
174 'callback='. $callback;
176 print $::form->redirect_header($url);
179 sub action_save_and_ar_transaction {
182 $main::auth->assert('general_ledger');
184 $self->_transaction('ar.pl');
187 sub action_save_and_ap_transaction {
190 $main::auth->assert('general_ledger');
192 $self->_transaction('ap.pl');
195 sub action_save_and_invoice {
198 if ( $self->is_vendor() ) {
199 $::auth->assert('vendor_invoice_edit');
201 $::auth->assert('invoice_edit');
204 $::form->{type} = 'invoice';
205 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
208 sub action_save_and_order {
211 if ( $self->is_vendor() ) {
212 $::auth->assert('purchase_order_edit');
214 $::auth->assert('sales_order_edit');
217 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
218 $self->_transaction('oe.pl');
221 sub action_save_and_rfq {
224 $::auth->assert('request_quotation_edit');
226 $::form->{type} = 'request_quotation';
227 $self->_transaction('oe.pl');
230 sub action_save_and_quotation {
233 $::auth->assert('sales_quotation_edit');
235 $::form->{type} = 'sales_quotation';
236 $self->_transaction('oe.pl');
242 if( !$self->is_orphaned() ) {
243 SL::Helper::Flash::flash('error', $::locale->text('blaabla'));
245 $self->action_edit();
248 $self->{cv}->delete();
250 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
251 SL::DB::History->new(
252 trans_id => $self->{cv}->id,
253 snumbers => $snumbers,
254 employee_id => SL::DB::Manager::Employee->current->id,
255 addition => 'DELETED',
258 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
259 $::form->redirect($msg);
265 sub action_delete_contact {
268 if ( !$self->{contact}->cp_id ) {
269 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
271 if ( $self->{contact}->used ) {
272 $self->{contact}->detach();
273 $self->{contact}->save();
274 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
276 $self->{contact}->delete();
277 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
280 $self->{contact} = SL::DB::Contact->new();
283 $self->action_edit();
286 sub action_delete_shipto {
289 if ( !$self->{shipto}->shipto_id ) {
290 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
292 if ( $self->{shipto}->used ) {
293 $self->{shipto}->detach();
294 $self->{shipto}->save();
295 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
297 $self->{shipto}->delete();
298 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
301 $self->{shipto} = SL::DB::Shipto->new();
304 $self->action_edit();
311 my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer');
313 if ( $::form->{callback} ) {
314 $url .= '&callback='. $::from->escape($::form->{callback});
317 print $::form->redirect_header($url);
321 sub action_search_contact {
324 my $url = 'ct.pl?action=search_contact&db=customer';
326 if ( $::form->{callback} ) {
327 $url .= '&callback='. $::from->escape($::form->{callback});
330 print $::form->redirect_header($url);
334 sub action_get_delivery {
337 my $dbh = $::form->get_standard_dbh();
339 my ($arap, $db, $qty_sign);
340 if ( $self->is_vendor() ) {
343 $qty_sign = ' * -1 AS qty';
351 my $where = ' WHERE 1=1 ';
354 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
355 $where .= "AND ${arap}.shipto_id = ?";
356 push(@values, $::form->{shipto_id});
359 if ( $::form->{delivery_from} ) {
360 $where .= "AND ${arap}.transdate >= ?";
361 push(@values, conv_date($::form->{delivery_from}));
364 if ( $::form->{delivery_to} ) {
365 $where .= "AND ${arap}.transdate <= ?";
366 push(@values, conv_date($::form->{delivery_to}));
385 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
388 ON ${arap}.id = i.trans_id
394 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
397 ORDER BY ${arap}.transdate DESC LIMIT 15";
399 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
401 $self->render('customer_vendor/get_delivery', { layout => 0 });
404 sub action_ajaj_get_shipto {
410 my $name = 'shipto'. $_;
411 $name => $self->{shipto}->$name;
413 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
417 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
420 sub action_ajaj_get_contact {
428 my $name = 'cp_'. $_;
430 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
431 $name => $self->{contact}->$name->to_lxoffice;
434 $name => $self->{contact}->$name;
438 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
439 satphone satfax project street zipcode city privatphone privatemail birthday
444 $data->{contact_cvars} = {
447 if ( $_->config->type eq 'number' ) {
448 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
451 $_->config->name => $_->value;
456 @{$self->{contact}->cvars_by_config}
461 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
464 sub action_ajaj_customer_autocomplete {
465 my ($self, %params) = @_;
467 my $limit = $::form->{limit} || 20;
468 my $type = $::form->{type} || {};
469 my $query = { ilike => '%'. $::form->{term} .'%' };
474 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
477 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
478 my $value_col = $::form->{column} || 'name';
484 value => $_->can($value_col)->($_),
485 label => $_->displayable_name,
487 customernumber => $_->customernumber,
495 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
499 return $::form->{db} eq 'vendor';
503 return $::form->{db} eq 'customer';
509 if ( defined($self->{_is_orphaned}) ) {
510 return $self->{_is_orphaned};
513 my $arap = $self->is_vendor ? 'ap' : 'ar';
516 my $cv = $self->is_vendor ? 'vendor' : 'customer';
520 FROM '. $arap .' AS a
521 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
528 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
532 if ( $self->is_vendor ) {
535 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
539 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
541 return $self->{_is_orphaned} = !$dummy;
544 sub _instantiate_args {
547 my $curr_employee = SL::DB::Manager::Employee->current;
549 if ( $::form->{cv}->{id} ) {
550 if ( $self->is_vendor() ) {
551 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
554 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
558 if ( $self->is_vendor() ) {
559 $self->{cv} = SL::DB::Vendor->new();
562 $self->{cv} = SL::DB::Customer->new();
565 $self->{cv}->assign_attributes(%{$::form->{cv}});
567 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
568 my $value = $::form->{cv_cvars}->{$cvar->config->name};
570 if ( $cvar->config->type eq 'number' ) {
571 $value = $::form->parse_amount(\%::myconfig, $value);
574 $cvar->value($value);
577 # foreach my $cvar_key (keys(%{$::form->{cv_cvars}})) {
578 # my $cvar_value = $::form->{cv_cvars}->{$cvar_key};
579 # my $cvar = $self->{cv}->cvar_by_name($cvar_key);
580 # $cvar->value($cvar_value);
583 if ( $::form->{note}->{id} ) {
584 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
587 $self->{note} = SL::DB::Note->new();
589 $self->{note}->assign_attributes(%{$::form->{note}});
590 $self->{note}->created_by($curr_employee->id);
591 $self->{note}->trans_module('ct');
592 # if ( $self->{note}->trans_id != $self->{cv}->id ) {
593 # die($::locale->text('Error'));
597 $self->{note_followup} = SL::DB::FollowUp->new();
598 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
599 $self->{note_followup}->note($self->{note});
600 $self->{note_followup}->created_by($curr_employee->id);
602 if ( $::form->{shipto}->{shipto_id} ) {
603 $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');
610 # if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
611 # die($::locale->text('Error'));
614 if ( $::form->{contact}->{cp_id} ) {
615 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
618 $self->{contact} = SL::DB::Contact->new();
620 $self->{contact}->assign_attributes(%{$::form->{contact}});
621 # if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
622 # die($::locale->text('Error'));
625 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
626 my $value = $::form->{contact_cvars}->{$cvar->config->name};
628 if ( $cvar->config->type eq 'number' ) {
629 $value = $::form->parse_amount(\%::myconfig, $value);
632 $cvar->value($value);
636 sub _load_customer_vendor {
639 if ( $self->is_vendor() ) {
640 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
643 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
646 $self->{note} = SL::DB::Note->new();
647 $self->{note_followup} = SL::DB::FollowUp->new();
649 if ( $::form->{shipto_id} ) {
650 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
652 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
653 die($::locale->text('Error'));
657 $self->{shipto} = SL::DB::Shipto->new();
660 if ( $::form->{contact_id} ) {
661 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
663 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
664 die($::locale->text('Error'));
668 $self->{contact} = SL::DB::Contact->new();
672 sub _create_customer_vendor {
675 if ( $self->is_vendor() ) {
676 $self->{cv} = SL::DB::Vendor->new();
679 $self->{cv} = SL::DB::Customer->new();
682 $self->{note} = SL::DB::Note->new();
684 $self->{note_followup} = SL::DB::FollowUp->new();
686 $self->{shipto} = SL::DB::Shipto->new();
688 $self->{contact} = SL::DB::Contact->new();
694 my $dbh = $::form->get_standard_dbh();
698 $self->{all_business} = SL::DB::Manager::Business->get_all();
700 $self->{all_employees} = SL::DB::Manager::Employee->get_all();
703 'SELECT DISTINCT(greeting)
705 WHERE greeting IS NOT NULL AND greeting != \'\'
707 SELECT DISTINCT(greeting)
709 WHERE greeting IS NOT NULL AND greeting != \'\'
711 $self->{all_greetings} = [
714 selectall_hashref_query($::form, $dbh, $query)
719 'SELECT DISTINCT(cp_title) AS title
721 WHERE cp_title IS NOT NULL AND cp_title != \'\'
723 $self->{all_titles} = [
726 selectall_hashref_query($::form, $dbh, $query)
733 my $curr = selectall_hashref_query($::form, $dbh, $query)->[0]->{curr};
734 my @currencies = grep(
741 $self->{all_currencies} = \@currencies;
743 $self->{all_languages} = SL::DB::Manager::Language->get_all();
745 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
749 #TODO: ALL_SALESMAN_CUSTOMERS
751 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
753 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
756 'SELECT DISTINCT(cp_abteilung) AS department
758 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
759 ORDER BY cp_abteilung';
760 $self->{all_departments} = [
762 { $_->{department}; }
763 selectall_hashref_query($::form, $dbh, $query)
767 $self->{contacts} = $self->{cv}->contacts;
768 $self->{contacts} ||= [];
770 $self->{shiptos} = $self->{cv}->shipto;
771 $self->{shiptos} ||= [];
773 $self->{template_args} = {};
775 $::request->{layout}->add_javascripts('autocomplete_customer.js');