DeliveryOrder: serialize stock to yaml
authorSven Schöling <s.schoeling@googlemail.com>
Fri, 19 Nov 2021 23:53:12 +0000 (00:53 +0100)
committerJan Büren <jan@kivitendo.de>
Mon, 14 Feb 2022 14:02:19 +0000 (15:02 +0100)
SL/Controller/DeliveryOrder.pm
js/kivi.DeliveryOrder.js
templates/webpages/delivery_order/stock_dialog.html

index 80a2155..42ee5d5 100644 (file)
@@ -53,7 +53,8 @@ use Rose::Object::MakeMethods::Generic
 
 
 # safety
-__PACKAGE__->run_before('check_auth');
+__PACKAGE__->run_before('check_auth',
+                        except => [ qw(pack_stock_information) ]);
 
 __PACKAGE__->run_before('get_unalterable_data',
                         only => [ qw(save save_as_new save_and_delivery_order save_and_invoice save_and_ap_transaction
@@ -906,8 +907,20 @@ sub action_stock_in_out_dialog {
     part  => $part,
     do_qty => $qty,
     do_unit => $unit,
+    delivered => $self->order->delivered,
   );
+}
+
+# we're using the old YAML based stock packing, but don't want to do this in
+# the frontend so we're doing a tiny roundtrip to the backend, back the info in
+# perl, serve it back to the frontend and store it in the DOM there
+sub action_pack_stock_information {
+  my ($self) = @_;
+
+  my $stock_info = $::form->{stock_info};
+  my $yaml = SL::YAML::Dump($stock_info);
 
+  $self->render(\$yaml, { layout => 0, process => 0 });
 }
 
 sub merge_stock_data {
index f708413..d937a51 100644 (file)
@@ -84,7 +84,7 @@ namespace('kivi.DeliveryOrder', function(ns) {
     $row.uniqueId();
 
     kivi.popup_dialog({
-      id: "stock_in_out",
+      id: "stock_in_out_dialog",
       url: "controller.pl?action=DeliveryOrder/stock_in_out_dialog",
       data: {
         id:            $("#id").val(),
@@ -100,6 +100,36 @@ namespace('kivi.DeliveryOrder', function(ns) {
     });
   };
 
+  ns.save_updated_stock = function() {
+    // stock information is saved in DOM as a yaml dump.
+    // we don't want to do this in javascript so we do a tiny roundtrip to the backend
+
+    let data = [];
+    $("#stock-in-out-table tr.listrow").each((i,row) => {
+      data.push({
+        qty:         kivi.parse_amount($(row).find(".data-qty").val()),
+        warehouse_id:                  $(row).find(".data-warehouse-id").val(),
+        bin_id:                        $(row).find(".data-bin-id").val(),
+        chargenumber:                  $(row).find(".data-chargenumber").val(),
+        bestbefore:                    $(row).find(".data-bestbefore").val(),
+        unit:                          $(row).find(".data-unit").val(),
+        delivery_order_items_stock_id: $(row).find(".data-stock-id").val(),
+      });
+    });
+
+    let row = $(".data-row").val();
+
+    $.post("controller.pl", kivi.serialize({
+        action: "DeliveryOrder/pack_stock_information",
+        stock_info: data
+      }),
+      (data) => {
+        $("[name=stock_info_" + row + "]").val(data);
+        $("#stock_in_out_dialog").dialog("close");
+      }
+    );
+  };
+
   ns.print = function() {
     $('#print_options').dialog('close');
 
index 9fbeb6e..40fa855 100644 (file)
@@ -1,6 +1,7 @@
 [%- USE T8 %]
 [%- USE HTML %]
 [%- USE LxERP %]
+[%- USE L %]
  [%- IF delivered %]
  [%- SET RO = ' readonly' %]
  [%- END %]
   [% L.hidden_tag("parts_id", parts_id) %]
   [% L.hidden_tag("do_qty", do_qty) %]
   [% L.hidden_tag("do_unit", do_unit) %]
-  [% L.hidden_tag("row", row) %]
+  [% L.hidden_tag("row", row, class="data-row") %]
 
   <p>
-   <table>
+   <table id="stock-in-out-table">
     <tr class="listheading">
      <th>&nbsp;</th>
      <th>[% 'Warehouse' | $T8 %]</th>
      [%- IF delivered %]
 
      <td>[% LxERP.format_amount(row.stock_qty) | html %]</td>
-     <td>[% row.stock_unit | html %]</td>
+     <td>
+      [% row.stock_unit | html %]
+      [% L.hidden_tag("unit", row.stock_unit, class="data-unit") %]
+     </td>
 
      [%- ELSE %]
 
      <td>[% row.available_qty | html %]</td>
-     <td><input name="qty_[% loop.count %]" style="text-align: right;" size="12"
-                [%- IF row.stock_qty %]
-                value="[% LxERP.format_amount(row.stock_qty) %]"
-                [%- ELSIF ((WHCONTENTS.size == 1) && (!row.stock_qty)) %]
-                value="[% do_qty | html %]"
-                [%- END %]
-                ></td>
      <td>
-       [% L.select_tag("unit_" _ loop.count, part.unit_obj.convertible_units, value_key="name", default=row.stock_unit) %]
-     </td>
+      [% L.input_tag("qty", row.stock_qty                              ? LxERP.format_amount(row.stock_qty)
+                          : (WHCONTENTS.size == 1) && (!row.stock_qty) ? LxERP.format_amount(do_qty)
+                          : "", class="numeric data-qty", size="12") %]</td>
+     <td>[% L.select_tag("unit_" _ loop.count, part.unit_obj.convertible_units, value_key="name", default=row.stock_unit, class="data-unit") %]</td>
 
      [%- END %]
+     <td style="display:none">
+      [% L.hidden_tag("warehouse_id", row.warehouse_id, class="data-warehouse-id") %]
+      [% L.hidden_tag("bin_id", row.bin_id, class="data-bin-id") %]
+      [% L.hidden_tag("chargenumber", row.chargenumber, class="data-chargenumber") %]
+      [% L.hidden_tag("delivery_order_items_stock_id", row.delivery_order_items_stock_id, class="data-stock-id") %]
+      [% L.hidden_tag("bestbefore", row.bestbefore, class="data-bestbefore") IF INSTANCE_CONF.get_show_bestbefore %]
+     </td>
     </tr>
 
-    <input type="hidden" name="warehouse_id_[% loop.count %]" value="[% HTML.escape(row.warehouse_id) %]">
-    <input type="hidden" name="bin_id_[% loop.count %]"       value="[% HTML.escape(row.bin_id) %]">
-    <input type="hidden" name="chargenumber_[% loop.count %]" value="[% HTML.escape(row.chargenumber) %]">
-    <input type="hidden" name="delivery_order_items_stock_id_[% loop.count %]" value="[% HTML.escape(row.stock_delivery_order_items_stock_id) %]">
-    [% IF INSTANCE_CONF.get_show_bestbefore %]
-    <input type="hidden" name="bestbefore_[% loop.count %]" value="[% HTML.escape(row.bestbefore) %]">
-    [% END %]
     [%- END %]
    </table>
   </p>
 
   <hr size="3" noshade>
 
-  <p>
-   [%- IF !delivered %]
-   [% L.button_tag('kivi.DeliveryOrder.save_updated_stock()', 'Save') | $T8 %]
-   [%- END %]
-  </p>
+  <p>[% L.button_tag('kivi.DeliveryOrder.save_updated_stock()', LxERP.t8('Save')) IF !delivered %]</p>
 
   [%- END %]
  </form>