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);
 
  46 require "bin/mozilla/io.pl";
 
  47 require "bin/mozilla/arap.pl";
 
  48 require "bin/mozilla/reportgenerator.pl";
 
  55 # $locale->text('Edit the purchase_order');
 
  56 # $locale->text('Edit the sales_order');
 
  57 # $locale->text('Edit the request_quotation');
 
  58 # $locale->text('Edit the sales_quotation');
 
  60 # $locale->text('Workflow purchase_order');
 
  61 # $locale->text('Workflow sales_order');
 
  62 # $locale->text('Workflow request_quotation');
 
  63 # $locale->text('Workflow sales_quotation');
 
  66   'sales_order'       => 'sales_order_edit',
 
  67   'purchase_order'    => 'purchase_order_edit',
 
  68   'request_quotation' => 'request_quotation_edit',
 
  69   'sales_quotation'   => 'sales_quotation_edit',
 
  73   my $right   = $oe_access_map->{$form->{type}};
 
  74   $right    ||= 'DOES_NOT_EXIST';
 
  76   $auth->assert($right);
 
  80   $lxdebug->enter_sub();
 
  86   if ($form->{type} eq 'purchase_order') {
 
  87     $form->{title}   = $action eq "edit" ?
 
  88       $locale->text('Edit Purchase Order') :
 
  89       $locale->text('Add Purchase Order');
 
  90     $form->{heading} = $locale->text('Purchase Order');
 
  91     $form->{vc}      = 'vendor';
 
  93   if ($form->{type} eq 'sales_order') {
 
  94     $form->{title}   = $action eq "edit" ?
 
  95       $locale->text('Edit Sales Order') :
 
  96       $locale->text('Add Sales Order');
 
  97     $form->{heading} = $locale->text('Sales Order');
 
  98     $form->{vc}      = 'customer';
 
 100   if ($form->{type} eq 'request_quotation') {
 
 101     $form->{title}   = $action eq "edit" ?
 
 102       $locale->text('Edit Request for Quotation') :
 
 103       $locale->text('Add Request for Quotation');
 
 104     $form->{heading} = $locale->text('Request for Quotation');
 
 105     $form->{vc}      = 'vendor';
 
 107   if ($form->{type} eq 'sales_quotation') {
 
 108     $form->{title}   = $action eq "edit" ?
 
 109       $locale->text('Edit Quotation') :
 
 110       $locale->text('Add Quotation');
 
 111     $form->{heading} = $locale->text('Quotation');
 
 112     $form->{vc}      = 'customer';
 
 115   $lxdebug->leave_sub();
 
 119   $lxdebug->enter_sub();
 
 126     "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
 
 127     unless $form->{callback};
 
 133   $lxdebug->leave_sub();
 
 137   $lxdebug->enter_sub();
 
 141   # show history button
 
 142   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 143   #/show hhistory button
 
 145   $form->{simple_save} = 0;
 
 147   set_headings("edit");
 
 149   # editing without stuff to edit? try adding it first
 
 150   if ($form->{rowcount} && !$form->{print_and_save}) {
 
 151     map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
 
 155       undef $form->{rowcount};
 
 157       $lxdebug->leave_sub();
 
 160   } elsif (!$form->{id}) {
 
 162     $lxdebug->leave_sub();
 
 166   if ($form->{print_and_save}) {
 
 167     $form->{action}   = "print";
 
 168     $form->{resubmit} = 1;
 
 169     $language_id = $form->{language_id};
 
 170     $printer_id = $form->{printer_id};
 
 173   set_headings("edit");
 
 177   $form->{rowcount} = 0;
 
 178   foreach $ref (@{ $form->{form_details} }) {
 
 180     map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
 
 185   if ($form->{print_and_save}) {
 
 186     $form->{language_id} = $language_id;
 
 187     $form->{printer_id} = $printer_id;
 
 192   $lxdebug->leave_sub();
 
 196   $lxdebug->enter_sub();
 
 200   # get customer/vendor
 
 201   $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
 
 203   # retrieve order/quotation
 
 204   $form->{webdav}   = $webdav;
 
 205   $form->{jsscript} = 1;
 
 207   my $editing = $form->{id};
 
 208   $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
 
 210   OE->retrieve(\%myconfig, \%$form);
 
 212   # if multiple rowcounts (== collective order) then check if the
 
 213   # there were more than one customer (in that case OE::retrieve removes
 
 214   # the content from the field)
 
 215   $form->error($locale->text('Collective Orders only work for orders from one customer!'))
 
 216     if          $form->{rowcount}  && $form->{type}     eq 'sales_order'
 
 217      && defined $form->{customer}  && $form->{customer} eq '';
 
 219   $form->{"$form->{vc}_id"} ||= $form->{"all_$form->{vc}"}->[0]->{id} if $form->{"all_$form->{vc}"};
 
 221   $form->{shipto} = 1 if $form->{id};
 
 223   # get customer / vendor
 
 224   IR->get_vendor(\%myconfig, \%$form)   if $form->{type} =~ /(purchase_order|request_quotation)/;
 
 225   IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
 
 227   $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
 
 228   $form->restore_vars(qw(taxincluded)) if $form->{id};
 
 229   $form->restore_vars(qw(salesman_id)) if $editing;
 
 230   $form->{forex}       = $form->{exchangerate};
 
 231   $form->{employee}    = "$form->{employee}--$form->{employee_id}";
 
 233   # build vendor/customer drop down comatibility... don't ask
 
 234   if (@{ $form->{"all_$form->{vc}"} }) {
 
 235     $form->{"select$form->{vc}"} = 1;
 
 236     $form->{$form->{vc}}         = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
 
 239   $form->{"old$form->{vc}"}  = $form->{$form->{vc}};
 
 240   $form->{"old$form->{vc}"} .= qq|--$form->{"$form->{vc}_id"}| unless ($form->{"old$form->{vc}"} =~ m/--\d+$/);
 
 242   $lxdebug->leave_sub();
 
 246   $lxdebug->enter_sub();
 
 250   $form->{formname} ||= $form->{type};
 
 252   # format discounts if values come from db. either as single id, or as a collective order
 
 253   my $format_discounts = $form->{id} || $form->{convert_from_oe_ids};
 
 255   for my $i (1 .. $form->{rowcount}) {
 
 256     $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
 
 257     $form->{"discount_$i"}  = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($format_discounts ? 100 : 1));
 
 258     $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
 
 259     $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"});
 
 262   $lxdebug->leave_sub();
 
 266   $lxdebug->enter_sub();
 
 271   # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
 
 274   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 276   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 277   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 279   # use JavaScript Calendar or not
 
 280   $form->{jsscript} = 1;
 
 282   # openclosed checkboxes
 
 284   push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
 
 285                         $form->{"delivered"} ? "checked" : "",  $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
 
 286   push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
 
 287                         $form->{"closed"}    ? "checked" : "",  $locale->text('Closed')    if $form->{id};
 
 288   $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
 
 291   my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
 
 293   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
 
 294   $form->get_lists("contacts"      => "ALL_CONTACTS",
 
 295                    "shipto"        => "ALL_SHIPTO",
 
 296                    "projects"      => { "key"      => "ALL_PROJECTS",
 
 298                                         "old_id"   => \@old_project_ids },
 
 299                    "employees"     => "ALL_EMPLOYEES",
 
 300                    "salesmen"      => "ALL_SALESMEN",
 
 301                    "taxzones"      => "ALL_TAXZONES",
 
 302                    "payments"      => "ALL_PAYMENTS",
 
 303                    "currencies"    => "ALL_CURRENCIES",
 
 304                    "departments"   => "ALL_DEPARTMENTS",
 
 305                    $vc             => { key   => "ALL_" . uc($vc),
 
 306                                         limit => $myconfig{vclimit} + 1 },
 
 307                    "price_factors" => "ALL_PRICE_FACTORS");
 
 310   $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
 
 311   $TMPL_VAR{shipto_labels}         = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
 
 312   $TMPL_VAR{contact_labels}        = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
 
 313   $TMPL_VAR{department_labels}     = sub { "$_[0]->{description}--$_[0]->{id}" };
 
 316   $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
 
 317   $TMPL_VAR{vclimit} = $myconfig{vclimit};
 
 318   $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
 
 319   push @custom_hiddens, "$form->{vc}_id";
 
 320   push @custom_hiddens, "old$form->{vc}";
 
 321   push @custom_hiddens, "select$form->{vc}";
 
 323   # currencies and exchangerate
 
 324   @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
 
 325   %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
 
 326   $form->{currency}            = $form->{defaultcurrency} unless $form->{currency};
 
 327   $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
 
 328   $TMPL_VAR{currencies}        = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
 
 329                                                       '-values' => \@values, '-labels' => \%labels)) if scalar @values;
 
 330   push @custom_hiddens, "forex";
 
 331   push @custom_hiddens, "exchangerate" if $form->{forex};
 
 334   $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
 
 335   $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
 
 338   $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
 
 340   push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
 
 342   $credittext = $locale->text('Credit Limit exceeded!!!');
 
 344   my $follow_up_vc                =  $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
 
 345   $follow_up_vc                   =~ s/--\d*\s*$//;
 
 346   $TMPL_VAR{follow_up_trans_info} =  ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
 
 349     my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
 
 351     if (scalar @{ $follow_ups }) {
 
 352       $TMPL_VAR{num_follow_ups}     = scalar                    @{ $follow_ups };
 
 353       $TMPL_VAR{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
 
 357   $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
 
 358           : ($form->{resubmit})                                ? "document.oe.submit()"
 
 359           : ($creditwarning)                                   ? "alert('$credittext')"
 
 362   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
 
 363   $onload .= qq|;setupPoints('|.   $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
 
 364   $TMPL_VAR{onload} = $onload;
 
 366   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
 
 367   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
 368   $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
 
 372   $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
 
 373      qw(id action type vc formname media format proforma queued printed emailed
 
 374         title creditlimit creditremaining tradediscount business
 
 375         max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
 
 376         shiptocity shiptocountry shiptocontact shiptophone shiptofax
 
 377         shiptodepartment_1 shiptodepartment_2 shiptoemail
 
 378         message email subject cc bcc taxpart taxservice taxaccounts cursor_fokus),
 
 380         map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ];  # deleted: discount
 
 384      is_sales        => scalar ($form->{type} =~ /^sales_/),              # these vars are exported, so that the template
 
 385      is_order        => scalar ($form->{type} =~ /_order$/),              # may determine what to show
 
 386      is_sales_quo    => scalar ($form->{type} =~ /sales_quotation$/),
 
 387      is_req_quo      => scalar ($form->{type} =~ /request_quotation$/),
 
 388      is_sales_ord    => scalar ($form->{type} =~ /sales_order$/),
 
 389      is_pur_ord      => scalar ($form->{type} =~ /purchase_order$/),
 
 392   print $form->parse_html_template("oe/form_header", { %TMPL_VAR });
 
 394   $lxdebug->leave_sub();
 
 398   $lxdebug->enter_sub();
 
 402   $form->{invtotal} = $form->{invsubtotal};
 
 404   $rows    = max 2, $form->numtextrows($form->{notes}, 25, 8);
 
 405   $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
 
 406   $rows    = max $rows, $introws;
 
 408   $TMPL_VAR{notes}    = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>| . H($form->{notes}) . qq|</textarea>|;
 
 409   $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$introws cols=35 wrap=soft>| . H($form->{intnotes}) . qq|</textarea>|;
 
 411   if (!$form->{taxincluded}) {
 
 413     foreach $item (split / /, $form->{taxaccounts}) {
 
 414       if ($form->{"${item}_base"}) {
 
 415         $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
 
 416         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
 
 418         $TMPL_VAR{tax} .= qq|
 
 420                 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
 
 421                 <td align=right>$form->{"${item}_total"}</td>
 
 426 #    $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0); # template does this
 
 429     foreach $item (split / /, $form->{taxaccounts}) {
 
 430       if ($form->{"${item}_base"}) {
 
 431         $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
 
 432         $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
 
 433         $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
 
 434         $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
 
 436         $TMPL_VAR{tax} .= qq|
 
 438                 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
 
 439                 <td align=right>$form->{"${item}_total"}</td>
 
 442                 <th align=right>Nettobetrag</th>
 
 443                 <td align=right>$form->{"${item}_netto"}</td>
 
 449   $form->{oldinvtotal} = $form->{invtotal};
 
 451   print $form->parse_html_template("oe/form_footer", {
 
 454      print_options   => print_options(inline => 1),
 
 455      label_edit      => $locale->text("Edit the $form->{type}"),
 
 456      label_workflow  => $locale->text("Workflow $form->{type}"),
 
 459   $lxdebug->leave_sub();
 
 463   $lxdebug->enter_sub();
 
 465   my ($recursive_call) = shift;
 
 469 #  $main::lxdebug->message(0, Dumper($form));
 
 471   set_headings($form->{"id"} ? "edit" : "add");
 
 473   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
 
 476   $payment_id = $form->{payment_id} if $form->{payment_id};
 
 478   &check_name($form->{vc});
 
 480   $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
 
 483   $buysell              = 'sell' if ($form->{vc} eq 'vendor');
 
 484   $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
 
 485   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 487   $exchangerate = $form->{exchangerate} || 1;
 
 489 ##################### process items ######################################
 
 491   $i = $form->{rowcount};
 
 492   if (   ($form->{"partnumber_$i"} eq "")
 
 493       && ($form->{"description_$i"} eq "")
 
 494       && ($form->{"partsgroup_$i"}  eq "")) {
 
 496     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
 
 501     if ($form->{type} =~ /^sales/) {
 
 502       IS->retrieve_item(\%myconfig, \%$form);
 
 504       IR->retrieve_item(\%myconfig, \%$form);
 
 507     my $rows = scalar @{ $form->{item_list} };
 
 509     # hier ist das problem fuer bug 817 $form->{discount} wird nicht durchgeschliffen
 
 510     # ferner fallunterscheidung fuer verkauf oder einkauf s.a. bug 736  jb 04.05.2009 
 
 511     # select discount as vendor_discount from vendor || 
 
 512     # select discount as customer_discount from customer
 
 513     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"$form->{vc}_discount"} * 100);
 
 516       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
 
 525         $sellprice             = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
 
 526         $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
 
 527         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g }    qw(partnumber description unit);
 
 528         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 529         $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
 
 531         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 533         ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
 
 534         $decimalplaces = max 2, length $1;
 
 537           $form->{"sellprice_$i"} = $sellprice;
 
 539           $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
 
 540           $form->{"sellprice_$i"} /= $exchangerate;   # if there is an exchange rate adjust sellprice
 
 543         $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
 
 544         map { $form->{"${_}_base"} = 0 }                                 split / /, $form->{taxaccounts};
 
 545         map { $form->{"${_}_base"} += $amount }                          split / /, $form->{"taxaccounts_$i"};
 
 546         map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
 
 548         $form->{creditremaining} -= $amount;
 
 550         $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 551         $form->{"qty_$i"}       = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
 553         # get pricegroups for parts
 
 554         IS->get_pricegroups_for_parts(\%myconfig, \%$form);
 
 556         # build up html code for prices_$i
 
 563       # ok, so this is a new part
 
 564       # ask if it is a part or service item
 
 566       if (   $form->{"partsgroup_$i"}
 
 567           && ($form->{"partsnumber_$i"} eq "")
 
 568           && ($form->{"description_$i"} eq "")) {
 
 570         $form->{"discount_$i"} = "";
 
 574         $form->{"id_$i"}   = 0;
 
 579 ##################### process items ######################################
 
 582   $lxdebug->leave_sub();
 
 586   $lxdebug->enter_sub();
 
 590   if ($form->{type} eq 'purchase_order') {
 
 591     $form->{title} = $locale->text('Purchase Orders');
 
 592     $form->{vc}    = 'vendor';
 
 593     $ordlabel      = $locale->text('Order Number');
 
 594     $ordnumber     = 'ordnumber';
 
 595     $employee      = $locale->text('Employee');
 
 598   if ($form->{type} eq 'request_quotation') {
 
 599     $form->{title} = $locale->text('Request for Quotations');
 
 600     $form->{vc}    = 'vendor';
 
 601     $ordlabel      = $locale->text('RFQ Number');
 
 602     $ordnumber     = 'quonumber';
 
 603     $employee      = $locale->text('Employee');
 
 606   if ($form->{type} eq 'sales_order') {
 
 607     $form->{title} = $locale->text('Sales Orders');
 
 608     $form->{vc}    = 'customer';
 
 609     $ordlabel      = $locale->text('Order Number');
 
 610     $ordnumber     = 'ordnumber';
 
 611     $employee      = $locale->text('Employee');
 
 614   if ($form->{type} eq 'sales_quotation') {
 
 615     $form->{title} = $locale->text('Quotations');
 
 616     $form->{vc}    = 'customer';
 
 617     $ordlabel      = $locale->text('Quotation Number');
 
 618     $ordnumber     = 'quonumber';
 
 619     $employee      = $locale->text('Employee');
 
 622   # setup vendor / customer selection
 
 623   $form->all_vc(\%myconfig, $form->{vc},
 
 624                 ($form->{vc} eq 'customer') ? "AR" : "AP");
 
 627   if (@{ $form->{all_departments} }) {
 
 628     $form->{selectdepartment} = "<option>\n";
 
 631       $form->{selectdepartment} .=
 
 632         "<option>$_->{description}--$_->{id}</option>\n"
 
 633     } (@{ $form->{all_departments} });
 
 638           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
 
 639           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
 
 641 | if $form->{selectdepartment};
 
 644   if (($form->{"type"} eq "sales_order") ||
 
 645       ($form->{"type"} eq "purchase_order")) {
 
 648           <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
 
 649             <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
 
 650           <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
 
 651             <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
 
 656   # use JavaScript Calendar or not
 
 657   $form->{jsscript} = 1;
 
 661      <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
 
 662      <input type=button name=transdatefrom id="trigger3" value=|
 
 663     . $locale->text('button') . qq|></td>
 
 666      <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
 
 667      <input type=button name=transdateto name=transdateto id="trigger4" value=|
 
 668     . $locale->text('button') . qq|></td>
 
 671      <td><input name=reqdatefrom id=reqdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
 
 672      <input type=button name=reqdatefrom id="trigger5" value=|
 
 673     . $locale->text('button') . qq|></td>
 
 676      <td><input name=reqdateto id=reqdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
 
 677      <input type=button name=reqdateto name=reqdateto id="trigger6" value=|
 
 678     . $locale->text('button') . qq|></td>
 
 683    Form->write_trigger(\%myconfig, "4", "transdatefrom", "BR", "trigger3",
 
 684                        "transdateto", "BL", "trigger4",
 
 685                        "reqdatefrom", "BR", "trigger5", "reqdateto", "BL", "trigger6");
 
 687   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
 
 689   $form->get_lists("projects"  => { "key" => "ALL_PROJECTS",
 
 691                    "employees" => "ALL_EMPLOYEES",
 
 692                    "salesmen"  => "ALL_SALESMEN",
 
 693                    $vc         => "ALL_" . uc($vc)
 
 698   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
 
 699     push(@values, $item->{"id"});
 
 700     $labels{$item->{"id"}} = $item->{"projectnumber"};
 
 703     NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
 
 704                          '-labels' => \%labels));
 
 709   foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
 
 710     push(@values, $item->{"id"});
 
 711     $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
 
 715   my %labels_salesmen = ();
 
 716   my @values_salesmen = ('');
 
 717   foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
 
 718     push(@values_salesmen, $item->{"id"});
 
 719     $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
 
 722   my $employee_block = qq|
 
 724       <th align="right">| . $locale->text('Employee') . qq|</th>
 
 725       <td>| .  NTI($cgi->popup_menu('-name'   => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
 
 728       <th align="right">| . $locale->text('Salesman') . qq|</th>
 
 730         NTI($cgi->popup_menu('-name'   => 'salesman_id',
 
 731                              '-values' => \@values_salesmen,
 
 732                              '-labels' => \%labels_salesmen)) . qq|
 
 739   foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
 
 740     push(@values, $item->{name}.qq|--|.$item->{"id"});
 
 741     $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
 
 744   my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
 
 746     $myconfig{vclimit} <=  scalar(@values)
 
 747     ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">|
 
 748     : NTI($cgi->popup_menu('-name' => "$form->{vc}",
 
 749                            '-default' => $form->{"old$form->{vc}"},
 
 750                            '-onChange' => 'document.getElementById(\'update_button\').click();',
 
 751                            '-values' => \@values,
 
 752                            '-labels' => \%labels));
 
 758 <form method=post action=$form->{script}>
 
 762     <th class=listtop>$form->{title}</th>
 
 769           <th align=right>$vc_label</th>
 
 770           <td colspan=3>$vc</td>
 
 774           <th align=right>$ordlabel</th>
 
 775           <td colspan=3><input name="$ordnumber" size=20></td>
 
 779           <th align="right">| . $locale->text('Transaction description') . qq|</th>
 
 780           <td colspan="3"><input name="transaction_description" size=20></td>
 
 783           <th align="right">| . $locale->text("Project Number") . qq|</th>
 
 784           <td colspan="3">$projectnumber</td>
 
 787           <th align=right>| . $locale->text('Order Date') . " " . $locale->text('From') . qq|</th> $button1
 
 788           <th align=right>| . $locale->text('Bis') . qq|</th> $button2
 
 790         <input type=hidden name=sort value=transdate>
 
 792           <th align=right>| . $locale->text('Delivery Date') . " " . $locale->text('From') . qq|</th> $button3
 
 793           <th align=right>| . $locale->text('Bis') . qq|</th> $button4
 
 795         <input type=hidden name=sort value=reqdate>
 
 797           <th align=right>| . $locale->text('Include in Report') . qq|</th>
 
 801           <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
 
 802           <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
 
 806                 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
 
 807                 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
 
 810                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
 
 811                 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
 
 814                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
 
 815                 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
 
 817                 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
 
 820                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
 
 821                 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
 
 822                 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
 
 825                 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | .             $locale->text('Ertrag') . qq|</td>
 
 826                 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | .             $locale->text('Ertrag prozentual') . qq|</td>
 
 829           <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
 
 830           <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
 
 833                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
 
 834                 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
 
 842   <tr><td colspan=4><hr size=3 noshade></td></tr>
 
 848 <input type=hidden name=nextsub value=orders>
 
 849 <input type=hidden name=vc value=$form->{vc}>
 
 850 <input type=hidden name=type value=$form->{type}>
 
 852 <input class=submit type=submit name=action value="|
 
 853     . $locale->text('Continue') . qq|">
 
 860   $lxdebug->leave_sub();
 
 863 sub create_subtotal_row {
 
 864   $lxdebug->enter_sub();
 
 866   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 868   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 870   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 872   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 874   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 876   $lxdebug->leave_sub();
 
 882   $lxdebug->enter_sub();
 
 886   $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
 
 888   ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
 
 890   report_generator_set_default_sort('transdate', 1);
 
 892   OE->transactions(\%myconfig, \%$form);
 
 894   $form->{rowcount} = scalar @{ $form->{OE} };
 
 897     "transdate",               "reqdate",
 
 903     "shipvia",                 "globalprojectnumber",
 
 904     "transaction_description", "open",
 
 905     "delivered", "marge_total", "marge_percent"
 
 908   # only show checkboxes if gotten here via sales_order form.
 
 909   my $allow_multiple_orders = $form->{type} eq 'sales_order';
 
 910   if ($allow_multiple_orders) {
 
 911     unshift @columns, "ids";
 
 914   $form->{l_open}      = $form->{l_closed} = "Y" if ($form->{open}      && $form->{closed});
 
 915   $form->{l_delivered} = "Y"                     if ($form->{delivered} && $form->{notdelivered});
 
 917   my $attachment_basename;
 
 918   if ($form->{vc} eq 'vendor') {
 
 919     if ($form->{type} eq 'purchase_order') {
 
 920       $form->{title}       = $locale->text('Purchase Orders');
 
 921       $attachment_basename = $locale->text('purchase_order_list');
 
 923       $form->{title}       = $locale->text('Request for Quotations');
 
 924       $attachment_basename = $locale->text('rfq_list');
 
 928     if ($form->{type} eq 'sales_order') {
 
 929       $form->{title}       = $locale->text('Sales Orders');
 
 930       $attachment_basename = $locale->text('sales_order_list');
 
 932       $form->{title}       = $locale->text('Quotations');
 
 933       $attachment_basename = $locale->text('quotation_list');
 
 937   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 939   my @hidden_variables = map { "l_${_}" } @columns;
 
 940   push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
 
 941                                                         transaction_description transdatefrom transdateto type vc employee_id salesman_id
 
 942                                                         reqdatefrom reqdateto);
 
 944   my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
 
 947     'ids'                     => { 'text' => '', },
 
 948     'transdate'               => { 'text' => $locale->text('Date'), },
 
 949     'reqdate'                 => { 'text' => $locale->text('Required by'), },
 
 950     'id'                      => { 'text' => $locale->text('ID'), },
 
 951     'ordnumber'               => { 'text' => $locale->text('Order'), },
 
 952     'quonumber'               => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
 
 953     'name'                    => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
 954     'netamount'               => { 'text' => $locale->text('Amount'), },
 
 955     'tax'                     => { 'text' => $locale->text('Tax'), },
 
 956     'amount'                  => { 'text' => $locale->text('Total'), },
 
 957     'curr'                    => { 'text' => $locale->text('Curr'), },
 
 958     'employee'                => { 'text' => $locale->text('Employee'), },
 
 959     'salesman'                => { 'text' => $locale->text('Salesman'), },
 
 960     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
 
 961     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
 
 962     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
 
 963     'open'                    => { 'text' => $locale->text('Open'), },
 
 964     'delivered'               => { 'text' => $locale->text('Delivered'), },
 
 965     'marge_total'             => { 'text' => $locale->text('Ertrag'), },
 
 966     'marge_percent'           => { 'text' => $locale->text('Ertrag prozentual'), }
 
 969   foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia transaction_description)) {
 
 970     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
 971     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
 
 974   my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
 
 976   $form->{"l_type"} = "Y";
 
 977   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 978   $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
 
 980   $report->set_columns(%column_defs);
 
 981   $report->set_column_order(@columns);
 
 982   $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
 
 983   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 986   my ($department) = split m/--/, $form->{department};
 
 988   push @options, $locale->text('Customer')                . " : $form->{customer}"                        if $form->{customer};
 
 989   push @options, $locale->text('Vendor')                  . " : $form->{vendor}"                          if $form->{vendor};
 
 990   push @options, $locale->text('Department')              . " : $department"                              if $form->{department};
 
 991   push @options, $locale->text('Order Number')            . " : $form->{ordnumber}"                       if $form->{ordnumber};
 
 992   push @options, $locale->text('Notes')                   . " : $form->{notes}"                           if $form->{notes};
 
 993   push @options, $locale->text('Transaction description') . " : $form->{transaction_description}"         if $form->{transaction_description};
 
 994   if ( $form->{transdatefrom} or $form->{transdateto} ) {
 
 995     push @options, $locale->text('Order Date');
 
 996     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1)     if $form->{transdatefrom};
 
 997     push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{transdateto},   1)     if $form->{transdateto};
 
 999   if ( $form->{reqdatefrom} or $form->{reqdateto} ) {
 
1000     push @options, $locale->text('Delivery Date');
 
1001     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{reqdatefrom}, 1)       if $form->{reqdatefrom};
 
1002     push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{reqdateto},   1)       if $form->{reqdateto};
 
1004   push @options, $locale->text('Open')                                                                    if $form->{open};
 
1005   push @options, $locale->text('Closed')                                                                  if $form->{closed};
 
1006   push @options, $locale->text('Delivered')                                                               if $form->{delivered};
 
1007   push @options, $locale->text('Not delivered')                                                           if $form->{notdelivered};
 
1009   $report->set_options('top_info_text'        => join("\n", @options),
 
1010                        'raw_top_info_text'    => $form->parse_html_template('oe/orders_top'),
 
1011                        'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
 
1012                        'output_format'        => 'HTML',
 
1013                        'title'                => $form->{title},
 
1014                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1016   $report->set_options_from_form();
 
1018   # add sort and escape callback, this one we use for the add sub
 
1019   $form->{callback} = $href .= "&sort=$form->{sort}";
 
1021   # escape callback for href
 
1022   $callback = $form->escape($href);
 
1024   my @subtotal_columns = qw(netamount amount marge_total marge_percent);
 
1026   my %totals    = map { $_ => 0 } @subtotal_columns;
 
1027   my %subtotals = map { $_ => 0 } @subtotal_columns;
 
1031   my $edit_url = build_std_url('action=edit', 'type', 'vc');
 
1033   foreach $oe (@{ $form->{OE} }) {
 
1034     map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
 
1036     $oe->{tax}       = $oe->{amount} - $oe->{netamount};
 
1037     $oe->{open}      = $oe->{closed}    ? $locale->text('No')  : $locale->text('Yes');
 
1038     $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
 
1040     map { $subtotals{$_} += $oe->{$_};
 
1041           $totals{$_}    += $oe->{$_} } @subtotal_columns;
 
1043     $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
 
1044     $totals{marge_percent}    = $totals{netamount}    ? ($totals{marge_total}    * 100 / $totals{netamount}   ) : 0;
 
1046     map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
 
1050     foreach my $column (@columns) {
 
1051       next if ($column eq 'ids');
 
1053         'data'  => $oe->{$column},
 
1054         'align' => $column_alignment{$column},
 
1059       'raw_data' =>   $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
 
1060                     . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
 
1061       'valign'   => 'center',
 
1062       'align'    => 'center',
 
1065     $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
 
1067     my $row_set = [ $row ];
 
1069     if (($form->{l_subtotal} eq 'Y')
 
1070         && (($idx == (scalar @{ $form->{OE} } - 1))
 
1071             || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
 
1072       push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
 
1075     $report->add_data($row_set);
 
1080   $report->add_separator();
 
1081   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
1083   $report->generate_with_headers();
 
1085   $lxdebug->leave_sub();
 
1088 sub check_delivered_flag {
 
1089   $lxdebug->enter_sub();
 
1093   if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
 
1094     return $lxdebug->leave_sub();
 
1097   my $all_delivered = 0;
 
1099   foreach my $i (1 .. $form->{rowcount}) {
 
1100     next if (!$form->{"id_$i"});
 
1102     if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
 
1111   $form->{delivered} = 1 if $all_delivered;
 
1113   $lxdebug->leave_sub();
 
1116 sub save_and_close {
 
1117   $lxdebug->enter_sub();
 
1121   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
1123   if ($form->{type} =~ /_order$/) {
 
1124     $form->isblank("transdate", $locale->text('Order Date missing!'));
 
1126     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
 
1129   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
 
1130   $form->{$idx} =~ s/^\s*//g;
 
1131   $form->{$idx} =~ s/\s*$//g;
 
1133   $msg = ucfirst $form->{vc};
 
1134   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
 
1136   # $locale->text('Customer missing!');
 
1137   # $locale->text('Vendor missing!');
 
1139   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
1140     if ($form->{currency} ne $form->{defaultcurrency});
 
1144   if($form->{payment_id}) {
 
1145     $payment_id = $form->{payment_id};
 
1148   # if the name changed get new values
 
1149   if (&check_name($form->{vc})) {
 
1150     if($form->{payment_id} eq "") {
 
1151       $form->{payment_id} = $payment_id;
 
1157   $form->{id} = 0 if $form->{saveasnew};
 
1159   # this is for the internal notes section for the [email] Subject
 
1160   if ($form->{type} =~ /_order$/) {
 
1161     if ($form->{type} eq 'sales_order') {
 
1162       $form->{label} = $locale->text('Sales Order');
 
1164       $numberfld = "sonumber";
 
1165       $ordnumber = "ordnumber";
 
1167       $form->{label} = $locale->text('Purchase Order');
 
1169       $numberfld = "ponumber";
 
1170       $ordnumber = "ordnumber";
 
1173     $err = $locale->text('Cannot save order!');
 
1175     check_delivered_flag();
 
1178     if ($form->{type} eq 'sales_quotation') {
 
1179       $form->{label} = $locale->text('Quotation');
 
1181       $numberfld = "sqnumber";
 
1182       $ordnumber = "quonumber";
 
1184       $form->{label} = $locale->text('Request for Quotation');
 
1186       $numberfld = "rfqnumber";
 
1187       $ordnumber = "quonumber";
 
1190     $err = $locale->text('Cannot save quotation!');
 
1194   # get new number in sequence if no number is given or if saveasnew was requested
 
1195   if (!$form->{$ordnumber} || $form->{saveasnew}) {
 
1196     $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
 
1201   $form->error($err) if (!OE->save(\%myconfig, \%$form));
 
1203   # saving the history
 
1204   if(!exists $form->{addition}) {
 
1205     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1206         $form->{addition} = "SAVED";
 
1207         $form->save_history($form->dbconnect(\%myconfig));
 
1209   # /saving the history
 
1211   $form->redirect($form->{label} . " $form->{$ordnumber} " .
 
1212                   $locale->text('saved!'));
 
1214   $lxdebug->leave_sub();
 
1218   $lxdebug->enter_sub();
 
1222   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
1225   if ($form->{type} =~ /_order$/) {
 
1226     $form->isblank("transdate", $locale->text('Order Date missing!'));
 
1228     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
 
1231   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
 
1232   $form->{$idx} =~ s/^\s*//g;
 
1233   $form->{$idx} =~ s/\s*$//g;
 
1235   $msg = ucfirst $form->{vc};
 
1236   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
 
1238   # $locale->text('Customer missing!');
 
1239   # $locale->text('Vendor missing!');
 
1241   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
 
1242     if ($form->{currency} ne $form->{defaultcurrency});
 
1246   if($form->{payment_id}) {
 
1247     $payment_id = $form->{payment_id};
 
1250   # if the name changed get new values
 
1251   if (&check_name($form->{vc})) {
 
1252     if($form->{payment_id} eq "") {
 
1253       $form->{payment_id} = $payment_id;
 
1259   $form->{id} = 0 if $form->{saveasnew};
 
1261   # this is for the internal notes section for the [email] Subject
 
1262   if ($form->{type} =~ /_order$/) {
 
1263     if ($form->{type} eq 'sales_order') {
 
1264       $form->{label} = $locale->text('Sales Order');
 
1266       $numberfld = "sonumber";
 
1267       $ordnumber = "ordnumber";
 
1269       $form->{label} = $locale->text('Purchase Order');
 
1271       $numberfld = "ponumber";
 
1272       $ordnumber = "ordnumber";
 
1275     $err = $locale->text('Cannot save order!');
 
1277     check_delivered_flag();
 
1280     if ($form->{type} eq 'sales_quotation') {
 
1281       $form->{label} = $locale->text('Quotation');
 
1283       $numberfld = "sqnumber";
 
1284       $ordnumber = "quonumber";
 
1286       $form->{label} = $locale->text('Request for Quotation');
 
1288       $numberfld = "rfqnumber";
 
1289       $ordnumber = "quonumber";
 
1292     $err = $locale->text('Cannot save quotation!');
 
1296   $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
 
1297     unless $form->{$ordnumber};
 
1301   OE->save(\%myconfig, \%$form);
 
1303   # saving the history
 
1304   if(!exists $form->{addition}) {
 
1305     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1306         $form->{addition} = "SAVED";
 
1307         $form->save_history($form->dbconnect(\%myconfig));
 
1309   # /saving the history
 
1311   $form->{simple_save} = 1;
 
1312   if(!$form->{print_and_save}) {
 
1313     set_headings("edit");
 
1317   $lxdebug->leave_sub();
 
1321   $lxdebug->enter_sub();
 
1327   if ($form->{type} =~ /_order$/) {
 
1328     $msg       = $locale->text('Are you sure you want to delete Order Number');
 
1329     $ordnumber = 'ordnumber';
 
1331     $msg = $locale->text('Are you sure you want to delete Quotation Number');
 
1332     $ordnumber = 'quonumber';
 
1338 <form method=post action=$form->{script}>
 
1341   # delete action variable
 
1342   map { delete $form->{$_} } qw(action header);
 
1344   foreach $key (keys %$form) {
 
1345     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1346     $form->{$key} =~ s/\"/"/g;
 
1347     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
1351 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
 
1353 <h4>$msg $form->{$ordnumber}</h4>
 
1355 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
 
1356 <input name=action class=submit type=submit value="|
 
1357     . $locale->text('Yes') . qq|">
 
1358 <button class=submit type=button onclick="history.back()">|
 
1359     . $locale->text('No') . qq|</button>
 
1366   $lxdebug->leave_sub();
 
1369 sub delete_order_quotation {
 
1370   $lxdebug->enter_sub();
 
1374   if ($form->{type} =~ /_order$/) {
 
1375     $msg = $locale->text('Order deleted!');
 
1376     $err = $locale->text('Cannot delete order!');
 
1378     $msg = $locale->text('Quotation deleted!');
 
1379     $err = $locale->text('Cannot delete quotation!');
 
1381   if (OE->delete(\%myconfig, \%$form, $spool)){
 
1382     # saving the history
 
1383     if(!exists $form->{addition}) {
 
1384       $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1385           $form->{addition} = "DELETED";
 
1386           $form->save_history($form->dbconnect(\%myconfig));
 
1388     # /saving the history
 
1394   $lxdebug->leave_sub();
 
1398   $lxdebug->enter_sub();
 
1401   $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
 
1403   $form->{old_employee_id} = $form->{employee_id};
 
1404   $form->{old_salesman_id} = $form->{salesman_id};
 
1406   if ($form->{type} =~ /_order$/) {
 
1408     # these checks only apply if the items don't bring their own ordnumbers/transdates.
 
1409     # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
 
1410     $form->isblank("ordnumber", $locale->text('Order Number missing!'))
 
1411       if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
 
1412     $form->isblank("transdate", $locale->text('Order Date missing!'))
 
1413       if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
 
1415     # also copy deliverydate from the order
 
1416     $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
 
1417     $form->{orddate}      = $form->{transdate};
 
1419     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
 
1420     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
 
1421     $form->{ordnumber}    = "";
 
1422     $form->{quodate}      = $form->{transdate};
 
1425   $payment_id = $form->{payment_id} if $form->{payment_id};
 
1427   # if the name changed get new values
 
1428   if (&check_name($form->{vc})) {
 
1429     $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
 
1434   $form->{cp_id} *= 1;
 
1436   for $i (1 .. $form->{rowcount}) {
 
1437     for (qw(ship qty sellprice listprice basefactor)) {
 
1438       $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
 
1442   if (   $form->{type} =~ /_order/
 
1443       && $form->{currency} ne $form->{defaultcurrency}) {
 
1445     # check if we need a new exchangerate
 
1446     $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
 
1448     $orddate      = $form->current_date(\%myconfig);
 
1449     $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
 
1451     if (!$exchangerate) {
 
1452       &backorder_exchangerate($orddate, $buysell);
 
1457   $form->{convert_from_oe_ids} = $form->{id};
 
1458   $form->{transdate}           = $form->{invdate} = $form->current_date(\%myconfig);
 
1459   $form->{duedate}             = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
 
1460   $form->{shipto}              = 1;
 
1461   $form->{defaultcurrency}     = $form->get_default_currency(\%myconfig);
 
1463   delete @{$form}{qw(id closed)};
 
1464   $form->{rowcount}--;
 
1466   if ($form->{type} =~ /_order$/) {
 
1467     $form->{exchangerate} = $exchangerate;
 
1471   if (   $form->{type} eq 'purchase_order'
 
1472       || $form->{type} eq 'request_quotation') {
 
1473     $form->{title}  = $locale->text('Add Vendor Invoice');
 
1474     $form->{script} = 'ir.pl';
 
1479   if (   $form->{type} eq 'sales_order'
 
1480       || $form->{type} eq 'sales_quotation') {
 
1481     $form->{title}  = $locale->text('Add Sales Invoice');
 
1482     $form->{script} = 'is.pl';
 
1487   # bo creates the id, reset it
 
1488   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
 
1489   $form->{ $form->{vc} } =~ s/--.*//g;
 
1490   $form->{type} = "invoice";
 
1493   $locale = new Locale "$myconfig{countrycode}", "$script";
 
1495   require "bin/mozilla/$form->{script}";
 
1497   map { $form->{"select$_"} = "" } ($form->{vc}, currency);
 
1499   $currency = $form->{currency};
 
1502   $form->{currency}     = $currency;
 
1503   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
 
1504   $form->{exchangerate} = $form->{forex} || '';
 
1506   $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
 
1511   for $i (1 .. $form->{rowcount}) {
 
1512     $form->{"discount_$i"} =
 
1513       $form->format_amount(\%myconfig, $form->{"discount_$i"});
 
1515     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
1517     $decimalplaces = ($dec > 2) ? $dec : 2;
 
1519     # copy delivery date from reqdate for order -> invoice conversion
 
1520     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
 
1521       unless $form->{"deliverydate_$i"};
 
1523     $form->{"sellprice_$i"} =
 
1524       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
 
1527     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
1528     $dec_qty = length $dec_qty;
 
1530       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
1532     map { $form->{"${_}_$i"} =~ s/\"/"/g }
 
1533       qw(partnumber description unit);
 
1539   $lxdebug->leave_sub();
 
1542 sub backorder_exchangerate {
 
1543   $lxdebug->enter_sub();
 
1547   my ($orddate, $buysell) = @_;
 
1554 <form method=post action=$form->{script}>
 
1557   # delete action variable
 
1558   map { delete $form->{$_} } qw(action header exchangerate);
 
1560   foreach $key (keys %$form) {
 
1561     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
 
1562     $form->{$key} =~ s/\"/"/g;
 
1563     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
 
1566   $form->{title} = $locale->text('Add Exchangerate');
 
1570 <input type=hidden name=exchangeratedate value=$orddate>
 
1571 <input type=hidden name=buysell value=$buysell>
 
1574   <tr><th class=listtop>$form->{title}</th></tr>
 
1575   <tr height="5"></tr>
 
1580           <th align=right>| . $locale->text('Currency') . qq|</th>
 
1581           <td>$form->{currency}</td>
 
1584           <th align=right>| . $locale->text('Date') . qq|</th>
 
1588           <th align=right>| . $locale->text('Exchangerate') . qq|</th>
 
1589           <td><input name=exchangerate size=11></td>
 
1599 <input type=hidden name=nextsub value=save_exchangerate>
 
1601 <input name=action class=submit type=submit value="|
 
1602     . $locale->text('Continue') . qq|">
 
1610   $lxdebug->leave_sub();
 
1613 sub save_exchangerate {
 
1614   $lxdebug->enter_sub();
 
1616   $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
 
1617   $form->{exchangerate} =
 
1618     $form->parse_amount(\%myconfig, $form->{exchangerate});
 
1619   $form->save_exchangerate(\%myconfig, $form->{currency},
 
1620                            $form->{exchangeratedate},
 
1621                            $form->{exchangerate}, $form->{buysell});
 
1625   $lxdebug->leave_sub();
 
1628 sub create_backorder {
 
1629   $lxdebug->enter_sub();
 
1631   $form->{shipped} = 1;
 
1633   # figure out if we need to create a backorder
 
1634   # items aren't saved if qty != 0
 
1636   for $i (1 .. $form->{rowcount}) {
 
1637     $totalqty  += $qty  = $form->{"qty_$i"};
 
1638     $totalship += $ship = $form->{"ship_$i"};
 
1640     $form->{"qty_$i"} = $qty - $ship;
 
1643   if ($totalship == 0) {
 
1644     map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
 
1645     $form->{ordtotal} = 0;
 
1646     $form->{shipped}  = 0;
 
1650   if ($totalqty == $totalship) {
 
1651     map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
 
1652     $form->{ordtotal} = 0;
 
1657     qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
 
1660   for $i (1 .. $form->{rowcount}) {
 
1662       $form->{"${_}_$i"} =
 
1663         $form->format_amount(\%myconfig, $form->{"${_}_$i"})
 
1664     } qw(sellprice discount);
 
1669   OE->save(\%myconfig, \%$form);
 
1671   # rebuild rows for invoice
 
1675   for $i (1 .. $form->{rowcount}) {
 
1676     $form->{"qty_$i"} = $form->{"ship_$i"};
 
1678     if ($form->{"qty_$i"}) {
 
1681       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
 
1686   $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
 
1687   $form->{rowcount} = $count;
 
1689   $lxdebug->leave_sub();
 
1693   $lxdebug->enter_sub();
 
1697   $form->{saveasnew} = 1;
 
1698   $form->{closed}    = 0;
 
1699   map { delete $form->{$_} } qw(printed emailed queued);
 
1701   # Let Lx-Office assign a new order number if the user hasn't changed the
 
1702   # previous one. If it has been changed manually then use it as-is.
 
1703   my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
 
1704   $form->{$idx} =~ s/^\s*//g;
 
1705   $form->{$idx} =~ s/\s*$//g;
 
1706   if ($form->{saved_xyznumber} &&
 
1707       ($form->{saved_xyznumber} eq $form->{$idx})) {
 
1708     delete($form->{$idx});
 
1713   $lxdebug->leave_sub();
 
1716 sub check_for_direct_delivery_yes {
 
1717   $lxdebug->enter_sub();
 
1721   $form->{direct_delivery_checked} = 1;
 
1722   delete @{$form}{grep /^shipto/, keys %{ $form }};
 
1723   map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
 
1724   $form->{shipto} = 1;
 
1726   $lxdebug->leave_sub();
 
1729 sub check_for_direct_delivery_no {
 
1730   $lxdebug->enter_sub();
 
1734   $form->{direct_delivery_checked} = 1;
 
1735   delete @{$form}{grep /^shipto/, keys %{ $form }};
 
1738   $lxdebug->leave_sub();
 
1741 sub check_for_direct_delivery {
 
1742   $lxdebug->enter_sub();
 
1746   if ($form->{direct_delivery_checked}
 
1747       || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
 
1748     $lxdebug->leave_sub();
 
1752   if ($form->{shipto_id}) {
 
1753     Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
 
1756     map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
 
1759   delete $form->{action};
 
1760   $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
 
1763   print $form->parse_html_template("oe/check_for_direct_delivery");
 
1765   $lxdebug->leave_sub();
 
1770 sub purchase_order {
 
1771   $lxdebug->enter_sub();
 
1774   $auth->assert('purchase_order_edit');
 
1776   if ($form->{type} eq 'sales_order') {
 
1777     check_for_direct_delivery();
 
1780   if ($form->{type} =~ /^sales_/) {
 
1781     delete($form->{ordnumber});
 
1784   $form->{cp_id} *= 1;
 
1786   $form->{title} = $locale->text('Add Purchase Order');
 
1787   $form->{vc}    = "vendor";
 
1788   $form->{type}  = "purchase_order";
 
1792   $lxdebug->leave_sub();
 
1796   $lxdebug->enter_sub();
 
1799   $auth->assert('sales_order_edit');
 
1801   if ($form->{type} eq "purchase_order") {
 
1802     delete($form->{ordnumber});
 
1805   $form->{cp_id} *= 1;
 
1807   $form->{title}  = $locale->text('Add Sales Order');
 
1808   $form->{vc}     = "customer";
 
1809   $form->{type}   = "sales_order";
 
1813   $lxdebug->leave_sub();
 
1817   $lxdebug->enter_sub();
 
1820   $auth->assert('purchase_order_edit | sales_order_edit');
 
1822   $form->{transdate} = $form->current_date(\%myconfig);
 
1823   delete $form->{duedate};
 
1825   $form->{convert_from_oe_ids} = $form->{id};
 
1826   $form->{closed}              = 0;
 
1828   $form->{old_employee_id}     = $form->{employee_id};
 
1829   $form->{old_salesman_id}     = $form->{salesman_id};
 
1832   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered
 
1835   for $i (1 .. $form->{rowcount}) {
 
1836     map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
 
1839   my %saved_vars = map { $_ => $form->{$_} } grep { $form->{$_} } qw(currency);
 
1843   map { $form->{$_} = $saved_vars{$_} } keys %saved_vars;
 
1848   for $i (1 .. $form->{rowcount} - 1) {
 
1849     map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
 
1854   $lxdebug->leave_sub();
 
1857 sub delivery_order {
 
1858   $lxdebug->enter_sub();
 
1860   if ($form->{type} =~ /^sales/) {
 
1861     $auth->assert('sales_delivery_order_edit');
 
1863     $form->{vc}    = 'customer';
 
1864     $form->{type}  = 'sales_delivery_order';
 
1867     $auth->assert('purchase_delivery_order_edit');
 
1869     $form->{vc}    = 'vendor';
 
1870     $form->{type}  = 'purchase_delivery_order';
 
1873   require "bin/mozilla/do.pl";
 
1875   $form->{cp_id}               *= 1;
 
1876   $form->{convert_from_oe_ids}  = $form->{id};
 
1877   $form->{transdate}            = $form->current_date(\%myconfig);
 
1878   delete $form->{duedate};
 
1880   $form->{old_employee_id}  = $form->{employee_id};
 
1881   $form->{old_salesman_id}  = $form->{salesman_id};
 
1884   delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
 
1886   for $i (1 .. $form->{rowcount}) {
 
1887     map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
 
1890   my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
 
1896   map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
 
1900   $lxdebug->leave_sub();
 
1904   $lxdebug->enter_sub();
 
1908   $form->{print_and_save} = 1;
 
1912   my $saved_form = save_form();
 
1916   restore_form($saved_form, 0, qw(id ordnumber quonumber));
 
1920   $lxdebug->leave_sub();
 
1924   call_sub($form->{yes_nextsub});
 
1928   call_sub($form->{no_nextsub});
 
1931 ######################################################################################################
 
1933 # ###############################################################################################
 
1935   $lxdebug->enter_sub();
 
1939   retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
 
1941   $form->{"taxaccounts"} =~ s/\s*$//;
 
1942   $form->{"taxaccounts"} =~ s/^\s*//;
 
1943   foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
 
1944     map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
 
1946   $form->{"taxaccounts"} = "";
 
1948   for my $i (1 .. $form->{"rowcount"}) {
 
1949     IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
 
1952   $form->{rowcount}++;
 
1953   $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
 
1955   $form->language_payment(\%myconfig);
 
1957   Common::webdav_folder($form) if ($webdav);
 
1962   display_row($form->{rowcount}) if $form->{rowcount};
 
1966   $lxdebug->leave_sub();
 
1969 sub report_for_todo_list {
 
1970   $lxdebug->enter_sub();
 
1972   my $quotations = OE->transactions_for_todo_list();
 
1975   if (@{ $quotations }) {
 
1976     my $edit_url = build_std_url('script=oe.pl', 'action=edit');
 
1978     $content     = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
 
1979                                                                            'edit_url'   => $edit_url });
 
1982   $lxdebug->leave_sub();