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 ######################################################################
20 $main::lxdebug->enter_sub(2);
22 my $form = $main::form;
24 my $script = $form->{script};
28 foreach my $key (@_) {
31 if ($key =~ /(.*?)=(.*)/) {
39 foreach my $var ($form->flatten_variables($key)) {
40 push @parts, E($var->{key}) . '=' . E($var->{value});
45 my $url = "${script}?" . join('&', @parts);
47 $main::lxdebug->leave_sub(2);
52 # -------------------------------------------------------------------------
55 $main::lxdebug->enter_sub();
57 my ($callback_sub, @parts) = @_;
59 my $form = $main::form;
60 my $locale = $main::locale;
62 my $remap_parts_id = 0;
63 if (defined($parts[0]->{parts_id}) && !defined($parts[0]->{id})) {
65 map { $_->{id} = $_->{parts_id}; } @parts;
68 my $remap_partnumber = 0;
69 if (defined($parts[0]->{partnumber}) && !defined($parts[0]->{number})) {
70 $remap_partnumber = 1;
71 map { $_->{number} = $_->{partnumber}; } @parts;
75 if (defined($parts[0]->{chargenumber})) {
77 map { $_->{has_charge} = 1; } @parts;
80 if (defined($parts[0]->{ean})) {
82 map { $_->{has_ean} = 1; } @parts;
85 my $old_form = save_form();
88 print $form->parse_html_template("generic/select_part",
90 "old_form" => $old_form,
91 "title" => $locale->text("Select a part"),
92 "nextsub" => "select_part_internal",
93 "callback_sub" => $callback_sub,
94 "has_charge" => $has_charge,
95 "has_ean" => $has_ean,
96 "remap_parts_id" => $remap_parts_id,
97 "remap_partnumber" => $remap_partnumber });
99 $main::lxdebug->leave_sub();
102 sub select_part_internal {
103 $main::lxdebug->enter_sub();
105 my $form = $main::form;
107 my ($new_item, $callback_sub);
109 my $re = "^new_.*_$form->{selection}\$";
111 foreach (grep /$re/, keys %{ $form }) {
113 $new_key =~ s/^new_//;
114 $new_key =~ s/_\d+$//;
115 $new_item->{$new_key} = $form->{$_};
118 if ($form->{remap_parts_id}) {
119 $new_item->{parts_id} = $new_item->{id};
120 delete $new_item->{id};
123 if ($form->{remap_partnumber}) {
124 $new_item->{partnumber} = $new_item->{number};
125 delete $new_item->{number};
128 $callback_sub = $form->{callback_sub};
130 restore_form($form->{old_form});
132 call_sub($callback_sub, $new_item);
134 $main::lxdebug->leave_sub();
137 sub part_selection_internal {
138 $main::lxdebug->enter_sub();
140 my $form = $main::form;
141 my %myconfig = %main::myconfig;
142 my $locale = $main::locale;
144 my $order_by = "description";
145 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
147 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
151 foreach my $opt (split m/:/, $form->{options}) {
153 my ($key, $value) = split m/=/, $opt, 2;
154 $options{$key} = $value;
161 map { $form->{$_} = $options{$_} if ($options{$_}) } qw(no_services no_assemblies assemblies click_button);
163 my $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
166 if (0 == scalar(@{$parts})) {
167 $form->show_generic_information($locale->text("No part was found matching the search parameters."));
168 } elsif (1 == scalar(@{$parts})) {
169 $onload = "part_selected('1')";
172 map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
174 my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
175 grep({ /^[fl]_/ } keys %{ $form }));
177 my @header_sort = qw(partnumber description);
178 my %header_title = ( "partnumber" => $locale->text("Part Number"),
179 "description" => $locale->text("Part description"),
183 map(+{ "column_title" => $header_title{$_},
185 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
189 $form->{formname} ||= 'Form';
191 $form->{title} = $locale->text("Select a part");
193 print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
195 "onload" => $onload });
197 $main::lxdebug->leave_sub();
200 # -------------------------------------------------------------------------
202 sub delivery_customer_selection {
203 $main::lxdebug->enter_sub();
205 my $form = $main::form;
206 my %myconfig = %main::myconfig;
207 my $locale = $main::locale;
209 my $order_by = "name";
210 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
212 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
214 my $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
215 map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
218 if (0 == scalar(@{$delivery})) {
219 $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
220 } elsif (1 == scalar(@{$delivery})) {
221 $onload = "customer_selected('1')";
224 my $callback = "$form->{script}?action=delivery_customer_selection&";
225 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
226 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
228 my @header_sort = qw(name customernumber address);
229 my %header_title = ( "name" => $locale->text("Name"),
230 "customernumber" => $locale->text("Customer Number"),
231 "address" => $locale->text("Address"),
235 map(+{ "column_title" => $header_title{$_},
237 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
241 $form->{"title"} = $locale->text("Select a Customer");
243 print $form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
244 "DELIVERY" => $delivery,
245 "onload" => $onload });
247 $main::lxdebug->leave_sub();
250 # -------------------------------------------------------------------------
252 sub vendor_selection {
253 $main::lxdebug->enter_sub();
255 my $form = $main::form;
256 my %myconfig = %main::myconfig;
257 my $locale = $main::locale;
259 my $order_by = "name";
260 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
262 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
264 my $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
265 map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
268 if (0 == scalar(@{$vendor})) {
269 $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
270 } elsif (1 == scalar(@{$vendor})) {
271 $onload = "vendor_selected('1')";
274 my $callback = "$form->{script}?action=vendor_selection&";
275 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
276 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
278 my @header_sort = qw(name customernumber address);
279 my %header_title = ( "name" => $locale->text("Name"),
280 "customernumber" => $locale->text("Customer Number"),
281 "address" => $locale->text("Address"),
285 map(+{ "column_title" => $header_title{$_},
287 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
291 $form->{"title"} = $locale->text("Select a Customer");
293 print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
295 "onload" => $onload });
297 $main::lxdebug->leave_sub();
300 # -------------------------------------------------------------------------
303 $main::lxdebug->enter_sub();
305 my $form = $main::form;
306 my $locale = $main::locale;
308 $form->{formel} =~ s/\r\n//g;
310 my ($variable_string, $formel) = split /###/,$form->{formel};
312 my $onload; # note! this sub is mostly called over a javascript invocation, and it's unlikey that onload is set.
314 foreach my $item (split m/;/, $variable_string) {
315 next unless $item =~ m/^ \s* (\w+) \s* = \s* (\w+) \s* (\w+) \s* $/x;
323 my @header_sort = qw(variable value unit);
325 variable => $locale->text("Variable"),
326 value => $locale->text("Value"),
327 unit => $locale->text("Unit"),
330 column_title => $header_title{$_},
334 $form->{formel} = $formel;
335 $form->{title} = $locale->text("Please enter values");
337 print $form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
338 "VARIABLES" => \@variable,
339 "onload" => $onload });
341 $main::lxdebug->leave_sub();
344 # -------------------------------------------------------------------------
346 sub set_longdescription {
347 $main::lxdebug->enter_sub();
349 my $form = $main::form;
350 my $locale = $main::locale;
352 $form->{title} = $locale->text("Enter longdescription");
354 print $form->parse_html_template("generic/set_longdescription");
356 $main::lxdebug->leave_sub();
359 # -------------------------------------------------------------------------
362 return $main::locale->quote_special_chars('HTML', $_[0]);
366 return $main::locale->quote_special_chars('URL@HTML', $_[0]);
370 return $main::form->escape($_[0]);
376 $element =~ s/tabindex\s*=\s*"\d+"//;
381 $main::lxdebug->enter_sub();
383 my ($dateformat, $longformat, @indices) = @_;
385 my $form = $main::form;
386 my %myconfig = %main::myconfig;
387 my $locale = $main::locale;
389 $dateformat = $myconfig{"dateformat"} unless ($dateformat);
391 foreach my $idx (@indices) {
392 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
393 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
394 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] =
395 $locale->reformat_date(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i],
396 $dateformat, $longformat);
400 next unless (defined($form->{$idx}));
402 if (!ref($form->{$idx})) {
403 $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
404 $dateformat, $longformat);
406 } elsif (ref($form->{$idx}) eq "ARRAY") {
407 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
408 $form->{$idx}->[$i] =
409 $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
410 $dateformat, $longformat);
415 $main::lxdebug->leave_sub();
418 sub reformat_numbers {
419 $main::lxdebug->enter_sub();
421 my ($numberformat, $places, @indices) = @_;
423 my $form = $main::form;
424 my %myconfig = %main::myconfig;
426 return $main::lxdebug->leave_sub()
427 if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
429 foreach my $idx (@indices) {
430 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
431 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
432 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->parse_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i]);
436 next unless (defined($form->{$idx}));
438 if (!ref($form->{$idx})) {
439 $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
441 } elsif (ref($form->{$idx}) eq "ARRAY") {
442 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
443 $form->{$idx}->[$i] =
444 $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
449 my $saved_numberformat = $myconfig{"numberformat"};
450 $myconfig{"numberformat"} = $numberformat;
452 foreach my $idx (@indices) {
453 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
454 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
455 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->format_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i], $places);
459 next unless (defined($form->{$idx}));
461 if (!ref($form->{$idx})) {
462 $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
464 } elsif (ref($form->{$idx}) eq "ARRAY") {
465 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
466 $form->{$idx}->[$i] =
467 $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
472 $myconfig{"numberformat"} = $saved_numberformat;
474 $main::lxdebug->leave_sub();
477 # -------------------------------------------------------------------------
480 $main::lxdebug->enter_sub();
482 my $form = $main::form;
483 my %myconfig = %main::myconfig;
484 my $locale = $main::locale;
486 my $dbh = $form->dbconnect(\%myconfig);
487 my ($sort, $sortby) = split(/\-\-/, $form->{order});
488 $sort =~ s/.*\.(.*)/$1/;
490 $form->{title} = $locale->text("History");
492 print $form->parse_html_template( "common/show_history", {
493 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
494 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
496 uc($sort)."BY" => $sortby
500 $main::lxdebug->leave_sub();
503 # -------------------------------------------------------------------------
506 $main::lxdebug->enter_sub();
510 my $form = $main::form;
511 my $locale = $main::locale;
514 $form->error($locale->text("Trying to call a sub without a name"));
517 $name =~ s/[^a-zA-Z0-9_]//g;
519 if (!defined(&{ $name })) {
520 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
528 $main::lxdebug->leave_sub();
531 # -------------------------------------------------------------------------
533 sub show_vc_details {
534 $main::lxdebug->enter_sub();
536 my $form = $main::form;
537 my %myconfig = %main::myconfig;
538 my $locale = $main::locale;
540 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
541 $form->isblank("vc_id",
542 $form->{vc} eq "customer" ?
543 $locale->text("No customer has been selected yet.") :
544 $locale->text("No vendor has been selected yet."));
546 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
548 $form->{title} = $form->{vc} eq "customer" ?
549 $locale->text("Customer details") : $locale->text("Vendor details");
551 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
553 $main::lxdebug->leave_sub();
556 # -------------------------------------------------------------------------
558 sub retrieve_partunits {
559 $main::lxdebug->enter_sub();
561 my $form = $main::form;
563 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
566 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
568 foreach my $i (1..$form->{rowcount}) {
569 next unless ($form->{"id_${i}"});
570 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
574 $main::lxdebug->leave_sub();
577 # -------------------------------------------------------------------------
579 sub mark_as_paid_common {
580 $main::lxdebug->enter_sub();
582 my ($myconfig, $db_name) = @_;
584 my $form = $main::form;
585 my $locale = $main::locale;
587 if($form->{mark_as_paid}) {
588 my $dbh ||= $form->get_standard_dbh($myconfig);
589 my $query = qq|UPDATE $db_name SET paid = amount WHERE id = ?|;
590 do_query($form, $dbh, $query, $form->{id});
592 $form->redirect($locale->text("Marked as paid"));
595 my $referer = $ENV{HTTP_REFERER};
598 if ($referer =~ /action/) {
599 $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
606 $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
609 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
610 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
611 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
612 print qq|</body></html>|;
615 $main::lxdebug->leave_sub();
618 sub cov_selection_internal {
619 $main::lxdebug->enter_sub();
621 my $form = $main::form;
622 my %myconfig = %main::myconfig;
623 my $locale = $main::locale;
625 my $order_by = "name";
626 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
628 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
630 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
632 my $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
633 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
636 if (0 == scalar(@{$covs})) {
637 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
638 } elsif (1 == scalar(@{$covs})) {
639 $onload = "cov_selected('1')";
642 my $callback = "$form->{script}?action=cov_selection_internal&";
643 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
644 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
646 my @header_sort = qw(name address contact);
647 my %header_title = ( "name" => $locale->text("Name"),
648 "address" => $locale->text("Address"),
649 "contact" => $locale->text("Contact"),
653 map(+{ "column_title" => $header_title{$_},
655 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
659 foreach my $cov (@{ $covs }) {
660 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
661 $cov->{address} =~ s{^,}{}x;
662 $cov->{address} =~ s{\ +}{\ }gx;
664 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_gender cp_title cp_givenname cp_name);
665 $cov->{contact} =~ s{\ +}{\ }gx;
668 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
670 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
672 "onload" => $onload }));
674 $main::lxdebug->leave_sub();
678 # Functions to call add routines beneath different reports
681 $main::lxdebug->enter_sub();
683 $main::auth->assert('invoice_edit');
685 my $form = $main::form;
686 my %myconfig = %main::myconfig;
687 my $locale = $main::locale;
689 $form->{script} = 'is.pl';
691 $form->{type} = "invoice";
692 $locale = new Locale "$myconfig{countrycode}", "$script";
694 require "bin/mozilla/$form->{script}";
697 $main::lxdebug->leave_sub();
701 $main::lxdebug->enter_sub();
703 $main::auth->assert('general_ledger');
705 my $form = $main::form;
706 my %myconfig = %main::myconfig;
707 my $locale = $main::locale;
709 $form->{script} = 'ar.pl';
711 $locale = new Locale "$myconfig{countrycode}", "$script";
713 require "bin/mozilla/$form->{script}";
716 $main::lxdebug->leave_sub();
720 $main::lxdebug->enter_sub();
722 $main::auth->assert('invoice_edit');
724 my $form = $main::form;
725 my %myconfig = %main::myconfig;
726 my $locale = $main::locale;
728 $form->{script} = 'ir.pl';
730 $form->{type} = "invoice";
731 $locale = new Locale "$myconfig{countrycode}", "$script";
733 require "bin/mozilla/$form->{script}";
736 $main::lxdebug->leave_sub();
740 $main::lxdebug->enter_sub();
742 $main::auth->assert('general_ledger');
744 my $form = $main::form;
745 my %myconfig = %main::myconfig;
746 my $locale = $main::locale;
748 $form->{script} = 'ap.pl';
750 $locale = new Locale "$myconfig{countrycode}", "$script";
752 require "bin/mozilla/$form->{script}";
755 $main::lxdebug->leave_sub();
759 $main::lxdebug->enter_sub();
761 $main::auth->assert('general_ledger');
763 my $form = $main::form;
764 my %myconfig = %main::myconfig;
765 my $locale = $main::locale;
767 $form->{script} = 'gl.pl';
769 $locale = new Locale "$myconfig{countrycode}", "$script";
771 require "bin/mozilla/$form->{script}";
774 $main::lxdebug->leave_sub();