Projekt

Allgemein

Profil

Herunterladen (93,3 KB) Statistiken
| Zweig: | Markierung: | Revision:
b6dc5623 Sven Schöling
#=====================================================================
d319704a Moritz Bunkus
# 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-2003
#
# 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
f7b15d43 Christian Wittmer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1335, USA.
d319704a Moritz Bunkus
#======================================================================
#
# Order entry module
# Quotation module
#======================================================================
77923db3 Moritz Bunkus
a2820962 Moritz Bunkus
use Carp;
77923db3 Moritz Bunkus
use POSIX qw(strftime);
d319704a Moritz Bunkus
29a62ccc Moritz Bunkus
use SL::DB::Order;
3a77cbe1 Bernd Bleßmann
use SL::DB::OrderItem;
480d9a56 Moritz Bunkus
use SL::DO;
7a7f33b5 Moritz Bunkus
use SL::FU;
d319704a Moritz Bunkus
use SL::OE;
use SL::IR;
use SL::IS;
94f5cb15 Bernd Bleßmann
use SL::Helper::UserPreferences::DisplayPreferences;
87949627 Sven Schöling
use SL::MoreCommon qw(ary_diff restore_form save_form);
3a77cbe1 Bernd Bleßmann
use SL::Presenter::ItemsList;
77923db3 Moritz Bunkus
use SL::ReportGenerator;
65b2387a Moritz Bunkus
use SL::YAML;
41adf433 Moritz Bunkus
use List::MoreUtils qw(uniq any none);
a2820962 Moritz Bunkus
use List::Util qw(min max reduce sum);
82574e78 Geoffrey Richardson
use Data::Dumper;
d319704a Moritz Bunkus
b39653f8 Bernd Bleßmann
use SL::Controller::Order;
6e08e454 Thomas Heck
use SL::DB::Customer;
3fcf64fc Bernd Bleßmann
use SL::DB::TaxZone;
77807bf5 Geoffrey Richardson
use SL::DB::PaymentTerm;
7a3a0017 Moritz Bunkus
use SL::DB::ValidityToken;
4cb932ab Moritz Bunkus
use SL::DB::Vendor;
6e08e454 Thomas Heck
46b5ee69 Moritz Bunkus
require "bin/mozilla/common.pl";
d629acd8 Sven Schöling
require "bin/mozilla/io.pl";
18da4a96 Moritz Bunkus
require "bin/mozilla/reportgenerator.pl";
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
use strict;

d319704a Moritz Bunkus
1;

# end of main

ccc50abd Moritz Bunkus
# For locales.pl:
# $locale->text('Edit the purchase_order');
# $locale->text('Edit the sales_order');
# $locale->text('Edit the request_quotation');
# $locale->text('Edit the sales_quotation');

# $locale->text('Workflow purchase_order');
# $locale->text('Workflow sales_order');
# $locale->text('Workflow request_quotation');
# $locale->text('Workflow sales_quotation');

8c7e4493 Moritz Bunkus
my $oe_access_map = {
565b77d7 Bernd Bleßmann
'sales_order_intake' => 'sales_order_edit',
'sales_order' => 'sales_order_edit',
'purchase_order' => 'purchase_order_edit',
c4bcc898 Bernd Bleßmann
'purchase_order_confirmation' => 'purchase_order_edit',
565b77d7 Bernd Bleßmann
'request_quotation' => 'request_quotation_edit',
'sales_quotation' => 'sales_quotation_edit',
'purchase_quotation_intake' => 'request_quotation_edit',
8c7e4493 Moritz Bunkus
};

3ad96f21 Bernd Bleßmann
my $oe_view_access_map = {
565b77d7 Bernd Bleßmann
'sales_order_intake' => 'sales_order_edit | sales_order_view',
'sales_order' => 'sales_order_edit | sales_order_view',
'purchase_order' => 'purchase_order_edit | purchase_order_view',
c4bcc898 Bernd Bleßmann
'purchase_order_confirmation' => 'purchase_order_edit | purchase_order_view',
565b77d7 Bernd Bleßmann
'request_quotation' => 'request_quotation_edit | request_quotation_view',
'sales_quotation' => 'sales_quotation_edit | sales_quotation_view',
'purchase_quotation_intake' => 'request_quotation_edit | request_quotation_view',
3ad96f21 Bernd Bleßmann
};

8c7e4493 Moritz Bunkus
sub check_oe_access {
3ad96f21 Bernd Bleßmann
my (%params) = @_;
5d59ad23 Sven Schöling
my $form = $main::form;

3ad96f21 Bernd Bleßmann
my $right = ($params{with_view}) ? $oe_view_access_map->{$form->{type}} : $oe_access_map->{$form->{type}};
8c7e4493 Moritz Bunkus
$right ||= 'DOES_NOT_EXIST';

5d59ad23 Sven Schöling
$main::auth->assert($right);
8c7e4493 Moritz Bunkus
}

f775b88a Moritz Bunkus
sub check_oe_conversion_to_sales_invoice_allowed {
return 1 if $::form->{type} !~ m/^sales/;
return 1 if ($::form->{type} =~ m/quotation/) && $::instance_conf->get_allow_sales_invoice_from_sales_quotation;
return 1 if ($::form->{type} =~ m/order/) && $::instance_conf->get_allow_sales_invoice_from_sales_order;

$::form->show_generic_error($::locale->text("You do not have the permissions to access this function."));

return 0;
}

ccc50abd Moritz Bunkus
sub set_headings {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my $locale = $main::locale;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

ccc50abd Moritz Bunkus
my ($action) = @_;

d319704a Moritz Bunkus
if ($form->{type} eq 'purchase_order') {
ccc50abd Moritz Bunkus
$form->{title} = $action eq "edit" ?
$locale->text('Edit Purchase Order') :
$locale->text('Add Purchase Order');
$form->{heading} = $locale->text('Purchase Order');
$form->{vc} = 'vendor';
d319704a Moritz Bunkus
}
if ($form->{type} eq 'sales_order') {
ccc50abd Moritz Bunkus
$form->{title} = $action eq "edit" ?
$locale->text('Edit Sales Order') :
$locale->text('Add Sales Order');
$form->{heading} = $locale->text('Sales Order');
$form->{vc} = 'customer';
d319704a Moritz Bunkus
}
if ($form->{type} eq 'request_quotation') {
ccc50abd Moritz Bunkus
$form->{title} = $action eq "edit" ?
$locale->text('Edit Request for Quotation') :
$locale->text('Add Request for Quotation');
$form->{heading} = $locale->text('Request for Quotation');
$form->{vc} = 'vendor';
d319704a Moritz Bunkus
}
if ($form->{type} eq 'sales_quotation') {
ccc50abd Moritz Bunkus
$form->{title} = $action eq "edit" ?
$locale->text('Edit Quotation') :
$locale->text('Add Quotation');
$form->{heading} = $locale->text('Quotation');
$form->{vc} = 'customer';
d319704a Moritz Bunkus
}

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
ccc50abd Moritz Bunkus
}

sub add {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
ccc50abd Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

ccc50abd Moritz Bunkus
set_headings("add");

d319704a Moritz Bunkus
$form->{callback} =
8c7e4493 Moritz Bunkus
"$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
d319704a Moritz Bunkus
unless $form->{callback};

519f43a2 Moritz Bunkus
$form->{show_details} = $::myconfig{show_form_details};

844a541e Moritz Bunkus
order_links(is_new => 1);
d319704a Moritz Bunkus
&prepare_order;
&display_form;

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub edit {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
8c7e4493 Moritz Bunkus
check_oe_access();

519f43a2 Moritz Bunkus
$form->{show_details} = $::myconfig{show_form_details};
ad1c22fa Thomas Heck
$form->{taxincluded_changed_by_user} = 1;

2b89ec97 Thomas Kasulke
# show history button
$form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
#/show hhistory button
d319704a Moritz Bunkus
b08a61d7 Moritz Bunkus
$form->{simple_save} = 0;

ccc50abd Moritz Bunkus
set_headings("edit");

626e0240 Stephan Köhler
# editing without stuff to edit? try adding it first
85f48169 Philip Reetz
if ($form->{rowcount} && !$form->{print_and_save}) {
b39653f8 Bernd Bleßmann
if ($::instance_conf->get_feature_experimental_order) {
my $c = SL::Controller::Order->new;
$c->action_edit_collective();

$main::lxdebug->leave_sub();
$::dispatcher->end_request;
}

5d59ad23 Sven Schöling
my $id;
9d0ebf28 Moritz Bunkus
map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
626e0240 Stephan Köhler
if (!$id) {
081a4f97 Moritz Bunkus
626e0240 Stephan Köhler
# reset rowcount
undef $form->{rowcount};
&add;
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
626e0240 Stephan Köhler
return;
}
9d0ebf28 Moritz Bunkus
} elsif (!$form->{id}) {
&add;
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
9d0ebf28 Moritz Bunkus
return;
626e0240 Stephan Köhler
}

5d59ad23 Sven Schöling
my ($language_id, $printer_id);
54e4131e Moritz Bunkus
if ($form->{print_and_save}) {
5d8dbe98 Bernd Bleßmann
$form->{action} = "dispatcher";
$form->{action_print} = "1";
54e4131e Moritz Bunkus
$form->{resubmit} = 1;
$language_id = $form->{language_id};
$printer_id = $form->{printer_id};
}

ccc50abd Moritz Bunkus
set_headings("edit");
d319704a Moritz Bunkus
&order_links;
8be9afee Moritz Bunkus
$form->{rowcount} = 0;
5d59ad23 Sven Schöling
foreach my $ref (@{ $form->{form_details} }) {
8be9afee Moritz Bunkus
$form->{rowcount}++;
map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
}

d319704a Moritz Bunkus
&prepare_order;
8be9afee Moritz Bunkus
54e4131e Moritz Bunkus
if ($form->{print_and_save}) {
$form->{language_id} = $language_id;
$form->{printer_id} = $printer_id;
}
8be9afee Moritz Bunkus
d319704a Moritz Bunkus
&display_form;

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub order_links {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

844a541e Moritz Bunkus
my (%params) = @_;

5d59ad23 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
8c7e4493 Moritz Bunkus
check_oe_access();

24e7a52f Jan Büren
# retrieve order/quotation
2ff471a7 Moritz Bunkus
my $editing = $form->{id};

d319704a Moritz Bunkus
OE->retrieve(\%myconfig, \%$form);

626e0240 Stephan Köhler
# if multiple rowcounts (== collective order) then check if the
081a4f97 Moritz Bunkus
# there were more than one customer (in that case OE::retrieve removes
626e0240 Stephan Köhler
# the content from the field)
d7420ba8 Sven Schöling
$form->error($locale->text('Collective Orders only work for orders from one customer!'))
if $form->{rowcount} && $form->{type} eq 'sales_order'
8cdaa6f0 Sven Schöling
&& defined $form->{customer} && $form->{customer} eq '';
626e0240 Stephan Köhler
03d3d025 Bernd Bleßmann
$form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes shipto_id delivery_term_id currency));
d319704a Moritz Bunkus
# get customer / vendor
844a541e Moritz Bunkus
if ($form->{type} =~ /(purchase_order|request_quotation)/) {
IR->get_vendor(\%myconfig, \%$form);
} else {
IS->get_customer(\%myconfig, \%$form);
$form->{billing_address_id} = $form->{default_billing_address_id} if $params{is_new};
}
648b2e7e Sven Schöling
03d3d025 Bernd Bleßmann
$form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id delivery_term_id));
94802c79 Bernd Bleßmann
$form->restore_vars(qw(currency)) if $form->{id};
d7420ba8 Sven Schöling
$form->restore_vars(qw(taxincluded)) if $form->{id};
$form->restore_vars(qw(salesman_id)) if $editing;
648b2e7e Sven Schöling
$form->{forex} = $form->{exchangerate};
$form->{employee} = "$form->{employee}--$form->{employee_id}";

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub prepare_order {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
8c7e4493 Moritz Bunkus
check_oe_access();

d7420ba8 Sven Schöling
$form->{formname} ||= $form->{type};
d319704a Moritz Bunkus
084d1390 Sven Schöling
# format discounts if values come from db. either as single id, or as a collective order
my $format_discounts = $form->{id} || $form->{convert_from_oe_ids};

081a4f97 Moritz Bunkus
for my $i (1 .. $form->{rowcount}) {
d7420ba8 Sven Schöling
$form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
084d1390 Sven Schöling
$form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($format_discounts ? 100 : 1));
b6dc5623 Sven Schöling
$form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
da804bf2 Geoffrey Richardson
$form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"});
b6dc5623 Sven Schöling
$form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
081a4f97 Moritz Bunkus
}
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

3ff7346d Moritz Bunkus
sub setup_oe_action_bar {
my %params = @_;
my $form = $::form;

my $has_active_periodic_invoice;
a5389b65 Moritz Bunkus
if ($params{oe_obj}) {
3ff7346d Moritz Bunkus
$has_active_periodic_invoice =
a5389b65 Moritz Bunkus
$params{oe_obj}->is_type('sales_order')
&& $params{oe_obj}->periodic_invoices_config
&& $params{oe_obj}->periodic_invoices_config->active
&& ( !$params{oe_obj}->periodic_invoices_config->end_date
|| ($params{oe_obj}->periodic_invoices_config->end_date > DateTime->today_local))
&& $params{oe_obj}->periodic_invoices_config->get_previous_billed_period_start_date;
3ff7346d Moritz Bunkus
}

6f1da6d4 Moritz Bunkus
my $allow_invoice = $params{is_req_quo}
|| $params{is_pur_ord}
|| ($params{is_sales_quo} && $::instance_conf->get_allow_sales_invoice_from_sales_quotation)
|| ($params{is_sales_ord} && $::instance_conf->get_allow_sales_invoice_from_sales_order);
my @req_trans_cost_art = qw(kivi.SalesPurchase.check_transport_cost_article_presence) x!!$::instance_conf->get_transport_cost_reminder_article_number_id;
my @warn_p_invoice = qw(kivi.SalesPurchase.oe_warn_save_active_periodic_invoice) x!!$has_active_periodic_invoice;
3ff7346d Moritz Bunkus
for my $bar ($::request->layout->get('actionbar')) {
ff424b75 Moritz Bunkus
$bar->add(
action => [
t8('Update'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "update" } ],
ff424b75 Moritz Bunkus
id => 'update_button',
accesskey => 'enter',
],

combobox => [
action => [
t8('Save'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "save" } ],
82372529 Sven Schöling
checks => [ 'kivi.validate_form', @req_trans_cost_art, @warn_p_invoice ],
ff424b75 Moritz Bunkus
],
action => [
t8('Save as new'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "save_as_new" } ],
82372529 Sven Schöling
checks => [ 'kivi.validate_form', @req_trans_cost_art ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
],
action => [
t8('Save and Close'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "save_and_close" } ],
82372529 Sven Schöling
checks => [ 'kivi.validate_form', @req_trans_cost_art, @warn_p_invoice ],
ff424b75 Moritz Bunkus
],
action => [
t8('Delete'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "delete" } ],
ff424b75 Moritz Bunkus
confirm => t8('Do you really want to delete this object?'),
disabled => !$form->{id} ? t8('This record has not been saved yet.')
: ( ($params{is_sales_ord} && !$::instance_conf->get_sales_order_show_delete)
|| ($params{is_pur_ord} && !$::instance_conf->get_purchase_order_show_delete)) ? t8('Deleting this type of record has been disabled in the configuration.')
: undef,
],
], # end of combobox "Save"

'separator',

combobox => [
action => [ t8('Workflow') ],
36c8af52 Moritz Bunkus
action => [
ff424b75 Moritz Bunkus
t8('Sales Order'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "sales_order" } ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
82372529 Sven Schöling
checks => [ 'kivi.validate_form' ],
3d774e4a Moritz Bunkus
only_if => $params{is_sales_quo} || $params{is_pur_ord},
36c8af52 Moritz Bunkus
],
action => [
ff424b75 Moritz Bunkus
t8('Purchase Order'),
3ddf1e88 Sven Schöling
submit => [ '#form', { action => "purchase_order" } ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
82372529 Sven Schöling
checks => [ 'kivi.validate_form' ],
3d774e4a Moritz Bunkus
only_if => $params{is_sales_ord} || $params{is_req_quo},
36c8af52 Moritz Bunkus
],
action => [
ff424b75 Moritz Bunkus
t8('Delivery Order'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "delivery_order" } ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
82372529 Sven Schöling
checks => [ 'kivi.validate_form' ],
36c8af52 Moritz Bunkus
only_if => $params{is_sales_ord} || $params{is_pur_ord},
],
action => [
ff424b75 Moritz Bunkus
t8('Invoice'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "invoice" } ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
82372529 Sven Schöling
checks => [ 'kivi.validate_form' ],
36c8af52 Moritz Bunkus
only_if => $allow_invoice,
],
ff424b75 Moritz Bunkus
action => [
t8('Quotation'),
c2aaf253 Moritz Bunkus
submit => [ '#form', { action => "quotation" } ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
82372529 Sven Schöling
checks => [ 'kivi.validate_form' ],
3d774e4a Moritz Bunkus
only_if => $params{is_sales_ord},
ff424b75 Moritz Bunkus
],
action => [
t8('Request for Quotation'),
3ddf1e88 Sven Schöling
submit => [ '#form', { action => "request_for_quotation" } ],
ff424b75 Moritz Bunkus
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
82372529 Sven Schöling
checks => [ 'kivi.validate_form' ],
3d774e4a Moritz Bunkus
only_if => $params{is_pur_ord},
ff424b75 Moritz Bunkus
],
], # end of combobox "Workflow"

combobox => [
action => [ t8('Export') ],
action => [
t8('Print'),
c2aaf253 Moritz Bunkus
call => [ 'kivi.SalesPurchase.show_print_dialog' ],
82372529 Sven Schöling
checks => [ 'kivi.validate_form' ],
ff424b75 Moritz Bunkus
],
action => [
t8('E Mail'),
c45d7eed Bernd Bleßmann
call => [ 'kivi.SalesPurchase.show_email_dialog' ],
checks => [ 'kivi.validate_form' ],
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
ff424b75 Moritz Bunkus
],
a59f11b0 Moritz Bunkus
action => [
t8('Download attachments of all parts'),
call => [ 'kivi.File.downloadOrderitemsFiles', $::form->{type}, $::form->{id} ],
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
only_if => $::instance_conf->get_doc_storage,
],
ff424b75 Moritz Bunkus
], #end of combobox "Export"

combobox => [
action => [ t8('more') ],
action => [
t8('History'),
call => [ 'set_history_window', $form->{id} * 1, 'id' ],
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
],
action => [
t8('Follow-Up'),
call => [ 'follow_up_window' ],
disabled => !$form->{id} ? t8('This record has not been saved yet.') : undef,
],
], # end of combobox "more"
);
3ff7346d Moritz Bunkus
}
82372529 Sven Schöling
$::request->layout->add_javascripts('kivi.Validator.js');
3ff7346d Moritz Bunkus
}

d281eedf Moritz Bunkus
sub setup_oe_search_action_bar {
my %params = @_;

for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
t8('Search'),
submit => [ '#form' ],
accesskey => 'enter',
82372529 Sven Schöling
checks => [ 'kivi.validate_form' ],
d281eedf Moritz Bunkus
],
);
}
82372529 Sven Schöling
$::request->layout->add_javascripts('kivi.Validator.js');
d281eedf Moritz Bunkus
}

cded3d3b Moritz Bunkus
sub setup_oe_orders_action_bar {
my %params = @_;

return unless $::form->{type} eq 'sales_order';

for my $bar ($::request->layout->get('actionbar')) {
$bar->add(
action => [
t8('New sales order'),
submit => [ '#form', { action => 'edit' } ],
checks => [ [ 'kivi.check_if_entries_selected', '[name^=multi_id_]' ] ],
accesskey => 'enter',
],
);
}
}

d319704a Moritz Bunkus
sub form_header {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();
b6dc5623 Sven Schöling
my @custom_hiddens;
dc0dff70 Sven Schöling
5d59ad23 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
5494f687 Sven Schöling
my $cgi = $::request->{cgi};
5d59ad23 Sven Schöling
8c7e4493 Moritz Bunkus
check_oe_access();

732533d0 Moritz Bunkus
# Container for template variables. Unfortunately this has to be
dc50b737 Sven Schöling
# visible in form_footer too, so package local level and not my here.
94f18b16 Sven Schöling
my $TMPL_VAR = $::request->cache('tmpl_var', {});
29a62ccc Moritz Bunkus
if ($form->{id}) {
94f18b16 Sven Schöling
$TMPL_VAR->{oe_obj} = SL::DB::Order->new(id => $form->{id})->load;
29a62ccc Moritz Bunkus
}
94f18b16 Sven Schöling
$TMPL_VAR->{vc_obj} = SL::DB::Customer->new(id => $form->{customer_id})->load if $form->{customer_id};
$TMPL_VAR->{vc_obj} = SL::DB::Vendor->new(id => $form->{vendor_id})->load if $form->{vendor_id};
d319704a Moritz Bunkus
fb37acdc Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);

08e820fa Moritz Bunkus
my $current_employee = SL::DB::Manager::Employee->current;
$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->{employee_id} ||= $current_employee->id;
$form->{salesman_id} ||= $current_employee->id;
d6bf475a Moritz Bunkus
b6dc5623 Sven Schöling
# openclosed checkboxes
a3db203d Moritz Bunkus
my @tmp;
b6dc5623 Sven Schöling
push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
dd9b159a Jan Büren
$form->{"delivered"} ? "checked" : "", $locale->text('Delivery Order(s) for full qty created') if $form->{"type"} =~ /_order$/;
b6dc5623 Sven Schöling
push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
$form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
94f18b16 Sven Schöling
$TMPL_VAR->{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
d319704a Moritz Bunkus
f5c1b2e1 Moritz Bunkus
my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
41adf433 Moritz Bunkus
6e603868 Geoffrey Richardson
$form->get_lists("taxzones" => ($form->{id} ? "ALL_TAXZONES" : "ALL_ACTIVE_TAXZONES"),
1e251313 Moritz Bunkus
"currencies" => "ALL_CURRENCIES",
"price_factors" => "ALL_PRICE_FACTORS");
77807bf5 Geoffrey Richardson
$form->{ALL_PAYMENTS} = SL::DB::Manager::PaymentTerm->get_all( where => [ or => [ obsolete => 0, id => $form->{payment_id} || undef ] ]);
f5c1b2e1 Moritz Bunkus
38823dc1 Moritz Bunkus
$form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
473431b1 Moritz Bunkus
$form->{ALL_LANGUAGES} = SL::DB::Manager::Language->get_all_sorted;
dd2ee66b Geoffrey Richardson
41adf433 Moritz Bunkus
# 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 (($vc eq 'customers') && $::instance_conf->get_customer_projects_only_in_sales) {
@customer_cond = (
or => [
customer_id => $::form->{customer_id},
billable_customer_id => $::form->{customer_id},
]);
}
my @conditions = (
or => [
and => [ active => 1, @customer_cond ],
@old_ids_cond,
]);

94f18b16 Sven Schöling
$TMPL_VAR->{ALL_PROJECTS} = SL::DB::Manager::Project->get_all_sorted(query => \@conditions);
2f5363d2 Moritz Bunkus
$TMPL_VAR->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_valid($form->{delivery_term_id});
94f18b16 Sven Schöling
$form->{ALL_PROJECTS} = $TMPL_VAR->{ALL_PROJECTS}; # make projects available for second row drop-down in io.pl
41adf433 Moritz Bunkus
91ab1ef6 Sven Schöling
# label subs
8af39a65 Moritz Bunkus
my $employee_list_query_gen = sub { $::form->{$_[0]} ? [ or => [ id => $::form->{$_[0]}, deleted => 0 ] ] : [ deleted => 0 ] };
94f18b16 Sven Schöling
$TMPL_VAR->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => $employee_list_query_gen->('employee_id'));
$TMPL_VAR->{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all_sorted(query => $employee_list_query_gen->('salesman_id'));
$TMPL_VAR->{ALL_SHIPTO} = SL::DB::Manager::Shipto->get_all_sorted(query => [
a75836ca Bernd Bleßmann
or => [ and => [ trans_id => $::form->{"$::form->{vc}_id"} * 1, module => 'CT' ], and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
9b67d27c Sven Schöling
]);
94f18b16 Sven Schöling
$TMPL_VAR->{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
cb2abccd Sven Schöling
or => [
cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
and => [
cp_cv_id => undef,
cp_id => $::form->{cp_id} * 1
]
]
]);
94f18b16 Sven Schöling
$TMPL_VAR->{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
b6dc5623 Sven Schöling
# currencies and exchangerate
$form->{currency} = $form->{defaultcurrency} unless $form->{currency};
94f18b16 Sven Schöling
$TMPL_VAR->{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
b6dc5623 Sven Schöling
push @custom_hiddens, "forex";
push @custom_hiddens, "exchangerate" if $form->{forex};

# credit remaining
5d59ad23 Sven Schöling
my $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
94f18b16 Sven Schöling
$TMPL_VAR->{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
96d3ef85 Sven Schöling
b6dc5623 Sven Schöling
# business
94f18b16 Sven Schöling
$TMPL_VAR->{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
54e4131e Moritz Bunkus
7349649b Geoffrey Richardson
push @custom_hiddens, "customer_pricegroup_id" if $form->{vc} eq 'customer';
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
my $credittext = $locale->text('Credit Limit exceeded!!!');
52983c08 Sven Schöling
7a7f33b5 Moritz Bunkus
my $follow_up_vc = $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
b4c74fe9 Moritz Bunkus
$follow_up_vc =~ s/--\d*\s*$//;
94f18b16 Sven Schöling
$TMPL_VAR->{follow_up_trans_info} = ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
7a7f33b5 Moritz Bunkus
if ($form->{id}) {
a7b92129 Waldemar Toews
my $follow_ups = FU->follow_ups('trans_id' => $form->{id}, 'not_done' => 1);
7a7f33b5 Moritz Bunkus
if (scalar @{ $follow_ups }) {
94f18b16 Sven Schöling
$TMPL_VAR->{num_follow_ups} = scalar @{ $follow_ups };
$TMPL_VAR->{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
7a7f33b5 Moritz Bunkus
}
}

3c255f28 Sven Schöling
my $dispatch_to_popup = '';
5d8dbe98 Bernd Bleßmann
if ($form->{resubmit} && ($form->{format} eq "html")) {
3c255f28 Sven Schöling
$dispatch_to_popup = "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';";
$dispatch_to_popup .= "document.do.submit();";
c30c60a3 freiphone
} elsif ($form->{resubmit} && $form->{action_print}) {
5d8dbe98 Bernd Bleßmann
# emulate click for resubmitting actions
c30c60a3 freiphone
$dispatch_to_popup = "kivi.SalesPurchase.show_print_dialog(); kivi.SalesPurchase.print_record();";
5d8dbe98 Bernd Bleßmann
} elsif ($creditwarning) {
0710af69 Bernd Bleßmann
$::request->{layout}->add_javascripts_inline("alert('$credittext');");
5d8dbe98 Bernd Bleßmann
}
52983c08 Sven Schöling
579920a7 Bernd Bleßmann
$::request->{layout}->add_javascripts_inline("\$(function(){$dispatch_to_popup});");
94f5cb15 Bernd Bleßmann
$TMPL_VAR->{dateformat} = $myconfig{dateformat};
$TMPL_VAR->{numberformat} = $myconfig{numberformat};
$TMPL_VAR->{longdescription_dialog_size_percentage} = SL::Helper::UserPreferences::DisplayPreferences->new()->get_longdescription_dialog_size_percentage();
7a7f33b5 Moritz Bunkus
92331b8e Moritz Bunkus
if ($form->{type} eq 'sales_order') {
if (!$form->{periodic_invoices_config}) {
$form->{periodic_invoices_status} = $locale->text('not configured');

} else {
65b2387a Moritz Bunkus
my $config = SL::YAML::Load($form->{periodic_invoices_config});
92331b8e Moritz Bunkus
$form->{periodic_invoices_status} = $config->{active} ? $locale->text('active') : $locale->text('inactive');
}
}

fdb9921e Sven Schöling
$::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.SalesPurchase kivi.File kivi.Part kivi.CustomerVendor kivi.Validator show_form_details show_history show_vc_details ckeditor5/ckeditor ckeditor5/translations/de kivi.io));
fb4d2ffa Moritz Bunkus
11dd62da Sven Schöling
# original snippets:
3ff7346d Moritz Bunkus
my %type_check_vars = (
is_sales => scalar($form->{type} =~ /^sales_/),
is_order => scalar($form->{type} =~ /_order$/),
is_sales_quo => scalar($form->{type} =~ /sales_quotation$/),
is_req_quo => scalar($form->{type} =~ /request_quotation$/),
is_sales_ord => scalar($form->{type} =~ /sales_order$/),
is_pur_ord => scalar($form->{type} =~ /purchase_order$/),
);
11dd62da Sven Schöling
a5389b65 Moritz Bunkus
setup_oe_action_bar(
%type_check_vars,
oe_obj => $TMPL_VAR->{oe_obj},
vc_obj => $TMPL_VAR->{vc_obj},
);
11dd62da Sven Schöling
d319704a Moritz Bunkus
$form->header;
6637a9e9 Martin Helmling
if ($form->{CFDD_shipto} && $form->{CFDD_shipto_id} ) {
$form->{shipto_id} = $form->{CFDD_shipto_id};
}
4493d1eb Moritz Bunkus
94f18b16 Sven Schöling
$TMPL_VAR->{HIDDENS} = [ map { name => $_, value => $form->{$_} },
22bcb6b1 Moritz Bunkus
qw(id type vc proforma queued printed emailed
b6dc5623 Sven Schöling
title creditlimit creditremaining tradediscount business
4cb932ab Moritz Bunkus
max_dunning_level dunning_amount
CFDD_shipto CFDD_shipto_id
22bcb6b1 Moritz Bunkus
taxpart taxservice taxaccounts cursor_fokus
cd812c6b Bernd Bleßmann
show_details useasnew),
b6dc5623 Sven Schöling
@custom_hiddens,
4e8e33e9 Geoffrey Richardson
map { $_.'_rate', $_.'_description', $_.'_taxnumber', $_.'_tax_id' } split / /, $form->{taxaccounts} ]; # deleted: discount
b6dc5623 Sven Schöling
94f18b16 Sven Schöling
$TMPL_VAR->{$_} = $type_check_vars{$_} for keys %type_check_vars;
d319704a Moritz Bunkus
94f18b16 Sven Schöling
$TMPL_VAR->{ORDER_PROBABILITIES} = [ map { { title => ($_ * 10) . '%', id => $_ * 10 } } (0..10) ];
3da73190 Moritz Bunkus
6f1da6d4 Moritz Bunkus
if ($type_check_vars{is_sales} && $::instance_conf->get_transport_cost_reminder_article_number_id) {
94f18b16 Sven Schöling
$TMPL_VAR->{transport_cost_reminder_article} = SL::DB::Part->new(id => $::instance_conf->get_transport_cost_reminder_article_number_id)->load;
6f1da6d4 Moritz Bunkus
}

94f18b16 Sven Schöling
print $form->parse_html_template("oe/form_header", {
%$TMPL_VAR,
%type_check_vars,
});
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub form_footer {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

d319704a Moritz Bunkus
$form->{invtotal} = $form->{invsubtotal};

94f18b16 Sven Schöling
my $TMPL_VAR = $::request->cache('tmpl_var', {});

2174b7ba Thomas Heck
if( $form->{customer_id} && !$form->{taxincluded_changed_by_user} ) {
6e08e454 Thomas Heck
my $customer = SL::DB::Customer->new(id => $form->{customer_id})->load();
$form->{taxincluded} = defined($customer->taxincluded_checked) ? $customer->taxincluded_checked : $myconfig{taxincluded_checked};
7bff84cb Thomas Heck
}

d319704a Moritz Bunkus
if (!$form->{taxincluded}) {

5d59ad23 Sven Schöling
foreach my $item (split / /, $form->{taxaccounts}) {
d319704a Moritz Bunkus
if ($form->{"${item}_base"}) {
b6dc5623 Sven Schöling
$form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
$form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);

94f18b16 Sven Schöling
$TMPL_VAR->{tax} .= qq|
b65a230d Sven Schöling
<tr>
<th align=right>$form->{"${item}_description"}&nbsp;| . $form->{"${item}_rate"} * 100 .qq|%</th>
<td align=right>$form->{"${item}_total"}</td>
</tr> |;
d319704a Moritz Bunkus
}
}
b6dc5623 Sven Schöling
} else {
5d59ad23 Sven Schöling
foreach my $item (split / /, $form->{taxaccounts}) {
d319704a Moritz Bunkus
if ($form->{"${item}_base"}) {
b6dc5623 Sven Schöling
$form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
$form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
$form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
$form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);

94f18b16 Sven Schöling
$TMPL_VAR->{tax} .= qq|
b65a230d Sven Schöling
<tr>
<th align=right>Enthaltene $form->{"${item}_description"}&nbsp;| . $form->{"${item}_rate"} * 100 .qq|%</th>
<td align=right>$form->{"${item}_total"}</td>
</tr>
<tr>
<th align=right>Nettobetrag</th>
<td align=right>$form->{"${item}_netto"}</td>
</tr> |;
d319704a Moritz Bunkus
}
}
}

030c2086 Rolf Fluehmann
my $grossamount = $form->{invtotal};
$form->{invtotal} = $form->round_amount( $form->{invtotal}, 2, 1);
$form->{rounding} = $form->round_amount(
$form->{invtotal} - $form->round_amount($grossamount, 2),
2
);
d319704a Moritz Bunkus
$form->{oldinvtotal} = $form->{invtotal};

22bcb6b1 Moritz Bunkus
my $print_options_html = setup_sales_purchase_print_options();

5428f5f6 Moritz Bunkus
my $shipto_cvars = SL::DB::Shipto->new->cvars_by_config;
foreach my $var (@{ $shipto_cvars }) {
my $name = "shiptocvar_" . $var->config->name;
$var->value($form->{$name}) if exists $form->{$name};
}

2a5030b2 Sven Schöling
print $form->parse_html_template("oe/form_footer", {
94f18b16 Sven Schöling
%$TMPL_VAR,
22bcb6b1 Moritz Bunkus
print_options => $print_options_html,
9aa0d477 Sven Schöling
is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
5428f5f6 Moritz Bunkus
shipto_cvars => $shipto_cvars,
b6dc5623 Sven Schöling
});
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub update {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();
96d3ef85 Sven Schöling
b95c5658 Moritz Bunkus
my ($recursive_call) = @_;
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;

8c7e4493 Moritz Bunkus
check_oe_access();

ccc50abd Moritz Bunkus
set_headings($form->{"id"} ? "edit" : "add");

54e4131e Moritz Bunkus
$form->{update} = 1;
96d3ef85 Sven Schöling
baf226d0 Moritz Bunkus
my $vc = $form->{vc};
if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
$::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};

844a541e Moritz Bunkus
if ($vc eq 'customer') {
IS->get_customer(\%myconfig, $form);
$::form->{billing_address_id} = $::form->{default_billing_address_id};
} else {
IR->get_vendor(\%myconfig, $form);
}
baf226d0 Moritz Bunkus
}
96d3ef85 Sven Schöling
2e26ccd5 Bernd Bleßmann
if (!$form->{forex}) { # read exchangerate from input field (not hidden)
map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
}
5d59ad23 Sven Schöling
my $buysell = 'buy';
d319704a Moritz Bunkus
$buysell = 'sell' if ($form->{vc} eq 'vendor');
a53233e5 Sven Schöling
$form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
$form->{exchangerate} = $form->{forex} if $form->{forex};
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
my $exchangerate = $form->{exchangerate} || 1;
d319704a Moritz Bunkus
b6dc5623 Sven Schöling
##################### process items ######################################
# for pricegroups
5d59ad23 Sven Schöling
my $i = $form->{rowcount};
d319704a Moritz Bunkus
if ( ($form->{"partnumber_$i"} eq "")
&& ($form->{"description_$i"} eq "")
&& ($form->{"partsgroup_$i"} eq "")) {

$form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});

82574e78 Geoffrey Richardson
&check_form;
54e4131e Moritz Bunkus
} else {

cb253140 Moritz Bunkus
my $mode;
b6dc5623 Sven Schöling
if ($form->{type} =~ /^sales/) {
d319704a Moritz Bunkus
IS->retrieve_item(\%myconfig, \%$form);
cb253140 Moritz Bunkus
$mode = 'IS';
b6dc5623 Sven Schöling
} else {
IR->retrieve_item(\%myconfig, \%$form);
cb253140 Moritz Bunkus
$mode = 'IR';
d319704a Moritz Bunkus
}

my $rows = scalar @{ $form->{item_list} };
a325f1b7 Geoffrey Richardson
97908d77 Bernd Bleßmann
$form->{"discount_$i"} = $form->parse_amount(\%myconfig, $form->{"discount_$i"}) / 100.0;
$form->{"discount_$i"} ||= $form->{"$form->{vc}_discount"};
d319704a Moritz Bunkus
da804bf2 Geoffrey Richardson
$form->{"lastcost_$i"} = $form->parse_amount(\%myconfig, $form->{"lastcost_$i"});

d319704a Moritz Bunkus
if ($rows) {
2a61343f Thomas Heck
$form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
if( !$form->{"qty_$i"} ) {
$form->{"qty_$i"} = 1;
}
d319704a Moritz Bunkus
if ($rows > 1) {

6ea1dd9b Bernd Bleßmann
select_item(mode => $mode, pre_entered_qty => $form->{"qty_$i"});
09479f02 Moritz Bunkus
$::dispatcher->end_request;
d319704a Moritz Bunkus
} else {

5d59ad23 Sven Schöling
my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
05fc6cef Jan Büren
# hier werden parts (Artikeleigenschaften) aus item_list (retrieve_item aus IS.pm)
# (item wahrscheinlich synonym für parts) entsprechend in die form geschrieben ...
224af4c2 Jan Büren
3c2e635c Sven Schöling
# Wäre dieses Mapping nicht besser in retrieve_items aufgehoben?
224af4c2 Jan Büren
#(Eine Funktion bekommt Daten -> ARBEIT -> Rückgabe DATEN)
# Das quot sieht doch auch nach Überarbeitung aus ... (hmm retrieve_items gibt es in IS und IR)
52983c08 Sven Schöling
map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g } qw(partnumber description unit);
map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
3c2e635c Sven Schöling
05fc6cef Jan Büren
# ... deswegen muss die prüfung, ob es sich um einen nicht rabattierfähigen artikel handelt später erfolgen (Bug 1136)
$form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
52983c08 Sven Schöling
$form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
d319704a Moritz Bunkus
1e251313 Moritz Bunkus
$form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};

52983c08 Sven Schöling
($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
5d59ad23 Sven Schöling
my $dec_qty = length $1;
my $decimalplaces = max 2, $dec_qty;
d319704a Moritz Bunkus
if ($sellprice) {
$form->{"sellprice_$i"} = $sellprice;
} else {
89b26688 Sven Schöling
my $record = _make_record();
my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
my $best_price = $price_source->best_price;
my $best_discount = $price_source->best_discount;
df1b03d5 Sven Schöling
if ($best_price) {
$::form->{"sellprice_$i"} = $best_price->price;
$::form->{"active_price_source_$i"} = $best_price->source;
}
89b26688 Sven Schöling
if ($best_discount) {
$::form->{"discount_$i"} = $best_discount->discount;
$::form->{"active_discount_source_$i"} = $best_discount->source;
}
df1b03d5 Sven Schöling
52983c08 Sven Schöling
$form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
d319704a Moritz Bunkus
}

97908d77 Bernd Bleßmann
my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"});
52983c08 Sven Schöling
map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
d319704a Moritz Bunkus
$form->{creditremaining} -= $amount;

52983c08 Sven Schöling
$form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
da804bf2 Geoffrey Richardson
$form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
52983c08 Sven Schöling
$form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
97908d77 Bernd Bleßmann
$form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
d319704a Moritz Bunkus
}

b6dc5623 Sven Schöling
display_form();
d319704a Moritz Bunkus
} else {

# ok, so this is a new part
# ask if it is a part or service item

if ( $form->{"partsgroup_$i"}
&& ($form->{"partsnumber_$i"} eq "")
&& ($form->{"description_$i"} eq "")) {
$form->{rowcount}--;
$form->{"discount_$i"} = "";

b6dc5623 Sven Schöling
display_form();
d83df93a Moritz Bunkus
} else {
d319704a Moritz Bunkus
$form->{"id_$i"} = 0;
d83df93a Moritz Bunkus
new_item();
d319704a Moritz Bunkus
}
}
}
b6dc5623 Sven Schöling
##################### process items ######################################

e5d45c41 Stephan Köhler
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub search {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
d319704a Moritz Bunkus
3ad96f21 Bernd Bleßmann
check_oe_access(with_view => 1);
8c7e4493 Moritz Bunkus
d319704a Moritz Bunkus
if ($form->{type} eq 'purchase_order') {
df005c49 Sven Schöling
$form->{vc} = 'vendor';
$form->{ordnrname} = 'ordnumber';
$form->{title} = $locale->text('Purchase Orders');
$form->{ordlabel} = $locale->text('Order Number');

c4bcc898 Bernd Bleßmann
} elsif ($form->{type} eq 'purchase_order_confirmation') {
$form->{vc} = 'vendor';
$form->{ordnrname} = 'ordnumber';
$form->{title} = $locale->text('Purchase Order Confirmations');
00162a3d Bernd Bleßmann
$form->{ordlabel} = $locale->text('Order Confirmation Number');
c4bcc898 Bernd Bleßmann
df005c49 Sven Schöling
} elsif ($form->{type} eq 'request_quotation') {
$form->{vc} = 'vendor';
$form->{ordnrname} = 'quonumber';
$form->{title} = $locale->text('Request for Quotations');
$form->{ordlabel} = $locale->text('RFQ Number');

c1de38e0 Bernd Bleßmann
} elsif ($form->{type} eq 'purchase_quotation_intake') {
$form->{vc} = 'vendor';
$form->{ordnrname} = 'quonumber';
$form->{title} = $locale->text('Purchase Quotation Intakes');
$form->{ordlabel} = $locale->text('Quotation Number');

30913a9b Bernd Bleßmann
} elsif ($form->{type} eq 'sales_order_intake') {
$form->{vc} = 'customer';
$form->{ordnrname} = 'ordnumber';
$form->{title} = $locale->text('Sales Order Intakes');
$form->{ordlabel} = $locale->text('Order Number');

df005c49 Sven Schöling
} elsif ($form->{type} eq 'sales_order') {
$form->{vc} = 'customer';
$form->{ordnrname} = 'ordnumber';
30913a9b Bernd Bleßmann
$form->{title} = $locale->text('Sales Order Confirmations');
df005c49 Sven Schöling
$form->{ordlabel} = $locale->text('Order Number');

} elsif ($form->{type} eq 'sales_quotation') {
$form->{vc} = 'customer';
$form->{ordnrname} = 'quonumber';
$form->{title} = $locale->text('Quotations');
$form->{ordlabel} = $locale->text('Quotation Number');
f5c1b2e1 Moritz Bunkus
df005c49 Sven Schöling
} else {
a8459f49 Moritz Bunkus
$form->show_generic_error($locale->text('oe.pl::search called with unknown type'));
e7367fb5 Thomas Kasulke
}
96d3ef85 Sven Schöling
df005c49 Sven Schöling
# setup vendor / customer data
$form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
3fcf64fc Bernd Bleßmann
"taxzones" => "ALL_TAXZONES",
"business_types" => "ALL_BUSINESS_TYPES",);
fa51a82e Bernd Bleßmann
$form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
$form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
$form->{ALL_ORDER_STATUSES} = SL::DB::Manager::OrderStatus->get_all_sorted;
e7367fb5 Thomas Kasulke
7e19c216 Bernd Bleßmann
$form->{CT_CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
($form->{CT_CUSTOM_VARIABLES_FILTER_CODE},
$form->{CT_CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CT_CUSTOM_VARIABLES},
'include_prefix' => 'l_',
'include_value' => 'Y');

df005c49 Sven Schöling
# constants and subs for template
672ff8cc Sven Schöling
$form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
f5c1b2e1 Moritz Bunkus
3da73190 Moritz Bunkus
$form->{ORDER_PROBABILITIES} = [ map { { title => ($_ * 10) . '%', id => $_ * 10 } } (0..10) ];

9e02b1f2 Moritz Bunkus
$::request->{layout}->use_javascript(map { "${_}.js" } qw(autocomplete_project));

d281eedf Moritz Bunkus
setup_oe_search_action_bar();

df005c49 Sven Schöling
$form->header();
d319704a Moritz Bunkus
a00e1b52 Sven Schöling
print $form->parse_html_template('oe/search', {
c7771eec Moritz Bunkus
is_order => scalar($form->{type} =~ /_order/),
a00e1b52 Sven Schöling
});
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

77923db3 Moritz Bunkus
sub create_subtotal_row {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();
77923db3 Moritz Bunkus
my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;

5d59ad23 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;

77923db3 Moritz Bunkus
my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };

map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };

$row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);

map { $totals->{$_} = 0 } @{ $subtotal_columns };

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
77923db3 Moritz Bunkus
return $row;
}

d319704a Moritz Bunkus
sub orders {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
5494f687 Sven Schöling
my $cgi = $::request->{cgi};
d319704a Moritz Bunkus
b122ba4c Jan Büren
my %params = @_;
3ad96f21 Bernd Bleßmann
check_oe_access(with_view => 1);
8c7e4493 Moritz Bunkus
c4bcc898 Bernd Bleßmann
my $ordnumber = ($form->{type} =~ /_order_intake$|_order$|purchase_order_confirmation/) ? "ordnumber" : "quonumber";
d319704a Moritz Bunkus
f797374a Sven Schöling
($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) = split(/--/, $form->{ $form->{vc} });
fc8ad1a3 Moritz Bunkus
report_generator_set_default_sort('transdate', 1);
12c4c658 Moritz Bunkus
OE->transactions(\%myconfig, \%$form);

77923db3 Moritz Bunkus
$form->{rowcount} = scalar @{ $form->{OE} };

my @columns = (
ce3ce404 Moritz Bunkus
"transdate", "reqdate",
f825d995 Bernd Bleßmann
"id", $ordnumber,
63c36f48 Bernd Bleßmann
"cusordnumber", "vendor_confirmation_number",
"customernumber",
ce3ce404 Moritz Bunkus
"name", "netamount",
"tax", "amount",
50133d13 Sven Schöling
"remaining_netamount", "remaining_amount",
ce3ce404 Moritz Bunkus
"curr", "employee",
4a19c3d4 Udo Spallek
"salesman",
ce3ce404 Moritz Bunkus
"shipvia", "globalprojectnumber",
bead93d9 Bernd Bleßmann
"transaction_description", "department", "open",
92331b8e Moritz Bunkus
"delivered", "periodic_invoices",
"marge_total", "marge_percent",
130ba50d David Ohlbrecht
"vcnumber", "ustid",
3fcf64fc Bernd Bleßmann
"country", "shippingpoint",
a809ab0a Bernd Bleßmann
"taxzone", "insertdate",
3da73190 Moritz Bunkus
"order_probability", "expected_billing_date", "expected_netamount",
fa51a82e Bernd Bleßmann
"payment_terms", "intnotes", "order_status",
3a77cbe1 Bernd Bleßmann
"items",
98b16fdb Tamino Steinert
"shiptoname", "shiptodepartment_1", "shiptodepartment_2", "shiptostreet",
"shiptozipcode", "shiptocity", "shiptocountry",
ce3ce404 Moritz Bunkus
);
d319704a Moritz Bunkus
626e0240 Stephan Köhler
# only show checkboxes if gotten here via sales_order form.
30913a9b Bernd Bleßmann
my $allow_multiple_orders = $form->{type} eq 'sales_order_intake' || $form->{type} eq 'sales_order';
77923db3 Moritz Bunkus
if ($allow_multiple_orders) {
unshift @columns, "ids";
626e0240 Stephan Köhler
}

92331b8e Moritz Bunkus
$form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
$form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
$form->{l_periodic_invoices} = "Y" if ($form->{periodic_invoices_active} && $form->{periodic_invoices_inactive});
3da73190 Moritz Bunkus
map { $form->{"l_${_}"} = 'Y' } qw(order_probability expected_billing_date expected_netamount) if $form->{l_order_probability_expected_billing_date};

77923db3 Moritz Bunkus
my $attachment_basename;
d319704a Moritz Bunkus
if ($form->{vc} eq 'vendor') {
5b60c0df Moritz Bunkus
if ($form->{type} eq 'purchase_order') {
77923db3 Moritz Bunkus
$form->{title} = $locale->text('Purchase Orders');
$attachment_basename = $locale->text('purchase_order_list');
c4bcc898 Bernd Bleßmann
} elsif ($form->{type} eq 'purchase_order_confirmation') {
$form->{title} = $locale->text('Purchase Order Confirmations');
$attachment_basename = $locale->text('purchase_order_confirmation_list');
c1de38e0 Bernd Bleßmann
} elsif ($form->{type} eq 'purchase_quotation_intake') {
$form->{title} = $locale->text('Purchase Quotation Intakes');
$attachment_basename = $locale->text('purchase_quotation_intake_list');
d319704a Moritz Bunkus
} else {
77923db3 Moritz Bunkus
$form->{title} = $locale->text('Request for Quotations');
$attachment_basename = $locale->text('rfq_list');
d319704a Moritz Bunkus
}
77923db3 Moritz Bunkus
} else {
30913a9b Bernd Bleßmann
if ($form->{type} eq 'sales_order_intake') {
$form->{title} = $locale->text('Sales Order Intakes');
$attachment_basename = $locale->text('sales_order_intake_list');
} elsif ($form->{type} eq 'sales_order') {
77923db3 Moritz Bunkus
$form->{title} = $locale->text('Sales Orders');
$attachment_basename = $locale->text('sales_order_list');
d319704a Moritz Bunkus
} else {
77923db3 Moritz Bunkus
$form->{title} = $locale->text('Quotations');
$attachment_basename = $locale->text('quotation_list');
d319704a Moritz Bunkus
}
77923db3 Moritz Bunkus
}

my $report = SL::ReportGenerator->new(\%myconfig, $form);

7e19c216 Bernd Bleßmann
my $ct_cvar_configs = CVar->get_configs('module' => 'CT');
my @ct_includeable_custom_variables = grep { $_->{includeable} } @{ $ct_cvar_configs };
my @ct_searchable_custom_variables = grep { $_->{searchable} } @{ $ct_cvar_configs };

my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @ct_includeable_custom_variables;
push @columns, map { "cvar_$_->{name}" } @ct_includeable_custom_variables;

77923db3 Moritz Bunkus
my @hidden_variables = map { "l_${_}" } @columns;
98b16fdb Tamino Steinert
push @hidden_variables, "l_subtotal", $form->{vc}, qw(
l_closed l_notdelivered open closed delivered notdelivered ordnumber
quonumber cusordnumber transaction_description transdatefrom transdateto
type vc employee_id salesman_id reqdatefrom reqdateto projectnumber
project_id periodic_invoices_active periodic_invoices_inactive
business_id shippingpoint taxzone_id reqdate_unset_or_old insertdatefrom
insertdateto order_probability_op order_probability_value
expected_billing_date_from expected_billing_date_to parts_partnumber
parts_description all department_id intnotes phone_notes fulltext
order_status_id shiptoname shiptodepartment_1 shiptodepartment_2
shiptostreet shiptozipcode shiptocity shiptocountry
63c36f48 Bernd Bleßmann
vendor_confirmation_number
98b16fdb Tamino Steinert
);
7e19c216 Bernd Bleßmann
push @hidden_variables, map { "cvar_$_->{name}" } @ct_searchable_custom_variables;
77923db3 Moritz Bunkus
3fcf64fc Bernd Bleßmann
my @keys_for_url = grep { $form->{$_} } @hidden_variables;
push @keys_for_url, 'taxzone_id' if $form->{taxzone_id} ne ''; # taxzone_id could be 0

b122ba4c Jan Büren
my $href = $params{want_binary_pdf} ? '' : build_std_url('action=orders', @keys_for_url);
77923db3 Moritz Bunkus
my %column_defs = (
'ids' => { 'text' => '', },
'transdate' => { 'text' => $locale->text('Date'), },
a00e1b52 Sven Schöling
'reqdate' => { 'text' => $form->{type} =~ /_order/ ? $locale->text('Required by') : $locale->text('Valid until') },
77923db3 Moritz Bunkus
'id' => { 'text' => $locale->text('ID'), },
00162a3d Bernd Bleßmann
'ordnumber' => { 'text' => $form->{type} eq "purchase_order_confirmation" ? $locale->text('Confirmation'): $locale->text('Order'), },
77923db3 Moritz Bunkus
'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
b96c67e5 Moritz Bunkus
'cusordnumber' => { 'text' => $locale->text('Customer Order Number'), },
77923db3 Moritz Bunkus
'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
7d026c7c Niclas Zimmermann
'customernumber' => { 'text' => $locale->text('Customer Number'), },
77923db3 Moritz Bunkus
'netamount' => { 'text' => $locale->text('Amount'), },
'tax' => { 'text' => $locale->text('Tax'), },
'amount' => { 'text' => $locale->text('Total'), },
50133d13 Sven Schöling
'remaining_amount' => { 'text' => $locale->text('Remaining Amount'), },
'remaining_netamount' => { 'text' => $locale->text('Remaining Net Amount'), },
77923db3 Moritz Bunkus
'curr' => { 'text' => $locale->text('Curr'), },
4a19c3d4 Udo Spallek
'employee' => { 'text' => $locale->text('Employee'), },
'salesman' => { 'text' => $locale->text('Salesman'), },
77923db3 Moritz Bunkus
'shipvia' => { 'text' => $locale->text('Ship via'), },
'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
'transaction_description' => { 'text' => $locale->text('Transaction description'), },
bead93d9 Bernd Bleßmann
'department' => { 'text' => $locale->text('Department'), },
77923db3 Moritz Bunkus
'open' => { 'text' => $locale->text('Open'), },
4ccf2898 Wulf Coulmann
'delivered' => { 'text' => $locale->text('Delivery Order created'), },
50d3e009 Moritz Bunkus
'marge_total' => { 'text' => $locale->text('Ertrag'), },
15bcb411 Sven Schöling
'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
130ba50d David Ohlbrecht
'vcnumber' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
15bcb411 Sven Schöling
'country' => { 'text' => $locale->text('Country'), },
'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
92331b8e Moritz Bunkus
'periodic_invoices' => { 'text' => $locale->text('Per. Inv.'), },
3fcf64fc Bernd Bleßmann
'shippingpoint' => { 'text' => $locale->text('Shipping Point'), },
'taxzone' => { 'text' => $locale->text('Steuersatz'), },
a809ab0a Bernd Bleßmann
'insertdate' => { 'text' => $locale->text('Insert Date'), },
3da73190 Moritz Bunkus
'order_probability' => { 'text' => $locale->text('Order probability'), },
'expected_billing_date' => { 'text' => $locale->text('Exp. bill. date'), },
'expected_netamount' => { 'text' => $locale->text('Exp. netamount'), },
ba8e82fe Bernd Bleßmann
'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
3906bb6a Bernd Bleßmann
'intnotes' => { 'text' => $locale->text('Internal Notes'), },
fa51a82e Bernd Bleßmann
'order_status' => { 'text' => $locale->text('Status'), },
3a77cbe1 Bernd Bleßmann
'items' => { 'text' => $locale->text('Positions'), },
98b16fdb Tamino Steinert
shiptoname => { 'text' => $locale->text('Name (Shipping)'), },
shiptodepartment_1 => { 'text' => $locale->text('Department 1 (Shipping)'), },
shiptodepartment_2 => { 'text' => $locale->text('Department 2 (Shipping)'), },
shiptostreet => { 'text' => $locale->text('Street (Shipping)'), },
shiptozipcode => { 'text' => $locale->text('Zipcode (Shipping)'), },
shiptocity => { 'text' => $locale->text('City (Shipping)'), },
shiptocountry => { 'text' => $locale->text('Country (Shipping)'), },
63c36f48 Bernd Bleßmann
vendor_confirmation_number => { 'text' => $locale->text('Vendor Confirmation Number'), },
7e19c216 Bernd Bleßmann
%column_defs_cvars,
77923db3 Moritz Bunkus
);

d641058f Bernd Bleßmann
foreach my $name (qw(id transdate reqdate quonumber ordnumber cusordnumber
name employee salesman shipvia transaction_description
shippingpoint taxzone insertdate payment_terms department
63c36f48 Bernd Bleßmann
intnotes order_status vendor_confirmation_number)) {
0000e2ae Moritz Bunkus
my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
$column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
77923db3 Moritz Bunkus
}

d641058f Bernd Bleßmann
my %column_alignment =
map { $_ => 'right' } qw(netamount tax amount curr
remaining_amount remaining_netamount
order_probability expected_billing_date
expected_netamount);
77923db3 Moritz Bunkus
$form->{"l_type"} = "Y";
3a77cbe1 Bernd Bleßmann
77923db3 Moritz Bunkus
map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
$column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;

$report->set_columns(%column_defs);
$report->set_column_order(@columns);
19688fca Moritz Bunkus
$report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
0000e2ae Moritz Bunkus
$report->set_sort_indicator($form->{sort}, $form->{sortdir});
f5e4c22b Moritz Bunkus
7e19c216 Bernd Bleßmann
CVar->add_custom_variables_to_report('module' => 'CT',
'trans_id_field' => "$form->{vc}_id",
'configs' => $ct_cvar_configs,
'column_defs' => \%column_defs,
'data' => $form->{OE});

77923db3 Moritz Bunkus
my @options;
69b5b918 Sven Schöling
f7150766 Moritz Bunkus
push @options, $locale->text('Customer') . " : $form->{customer}" if $form->{customer};
push @options, $locale->text('Vendor') . " : $form->{vendor}" if $form->{vendor};
b98b8e3f Sven Schöling
push @options, $locale->text('Contact Person') . " : $form->{cp_name}" if $form->{cp_name};
dd2ee66b Geoffrey Richardson
push @options, $locale->text('Department') . " : $form->{department}" if $form->{department};
f7150766 Moritz Bunkus
push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if $form->{ordnumber};
63c36f48 Bernd Bleßmann
push @options, $locale->text('Vendor Confirmation Number') . " : $form->{vendor_confirmation_number}" if $form->{vendor_confirmation_number};
b96c67e5 Moritz Bunkus
push @options, $locale->text('Customer Order Number') . " : $form->{cusordnumber}" if $form->{cusordnumber};
f7150766 Moritz Bunkus
push @options, $locale->text('Notes') . " : $form->{notes}" if $form->{notes};
3906bb6a Bernd Bleßmann
push @options, $locale->text('Internal Notes') . " : $form->{intnotes}" if $form->{intnotes};
69b5b918 Sven Schöling
push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if $form->{transaction_description};
aa4fda0e Sven Schöling
push @options, $locale->text('Quick Search') . " : $form->{all}" if $form->{all};
3fcf64fc Bernd Bleßmann
push @options, $locale->text('Shipping Point') . " : $form->{shippingpoint}" if $form->{shippingpoint};
98b16fdb Tamino Steinert
push @options, $locale->text('Name (Shipping)') . " : $form->{shiptoname}"
if $form->{shiptoname};
push @options, $locale->text('Department 1 (Shipping)') . " : $form->{shiptodepartment_1}"
if $form->{shiptodepartment_1};
push @options, $locale->text('Department 2 (Shipping)') . " : $form->{shiptodepartment_2}"
if $form->{shiptodepartment_2};
push @options, $locale->text('Street (Shipping)') . " : $form->{shiptostreet}"
if $form->{shiptostreet};
push @options, $locale->text('Zipcode (Shipping)') . " : $form->{shiptozipcode}"
if $form->{shiptozipcode};
push @options, $locale->text('City (Shipping)') . " : $form->{shiptocity}"
if $form->{shiptocity};
push @options, $locale->text('Country (Shipping)') . " : $form->{shiptocountry}"
if $form->{shiptocountry};
fdf5b8ae Moritz Bunkus
push @options, $locale->text('Part Description') . " : $form->{parts_description}" if $form->{parts_description};
push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}" if $form->{parts_partnumber};
2d51ec2e Bernd Bleßmann
push @options, $locale->text('Phone Notes') . " : $form->{phone_notes}" if $form->{phone_notes};
991369b1 Bernd Bleßmann
push @options, $locale->text('Full Text') . " : $form->{fulltext}" if $form->{fulltext};
813b99ca Geoffrey Richardson
if ( $form->{transdatefrom} or $form->{transdateto} ) {
push @options, $locale->text('Order Date');
push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
};
if ( $form->{reqdatefrom} or $form->{reqdateto} ) {
push @options, $locale->text('Delivery Date');
push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{reqdatefrom}, 1) if $form->{reqdatefrom};
push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{reqdateto}, 1) if $form->{reqdateto};
};
a809ab0a Bernd Bleßmann
if ( $form->{insertdatefrom} or $form->{insertdateto} ) {
push @options, $locale->text('Insert Date');
push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{insertdatefrom}, 1) if $form->{insertdatefrom};
push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{insertdateto}, 1) if $form->{insertdateto};
};
69b5b918 Sven Schöling
push @options, $locale->text('Open') if $form->{open};
push @options, $locale->text('Closed') if $form->{closed};
4ccf2898 Wulf Coulmann
push @options, $locale->text('Delivery Order created') if $form->{delivered};
69b5b918 Sven Schöling
push @options, $locale->text('Not delivered') if $form->{notdelivered};
9ed4151c Bernd Bleßmann
push @options, $locale->text('Periodic invoices active') if $form->{periodic_invoices_active};
2ea1a379 Moritz Bunkus
push @options, $locale->text('Reqdate not set or before current month') if $form->{reqdate_unset_or_old};
d319704a Moritz Bunkus
fac049a0 Bernd Bleßmann
if ($form->{business_id}) {
my $vc_type_label = $form->{vc} eq 'customer' ? $locale->text('Customer type') : $locale->text('Vendor type');
push @options, $vc_type_label . " : " . SL::DB::Business->new(id => $form->{business_id})->load->description;
}
3fcf64fc Bernd Bleßmann
if ($form->{taxzone_id} ne '') { # taxzone_id could be 0
push @options, $locale->text('Steuersatz') . " : " . SL::DB::TaxZone->new(id => $form->{taxzone_id})->load->description;
}
fac049a0 Bernd Bleßmann
dd2ee66b Geoffrey Richardson
if ($form->{department_id}) {
push @options, $locale->text('Department') . " : " . SL::DB::Department->new(id => $form->{department_id})->load->description;
}

3da73190 Moritz Bunkus
if (($form->{order_probability_value} || '') ne '') {
push @options, $::locale->text('Order probability') . ' ' . ($form->{order_probability_op} eq 'le' ? '<=' : '>=') . ' ' . $form->{order_probability_value} . '%';
}

if ($form->{expected_billing_date_from} or $form->{expected_billing_date_to}) {
push @options, $locale->text('Expected billing date');
push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{expected_billing_date_from}, 1) if $form->{expected_billing_date_from};
push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{expected_billing_date_to}, 1) if $form->{expected_billing_date_to};
}

fa51a82e Bernd Bleßmann
if ($form->{order_status_id}) {
push @options, $locale->text('Status') . " : " . SL::DB::OrderStatus->new(id => $form->{order_status_id})->load->name;
}

77923db3 Moritz Bunkus
$report->set_options('top_info_text' => join("\n", @options),
9aaca433 Moritz Bunkus
'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
cded3d3b Moritz Bunkus
'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom'),
77923db3 Moritz Bunkus
'output_format' => 'HTML',
'title' => $form->{title},
'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
);
$report->set_options_from_form();
a873249c Moritz Bunkus
$locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
# add sort and escape callback, this one we use for the add sub
$form->{callback} = $href .= "&sort=$form->{sort}";
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
# escape callback for href
5d59ad23 Sven Schöling
my $callback = $form->escape($href);
d319704a Moritz Bunkus
50133d13 Sven Schöling
my @subtotal_columns = qw(netamount amount marge_total marge_percent remaining_amount remaining_netamount);
3da73190 Moritz Bunkus
push @subtotal_columns, 'expected_netamount' if $form->{l_order_probability_expected_billing_date};
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
my %totals = map { $_ => 0 } @subtotal_columns;
my %subtotals = map { $_ => 0 } @subtotal_columns;
d319704a Moritz Bunkus
8dd087bf Bernd Bleßmann
my $idx = 1;
626e0240 Stephan Köhler
b122ba4c Jan Büren
my $edit_url = $params{want_binary_pdf}
? ''
0213d50a Jan Büren
: ($::instance_conf->get_feature_experimental_order)
f825d995 Bernd Bleßmann
? build_std_url('script=controller.pl', 'action=Order/edit', 'type')
: build_std_url('action=edit', 'type', 'vc');
5d59ad23 Sven Schöling
foreach my $oe (@{ $form->{OE} }) {
77923db3 Moritz Bunkus
map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
d319704a Moritz Bunkus
92331b8e Moritz Bunkus
$oe->{tax} = $oe->{amount} - $oe->{netamount};
$oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
$oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
$oe->{periodic_invoices} = $oe->{periodic_invoices} ? $locale->text('On') : $locale->text('Off');
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
map { $subtotals{$_} += $oe->{$_};
$totals{$_} += $oe->{$_} } @subtotal_columns;
3505ff1b Moritz Bunkus
$subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
$totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
d319704a Moritz Bunkus
3da73190 Moritz Bunkus
map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent remaining_amount remaining_netamount expected_netamount);

$oe->{order_probability} = ($oe->{order_probability} || 0) . '%';
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
my $row = { };
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
foreach my $column (@columns) {
next if ($column eq 'ids');
3a77cbe1 Bernd Bleßmann
next if ($column eq 'items');
77923db3 Moritz Bunkus
$row->{$column} = {
'data' => $oe->{$column},
'align' => $column_alignment{$column},
};
}
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
$row->{ids} = {
'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
. $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
'valign' => 'center',
'align' => 'center',
};
d319704a Moritz Bunkus
b122ba4c Jan Büren
$row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}" unless $params{want_binary_pdf};
d319704a Moritz Bunkus
3a77cbe1 Bernd Bleßmann
if ($form->{l_items}) {
my $items = SL::DB::Manager::OrderItem->get_all_sorted(where => [id => $oe->{item_ids}]);
$row->{items}->{raw_data} = SL::Presenter::ItemsList::items_list($items) if lc($report->{options}->{output_format}) eq 'html';
$row->{items}->{data} = SL::Presenter::ItemsList::items_list($items, as_text => 1) if lc($report->{options}->{output_format}) ne 'html';
}

77923db3 Moritz Bunkus
my $row_set = [ $row ];
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
if (($form->{l_subtotal} eq 'Y')
8dd087bf Bernd Bleßmann
&& (($idx == (scalar @{ $form->{OE} }))
|| ($oe->{ $form->{sort} } ne $form->{OE}->[$idx]->{ $form->{sort} }))) {
77923db3 Moritz Bunkus
push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
}
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
$report->add_data($row_set);
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
$idx++;
}
d319704a Moritz Bunkus
77923db3 Moritz Bunkus
$report->add_separator();
$report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
b122ba4c Jan Büren
if ($params{want_binary_pdf}) {
$report->generate_with_headers();
return $report->generate_pdf_content(want_binary_pdf => 1);
}
cded3d3b Moritz Bunkus
setup_oe_orders_action_bar();
e7913c4c Moritz Bunkus
$report->generate_with_headers();
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

d7fef83a Moritz Bunkus
sub check_delivered_flag {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
d7fef83a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

d7fef83a Moritz Bunkus
if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
5d59ad23 Sven Schöling
return $main::lxdebug->leave_sub();
d7fef83a Moritz Bunkus
}

my $all_delivered = 0;

foreach my $i (1 .. $form->{rowcount}) {
next if (!$form->{"id_$i"});

661daf00 Martin Helmling
$form->{"ship_$i"} = 0 if $form->{saveasnew};

d7fef83a Moritz Bunkus
if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
$all_delivered = 1;
next;
}

$all_delivered = 0;
last;
}

$form->{delivered} = 1 if $all_delivered;
661daf00 Martin Helmling
$form->{delivered} = 0 if $form->{saveasnew};
d7fef83a Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d7fef83a Moritz Bunkus
}

54e4131e Moritz Bunkus
sub save_and_close {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();
d735aab3 Martin Helmling
$form->mtime_ischanged('oe');
8c7e4493 Moritz Bunkus
fb37acdc Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);

d319704a Moritz Bunkus
if ($form->{type} =~ /_order$/) {
$form->isblank("transdate", $locale->text('Order Date missing!'));
} else {
$form->isblank("transdate", $locale->text('Quotation Date missing!'));
}

a426a839 Moritz Bunkus
my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
$form->{$idx} =~ s/^\s*//g;
$form->{$idx} =~ s/\s*$//g;

5d59ad23 Sven Schöling
my $msg = ucfirst $form->{vc};
baf226d0 Moritz Bunkus
$form->isblank($form->{vc} . '_id', $locale->text($msg . " missing!"));
d319704a Moritz Bunkus
# $locale->text('Customer missing!');
# $locale->text('Vendor missing!');

$form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
if ($form->{currency} ne $form->{defaultcurrency});

&validate_items;
96d3ef85 Sven Schöling
baf226d0 Moritz Bunkus
my $vc = $form->{vc};
if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
$::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
96d3ef85 Sven Schöling
baf226d0 Moritz Bunkus
IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
IR->get_vendor(\%myconfig, $form) if $vc eq 'vendor';

844a541e Moritz Bunkus
$::form->{billing_address_id} = $::form->{default_billing_address_id};

baf226d0 Moritz Bunkus
update();
09479f02 Moritz Bunkus
$::dispatcher->end_request;
d319704a Moritz Bunkus
}

$form->{id} = 0 if $form->{saveasnew};

5d59ad23 Sven Schöling
my ($numberfld, $ordnumber, $err);
d319704a Moritz Bunkus
# this is for the internal notes section for the [email] Subject
if ($form->{type} =~ /_order$/) {
if ($form->{type} eq 'sales_order') {
$form->{label} = $locale->text('Sales Order');

$numberfld = "sonumber";
$ordnumber = "ordnumber";
} else {
$form->{label} = $locale->text('Purchase Order');

$numberfld = "ponumber";
$ordnumber = "ordnumber";
}

$err = $locale->text('Cannot save order!');

d7fef83a Moritz Bunkus
check_delivered_flag();

d319704a Moritz Bunkus
} else {
if ($form->{type} eq 'sales_quotation') {
$form->{label} = $locale->text('Quotation');

$numberfld = "sqnumber";
$ordnumber = "quonumber";
} else {
$form->{label} = $locale->text('Request for Quotation');

$numberfld = "rfqnumber";
$ordnumber = "quonumber";
}

$err = $locale->text('Cannot save quotation!');

}

aff32344 Moritz Bunkus
# get new number in sequence if saveasnew was requested
delete $form->{$ordnumber} if $form->{saveasnew};
d319704a Moritz Bunkus
b3501bdf Moritz Bunkus
relink_accounts();
e6f429e4 Moritz Bunkus
$form->error($err) if (!OE->save(\%myconfig, \%$form));

2b89ec97 Thomas Kasulke
# saving the history
if(!exists $form->{addition}) {
8cef58b2 Thomas Kasulke
$form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
b65a230d Sven Schöling
$form->{addition} = "SAVED";
a590a651 Sven Schöling
$form->save_history;
2b89ec97 Thomas Kasulke
}
e6f429e4 Moritz Bunkus
# /saving the history
b3501bdf Moritz Bunkus
e6f429e4 Moritz Bunkus
$form->redirect($form->{label} . " $form->{$ordnumber} " .
$locale->text('saved!'));
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

54e4131e Moritz Bunkus
sub save {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
54e4131e Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

d735aab3 Martin Helmling
$form->mtime_ischanged('oe');
fb37acdc Moritz Bunkus
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);


54e4131e Moritz Bunkus
if ($form->{type} =~ /_order$/) {
$form->isblank("transdate", $locale->text('Order Date missing!'));
} else {
$form->isblank("transdate", $locale->text('Quotation Date missing!'));
}

a426a839 Moritz Bunkus
my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
$form->{$idx} =~ s/^\s*//g;
$form->{$idx} =~ s/\s*$//g;

5d59ad23 Sven Schöling
my $msg = ucfirst $form->{vc};
baf226d0 Moritz Bunkus
$form->isblank($form->{vc} . '_id', $locale->text($msg . " missing!"));
54e4131e Moritz Bunkus
# $locale->text('Customer missing!');
# $locale->text('Vendor missing!');

$form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
if ($form->{currency} ne $form->{defaultcurrency});

0a5317e7 Moritz Bunkus
remove_emptied_rows();
54e4131e Moritz Bunkus
&validate_items;
96d3ef85 Sven Schöling
baf226d0 Moritz Bunkus
my $vc = $form->{vc};
if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
$::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
96d3ef85 Sven Schöling
844a541e Moritz Bunkus
if ($vc eq 'customer') {
IS->get_customer(\%myconfig, $form);
$::form->{billing_address_id} = $::form->{default_billing_address_id};

} else {
IR->get_vendor(\%myconfig, $form);
}
baf226d0 Moritz Bunkus
update();
09479f02 Moritz Bunkus
$::dispatcher->end_request;
54e4131e Moritz Bunkus
}

$form->{id} = 0 if $form->{saveasnew};

5d59ad23 Sven Schöling
my ($numberfld, $ordnumber, $err);

54e4131e Moritz Bunkus
# this is for the internal notes section for the [email] Subject
if ($form->{type} =~ /_order$/) {
if ($form->{type} eq 'sales_order') {
$form->{label} = $locale->text('Sales Order');

$numberfld = "sonumber";
$ordnumber = "ordnumber";
} else {
$form->{label} = $locale->text('Purchase Order');

$numberfld = "ponumber";
$ordnumber = "ordnumber";
}

$err = $locale->text('Cannot save order!');

d7fef83a Moritz Bunkus
check_delivered_flag();

54e4131e Moritz Bunkus
} else {
if ($form->{type} eq 'sales_quotation') {
$form->{label} = $locale->text('Quotation');

$numberfld = "sqnumber";
$ordnumber = "quonumber";
} else {
$form->{label} = $locale->text('Request for Quotation');

$numberfld = "rfqnumber";
$ordnumber = "quonumber";
}

$err = $locale->text('Cannot save quotation!');

}

b3501bdf Moritz Bunkus
relink_accounts();
e6f429e4 Moritz Bunkus
OE->save(\%myconfig, \%$form);

2b89ec97 Thomas Kasulke
# saving the history
if(!exists $form->{addition}) {
f9937164 Geoffrey Richardson
if ( $form->{formname} eq 'sales_quotation' or $form->{formname} eq 'request_quotation' ) {
$form->{snumbers} = qq|quonumber_| . $form->{quonumber};
} elsif ( $form->{formname} eq 'sales_order' or $form->{formname} eq 'purchase_order') {
$form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
};
$form->{what_done} = $form->{formname};
b65a230d Sven Schöling
$form->{addition} = "SAVED";
a590a651 Sven Schöling
$form->save_history;
2b89ec97 Thomas Kasulke
}
96d3ef85 Sven Schöling
# /saving the history
54e4131e Moritz Bunkus
$form->{simple_save} = 1;
if(!$form->{print_and_save}) {
0f179c9a Sven Schöling
delete @{$form}{ary_diff([keys %{ $form }], [qw(login id script type cursor_fokus)])};
066dbf0a Moritz Bunkus
edit();
09479f02 Moritz Bunkus
$::dispatcher->end_request;
54e4131e Moritz Bunkus
}
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
54e4131e Moritz Bunkus
}

d319704a Moritz Bunkus
sub delete {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

5d59ad23 Sven Schöling
my ($msg, $err);
d319704a Moritz Bunkus
if ($form->{type} =~ /_order$/) {
$msg = $locale->text('Order deleted!');
$err = $locale->text('Cannot delete order!');
} else {
$msg = $locale->text('Quotation deleted!');
$err = $locale->text('Cannot delete quotation!');
}
8cd05ad6 Moritz Bunkus
if (OE->delete(\%myconfig, \%$form)){
2b89ec97 Thomas Kasulke
# saving the history
if(!exists $form->{addition}) {
b36f6606 Geoffrey Richardson
if ( $form->{formname} eq 'sales_quotation' or $form->{formname} eq 'request_quotation' ) {
$form->{snumbers} = qq|quonumber_| . $form->{quonumber};
} elsif ( $form->{formname} eq 'sales_order' or $form->{formname} eq 'purchase_order') {
$form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
};
$form->{what_done} = $form->{formname};
$form->{addition} = "DELETED";
$form->save_history;
2b89ec97 Thomas Kasulke
}
96d3ef85 Sven Schöling
# /saving the history
7715064f Moritz Bunkus
$form->info($msg);
09479f02 Moritz Bunkus
$::dispatcher->end_request;
2b89ec97 Thomas Kasulke
}
d319704a Moritz Bunkus
$form->error($err);

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub invoice {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();
f775b88a Moritz Bunkus
check_oe_conversion_to_sales_invoice_allowed();
d735aab3 Martin Helmling
$form->mtime_ischanged('oe');

5d59ad23 Sven Schöling
$main::auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
8c7e4493 Moritz Bunkus
0cb4ad8c Philip Reetz
$form->{old_salesman_id} = $form->{salesman_id};
b0e58528 Sven Schöling
$form->get_employee();

0cb4ad8c Philip Reetz
c81ae945 Bernd Bleßmann
if ($form->{type} =~ /_order$|purchase_order_confirmation/) {
081a4f97 Moritz Bunkus
626e0240 Stephan Köhler
# these checks only apply if the items don't bring their own ordnumbers/transdates.
# The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
081a4f97 Moritz Bunkus
$form->isblank("ordnumber", $locale->text('Order Number missing!'))
50305a6d Sven Schöling
if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
081a4f97 Moritz Bunkus
$form->isblank("transdate", $locale->text('Order Date missing!'))
50305a6d Sven Schöling
if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
d319704a Moritz Bunkus
487d4f1f Stephan Köhler
# also copy deliverydate from the order
$form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
a05eead3 Sven Schöling
$form->{orddate} = $form->{transdate};
d319704a Moritz Bunkus
} else {
$form->isblank("quonumber", $locale->text('Quotation Number missing!'));
$form->isblank("transdate", $locale->text('Quotation Date missing!'));
a05eead3 Sven Schöling
$form->{ordnumber} = "";
$form->{quodate} = $form->{transdate};
d319704a Moritz Bunkus
}
96d3ef85 Sven Schöling
baf226d0 Moritz Bunkus
my $vc = $form->{vc};
if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
$::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};

IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
IR->get_vendor(\%myconfig, $form) if $vc eq 'vendor';
96d3ef85 Sven Schöling
baf226d0 Moritz Bunkus
update();
09479f02 Moritz Bunkus
$::dispatcher->end_request;
d319704a Moritz Bunkus
}

72e545d6 Bernd Bleßmann
_oe_remove_delivered_or_billed_rows(id => $form->{id}, type => 'billed') if $form->{new_invoice_type} ne 'final_invoice';
a2820962 Moritz Bunkus
d319704a Moritz Bunkus
$form->{cp_id} *= 1;

5d59ad23 Sven Schöling
for my $i (1 .. $form->{rowcount}) {
e6d64cd0 Jan Büren
for (qw(ship qty sellprice basefactor discount)) {
a05eead3 Sven Schöling
$form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
}
f0ad2143 Jan Büren
$form->{"converted_from_orderitems_id_$i"} = delete $form->{"orderitems_id_$i"};
d316e88e Moritz Bunkus
}

5d59ad23 Sven Schöling
my ($buysell, $orddate, $exchangerate);
d319704a Moritz Bunkus
if ( $form->{type} =~ /_order/
&& $form->{currency} ne $form->{defaultcurrency}) {

# check if we need a new exchangerate
$buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";

$orddate = $form->current_date(\%myconfig);
a05eead3 Sven Schöling
$exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
d319704a Moritz Bunkus
if (!$exchangerate) {
bc5c9dac Peter Schulgin
$exchangerate = 0;
d319704a Moritz Bunkus
}
}

94e11003 Moritz Bunkus
$form->{convert_from_oe_ids} = $form->{id};
$form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
$form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
$form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
d319704a Moritz Bunkus
94e11003 Moritz Bunkus
delete @{$form}{qw(id closed)};
d319704a Moritz Bunkus
$form->{rowcount}--;
fb37acdc Moritz Bunkus
5d59ad23 Sven Schöling
my ($script);
d319704a Moritz Bunkus
if ( $form->{type} eq 'purchase_order'
c81ae945 Bernd Bleßmann
|| $form->{type} eq 'purchase_order_confirmation'
d319704a Moritz Bunkus
|| $form->{type} eq 'request_quotation') {
$form->{title} = $locale->text('Add Vendor Invoice');
$form->{script} = 'ir.pl';
$script = "ir";
$buysell = 'sell';
1927b931 Bernd Bleßmann
$form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST())->token;
d319704a Moritz Bunkus
}
a05eead3 Sven Schöling
96d3ef85 Sven Schöling
if ( $form->{type} eq 'sales_order'
a05eead3 Sven Schöling
|| $form->{type} eq 'sales_quotation') {
72e545d6 Bernd Bleßmann
$form->{title} = ($form->{new_invoice_type} eq 'invoice_for_advance_payment') ? $locale->text('Add Invoice for Advance Payment')
: ($form->{new_invoice_type} eq 'final_invoice') ? $locale->text('Add Final Invoice')
19132371 Bernd Bleßmann
: $locale->text('Add Sales Invoice');
d319704a Moritz Bunkus
$form->{script} = 'is.pl';
$script = "is";
$buysell = 'buy';
1927b931 Bernd Bleßmann
$form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_SALES_INVOICE_POST())->token;
d319704a Moritz Bunkus
}

# bo creates the id, reset it
a05eead3 Sven Schöling
map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
d319704a Moritz Bunkus
$form->{ $form->{vc} } =~ s/--.*//g;
19132371 Bernd Bleßmann
$form->{type} = $form->{new_invoice_type} || "invoice";
d319704a Moritz Bunkus
# locale messages
c607fb40 Sven Schöling
$main::locale = Locale->new("$myconfig{countrycode}", "$script");
fd954b14 Sven Schöling
$locale = $main::locale;
d319704a Moritz Bunkus
d629acd8 Sven Schöling
require "bin/mozilla/$form->{script}";
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
my $currency = $form->{currency};
d319704a Moritz Bunkus
&invoice_links;

$form->{currency} = $currency;
a53233e5 Sven Schöling
$form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
$form->{exchangerate} = $form->{forex} || '';
d319704a Moritz Bunkus
$form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});

&prepare_invoice;

# format amounts
5d59ad23 Sven Schöling
for my $i (1 .. $form->{rowcount}) {
d319704a Moritz Bunkus
$form->{"discount_$i"} =
$form->format_amount(\%myconfig, $form->{"discount_$i"});

5d59ad23 Sven Schöling
my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
d319704a Moritz Bunkus
$dec = length $dec;
5d59ad23 Sven Schöling
my $decimalplaces = ($dec > 2) ? $dec : 2;
d319704a Moritz Bunkus
7ff673f9 Stephan Köhler
# copy delivery date from reqdate for order -> invoice conversion
081a4f97 Moritz Bunkus
$form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
unless $form->{"deliverydate_$i"};
7ff673f9 Stephan Köhler
d319704a Moritz Bunkus
$form->{"sellprice_$i"} =
$form->format_amount(\%myconfig, $form->{"sellprice_$i"},
$decimalplaces);
081a4f97 Moritz Bunkus
e44cbe16 Stephan Köhler
(my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
081a4f97 Moritz Bunkus
$dec_qty = length $dec_qty;
$form->{"qty_$i"} =
$form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
d319704a Moritz Bunkus
}

&display_form;

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub save_as_new {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

d319704a Moritz Bunkus
$form->{saveasnew} = 1;
b03dd3d7 Sven Schöling
map { delete $form->{$_} } qw(printed emailed queued delivered closed);
b8125c17 Bernd Bleßmann
$form->{"converted_from_orderitems_id_$_"} = delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
06bbc8e7 Moritz Bunkus
008c2e15 Moritz Bunkus
# Let kivitendo assign a new order number if the user hasn't changed the
06bbc8e7 Moritz Bunkus
# previous one. If it has been changed manually then use it as-is.
my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
a426a839 Moritz Bunkus
$form->{$idx} =~ s/^\s*//g;
$form->{$idx} =~ s/\s*$//g;
06bbc8e7 Moritz Bunkus
if ($form->{saved_xyznumber} &&
($form->{saved_xyznumber} eq $form->{$idx})) {
delete($form->{$idx});
}
d319704a Moritz Bunkus
133c31d7 Thomas Heck
# clear reqdate and transdate unless changed
if ( $form->{reqdate} && $form->{id} ) {
b03dd3d7 Sven Schöling
my $saved_order = OE->retrieve_simple(id => $form->{id});
133c31d7 Thomas Heck
if ( $saved_order && $saved_order->{reqdate} eq $form->{reqdate} && $saved_order->{transdate} eq $form->{transdate} ) {
2d44a547 Jan Büren
my $extra_days = $form->{type} eq 'sales_quotation' ? $::instance_conf->get_reqdate_interval :
$form->{type} eq 'sales_order' ? $::instance_conf->get_delivery_date_interval : 1;
bee7d33f Jan Büren
if ( ($form->{type} eq 'sales_order' && !$::instance_conf->get_deliverydate_on)
|| ($form->{type} eq 'sales_quotation' && !$::instance_conf->get_reqdate_on)) {
$form->{reqdate} = '';
} else {
3f24ab6b Bernd Bleßmann
$form->{reqdate} = DateTime->today_local->next_workday(extra_days => $extra_days)->to_kivitendo;
bee7d33f Jan Büren
}
ff494355 Moritz Bunkus
$form->{transdate} = DateTime->today_local->to_kivitendo;
b03dd3d7 Sven Schöling
}
}
# update employee
$form->get_employee();

d319704a Moritz Bunkus
&save;

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

480c6709 Moritz Bunkus
sub check_for_direct_delivery_yes {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
480c6709 Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

480c6709 Moritz Bunkus
$form->{direct_delivery_checked} = 1;
delete @{$form}{grep /^shipto/, keys %{ $form }};
map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
6637a9e9 Martin Helmling
$form->{CFDD_shipto} = 1;
480c6709 Moritz Bunkus
purchase_order();
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
480c6709 Moritz Bunkus
}

sub check_for_direct_delivery_no {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
480c6709 Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

480c6709 Moritz Bunkus
$form->{direct_delivery_checked} = 1;
delete @{$form}{grep /^shipto/, keys %{ $form }};
6637a9e9 Martin Helmling
$form->{CFDD_shipto} = 0;
480c6709 Moritz Bunkus
purchase_order();

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
480c6709 Moritz Bunkus
}

sub check_for_direct_delivery {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
480c6709 Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();

480c6709 Moritz Bunkus
if ($form->{direct_delivery_checked}
|| (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
480c6709 Moritz Bunkus
return;
}

4493d1eb Moritz Bunkus
my $cvars = SL::DB::Shipto->new->cvars_by_config;

480c6709 Moritz Bunkus
if ($form->{shipto_id}) {
Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");

} else {
map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
}

4493d1eb Moritz Bunkus
$_->value($::form->{"CFDD_shiptocvar_" . $_->config->name}) for @{ $cvars };

480c6709 Moritz Bunkus
delete $form->{action};
8c7e4493 Moritz Bunkus
$form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
480c6709 Moritz Bunkus
$form->header();
4493d1eb Moritz Bunkus
print $form->parse_html_template("oe/check_for_direct_delivery", { cvars => $cvars });
480c6709 Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
480c6709 Moritz Bunkus
09479f02 Moritz Bunkus
$::dispatcher->end_request;
480c6709 Moritz Bunkus
}

d319704a Moritz Bunkus
sub purchase_order {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my $locale = $main::locale;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();
d735aab3 Martin Helmling
$form->mtime_ischanged('oe');

5d59ad23 Sven Schöling
$main::auth->assert('purchase_order_edit');
8c7e4493 Moritz Bunkus
c4bb14d6 Geoffrey Richardson
$form->{sales_order_to_purchase_order} = 0;
480c6709 Moritz Bunkus
if ($form->{type} eq 'sales_order') {
c4bb14d6 Geoffrey Richardson
$form->{sales_order_to_purchase_order} = 1;
480c6709 Moritz Bunkus
check_for_direct_delivery();
}

6e50e0c2 Moritz Bunkus
if ($form->{type} =~ /^sales_/) {
delete($form->{ordnumber});
68646341 Jan Büren
delete($form->{payment_id});
delete($form->{delivery_term_id});
6e50e0c2 Moritz Bunkus
}

b59ec969 Stephan Köhler
$form->{cp_id} *= 1;

ba7276fa Moritz Bunkus
my $source_type = $form->{type};
d319704a Moritz Bunkus
$form->{title} = $locale->text('Add Purchase Order');
$form->{vc} = "vendor";
$form->{type} = "purchase_order";

b0e58528 Sven Schöling
$form->get_employee();

ba7276fa Moritz Bunkus
poso(source_type => $form->{type});
d319704a Moritz Bunkus
c4bb14d6 Geoffrey Richardson
delete $form->{sales_order_to_purchase_order};

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub sales_order {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my $locale = $main::locale;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();
5d59ad23 Sven Schöling
$main::auth->assert('sales_order_edit');
d2af074a Jan Büren
$form->mtime_ischanged('oe');
8c7e4493 Moritz Bunkus
6e50e0c2 Moritz Bunkus
if ($form->{type} eq "purchase_order") {
delete($form->{ordnumber});
754482db Moritz Bunkus
$form->{"lastcost_$_"} = $form->{"sellprice_$_"} for (1..$form->{rowcount});
6e50e0c2 Moritz Bunkus
}

94e11003 Moritz Bunkus
$form->{cp_id} *= 1;
d319704a Moritz Bunkus
ba7276fa Moritz Bunkus
my $source_type = $form->{type};
94e11003 Moritz Bunkus
$form->{title} = $locale->text('Add Sales Order');
$form->{vc} = "customer";
$form->{type} = "sales_order";
d319704a Moritz Bunkus
b0e58528 Sven Schöling
$form->get_employee();

ba7276fa Moritz Bunkus
poso(source_type => $source_type);
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

sub poso {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

ba7276fa Moritz Bunkus
my %param = @_;
5d59ad23 Sven Schöling
my $form = $main::form;
my %myconfig = %main::myconfig;
d319704a Moritz Bunkus
8c7e4493 Moritz Bunkus
check_oe_access();
5d59ad23 Sven Schöling
$main::auth->assert('purchase_order_edit | sales_order_edit');
8c7e4493 Moritz Bunkus
d319704a Moritz Bunkus
$form->{transdate} = $form->current_date(\%myconfig);
delete $form->{duedate};

ba7276fa Moritz Bunkus
# "reqdate" is the validity date for a quotation and the delivery
# date for an order. Therefore it makes no sense to keep the value
# when converting from one into the other.
delete $form->{reqdate} if ($param{source_type} =~ /_quotation$/) == ($form->{type} =~ /_quotation$/);

94e11003 Moritz Bunkus
$form->{convert_from_oe_ids} = $form->{id};
$form->{closed} = 0;
d319704a Moritz Bunkus
94e11003 Moritz Bunkus
$form->{old_employee_id} = $form->{employee_id};
$form->{old_salesman_id} = $form->{salesman_id};
0cb4ad8c Philip Reetz
d319704a Moritz Bunkus
# reset
4341dfaf Jan Büren
map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered ordnumber
taxzone_id currency);
991d16bb Jan Büren
# this converted variable is also used for sales_order to purchase order and vice versa
2f9a5220 Jan Büren
$form->{"converted_from_orderitems_id_$_"} = delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
c4bb14d6 Geoffrey Richardson
da804bf2 Geoffrey Richardson
# if purchase_order was generated from sales_order, use lastcost_$i as sellprice_$i
2521c1bb Sven Schöling
# also reset discounts
c4bb14d6 Geoffrey Richardson
if ( $form->{sales_order_to_purchase_order} ) {
for my $i (1 .. $form->{rowcount}) {
55909313 Bernd Bleßmann
$form->{"sellprice_${i}"} = $form->{"lastcost_${i}"};
2521c1bb Sven Schöling
$form->{"discount_${i}"} = 0;
c4bb14d6 Geoffrey Richardson
};
};
d319704a Moritz Bunkus
5d59ad23 Sven Schöling
for my $i (1 .. $form->{rowcount}) {
2f6636f6 Bernd Bleßmann
map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice basefactor discount lastcost);
19be3a57 Moritz Bunkus
}

27e91abd Moritz Bunkus
my %saved_vars = map { $_ => $form->{$_} } grep { $form->{$_} } qw(currency);

d319704a Moritz Bunkus
&order_links;

27e91abd Moritz Bunkus
map { $form->{$_} = $saved_vars{$_} } keys %saved_vars;

51e25e57 Geoffrey Richardson
# prepare_order assumes that the discount is in db-notation (0.05) and not user-notation (5)
20e6c669 Jan Büren
# and therefore multiplies the values by 100 in the case of reading from db or making an order
# from several quotation, so we convert this back into percent-notation for the user interface by multiplying with 0.01
# ergänzung 03.10.2010 muss vor prepare_order passieren (s.a. Svens Kommentar zu Bug 1017)
# das parse_amount wird oben schon ausgeführt, deswegen an dieser stelle raus (wichtig: kommawerte bei discount testen)
5d59ad23 Sven Schöling
for my $i (1 .. $form->{rowcount}) {
20e6c669 Jan Büren
$form->{"discount_$i"} /=100;
51e25e57 Geoffrey Richardson
};

20e6c669 Jan Büren
&prepare_order;
d319704a Moritz Bunkus
&update;

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d319704a Moritz Bunkus
}

d707f7ac Moritz Bunkus
sub delivery_order {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
d707f7ac Moritz Bunkus
d735aab3 Martin Helmling
$form->mtime_ischanged('oe');

d707f7ac Moritz Bunkus
if ($form->{type} =~ /^sales/) {
5d59ad23 Sven Schöling
$main::auth->assert('sales_delivery_order_edit');
d707f7ac Moritz Bunkus
$form->{vc} = 'customer';
$form->{type} = 'sales_delivery_order';

} else {
5d59ad23 Sven Schöling
$main::auth->assert('purchase_delivery_order_edit');
d707f7ac Moritz Bunkus
$form->{vc} = 'vendor';
$form->{type} = 'purchase_delivery_order';
}

a03e4841 Bernd Bleßmann
$form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_DELIVERY_ORDER_SAVE())->token;

b0e58528 Sven Schöling
$form->get_employee();

d707f7ac Moritz Bunkus
require "bin/mozilla/do.pl";

9f3bf071 Moritz Bunkus
$form->{script} = 'do.pl';
00738f6f Moritz Bunkus
$form->{cp_id} *= 1;
$form->{convert_from_oe_ids} = $form->{id};
$form->{transdate} = $form->current_date(\%myconfig);
d707f7ac Moritz Bunkus
delete $form->{duedate};

$form->{old_employee_id} = $form->{employee_id};
$form->{old_salesman_id} = $form->{salesman_id};

a2820962 Moritz Bunkus
_oe_remove_delivered_or_billed_rows(id => $form->{id}, type => 'delivered');

d707f7ac Moritz Bunkus
# reset
00738f6f Moritz Bunkus
delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
d707f7ac Moritz Bunkus
5d59ad23 Sven Schöling
for my $i (1 .. $form->{rowcount}) {
2f6636f6 Bernd Bleßmann
map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice lastcost basefactor discount);
f0ad2143 Jan Büren
$form->{"converted_from_orderitems_id_$i"} = delete $form->{"orderitems_id_$i"};
d707f7ac Moritz Bunkus
}

eeff7895 Sven Schöling
my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor shipto_id);
d707f7ac Moritz Bunkus
order_links();

prepare_order();

map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;

34f5531f Bernd Bleßmann
for my $i (1 .. $form->{rowcount}) {
(my $dummy, $form->{"pricegroup_id_$i"}) = split /--/, $form->{"sellprice_pg_$i"};
}
d707f7ac Moritz Bunkus
update();

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
d707f7ac Moritz Bunkus
}

2f105e47 Moritz Bunkus
sub oe_prepare_xyz_from_order {
048a4ee5 Bernd Bleßmann
return if !$::form->{id};

my $order = SL::DB::Order->new(id => $::form->{id})->load;
3b31aeed Moritz Bunkus
if (exists $::form->{only_items}) {
my @wanted_indexes = sort { $a <=> $b } map { $_ - 1 } split(",", $::form->{only_items} // "");
67f4335c Jan Büren
my @items = @{ $order->items_sorted };
3b31aeed Moritz Bunkus
@items = @items[@wanted_indexes];
$order->items(\@items);
}

048a4ee5 Bernd Bleßmann
$order->flatten_to_form($::form, format_amounts => 1);
41cba9bf Werner Hahn
$::form->{taxincluded_changed_by_user} = 1;
048a4ee5 Bernd Bleßmann
e070b638 Bernd Bleßmann
# hack: add partsgroup for first row if it does not exists,
# because _remove_billed_or_delivered_rows and _remove_full_delivered_rows
# determine fields to handled by existing fields for the first row. If partsgroup
# is missing there, for deleted rows the partsgroup_field is not emptied and in
# update_delivery_order it will not considered an empty row ...
$::form->{partsgroup_1} = '' if !exists $::form->{partsgroup_1};

048a4ee5 Bernd Bleßmann
# fake last empty row
$::form->{rowcount}++;

e070b638 Bernd Bleßmann
_update_ship();
2f105e47 Moritz Bunkus
}

sub oe_delivery_order_from_order {
oe_prepare_xyz_from_order();
048a4ee5 Bernd Bleßmann
delivery_order();
}

07dd84c0 Bernd Bleßmann
sub oe_invoice_from_order {
2f105e47 Moritz Bunkus
oe_prepare_xyz_from_order();
07dd84c0 Bernd Bleßmann
invoice();
}

480c6709 Moritz Bunkus
sub yes {
5d59ad23 Sven Schöling
call_sub($main::form->{yes_nextsub});
480c6709 Moritz Bunkus
}

sub no {
5d59ad23 Sven Schöling
call_sub($main::form->{no_nextsub});
480c6709 Moritz Bunkus
}
b6dc5623 Sven Schöling
######################################################################################################
# IO ENTKOPPLUNG
# ###############################################################################################
sub display_form {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
my %myconfig = %main::myconfig;
b6dc5623 Sven Schöling
8c7e4493 Moritz Bunkus
check_oe_access();

d707f7ac Moritz Bunkus
retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);

b6dc5623 Sven Schöling
$form->{"taxaccounts"} =~ s/\s*$//;
$form->{"taxaccounts"} =~ s/^\s*//;
foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
}
$form->{"taxaccounts"} = "";

a87dd0ec Sven Schöling
IC->retrieve_accounts(\%myconfig, $form, map { $_ => $form->{"id_$_"} } 1 .. $form->{rowcount});
b6dc5623 Sven Schöling
$form->{rowcount}++;
$form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};

$form->language_payment(\%myconfig);

be6f6cfd Moritz Bunkus
Common::webdav_folder($form);
b6dc5623 Sven Schöling
&form_header;

# create rows
display_row($form->{rowcount}) if $form->{rowcount};

&form_footer;

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
b6dc5623 Sven Schöling
}

7a7f33b5 Moritz Bunkus
sub report_for_todo_list {
5d59ad23 Sven Schöling
$main::lxdebug->enter_sub();

my $form = $main::form;
7a7f33b5 Moritz Bunkus
a46b8625 Bernd Bleßmann
my $is_for_sales = $::auth->assert($oe_view_access_map->{'sales_quotation'}, 'may fail');
my $is_for_purchase = $::auth->assert($oe_view_access_map->{'request_quotation'}, 'may fail');
my $quotations = OE->transactions_for_todo_list(sales => $is_for_sales, purchase => $is_for_purchase);
7a7f33b5 Moritz Bunkus
my $content;

if (@{ $quotations }) {
233800e3 Bernd Bleßmann
my $callback = build_std_url('action');
984f6322 Bernd Bleßmann
my $edit_url = ($::instance_conf->get_feature_experimental_order)
233800e3 Bernd Bleßmann
? build_std_url('script=controller.pl', 'action=Order/edit', 'callback=' . E($callback))
: build_std_url('script=oe.pl', 'action=edit', 'callback=' . E($callback));
7a7f33b5 Moritz Bunkus
$content = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
233800e3 Bernd Bleßmann
'edit_url' => $edit_url,
'callback' => $callback });
7a7f33b5 Moritz Bunkus
}

5d59ad23 Sven Schöling
$main::lxdebug->leave_sub();
7a7f33b5 Moritz Bunkus
return $content;
}

92331b8e Moritz Bunkus
sub edit_periodic_invoices_config {
$::lxdebug->enter_sub();

$::form->{type} = 'sales_order';

check_oe_access();

my $config;
65b2387a Moritz Bunkus
$config = SL::YAML::Load($::form->{periodic_invoices_config}) if $::form->{periodic_invoices_config};
92331b8e Moritz Bunkus
if ('HASH' ne ref $config) {
82ff5451 Moritz Bunkus
$config = { periodicity => 'm',
order_value_periodicity => 'p', # = same as periodicity
f48af817 Moritz Bunkus
start_date_as_date => $::form->{transdate} || $::form->current_date,
f49ad7f1 Moritz Bunkus
extend_automatically_by => 12,
active => 1,
};
92331b8e Moritz Bunkus
}
f042650d Jan Büren
# for older configs, replace email preset text if not yet set.
ca5bdfd6 Moritz Bunkus
$config->{email_subject} ||= GenericTranslations->get(language_id => $::form->{lanuage_id}, translation_type => "preset_text_periodic_invoices_email_subject");
$config->{email_body} ||= GenericTranslations->get(language_id => $::form->{lanuage_id}, translation_type => "salutation_general")
. GenericTranslations->get(language_id => $::form->{lanuage_id}, translation_type => "salutation_punctuation_mark")
. "\n\n"
. GenericTranslations->get(language_id => $::form->{lanuage_id}, translation_type => "preset_text_periodic_invoices_email_body");
$config->{email_body} =~ s{\A[ \n\r]+|[ \n\r]+\Z}{}g;
92331b8e Moritz Bunkus
82ff5451 Moritz Bunkus
$config->{periodicity} = 'm' if none { $_ eq $config->{periodicity} } @SL::DB::PeriodicInvoicesConfig::PERIODICITIES;
$config->{order_value_periodicity} = 'p' if none { $_ eq $config->{order_value_periodicity} } ('p', @SL::DB::PeriodicInvoicesConfig::ORDER_VALUE_PERIODICITIES);
92331b8e Moritz Bunkus
$::form->get_lists(printers => "ALL_PRINTERS",
charts => { key => 'ALL_CHARTS',
transdate => 'current_date' });

$::form->{AR} = [ grep { $_->{link} =~ m/(?:^|:)AR(?::|$)/ } @{ $::form->{ALL_CHARTS} } ];
$::form->{title} = $::locale->text('Edit the configuration for periodic invoices');

9c5f94c0 Moritz Bunkus
if ($::form->{customer_id}) {
$::form->{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(where => [ cp_cv_id => $::form->{customer_id} ]);
cea7e7c5 Jan Büren
$::form->{email_recipient_invoice_address} = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id})->invoice_mail;
9c5f94c0 Moritz Bunkus
}

ddb162b6 Sven Schöling
$::form->header(no_layout => 1);
13412f21 Bernd Bleßmann
print $::form->parse_html_template('oe/edit_periodic_invoices_config', {config => $config});
92331b8e Moritz Bunkus
$::lxdebug->leave_sub();
}

sub save_periodic_invoices_config {
$::lxdebug->enter_sub();

$::form->{type} = 'sales_order';

check_oe_access();

f49ad7f1 Moritz Bunkus
$::form->isblank('start_date_as_date', $::locale->text('The start date is missing.'));

my $config = { active => $::form->{active} ? 1 : 0,
terminated => $::form->{terminated} ? 1 : 0,
3649d1c4 Moritz Bunkus
direct_debit => $::form->{direct_debit} ? 1 : 0,
82ff5451 Moritz Bunkus
periodicity => (any { $_ eq $::form->{periodicity} } @SL::DB::PeriodicInvoicesConfig::PERIODICITIES) ? $::form->{periodicity} : 'm',
order_value_periodicity => (any { $_ eq $::form->{order_value_periodicity} } ('p', @SL::DB::PeriodicInvoicesConfig::ORDER_VALUE_PERIODICITIES)) ? $::form->{order_value_periodicity} : 'p',
f49ad7f1 Moritz Bunkus
start_date_as_date => $::form->{start_date_as_date},
end_date_as_date => $::form->{end_date_as_date},
f48af817 Moritz Bunkus
first_billing_date_as_date => $::form->{first_billing_date_as_date},
f49ad7f1 Moritz Bunkus
print => $::form->{print} ? 1 : 0,
printer_id => $::form->{print} ? $::form->{printer_id} * 1 : undef,
copies => $::form->{copies} * 1 ? $::form->{copies} : 1,
extend_automatically_by => $::form->{extend_automatically_by} * 1 || undef,
ar_chart_id => $::form->{ar_chart_id} * 1,
9c5f94c0 Moritz Bunkus
send_email => $::form->{send_email} ? 1 : 0,
email_recipient_contact_id => $::form->{email_recipient_contact_id} * 1 || undef,
email_recipient_address => $::form->{email_recipient_address},
email_sender => $::form->{email_sender},
email_subject => $::form->{email_subject},
email_body => $::form->{email_body},
92331b8e Moritz Bunkus
};

65b2387a Moritz Bunkus
$::form->{periodic_invoices_config} = SL::YAML::Dump($config);
92331b8e Moritz Bunkus
$::form->{title} = $::locale->text('Edit the configuration for periodic invoices');
$::form->header;
print $::form->parse_html_template('oe/save_periodic_invoices_config', $config);

$::lxdebug->leave_sub();
}

d814a525 Martin Helmling
sub _remove_full_delivered_rows {

my @fields = map { s/_1$//; $_ } grep { m/_1$/ } keys %{ $::form };
my @new_rows;

my $removed_rows = 0;
my $row = 0;
while ($row < $::form->{rowcount}) {
$row++;
next unless $::form->{"id_$row"};
my $base_factor = SL::DB::Manager::Unit->find_by(name => $::form->{"unit_$row"})->base_factor;
my $base_qty = $::form->parse_amount(\%::myconfig, $::form->{"qty_$row"}) * $base_factor;
c4a8cd8c Moritz Bunkus
my $ship_qty = $::form->{"ship_$row"} * $base_factor;
d814a525 Martin Helmling
#$main::lxdebug->message(LXDebug->DEBUG2(),"shipto=".$ship_qty." qty=".$base_qty);

if (!$ship_qty || ($ship_qty < $base_qty)) {
$::form->{"qty_$row"} = $::form->format_amount(\%::myconfig, ($base_qty - $ship_qty) / $base_factor );
$::form->{"ship_$row"} = 0;
push @new_rows, { map { $_ => $::form->{"${_}_${row}"} } @fields };

} else {
$removed_rows++;
}
}
$::form->redo_rows(\@fields, \@new_rows, scalar(@new_rows), $::form->{rowcount});
$::form->{rowcount} -= $removed_rows;
}

a2820962 Moritz Bunkus
sub _oe_remove_delivered_or_billed_rows {
my (%params) = @_;

return if !$params{id} || !$params{type};

my $ord_quot = SL::DB::Order->new(id => $params{id})->load;
return if !$ord_quot;

d814a525 Martin Helmling
# Prüfung ob itemlinks existieren, falls ja dann neue Implementierung

if ( $params{type} eq 'delivered' ) {
my $orderitem = SL::DB::Manager::OrderItem->get_first( where => [trans_id => $ord_quot->id]);
if ( $orderitem) {
my @links = $orderitem->linked_records(to => 'SL::DB::DeliveryOrderItem');
if ( scalar(@links ) > 0 ) {
#$main::lxdebug->message(LXDebug->DEBUG2(),"item recordlinks vorhanden");
return _remove_full_delivered_rows();
}
}
}
a2820962 Moritz Bunkus
my %args = (
direction => 'to',
to => $params{type} eq 'delivered' ? 'DeliveryOrder' : 'Invoice',
via => [ $params{type} eq 'delivered' ? qw(Order) : qw(Order DeliveryOrder) ],
);

my %handled_base_qtys;
foreach my $record (@{ $ord_quot->linked_records(%args) }) {
next if $ord_quot->is_sales != $record->is_sales;
6f22c9ac Bernd Bleßmann
next if $record->type eq 'invoice' && $record->storno;
a2820962 Moritz Bunkus
foreach my $item (@{ $record->items }) {
1beca8de Moritz Bunkus
my $key = $item->parts_id;
$key .= ':' . $item->serialnumber if $item->serialnumber;
$handled_base_qtys{$key} += $item->qty * $item->unit_obj->base_factor;
a2820962 Moritz Bunkus
}
}

_remove_billed_or_delivered_rows(quantities => \%handled_base_qtys);
}

817f7300 Moritz Bunkus
sub dispatcher {
a6ce2c7f Moritz Bunkus
foreach my $action (qw(delete delivery_order invoice print purchase_order quotation
b067e974 Jan Büren
request_for_quotation sales_order save save_and_close save_as_new ship_to update)) {
c0f1c3ee Sven Schöling
if ($::form->{"action_${action}"}) {
817f7300 Moritz Bunkus
call_sub($action);
return;
}
}

c0f1c3ee Sven Schöling
$::form->error($::locale->text('No action defined.'));
817f7300 Moritz Bunkus
}