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

(-)a/Koha/REST/V1/Acquisitions/Vendors.pm (+173 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::Bookseller;
23
use Koha::Acquisition::Booksellers;
24
25
use Try::Tiny;
26
27
sub list {
28
    my ( $c, $args, $cb ) = @_;
29
30
    my @vendors;
31
32
    return try {
33
        @vendors = map { _to_api($_) } Koha::Acquisition::Booksellers->search($args);
34
        return $c->$cb( \@vendors, 200 );
35
    }
36
    catch {
37
        if ( $_->isa('DBIx::Class::Exception') ) {
38
            return $c->$cb( { error => $_->{msg} }, 500 );
39
        }
40
        else {
41
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
42
        }
43
    };
44
}
45
46
sub get {
47
    my ( $c, $args, $cb ) = @_;
48
49
    my $vendor = Koha::Acquisition::Booksellers->find( $args->{vendor_id} );
50
    unless ($vendor) {
51
        return $c->$cb( { error => "Vendor not found" }, 404 );
52
    }
53
54
    return $c->$cb( _to_api($vendor), 200 );
55
}
56
57
sub add {
58
    my ( $c, $args, $cb ) = @_;
59
60
    my $vendor = Koha::Acquisition::Bookseller->new( _to_model( $args->{body} ) );
61
62
    return try {
63
        $vendor->store;
64
        return $c->$cb( _to_api($vendor), 200 );
65
    }
66
    catch {
67
        if ( $_->isa('DBIx::Class::Exception') ) {
68
            return $c->$cb( { error => $_->msg }, 500 );
69
        }
70
        else {
71
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
72
        }
73
    };
74
}
75
76
sub update {
77
    my ( $c, $args, $cb ) = @_;
78
79
    my $vendor;
80
81
    return try {
82
        $vendor = Koha::Acquisition::Booksellers->find( $args->{vendor_id} );
83
        $vendor->set( _to_model( $args->{body} ) );
84
        $vendor->store();
85
        return $c->$cb( _to_api($vendor), 200 );
86
    }
87
    catch {
88
        if ( not defined $vendor ) {
89
            return $c->$cb( { error => "Object not found" }, 404 );
90
        }
91
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
92
            return $c->$cb( { error => $_->message }, 500 );
93
        }
94
        else {
95
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
96
        }
97
    };
98
99
}
100
101
sub delete {
102
    my ( $c, $args, $cb ) = @_;
103
104
    my $vendor;
105
106
    return try {
107
        $vendor = Koha::Acquisition::Booksellers->find( $args->{vendor_id} );
108
        $vendor->delete;
109
        return $c->$cb( q{}, 200 );
110
    }
111
    catch {
112
        if ( not defined $vendor ) {
113
            return $c->$cb( { error => "Object not found" }, 404 );
114
        }
115
        elsif ( $_->isa('DBIx::Class::Exception') ) {
116
            return $c->$cb( { error => $_->msg }, 500 );
117
        }
118
        else {
119
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
120
        }
121
    };
122
123
}
124
125
sub _to_api {
126
127
    my $vendor_param = shift;
128
129
    my $vendor = $vendor_param->TO_JSON;
130
131
    # Delete unused fields
132
    delete $vendor->{booksellerfax};
133
    delete $vendor->{bookselleremail};
134
    delete $vendor->{booksellerurl};
135
    delete $vendor->{currency};
136
    delete $vendor->{othersupplier};
137
138
    # Rename changed fields
139
    $vendor->{list_currency} = $vendor->{listprice};
140
    delete $vendor->{listprice};
141
    $vendor->{invoice_currency} = $vendor->{invoiceprice};
142
    delete $vendor->{invoiceprice};
143
    $vendor->{gst} = $vendor->{gstreg};
144
    delete $vendor->{gstreg};
145
    $vendor->{list_includes_gst} = $vendor->{listincgst};
146
    delete $vendor->{listincgst};
147
    $vendor->{invoice_includes_gst} = $vendor->{invoiceincgst};
148
    delete $vendor->{invoiceincgst};
149
150
    return $vendor;
151
}
152
153
sub _to_model {
154
    my $vendor_param = shift;
155
156
    my $vendor = $vendor_param;
157
158
    # Rename back
159
    $vendor->{listprice} = $vendor->{list_currency};
160
    delete $vendor->{list_currency};
161
    $vendor->{invoiceprice} = $vendor->{invoice_currency};
162
    delete $vendor->{invoice_currency};
163
    $vendor->{gstreg} = $vendor->{gst};
164
    delete $vendor->{gst};
165
    $vendor->{listincgst} = $vendor->{list_includes_gst};
166
    delete $vendor->{list_includes_gst};
167
    $vendor->{invoiceincgst} = $vendor->{invoice_includes_gst};
168
    delete $vendor->{invoice_includes_gst};
169
170
    return $vendor;
171
}
172
173
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 (+3 lines)
Lines 13-17 Link Here
13
  },
13
  },
14
  "error": {
14
  "error": {
15
    "$ref": "definitions/error.json"
15
    "$ref": "definitions/error.json"
16
  },
17
  "vendor": {
18
    "$ref": "definitions/vendor.json"
16
  }
19
  }
17
}
20
}
(-)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 (+231 lines)
Line 0 Link Here
1
{
2
  "/acquisitions/vendors": {
3
    "get": {
4
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
5
      "operationId": "list",
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
        "403": {
34
          "description": "Access forbidden",
35
          "schema": {
36
            "$ref": "../definitions.json#/error"
37
          }
38
        },
39
        "500": {
40
          "description": "Internal error",
41
          "schema": {
42
            "$ref": "../definitions.json#/error"
43
          }
44
        }
45
      },
46
      "x-koha-authorization": {
47
        "permissions": {
48
          "acquisition": "1"
49
        }
50
      }
51
    },
52
    "post": {
53
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
54
      "operationId": "add",
55
      "tags": ["acquisitions","vendors"],
56
      "parameters": [{
57
        "name": "body",
58
        "in": "body",
59
        "description": "A JSON object representing a vendor",
60
        "required": true,
61
        "schema": {
62
          "$ref": "../definitions.json#/vendor"
63
        }
64
      }],
65
      "produces": [
66
        "application/json"
67
      ],
68
      "responses": {
69
        "200": {
70
          "description": "Vendor added",
71
          "schema": {
72
            "$ref": "../definitions.json#/vendor"
73
          }
74
        },
75
        "403": {
76
          "description": "Access forbidden",
77
          "schema": {
78
            "$ref": "../definitions.json#/error"
79
          }
80
        },
81
        "500": {
82
          "description": "Internal error",
83
          "schema": {
84
            "$ref": "../definitions.json#/error"
85
          }
86
        }
87
      },
88
      "x-koha-authorization": {
89
        "permissions": {
90
          "acquisition": "vendors_manage"
91
        }
92
      }
93
    }
94
  },
95
  "/acquisitions/vendors/{vendor_id}": {
96
    "get": {
97
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
98
      "operationId": "get",
99
      "tags": ["acquisitions","vendors"],
100
      "parameters": [{
101
        "$ref": "../parameters.json#/vendoridPathParam"
102
      }],
103
      "produces": [
104
        "application/json"
105
      ],
106
      "responses": {
107
        "200": {
108
          "description": "A vendor",
109
          "schema": {
110
            "$ref": "../definitions.json#/vendor"
111
          }
112
        },
113
        "403": {
114
          "description": "Access forbidden",
115
          "schema": {
116
            "$ref": "../definitions.json#/error"
117
          }
118
        },
119
        "404": {
120
          "description": "Vendor not found",
121
          "schema": {
122
            "$ref": "../definitions.json#/error"
123
          }
124
        },
125
        "500": {
126
          "description": "Internal error",
127
          "schema": {
128
            "$ref": "../definitions.json#/error"
129
          }
130
        }
131
      },
132
      "x-koha-authorization": {
133
        "permissions": {
134
          "acquisition": "1"
135
        }
136
      }
137
    },
138
    "put": {
139
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
140
      "operationId": "update",
141
      "tags": ["acquisitions","vendors"],
142
      "parameters": [{
143
        "$ref": "../parameters.json#/vendoridPathParam"
144
      }, {
145
        "name": "body",
146
        "in": "body",
147
        "description": "A JSON object representing a vendor",
148
        "required": true,
149
        "schema": {
150
          "$ref": "../definitions.json#/vendor"
151
        }
152
      }],
153
      "produces": [
154
        "application/json"
155
      ],
156
      "responses": {
157
        "200": {
158
          "description": "A vendor",
159
          "schema": {
160
            "$ref": "../definitions.json#/vendor"
161
          }
162
        },
163
        "403": {
164
          "description": "Access forbidden",
165
          "schema": {
166
            "$ref": "../definitions.json#/error"
167
          }
168
        },
169
        "404": {
170
          "description": "Vendor not found",
171
          "schema": {
172
            "$ref": "../definitions.json#/error"
173
          }
174
        },
175
        "500": {
176
          "description": "Internal error",
177
          "schema": {
178
            "$ref": "../definitions.json#/error"
179
          }
180
        }
181
      },
182
      "x-koha-authorization": {
183
        "permissions": {
184
          "acquisition": "1"
185
        }
186
      }
187
    },
188
    "delete": {
189
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
190
      "operationId": "delete",
191
      "tags": ["acquisitions","vendors"],
192
      "parameters": [{
193
        "$ref": "../parameters.json#/vendoridPathParam"
194
      }],
195
      "produces": [
196
        "application/json"
197
      ],
198
      "responses": {
199
        "200": {
200
          "description": "Vendor deleted",
201
          "schema": {
202
            "type": "string"
203
          }
204
        },
205
        "403": {
206
          "description": "Access forbidden",
207
          "schema": {
208
            "$ref": "../definitions.json#/error"
209
          }
210
        },
211
        "404": {
212
          "description": "Vendor not found",
213
          "schema": {
214
            "$ref": "../definitions.json#/error"
215
          }
216
        },
217
        "500": {
218
          "description": "Internal error",
219
          "schema": {
220
            "$ref": "../definitions.json#/error"
221
          }
222
        }
223
      },
224
      "x-koha-authorization": {
225
        "permissions": {
226
          "acquisition": "1"
227
        }
228
      }
229
    }
230
  }
231
}
(-)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