1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
11 use Time::HiRes qw(gettimeofday);
14 my ($a, $b) = gettimeofday();
15 return "${a}-${b}-${$}";
19 return "/tmp/lx-office-tmp-" . unique_id();
23 $main::lxdebug->enter_sub();
25 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
27 my $dbh = $form->dbconnect($myconfig);
29 my (@filter_values, $filter);
30 if ($form->{"partnumber"}) {
31 $filter .= qq| AND (partnumber ILIKE ?)|;
32 push(@filter_values, '%' . $form->{"partnumber"} . '%');
34 if ($form->{"description"}) {
35 $filter .= qq| AND (description ILIKE ?)|;
36 push(@filter_values, '%' . $form->{"description"} . '%');
38 substr($filter, 1, 3) = "WHERE" if ($filter);
40 $order_by =~ s/[^a-zA-Z_]//g;
41 $order_dir = $order_dir ? "ASC" : "DESC";
44 qq|SELECT id, partnumber, description | .
45 qq|FROM parts $filter | .
46 qq|ORDER BY $order_by $order_dir|;
47 my $sth = $dbh->prepare($query);
48 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
50 while (my $ref = $sth->fetchrow_hashref()) {
51 push(@{$parts}, $ref);
56 $main::lxdebug->leave_sub();
61 sub retrieve_projects {
62 $main::lxdebug->enter_sub();
64 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
66 my $dbh = $form->dbconnect($myconfig);
68 my (@filter_values, $filter);
69 if ($form->{"projectnumber"}) {
70 $filter .= qq| AND (projectnumber ILIKE ?)|;
71 push(@filter_values, '%' . $form->{"projectnumber"} . '%');
73 if ($form->{"description"}) {
74 $filter .= qq| AND (description ILIKE ?)|;
75 push(@filter_values, '%' . $form->{"description"} . '%');
77 substr($filter, 1, 3) = "WHERE" if ($filter);
79 $order_by =~ s/[^a-zA-Z_]//g;
80 $order_dir = $order_dir ? "ASC" : "DESC";
83 qq|SELECT id, projectnumber, description | .
84 qq|FROM project $filter | .
85 qq|ORDER BY $order_by $order_dir|;
86 my $sth = $dbh->prepare($query);
87 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
89 while (my $ref = $sth->fetchrow_hashref()) {
90 push(@{$projects}, $ref);
95 $main::lxdebug->leave_sub();
100 sub retrieve_employees {
101 $main::lxdebug->enter_sub();
103 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
105 my $dbh = $form->dbconnect($myconfig);
107 my (@filter_values, $filter);
108 if ($form->{"name"}) {
109 $filter .= qq| AND (name ILIKE ?)|;
110 push(@filter_values, '%' . $form->{"name"} . '%');
112 substr($filter, 1, 3) = "WHERE" if ($filter);
114 $order_by =~ s/[^a-zA-Z_]//g;
115 $order_dir = $order_dir ? "ASC" : "DESC";
118 qq|SELECT id, name | .
119 qq|FROM employee $filter | .
120 qq|ORDER BY $order_by $order_dir|;
121 my $sth = $dbh->prepare($query);
122 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
124 while (my $ref = $sth->fetchrow_hashref()) {
125 push(@{$employees}, $ref);
130 $main::lxdebug->leave_sub();
135 sub retrieve_delivery_customer {
136 $main::lxdebug->enter_sub();
138 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
140 my $dbh = $form->dbconnect($myconfig);
142 my (@filter_values, $filter);
143 if ($form->{"name"}) {
144 $filter .= qq| (name ILIKE ?) AND|;
145 push(@filter_values, '%' . $form->{"name"} . '%');
148 $order_by =~ s/[^a-zA-Z_]//g;
149 $order_dir = $order_dir ? "ASC" : "DESC";
152 qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
154 qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
155 qq!ORDER BY $order_by $order_dir!;
156 my $sth = $dbh->prepare($query);
157 $sth->execute(@filter_values) ||
158 $form->dberror($query . " (" . join(", ", @filter_values) . ")");
159 my $delivery_customers = [];
160 while (my $ref = $sth->fetchrow_hashref()) {
161 push(@{$delivery_customers}, $ref);
166 $main::lxdebug->leave_sub();
168 return $delivery_customers;
171 sub retrieve_vendor {
172 $main::lxdebug->enter_sub();
174 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
176 my $dbh = $form->dbconnect($myconfig);
178 my (@filter_values, $filter);
179 if ($form->{"name"}) {
180 $filter .= qq| (name ILIKE ?) AND|;
181 push(@filter_values, '%' . $form->{"name"} . '%');
184 $order_by =~ s/[^a-zA-Z_]//g;
185 $order_dir = $order_dir ? "ASC" : "DESC";
188 qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
189 qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
190 qq!ORDER BY $order_by $order_dir!;
191 my $sth = $dbh->prepare($query);
192 $sth->execute(@filter_values) ||
193 $form->dberror($query . " (" . join(", ", @filter_values) . ")");
195 while (my $ref = $sth->fetchrow_hashref()) {
196 push(@{$vendors}, $ref);
201 $main::lxdebug->leave_sub();
206 sub mkdir_with_parents {
207 $main::lxdebug->enter_sub();
209 my ($full_path) = @_;
213 $full_path =~ s|/+|/|;
215 foreach my $part (split(m|/|, $full_path)) {
216 $path .= "/" if ($path);
219 die("Could not create directory '$path' because a file exists with " .
220 "the same name.\n") if (-f $path);
223 mkdir($path, 0770) || die("Could not create the directory '$path'. " .
228 $main::lxdebug->leave_sub();
232 $main::lxdebug->enter_sub();
236 return $main::lxdebug->leave_sub()
237 unless ($main::webdav && $form->{id});
241 $form->{WEBDAV} = {};
243 if ($form->{type} eq "sales_quotation") {
244 ($path, $number) = ("angebote", $form->{quonumber});
245 } elsif ($form->{type} eq "sales_order") {
246 ($path, $number) = ("bestellungen", $form->{ordnumber});
247 } elsif ($form->{type} eq "request_quotation") {
248 ($path, $number) = ("anfragen", $form->{quonumber});
249 } elsif ($form->{type} eq "purchase_order") {
250 ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
251 } elsif ($form->{type} eq "credit_note") {
252 ($path, $number) = ("gutschriften", $form->{invnumber});
253 } elsif ($form->{vc} eq "customer") {
254 ($path, $number) = ("rechnungen", $form->{invnumber});
256 ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
259 return $main::lxdebug->leave_sub() unless ($path && $number);
261 $path = "webdav/${path}/${number}";
264 mkdir_with_parents($path);
267 my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
268 $base_path =~ s|[^/]+$||;
270 foreach my $file (<$path/*>) {
273 $form->{WEBDAV}{$fname} =
274 ($ENV{"HTTPS"} ? "https://" : "http://") .
275 $ENV{'SERVER_NAME'} . "/" . $base_path . $file;
279 $main::lxdebug->leave_sub();