]> wagnertech.de Git - mfinanz.git/blob - SL/BackgroundJob/SetBankAccountsMasterData.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.git] / SL / BackgroundJob / SetBankAccountsMasterData.pm
1 package SL::BackgroundJob::SetBankAccountsMasterData;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use SL::DBUtils;
8
9 sub run {
10   my ($self, $db_obj)     = @_;
11
12   my $data                = $db_obj->data_as_hash;
13
14   die "No valid integer for months"  if $data->{months}    && $data->{months}     !~ /^[1-9][0-9]*$/;
15   die "No valid value for overwrite" if $data->{overwrite} && $data->{overwrite}  !~ /^(0|1)$/;
16   die "No valid value for dry_run"   if $data->{dry_run}   && $data->{dry_run}    !~ /^(0|1)$/;
17
18   $self->{dry_run}   = $data->{dry_run}    ? 1               : 0;
19   $self->{overwrite} = $data->{overwrite}  ? 1               : 0;
20   $self->{months}    = $data->{months}     ? $data->{months} : 6;
21
22   my (@updates_vendor, @updates_customer);
23
24   foreach my $vc_type (qw(customer vendor)) {
25     my $bank_vc = _get_bank_data_vc(vc => $vc_type, months => $self->{months});
26
27     foreach my $bank_vc_entry (@{ $bank_vc }) {
28       if ($bank_vc_entry->{remote_account_number}) {
29         my $vc =  $vc_type eq 'customer'
30                 ? SL::DB::Customer->new(id => $bank_vc_entry->{customer_id})->load
31                 : SL::DB::Vendor  ->new(id => $bank_vc_entry->{vendor_id})  ->load;
32
33         next if $vc->can('mandate_date_of_signature') && $vc->mandate_date_of_signature;
34         next if $vc->iban && !$self->{overwrite};
35
36         push @updates_customer, $vc->name . " -> " . $bank_vc_entry->{remote_account_number} if $vc_type eq 'customer';
37         push @updates_vendor,   $vc->name . " -> " . $bank_vc_entry->{remote_account_number} if $vc_type eq 'vendor';
38
39         next if $self->{dry_run};
40
41         $vc->update_attributes(iban => $bank_vc_entry->{remote_account_number}, bic => $bank_vc_entry->{remote_bank_code});
42       }
43     }
44   }
45   my $msg = $self->{dry_run} ? "DRY RUN Updates: " : "Updates: ";
46   $msg   .= "Customer: " . join (',', @updates_customer) . "\n Vendors: "  . join (',', @updates_vendor);
47
48   return $msg;
49 }
50
51 sub _get_bank_data_vc {
52   my (%params) = @_;
53
54   die "Need a defined value for params(vc)"     unless $params{vc};
55   die "Need a defined value for params(months)" unless $params{months};
56
57   die "Need valid vc param, got:"     . $params{vc}     unless $params{vc}     =~ /^(customer|vendor)$/;
58   die "Need valid months param, got:" . $params{months} unless $params{months} =~ /^[1-9][0-9]*$/;
59
60   my $vc_id = $params{vc} . '_id';
61
62   my $arap  =   $params{vc} eq 'customer' ? 'ar'
63               : $params{vc} eq 'vendor'   ? 'ap'
64               : undef;
65
66
67   my $dbh = SL::DB->client->dbh;
68   my $query = <<SQL;
69   SELECT bt.remote_bank_code, bt.remote_account_number, $vc_id
70   FROM $arap
71   LEFT JOIN bank_transaction_acc_trans bta ON id = bta.${arap}_id
72   LEFT JOIN bank_transactions bt ON bt.id = bta.bank_transaction_id
73   WHERE $vc_id IN (SELECT DISTINCT $vc_id FROM $arap WHERE transdate > now() - interval '$params{months} month' AND paid = amount)
74   AND bta.${arap}_id IS NOT NULL
75   GROUP BY bt.remote_account_number,bt.remote_bank_code, $vc_id
76   ORDER BY $vc_id
77 SQL
78
79   my $result = selectall_hashref_query($::form, $dbh, $query);
80
81   return $result;
82 }
83
84 1;
85
86 __END__
87
88 =encoding utf8
89
90 =head1 NAME
91
92 SL::BackgroundJob::SetBankAccountsMasterData —
93 Background job for setting IBAN and BIC for Customers and Vendors
94 regarding to the booked bank transactions for this companies.
95
96 =head1 SYNOPSIS
97
98 This background job searches all invoices which are paid by bank transactions
99 and gets the IBAN and BIC for those transactions.
100 If the IBAN and BICs in the master data are not yet set, they will be
101 set via this background jobs.
102
103 By default the job only adds IBAN and BIC for entries which have no
104 manual entry before.
105 The job accepts three parameters:
106
107 C<dry_run> -> No data will be changed, instead the changes will be
108 written to the job journal.
109
110 C<months> -> The intervall in months for which invoices are fetched, defaults
111 to 6 (months).
112
113 C<overwrite> -> If set to 1 values in the master data will be changed
114 even if they are already exists, except if a mandate_date_of_signature is
115 found. Those data sets won't be changed because kivitendo assumes that there
116 is a direct debit contract for exactly this account with this specific company.
117
118 The job is deactivated by default. Administrators of installations
119 where such a feature is wanted have to create a job entry manually.
120
121 =head1 AUTHOR
122
123 Jan Büren E<lt>jan@kivitendo.deE<gt>
124
125 =cut