X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=bin%2Fmozilla%2Far.pl;h=562f8aac2968758ea6909fc65ef6aedcd76799d9;hb=e070b6383cd90405970527fdb90f08fb1d96ba6d;hp=3b4540457d2ebe1c896af59fde1d83db05806979;hpb=e6346d3f9b5d3e24727def67274f39c02b2ed12b;p=kivitendo-erp.git diff --git a/bin/mozilla/ar.pl b/bin/mozilla/ar.pl index 3b4540457..562f8aac2 100644 --- a/bin/mozilla/ar.pl +++ b/bin/mozilla/ar.pl @@ -24,23 +24,42 @@ # 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 Receivables # #====================================================================== +use POSIX qw(strftime); +use List::Util qw(sum first max); +use List::UtilsBy qw(sort_by); + use SL::AR; +use SL::Controller::Base; +use SL::FU; +use SL::GL; use SL::IS; -use SL::PE; -use Data::Dumper; - -require "$form->{path}/arap.pl"; - -1; - -# end of main +use SL::DB::Business; +use SL::DB::Chart; +use SL::DB::Currency; +use SL::DB::Default; +use SL::DB::Employee; +use SL::DB::Invoice; +use SL::DB::RecordTemplate; +use SL::DB::Tax; +use SL::Helper::Flash qw(flash); +use SL::Locale::String qw(t8); +use SL::Presenter::Tag; +use SL::Presenter::Chart; +use SL::ReportGenerator; + +require "bin/mozilla/common.pl"; +require "bin/mozilla/reportgenerator.pl"; + +use strict; +#use warnings; # this is for our long dates # $locale->text('January') @@ -70,774 +89,511 @@ require "$form->{path}/arap.pl"; # $locale->text('Nov') # $locale->text('Dec') +sub load_record_template { + $::auth->assert('ar_transactions'); + + # Load existing template and verify that its one for this module. + my $template = SL::DB::RecordTemplate + ->new(id => $::form->{id}) + ->load( + with_object => [ qw(customer payment currency record_items record_items.chart) ], + ); + + die "invalid template type" unless $template->template_type eq 'ar_transaction'; + + $template->substitute_variables; + + # Clean the current $::form before rebuilding it from the template. + my $form_defaults = delete $::form->{form_defaults}; + delete @{ $::form }{ grep { !m{^(?:script|login)$}i } keys %{ $::form } }; + + # Fill $::form from the template. + my $today = DateTime->today_local; + $::form->{title} = "Add"; + $::form->{currency} = $template->currency->name; + $::form->{direct_debit} = $template->direct_debit; + $::form->{globalproject_id} = $template->project_id; + $::form->{AR_chart_id} = $template->ar_ap_chart_id; + $::form->{transdate} = $today->to_kivitendo; + $::form->{duedate} = $today->to_kivitendo; + $::form->{rowcount} = @{ $template->items }; + $::form->{paidaccounts} = 1; + $::form->{$_} = $template->$_ for qw(department_id ordnumber taxincluded employee_id notes); + + if ($template->customer) { + $::form->{customer_id} = $template->customer_id; + $::form->{customer} = $template->customer->name; + $::form->{duedate} = $template->customer->payment->calc_date(reference_date => $today)->to_kivitendo if $template->customer->payment; + } + + my $row = 0; + foreach my $item (@{ $template->items }) { + $row++; + + my $active_taxkey = $item->chart->get_active_taxkey; + my $taxes = SL::DB::Manager::Tax->get_all( + where => [ chart_categories => { like => '%' . $item->chart->category . '%' }], + sort_by => 'taxkey, rate', + ); + + my $tax = first { $item->tax_id == $_->id } @{ $taxes }; + $tax //= first { $active_taxkey->tax_id == $_->id } @{ $taxes }; + $tax //= $taxes->[0]; + + if (!$tax) { + $row--; + next; + } + + $::form->{"AR_amount_chart_id_${row}"} = $item->chart_id; + $::form->{"previous_AR_amount_chart_id_${row}"} = $item->chart_id; + $::form->{"amount_${row}"} = $::form->format_amount(\%::myconfig, $item->amount1, 2); + $::form->{"taxchart_${row}"} = $item->tax_id . '--' . $tax->rate; + $::form->{"project_id_${row}"} = $item->project_id; + } + + $::form->{$_} = $form_defaults->{$_} for keys %{ $form_defaults // {} }; + + flash('info', $::locale->text("The record template '#1' has been loaded.", $template->template_name)); + + update( + keep_rows_without_amount => 1, + dont_add_new_row => 1, + ); +} + +sub save_record_template { + $::auth->assert('ar_transactions'); + + my $template = $::form->{record_template_id} ? SL::DB::RecordTemplate->new(id => $::form->{record_template_id})->load : SL::DB::RecordTemplate->new; + my $js = SL::ClientJS->new(controller => SL::Controller::Base->new); + my $new_name = $template->template_name_to_use($::form->{record_template_new_template_name}); + + $js->dialog->close('#record_template_dialog'); + + my @items = grep { + $_->{chart_id} && (($_->{tax_id} // '') ne '') + } map { + +{ chart_id => $::form->{"AR_amount_chart_id_${_}"}, + amount1 => $::form->parse_amount(\%::myconfig, $::form->{"amount_${_}"}), + tax_id => (split m{--}, $::form->{"taxchart_${_}"})[0], + project_id => $::form->{"project_id_${_}"} || undef, + } + } (1..($::form->{rowcount} || 1)); + + $template->assign_attributes( + template_type => 'ar_transaction', + template_name => $new_name, + + currency_id => SL::DB::Manager::Currency->find_by(name => $::form->{currency})->id, + ar_ap_chart_id => $::form->{AR_chart_id} || undef, + customer_id => $::form->{customer_id} || undef, + department_id => $::form->{department_id} || undef, + project_id => $::form->{globalproject_id} || undef, + employee_id => $::form->{employee_id} || undef, + taxincluded => $::form->{taxincluded} ? 1 : 0, + direct_debit => $::form->{direct_debit} ? 1 : 0, + ordnumber => $::form->{ordnumber}, + notes => $::form->{notes}, + + items => \@items, + ); + + eval { + $template->save; + 1; + } or do { + return $js + ->flash('error', $::locale->text("Saving the record template '#1' failed.", $new_name)) + ->render; + }; + + return $js + ->flash('info', $::locale->text("The record template '#1' has been saved.", $new_name)) + ->render; +} + sub add { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + $main::auth->assert('ar_transactions'); + + my $form = $main::form; + my %myconfig = %main::myconfig; + + # saving the history + if(!exists $form->{addition} && ($form->{id} ne "")) { + $form->{snumbers} = qq|invnumber_| . $form->{invnumber}; + $form->{addition} = "ADDED"; + $form->save_history; + } + # /saving the history $form->{title} = "Add"; - $form->{callback} = - "$form->{script}?action=add&path=$form->{path}&login=$form->{login}&password=$form->{password}" - unless $form->{callback}; + $form->{callback} = "ar.pl?action=add" unless $form->{callback}; - &create_links; - &display_form; + AR->get_transdate(\%myconfig, $form); + $form->{initial_transdate} = $form->{transdate}; + create_links(dont_save => 1); + $form->{transdate} = $form->{initial_transdate}; + + if ($form->{customer_id}) { + my $last_used_ar_chart = SL::DB::Customer->load_cached($form->{customer_id})->last_used_ar_chart; + $form->{"AR_amount_chart_id_1"} = $last_used_ar_chart->id if $last_used_ar_chart; + } - $lxdebug->leave_sub(); + &display_form; + $main::lxdebug->leave_sub(); } sub edit { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + $main::auth->assert('ar_transactions'); + my $form = $main::form; + + # show history button + $form->{javascript} = qq||; + #/show hhistory button + $form->{javascript} .= qq||; $form->{title} = "Edit"; - &create_links; + create_links(); &display_form; - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } sub display_form { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + $main::auth->assert('ar_transactions'); + + my $form = $main::form; &form_header; &form_footer; - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } -sub create_links { - $lxdebug->enter_sub(); - - $form->create_links("AR", \%myconfig, "customer"); - $duedate = $form->{duedate}; +sub _retrieve_invoice_object { + return undef if !$::form->{id}; + return $::form->{invoice_obj} if $::form->{invoice_obj} && $::form->{invoice_obj}->id == $::form->{id}; + return SL::DB::Invoice->new(id => $::form->{id})->load; +} - $taxincluded = $form->{taxincluded}; - my $id = $form->{id}; - IS->get_customer(\%myconfig, \%$form); - $form->{taxincluded} = $taxincluded; - $form->{id} = $id; +sub create_links { + $main::lxdebug->enter_sub(); - $form->{duedate} = $duedate if $duedate; - $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}"; - $form->{rowcount} = 1; + $main::auth->assert('ar_transactions'); - # currencies - @curr = split(/:/, $form->{currencies}); - chomp $curr[0]; - $form->{defaultcurrency} = $curr[0]; + my %params = @_; + my $form = $main::form; + my %myconfig = %main::myconfig; - map { $form->{selectcurrency} .= "\n"; - } else { - $form->{"select$key"} .= - "\n"; - } - } - - $form->{$key} = $form->{"select$key"}; - - # if there is a value we have an old entry - my $j = 0; - my $k = 0; - - for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) { - if ($key eq "AR_paid") { - $j++; - $form->{"AR_paid_$j"} = - "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; - - # reverse paid - $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1; - $form->{"datepaid_$j"} = - $form->{acc_trans}{$key}->[$i - 1]->{transdate}; - $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source}; - $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo}; - - $form->{"forex_$j"} = $form->{"exchangerate_$i"} = - $form->{acc_trans}{$key}->[$i - 1]->{exchangerate}; - $form->{"AR_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}"; - $form->{paidaccounts}++; - } else { - - $akey = $key; - $akey =~ s/AR_//; - - if ($key eq "AR_tax" || $key eq "AP_tax") { - $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = - "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; - $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = - $form->round_amount( - $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, - 2); - - if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) { - $totaltax += - $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"}; - $taxrate += - $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"}; - } else { - $totalwithholding += - $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"}; - $withholdingrate += - $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"}; - } - $index = $form->{acc_trans}{$key}->[$i - 1]->{index}; - $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount}; - $totaltax += $form->{"tax_$index"}; - - } else { - $k++; - $form->{"${akey}_$k"} = - $form->round_amount( - $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, - 2); - if ($akey eq 'amount') { - $form->{rowcount}++; - $totalamount += $form->{"${akey}_$i"}; - - $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"} = - "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}"; - $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate}; - $form->{"project_id_$k"} = - "$form->{acc_trans}{$key}->[$i-1]->{project_id}"; - } - $form->{"${key}_$k"} = - "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; - $form->{"${key}_$i"} = - "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; - my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description}); - $form->{"select${key}"} =~ - /