Revision ca18593d
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 overwrite" 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 valid vc param, got" . $params{vc} unless $params{vc} && $params{vc} =~ /^(customer|vendor)$/; |
|
55 |
die "Need valid months param, got" . $params{months} unless $params{months} && $params{months} =~ /^[1-9][0-9]*$/; |
|
56 |
|
|
57 |
my $vc_id = $params{vc} . '_id'; |
|
58 |
my $arap = $params{vc} eq 'customer' ? 'ar' |
|
59 |
: $params{vc} eq 'vendor' ? 'ap' |
|
60 |
: undef; |
|
61 |
|
|
62 |
die "Invalid state" unless $arap; |
|
63 |
|
|
64 |
my $dbh = SL::DB->client->dbh; |
|
65 |
my $query = <<SQL; |
|
66 |
SELECT bt.remote_bank_code, bt.remote_account_number, $vc_id |
|
67 |
FROM $arap |
|
68 |
LEFT JOIN bank_transaction_acc_trans bta on id = bta.${arap}_id |
|
69 |
LEFT JOIN bank_transactions bt on bt.id = bta.bank_transaction_id |
|
70 |
WHERE $vc_id in (select distinct $vc_id from $arap where transdate > now() - interval '$params{months} month' AND paid = amount) |
|
71 |
AND bta.${arap}_id is not NULL |
|
72 |
GROUP BY bt.remote_account_number,bt.remote_bank_code, $vc_id |
|
73 |
ORDER BY $vc_id |
|
74 |
SQL |
|
75 |
|
|
76 |
my $result = selectall_hashref_query($::form, $dbh, $query); |
|
77 |
|
|
78 |
return $result; |
|
79 |
} |
|
80 |
|
|
81 |
1; |
|
82 |
|
|
83 |
__END__ |
|
84 |
|
|
85 |
=encoding utf8 |
|
86 |
|
|
87 |
=head1 NAME |
|
88 |
|
|
89 |
SL::BackgroundJob::SetBankAccountsMasterData — |
|
90 |
Background job for setting IBAN and BIC for Customers and Vendors |
|
91 |
regarding to the booked bank transactions for this companies. |
|
92 |
|
|
93 |
=head1 SYNOPSIS |
|
94 |
|
|
95 |
This background job searches all invoices which are paid by bank transactions |
|
96 |
and gets the IBAN and BIC for those transactions. |
|
97 |
If the IBAN and BICs in the master data are not yet set, they will be |
|
98 |
set via this background jobs. |
|
99 |
|
|
100 |
By default the job only adds IBAN and BIC for entries which have no |
|
101 |
manual entry before. |
|
102 |
The job accepts three parameters: |
|
103 |
|
|
104 |
C<dry_run> -> No data will be changed, instead the changes will be |
|
105 |
written to the job journal. |
|
106 |
|
|
107 |
C<months> -> The intervall in months for which invoices are fetched, defaults |
|
108 |
to 6 (months). |
|
109 |
|
|
110 |
C<overwrite> -> If set to 1 values in the master data will be changed |
|
111 |
even if they are already exists, except if a mandate_date_of_signature is |
|
112 |
found. Those data sets won't be changed because kivitendo assumes that there |
|
113 |
is a direct debit contract for exactly this account with this specific company. |
|
114 |
|
|
115 |
The job is deactivated by default. Administrators of installations |
|
116 |
where such a feature is wanted have to create a job entry manually. |
|
117 |
|
|
118 |
=head1 AUTHOR |
|
119 |
|
|
120 |
Jan Büren E<lt>jan@kivitendo.deE<gt> |
|
121 |
|
|
122 |
=cut |
Auch abrufbar als: Unified diff
Neuer Background-Job, um IBAN und BIC aus Kontobuchungen zu setzen