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;
15 use SL::DB::Helper::TransNumberGenerator;
18 __PACKAGE__->meta->add_relationship(
20 type => 'one to many',
21 class => 'SL::DB::InvoiceItem',
22 column_map => { id => 'trans_id' },
24 with_objects => [ 'part' ]
29 class => 'SL::DB::PaymentTerm',
30 column_map => { payment_id => 'id' },
34 __PACKAGE__->meta->initialize;
38 sub items { goto &invoiceitems; }
40 # it is assumed, that ordnumbers are unique here.
41 sub first_order_by_ordnumber {
44 my $orders = SL::DB::Manager::Order->get_all(
46 ordnumber => $self->ordnumber,
51 return first { $_->is_type('sales_order') } @{ $orders };
54 sub abschlag_percentage {
56 my $order = $self->first_order_by_ordnumber or return;
57 my $order_amount = $order->netamount or return;
58 return $self->abschlag
59 ? $self->netamount / $order_amount
65 die 'not a setter method' if @_;
67 return $self->amount - $self->netamount;
70 __PACKAGE__->meta->make_attr_helpers(taxamount => 'numeric(15,5)');
74 return $self->paid >= $self->amount;
78 my ($class, $source, %params) = @_;
80 croak("Unsupported source object type '" . ref($source) . "'") unless ref($source) =~ m/^ SL::DB:: (?: Order | DeliveryOrder ) $/x;
81 croak("Cannot create invoices for purchase records") unless $source->customer_id;
83 my $terms = $source->can('payment_id') && $source->payment_id ? $source->payment_term->terms_netto : 0;
85 my %args = ( map({ ( $_ => $source->$_ ) } qw(customer_id taxincluded shippingpoint shipvia notes intnotes curr salesman_id cusordnumber ordnumber quonumber
86 department_id cp_id language_id payment_id delivery_customer_id delivery_vendor_id taxzone_id shipto_id
87 globalproject_id transaction_description)),
88 transdate => DateTime->today_local,
89 gldate => DateTime->today_local,
90 duedate => DateTime->today_local->add(days => $terms * 1),
95 employee_id => (SL::DB::Manager::Employee->current || SL::DB::Employee->new(id => $source->employee_id))->id,
98 if ($source->type =~ /_order$/) {
99 $args{deliverydate} = $source->reqdate;
100 $args{orddate} = $source->transdate;
102 $args{quodate} = $source->transdate;
105 my $invoice = $class->new(%args, %params);
108 my $source_item = $_;
109 SL::DB::InvoiceItem->new(map({ ( $_ => $source_item->$_ ) }
110 qw(parts_id description qty sellprice discount project_id
111 serialnumber pricegroup_id ordnumber transdate cusordnumber unit
112 base_qty subtotal longdescription lastcost price_factor_id)),
113 deliverydate => $source_item->reqdate,
114 fxsellprice => $source_item->sellprice,);
115 } @{ $source->items };
117 $invoice->invoiceitems(\@items);
123 my ($self, %params) = @_;
125 croak("Missing parameter 'ar_id'") unless $params{ar_id};
128 my %data = $self->calculate_prices_and_taxes;
130 $self->_post_create_assemblyitem_entries($data{assembly_items});
131 $self->create_trans_number;
134 $self->_post_add_acctrans($data{amounts_cogs});
135 $self->_post_add_acctrans($data{amounts});
136 $self->_post_add_acctrans($data{taxes});
138 $self->_post_add_acctrans({ $params{ar_id} => $self->amount * -1 });
140 $self->_post_update_allocated($data{allocated});
143 if ($self->db->in_transaction) {
145 } elsif (!$self->db->do_transaction($worker)) {
146 $::lxdebug->message(0, "convert_to_invoice failed: " . join("\n", (split(/\n/, $self->db->error))[0..2]));
153 sub _post_add_acctrans {
154 my ($self, $entries) = @_;
156 while (my ($chart_id, $spec) = each %{ $entries }) {
157 $spec = { taxkey => 0, amount => $spec } unless ref $spec;
158 SL::DB::AccTrans->new(trans_id => $self->id,
159 chart_id => $chart_id,
160 amount => $spec->{amount},
161 taxkey => $spec->{taxkey},
162 project_id => $self->globalproject_id,
163 transdate => $self->transdate)->save;
167 sub _post_create_assemblyitem_entries {
168 my ($self, $assembly_entries) = @_;
170 my $items = $self->invoiceitems;
174 foreach my $item (@{ $items }) {
175 next if $item->assemblyitem;
177 push @new_items, $item;
180 foreach my $assembly_item (@{ $assembly_entries->[$item_idx] || [ ] }) {
181 push @new_items, SL::DB::InvoiceItem->new(parts_id => $assembly_item->{part},
182 description => $assembly_item->{part}->description,
183 unit => $assembly_item->{part}->unit,
184 qty => $assembly_item->{qty},
185 allocated => $assembly_item->{allocated},
188 assemblyitem => 't');
192 $self->invoiceitems(\@new_items);
195 sub _post_update_allocated {
196 my ($self, $allocated) = @_;
198 while (my ($invoice_id, $diff) = each %{ $allocated }) {
199 SL::DB::Manager::InvoiceItem->update_all(set => { allocated => { sql => "allocated + $diff" } },
200 where => [ id => $invoice_id ]);
204 >>>>>>> b6be290... Prototypisiertes Buchen von Rechnungen