Merge branch 'mass_convert_delivery_orders_to_invoice'
[kivitendo-erp.git] / bin / mozilla / ap.pl
index dddf6c5..0460483 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')
@@ -291,8 +296,6 @@ sub form_header {
     $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)";
@@ -372,6 +375,10 @@ sub form_header {
   if ( $form->{'paid_'. $form->{paidaccounts}} ) {
     $form->{paidaccounts}++;
   }
+
+  # 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"};
 
@@ -476,23 +483,15 @@ sub update {
   my $count = 0;
   my (@a, $j, $totaltax);
   for my $i (1 .. $form->{rowcount}) {
-    $form->{"amount_$i"} =
-      $form->parse_amount(\%myconfig, $form->{"amount_$i"});
-    $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
+    $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
     if ($form->{"amount_$i"}) {
       push @a, {};
       $j = $#a;
       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;
-        }
-      } else {
-        $form->{"tax_$i"} = 0;
-      }
-      $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
+
+      # calculate tax exactly the same way as AP in post_transaction via form->calculate_tax
+      my $tmpnetamount;
+      ($tmpnetamount,$form->{"tax_$i"}) = $form->calculate_tax($form->{"amount_$i"},$rate,$form->{taxincluded},2);
 
       $totaltax += $form->{"tax_$i"};
       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
@@ -583,9 +582,15 @@ sub post_payment {
 
   ($form->{AP})      = split /--/, $form->{AP};
   ($form->{AP_paid}) = split /--/, $form->{AP_paid};
-  $form->redirect($locale->text('Payment posted!'))
-      if (AP->post_payment(\%myconfig, \%$form));
+  if (AP->post_payment(\%myconfig, \%$form)) {
+    $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
+    $form->{what_done} = 'invoice';
+    $form->{addition}  = "PAYMENT POSTED";
+    $form->save_history;
+    $form->redirect($locale->text('Payment posted!'))
+  } else {
     $form->error($locale->text('Cannot post payment!'));
+  };
 
 
   $main::lxdebug->leave_sub();
@@ -603,10 +608,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.');
@@ -616,6 +622,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;
@@ -670,8 +679,9 @@ sub post {
   if (AP->post_transaction(\%myconfig, \%$form)) {
     # saving the history
     if(!exists $form->{addition} && $form->{id} ne "") {
-      $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
-      $form->{addition} = "POSTED";
+      $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
+      $form->{addition}  = "POSTED";
+      $form->{what_done} = "invoice";
       $form->save_history;
     }
     # /saving the history
@@ -696,8 +706,12 @@ sub post_as_new {
   $form->{postasnew} = 1;
   # saving the history
   if(!exists $form->{addition} && $form->{id} ne "") {
-    $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
-    $form->{addition} = "POSTED AS NEW";
+    # does this work? post_as_new for ap doesn't immediately save the
+    # invoice, because the invnumber has to be entered by hand.
+    # And the value of $form->{postasnew} isn't checked when calling post
+    $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
+    $form->{addition}  = "POSTED AS NEW";
+    $form->{what_done} = "invoice";
     $form->save_history;
   }
   # /saving the history
@@ -774,8 +788,9 @@ sub yes {
   if (AP->delete_transaction(\%myconfig, \%$form)) {
     # saving the history
     if(!exists $form->{addition}) {
-      $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
-      $form->{addition} = "DELETED";
+      $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
+      $form->{addition}  = "DELETED";
+      $form->{what_done} = "invoice";
       $form->save_history;
     }
     # /saving the history
@@ -789,7 +804,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;
@@ -799,15 +814,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;
@@ -844,7 +856,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});
 
@@ -911,6 +923,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('Invoice Number')          . " : $form->{invnumber}"                      if ($form->{invnumber});
   push @options, $locale->text('Order Number')            . " : $form->{ordnumber}"                      if ($form->{ordnumber});
@@ -1021,8 +1034,9 @@ sub storno {
 
   # saving the history
   if(!exists $form->{addition} && $form->{id} ne "") {
-    $form->{snumbers} = "ordnumber_$form->{ordnumber}";
-    $form->{addition} = "STORNO";
+    $form->{snumbers}  = qq|invnumber_| . $form->{invnumber};
+    $form->{addition}  = "STORNO";
+    $form->{what_done} = "invoice";
     $form->save_history;
   }
   # /saving the history