Zwei neue Features:
[kivitendo-erp.git] / bin / mozilla / ar.pl
index 13c3802..88a9b97 100644 (file)
 #======================================================================
 
 use POSIX qw(strftime);
+use List::Util qw(sum);
 
 use SL::AR;
+use SL::FU;
 use SL::IS;
 use SL::PE;
 use SL::ReportGenerator;
@@ -47,7 +49,7 @@ our ($cgi, $form, $lxdebug, $locale, %myconfig);
 require "bin/mozilla/arap.pl";
 require "bin/mozilla/common.pl";
 require "bin/mozilla/drafts.pl";
-require "bin/mozilla/report_generator.pl";
+require "bin/mozilla/reportgenerator.pl";
 
 1;
 
@@ -84,6 +86,8 @@ require "bin/mozilla/report_generator.pl";
 sub add {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   return $lxdebug->leave_sub() if (load_draft_maybe());
 
   # saving the history
@@ -95,9 +99,7 @@ sub add {
   # /saving the history 
   
   $form->{title}    = "Add";
-  $form->{callback} =
-    "$form->{script}?action=add&login=$form->{login}&password=$form->{password}"
-    unless $form->{callback};
+  $form->{callback} = "ar.pl?action=add" unless $form->{callback};
 
   AR->get_transdate(\%myconfig, $form);
   $form->{initial_transdate} = $form->{transdate};
@@ -109,6 +111,9 @@ sub add {
 
 sub edit {
   $lxdebug->enter_sub();
+
+  $auth->assert('general_ledger');
+
   # show history button
   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
   #/show hhistory button
@@ -124,6 +129,8 @@ sub edit {
 sub display_form {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   &form_header;
   &form_footer;
 
@@ -133,6 +140,8 @@ sub display_form {
 sub create_links {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   my ($duedate, $taxincluded, @curr);
 
   $form->create_links("AR", \%myconfig, "customer");
@@ -200,6 +209,8 @@ sub create_links {
 sub form_header {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   my ($title, $readonly, $exchangerate, $rows);
   my ($taxincluded, $notes, $department, $customer, $employee, $amount, $project);
   my ($jsscript, $button1, $button2, $onload);
@@ -422,9 +433,14 @@ sub form_header {
       qq|<td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\"></td>|;
   }
 
+  my $follow_up_vc         =  $form->{customer};
+  $follow_up_vc            =~ s/--.*?//;
+  my $follow_up_trans_info =  "$form->{invnumber} ($follow_up_vc)";
+
   $form->{javascript} .=
     qq|<script type="text/javascript" src="js/common.js"></script>| .
-    qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
+    qq|<script type="text/javascript" src="js/show_vc_details.js"></script>| .
+    qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
 
   $form->header;
   $onload = qq|focus()|;
@@ -440,6 +456,10 @@ sub form_header {
 <input type=hidden name=closedto value=$form->{closedto}>
 <input type=hidden name=locked value=$form->{locked}>
 <input type=hidden name=title value="$title">
+<input type="hidden" name="follow_up_trans_id_1" value="| . H($form->{id}) . qq|">
+<input type="hidden" name="follow_up_trans_type_1" value="ar_transaction">
+<input type="hidden" name="follow_up_trans_info_1" value="| . H($follow_up_trans_info) . qq|">
+<input type="hidden" name="follow_up_rowcount" value="1">
 
 | . ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") . qq|
 
@@ -787,16 +807,27 @@ $jsscript
 sub form_footer {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   my ($transdate, $closedto);
 
+  my $follow_ups_block;
+  if ($form->{id}) {
+    my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
+
+    if (@{ $follow_ups} ) {
+      my $num_due       = sum map { $_->{due} * 1 } @{ $follow_ups };
+      $follow_ups_block = qq|<p>| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</p>|;
+    }
+  }
+
   print qq|
 
+$follow_ups_block
+
 <input name=gldate type=hidden value="| . Q($form->{gldate}) . qq|">
 
 <input name=callback type=hidden value="$form->{callback}">
-
-<input type=hidden name=login value=$form->{login}>
-<input type=hidden name=password value=$form->{password}>
 |
 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
@@ -820,8 +851,8 @@ sub form_footer {
   print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">\n|;
 
   # 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') && !$form->{paid_1});
+  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}) {
     if ($form->{radier}) {
@@ -834,11 +865,14 @@ sub form_footer {
         <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|"> |;
     }
     print qq|
-        <input class=submit type=submit name=action value="| . $locale->text('Post Payment') .    qq|"> |;
+        <input class=submit type=submit name=action value="| . $locale->text('Post Payment') .    qq|">
+        <input type="button" class="submit" onclick="follow_up_window()" value="|
+      . $locale->text('Follow-Up')
+      . qq|"> |;
 
   } else {
     if ($transdate > $closedto) {
-      print qq|<input class=submit type=submit name=action value="| . $locale->text('Post') .     qq|"> | .
+      print qq| <input class=submit type=submit name=action value="| . $locale->text('Post') .     qq|"> | .
         NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
     }
   }
@@ -849,7 +883,7 @@ sub form_footer {
   }
   # button for saving history
   if($form->{id} ne "") {
-    print qq|<input type=button class=submit onclick=set_history_window($form->{id}); name=history id=history value=| . $locale->text('history') . qq|>|;
+    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 
@@ -871,13 +905,19 @@ sub form_footer {
 
 sub mark_as_paid {
   $lxdebug->enter_sub();
+
+  $auth->assert('general_ledger');
+
   &mark_as_paid_common(\%myconfig,"ar");  
+
   $lxdebug->leave_sub();
 }
 
 sub update {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   my $display = shift;
 
   my ($totaltax, $exchangerate, $totalpaid);
@@ -973,6 +1013,8 @@ sub update {
 sub post_payment {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
   for my $i (1 .. $form->{paidaccounts}) {
@@ -1000,6 +1042,9 @@ sub post_payment {
 }
 
 sub _post {
+
+  $auth->assert('general_ledger');
+
   # inline post
   post(1);
 }
@@ -1007,6 +1052,8 @@ sub _post {
 sub post {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   my ($inline) = @_;
 
   my ($datepaid);
@@ -1075,6 +1122,8 @@ sub post {
 sub post_as_new {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   $form->{postasnew} = 1;
   # saving the history
   if(!exists $form->{addition} && $form->{id} ne "") {
@@ -1091,6 +1140,8 @@ sub post_as_new {
 sub use_as_template {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
   $form->{paidaccounts} = 1;
   $form->{rowcount}--;
@@ -1103,6 +1154,8 @@ sub use_as_template {
 sub delete {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   $form->{title} = $locale->text('Confirm!');
 
   $form->header;
@@ -1116,6 +1169,7 @@ sub delete {
 |;
 
   foreach my $key (keys %$form) {
+    next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
     $form->{$key} =~ s/\"/&quot;/g;
     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
   }
@@ -1140,6 +1194,9 @@ sub delete {
 
 sub yes {
   $lxdebug->enter_sub();
+
+  $auth->assert('general_ledger');
+
   if (AR->delete_transaction(\%myconfig, \%$form)) {
     # saving the history
     if(!exists $form->{addition}) {
@@ -1158,6 +1215,8 @@ sub yes {
 sub search {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger | invoice_edit');
+
   my ($customer, $department);
   my ($jsscript, $button1, $button2, $onload);
 
@@ -1340,6 +1399,12 @@ sub search {
                <td align=right><input name="l_shipvia" class=checkbox type=checkbox value=Y></td>
                <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>
+             </tr>
              <tr>
                <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
                <td nowrap>| . $locale->text('Subtotal') . qq|</td>
@@ -1361,9 +1426,6 @@ sub search {
 
 <input type=hidden name=nextsub value=$form->{nextsub}>
 
-<input type=hidden name=login value=$form->{login}>
-<input type=hidden name=password value=$form->{password}>
-
 <br>
 <input class=submit type=submit name=action value="|
     . $locale->text('Continue') . qq|">
@@ -1401,6 +1463,8 @@ sub create_subtotal_row {
 sub ar_transactions {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger | invoice_edit');
+
   my ($callback, $href, @columns);
 
   $form->{customer} = $form->unescape($form->{customer});
@@ -1417,7 +1481,7 @@ sub ar_transactions {
   @columns =
     qw(transdate id type invnumber ordnumber name netamount tax amount paid
        datepaid due duedate transaction_description notes employee shippingpoint shipvia
-       globalprojectnumber);
+       marge_total marge_percent globalprojectnumber);
 
   my @hidden_variables = map { "l_${_}" } @columns;
   push @hidden_variables, "l_subtotal", qw(open closed customer invnumber ordnumber transaction_description notes project_id transdatefrom transdateto);
@@ -1444,6 +1508,8 @@ sub ar_transactions {
     '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'), },
   );
 
   foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid
@@ -1510,7 +1576,7 @@ sub ar_transactions {
   # escape callback for href
   $callback = $form->escape($href);
 
-  my @subtotal_columns = qw(netamount amount paid due);
+  my @subtotal_columns = qw(netamount amount paid due marge_total marge_percent);
 
   my %totals    = map { $_ => 0 } @subtotal_columns;
   my %subtotals = map { $_ => 0 } @subtotal_columns;
@@ -1524,10 +1590,13 @@ sub ar_transactions {
     map { $subtotals{$_} += $ar->{$_};
           $totals{$_}    += $ar->{$_} } @subtotal_columns;
 
-    map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(netamount tax amount paid due);
+    $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
+    $totals{marge_percent}    = $totals{netamount}    ? ($totals{marge_total}    * 100 / $totals{netamount}   ) : 0;
 
-    my $is_storno  = $ar->{storno} && IS->is_storno(\%myconfig, $form, 'ar');
-    my $has_storno = $ar->{storno} && !$is_storno;
+    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};
 
     $ar->{type} =
       $has_storno       ? $locale->text("Invoice with Storno (abbreviation)") :
@@ -1572,6 +1641,8 @@ sub ar_transactions {
 sub storno {
   $lxdebug->enter_sub();
 
+  $auth->assert('general_ledger');
+
   # don't cancel cancelled transactions
   if (IS->has_storno(\%myconfig, $form, 'ar')) {
     $form->{title} = $locale->text("Cancel Accounts Receivables Transaction");