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),
94 employee_id => (SL::DB::Manager::Employee->current || SL::DB::Employee->new(id => $source->employee_id))->id,
97 if ($source->type =~ /_order$/) {
98 $args{deliverydate} = $source->reqdate;
99 $args{orddate} = $source->transdate;
101 $args{quodate} = $source->transdate;
104 my $invoice = $class->new(%args, %params);
107 my $source_item = $_;
108 SL::DB::InvoiceItem->new(map({ ( $_ => $source_item->$_ ) }
109 qw(parts_id description qty sellprice discount project_id
110 serialnumber pricegroup_id ordnumber transdate cusordnumber unit
111 base_qty subtotal longdescription lastcost price_factor_id)),
112 deliverydate => $source_item->reqdate);
113 } @{ $source->items };
115 $invoice->invoiceitems(\@items);
121 my ($self, %params) = @_;
123 $self->db->do_transaction(sub {
124 1; # dummy instruction for Emacs ;)
126 my %data = $self->calculate_prices_and_taxes;
128 $self->_post_create_assemblyitem_entries($data{assembly_items});
132 $self->_post_add_acctrans($data{amounts_cogs});
133 $self->_post_add_acctrans($data{amounts});
134 $self->_post_add_acctrans($data{taxes});
136 $self->_post_update_allocated($data{allocated});
142 sub _post_add_acctrans {
143 my ($self, $entries) = @_;
145 while (my ($chart_id, $spec) = each %{ $entries }) {
146 $spec = { taxkey => 0, amount => $spec } unless ref $spec;
147 SL::DB::AccTrans->new(trans_id => $self->id,
148 chart_id => $chart_id,
149 amount => $spec->{amount},
150 taxkey => $spec->{taxkey},
151 project_id => $self->project_id,
152 transdate => $self->transdate)->save;
156 sub _post_create_assemblyitem_entries {
157 my ($self, $assembly_entries) = @_;
159 my $items = $self->invoiceitems;
163 foreach my $item (@{ $items }) {
164 next if $item->assemblyitem;
166 push @new_items, $item;
169 foreach my $assembly_item (@{ $assembly_entries->[$item_idx] || [ ] }) {
170 push @new_items, SL::DB::InvoiceItem->new(parts_id => $assembly_item->{part},
171 description => $assembly_item->{part}->description,
172 unit => $assembly_item->{part}->unit,
173 qty => $assembly_item->{qty},
174 allocated => $assembly_item->{allocated},
177 assemblyitem => 't');
181 $self->invoiceitems(\@new_items);
184 sub _post_update_allocated {
185 my ($self, $allocated) = @_;
187 while (my ($invoice_id, $diff) = each %{ $allocated }) {
188 SL::DB::Manager::InvoiceItem->update_all(set => { allocated => { sql => [ 'allocated + ?', $diff ] } },
189 where => [ id => $invoice_id ]);
193 >>>>>>> b6be290... Prototypisiertes Buchen von Rechnungen