#
#======================================================================
+use YAML;
+
use SL::Drafts;
require "bin/mozilla/common.pl";
+use strict;
+
sub save_draft {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
if (!$form->{draft_id} && !$form->{draft_description}) {
restore_form($form->{SAVED_FORM}, 1) if ($form->{SAVED_FORM});
- delete($form->{SAVED_FORM});
- $form->{SAVED_FORM} = save_form();
+ delete $form->{SAVED_FORM};
+
+ $form->{SAVED_FORM} = save_form(qw(login password));
+ $form->{remove_draft} = 1;
$form->header();
print($form->parse_html_template("drafts/save_new"));
- return $lxdebug->leave_sub();
+
+ return $main::lxdebug->leave_sub();
}
- my ($draft_id, $draft_description) =
- ($form->{draft_id}, $form->{draft_description});
+ my ($draft_id, $draft_description) = ($form->{draft_id}, $form->{draft_description});
restore_form($form->{SAVED_FORM}, 1);
- delete($form->{SAVED_FORM});
+ delete $form->{SAVED_FORM};
Drafts->save(\%myconfig, $form, $draft_id, $draft_description);
update();
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
sub remove_draft {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
Drafts->remove(\%myconfig, $form, $form->{draft_id}) if ($form->{draft_id});
- delete($form->{draft_id});
- delete($form->{draft_description});
+ delete @{$form}{qw(draft_id draft_description)};
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
sub load_draft_maybe {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
- $lxdebug->leave_sub() and return 0 if ($form->{DONT_LOAD_DRAFT});
+ $main::lxdebug->leave_sub() and return 0 if ($form->{DONT_LOAD_DRAFT});
my ($draft_nextsub) = @_;
my @drafts = Drafts->list(\%myconfig, $form);
- $lxdebug->leave_sub() and return 0 unless (@drafts);
+ $main::lxdebug->leave_sub() and return 0 unless (@drafts);
$draft_nextsub = "add" unless ($draft_nextsub);
- delete($form->{action});
- my $saved_form = save_form();
+ delete $form->{action};
+ my $saved_form = save_form(qw(login password));
$form->header();
print($form->parse_html_template("drafts/load",
- { "DRAFTS" => \@drafts,
- "SAVED_FORM" => $saved_form,
+ { "DRAFTS" => \@drafts,
+ "SAVED_FORM" => $saved_form,
"draft_nextsub" => $draft_nextsub }));
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
return 1;
}
sub dont_load_draft {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+
+ my $draft_nextsub = $form->{draft_nextsub} || "add";
- my $draft_nextsub = $form->{draft_nextsub};
- $draft_nextsub = "add" unless ($form->{draft_nextsub});
restore_form($form->{SAVED_FORM}, 1);
- delete($form->{SAVED_FORM});
- delete($form->{action});
+ delete $form->{SAVED_FORM};
+
$form->{DONT_LOAD_DRAFT} = 1;
- &{ $draft_nextsub }();
+ call_sub($draft_nextsub);
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
sub load_draft {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+
+ # check and store certain form parameters that might have been passed as get, so we can later overwrite the values from the draft
+ # the overwrite happens at the end of this function
+ my @valid_overwrite_vars = qw(remove_draft amount_1 invnumber ordnumber transdate duedate notes datepaid_1 paid_1 callback AP_paid_1 currency); # reference description
+ my $overwrite_hash;
+ # my @valid_fields;
+ foreach ( @valid_overwrite_vars ) {
+ $overwrite_hash->{$_} = $form->{$_} if exists $form->{$_}; # variant 1
+ # push(@valid_fields, $_) if exists $form->{$_}; # variant 2
+ };
my ($old_form, $id, $description) = Drafts->load(\%myconfig, $form, $form->{id});
+
if ($old_form) {
- restore_form($old_form, 1);
- $form->{draft_id} = $id;
- $form->{draft_description} = $description;
- $form->{remove_draft} = 'checked';
+ $old_form = YAML::Load($old_form);
+
+ my %dont_save_vars = map { $_ => 1 } Drafts->dont_save;
+ my @restore_vars = grep { !$dont_save_vars{$_} } keys %{ $old_form };
+
+ @{$form}{@restore_vars} = @{$old_form}{@restore_vars};
+
+ $form->{draft_id} = $id;
+ $form->{draft_description} = $description;
+ $form->{remove_draft} = 'checked';
}
+ # Ich vergesse bei Rechnungsentwürfe das Rechnungsdatum zu ändern. Dadurch entstehen
+ # ungültige Belege. Vielleicht geht es anderen ähnlich jan 19.2.2011
+ $form->{invdate} = $form->current_date(\%myconfig); # Aktuelles Rechnungsdatum ...
+ $form->{duedate} = $form->current_date(\%myconfig); # Aktuelles Fälligkeitsdatum ...
+
+ if ( $overwrite_hash ) {
+ foreach ( keys $overwrite_hash ) {
+ $form->{$_} = $overwrite_hash->{$_}; # variante 1
+ };
+ };
+ # @{$form}{@valid_fields} = @{$overwrite_hash}{@valid_fields}; # variante 2
update();
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
sub delete_drafts {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
my @ids;
- foreach (keys(%{$form})) {
- push(@ids, $1) if (/^checked_(.*)/ && $form->{$_});
+ foreach (keys %{$form}) {
+ push @ids, $1 if (/^checked_(.*)/ && $form->{$_});
}
Drafts->remove(\%myconfig, $form, @ids) if (@ids);
+
restore_form($form->{SAVED_FORM}, 1);
- delete($form->{SAVED_FORM});
+ delete $form->{SAVED_FORM};
add();
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
sub draft_action_dispatcher {
- $lxdebug->enter_sub();
+ $main::lxdebug->enter_sub();
+
+ my $form = $main::form;
+ my $locale = $main::locale;
if ($form->{draft_action} eq $locale->text("Skip")) {
dont_load_draft();
+
} elsif ($form->{draft_action} eq $locale->text("Delete drafts")) {
delete_drafts();
}
- $lxdebug->leave_sub();
+ $main::lxdebug->leave_sub();
}
1;