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

(-)a/Koha/MarcOrder.pm (-65 / +349 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
                import_batch_id => $import_batch_id,
131
                import_record   => $import_record,
132
                matcher_id      => $params->{matcher_id},
133
                overlay_action  => $params->{overlay_action},
134
                agent           => $agent,
135
            });
136
            warn "Duplicates found in $result->{duplicates_in_batch}, record was skipped." if $result->{duplicates_in_batch};
137
            next if $result->{skip};
138
139
            my $order_line_details = add_items_from_import_record(
140
                {
141
                    record_result => $result->{record_result},
142
                    basket_id     => $basket_id,
143
                    vendor        => $vendor_record,
144
                    budget_id     => $budget_id,
145
                    agent         => $agent,
146
                }
147
            );
148
        }
149
        SetImportBatchStatus( $import_batch_id, 'imported' )
150
            if Koha::Import::Records->search( { import_batch_id => $import_batch_id, status => 'imported' } )->count ==
151
            Koha::Import::Records->search( { import_batch_id => $import_batch_id } )->count;
152
153
        $success = 1;
154
    } catch {
155
        $success = 0;
156
        $error   = $_;
157
    };
158
159
    return $success ? { success => 1, error => '' } : { success => 0, error => $error };
160
}
161
73
=head3 import_record_and_create_order_lines
162
=head3 import_record_and_create_order_lines
74
163
75
    my $result = Koha::MarcOrder->import_record_and_create_order_lines($args);
164
    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 ) = @_;
171
    my ( $self, $args ) = @_;
83
172
84
    my $import_batch_id           = $args->{import_batch_id};
173
    my $import_batch_id           = $args->{import_batch_id};
85
    my $import_record_id_selected = $args->{import_record_id_selected} || ();
174
    my @import_record_id_selected = $args->{import_record_id_selected} || ();
86
    my $matcher_id                = $args->{matcher_id};
175
    my $matcher_id                = $args->{matcher_id};
87
    my $overlay_action            = $args->{overlay_action};
176
    my $overlay_action            = $args->{overlay_action};
88
    my $import_record             = $args->{import_record};
177
    my $import_record             = $args->{import_record};
Lines 125-130 sub import_record_and_create_order_lines { Link Here
125
    };
214
    };
126
}
215
}
127
216
217
=head3 _create_basket_for_file
218
219
    my $basket_id = _create_basket_for_file({
220
        filename  => $filename,
221
        vendor_id => $vendor_id
222
    });
223
224
    Creates a basket ready to receive order lines based on the imported file
225
226
=cut
227
228
sub _create_basket_for_file {
229
    my ($args) = @_;
230
231
    my $filename  = $args->{filename};
232
    my $vendor_id = $args->{vendor_id};
233
234
    # aqbasketname.basketname has a max length of 50 characters so long file names will need to be truncated
235
    my $basketname = length($filename) > 50 ? substr( $filename, 0, 50 ) : $filename;
236
237
    my $basketno = NewBasket(
238
        $vendor_id, 0, $basketname, q{},
239
        q{} . q{}
240
    );
241
242
    return $basketno;
243
}
244
245
=head3 _stage_file
246
247
    $file->_stage_file($params)
248
249
    Stages a file directly using parameters from a marc ordering account and without using the background job
250
    This function is a mirror of Koha::BackgroundJob::StageMARCForImport->process but with the background job functionality removed
251
252
=cut
253
254
sub _stage_file {
255
    my ($args) = @_;
256
257
    my $record_type                = $args->{record_type};
258
    my $encoding                   = $args->{encoding};
259
    my $format                     = $args->{format};
260
    my $filepath                   = $args->{filepath};
261
    my $filename                   = $args->{filename};
262
    my $marc_modification_template = $args->{marc_modification_template};
263
    my $comments                   = $args->{comments};
264
    my $parse_items                = $args->{parse_items};
265
    my $matcher_id                 = $args->{matcher_id};
266
    my $overlay_action             = $args->{overlay_action};
267
    my $nomatch_action             = $args->{nomatch_action};
268
    my $item_action                = $args->{item_action};
269
270
    my ( $batch_id, $num_valid, $num_items, @import_errors );
271
    my $num_with_matches = 0;
272
    my $checked_matches  = 0;
273
    my $matcher_failed   = 0;
274
    my $matcher_code     = "";
275
276
    my $schema = Koha::Database->new->schema;
277
    try {
278
        $schema->storage->txn_begin;
279
280
        my ( $errors, $marcrecords );
281
        if ( $format eq 'MARCXML' ) {
282
            ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding );
283
        } elsif ( $format eq 'ISO2709' ) {
284
            ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File(
285
                $filepath, $record_type,
286
                $encoding
287
            );
288
        } else {    # plugin based
289
            $errors      = [];
290
            $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin(
291
                $filepath, $format,
292
                $encoding
293
            );
294
        }
295
296
        ( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords(
297
            $record_type,                $encoding,
298
            $marcrecords,                $filename,
299
            $marc_modification_template, $comments,
300
            '',                          $parse_items,
301
            0
302
        );
303
304
        if ($matcher_id) {
305
            my $matcher = C4::Matcher->fetch($matcher_id);
306
            if ( defined $matcher ) {
307
                $checked_matches  = 1;
308
                $matcher_code     = $matcher->code();
309
                $num_with_matches = BatchFindDuplicates( $batch_id, $matcher, 10 );
310
                SetImportBatchMatcher( $batch_id, $matcher_id );
311
                SetImportBatchOverlayAction( $batch_id, $overlay_action );
312
                SetImportBatchNoMatchAction( $batch_id, $nomatch_action );
313
                SetImportBatchItemAction( $batch_id, $item_action );
314
                $schema->storage->txn_commit;
315
            } else {
316
                $matcher_failed = 1;
317
                $schema->storage->txn_rollback;
318
            }
319
        } else {
320
            $schema->storage->txn_commit;
321
        }
322
323
        return $batch_id;
324
    } catch {
325
        warn $_;
326
        $schema->storage->txn_rollback;
327
        die "Something terrible has happened!"
328
            if ( $_ =~ /Rollback failed/ );    # TODO Check test: Rollback failed
329
    };
330
}
331
128
=head3 _get_syspref_mappings
332
=head3 _get_syspref_mappings
129
333
130
    my $syspref_info = _get_syspref_mappings( $marcrecord, $syspref_name );
334
    my $syspref_info = _get_syspref_mappings( $marcrecord, $syspref_name );
Lines 234-240 sub add_biblio_from_import_record { Link Here
234
    my ($args) = @_;
438
    my ($args) = @_;
235
439
236
    my $import_batch_id           = $args->{import_batch_id};
440
    my $import_batch_id           = $args->{import_batch_id};
237
    my $import_record_id_selected = $args->{import_record_id_selected} || ();
441
    my @import_record_id_selected = $args->{import_record_id_selected} || ();
238
    my $matcher_id                = $args->{matcher_id};
442
    my $matcher_id                = $args->{matcher_id};
239
    my $overlay_action            = $args->{overlay_action};
443
    my $overlay_action            = $args->{overlay_action};
240
    my $import_record             = $args->{import_record};
444
    my $import_record             = $args->{import_record};
Lines 247-253 sub add_biblio_from_import_record { Link Here
247
            record_result       => 0,
451
            record_result       => 0,
248
            duplicates_in_batch => 0,
452
            duplicates_in_batch => 0,
249
            skip                => 1
453
            skip                => 1
250
        } if not grep { $_ eq $import_record->import_record_id } @{$import_record_id_selected};
454
        } if not grep { $_ eq $import_record->import_record_id } @import_record_id_selected;
251
    }
455
    }
252
456
253
    my $marcrecord   = $import_record->get_marc_record || die "Couldn't translate marc information";
457
    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' );
537
        my $marc_fields_to_order      = _get_syspref_mappings( $marcrecord, 'MarcFieldsToOrder' );
334
        my $marc_item_fields_to_order = _get_syspref_mappings( $marcrecord, 'MarcItemFieldsToOrder' );
538
        my $marc_item_fields_to_order = _get_syspref_mappings( $marcrecord, 'MarcItemFieldsToOrder' );
335
539
336
        my $item_fields = {
540
        my $item_fields = _create_item_fields_from_syspref(
337
            homebranch       => $marc_item_fields_to_order->{homebranch},
541
            {
338
            holdingbranch    => $marc_item_fields_to_order->{holdingbranch},
542
                marc_fields_to_order      => $marc_fields_to_order,
339
            itype            => $marc_item_fields_to_order->{itype},
543
                marc_item_fields_to_order => $marc_item_fields_to_order,
340
            nonpublic_note   => $marc_item_fields_to_order->{nonpublic_note},
544
                budget_id                 => $budget_id
341
            public_note      => $marc_item_fields_to_order->{public_note},
545
            }
342
            loc              => $marc_item_fields_to_order->{loc},
546
        );
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
547
360
        my $order_line_fields = parse_input_into_order_line_fields(
548
        my $order_line_fields = parse_input_into_order_line_fields(
361
            {
549
            {
Lines 405-410 sub add_items_from_import_record { Link Here
405
    }
593
    }
406
}
594
}
407
595
596
408
=head3 import_batches_list
597
=head3 import_batches_list
409
598
410
Fetches import batches matching the batch to be added to the basket and returns these to the template
599
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
    }
857
    }
669
);
858
);
670
859
671
672
=cut
860
=cut
673
861
674
sub parse_input_into_order_line_fields {
862
sub parse_input_into_order_line_fields {
Lines 682-729 sub parse_input_into_order_line_fields { Link Here
682
    my $fields       = $args->{fields};
870
    my $fields       = $args->{fields};
683
    my $marcrecord   = $args->{marcrecord};
871
    my $marcrecord   = $args->{marcrecord};
684
872
685
    my $quantity        = $fields->{quantity} || 1;
873
    my $quantity          = $fields->{quantity} || 1;
686
    my @homebranches    = $client ? @{ $fields->{homebranches} }    : ( ( $fields->{homebranch} ) x $quantity );
874
    my @homebranches      = $fields->{homebranches}      ? @{ $fields->{homebranches} }      : ();
687
    my @holdingbranches = $client ? @{ $fields->{holdingbranches} } : ( ( $fields->{holdingbranch} ) x $quantity );
875
    my @holdingbranches   = $fields->{holdingbranches}   ? @{ $fields->{holdingbranches} }   : ();
688
    my @itypes          = $client ? @{ $fields->{itypes} }          : ( ( $fields->{itype} ) x $quantity );
876
    my @itypes            = $fields->{itypes}            ? @{ $fields->{itypes} }            : ();
689
    my @nonpublic_notes = $client ? @{ $fields->{nonpublic_notes} } : ( ( $fields->{nonpublic_note} ) x $quantity );
877
    my @nonpublic_notes   = $fields->{nonpublic_notes}   ? @{ $fields->{nonpublic_notes} }   : ();
690
    my @public_notes    = $client ? @{ $fields->{public_notes} }    : ( ( $fields->{public_note} ) x $quantity );
878
    my @public_notes      = $fields->{public_notes}      ? @{ $fields->{public_notes} }      : ();
691
    my @locs            = $client ? @{ $fields->{locs} }            : ( ( $fields->{loc} ) x $quantity );
879
    my @locs              = $fields->{locs}              ? @{ $fields->{locs} }              : ();
692
    my @ccodes          = $client ? @{ $fields->{ccodes} }          : ( ( $fields->{ccode} ) x $quantity );
880
    my @ccodes            = $fields->{ccodes}            ? @{ $fields->{ccodes} }            : ();
693
    my @notforloans     = $client ? @{ $fields->{notforloans} }     : ( ( $fields->{notforloan} ) x $quantity );
881
    my @notforloans       = $fields->{notforloans}       ? @{ $fields->{notforloans} }       : ();
694
    my @uris            = $client ? @{ $fields->{uris} }            : ( ( $fields->{uri} ) x $quantity );
882
    my @uris              = $fields->{uris}              ? @{ $fields->{uris} }              : ();
695
    my @copynos         = $client ? @{ $fields->{copynos} }         : ( ( $fields->{copyno} ) x $quantity );
883
    my @copynos           = $fields->{copynos}           ? @{ $fields->{copynos} }           : ();
696
    my @itemprices      = $client ? @{ $fields->{itemprices} }      : ( ( $fields->{price} ) x $quantity );
884
    my @itemprices        = $fields->{itemprices}        ? @{ $fields->{itemprices} }        : ();
697
    my @replacementprices =
885
    my @replacementprices = $fields->{replacementprices} ? @{ $fields->{replacementprices} } : ();
698
        $client ? @{ $fields->{replacementprices} } : ( ( $fields->{replacementprice} ) x $quantity );
886
    my @itemcallnumbers   = $fields->{itemcallnumbers}   ? @{ $fields->{itemcallnumbers} }   : ();
699
    my @itemcallnumbers = $client ? @{ $fields->{itemcallnumbers} } : ( ( $fields->{itemcallnumber} ) x $quantity );
700
    my @coded_location_qualifiers =
887
    my @coded_location_qualifiers =
701
        $client ? @{ $fields->{coded_location_qualifiers} } : ( ( $fields->{coded_location_qualifier} ) x $quantity );
888
        $fields->{coded_location_qualifiers} ? @{ $fields->{coded_location_qualifiers} } : ();
702
    my @barcodes            = $client ? @{ $fields->{barcodes} }       : ( ( $fields->{barcode} ) x $quantity );
889
    my @barcodes            = $fields->{barcodes}     ? @{ $fields->{barcodes} }     : ();
703
    my @enumchrons          = $client ? @{ $fields->{enumchrons} }     : ( ( $fields->{enumchron} ) x $quantity );
890
    my @enumchrons          = $fields->{enumchrons}   ? @{ $fields->{enumchrons} }   : ();
704
    my $c_quantity          = $client ? $fields->{c_quantity}          : $fields->{c_quantity};
891
    my @budget_codes        = $fields->{budget_codes} ? @{ $fields->{budget_codes} } : ();
705
    my $c_budget_id         = $client ? $fields->{c_budget_id}         : $fields->{c_budget_id};
892
    my $c_quantity          = $fields->{c_quantity};
706
    my $c_discount          = $client ? $fields->{c_discount}          : $fields->{c_discount};
893
    my $c_budget_id         = $fields->{c_budget_id};
707
    my $c_sort1             = $client ? $fields->{c_sort1}             : $fields->{c_sort1};
894
    my $c_discount          = $fields->{c_discount};
708
    my $c_sort2             = $client ? $fields->{c_sort2}             : $fields->{c_sort2};
895
    my $c_sort1             = $fields->{c_sort1};
709
    my $c_replacement_price = $client ? $fields->{c_replacement_price} : $fields->{c_replacement_price};
896
    my $c_sort2             = $fields->{c_sort2};
710
    my $c_price             = $client ? $fields->{c_price}             : $fields->{c_price};
897
    my $c_replacement_price = $fields->{c_replacement_price};
898
    my $c_price             = $fields->{c_price};
711
899
712
    # If using the cronjob, we want to default to the account budget if not mapped on the record
900
    # If using the cronjob, we want to default to the account budget if not mapped on the record
713
    my $item_budget_id;
901
    if ( !$client && ( !@budget_codes || scalar(@budget_codes) == 0 ) ) {
714
    if ( !$client && ( $fields->{budget_code} || $fields->{c_budget_code} ) ) {
902
        for ( 1 .. $quantity ) {
715
        my $budget_code = $fields->{budget_code} || $fields->{c_budget_code};
903
            my $item_budget = GetBudgetByCode($c_budget_id);
716
        my $item_budget = GetBudgetByCode($budget_code);
904
            if ($item_budget) {
717
        if ($item_budget) {
905
                push( @budget_codes, $item_budget );
718
            $item_budget_id = $item_budget->{budget_id};
906
            } else {
719
        } else {
907
                push( @budget_codes, $budget_id );
720
            $item_budget_id = $budget_id;
908
            }
721
        }
909
        }
722
    } else {
723
        $item_budget_id = $budget_id;
724
    }
910
    }
725
    my @budget_codes = $client ? @{ $fields->{budget_codes} } : ($item_budget_id);
911
    my $loop_limit = $quantity;
726
    my $loop_limit   = $client ? scalar(@homebranches)        : $quantity;
727
912
728
    my $order_line_fields = {
913
    my $order_line_fields = {
729
        biblionumber             => $biblionumber,
914
        biblionumber             => $biblionumber,
Lines 959-962 sub _format_price_to_CurrencyFormat_syspref { Link Here
959
    return $price;
1144
    return $price;
960
}
1145
}
961
1146
1147
=head3 _create_item_fields_from_syspref
1148
1149
Takes the two sysprefs and returns the item fields required to process and create orderlines
1150
1151
my $item_fields = _create_item_fields_from_syspref(
1152
    {
1153
        marc_fields_to_order      => $marc_fields_to_order,
1154
        marc_item_fields_to_order => $marc_item_fields_to_order
1155
    }
1156
);
1157
1158
=cut
1159
1160
sub _create_item_fields_from_syspref {
1161
    my ($args) = @_;
1162
1163
    my $marc_item_fields_to_order = $args->{marc_item_fields_to_order};
1164
    my $marc_fields_to_order      = $args->{marc_fields_to_order};
1165
    my $budget_id                 = $args->{budget_id};
1166
1167
    my @homebranches      = ();
1168
    my @holdingbranches   = ();
1169
    my @itypes            = ();
1170
    my @nonpublic_notes   = ();
1171
    my @public_notes      = ();
1172
    my @locs              = ();
1173
    my @ccodes            = ();
1174
    my @notforloans       = ();
1175
    my @uris              = ();
1176
    my @copynos           = ();
1177
    my @budget_codes      = ();
1178
    my @itemprices        = ();
1179
    my @replacementprices = ();
1180
    my @itemcallnumbers   = ();
1181
1182
    foreach my $infoset (@$marc_item_fields_to_order) {
1183
        my $quantity = $infoset->{quantity} || 1;
1184
        for ( my $i = 0 ; $i < $quantity ; $i++ ) {
1185
            my $budget_code;
1186
            if ( !$infoset->{budget_code} ) {
1187
                if ( $marc_fields_to_order->{budget_code} ) {
1188
                    $budget_code = $marc_fields_to_order->{budget_code};
1189
                }
1190
            } else {
1191
                $budget_code = $infoset->{budget_code};
1192
            }
1193
1194
            my $item_budget_id;
1195
            my $item_budget = GetBudgetByCode($budget_code);
1196
            if ($item_budget) {
1197
                $item_budget_id = $item_budget->{budget_id};
1198
            } else {
1199
                $item_budget_id = $budget_id;
1200
            }
1201
1202
            push @homebranches,      $infoset->{homebranch};
1203
            push @holdingbranches,   $infoset->{holdingbranch};
1204
            push @itypes,            $infoset->{itype};
1205
            push @nonpublic_notes,   $infoset->{nonpublic_note};
1206
            push @public_notes,      $infoset->{public_note};
1207
            push @locs,              $infoset->{loc};
1208
            push @ccodes,            $infoset->{ccode};
1209
            push @notforloans,       $infoset->{notforloan};
1210
            push @uris,              $infoset->{uri};
1211
            push @copynos,           $infoset->{copyno};
1212
            push @budget_codes,      $item_budget_id;
1213
            push @itemprices,        $infoset->{price};
1214
            push @replacementprices, $infoset->{replacementprice};
1215
            push @itemcallnumbers,   $infoset->{itemcallnumber};
1216
        }
1217
    }
1218
1219
    my $item_fields = {
1220
        quantity          => scalar(@homebranches),
1221
        homebranches      => \@homebranches,
1222
        holdingbranches   => \@holdingbranches,
1223
        itypes            => \@itypes,
1224
        nonpublic_notes   => \@nonpublic_notes,
1225
        public_notes      => \@public_notes,
1226
        locs              => \@locs,
1227
        ccodes            => \@ccodes,
1228
        notforloans       => \@notforloans,
1229
        uris              => \@uris,
1230
        copynos           => \@copynos,
1231
        budget_codes      => \@budget_codes,
1232
        itemprices        => \@itemprices,
1233
        replacementprices => \@replacementprices,
1234
        itemcallnumbers   => \@itemcallnumbers,
1235
        c_quantity        => $marc_fields_to_order->{quantity},
1236
        c_budget_code     => $marc_fields_to_order->{budget_code},
1237
        c_price           => $marc_fields_to_order->{price},
1238
        c_discount        => $marc_fields_to_order->{discount},
1239
        c_sort1           => $marc_fields_to_order->{sort1},
1240
        c_sort2           => $marc_fields_to_order->{sort2},
1241
    };
1242
1243
    return $item_fields;
1244
}
1245
962
1;
1246
1;
(-)a/misc/cronjobs/marc_ordering_process.pl (-1 / +134 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
    print "No new files found\n" if scalar(@files) == 0;
102
103
    my $files_processed = 0;
104
105
    foreach my $filename (@files) {
106
        my $full_path = "$working_dir/$filename";
107
        my $args      = {
108
            filename => $filename,
109
            filepath => $full_path,
110
            profile  => $acct,
111
            agent    => 'cron'
112
        };
113
        if ( $acct->match_field && $acct->match_value ) {
114
            my $file_match = Koha::MarcOrder->match_file_to_account($args);
115
            next if !$file_match;
116
        }
117
        if ($confirm) {
118
            say sprintf "Creating order lines from file %s", $filename if $verbose;
119
            my $result = Koha::MarcOrder->create_order_lines_from_file($args);
120
            if ( $result->{success} ) {
121
                $files_processed++;
122
                say sprintf "Successfully processed file: %s", $filename if $verbose;
123
                unlink $full_path;
124
            } else {
125
                say sprintf "Error processing file: %s", $filename        if $verbose;
126
                say sprintf "Error message: %s",         $result->{error} if $verbose;
127
            }
128
        }
129
    }
130
    say sprintf "%s files processed", $files_processed unless $files_processed == 0;
131
    print "Moving to next account\n\n";
132
}
133
print "Process complete\n";
134
cronlogaction( { action => 'End', info => "COMPLETED" } );

Return to bug 34355