X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/487d4f1f6ccd3e22c05469f679bf5d0685914611..fbcd5580:/bin/mozilla/is.pl?ds=sidebyside diff --git a/bin/mozilla/is.pl b/bin/mozilla/is.pl index 3e7704972..5ac59b27e 100644 --- a/bin/mozilla/is.pl +++ b/bin/mozilla/is.pl @@ -24,894 +24,528 @@ # 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. #====================================================================== # # Inventory invoicing module # #====================================================================== +use SL::FU; use SL::IS; -use SL::PE; +use SL::OE; +use SL::MoreCommon qw(restore_form save_form); use Data::Dumper; +use DateTime; +use List::MoreUtils qw(uniq); +use List::Util qw(max sum); +use List::UtilsBy qw(sort_by); +use English qw(-no_match_vars); -require "$form->{path}/io.pl"; -require "$form->{path}/arap.pl"; +use SL::DB::Default; +use SL::DB::Customer; +use SL::DB::Department; +use SL::DB::Invoice; +use SL::DB::PaymentTerm; + +require "bin/mozilla/common.pl"; +require "bin/mozilla/io.pl"; + +use strict; 1; # end of main sub add { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my $locale = $main::locale; + + $main::auth->assert('invoice_edit'); - $form->{title} = $locale->text('Add Sales Invoice'); + $form->{show_details} = $::myconfig{show_form_details}; - $form->{callback} = - "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&password=$form->{password}" - unless $form->{callback}; + if ($form->{type} eq "credit_note") { + $form->{title} = $locale->text('Add Credit Note'); - $form{jsscript} = "date"; + if ($form->{storno}) { + $form->{title} = $locale->text('Add Storno Credit Note'); + } + } else { + $form->{title} = $locale->text('Add Sales Invoice'); - if ($myconfig{acs} =~ "AR--Add Sales Invoice" || $myconfig{acs} =~ "AR--AR") - { - $form->error("Access Denied"); } + + + $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback}; + &invoice_links; &prepare_invoice; - $form->{format} ="pdf"; - &display_form; - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } sub edit { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); - $form->{title} = $locale->text('Edit Sales Invoice'); + my $form = $main::form; + my $locale = $main::locale; - if ($myconfig{acs} =~ "AR--Add Sales Invoice" || $myconfig{acs} =~ "AR--AR") - { - $form->error("Access Denied"); - } + $main::auth->assert('invoice_edit'); + + $form->{show_details} = $::myconfig{show_form_details}; + $form->{taxincluded_changed_by_user} = 1; + + # show history button + $form->{javascript} = qq||; + + my ($language_id, $printer_id); if ($form->{print_and_post}) { - $form->{action} = "print"; + $form->{action} = "print"; $form->{resubmit} = 1; + $language_id = $form->{language_id}; + $printer_id = $form->{printer_id}; } + &invoice_links; + if ($form->{type} eq "credit_note") { + $form->{title} = $locale->text('Edit Credit Note'); + $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno}; + } else { + $form->{title} = $locale->text('Edit Sales Invoice'); + $form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno}; + } + &prepare_invoice; + if ($form->{print_and_post}) { + $form->{language_id} = $language_id; + $form->{printer_id} = $printer_id; + } + &display_form; - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } sub invoice_links { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + + $main::auth->assert('invoice_edit'); $form->{vc} = 'customer'; # create links - $form->{webdav} = $webdav; - $form->{lizenzen} = $lizenzen; - $form->create_links("AR", \%myconfig, "customer"); - if ($form->{all_customer}) { - unless ($form->{customer_id}) { - $form->{customer_id} = $form->{all_customer}->[0]->{id}; - } - } - $cp_id = $form->{cp_id}; - IS->get_customer(\%myconfig, \%$form); - - IS->retrieve_invoice(\%myconfig, \%$form); - $form->{cp_id} = $cp_id; - - # currencies - @curr = split /:/, $form->{currencies}; - chomp $curr[0]; - $form->{defaultcurrency} = $curr[0]; - - map { $form->{selectcurrency} .= "\n"; } if ($key eq "AR_paid") { - for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) { - $form->{"AR_paid_$i"} = - "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; + next unless $form->{acc_trans}{$key}; + for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) { + $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; + $form->{"acc_trans_id_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id}; # reverse paid - $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1; - $form->{"datepaid_$i"} = - $form->{acc_trans}{$key}->[$i - 1]->{transdate}; - $form->{"forex_$i"} = $form->{"exchangerate_$i"} = - $form->{acc_trans}{$key}->[$i - 1]->{exchangerate}; - $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source}; - $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo}; + $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1; + $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate}; + $form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate}; + $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate}; + $form->{"forex_$i"} = $form->{"exchangerate_$i"}; + $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source}; + $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo}; $form->{paidaccounts} = $i; } } else { - $form->{$key} = - "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}"; + $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}"; } - } $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts}); $form->{AR} = $form->{AR_1} unless $form->{id}; - $form->{locked} = - ($form->datetonum($form->{invdate}, \%myconfig) <= - $form->datetonum($form->{closedto}, \%myconfig)); + $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig) + <= $form->datetonum($form->{closedto}, \%myconfig)); - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } sub prepare_invoice { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); - $form->{type} = "invoice"; - $form->{formname} = "invoice"; - $form->{media} = "screen"; + my $form = $main::form; + my %myconfig = %main::myconfig; - if ($form->{id}) { + $main::auth->assert('invoice_edit'); - map { $form->{$_} =~ s/\"/"/g } - qw(invnumber ordnumber quonumber shippingpoint shipvia notes intnotes); + if ($form->{type} eq "credit_note") { + $form->{type} = "credit_note"; + $form->{formname} = "credit_note"; + } elsif ($form->{formname} eq "proforma" ) { + $form->{type} = "invoice"; + } else { + $form->{type} = "invoice"; + $form->{formname} = "invoice"; + } + + if ($form->{id}) { - # # get pricegroups for parts - # IS->get_pricegroups_for_parts(\%myconfig, \%$form); + my $i = 0; - foreach $ref (@{ $form->{invoice_details} }) { + foreach my $ref (@{ $form->{invoice_details} }) { $i++; + map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref}; - $form->{"discount_$i"} = - $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100); - ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/); - $dec = length $dec; - $decimalplaces = ($dec > 2) ? $dec : 2; - $form->{"sellprice_$i"} = - $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, - $decimalplaces); + $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100); + my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/); + $dec = length $dec; + my $decimalplaces = ($dec > 2) ? $dec : 2; + + $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces); + (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/); + $dec_qty = length $dec_qty; - (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/); - $dec_qty = length $dec_qty; + $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces); - $form->{"qty_$i"} = - $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty); + $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty); - map { $form->{"${_}_$i"} =~ s/\"/"/g } - qw(partnumber description unit partnotes); - $form->{rowcount} = $i; + $form->{"sellprice_pg_$i"} = join ('--', $form->{"sellprice_$i"}, $form->{"pricegroup_id_$i"}); + + $form->{rowcount} = $i; } } - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } sub form_header { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); - # set option selected - foreach $item (qw(AR customer currency department employee contact)) { - $form->{"select$item"} =~ s/ selected//; - $form->{"select$item"} =~ - s/option>\Q$form->{$item}\E/option selected>$form->{$item}/; - } + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + my $cgi = $::request->{cgi}; - #build contacts - if ($form->{all_contacts}) { + $main::auth->assert('invoice_edit'); - $form->{selectcontact} = ""; - foreach $item (@{ $form->{all_contacts} }) { - if ($form->{cp_id} == $item->{cp_id}) { - $form->{selectcontact} .= - "|; - foreach $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) { - $form->{"lizenzen_$i"} .= - qq||; - } - $form->{"lizenzen_$i"} .= - qq||; - } - } - - # get pricegroups for parts - IS->get_pricegroups_for_parts(\%myconfig, \%$form); + map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice lastcost); - # build up html code for prices_$i - &set_pricegroup($i); + $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}); + $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0); } &display_form; @@ -1017,53 +638,131 @@ sub update { && ($form->{"description_$i"} eq "")) { $form->{rowcount}--; $form->{"discount_$i"} = ""; - &display_form; - } else { + display_form(); + } else { $form->{"id_$i"} = 0; - $form->{"unit_$i"} = $locale->text('ea'); + new_item(); + } + } + } + $main::lxdebug->leave_sub(); +} + +sub post_payment { + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + + $main::auth->assert('invoice_edit'); - &new_item; + $form->mtime_ischanged('ar') ; + my $invdate = $form->datetonum($form->{invdate}, \%myconfig); + $form->{defaultcurrency} = $form->get_default_currency(\%myconfig); + for my $i (1 .. $form->{paidaccounts}) { + if ($form->{"paid_$i"}) { + my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig); + + $form->isblank("datepaid_$i", $locale->text('Payment date missing!')); + + + if ($form->{currency} ne $form->{defaultcurrency}) { + $form->{"exchangerate_$i"} = $form->{exchangerate} + if ($invdate == $datepaid); + $form->isblank("exchangerate_$i", + $locale->text('Exchangerate for payment 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"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig)); } } - $lxdebug->leave_sub(); + + ($form->{AR}) = split /--/, $form->{AR}; + ($form->{AR_paid}) = split /--/, $form->{AR_paid}; + relink_accounts(); + if ( IS->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(); } sub post { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + + $main::auth->assert('invoice_edit'); + $form->mtime_ischanged('ar'); + + $form->{defaultcurrency} = $form->get_default_currency(\%myconfig); $form->isblank("invdate", $locale->text('Invoice Date missing!')); - $form->isblank("customer", $locale->text('Customer missing!')); + $form->isblank("customer_id", $locale->text('Customer missing!')); + $form->error($locale->text('Cannot post invoice for a closed period!')) + if ($form->date_closed($form->{"invdate"}, \%myconfig)); + + $form->{invnumber} =~ s/^\s*//g; + $form->{invnumber} =~ s/\s*$//g; # if oldcustomer ne customer redo form - if (&check_name(customer)) { + if (($form->{previous_customer_id} || $form->{customer_id}) != $form->{customer_id}) { &update; + $::dispatcher->end_request; + } + + if ($myconfig{mandatory_departments} && !$form->{department_id}) { + $form->{saved_message} = $::locale->text('You have to specify a department.'); + update(); exit; } + if ($form->{second_run}) { $form->{print_and_post} = 0; } + remove_emptied_rows(); &validate_items; - $closedto = $form->datetonum($form->{closedto}, \%myconfig); - $invdate = $form->datetonum($form->{invdate}, \%myconfig); + my $closedto = $form->datetonum($form->{closedto}, \%myconfig); + my $invdate = $form->datetonum($form->{invdate}, \%myconfig); + $form->error($locale->text('Cannot post transaction above the maximum future booking date!')) + if ($form->date_max_future($invdate, \%myconfig)); $form->error($locale->text('Cannot post invoice for a closed period!')) if ($invdate <= $closedto); $form->isblank("exchangerate", $locale->text('Exchangerate missing!')) if ($form->{currency} ne $form->{defaultcurrency}); - for $i (1 .. $form->{paidaccounts}) { - if ($form->{"paid_$i"}) { - $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig); + for my $i (1 .. $form->{paidaccounts}) { + if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) { + my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig); $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 ($datepaid <= $closedto); + if ($form->date_closed($form->{"datepaid_$i"}) && !$form->date_closed($form->{"gldate_$i"}, \%myconfig)); if ($form->{currency} ne $form->{defaultcurrency}) { $form->{"exchangerate_$i"} = $form->{exchangerate} @@ -1074,101 +773,336 @@ sub post { } } - ($form->{AR}) = split /--/, $form->{AR}; - ($form->{AR_paid}) = split /--/, $form->{AR_paid}; + ($form->{AR}) = split /--/, $form->{AR}; + ($form->{AR_paid}) = split /--/, $form->{AR_paid}; + $form->{storno} ||= 0; - $form->{label} = $locale->text('Invoice'); + $form->{label} = $form->{type} eq 'credit_note' ? $locale->text('Credit Note') : $locale->text('Invoice'); $form->{id} = 0 if $form->{postasnew}; # get new invnumber in sequence if no invnumber is given or if posasnew was requested - if (!$form->{invnumber} || $form->{postasnew}) { - $form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber") + if ($form->{postasnew}) { + if ($form->{type} eq "credit_note") { + undef($form->{cnnumber}); + } else { + undef($form->{invnumber}); + } } - if ($print_post) { - if (!(IS->post_invoice(\%myconfig, \%$form))) { - $form->error($locale->text('Cannot post invoice!')); + + relink_accounts(); + + my $terms = get_payment_terms_for_invoice(); + $form->{duedate} = $terms->calc_date(reference_date => $form->{invdate}, due_date => $form->{due_due})->to_kivitendo if $terms; + + # If transfer_out is requested, get rose db handle and do post and + # transfer out in one transaction. Otherwise just post the invoice. + if ($::instance_conf->get_is_transfer_out && $form->{type} ne 'credit_note' && !$form->{storno}) { + require SL::DB::Inventory; + my $rose_db = SL::DB::Inventory->new->db; + my @errors; + + if (!$rose_db->with_transaction(sub { + if (!eval { + if (!IS->post_invoice(\%myconfig, \%$form, $rose_db->dbh)) { + push @errors, $locale->text('Cannot post invoice!'); + die 'posting error'; + } + my $err = IS->transfer_out(\%$form, $rose_db->dbh); + if (@{ $err }) { + push @errors, @{ $err }; + die 'transfer error'; + } + + 1; + }) { + push @errors, $EVAL_ERROR; + die 'transaction error'; + } + + 1; + })) { + push @errors, $rose_db->error; + $form->error($locale->text('Cannot post invoice and/or transfer out! Error message:') . "\n" . join("\n", @errors)); } } else { - $form->redirect( - $form->{label} . " $form->{invnumber} " . $locale->text('posted!')) - if (IS->post_invoice(\%myconfig, \%$form)); - $form->error($locale->text('Cannot post invoice!')); + if (!IS->post_invoice(\%myconfig, \%$form)) { + $form->error($locale->text('Cannot post invoice!')); + } + } + + if(!exists $form->{addition}) { + $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber}; + $form->{what_done} = 'invoice'; + $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" : + $form->{storno} ? "STORNO" : + "POSTED"; + $form->save_history; } - $lxdebug->leave_sub(); + if (!$form->{no_redirect_after_post}) { + $form->{action} = 'edit'; + $form->{script} = 'is.pl'; + $form->{callback} = build_std_url(qw(action edit id callback saved_message)); + $form->redirect($form->{label} . " $form->{invnumber} " . $locale->text('posted!')); + } + + $main::lxdebug->leave_sub(); } sub print_and_post { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; - $old_form = new Form; - $print_post = 1; - $form->{print_and_post} = 1; + $main::auth->assert('invoice_edit'); + + my $old_form = Form->new; + $form->{no_redirect_after_post} = 1; + $form->{print_and_post} = 1; &post(); &edit(); - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); + +} + +sub use_as_new { + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + $main::auth->assert('invoice_edit'); + + delete @{ $form }{qw(printed emailed queued invnumber invdate exchangerate forex deliverydate id datepaid_1 gldate_1 acc_trans_id_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno locked)}; + $form->{rowcount}--; + $form->{paidaccounts} = 1; + $form->{invdate} = $form->current_date(\%myconfig); + my $terms = get_payment_terms_for_invoice(); + $form->{duedate} = $terms ? $terms->calc_date(reference_date => $form->{invdate})->to_kivitendo : $form->{invdate}; + $form->{employee_id} = SL::DB::Manager::Employee->current->id; + $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy'); + $form->{exchangerate} = $form->{forex} if $form->{forex}; + + $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"}; + + $form->{useasnew} = 1; + &display_form; + + $main::lxdebug->leave_sub(); +} + +sub storno { + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + + $main::auth->assert('invoice_edit'); + + if ($form->{storno}) { + $form->error($locale->text('Cannot storno storno invoice!')); + } + + if (IS->has_storno(\%myconfig, $form, "ar")) { + $form->error($locale->text("Invoice has already been storno'd!")); + } + if ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig)) { + $form->error($locale->text('Cannot storno invoice for a closed period!')); + } + + # save the history of invoice being stornoed + $form->{snumbers} = qq|invnumber_| . $form->{invnumber}; + $form->{what_done} = 'invoice'; + $form->{addition} = "STORNO"; + $form->save_history; + + map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password type))); } keys(%{ $form })); + + invoice_links(); + prepare_invoice(); + relink_accounts(); + + # Payments must not be recorded for the new storno invoice. + $form->{paidaccounts} = 0; + map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ gldate_ acc_trans_id_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form }; + + # record link invoice to storno + $form->{convert_from_ar_ids} = $form->{id}; + $form->{storno_id} = $form->{id}; + $form->{storno} = 1; + $form->{id} = ""; + $form->{invnumber} = "Storno zu " . $form->{invnumber}; + $form->{invdate} = DateTime->today->to_lxoffice; + $form->{rowcount}++; + # set new ids for storno invoice + # set new persistent ids for storno invoice items + $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"}; + + post(); + $main::lxdebug->leave_sub(); } sub preview { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + + $main::auth->assert('invoice_edit'); $form->{preview} = 1; - $old_form = new Form; + my $old_form = Form->new; for (keys %$form) { $old_form->{$_} = $form->{$_} } - $old_form->{rowcount}++; &print_form($old_form); - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } +sub credit_note { + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + + $main::auth->assert('invoice_edit'); + + $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig); + $form->{duedate} = + $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1); + + $form->{convert_from_ar_ids} = $form->{id}; + $form->{id} = ''; + $form->{rowcount}--; + + + $form->{title} = $locale->text('Add Credit Note'); + $form->{script} = 'is.pl'; + + # Bei Gutschriften bezug zur Rechnungsnummer + $form->{invnumber_for_credit_note} = $form->{invnumber}; + # bo creates the id, reset it + map { delete $form->{$_} } + qw(id invnumber subject message cc bcc printed emailed queued); + $form->{ $form->{vc} } =~ s/--.*//g; + $form->{type} = "credit_note"; + + + map { $form->{"select$_"} = "" } ($form->{vc}, 'currency'); + +# map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } +# qw(creditlimit creditremaining); + + # set new persistent ids for credit note and link previous invoice id + $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"}; + + my $currency = $form->{currency}; + &invoice_links; + + $form->{currency} = $currency; + $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, 'buy'); + $form->{exchangerate} = $form->{forex} || ''; + + $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal}); + + # bei Gutschriften werden Zahlungseingänge aus Rechnung nicht übernommen + for my $i (1 .. $form->{paidaccounts}) { + delete $form->{"paid_$i"}; + delete $form->{"source_$i"}; + delete $form->{"memo_$i"}; + delete $form->{"datepaid_$i"}; + delete $form->{"gldate_$i"}; + delete $form->{"acc_trans_id_$i"}; + delete $form->{"AR_paid_$i"}; + }; + $form->{paidaccounts} = 1; + + &prepare_invoice; + + + &display_form; + + $main::lxdebug->leave_sub(); +} + +sub display_form { + $::lxdebug->enter_sub; + + $::auth->assert('invoice_edit'); + + relink_accounts(); + + my $new_rowcount = $::form->{"rowcount"} * 1 + 1; + $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"}; + + $::form->language_payment(\%::myconfig); + + Common::webdav_folder($::form); + + form_header(); + display_row(++$::form->{rowcount}); + form_footer(); + + $::lxdebug->leave_sub; +} + sub delete { - $lxdebug->enter_sub(); - if ($form->{second_run}) { - $form->{print_and_post} = 0; + $::auth->assert('invoice_edit'); + + if (IS->delete_invoice(\%::myconfig, $::form)) { + # saving the history + if(!exists $::form->{addition}) { + $::form->{snumbers} = 'invnumber' .'_'. $::form->{invnumber}; + $::form->{what_done} = 'invoice'; + $::form->{addition} = "DELETED"; + $::form->save_history; + } + # /saving the history + $::form->redirect($::locale->text('Invoice deleted!')); } - $form->header; + $::form->error($::locale->text('Cannot delete invoice!')); +} - print qq| - +sub post_and_e_mail { + e_mail(); +}; -
{script}> -|; +sub e_mail { + $main::lxdebug->enter_sub(); - # delete action variable - map { delete $form->{$_} } qw(action header); + my $form = $main::form; - foreach $key (keys %$form) { - $form->{$key} =~ s/\"/"/g; - print qq|\n|; - } + $main::auth->assert('invoice_edit'); - print qq| -

| . $locale->text('Confirm!') . qq|

+ if (!$form->{id}) { + $form->{no_redirect_after_post} = 1; -

| - . $locale->text('Are you sure you want to delete Invoice Number') - . qq| $form->{invnumber} -

+ my $saved_form = save_form(); -

- -

-|; + post(); - $lxdebug->leave_sub(); -} + restore_form($saved_form, 0, qw(id invnumber)); + } -sub yes { - $lxdebug->enter_sub(); + edit_e_mail(); - $form->redirect($locale->text('Invoice deleted!')) - if (IS->delete_invoice(\%myconfig, \%$form, $spool)); - $form->error($locale->text('Cannot delete invoice!')); + $main::lxdebug->leave_sub(); +} + +sub dispatcher { + for my $action (qw( + print update ship_to e_mail storno post_payment use_as_new credit_note + delete post order preview post_and_e_mail print_and_post + mark_as_paid + )) { + if ($::form->{"action_$action"}) { + call_sub($action); + return; + } + } - $lxdebug->leave_sub(); + $::form->error($::locale->text('No action defined.')); }