1 package SL::Controller::CustomerVendor;
4 use parent qw(SL::Controller::Base);
17 use SL::DB::PaymentTerm;
18 use SL::DB::Pricegroup;
21 use SL::DB::FollowUpLink;
26 __PACKAGE__->run_before(
28 $::auth->assert('customer_vendor_edit');
32 __PACKAGE__->run_before(
36 'save_and_ap_transaction',
37 'save_and_ar_transaction',
49 __PACKAGE__->run_before(
50 '_load_customer_vendor',
58 __PACKAGE__->run_before(
59 '_create_customer_vendor',
70 'customer_vendor/form',
71 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
72 %{$self->{template_args}}
81 'customer_vendor/form',
82 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
83 %{$self->{template_args}}
90 my $db = $self->{cv}->db;
92 $db->do_transaction(sub {
94 if ( $self->is_vendor() ) {
95 if ( $self->{cv}->vendornumber ) {
96 $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
99 if ( $self->{cv}->customernumber ) {
100 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
104 foreach my $entry (@{$cvs_by_nr}) {
105 if( $entry->id != $self->{cv}->id ) {
107 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
109 $::form->error($msg);
113 $self->{cv}->save(cascade => 1);
115 $self->{contact}->cp_cv_id($self->{cv}->id);
116 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
117 $self->{contact}->save();
120 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
122 if ( !$self->{note_followup}->follow_up_date ) {
123 $::form->error($::locale->text('Date missing!'));
126 $self->{note}->trans_id($self->{cv}->id);
127 $self->{note}->save();
129 $self->{note_followup}->save();
131 $self->{note_followup_link}->follow_up_id($self->{note_followup}->id);
132 $self->{note_followup_link}->trans_id($self->{cv}->id);
133 $self->{note_followup_link}->save();
135 SL::Helper::Flash::flash_later('info', $::locale->text('Follow-Up saved.'));
138 $self->{shipto}->trans_id($self->{cv}->id);
139 if( $self->{shipto}->shiptoname ne '' ) {
140 $self->{shipto}->save();
143 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
144 SL::DB::History->new(
145 trans_id => $self->{cv}->id,
146 snumbers => $snumbers,
147 employee_id => SL::DB::Manager::Employee->current->id,
151 if ( $::form->{delete_notes} ) {
152 foreach my $note_id (@{ $::form->{delete_notes} }) {
153 my $note = SL::DB::Note->new(id => $note_id)->load();
154 if ( $note->follow_up ) {
155 if ( $note->follow_up->follow_up_link ) {
156 $note->follow_up->follow_up_link->delete(cascade => 'delete');
158 $note->follow_up->delete(cascade => 'delete');
160 $note->delete(cascade => 'delete');
163 }) || die($db->error);
170 my $pattern = '[ \t\n\r]+';
171 my $name = $self->{cv}->name;
172 $name =~ s/^$pattern//;
173 $name =~ s/$pattern$//;
174 $name =~ s/$pattern/ /g;
176 $::form->dberror( $::locale->text('Customer missing!') );
178 $self->{cv}->name($name);
183 my @redirect_params = (
185 id => $self->{cv}->id,
186 db => ($self->is_vendor() ? 'vendor' : 'customer'),
189 if ( $self->{contact}->cp_id ) {
190 push(@redirect_params, contact_id => $self->{contact}->cp_id);
193 if ( $self->{shipto}->shipto_id ) {
194 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
197 $self->redirect_to(@redirect_params);
200 sub action_save_and_close {
205 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
206 $::form->redirect($msg);
210 my ($self, $script) = @_;
212 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
213 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
217 my $callback = $::form->escape($::form->{callback}, 1);
218 my $name = $::form->escape($self->{cv}->name, 1);
219 my $db = $self->is_vendor() ? 'vendor' : 'customer';
221 my $url = $self->url_for(
222 controller => $script,
225 $db .'_id' => $self->{cv}->id,
227 type => $::form->{type},
228 callback => $callback,
231 print $::form->redirect_header($url);
234 sub action_save_and_ar_transaction {
237 $main::auth->assert('general_ledger');
239 $self->_transaction('ar.pl');
242 sub action_save_and_ap_transaction {
245 $main::auth->assert('general_ledger');
247 $self->_transaction('ap.pl');
250 sub action_save_and_invoice {
253 if ( $self->is_vendor() ) {
254 $::auth->assert('vendor_invoice_edit');
256 $::auth->assert('invoice_edit');
259 $::form->{type} = 'invoice';
260 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
263 sub action_save_and_order {
266 if ( $self->is_vendor() ) {
267 $::auth->assert('purchase_order_edit');
269 $::auth->assert('sales_order_edit');
272 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
273 $self->_transaction('oe.pl');
276 sub action_save_and_rfq {
279 $::auth->assert('request_quotation_edit');
281 $::form->{type} = 'request_quotation';
282 $self->_transaction('oe.pl');
285 sub action_save_and_quotation {
288 $::auth->assert('sales_quotation_edit');
290 $::form->{type} = 'sales_quotation';
291 $self->_transaction('oe.pl');
297 my $db = $self->{cv}->db;
299 if( !$self->is_orphaned() ) {
300 $self->action_edit();
303 $db->do_transaction(sub {
304 $self->{cv}->delete(cascade => 1);
306 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
307 SL::DB::History->new(
308 trans_id => $self->{cv}->id,
309 snumbers => $snumbers,
310 employee_id => SL::DB::Manager::Employee->current->id,
311 addition => 'DELETED',
313 }) || die($db->error);
315 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
316 $::form->redirect($msg);
322 sub action_delete_contact {
325 my $db = $self->{contact}->db;
327 if ( !$self->{contact}->cp_id ) {
328 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
331 $db->do_transaction(sub {
332 if ( $self->{contact}->used ) {
333 $self->{contact}->detach();
334 $self->{contact}->save();
335 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
337 $self->{contact}->delete(cascade => 1);
338 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
340 }) || die($db->error);
342 $self->{contact} = SL::DB::Contact->new();
345 $self->action_edit();
348 sub action_delete_shipto {
351 my $db = $self->{shipto}->db;
353 if ( !$self->{shipto}->shipto_id ) {
354 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
357 $db->do_transaction(sub {
358 if ( $self->{shipto}->used ) {
359 $self->{shipto}->detach();
360 $self->{shipto}->save(cascade => 1);
361 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
363 $self->{shipto}->delete(cascade => 1);
364 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
366 }) || die($db->error);
368 $self->{shipto} = SL::DB::Shipto->new();
371 $self->action_edit();
379 controller => 'ct.pl',
381 db => $self->is_vendor() ? 'vendor' : 'customer',
384 if ( $::form->{callback} ) {
385 push(@url_params, callback => $::form->{callback});
388 $self->redirect_to(@url_params);
392 sub action_search_contact {
395 my $url = 'ct.pl?action=search_contact&db=customer';
397 if ( $::form->{callback} ) {
398 $url .= '&callback='. $::form->escape($::form->{callback});
401 print $::form->redirect_header($url);
405 sub action_get_delivery {
408 $::auth->assert('sales_all_edit');
410 my $dbh = $::form->get_standard_dbh();
412 my ($arap, $db, $qty_sign);
413 if ( $self->is_vendor() ) {
416 $qty_sign = ' * -1 AS qty';
423 my $where = ' WHERE 1=1';
426 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
427 $where .= " AND ${arap}.shipto_id = ?";
428 push(@values, $::form->{shipto_id});
430 $where .= " AND ${arap}.${db}_id = ?";
431 push(@values, $::form->{id});
434 if ( $::form->{delivery_from} ) {
435 $where .= " AND ${arap}.transdate >= ?";
436 push(@values, conv_date($::form->{delivery_from}));
439 if ( $::form->{delivery_to} ) {
440 $where .= " AND ${arap}.transdate <= ?";
441 push(@values, conv_date($::form->{delivery_to}));
460 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
463 ON ${arap}.id = i.trans_id
469 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
472 ORDER BY ${arap}.transdate DESC LIMIT 15";
474 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
476 $self->render('customer_vendor/get_delivery', { layout => 0 });
479 sub action_ajaj_get_shipto {
485 my $name = 'shipto'. $_;
486 $name => $self->{shipto}->$name;
488 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
492 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
495 sub action_ajaj_get_contact {
503 my $name = 'cp_'. $_;
505 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
506 $name => $self->{contact}->$name->to_lxoffice;
508 $name => $self->{contact}->$name;
512 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
513 satphone satfax project street zipcode city privatphone privatemail birthday
518 $data->{contact_cvars} = {
521 if ( $_->config->type eq 'number' ) {
522 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
524 $_->config->name => $_->value;
529 @{$self->{contact}->cvars_by_config}
534 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
537 sub action_ajaj_customer_autocomplete {
538 my ($self, %params) = @_;
540 my $limit = $::form->{limit} || 20;
541 my $type = $::form->{type} || {};
542 my $query = { ilike => '%'. $::form->{term} .'%' };
547 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
550 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
551 my $value_col = $::form->{column} || 'name';
557 value => $_->can($value_col)->($_),
558 label => $_->displayable_name,
560 customernumber => $_->customernumber,
568 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
572 return $::form->{db} eq 'vendor';
576 return $::form->{db} eq 'customer';
582 if ( defined($self->{_is_orphaned}) ) {
583 return $self->{_is_orphaned};
586 my $arap = $self->is_vendor ? 'ap' : 'ar';
589 my $cv = $self->is_vendor ? 'vendor' : 'customer';
593 FROM '. $arap .' AS a
594 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
601 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
605 if ( $self->is_vendor ) {
608 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
612 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
614 return $self->{_is_orphaned} = !$dummy;
617 sub _instantiate_args {
620 my $curr_employee = SL::DB::Manager::Employee->current;
622 if ( $::form->{cv}->{id} ) {
623 if ( $self->is_vendor() ) {
624 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
626 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
629 if ( $self->is_vendor() ) {
630 $self->{cv} = SL::DB::Vendor->new();
632 $self->{cv} = SL::DB::Customer->new();
635 $self->{cv}->assign_attributes(%{$::form->{cv}});
637 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
638 $self->{cv}->taxincluded_checked(undef);
642 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
643 my $value = $::form->{cv_cvars}->{$cvar->config->name};
645 if ( $cvar->config->type eq 'number' ) {
646 $value = $::form->parse_amount(\%::myconfig, $value);
649 $cvar->value($value);
652 if ( $::form->{note}->{id} ) {
653 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
654 $self->{note_followup} = $self->{note}->follow_up;
655 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
657 $self->{note} = SL::DB::Note->new();
658 $self->{note_followup} = SL::DB::FollowUp->new();
659 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
662 $self->{note}->assign_attributes(%{$::form->{note}});
663 $self->{note}->created_by($curr_employee->id);
664 $self->{note}->trans_module('ct');
666 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
667 $self->{note_followup}->note($self->{note});
668 $self->{note_followup}->created_by($curr_employee->id);
670 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
671 $self->{note_followup_link}->trans_info($self->{cv}->name);
673 if ( $::form->{shipto}->{shipto_id} ) {
674 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
676 $self->{shipto} = SL::DB::Shipto->new();
678 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
679 $self->{shipto}->module('CT');
681 if ( $::form->{contact}->{cp_id} ) {
682 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
684 $self->{contact} = SL::DB::Contact->new();
686 $self->{contact}->assign_attributes(%{$::form->{contact}});
688 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
689 my $value = $::form->{contact_cvars}->{$cvar->config->name};
691 if ( $cvar->config->type eq 'number' ) {
692 $value = $::form->parse_amount(\%::myconfig, $value);
695 $cvar->value($value);
699 sub _load_customer_vendor {
702 if ( $self->is_vendor() ) {
703 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
705 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
708 if ( $::form->{note_id} ) {
709 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
710 $self->{note_followup} = $self->{note}->follow_up;
711 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
713 $self->{note} = SL::DB::Note->new();
714 $self->{note_followup} = SL::DB::FollowUp->new();
715 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
718 if ( $::form->{shipto_id} ) {
719 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
721 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
722 die($::locale->text('Error'));
725 $self->{shipto} = SL::DB::Shipto->new();
728 if ( $::form->{contact_id} ) {
729 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
731 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
732 die($::locale->text('Error'));
735 $self->{contact} = SL::DB::Contact->new();
739 sub _create_customer_vendor {
742 if ( $self->is_vendor() ) {
743 $self->{cv} = SL::DB::Vendor->new();
745 $self->{cv} = SL::DB::Customer->new();
747 $self->{cv}->currency_id($::instance_conf->get_currency_id());
749 $self->{note} = SL::DB::Note->new();
751 $self->{note_followup} = SL::DB::FollowUp->new();
753 $self->{shipto} = SL::DB::Shipto->new();
755 $self->{contact} = SL::DB::Contact->new();
761 my $dbh = $::form->get_standard_dbh();
765 $self->{all_business} = SL::DB::Manager::Business->get_all();
767 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
770 'SELECT DISTINCT(greeting)
772 WHERE greeting IS NOT NULL AND greeting != \'\'
774 SELECT DISTINCT(greeting)
776 WHERE greeting IS NOT NULL AND greeting != \'\'
778 $self->{all_greetings} = [
781 selectall_hashref_query($::form, $dbh, $query)
786 'SELECT DISTINCT(cp_title) AS title
788 WHERE cp_title IS NOT NULL AND cp_title != \'\'
790 $self->{all_titles} = [
793 selectall_hashref_query($::form, $dbh, $query)
797 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
799 $self->{all_languages} = SL::DB::Manager::Language->get_all();
801 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
803 if ( $::instance_conf->get_vertreter() ) {
811 selectall_hashref_query($::form, $dbh, $query)
815 if ( $business_ids->[0] ) {
816 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
818 $self->{all_salesman_customers} = [];
821 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
824 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
826 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
828 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
831 'SELECT DISTINCT(cp_abteilung) AS department
833 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
834 ORDER BY cp_abteilung';
835 $self->{all_departments} = [
837 { $_->{department}; }
838 selectall_hashref_query($::form, $dbh, $query)
842 $self->{contacts} = $self->{cv}->contacts;
843 $self->{contacts} ||= [];
845 $self->{shiptos} = $self->{cv}->shipto;
846 $self->{shiptos} ||= [];
848 $self->{notes} = SL::DB::Manager::Note->get_all(
850 trans_id => $self->{cv}->id,
851 trans_module => 'ct',
853 with_objects => ['follow_up'],
856 $self->{template_args} ||= {};
858 $::request->{layout}->add_javascripts('autocomplete_customer.js');
859 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');