1 #======================================================================
4 #======================================================================
6 # Saving and loading drafts
8 #======================================================================
18 $main::lxdebug->enter_sub();
20 my ($self, $form) = @_;
22 my ($module, $submodule);
24 $module = $form->{"script"};
26 if (grep({ $module eq $_ } qw(is ir ar ap))) {
27 $submodule = "invoice";
29 $submodule = "unknown";
32 $main::lxdebug->leave_sub();
34 return ($module, $submodule);
37 @dont_save = qw(login password stylesheet action);
40 $main::lxdebug->enter_sub();
42 my ($self, $myconfig, $form, $draft_id, $draft_description) = @_;
44 my ($dbh, $sth, $query, %saved, $dumped);
46 $dbh = $form->dbconnect_noauto($myconfig);
48 my ($module, $submodule) = $self->get_module($form);
50 $query = "SELECT COUNT(*) FROM drafts WHERE id = ?";
51 my ($res) = selectrow_query($form, $dbh, $query, $draft_id);
54 $draft_id = $module . "-" . $submodule . "-" . Common::unique_id();
55 $query = "INSERT INTO drafts (id, module, submodule) VALUES (?, ?, ?)";
56 do_query($form, $dbh, $query, $draft_id, $module, $submodule);
59 map({ $saved{$_} = $form->{$_};
60 delete($form->{$_}); } @dont_save);
62 $dumped = YAML::Dump($form);
63 map({ $form->{$_} = $saved{$_}; } @dont_save);
66 qq|UPDATE drafts SET description = ?, form = ?, employee_id = | .
67 qq| (SELECT id FROM employee WHERE login = ?) | .
70 do_query($form, $dbh, $query, $draft_description, $dumped, $form->{login}, $draft_id);
75 $form->{draft_id} = $draft_id;
76 $form->{draft_description} = $draft_description;
78 $main::lxdebug->leave_sub();
82 $main::lxdebug->enter_sub();
84 my ($self, $myconfig, $form, $draft_id) = @_;
86 my ($dbh, $sth, $query, @values);
88 $dbh = $form->dbconnect($myconfig);
90 $query = qq|SELECT id, description, form FROM drafts WHERE id = ?|;
92 $sth = prepare_execute_query($form, $dbh, $query, $draft_id);
95 if (my $ref = $sth->fetchrow_hashref()) {
96 @values = ($ref->{form}, $ref->{id}, $ref->{description});
102 $main::lxdebug->leave_sub();
108 $main::lxdebug->enter_sub();
110 my ($self, $myconfig, $form, @draft_ids) = @_;
112 return $main::lxdebug->leave_sub() unless (@draft_ids);
114 my ($dbh, $sth, $query);
116 $dbh = $form->dbconnect($myconfig);
118 $query = qq|DELETE FROM drafts WHERE id IN (| . join(", ", map { "?" } @draft_ids) . qq|)|;
119 do_query($form, $dbh, $query, @draft_ids);
123 $main::lxdebug->leave_sub();
127 $main::lxdebug->enter_sub();
129 my ($self, $myconfig, $form) = @_;
131 my ($dbh, $sth, $query, @values);
133 $dbh = $form->dbconnect($myconfig);
135 my ($module, $submodule) = $self->get_module($form);
139 qq|SELECT d.id, d.description, d.itime::timestamp(0) AS itime, | .
140 qq| e.name AS employee_name | .
142 qq|LEFT JOIN employee e ON d.employee_id = e.id | .
143 qq|WHERE (d.module = ?) AND (d.submodule = ?) | .
144 qq|ORDER BY d.itime|;
145 my @values = ($module, $submodule);
147 $sth = prepare_execute_query($form, $dbh, $query, @values);
149 while (my $ref = $sth->fetchrow_hashref()) {
156 $main::lxdebug->leave_sub();