|
#=====================================================================
|
|
# LX-Office ERP
|
|
# Copyright (C) 2004
|
|
# Based on SQL-Ledger Version 2.1.9
|
|
# Web http://www.lx-office.org
|
|
#
|
|
#=====================================================================
|
|
# SQL-Ledger, Accounting
|
|
# Copyright (c) 1998-2002
|
|
#
|
|
# Author: Dieter Simader
|
|
# Email: dsimader@sql-ledger.org
|
|
# Web: http://www.sql-ledger.org
|
|
#
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# 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., 51 Franklin Street, Fifth Floor, Boston,
|
|
# MA 02110-1335, USA.
|
|
#======================================================================
|
|
#
|
|
# Inventory received module
|
|
#
|
|
#======================================================================
|
|
|
|
use SL::FU;
|
|
use SL::Helper::Flash qw(flash_later);
|
|
use SL::Helper::UserPreferences::DisplayPreferences;
|
|
use SL::IR;
|
|
use SL::IS;
|
|
use SL::DB::BankTransactionAccTrans;
|
|
use SL::DB::Default;
|
|
use SL::DB::Department;
|
|
use SL::DB::Project;
|
|
use SL::DB::PurchaseInvoice;
|
|
use SL::DB::ValidityToken;
|
|
use SL::DB::Vendor;
|
|
use List::MoreUtils qw(uniq);
|
|
use List::Util qw(max sum);
|
|
use List::UtilsBy qw(sort_by);
|
|
|
|
require "bin/mozilla/io.pl";
|
|
require "bin/mozilla/common.pl";
|
|
|
|
use strict;
|
|
|
|
1;
|
|
|
|
# end of main
|
|
|
|
sub _may_view_or_edit_this_invoice {
|
|
return 1 if $::auth->assert('ap_transactions', 1); # may edit all invoices
|
|
return 0 if !$::form->{id}; # creating new invoices isn't allowed without invoice_edit
|
|
return 1 if $::auth->assert('purchase_invoice_view', 1); # viewing is allowed with this right
|
|
return 0 if !$::form->{globalproject_id}; # existing records without a project ID are not allowed
|
|
return SL::DB::Project->new(id => $::form->{globalproject_id})->load->may_employee_view_project_invoices(SL::DB::Manager::Employee->current);
|
|
}
|
|
|
|
sub _assert_access {
|
|
my $cache = $::request->cache('ap.pl::_assert_access');
|
|
|
|
$cache->{_may_view_or_edit_this_invoice} = _may_view_or_edit_this_invoice() if !exists $cache->{_may_view_or_edit_this_invoice};
|
|
$::form->show_generic_error($::locale->text("You do not have the permissions to access this function.")) if ! $cache->{_may_view_or_edit_this_invoice};
|
|
}
|
|
|
|
sub add {
|
|
$main::lxdebug->enter_sub();
|
|
|
|
my $form = $main::form;
|
|
my $locale = $main::locale;
|
|
|
|
$main::auth->assert('vendor_invoice_edit');
|
|
|
|
if (!$::instance_conf->get_allow_new_purchase_invoice) {
|
|
$::form->show_generic_error($::locale->text("You do not have the permissions to access this function."));
|
|
}
|
|
|
|
$form->{show_details} = $::myconfig{show_form_details};
|
|
|
|
$form->{title} = $locale->text('Record Vendor Invoice');
|
|
|
|
if (!$form->{form_validity_token}) {
|
|
$form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST())->token;
|
|
}
|
|
|
|
&invoice_links;
|
|
&prepare_invoice;
|
|
&display_form;
|
|
|
|
$main::lxdebug->leave_sub();
|
|
}
|
|
|
|
sub edit {
|
|
$main::lxdebug->enter_sub();
|
|
|
|
# Delay access check to after the invoice's been loaded in
|
|
# "create_links" so that project-specific invoice rights can be
|
|
# evaluated.
|
|
|
|
my $form = $main::form;
|
|
my $locale = $main::locale;
|
|
|
|
$form->{show_details} = $::myconfig{show_form_details};
|
|
|
|
# show history button
|
|
$form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
|
|
#/show hhistory button
|
|
|
|
$form->{title} = $locale->text('Edit Vendor Invoice');
|
|
|
|
&invoice_links;
|
|
&prepare_invoice;
|
|
&display_form;
|
|
|
|
$main::lxdebug->leave_sub();
|
|
}
|
|
|
|
sub invoice_links {
|
|
$main::lxdebug->enter_sub();
|
|
|
|
# Delay access check to after the invoice's been loaded so that
|
|
# project-specific invoice rights can be evaluated.
|
|
|
|
my $form = $main::form;
|
|
my %myconfig = %main::myconfig;
|
|
|
|
$form->{vc} = 'vendor';
|
|
|
|
# create links
|
|
$form->create_links("AP", \%myconfig, "vendor");
|
|
|
|
_assert_access();
|
|
|
|
$form->backup_vars(qw(payment_id language_id taxzone_id
|
|
currency delivery_term_id intnotes cp_id));
|
|
|
|
IR->get_vendor(\%myconfig, \%$form);
|
|
IR->retrieve_invoice(\%myconfig, \%$form);
|
|
|
|
$form->restore_vars(qw(payment_id language_id taxzone_id
|
|
currency delivery_term_id intnotes cp_id));
|
|
|
|
my @curr = $form->get_all_currencies();
|
|
map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
|
|
|
|
# forex
|
|
$form->{forex} = $form->{exchangerate};
|
|
my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
|
|
|
|
foreach my $key (keys %{ $form->{AP_links} }) {
|
|
|
|
foreach my $ref (@{ $form->{AP_links}{$key} }) {
|
|
$form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>";
|
|
}
|
|
|
|
next unless $form->{acc_trans}{$key};
|
|
|
|
if ($key eq "AP_paid") {
|
|
for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
|
|
$form->{"AP_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};
|
|
$form->{"datepaid_$i"} =
|
|
$form->{acc_trans}{$key}->[$i - 1]->{transdate};
|
|
$form->{"gldate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
|
|
$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->{paidaccounts} = $i;
|
|
# hook for calc of of fx_paid and check if banktransaction has a record exchangerate
|
|
if ($form->{"exchangerate_$i"}) {
|
|
my $bt_acc_trans = SL::DB::Manager::BankTransactionAccTrans->find_by(acc_trans_id => $form->{"acc_trans_id_$i"});
|
|
if ($bt_acc_trans) {
|
|
|