Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 48ef5a7c

Von Tamino Steinert vor 8 Monaten hinzugefügt

  • ID 48ef5a7cdf3e4e8a6163c3f9bda6f2a1e7fbcb04
  • Vorgänger b8dfb10a
  • Nachfolger 6e00af5b

EmailJournal: Suche nach E-Mail von Kunde und Lieferant getrennt

Unterschiede anzeigen:

SL/Controller/EmailJournal.pm
254 254
  my @record_types_with_info = $self->get_record_types_with_info();
255 255
  my %record_types_to_text   = $self->get_record_types_to_text();
256 256

  
257
  my $customer_vendor = $self->find_customer_vendor_from_email($self->entry);
258
  my $cv_type = $customer_vendor && $customer_vendor->is_vendor ? 'vendor' : 'customer';
259

  
260
  my $record_types = $self->record_types_for_customer_vendor_type_and_action($cv_type, 'workflow_record');
257
  my $customer = $self->find_customer_vendor_from_email('customer', $self->entry);
258
  my $vendor   = $self->find_customer_vendor_from_email('vendor'  , $self->entry);
259
  my $cv_type_found;
260
  $cv_type_found   = 'vendor' if $self->entry->record_type eq 'ap_transaction';
261
  $cv_type_found ||= 'vendor' if defined $vendor;
262
  $cv_type_found ||= 'customer';
263

  
264
  my $record_types = $self->record_types_for_customer_vendor_type_and_action(
265
    $cv_type_found, 'workflow_record'
266
  );
261 267

  
262 268
  $self->setup_show_action_bar;
263
  my $cv_type_found = $customer_vendor && $customer_vendor->is_vendor ? 'vendor' : 'customer';
264
  # overwrite on record_type
265
  $cv_type_found = 'vendor' if $self->entry->record_type eq 'ap_transaction';
266 269
  $self->render(
267 270
    'email_journal/show',
268 271
    title                  => $::locale->text('View email'),
269
    CUSTOMER_VENDOR        => $customer_vendor,
272
    CUSTOMER               => $customer,
273
    VENDOR                 => $vendor,
270 274
    CV_TYPE_FOUND          => $cv_type_found,
271 275
    RECORD_TYPES_WITH_INFO => \@record_types_with_info,
272 276
    RECORD_TYPES_TO_TEXT   => \%record_types_to_text,
......
607 611
}
608 612

  
609 613
sub find_customer_vendor_from_email {
610
  my ($self, $email_journal, $cv_type) = @_;
614
  my ($self, $cv_type, $email_journal) = @_;
615

  
616
  my $manager = $cv_type eq 'customer' ? 'SL::DB::Manager::Customer'
617
              : $cv_type eq 'vendor'   ? 'SL::DB::Manager::Vendor'
618
              : die "No valid customer vendor option: $cv_type";
619

  
611 620
  my $email_address = $email_journal->from;
612 621
  $email_address =~ s/.*<(.*)>/$1/; # address can look like "name surname <email_address>"
613 622

  
614 623
  # Separate query otherwise cv without contacts and shipto is not found
615 624
  my $customer_vendor;
616
  foreach my $manager (qw(SL::DB::Manager::Customer SL::DB::Manager::Vendor)) {
617
    $customer_vendor ||= $manager->get_first(
618
      where => [
619
        or => [
620
          email => $email_address,
621
          cc    => $email_address,
622
          bcc   => $email_address,
623
        ],
625
  $customer_vendor ||= $manager->get_first(
626
    where => [
627
      or => [
628
        email => $email_address,
629
        cc    => $email_address,
630
        bcc   => $email_address,
624 631
      ],
625
    );
626
    $customer_vendor ||= $manager->get_first(
627
      where => [
628
        or => [
629
          'contacts.cp_email' => $email_address,
630
          'contacts.cp_privatemail' => $email_address,
631
        ],
632
    ],
633
  );
634
  $customer_vendor ||= $manager->get_first(
635
    where => [
636
      or => [
637
        'contacts.cp_email' => $email_address,
638
        'contacts.cp_privatemail' => $email_address,
632 639
      ],
633
      with_objects => [ 'contacts'],
634
    );
640
    ],
641
    with_objects => [ 'contacts'],
642
  );
643
  $customer_vendor ||= $manager->get_first(
644
    where => [
645
      or => [
646
        'shipto.shiptoemail' => $email_address,
647
      ],
648
    ],
649
    with_objects => [ 'shipto' ],
650
  );
651
  if ($manager eq 'SL::DB::Manager::Customer') {
635 652
    $customer_vendor ||= $manager->get_first(
636 653
      where => [
637 654
        or => [
638
          'shipto.shiptoemail' => $email_address,
655
          'additional_billing_addresses.email' => $email_address,
639 656
        ],
640 657
      ],
641
      with_objects => [ 'shipto' ],
658
      with_objects => [ 'additional_billing_addresses' ],
642 659
    );
643
    if ($manager eq 'SL::DB::Manager::Customer') {
644
      $customer_vendor ||= $manager->get_first(
645
        where => [
646
          or => [
647
            'additional_billing_addresses.email' => $email_address,
648
          ],
649
        ],
650
        with_objects => [ 'additional_billing_addresses' ],
651
      );
652
    }
653 660
  }
654 661

  
655 662
  # if no exact match is found search for domain and match only on one hit
......
657 664
    my $email_domain = $email_address;
658 665
    $email_domain =~ s/.*@(.*)/$1/;
659 666
    my @domain_hits_cusotmer_vendor = ();
660
    foreach my $manager (qw(SL::DB::Manager::Customer SL::DB::Manager::Vendor)) {
661
      my @domain_hits = ();
662
      push @domain_hits, @{$manager->get_all(
663
        where => [
664
          or => [
665
            email => {ilike => "%$email_domain"},
666
            cc    => {ilike => "%$email_domain"},
667
            bcc   => {ilike => "%$email_domain"},
668
          ],
667
    my @domain_hits = ();
668
    push @domain_hits, @{$manager->get_all(
669
      where => [
670
        or => [
671
          email => {ilike => "%$email_domain"},
672
          cc    => {ilike => "%$email_domain"},
673
          bcc   => {ilike => "%$email_domain"},
669 674
        ],
670
      )};
671
      push @domain_hits, @{$manager->get_all(
672
        where => [
673
          or => [
674
            'contacts.cp_email'       => {ilike => "%$email_domain"},
675
            'contacts.cp_privatemail' => {ilike => "%$email_domain"},
676
          ],
675
      ],
676
    )};
677
    push @domain_hits, @{$manager->get_all(
678
      where => [
679
        or => [
680
          'contacts.cp_email'       => {ilike => "%$email_domain"},
681
          'contacts.cp_privatemail' => {ilike => "%$email_domain"},
677 682
        ],
678
        with_objects => [ 'contacts'],
679
      )};
680
      push @domain_hits, @{$manager->get_all(
681
        where => [
682
          or => [
683
            'shipto.shiptoemail' => {ilike => "%$email_domain"},
684
          ],
683
      ],
684
      with_objects => [ 'contacts'],
685
    )};
686
    push @domain_hits, @{$manager->get_all(
687
      where => [
688
        or => [
689
          'shipto.shiptoemail' => {ilike => "%$email_domain"},
685 690
        ],
686
        with_objects => [ 'shipto' ],
687
      )};
691
      ],
692
      with_objects => [ 'shipto' ],
693
    )};
694
    push @domain_hits, @{$manager->get_all(
695
      where => [
696
        or => [
697
          'shipto.shiptoemail' => {ilike => "%$email_domain"},
698
        ],
699
      ],
700
      with_objects => [ 'shipto' ],
701
    )};
702
    if ($manager eq 'SL::DB::Manager::Customer') {
688 703
      push @domain_hits, @{$manager->get_all(
689 704
        where => [
690 705
          or => [
691
            'shipto.shiptoemail' => {ilike => "%$email_domain"},
706
            'additional_billing_addresses.email' => {ilike => "%$email_domain"},
692 707
          ],
693 708
        ],
694
        with_objects => [ 'shipto' ],
709
        with_objects => [ 'additional_billing_addresses' ],
695 710
      )};
696
      if ($manager eq 'SL::DB::Manager::Customer') {
697
        push @domain_hits, @{$manager->get_all(
698
          where => [
699
            or => [
700
              'additional_billing_addresses.email' => {ilike => "%$email_domain"},
701
            ],
702
          ],
703
          with_objects => [ 'additional_billing_addresses' ],
704
        )};
705
      }
706
      # get every customer_vendor only once
707
      my %id_to_customer_vendor = ();
708
      $id_to_customer_vendor{$_->id} = $_ for @domain_hits;
709
      push @domain_hits_cusotmer_vendor, $id_to_customer_vendor{$_} for keys %id_to_customer_vendor;
710 711
    }
711

  
712
    if (scalar @domain_hits_cusotmer_vendor == 1) {
713
      $customer_vendor = $domain_hits_cusotmer_vendor[0];
712
    # update on only one unique customer_vendor
713
    if (scalar @domain_hits) {
714
      my $first_customer_vendor = $domain_hits[0];
715
      unless (any {$_->id != $first_customer_vendor->id} @domain_hits) {
716
        $customer_vendor = $first_customer_vendor;
717
      }
714 718
    }
715 719
  }
716 720

  

Auch abrufbar als: Unified diff