use Carp;
use CGI;
use List::MoreUtils qw(any uniq apply);
-use List::Util qw(min max first);
+use List::Util qw(sum min max first);
use List::UtilsBy qw(sort_by uniq_by);
use SL::ClientJS;
use SL::Presenter::Part;
use SL::DB::Contact;
+use SL::DB::Currency;
use SL::DB::Customer;
use SL::DB::Default;
use SL::DB::Language;
_order();
if ($::instance_conf->get_feature_experimental_order) {
+
+ # At this point, the record is saved and the exchangerate contains
+ # an unformatted value. _make_record uses RDBO attributes (i.e. _as_number)
+ # to assign values and thus expects an formatted value.
+ $::form->{exchangerate} = $::form->format_amount(\%::myconfig, $::form->{exchangerate});
+
my $order = _make_record();
- $order->globalproject_id(undef) if !$order->globalproject_id;
- $order->payment_id(undef) if !$order->payment_id;
+
+ $order->currency(SL::DB::Currency->new(name => $::form->{currency})->load) if $::form->{currency};
+ $order->globalproject_id(undef) if !$order->globalproject_id;
+ $order->payment_id(undef) if !$order->payment_id;
+
my $row = 1;
foreach my $item (@{$order->items_sorted}) {
$item->custom_variables([]);
$item->price_factor_id(undef) if !$item->price_factor_id;
$item->project_id(undef) if !$item->project_id;
- $item->discount($item->discount/100.0);
# autovivify all cvars that are not in the form (cvars_by_config can do it).
# workaround to pre-parse number-cvars (parse_custom_variable_values does not parse number values).
require SL::Controller::Order;
my $c = SL::Controller::Order->new(order => $order);
+ $c->setup_custom_shipto_from_form($order, $::form);
$c->action_edit();
$main::lxdebug->leave_sub();
$form->{"taxaccounts"} =~ s/\s*$//;
$form->{"taxaccounts"} =~ s/^\s*//;
foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
- map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
+ map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber tax_id)); # add tax_id ?
}
$form->{"taxaccounts"} = "";
# TODO: both of these are makeshift so that price sources can operate on rdbo objects. if
# this ever gets rewritten in controller style, throw this out
sub _make_record_item {
- my ($row) = @_;
+ my ($row, %params) = @_;
my $class = {
sales_order => 'OrderItem',
$obj->part(SL::DB::Part->load_cached($::form->{"id_$row"}));
}
+ if ($obj->can('qty')) {
+ $obj->qty( $obj->qty * $params{factor});
+ $obj->base_qty($obj->base_qty * $params{factor});
+ }
+
return $obj;
}
: do { die 'unknown invoice type' };
}
+ my $factor = $::form->{type} =~ m{credit_note} ? -1 : 1;
+
return unless $class;
$class = 'SL::DB::' . $class;
my @items;
for my $i (1 .. $::form->{rowcount}) {
next unless $::form->{"id_$i"};
- push @items, _make_record_item($i);
+ push @items, _make_record_item($i, factor => $factor);
}
$obj->items(@items) if @items;
$obj->is_sales(!!$obj->customer_id) if $class eq 'SL::DB::DeliveryOrder';
+ if ($class eq 'SL::DB::Invoice') {
+ my $paid = $factor *
+ sum
+ map { $::form->parse_amount(\%::myconfig, $::form->{$_}) }
+ grep { m{^paid_\d+$} }
+ keys %{ $::form };
+ $obj->paid($paid);
+ }
+
return $obj;
}
sub setup_sales_purchase_print_options {
my $print_form = Form->new('');
$print_form->{printers} = SL::DB::Manager::Printer->get_all_sorted;
- $print_form->{languages} = SL::DB::Manager::Language->get_all_sorted;
- $print_form->{$_} = $::form->{$_} for qw(type media language_id printer_id storno);
+ $print_form->{$_} = $::form->{$_} for qw(type media printer_id storno formname groupitems);
return SL::Helper::PrintOptions->get_print_options(
form => $print_form,
$::form->{language} = $::form->get_template_language(\%::myconfig);
$::form->{language} = "_" . $::form->{language};
+ my %body_params = (record_email => $record_email);
+ if (($::form->{type} eq 'invoice') && $::form->{direct_debit}) {
+ $body_params{translation_type} = "preset_text_invoice_direct_debit";
+ $body_params{fallback_translation_type} = "preset_text_invoice";
+ }
+
my $email_form = {
to => $email,
cc => $email_cc,
subject => $::form->generate_email_subject,
- message => $::form->generate_email_body('record_email' => $record_email),
+ message => $::form->generate_email_body(%body_params),
attachment_filename => $::form->generate_attachment_filename,
js_send_function => 'kivi.SalesPurchase.send_email()',
};
sub _maybe_attach_zugferd_data {
my ($form) = @_;
- return if !$::instance_conf->get_create_zugferd_invoices;
-
my $record = _make_record();
- return if !$record || !$record->can('create_pdf_a_print_options') || !$record->can('create_zugferd_data');
+ return if !$record
+ || !$record->can('customer')
+ || !$record->customer
+ || !$record->can('create_pdf_a_print_options')
+ || !$record->can('create_zugferd_data')
+ || !$record->customer->create_zugferd_invoices_for_this_customer;
eval {
my $xmlfile = File::Temp->new;
$form->{TEMPLATE_DRIVER_OPTIONS}->{pdf_a} = $record->create_pdf_a_print_options(zugferd_xmp_data => $record->create_zugferd_xmp_data);
$form->{TEMPLATE_DRIVER_OPTIONS}->{pdf_attachments} = [
{ source => $xmlfile,
- name => 'ZUGFeRD-invoice.xml',
- description => $::locale->text('ZUGFeRD invoice'),
+ name => 'factur-x.xml',
+ description => $::locale->text('Factur-X/ZUGFeRD invoice'),
relationship => '/Alternative',
mime_type => 'text/xml',
}