d1eaded8ff9ca39d7b3d49c69a25b63749ba08a3
[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   custom_shipto     => {
32     type            => 'one to one',
33     class           => 'SL::DB::Shipto',
34     column_map      => { id => 'trans_id' },
35     query_args      => [ module => 'AP' ],
36   },
37   transactions   => {
38     type         => 'one to many',
39     class        => 'SL::DB::AccTransaction',
40     column_map   => { id => 'trans_id' },
41     manager_args => { with_objects => [ 'chart' ],
42                       sort_by      => 'acc_trans_id ASC' }
43   },
44 );
45
46 __PACKAGE__->meta->initialize;
47
48 __PACKAGE__->attr_html('notes');
49 __PACKAGE__->attr_sorted('items');
50
51 sub items { goto &invoiceitems; }
52 sub add_items { goto &add_invoiceitems; }
53 sub record_number { goto &invnumber; };
54
55 sub is_sales {
56   # For compatibility with Order, DeliveryOrder
57   croak 'not an accessor' if @_ > 1;
58   return 0;
59 }
60
61 sub date {
62   goto &transdate;
63 }
64
65 sub reqdate {
66   goto &duedate;
67 }
68
69 sub customervendor {
70   goto &vendor;
71 }
72
73 sub abbreviation {
74   my $self = shift;
75
76   return t8('AP Transaction (abbreviation)') if !$self->invoice && !$self->storno;
77   return t8('AP Transaction (abbreviation)') . '(' . t8('Storno (one letter abbreviation)') . ')' if !$self->invoice && $self->storno;
78   return t8('Invoice (one letter abbreviation)'). '(' . t8('Storno (one letter abbreviation)') . ')' if $self->storno;
79   return t8('Invoice (one letter abbreviation)');
80
81 };
82
83 sub link {
84   my ($self) = @_;
85
86   my $html;
87   $html   = SL::Presenter->get->purchase_invoice($self, display => 'inline') if $self->invoice;
88   $html   = SL::Presenter->get->ap_transaction($self, display => 'inline') if !$self->invoice;
89
90   return $html;
91 }
92
93 sub invoice_type {
94   my ($self) = @_;
95
96   return 'ap_transaction' if !$self->invoice;
97   return 'purchase_invoice';
98 }
99
100 sub displayable_type {
101   my ($self) = @_;
102
103   return t8('AP Transaction')    if $self->invoice_type eq 'ap_transaction';
104   return t8('Purchase Invoice');
105 }
106
107 sub displayable_name {
108   join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
109 };
110
111 1;