View | Details | Raw Unified | Return to bug 34355
Collapse All | Expand All

(-)a/Koha/MarcOrder.pm (-65 / +353 lines)
Lines 19-24 package Koha::MarcOrder; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Try::Tiny qw( catch try );
21
use Try::Tiny qw( catch try );
22
use Net::FTP;
22
23
23
use base qw(Koha::Object);
24
use base qw(Koha::Object);
24
25
Lines 47-53 use C4::Biblio qw( Link Here
47
    AddBiblio
48
    AddBiblio
48
    GetMarcFromKohaField
49
    GetMarcFromKohaField
49
    TransformHtmlToXml
50
    TransformHtmlToXml
50
    GetMarcQuantity
51
);
51
);
52
use C4::Items   qw( AddItemFromMarc );
52
use C4::Items   qw( AddItemFromMarc );
53
use C4::Budgets qw( GetBudgetByCode );
53
use C4::Budgets qw( GetBudgetByCode );
Lines 70-75 Koha::MarcOrder - Koha Marc Order Object class Link Here
70
70
71
=cut
71
=cut
72
72
73
=head3 create_order_lines_from_file
74
75
    my $result = Koha::MarcOrder->create_order_lines_from_file($args);
76
77
    Controller for file staging, basket creation and order line creation when using the cronjob in marc_ordering_process.pl
78
79
=cut
80
81
sub create_order_lines_from_file {
82
    my ( $self, $args ) = @_;
83
84
    my $filename = $args->{filename};
85
    my $filepath = $args->{filepath};
86
    my $profile  = $args->{profile};
87
    my $agent    = $args->{agent};
88
89
    my $success;
90
    my $error;
91
92
    my $vendor_id = $profile->vendor_id;
93
    my $budget_id = $profile->budget_id;
94
95
    my $vendor_record = Koha::Acquisition::Booksellers->find( { id => $vendor_id } );
96
97
    my $basket_id = _create_basket_for_file(
98
        {
99
            filename  => $filename,
100
            vendor_id => $vendor_id
101
        }
102
    );
103
104
    my $format = index( $filename, '.mrc' ) != -1 ? 'ISO2709' : 'MARCXML';
105
    my $params = {
106
        record_type    => $profile->record_type,
107
        encoding       => $profile->encoding,
108
        format         => $format,
109
        filepath       => $filepath,
110
        filename       => $filename,
111
        comments       => undef,
112
        parse_items    => $profile->parse_items,
113
        matcher_id     => $profile->matcher_id,
114
        overlay_action => $profile->overlay_action,
115
        nomatch_action => $profile->nomatch_action,
116
        item_action    => $profile->item_action,
117
    };
118
119
    try {
120
        my $import_batch_id = _stage_file($params);
121
122
        my $import_records = Koha::Import::Records->search(
123
            {
124
                import_batch_id => $import_batch_id,
125
            }
126
        );
127
128
        while ( my $import_record = $import_records->next ) {
129
            my $result = add_biblio_from_import_record(
130
                {
131
                    import_batch_id => $import_batch_id,
132
                    import_record   => $import_record,
133
                    matcher_id      => $params->{matcher_id},
134
                    overlay_action  => $params->{overlay_action},
135
                    agent           => $agent,
136
                }
137
            );
138
            warn "Duplicates found in $result->{duplicates_in_batch}, record was skipped."
139
                if $result->{duplicates_in_batch};
140
            next if $result->{skip};
141
142
            my $order_line_details = add_items_from_import_record(
143
                {
144
                    record_result => $result->{record_result},
145
                    basket_id     => $basket_id,
146
                    vendor        => $vendor_record,
147
                    budget_id     => $budget_id,
148
                    agent         => $agent,
149
                }
150
            );
151
        }
152
        SetImportBatchStatus( $import_batch_id, 'imported' )
153
            if Koha::Import::Records->search( { import_batch_id => $import_batch_id, status => 'imported' } )->count ==
154
            Koha::Import::Records->search( { import_batch_id => $import_batch_id } )->count;
155
156
        $success = 1;
157
    } catch {
158
        $success = 0;
159
        $error   = $_;
160
    };
161
162
    return $success ? { success => 1, error => '' } : { success => 0, error => $error };
163
}
164
73
=head3 import_record_and_create_order_lines
165
=head3 import_record_and_create_order_lines
74
166
75
    my $result = Koha::MarcOrder->import_record_and_create_order_lines($args);
167
    my $result = Koha::MarcOrder->import_record_and_create_order_lines($args);
Lines 82-88 sub import_record_and_create_order_lines { Link Here
82
    my ( $self, $args ) = @_;
174
    my ( $self, $args ) = @_;
83
175
84
    my $import_batch_id           = $args->{import_batch_id};
176
    my $import_batch_id           = $args->{import_batch_id};
85
    my $import_record_id_selected = $args->{import_record_id_selected} || ();
177
    my @import_record_id_selected = $args->{import_record_id_selected} || ();
86
    my $matcher_id                = $args->{matcher_id};
178
    my $matcher_id                = $args->{matcher_id};
87
    my $overlay_action            = $args->{overlay_action};
179
    my $overlay_action            = $args->{overlay_action};
88
    my $import_record             = $args->{import_record};
180
    my $import_record             = $args->{import_record};
Lines 125-130 sub import_record_and_create_order_lines { Link Here
125
    };
217
    };
126
}
218
}
127
219
220
=head3 _create_basket_for_file
221
222
    my $basket_id = _create_basket_for_file({
223
        filename  => $filename,
224
        vendor_id => $vendor_id
225
    });
226
227
    Creates a basket ready to receive order lines based on the imported file
228
229
=cut
230
231
sub _create_basket_for_file {
232
    my ($args) = @_;
233
234
    my $filename  = $args->{filename};
235
    my $vendor_id = $args->{vendor_id};
236
237
    # aqbasketname.basketname has a max length of 50 characters so long file names will need to be truncated
238
    my $basketname = length($filename) > 50 ? substr( $filename, 0, 50 ) : $filename;
239
240
    my $basketno = NewBasket(
241
        $vendor_id, 0, $basketname, q{},
242
        q{} . q{}
243
    );
244
245
    return $basketno;
246
}
247
248
=head3 _stage_file
249
250
    $file->_stage_file($params)
251
252
    Stages a file directly using parameters from a marc ordering account and without using the background job
253
    This function is a mirror of Koha::BackgroundJob::StageMARCForImport->process but with the background job functionality removed
254
255
=cut
256
257
sub _stage_file {
258
    my ($args) = @_;
259
260
    my $record_type                = $args->{record_type};
261
    my $encoding                   = $args->{encoding};
262
    my $format                     = $args->{format};
263
    my $filepath                   = $args->{filepath};
264
    my $filename                   = $args->{filename};
265
    my $marc_modification_template = $args->{marc_modification_template};
266
    my $comments                   = $args->{comments};
267
    my $parse_items                = $args->{parse_items};
268
    my $matcher_id                 = $args->{matcher_id};
269
    my $overlay_action             = $args->{overlay_action};
270
    my $nomatch_action             = $args->{nomatch_action};
271
    my $item_action                = $args->{item_action};
272
273
    my ( $batch_id, $num_valid, $num_items, @import_errors );
274
    my $num_with_matches = 0;
275
    my $checked_matches  = 0;
276
    my $matcher_failed   = 0;
277
    my $matcher_code     = "";
278
279
    my $schema = Koha::Database->new->schema;
280
    try {
281
        $schema->storage->txn_begin;
282
283
        my ( $errors, $marcrecords );
284
        if ( $format eq 'MARCXML' ) {
285
            ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding );
286
        } elsif ( $format eq 'ISO2709' ) {
287
            ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File(
288
                $filepath, $record_type,
289
                $encoding
290
            );
291
        } else {    # plugin based
292
            $errors      = [];
293
            $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin(
294
                $filepath, $format,
295
                $encoding
296
            );
297
        }
298
299
        ( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords(
300
            $record_type,                $encoding,
301
            $marcrecords,                $filename,
302
            $marc_modification_template, $comments,
303
            '',                          $parse_items,
304
            0
305
        );
306
307
        if ($matcher_id) {
308
            my $matcher = C4::Matcher->fetch($matcher_id);
309
            if ( defined $matcher ) {
310
                $checked_matches  = 1;
311
                $matcher_code     = $matcher->code();
312
                $num_with_matches = BatchFindDuplicates( $batch_id, $matcher, 10 );
313
                SetImportBatchMatcher( $batch_id, $matcher_id );
314
                SetImportBatchOverlayAction( $batch_id, $overlay_action );
315
                SetImportBatchNoMatchAction( $batch_id, $nomatch_action );
316
                SetImportBatchItemAction( $batch_id, $item_action );
317
                $schema->storage->txn_commit;
318
            } else {
319
                $matcher_failed = 1;
320
                $schema->storage->txn_rollback;
321
            }
322
        } else {
323
            $schema->storage->txn_commit;
324
        }
325
326
        return $batch_id;
327
    } catch {
328
        warn $_;
329
        $schema->storage->txn_rollback;
330
        die "Something terrible has happened!"
331
            if ( $_ =~ /Rollback failed/ );    # TODO Check test: Rollback failed
332
    };
333
}
334
128
=head3 _get_syspref_mappings
335
=head3 _get_syspref_mappings
129
336
130
    my $syspref_info = _get_syspref_mappings( $marcrecord, $syspref_name );
337
    my $syspref_info = _get_syspref_mappings( $marcrecord, $syspref_name );
Lines 210-215 sub _verify_number_of_fields { Link Here
210
                $tags_count;                              # All counts of various fields should be equal if they exist
417
                $tags_count;                              # All counts of various fields should be equal if they exist
211
        }
418
        }
212
    }
419
    }
420
213
    return { error => 0, count => $tags_count };
421
    return { error => 0, count => $tags_count };
214
}
422
}
215
423
Lines 234-240 sub add_biblio_from_import_record { Link Here
234
    my ($args) = @_;
442
    my ($args) = @_;
235
443
236
    my $import_batch_id           = $args->{import_batch_id};
444
    my $import_batch_id           = $args->{import_batch_id};
237
    my $import_record_id_selected = $args->{import_record_id_selected} || ();
445
    my @import_record_id_selected = $args->{import_record_id_selected} || ();
238
    my $matcher_id                = $args->{matcher_id};
446
    my $matcher_id                = $args->{matcher_id};
239
    my $overlay_action            = $args->{overlay_action};
447
    my $overlay_action            = $args->{overlay_action};
240
    my $import_record             = $args->{import_record};
448
    my $import_record             = $args->{import_record};
Lines 247-253 sub add_biblio_from_import_record { Link Here
247
            record_result       => 0,
455
            record_result       => 0,
248
            duplicates_in_batch => 0,
456
            duplicates_in_batch => 0,
249
            skip                => 1
457
            skip                => 1
250
        } if not grep { $_ eq $import_record->import_record_id } @{$import_record_id_selected};
458
        } if not grep { $_ eq $import_record->import_record_id } @import_record_id_selected;
251
    }
459
    }
252
460
253
    my $marcrecord   = $import_record->get_marc_record || die "Couldn't translate marc information";
461
    my $marcrecord   = $import_record->get_marc_record || die "Couldn't translate marc information";
Lines 333-361 sub add_items_from_import_record { Link Here
333
        my $marc_fields_to_order      = _get_syspref_mappings( $marcrecord, 'MarcFieldsToOrder' );
541
        my $marc_fields_to_order      = _get_syspref_mappings( $marcrecord, 'MarcFieldsToOrder' );
334
        my $marc_item_fields_to_order = _get_syspref_mappings( $marcrecord, 'MarcItemFieldsToOrder' );
542
        my $marc_item_fields_to_order = _get_syspref_mappings( $marcrecord, 'MarcItemFieldsToOrder' );
335
543
336
        my $item_fields = {
544
        my $item_fields = _create_item_fields_from_syspref(
337
            homebranch       => $marc_item_fields_to_order->{homebranch},
545
            {
338
            holdingbranch    => $marc_item_fields_to_order->{holdingbranch},
546
                marc_fields_to_order      => $marc_fields_to_order,
339
            itype            => $marc_item_fields_to_order->{itype},
547
                marc_item_fields_to_order => $marc_item_fields_to_order,
340
            nonpublic_note   => $marc_item_fields_to_order->{nonpublic_note},
548
                budget_id                 => $budget_id
341
            public_note      => $marc_item_fields_to_order->{public_note},
549
            }
342
            loc              => $marc_item_fields_to_order->{loc},
550
        );
343
            ccode            => $marc_item_fields_to_order->{ccode},
344
            notforloan       => $marc_item_fields_to_order->{notforloan},
345
            uri              => $marc_item_fields_to_order->{uri},
346
            copyno           => $marc_item_fields_to_order->{copyno},
347
            quantity         => $marc_item_fields_to_order->{quantity},
348
            price            => $marc_item_fields_to_order->{price},
349
            replacementprice => $marc_item_fields_to_order->{replacementprice},
350
            itemcallnumber   => $marc_item_fields_to_order->{itemcallnumber},
351
            budget_code      => $marc_item_fields_to_order->{budget_code},
352
            c_quantity       => $marc_fields_to_order->{quantity},
353
            c_budget_code    => $marc_fields_to_order->{budget_code},
354
            c_price          => $marc_fields_to_order->{price},
355
            c_discount       => $marc_fields_to_order->{discount},
356
            c_sort1          => $marc_fields_to_order->{sort1},
357
            c_sort2          => $marc_fields_to_order->{sort2},
358
        };
359
551
360
        my $order_line_fields = parse_input_into_order_line_fields(
552
        my $order_line_fields = parse_input_into_order_line_fields(
361
            {
553
            {
Lines 405-410 sub add_items_from_import_record { Link Here
405
    }
597
    }
406
}
598
}
407
599
600
408
=head3 import_batches_list
601
=head3 import_batches_list
409
602
410
Fetches import batches matching the batch to be added to the basket and returns these to the template
603
Fetches import batches matching the batch to be added to the basket and returns these to the template
Lines 668-674 my $order_line_fields = parse_input_into_order_line_fields( Link Here
668
    }
861
    }
669
);
862
);
670
863
671
672
=cut
864
=cut
673
865
674
sub parse_input_into_order_line_fields {
866
sub parse_input_into_order_line_fields {
Lines 682-729 sub parse_input_into_order_line_fields { Link Here
682
    my $fields       = $args->{fields};
874
    my $fields       = $args->{fields};
683
    my $marcrecord   = $args->{marcrecord};
875
    my $marcrecord   = $args->{marcrecord};
684
876
685
    my $quantity        = $fields->{quantity} || 1;
877
    my $quantity          = $fields->{quantity} || 1;
686
    my @homebranches    = $client ? @{ $fields->{homebranches} }    : ( ( $fields->{homebranch} ) x $quantity );
878
    my @homebranches      = $fields->{homebranches}      ? @{ $fields->{homebranches} }      : ();
687
    my @holdingbranches = $client ? @{ $fields->{holdingbranches} } : ( ( $fields->{holdingbranch} ) x $quantity );
879
    my @holdingbranches   = $fields->{holdingbranches}   ? @{ $fields->{holdingbranches} }   : ();
688
    my @itypes          = $client ? @{ $fields->{itypes} }          : ( ( $fields->{itype} ) x $quantity );
880
    my @itypes            = $fields->{itypes}            ? @{ $fields->{itypes} }            : ();
689
    my @nonpublic_notes = $client ? @{ $fields->{nonpublic_notes} } : ( ( $fields->{nonpublic_note} ) x $quantity );
881
    my @nonpublic_notes   = $fields->{nonpublic_notes}   ? @{ $fields->{nonpublic_notes} }   : ();
690
    my @public_notes    = $client ? @{ $fields->{public_notes} }    : ( ( $fields->{public_note} ) x $quantity );
882
    my @public_notes      = $fields->{public_notes}      ? @{ $fields->{public_notes} }      : ();
691
    my @locs            = $client ? @{ $fields->{locs} }            : ( ( $fields->{loc} ) x $quantity );
883
    my @locs              = $fields->{locs}              ? @{ $fields->{locs} }              : ();
692
    my @ccodes          = $client ? @{ $fields->{ccodes} }          : ( ( $fields->{ccode} ) x $quantity );
884
    my @ccodes            = $fields->{ccodes}            ? @{ $fields->{ccodes} }            : ();
693
    my @notforloans     = $client ? @{ $fields->{notforloans} }     : ( ( $fields->{notforloan} ) x $quantity );
885
    my @notforloans       = $fields->{notforloans}       ? @{ $fields->{notforloans} }       : ();
694
    my @uris            = $client ? @{ $fields->{uris} }            : ( ( $fields->{uri} ) x $quantity );
886
    my @uris              = $fields->{uris}              ? @{ $fields->{uris} }              : ();
695
    my @copynos         = $client ? @{ $fields->{copynos} }         : ( ( $fields->{copyno} ) x $quantity );
887
    my @copynos           = $fields->{copynos}           ? @{ $fields->{copynos} }           : ();
696
    my @itemprices      = $client ? @{ $fields->{itemprices} }      : ( ( $fields->{price} ) x $quantity );
888
    my @itemprices        = $fields->{itemprices}        ? @{ $fields->{itemprices} }        : ();
697
    my @replacementprices =
889
    my @replacementprices = $fields->{replacementprices} ? @{ $fields->{replacementprices} } : ();
698
        $client ? @{ $fields->{replacementprices} } : ( ( $fields->{replacementprice} ) x $quantity );
890
    my @itemcallnumbers   = $fields->{itemcallnumbers}   ? @{ $fields->{itemcallnumbers} }   : ();
699
    my @itemcallnumbers = $client ? @{ $fields->{itemcallnumbers} } : ( ( $fields->{itemcallnumber} ) x $quantity );
700
    my @coded_location_qualifiers =
891
    my @coded_location_qualifiers =
701
        $client ? @{ $fields->{coded_location_qualifiers} } : ( ( $fields->{coded_location_qualifier} ) x $quantity );
892
        $fields->{coded_location_qualifiers} ? @{ $fields->{coded_location_qualifiers} } : ();
702
    my @barcodes            = $client ? @{ $fields->{barcodes} }       : ( ( $fields->{barcode} ) x $quantity );
893
    my @barcodes            = $fields->{barcodes}     ? @{ $fields->{barcodes} }     : ();
703
    my @enumchrons          = $client ? @{ $fields->{enumchrons} }     : ( ( $fields->{enumchron} ) x $quantity );
894
    my @enumchrons          = $fields->{enumchrons}   ? @{ $fields->{enumchrons} }   : ();
704
    my $c_quantity          = $client ? $fields->{c_quantity}          : $fields->{c_quantity};
895
    my @budget_codes        = $fields->{budget_codes} ? @{ $fields->{budget_codes} } : ();
705
    my $c_budget_id         = $client ? $fields->{c_budget_id}         : $fields->{c_budget_id};
896
    my $c_quantity          = $fields->{c_quantity};
706
    my $c_discount          = $client ? $fields->{c_discount}          : $fields->{c_discount};
897
    my $c_budget_id         = $fields->{c_budget_id};
707
    my $c_sort1             = $client ? $fields->{c_sort1}             : $fields->{c_sort1};
898
    my $c_discount          = $fields->{c_discount};
708
    my $c_sort2             = $client ? $fields->{c_sort2}             : $fields->{c_sort2};
899
    my $c_sort1             = $fields->{c_sort1};
709
    my $c_replacement_price = $client ? $fields->{c_replacement_price} : $fields->{c_replacement_price};
900
    my $c_sort2             = $fields->{c_sort2};
710
    my $c_price             = $client ? $fields->{c_price}             : $fields->{c_price};
901
    my $c_replacement_price = $fields->{c_replacement_price};
902
    my $c_price             = $fields->{c_price};
711
903
712
    # If using the cronjob, we want to default to the account budget if not mapped on the record
904
    # If using the cronjob, we want to default to the account budget if not mapped on the record
713
    my $item_budget_id;
905
    if ( !$client && ( !@budget_codes || scalar(@budget_codes) == 0 ) ) {
714
    if ( !$client && ( $fields->{budget_code} || $fields->{c_budget_code} ) ) {
906
        for ( 1 .. $quantity ) {
715
        my $budget_code = $fields->{budget_code} || $fields->{c_budget_code};
907
            my $item_budget = GetBudgetByCode($c_budget_id);
716
        my $item_budget = GetBudgetByCode($budget_code);
908
            if ($item_budget) {
717
        if ($item_budget) {
909
                push( @budget_codes, $item_budget );
718
            $item_budget_id = $item_budget->{budget_id};
910
            } else {
719
        } else {
911
                push( @budget_codes, $budget_id );
720
            $item_budget_id = $budget_id;
912
            }
721
        }
913
        }
722
    } else {
723
        $item_budget_id = $budget_id;
724
    }
914
    }
725
    my @budget_codes = $client ? @{ $fields->{budget_codes} } : ($item_budget_id);
915
    my $loop_limit = $quantity;
726
    my $loop_limit   = $client ? scalar(@homebranches)        : $quantity;
727
916
728
    my $order_line_fields = {
917
    my $order_line_fields = {
729
        biblionumber             => $biblionumber,
918
        biblionumber             => $biblionumber,
Lines 959-962 sub _format_price_to_CurrencyFormat_syspref { Link Here
959
    return $price;
1148
    return $price;
960
}
1149
}
961
1150
1151
=head3 _create_item_fields_from_syspref
1152
1153
Takes the two sysprefs and returns the item fields required to process and create orderlines
1154
1155
my $item_fields = _create_item_fields_from_syspref(
1156
    {
1157
        marc_fields_to_order      => $marc_fields_to_order,
1158
        marc_item_fields_to_order => $marc_item_fields_to_order
1159
    }
1160
);
1161
1162
=cut
1163
1164
sub _create_item_fields_from_syspref {
1165
    my ($args) = @_;
1166
1167
    my $marc_item_fields_to_order = $args->{marc_item_fields_to_order};
1168
    my $marc_fields_to_order      = $args->{marc_fields_to_order};
1169
    my $budget_id                 = $args->{budget_id};
1170
1171
    my @homebranches      = ();
1172
    my @holdingbranches   = ();
1173
    my @itypes            = ();
1174
    my @nonpublic_notes   = ();
1175
    my @public_notes      = ();
1176
    my @locs              = ();
1177
    my @ccodes            = ();
1178
    my @notforloans       = ();
1179
    my @uris              = ();
1180
    my @copynos           = ();
1181
    my @budget_codes      = ();
1182
    my @itemprices        = ();
1183
    my @replacementprices = ();
1184
    my @itemcallnumbers   = ();
1185
1186
    foreach my $infoset (@$marc_item_fields_to_order) {
1187
        my $quantity = $infoset->{quantity} || 1;
1188
        for ( my $i = 0 ; $i < $quantity ; $i++ ) {
1189
            my $budget_code;
1190
            if ( !$infoset->{budget_code} ) {
1191
                if ( $marc_fields_to_order->{budget_code} ) {
1192
                    $budget_code = $marc_fields_to_order->{budget_code};
1193
                }
1194
            } else {
1195
                $budget_code = $infoset->{budget_code};
1196
            }
1197
1198
            my $item_budget_id;
1199
            my $item_budget = GetBudgetByCode($budget_code);
1200
            if ($item_budget) {
1201
                $item_budget_id = $item_budget->{budget_id};
1202
            } else {
1203
                $item_budget_id = $budget_id;
1204
            }
1205
1206
            push @homebranches,      $infoset->{homebranch};
1207
            push @holdingbranches,   $infoset->{holdingbranch};
1208
            push @itypes,            $infoset->{itype};
1209
            push @nonpublic_notes,   $infoset->{nonpublic_note};
1210
            push @public_notes,      $infoset->{public_note};
1211
            push @locs,              $infoset->{loc};
1212
            push @ccodes,            $infoset->{ccode};
1213
            push @notforloans,       $infoset->{notforloan};
1214
            push @uris,              $infoset->{uri};
1215
            push @copynos,           $infoset->{copyno};
1216
            push @budget_codes,      $item_budget_id;
1217
            push @itemprices,        $infoset->{itemprice};
1218
            push @replacementprices, $infoset->{replacementprice};
1219
            push @itemcallnumbers,   $infoset->{itemcallnumber};
1220
        }
1221
    }
1222
1223
    my $item_fields = {
1224
        quantity          => scalar(@homebranches),
1225
        homebranches      => \@homebranches,
1226
        holdingbranches   => \@holdingbranches,
1227
        itypes            => \@itypes,
1228
        nonpublic_notes   => \@nonpublic_notes,
1229
        public_notes      => \@public_notes,
1230
        locs              => \@locs,
1231
        ccodes            => \@ccodes,
1232
        notforloans       => \@notforloans,
1233
        uris              => \@uris,
1234
        copynos           => \@copynos,
1235
        budget_codes      => \@budget_codes,
1236
        itemprices        => \@itemprices,
1237
        replacementprices => \@replacementprices,
1238
        itemcallnumbers   => \@itemcallnumbers,
1239
        c_quantity        => $marc_fields_to_order->{quantity},
1240
        c_budget_code     => $marc_fields_to_order->{budget_code},
1241
        c_price           => $marc_fields_to_order->{price},
1242
        c_discount        => $marc_fields_to_order->{discount},
1243
        c_sort1           => $marc_fields_to_order->{sort1},
1244
        c_sort2           => $marc_fields_to_order->{sort2},
1245
    };
1246
1247
    return $item_fields;
1248
}
1249
962
1;
1250
1;
(-)a/misc/cronjobs/marc_ordering_process.pl (-1 / +131 lines)
Line 0 Link Here
0
- 
1
2
#!/usr/bin/perl
3
4
# This file is part of Koha.
5
#
6
# Copyright (C) 2023 PTFS Europe Ltd
7
#
8
# Koha is free software; you can redistribute it and/or modify it
9
# under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# Koha is distributed in the hope that it will be useful, but
14
# WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
=head1 NAME
22
23
marc_ordering_process.pl - cron script to retrieve marc files and create order lines
24
25
=head1 SYNOPSIS
26
27
./marc_ordering_process.pl [-c|--confirm] [-v|--verbose]
28
29
or, in crontab:
30
# Once every day
31
0 3 * * * marc_ordering_process.pl -c
32
33
=head1 DESCRIPTION
34
35
This script searches for new marc files in an SFTP location
36
If there are new files, it stages those files, adds bilbios/items and creates order lines
37
38
=head1 OPTIONS
39
40
=over
41
42
=item B<-v|--verbose>
43
44
Print report to standard out.
45
46
=item B<-c|--confirm>
47
48
Without this parameter no changes will be made
49
50
=back
51
52
=cut
53
54
use Modern::Perl;
55
use Pod::Usage   qw( pod2usage );
56
use Getopt::Long qw( GetOptions );
57
use File::Copy   qw( copy move );
58
59
use Koha::Script -cron;
60
use Koha::MarcOrder;
61
use Koha::MarcOrderAccounts;
62
63
use C4::Log qw( cronlogaction );
64
65
my $command_line_options = join( " ", @ARGV );
66
67
my ( $help, $verbose, $confirm );
68
GetOptions(
69
    'h|help'    => \$help,
70
    'v|verbose' => \$verbose,
71
    'c|confirm' => \$confirm,
72
) || pod2usage(1);
73
74
pod2usage(0) if $help;
75
76
cronlogaction( { info => $command_line_options } );
77
78
$verbose = 1            unless $verbose or $confirm;
79
print "Test run only\n" unless $confirm;
80
81
print "Fetching marc ordering accounts\n" if $verbose;
82
my @accounts = Koha::MarcOrderAccounts->search(
83
    {},
84
    { join => [ 'vendor', 'budget' ] }
85
)->as_list;
86
87
if ( scalar(@accounts) == 0 ) {
88
    print "No accounts found - you must create a Marc order account for this cronjob to run\n" if $verbose;
89
}
90
91
foreach my $acct (@accounts) {
92
    if ($verbose) {
93
        say sprintf "Starting marc ordering process for %s", $acct->vendor->name;
94
        say sprintf "Looking for new files in %s",           $acct->download_directory;
95
    }
96
97
    my $working_dir = $acct->download_directory;
98
    opendir my $dir, $working_dir or die "Can't open filepath";
99
    my @files = grep { /\.(mrc|marcxml|mrk)/i } readdir $dir;
100
    closedir $dir;
101
102
    foreach my $filename (@files) {
103
        my $full_path = "$working_dir/$filename";
104
        my $args      = {
105
            filename => $filename,
106
            filepath => $full_path,
107
            profile  => $acct,
108
            agent    => 'cron'
109
        };
110
        if ( $acct->match_field && $acct->match_value ) {
111
            my $file_match = Koha::MarcOrder->match_file_to_account($args);
112
            next if !$file_match;
113
        }
114
        if ($confirm) {
115
            say sprintf "Creating order lines from file %s", $filename if $verbose;
116
            my $result = Koha::MarcOrder->create_order_lines_from_file($args);
117
            if ( $result->{success} ) {
118
                $files_processed++;
119
                say sprintf "Successfully processed file: %s", $filename if $verbose;
120
                unlink $full_path;
121
            } else {
122
                say sprintf "Error processing file: %s", $filename        if $verbose;
123
                say sprintf "Error message: %s",         $result->{error} if $verbose;
124
            }
125
        }
126
    }
127
    print "All files completed\n";
128
    print "Moving to next account\n\n";
129
}
130
print "Process complete\n";
131
cronlogaction( { action => 'End', info => "COMPLETED" } );

Return to bug 34355