draft strict v2
[kivitendo-erp.git] / bin / mozilla / ar.pl
index 31c9cff..f506cd9 100644 (file)
@@ -32,7 +32,7 @@
 #======================================================================
 
 use POSIX qw(strftime);
-use List::Util qw(sum);
+use List::Util qw(sum first);
 
 use SL::AR;
 use SL::FU;
@@ -96,8 +96,8 @@ sub add {
        $form->{addition} = "ADDED";
        $form->save_history($form->dbconnect(\%myconfig));
   }
-  # /saving the history 
-  
+  # /saving the history
+
   $form->{title}    = "Add";
   $form->{callback} = "ar.pl?action=add" unless $form->{callback};
 
@@ -214,7 +214,7 @@ sub form_header {
   my ($title, $readonly, $exchangerate, $rows);
   my ($taxincluded, $notes, $department, $customer, $employee, $amount, $project);
   my ($jsscript, $button1, $button2, $onload);
-  my ($selectAR_amount, $selectAR_paid, $korrektur_checked, $ARselected, $tax);
+  my ($selectAR_amount, $selectAR_paid, $ARselected, $tax);
   my (@column_index, %column_data);
 
 
@@ -318,8 +318,8 @@ sub form_header {
 
   my $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
 
-  $customer = ($form->{selectcustomer}) 
-    ? qq|<select name="customer" onchange="document.getElementById('update_button').click();">$form->{selectcustomer}</select>| 
+  $customer = ($form->{selectcustomer})
+    ? qq|<select name="customer" onchange="document.getElementById('update_button').click();">$form->{selectcustomer}</select>|
     : qq|<input name=customer value="$form->{customer}" size=35>|;
 
   $employee = qq|
@@ -340,12 +340,13 @@ sub form_header {
   map({ push(@old_project_ids, $form->{"project_id_$_"})
           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
 
-  $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
-                                   "all" => 0,
-                                   "old_id" => \@old_project_ids },
-                   "charts" => { "key" => "ALL_CHARTS",
-                                 "transdate" => $form->{transdate} },
-                   "taxcharts" => "ALL_TAXCHARTS");
+  $form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
+                                    "all"       => 0,
+                                    "old_id"    => \@old_project_ids },
+                   "charts"    => { "key"       => "ALL_CHARTS",
+                                    "transdate" => $form->{transdate} },
+                   "taxcharts" => { "key"       => "ALL_TAXCHARTS",
+                                    "module"    => "AR" },);
 
   map({ $_->{link_split} = [ split(/:/, $_->{link}) ]; }
       @{ $form->{ALL_CHARTS} });
@@ -471,7 +472,7 @@ sub form_header {
            <table>
              <tr>
                <th align="right" nowrap>| . $locale->text('Customer') . qq|</th>
-               <td colspan=3>$customer <input type="button" value="?" onclick="show_vc_details('customer')"></td>
+               <td colspan=3>$customer <input type="button" value="| . $locale->text('Details (one letter abbreviation)') . qq|" onclick="show_vc_details('customer')"></td>
                <input type=hidden name=selectcustomer value="$form->{selectcustomer}">
                <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
                <input type=hidden name=customer_id value="$form->{customer_id}">
@@ -544,8 +545,6 @@ $jsscript
           <th class=listheading style="width:10%">|
     . $locale->text('Tax') . qq|</th>
           <th class=listheading style="width:5%">|
-    . $locale->text('Korrektur') . qq|</th>
-          <th class=listheading style="width:10%">|
     . $locale->text('Taxkey') . qq|</th>
           <th class=listheading style="width:10%">|
     . $locale->text('Project') . qq|</th>
@@ -578,7 +577,13 @@ $jsscript
       $selected_taxchart = "$item->{id}--$item->{rate}";
     }
 
-    $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
+    if (!$form->{"taxchart_$i"}) {
+      if ($form->{"AR_amount_$i"} =~ m/.--./) {
+        $selected_taxchart = join '--', map { ($_->{id}, $_->{rate}) } first { $_->{id} == $item->{tax_id} } @{ $form->{ALL_TAXCHARTS} };
+      } else {
+        $selected_taxchart = $taxchart_init;
+      }
+    }
 
     $selectAR_amount =
       NTI($cgi->popup_menu('-name' => "AR_amount_$i",
@@ -600,8 +605,6 @@ $jsscript
                            '-default' => $selected_taxchart))
       . qq|</td>|;
 
-    $korrektur_checked = ($form->{"korrektur_$i"} ? 'checked' : '');
-
     my $projectnumber =
       NTI($cgi->popup_menu('-name' => "project_id_$i",
                            '-values' => \@project_values,
@@ -612,8 +615,7 @@ $jsscript
        <tr>
           <td>$selectAR_amount</td>
           <td><input name="amount_$i" size=10 value=$form->{"amount_$i"}></td>
-          <td><input name="tax_$i" size=10 value=$form->{"tax_$i"}></td>
-          <td><input type="checkbox" name="korrektur_$i" value="1" $korrektur_checked></td>
+          <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
           $tax
           <td>$projectnumber</td>
        </tr>
@@ -683,8 +685,8 @@ $jsscript
   $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
   $column_data{AR_paid}      = "<th>" . $locale->text('Account') . "</th>";
   $column_data{source}       = "<th>" . $locale->text('Source') . "</th>";
-  $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>"; 
-  $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>"; 
+  $column_data{memo}         = "<th>" . $locale->text('Memo') . "</th>";
+  $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>";
 
   print "
         <tr>
@@ -847,7 +849,7 @@ $follow_ups_block
 
   # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
   print qq| <input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|"> |
-    if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ar') && !IS->is_storno(\%myconfig, $form, 'ar'));
+    if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ar') && !IS->is_storno(\%myconfig, $form, 'ar') && (($total_paid == 0) || ($total_paid eq "")));
 
   if ($form->{id}) {
     if ($form->{radier}) {
@@ -881,9 +883,9 @@ $follow_ups_block
     print qq| <input type=button class=submit onclick=set_history_window($form->{id}); name=history id=history value=| . $locale->text('history') . qq|> |;
   }
   # /button for saving history
-  # mark_as_paid button 
-  if($form->{id} ne "") {  
-    print qq|<input type="submit" class="submit" name="action" value="| 
+  # mark_as_paid button
+  if($form->{id} ne "") {
+    print qq|<input type="submit" class="submit" name="action" value="|
           . $locale->text('mark as paid') . qq|">|;
   }
   # /mark_as_paid button
@@ -903,7 +905,7 @@ sub mark_as_paid {
 
   $auth->assert('general_ledger');
 
-  &mark_as_paid_common(\%myconfig,"ar");  
+  &mark_as_paid_common(\%myconfig,"ar");
 
   $lxdebug->leave_sub();
 }
@@ -919,6 +921,8 @@ sub update {
 
   $form->{invtotal} = 0;
 
+  delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
+
   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
     qw(exchangerate creditlimit creditremaining);
 
@@ -932,17 +936,15 @@ sub update {
     if ($form->{"amount_$i"}) {
       push @a, {};
       my $j = $#a;
-      if (!$form->{"korrektur_$i"}) {
-        my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
-        if ($taxkey > 1) {
-          if ($form->{taxincluded}) {
-            $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
-          } else {
-            $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
-          }
+      my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
+      if ($taxkey > 1) {
+        if ($form->{taxincluded}) {
+          $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
         } else {
-          $form->{"tax_$i"} = 0;
+          $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
         }
+      } else {
+        $form->{"tax_$i"} = 0;
       }
       $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
 
@@ -960,9 +962,19 @@ sub update {
   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
   $form->{invdate} = $form->{transdate};
-  my $save_AR = $form->{AR};
-  check_name("customer");
-  $form->{AR} = $save_AR;
+
+  $form->{invdate} = $form->{transdate};
+
+  my %saved_variables = map +( $_ => $form->{$_} ), qw(AR AR_amount_1 taxchart_1);
+
+  &check_name("customer");
+
+  $form->{AR} = $saved_variables{AR};
+  if ($saved_variables{AR_amount_1} =~ m/.--./) {
+    map { $form->{$_} = $saved_variables{$_} } qw(AR_amount_1 taxchart_1);
+  } else {
+    delete $form->{taxchart_1};
+  }
 
   $form->{invtotal} =
     ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
@@ -1009,7 +1021,7 @@ sub post_payment {
 
       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
-#      $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
+      $form->error($locale->text('Cannot post payment for a closed period!')) if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
       if ($form->{currency} ne $form->{defaultcurrency}) {
 #        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
@@ -1050,9 +1062,9 @@ sub post {
 
   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
-  $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
+  $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
 
-  $form->error($locale->text('Zero amount posting!')) 
+  $form->error($locale->text('Zero amount posting!'))
     unless grep $_*1, map $form->parse_amount(\%myconfig, $form->{"amount_$_"}), 1..$form->{rowcount};
 
   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
@@ -1067,7 +1079,7 @@ sub post {
       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
       $form->error($locale->text('Cannot post payment for a closed period!'))
-        if ($datepaid <= $closedto);
+        if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
 
       if ($form->{currency} ne $form->{defaultcurrency}) {
         $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
@@ -1096,7 +1108,7 @@ sub post {
     $form->{addition} = "POSTED";
     $form->save_history($form->dbconnect(\%myconfig));
   }
-  # /saving the history 
+  # /saving the history
   remove_draft() if $form->{remove_draft};
 
   $form->redirect($locale->text('Transaction posted!')) unless $inline;
@@ -1116,7 +1128,7 @@ sub post_as_new {
        $form->{addition} = "POSTED AS NEW";
        $form->save_history($form->dbconnect(\%myconfig));
   }
-  # /saving the history 
+  # /saving the history
   &post;
 
   $lxdebug->leave_sub();
@@ -1189,7 +1201,7 @@ sub yes {
          $form->{addition} = "DELETED";
          $form->save_history($form->dbconnect(\%myconfig));
     }
-    # /saving the history 
+    # /saving the history
     $form->redirect($locale->text('Transaction deleted!'));
   }
   $form->error($locale->text('Cannot delete transaction!'));
@@ -1234,9 +1246,9 @@ sub search {
 | if $form->{selectdepartment};
 
   $form->{title} = $locale->text('AR Transactions');
-  
+
   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
-  
+
   # use JavaScript Calendar or not
   $form->{jsscript} = 1;
   $jsscript = "";
@@ -1377,7 +1389,7 @@ sub search {
              <tr>
                <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
                <td nowrap>| . $locale->text('Notes') . qq|</td>
-               <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
+               <td align=right><input name="l_salesman" class=checkbox type=checkbox value=Y></td>
                <td nowrap>| . $locale->text('Salesperson') . qq|</td>
                <td align=right><input name="l_shippingpoint" class=checkbox type=checkbox value=Y></td>
                <td nowrap>| . $locale->text('Shipping Point') . qq|</td>
@@ -1385,10 +1397,12 @@ sub search {
                <td nowrap>| . $locale->text('Ship via') . qq|</td>
              </tr>
              <tr>
-               <td align=right><input name="l_marge_total" class=checkbox type=checkbox value=Y></td><td> |
-    .             $locale->text('Ertrag') . qq|</td>
-               <td align=right><input name="l_marge_percent" class=checkbox type=checkbox value=Y></td><td> |
-    .             $locale->text('Ertrag prozentual') . qq|</td>
+               <td align=right><input name="l_marge_total" class=checkbox type=checkbox value=Y></td>
+                <td nowrap> | . $locale->text('Ertrag') . qq|</td>
+               <td align=right><input name="l_marge_percent" class=checkbox type=checkbox value=Y></td>
+                <td nowrap> | . $locale->text('Ertrag prozentual') . qq|</td>
+               <td align=right><input name="l_employee" class=checkbox type=checkbox value=Y></td>
+               <td nowrap>| . $locale->text('Employee') . qq|</td>
              </tr>
              <tr>
                <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
@@ -1398,6 +1412,26 @@ sub search {
                <td align=right><input name="l_transaction_description" class=checkbox type=checkbox value=Y></td>
                <td nowrap>| . $locale->text('Transaction description') . qq|</td>
              </tr>
+          <tr>
+        <td colspan=4 align=left><b>| . $locale->text('Customer') . qq| </td>
+          </tr>
+          <tr>
+               <td align=right><input name="l_customernumber" class=checkbox type=checkbox value=Y></td>
+               <td nowrap>| . $locale->text('Customer Number') . qq|</td>
+               <td align=right><input name="l_country" class=checkbox type=checkbox value=Y></td>
+               <td nowrap>| . $locale->text('Country') . qq|</td>
+               <td align=right><input name="l_ustid" class=checkbox type=checkbox value=Y></td>
+               <td nowrap>| . $locale->text('USt-IdNr.') . qq|</td>
+               <td align=right><input name="l_taxzone" class=checkbox type=checkbox value=Y></td>
+               <td nowrap>| . $locale->text('Steuersatz') . qq|</td>
+          </tr>
+          <tr>
+               <td align=right><input name="l_payment_terms" class=checkbox type=checkbox value=Y></td>
+               <td nowrap>| . $locale->text('Payment Terms') . qq|</td>
+               <td align=right><input name="l_charts" class=checkbox type=checkbox value=Y></td>
+               <td nowrap>| . $locale->text('Buchungskonto') . qq|</td>
+          </tr>
+
            </table>
          </td>
        </tr>
@@ -1455,7 +1489,7 @@ sub ar_transactions {
   $form->{customer} = $form->unescape($form->{customer});
   ($form->{customer}, $form->{customer_id}) = split(/--/, $form->{customer});
 
-  $form->{sort} ||= 'transdate';
+  report_generator_set_default_sort('transdate', 1);
 
   AR->ar_transactions(\%myconfig, \%$form);
 
@@ -1465,8 +1499,8 @@ sub ar_transactions {
 
   @columns =
     qw(transdate id type invnumber ordnumber name netamount tax amount paid
-       datepaid due duedate transaction_description notes employee shippingpoint shipvia
-       marge_total marge_percent globalprojectnumber);
+       datepaid due duedate transaction_description notes salesman employee shippingpoint shipvia
+       marge_total marge_percent globalprojectnumber customernumber country ustid taxzone payment_terms charts);
 
   my @hidden_variables = map { "l_${_}" } @columns;
   push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto);
@@ -1489,17 +1523,24 @@ sub ar_transactions {
     'duedate'                 => { 'text' => $locale->text('Due Date'), },
     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
     'notes'                   => { 'text' => $locale->text('Notes'), },
-    'employee'                => { 'text' => $locale->text('Salesperson'), },
+    'salesman'                => { 'text' => $locale->text('Salesperson'), },
+    'employee'                => { 'text' => $locale->text('Employee'), },
     'shippingpoint'           => { 'text' => $locale->text('Shipping Point'), },
     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
     'marge_total'             => { 'text' => $locale->text('Ertrag'), },
     'marge_percent'           => { 'text' => $locale->text('Ertrag prozentual'), },
+    'customernumber'          => { 'text' => $locale->text('Customer Number'), },
+    'country'                 => { 'text' => $locale->text('Country'), },
+    'ustid'                   => { 'text' => $locale->text('USt-IdNr.'), },
+    'taxzone'                 => { 'text' => $locale->text('Steuersatz'), },
+    'payment_terms'           => { 'text' => $locale->text('Payment Terms'), },
+    'charts'                  => { 'text' => $locale->text('Buchungskonto'), },
   );
 
-  foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid
-                       employee shippingpoint shipvia)) {
-    $column_defs{$name}->{link} = $href . "&sort=$name";
+  foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
+    my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
+    $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
   }
 
   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
@@ -1510,9 +1551,9 @@ sub ar_transactions {
   $report->set_columns(%column_defs);
   $report->set_column_order(@columns);
 
-  $report->set_export_options('ar_transactions', @hidden_variables);
+  $report->set_export_options('ar_transactions', @hidden_variables, qw(sort sortdir));
 
-  $report->set_sort_indicator($form->{sort}, 1);
+  $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
   my @options;
   if ($form->{customer}) {
@@ -1535,10 +1576,10 @@ sub ar_transactions {
     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
   }
   if ($form->{transdatefrom}) {
-    push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
+    push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
   }
   if ($form->{transdateto}) {
-    push @options, $locale->text('Bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdateto}, 1);
+    push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
   }
   if ($form->{open}) {
     push @options, $locale->text('Open');
@@ -1580,8 +1621,8 @@ sub ar_transactions {
 
     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due marge_total marge_percent);
 
-    my $is_storno  = $ar->{storno} && !$ar->{storno_id};
-    my $has_storno = $ar->{storno} && $ar->{storno_id};
+    my $is_storno  = $ar->{storno} &&  $ar->{storno_id};
+    my $has_storno = $ar->{storno} && !$ar->{storno_id};
 
     $ar->{type} =
       $has_storno       ? $locale->text("Invoice with Storno (abbreviation)") :
@@ -1642,9 +1683,9 @@ sub storno {
     $form->{addition} = "STORNO";
     $form->save_history($form->dbconnect(\%myconfig));
   }
-  # /saving the history 
+  # /saving the history
 
-  $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id}); 
+  $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
 
   $lxdebug->leave_sub();
 }