From d62c5ad57e957eb7315c16c05f8abbffbedd759f Mon Sep 17 00:00:00 2001
From: Petro Vashchuk <stalkernoid@gmail.com>
Date: Wed, 3 Mar 2021 14:34:52 +0200
Subject: [PATCH] Bug 27708: Unify two item object creation blocks to be stored
 as hash
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Previously for existing item data was stored as an object reference and
then treated as one, but for not yet existing item data was stored as
keys in hash reference in the same variable and later it was treated
like an object, hence why it crashed with "no method".
This patch unifies that variable in both cases filled as hash and
treated as such.

To reproduce:
    1) Go to "Administration->System preferences" and change
"AcqCreateItem" to "receiving an order."
    2) Now, go to "Acquisitions" and create a new Vendor,
or use an existing one.
    3) Next, go to "Administration->EDI Account" and add EDI account
(pick that Vendor that you created recently, or the one that you will
use for this test).
    4) Also in "Administration->Library EANs" add EAN if you didn't
have one previously.
    5) Go back to "Acquisitions" and add a new basket to your Vendor
that you will use for this test.
    6) Press "Create EDIFACT order" button. It should throw
"Can't call method "homebranch" on unblessed reference..."
software error.
    7) Apply the patch.
    8) Reload the page that threw software error previously
(or repeat steps 5, 6 if you need another basket),
it should go through now.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Joonas Kylmälä <joonas.kylmala@iki.fi>
---
 Koha/Edifact/Order.pm | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/Koha/Edifact/Order.pm b/Koha/Edifact/Order.pm
index 772eb03861..87e16fce99 100644
--- a/Koha/Edifact/Order.pm
+++ b/Koha/Edifact/Order.pm
@@ -388,18 +388,23 @@ sub order_line {
         foreach my $item (@linked_itemnumbers) {
             my $i_obj = $schema->resultset('Item')->find( $item->itemnumber );
             if ( defined $i_obj ) {
-                push @items, $i_obj;
+                push @items, {
+                    branchcode     => $item->{branchcode},
+                    itype          => $item->{itype},
+                    location       => $item->{location},
+                    itemcallnumber => $item->{itemcallnumber},
+                };
             }
         }
     }
     else {
         my $item_hash = {
-            itemtype  => $biblioitem->itemtype,
-            shelfmark => $biblioitem->cn_class,
+            itemtype       => $biblioitem->itemtype,
+            itemcallnumber => $biblioitem->cn_class,
         };
         my $branch = $orderline->basketno->deliveryplace;
         if ($branch) {
-            $item_hash->{branch} = $branch;
+            $item_hash->{branchcode} = $branch;
         }
         for ( 1 .. $orderline->quantity ) {
             push @items, $item_hash;
@@ -412,10 +417,10 @@ sub order_line {
     for my $item (@items) {
         push @{$item_fields},
           {
-            branchcode     => $item->homebranch->branchcode,
-            itype          => $item->itype,
-            location       => $item->location,
-            itemcallnumber => $item->itemcallnumber,
+            branchcode     => $item->{branchcode},
+            itype          => $item->{itype},
+            location       => $item->{location},
+            itemcallnumber => $item->{itemcallnumber},
           };
     }
     $self->add_seg(
-- 
2.20.1