Tabelle parts: Fremdschlüssel für Konten - bessere Namen
[kivitendo-erp.git] / SL / AR.pm
index a2cad11..8e8cf2f 100644 (file)
--- a/SL/AR.pm
+++ b/SL/AR.pm
@@ -130,12 +130,15 @@ sub post_transaction {
       qq|UPDATE ar set
            invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?,
            taxincluded = ?, amount = ?, duedate = ?, paid = ?,
+           currency_id = (SELECT id FROM currencies WHERE name = ?),
            netamount = ?, notes = ?, department_id = ?,
            employee_id = ?, storno = ?, storno_id = ?, globalproject_id = ?,
            direct_debit = ?
          WHERE id = ?|;
     my @values = ($form->{invnumber}, $form->{ordnumber}, conv_date($form->{transdate}), conv_i($form->{customer_id}), $form->{taxincluded} ? 't' : 'f', $form->{amount},
-                  conv_date($form->{duedate}), $form->{paid}, $form->{netamount}, $form->{notes}, conv_i($form->{department_id}),
+                  conv_date($form->{duedate}), $form->{paid},
+                  $form->{currency},
+                  $form->{netamount}, $form->{notes}, conv_i($form->{department_id}),
                   conv_i($form->{employee_id}), $form->{storno} ? 't' : 'f', $form->{storno_id},
                   conv_i($form->{globalproject_id}), $form->{direct_debit} ? 't' : 'f', conv_i($form->{id}));
     do_query($form, $dbh, $query, @values);
@@ -272,6 +275,15 @@ sub post_transaction {
         $amount = $form->round_amount( $form->{"paid_$i"} * ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1, 2);
 
         if ($amount != 0) {
+          # fetch fxgain and fxloss chart info from defaults if charts aren't already filled in form
+          if ( !$form->{fxgain_accno} && $::instance_conf->get_fxgain_accno_id ) {
+            $form->{fxgain_accno} = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_fxgain_accno_id)->accno;
+          };
+          if ( !$form->{fxloss_accno} && $::instance_conf->get_fxloss_accno_id ) {
+            $form->{fxloss_accno} = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_fxloss_accno_id)->accno;
+          };
+          die "fxloss_accno missing" if $amount < 0 and not $form->{fxloss_accno};
+          die "fxgain_accno missing" if $amount > 0 and not $form->{fxgain_accno};
           my $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link)
                        VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?),
@@ -473,6 +485,7 @@ sub ar_transactions {
     qq|  dc.dunning_description, | .
     qq|  tz.description AS taxzone, | .
     qq|  pt.description AS payment_terms, | .
+    qq|  d.description AS department, | .
     qq{  ( SELECT ch.accno || ' -- ' || ch.description
            FROM acc_trans at
            LEFT JOIN chart ch ON ch.id = at.chart_id
@@ -507,11 +520,11 @@ sub ar_transactions {
     push(@values, $form->{customer_id});
   } elsif ($form->{customer}) {
     $where .= " AND c.name ILIKE ?";
-    push(@values, $form->like($form->{customer}));
+    push(@values, like($form->{customer}));
   }
   if ($form->{"cp_name"}) {
     $where .= " AND (cp.cp_name ILIKE ? OR cp.cp_givenname ILIKE ?)";
-    push(@values, ('%' . trim($form->{"cp_name"}) . '%')x2);
+    push(@values, (like($form->{"cp_name"}))x2);
   }
   if ($form->{business_id}) {
     my $business_id = $form->{business_id};
@@ -524,14 +537,14 @@ sub ar_transactions {
     push(@values, $department_id);
   }
   if ($form->{department}) {
-    my $department = "%" . trim($form->{department}) . "%";
+    my $department = like($form->{department});
     $where .= " AND d.description ILIKE ?";
     push(@values, $department);
   }
   foreach my $column (qw(invnumber ordnumber cusordnumber notes transaction_description)) {
     if ($form->{$column}) {
       $where .= " AND a.$column ILIKE ?";
-      push(@values, $form->like($form->{$column}));
+      push(@values, like($form->{$column}));
     }
   }
   if ($form->{"project_id"}) {
@@ -584,6 +597,33 @@ sub ar_transactions {
     }
   };
 
+  if ($form->{parts_partnumber}) {
+    $where .= <<SQL;
+      AND EXISTS (
+        SELECT invoice.trans_id
+        FROM invoice
+        LEFT JOIN parts ON (invoice.parts_id = parts.id)
+        WHERE (invoice.trans_id = a.id)
+          AND (parts.partnumber ILIKE ?)
+        LIMIT 1
+      )
+SQL
+    push @values, like($form->{parts_partnumber});
+  }
+
+  if ($form->{parts_description}) {
+    $where .= <<SQL;
+      AND EXISTS (
+        SELECT invoice.trans_id
+        FROM invoice
+        WHERE (invoice.trans_id = a.id)
+          AND (invoice.description ILIKE ?)
+        LIMIT 1
+      )
+SQL
+    push @values, like($form->{parts_description});
+  }
+
   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'CT',
                                                             'trans_id_field' => 'c.id',
                                                             'filter'         => $form,
@@ -643,6 +683,7 @@ sub setup_form {
   $form->{forex} = $form->{exchangerate};
   $exchangerate  = $form->{exchangerate} ? $form->{exchangerate} : 1;
 
+  # expected keys: AR, AR_paid, AR_tax, AR_amount 
   foreach my $key (keys %{ $form->{AR_links} }) {
     $j = 0;
     $k = 0;
@@ -671,15 +712,17 @@ sub setup_form {
         $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
         $form->{paidaccounts}++;
 
-      } else {
+      } else { # e.g. AR_amount, AR, AR_tax
 
         $akey = $key;
-        $akey =~ s/AR_//;
+        $akey =~ s/AR_//; # e.g. tax, amount, AR, used to store form key tax_$i, amount_$i, ...
 
-        if ($key eq "AR_tax" || $key eq "AP_tax") {
+        if ($key eq "AR_tax" || $key eq "AP_tax") { # AR_tax
           $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"}  = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
+          # determine the rounded tax amounts for each account, e.g. tax_1776
           $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
 
+          # check e.g. $form->{1776_rate}, does this make sense for AR_tax charts? Is this ever valid? If it was, totaltax would be calculated twice
           if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
             $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
             $taxrate  += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
@@ -690,10 +733,11 @@ sub setup_form {
           }
 
           $index                 = $form->{acc_trans}{$key}->[$i - 1]->{index};
-          $form->{"tax_$index"}  = $form->{acc_trans}{$key}->[$i - 1]->{amount};
+          $form->{"tax_$index"}  = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2); # convert the tax_$i amounts
+          # currently totaltax is the sum of rounded tax amounts, is this correct?
           $totaltax             += $form->{"tax_$index"};
 
-        } else {
+        } else { # e.g. AR_amount, AR
           $k++;
           $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);