Mahnungen: Funktion »Diese Sprache verwenden« gefixt
[kivitendo-erp.git] / bin / mozilla / ap.pl
index b3b111a..3440f93 100644 (file)
@@ -24,7 +24,8 @@
 # GNU General Public License for more details.
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1335, USA.
 #======================================================================
 #
 # Accounts Payables
@@ -39,14 +40,14 @@ use SL::AP;
 use SL::FU;
 use SL::IR;
 use SL::IS;
-use SL::PE;
 use SL::ReportGenerator;
 use SL::DB::Default;
 use SL::DB::PurchaseInvoice;
+use SL::Webdav;
+use SL::Locale::String qw(t8);
 
 require "bin/mozilla/arap.pl";
 require "bin/mozilla/common.pl";
-require "bin/mozilla/drafts.pl";
 require "bin/mozilla/reportgenerator.pl";
 
 use strict;
@@ -89,18 +90,17 @@ sub add {
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
-  $main::auth->assert('general_ledger');
-
-  return $main::lxdebug->leave_sub() if (load_draft_maybe());
+  $main::auth->assert('ap_transactions');
 
   $form->{title} = "Add";
 
-  $form->{callback} = "ap.pl?action=add&DONT_LOAD_DRAFT=1" unless $form->{callback};
+  $form->{callback} = "ap.pl?action=add" unless $form->{callback};
 
   AP->get_transdate(\%myconfig, $form);
   $form->{initial_transdate} = $form->{transdate};
   create_links(dont_save => 1);
   $form->{transdate} = $form->{initial_transdate};
+
   &display_form;
 
   $main::lxdebug->leave_sub();
@@ -111,7 +111,7 @@ sub edit {
 
   my $form     = $main::form;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   $form->{title} = "Edit";
 
@@ -126,8 +126,21 @@ sub display_form {
 
   my $form     = $main::form;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
+  # get all files stored in the webdav folder
+  if ($form->{invnumber} && $::instance_conf->get_webdav) {
+    my $webdav = SL::Webdav->new(
+      type     => 'accounts_payable',
+      number   => $form->{invnumber},
+    );
+    my $webdav_path = $webdav->webdav_path;
+    my @all_objects = $webdav->get_all_objects;
+    @{ $form->{WEBDAV} } = map { { name => $_->filename,
+                                   type => t8('File'),
+                                   link => File::Spec->catfile($_->full_filedescriptor),
+                               } } @all_objects;
+  }
   &form_header;
   &form_footer;
 
@@ -142,13 +155,15 @@ sub create_links {
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   $form->create_links("AP", \%myconfig, "vendor");
   my %saved;
   if (!$params{dont_save}) {
     %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
     $saved{duedate} = $form->{duedate} if $form->{duedate};
+    $saved{currency} = $form->{currency} if $form->{currency};
+    $saved{taxincluded} = $form->{taxincluded} if $form->{taxincluded};
   }
 
   IR->get_vendor(\%myconfig, \%$form);
@@ -163,6 +178,7 @@ sub create_links {
   # currencies
   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
+  $form->{selectcurrency} = "";
   map { my $quoted = H($_); $form->{selectcurrency} .= "<option value=\"${quoted}\">${quoted}\n" } $form->get_all_currencies(\%myconfig);
 
   # vendors
@@ -172,11 +188,7 @@ sub create_links {
       (@{ $form->{all_vendor} });
   }
 
-  # departments
-  if (@{ $form->{all_departments} || [] }) {
-    $form->{department}       = "$form->{department}--$form->{department_id}";
-    $form->{selectdepartment} = "<option>\n" . join('', map { my $quoted = H("$_->{description}--$_->{id}"); "<option value=\"${quoted}\">${quoted}\n"} @{ $form->{all_departments} || [] });
-  }
+  $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
 
   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
@@ -216,7 +228,7 @@ sub form_header {
   my $locale   = $main::locale;
   my $cgi      = $::request->{cgi};
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   $::form->{invoice_obj} = SL::DB::PurchaseInvoice->new(id => $::form->{id})->load if $::form->{id};
 
@@ -227,7 +239,7 @@ sub form_header {
   # type=submit $locale->text('Edit Accounts Payables Transaction')
 
   # set option selected
-  foreach my $item (qw(vendor currency department)) {
+  foreach my $item (qw(vendor currency)) {
     my $to_replace         =  H($form->{$item});
     $form->{"select$item"} =~ s/ selected//;
     $form->{"select$item"} =~ s/>\Q${to_replace}\E/ selected>${to_replace}/;
@@ -282,6 +294,8 @@ sub form_header {
     @{ $form->{ALL_CHARTS} }
   );
 
+  $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
+
   my %project_labels = ();
   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
     $project_labels{$item->{id}} = $item->{projectnumber};
@@ -326,6 +340,7 @@ sub form_header {
   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
   $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
+  $form->{javascript} .= qq|<script type="text/javascript" src="js/kivi.Draft.js"></script>|;
 
   $form->header();
 
@@ -428,6 +443,11 @@ sub form_header {
       $changeable = (($form->{"gldate_$i"} eq '') || $form->current_date(\%myconfig) eq $form->{"gldate_$i"});
     }
 
+    #deaktivieren von gebuchten Zahlungen ausserhalb der Bücherkontrolle, vorher prüfen ob heute eingegeben
+    if ($form->date_closed($form->{"gldate_$i"})) {
+       $changeable = 0;
+    }
+
     $form->{'paidaccount_changeable_'. $i} = $changeable;
 
     $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
@@ -444,12 +464,12 @@ sub form_header {
 
 sub form_footer {
   $::lxdebug->enter_sub;
-  $::auth->assert('general_ledger');
+  $::auth->assert('ap_transactions');
 
   my $num_due;
   my $num_follow_ups;
   if ($::form->{id}) {
-    my $follow_ups = FU->follow_ups('trans_id' => $::form->{id});
+    my $follow_ups = FU->follow_ups('trans_id' => $::form->{id}, 'not_done' => 1);
 
     if (@{ $follow_ups }) {
       $num_due        = sum map { $_->{due} * 1 } @{ $follow_ups };
@@ -482,20 +502,26 @@ sub mark_as_paid {
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   &mark_as_paid_common(\%myconfig,"ap");
 
   $main::lxdebug->leave_sub();
 }
 
+sub show_draft {
+  $::form->{transdate} = DateTime->today_local->to_kivitendo if !$::form->{transdate};
+  $::form->{gldate}    = $::form->{transdate} if !$::form->{gldate};
+  update();
+}
+
 sub update {
   $main::lxdebug->enter_sub();
 
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   my $display = shift;
 
@@ -583,7 +609,7 @@ sub post_payment {
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
   $form->mtime_ischanged('ap');
 
   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
@@ -596,8 +622,13 @@ sub post_payment {
 
       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
+      $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
+        if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
+
+      #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
+      # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
       $form->error($locale->text('Cannot post payment for a closed period!'))
-        if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
+        if ($form->date_closed($form->{"datepaid_$i"})  && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
 
       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
         $form->{"exchangerate_$i"} = $form->{exchangerate}
@@ -632,7 +663,7 @@ sub post {
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
   $form->mtime_ischanged('ap');
 
   my ($inline) = @_;
@@ -643,7 +674,7 @@ sub post {
   $form->isblank("vendor",    $locale->text('Vendor missing!'));
   $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
 
-  if ($myconfig{mandatory_departments} && !$form->{department}) {
+  if ($myconfig{mandatory_departments} && !$form->{department_id}) {
     $form->{saved_message} = $::locale->text('You have to specify a department.');
     update();
     exit;
@@ -676,8 +707,13 @@ sub post {
 
       $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
 
+      $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
+      if ($form->date_max_future($form->{"datepaid_$i"}, \%myconfig));
+
+      #Zusätzlich noch das Buchungsdatum in die Bücherkontrolle einbeziehen
+      # (Dient zur Prüfung ob ZE oder ZA geprüft werden soll)
       $form->error($locale->text('Cannot post payment for a closed period!'))
-        if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
+        if ($form->date_closed($form->{"datepaid_$i"})  && !$form->date_closed($form->{"gldate_$i"}, \%myconfig));
 
       if ($form->{defaultcurrency} && ($form->{currency} ne $form->{defaultcurrency})) {
         $form->{"exchangerate_$i"} = $form->{exchangerate}
@@ -693,7 +729,7 @@ sub post {
   my ($vendor) = split /--/, $form->{vendor};
   if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
     &update;
-    ::end_of_request();
+    $::dispatcher->end_request;
   }
   my ($debitaccno,    $debittaxkey)    = split /--/, $form->{AP_amountselected};
   my ($taxkey,        $NULL)           = split /--/, $form->{taxchartselected};
@@ -706,6 +742,12 @@ sub post {
   $form->{id} = 0 if $form->{postasnew};
 
   if (AP->post_transaction(\%myconfig, \%$form)) {
+    # create webdav folder
+    if ($::instance_conf->get_webdav) {
+      SL::Webdav->new(type     => 'accounts_payable',
+                      number   => $form->{invnumber},
+                     )->webdav_path;
+    }
     # saving the history
     if(!exists $form->{addition} && $form->{id} ne "") {
       $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
@@ -714,7 +756,6 @@ sub post {
       $form->save_history;
     }
     # /saving the history
-    remove_draft() if $form->{remove_draft};
     # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
     $form->redirect($locale->text('Transaction posted!')) unless $inline;
   } else {
@@ -730,7 +771,7 @@ sub post_as_new {
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   $form->{postasnew} = 1;
   # saving the history
@@ -755,7 +796,7 @@ sub use_as_new {
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
   $form->{paidaccounts} = 1;
@@ -772,7 +813,7 @@ sub delete {
   my $form     = $main::form;
   my $locale   = $main::locale;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   $form->{title} = $locale->text('Confirm!');
 
@@ -812,7 +853,7 @@ sub yes {
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   if (AP->delete_transaction(\%myconfig, \%$form)) {
     # saving the history
@@ -845,9 +886,9 @@ sub search {
   $form->{title}    = $locale->text('AP Transactions');
 
   $form->get_lists("projects"     => { "key" => "ALL_PROJECTS", "all" => 1 },
-                   "departments"  => "ALL_DEPARTMENTS",
                    "vendors"      => "ALL_VC");
 
+  $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
   # constants and subs for template
   $form->{vc_keys}   = sub { "$_[0]->{name}--$_[0]->{id}" };
 
@@ -903,7 +944,7 @@ sub ap_transactions {
        vendornumber country ustid taxzone payment_terms charts direct_debit);
 
   my @hidden_variables = map { "l_${_}" } @columns;
-  push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto department
+  push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto
                                            parts_partnumber parts_description);
 
   my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
@@ -929,7 +970,7 @@ sub ap_transactions {
     'vendornumber'            => { 'text' => $locale->text('Vendor Number'), },
     'country'                 => { 'text' => $locale->text('Country'), },
     'ustid'                   => { 'text' => $locale->text('USt-IdNr.'), },
-    'taxzone'                 => { 'text' => $locale->text('Steuersatz'), },
+    'taxzone'                 => { 'text' => $locale->text('Tax rate'), },
     'payment_terms'           => { 'text' => $locale->text('Payment Terms'), },
     'charts'                  => { 'text' => $locale->text('Buchungskonto'), },
     'direct_debit'            => { 'text' => $locale->text('direct debit'), },
@@ -955,7 +996,7 @@ sub ap_transactions {
   my @options;
   push @options, $locale->text('Vendor')                  . " : $form->{vendor}"                         if ($form->{vendor});
   push @options, $locale->text('Contact Person')          . " : $form->{cp_name}"                        if ($form->{cp_name});
-  push @options, $locale->text('Department')              . " : " . (split /--/, $form->{department})[0] if ($form->{department});
+  push @options, $locale->text('Department')              . " : $form->{department}"                     if ($form->{department});
   push @options, $locale->text('Invoice Number')          . " : $form->{invnumber}"                      if ($form->{invnumber});
   push @options, $locale->text('Order Number')            . " : $form->{ordnumber}"                      if ($form->{ordnumber});
   push @options, $locale->text('Notes')                   . " : $form->{notes}"                          if ($form->{notes});
@@ -1055,7 +1096,7 @@ sub storno {
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('ap_transactions');
 
   if (IS->has_storno(\%myconfig, $form, 'ap')) {
     $form->{title} = $locale->text("Cancel Accounts Payables Transaction");