1 package SL::Controller::CustomerVendor;
 
   4 use parent qw(SL::Controller::Base);
 
  17 use SL::DB::PaymentTerm;
 
  18 use SL::DB::Pricegroup;
 
  23 __PACKAGE__->run_before(
 
  25     $::auth->assert('customer_vendor_edit');
 
  29 __PACKAGE__->run_before(
 
  33     'save_and_ap_transaction',
 
  34     'save_and_ar_transaction',
 
  45 __PACKAGE__->run_before(
 
  46   '_load_customer_vendor',
 
  54 __PACKAGE__->run_before(
 
  55   '_create_customer_vendor',
 
  66     'customer_vendor/form',
 
  67     title => ($self->is_vendor() ? $::locale->text('Add Vendor') : $::locale->text('Add Customer')),
 
  68     %{$self->{template_args}}
 
  77     'customer_vendor/form',
 
  78     title => ($self->is_vendor() ? $::locale->text('Edit Vendor') : $::locale->text('Edit Customer')),
 
  79     %{$self->{template_args}}
 
  87   if ( $self->is_vendor() ) {
 
  88     if ( $self->{cv}->vendornumber ) {
 
  89       $cvs_by_nr = SL::DB::Manager::Vendor->get_all(query => [vendornumber => $self->{cv}->vendornumber]);
 
  93     if ( $self->{cv}->customernumber ) {
 
  94       $cvs_by_nr = SL::DB::Manager::Customer->get_all(query => [customernumber => $self->{cv}->customernumber]);
 
  98   foreach my $entry (@{$cvs_by_nr}) {
 
  99     if( $entry->id != $self->{cv}->id ) {
 
 101         $self->is_vendor() ? $::locale->text('This vendor number is already in use.') : $::locale->text('This customer number is already in use.');
 
 103       $::form->error($msg);
 
 107   $self->{cv}->save(cascade => 1);
 
 109   $self->{contact}->cp_cv_id($self->{cv}->id);
 
 110   if( $self->{contact}->cp_name ne '' || $self->{contact}->cp_givenname ne '' ) {
 
 111     $self->{contact}->save();
 
 114   if( $self->{note}->subject ne '' && $self->{note}->body ne '' ) {
 
 115     $self->{note}->trans_id($self->{cv}->id);
 
 116     $self->{note}->save();
 
 117     $self->{note_followup}->save();
 
 119     $self->{note} = SL::DB::Note->new();
 
 120     $self->{note_followup} = SL::DB::FollowUp->new();
 
 123   $self->{shipto}->trans_id($self->{cv}->id);
 
 124   if( $self->{shipto}->shiptoname ne '' ) {
 
 125     $self->{shipto}->save();
 
 136   $self->action_edit();
 
 139 sub action_save_and_close {
 
 144   my $msg = $self->is_vendor() ? $::locale->text('Vendor saved') : $::locale->text('Customer saved');
 
 145   $::form->redirect($msg);
 
 149   my ($self, $script) = @_;
 
 151   $::auth->assert('general_ledger         | invoice_edit         | vendor_invoice_edit | ' .
 
 152                  ' request_quotation_edit | sales_quotation_edit | sales_order_edit    | purchase_order_edit');
 
 156   my $callback = $::form->escape($::form->{callback}, 1);
 
 157   my $name = $::form->escape($self->{cv}->name, 1);
 
 158   my $db = $self->is_vendor() ? 'vendor' : 'customer';
 
 164     $db .'_id=' . $self->{cv}->id .'&'.
 
 165     $db .'='. $name .'&'.
 
 166     'type='. $::form->{type} .'&'.
 
 167     'callback='. $callback;
 
 169   print $::form->redirect_header($url);
 
 172 sub action_save_and_ar_transaction {
 
 175   $main::auth->assert('general_ledger');
 
 177   $self->_transaction('ar.pl');
 
 180 sub action_save_and_ap_transaction {
 
 183   $main::auth->assert('general_ledger');
 
 185   $self->_transaction('ap.pl');
 
 188 sub action_save_and_invoice {
 
 191   if ( $self->is_vendor() ) {
 
 192     $::auth->assert('vendor_invoice_edit');
 
 194     $::auth->assert('invoice_edit');
 
 197   $::form->{type} = 'invoice';
 
 198   $self->_transaction($self->is_vendor() ? 'ir.pl' : 'is.pl');
 
 201 sub action_save_and_order {
 
 204   if ( $self->is_vendor() ) {
 
 205     $::auth->assert('purchase_order_edit');
 
 207     $::auth->assert('sales_order_edit');
 
 210   $::form->{type} = $self->is_vendor() ? 'purchase_order' : 'sales_order';
 
 211   $self->_transaction('oe.pl');
 
 214 sub action_save_and_rfq {
 
 217   $::auth->assert('request_quotation_edit');
 
 219   $::form->{type} = 'request_quotation';
 
 220   $self->_transaction('oe.pl');
 
 223 sub action_save_and_quotation {
 
 226   $::auth->assert('sales_quotation_edit');
 
 228   $::form->{type} = 'sales_quotation';
 
 229   $self->_transaction('oe.pl');
 
 235   if( !$self->is_orphaned() ) {
 
 236     SL::Helper::Flash::flash('error', $::locale->text('blaabla'));
 
 238     $self->action_edit();
 
 241     $self->{cv}->delete();
 
 245     my $msg = $self->is_vendor() ? $::locale->text('Vendor deleted!') : $::locale->text('Customer deleted!');
 
 246     $::form->redirect($msg);
 
 252 sub action_delete_contact {
 
 255   if ( !$self->{contact}->cp_id ) {
 
 256     SL::Helper::Flash::flash('error', $::locale->text('No contact selected to delete'));
 
 258     if ( $self->{contact}->used ) {
 
 259       $self->{contact}->detach();
 
 260       $self->{contact}->save();
 
 261       SL::Helper::Flash::flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
 
 263       $self->{contact}->delete();
 
 264       SL::Helper::Flash::flash('info', $::locale->text('Contact deleted.'));
 
 267     $self->{contact} = SL::DB::Contact->new();
 
 270   $self->action_edit();
 
 273 sub action_delete_shipto {
 
 276   if ( !$self->{shipto}->shipto_id ) {
 
 277     SL::Helper::Flash::flash('error', $::locale->text('No shipto selected to delete'));
 
 279     if ( $self->{shipto}->used ) {
 
 280       $self->{shipto}->detach();
 
 281       $self->{shipto}->save();
 
 282       SL::Helper::Flash::flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
 
 284       $self->{shipto}->delete();
 
 285       SL::Helper::Flash::flash('info', $::locale->text('Shipto deleted.'));
 
 288     $self->{shipto} = SL::DB::Shipto->new();
 
 291   $self->action_edit();
 
 298   my $url = 'ct.pl?action=search&db='. ($self->is_vendor() ? 'vendor' : 'customer');
 
 300   if ( $::form->{callback} ) {
 
 301     $url .= '&callback='. $::from->escape($::form->{callback});
 
 304   print $::form->redirect_header($url);
 
 308 sub action_search_contact {
 
 311   my $url = 'ct.pl?action=search_contact&db=customer';
 
 313   if ( $::form->{callback} ) {
 
 314     $url .= '&callback='. $::from->escape($::form->{callback});
 
 317   print $::form->redirect_header($url);
 
 321 sub action_get_delivery {
 
 324   my $dbh = $::form->get_standard_dbh();
 
 326   my ($arap, $db, $qty_sign);
 
 327   if ( $self->is_vendor() ) {
 
 330     $qty_sign = ' * -1 AS qty';
 
 338   my $where = ' WHERE 1=1 ';
 
 341   if ( !$self->is_vendor() && $::form->{shipto_id} && $::form->{shipto_id} ne 'all' ) {
 
 342     $where .= "AND ${arap}.shipto_id = ?";
 
 343     push(@values, $::form->{shipto_id});
 
 346   if ( $::form->{delivery_from} ) {
 
 347     $where .= "AND ${arap}.transdate >= ?";
 
 348     push(@values, conv_date($::form->{delivery_from}));
 
 351   if ( $::form->{delivery_to} ) {
 
 352     $where .= "AND ${arap}.transdate <= ?";
 
 353     push(@values, conv_date($::form->{delivery_to}));
 
 372       ON ". ($arap eq 'ar' ? '(ar.shipto_id = s.shipto_id) ' : '(ap.id = s.trans_id) ') ."
 
 375        ON ${arap}.id = i.trans_id
 
 381        ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '')
 
 384      ORDER BY ${arap}.transdate DESC LIMIT 15";
 
 386   $self->{delivery} = selectall_hashref_query($::form, $dbh, $query, @values);
 
 388   $self->render('customer_vendor/get_delivery', { layout => 0 });
 
 391 sub action_ajaj_get_shipto {
 
 397         my $name = 'shipto'. $_;
 
 398         $name => $self->{shipto}->$name;
 
 400       qw(_id name department_1 department_2 street zipcode city country contact phone fax email)
 
 404   $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
 
 407 sub action_ajaj_get_contact {
 
 415         my $name = 'cp_'. $_;
 
 417         if ( $_ eq 'birthday' && $self->{contact}->$name ) {
 
 418           $name => $self->{contact}->$name->to_lxoffice;
 
 421           $name => $self->{contact}->$name;
 
 425         id gender abteilung title position givenname name email phone1 phone2 fax mobile1 mobile2
 
 426         satphone satfax project street zipcode city privatphone privatemail birthday
 
 431   $data->{contact_cvars} = {
 
 434         if ( $_->config->type eq 'number' ) {
 
 435           $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
 
 438           $_->config->name => $_->value;
 
 443         @{$self->{contact}->cvars_by_config}
 
 448   $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
 
 451 sub action_ajaj_customer_autocomplete {
 
 452   my ($self, %params) = @_;
 
 454   my $limit = $::form->{limit} || 20;
 
 455   my $type  = $::form->{type}  || {};
 
 456   my $query = { ilike => '%'. $::form->{term} .'%' };
 
 461     $::form->{column} ? ($::form->{column} => $query) : (or => [ customernumber => $query, name => $query ])
 
 464   my $customers = SL::DB::Manager::Customer->get_all(query => [ @filter ], limit => $limit);
 
 465   my $value_col = $::form->{column} || 'name';
 
 471           value => $_->can($value_col)->($_),
 
 472           label => $_->displayable_name,
 
 474           customernumber => $_->customernumber,
 
 482   $self->render(\SL::JSON::to_json($data), { layout => 0, type => 'json' });
 
 486   return $::form->{db} eq 'vendor';
 
 490   return $::form->{db} eq 'customer';
 
 496   if ( defined($self->{_is_orphaned}) ) {
 
 497     return $self->{_is_orphaned};
 
 500   my $arap      = $self->is_vendor ? 'ap' : 'ar';
 
 503   my $cv = $self->is_vendor ? 'vendor' : 'customer';
 
 507     FROM '. $arap .' AS a
 
 508     JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
 
 515     JOIN '. $cv .' ct ON (a.'. $cv .'_id = ct.id)
 
 519   if ( $self->is_vendor ) {
 
 522       SELECT 1 FROM makemodel mm WHERE mm.make = ?';
 
 526   my ($dummy) = selectrow_query($::form, $::form->get_standard_dbh(), $query, (conv_i($self->{cv}->id)) x $num_args);
 
 528   return $self->{_is_orphaned} = !$dummy;
 
 531 sub _instantiate_args {
 
 534   my $curr_employee = SL::DB::Manager::Employee->current;
 
 536   foreach ( 'cv.creditlimit', 'cv.discount' ) {
 
 537     my ($namespace, $varname) = split('.', $_, 2);
 
 538     $::form->{$namespace}->{$varname} = $::form->parse_amount(\%::myconfig, $::form->{$namespace}->{$varname});
 
 540   $::form->{cv}->{discount} /= 100;
 
 542   if ( $::form->{cv}->{id} ) {
 
 543     if ( $self->is_vendor() ) {
 
 544       $self->{cv} = SL::DB::Vendor->new(id => $::form->{cv}->{id})->load();
 
 547       $self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
 
 551     if ( $self->is_vendor() ) {
 
 552       $self->{cv} = SL::DB::Vendor->new();
 
 555       $self->{cv} = SL::DB::Customer->new();
 
 558   $self->{cv}->assign_attributes(%{$::form->{cv}});
 
 560   foreach my $cvar (@{$self->{cv}->cvars_by_config()}) {
 
 561     my $value = $::form->{cv_cvars}->{$cvar->config->name};
 
 563     if ( $cvar->config->type eq 'number' ) {
 
 564       $value = $::form->parse_amount(\%::myconfig, $value);
 
 567     $cvar->value($value);
 
 570 #  foreach my $cvar_key (keys(%{$::form->{cv_cvars}})) {
 
 571 #    my $cvar_value = $::form->{cv_cvars}->{$cvar_key};
 
 572 #    my $cvar = $self->{cv}->cvar_by_name($cvar_key);
 
 573 #    $cvar->value($cvar_value);
 
 576   if ( $::form->{note}->{id} ) {
 
 577     $self->{note} = SL::DB::Note->new(id => $::form->{note}->{id})->load();
 
 580     $self->{note} = SL::DB::Note->new();
 
 582   $self->{note}->assign_attributes(%{$::form->{note}});
 
 583   $self->{note}->created_by($curr_employee->id);
 
 584   $self->{note}->trans_module('ct');
 
 585   #  if ( $self->{note}->trans_id != $self->{cv}->id ) {
 
 586   #    die($::locale->text('Error'));
 
 590   $self->{note_followup} = SL::DB::FollowUp->new();
 
 591   $self->{note_followup}->assign_attributes(%{$::form->{note_followup}});
 
 592   $self->{note_followup}->note($self->{note});
 
 593   $self->{note_followup}->created_by($curr_employee->id);
 
 595   if ( $::form->{shipto}->{shipto_id} ) {
 
 596     $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto}->{shipto_id})->load();
 
 599     $self->{shipto} = SL::DB::Shipto->new();
 
 601   $self->{shipto}->assign_attributes(%{$::form->{shipto}});
 
 602   $self->{shipto}->module('CT');
 
 603 #  if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
 
 604 #    die($::locale->text('Error'));
 
 607   if ( $::form->{contact}->{cp_id} ) {
 
 608     $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
 
 611     $self->{contact} = SL::DB::Contact->new();
 
 613   $self->{contact}->assign_attributes(%{$::form->{contact}});
 
 614 #  if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
 
 615 #    die($::locale->text('Error'));
 
 618   foreach my $cvar (@{$self->{contact}->cvars_by_config()}) {
 
 619     my $value = $::form->{contact_cvars}->{$cvar->config->name};
 
 621     if ( $cvar->config->type eq 'number' ) {
 
 622       $value = $::form->parse_amount(\%::myconfig, $value);
 
 625     $cvar->value($value);
 
 629 sub _load_customer_vendor {
 
 632   if ( $self->is_vendor() ) {
 
 633     $self->{cv} = SL::DB::Vendor->new(id => $::form->{id})->load();
 
 636     $self->{cv} = SL::DB::Customer->new(id => $::form->{id})->load();
 
 639   $self->{note} = SL::DB::Note->new();
 
 640   $self->{note_followup} = SL::DB::FollowUp->new();
 
 642   if ( $::form->{shipto_id} ) {
 
 643     $self->{shipto} = SL::DB::Shipto->new(shipto_id => $::form->{shipto_id})->load();
 
 645     if ( $self->{shipto}->trans_id != $self->{cv}->id ) {
 
 646       die($::locale->text('Error'));
 
 650     $self->{shipto} = SL::DB::Shipto->new();
 
 653   if ( $::form->{contact_id} ) {
 
 654     $self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact_id})->load();
 
 656     if ( $self->{contact}->cp_cv_id != $self->{cv}->id ) {
 
 657       die($::locale->text('Error'));
 
 661     $self->{contact} = SL::DB::Contact->new();
 
 665 sub _create_customer_vendor {
 
 668   if ( $self->is_vendor() ) {
 
 669     $self->{cv} = SL::DB::Vendor->new();
 
 672     $self->{cv} = SL::DB::Customer->new();
 
 675   $self->{note} = SL::DB::Note->new();
 
 677   $self->{note_followup} = SL::DB::FollowUp->new();
 
 679   $self->{shipto} = SL::DB::Shipto->new();
 
 681   $self->{contact} = SL::DB::Contact->new();
 
 687   my $dbh = $::form->get_standard_dbh();
 
 691   $self->{all_business} = SL::DB::Manager::Business->get_all();
 
 693   $self->{all_employees} = SL::DB::Manager::Employee->get_all();
 
 696     'SELECT DISTINCT(greeting)
 
 698      WHERE greeting IS NOT NULL AND greeting != \'\'
 
 700        SELECT DISTINCT(greeting)
 
 702        WHERE greeting IS NOT NULL AND greeting != \'\'
 
 704   $self->{all_greetings} = [
 
 707       selectall_hashref_query($::form, $dbh, $query)
 
 712     'SELECT DISTINCT(cp_title) AS title
 
 714      WHERE cp_title IS NOT NULL AND cp_title != \'\'
 
 716   $self->{all_titles} = [
 
 719       selectall_hashref_query($::form, $dbh, $query)
 
 726   my $curr = selectall_hashref_query($::form, $dbh, $query)->[0]->{curr};
 
 727   my @currencies = grep(
 
 734   $self->{all_currencies} = \@currencies;
 
 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   $self->{cv}->discount($self->{cv}->discount * 100);
 
 771   $::request->{layout}->add_javascripts('autocomplete_customer.js');