1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 ######################################################################
8 # Stuff that can be used from other modules
10 ######################################################################
13 use SL::DB::Helper::Mappings;
17 use SL::Helper::Flash;
22 $main::lxdebug->enter_sub(2);
24 my $form = $main::form;
26 my $script = $form->{script};
30 foreach my $key (@_) {
33 if ($key =~ /(.*?)=(.*)/) {
41 foreach my $var ($form->flatten_variables($key)) {
42 push @parts, E($var->{key}) . '=' . E($var->{value});
47 my $url = "${script}?" . join('&', @parts);
49 $main::lxdebug->leave_sub(2);
54 # -------------------------------------------------------------------------
57 $main::lxdebug->enter_sub();
59 my ($callback_sub, @parts) = @_;
61 my $form = $main::form;
62 my $locale = $main::locale;
64 my $remap_parts_id = 0;
65 if (defined($parts[0]->{parts_id}) && !defined($parts[0]->{id})) {
67 map { $_->{id} = $_->{parts_id}; } @parts;
70 my $remap_partnumber = 0;
71 if (defined($parts[0]->{partnumber}) && !defined($parts[0]->{number})) {
72 $remap_partnumber = 1;
73 map { $_->{number} = $_->{partnumber}; } @parts;
77 if (defined($parts[0]->{chargenumber})) {
79 map { $_->{has_charge} = 1; } @parts;
81 my $has_bestbefore = 0;
82 if (defined($parts[0]->{bestbefore})) {
84 map { $_->{has_bestbefore} = 1; } @parts;
87 if (defined($parts[0]->{ean})) {
89 map { $_->{has_ean} = 1; } @parts;
92 my $old_form = save_form();
95 print $form->parse_html_template("generic/select_part",
97 "old_form" => $old_form,
98 "title" => $locale->text("Select a part"),
99 "nextsub" => "select_part_internal",
100 "callback_sub" => $callback_sub,
101 "has_charge" => $has_charge,
102 "has_bestbefore" => $has_bestbefore,
103 "has_ean" => $has_ean,
104 "remap_parts_id" => $remap_parts_id,
105 "remap_partnumber" => $remap_partnumber });
107 $main::lxdebug->leave_sub();
110 sub select_part_internal {
111 $main::lxdebug->enter_sub();
113 my $form = $main::form;
115 my ($new_item, $callback_sub);
117 my $re = "^new_.*_$form->{selection}\$";
119 foreach (grep /$re/, keys %{ $form }) {
121 $new_key =~ s/^new_//;
122 $new_key =~ s/_\d+$//;
123 $new_item->{$new_key} = $form->{$_};
126 if ($form->{remap_parts_id}) {
127 $new_item->{parts_id} = $new_item->{id};
128 delete $new_item->{id};
131 if ($form->{remap_partnumber}) {
132 $new_item->{partnumber} = $new_item->{number};
133 delete $new_item->{number};
136 $callback_sub = $form->{callback_sub};
138 restore_form($form->{old_form});
140 call_sub($callback_sub, $new_item);
142 $main::lxdebug->leave_sub();
145 sub part_selection_internal {
146 $main::lxdebug->enter_sub();
148 my $form = $main::form;
149 my %myconfig = %main::myconfig;
150 my $locale = $main::locale;
152 my $order_by = "description";
153 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
155 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
159 foreach my $opt (split m/:/, $form->{options}) {
161 my ($key, $value) = split m/=/, $opt, 2;
162 $options{$key} = $value;
169 map { $form->{$_} = $options{$_} if ($options{$_}) } qw(no_services no_assemblies assemblies click_button);
171 my $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
174 if (0 == scalar(@{$parts})) {
175 $form->show_generic_information($locale->text("No part was found matching the search parameters."));
176 } elsif (1 == scalar(@{$parts})) {
177 $onload = "part_selected('1')";
180 map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
182 my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
183 grep({ /^[fl]_/ } keys %{ $form }));
185 my @header_sort = qw(partnumber description);
186 my %header_title = ( "partnumber" => $locale->text("Part Number"),
187 "description" => $locale->text("Part Description"),
191 map(+{ "column_title" => $header_title{$_},
193 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
197 $form->{formname} ||= 'Form';
199 $form->{title} = $locale->text("Select a part");
201 print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
203 "onload" => $onload });
205 $main::lxdebug->leave_sub();
208 # -------------------------------------------------------------------------
210 sub delivery_customer_selection {
211 $main::lxdebug->enter_sub();
213 my $form = $main::form;
214 my %myconfig = %main::myconfig;
215 my $locale = $main::locale;
217 my $order_by = "name";
218 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
220 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
222 my $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
223 map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
226 if (0 == scalar(@{$delivery})) {
227 $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
228 } elsif (1 == scalar(@{$delivery})) {
229 $onload = "customer_selected('1')";
232 my $callback = "$form->{script}?action=delivery_customer_selection&";
233 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
234 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
236 my @header_sort = qw(name customernumber address);
237 my %header_title = ( "name" => $locale->text("Name"),
238 "customernumber" => $locale->text("Customer Number"),
239 "address" => $locale->text("Address"),
243 map(+{ "column_title" => $header_title{$_},
245 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
249 $form->{"title"} = $locale->text("Select a Customer");
251 print $form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
252 "DELIVERY" => $delivery,
253 "onload" => $onload });
255 $main::lxdebug->leave_sub();
258 # -------------------------------------------------------------------------
260 sub vendor_selection {
261 $main::lxdebug->enter_sub();
263 my $form = $main::form;
264 my %myconfig = %main::myconfig;
265 my $locale = $main::locale;
267 my $order_by = "name";
268 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
270 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
272 my $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
273 map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
276 if (0 == scalar(@{$vendor})) {
277 $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
278 } elsif (1 == scalar(@{$vendor})) {
279 $onload = "vendor_selected('1')";
282 my $callback = "$form->{script}?action=vendor_selection&";
283 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
284 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
286 my @header_sort = qw(name customernumber address);
287 my %header_title = ( "name" => $locale->text("Name"),
288 "customernumber" => $locale->text("Customer Number"),
289 "address" => $locale->text("Address"),
293 map(+{ "column_title" => $header_title{$_},
295 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
299 $form->{"title"} = $locale->text("Select a Customer");
301 print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
303 "onload" => $onload });
305 $main::lxdebug->leave_sub();
308 # -------------------------------------------------------------------------
311 $main::lxdebug->enter_sub();
313 my $form = $main::form;
314 my $locale = $main::locale;
316 $form->{formel} =~ s/\r\n//g;
318 my ($variable_string, $formel) = split /###/,$form->{formel};
320 my $onload; # note! this sub is mostly called over a javascript invocation, and it's unlikey that onload is set.
322 foreach my $item (split m/;/, $variable_string) {
323 next unless $item =~ m/^ \s* (\w+) \s* = \s* (\w+) \s* (\w+) \s* $/x;
331 my @header_sort = qw(variable value unit);
333 variable => $locale->text("Variable"),
334 value => $locale->text("Value"),
335 unit => $locale->text("Unit"),
338 column_title => $header_title{$_},
342 $form->{formel} = $formel;
343 $form->{title} = $locale->text("Please enter values");
345 print $form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
346 "VARIABLES" => \@variable,
347 "onload" => $onload });
349 $main::lxdebug->leave_sub();
352 # -------------------------------------------------------------------------
354 sub set_longdescription {
355 $main::lxdebug->enter_sub();
357 my $form = $main::form;
358 my $locale = $main::locale;
360 $form->{title} = $locale->text("Enter longdescription");
362 print $form->parse_html_template("generic/set_longdescription");
364 $main::lxdebug->leave_sub();
367 # -------------------------------------------------------------------------
370 return $main::locale->quote_special_chars('HTML', $_[0]);
374 return $main::locale->quote_special_chars('URL@HTML', $_[0]);
378 return $main::form->escape($_[0]);
384 $element =~ s/tabindex\s*=\s*"\d+"//;
389 return $::form->format_dates(@_);
392 sub reformat_numbers {
393 return $::form->reformat_numbers(@_);
396 # -------------------------------------------------------------------------
399 $main::lxdebug->enter_sub();
401 my $form = $main::form;
402 my %myconfig = %main::myconfig;
403 my $locale = $main::locale;
405 my $dbh = $form->dbconnect(\%myconfig);
406 my ($sort, $sortby) = split(/\-\-/, $form->{order});
407 $sort =~ s/.*\.(.*)/$1/;
409 $form->{title} = $locale->text("History");
411 print $form->parse_html_template( "common/show_history", {
412 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
413 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
415 uc($sort)."BY" => $sortby
419 $main::lxdebug->leave_sub();
422 # -------------------------------------------------------------------------
425 $main::lxdebug->enter_sub();
429 my $form = $main::form;
430 my $locale = $main::locale;
433 $form->error($locale->text("Trying to call a sub without a name"));
436 $name =~ s/[^a-zA-Z0-9_]//g;
438 if (!defined(&{ $name })) {
439 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
447 $main::lxdebug->leave_sub();
450 # -------------------------------------------------------------------------
452 sub show_vc_details {
453 $main::lxdebug->enter_sub();
455 my $form = $main::form;
456 my %myconfig = %main::myconfig;
457 my $locale = $main::locale;
459 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
460 $form->isblank("vc_id",
461 $form->{vc} eq "customer" ?
462 $locale->text("No customer has been selected yet.") :
463 $locale->text("No vendor has been selected yet."));
465 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
467 $form->{title} = $form->{vc} eq "customer" ?
468 $locale->text("Customer details") : $locale->text("Vendor details");
470 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
472 $main::lxdebug->leave_sub();
475 # -------------------------------------------------------------------------
477 sub retrieve_partunits {
478 $main::lxdebug->enter_sub();
480 my $form = $main::form;
482 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
485 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
487 foreach my $i (1..$form->{rowcount}) {
488 next unless ($form->{"id_${i}"});
489 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
493 $main::lxdebug->leave_sub();
496 # -------------------------------------------------------------------------
498 sub mark_as_paid_common {
499 $main::lxdebug->enter_sub();
501 my ($myconfig, $db_name) = @_;
503 my $form = $main::form;
504 my $locale = $main::locale;
506 if($form->{mark_as_paid}) {
507 my $dbh ||= $form->get_standard_dbh($myconfig);
508 my $query = qq|UPDATE $db_name SET paid = amount, datepaid = current_date WHERE id = ?|;
509 do_query($form, $dbh, $query, $form->{id});
511 $form->redirect($locale->text("Marked as paid"));
514 my $referer = $ENV{HTTP_REFERER};
517 if ($referer =~ /action/) {
518 $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
525 $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
528 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
529 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
530 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
531 print qq|</body></html>|;
534 $main::lxdebug->leave_sub();
537 sub cov_selection_internal {
538 $main::lxdebug->enter_sub();
540 my $form = $main::form;
541 my %myconfig = %main::myconfig;
542 my $locale = $main::locale;
544 my $order_by = "name";
545 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
547 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
549 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
551 my $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
552 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
555 if (0 == scalar(@{$covs})) {
556 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
557 } elsif (1 == scalar(@{$covs})) {
558 $onload = "cov_selected('1')";
561 my $callback = "$form->{script}?action=cov_selection_internal&";
562 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
563 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
565 my @header_sort = qw(name address contact);
566 my %header_title = ( "name" => $locale->text("Name"),
567 "address" => $locale->text("Address"),
568 "contact" => $locale->text("Contact"),
572 map(+{ "column_title" => $header_title{$_},
574 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
578 foreach my $cov (@{ $covs }) {
579 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
580 $cov->{address} =~ s{^,}{}x;
581 $cov->{address} =~ s{\ +}{\ }gx;
583 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_gender cp_title cp_givenname cp_name);
584 $cov->{contact} =~ s{\ +}{\ }gx;
587 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
589 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
591 "onload" => $onload }));
593 $main::lxdebug->leave_sub();
597 # Functions to call add routines beneath different reports
600 $main::lxdebug->enter_sub();
602 print $::form->redirect_header('is.pl?action=add&type=invoice');
604 $main::lxdebug->leave_sub();
608 $main::lxdebug->enter_sub();
610 print $::form->redirect_header('ar.pl?action=add');
612 $main::lxdebug->leave_sub();
616 $main::lxdebug->enter_sub();
618 print $::form->redirect_header('ir.pl?action=add&type=invoice');
620 $main::lxdebug->leave_sub();
624 $main::lxdebug->enter_sub();
626 print $::form->redirect_header('ap.pl?action=add');
628 $main::lxdebug->leave_sub();
632 $main::lxdebug->enter_sub();
634 print $::form->redirect_header('gl.pl?action=add');
636 $main::lxdebug->leave_sub();
640 goto &SL::DB::Helper::Mappings::db;