1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  16 # This program is free software; you can redistribute it and/or modify
 
  17 # it under the terms of the GNU General Public License as published by
 
  18 # the Free Software Foundation; either version 2 of the License, or
 
  19 # (at your option) any later version.
 
  21 # This program is distributed in the hope that it will be useful,
 
  22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  24 # GNU General Public License for more details.
 
  25 # You should have received a copy of the GNU General Public License
 
  26 # along with this program; if not, write to the Free Software
 
  27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  28 #======================================================================
 
  30 # common routines for gl, ar, ap, is, ir, oe
 
  35 # any custom scripts for this one
 
  36 if (-f "bin/mozilla/custom_arap.pl") {
 
  37   eval { require "bin/mozilla/custom_arap.pl"; };
 
  39 if (-f "bin/mozilla/$main::form->{login}_arap.pl") {
 
  40   eval { require "bin/mozilla/$main::form->{login}_arap.pl"; };
 
  45 require "bin/mozilla/common.pl";
 
  50   $main::lxdebug->enter_sub();
 
  52   my $form     = $main::form;
 
  53   my %myconfig = %main::myconfig;
 
  54   my $locale   = $main::locale;
 
  56   $main::auth->assert('general_ledger               | vendor_invoice_edit       | sales_order_edit    | invoice_edit |' .
 
  57                 'request_quotation_edit       | sales_quotation_edit      | purchase_order_edit | cash         |' .
 
  58                 'purchase_delivery_order_edit | sales_delivery_order_edit');
 
  60   my ($name, %params) = @_;
 
  62   $name = $name eq "customer" ? "customer" : "vendor";
 
  64   my ($new_name,$new_id) = $form->{$name} =~ /^(.*?)--(\d+)$/;
 
  66   # if we use a selection
 
  67   if ($form->{"select$name"}) {
 
  68     if ($form->{"old$name"} ne $form->{$name}) {
 
  70       # this is needed for is, ir and oe
 
  72       # for credit calculations
 
  73       $form->{oldinvtotal}  = 0;
 
  74       $form->{oldtotalpaid} = 0;
 
  77       $form->{"${name}_id"} = $new_id;
 
  80       IS->get_customer(\%myconfig, \%$form) if ($name eq 'customer');
 
  81       IR->get_vendor(\%myconfig, \%$form) if ($name eq 'vendor');
 
  83       $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
 
  89     # check name, combine name and id
 
  90     if ($form->{"old$name"} ne qq|$form->{$name}--$form->{"${name}_id"}|) {
 
  92       # this is needed for is, ir and oe
 
  95       # for credit calculations
 
  96       $form->{oldinvtotal}  = 0;
 
  97       $form->{oldtotalpaid} = 0;
 
 100       # return one name or a list of names in $form->{name_list}
 
 101       $i = $form->get_name(\%myconfig, $name);
 
 104         if ($params{no_select}) {
 
 105           # $locale->text('Customer')
 
 106           # $locale->text('Vendor')
 
 107           $form->error($locale->text("More than one #1 found matching, please be more specific.", $locale->text(ucfirst $name)));
 
 117         $form->{"${name}_id"} = $form->{name_list}[0]->{id};
 
 118         $form->{$name}        = $form->{name_list}[0]->{name};
 
 119         $form->{"old$name"}   = qq|$form->{$name}--$form->{"${name}_id"}|;
 
 121         _reset_salesman_id();
 
 122         IS->get_customer(\%myconfig, \%$form) if ($name eq 'customer');
 
 123         IR->get_vendor(\%myconfig, \%$form) if ($name eq 'vendor');
 
 127         # name is not on file
 
 128         # $locale->text('Customer not on file or locked!')
 
 129         # $locale->text('Vendor not on file or locked!')
 
 130         my $msg = ucfirst $name . " not on file or locked!";
 
 131         $form->error($locale->text($msg));
 
 135   $form->language_payment(\%myconfig);
 
 137   $main::lxdebug->leave_sub();
 
 142 # $locale->text('Customer not on file!')
 
 143 # $locale->text('Vendor not on file!')
 
 146   $main::lxdebug->enter_sub();
 
 148   my $form     = $main::form;
 
 149   my $locale   = $main::locale;
 
 151   $main::auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit | sales_delivery_order_edit |' .
 
 152                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash');
 
 156   my @column_index = qw(ndx name address);
 
 158   my $label             = ucfirst $table;
 
 160   $column_data{ndx}  = qq|<th> </th>|;
 
 162     qq|<th class=listheading>| . $locale->text($label) . qq|</th>|;
 
 163   $column_data{address} =
 
 164     qq|<th class=listheading>| . $locale->text('Address') . qq|</th>|;
 
 166   # list items with radio button on a form
 
 169   my $title = $locale->text('Select from one of the names below');
 
 172 <form method=post action=$form->{script}>
 
 176     <th class=listtop>$title</th>
 
 182         <tr class=listheading>|;
 
 184   map { print "\n$column_data{$_}" } @column_index;
 
 192   foreach my $ref (@{ $form->{name_list} }) {
 
 193     my $checked = ($i++) ? "" : "checked";
 
 195     $ref->{name} =~ s/\"/"/g;
 
 198       qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
 
 200       qq|<td><input name="new_name_$i" type=hidden value="$ref->{name}">$ref->{name}</td>|;
 
 201     $column_data{address} = qq|<td>$ref->{address} </td>|;
 
 206         <tr class=listrow$j>|;
 
 208     map { print "\n$column_data{$_}" } @column_index;
 
 213 <input name="new_id_$i" type=hidden value=$ref->{id}>
 
 224     <td><hr size=3 noshade></td>
 
 228 <input name=lastndx type=hidden value=$i>
 
 233   map { delete $form->{$_} } qw(action name_list header);
 
 235   # save all other form variables
 
 236   foreach my $key (keys %${form}) {
 
 237     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
 238     $form->{$key} =~ s/\"/"/g;
 
 239     print qq|<input name=$key type=hidden value="$form->{$key}">\n|;
 
 243 <input type=hidden name=nextsub value=name_selected>
 
 245 <input type=hidden name=vc value=$table>
 
 247 <input class=submit type=submit name=action value="|
 
 248     . $locale->text('Continue') . qq|">
 
 252   $main::lxdebug->leave_sub();
 
 256   $main::lxdebug->enter_sub();
 
 258   my $form     = $main::form;
 
 259   my %myconfig = %main::myconfig;
 
 261   $main::auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit | sales_delivery_order_edit | ' .
 
 262                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash');
 
 264   # replace the variable with the one checked
 
 267   my $i = $form->{ndx};
 
 269   _reset_salesman_id();
 
 271   $form->{ $form->{vc} }    = $form->{"new_name_$i"};
 
 272   $form->{"$form->{vc}_id"} = $form->{"new_id_$i"};
 
 273   $form->{"old$form->{vc}"} =
 
 274     qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
 
 276   # delete all the new_ variables
 
 277   for $i (1 .. $form->{lastndx}) {
 
 278     map { delete $form->{"new_${_}_$i"} } qw(id name);
 
 281   map { delete $form->{$_} } qw(ndx lastndx nextsub);
 
 283   IS->get_customer(\%myconfig, \%$form) if ($form->{vc} eq 'customer');
 
 284   IR->get_vendor(\%myconfig, \%$form) if ($form->{vc} eq 'vendor');
 
 288   $main::lxdebug->leave_sub();
 
 291 # Reset the $::form field 'salesman_id' to the ID of the currently
 
 292 # logged in user. Useful when changing to a customer/vendor that has
 
 293 # no salesman listed in their master data.
 
 294 sub _reset_salesman_id {
 
 295   my $current_employee   = SL::DB::Manager::Employee->current;
 
 296   $::form->{salesman_id} = $current_employee->id if $current_employee && exists $::form->{salesman_id};
 
 300   $::lxdebug->enter_sub;
 
 302   $::auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
 
 303                   'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash         | report');
 
 305   my ($is_global, $nextsub) = @_;
 
 306   my $project_list = delete $::form->{project_list};
 
 308   map { delete $::form->{$_} } qw(action header update);
 
 311   for my $key (keys %$::form) {
 
 312     next if $key eq 'login' || $key eq 'password' || '' ne ref $::form->{$key};
 
 313     push @hiddens, { key => $key, value => $::form->{$key} };
 
 315   push @hiddens, { key => 'is_global',                value => $is_global },
 
 316                  { key => 'project_selected_nextsub', value => $nextsub };
 
 319   print $::form->parse_html_template('arap/select_project', { hiddens => \@hiddens, project_list => $project_list });
 
 321   $::lxdebug->leave_sub;
 
 324 sub project_selected {
 
 325   $main::lxdebug->enter_sub();
 
 327   my $form     = $main::form;
 
 329   $main::auth->assert('general_ledger         | vendor_invoice_edit  | sales_order_edit    | invoice_edit |' .
 
 330                 'request_quotation_edit | sales_quotation_edit | purchase_order_edit | cash         | report');
 
 332   # replace the variable with the one checked
 
 335   my $i = $form->{ndx};
 
 337   my $prefix = $form->{"is_global"} ? "global" : "";
 
 338   my $suffix = $form->{"is_global"} ? "" : "_$form->{rownumber}";
 
 340   $form->{"${prefix}projectnumber${suffix}"} =
 
 341     $form->{"new_projectnumber_$i"};
 
 342   $form->{"old${prefix}projectnumber${suffix}"} =
 
 343     $form->{"new_projectnumber_$i"};
 
 344   $form->{"${prefix}project_id${suffix}"} = $form->{"new_id_$i"};
 
 346   # delete all the new_ variables
 
 347   for $i (1 .. $form->{lastndx}) {
 
 348     map { delete $form->{"new_${_}_$i"} } qw(id projectnumber description);
 
 351   my $nextsub = $form->{project_selected_nextsub} || 'update';
 
 353   map { delete $form->{$_} } qw(ndx lastndx nextsub is_global project_selected_nextsub);
 
 357   $main::lxdebug->leave_sub();
 
 360 sub continue       { call_sub($main::form->{"nextsub"}); }
 
 368 arap.pl - helper functions or customer/vendor retrieval
 
 376 Don't use anyting in this file without extreme care, and even then be prepared for massive headaches.
 
 378 It's a collection of helper routines that wrap the customer/vendor dropdown/textfield duality into something even complexer.
 
 382 =head2 check_name customer|vendor
 
 384 check_name was originally meant to update the selected customer or vendor. The
 
 385 way it does that has generted more hate than almost any other part of this
 
 394 It checks if a vendor or customer is given. No failsafe, vendor fallback if
 
 395 $_[0] is something fancy.
 
 399 It assumes, that there is a field named customer or vendor in $form.
 
 403 It assumes, that this field is filled with name--id, and tries to split that.
 
 404 sql ledger uses that combination to get ids into the select keys.
 
 408 It looks for a field selectcustomer or selectvendor in $form. sql ledger used
 
 409 to store a copy of the html select in there. (again, don't ask)
 
 413 If this field exists, it looks for a field called oldcustomer or oldvendor, in
 
 414 which the old name--id string was stored in sql ledger, and compares those.
 
 418 if they don't match, it will set customer_id or vendor_id in $form, load the
 
 419 entry (which will clobber everything in $form named like a column in customer
 
 420 oder vendor) and return.
 
 424 If there was no select* entry, it assumes that vclimit was lower than the
 
 425 number of entries, and that an input field was generated. In that case the
 
 426 splitting is omitted (since users don't generally include ids in entered names)
 
 430 It looks for a *_id field, and combines it with the given input into a name--id
 
 431 entry and compares it to the old* entry. (Missing any of these will instantly
 
 436 If those do not match, $form->get_name is called to get matching results.
 
 437 get_name only matches by *number and name, not by id, don't try to get it to do
 
 442 The results are stored in $form>{name_list} but a count is returned, and
 
 447 If only one result was found, *_id, * and old* are copied into $form, the entry
 
 448 is loaded (like above, clobbering)
 
 452 If there is more than one, a selection dialog is rendered
 
 456 If none is found, an error is generated.
 
 460 =head3 I built a customer/vendor box somewhere and it doesn't work, what's wrong?
 
 462 Make sure a select* field is given if and only if you render a select box. The
 
 463 actual contents are ignored, but recognition fails if not present.
 
 465 Make sure old* and *_id fields are set correctly (name--id form for old*). They
 
 466 are necessary in all steps and branches.
 
 468 Since get_customer and get_vendor clobber a lot of fields, make sure what
 
 471 =head3 select- version works fine, but things go awry when I use a textbox, any idea?
 
 473 If there is more than one match, check_name will display a select form, that
 
 474 will redirect to the original C<nextsub>. Unfortunately any hidden vars or
 
 475 input fields will be lost in the process unless saved before in a callback.
 
 477 If you still want to use it, you can disable this feature, like this:
 
 479   check_name('customer', no_select => 1)
 
 481 In that case multiple matches will trigger an error.
 
 483 Otherwise you'll have to care to include a complete state in callback.