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);
173 if (0 == scalar(@{$parts})) {
174 $form->show_generic_information($locale->text("No part was found matching the search parameters."));
175 } elsif (1 == scalar(@{$parts})) {
176 $::request->{layout}->add_javascripts_inline("part_selected('1')");
179 map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
181 my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
182 grep({ /^[fl]_/ } keys %{ $form }));
184 my @header_sort = qw(partnumber description);
185 my %header_title = ( "partnumber" => $locale->text("Part Number"),
186 "description" => $locale->text("Part Description"),
190 map(+{ "column_title" => $header_title{$_},
192 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
196 $form->{formname} ||= 'Form';
198 $form->{title} = $locale->text("Select a part");
199 $form->header(no_layout => 1);
200 print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
201 "PARTS" => $parts, });
203 $main::lxdebug->leave_sub();
206 # -------------------------------------------------------------------------
208 sub delivery_customer_selection {
209 $main::lxdebug->enter_sub();
211 my $form = $main::form;
212 my %myconfig = %main::myconfig;
213 my $locale = $main::locale;
215 my $order_by = "name";
216 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
218 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
220 my $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
221 map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
223 if (0 == scalar(@{$delivery})) {
224 $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
225 } elsif (1 == scalar(@{$delivery})) {
226 $::request->{layout}->add_javascripts_inline("customer_selected('1')");
229 my $callback = "$form->{script}?action=delivery_customer_selection&";
230 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
231 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
233 my @header_sort = qw(name customernumber address);
234 my %header_title = ( "name" => $locale->text("Name"),
235 "customernumber" => $locale->text("Customer Number"),
236 "address" => $locale->text("Address"),
240 map(+{ "column_title" => $header_title{$_},
242 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
246 $form->{"title"} = $locale->text("Select a Customer");
247 $form->header(no_layout => 1);
248 print $form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
249 "DELIVERY" => $delivery, });
251 $main::lxdebug->leave_sub();
254 # -------------------------------------------------------------------------
256 sub vendor_selection {
257 $main::lxdebug->enter_sub();
259 my $form = $main::form;
260 my %myconfig = %main::myconfig;
261 my $locale = $main::locale;
263 my $order_by = "name";
264 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
266 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
268 my $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
269 map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
271 if (0 == scalar(@{$vendor})) {
272 $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
273 } elsif (1 == scalar(@{$vendor})) {
274 $::request->{layout}->add_javascripts_inline("vendor_selected('1')");
277 my $callback = "$form->{script}?action=vendor_selection&";
278 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
279 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
281 my @header_sort = qw(name customernumber address);
282 my %header_title = ( "name" => $locale->text("Name"),
283 "customernumber" => $locale->text("Customer Number"),
284 "address" => $locale->text("Address"),
288 map(+{ "column_title" => $header_title{$_},
290 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
294 $form->{"title"} = $locale->text("Select a Customer");
295 $form->header(no_layout => 1);
296 print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
297 "VENDOR" => $vendor, });
299 $main::lxdebug->leave_sub();
302 # -------------------------------------------------------------------------
305 $main::lxdebug->enter_sub();
307 my $form = $main::form;
308 my $locale = $main::locale;
310 $form->{formel} =~ s/\r\n//g;
312 my ($variable_string, $formel) = split /###/,$form->{formel};
315 foreach my $item (split m/;/, $variable_string) {
316 next unless $item =~ m/^ \s* (\w+) \s* = \s* (\w+) \s* (\w+) \s* $/x;
324 my @header_sort = qw(variable value unit);
326 variable => $locale->text("Variable"),
327 value => $locale->text("Value"),
328 unit => $locale->text("Unit"),
331 column_title => $header_title{$_},
335 $form->{formel} = $formel;
336 $form->{title} = $locale->text("Please enter values");
337 $form->header(no_layout => 1);
338 print $form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
339 "VARIABLES" => \@variable, });
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");
353 $form->header(no_layout => 1);
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 return $::form->format_dates(@_);
384 sub reformat_numbers {
385 return $::form->reformat_numbers(@_);
388 # -------------------------------------------------------------------------
391 $main::lxdebug->enter_sub();
393 my $form = $main::form;
394 my %myconfig = %main::myconfig;
395 my $locale = $main::locale;
397 my $dbh = $form->dbconnect(\%myconfig);
398 my ($sort, $sortby) = split(/\-\-/, $form->{order});
399 $sort =~ s/.*\.(.*)/$1/;
401 $form->{title} = $locale->text("History");
402 $form->header(no_layout => 1);
403 print $form->parse_html_template( "common/show_history", {
404 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
405 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
407 uc($sort)."BY" => $sortby,
411 $main::lxdebug->leave_sub();
414 # -------------------------------------------------------------------------
417 $main::lxdebug->enter_sub();
421 my $form = $main::form;
422 my $locale = $main::locale;
425 $form->error($locale->text("Trying to call a sub without a name"));
428 $name =~ s/[^a-zA-Z0-9_]//g;
430 if (!defined(&{ $name })) {
431 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
439 $main::lxdebug->leave_sub();
442 # -------------------------------------------------------------------------
444 sub show_vc_details {
445 $main::lxdebug->enter_sub();
447 my $form = $main::form;
448 my %myconfig = %main::myconfig;
449 my $locale = $main::locale;
451 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
452 $form->isblank("vc_id",
453 $form->{vc} eq "customer" ?
454 $locale->text("No customer has been selected yet.") :
455 $locale->text("No vendor has been selected yet."));
457 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
459 $form->{title} = $form->{vc} eq "customer" ?
460 $locale->text("Customer details") : $locale->text("Vendor details");
461 $form->header(no_layout => 1);
462 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
464 $main::lxdebug->leave_sub();
467 # -------------------------------------------------------------------------
469 sub retrieve_partunits {
470 $main::lxdebug->enter_sub();
472 my $form = $main::form;
474 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
477 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
479 foreach my $i (1..$form->{rowcount}) {
480 next unless ($form->{"id_${i}"});
481 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
485 $main::lxdebug->leave_sub();
488 # -------------------------------------------------------------------------
490 sub mark_as_paid_common {
491 $main::lxdebug->enter_sub();
493 my ($myconfig, $db_name) = @_;
495 my $form = $main::form;
496 my $locale = $main::locale;
498 if($form->{mark_as_paid}) {
499 my $dbh ||= $form->get_standard_dbh($myconfig);
500 my $query = qq|UPDATE $db_name SET paid = amount, datepaid = current_date WHERE id = ?|;
501 do_query($form, $dbh, $query, $form->{id});
503 $form->redirect($locale->text("Marked as paid"));
506 my $referer = $ENV{HTTP_REFERER};
509 if ($referer =~ /action/) {
510 $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
517 $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
519 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
520 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
521 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
524 $main::lxdebug->leave_sub();
527 sub cov_selection_internal {
528 $main::lxdebug->enter_sub();
530 my $form = $main::form;
531 my %myconfig = %main::myconfig;
532 my $locale = $main::locale;
534 my $order_by = "name";
535 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
537 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
539 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
541 my $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
542 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
544 if (0 == scalar(@{$covs})) {
545 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
546 } elsif (1 == scalar(@{$covs})) {
547 $::request->{layout}->add_javascripts_inline("cov_selected('1')");
550 my $callback = "$form->{script}?action=cov_selection_internal&";
551 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
552 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
554 my @header_sort = qw(name address contact);
555 my %header_title = ( "name" => $locale->text("Name"),
556 "address" => $locale->text("Address"),
557 "contact" => $locale->text("Contact"),
561 map(+{ "column_title" => $header_title{$_},
563 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
567 foreach my $cov (@{ $covs }) {
568 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
569 $cov->{address} =~ s{^,}{}x;
570 $cov->{address} =~ s{\ +}{\ }gx;
572 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_gender cp_title cp_givenname cp_name);
573 $cov->{contact} =~ s{\ +}{\ }gx;
576 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
578 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
579 "COVS" => $covs, }));
581 $main::lxdebug->leave_sub();
585 # Functions to call add routines beneath different reports
588 $main::lxdebug->enter_sub();
590 print $::form->redirect_header('is.pl?action=add&type=invoice');
592 $main::lxdebug->leave_sub();
596 $main::lxdebug->enter_sub();
598 print $::form->redirect_header('ar.pl?action=add');
600 $main::lxdebug->leave_sub();
604 $main::lxdebug->enter_sub();
606 print $::form->redirect_header('ir.pl?action=add&type=invoice');
608 $main::lxdebug->leave_sub();
612 $main::lxdebug->enter_sub();
614 print $::form->redirect_header('ap.pl?action=add');
616 $main::lxdebug->leave_sub();
620 $main::lxdebug->enter_sub();
622 print $::form->redirect_header('gl.pl?action=add');
624 $main::lxdebug->leave_sub();
628 goto &SL::DB::Helper::Mappings::db;