Bankerweiterung - Zwischenstand, erster Entwurf
[kivitendo-erp.git] / SL / DB / PurchaseInvoice.pm
1 package SL::DB::PurchaseInvoice;
2
3 use strict;
4
5 use Carp;
6
7 use SL::DB::MetaSetup::PurchaseInvoice;
8 use SL::DB::Manager::PurchaseInvoice;
9 use SL::DB::Helper::AttrHTML;
10 use SL::DB::Helper::LinkedRecords;
11 use SL::Locale::String qw(t8);
12
13 # The calculator hasn't been adjusted for purchase invoices yet.
14 # use SL::DB::Helper::PriceTaxCalculator;
15
16 __PACKAGE__->meta->add_relationship(
17   invoiceitems   => {
18     type         => 'one to many',
19     class        => 'SL::DB::InvoiceItem',
20     column_map   => { id => 'trans_id' },
21     manager_args => { with_objects => [ 'part' ] }
22   },
23   sepa_export_items => {
24     type            => 'one to many',
25     class           => 'SL::DB::SepaExportItem',
26     column_map      => { id => 'ap_id' },
27     manager_args    => { with_objects => [ 'sepa_export' ] }
28   },
29   custom_shipto     => {
30     type            => 'one to one',
31     class           => 'SL::DB::Shipto',
32     column_map      => { id => 'trans_id' },
33     query_args      => [ module => 'AP' ],
34   },
35   transactions   => {
36     type         => 'one to many',
37     class        => 'SL::DB::AccTransaction',
38     column_map   => { id => 'trans_id' },
39     manager_args => { with_objects => [ 'chart' ],
40                       sort_by      => 'acc_trans_id ASC' }
41   },
42 );
43
44 __PACKAGE__->meta->initialize;
45
46 __PACKAGE__->attr_html('notes');
47
48 sub items { goto &invoiceitems; }
49 sub add_items { goto &add_invoiceitems; }
50
51 sub items_sorted {
52   my ($self) = @_;
53
54   return [ sort {$a->position <=> $b->position } @{ $self->items } ];
55 }
56
57 sub is_sales {
58   # For compatibility with Order, DeliveryOrder
59   croak 'not an accessor' if @_ > 1;
60   return 0;
61 }
62
63 sub date {
64   goto &transdate;
65 }
66
67 sub reqdate {
68   goto &duedate;
69 }
70
71 sub customervendor {
72   goto &vendor;
73 }
74
75 sub abbreviation {
76   my $self = shift;
77
78   return t8('AP Transaction (abbreviation)') if !$self->invoice && !$self->storno;
79   return t8('AP Transaction (abbreviation)') . '(' . t8('Storno (one letter abbreviation)') . ')' if !$self->invoice && $self->storno;
80   return t8('Invoice (one letter abbreviation)'). '(' . t8('Storno (one letter abbreviation)') . ')' if $self->storno;
81   return t8('Invoice (one letter abbreviation)');
82
83 };
84
85 sub pay_invoice {
86   my ($self, %params) = @_;
87
88   #Mark invoice as paid
89   $self->paid($self->paid+$params{amount});
90   $self->save;
91
92   Common::check_params(\%params, qw(chart_id trans_id amount transdate));
93
94   #account of bank account or cash
95   my $account_bank = SL::DB::Manager::Chart->find_by(id => $params{chart_id});
96
97   #Search the contra account
98   my $acc_trans = SL::DB::Manager::AccTransaction->find_by(trans_id   => $params{trans_id},
99                                                            or => [ chart_link => { like => "%:AP" },
100                                                                    chart_link => { like => "AP:%" },
101                                                                    chart_link => "AP" ]);
102   my $contra_account = SL::DB::Manager::Chart->find_by(id => $acc_trans->chart_id);
103
104   #Two new transfers in acc_trans (for bank account and for contra account)
105   my $new_acc_trans = SL::DB::AccTransaction->new(trans_id   => $params{trans_id},
106                                                   chart_id   => $account_bank->id,
107                                                   chart_link => $account_bank->link,
108                                                   amount     => $params{amount},
109                                                   transdate  => $params{transdate},
110                                                   source     => $params{source},
111                                                   memo       => '',
112                                                   taxkey     => 0,
113                                                   tax_id     => SL::DB::Manager::Tax->find_by(taxkey => 0)->id);
114   $new_acc_trans->save;
115   $new_acc_trans = SL::DB::AccTransaction->new(trans_id   => $params{trans_id},
116                                                chart_id   => $contra_account->id,
117                                                chart_link => $contra_account->link,
118                                                amount     => (-1 * $params{amount}),
119                                                transdate  => $params{transdate},
120                                                source     => $params{source},
121                                                memo       => '',
122                                                taxkey     => 0,
123                                                tax_id     => SL::DB::Manager::Tax->find_by(taxkey => 0)->id);
124   $new_acc_trans->save;
125 }
126
127 sub link {
128   my ($self) = @_;
129
130   my $html;
131   $html   = SL::Presenter->get->purchase_invoice($self, display => 'inline') if $self->invoice;
132   $html   = SL::Presenter->get->ap_transaction($self, display => 'inline') if !$self->invoice;
133
134   return $html;
135 }
136
137 1;