1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger, Accounting
 
   9 # Copyright (c) 1998-2003
 
  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 #======================================================================
 
  32 #======================================================================
 
  34 use POSIX qw(strftime);
 
  42 use SL::ReportGenerator;
 
  43 use List::Util qw(max reduce sum);
 
  45 require "bin/mozilla/io.pl";
 
  46 require "bin/mozilla/arap.pl";
 
  47 require "bin/mozilla/reportgenerator.pl";
 
  54 # $locale->text('Edit the purchase_order');
 
  55 # $locale->text('Edit the sales_order');
 
  56 # $locale->text('Edit the request_quotation');
 
  57 # $locale->text('Edit the sales_quotation');
 
  59 # $locale->text('Workflow purchase_order');
 
  60 # $locale->text('Workflow sales_order');
 
  61 # $locale->text('Workflow request_quotation');
 
  62 # $locale->text('Workflow sales_quotation');
 
  65   'sales_order'       => 'sales_order_edit',
 
  66   'purchase_order'    => 'purchase_order_edit',
 
  67   'request_quotation' => 'request_quotation_edit',
 
  68   'sales_quotation'   => 'sales_quotation_edit',
 
  72   my $right   = $oe_access_map->{$form->{type}};
 
  73   $right    ||= 'DOES_NOT_EXIST';
 
  75   $auth->assert($right);
 
  79   $lxdebug->enter_sub();
 
  85   if ($form->{type} eq 'purchase_order') {
 
  86     $form->{title}   = $action eq "edit" ?
 
  87       $locale->text('Edit Purchase Order') :
 
  88       $locale->text('Add Purchase Order');
 
  89     $form->{heading} = $locale->text('Purchase Order');
 
  90     $form->{vc}      = 'vendor';
 
  92   if ($form->{type} eq 'sales_order') {
 
  93     $form->{title}   = $action eq "edit" ?
 
  94       $locale->text('Edit Sales Order') :
 
  95       $locale->text('Add Sales Order');
 
  96     $form->{heading} = $locale->text('Sales Order');
 
  97     $form->{vc}      = 'customer';
 
  99   if ($form->{type} eq 'request_quotation') {
 
 100     $form->{title}   = $action eq "edit" ?
 
 101       $locale->text('Edit Request for Quotation') :
 
 102       $locale->text('Add Request for Quotation');
 
 103     $form->{heading} = $locale->text('Request for Quotation');
 
 104     $form->{vc}      = 'vendor';
 
 106   if ($form->{type} eq 'sales_quotation') {
 
 107     $form->{title}   = $action eq "edit" ?
 
 108       $locale->text('Edit Quotation') :
 
 109       $locale->text('Add Quotation');
 
 110     $form->{heading} = $locale->text('Quotation');
 
 111     $form->{vc}      = 'customer';
 
 114   $lxdebug->leave_sub();
 
 118   $lxdebug->enter_sub();
 
 125     "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
 
 126     unless $form->{callback};
 
 132   $lxdebug->leave_sub();
 
 136   $lxdebug->enter_sub();
 
 140   # show history button
 
 141   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 142   #/show hhistory button
 
 144   $form->{simple_save} = 0;
 
 146   set_headings("edit");
 
 148   # editing without stuff to edit? try adding it first
 
 149   if ($form->{rowcount} && !$form->{print_and_save}) {
 
 150     map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
 
 154       undef $form->{rowcount};
 
 156       $lxdebug->leave_sub();
 
 159   } elsif (!$form->{id}) {
 
 161     $lxdebug->leave_sub();
 
 165   if ($form->{print_and_save}) {
 
 166     $form->{action}   = "print";
 
 167     $form->{resubmit} = 1;
 
 168     $language_id = $form->{language_id};
 
 169     $printer_id = $form->{printer_id};
 
 172   set_headings("edit");
 
 176   $form->{rowcount} = 0;
 
 177   foreach $ref (@{ $form->{form_details} }) {
 
 179     map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
 
 184   if ($form->{print_and_save}) {
 
 185     $form->{language_id} = $language_id;
 
 186     $form->{printer_id} = $printer_id;
 
 191   $lxdebug->leave_sub();
 
 195   $lxdebug->enter_sub();
 
 199   # get customer/vendor
 
 200   $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
 
 202   # retrieve order/quotation
 
 203   $form->{webdav}   = $webdav;
 
 204   $form->{jsscript} = 1;
 
 206   my $editing = $form->{id};
 
 208   OE->retrieve(\%myconfig, \%$form);
 
 210   # if multiple rowcounts (== collective order) then check if the
 
 211   # there were more than one customer (in that case OE::retrieve removes
 
 212   # the content from the field)
 
 213   $form->error($locale->text('Collective Orders only work for orders from one customer!'))
 
 214     if          $form->{rowcount}  && $form->{type}     eq 'sales_order'
 
 215      && defined $form->{customer}  && $form->{customer} eq '';
 
 217   $form->{"$form->{vc}_id"} ||= $form->{"all_$form->{vc}"}->[0]->{id} if $form->{"all_$form->{vc}"};
 
 219   $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
 
 220   $form->{shipto} = 1 if $form->{id};
 
 222   # get customer / vendor
 
 223   IR->get_vendor(\%myconfig, \%$form)   if $form->{type} =~ /(purchase_order|request_quotation)/;
 
 224   IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
 
 226   $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
 
 227   $form->restore_vars(qw(taxincluded)) if $form->{id};
 
 228   $form->restore_vars(qw(salesman_id)) if $editing;
 
 229   $form->{forex}       = $form->{exchangerate};
 
 230   $form->{employee}    = "$form->{employee}--$form->{employee_id}";
 
 232   # build vendor/customer drop down comatibility... don't ask
 
 233   if (@{ $form->{"all_$form->{vc}"} }) {
 
 234     $form->{"select$form->{vc}"} = 1;
 
 235     $form->{$form->{vc}}         = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
 
 238   $form->{"old$form->{vc}"}  = $form->{$form->{vc}};
 
 239   $form->{"old$form->{vc}"} .= qq|--$form->{"$form->{vc}_id"}| unless ($form->{"old$form->{vc}"} =~ m/--\d+$/);
 
 241   $lxdebug->leave_sub();
 
 245   $lxdebug->enter_sub();
 
 249   $form->{formname} ||= $form->{type};
 
 251   # format discounts if values come from db. either as single id, or as a collective order
 
 252   my $format_discounts = $form->{id} || $form->{convert_from_oe_ids};
 
 254   for my $i (1 .. $form->{rowcount}) {
 
 255     $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
 
 256     $form->{"discount_$i"}  = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($format_discounts ? 100 : 1));
 
 257     $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
 
 258     $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"});
 
 261   $lxdebug->leave_sub();
 
 265   $lxdebug->enter_sub();
 
 270   # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
 
 273   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 275   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 276   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 278   # use JavaScript Calendar or not
 
 279   $form->{jsscript} = 1;
 
 281   # openclosed checkboxes
 
 283   push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
 
 284                         $form->{"delivered"} ? "checked" : "",  $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
 
 285   push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
 
 286                         $form->{"closed"}    ? "checked" : "",  $locale->text('Closed')    if $form->{id};
 
 287   $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
 
 290   my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
 
 292   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
 
 293   $form->get_lists("contacts"      => "ALL_CONTACTS",
 
 294                    "shipto"        => "ALL_SHIPTO",
 
 295                    "projects"      => { "key"      => "ALL_PROJECTS",
 
 297                                         "old_id"   => \@old_project_ids },
 
 298                    "employees"     => "ALL_EMPLOYEES",
 
 299                    "salesmen"      => "ALL_SALESMEN",
 
 300                    "taxzones"      => "ALL_TAXZONES",
 
 301                    "payments"      => "ALL_PAYMENTS",
 
 302                    "currencies"    => "ALL_CURRENCIES",
 
 303                    "departments"   => "ALL_DEPARTMENTS",
 
 304                    $vc             => { key   => "ALL_" . uc($vc),
 
 305                                         limit => $myconfig{vclimit} + 1 },
 
 306                    "price_factors" => "ALL_PRICE_FACTORS");
 
 309   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
 
 310   $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
 
 311   $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
 
 312   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
 315   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
 
 316   $TMPL_VAR{vclimit} = $myconfig{vclimit};
 
 317   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
 
 318   push @custom_hiddens, "$form->{vc}_id";
 
 319   push @custom_hiddens, "old$form->{vc}";
 
 320   push @custom_hiddens, "select$form->{vc}";
 
 322   # currencies and exchangerate
 
 323   @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
 
 324   %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
 
 325   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
 
 326   $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
 
 327   $TMPL_VAR{currencies}        = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
 
 328                                                       '-values' => \@values, '-labels' => \%labels)) if scalar @values;
 
 329   push @custom_hiddens, "forex";
 
 330   push @custom_hiddens, "exchangerate" if $form->{forex};
 
 333   $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
 
 334   $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
 
 337   $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
 
 339   push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
 
 341   $credittext = $locale->text('Credit Limit exceeded!!!');
 
 343   my $follow_up_vc                =  $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
 
 344   $follow_up_vc                   =~ s/--.*?//;
 
 345   $TMPL_VAR{follow_up_trans_info} =  ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
 
 348     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
 
 350     if (scalar @{ $follow_ups }) {
 
 351       $TMPL_VAR{num_follow_ups}     = scalar                    @{ $follow_ups };
 
 352       $TMPL_VAR{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
 
 356   $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
 
 357           : ($form->{resubmit})                                ? "document.oe.submit()"
 
 358           : ($creditwarning)                                   ? "alert('$credittext')"
 
 361   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
 
 362   $onload .= qq|;setupPoints('|.   $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
 
 363   $TMPL_VAR{onload} = $onload;
 
 365   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
 
 366   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 367   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
 
 371   $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
 
 372      qw(id action type vc formname media format proforma queued printed emailed
 
 373         title creditlimit creditremaining tradediscount business
 
 374         max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
 
 375         shiptocity shiptocountry shiptocontact shiptophone shiptofax
 
 376         shiptodepartment_1 shiptodepartment_2 shiptoemail
 
 377         message email subject cc bcc taxpart taxservice taxaccounts cursor_fokus),
 
 379         map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ];  # deleted: discount
 
 383      is_sales        => scalar ($form->{type} =~ /^sales_/),              # these vars are exported, so that the template
 
 384      is_order        => scalar ($form->{type} =~ /_order$/),              # may determine what to show
 
 385      is_sales_quo    => scalar ($form->{type} =~ /sales_quotation$/),
 
 386      is_req_quo      => scalar ($form->{type} =~ /request_quotation$/),
 
 387      is_sales_ord    => scalar ($form->{type} =~ /sales_order$/),
 
 388      is_pur_ord      => scalar ($form->{type} =~ /purchase_order$/),
 
 391   print $form->parse_html_template("oe/form_header", { %TMPL_VAR });
 
 393   $lxdebug->leave_sub();
 
 397   $lxdebug->enter_sub();
 
 401   $form->{invtotal} = $form->{invsubtotal};
 
 403   $rows    = max 2, $form->numtextrows($form->{notes}, 25, 8);
 
 404   $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
 
 405   $rows    = max $rows, $introws;
 
 407   $TMPL_VAR{notes}    = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>| . H($form->{notes}) . qq|</textarea>|;
 
 408   $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$introws cols=35 wrap=soft>| . H($form->{intnotes}) . qq|</textarea>|;
 
 410   if (!$form->{taxincluded}) {
 
 412     foreach $item (split / /, $form->{taxaccounts}) {
 
 413       if ($form->{"${item}_base"}) {
 
 414         $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
 
 415         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
 
 417         $TMPL_VAR{tax} .= qq|
 
 419                 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
 
 420                 <td align=right>$form->{"${item}_total"}</td>
 
 425 #    $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0); # template does this
 
 428     foreach $item (split / /, $form->{taxaccounts}) {
 
 429       if ($form->{"${item}_base"}) {
 
 430         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
 
 431         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
 
 432         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
 
 433         $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
 
 435         $TMPL_VAR{tax} .= qq|
 
 437                 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
 
 438                 <td align=right>$form->{"${item}_total"}</td>
 
 441                 <th align=right>Nettobetrag</th>
 
 442                 <td align=right>$form->{"${item}_netto"}</td>
 
 448   $form->{oldinvtotal} = $form->{invtotal};
 
 450   print $form->parse_html_template("oe/form_footer", {
 
 453      print_options   => print_options(inline => 1),
 
 454      label_edit      => $locale->text("Edit the $form->{type}"),
 
 455      label_workflow  => $locale->text("Workflow $form->{type}"),
 
 458   $lxdebug->leave_sub();
 
 462   $lxdebug->enter_sub();
 
 464   my ($recursive_call) = shift;
 
 468   set_headings($form->{"id"} ? "edit" : "add");
 
 470   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
 
 473   $payment_id = $form->{payment_id} if $form->{payment_id};
 
 475   &check_name($form->{vc});
 
 477   $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
 
 480   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
 
 481   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
 
 482   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 484   $exchangerate = $form->{exchangerate} || 1;
 
 486 ##################### process items ######################################
 
 488   $i = $form->{rowcount};
 
 489   if (   ($form->{"partnumber_$i"} eq "")
 
 490       && ($form->{"description_$i"} eq "")
 
 491       && ($form->{"partsgroup_$i"}  eq "")) {
 
 493     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 498     if ($form->{type} =~ /^sales/) {
 
 499       IS->retrieve_item(\%myconfig, \%$form);
 
 501       IR->retrieve_item(\%myconfig, \%$form);
 
 504     my $rows = scalar @{ $form->{item_list} };
 
 506     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
 
 509       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
 
 518         $sellprice             = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 519         $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
 
 520         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g }    qw(partnumber description unit);
 
 521         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 522         $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
 
 524         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 526         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 527         $decimalplaces = max 2, length $1;
 
 530           $form->{"sellprice_$i"} = $sellprice;
 
 532           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
 
 533           $form->{"sellprice_$i"} /= $exchangerate;   # if there is an exchange rate adjust sellprice
 
 536         $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
 
 537         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
 
 538         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
 
 539         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
 
 541         $form->{creditremaining} -= $amount;
 
 543         $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 544         $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
 546         # get pricegroups for parts
 
 547         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 549         # build up html code for prices_$i
 
 556       # ok, so this is a new part
 
 557       # ask if it is a part or service item
 
 559       if (   $form->{"partsgroup_$i"}
 
 560           && ($form->{"partsnumber_$i"} eq "")
 
 561           && ($form->{"description_$i"} eq "")) {
 
 563         $form->{"discount_$i"} = "";
 
 567         $form->{"id_$i"}   = 0;
 
 572 ##################### process items ######################################
 
 575   $lxdebug->leave_sub();
 
 579   $lxdebug->enter_sub();
 
 583   if ($form->{type} eq 'purchase_order') {
 
 584     $form->{title} = $locale->text('Purchase Orders');
 
 585     $form->{vc}    = 'vendor';
 
 586     $ordlabel      = $locale->text('Order Number');
 
 587     $ordnumber     = 'ordnumber';
 
 588     $employee      = $locale->text('Employee');
 
 591   if ($form->{type} eq 'request_quotation') {
 
 592     $form->{title} = $locale->text('Request for Quotations');
 
 593     $form->{vc}    = 'vendor';
 
 594     $ordlabel      = $locale->text('RFQ Number');
 
 595     $ordnumber     = 'quonumber';
 
 596     $employee      = $locale->text('Employee');
 
 599   if ($form->{type} eq 'sales_order') {
 
 600     $form->{title} = $locale->text('Sales Orders');
 
 601     $form->{vc}    = 'customer';
 
 602     $ordlabel      = $locale->text('Order Number');
 
 603     $ordnumber     = 'ordnumber';
 
 604     $employee      = $locale->text('Employee');
 
 607   if ($form->{type} eq 'sales_quotation') {
 
 608     $form->{title} = $locale->text('Quotations');
 
 609     $form->{vc}    = 'customer';
 
 610     $ordlabel      = $locale->text('Quotation Number');
 
 611     $ordnumber     = 'quonumber';
 
 612     $employee      = $locale->text('Employee');
 
 615   # setup vendor / customer selection
 
 616   $form->all_vc(\%myconfig, $form->{vc},
 
 617                 ($form->{vc} eq 'customer') ? "AR" : "AP");
 
 620   if (@{ $form->{all_departments} }) {
 
 621     $form->{selectdepartment} = "<option>\n";
 
 624       $form->{selectdepartment} .=
 
 625         "<option>$_->{description}--$_->{id}</option>\n"
 
 626     } (@{ $form->{all_departments} });
 
 631           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
 
 632           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
 
 634 | if $form->{selectdepartment};
 
 637   if (($form->{"type"} eq "sales_order") ||
 
 638       ($form->{"type"} eq "purchase_order")) {
 
 641           <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
 
 642             <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
 
 643           <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
 
 644             <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
 
 649   # use JavaScript Calendar or not
 
 650   $form->{jsscript} = 1;
 
 654      <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
 
 655      <input type=button name=transdatefrom id="trigger3" value=|
 
 656     . $locale->text('button') . qq|></td>
 
 659      <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
 
 660      <input type=button name=transdateto name=transdateto id="trigger4" value=|
 
 661     . $locale->text('button') . qq|></td>
 
 664      <td><input name=reqdatefrom id=reqdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
 
 665      <input type=button name=reqdatefrom id="trigger5" value=|
 
 666     . $locale->text('button') . qq|></td>
 
 669      <td><input name=reqdateto id=reqdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
 
 670      <input type=button name=reqdateto name=reqdateto id="trigger6" value=|
 
 671     . $locale->text('button') . qq|></td>
 
 676    Form->write_trigger(\%myconfig, "4", "transdatefrom", "BR", "trigger3",
 
 677                        "transdateto", "BL", "trigger4",
 
 678                        "reqdatefrom", "BR", "trigger5", "reqdateto", "BL", "trigger6");
 
 680   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
 
 682   $form->get_lists("projects"  => { "key" => "ALL_PROJECTS",
 
 684                    "employees" => "ALL_EMPLOYEES",
 
 685                    "salesmen"  => "ALL_SALESMEN",
 
 686                    $vc         => "ALL_" . uc($vc)
 
 691   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
 
 692     push(@values, $item->{"id"});
 
 693     $labels{$item->{"id"}} = $item->{"projectnumber"};
 
 696     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
 
 697                          '-labels' => \%labels));
 
 702   foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
 
 703     push(@values, $item->{"id"});
 
 704     $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
 
 708   my %labels_salesmen = ();
 
 709   my @values_salesmen = ('');
 
 710   foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
 
 711     push(@values_salesmen, $item->{"id"});
 
 712     $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
 
 715   my $employee_block = qq|
 
 717       <th align="right">| . $locale->text('Employee') . qq|</th>
 
 718       <td>| .  NTI($cgi->popup_menu('-name'   => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
 
 721       <th align="right">| . $locale->text('Salesman') . qq|</th>
 
 723         NTI($cgi->popup_menu('-name'   => 'salesman_id',
 
 724                              '-values' => \@values_salesmen,
 
 725                              '-labels' => \%labels_salesmen)) . qq|
 
 732   foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
 
 733     push(@values, $item->{name}.qq|--|.$item->{"id"});
 
 734     $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
 
 737   my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
 
 739     $myconfig{vclimit} <=  scalar(@values)
 
 740     ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">|
 
 741     : NTI($cgi->popup_menu('-name' => "$form->{vc}",
 
 742                            '-default' => $form->{"old$form->{vc}"},
 
 743                            '-onChange' => 'document.getElementById(\'update_button\').click();',
 
 744                            '-values' => \@values,
 
 745                            '-labels' => \%labels));
 
 751 <form method=post action=$form->{script}>
 
 755     <th class=listtop>$form->{title}</th>
 
 762           <th align=right>$vc_label</th>
 
 763           <td colspan=3>$vc</td>
 
 767           <th align=right>$ordlabel</th>
 
 768           <td colspan=3><input name="$ordnumber" size=20></td>
 
 772           <th align="right">| . $locale->text('Transaction description') . qq|</th>
 
 773           <td colspan="3"><input name="transaction_description" size=20></td>
 
 776           <th align="right">| . $locale->text("Project Number") . qq|</th>
 
 777           <td colspan="3">$projectnumber</td>
 
 780           <th align=right>| . $locale->text('Order Date') . " " . $locale->text('From') . qq|</th> $button1
 
 781           <th align=right>| . $locale->text('Bis') . qq|</th> $button2
 
 783         <input type=hidden name=sort value=transdate>
 
 785           <th align=right>| . $locale->text('Delivery Date') . " " . $locale->text('From') . qq|</th> $button3
 
 786           <th align=right>| . $locale->text('Bis') . qq|</th> $button4
 
 788         <input type=hidden name=sort value=reqdate>
 
 790           <th align=right>| . $locale->text('Include in Report') . qq|</th>
 
 794           <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
 
 795           <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
 
 799                 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
 
 800                 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
 
 803                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
 
 804                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
 
 807                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
 
 808                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
 
 810                 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
 
 813                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
 
 814                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
 
 815                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
 
 818                 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | .             $locale->text('Ertrag') . qq|</td>
 
 819                 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | .             $locale->text('Ertrag prozentual') . qq|</td>
 
 822           <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
 
 823           <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
 
 826                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
 
 827                 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
 
 835   <tr><td colspan=4><hr size=3 noshade></td></tr>
 
 841 <input type=hidden name=nextsub value=orders>
 
 842 <input type=hidden name=vc value=$form->{vc}>
 
 843 <input type=hidden name=type value=$form->{type}>
 
 845 <input class=submit type=submit name=action value="|
 
 846     . $locale->text('Continue') . qq|">
 
 853   $lxdebug->leave_sub();
 
 856 sub create_subtotal_row {
 
 857   $lxdebug->enter_sub();
 
 859   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 861   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 863   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 865   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 867   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 869   $lxdebug->leave_sub();
 
 875   $lxdebug->enter_sub();
 
 879   $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
 
 881   ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
 
 883   report_generator_set_default_sort('transdate', 1);
 
 885   OE->transactions(\%myconfig, \%$form);
 
 887   $form->{rowcount} = scalar @{ $form->{OE} };
 
 890     "transdate",               "reqdate",
 
 896     "shipvia",                 "globalprojectnumber",
 
 897     "transaction_description", "open",
 
 898     "delivered", "marge_total", "marge_percent"
 
 901   # only show checkboxes if gotten here via sales_order form.
 
 902   my $allow_multiple_orders = $form->{type} eq 'sales_order';
 
 903   if ($allow_multiple_orders) {
 
 904     unshift @columns, "ids";
 
 907   $form->{l_open}      = $form->{l_closed} = "Y" if ($form->{open}      && $form->{closed});
 
 908   $form->{l_delivered} = "Y"                     if ($form->{delivered} && $form->{notdelivered});
 
 910   my $attachment_basename;
 
 911   if ($form->{vc} eq 'vendor') {
 
 912     if ($form->{type} eq 'purchase_order') {
 
 913       $form->{title}       = $locale->text('Purchase Orders');
 
 914       $attachment_basename = $locale->text('purchase_order_list');
 
 916       $form->{title}       = $locale->text('Request for Quotations');
 
 917       $attachment_basename = $locale->text('rfq_list');
 
 921     if ($form->{type} eq 'sales_order') {
 
 922       $form->{title}       = $locale->text('Sales Orders');
 
 923       $attachment_basename = $locale->text('sales_order_list');
 
 925       $form->{title}       = $locale->text('Quotations');
 
 926       $attachment_basename = $locale->text('quotation_list');
 
 930   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 932   my @hidden_variables = map { "l_${_}" } @columns;
 
 933   push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
 
 934                                                         transaction_description transdatefrom transdateto type vc employee_id salesman_id
 
 935                                                         reqdatefrom reqdateto);
 
 937   my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
 
 940     'ids'                     => { 'text' => '', },
 
 941     'transdate'               => { 'text' => $locale->text('Date'), },
 
 942     'reqdate'                 => { 'text' => $locale->text('Required by'), },
 
 943     'id'                      => { 'text' => $locale->text('ID'), },
 
 944     'ordnumber'               => { 'text' => $locale->text('Order'), },
 
 945     'quonumber'               => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
 
 946     'name'                    => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
 947     'netamount'               => { 'text' => $locale->text('Amount'), },
 
 948     'tax'                     => { 'text' => $locale->text('Tax'), },
 
 949     'amount'                  => { 'text' => $locale->text('Total'), },
 
 950     'curr'                    => { 'text' => $locale->text('Curr'), },
 
 951     'employee'                => { 'text' => $locale->text('Employee'), },
 
 952     'salesman'                => { 'text' => $locale->text('Salesman'), },
 
 953     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
 
 954     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
 
 955     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
 
 956     'open'                    => { 'text' => $locale->text('Open'), },
 
 957     'delivered'               => { 'text' => $locale->text('Delivered'), },
 
 958     'marge_total'             => { 'text' => $locale->text('Ertrag'), },
 
 959     'marge_percent'           => { 'text' => $locale->text('Ertrag prozentual'), }
 
 962   foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia transaction_description)) {
 
 963     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
 964     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
 
 967   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
 
 969   $form->{"l_type"} = "Y";
 
 970   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 971   $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
 
 973   $report->set_columns(%column_defs);
 
 974   $report->set_column_order(@columns);
 
 975   $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
 
 976   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 979   my ($department) = split m/--/, $form->{department};
 
 981   push @options, $locale->text('Customer')                . " : $form->{customer}"                        if $form->{customer};
 
 982   push @options, $locale->text('Vendor')                  . " : $form->{vendor}"                          if $form->{vendor};
 
 983   push @options, $locale->text('Department')              . " : $department"                              if $form->{department};
 
 984   push @options, $locale->text('Order Number')            . " : $form->{ordnumber}"                       if $form->{ordnumber};
 
 985   push @options, $locale->text('Notes')                   . " : $form->{notes}"                           if $form->{notes};
 
 986   push @options, $locale->text('Transaction description') . " : $form->{transaction_description}"         if $form->{transaction_description};
 
 987   if ( $form->{transdatefrom} or $form->{transdateto} ) {
 
 988     push @options, $locale->text('Order Date');
 
 989     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1)     if $form->{transdatefrom};
 
 990     push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{transdateto},   1)     if $form->{transdateto};
 
 992   if ( $form->{reqdatefrom} or $form->{reqdateto} ) {
 
 993     push @options, $locale->text('Delivery Date');
 
 994     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{reqdatefrom}, 1)       if $form->{reqdatefrom};
 
 995     push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{reqdateto},   1)       if $form->{reqdateto};
 
 997   push @options, $locale->text('Open')                                                                    if $form->{open};
 
 998   push @options, $locale->text('Closed')                                                                  if $form->{closed};
 
 999   push @options, $locale->text('Delivered')                                                               if $form->{delivered};
 
1000   push @options, $locale->text('Not delivered')                                                           if $form->{notdelivered};
 
1002   $report->set_options('top_info_text'        => join("\n", @options),
 
1003                        'raw_top_info_text'    => $form->parse_html_template('oe/orders_top'),
 
1004                        'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
 
1005                        'output_format'        => 'HTML',
 
1006                        'title'                => $form->{title},
 
1007                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1009   $report->set_options_from_form();
 
1011   # add sort and escape callback, this one we use for the add sub
 
1012   $form->{callback} = $href .= "&sort=$form->{sort}";
 
1014   # escape callback for href
 
1015   $callback = $form->escape($href);
 
1017   my @subtotal_columns = qw(netamount amount marge_total marge_percent);
 
1019   my %totals    = map { $_ => 0 } @subtotal_columns;
 
1020   my %subtotals = map { $_ => 0 } @subtotal_columns;
 
1024   my $edit_url = build_std_url('action=edit', 'type', 'vc');
 
1026   foreach $oe (@{ $form->{OE} }) {
 
1027     map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
 
1029     $oe->{tax}       = $oe->{amount} - $oe->{netamount};
 
1030     $oe->{open}      = $oe->{closed}    ? $locale->text('No')  : $locale->text('Yes');
 
1031     $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
 
1033     map { $subtotals{$_} += $oe->{$_};
 
1034           $totals{$_}    += $oe->{$_} } @subtotal_columns;
 
1036     $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
 
1037     $totals{marge_percent}    = $totals{netamount}    ? ($totals{marge_total}    * 100 / $totals{netamount}   ) : 0;
 
1039     map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
 
1043     foreach my $column (@columns) {
 
1044       next if ($column eq 'ids');
 
1046         'data'  => $oe->{$column},
 
1047         'align' => $column_alignment{$column},
 
1052       'raw_data' =>   $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
 
1053                     . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
 
1054       'valign'   => 'center',
 
1055       'align'    => 'center',
 
1058     $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
 
1060     my $row_set = [ $row ];
 
1062     if (($form->{l_subtotal} eq 'Y')
 
1063         && (($idx == (scalar @{ $form->{OE} } - 1))
 
1064             || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
 
1065       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
 
1068     $report->add_data($row_set);
 
1073   $report->add_separator();
 
1074   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
1076   $report->generate_with_headers();
 
1078   $lxdebug->leave_sub();
 
1081 sub check_delivered_flag {
 
1082   $lxdebug->enter_sub();
 
1086   if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
 
1087     return $lxdebug->leave_sub();
 
1090   my $all_delivered = 0;
 
1092   foreach my $i (1 .. $form->{rowcount}) {
 
1093     next if (!$form->{"id_$i"});
 
1095     if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
 
1104   $form->{delivered} = 1 if $all_delivered;
 
1106   $lxdebug->leave_sub();
 
1109 sub save_and_close {
 
1110   $lxdebug->enter_sub();
 
1114   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
1116   if ($form->{type} =~ /_order$/) {
 
1117     $form->isblank("transdate", $locale->text('Order Date missing!'));
 
1119     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
 
1122   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
 
1123   $form->{$idx} =~ s/^\s*//g;
 
1124   $form->{$idx} =~ s/\s*$//g;
 
1126   $msg = ucfirst $form->{vc};
 
1127   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
 
1129   # $locale->text('Customer missing!');
 
1130   # $locale->text('Vendor missing!');
 
1132   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
1133     if ($form->{currency} ne $form->{defaultcurrency});
 
1137   if($form->{payment_id}) {
 
1138     $payment_id = $form->{payment_id};
 
1141   # if the name changed get new values
 
1142   if (&check_name($form->{vc})) {
 
1143     if($form->{payment_id} eq "") {
 
1144       $form->{payment_id} = $payment_id;
 
1150   $form->{id} = 0 if $form->{saveasnew};
 
1152   # this is for the internal notes section for the [email] Subject
 
1153   if ($form->{type} =~ /_order$/) {
 
1154     if ($form->{type} eq 'sales_order') {
 
1155       $form->{label} = $locale->text('Sales Order');
 
1157       $numberfld = "sonumber";
 
1158       $ordnumber = "ordnumber";
 
1160       $form->{label} = $locale->text('Purchase Order');
 
1162       $numberfld = "ponumber";
 
1163       $ordnumber = "ordnumber";
 
1166     $err = $locale->text('Cannot save order!');
 
1168     check_delivered_flag();
 
1171     if ($form->{type} eq 'sales_quotation') {
 
1172       $form->{label} = $locale->text('Quotation');
 
1174       $numberfld = "sqnumber";
 
1175       $ordnumber = "quonumber";
 
1177       $form->{label} = $locale->text('Request for Quotation');
 
1179       $numberfld = "rfqnumber";
 
1180       $ordnumber = "quonumber";
 
1183     $err = $locale->text('Cannot save quotation!');
 
1187   # get new number in sequence if no number is given or if saveasnew was requested
 
1188   if (!$form->{$ordnumber} || $form->{saveasnew}) {
 
1189     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
 
1194   $form->error($err) if (!OE->save(\%myconfig, \%$form));
 
1196   # saving the history
 
1197   if(!exists $form->{addition}) {
 
1198     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1199         $form->{addition} = "SAVED";
 
1200         $form->save_history($form->dbconnect(\%myconfig));
 
1202   # /saving the history
 
1204   $form->redirect($form->{label} . " $form->{$ordnumber} " .
 
1205                   $locale->text('saved!'));
 
1207   $lxdebug->leave_sub();
 
1211   $lxdebug->enter_sub();
 
1215   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
1218   if ($form->{type} =~ /_order$/) {
 
1219     $form->isblank("transdate", $locale->text('Order Date missing!'));
 
1221     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
 
1224   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
 
1225   $form->{$idx} =~ s/^\s*//g;
 
1226   $form->{$idx} =~ s/\s*$//g;
 
1228   $msg = ucfirst $form->{vc};
 
1229   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
 
1231   # $locale->text('Customer missing!');
 
1232   # $locale->text('Vendor missing!');
 
1234   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
1235     if ($form->{currency} ne $form->{defaultcurrency});
 
1239   if($form->{payment_id}) {
 
1240     $payment_id = $form->{payment_id};
 
1243   # if the name changed get new values
 
1244   if (&check_name($form->{vc})) {
 
1245     if($form->{payment_id} eq "") {
 
1246       $form->{payment_id} = $payment_id;
 
1252   $form->{id} = 0 if $form->{saveasnew};
 
1254   # this is for the internal notes section for the [email] Subject
 
1255   if ($form->{type} =~ /_order$/) {
 
1256     if ($form->{type} eq 'sales_order') {
 
1257       $form->{label} = $locale->text('Sales Order');
 
1259       $numberfld = "sonumber";
 
1260       $ordnumber = "ordnumber";
 
1262       $form->{label} = $locale->text('Purchase Order');
 
1264       $numberfld = "ponumber";
 
1265       $ordnumber = "ordnumber";
 
1268     $err = $locale->text('Cannot save order!');
 
1270     check_delivered_flag();
 
1273     if ($form->{type} eq 'sales_quotation') {
 
1274       $form->{label} = $locale->text('Quotation');
 
1276       $numberfld = "sqnumber";
 
1277       $ordnumber = "quonumber";
 
1279       $form->{label} = $locale->text('Request for Quotation');
 
1281       $numberfld = "rfqnumber";
 
1282       $ordnumber = "quonumber";
 
1285     $err = $locale->text('Cannot save quotation!');
 
1289   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
 
1290     unless $form->{$ordnumber};
 
1294   OE->save(\%myconfig, \%$form);
 
1296   # saving the history
 
1297   if(!exists $form->{addition}) {
 
1298     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1299         $form->{addition} = "SAVED";
 
1300         $form->save_history($form->dbconnect(\%myconfig));
 
1302   # /saving the history
 
1304   $form->{simple_save} = 1;
 
1305   if(!$form->{print_and_save}) {
 
1306     set_headings("edit");
 
1310   $lxdebug->leave_sub();
 
1314   $lxdebug->enter_sub();
 
1320   if ($form->{type} =~ /_order$/) {
 
1321     $msg       = $locale->text('Are you sure you want to delete Order Number');
 
1322     $ordnumber = 'ordnumber';
 
1324     $msg = $locale->text('Are you sure you want to delete Quotation Number');
 
1325     $ordnumber = 'quonumber';
 
1331 <form method=post action=$form->{script}>
 
1334   # delete action variable
 
1335   map { delete $form->{$_} } qw(action header);
 
1337   foreach $key (keys %$form) {
 
1338     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1339     $form->{$key} =~ s/\"/"/g;
 
1340     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
1344 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
 
1346 <h4>$msg $form->{$ordnumber}</h4>
 
1348 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
 
1349 <input name=action class=submit type=submit value="|
 
1350     . $locale->text('Yes') . qq|">
 
1351 <button class=submit type=button onclick="history.back()">|
 
1352     . $locale->text('No') . qq|</button>
 
1359   $lxdebug->leave_sub();
 
1362 sub delete_order_quotation {
 
1363   $lxdebug->enter_sub();
 
1367   if ($form->{type} =~ /_order$/) {
 
1368     $msg = $locale->text('Order deleted!');
 
1369     $err = $locale->text('Cannot delete order!');
 
1371     $msg = $locale->text('Quotation deleted!');
 
1372     $err = $locale->text('Cannot delete quotation!');
 
1374   if (OE->delete(\%myconfig, \%$form, $spool)){
 
1375     # saving the history
 
1376     if(!exists $form->{addition}) {
 
1377       $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1378           $form->{addition} = "DELETED";
 
1379           $form->save_history($form->dbconnect(\%myconfig));
 
1381     # /saving the history
 
1387   $lxdebug->leave_sub();
 
1391   $lxdebug->enter_sub();
 
1394   $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
 
1396   $form->{old_employee_id} = $form->{employee_id};
 
1397   $form->{old_salesman_id} = $form->{salesman_id};
 
1399   if ($form->{type} =~ /_order$/) {
 
1401     # these checks only apply if the items don't bring their own ordnumbers/transdates.
 
1402     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
 
1403     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
 
1404       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
 
1405     $form->isblank("transdate", $locale->text('Order Date missing!'))
 
1406       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
 
1408     # also copy deliverydate from the order
 
1409     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
 
1410     $form->{orddate}      = $form->{transdate};
 
1412     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
 
1413     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
 
1414     $form->{ordnumber}    = "";
 
1415     $form->{quodate}      = $form->{transdate};
 
1418   $payment_id = $form->{payment_id} if $form->{payment_id};
 
1420   # if the name changed get new values
 
1421   if (&check_name($form->{vc})) {
 
1422     $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
 
1427   $form->{cp_id} *= 1;
 
1429   for $i (1 .. $form->{rowcount}) {
 
1430     for (qw(ship qty sellprice listprice basefactor)) {
 
1431       $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
 
1435   if (   $form->{type} =~ /_order/
 
1436       && $form->{currency} ne $form->{defaultcurrency}) {
 
1438     # check if we need a new exchangerate
 
1439     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
 
1441     $orddate      = $form->current_date(\%myconfig);
 
1442     $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
 
1444     if (!$exchangerate) {
 
1445       &backorder_exchangerate($orddate, $buysell);
 
1450   $form->{convert_from_oe_ids} = $form->{id};
 
1451   $form->{transdate}           = $form->{invdate} = $form->current_date(\%myconfig);
 
1452   $form->{duedate}             = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
 
1453   $form->{shipto}              = 1;
 
1454   $form->{defaultcurrency}     = $form->get_default_currency(\%myconfig);
 
1456   delete @{$form}{qw(id closed)};
 
1457   $form->{rowcount}--;
 
1459   if ($form->{type} =~ /_order$/) {
 
1460     $form->{exchangerate} = $exchangerate;
 
1464   if (   $form->{type} eq 'purchase_order'
 
1465       || $form->{type} eq 'request_quotation') {
 
1466     $form->{title}  = $locale->text('Add Vendor Invoice');
 
1467     $form->{script} = 'ir.pl';
 
1472   if (   $form->{type} eq 'sales_order'
 
1473       || $form->{type} eq 'sales_quotation') {
 
1474     $form->{title}  = $locale->text('Add Sales Invoice');
 
1475     $form->{script} = 'is.pl';
 
1480   # bo creates the id, reset it
 
1481   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
 
1482   $form->{ $form->{vc} } =~ s/--.*//g;
 
1483   $form->{type} = "invoice";
 
1486   $locale = new Locale "$myconfig{countrycode}", "$script";
 
1488   require "bin/mozilla/$form->{script}";
 
1490   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
 
1492   $currency = $form->{currency};
 
1495   $form->{currency}     = $currency;
 
1496   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
 
1497   $form->{exchangerate} = $form->{forex} || '';
 
1499   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
 
1504   for $i (1 .. $form->{rowcount}) {
 
1505     $form->{"discount_$i"} =
 
1506       $form->format_amount(\%myconfig, $form->{"discount_$i"});
 
1508     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
1510     $decimalplaces = ($dec > 2) ? $dec : 2;
 
1512     # copy delivery date from reqdate for order -> invoice conversion
 
1513     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
 
1514       unless $form->{"deliverydate_$i"};
 
1516     $form->{"sellprice_$i"} =
 
1517       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
 
1520     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
1521     $dec_qty = length $dec_qty;
 
1523       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
1525     map { $form->{"${_}_$i"} =~ s/\"/"/g }
 
1526       qw(partnumber description unit);
 
1532   $lxdebug->leave_sub();
 
1535 sub backorder_exchangerate {
 
1536   $lxdebug->enter_sub();
 
1540   my ($orddate, $buysell) = @_;
 
1547 <form method=post action=$form->{script}>
 
1550   # delete action variable
 
1551   map { delete $form->{$_} } qw(action header exchangerate);
 
1553   foreach $key (keys %$form) {
 
1554     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1555     $form->{$key} =~ s/\"/"/g;
 
1556     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
1559   $form->{title} = $locale->text('Add Exchangerate');
 
1563 <input type=hidden name=exchangeratedate value=$orddate>
 
1564 <input type=hidden name=buysell value=$buysell>
 
1567   <tr><th class=listtop>$form->{title}</th></tr>
 
1568   <tr height="5"></tr>
 
1573           <th align=right>| . $locale->text('Currency') . qq|</th>
 
1574           <td>$form->{currency}</td>
 
1577           <th align=right>| . $locale->text('Date') . qq|</th>
 
1581           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
 
1582           <td><input name=exchangerate size=11></td>
 
1592 <input type=hidden name=nextsub value=save_exchangerate>
 
1594 <input name=action class=submit type=submit value="|
 
1595     . $locale->text('Continue') . qq|">
 
1603   $lxdebug->leave_sub();
 
1606 sub save_exchangerate {
 
1607   $lxdebug->enter_sub();
 
1609   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
 
1610   $form->{exchangerate} =
 
1611     $form->parse_amount(\%myconfig, $form->{exchangerate});
 
1612   $form->save_exchangerate(\%myconfig, $form->{currency},
 
1613                            $form->{exchangeratedate},
 
1614                            $form->{exchangerate}, $form->{buysell});
 
1618   $lxdebug->leave_sub();
 
1621 sub create_backorder {
 
1622   $lxdebug->enter_sub();
 
1624   $form->{shipped} = 1;
 
1626   # figure out if we need to create a backorder
 
1627   # items aren't saved if qty != 0
 
1629   for $i (1 .. $form->{rowcount}) {
 
1630     $totalqty  += $qty  = $form->{"qty_$i"};
 
1631     $totalship += $ship = $form->{"ship_$i"};
 
1633     $form->{"qty_$i"} = $qty - $ship;
 
1636   if ($totalship == 0) {
 
1637     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
 
1638     $form->{ordtotal} = 0;
 
1639     $form->{shipped}  = 0;
 
1643   if ($totalqty == $totalship) {
 
1644     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
 
1645     $form->{ordtotal} = 0;
 
1650     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
 
1653   for $i (1 .. $form->{rowcount}) {
 
1655       $form->{"${_}_$i"} =
 
1656         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
 
1657     } qw(sellprice discount);
 
1662   OE->save(\%myconfig, \%$form);
 
1664   # rebuild rows for invoice
 
1668   for $i (1 .. $form->{rowcount}) {
 
1669     $form->{"qty_$i"} = $form->{"ship_$i"};
 
1671     if ($form->{"qty_$i"}) {
 
1674       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
 
1679   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
 
1680   $form->{rowcount} = $count;
 
1682   $lxdebug->leave_sub();
 
1686   $lxdebug->enter_sub();
 
1690   $form->{saveasnew} = 1;
 
1691   $form->{closed}    = 0;
 
1692   map { delete $form->{$_} } qw(printed emailed queued);
 
1694   # Let Lx-Office assign a new order number if the user hasn't changed the
 
1695   # previous one. If it has been changed manually then use it as-is.
 
1696   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
 
1697   $form->{$idx} =~ s/^\s*//g;
 
1698   $form->{$idx} =~ s/\s*$//g;
 
1699   if ($form->{saved_xyznumber} &&
 
1700       ($form->{saved_xyznumber} eq $form->{$idx})) {
 
1701     delete($form->{$idx});
 
1706   $lxdebug->leave_sub();
 
1709 sub check_for_direct_delivery_yes {
 
1710   $lxdebug->enter_sub();
 
1714   $form->{direct_delivery_checked} = 1;
 
1715   delete @{$form}{grep /^shipto/, keys %{ $form }};
 
1716   map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
 
1717   $form->{shipto} = 1;
 
1719   $lxdebug->leave_sub();
 
1722 sub check_for_direct_delivery_no {
 
1723   $lxdebug->enter_sub();
 
1727   $form->{direct_delivery_checked} = 1;
 
1728   delete @{$form}{grep /^shipto/, keys %{ $form }};
 
1731   $lxdebug->leave_sub();
 
1734 sub check_for_direct_delivery {
 
1735   $lxdebug->enter_sub();
 
1739   if ($form->{direct_delivery_checked}
 
1740       || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
 
1741     $lxdebug->leave_sub();
 
1745   if ($form->{shipto_id}) {
 
1746     Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
 
1749     map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
 
1752   delete $form->{action};
 
1753   $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
 
1756   print $form->parse_html_template("oe/check_for_direct_delivery");
 
1758   $lxdebug->leave_sub();
 
1763 sub purchase_order {
 
1764   $lxdebug->enter_sub();
 
1767   $auth->assert('purchase_order_edit');
 
1769   if ($form->{type} eq 'sales_order') {
 
1770     check_for_direct_delivery();
 
1773   if ($form->{type} =~ /^sales_/) {
 
1774     delete($form->{ordnumber});
 
1777   $form->{cp_id} *= 1;
 
1779   $form->{title} = $locale->text('Add Purchase Order');
 
1780   $form->{vc}    = "vendor";
 
1781   $form->{type}  = "purchase_order";
 
1785   $lxdebug->leave_sub();
 
1789   $lxdebug->enter_sub();
 
1792   $auth->assert('sales_order_edit');
 
1794   if ($form->{type} eq "purchase_order") {
 
1795     delete($form->{ordnumber});
 
1798   $form->{cp_id} *= 1;
 
1800   $form->{title}  = $locale->text('Add Sales Order');
 
1801   $form->{vc}     = "customer";
 
1802   $form->{type}   = "sales_order";
 
1806   $lxdebug->leave_sub();
 
1810   $lxdebug->enter_sub();
 
1813   $auth->assert('purchase_order_edit | sales_order_edit');
 
1815   $form->{transdate} = $form->current_date(\%myconfig);
 
1816   delete $form->{duedate};
 
1818   $form->{convert_from_oe_ids} = $form->{id};
 
1819   $form->{closed}              = 0;
 
1821   $form->{old_employee_id}     = $form->{employee_id};
 
1822   $form->{old_salesman_id}     = $form->{salesman_id};
 
1825   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered
 
1828   for $i (1 .. $form->{rowcount}) {
 
1829     map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
 
1832   my %saved_vars = map { $_ => $form->{$_} } grep { $form->{$_} } qw(currency);
 
1836   map { $form->{$_} = $saved_vars{$_} } keys %saved_vars;
 
1841   for $i (1 .. $form->{rowcount} - 1) {
 
1842     map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
 
1847   $lxdebug->leave_sub();
 
1850 sub delivery_order {
 
1851   $lxdebug->enter_sub();
 
1853   if ($form->{type} =~ /^sales/) {
 
1854     $auth->assert('sales_delivery_order_edit');
 
1856     $form->{vc}    = 'customer';
 
1857     $form->{type}  = 'sales_delivery_order';
 
1860     $auth->assert('purchase_delivery_order_edit');
 
1862     $form->{vc}    = 'vendor';
 
1863     $form->{type}  = 'purchase_delivery_order';
 
1866   require "bin/mozilla/do.pl";
 
1868   $form->{cp_id}               *= 1;
 
1869   $form->{convert_from_oe_ids}  = $form->{id};
 
1870   $form->{transdate}            = $form->current_date(\%myconfig);
 
1871   delete $form->{duedate};
 
1873   $form->{old_employee_id}  = $form->{employee_id};
 
1874   $form->{old_salesman_id}  = $form->{salesman_id};
 
1877   delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
 
1879   for $i (1 .. $form->{rowcount}) {
 
1880     map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
 
1883   my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
 
1889   map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
 
1893   $lxdebug->leave_sub();
 
1897   $lxdebug->enter_sub();
 
1901   $form->{print_and_save} = 1;
 
1905   my $saved_form = save_form();
 
1909   restore_form($saved_form, 0, qw(id ordnumber quonumber));
 
1913   $lxdebug->leave_sub();
 
1917   call_sub($form->{yes_nextsub});
 
1921   call_sub($form->{no_nextsub});
 
1924 ######################################################################################################
 
1926 # ###############################################################################################
 
1928   $lxdebug->enter_sub();
 
1932   retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
 
1934   $form->{"taxaccounts"} =~ s/\s*$//;
 
1935   $form->{"taxaccounts"} =~ s/^\s*//;
 
1936   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
 
1937     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
 
1939   $form->{"taxaccounts"} = "";
 
1941   for my $i (1 .. $form->{"rowcount"}) {
 
1942     IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
 
1945   $form->{rowcount}++;
 
1946   $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
 
1948   $form->language_payment(\%myconfig);
 
1950   Common::webdav_folder($form) if ($webdav);
 
1955   display_row($form->{rowcount}) if $form->{rowcount};
 
1959   $lxdebug->leave_sub();
 
1962 sub report_for_todo_list {
 
1963   $lxdebug->enter_sub();
 
1965   my $quotations = OE->transactions_for_todo_list();
 
1968   if (@{ $quotations }) {
 
1969     my $edit_url = build_std_url('script=oe.pl', 'action=edit');
 
1971     $content     = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
 
1972                                                                            'edit_url'   => $edit_url });
 
1975   $lxdebug->leave_sub();