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 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
355 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
356 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] =
357 $locale->reformat_date(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i],
358 $dateformat, $longformat);
362 next unless (defined($form->{$idx}));
364 if (!ref($form->{$idx})) {
365 $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
366 $dateformat, $longformat);
368 } elsif (ref($form->{$idx}) eq "ARRAY") {
369 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
370 $form->{$idx}->[$i] =
371 $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
372 $dateformat, $longformat);
377 $lxdebug->leave_sub();
380 sub reformat_numbers {
381 $lxdebug->enter_sub();
383 my ($numberformat, $places, @indices) = @_;
385 return $lxdebug->leave_sub()
386 if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
388 foreach my $idx (@indices) {
389 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
390 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
391 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->parse_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i]);
395 next unless (defined($form->{$idx}));
397 if (!ref($form->{$idx})) {
398 $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
400 } elsif (ref($form->{$idx}) eq "ARRAY") {
401 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
402 $form->{$idx}->[$i] =
403 $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
408 my $saved_numberformat = $myconfig{"numberformat"};
409 $myconfig{"numberformat"} = $numberformat;
411 foreach my $idx (@indices) {
412 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
413 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
414 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->format_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i], $places);
418 next unless (defined($form->{$idx}));
420 if (!ref($form->{$idx})) {
421 $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
423 } elsif (ref($form->{$idx}) eq "ARRAY") {
424 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
425 $form->{$idx}->[$i] =
426 $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
431 $myconfig{"numberformat"} = $saved_numberformat;
433 $lxdebug->leave_sub();
436 # -------------------------------------------------------------------------
439 $lxdebug->enter_sub();
440 my $dbh = $form->dbconnect(\%myconfig);
441 my ($sort, $sortby) = split(/\-\-/, $form->{order});
442 $sort =~ s/.*\.(.*)/$1/;
444 $form->{title} = $locale->text("History");
446 print $form->parse_html_template( "common/show_history", {
447 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
448 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
450 uc($sort)."BY" => $sortby
454 $lxdebug->leave_sub();
457 # -------------------------------------------------------------------------
460 $lxdebug->enter_sub();
465 $form->error($locale->text("Trying to call a sub without a name"));
468 $name =~ s/[^a-zA-Z0-9_]//g;
470 if (!defined(&{ $name })) {
471 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
476 $lxdebug->leave_sub();
479 # -------------------------------------------------------------------------
481 sub show_vc_details {
482 $lxdebug->enter_sub();
484 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
485 $form->isblank("vc_id",
486 $form->{vc} eq "customer" ?
487 $locale->text("No customer has been selected yet.") :
488 $locale->text("No vendor has been selected yet."));
490 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
492 $form->{title} = $form->{vc} eq "customer" ?
493 $locale->text("Customer details") : $locale->text("Vendor details");
495 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
497 $lxdebug->leave_sub();
500 # -------------------------------------------------------------------------
502 sub retrieve_partunits {
503 $lxdebug->enter_sub();
505 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
508 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
510 foreach my $i (1..$form->{rowcount}) {
511 next unless ($form->{"id_${i}"});
512 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
516 $lxdebug->leave_sub();
519 # -------------------------------------------------------------------------
521 sub mark_as_paid_common {
522 $lxdebug->enter_sub();
524 my ($myconfig, $db_name) = @_;
526 if($form->{mark_as_paid}) {
527 my $dbh ||= $form->get_standard_dbh($myconfig);
528 my $query = qq|UPDATE $db_name SET paid = amount WHERE id = ?|;
529 do_query($form, $dbh, $query, $form->{id});
531 $form->redirect($locale->text("Marked as paid"));
534 my $referer = $ENV{HTTP_REFERER};
535 if ($referer =~ /action/) {
536 $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
543 $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
546 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
547 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
548 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
549 print qq|</body></html>|;
552 $lxdebug->leave_sub();
555 sub cov_selection_internal {
556 $lxdebug->enter_sub();
559 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
561 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
563 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
565 $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
566 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
567 if (0 == scalar(@{$covs})) {
568 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
569 } elsif (1 == scalar(@{$covs})) {
570 $onload = "cov_selected('1')";
573 my $callback = "$form->{script}?action=cov_selection_internal&";
574 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
575 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
577 my @header_sort = qw(name address contact);
578 my %header_title = ( "name" => $locale->text("Name"),
579 "address" => $locale->text("Address"),
580 "contact" => $locale->text("Contact"),
584 map(+{ "column_title" => $header_title{$_},
586 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
590 foreach my $cov (@{ $covs }) {
591 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
592 $cov->{address} =~ s{^,}{}x;
593 $cov->{address} =~ s{\ +}{\ }gx;
595 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_gender cp_title cp_givenname cp_name);
596 $cov->{contact} =~ s{\ +}{\ }gx;
599 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
601 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
603 "onload" => $onload }));
605 $lxdebug->leave_sub();
609 # Functions to call add routines beneath different reports
612 $lxdebug->enter_sub();
614 $auth->assert('invoice_edit');
617 $form->{script} = 'is.pl';
619 $form->{type} = "invoice";
620 $locale = new Locale "$myconfig{countrycode}", "$script";
622 require "bin/mozilla/$form->{script}";
625 $lxdebug->leave_sub();
629 $lxdebug->enter_sub();
631 $auth->assert('general_ledger');
634 $form->{script} = 'ar.pl';
636 $locale = new Locale "$myconfig{countrycode}", "$script";
638 require "bin/mozilla/$form->{script}";
641 $lxdebug->leave_sub();
645 $lxdebug->enter_sub();
647 $auth->assert('invoice_edit');
650 $form->{script} = 'ir.pl';
652 $form->{type} = "invoice";
653 $locale = new Locale "$myconfig{countrycode}", "$script";
655 require "bin/mozilla/$form->{script}";
658 $lxdebug->leave_sub();
662 $lxdebug->enter_sub();
664 $auth->assert('general_ledger');
667 $form->{script} = 'ap.pl';
669 $locale = new Locale "$myconfig{countrycode}", "$script";
671 require "bin/mozilla/$form->{script}";
674 $lxdebug->leave_sub();
678 $lxdebug->enter_sub();
680 $auth->assert('general_ledger');
683 $form->{script} = 'gl.pl';
685 $locale = new Locale "$myconfig{countrycode}", "$script";
687 require "bin/mozilla/$form->{script}";
690 $lxdebug->leave_sub();