X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=bin%2Fmozilla%2Fis.pl;h=77ae22c8b1761180fcf2434c4aeeb56fa43fb3d8;hb=f087c3736d8c707c18cb3ea87692995940361057;hp=3e7704972c7d605482b5e8668dee85395450d1c9;hpb=487d4f1f6ccd3e22c05469f679bf5d0685914611;p=kivitendo-erp.git diff --git a/bin/mozilla/is.pl b/bin/mozilla/is.pl index 3e7704972..e1e70ece5 100644 --- a/bin/mozilla/is.pl +++ b/bin/mozilla/is.pl @@ -31,887 +31,513 @@ # #====================================================================== +use SL::FU; use SL::IS; use SL::PE; +use SL::OE; use Data::Dumper; +use DateTime; +use List::MoreUtils qw(uniq); +use List::Util qw(max sum); -require "$form->{path}/io.pl"; -require "$form->{path}/arap.pl"; +use SL::DB::Default; +use SL::DB::Customer; + +require "bin/mozilla/io.pl"; +require "bin/mozilla/invoice_io.pl"; +require "bin/mozilla/arap.pl"; +require "bin/mozilla/drafts.pl"; + +use strict; 1; # end of main sub add { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my $locale = $main::locale; - $form->{title} = $locale->text('Add Sales Invoice'); + $main::auth->assert('invoice_edit'); - $form->{callback} = - "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&password=$form->{password}" - unless $form->{callback}; + return $main::lxdebug->leave_sub() if (load_draft_maybe()); - $form{jsscript} = "date"; + if ($form->{type} eq "credit_note") { + $form->{title} = $locale->text('Add Credit Note'); + + 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->{taxincluded_changed_by_user} = 1; + + # show history button + $form->{javascript} = qq||; + #/show hhistory button + + 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->{webdav} = $::instance_conf->get_webdav; $form->create_links("AR", \%myconfig, "customer"); if ($form->{all_customer}) { unless ($form->{customer_id}) { $form->{customer_id} = $form->{all_customer}->[0]->{id}; + $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id}; } } - $cp_id = $form->{cp_id}; + + my $editing = $form->{id}; + + $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id + taxincluded currency cp_id intnotes id shipto_id + delivery_term_id)); + IS->get_customer(\%myconfig, \%$form); - IS->retrieve_invoice(\%myconfig, \%$form); - $form->{cp_id} = $cp_id; + #quote all_customer Bug 133 + foreach my $ref (@{ $form->{all_customer} }) { + $ref->{name} = $form->quote($ref->{name}); + } - # currencies - @curr = split /:/, $form->{currencies}; - chomp $curr[0]; - $form->{defaultcurrency} = $curr[0]; + $form->restore_vars(qw(id)); - 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"; + } else { + $form->{type} = "invoice"; + $form->{formname} = "invoice"; + } - # # get pricegroups for parts - # IS->get_pricegroups_for_parts(\%myconfig, \%$form); + if ($form->{id}) { + + 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(); - - # 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}/; + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + my $cgi = $::request->{cgi}; + + $main::auth->assert('invoice_edit'); + + my %TMPL_VAR = (); + my @custom_hiddens; + + $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id}; + $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id}; + + $form->{defaultcurrency} = $form->get_default_currency(\%myconfig); + + $form->get_lists("taxzones" => ($form->{id} ? "ALL_TAXZONES" : "ALL_ACTIVE_TAXZONES"), + "currencies" => "ALL_CURRENCIES", + "customers" => "ALL_CUSTOMERS", + "departments" => "all_departments", + "price_factors" => "ALL_PRICE_FACTORS"); + + # Projects + my @old_project_ids = uniq grep { $_ } map { $_ * 1 } ($form->{"globalproject_id"}, map { $form->{"project_id_$_"} } 1..$form->{"rowcount"}); + my @old_ids_cond = @old_project_ids ? (id => \@old_project_ids) : (); + my @customer_cond; + if ($::instance_conf->get_customer_projects_only_in_sales) { + @customer_cond = ( + or => [ + customer_id => $::form->{customer_id}, + billable_customer_id => $::form->{customer_id}, + ]); } - - #build contacts - if ($form->{all_contacts}) { - - $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"}); } &display_form; @@ -1017,53 +632,124 @@ 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(); - &new_item; + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + $main::auth->assert('invoice_edit'); + + 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!')); } } } - $lxdebug->leave_sub(); + # Abgeschlossene Zeiträume nur für den letzten (aktuellen) Zahlungseingang prüfen + # Details s.a. Bug 1502 + # Das Problem ist jetzt, dass man Zahlungseingänge nachträglich ändern kann + # Wobei dies für Installationen die sowieso nicht mit Bücherkontrolle arbeiten keinen + # keinen Unterschied macht. + # Optimal wäre, wenn gegen einen Zeitstempel des Zahlungsfelds geprüft würde ... + # Das Problem hierbei ist, dass in IS.pm post_invoice IMMER alle Zahlungseingänge aus $form + # erneut gespeichert werden. Prinzipiell wäre es besser NUR die Änderungen des Rechnungs- + # belegs (neue Zahlung aber nichts anderes) zu speichern ... + # Vielleicht könnte man ähnlich wie bei Rechnung löschen verfahren + $form->error($locale->text('Cannot post payment for a closed period!')) + if ($form->date_closed($form->{"datepaid_$form->{paidaccounts}"}, \%myconfig)); + + ($form->{AR}) = split /--/, $form->{AR}; + ($form->{AR_paid}) = split /--/, $form->{AR_paid}; + relink_accounts(); + $form->redirect($locale->text('Payment posted!')) + if (IS->post_payment(\%myconfig, \%$form)); + $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->{defaultcurrency} = $form->get_default_currency(\%myconfig); $form->isblank("invdate", $locale->text('Invoice Date missing!')); $form->isblank("customer", $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 (&check_name('customer')) { &update; + ::end_of_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 payment for a closed period!')) - if ($datepaid <= $closedto); + if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig)); if ($form->{currency} ne $form->{defaultcurrency}) { $form->{"exchangerate_$i"} = $form->{exchangerate} @@ -1074,80 +760,174 @@ 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 ($print_post) { - if (!(IS->post_invoice(\%myconfig, \%$form))) { - $form->error($locale->text('Cannot post invoice!')); + if ($form->{postasnew}) { + if ($form->{type} eq "credit_note") { + undef($form->{cnnumber}); + } else { + undef($form->{invnumber}); } - } else { - $form->redirect( - $form->{label} . " $form->{invnumber} " . $locale->text('posted!')) - if (IS->post_invoice(\%myconfig, \%$form)); - $form->error($locale->text('Cannot post invoice!')); } - $lxdebug->leave_sub(); + relink_accounts(); + $form->error($locale->text('Cannot post invoice!')) + unless IS->post_invoice(\%myconfig, \%$form); + remove_draft() if $form->{remove_draft}; + + if(!exists $form->{addition}) { + $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber}; + $form->{addition} = $form->{print_and_post} ? "PRINTED AND POSTED" : + $form->{storno} ? "STORNO" : + "POSTED"; + $form->save_history; + } + + if (!$form->{no_redirect_after_post}) { + $form->{action} = 'edit'; + $form->{script} = 'is.pl'; + $form->{saved_message} = $form->{label} . " $form->{invnumber} " . $locale->text('posted!'); + $form->{callback} = build_std_url(qw(action edit id saved_message)); + $form->redirect; + } + + $main::lxdebug->leave_sub(); } sub print_and_post { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + + $main::auth->assert('invoice_edit'); - $old_form = new Form; - $print_post = 1; - $form->{print_and_post} = 1; + my $old_form = new Form; + $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); + $form->{duedate} = $form->get_duedate(\%myconfig, $form->{invdate}) || $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}; + delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"}; + + &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!')); + } + + 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 }; + + $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 + 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 = new Form; for (keys %$form) { $old_form->{$_} = $form->{$_} } - $old_form->{rowcount}++; &print_form($old_form); - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } sub delete { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my $locale = $main::locale; + + $main::auth->assert('invoice_edit'); + if ($form->{second_run}) { $form->{print_and_post} = 0; } $form->header; print qq| - - -
{script}> + |; # delete action variable map { delete $form->{$_} } qw(action header); - foreach $key (keys %$form) { + foreach my $key (keys %$form) { + next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key})); $form->{$key} =~ s/\"/"/g; - print qq|\n|; + print qq|\n|; } print qq| -

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

+

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

| . $locale->text('Are you sure you want to delete Invoice Number') @@ -1155,20 +935,132 @@ sub delete {

-

|; - $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); + + for my $i (1 .. $form->{rowcount}) { + for (qw(listprice)) { + $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"}; + } + } + # set new persistent ids for credit note + 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 yes { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); - $form->redirect($locale->text('Invoice deleted!')) - if (IS->delete_invoice(\%myconfig, \%$form, $spool)); + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + + $main::auth->assert('invoice_edit'); + + if (IS->delete_invoice(\%myconfig, \%$form)) { + # saving the history + if(!exists $form->{addition}) { + $form->{snumbers} = 'invnumber' .'_'. $form->{invnumber}; # ($form->{type} eq 'credit_note' ? 'cnnumber' : 'invnumber') .'_'. $form->{invnumber}; + $form->{addition} = "DELETED"; + $form->save_history; + } + # /saving the history + $form->redirect($locale->text('Invoice deleted!')); + } $form->error($locale->text('Cannot delete invoice!')); - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); +} + +sub post_and_e_mail { + e_mail(); +}; + +sub e_mail { + $main::lxdebug->enter_sub(); + + my $form = $main::form; + + $main::auth->assert('invoice_edit'); + + if (!$form->{id}) { + $form->{no_redirect_after_post} = 1; + + my $saved_form = save_form(); + + post(); + + restore_form($saved_form, 0, qw(id invnumber)); + } + + edit_e_mail(); + + $main::lxdebug->leave_sub(); }