Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 0eebc84f

Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt

  • ID 0eebc84fe78ab6d414b9a9e884dea46f63d951cf
  • Vorgänger d9745d30
  • Nachfolger 6a73c38f

BJ zum Belege synchronisieren angepasst, auch EmailJournal nutzen

Alle Emails in Unterordnern werden synchronisiert und Emaildateien zu
den Belegen gespeichert. Alle unter Ordner, die nicht zu offenen Belegen
gehören, werden gelöscht.

Unterschiede anzeigen:

SL/BackgroundJob/CleanUpEmailSubfolders.pm
1
package SL::BackgroundJob::CleanUpEmailSubfolders;
2

  
3
use strict;
4
use warnings;
5

  
6
use parent qw(SL::BackgroundJob::Base);
7

  
8
use SL::IMAPClient;
9

  
10
sub clean_up_subfolders {
11
  my ($self) = @_;
12
  my $imap_client = SL::IMAPClient->new();
13

  
14
  my $open_sales_orders = SL::DB::Manager::Order->get_all(
15
    query => [
16
      vendor_id => undef,
17
      closed => 0,
18
    ],
19
  );
20

  
21
  my $email_import = $imap_client->clean_up_subfolders($open_sales_orders);
22
  return unless $email_import;
23

  
24
  $self->{job_obj}->set_data(last_email_import_id => $email_import->id)->save;
25
}
26

  
27
sub run {
28
  my ($self, $job_obj) = @_;
29
  $self->{job_obj} = $job_obj;
30

  
31
  $self->clean_up_subfolders();
32

  
33
  return;
34
}
35

  
36
1;
37

  
38
__END__
39

  
40
=encoding utf8
41

  
42
=head1 NAME
43

  
44
SL::BackgroundJob::CleanUpEmailSubfolders - Background job for removing all email
45
subfolders except open records.
46

  
47
=head1 SYNOPSIS
48

  
49
This background job syncs all emails in subfolders and adds emails files to the
50
corresponding record. After that is removes all subfolders except for open
51
records.
52

  
53
=head1 BUGS
54

  
55
Nothing here yet.
56

  
57
=head1 AUTHOR
58

  
59
Tamino Steinert E<lt>tamino.steinert@tamino.stE<gt>
60

  
61
=cut
SL/BackgroundJob/CleanUpRecordEmailFolders.pm
1
package SL::BackgroundJob::CleanUpRecordEmailFolders;
2

  
3
use strict;
4
use warnings;
5

  
6
use parent qw(SL::BackgroundJob::Base);
7

  
8
use SL::IMAPClient;
9

  
10
sub clean_up_record_folders {
11
  my ($self) = @_;
12
  my $imap_client = SL::IMAPClient->new();
13

  
14
  my $open_sales_orders = SL::DB::Manager::Order->get_all(
15
    query => [
16
      vendor_id => undef,
17
      closed => 0,
18
    ],
19
  );
20

  
21
  $imap_client->clean_up_record_folders($open_sales_orders);
22
}
23

  
24
sub run {
25
  my ($self, $job_obj) = @_;
26
  $self->{job_obj} = $job_obj;
27

  
28
  $self->clean_up_record_folders();
29

  
30
  return;
31
}
32

  
33
1;
34

  
35
__END__
36

  
37
=encoding utf8
38

  
39
=head1 NAME
40

  
41
SL::BackgroundJob::CleanUpRecordEmailFolders - Background job for removing email folders of closed records.
42

  
43
=head1 SYNOPSIS
44

  
45
This background job syncs all emails to emails files to the corresponding
46
record and than removes email folders of closed records.
47

  
48
=head1 BUGS
49

  
50
Nothing here yet.
51

  
52
=head1 AUTHOR
53

  
54
Tamino Steinert E<lt>tamino.steinert@tamino.stE<gt>
55

  
56
=cut
SL/IMAPClient.pm
244 244
  }
245 245
}
246 246

  
247
sub update_email_files_for_all_records {
247
sub update_email_subfolders_and_files_for_records {
248 248
  my ($self) = @_;
249
  my $record_folder_path = $self->{base_folder};
249
  my $base_folder_path = $self->{base_folder};
250
  my $base_folder_string = $self->get_folder_string_from_path($base_folder_path);
250 251

  
251
  my $subfolder_strings = $self->{imap_client}->folders($record_folder_path)
252
  my $folder_strings = $self->{imap_client}->folders($base_folder_string)
252 253
    or die "Could not get folders via IMAP: $@\n";
253
  my @record_folder_strings = grep { $_ ne $record_folder_path }
254
    @$subfolder_strings;
254
  my @subfolder_strings = grep { $_ ne $base_folder_string } @$folder_strings;
255

  
256
  # Store all emails in journal
257
  my $email_import =
258
    $self->_update_emails_from_folder_strings($base_folder_path, \@subfolder_strings);
255 259

  
256
  foreach my $record_folder_string (@record_folder_strings) {
257
    my $ilike_folder_path = $self->get_ilike_folder_path_from_string($record_folder_string);
260
  # Store the emails to the records
261
  foreach my $subfolder_string (@subfolder_strings) {
262
    my $ilike_folder_path = $self->get_ilike_folder_path_from_string($subfolder_string);
258 263
    my (
259 264
      $ilike_record_folder_path, # is greedily matched
260 265
      $ilike_customer_number, # no spaces allowed
......
266 271
    my $record_type = $RECORD_FOLDER_TO_TYPE{$record_folder};
267 272
    next unless $record_type;
268 273

  
274
    # TODO make it generic for all records
269 275
    my $is_quotation = $record_type eq 'sales_quotation' ? 1 : 0;
270 276
    my $number_field = $is_quotation ? 'quonumber' : 'ordnumber';
271
    my $order = SL::DB::Manager::Order->get_first(
277
    my $record = SL::DB::Manager::Order->get_first(
272 278
      query => [
273 279
        and => [
274 280
          vendor_id => undef,
......
276 282
          $number_field => { ilike => $ilike_record_number },
277 283
        ],
278 284
    ]);
279
    next unless $order;
280

  
281
    $self->update_email_files_for_record($order);
285
    next unless $record;
286
    $self->update_email_files_for_record($record);
282 287
  }
288

  
289
  return ($email_import, \@subfolder_strings);
283 290
}
284 291

  
285 292
sub create_folder {
......
318 325
  return;
319 326
}
320 327

  
321
sub clean_up_record_folders {
328
sub clean_up_subfolders {
322 329
  my ($self, $active_records) = @_;
323 330
  my $record_folder_path = $self->{base_folder};
324 331

  
325
  $self->update_email_files_for_all_records();
326

  
327
  my $base_folder_string = $self->get_folder_string_from_path($record_folder_path);
328
  my @folders = $self->{imap_client}->folders($base_folder_string)
329
    or die "Could not get folders via IMAP: $@\n";
332
  my ($email_import, $synced_folders) =
333
    $self->update_email_subfolders_and_files_for_records();
330 334

  
331 335
  my @active_folders = map { $self->_get_folder_string_for_record($_) }
332 336
    @$active_records;
333
  push @active_folders, $base_folder_string;
334 337

  
335 338
  my %keep_folder = map { $_ => 1 } @active_folders;
336
  my @folders_to_delete = grep { !$keep_folder{$_} } @folders;
339
  my @folders_to_delete = grep { !$keep_folder{$_} } @$synced_folders;
337 340

  
338 341
  foreach my $folder (@folders_to_delete) {
339 342
    $self->{imap_client}->delete($folder)
340 343
      or die "Could not delete IMAP folder '$folder': $@\n";
341 344
  }
345

  
346
  return $email_import;
342 347
}
343 348

  
344 349
sub _get_folder_string_for_record {
......
501 506
  Updates the email files for a record. Checks which emails are missing and
502 507
  fetches these from the IMAP server.
503 508

  
504
=item C<update_email_files_for_all_records>
505
  
506
    Updates the email files for all records. Checks which emails are missing and
507
    fetches these from the IMAP server.
509
=item C<update_email_subfolders_and_files_for_records>
510

  
511
    Updates all subfolders and the email files for all records.
508 512

  
509 513
=item C<create_folder>
510 514

  
......
518 522
  The folder string is encoded in IMAP-UTF-7.
519 523

  
520 524
=item C<get_ilike_folder_path_from_string>
521
  
525

  
522 526
  Converts a folder string to a folder path. The folder path is like path
523 527
  on unix filesystem. The folder string is the path on the IMAP server.
524 528
  The folder string is encoded in IMAP-UTF-7. It can happend that
525 529
  C<get_folder_string_from_path> and C<get_ilike_folder_path_from_string>
526
  don't cancel each other out. This is because the IMAP server has a different
527
  separator than the unix filesystem. The changes are made so that a ILIKE
528
  query on the database works.
530
  don't cancel each other out. This is because the IMAP server can has a
531
  different Ieparator than the unix filesystem. The changes are made so that a
532
  ILIKE query on the database works.
529 533

  
530 534
=item C<create_folder_for_record>
531 535

  
......
534 538
  e.g. INBOX/1234 Testkunde/Angebot/123
535 539
  If the folder already exists, nothing happens.
536 540

  
537
=item C<clean_up_record_folders>
541
=item C<clean_up_subfolders>
538 542

  
539
  Gets a list of acitve records. First syncs the folders on the IMAP server with
540
  the corresponding record, by creating email files. Then deletes all folders
541
  which are not corresponding to an active record.
543
  Gets a list of acitve records. Syncs all subfolders and add email files to
544
  the records. Then deletes all subfolders which are not corresponding to an
545
  active record.
542 546

  
543 547
=item C<_get_folder_string_for_record>
544 548

  
......
556 560

  
557 561
=head1 BUGS
558 562

  
559
Nothing here yet.
563
The mapping from record to email folder is not bijective. If the record or
564
customer number has special characters, the mapping can fail. Read
565
C<get_ilike_folder_path_from_string> for more information.
560 566

  
561 567
=head1 AUTHOR
562 568

  

Auch abrufbar als: Unified diff