Fehler bei Lieferadressen die Anfuehrungszeichen im Namen enthalten
[kivitendo-erp.git] / bin / mozilla / oe.pl
index d9ccd05..6941424 100644 (file)
@@ -1,4 +1,4 @@
-#=====================================================================
+# #=====================================================================
 # LX-Office ERP
 # Copyright (C) 2004
 # Based on SQL-Ledger Version 2.1.9
@@ -30,6 +30,7 @@
 # Order entry module
 # Quotation module
 #======================================================================
+use Data::Dumper;
 
 use SL::OE;
 use SL::IR;
@@ -43,26 +44,59 @@ require "$form->{path}/arap.pl";
 
 # end of main
 
-sub add {
+# For locales.pl:
+# $locale->text('Edit the purchase_order');
+# $locale->text('Edit the sales_order');
+# $locale->text('Edit the request_quotation');
+# $locale->text('Edit the sales_quotation');
+
+# $locale->text('Workflow purchase_order');
+# $locale->text('Workflow sales_order');
+# $locale->text('Workflow request_quotation');
+# $locale->text('Workflow sales_quotation');
+
+sub set_headings {
   $lxdebug->enter_sub();
 
+  my ($action) = @_;
+
   if ($form->{type} eq 'purchase_order') {
-    $form->{title} = $locale->text('Add Purchase Order');
-    $form->{vc}    = 'vendor';
+    $form->{title}   = $action eq "edit" ?
+      $locale->text('Edit Purchase Order') :
+      $locale->text('Add Purchase Order');
+    $form->{heading} = $locale->text('Purchase Order');
+    $form->{vc}      = 'vendor';
   }
   if ($form->{type} eq 'sales_order') {
-    $form->{title} = $locale->text('Add Sales Order');
-    $form->{vc}    = 'customer';
+    $form->{title}   = $action eq "edit" ?
+      $locale->text('Edit Sales Order') :
+      $locale->text('Add Sales Order');
+    $form->{heading} = $locale->text('Sales Order');
+    $form->{vc}      = 'customer';
   }
   if ($form->{type} eq 'request_quotation') {
-    $form->{title} = $locale->text('Add Request for Quotation');
-    $form->{vc}    = 'vendor';
+    $form->{title}   = $action eq "edit" ?
+      $locale->text('Edit Request for Quotation') :
+      $locale->text('Add Request for Quotation');
+    $form->{heading} = $locale->text('Request for Quotation');
+    $form->{vc}      = 'vendor';
   }
   if ($form->{type} eq 'sales_quotation') {
-    $form->{title} = $locale->text('Add Quotation');
-    $form->{vc}    = 'customer';
+    $form->{title}   = $action eq "edit" ?
+      $locale->text('Edit Quotation') :
+      $locale->text('Add Quotation');
+    $form->{heading} = $locale->text('Quotation');
+    $form->{vc}      = 'customer';
   }
 
+  $lxdebug->leave_sub();
+}
+
+sub add {
+  $lxdebug->enter_sub();
+
+  set_headings("add");
+
   $form->{callback} =
     "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&path=$form->{path}&password=$form->{password}"
     unless $form->{callback};
@@ -77,29 +111,42 @@ sub add {
 sub edit {
   $lxdebug->enter_sub();
 
-  if ($form->{type} eq 'purchase_order') {
-    $form->{title}   = $locale->text('Edit Purchase Order');
-    $form->{heading} = $locale->text('Purchase Order');
-    $form->{vc}      = 'vendor';
-  }
-  if ($form->{type} eq 'sales_order') {
-    $form->{title}   = $locale->text('Edit Sales Order');
-    $form->{heading} = $locale->text('Sales Order');
-    $form->{vc}      = 'customer';
-  }
-  if ($form->{type} eq 'request_quotation') {
-    $form->{title}   = $locale->text('Edit Request for Quotation');
-    $form->{heading} = $locale->text('Request for Quotation');
-    $form->{vc}      = 'vendor';
+  $form->{simple_save} = 0;
+
+  set_headings("edit");
+
+  # editing without stuff to edit? try adding it first
+  if ($form->{rowcount}) {
+    map { $id++ if $form->{"id_$_"} } (1 .. $form->{rowcount});
+    if (!$id) {
+
+      # reset rowcount
+      undef $form->{rowcount};
+      &add;
+      return;
+    }
+  } else {
+    if (!$form->{id}) {
+      &add;
+      return;
+    }
   }
-  if ($form->{type} eq 'sales_quotation') {
-    $form->{title}   = $locale->text('Edit Quotation');
-    $form->{heading} = $locale->text('Quotation');
-    $form->{vc}      = 'customer';
+
+  if ($form->{print_and_save}) {
+    $form->{action}   = "print";
+    $form->{resubmit} = 1;
+    $language_id = $form->{language_id};
+    $printer_id = $form->{printer_id};
   }
 
+  set_headings("edit");
+
   &order_links;
   &prepare_order;
+  if ($form->{print_and_save}) {
+    $form->{language_id} = $language_id;
+    $form->{printer_id} = $printer_id;
+  }
   &display_form;
 
   $lxdebug->leave_sub();
@@ -114,12 +161,37 @@ sub order_links {
 
   # retrieve order/quotation
   $form->{webdav} = $webdav;
-
   # set jscalendar
   $form->{jscalendar} = $jscalendar;
 
   OE->retrieve(\%myconfig, \%$form);
 
+  if ($form->{payment_id}) {
+    $payment_id = $form->{payment_id};
+  }
+  if ($form->{language_id}) {
+    $language_id = $form->{language_id};
+  }
+  if ($form->{taxzone_id}) {
+    $taxzone_id = $form->{taxzone_id};
+  }
+
+
+  # if multiple rowcounts (== collective order) then check if the
+  # there were more than one customer (in that case OE::retrieve removes
+  # the content from the field)
+  if (   $form->{rowcount}
+      && $form->{type} eq 'sales_order'
+      && defined $form->{customer}
+      && $form->{customer} eq '') {
+
+    #    $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
+    $form->error(
+                 $locale->text(
+                   'Collective Orders only work for orders from one customer!')
+    );
+  }
+
   $taxincluded = $form->{taxincluded};
   $form->{shipto} = 1 if $form->{id};
 
@@ -135,12 +207,32 @@ sub order_links {
   # get customer / vendor
   if ($form->{type} =~ /(purchase_order|request_quotation|receive_order)/) {
     IR->get_vendor(\%myconfig, \%$form);
+
+    #quote all_vendor Bug 133
+    foreach $ref (@{ $form->{all_vendor} }) {
+      $ref->{name} = $form->quote($ref->{name});
+    }
+
   }
   if ($form->{type} =~ /(sales|ship)_(order|quotation)/) {
     IS->get_customer(\%myconfig, \%$form);
+
+    #quote all_vendor Bug 133
+    foreach $ref (@{ $form->{all_customer} }) {
+      $ref->{name} = $form->quote($ref->{name});
+    }
+
   }
   $form->{cp_id} = $cp_id;
-
+  if ($payment_id) {
+    $form->{payment_id} = $payment_id;
+  }
+  if ($language_id) {
+    $form->{language_id} = $language_id;
+  }
+  if ($taxzone_id) {
+    $form->{taxzone_id} = $taxzone_id;
+  }
   $form->{intnotes} = $intnotes;
   ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
   $form->{"old$form->{vc}"} =
@@ -155,7 +247,7 @@ sub order_links {
   }
 
   # currencies
-  @curr = split /:/, $form->{currencies};
+  @curr = split(/:/, $form->{currencies});
   chomp $curr[0];
   $form->{defaultcurrency} = $curr[0];
   $form->{currency}        = $form->{defaultcurrency} unless $form->{currency};
@@ -192,34 +284,42 @@ sub order_links {
 
 sub prepare_order {
   $lxdebug->enter_sub();
-  $form->{format}   = "html";
   $form->{media}    = "screen";
-  $form->{formname} = $form->{type};
+  $form->{formname} = $form->{type} unless $form->{formname};
 
-  if ($form->{id}) {
-
-    map { $form->{$_} =~ s/\"/"/g }
-      qw(ordnumber quonumber shippingpoint shipvia notes intnotes shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact);
+  my $i = 0;
+  foreach $ref (@{ $form->{form_details} }) {
+    $form->{rowcount} = ++$i;
 
-    foreach $ref (@{ $form->{form_details} }) {
-      $i++;
-      map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
+    map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
+  }
+  for my $i (1 .. $form->{rowcount}) {
+    if ($form->{id}) {
       $form->{"discount_$i"} =
         $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
+    } else {
+      $form->{"discount_$i"} =
+        $form->format_amount(\%myconfig, $form->{"discount_$i"});
+    }
+    ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
+    $dec           = length $dec;
+    $decimalplaces = ($dec > 2) ? $dec : 2;
 
-      ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
-      $dec           = length $dec;
-      $decimalplaces = ($dec > 2) ? $dec : 2;
+    # copy reqdate from deliverydate for invoice -> order conversion
+    $form->{"reqdate_$i"} = $form->{"deliverydate_$i"}
+      unless $form->{"reqdate_$i"};
 
-      $form->{"sellprice_$i"} =
-        $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
-                             $decimalplaces);
-      $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
+    $form->{"sellprice_$i"} =
+      $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
+                           $decimalplaces);
 
-      map { $form->{"${_}_$i"} =~ s/\"/"/g }
-        qw(partnumber description unit);
-      $form->{rowcount} = $i;
-    }
+    (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
+    $dec_qty = length $dec_qty;
+    $form->{"qty_$i"} =
+      $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
+
+    map { $form->{"${_}_$i"} =~ s/\"/"/g }
+      qw(partnumber description unit);
   }
 
   $lxdebug->leave_sub();
@@ -231,24 +331,35 @@ sub form_header {
   $checkedopen   = ($form->{closed}) ? ""        : "checked";
   $checkedclosed = ($form->{closed}) ? "checked" : "";
 
+  map { $form->{$_} =~ s/\"/"/g }
+    qw(ordnumber quonumber shippingpoint shipvia notes intnotes shiptoname
+       shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
+       shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
+
   # use JavaScript Calendar or not
   $form->{jsscript} = $form->{jscalendar};
   $jsscript = "";
 
+  $payment = qq|<option value=""></option>|;
+  foreach $item (@{ $form->{payment_terms} }) {
+    if ($form->{payment_id} eq $item->{id}) {
+      $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
+    } else {
+      $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
+    }
+  }
   if ($form->{jsscript}) {
 
     # with JavaScript Calendar
     $button1 = qq|
        <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
        <td><input type=button name=transdate id="trigger1" value=|
-      . $locale->text('button')
-      . qq|></td>            
+      . $locale->text('button') . qq|></td>
       |;
     $button2 = qq|
        <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value=$form->{reqdate}></td>
        <td width="4"><input type=button name=reqdate name=reqdate id="trigger2" value=|
-      . $locale->text('button')
-      . qq|></td>
+      . $locale->text('button') . qq|></td>
      |;
 
     #write Trigger
@@ -272,11 +383,9 @@ sub form_header {
          <table>
            <tr>
              <th nowrap><input name=closed type=radio class=radio value=0 $checkedopen> |
-      . $locale->text('Open')
-      . qq|</th>
+      . $locale->text('Open') . qq|</th>
              <th nowrap><input name=closed type=radio class=radio value=1 $checkedclosed> |
-      . $locale->text('Closed')
-      . qq|</th>
+      . $locale->text('Closed') . qq|</th>
            </tr>
          </table>
        </td>
@@ -285,29 +394,73 @@ sub form_header {
   }
 
   # set option selected
-  foreach $item ($form->{vc}, currency, department, employee, contact) {
+  foreach $item ($form->{vc}, currency, department, employee) {
     $form->{"select$item"} =~ s/ selected//;
     $form->{"select$item"} =~
       s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
   }
 
+  #quote select[customer|vendor] Bug 133
+  $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
+
   #build contacts
   if ($form->{all_contacts}) {
 
-    $form->{selectcontact} = "";
+    $form->{selectcontact} = "<option></option>";
     foreach $item (@{ $form->{all_contacts} }) {
+      my $department = ($item->{cp_abteilung}) ? "--$item->{cp_abteilung}" : "";
       if ($form->{cp_id} == $item->{cp_id}) {
         $form->{selectcontact} .=
-          "<option selected>$item->{cp_name}--$item->{cp_id}";
+          "<option value=$item->{cp_id} selected>$item->{cp_name}$department</option>";
       } else {
-        $form->{selectcontact} .= "<option>$item->{cp_name}--$item->{cp_id}";
+        $form->{selectcontact} .= "<option value=$item->{cp_id}>$item->{cp_name}$department</option>";
       }
     }
+  } else {
+    $form->{selectcontact} =~ s/ selected//g;
+    if ($form->{cp_id} ne "") {
+      $form->{selectcontact} =~ s/value=$form->{cp_id}/value=$form->{cp_id} selected/;
+    }
   }
 
+
+  if (@{ $form->{SHIPTO} }) {
+    $form->{selectshipto} = "<option value=0></option>";
+    foreach $item (@{ $form->{SHIPTO} }) {
+      if ($item->{shipto_id} == $form->{shipto_id}) {
+        $form->{selectshipto} .=
+          "<option value=$item->{shipto_id} selected>$item->{shiptoname} $item->{shiptodepartment_1}</option>";
+      } else {
+        $form->{selectshipto} .=
+          "<option value=$item->{shipto_id}>$item->{shiptoname} $item->{shiptodepartment}</option>";
+      }
+
+    }
+  } else {
+    $form->{selectshipto} = $form->unquote($form->{selectshipto});
+    $form->{selectshipto} =~ s/ selected//g;
+    if ($form->{shipto_id} ne "") {
+      $form->{selectshipto} =~ s/value=$form->{shipto_id}/value=$form->{shipto_id} selected/;
+    }
+  }
+
+  $shipto = qq|
+               <th align=right>| . $locale->text('Shipping Address') . qq|</th>
+               <td><select name=shipto_id style="width:200px;">$form->{selectshipto}</select></td>|;
+  $form->{selectshipto} = $form->quote($form->{selectshipto});
+  $shipto .= qq| <input type=hidden name=selectshipto value="$form->{selectshipto}">|;
+
+
+
   $form->{exchangerate} =
     $form->format_amount(\%myconfig, $form->{exchangerate});
 
+  if (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) {
+    $creditwarning = 1;
+  } else {
+    $creditwarning = 0;
+  }
+
   $form->{creditlimit} =
     $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
   $form->{creditremaining} =
@@ -315,7 +468,7 @@ sub form_header {
 
   $contact =
     ($form->{selectcontact})
-    ? qq|<select name=contact>$form->{selectcontact}</select>\n<input type=hidden name="selectcontact" value="$form->{selectcontact}">|
+    ? qq|<select name=cp_id>$form->{selectcontact}</select>\n<input type=hidden name="selectcontact" value="$form->{selectcontact}">|
     : qq|<input name=contact value="$form->{contact}" size=35>|;
 
   $exchangerate = qq|
@@ -341,14 +494,8 @@ sub form_header {
   $vclabel = ucfirst $form->{vc};
   $vclabel = $locale->text($vclabel);
 
-  $terms = qq|
-                    <tr>
-                     <th align=right nowrap>| . $locale->text('Terms: Net') . qq|</th>
-                     <td nowrap><input name=terms size="3" maxlength="3" value=$form->{terms}> |
-    . $locale->text('days')
-    . qq|</td>
-                    </tr>
-|;
+  $terms = qq|<input name=terms size="3" maxlength="3" value="| .
+    $form->quote($form->{terms}) . qq|">|;
 
   if ($form->{business}) {
     $business = qq|
@@ -363,6 +510,53 @@ sub form_header {
 |;
   }
 
+  if ($form->{max_dunning_level}) {
+    $dunning = qq|
+             <tr>
+                <td colspan=4>
+                <table>
+                  <tr>
+               <th align=right>| . $locale->text('Max. Dunning Level') . qq|:</th>
+               <td><b>$form->{max_dunning_level}</b></td>
+               <th align=right>| . $locale->text('Dunning Amount') . qq|:</th>
+               <td><b>|
+      . $form->format_amount(\%myconfig, $form->{dunning_amount},2)
+      . qq|</b></td>
+             </tr>
+              </table>
+             </td>
+            </tr>
+|;
+  }
+
+  if (@{ $form->{TAXZONE} }) {
+    $form->{selecttaxzone} = "";
+    foreach $item (@{ $form->{TAXZONE} }) {
+      if ($item->{id} == $form->{taxzone_id}) {
+        $form->{selecttaxzone} .=
+          "<option value=$item->{id} selected>" . H($item->{description}) .
+          "</option>";
+      } else {
+        $form->{selecttaxzone} .=
+          "<option value=$item->{id}>" . H($item->{description}) . "</option>";
+      }
+
+    }
+  } else {
+    $form->{selecttaxzone} =~ s/ selected//g;
+    if ($form->{taxzone_id} ne "") {
+      $form->{selecttaxzone} =~ s/value=$form->{taxzone_id}>/value=$form->{taxzone_id} selected>/;
+    }
+  }
+
+  $taxzone = qq|
+             <tr>
+               <th align=right>| . $locale->text('Steuersatz') . qq|</th>
+               <td><select name=taxzone_id>$form->{selecttaxzone}</select></td>
+               <input type=hidden name=selecttaxzone value="$form->{selecttaxzone}">
+             </tr>|;
+
+
   if ($form->{type} !~ /_quotation$/) {
     $ordnumber = qq|
              <tr>
@@ -371,14 +565,12 @@ sub form_header {
              </tr>
              <tr>
                <th width=70% align=right nowrap>|
-      . $locale->text('Quotation Number')
-      . qq|</th>
+      . $locale->text('Quotation Number') . qq|</th>
                 <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
              </tr>
               <tr>
                <th width=70% align=right nowrap>|
-      . $locale->text('Customer Order Number')
-      . qq|</th>
+      . $locale->text('Customer Order Number') . qq|</th>
                 <td><input name=cusordnumber size=11 value="$form->{cusordnumber}"></td>
              </tr>
              <tr>
@@ -404,10 +596,11 @@ sub form_header {
                      <td>$form->{creditlimit}</td>
                      <td width=20%></td>
                      <th nowrap>| . $locale->text('Remaining') . qq|</th>
-                     <td class="plus$n">$form->{creditremaining}</td>
+                     <td class="plus$n" nowrap>$form->{creditremaining}</td>
                    </tr>
                  </table>
                </td>
+                $shipto
              </tr>
 |;
   } else {
@@ -419,8 +612,7 @@ sub form_header {
       $ordnumber = qq|
              <tr>
                <th width=70% align=right nowrap>|
-        . $locale->text('Quotation Number')
-        . qq|</th>
+        . $locale->text('Quotation Number') . qq|</th>
                <td><input name=quonumber size=11 value="$form->{quonumber}"></td>
                <input type=hidden name=ordnumber value="$form->{ordnumber}">
              </tr>
@@ -447,7 +639,10 @@ sub form_header {
                 $button2
              </tr>
 |;
-
+    $creditremaining = qq| <tr>
+                            <td colspan=4></td>
+                            $shipto
+                          </tr>|;
   }
 
   $vc =
@@ -471,6 +666,7 @@ sub form_header {
   if ($form->{type} eq 'sales_order') {
     if ($form->{selectemployee}) {
       $employee = qq|
+    <input type=hidden name=customer_klass value=$form->{customer_klass}>
              <tr>
                <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
                <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
@@ -481,6 +677,7 @@ sub form_header {
     }
   } else {
     $employee = qq|
+    <input type=hidden name=customer_klass value=$form->{customer_klass}>
              <tr>
                <th align=right nowrap>| . $locale->text('Employee') . qq|</th>
                <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
@@ -489,20 +686,39 @@ sub form_header {
              </tr>
 |;
   }
+  if ($form->{resubmit} && ($form->{format} eq "html")) {
+    $onload =
+      qq|window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()|;
+  } elsif ($form->{resubmit}) {
+    $onload = qq|document.oe.submit()|;
+  } else {
+    $onload = "fokus()";
+  }
+
+  $credittext = $locale->text('Credit Limit exceeded!!!');
+  if ($creditwarning) {
+    $onload = qq|alert('$credittext')|;
+  }
 
   $form->header;
 
   print qq|
-<body>
+<body onLoad="$onload">
 
-<form method=post action=$form->{script}>
+<form method=post name=oe action=$form->{script}>
+ <script type="text/javascript" src="js/common.js"></script>
+ <script type="text/javascript" src="js/delivery_customer_selection.js"></script>
+ <script type="text/javascript" src="js/vendor_selection.js"></script>
+ <script type="text/javascript" src="js/calculate_qty.js"></script>
 
 <input type=hidden name=id value=$form->{id}>
+<input type=hidden name=action value=$form->{action}>
 
 <input type=hidden name=type value=$form->{type}>
 <input type=hidden name=formname value=$form->{formname}>
 <input type=hidden name=media value=$form->{media}>
 <input type=hidden name=format value=$form->{format}>
+<input type=hidden name=proforma value=$form->{proforma}>
 
 <input type=hidden name=queued value="$form->{queued}">
 <input type=hidden name=printed value="$form->{printed}">
@@ -537,12 +753,13 @@ sub form_header {
                <input type=hidden name=$form->{vc}_id value=$form->{"$form->{vc}_id"}>
                <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
                 <th align=richt nowrap>|
-    . $locale->text('Contact Person')
-    . qq|</th>
+    . $locale->text('Contact Person') . qq|</th>
                 <td colspan=3>$contact</td>
              </tr>
              $creditremaining
              $business
+              $dunning
+              $taxzone
              $department
              <tr>
                <th align=right>| . $locale->text('Currency') . qq|</th>
@@ -558,8 +775,28 @@ sub form_header {
              <tr>
                <th align=right>| . $locale->text('Ship via') . qq|</th>
                <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
-             </tr>
-           </table>
+             </tr>|;
+#              <tr>
+#                 <td colspan=4>
+#                   <table>
+#                     <tr>
+#                       <td colspan=2>
+#                         <button type="button" onclick="delivery_customer_selection_window('delivery_customer_string','delivery_customer_id')">| . $locale->text('Choose Customer') . qq|</button>
+#                       </td>
+#                       <td colspan=2><input type=hidden name=delivery_customer_id value="$form->{delivery_customer_id}">
+#                       <input size=45 id=delivery_customer_string name=delivery_customer_string value="$form->{delivery_customer_string}"></td>
+#                     </tr>
+#                     <tr>
+#                       <td colspan=2>
+#                         <button type="button" onclick="vendor_selection_window('delivery_vendor_string','delivery_vendor_id')">| . $locale->text('Choose Vendor') . qq|</button>
+#                       </td>
+#                       <td colspan=2><input type=hidden name=delivery_vendor_id value="$form->{delivery_vendor_id}">
+#                       <input size=45 id=vendor_string name=delivery_vendor_string value="$form->{delivery_vendor_string}"></td>
+#                     </tr>
+#                   </table>
+#                 </td>
+#               </tr>
+print qq|          </table>
          </td>
          <td align=right>
            <table>
@@ -573,7 +810,7 @@ sub form_header {
       </table>
     </td>
   </tr>
-  
+
 $jsscript
 
 <!-- shipto are in hidden variables -->
@@ -586,10 +823,12 @@ $jsscript
 <input type=hidden name=shiptocontact value="$form->{shiptocontact}">
 <input type=hidden name=shiptophone value="$form->{shiptophone}">
 <input type=hidden name=shiptofax value="$form->{shiptofax}">
+<input type=hidden name=shiptodepartment_1 value="$form->{shiptodepartment_1}">
+<input type=hidden name=shiptodepartment_2 value="$form->{shiptodepartment_2}">
 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
 
 <!-- email variables -->
-<input type=hidden name=message value="$form->{message}"> 
+<input type=hidden name=message value="$form->{message}">
 <input type=hidden name=email value="$form->{email}">
 <input type=hidden name=subject value="$form->{subject}">
 <input type=hidden name=cc value="$form->{cc}">
@@ -633,8 +872,7 @@ sub form_footer {
   if ($form->{taxaccounts}) {
     $taxincluded = qq|
              <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
-      . $locale->text('Tax Included')
-      . qq|</b><br><br>
+      . $locale->text('Tax Included') . qq|</b><br><br>
 |;
   }
 
@@ -722,6 +960,9 @@ sub form_footer {
                <td>$notes</td>
                <td>$intnotes</td>
              </tr>
+         <th align=right>| . $locale->text('Payment Terms') . qq|</th>
+         <td><select name=payment_id tabindex=24>$payment
+                          </select></td>
            </table>
          </td>
          <td align=right width=100%>
@@ -749,7 +990,7 @@ sub form_footer {
   if ($webdav) {
     $webdav_list = qq|
 
-  <tr>   
+  <tr>
     <th class=listtop align=left>Dokumente im Webdav-Repository</th>
   </tr>
     <table width=100%>
@@ -758,7 +999,7 @@ sub form_footer {
 |;
     foreach $file (keys %{ $form->{WEBDAV} }) {
       $webdav_list .= qq|
-      <tr>     
+      <tr>
         <td align=left>$file</td>
         <td align=left><a href="$form->{WEBDAV}{$file}">$form->{WEBDAV}{$file}</a></td>
       </tr>
@@ -788,7 +1029,7 @@ sub form_footer {
   </tr>
 </table>
 
-Bearbeiten des $form->{heading}<br>
+| . $locale->text("Edit the $form->{type}") . qq|<br>
 <input class=submit type=submit name=action value="|
     . $locale->text('Update') . qq|">
 <input class=submit type=submit name=action value="|
@@ -799,24 +1040,33 @@ Bearbeiten des $form->{heading}<br>
     . $locale->text('E-mail') . qq|">
 <input class=submit type=submit name=action value="|
     . $locale->text('Save') . qq|">
+<input class=submit type=submit name=action value="|
+    . $locale->text('Save and Close') . qq|">
 |;
 
-  if ($form->{id}) {
+  if (($form->{id})) {
     print qq|
-<br>Workflow  $form->{heading}<br>
+<br>| . $locale->text("Workflow $form->{type}") . qq|<br>
 <input class=submit type=submit name=action value="|
       . $locale->text('Save as new') . qq|">
 <input class=submit type=submit name=action value="|
       . $locale->text('Delete') . qq|">|;
-    if ($form->{type} =~ /quotation$/) {
+    if (($form->{type} =~ /sales_quotation$/)) {
       print qq|
 <input class=submit type=submit name=action value="|
-        . $locale->text('Order') . qq|">|;
+        . $locale->text('Sales Order') . qq|">|;
     }
-    print qq|   
+    if ($form->{type} =~ /request_quotation$/) {
+      print qq|
+<input class=submit type=submit name=action value="|
+        . $locale->text('Purchase Order') . qq|">|;
+    }
+    if (1) {
+    print qq|
 <input class=submit type=submit name=action value="|
       . $locale->text('Invoice') . qq|">
 |;
+}
 
     if ($form->{type} =~ /sales_order$/) {
       print qq|
@@ -833,7 +1083,7 @@ Bearbeiten des $form->{heading}<br>
 <input class=submit type=submit name=action value="|
         . $locale->text('Sales Order') . qq|">
 <input class=submit type=submit name=action value="|
-        . $locale->text('Quotation') . qq|">
+        . $locale->text('Request for Quotation') . qq|">
 |;
 
     } else {
@@ -843,6 +1093,14 @@ Bearbeiten des $form->{heading}<br>
         . $locale->text('Order') . qq|">
 |;
     }
+  } elsif ($form->{type} =~ /sales_order$/ && $form->{rowcount} && !$form->{proforma}) {
+    print qq|
+<br>Workflow  $form->{heading}<br>
+<input class=submit type=submit name=action value="|
+      . $locale->text('Save as new') . qq|">
+<input class=submit type=submit name=action value="|
+      . $locale->text('Invoice') . qq|">
+|;
   }
 
   if ($form->{menubar}) {
@@ -850,7 +1108,7 @@ Bearbeiten des $form->{heading}<br>
     &menubar;
   }
 
-  print qq| 
+  print qq|
 
 <input type=hidden name=rowcount value=$form->{rowcount}>
 
@@ -871,8 +1129,11 @@ Bearbeiten des $form->{heading}<br>
 sub update {
   $lxdebug->enter_sub();
 
+  set_headings($form->{"id"} ? "edit" : "add");
+
   map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
     qw(exchangerate creditlimit creditremaining);
+  $form->{update} = 1;
 
   &check_name($form->{vc});
 
@@ -888,7 +1149,9 @@ sub update {
                     \%myconfig, $form->{currency}, $form->{transdate}, $buysell
                     )));
 
-  my $i = $form->{rowcount};
+  # for pricegroups
+  $i = $form->{rowcount};
+
   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
 
   if (   ($form->{"partnumber_$i"} eq "")
@@ -898,7 +1161,7 @@ sub update {
     $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
     &check_form;
 
-      } else {
+  } else {
 
     if (   $form->{type} eq 'purchase_order'
         || $form->{type} eq 'request_quotation') {
@@ -924,11 +1187,16 @@ sub update {
       } else {
 
         $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
-
+        if ($form->{"not_discountable_$i"}) {
+          $form->{"discount_$i"} = 0;
+        }
         map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }
           qw(partnumber description unit);
         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
           keys %{ $form->{item_list}[0] };
+        if ($form->{"part_payment_id_$i"} ne "") {
+          $form->{payment_id} = $form->{"part_payment_id_$i"};
+        }
 
         $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
 
@@ -940,6 +1208,8 @@ sub update {
           $form->{"sellprice_$i"} = $sellprice;
         } else {
 
+          $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
+
           # if there is an exchange rate adjust sellprice
           $form->{"sellprice_$i"} /= $exchangerate;
         }
@@ -960,7 +1230,13 @@ sub update {
           $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
                                $decimalplaces);
         $form->{"qty_$i"} =
-          $form->format_amount(\%myconfig, $form->{"qty_$i"});
+          $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
+
+        # get pricegroups for parts
+        IS->get_pricegroups_for_parts(\%myconfig, \%$form);
+
+        # build up html code for prices_$i
+        &set_pricegroup($i);
       }
 
       &display_form;
@@ -976,7 +1252,7 @@ sub update {
         $form->{rowcount}--;
         $form->{"discount_$i"} = "";
         &display_form;
-          } else {
+      } else {
 
         $form->{"id_$i"}   = 0;
         $form->{"unit_$i"} = $locale->text('ea');
@@ -986,6 +1262,7 @@ sub update {
       }
     }
   }
+
   $lxdebug->leave_sub();
 }
 
@@ -1046,7 +1323,8 @@ sub search {
       $form->{warehouse}       = qq|$form->{warehouse}--$form->{warehouse_id}|;
 
       map {
-        $form->{selectwarehouse} .= "<option>$_->{description}--$_->{id}\n"
+        $form->{selectwarehouse} .=
+          "<option>$_->{description}--$_->{id}\n"
       } (@{ $form->{all_warehouses} });
 
       $warehouse = qq|
@@ -1088,8 +1366,8 @@ sub search {
     } (@{ $form->{all_departments} });
   }
 
-  $department = qq|  
-        <tr>  
+  $department = qq|
+        <tr>
          <th align=right nowrap>| . $locale->text('Department') . qq|</th>
          <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
        </tr>
@@ -1099,11 +1377,9 @@ sub search {
     $openclosed = qq|
              <tr>
                <td><input name="open" class=checkbox type=checkbox value=1 checked> |
-      . $locale->text('Open')
-      . qq|</td>
+      . $locale->text('Open') . qq|</td>
                <td><input name="closed" class=checkbox type=checkbox value=1 $form->{closed}> |
-      . $locale->text('Closed')
-      . qq|</td>
+      . $locale->text('Closed') . qq|</td>
              </tr>
 |;
   } else {
@@ -1122,14 +1398,12 @@ sub search {
     $button1 = qq|
        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}">
        <input type=button name=transdatefrom id="trigger3" value=|
-      . $locale->text('button')
-      . qq|></td>
+      . $locale->text('button') . qq|></td>
       |;
     $button2 = qq|
        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}">
        <input type=button name=transdateto name=transdateto id="trigger4" value=|
-      . $locale->text('button')
-      . qq|></td>
+      . $locale->text('button') . qq|></td>
      |;
 
     #write Trigger
@@ -1187,34 +1461,27 @@ sub search {
                | . $locale->text('ID') . qq|</td>
                <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
                <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> |
-    . $locale->text('Date')
-    . qq|</td>
+    . $locale->text('Date') . qq|</td>
                <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> |
-    . $locale->text('Required by')
-    . qq|</td>
+    . $locale->text('Required by') . qq|</td>
              </tr>
              <tr>
                <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vclabel</td>
                <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
                <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> |
-    . $locale->text('Ship via')
-    . qq|</td>
+    . $locale->text('Ship via') . qq|</td>
              </tr>
              <tr>
                <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |
-    . $locale->text('Amount')
-    . qq|</td>
+    . $locale->text('Amount') . qq|</td>
                <td><input name="l_tax" class=checkbox type=checkbox value=Y> |
-    . $locale->text('Tax')
-    . qq|</td>
+    . $locale->text('Tax') . qq|</td>
                <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |
-    . $locale->text('Total')
-    . qq|</td>
+    . $locale->text('Total') . qq|</td>
              </tr>
              <tr>
                <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |
-    . $locale->text('Subtotal')
-    . qq|</td>
+    . $locale->text('Subtotal') . qq|</td>
              </tr>
            </table>
           </td>
@@ -1294,6 +1561,11 @@ sub orders {
     }
   }
 
+  # only show checkboxes if gotten here via sales_order form.
+  if ($form->{type} =~ /sales_order/) {
+    unshift @column_index, "ids";
+  }
+
   if ($form->{l_subtotal} eq 'Y') {
     $callback .= "&l_subtotal=Y";
     $href     .= "&l_subtotal=Y";
@@ -1342,7 +1614,9 @@ sub orders {
     . qq|</a></th>|;
   $column_header{quonumber} =
       qq|<th><a class=listheading href=$href&sort=quonumber>|
-    . $locale->text('Quotation')
+    . ($form->{"type"} eq "request_quotation" ?
+       $locale->text('RFQ') :
+       $locale->text('Quotation'))
     . qq|</a></th>|;
   $column_header{name} =
     qq|<th><a class=listheading href=$href&sort=name>$name</a></th>|;
@@ -1366,6 +1640,8 @@ sub orders {
   $column_header{employee} =
     qq|<th><a class=listheading href=$href&sort=employee>$employee</a></th>|;
 
+  $column_header{ids} = qq|<th></th>|;
+
   if ($form->{ $form->{vc} }) {
     $option = $locale->text(ucfirst $form->{vc});
     $option .= " : $form->{$form->{vc}}";
@@ -1405,6 +1681,7 @@ sub orders {
   print qq|
 <body>
 
+<form method="post" action="oe.pl">
 <table width=100%>
   <tr>
     <th class=listtop>$form->{title}</th>
@@ -1425,7 +1702,7 @@ sub orders {
 |;
 
   # add sort and escape callback
-  $callback = $form->escape($callback . "&sort=$form->{sort}");
+  $callback_escaped = $form->escape($callback . "&sort=$form->{sort}");
 
   if (@{ $form->{OE} }) {
     $sameitem = $form->{OE}->[0]->{ $form->{sort} };
@@ -1437,6 +1714,7 @@ sub orders {
   $warehouse = $form->escape($form->{warehouse});
 
   foreach $oe (@{ $form->{OE} }) {
+    $form->{rowcount} = ++$j;
 
     if ($form->{l_subtotal} eq 'Y') {
       if ($sameitem ne $oe->{ $form->{sort} }) {
@@ -1465,12 +1743,14 @@ sub orders {
     $subtotalnetamount += $oe->{netamount};
     $subtotalamount    += $oe->{amount};
 
+    $column_data{ids} =
+      qq|<td><input name="id_$j" class=checkbox type=checkbox><input type="hidden" name="trans_id_$j" value="$oe->{id}"></td>|;
     $column_data{id}        = "<td>$oe->{id}</td>";
     $column_data{transdate} = "<td>$oe->{transdate}&nbsp;</td>";
     $column_data{reqdate}   = "<td>$oe->{reqdate}&nbsp;</td>";
 
     $column_data{$ordnumber} =
-      "<td><a href=oe.pl?path=$form->{path}&action=$action&type=$form->{type}&id=$oe->{id}&warehouse=$warehouse&vc=$form->{vc}&login=$form->{login}&password=$form->{password}&callback=$callback>$oe->{$ordnumber}</a></td>";
+      "<td><a href=oe.pl?path=$form->{path}&action=$action&type=$form->{type}&id=$oe->{id}&warehouse=$warehouse&vc=$form->{vc}&login=$form->{login}&password=$form->{password}&callback=$callback_escaped>$oe->{$ordnumber}</a></td>";
     $column_data{name} = "<td>$oe->{name}</td>";
 
     $column_data{employee} = "<td>$oe->{employee}&nbsp;</td>";
@@ -1528,7 +1808,27 @@ sub orders {
   <tr>
     <td><hr size=3 noshade></td>
   </tr>
-</table>
+</table>|;
+
+  # multiple invoice edit button only if gotten there via sales_order form.
+
+  if ($form->{type} =~ /sales_order/) {
+    print qq|
+  <input type="hidden" name="path" value="$form->{path}">
+  <input class"submit" type="submit" name="action" value="|
+      . $locale->text('Continue') . qq|">
+  <input type="hidden" name="nextsub" value="edit">
+  <input type="hidden" name="type" value="$form->{type}">
+  <input type="hidden" name="warehouse" value="$warehouse">
+  <input type="hidden" name="vc" value="$form->{vc}">
+  <input type="hidden" name="login" value="$form->{login}">
+  <input type="hidden" name="password" value="$form->{password}">
+  <input type="hidden" name="callback" value="$callback">
+  <input type="hidden" name="rowcount" value="$form->{rowcount}">|;
+  }
+
+  print qq|
+</form>
 
 <br>
 <form method=post action=$form->{script}>
@@ -1597,7 +1897,7 @@ sub subtotal {
   $lxdebug->leave_sub();
 }
 
-sub save {
+sub save_and_close {
   $lxdebug->enter_sub();
 
   if ($form->{type} =~ /_order$/) {
@@ -1658,8 +1958,12 @@ sub save {
 
   }
 
-  $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
-    unless $form->{$ordnumber};
+  # get new number in sequence if no number is given or if saveasnew was requested
+  if (!$form->{$ordnumber} || $form->{saveasnew}) {
+    $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
+  }
+
+  relink_accounts();
 
   $form->redirect(
             $form->{label} . " $form->{$ordnumber} " . $locale->text('saved!'))
@@ -1669,6 +1973,82 @@ sub save {
   $lxdebug->leave_sub();
 }
 
+sub save {
+  $lxdebug->enter_sub();
+
+  if ($form->{type} =~ /_order$/) {
+    $form->isblank("transdate", $locale->text('Order Date missing!'));
+  } else {
+    $form->isblank("transdate", $locale->text('Quotation Date missing!'));
+  }
+
+  $msg = ucfirst $form->{vc};
+  $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
+
+  # $locale->text('Customer missing!');
+  # $locale->text('Vendor missing!');
+
+  $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
+    if ($form->{currency} ne $form->{defaultcurrency});
+
+  &validate_items;
+
+  # if the name changed get new values
+  if (&check_name($form->{vc})) {
+    &update;
+    exit;
+  }
+
+  $form->{id} = 0 if $form->{saveasnew};
+
+  # this is for the internal notes section for the [email] Subject
+  if ($form->{type} =~ /_order$/) {
+    if ($form->{type} eq 'sales_order') {
+      $form->{label} = $locale->text('Sales Order');
+
+      $numberfld = "sonumber";
+      $ordnumber = "ordnumber";
+    } else {
+      $form->{label} = $locale->text('Purchase Order');
+
+      $numberfld = "ponumber";
+      $ordnumber = "ordnumber";
+    }
+
+    $err = $locale->text('Cannot save order!');
+
+  } else {
+    if ($form->{type} eq 'sales_quotation') {
+      $form->{label} = $locale->text('Quotation');
+
+      $numberfld = "sqnumber";
+      $ordnumber = "quonumber";
+    } else {
+      $form->{label} = $locale->text('Request for Quotation');
+
+      $numberfld = "rfqnumber";
+      $ordnumber = "quonumber";
+    }
+
+    $err = $locale->text('Cannot save quotation!');
+
+  }
+
+  $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
+    unless $form->{$ordnumber};
+
+  relink_accounts();
+
+  OE->save(\%myconfig, \%$form);
+  $form->{simple_save} = 1;
+  if(!$form->{print_and_save}) {
+    set_headings("edit");
+    &update;
+    exit;
+  }
+  $lxdebug->leave_sub();
+}
+
 sub delete {
   $lxdebug->enter_sub();
 
@@ -1733,9 +2113,18 @@ sub invoice {
   $lxdebug->enter_sub();
 
   if ($form->{type} =~ /_order$/) {
-    $form->isblank("ordnumber", $locale->text('Order Number missing!'));
-    $form->isblank("transdate", $locale->text('Order Date missing!'));
 
+    # these checks only apply if the items don't bring their own ordnumbers/transdates.
+    # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
+    $form->isblank("ordnumber", $locale->text('Order Number missing!'))
+      if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
+          ->{''});
+    $form->isblank("transdate", $locale->text('Order Date missing!'))
+      if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }
+          ->{''});
+
+    # also copy deliverydate from the order
+    $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
   } else {
     $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
     $form->isblank("transdate", $locale->text('Quotation Date missing!'));
@@ -1748,9 +2137,15 @@ sub invoice {
     exit;
   }
 
-  ($null, $form->{cp_id}) = split /--/, $form->{contact};
   $form->{cp_id} *= 1;
 
+  for $i (1 .. $form->{rowcount}) {
+    map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
+                                                     $form->{"${_}_${i}"})
+            if ($form->{"${_}_${i}"}) }
+        qw(ship qty sellprice listprice basefactor));
+  }
+
   if (   $form->{type} =~ /_order/
       && $form->{currency} ne $form->{defaultcurrency}) {
 
@@ -1770,7 +2165,15 @@ sub invoice {
 
   # close orders/quotations
   $form->{closed} = 1;
-  OE->save(\%myconfig, \%$form);
+
+  # save order if one ordnumber has been given
+  # if not it's most likely a collective order, which can't be saved back
+  # so they just have to be closed
+  if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
+    OE->close_order(\%myconfig, \%$form);
+  } else {
+    OE->close_orders(\%myconfig, \%$form);
+  }
 
   $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
   $form->{duedate} =
@@ -1843,10 +2246,18 @@ sub invoice {
     $dec           = length $dec;
     $decimalplaces = ($dec > 2) ? $dec : 2;
 
+    # copy delivery date from reqdate for order -> invoice conversion
+    $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
+      unless $form->{"deliverydate_$i"};
+
     $form->{"sellprice_$i"} =
       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
                            $decimalplaces);
-    $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
+
+    (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
+    $dec_qty = length $dec_qty;
+    $form->{"qty_$i"} =
+      $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
     map { $form->{"${_}_$i"} =~ s/\"/&quot;/g }
       qw(partnumber description unit);
@@ -1983,6 +2394,8 @@ sub create_backorder {
     } qw(sellprice discount);
   }
 
+  relink_accounts();
+
   OE->save(\%myconfig, \%$form);
 
   # rebuild rows for invoice
@@ -2011,7 +2424,7 @@ sub save_as_new {
 
   $form->{saveasnew} = 1;
   $form->{closed}    = 0;
-  map { delete $form->{$_} } qw(printed emailed queued);
+  map { delete $form->{$_} } qw(printed emailed queued ordnumber quonumber);
 
   &save;
 
@@ -2021,6 +2434,13 @@ sub save_as_new {
 sub purchase_order {
   $lxdebug->enter_sub();
 
+  if (   $form->{type} eq 'sales_quotation'
+      || $form->{type} eq 'request_quotation') {
+    OE->close_order(\%myconfig, \%$form);
+  }
+
+  $form->{cp_id} *= 1;
+
   $form->{title} = $locale->text('Add Purchase Order');
   $form->{vc}    = "vendor";
   $form->{type}  = "purchase_order";
@@ -2035,11 +2455,9 @@ sub sales_order {
 
   if (   $form->{type} eq 'sales_quotation'
       || $form->{type} eq 'request_quotation') {
-    $form->{closed} = 1;
-    OE->save(\%myconfig, \%$form);
+    OE->close_order(\%myconfig, $form);
   }
 
-  ($null, $form->{cp_id}) = split /--/, $form->{contact};
   $form->{cp_id} *= 1;
 
   $form->{title} = $locale->text('Add Sales Order');
@@ -2063,6 +2481,13 @@ sub poso {
   map { delete $form->{$_} }
     qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
 
+  for $i (1 .. $form->{rowcount}) {
+    map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
+                                                     $form->{"${_}_${i}"})
+            if ($form->{"${_}_${i}"}) }
+        qw(ship qty sellprice listprice basefactor));
+  }
+
   &order_links;
 
   &prepare_order;
@@ -2119,7 +2544,8 @@ sub ship_receive {
 
     # undo formatting from prepare_order
     map {
-      $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
+      $form->{"${_}_$i"} =
+        $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
     } qw(qty ship);
     $n = ($form->{"qty_$i"} -= $form->{"ship_$i"});
     if (abs($n) > 0
@@ -2269,9 +2695,11 @@ sub display_ship_receive {
 <input type=hidden name=shiptophone value="$form->{shiptophone}">
 <input type=hidden name=shiptofax value="$form->{shiptofax}">
 <input type=hidden name=shiptoemail value="$form->{shiptoemail}">
+<input type=hidden name=shiptodepartment_1 value="$form->{shiptodepartment_1}">
+<input type=hidden name=shiptodepartment_2 value="$form->{shiptodepartment_2}">
 
 <!-- email variables -->
-<input type=hidden name=message value="$form->{message}"> 
+<input type=hidden name=message value="$form->{message}">
 <input type=hidden name=email value="$form->{email}">
 <input type=hidden name=subject value="$form->{subject}">
 <input type=hidden name=cc value="$form->{cc}">
@@ -2344,7 +2772,7 @@ sub display_ship_receive {
       qq|<td>$description<input type=hidden name="description_$i" value="$form->{"description_$i"}"></td>|;
     $column_data{qty} =
         qq|<td align=right>|
-      . $form->format_amount(\%myconfig, $form->{"qty_$i"})
+      . $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty)
       . qq|<input type=hidden name="qty_$i" value="$form->{"qty_$i"}"></td>|;
     $column_data{ship} =
         qq|<td align=right><input name="ship_$i" size=5 value=|
@@ -2422,7 +2850,7 @@ sub display_ship_receive {
   }
 
   print qq|
-  
+
 <input type=hidden name=rowcount value=$form->{rowcount}>
 
 <input name=callback type=hidden value="$callback">
@@ -2655,7 +3083,7 @@ sub list_transfer {
       qq|<td><input type=hidden name="warehouse_id_$i" value=$ref->{warehouse_id}>$ref->{warehouse}&nbsp;</td>|;
     $column_data{qty} =
         qq|<td><input type=hidden name="qty_$i" value=$ref->{qty}>|
-      . $form->format_amount(\%myconfig, $ref->{qty})
+      . $form->format_amount(\%myconfig, $ref->{qty}, $dec_qty)
       . qq|</td>|;
     $column_data{transfer} = qq|<td><input name="transfer_$i" size=4></td>|;
 
@@ -2676,7 +3104,7 @@ sub list_transfer {
       </table>
     </td>
   </tr>
-  
+
   <tr>
     <td><hr size=3 noshade></td>
   </tr>
@@ -2719,4 +3147,3 @@ sub transfer {
 
   $lxdebug->leave_sub();
 }
-