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 ######################################################################
18 $lxdebug->enter_sub(2);
20 my $script = $form->{script};
24 foreach my $key (@_) {
27 if ($key =~ /(.*?)=(.*)/) {
35 foreach my $var ($form->flatten_variables($key)) {
36 push @parts, E($var->{key}) . '=' . E($var->{value});
41 my $url = "${script}?" . join('&', @parts);
43 $lxdebug->leave_sub(2);
48 # -------------------------------------------------------------------------
51 $lxdebug->enter_sub();
53 my ($callback_sub, @parts) = @_;
55 my $remap_parts_id = 0;
56 if (defined($parts[0]->{parts_id}) && !defined($parts[0]->{id})) {
58 map { $_->{id} = $_->{parts_id}; } @parts;
61 my $remap_partnumber = 0;
62 if (defined($parts[0]->{partnumber}) && !defined($parts[0]->{number})) {
63 $remap_partnumber = 1;
64 map { $_->{number} = $_->{partnumber}; } @parts;
68 if (defined($parts[0]->{chargenumber})) {
70 map { $_->{has_charge} = 1; } @parts;
73 if (defined($parts[0]->{ean})) {
75 map { $_->{has_ean} = 1; } @parts;
78 my $old_form = save_form();
81 print $form->parse_html_template("generic/select_part",
83 "old_form" => $old_form,
84 "title" => $locale->text("Select a part"),
85 "nextsub" => "select_part_internal",
86 "callback_sub" => $callback_sub,
87 "has_charge" => $has_charge,
88 "has_ean" => $has_ean,
89 "remap_parts_id" => $remap_parts_id,
90 "remap_partnumber" => $remap_partnumber });
92 $lxdebug->leave_sub();
95 sub select_part_internal {
96 $lxdebug->enter_sub();
98 my ($new_item, $callback_sub);
100 my $re = "^new_.*_$form->{selection}\$";
102 foreach (grep /$re/, keys %{ $form }) {
104 $new_key =~ s/^new_//;
105 $new_key =~ s/_\d+$//;
106 $new_item->{$new_key} = $form->{$_};
109 if ($form->{remap_parts_id}) {
110 $new_item->{parts_id} = $new_item->{id};
111 delete $new_item->{id};
114 if ($form->{remap_partnumber}) {
115 $new_item->{partnumber} = $new_item->{number};
116 delete $new_item->{number};
119 my $callback_sub = $form->{callback_sub};
121 restore_form($form->{old_form});
123 call_sub($callback_sub, $new_item);
125 $lxdebug->leave_sub();
128 sub part_selection_internal {
129 $lxdebug->enter_sub();
131 $order_by = "description";
132 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
134 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
138 foreach my $opt (split m/:/, $form->{options}) {
140 my ($key, $value) = split m/=/, $opt, 2;
141 $options{$key} = $value;
148 map { $form->{$_} = $options{$_} if ($options{$_}) } qw(no_services no_assemblies assemblies click_button);
150 $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
152 if (0 == scalar(@{$parts})) {
153 $form->show_generic_information($locale->text("No part was found matching the search parameters."));
154 } elsif (1 == scalar(@{$parts})) {
155 $onload = "part_selected('1')";
158 map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
160 my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
161 grep({ /^[fl]_/ } keys %{ $form }));
163 my @header_sort = qw(partnumber description);
164 my %header_title = ( "partnumber" => $locale->text("Part Number"),
165 "description" => $locale->text("Part description"),
169 map(+{ "column_title" => $header_title{$_},
171 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
175 $form->{formname} ||= 'Form';
177 $form->{title} = $locale->text("Select a part");
179 print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
181 "onload" => $onload });
183 $lxdebug->leave_sub();
186 # -------------------------------------------------------------------------
188 sub delivery_customer_selection {
189 $lxdebug->enter_sub();
192 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
194 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
196 $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
197 map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
198 if (0 == scalar(@{$delivery})) {
199 $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
200 } elsif (1 == scalar(@{$delivery})) {
201 $onload = "customer_selected('1')";
204 my $callback = "$form->{script}?action=delivery_customer_selection&";
205 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
206 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
208 my @header_sort = qw(name customernumber address);
209 my %header_title = ( "name" => $locale->text("Name"),
210 "customernumber" => $locale->text("Customer Number"),
211 "address" => $locale->text("Address"),
215 map(+{ "column_title" => $header_title{$_},
217 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
221 $form->{"title"} = $locale->text("Select a Customer");
223 print $form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
224 "DELIVERY" => $delivery,
225 "onload" => $onload });
227 $lxdebug->leave_sub();
230 # -------------------------------------------------------------------------
232 sub vendor_selection {
233 $lxdebug->enter_sub();
236 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
238 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
240 $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
241 map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
242 if (0 == scalar(@{$vendor})) {
243 $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
244 } elsif (1 == scalar(@{$vendor})) {
245 $onload = "vendor_selected('1')";
248 my $callback = "$form->{script}?action=vendor_selection&";
249 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
250 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
252 my @header_sort = qw(name customernumber address);
253 my %header_title = ( "name" => $locale->text("Name"),
254 "customernumber" => $locale->text("Customer Number"),
255 "address" => $locale->text("Address"),
259 map(+{ "column_title" => $header_title{$_},
261 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
265 $form->{"title"} = $locale->text("Select a Customer");
267 print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
269 "onload" => $onload });
271 $lxdebug->leave_sub();
274 # -------------------------------------------------------------------------
277 $lxdebug->enter_sub();
279 $form->{formel} =~ s/\r\n//g;
281 my ($variable_string, $formel) = split /###/,$form->{formel};
283 foreach $item (split m/;/, $variable_string) {
284 next unless $item =~ m/^ \s* (\w+) \s* = \s* (\w+) \s* (\w+) \s* $/x;
292 my @header_sort = qw(variable value unit);
294 variable => $locale->text("Variable"),
295 value => $locale->text("Value"),
296 unit => $locale->text("Unit"),
299 column_title => $header_title{$_},
303 $form->{formel} = $formel;
304 $form->{title} = $locale->text("Please enter values");
306 print $form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
307 "VARIABLES" => \@variable,
308 "onload" => $onload });
310 $lxdebug->leave_sub();
313 # -------------------------------------------------------------------------
315 sub set_longdescription {
316 $lxdebug->enter_sub();
318 $form->{title} = $locale->text("Enter longdescription");
320 print $form->parse_html_template("generic/set_longdescription");
322 $lxdebug->leave_sub();
325 # -------------------------------------------------------------------------
328 return $locale->quote_special_chars('HTML', $_[0]);
332 return $locale->quote_special_chars('URL@HTML', $_[0]);
336 return $form->escape($_[0]);
342 $element =~ s/tabindex\s*=\s*"\d+"//;
347 $lxdebug->enter_sub();
349 my ($dateformat, $longformat, @indices) = @_;
351 $dateformat = $myconfig{"dateformat"} unless ($dateformat);
353 foreach my $idx (@indices) {
354 next unless (defined($form->{$idx}));
356 if (!ref($form->{$idx})) {
357 $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
358 $dateformat, $longformat);
360 } elsif (ref($form->{$idx}) eq "ARRAY") {
361 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
362 $form->{$idx}->[$i] =
363 $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
364 $dateformat, $longformat);
367 if (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY") {
368 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
369 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] =
370 $locale->reformat_date(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i],
371 $dateformat, $longformat);
376 $lxdebug->leave_sub();
379 sub reformat_numbers {
380 $lxdebug->enter_sub();
382 my ($numberformat, $places, @indices) = @_;
384 return $lxdebug->leave_sub()
385 if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
387 foreach my $idx (@indices) {
388 next unless (defined($form->{$idx}));
390 if (!ref($form->{$idx})) {
391 $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
393 } elsif (ref($form->{$idx}) eq "ARRAY") {
394 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
395 $form->{$idx}->[$i] =
396 $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
401 my $saved_numberformat = $myconfig{"numberformat"};
402 $myconfig{"numberformat"} = $numberformat;
404 foreach my $idx (@indices) {
405 next unless (defined($form->{$idx}));
407 if (!ref($form->{$idx})) {
408 $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
410 } elsif (ref($form->{$idx}) eq "ARRAY") {
411 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
412 $form->{$idx}->[$i] =
413 $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
418 $myconfig{"numberformat"} = $saved_numberformat;
420 $lxdebug->leave_sub();
423 # -------------------------------------------------------------------------
426 $lxdebug->enter_sub();
427 my $dbh = $form->dbconnect(\%myconfig);
428 my ($sort, $sortby) = split(/\-\-/, $form->{order});
429 $sort =~ s/.*\.(.*)/$1/;
431 $form->{title} = $locale->text("History");
433 print $form->parse_html_template( "common/show_history", {
434 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
435 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
437 uc($sort)."BY" => $sortby
441 $lxdebug->leave_sub();
444 # -------------------------------------------------------------------------
447 $lxdebug->enter_sub();
452 $form->error($locale->text("Trying to call a sub without a name"));
455 $name =~ s/[^a-zA-Z0-9_]//g;
457 if (!defined(&{ $name })) {
458 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
463 $lxdebug->leave_sub();
466 # -------------------------------------------------------------------------
468 sub show_vc_details {
469 $lxdebug->enter_sub();
471 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
472 $form->isblank("vc_id",
473 $form->{vc} eq "customer" ?
474 $locale->text("No customer has been selected yet.") :
475 $locale->text("No vendor has been selected yet."));
477 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
479 $form->{title} = $form->{vc} eq "customer" ?
480 $locale->text("Customer details") : $locale->text("Vendor details");
482 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
484 $lxdebug->leave_sub();
487 # -------------------------------------------------------------------------
489 sub retrieve_partunits {
490 $lxdebug->enter_sub();
492 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
495 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
497 foreach my $i (1..$form->{rowcount}) {
498 next unless ($form->{"id_${i}"});
499 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
503 $lxdebug->leave_sub();
506 # -------------------------------------------------------------------------
508 sub mark_as_paid_common {
509 $lxdebug->enter_sub();
511 my ($myconfig, $db_name) = @_;
513 if($form->{mark_as_paid}) {
514 my $dbh ||= $form->get_standard_dbh($myconfig);
515 my $query = qq|UPDATE $db_name SET paid = amount WHERE id = ?|;
516 do_query($form, $dbh, $query, $form->{id});
518 $form->redirect($locale->text("Marked as paid"));
521 my $referer = $ENV{HTTP_REFERER};
522 if ($referer =~ /action/) {
523 $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
530 $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
533 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
534 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
535 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
536 print qq|</body></html>|;
539 $lxdebug->leave_sub();
542 sub cov_selection_internal {
543 $lxdebug->enter_sub();
546 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
548 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
550 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
552 $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
553 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
554 if (0 == scalar(@{$covs})) {
555 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
556 } elsif (1 == scalar(@{$covs})) {
557 $onload = "cov_selected('1')";
560 my $callback = "$form->{script}?action=cov_selection_internal&";
561 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
562 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
564 my @header_sort = qw(name address contact);
565 my %header_title = ( "name" => $locale->text("Name"),
566 "address" => $locale->text("Address"),
567 "contact" => $locale->text("Contact"),
571 map(+{ "column_title" => $header_title{$_},
573 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
577 foreach my $cov (@{ $covs }) {
578 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
579 $cov->{address} =~ s{^,}{}x;
580 $cov->{address} =~ s{\ +}{\ }gx;
582 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_gender cp_title cp_givenname cp_name);
583 $cov->{contact} =~ s{\ +}{\ }gx;
586 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
588 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
590 "onload" => $onload }));
592 $lxdebug->leave_sub();
596 # Functions to call add routines beneath different reports
599 $lxdebug->enter_sub();
601 $auth->assert('invoice_edit');
604 $form->{script} = 'is.pl';
606 $form->{type} = "invoice";
607 $locale = new Locale "$myconfig{countrycode}", "$script";
609 require "bin/mozilla/$form->{script}";
612 $lxdebug->leave_sub();
616 $lxdebug->enter_sub();
618 $auth->assert('general_ledger');
621 $form->{script} = 'ar.pl';
623 $locale = new Locale "$myconfig{countrycode}", "$script";
625 require "bin/mozilla/$form->{script}";
628 $lxdebug->leave_sub();
632 $lxdebug->enter_sub();
634 $auth->assert('invoice_edit');
637 $form->{script} = 'ir.pl';
639 $form->{type} = "invoice";
640 $locale = new Locale "$myconfig{countrycode}", "$script";
642 require "bin/mozilla/$form->{script}";
645 $lxdebug->leave_sub();
649 $lxdebug->enter_sub();
651 $auth->assert('general_ledger');
654 $form->{script} = 'ap.pl';
656 $locale = new Locale "$myconfig{countrycode}", "$script";
658 require "bin/mozilla/$form->{script}";
661 $lxdebug->leave_sub();
665 $lxdebug->enter_sub();
667 $auth->assert('general_ledger');
670 $form->{script} = 'gl.pl';
672 $locale = new Locale "$myconfig{countrycode}", "$script";
674 require "bin/mozilla/$form->{script}";
677 $lxdebug->leave_sub();