]> wagnertech.de Git - mfinanz.git/blob - SL/BackgroundJob/ShopwareSetPaid.pm
kivitendo 3.9.2-0.2
[mfinanz.git] / SL / BackgroundJob / ShopwareSetPaid.pm
1 package SL::BackgroundJob::ShopwareSetPaid;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use SL::DB::Invoice;
8 use SL::Locale::String qw(t8);
9 use SL::Shop;
10
11 sub run {
12   my ($self, $db_obj)     = @_;
13   my $data       = $db_obj->data_as_hash;
14
15   my $dry_run = ($data->{dry_run})  ? 1 : 0;
16   my $today   = ($data->{datepaid}) ? DateTime->from_kivitendo($data->{datepaid}) : DateTime->today_local;
17
18   my $paid_invoices = SL::DB::Manager::Invoice->get_all(query => [ and => [ datepaid => { ge => $today }, amount  =>   \'paid'  ]]);
19
20   my @shoporders;
21   foreach my $invoice (@{ $paid_invoices }) {
22     # check if we have a shop order invoice
23     my @linked_shop_orders = $invoice->linked_records(
24                                from => 'ShopOrder',
25                                via  => ['DeliveryOrder','Order'],
26                              );
27     my $shop_order = $linked_shop_orders[0][0];
28     if ( $shop_order ) {
29        #do update
30        push @shoporders, $shop_order->shop_ordernumber;
31        next if $dry_run;
32        my $shop_config = SL::DB::Manager::Shop->get_first( query => [ id => $shop_order->shop_id ] );
33        my $shop = SL::Shop->new( config => $shop_config );
34        $shop->connector->set_order_transaction_status($shop_order->shop_ordernumber, "paid");
35     }
36   }
37   # nothing found
38   return t8("No valid invoice(s) found") if scalar @shoporders == 0;
39
40   my $message = t8("The following Shop Orders: ") . join (', ', @shoporders);
41   $message   .= $dry_run ? t8(" would be set to the state 'paid'") : t8(" have been set to the state 'paid'");
42
43   return $message;
44 }
45
46 1;
47
48 __END__
49
50 =encoding utf8
51
52 =head1 NAME
53
54 SL::BackgroundJob::ShopwareSetPaid
55
56 Background job for setting the shopware state paid for shopware orders
57
58 With the default values the job should be run once a day after all payments are booked.
59
60 =head1 SYNOPSIS
61
62 Accepts two params 'dry_run' and 'datepaid'.
63 If 'dry_run' has trueish vale, the job simply returns what would have been done in the Background Job Journal.
64 If 'datepaid' is set all Invoices with a datepaid higher or equal the 'datepaid' value are checked. Date should be
65 in the correct system locales. If ommitted datepaid will be the current date.
66
67
68 =head1 AUTHOR
69
70 Jan Büren
71
72 =cut