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);
38 $main::lxdebug->enter_sub();
40 my ($self, $myconfig, $form, $draft_id, $draft_description) = @_;
42 my ($dbh, $sth, $query, %saved, @dont_save, $dumped);
44 $dbh = $form->dbconnect_noauto($myconfig);
46 my ($module, $submodule) = $self->get_module($form);
48 $query = "SELECT COUNT(*) FROM drafts WHERE id = ?";
49 my ($res) = selectrow_query($form, $dbh, $query, $draft_id);
51 $draft_id = $module . "-" . $submodule . "-" . Common::unique_id();
52 $query = "INSERT INTO drafts (id, module, submodule) VALUES (?, ?, ?)";
53 do_query($form, $dbh, $query, $draft_id, $module, $submodule);
56 @dont_save = qw(login password action);
57 map({ $saved{$_} = $form->{$_};
58 delete($form->{$_}); } @dont_save);
59 $dumped = YAML::Dump($form);
60 map({ $form->{$_} = $saved{$_}; } @dont_save);
63 qq|UPDATE drafts SET description = ?, form = ?, employee_id = | .
64 qq| (SELECT id FROM employee WHERE login = ?) | .
67 do_query($form, $dbh, $query, $draft_description, $dumped,
68 $form->{login}, $draft_id);
73 $form->{draft_id} = $draft_id;
74 $form->{draft_description} = $draft_description;
76 $main::lxdebug->leave_sub();
80 $main::lxdebug->enter_sub();
82 my ($self, $myconfig, $form, $draft_id) = @_;
84 my ($dbh, $sth, $query, @values);
86 $dbh = $form->dbconnect($myconfig);
88 $query = qq|SELECT id, description, form FROM drafts WHERE id = ?|;
90 $sth = $dbh->prepare($query);
91 $sth->execute($draft_id) || $form->dberror("$query ($draft_id)");
94 if (my $ref = $sth->fetchrow_hashref()) {
95 @values = ($ref->{form}, $ref->{id}, $ref->{description});
99 # do_query($form, $dbh, "DELETE FROM drafts WHERE id = ?", $draft_id);
103 $main::lxdebug->leave_sub();
109 $main::lxdebug->enter_sub();
111 my ($self, $myconfig, $form, @draft_ids) = @_;
113 return $main::lxdebug->leave_sub() unless (@draft_ids);
115 my ($dbh, $sth, $query);
117 $dbh = $form->dbconnect($myconfig);
119 $query = qq|DELETE FROM drafts WHERE id IN (| .
120 join(", ", map({ "?" } @draft_ids)) . qq|)|;
121 do_query($form, $dbh, $query, @draft_ids);
125 $main::lxdebug->leave_sub();
129 $main::lxdebug->enter_sub();
131 my ($self, $myconfig, $form) = @_;
133 my ($dbh, $sth, $query, @values);
135 $dbh = $form->dbconnect($myconfig);
137 my ($module, $submodule) = $self->get_module($form);
141 qq|SELECT d.id, d.description, d.itime::timestamp(0) AS itime, | .
142 qq| e.name AS employee_name | .
144 qq|LEFT JOIN employee e ON d.employee_id = e.id | .
145 qq|WHERE (d.module = ?) AND (d.submodule = ?) | .
146 qq|ORDER BY d.itime|;
147 my @values = ($module, $submodule);
149 $sth = $dbh->prepare($query);
150 $sth->execute(@values) ||
151 $form->dberror($query . " (" . join(", ", @values) . ")");
153 while (my $ref = $sth->fetchrow_hashref()) {
160 $main::lxdebug->leave_sub();