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

(-)a/Koha/REST/V1/Acquisitions/Vendors.pm (+197 lines)
Line 0 Link Here
1
package Koha::REST::V1::Acquisitions::Vendors;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Acquisition::Booksellers;
23
24
use Try::Tiny;
25
26
sub list_vendors {
27
    my $c = shift->openapi->valid_input or return;
28
29
    my $args = _to_model($c->req->params->to_hash);
30
    my $filter;
31
32
    for my $filter_param ( keys %$args ) {
33
        $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" }
34
            if $args->{$filter_param};
35
    }
36
37
    my @vendors;
38
39
    return try {
40
        @vendors = Koha::Acquisition::Booksellers->search($filter);
41
        @vendors = map { _to_api($_->TO_JSON) } @vendors;
42
        return $c->render( status  => 200,
43
                           openapi => \@vendors );
44
    }
45
    catch {
46
        if ( $_->isa('DBIx::Class::Exception') ) {
47
            return $c->render( status  => 500,
48
                               openapi => { error => $_->{msg} } );
49
        }
50
        else {
51
            return $c->render( status  => 500,
52
                               openapi => { error => "Something went wrong, check the logs." } );
53
        }
54
    };
55
}
56
57
sub get_vendor {
58
    my $c = shift->openapi->valid_input or return;
59
60
    my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
61
    unless ($vendor) {
62
        return $c->render( status  => 404,
63
                           openapi => { error => "Vendor not found" } );
64
    }
65
66
    return $c->render( status  => 200,
67
                       openapi => _to_api($vendor->TO_JSON) );
68
}
69
70
sub add_vendor {
71
    my $c = shift->openapi->valid_input or return;
72
73
    my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $c->validation->param('body') ) );
74
75
    return try {
76
        $vendor->store;
77
        return $c->render( status  => 200,
78
                           openapi => _to_api($vendor->TO_JSON) );
79
    }
80
    catch {
81
        if ( $_->isa('DBIx::Class::Exception') ) {
82
            return $c->render( status  => 500,
83
                               openapi => { error => $_->msg } );
84
        }
85
        else {
86
            return $c->render( status  => 500,
87
                               openapi => { error => "Something went wrong, check the logs." } );
88
        }
89
    };
90
}
91
92
sub update_vendor {
93
    my $c = shift->openapi->valid_input or return;
94
95
    my $vendor;
96
97
    return try {
98
        $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
99
        $vendor->set( _to_model( $c->validation->param('body') ) );
100
        $vendor->store();
101
        return $c->render( status  => 200,
102
                           openapi => _to_api($vendor->TO_JSON) );
103
    }
104
    catch {
105
        if ( not defined $vendor ) {
106
            return $c->render( status  => 404,
107
                               openapi => { error => "Object not found" } );
108
        }
109
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
110
            return $c->render( status  => 500,
111
                               openapi => { error => $_->message } );
112
        }
113
        else {
114
            return $c->render( status  => 500,
115
                               openapi => { error => "Something went wrong, check the logs." } );
116
        }
117
    };
118
119
}
120
121
sub delete_vendor {
122
    my $c = shift->openapi->valid_input or return;
123
124
    my $vendor;
125
126
    return try {
127
        $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
128
        $vendor->delete;
129
        return $c->render( status => 200,
130
                           openapi => q{} );
131
    }
132
    catch {
133
        if ( not defined $vendor ) {
134
            return $c->render( status  => 404,
135
                               openapi => { error => "Object not found" } );
136
        }
137
        elsif ( $_->isa('DBIx::Class::Exception') ) {
138
            return $c->render( status  => 500,
139
                               openapi => { error => $_->msg } );
140
        }
141
        else {
142
            return $c->render( status  => 500,
143
                               openapi => { error => "Something went wrong, check the logs." } );
144
        }
145
    };
146
147
}
148
149
sub _to_api {
150
151
    my $vendor = shift;
152
153
    #my $vendor = $vendor_param->TO_JSON;
154
155
    # Delete unused fields
156
    delete $vendor->{booksellerfax};
157
    delete $vendor->{bookselleremail};
158
    delete $vendor->{booksellerurl};
159
    delete $vendor->{currency};
160
    delete $vendor->{othersupplier};
161
162
    # Rename changed fields
163
    $vendor->{list_currency} = $vendor->{listprice};
164
    delete $vendor->{listprice};
165
    $vendor->{invoice_currency} = $vendor->{invoiceprice};
166
    delete $vendor->{invoiceprice};
167
    $vendor->{gst} = $vendor->{gstreg};
168
    delete $vendor->{gstreg};
169
    $vendor->{list_includes_gst} = $vendor->{listincgst};
170
    delete $vendor->{listincgst};
171
    $vendor->{invoice_includes_gst} = $vendor->{invoiceincgst};
172
    delete $vendor->{invoiceincgst};
173
174
    return $vendor;
175
}
176
177
sub _to_model {
178
    my $vendor_param = shift;
179
180
    my $vendor = $vendor_param;
181
182
    # Rename back
183
    $vendor->{listprice} = $vendor->{list_currency};
184
    delete $vendor->{list_currency};
185
    $vendor->{invoiceprice} = $vendor->{invoice_currency};
186
    delete $vendor->{invoice_currency};
187
    $vendor->{gstreg} = $vendor->{gst};
188
    delete $vendor->{gst};
189
    $vendor->{listincgst} = $vendor->{list_includes_gst};
190
    delete $vendor->{list_includes_gst};
191
    $vendor->{invoiceincgst} = $vendor->{invoice_includes_gst};
192
    delete $vendor->{invoice_includes_gst};
193
194
    return $vendor;
195
}
196
197
1;
(-)a/Koha/Schema/Result/Aqbookseller.pm (+7 lines)
Lines 385-390 __PACKAGE__->has_many( Link Here
385
# Created by DBIx::Class::Schema::Loader v0.07042 @ 2016-09-09 13:43:30
385
# Created by DBIx::Class::Schema::Loader v0.07042 @ 2016-09-09 13:43:30
386
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:b3aUNZsdvNzEuKScGD7ZPQ
386
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:b3aUNZsdvNzEuKScGD7ZPQ
387
387
388
__PACKAGE__->add_columns(
389
    '+active' => { is_boolean => 1 },
390
    '+gstreg' => { is_boolean => 1 },
391
    '+listincgst' => { is_boolean => 1 },
392
    '+invoiceincgst' => { is_boolean => 1 },
393
);
394
388
395
389
# You can replace this text with custom code or comments, and it will be preserved on regeneration
396
# You can replace this text with custom code or comments, and it will be preserved on regeneration
390
1;
397
1;
(-)a/api/v1/swagger/definitions.json (+6 lines)
Lines 13-17 Link Here
13
  },
13
  },
14
  "patron": {
14
  "patron": {
15
    "$ref": "definitions/patron.json"
15
    "$ref": "definitions/patron.json"
16
  },
17
  "error": {
18
    "$ref": "definitions/error.json"
19
  },
20
  "vendor": {
21
    "$ref": "definitions/vendor.json"
16
  }
22
  }
17
}
23
}
(-)a/api/v1/swagger/definitions/vendor.json (+149 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "id": {
5
      "$ref": "../x-primitives.json#/vendor_id"
6
    },
7
    "name": {
8
      "type": [
9
        "string"
10
      ],
11
      "description": "Vendor name"
12
    },
13
    "address1": {
14
      "type": [
15
        "string",
16
        "null"
17
      ],
18
      "description": "Vendor physical address (line 1)"
19
    },
20
    "address2": {
21
      "type": [
22
        "string",
23
        "null"
24
      ],
25
      "description": "Vendor physical address (line 2)"
26
    },
27
    "address3": {
28
      "type": [
29
        "string",
30
        "null"
31
      ],
32
      "description": "Vendor physical address (line 3)"
33
    },
34
    "address4": {
35
      "type": [
36
        "string",
37
        "null"
38
      ],
39
      "description": "Vendor physical address (line 4)"
40
    },
41
    "phone": {
42
      "type": [
43
        "string",
44
        "null"
45
      ],
46
      "description": "Vendor phone number"
47
    },
48
    "fax": {
49
      "type": [
50
        "string",
51
        "null"
52
      ],
53
      "description": "Vendor fax number"
54
    },
55
    "accountnumber": {
56
      "type": [
57
        "string",
58
        "null"
59
      ],
60
      "description": "Vendor account number"
61
    },
62
    "notes": {
63
      "type": [
64
        "string",
65
        "null"
66
      ],
67
      "description": "Vendor notes"
68
    },
69
    "postal": {
70
      "type": [
71
        "string",
72
        "null"
73
      ],
74
      "description": "Vendor postal address"
75
    },
76
    "url": {
77
      "type": [
78
        "string",
79
        "null"
80
      ],
81
      "description": "Vendor web address"
82
    },
83
    "active": {
84
      "type": [
85
        "boolean",
86
        "null"
87
      ],
88
      "description": "Is this vendor active"
89
    },
90
    "list_currency": {
91
      "type": [
92
        "string",
93
        "null"
94
      ],
95
      "description": "List prices currency"
96
    },
97
    "invoice_currency": {
98
      "type": [
99
        "string",
100
        "null"
101
      ],
102
      "description": "Invoice prices currency"
103
    },
104
    "gst": {
105
      "type": [
106
        "boolean",
107
        "null"
108
      ],
109
      "description": "Is the library taxed when buying to this vendor"
110
    },
111
    "list_includes_gst": {
112
      "type": [
113
        "boolean",
114
        "null"
115
      ],
116
      "description": "List prices include taxes"
117
    },
118
    "invoice_includes_gst": {
119
      "type": [
120
        "boolean",
121
        "null"
122
      ],
123
      "description": "Invoice prices include taxes"
124
    },
125
    "tax_rate": {
126
      "type": [
127
        "number",
128
        "null"
129
      ],
130
      "description": "Tax rate"
131
    },
132
    "discount": {
133
      "type": [
134
        "number",
135
        "null"
136
      ],
137
      "description": "Discount offered on all items ordered from this vendor"
138
    },
139
    "deliverytime": {
140
      "type": [
141
        "integer",
142
        "null"
143
      ],
144
      "description": "Vendor expected delivery time (in days)"
145
    }
146
  },
147
  "additionalProperties": false,
148
  "required": ["name"]
149
}
(-)a/api/v1/swagger/parameters.json (+3 lines)
Lines 10-14 Link Here
10
  },
10
  },
11
  "holdIdPathParam": {
11
  "holdIdPathParam": {
12
    "$ref": "parameters/hold.json#/holdIdPathParam"
12
    "$ref": "parameters/hold.json#/holdIdPathParam"
13
  },
14
  "vendoridPathParam": {
15
    "$ref": "parameters/vendor.json#/vendoridPathParam"
13
  }
16
  }
14
}
17
}
(-)a/api/v1/swagger/parameters/vendor.json (+9 lines)
Line 0 Link Here
1
{
2
    "vendoridPathParam": {
3
      "name": "vendor_id",
4
      "in": "path",
5
      "description": "Vendor id",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/paths.json (+6 lines)
Lines 1-4 Link Here
1
{
1
{
2
  "/acquisitions/vendors": {
3
    "$ref": "paths/acquisitions_vendors.json#/~1acquisitions~1vendors"
4
  },
5
  "/acquisitions/vendors/{vendor_id}": {
6
    "$ref": "paths/acquisitions_vendors.json#/~1acquisitions~1vendors~1{vendor_id}"
7
  },
2
  "/cities": {
8
  "/cities": {
3
    "$ref": "paths/cities.json#/~1cities"
9
    "$ref": "paths/cities.json#/~1cities"
4
  },
10
  },
(-)a/api/v1/swagger/paths/acquisitions_vendors.json (+303 lines)
Line 0 Link Here
1
{
2
  "/acquisitions/vendors": {
3
    "get": {
4
      "x-mojo-to": "Acquisitions::Vendors#list_vendors",
5
      "operationId": "listVendors",
6
      "tags": ["acquisitions","vendors"],
7
      "produces": [
8
        "application/json"
9
      ],
10
      "parameters": [{
11
        "name": "name",
12
        "in": "query",
13
        "description": "Case insensitive search on vendor name",
14
        "required": false,
15
        "type": "string"
16
      }, {
17
        "name": "accountnumber",
18
        "in": "query",
19
        "description": "Case insensitive search on vendor's account number",
20
        "required": false,
21
        "type": "string"
22
      }],
23
      "responses": {
24
        "200": {
25
          "description": "A list of vendors",
26
          "schema": {
27
            "type": "array",
28
            "items": {
29
              "$ref": "../definitions.json#/vendor"
30
            }
31
          }
32
        },
33
        "401": {
34
          "description": "Authentication required",
35
          "schema": {
36
            "$ref": "../definitions.json#/error"
37
          }
38
        },
39
        "403": {
40
          "description": "Access forbidden",
41
          "schema": {
42
            "$ref": "../definitions.json#/error"
43
          }
44
        },
45
        "404": {
46
          "description": "Vendor not found",
47
          "schema": {
48
            "$ref": "../definitions.json#/error"
49
          }
50
        },
51
        "500": {
52
          "description": "Internal server error",
53
          "schema": {
54
            "$ref": "../definitions.json#/error"
55
          }
56
        },
57
        "503": {
58
          "description": "Under maintenance",
59
          "schema": {
60
            "$ref": "../definitions.json#/error"
61
          }
62
        }
63
      },
64
      "x-koha-authorization": {
65
        "permissions": {
66
          "acquisition": "1"
67
        }
68
      }
69
    },
70
    "post": {
71
      "x-mojo-to": "Acquisitions::Vendors#add_vendor",
72
      "operationId": "addVendor",
73
      "tags": ["acquisitions","vendors"],
74
      "parameters": [{
75
        "name": "body",
76
        "in": "body",
77
        "description": "A JSON object representing a vendor",
78
        "required": true,
79
        "schema": {
80
          "$ref": "../definitions.json#/vendor"
81
        }
82
      }],
83
      "produces": [
84
        "application/json"
85
      ],
86
      "responses": {
87
        "200": {
88
          "description": "Vendor added",
89
          "schema": {
90
            "$ref": "../definitions.json#/vendor"
91
          }
92
        },
93
        "401": {
94
          "description": "Authentication required",
95
          "schema": {
96
            "$ref": "../definitions.json#/error"
97
          }
98
        },
99
        "403": {
100
          "description": "Access forbidden",
101
          "schema": {
102
            "$ref": "../definitions.json#/error"
103
          }
104
        },
105
        "404": {
106
          "description": "Vendor not found",
107
          "schema": {
108
            "$ref": "../definitions.json#/error"
109
          }
110
        },
111
        "500": {
112
          "description": "Internal server error",
113
          "schema": {
114
            "$ref": "../definitions.json#/error"
115
          }
116
        },
117
        "503": {
118
          "description": "Under maintenance",
119
          "schema": {
120
            "$ref": "../definitions.json#/error"
121
          }
122
        }
123
      },
124
      "x-koha-authorization": {
125
        "permissions": {
126
          "acquisition": "vendors_manage"
127
        }
128
      }
129
    }
130
  },
131
  "/acquisitions/vendors/{vendor_id}": {
132
    "get": {
133
      "x-mojo-to": "Acquisitions::Vendors#get_vendor",
134
      "operationId": "getVendor",
135
      "tags": ["acquisitions","vendors"],
136
      "parameters": [{
137
        "$ref": "../parameters.json#/vendoridPathParam"
138
      }],
139
      "produces": [
140
        "application/json"
141
      ],
142
      "responses": {
143
        "200": {
144
          "description": "A vendor",
145
          "schema": {
146
            "$ref": "../definitions.json#/vendor"
147
          }
148
        },
149
        "401": {
150
          "description": "Authentication required",
151
          "schema": {
152
            "$ref": "../definitions.json#/error"
153
          }
154
        },
155
        "403": {
156
          "description": "Access forbidden",
157
          "schema": {
158
            "$ref": "../definitions.json#/error"
159
          }
160
        },
161
        "404": {
162
          "description": "Vendor not found",
163
          "schema": {
164
            "$ref": "../definitions.json#/error"
165
          }
166
        },
167
        "500": {
168
          "description": "Internal server error",
169
          "schema": {
170
            "$ref": "../definitions.json#/error"
171
          }
172
        },
173
        "503": {
174
          "description": "Under maintenance",
175
          "schema": {
176
            "$ref": "../definitions.json#/error"
177
          }
178
        }
179
      },
180
      "x-koha-authorization": {
181
        "permissions": {
182
          "acquisition": "1"
183
        }
184
      }
185
    },
186
    "put": {
187
      "x-mojo-to": "Acquisitions::Vendors#update_vendor",
188
      "operationId": "updateVendor",
189
      "tags": ["acquisitions","vendors"],
190
      "parameters": [{
191
        "$ref": "../parameters.json#/vendoridPathParam"
192
      }, {
193
        "name": "body",
194
        "in": "body",
195
        "description": "A JSON object representing a vendor",
196
        "required": true,
197
        "schema": {
198
          "$ref": "../definitions.json#/vendor"
199
        }
200
      }],
201
      "produces": [
202
        "application/json"
203
      ],
204
      "responses": {
205
        "200": {
206
          "description": "A vendor",
207
          "schema": {
208
            "$ref": "../definitions.json#/vendor"
209
          }
210
        },
211
        "401": {
212
          "description": "Authentication required",
213
          "schema": {
214
            "$ref": "../definitions.json#/error"
215
          }
216
        },
217
        "403": {
218
          "description": "Access forbidden",
219
          "schema": {
220
            "$ref": "../definitions.json#/error"
221
          }
222
        },
223
        "404": {
224
          "description": "Vendor not found",
225
          "schema": {
226
            "$ref": "../definitions.json#/error"
227
          }
228
        },
229
        "500": {
230
          "description": "Internal server error",
231
          "schema": {
232
            "$ref": "../definitions.json#/error"
233
          }
234
        },
235
        "503": {
236
          "description": "Under maintenance",
237
          "schema": {
238
            "$ref": "../definitions.json#/error"
239
          }
240
        }
241
      },
242
      "x-koha-authorization": {
243
        "permissions": {
244
          "acquisition": "1"
245
        }
246
      }
247
    },
248
    "delete": {
249
      "x-mojo-to": "Acquisitions::Vendors#delete_vendor",
250
      "operationId": "deleteVendor",
251
      "tags": ["acquisitions","vendors"],
252
      "parameters": [{
253
        "$ref": "../parameters.json#/vendoridPathParam"
254
      }],
255
      "produces": [
256
        "application/json"
257
      ],
258
      "responses": {
259
        "200": {
260
          "description": "Vendor deleted",
261
          "schema": {
262
            "type": "string"
263
          }
264
        },
265
        "401": {
266
          "description": "Authentication required",
267
          "schema": {
268
            "$ref": "../definitions.json#/error"
269
          }
270
        },
271
        "403": {
272
          "description": "Access forbidden",
273
          "schema": {
274
            "$ref": "../definitions.json#/error"
275
          }
276
        },
277
        "404": {
278
          "description": "Vendor not found",
279
          "schema": {
280
            "$ref": "../definitions.json#/error"
281
          }
282
        },
283
        "500": {
284
          "description": "Internal server error",
285
          "schema": {
286
            "$ref": "../definitions.json#/error"
287
          }
288
        },
289
        "503": {
290
          "description": "Under maintenance",
291
          "schema": {
292
            "$ref": "../definitions.json#/error"
293
          }
294
        }
295
      },
296
      "x-koha-authorization": {
297
        "permissions": {
298
          "acquisition": "1"
299
        }
300
      }
301
    }
302
  }
303
}
(-)a/api/v1/swagger/x-primitives.json (-1 / +5 lines)
Lines 39-43 Link Here
39
  "surname": {
39
  "surname": {
40
    "type": "string",
40
    "type": "string",
41
    "description": "patron's last name"
41
    "description": "patron's last name"
42
  },
43
  "vendor_id": {
44
    "type": "integer",
45
    "description": "internally assigned vendor identifier",
46
    "readOnly": true
42
  }
47
  }
43
}
48
}
44
- 

Return to bug 18120