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]);
97 if ( $self->{cv}->customernumber ) {
98 $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
102 foreach my $entry (@{$cvs_by_nr}) {
103 if( $entry->id != $self->{cv}->id ) {
105 $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
107 $::form->error($msg);
111 $self->{cv}->save(cascade => 1);
113 $self->{contact}->cp_cv_id($self->{cv}->id);
114 if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
115 $self->{contact}->save();
118 if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
119 $self->{note}->trans_id($self->{cv}->id);
120 $self->{note}->save();
121 $self->{note_followup}->save();
123 $self->{note} = SL::DB::Note->new();
124 $self->{note_followup} = SL::DB::FollowUp->new();
127 $self->{shipto}->trans_id($self->{cv}->id);
128 if( $self->{shipto}->shiptoname ne '' ) {
129 $self->{shipto}->save();
132 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
133 SL::DB::History->new(
134 trans_id => $self->{cv}->id,
135 snumbers => $snumbers,
136 employee_id => SL::DB::Manager::Employee->current->id,
139 }) || die($db->error);
148 $self->redirect_to(action => 'edit', id => $self->{cv}->id);
151 sub action_save_and_close {
156 my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
157 $::form->redirect($msg);
161 my ($self, $script) = @_;
163 $::auth->assert('general_ledger | invoice_edit | vendor_invoice_edit | ' .
164 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit');
168 my $callback = $::form->escape($::form->{callback}, 1);
169 my $name = $::form->escape($self->{cv}->name, 1);
170 my $db = $self->is_vendor() ? 'vendor' : 'customer';
176 $db .'_id=' . $self->{cv}->id .'&'.
177 $db .'='. $name .'&'.
178 'type='. $::form->{type} .'&'.
179 'callback='. $callback;
181 print $::form->redirect_header($url);
184 sub action_save_and_ar_transaction {
187 $main::auth->assert('general_ledger');
189 $self->_transaction('ar.pl');
192 sub action_save_and_ap_transaction {
195 $main::auth->assert('general_ledger');
197 $self->_transaction('ap.pl');
200 sub action_save_and_invoice {
203 if ( $self->is_vendor() ) {
204 $::auth->assert('vendor_invoice_edit');
206 $::auth->assert('invoice_edit');
209 $::form->{type} = 'invoice';
210 $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
213 sub action_save_and_order {
216 if ( $self->is_vendor() ) {
217 $::auth->assert('purchase_order_edit');
219 $::auth->assert('sales_order_edit');
222 $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
223 $self->_transaction('oe.pl');
226 sub action_save_and_rfq {
229 $::auth->assert('request_quotation_edit');
231 $::form->{type} = 'request_quotation';
232 $self->_transaction('oe.pl');
235 sub action_save_and_quotation {
238 $::auth->assert('sales_quotation_edit');
240 $::form->{type} = 'sales_quotation';
241 $self->_transaction('oe.pl');
247 my $db = $self->{cv}->db;
249 if( !$self->is_orphaned() ) {
250 $self->action_edit();
253 $db->do_transaction(sub {
254 $self->{cv}->delete(cascade => 1);
256 my $snumbers = $self->is_vendor() ? 'vendornumber_'. $self->{cv}->vendornumber : 'customernumber_'. $self->{cv}->customernumber;
257 SL::DB::History->new(
258 trans_id => $self->{cv}->id,
259 snumbers => $snumbers,
260 employee_id => SL::DB::Manager::Employee->current->id,
261 addition => 'DELETED',
263 }) || die($db->error);
265 my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
266 $::form->redirect($msg);
272 sub action_delete_contact {
275 my $db = $self->{contact}->db;
277 if ( !$self->{contact}->cp_id ) {
278 SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
281 $db->do_transaction(sub {
282 if ( $self->{contact}->used ) {
283 $self->{contact}->detach();
284 $self->{contact}->save();
285 SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
287 $self->{contact}->delete(cascade => 1);
288 SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
290 }) || die($db->error);
292 $self->{contact} = SL::DB::Contact->new();
295 $self->action_edit();
298 sub action_delete_shipto {
301 my $db = $self->{shipto}->db;
303 if ( !$self->{shipto}->shipto_id ) {
304 SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
307 $db->do_transaction(sub {
308 if ( $self->{shipto}->used ) {
309 $self->{shipto}->detach();
310 $self->{shipto}->save(cascade => 1);
311 SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
313 $self->{shipto}->delete(cascade => 1);
314 SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
316 }) || die($db->error);
318 $self->{shipto} = SL::DB::Shipto->new();
321 $self->action_edit();
328 my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer');
330 if ( $::form->{callback} ) {
331 $url .= '&callback='. $::from->escape($::form->{callback});
334 print $::form->redirect_header($url);
338 sub action_search_contact {
341 my $url = 'ct.pl?action=search_contact&db=customer';
343 if ( $::form->{callback} ) {
344 $url .= '&callback='. $::from->escape($::form->{callback});
347 print $::form->redirect_header($url);
351 sub action_get_delivery {
354 my $dbh = $::form->get_standard_dbh();
356 my ($arap, $db, $qty_sign);
357 if ( $self->is_vendor() ) {
360 $qty_sign = ' * -1 AS qty';
367 my $where = ' WHERE 1=1 ';
370 if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
371 $where .= "AND ${arap}.shipto_id = ?";
372 push(@values, $::form->{shipto_id});
375 if ( $::form->{delivery_from} ) {
376 $where .= "AND ${arap}.transdate >= ?";
377 push(@values, conv_date($::form->{delivery_from}));
380 if ( $::form->{delivery_to} ) {
381 $where .= "AND ${arap}.transdate <= ?";
382 push(@values, conv_date($::form->{delivery_to}));
401 ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
404 ON ${arap}.id = i.trans_id
410 ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
413 ORDER BY ${arap}.transdate DESC LIMIT 15";
415 $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
417 $self->render('customer_vendor/get_delivery', { layout => 0 });
420 sub action_ajaj_get_shipto {
426 my $name = 'shipto'. $_;
427 $name => $self->{shipto}->$name;
429 qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
433 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
436 sub action_ajaj_get_contact {
444 my $name = 'cp_'. $_;
446 if ( $_ eq 'birthday' && $self->{contact}->$name ) {
447 $name => $self->{contact}->$name->to_lxoffice;
449 $name => $self->{contact}->$name;
453 id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
454 satphone satfax project street zipcode city privatphone privatemail birthday
459 $data->{contact_cvars} = {
462 if ( $_->config->type eq 'number' ) {
463 $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
465 $_->config->name => $_->value;
470 @{$self->{contact}->cvars_by_config}
475 $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
478 sub action_ajaj_customer_autocomplete {
479 my ($self, %params) = @_;
481 my $limit = $::form->{limit} || 20;
482 my $type = $::form->{type} || {};
483 my $query = { ilike => '%'. $::form->{term} .'%' };
488 $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
491 my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
492 my $value_col = $::form->{column} || 'name';
498 value => $_->can($value_col)->($_),
499 label => $_->displayable_name,
501 customernumber => $_->customernumber,
509 $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
513 return $::form->{db} eq 'vendor';
517 return $::form->{db} eq 'customer';
523 if ( defined($self->{_is_orphaned}) ) {
524 return $self->{_is_orphaned};
527 my $arap = $self->is_vendor ? 'ap' : 'ar';
530 my $cv = $self->is_vendor ? 'vendor' : 'customer';
534 FROM '. $arap .' AS a
535 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
542 JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
546 if ( $self->is_vendor ) {
549 SELECT 1 FROM makemodel mm WHERE mm.make = ?';
553 my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
555 return $self->{_is_orphaned} = !$dummy;
558 sub _instantiate_args {
561 my $curr_employee = SL::DB::Manager::Employee->current;
563 if ( $::form->{cv}->{id} ) {
564 if ( $self->is_vendor() ) {
565 $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
567 $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
570 if ( $self->is_vendor() ) {
571 $self->{cv} = SL::DB::Vendor->new();
573 $self->{cv} = SL::DB::Customer->new();
576 $self->{cv}->assign_attributes(%{$::form->{cv}});
578 foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
579 my $value = $::form->{cv_cvars}->{$cvar->config->name};
581 if ( $cvar->config->type eq 'number' ) {
582 $value = $::form->parse_amount(\%::myconfig, $value);
585 $cvar->value($value);
588 # foreach my $cvar_key (keys(%{$::form->{cv_cvars}})) {
589 # my $cvar_value = $::form->{cv_cvars}->{$cvar_key};
590 # my $cvar = $self->{cv}->cvar_by_name($cvar_key);
591 # $cvar->value($cvar_value);
594 if ( $::form->{note}->{id} ) {
595 $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
597 $self->{note} = SL::DB::Note->new();
599 $self->{note}->assign_attributes(%{$::form->{note}});
600 $self->{note}->created_by($curr_employee->id);
601 $self->{note}->trans_module('ct');
602 # if ( $self->{note}->trans_id != $self->{cv}->id ) {
603 # die($::locale->text('Error'));
607 $self->{note_followup} = SL::DB::FollowUp->new();
608 $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
609 $self->{note_followup}->note($self->{note});
610 $self->{note_followup}->created_by($curr_employee->id);
612 if ( $::form->{shipto}->{shipto_id} ) {
613 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
615 $self->{shipto} = SL::DB::Shipto->new();
617 $self->{shipto}->assign_attributes(%{$::form->{shipto}});
618 $self->{shipto}->module('CT');
619 # if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
620 # die($::locale->text('Error'));
623 if ( $::form->{contact}->{cp_id} ) {
624 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
626 $self->{contact} = SL::DB::Contact->new();
628 $self->{contact}->assign_attributes(%{$::form->{contact}});
629 # if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
630 # die($::locale->text('Error'));
633 foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
634 my $value = $::form->{contact_cvars}->{$cvar->config->name};
636 if ( $cvar->config->type eq 'number' ) {
637 $value = $::form->parse_amount(\%::myconfig, $value);
640 $cvar->value($value);
644 sub _load_customer_vendor {
647 if ( $self->is_vendor() ) {
648 $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
650 $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
653 $self->{note} = SL::DB::Note->new();
654 $self->{note_followup} = SL::DB::FollowUp->new();
656 if ( $::form->{shipto_id} ) {
657 $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
659 if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
660 die($::locale->text('Error'));
663 $self->{shipto} = SL::DB::Shipto->new();
666 if ( $::form->{contact_id} ) {
667 $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
669 if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
670 die($::locale->text('Error'));
673 $self->{contact} = SL::DB::Contact->new();
677 sub _create_customer_vendor {
680 if ( $self->is_vendor() ) {
681 $self->{cv} = SL::DB::Vendor->new();
683 $self->{cv} = SL::DB::Customer->new();
686 $self->{note} = SL::DB::Note->new();
688 $self->{note_followup} = SL::DB::FollowUp->new();
690 $self->{shipto} = SL::DB::Shipto->new();
692 $self->{contact} = SL::DB::Contact->new();
698 my $dbh = $::form->get_standard_dbh();
702 $self->{all_business} = SL::DB::Manager::Business->get_all();
704 $self->{all_employees} = SL::DB::Manager::Employee->get_all();
707 'SELECT DISTINCT(greeting)
709 WHERE greeting IS NOT NULL AND greeting != \'\'
711 SELECT DISTINCT(greeting)
713 WHERE greeting IS NOT NULL AND greeting != \'\'
715 $self->{all_greetings} = [
718 selectall_hashref_query($::form, $dbh, $query)
723 'SELECT DISTINCT(cp_title) AS title
725 WHERE cp_title IS NOT NULL AND cp_title != \'\'
727 $self->{all_titles} = [
730 selectall_hashref_query($::form, $dbh, $query)
734 $self->{all_currencies} = SL::DB::Manager::Currency->get_all();
736 $self->{all_languages} = SL::DB::Manager::Language->get_all();
738 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all();
742 #TODO: ALL_SALESMAN_CUSTOMERS
744 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all();
746 $self->{all_pricegroups} = SL::DB::Manager::Pricegroup->get_all();
749 'SELECT DISTINCT(cp_abteilung) AS department
751 WHERE cp_abteilung IS NOT NULL AND cp_abteilung != \'\'
752 ORDER BY cp_abteilung';
753 $self->{all_departments} = [
755 { $_->{department}; }
756 selectall_hashref_query($::form, $dbh, $query)
760 $self->{contacts} = $self->{cv}->contacts;
761 $self->{contacts} ||= [];
763 $self->{shiptos} = $self->{cv}->shipto;
764 $self->{shiptos} ||= [];
766 $self->{template_args} = {};
768 $::request->{layout}->add_javascripts('autocomplete_customer.js');