added roundings for periodic invoices
authorRolf Fluehmann <rolf.fluehmann@revamp-it.ch>
Thu, 30 Jun 2016 11:37:42 +0000 (13:37 +0200)
committerRolf Fluehmann <rolf.fluehmann@revamp-it.ch>
Thu, 30 Jun 2016 11:49:44 +0000 (13:49 +0200)
SL/DB/Invoice.pm

index 9aa17ad..0ef5b66 100644 (file)
@@ -249,6 +249,12 @@ sub post {
 
   my $worker = sub {
     my %data = $self->calculate_prices_and_taxes;
+    my $grossamount = $self->amount;
+    $self->amount($::form->round_amount($grossamount, 2, 1));
+    my $rounding = $::form->round_amount(
+      $self->amount - $grossamount,
+      2
+    );
 
     $self->_post_create_assemblyitem_entries($data{assembly_items});
     $self->save;
@@ -256,10 +262,11 @@ sub post {
     $self->_post_add_acctrans($data{amounts_cogs});
     $self->_post_add_acctrans($data{amounts});
     $self->_post_add_acctrans($data{taxes});
-
     $self->_post_add_acctrans({ $params{ar_id} => $self->amount * -1 });
 
     $self->_post_update_allocated($data{allocated});
+
+    $self->_post_book_rounding($rounding);
   };
 
   if ($self->db->in_transaction) {
@@ -296,6 +303,26 @@ sub _post_add_acctrans {
   }
 }
 
+sub _post_book_rounding {
+  my ($self, $rounding) = @_;
+
+  my $tax_id = SL::DB::Manager::Tax->find_by(taxkey => 0)->id;
+  my $rnd_accno = $rounding == 0 ? 0
+                : $rounding > 0  ? SL::DB::Default->get->rndgain_accno_id
+                :                  SL::DB::Default->get->rndloss_accno_id
+  ;
+  if ($rnd_accno != 0) {
+    SL::DB::AccTransaction->new(trans_id   => $self->id,
+                                chart_id   => $rnd_accno,
+                                amount     => $rounding,
+                                tax_id     => $tax_id,
+                                taxkey     => 0,
+                                project_id => $self->globalproject_id,
+                                transdate  => $self->transdate,
+                                chart_link => $rnd_accno)->save;
+  }
+}
+
 sub add_ar_amount_row {
   my ($self, %params ) = @_;