c63a2ed70292f94108b514c9917e74fccbba43d2
[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::AttrSorted;
11 use SL::DB::Helper::LinkedRecords;
12 use SL::DB::Helper::Payment qw(:ALL);
13 use SL::Locale::String qw(t8);
14
15 # The calculator hasn't been adjusted for purchase invoices yet.
16 # use SL::DB::Helper::PriceTaxCalculator;
17
18 __PACKAGE__->meta->add_relationship(
19   invoiceitems   => {
20     type         => 'one to many',
21     class        => 'SL::DB::InvoiceItem',
22     column_map   => { id => 'trans_id' },
23     manager_args => { with_objects => [ 'part' ] }
24   },
25   sepa_export_items => {
26     type            => 'one to many',
27     class           => 'SL::DB::SepaExportItem',
28     column_map      => { id => 'ap_id' },
29     manager_args    => { with_objects => [ 'sepa_export' ] }
30   },
31   sepa_exports      => {
32     type            => 'many to many',
33     map_class       => 'SL::DB::SepaExportItem',
34     map_from        => 'ap',
35     map_to          => 'sepa_export',
36   },
37   custom_shipto     => {
38     type            => 'one to one',
39     class           => 'SL::DB::Shipto',
40     column_map      => { id => 'trans_id' },
41     query_args      => [ module => 'AP' ],
42   },
43   transactions   => {
44     type         => 'one to many',
45     class        => 'SL::DB::AccTransaction',
46     column_map   => { id => 'trans_id' },
47     manager_args => { with_objects => [ 'chart' ],
48                       sort_by      => 'acc_trans_id ASC' }
49   },
50 );
51
52 __PACKAGE__->meta->initialize;
53
54 __PACKAGE__->attr_html('notes');
55 __PACKAGE__->attr_sorted('items');
56
57 sub items { goto &invoiceitems; }
58 sub add_items { goto &add_invoiceitems; }
59 sub record_number { goto &invnumber; };
60
61 sub is_sales {
62   # For compatibility with Order, DeliveryOrder
63   croak 'not an accessor' if @_ > 1;
64   return 0;
65 }
66
67 sub date {
68   goto &transdate;
69 }
70
71 sub reqdate {
72   goto &duedate;
73 }
74
75 sub customervendor {
76   goto &vendor;
77 }
78
79 sub abbreviation {
80   my $self = shift;
81
82   return t8('AP Transaction (abbreviation)') if !$self->invoice && !$self->storno;
83   return t8('AP Transaction (abbreviation)') . '(' . t8('Storno (one letter abbreviation)') . ')' if !$self->invoice && $self->storno;
84   return t8('Invoice (one letter abbreviation)'). '(' . t8('Storno (one letter abbreviation)') . ')' if $self->storno;
85   return t8('Invoice (one letter abbreviation)');
86
87 };
88
89 sub link {
90   my ($self) = @_;
91
92   my $html;
93   $html   = SL::Presenter->get->purchase_invoice($self, display => 'inline') if $self->invoice;
94   $html   = SL::Presenter->get->ap_transaction($self, display => 'inline') if !$self->invoice;
95
96   return $html;
97 }
98
99 sub invoice_type {
100   my ($self) = @_;
101
102   return 'ap_transaction' if !$self->invoice;
103   return 'purchase_invoice';
104 }
105
106 sub displayable_type {
107   my ($self) = @_;
108
109   return t8('AP Transaction')    if $self->invoice_type eq 'ap_transaction';
110   return t8('Purchase Invoice');
111 }
112
113 sub displayable_name {
114   join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
115 };
116
117 1;