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',
48 __PACKAGE__->run_before(
49 '_load_customer_vendor',
57 __PACKAGE__->run_before(
58 '_create_customer_vendor',
69 'customer_vendor/form',
70 title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
71 %{$self->{template_args}}
80 'customer_vendor/form',
81 title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
82 %{$self->{template_args}}
89 my $db = $self->{cv}->db;
91 $db->do_transaction(sub {
93 if ( $self->is_vendor() ) {
94 if ( $self->{cv}->vendornumber ) {
95 $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 my @redirect_params = (
151 id => $self->{cv}->id,
152 db => ($self->is_vendor() ? 'vendor' : 'customer'),
155 if ( $self->{contact}->cp_id ) {
156 push(@redirect_params, contact_id => $self->{contact}->cp_id);
159 if ( $self->{shipto}->shipto_id ) {
160 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
163 $self->redirect_to(@redirect_params);
166 sub action_save_and_close {
171 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
172 $::form->redirect($msg);
176 my ($self, $script) = @_;
178 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
179 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
183 my $callback = $::form->escape($::form->{callback}, 1);
184 my $name = $::form->escape($self->{cv}->name, 1);
185 my $db = $self->is_vendor() ? 'vendor' : 'customer';
187 my $url = $self->url_for(
188 controller => $script,
191 $db .'_id' => $self->{cv}->id,
193 type => $::form->{type},
194 callback => $callback,
197 print $::form->redirect_header($url);
200 sub action_save_and_ar_transaction {
203 $main::auth->assert('general_ledger');
205 $self->_transaction('ar.pl');
208 sub action_save_and_ap_transaction {
211 $main::auth->assert('general_ledger');
213 $self->_transaction('ap.pl');
216 sub action_save_and_invoice {
219 if ( $self->is_vendor() ) {
220 $::auth->assert('vendor_invoice_edit');
222 $::auth->assert('invoice_edit');
225 $::form->{type} = 'invoice';
226 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
229 sub action_save_and_order {
232 if ( $self->is_vendor() ) {
233 $::auth->assert('purchase_order_edit');
235 $::auth->assert('sales_order_edit');
238 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
239 $self->_transaction('oe.pl');
242 sub action_save_and_rfq {
245 $::auth->assert('request_quotation_edit');
247 $::form->{type} = 'request_quotation';
248 $self->_transaction('oe.pl');
251 sub action_save_and_quotation {
254 $::auth->assert('sales_quotation_edit');
256 $::form->{type} = 'sales_quotation';
257 $self->_transaction('oe.pl');
263 my $db = $self->{cv}->db;
265 if( !$self->is_orphaned() ) {
266 $self->action_edit();
269 $db->do_transaction(sub {
270 $self->{cv}->delete(cascade => 1);
272 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
273 SL::DB::History->new(
274 trans_id => $self->{cv}->id,
275 snumbers => $snumbers,
276 employee_id => SL::DB::Manager::Employee->current->id,
277 addition => 'DELETED',
279 }) || die($db->error);
281 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
282 $::form->redirect($msg);
288 sub action_delete_contact {
291 my $db = $self->{contact}->db;
293 if ( !$self->{contact}->cp_id ) {
294 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
297 $db->do_transaction(sub {
298 if ( $self->{contact}->used ) {
299 $self->{contact}->detach();
300 $self->{contact}->save();
301 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
303 $self->{contact}->delete(cascade => 1);
304 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
306 }) || die($db->error);
308 $self->{contact} = SL::DB::Contact->new();
311 $self->action_edit();
314 sub action_delete_shipto {
317 my $db = $self->{shipto}->db;
319 if ( !$self->{shipto}->shipto_id ) {
320 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
323 $db->do_transaction(sub {
324 if ( $self->{shipto}->used ) {
325 $self->{shipto}->detach();
326 $self->{shipto}->save(cascade => 1);
327 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
329 $self->{shipto}->delete(cascade => 1);
330 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
332 }) || die($db->error);
334 $self->{shipto} = SL::DB::Shipto->new();
337 $self->action_edit();
344 my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer');
346 if ( $::form->{callback} ) {
347 $url .= '&callback='. $::from->escape($::form->{callback});
350 print $::form->redirect_header($url);
354 sub action_search_contact {
357 my $url = 'ct.pl?action=search_contact&db=customer';
359 if ( $::form->{callback} ) {
360 $url .= '&callback='. $::from->escape($::form->{callback});
363 print $::form->redirect_header($url);
367 sub action_get_delivery {
370 my $dbh = $::form->get_standard_dbh();
372 my ($arap, $db, $qty_sign);
373 if ( $self->is_vendor() ) {
376 $qty_sign = ' * -1 AS qty';
383 my $where = ' WHERE 1=1 ';
386 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
387 $where .= "AND ${arap}.shipto_id = ?";
388 push(@values, $::form->{shipto_id});
391 if ( $::form->{delivery_from} ) {
392 $where .= "AND ${arap}.transdate >= ?";
393 push(@values, conv_date($::form->{delivery_from}));
396 if ( $::form->{delivery_to} ) {
397 $where .= "AND ${arap}.transdate <= ?";
398 push(@values, conv_date($::form->{delivery_to}));
417 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
420 ON ${arap}.id = i.trans_id
426 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
429 ORDER BY ${arap}.transdate DESC LIMIT 15";
431 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
433 $self->render('customer_vendor/get_delivery', { layout => 0 });
436 sub action_ajaj_get_shipto {
442 my $name = 'shipto'. $_;
443 $name => $self->{shipto}->$name;
445 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
449 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
452 sub action_ajaj_get_contact {
460 my $name = 'cp_'. $_;
462 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
463 $name => $self->{contact}->$name->to_lxoffice;
465 $name => $self->{contact}->$name;
469 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
470 satphone satfax project street zipcode city privatphone privatemail birthday
475 $data->{contact_cvars} = {
478 if ( $_->config->type eq 'number' ) {
479 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
481 $_->config->name => $_->value;
486 @{$self->{contact}->cvars_by_config}
491 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
494 sub action_ajaj_customer_autocomplete {
495 my ($self, %params) = @_;
497 my $limit = $::form->{limit} || 20;
498 my $type = $::form->{type} || {};
499 my $query = { ilike => '%'. $::form->{term} .'%' };
504 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
507 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
508 my $value_col = $::form->{column} || 'name';
514 value => $_->can($value_col)->($_),
515 label => $_->displayable_name,
517 customernumber => $_->customernumber,
525 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
529 return $::form->{db} eq 'vendor';
533 return $::form->{db} eq 'customer';
539 if ( defined($self->{_is_orphaned}) ) {
540 return $self->{_is_orphaned};
543 my $arap = $self->is_vendor ? 'ap' : 'ar';
546 my $cv = $self->is_vendor ? 'vendor' : 'customer';
550 FROM '. $arap .' AS a
551 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
558 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
562 if ( $self->is_vendor ) {
565 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
569 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
571 return $self->{_is_orphaned} = !$dummy;
574 sub _instantiate_args {
577 my $curr_employee = SL::DB::Manager::Employee->current;
579 if ( $::form->{cv}->{id} ) {
580 if ( $self->is_vendor() ) {
581 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
583 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
586 if ( $self->is_vendor() ) {
587 $self->{cv} = SL::DB::Vendor->new();
589 $self->{cv} = SL::DB::Customer->new();
592 $self->{cv}->assign_attributes(%{$::form->{cv}});
594 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
595 $self->{cv}->taxincluded_checked(undef);
599 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
600 my $value = $::form->{cv_cvars}->{$cvar->config->name};
602 if ( $cvar->config->type eq 'number' ) {
603 $value = $::form->parse_amount(\%::myconfig, $value);
606 $cvar->value($value);
609 if ( $::form->{note}->{id} ) {
610 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
612 $self->{note} = SL::DB::Note->new();
614 $self->{note}->assign_attributes(%{$::form->{note}});
615 $self->{note}->created_by($curr_employee->id);
616 $self->{note}->trans_module('ct');
618 $self->{note_followup} = SL::DB::FollowUp->new();
619 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
620 $self->{note_followup}->note($self->{note});
621 $self->{note_followup}->created_by($curr_employee->id);
623 if ( $::form->{shipto}->{shipto_id} ) {
624 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
626 $self->{shipto} = SL::DB::Shipto->new();
628 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
629 $self->{shipto}->module('CT');
631 if ( $::form->{contact}->{cp_id} ) {
632 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
634 $self->{contact} = SL::DB::Contact->new();
636 $self->{contact}->assign_attributes(%{$::form->{contact}});
638 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
639 my $value = $::form->{contact_cvars}->{$cvar->config->name};
641 if ( $cvar->config->type eq 'number' ) {
642 $value = $::form->parse_amount(\%::myconfig, $value);
645 $cvar->value($value);
649 sub _load_customer_vendor {
652 if ( $self->is_vendor() ) {
653 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
655 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
658 $self->{note} = SL::DB::Note->new();
659 $self->{note_followup} = SL::DB::FollowUp->new();
661 if ( $::form->{shipto_id} ) {
662 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
664 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
665 die($::locale->text('Error'));
668 $self->{shipto} = SL::DB::Shipto->new();
671 if ( $::form->{contact_id} ) {
672 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
674 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
675 die($::locale->text('Error'));
678 $self->{contact} = SL::DB::Contact->new();
682 sub _create_customer_vendor {
685 if ( $self->is_vendor() ) {
686 $self->{cv} = SL::DB::Vendor->new();
688 $self->{cv} = SL::DB::Customer->new();
690 $self->{cv}->currency_id($::instance_conf->get_currency_id());
692 $self->{note} = SL::DB::Note->new();
694 $self->{note_followup} = SL::DB::FollowUp->new();
696 $self->{shipto} = SL::DB::Shipto->new();
698 $self->{contact} = SL::DB::Contact->new();
704 my $dbh = $::form->get_standard_dbh();
708 $self->{all_business} = SL::DB::Manager::Business->get_all();
710 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
713 'SELECT DISTINCT(greeting)
715 WHERE greeting IS NOT NULL AND greeting != \'\'
717 SELECT DISTINCT(greeting)
719 WHERE greeting IS NOT NULL AND greeting != \'\'
721 $self->{all_greetings} = [
724 selectall_hashref_query($::form, $dbh, $query)
729 'SELECT DISTINCT(cp_title) AS title
731 WHERE cp_title IS NOT NULL AND cp_title != \'\'
733 $self->{all_titles} = [
736 selectall_hashref_query($::form, $dbh, $query)
740 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
742 $self->{all_languages} = SL::DB::Manager::Language->get_all();
744 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
746 if ( $::instance_conf->get_vertreter() ) {
754 selectall_hashref_query($::form, $dbh, $query)
758 if ( $business_ids->[0] ) {
759 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
761 $self->{all_salesman_customers} = [];
764 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
767 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
769 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
772 'SELECT DISTINCT(cp_abteilung) AS department
774 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
775 ORDER BY cp_abteilung';
776 $self->{all_departments} = [
778 { $_->{department}; }
779 selectall_hashref_query($::form, $dbh, $query)
783 $self->{contacts} = $self->{cv}->contacts;
784 $self->{contacts} ||= [];
786 $self->{shiptos} = $self->{cv}->shipto;
787 $self->{shiptos} ||= [];
789 $self->{template_args} ||= {};
791 $::request->{layout}->add_javascripts('autocomplete_customer.js');
792 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');