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::FlattenToForm;
 
  14 use SL::DB::Helper::LinkedRecords;
 
  15 use SL::DB::Helper::PriceTaxCalculator;
 
  16 use SL::DB::Helper::PriceUpdater;
 
  17 use SL::DB::Helper::TransNumberGenerator;
 
  18 use SL::DB::AccTransaction;
 
  22 __PACKAGE__->meta->add_relationship(
 
  24     type         => 'one to many',
 
  25     class        => 'SL::DB::InvoiceItem',
 
  26     column_map   => { id => 'trans_id' },
 
  28       with_objects => [ 'part' ]
 
  33     class      => 'SL::DB::PaymentTerm',
 
  34     column_map => { payment_id => 'id' },
 
  38 __PACKAGE__->meta->initialize;
 
  42 sub items { goto &invoiceitems; }
 
  44 # it is assumed, that ordnumbers are unique here.
 
  45 sub first_order_by_ordnumber {
 
  48   my $orders = SL::DB::Manager::Order->get_all(
 
  50       ordnumber => $self->ordnumber,
 
  55   return first { $_->is_type('sales_order') } @{ $orders };
 
  58 sub abschlag_percentage {
 
  60   my $order        = $self->first_order_by_ordnumber or return;
 
  61   my $order_amount = $order->netamount               or return;
 
  62   return $self->abschlag
 
  63     ? $self->netamount / $order_amount
 
  69   die 'not a setter method' if @_;
 
  71   return $self->amount - $self->netamount;
 
  74 __PACKAGE__->meta->make_attr_helpers(taxamount => 'numeric(15,5)');
 
  78   return $self->paid >= $self->amount;
 
  82   my ($class, $source, %params) = @_;
 
  84   croak("Unsupported source object type '" . ref($source) . "'") unless ref($source) =~ m/^ SL::DB:: (?: Order | DeliveryOrder ) $/x;
 
  85   croak("Cannot create invoices for purchase records")           unless $source->customer_id;
 
  87   my $terms = $source->can('payment_id') && $source->payment_id ? $source->payment_term->terms_netto : 0;
 
  89   my %args = ( map({ ( $_ => $source->$_ ) } qw(customer_id taxincluded shippingpoint shipvia notes intnotes curr salesman_id cusordnumber ordnumber quonumber
 
  90                                                 department_id cp_id language_id payment_id delivery_customer_id delivery_vendor_id taxzone_id shipto_id
 
  91                                                 globalproject_id transaction_description)),
 
  92                transdate   => DateTime->today_local,
 
  93                gldate      => DateTime->today_local,
 
  94                duedate     => DateTime->today_local->add(days => $terms * 1),
 
  99                employee_id => (SL::DB::Manager::Employee->current || SL::DB::Employee->new(id => $source->employee_id))->id,
 
 102   if ($source->type =~ /_order$/) {
 
 103     $args{deliverydate} = $source->reqdate;
 
 104     $args{orddate}      = $source->transdate;
 
 106     $args{quodate}      = $source->transdate;
 
 109   my $invoice = $class->new(%args, %params);
 
 112     my $source_item = $_;
 
 113     SL::DB::InvoiceItem->new(map({ ( $_ => $source_item->$_ ) }
 
 114                                  qw(parts_id description qty sellprice discount project_id
 
 115                                     serialnumber pricegroup_id ordnumber transdate cusordnumber unit
 
 116                                     base_qty subtotal longdescription lastcost price_factor_id)),
 
 117                             deliverydate => $source_item->reqdate,
 
 118                             fxsellprice  => $source_item->sellprice,);
 
 119   } @{ $source->items };
 
 121   $invoice->invoiceitems(\@items);
 
 127   my ($self, %params) = @_;
 
 129   if (!$params{ar_id}) {
 
 130     my $chart = SL::DB::Manager::Chart->get_all(query   => [ SL::DB::Manager::Chart->link_filter('AR') ],
 
 133     croak("No AR chart found and no parameter `ar_id' given") unless $chart;
 
 134     $params{ar_id} = $chart->id;
 
 138     my %data = $self->calculate_prices_and_taxes;
 
 140     $self->_post_create_assemblyitem_entries($data{assembly_items});
 
 141     $self->create_trans_number;
 
 144     $self->_post_add_acctrans($data{amounts_cogs});
 
 145     $self->_post_add_acctrans($data{amounts});
 
 146     $self->_post_add_acctrans($data{taxes});
 
 148     $self->_post_add_acctrans({ $params{ar_id} => $self->amount * -1 });
 
 150     $self->_post_update_allocated($data{allocated});
 
 153   if ($self->db->in_transaction) {
 
 155   } elsif (!$self->db->do_transaction($worker)) {
 
 156     $::lxdebug->message(0, "convert_to_invoice failed: " . join("\n", (split(/\n/, $self->db->error))[0..2]));
 
 163 sub _post_add_acctrans {
 
 164   my ($self, $entries) = @_;
 
 166   while (my ($chart_id, $spec) = each %{ $entries }) {
 
 167     $spec = { taxkey => 0, amount => $spec } unless ref $spec;
 
 168     SL::DB::AccTransaction->new(trans_id   => $self->id,
 
 169                                 chart_id   => $chart_id,
 
 170                                 amount     => $spec->{amount},
 
 171                                 taxkey     => $spec->{taxkey},
 
 172                                 project_id => $self->globalproject_id,
 
 173                                 transdate  => $self->transdate)->save;
 
 177 sub _post_create_assemblyitem_entries {
 
 178   my ($self, $assembly_entries) = @_;
 
 180   my $items = $self->invoiceitems;
 
 184   foreach my $item (@{ $items }) {
 
 185     next if $item->assemblyitem;
 
 187     push @new_items, $item;
 
 190     foreach my $assembly_item (@{ $assembly_entries->[$item_idx] || [ ] }) {
 
 191       push @new_items, SL::DB::InvoiceItem->new(parts_id     => $assembly_item->{part},
 
 192                                                 description  => $assembly_item->{part}->description,
 
 193                                                 unit         => $assembly_item->{part}->unit,
 
 194                                                 qty          => $assembly_item->{qty},
 
 195                                                 allocated    => $assembly_item->{allocated},
 
 198                                                 assemblyitem => 't');
 
 202   $self->invoiceitems(\@new_items);
 
 205 sub _post_update_allocated {
 
 206   my ($self, $allocated) = @_;
 
 208   while (my ($invoice_id, $diff) = each %{ $allocated }) {
 
 209     SL::DB::Manager::InvoiceItem->update_all(set   => { allocated => { sql => "allocated + $diff" } },
 
 210                                              where => [ id        => $invoice_id ]);
 
 222 SL::DB::Invoice: Rose model for invoices (table "ar")
 
 228 =item C<new_from $source>
 
 230 Creates a new C<SL::DB::Invoice> instance and copies as much
 
 231 information from C<$source> as possible. At the moment only sales
 
 232 orders and sales quotations are supported as sources.
 
 234 The conversion copies order items into invoice items. Dates are copied
 
 235 as appropriate, e.g. the C<transdate> field from an order will be
 
 236 copied into the invoice's C<orddate> field.
 
 238 Amounts, prices and taxes are not
 
 239 calculated. L<SL::DB::Helper::PriceTaxCalculator::calculate_prices_and_taxes>
 
 240 can be used for this.
 
 242 The object returned is not saved.
 
 244 =item C<post %params>
 
 246 Posts the invoice. Required parameters are:
 
 252 The ID of the accounds receivable chart the invoices amounts are
 
 253 posted to. If it is not set then the first chart configured for
 
 254 accounts receivables is used.
 
 258 This function implements several steps:
 
 262 =item 1. It calculates all prices, amounts and taxes by calling
 
 263 L<SL::DB::Helper::PriceTaxCalculator::calculate_prices_and_taxes>.
 
 265 =item 2. A new and unique invoice number is created.
 
 267 =item 3. All amounts for costs of goods sold are recorded in
 
 270 =item 4. All amounts for parts, services and assemblies are recorded
 
 271 in C<acc_trans> with their respective charts. This is determined by
 
 272 the part's buchungsgruppen.
 
 274 =item 5. The total amount is posted to the accounts receivable chart
 
 275 and recorded in C<acc_trans>.
 
 277 =item 6. Items in C<invoice> are updated according to their allocation
 
 278 status (regarding for costs of goold sold). Will only be done if
 
 279 Lx-Office is not configured to use Einnahmenüberschussrechnungen
 
 282 =item 7. The invoice and its items are saved.
 
 286 Returns C<$self> on success and C<undef> on failure. The whole process
 
 287 is run inside a transaction. If it fails then nothing is saved to or
 
 288 changed in the database. A new transaction is only started if none is
 
 291 =item C<basic_info $field>
 
 293 See L<SL::DB::Object::basic_info>.
 
 299 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>