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;
15 use SL::DBUtils qw(do_query);
17 use SL::MoreCommon qw(restore_form save_form);
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 # -------------------------------------------------------------------------
347 return $main::locale->quote_special_chars('HTML', $_[0]);
351 return $main::locale->quote_special_chars('URL@HTML', $_[0]);
355 return $main::form->escape($_[0]);
361 $element =~ s/tabindex\s*=\s*"\d+"//;
366 return $::form->format_dates(@_);
369 sub reformat_numbers {
370 return $::form->reformat_numbers(@_);
373 # -------------------------------------------------------------------------
376 $main::lxdebug->enter_sub();
378 my $form = $main::form;
379 my %myconfig = %main::myconfig;
380 my $locale = $main::locale;
382 my $dbh = $form->dbconnect(\%myconfig);
383 my ($sort, $sortby) = split(/\-\-/, $form->{order});
384 $sort =~ s/.*\.(.*)/$1/;
386 $form->{title} = $locale->text("History");
387 $form->header(no_layout => 1);
389 my $callback = build_std_url(qw(action longdescription trans_id_type input_name));
391 if ( $form->{trans_id_type} eq 'glid' ) {
392 $restriction = "AND ( snumbers LIKE 'invnumber%' OR what_done LIKE '%Buchungsnummer%' OR snumbers LIKE 'gltransaction%' ) ";
393 } elsif ( $form->{trans_id_type} eq 'id' ) {
394 $restriction = " AND ( snumbers NOT LIKE 'invnumber_%' AND snumbers NOT LIKE 'gltransaction%' AND (what_done NOT LIKE '%Buchungsnummer%' OR what_done IS null))";
399 print $form->parse_html_template( "common/show_history", {
400 "DATEN" => $form->get_history($dbh,$form->{input_name},$restriction,$form->{order}),
401 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
403 uc($sort)."BY" => $sortby,
404 callback => $callback,
408 $main::lxdebug->leave_sub();
411 # -------------------------------------------------------------------------
414 $main::lxdebug->enter_sub();
418 my $form = $main::form;
419 my $locale = $main::locale;
422 $form->error($locale->text("Trying to call a sub without a name"));
425 $name =~ s/[^a-zA-Z0-9_]//g;
427 if (!defined(&{ $name })) {
428 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
436 $main::lxdebug->leave_sub();
439 # -------------------------------------------------------------------------
441 sub show_vc_details {
442 $main::lxdebug->enter_sub();
444 my $form = $main::form;
445 my %myconfig = %main::myconfig;
446 my $locale = $main::locale;
448 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
449 $form->isblank("vc_id",
450 $form->{vc} eq "customer" ?
451 $locale->text("No customer has been selected yet.") :
452 $locale->text("No vendor has been selected yet."));
454 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
455 $form->{discount_as_percent} = $form->format_amount(\%::myconfig, $form->parse_amount(\%::myconfig, $form->{discount}) * 100, 2);
457 $form->{title} = $form->{vc} eq "customer" ?
458 $locale->text("Customer details") : $locale->text("Vendor details");
459 $form->header(no_layout => 1);
460 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
462 $main::lxdebug->leave_sub();
465 # -------------------------------------------------------------------------
467 sub retrieve_partunits {
468 $main::lxdebug->enter_sub();
470 my $form = $main::form;
472 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
475 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
477 foreach my $i (1..$form->{rowcount}) {
478 next unless ($form->{"id_${i}"});
479 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
483 $main::lxdebug->leave_sub();
486 # -------------------------------------------------------------------------
488 sub mark_as_paid_common {
489 $main::lxdebug->enter_sub();
491 my ($myconfig, $db_name) = @_;
493 my $form = $main::form;
494 my $locale = $main::locale;
496 if($form->{mark_as_paid}) {
497 SL::DB->client->with_transaction(sub {
498 my $dbh ||= SL::DB->client->dbh;
499 my $query = qq|UPDATE $db_name SET paid = amount, datepaid = current_date WHERE id = ?|;
500 do_query($form, $dbh, $query, $form->{id});
502 }) or do { $::form->error(SL::DB->client->error) };
503 $form->redirect($locale->text("Marked as paid"));
506 my $referer = $ENV{HTTP_REFERER};
509 if ($referer =~ /action/) {
510 $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
513 } elsif ($referer =~ /RESTORE_FORM_FROM_SESSION_ID/){
514 $referer =~ /^(.*)\?RESTORE_FORM_FROM_SESSION_ID\=(.*)$/;
521 $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
523 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
524 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
525 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
528 $main::lxdebug->leave_sub();
531 sub cov_selection_internal {
532 $main::lxdebug->enter_sub();
534 my $form = $main::form;
535 my %myconfig = %main::myconfig;
536 my $locale = $main::locale;
538 my $order_by = "name";
539 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
541 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
543 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
545 my $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
546 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
548 if (0 == scalar(@{$covs})) {
549 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
550 } elsif (1 == scalar(@{$covs})) {
551 $::request->{layout}->add_javascripts_inline("cov_selected('1')");
554 my $callback = "$form->{script}?action=cov_selection_internal&";
555 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
556 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
558 my @header_sort = qw(name address contact);
559 my %header_title = ( "name" => $locale->text("Name"),
560 "address" => $locale->text("Address"),
561 "contact" => $locale->text("Contact"),
565 map(+{ "column_title" => $header_title{$_},
567 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
571 foreach my $cov (@{ $covs }) {
572 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
573 $cov->{address} =~ s{^,}{}x;
574 $cov->{address} =~ s{\ +}{\ }gx;
576 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_gender cp_title cp_givenname cp_name);
577 $cov->{contact} =~ s{\ +}{\ }gx;
580 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
582 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
583 "COVS" => $covs, }));
585 $main::lxdebug->leave_sub();
589 # Functions to call add routines beneath different reports
592 $main::lxdebug->enter_sub();
594 print $::form->redirect_header('is.pl?action=add&type=invoice');
596 $main::lxdebug->leave_sub();
600 $main::lxdebug->enter_sub();
602 print $::form->redirect_header('ar.pl?action=add');
604 $main::lxdebug->leave_sub();
608 $main::lxdebug->enter_sub();
610 print $::form->redirect_header('ir.pl?action=add&type=invoice');
612 $main::lxdebug->leave_sub();
616 $main::lxdebug->enter_sub();
618 print $::form->redirect_header('ap.pl?action=add');
620 $main::lxdebug->leave_sub();
624 $main::lxdebug->enter_sub();
626 print $::form->redirect_header('gl.pl?action=add');
628 $main::lxdebug->leave_sub();
632 goto &SL::DB::Helper::Mappings::db;