1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
4 package SL::DB::Invoice;
9 use List::Util qw(first);
11 use SL::DB::MetaSetup::Invoice;
12 use SL::DB::Manager::Invoice;
13 use SL::DB::Helper::LinkedRecords;
14 use SL::DB::Helper::PriceTaxCalculator;
17 __PACKAGE__->meta->add_relationship(
19 type => 'one to many',
20 class => 'SL::DB::InvoiceItem',
21 column_map => { id => 'trans_id' },
23 with_objects => [ 'part' ]
28 class => 'SL::DB::PaymentTerm',
29 column_map => { payment_id => 'id' },
33 __PACKAGE__->meta->initialize;
37 sub items { goto &invoiceitems; }
39 # it is assumed, that ordnumbers are unique here.
40 sub first_order_by_ordnumber {
43 my $orders = SL::DB::Manager::Order->get_all(
45 ordnumber => $self->ordnumber,
50 return first { $_->is_type('sales_order') } @{ $orders };
53 sub abschlag_percentage {
55 my $order = $self->first_order_by_ordnumber or return;
56 my $order_amount = $order->netamount or return;
57 return $self->abschlag
58 ? $self->netamount / $order_amount
64 die 'not a setter method' if @_;
66 return $self->amount - $self->netamount;
69 __PACKAGE__->meta->make_attr_helpers(taxamount => 'numeric(15,5)');
73 return $self->paid >= $self->amount;
77 my ($class, $source, %params) = @_;
79 croak("Unsupported source object type '" . ref($source) . "'") unless ref($source) =~ m/^ SL::DB:: (?: Order | DeliveryOrder ) $/x;
80 croak("Cannot create invoices for purchase records") unless $source->customer_id;
82 my $terms = $source->can('payment_id') && $source->payment_id ? $source->payment_term->terms_netto : 0;
84 my %args = ( map({ ( $_ => $source->$_ ) } qw(customer_id taxincluded shippingpoint shipvia notes intnotes curr salesman_id cusordnumber ordnumber quonumber
85 department_id cp_id language_id payment_id delivery_customer_id delivery_vendor_id taxzone_id shipto_id
86 globalproject_id transaction_description)),
87 transdate => DateTime->today_local,
88 gldate => DateTime->today_local,
89 duedate => DateTime->today_local->add(days => $terms * 1),
93 employee_id => (SL::DB::Manager::Employee->current || SL::DB::Employee->new(id => $source->employee_id))->id,
96 if ($source->type =~ /_order$/) {
97 $args{deliverydate} = $source->reqdate;
98 $args{orddate} = $source->transdate;
100 $args{quodate} = $source->transdate;
103 my $invoice = $class->new(%args, %params);
106 my $source_item = $_;
107 SL::DB::InvoiceItem->new(map({ ( $_ => $source_item->$_ ) }
108 qw(parts_id description qty sellprice discount project_id
109 serialnumber pricegroup_id ordnumber transdate cusordnumber unit
110 base_qty subtotal longdescription lastcost price_factor_id)),
111 deliverydate => $source_item->reqdate);
112 } @{ $source->items };
114 $invoice->invoiceitems(\@items);
120 my ($self, %params) = @_;
122 $self->db->do_transaction(sub {
123 1; # dummy instruction for Emacs ;)
125 my %data = $self->calculate_prices_and_taxes;
127 $self->_post_create_assemblyitem_entries($data{assembly_items});
131 $self->_post_add_acctrans($data{amounts_cogs});
132 $self->_post_add_acctrans($data{amounts});
133 $self->_post_add_acctrans($data{taxes});
135 $self->_post_update_allocated($data{allocated});
141 sub _post_add_acctrans {
142 my ($self, $entries) = @_;
144 while (my ($chart_id, $spec) = each %{ $entries }) {
145 $spec = { taxkey => 0, amount => $spec } unless ref $spec;
146 SL::DB::AccTrans->new(trans_id => $self->id,
147 chart_id => $chart_id,
148 amount => $spec->{amount},
149 taxkey => $spec->{taxkey},
150 project_id => $self->project_id,
151 transdate => $self->transdate)->save;
155 sub _post_create_assemblyitem_entries {
156 my ($self, $assembly_entries) = @_;
158 my $items = $self->invoiceitems;
162 foreach my $item (@{ $items }) {
163 next if $item->assemblyitem;
165 push @new_items, $item;
168 foreach my $assembly_item (@{ $assembly_entries->[$item_idx] || [ ] }) {
169 push @new_items, SL::DB::InvoiceItem->new(parts_id => $assembly_item->{part},
170 description => $assembly_item->{part}->description,
171 unit => $assembly_item->{part}->unit,
172 qty => $assembly_item->{qty},
173 allocated => $assembly_item->{allocated},
176 assemblyitem => 't');
180 $self->invoiceitems(\@new_items);
183 sub _post_update_allocated {
184 my ($self, $allocated) = @_;
186 while (my ($invoice_id, $diff) = each %{ $allocated }) {
187 SL::DB::Manager::InvoiceItem->update_all(set => { allocated => { sql => [ 'allocated + ?', $diff ] } },
188 where => [ id => $invoice_id ]);
192 >>>>>>> b6be290... Prototypisiertes Buchen von Rechnungen