Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[kivitendo-erp.git] / bin / mozilla / ap.pl
index 7829d74..7b16657 100644 (file)
@@ -97,7 +97,7 @@ sub add {
 
   AP->get_transdate(\%myconfig, $form);
   $form->{initial_transdate} = $form->{transdate};
-  &create_links;
+  create_links(dont_save => 1);
   $form->{transdate} = $form->{initial_transdate};
   &display_form;
 
@@ -113,7 +113,7 @@ sub edit {
 
   $form->{title} = "Edit";
 
-  &create_links;
+  create_links();
   &display_form;
 
   $main::lxdebug->leave_sub();
@@ -135,18 +135,23 @@ sub display_form {
 sub create_links {
   $main::lxdebug->enter_sub();
 
+  my %params   = @_;
+
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
   $main::auth->assert('general_ledger');
 
   $form->create_links("AP", \%myconfig, "vendor");
-  my $taxincluded = $form->{taxincluded};
-  my $duedate     = $form->{duedate};
+  my %saved;
+  if (!$params{dont_save}) {
+    %saved = map { ($_ => $form->{$_}) } qw(direct_debit taxincluded);
+    $saved{duedate} = $form->{duedate} if $form->{duedate};
+  }
 
   IR->get_vendor(\%myconfig, \%$form);
-  $form->{taxincluded} = $taxincluded;
-  $form->{duedate}   = $duedate if $duedate;
+
+  $form->{$_}        = $saved{$_} for keys %saved;
   $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
   $form->{rowcount}  = 1;
 
@@ -193,7 +198,7 @@ sub form_header {
   $main::auth->assert('general_ledger');
 
   $form->{title_} = $form->{title};
-  $form->{title} = $locale->text($form->{title} .' Accounts Payables Transaction');
+  $form->{title} = $form->{title} eq 'Add' ? $locale->text('Add Accounts Payables Transaction') : $locale->text('Edit Accounts Payables Transaction');
 
   # type=submit $locale->text('Add Accounts Payables Transaction')
   # type=submit $locale->text('Edit Accounts Payables Transaction')
@@ -255,53 +260,42 @@ sub form_header {
   );
 
   my %project_labels = ();
-  my @project_values = ("");
   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
-    push(@project_values, $item->{id});
     $project_labels{$item->{id}} = $item->{projectnumber};
   }
 
-  my (%AP_amount_labels, @AP_amount_values);
-  my (%AP_labels, @AP_values);
-  my (%AP_paid_labels, @AP_paid_values);
   my %charts;
   my $taxchart_init;
 
   foreach my $item (@{ $form->{ALL_CHARTS} }) {
-    if (grep({ $_ eq "AP_amount" } @{ $item->{link_split} })) {
-      $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
-      my $key = "$item->{accno}--$item->{tax_id}";
-      push(@AP_amount_values, $key);
-      $AP_amount_labels{$key} =
-        "$item->{accno}--$item->{description}";
-
-    } elsif (grep({ $_ eq "AP" } @{ $item->{link_split} })) {
-      push(@AP_values, $item->{accno});
-      $AP_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
-
-    } elsif (grep({ $_ eq "AP_paid" } @{ $item->{link_split} })) {
-      push(@AP_paid_values, $item->{accno});
-      $AP_paid_labels{$item->{accno}} =
-        "$item->{accno}--$item->{description}";
+    if ( grep({ $_ eq 'AP_amount' } @{ $item->{link_split} }) ) {
+      if ( $taxchart_init eq '' ) {
+        $taxchart_init = $item->{tax_id};
+      }
+
+      push(@{ $form->{ALL_CHARTS_AP_amount} }, $item);
+    }
+    elsif ( grep({ $_ eq 'AP' } @{ $item->{link_split} }) ) {
+      push(@{ $form->{ALL_CHARTS_AP} }, $item);
+    }
+    elsif ( grep({ $_ eq 'AP_paid' } @{ $item->{link_split} }) ) {
+      push(@{ $form->{ALL_CHARTS_AP_paid} }, $item);
     }
 
     $charts{$item->{accno}} = $item;
   }
 
-  my %taxchart_labels = ();
-  my @taxchart_values = ();
   my %taxcharts = ();
   foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
-    my $key = "$item->{id}--$item->{rate}";
-    $taxchart_init = $key if ($taxchart_init eq $item->{id});
-    push(@taxchart_values, $key);
-    $taxchart_labels{$key} =
-      "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
+    my $key = $item->{id} .'--'. $item->{rate};
+
+    if ( $taxchart_init eq $item->{id} ) {
+      $taxchart_init = $key;
+    }
+
     $taxcharts{$item->{id}} = $item;
   }
 
-  $form->{jsscript} = 1;
-
   my $follow_up_vc         =  $form->{vendor};
   $follow_up_vc            =~ s/--.*?//;
   my $follow_up_trans_info =  "$form->{invnumber} ($follow_up_vc)";
@@ -310,13 +304,7 @@ sub form_header {
   $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->{globalprojectnumber} =
-    NTI($cgi->popup_menu('-name' => "globalproject_id",
-                         '-values' => \@project_values,
-                         '-labels' => \%project_labels,
-                         '-default' => $form->{"globalproject_id"} ));
-
-  $form->header;
+  $form->header();
 
   for my $i (1 .. $form->{rowcount}) {
 
@@ -342,56 +330,56 @@ sub form_header {
 
     $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
 
-    $form->{'selectAP_amount_'. $i} =
-      NTI($cgi->popup_menu('-name' => "AP_amount_$i",
-                           '-id' => "AP_amount_$i",
-                           '-style' => 'width:400px',
-                           '-onChange' => "setTaxkey(this, $i)",
-                           '-values' => \@AP_amount_values,
-                           '-labels' => \%AP_amount_labels,
-                           '-default' => $selected_accno_full))
-      . $cgi->hidden('-name' => "previous_AP_amount_$i",
-                     '-default' => $selected_accno_full);
-
-    $form->{'select_tax_'. $i} =
-      NTI($cgi->popup_menu('-name' => "taxchart_$i",
-                           '-id' => "taxchart_$i",
-                           '-style' => 'width:200px',
-                           '-values' => \@taxchart_values,
-                           '-labels' => \%taxchart_labels,
-                           '-default' => $selected_taxchart));
-
-    $form->{'select_projectnumber_'. $i} =
-      NTI($cgi->popup_menu('-name' => "project_id_$i",
-                           '-values' => \@project_values,
-                           '-labels' => \%project_labels,
-                           '-default' => ($i==$form->{rowcount})? $form->{globalproject_id} : $form->{"project_id_$i"} ));
+    $form->{'selected_accno_full_'. $i} = $selected_accno_full;
+
+    $form->{'selected_taxchart_'. $i} = $selected_taxchart;
   }
 
+  $form->{AP_amount_value_title_sub} = sub {
+    my $item = shift;
+    return [
+      $item->{accno} .'--'. $item->{tax_id},
+      $item->{accno} .'--'. $item->{description},
+    ];
+  };
+
+  $form->{taxchart_value_title_sub} = sub {
+    my $item = shift;
+    return [
+      $item->{id} .'--'. $item->{rate},
+      $item->{taxdescription} .' '. ($item->{rate} * 100) .' %',
+    ];
+  };
+
+  $form->{AP_paid_value_title_sub} = sub {
+    my $item = shift;
+    return [
+      $item->{accno},
+      $item->{accno} .'--'. $item->{description}
+    ];
+  };
+
+  $form->{APselected_value_title_sub} = sub {
+    my $item = shift;
+    return [
+      $item->{accno},
+      $item->{accno} .'--'. $item->{description}
+    ];
+  };
 
   $form->{invtotal_unformatted} = $form->{invtotal};
   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
 
-  $form->{APselected} =
-    NTI($cgi->popup_menu('-name' => "APselected", '-id' => "APselected",
-                         '-style' => 'width:400px',
-                         '-values' => \@AP_values, '-labels' => \%AP_labels,
-                         '-default' => $form->{APselected}));
-
   $form->{totalpaid} = 0;
 
   if ( $form->{'paid_'. $form->{paidaccounts}} ) {
     $form->{paidaccounts}++;
   }
-  for my $i (1 .. $form->{paidaccounts}) {
 
-    $form->{'selectAP_paid_'. $i} =
-      NTI($cgi->popup_menu('-name' => "AP_paid_$i",
-                           '-id' => "AP_paid_$i",
-                           '-values' => \@AP_paid_values,
-                           '-labels' => \%AP_paid_labels,
-                           '-default' => $form->{"AP_paid_$i"}));
+  # default account for current assets (i.e. 1801 - SKR04)
+  $form->{accno_arap} = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
 
+  for my $i (1 .. $form->{paidaccounts}) {
     $form->{totalpaid} += $form->{"paid_$i"};
 
     # format amounts
@@ -417,15 +405,7 @@ sub form_header {
 
     $form->{'paidaccount_changeable_'. $i} = $changeable;
 
-    if ( $changeable ) {
-      $form->{'selectpaid_project_id_'. $i} =
-        NTI($cgi->popup_menu('-name' => "paid_project_id_$i",
-          '-values' => \@project_values,
-          '-labels' => \%project_labels,
-          '-default' => $form->{"paid_project_id_$i"}));
-    } else {
-      $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
-    }
+    $form->{'labelpaid_project_id_'. $i} = $project_labels{$form->{'paid_project_id_'. $i}};
   }
 
   $form->{paid_missing} = $form->{invtotal_unformatted} - $form->{totalpaid};
@@ -630,10 +610,11 @@ sub post {
 
   my ($inline) = @_;
 
-  # check if there is a vendor, invoice and due date
+  # check if there is a vendor, invoice, due date and invnumber
   $form->isblank("transdate", $locale->text("Invoice Date missing!"));
   $form->isblank("duedate",   $locale->text("Due Date missing!"));
   $form->isblank("vendor",    $locale->text('Vendor missing!'));
+  $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
 
   if ($myconfig{mandatory_departments} && !$form->{department}) {
     $form->{saved_message} = $::locale->text('You have to specify a department.');
@@ -643,6 +624,9 @@ sub post {
 
   my $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
   my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
+
+  $form->error($locale->text('Cannot post transaction above the maximum future booking date!'))
+    if ($form->date_max_future($form->{"transdate"}, \%myconfig));
   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
 
   my $zero_amount_posting = 1;
@@ -816,7 +800,7 @@ sub yes {
 sub search {
   $main::lxdebug->enter_sub();
 
-  $main::auth->assert('general_ledger | vendor_invoice_edit');
+  $main::auth->assert('vendor_invoice_edit');
 
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
@@ -826,15 +810,12 @@ sub search {
   $form->all_vc(\%myconfig, "vendor", "AP");
 
   $form->{title}    = $locale->text('AP Transactions');
-  $::request->{layout}->focus('#vendor');
-  $form->{jsscript} = 1;
 
   $form->get_lists("projects"     => { "key" => "ALL_PROJECTS", "all" => 1 },
                    "departments"  => "ALL_DEPARTMENTS",
                    "vendors"      => "ALL_VC");
 
   # constants and subs for template
-  $form->{jsscript}  = 1;
   $form->{vc_keys}   = sub { "$_[0]->{name}--$_[0]->{id}" };
 
   $form->header;
@@ -871,7 +852,7 @@ sub ap_transactions {
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
 
-  $main::auth->assert('general_ledger | vendor_invoice_edit');
+  $main::auth->assert('vendor_invoice_edit');
 
   ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});