1 package SL::BackgroundJob::SetBankAccountsMasterData;
5 use parent qw(SL::BackgroundJob::Base);
10 my ($self, $db_obj) = @_;
12 my $data = $db_obj->data_as_hash;
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)$/;
18 $self->{dry_run} = $data->{dry_run} ? 1 : 0;
19 $self->{overwrite} = $data->{overwrite} ? 1 : 0;
20 $self->{months} = $data->{months} ? $data->{months} : 6;
22 my (@updates_vendor, @updates_customer);
24 foreach my $vc_type (qw(customer vendor)) {
25 my $bank_vc = _get_bank_data_vc(vc => $vc_type, months => $self->{months});
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;
33 next if $vc->can('mandate_date_of_signature') && $vc->mandate_date_of_signature;
34 next if $vc->iban && !$self->{overwrite};
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';
39 next if $self->{dry_run};
41 $vc->update_attributes(iban => $bank_vc_entry->{remote_account_number}, bic => $bank_vc_entry->{remote_bank_code});
45 my $msg = $self->{dry_run} ? "DRY RUN Updates: " : "Updates: ";
46 $msg .= "Customer: " . join (',', @updates_customer) . "\n Vendors: " . join (',', @updates_vendor);
51 sub _get_bank_data_vc {
54 die "Need a defined value for params(vc)" unless $params{vc};
55 die "Need a defined value for params(months)" unless $params{months};
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]*$/;
60 my $vc_id = $params{vc} . '_id';
62 my $arap = $params{vc} eq 'customer' ? 'ar'
63 : $params{vc} eq 'vendor' ? 'ap'
67 my $dbh = SL::DB->client->dbh;
69 SELECT bt.remote_bank_code, bt.remote_account_number, $vc_id
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
79 my $result = selectall_hashref_query($::form, $dbh, $query);
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.
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.
103 By default the job only adds IBAN and BIC for entries which have no
105 The job accepts three parameters:
107 C<dry_run> -> No data will be changed, instead the changes will be
108 written to the job journal.
110 C<months> -> The intervall in months for which invoices are fetched, defaults
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.
118 The job is deactivated by default. Administrators of installations
119 where such a feature is wanted have to create a job entry manually.
123 Jan Büren E<lt>jan@kivitendo.deE<gt>