Merge branch 'no-cleartext-passwords-in-db'
[kivitendo-erp.git] / SL / AR.pm
index fe3e443..a3284ed 100644 (file)
--- a/SL/AR.pm
+++ b/SL/AR.pm
@@ -171,7 +171,7 @@ sub post_transaction {
         # insert detail records in acc_trans
         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
                      VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|;
-        @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"amount_$i"}), conv_i($form->{"amount_$i"}), conv_date($form->{transdate}), $project_id,
+        @values = (conv_i($form->{id}), $form->{AR_amounts}{"amount_$i"}, conv_i($form->{"amount_$i"}), conv_date($form->{transdate}), $project_id,
                    conv_i($form->{"taxkey_$i"}));
         do_query($form, $dbh, $query, @values);
 
@@ -179,7 +179,7 @@ sub post_transaction {
           # insert detail records in acc_trans
           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
                        VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|;
-          @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"tax_$i"}), conv_i($form->{"tax_$i"}), conv_date($form->{transdate}), $project_id,
+          @values = (conv_i($form->{id}), $form->{AR_amounts}{"tax_$i"}, conv_i($form->{"tax_$i"}), conv_date($form->{transdate}), $project_id,
                      conv_i($form->{"taxkey_$i"}));
           do_query($form, $dbh, $query, @values);
         }
@@ -417,7 +417,7 @@ sub ar_transactions {
     qq|  a.marge_total, a.marge_percent, | .
     qq|  a.transaction_description, | .
     qq|  pr.projectnumber AS globalprojectnumber, | .
-    qq|  c.name, c.customernumber, c.country, c.ustid, | .
+    qq|  c.name, c.customernumber, c.country, c.ustid, b.description as customertype, | .
     qq|  e.name AS employee, | .
     qq|  e2.name AS salesman, | .
     qq|  tz.description AS taxzone, | .
@@ -436,6 +436,7 @@ sub ar_transactions {
     qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id)| .
     qq|LEFT JOIN tax_zones tz ON (tz.id = c.taxzone_id)| .
     qq|LEFT JOIN payment_terms pt ON (pt.id = c.payment_id)| .
+    qq|LEFT JOIN business b ON (b.id = c.business_id)| .
     qq|LEFT JOIN department d ON (d.id = a.department_id)|;
 
   my $where = "1 = 1";
@@ -446,6 +447,11 @@ sub ar_transactions {
     $where .= " AND c.name ILIKE ?";
     push(@values, $form->like($form->{customer}));
   }
+  if ($form->{business_id}) {
+    my $business_id = $form->{business_id};
+    $where .= " AND c.business_id = ?";
+    push(@values, $business_id);
+  }
   if ($form->{department_id}) {
     my $department_id = $form->{department_id};
     $where .= " AND a.department_id = ?";
@@ -485,10 +491,21 @@ sub ar_transactions {
     }
   }
 
- if (!$main::auth->assert('sales_all_edit', 1)) {
+  if (!$main::auth->assert('sales_all_edit', 1)) {
+    # only show own invoices
     $where .= " AND a.employee_id = (select id from employee where login= ?)";
     push (@values, $form->{login});
-  }
+  } else {
+    if ($form->{employee_id}) {
+      $where .= " AND a.employee_id = ?";
+      push @values, conv_i($form->{employee_id});
+    }
+    if ($form->{salesman_id}) {
+      $where .= " AND a.salesman_id = ?";
+      push @values, conv_i($form->{salesman_id});
+    }
+  };
+
   my @a = qw(transdate invnumber name);
   push @a, "employee" if $form->{l_employee};
   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
@@ -674,14 +691,14 @@ sub storno {
   }
 
   for my $row (@$rowref) {
-    delete @$row{qw(itime mtime link)};
+    delete @$row{qw(itime mtime link acc_trans_id)};
     $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
     $row->{trans_id}   = $new_id;
     $row->{amount}    *= -1;
     do_query($form, $dbh, $query, (values %$row));
   }
 
-  map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
+  map { IO->set_datepaid(table => 'ar', id => $_, dbh => $dbh) } ($id, $new_id);
 
   $dbh->commit;