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();
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 (+218 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
      "responses": {
11
        "200": {
12
          "description": "A list of vendors",
13
          "schema": {
14
            "type": "array",
15
            "items": {
16
              "$ref": "../definitions.json#/vendor"
17
            }
18
          }
19
        },
20
        "403": {
21
          "description": "Access forbidden",
22
          "schema": {
23
            "$ref": "../definitions.json#/error"
24
          }
25
        },
26
        "500": {
27
          "description": "Internal error",
28
          "schema": {
29
            "$ref": "../definitions.json#/error"
30
          }
31
        }
32
      },
33
      "x-koha-authorization": {
34
        "permissions": {
35
          "acquisition": "1"
36
        }
37
      }
38
    },
39
    "post": {
40
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
41
      "operationId": "add",
42
      "tags": ["acquisitions","vendors"],
43
      "parameters": [{
44
        "name": "body",
45
        "in": "body",
46
        "description": "A JSON object representing a vendor",
47
        "required": true,
48
        "schema": {
49
          "$ref": "../definitions.json#/vendor"
50
        }
51
      }],
52
      "produces": [
53
        "application/json"
54
      ],
55
      "responses": {
56
        "200": {
57
          "description": "Vendor added",
58
          "schema": {
59
            "$ref": "../definitions.json#/vendor"
60
          }
61
        },
62
        "403": {
63
          "description": "Access forbidden",
64
          "schema": {
65
            "$ref": "../definitions.json#/error"
66
          }
67
        },
68
        "500": {
69
          "description": "Internal error",
70
          "schema": {
71
            "$ref": "../definitions.json#/error"
72
          }
73
        }
74
      },
75
      "x-koha-authorization": {
76
        "permissions": {
77
          "acquisition": "vendors_manage"
78
        }
79
      }
80
    }
81
  },
82
  "/acquisitions/vendors/{vendor_id}": {
83
    "get": {
84
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
85
      "operationId": "get",
86
      "tags": ["acquisitions","vendors"],
87
      "parameters": [{
88
        "$ref": "../parameters.json#/vendoridPathParam"
89
      }],
90
      "produces": [
91
        "application/json"
92
      ],
93
      "responses": {
94
        "200": {
95
          "description": "A vendor",
96
          "schema": {
97
            "$ref": "../definitions.json#/vendor"
98
          }
99
        },
100
        "403": {
101
          "description": "Access forbidden",
102
          "schema": {
103
            "$ref": "../definitions.json#/error"
104
          }
105
        },
106
        "404": {
107
          "description": "Vendor not found",
108
          "schema": {
109
            "$ref": "../definitions.json#/error"
110
          }
111
        },
112
        "500": {
113
          "description": "Internal error",
114
          "schema": {
115
            "$ref": "../definitions.json#/error"
116
          }
117
        }
118
      },
119
      "x-koha-authorization": {
120
        "permissions": {
121
          "acquisition": "1"
122
        }
123
      }
124
    },
125
    "put": {
126
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
127
      "operationId": "update",
128
      "tags": ["acquisitions","vendors"],
129
      "parameters": [{
130
        "$ref": "../parameters.json#/vendoridPathParam"
131
      }, {
132
        "name": "body",
133
        "in": "body",
134
        "description": "A JSON object representing a vendor",
135
        "required": true,
136
        "schema": {
137
          "$ref": "../definitions.json#/vendor"
138
        }
139
      }],
140
      "produces": [
141
        "application/json"
142
      ],
143
      "responses": {
144
        "200": {
145
          "description": "A vendor",
146
          "schema": {
147
            "$ref": "../definitions.json#/vendor"
148
          }
149
        },
150
        "403": {
151
          "description": "Access forbidden",
152
          "schema": {
153
            "$ref": "../definitions.json#/error"
154
          }
155
        },
156
        "404": {
157
          "description": "Vendor not found",
158
          "schema": {
159
            "$ref": "../definitions.json#/error"
160
          }
161
        },
162
        "500": {
163
          "description": "Internal error",
164
          "schema": {
165
            "$ref": "../definitions.json#/error"
166
          }
167
        }
168
      },
169
      "x-koha-authorization": {
170
        "permissions": {
171
          "acquisition": "1"
172
        }
173
      }
174
    },
175
    "delete": {
176
      "x-mojo-controller": "Koha::REST::V1::Acquisitions::Vendors",
177
      "operationId": "delete",
178
      "tags": ["acquisitions","vendors"],
179
      "parameters": [{
180
        "$ref": "../parameters.json#/vendoridPathParam"
181
      }],
182
      "produces": [
183
        "application/json"
184
      ],
185
      "responses": {
186
        "200": {
187
          "description": "Vendor deleted",
188
          "schema": {
189
            "type": "string"
190
          }
191
        },
192
        "403": {
193
          "description": "Access forbidden",
194
          "schema": {
195
            "$ref": "../definitions.json#/error"
196
          }
197
        },
198
        "404": {
199
          "description": "Vendor not found",
200
          "schema": {
201
            "$ref": "../definitions.json#/error"
202
          }
203
        },
204
        "500": {
205
          "description": "Internal error",
206
          "schema": {
207
            "$ref": "../definitions.json#/error"
208
          }
209
        }
210
      },
211
      "x-koha-authorization": {
212
        "permissions": {
213
          "acquisition": "1"
214
        }
215
      }
216
    }
217
  }
218
}
(-)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