1 package SL::DB::PurchaseInvoice;
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);
13 # The calculator hasn't been adjusted for purchase invoices yet.
14 # use SL::DB::Helper::PriceTaxCalculator;
16 __PACKAGE__->meta->add_relationship(
18 type => 'one to many',
19 class => 'SL::DB::InvoiceItem',
20 column_map => { id => 'trans_id' },
21 manager_args => { with_objects => [ 'part' ] }
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' ] }
31 class => 'SL::DB::Shipto',
32 column_map => { id => 'trans_id' },
33 query_args => [ module => 'AP' ],
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' }
44 __PACKAGE__->meta->initialize;
46 __PACKAGE__->attr_html('notes');
48 sub items { goto &invoiceitems; }
49 sub add_items { goto &add_invoiceitems; }
54 return [ sort {$a->position <=> $b->position } @{ $self->items } ];
58 # For compatibility with Order, DeliveryOrder
59 croak 'not an accessor' if @_ > 1;
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)');
86 my ($self, %params) = @_;
89 $self->paid($self->paid+$params{amount});
92 Common::check_params(\%params, qw(chart_id trans_id amount transdate));
94 #account of bank account or cash
95 my $account_bank = SL::DB::Manager::Chart->find_by(id => $params{chart_id});
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);
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},
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},
123 tax_id => SL::DB::Manager::Tax->find_by(taxkey => 0)->id);
124 $new_acc_trans->save;
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;