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

(-)a/admin/edi_accounts.pl (-49 / +24 lines)
Lines 20-35 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI;
22
use CGI;
23
use JSON qw( decode_json );
24
23
25
use C4::Auth   qw( get_template_and_user );
24
use C4::Auth   qw( get_template_and_user );
26
use C4::Output qw( output_html_with_http_headers );
25
use C4::Output qw( output_html_with_http_headers );
27
use Koha::Database;
26
use Koha::Acquisition::Booksellers;
27
use Koha::EDI::Account;
28
use Koha::EDI::Accounts;
28
use Koha::Encryption;
29
use Koha::Encryption;
30
use Koha::File::Transports;
29
use Koha::Plugins;
31
use Koha::Plugins;
30
32
31
our $input  = CGI->new();
33
our $input = CGI->new();
32
our $schema = Koha::Database->new()->schema();
33
34
34
our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35
our ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35
    {
36
    {
Lines 48-68 $op ||= 'display'; Link Here
48
if ( $op eq 'acct_form' ) {
49
if ( $op eq 'acct_form' ) {
49
    show_account($crypt);
50
    show_account($crypt);
50
    $template->param( acct_form => 1 );
51
    $template->param( acct_form => 1 );
51
    my @vendors = $schema->resultset('Aqbookseller')->search(
52
    my $vendors = Koha::Acquisition::Booksellers->search(
52
        undef,
53
        {},
53
        {
54
        { order_by => { -asc => 'name' } }
54
            columns  => [ 'name', 'id' ],
55
            order_by => { -asc => 'name' }
56
        }
57
    );
55
    );
58
    $template->param( vendors => \@vendors );
56
    $template->param( vendors => $vendors );
59
57
60
    # Get available file transports for selection
58
    # Get available file transports for selection
61
    my @file_transports = $schema->resultset('FileTransport')->search(
59
    my $file_transports = Koha::File::Transports->search(
62
        {},
60
        {},
63
        { order_by => { -asc => 'name' } }
61
        { order_by => { -asc => 'name' } }
64
    );
62
    );
65
    $template->param( file_transports => \@file_transports );
63
    $template->param( file_transports => $file_transports );
66
64
67
    if ( C4::Context->config("enable_plugins") ) {
65
    if ( C4::Context->config("enable_plugins") ) {
68
        my @plugins = Koha::Plugins->new()->GetPlugins(
66
        my @plugins = Koha::Plugins->new()->GetPlugins(
Lines 80-86 if ( $op eq 'acct_form' ) { Link Here
80
78
81
        # validate & display
79
        # validate & display
82
        my $id     = $input->param('id');
80
        my $id     = $input->param('id');
83
        my $fields = {
81
        my $params = {
84
            description       => scalar $input->param('description'),
82
            description       => scalar $input->param('description'),
85
            vendor_id         => scalar $input->param('vendor_id'),
83
            vendor_id         => scalar $input->param('vendor_id'),
86
            file_transport_id => scalar $input->param('file_transport_id') || undef,
84
            file_transport_id => scalar $input->param('file_transport_id') || undef,
Lines 96-136 if ( $op eq 'acct_form' ) { Link Here
96
        };
94
        };
97
95
98
        if ($id) {
96
        if ($id) {
99
            $schema->resultset('VendorEdiAccount')->search(
97
            my $account = Koha::EDI::Accounts->find($id);
100
                {
98
            $account->set($params)->store if $account;
101
                    id => $id,
102
                }
103
            )->update_all($fields);
104
        } else {    # new record
99
        } else {    # new record
105
            $schema->resultset('VendorEdiAccount')->create($fields);
100
            Koha::EDI::Account->new($params)->store;
106
        }
101
        }
107
    } elsif ( $op eq 'cud-delete_confirmed' ) {
102
    } elsif ( $op eq 'cud-delete_confirmed' ) {
108
103
        my $account = Koha::EDI::Accounts->find( scalar $input->param('id') );
109
        $schema->resultset('VendorEdiAccount')->search( { id => scalar $input->param('id'), } )->delete_all;
104
        $account->delete if $account;
110
    }
105
    }
111
106
112
    # we do a default display after deletes and saves
107
    # we do a default display after deletes and saves
113
    # as well as when that's all you want
108
    # as well as when that's all you want
114
    $template->param( display => 1 );
109
    $template->param( display => 1 );
115
    my $ediaccounts = $schema->resultset('VendorEdiAccount')->search(
110
    my $ediaccounts = Koha::EDI::Accounts->search();
116
        {},
117
        { prefetch => [ 'vendor', 'file_transport' ] }
118
    );
119
111
120
    # Decode file_transport status for each account
112
    # Convert to API representation with embedded relationships
121
    my @ediaccounts;
113
    my @accounts_for_template =
122
    while ( my $ediaccount = $ediaccounts->next ) {
114
        map { $_->to_api( { embed => { vendor => {}, file_transport => {} } } ) } $ediaccounts->as_list;
123
        my $unblessed = { $ediaccount->get_inflated_columns };
124
        $unblessed->{vendor} = { $ediaccount->vendor->get_inflated_columns };
125
        if ( $ediaccount->file_transport ) {
126
            $unblessed->{file_transport} = { $ediaccount->file_transport->get_inflated_columns };
127
            $unblessed->{file_transport}->{status} =
128
                $ediaccount->file_transport->status ? decode_json( $ediaccount->file_transport->status ) : undef;
129
        }
130
        push @ediaccounts, $unblessed;
131
    }
132
115
133
    $template->param( ediaccounts => \@ediaccounts );
116
    $template->param( ediaccounts => \@accounts_for_template );
134
}
117
}
135
118
136
$template->param(
119
$template->param(
Lines 159-179 output_html_with_http_headers( $input, $cookie, $template->output ); Link Here
159
142
160
sub get_account {
143
sub get_account {
161
    my $id = shift;
144
    my $id = shift;
162
145
    return Koha::EDI::Accounts->find($id);
163
    my $account = $schema->resultset('VendorEdiAccount')->find($id);
164
    if ($account) {
165
        return $account;
166
    }
167
168
    # passing undef will default to add
169
    return;
170
}
146
}
171
147
172
sub show_account {
148
sub show_account {
173
    my $crypt   = shift;
149
    my $crypt   = shift;
174
    my $acct_id = $input->param('id');
150
    my $acct_id = $input->param('id');
175
    if ($acct_id) {
151
    if ($acct_id) {
176
        my $acct = $schema->resultset('VendorEdiAccount')->find($acct_id);
152
        my $acct = Koha::EDI::Accounts->find($acct_id);
177
        if ($acct) {
153
        if ($acct) {
178
            $template->param( account => $acct );
154
            $template->param( account => $acct );
179
        }
155
        }
180
- 

Return to bug 38195