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);
172 my @redirect_params = (
174 id => $self->{cv}->id,
175 db => ($self->is_vendor() ? 'vendor' : 'customer'),
178 if ( $self->{contact}->cp_id ) {
179 push(@redirect_params, contact_id => $self->{contact}->cp_id);
182 if ( $self->{shipto}->shipto_id ) {
183 push(@redirect_params, shipto_id => $self->{shipto}->shipto_id);
186 $self->redirect_to(@redirect_params);
189 sub action_save_and_close {
194 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
195 $::form->redirect($msg);
199 my ($self, $script) = @_;
201 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
202 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
206 my $callback = $::form->escape($::form->{callback}, 1);
207 my $name = $::form->escape($self->{cv}->name, 1);
208 my $db = $self->is_vendor() ? 'vendor' : 'customer';
210 my $url = $self->url_for(
211 controller => $script,
214 $db .'_id' => $self->{cv}->id,
216 type => $::form->{type},
217 callback => $callback,
220 print $::form->redirect_header($url);
223 sub action_save_and_ar_transaction {
226 $main::auth->assert('general_ledger');
228 $self->_transaction('ar.pl');
231 sub action_save_and_ap_transaction {
234 $main::auth->assert('general_ledger');
236 $self->_transaction('ap.pl');
239 sub action_save_and_invoice {
242 if ( $self->is_vendor() ) {
243 $::auth->assert('vendor_invoice_edit');
245 $::auth->assert('invoice_edit');
248 $::form->{type} = 'invoice';
249 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
252 sub action_save_and_order {
255 if ( $self->is_vendor() ) {
256 $::auth->assert('purchase_order_edit');
258 $::auth->assert('sales_order_edit');
261 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
262 $self->_transaction('oe.pl');
265 sub action_save_and_rfq {
268 $::auth->assert('request_quotation_edit');
270 $::form->{type} = 'request_quotation';
271 $self->_transaction('oe.pl');
274 sub action_save_and_quotation {
277 $::auth->assert('sales_quotation_edit');
279 $::form->{type} = 'sales_quotation';
280 $self->_transaction('oe.pl');
286 my $db = $self->{cv}->db;
288 if( !$self->is_orphaned() ) {
289 $self->action_edit();
292 $db->do_transaction(sub {
293 $self->{cv}->delete(cascade => 1);
295 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
296 SL::DB::History->new(
297 trans_id => $self->{cv}->id,
298 snumbers => $snumbers,
299 employee_id => SL::DB::Manager::Employee->current->id,
300 addition => 'DELETED',
302 }) || die($db->error);
304 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
305 $::form->redirect($msg);
311 sub action_delete_contact {
314 my $db = $self->{contact}->db;
316 if ( !$self->{contact}->cp_id ) {
317 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
320 $db->do_transaction(sub {
321 if ( $self->{contact}->used ) {
322 $self->{contact}->detach();
323 $self->{contact}->save();
324 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
326 $self->{contact}->delete(cascade => 1);
327 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
329 }) || die($db->error);
331 $self->{contact} = SL::DB::Contact->new();
334 $self->action_edit();
337 sub action_delete_shipto {
340 my $db = $self->{shipto}->db;
342 if ( !$self->{shipto}->shipto_id ) {
343 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
346 $db->do_transaction(sub {
347 if ( $self->{shipto}->used ) {
348 $self->{shipto}->detach();
349 $self->{shipto}->save(cascade => 1);
350 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
352 $self->{shipto}->delete(cascade => 1);
353 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
355 }) || die($db->error);
357 $self->{shipto} = SL::DB::Shipto->new();
360 $self->action_edit();
368 controller => 'ct.pl',
370 db => $self->is_vendor() ? 'vendor' : 'customer',
373 if ( $::form->{callback} ) {
374 push(@url_params, callback => $::form->{callback});
377 $self->redirect_to(@url_params);
381 sub action_search_contact {
384 my $url = 'ct.pl?action=search_contact&db=customer';
386 if ( $::form->{callback} ) {
387 $url .= '&callback='. $::form->escape($::form->{callback});
390 print $::form->redirect_header($url);
394 sub action_get_delivery {
397 $::auth->assert('sales_all_edit');
399 my $dbh = $::form->get_standard_dbh();
401 my ($arap, $db, $qty_sign);
402 if ( $self->is_vendor() ) {
405 $qty_sign = ' * -1 AS qty';
412 my $where = ' WHERE 1=1';
415 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
416 $where .= " AND ${arap}.shipto_id = ?";
417 push(@values, $::form->{shipto_id});
419 $where .= " AND ${arap}.${db}_id = ?";
420 push(@values, $::form->{id});
423 if ( $::form->{delivery_from} ) {
424 $where .= " AND ${arap}.transdate >= ?";
425 push(@values, conv_date($::form->{delivery_from}));
428 if ( $::form->{delivery_to} ) {
429 $where .= " AND ${arap}.transdate <= ?";
430 push(@values, conv_date($::form->{delivery_to}));
449 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
452 ON ${arap}.id = i.trans_id
458 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
461 ORDER BY ${arap}.transdate DESC LIMIT 15";
463 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
465 $self->render('customer_vendor/get_delivery', { layout => 0 });
468 sub action_ajaj_get_shipto {
474 my $name = 'shipto'. $_;
475 $name => $self->{shipto}->$name;
477 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
481 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
484 sub action_ajaj_get_contact {
492 my $name = 'cp_'. $_;
494 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
495 $name => $self->{contact}->$name->to_lxoffice;
497 $name => $self->{contact}->$name;
501 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
502 satphone satfax project street zipcode city privatphone privatemail birthday
507 $data->{contact_cvars} = {
510 if ( $_->config->type eq 'number' ) {
511 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
513 $_->config->name => $_->value;
518 @{$self->{contact}->cvars_by_config}
523 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
526 sub action_ajaj_customer_autocomplete {
527 my ($self, %params) = @_;
529 my $limit = $::form->{limit} || 20;
530 my $type = $::form->{type} || {};
531 my $query = { ilike => '%'. $::form->{term} .'%' };
536 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
539 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
540 my $value_col = $::form->{column} || 'name';
546 value => $_->can($value_col)->($_),
547 label => $_->displayable_name,
549 customernumber => $_->customernumber,
557 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
561 return $::form->{db} eq 'vendor';
565 return $::form->{db} eq 'customer';
571 if ( defined($self->{_is_orphaned}) ) {
572 return $self->{_is_orphaned};
575 my $arap = $self->is_vendor ? 'ap' : 'ar';
578 my $cv = $self->is_vendor ? 'vendor' : 'customer';
582 FROM '. $arap .' AS a
583 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
590 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
594 if ( $self->is_vendor ) {
597 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
601 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
603 return $self->{_is_orphaned} = !$dummy;
606 sub _instantiate_args {
609 my $curr_employee = SL::DB::Manager::Employee->current;
611 if ( $::form->{cv}->{id} ) {
612 if ( $self->is_vendor() ) {
613 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
615 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
618 if ( $self->is_vendor() ) {
619 $self->{cv} = SL::DB::Vendor->new();
621 $self->{cv} = SL::DB::Customer->new();
624 $self->{cv}->assign_attributes(%{$::form->{cv}});
626 if ( $self->is_customer() && $::form->{cv}->{taxincluded_checked} eq '' ) {
627 $self->{cv}->taxincluded_checked(undef);
631 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
632 my $value = $::form->{cv_cvars}->{$cvar->config->name};
634 if ( $cvar->config->type eq 'number' ) {
635 $value = $::form->parse_amount(\%::myconfig, $value);
638 $cvar->value($value);
641 if ( $::form->{note}->{id} ) {
642 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
643 $self->{note_followup} = $self->{note}->follow_up;
644 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
646 $self->{note} = SL::DB::Note->new();
647 $self->{note_followup} = SL::DB::FollowUp->new();
648 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
651 $self->{note}->assign_attributes(%{$::form->{note}});
652 $self->{note}->created_by($curr_employee->id);
653 $self->{note}->trans_module('ct');
655 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
656 $self->{note_followup}->note($self->{note});
657 $self->{note_followup}->created_by($curr_employee->id);
659 $self->{note_followup_link}->trans_type($self->is_vendor() ? 'vendor' : 'customer');
660 $self->{note_followup_link}->trans_info($self->{cv}->name);
662 if ( $::form->{shipto}->{shipto_id} ) {
663 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
665 $self->{shipto} = SL::DB::Shipto->new();
667 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
668 $self->{shipto}->module('CT');
670 if ( $::form->{contact}->{cp_id} ) {
671 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
673 $self->{contact} = SL::DB::Contact->new();
675 $self->{contact}->assign_attributes(%{$::form->{contact}});
677 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
678 my $value = $::form->{contact_cvars}->{$cvar->config->name};
680 if ( $cvar->config->type eq 'number' ) {
681 $value = $::form->parse_amount(\%::myconfig, $value);
684 $cvar->value($value);
688 sub _load_customer_vendor {
691 if ( $self->is_vendor() ) {
692 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
694 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
697 if ( $::form->{note_id} ) {
698 $self->{note} = SL::DB::Note->new(id => $::form->{note_id})->load();
699 $self->{note_followup} = $self->{note}->follow_up;
700 $self->{note_followup_link} = $self->{note_followup}->follow_up_link;
702 $self->{note} = SL::DB::Note->new();
703 $self->{note_followup} = SL::DB::FollowUp->new();
704 $self->{note_followup_link} = SL::DB::FollowUpLink->new();
707 if ( $::form->{shipto_id} ) {
708 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
710 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
711 die($::locale->text('Error'));
714 $self->{shipto} = SL::DB::Shipto->new();
717 if ( $::form->{contact_id} ) {
718 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
720 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
721 die($::locale->text('Error'));
724 $self->{contact} = SL::DB::Contact->new();
728 sub _create_customer_vendor {
731 if ( $self->is_vendor() ) {
732 $self->{cv} = SL::DB::Vendor->new();
734 $self->{cv} = SL::DB::Customer->new();
736 $self->{cv}->currency_id($::instance_conf->get_currency_id());
738 $self->{note} = SL::DB::Note->new();
740 $self->{note_followup} = SL::DB::FollowUp->new();
742 $self->{shipto} = SL::DB::Shipto->new();
744 $self->{contact} = SL::DB::Contact->new();
750 my $dbh = $::form->get_standard_dbh();
754 $self->{all_business} = SL::DB::Manager::Business->get_all();
756 $self->{all_employees} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
759 'SELECT DISTINCT(greeting)
761 WHERE greeting IS NOT NULL AND greeting != \'\'
763 SELECT DISTINCT(greeting)
765 WHERE greeting IS NOT NULL AND greeting != \'\'
767 $self->{all_greetings} = [
770 selectall_hashref_query($::form, $dbh, $query)
775 'SELECT DISTINCT(cp_title) AS title
777 WHERE cp_title IS NOT NULL AND cp_title != \'\'
779 $self->{all_titles} = [
782 selectall_hashref_query($::form, $dbh, $query)
786 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
788 $self->{all_languages} = SL::DB::Manager::Language->get_all();
790 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
792 if ( $::instance_conf->get_vertreter() ) {
800 selectall_hashref_query($::form, $dbh, $query)
804 if ( $business_ids->[0] ) {
805 $self->{all_salesman_customers} = SL::DB::Manager::Customer->get_all(query => [business_id => $business_ids]);
807 $self->{all_salesman_customers} = [];
810 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $self->{cv}->salesman_id, deleted => 0 ] ]);
813 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
815 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all();
817 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
820 'SELECT DISTINCT(cp_abteilung) AS department
822 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
823 ORDER BY cp_abteilung';
824 $self->{all_departments} = [
826 { $_->{department}; }
827 selectall_hashref_query($::form, $dbh, $query)
831 $self->{contacts} = $self->{cv}->contacts;
832 $self->{contacts} ||= [];
834 $self->{shiptos} = $self->{cv}->shipto;
835 $self->{shiptos} ||= [];
837 $self->{notes} = SL::DB::Manager::Note->get_all(
839 trans_id => $self->{cv}->id,
840 trans_module => 'ct',
842 with_objects => ['follow_up'],
845 $self->{template_args} ||= {};
847 $::request->{layout}->add_javascripts('autocomplete_customer.js');
848 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');