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}}
88 my $db = $self->{cv}->db;
90 $db->do_transaction(sub {
92 if ( $self->is_vendor() ) {
93 if ( $self->{cv}->vendornumber ) {
94 $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);
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();
255 $db->do_transaction(sub {
256 $self->{cv}->delete();
258 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
259 SL::DB::History->new(
260 trans_id => $self->{cv}->id,
261 snumbers => $snumbers,
262 employee_id => SL::DB::Manager::Employee->current->id,
263 addition => 'DELETED',
265 }) || die($db->error);
267 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
268 $::form->redirect($msg);
274 sub action_delete_contact {
277 my $db = $self->{contact}->db;
279 if ( !$self->{contact}->cp_id ) {
280 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
283 $db->do_transaction(sub {
284 if ( $self->{contact}->used ) {
285 $self->{contact}->detach();
286 $self->{contact}->save();
287 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
289 $self->{contact}->delete();
290 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
292 }) || die($db->error);
294 $self->{contact} = SL::DB::Contact->new();
297 $self->action_edit();
300 sub action_delete_shipto {
303 my $db = $self->{shipto}->db;
305 if ( !$self->{shipto}->shipto_id ) {
306 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
309 $db->do_transaction(sub {
310 if ( $self->{shipto}->used ) {
311 $self->{shipto}->detach();
312 $self->{shipto}->save();
313 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
315 $self->{shipto}->delete();
316 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
318 }) || die($db->error);
320 $self->{shipto} = SL::DB::Shipto->new();
323 $self->action_edit();
330 my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer');
332 if ( $::form->{callback} ) {
333 $url .= '&callback='. $::from->escape($::form->{callback});
336 print $::form->redirect_header($url);
340 sub action_search_contact {
343 my $url = 'ct.pl?action=search_contact&db=customer';
345 if ( $::form->{callback} ) {
346 $url .= '&callback='. $::from->escape($::form->{callback});
349 print $::form->redirect_header($url);
353 sub action_get_delivery {
356 my $dbh = $::form->get_standard_dbh();
358 my ($arap, $db, $qty_sign);
359 if ( $self->is_vendor() ) {
362 $qty_sign = ' * -1 AS qty';
370 my $where = ' WHERE 1=1 ';
373 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
374 $where .= "AND ${arap}.shipto_id = ?";
375 push(@values, $::form->{shipto_id});
378 if ( $::form->{delivery_from} ) {
379 $where .= "AND ${arap}.transdate >= ?";
380 push(@values, conv_date($::form->{delivery_from}));
383 if ( $::form->{delivery_to} ) {
384 $where .= "AND ${arap}.transdate <= ?";
385 push(@values, conv_date($::form->{delivery_to}));
404 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
407 ON ${arap}.id = i.trans_id
413 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
416 ORDER BY ${arap}.transdate DESC LIMIT 15";
418 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
420 $self->render('customer_vendor/get_delivery', { layout => 0 });
423 sub action_ajaj_get_shipto {
429 my $name = 'shipto'. $_;
430 $name => $self->{shipto}->$name;
432 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
436 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
439 sub action_ajaj_get_contact {
447 my $name = 'cp_'. $_;
449 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
450 $name => $self->{contact}->$name->to_lxoffice;
453 $name => $self->{contact}->$name;
457 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
458 satphone satfax project street zipcode city privatphone privatemail birthday
463 $data->{contact_cvars} = {
466 if ( $_->config->type eq 'number' ) {
467 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
470 $_->config->name => $_->value;
475 @{$self->{contact}->cvars_by_config}
480 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
483 sub action_ajaj_customer_autocomplete {
484 my ($self, %params) = @_;
486 my $limit = $::form->{limit} || 20;
487 my $type = $::form->{type} || {};
488 my $query = { ilike => '%'. $::form->{term} .'%' };
493 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
496 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
497 my $value_col = $::form->{column} || 'name';
503 value => $_->can($value_col)->($_),
504 label => $_->displayable_name,
506 customernumber => $_->customernumber,
514 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
518 return $::form->{db} eq 'vendor';
522 return $::form->{db} eq 'customer';
528 if ( defined($self->{_is_orphaned}) ) {
529 return $self->{_is_orphaned};
532 my $arap = $self->is_vendor ? 'ap' : 'ar';
535 my $cv = $self->is_vendor ? 'vendor' : 'customer';
539 FROM '. $arap .' AS a
540 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
547 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
551 if ( $self->is_vendor ) {
554 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
558 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
560 return $self->{_is_orphaned} = !$dummy;
563 sub _instantiate_args {
566 my $curr_employee = SL::DB::Manager::Employee->current;
568 if ( $::form->{cv}->{id} ) {
569 if ( $self->is_vendor() ) {
570 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
573 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
577 if ( $self->is_vendor() ) {
578 $self->{cv} = SL::DB::Vendor->new();
581 $self->{cv} = SL::DB::Customer->new();
584 $self->{cv}->assign_attributes(%{$::form->{cv}});
586 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
587 my $value = $::form->{cv_cvars}->{$cvar->config->name};
589 if ( $cvar->config->type eq 'number' ) {
590 $value = $::form->parse_amount(\%::myconfig, $value);
593 $cvar->value($value);
596 # foreach my $cvar_key (keys(%{$::form->{cv_cvars}})) {
597 # my $cvar_value = $::form->{cv_cvars}->{$cvar_key};
598 # my $cvar = $self->{cv}->cvar_by_name($cvar_key);
599 # $cvar->value($cvar_value);
602 if ( $::form->{note}->{id} ) {
603 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
606 $self->{note} = SL::DB::Note->new();
608 $self->{note}->assign_attributes(%{$::form->{note}});
609 $self->{note}->created_by($curr_employee->id);
610 $self->{note}->trans_module('ct');
611 # if ( $self->{note}->trans_id != $self->{cv}->id ) {
612 # die($::locale->text('Error'));
616 $self->{note_followup} = SL::DB::FollowUp->new();
617 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
618 $self->{note_followup}->note($self->{note});
619 $self->{note_followup}->created_by($curr_employee->id);
621 if ( $::form->{shipto}->{shipto_id} ) {
622 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
625 $self->{shipto} = SL::DB::Shipto->new();
627 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
628 $self->{shipto}->module('CT');
629 # if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
630 # die($::locale->text('Error'));
633 if ( $::form->{contact}->{cp_id} ) {
634 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
637 $self->{contact} = SL::DB::Contact->new();
639 $self->{contact}->assign_attributes(%{$::form->{contact}});
640 # if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
641 # die($::locale->text('Error'));
644 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
645 my $value = $::form->{contact_cvars}->{$cvar->config->name};
647 if ( $cvar->config->type eq 'number' ) {
648 $value = $::form->parse_amount(\%::myconfig, $value);
651 $cvar->value($value);
655 sub _load_customer_vendor {
658 if ( $self->is_vendor() ) {
659 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
662 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
665 $self->{note} = SL::DB::Note->new();
666 $self->{note_followup} = SL::DB::FollowUp->new();
668 if ( $::form->{shipto_id} ) {
669 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
671 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
672 die($::locale->text('Error'));
676 $self->{shipto} = SL::DB::Shipto->new();
679 if ( $::form->{contact_id} ) {
680 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
682 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
683 die($::locale->text('Error'));
687 $self->{contact} = SL::DB::Contact->new();
691 sub _create_customer_vendor {
694 if ( $self->is_vendor() ) {
695 $self->{cv} = SL::DB::Vendor->new();
698 $self->{cv} = SL::DB::Customer->new();
701 $self->{note} = SL::DB::Note->new();
703 $self->{note_followup} = SL::DB::FollowUp->new();
705 $self->{shipto} = SL::DB::Shipto->new();
707 $self->{contact} = SL::DB::Contact->new();
713 my $dbh = $::form->get_standard_dbh();
717 $self->{all_business} = SL::DB::Manager::Business->get_all();
719 $self->{all_employees} = SL::DB::Manager::Employee->get_all();
722 'SELECT DISTINCT(greeting)
724 WHERE greeting IS NOT NULL AND greeting != \'\'
726 SELECT DISTINCT(greeting)
728 WHERE greeting IS NOT NULL AND greeting != \'\'
730 $self->{all_greetings} = [
733 selectall_hashref_query($::form, $dbh, $query)
738 'SELECT DISTINCT(cp_title) AS title
740 WHERE cp_title IS NOT NULL AND cp_title != \'\'
742 $self->{all_titles} = [
745 selectall_hashref_query($::form, $dbh, $query)
749 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
751 $self->{all_languages} = SL::DB::Manager::Language->get_all();
753 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
757 #TODO: ALL_SALESMAN_CUSTOMERS
759 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
761 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
764 'SELECT DISTINCT(cp_abteilung) AS department
766 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
767 ORDER BY cp_abteilung';
768 $self->{all_departments} = [
770 { $_->{department}; }
771 selectall_hashref_query($::form, $dbh, $query)
775 $self->{contacts} = $self->{cv}->contacts;
776 $self->{contacts} ||= [];
778 $self->{shiptos} = $self->{cv}->shipto;
779 $self->{shiptos} ||= [];
781 $self->{template_args} = {};
783 $::request->{layout}->add_javascripts('autocomplete_customer.js');