PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.6.7
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.6.7
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / modules / accounting / api / class-rest-api-journals.php

class-rest-api-journals.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.7, at modules/accounting/api/class-rest-api-journals.php

401 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WeDevs\ERP\Accounting\API;
4
5 use WP_Error;
6 use WP_REST_Response;
7 use WP_REST_Server;
8
9 class Journals_Controller extends \WeDevs\ERP\API\REST_Controller {
10
11 /**
12 * Endpoint namespace.
13 *
14 * @var string
15 */
16 protected $namespace = 'erp/v1';
17
18 /**
19 * Route base.
20 *
21 * @var string
22 */
23 protected $rest_base = 'accounting/v1/journals';
24
25 /**
26 * Register the routes for the objects of the controller.
27 */
28 public function register_routes() {
29 register_rest_route(
30 $this->namespace,
31 '/' . $this->rest_base,
32 [
33 [
34 'methods' => WP_REST_Server::READABLE,
35 'callback' => [ $this, 'get_journals' ],
36 'args' => [],
37 'permission_callback' => function ( $request ) {
38 return current_user_can( 'erp_ac_view_journal' );
39 },
40 ],
41 [
42 'methods' => WP_REST_Server::CREATABLE,
43 'callback' => [ $this, 'create_journal' ],
44 'args' => [],
45 'permission_callback' => function ( $request ) {
46 return current_user_can( 'erp_ac_create_journal' );
47 },
48 ],
49 'schema' => [ $this, 'get_item_schema' ],
50 ]
51 );
52
53 register_rest_route(
54 $this->namespace,
55 '/' . $this->rest_base . '/(?P<id>[\d]+)',
56 [
57 [
58 'methods' => WP_REST_Server::READABLE,
59 'callback' => [ $this, 'get_journal' ],
60 'args' => [
61 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
62 ],
63 'permission_callback' => function ( $request ) {
64 return current_user_can( 'erp_ac_view_journal' );
65 },
66 ],
67 'schema' => [ $this, 'get_item_schema' ],
68 ]
69 );
70
71 register_rest_route(
72 $this->namespace,
73 '/' . $this->rest_base . '/next',
74 [
75 [
76 'methods' => WP_REST_Server::READABLE,
77 'callback' => [ $this, 'get_next_journal_id' ],
78 'args' => [],
79 'permission_callback' => function ( $request ) {
80 return current_user_can( 'erp_ac_view_journal' );
81 },
82 ],
83 'schema' => [ $this, 'get_item_schema' ],
84 ]
85 );
86 }
87
88 /**
89 * Get a collection of journals
90 *
91 * @param WP_REST_Request $request
92 *
93 * @return WP_Error|WP_REST_Response
94 */
95 public function get_journals( $request ) {
96 $args['number'] = ! empty( $request['per_page'] ) ? $request['per_page'] : 20;
97 $args['offset'] = ( $request['per_page'] * ( $request['page'] - 1 ) );
98
99 $additional_fields = [];
100
101 $additional_fields['namespace'] = $this->namespace;
102 $additional_fields['rest_base'] = $this->rest_base;
103
104 $items = erp_acct_get_all_journals( $args );
105 $total_items = erp_acct_get_all_journals(
106 [
107 'count' => true,
108 'number' => -1,
109 ]
110 );
111
112 $formatted_items = [];
113
114 foreach ( $items as $item ) {
115 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
116 $formatted_items[] = $this->prepare_response_for_collection( $data );
117 }
118
119 $response = rest_ensure_response( $formatted_items );
120 $response = $this->format_collection_response( $response, $request, $total_items );
121
122 $response->set_status( 200 );
123
124 return $response;
125 }
126
127 /**
128 * Get a specific journal
129 *
130 * @param WP_REST_Request $request
131 *
132 * @return WP_Error|WP_REST_Response
133 */
134 public function get_journal( $request ) {
135 $id = (int) $request['id'];
136 $additional_fields = [];
137
138 if ( empty( $id ) ) {
139 return new WP_Error( 'rest_journal_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
140 }
141
142 $item = erp_acct_get_journal( $id );
143
144 $additional_fields['namespace'] = $this->namespace;
145 $additional_fields['rest_base'] = $this->rest_base;
146
147 $item = $this->prepare_item_for_response( $item, $request, $additional_fields );
148
149 $response = rest_ensure_response( $item );
150
151 return $response;
152 }
153
154 /**
155 * Get a next journal id
156 *
157 * @param WP_REST_Request $request
158 *
159 * @return WP_Error|WP_REST_Response
160 */
161 public function get_next_journal_id( $request ) {
162 global $wpdb;
163
164 $count = $wpdb->get_row( 'SELECT count(*) FROM ' . $wpdb->prefix . 'erp_acct_journals', ARRAY_N );
165 $item['id'] = $count['0'] + 1;
166
167 $response = rest_ensure_response( $item );
168
169 return $response;
170 }
171
172 /**
173 * Create a journal
174 *
175 * @param WP_REST_Request $request
176 *
177 * @return WP_Error|WP_REST_Request
178 */
179 public function create_journal( $request ) {
180 $trans_data = $this->prepare_item_for_database( $request );
181
182 $items = $trans_data['line_items'];
183 $vocher_amount_dr = [];
184 $vocher_amount_cr = [];
185
186 foreach ( $items as $key => $item ) {
187 $vocher_amount_dr[ $key ] = (float) $item['debit'];
188 $vocher_amount_cr[ $key ] = (float) $item['credit'];
189 }
190
191 $total_dr = array_sum( $vocher_amount_dr );
192 $total_cr = array_sum( $vocher_amount_cr );
193
194 $trans_data['voucher_amount'] = $total_dr;
195
196 $journal = erp_acct_insert_journal( $trans_data );
197
198 $this->add_log( $journal, 'add' );
199
200 $additional_fields['namespace'] = $this->namespace;
201 $additional_fields['rest_base'] = $this->rest_base;
202
203 $response = $this->prepare_item_for_response( $journal, $request, $additional_fields );
204 $response = rest_ensure_response( $response );
205
206 $response->set_status( 201 );
207
208 return $response;
209 }
210
211 /**
212 * Log when journal is created
213 *
214 * @param $data
215 * @param $action
216 */
217 public function add_log( $data, $action ) {
218 erp_log()->add(
219 [
220 'component' => 'Accounting',
221 'sub_component' => __( 'Journal', 'erp' ),
222 'old_value' => '',
223 'new_value' => '',
224 // translators: %s: amount
225 'message' => sprintf( __( 'An journal of %s has been created', 'erp' ), $data['voucher_amount'] ),
226 'changetype' => $action,
227 'created_by' => get_current_user_id(),
228 ]
229 );
230 }
231
232 /**
233 * Prepare a single item for create or update
234 *
235 * @param WP_REST_Request $request request object
236 *
237 * @return array $prepared_item
238 */
239 protected function prepare_item_for_database( $request ) {
240 $prepared_item = [];
241
242 if ( isset( $request['type'] ) ) {
243 $prepared_item['type'] = $request['type'];
244 }
245
246 if ( isset( $request['trn_date'] ) ) {
247 $prepared_item['date'] = $request['trn_date'];
248 }
249
250 if ( isset( $request['attachments'] ) ) {
251 $prepared_item['attachments'] = maybe_serialize( $request['attachments'] );
252 }
253
254 if ( isset( $request['particulars'] ) ) {
255 $prepared_item['particulars'] = $request['particulars'];
256 }
257
258 if ( isset( $request['line_items'] ) ) {
259 $prepared_item['line_items'] = $request['line_items'];
260 }
261
262 if ( isset( $request['ref'] ) ) {
263 $prepared_item['ref'] = $request['ref'];
264 }
265
266 return $prepared_item;
267 }
268
269 /**
270 * Prepare a single user output for response
271 *
272 * @param object $item
273 * @param WP_REST_Request $request request object
274 * @param array $additional_fields (optional)
275 *
276 * @return WP_REST_Response $response response data
277 */
278 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
279 $item = (object) $item;
280
281 $data = [
282 'id' => $item->id,
283 'voucher_no' => $item->voucher_no,
284 'particulars' => $item->particulars,
285 'ref' => $item->ref,
286 'trn_date' => $item->trn_date,
287 'line_items' => ! empty( $item->line_items ) ? $item->line_items : [],
288 'attachments' => maybe_unserialize( $item->attachments ),
289 'total' => (float) $item->voucher_amount,
290 'created_at' => $item->created_at,
291 ];
292
293 if ( isset( $request['include'] ) ) {
294 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
295
296 if ( in_array( 'created_by', $include_params, true ) ) {
297 $data['created_by'] = $this->get_user( intval( $item->created_by ) );
298 }
299 }
300
301 $data = array_merge( $data, $additional_fields );
302
303 // Wrap the data in a response object
304 $response = rest_ensure_response( $data );
305
306 $response = $this->add_links( $response, $item, $additional_fields );
307
308 return $response;
309 }
310
311 /**
312 * Get the User's schema, conforming to JSON Schema
313 *
314 * @return array
315 */
316 public function get_item_schema() {
317 $schema = [
318 '$schema' => 'http://json-schema.org/draft-04/schema#',
319 'title' => 'journal',
320 'type' => 'object',
321 'properties' => [
322 'id' => [
323 'description' => __( 'Unique identifier for the resource.' ),
324 'type' => 'integer',
325 'context' => [ 'embed', 'view', 'edit' ],
326 'readonly' => true,
327 ],
328 'trn_date' => [
329 'description' => __( 'Transaction date for the resource.' ),
330 'type' => 'string',
331 'context' => [ 'edit' ],
332 'arg_options' => [
333 'sanitize_callback' => 'sanitize_text_field',
334 ],
335 ],
336 'ref' => [
337 'description' => __( 'Reference for the resource.' ),
338 'type' => 'string',
339 'context' => [ 'edit' ],
340 'arg_options' => [
341 'sanitize_callback' => 'sanitize_text_field',
342 ],
343 ],
344 'type' => [
345 'description' => __( 'Type for the resource.' ),
346 'type' => 'string',
347 'context' => [ 'edit' ],
348 'arg_options' => [
349 'sanitize_callback' => 'sanitize_text_field',
350 ],
351 ],
352 'line_items' => [
353 'description' => __( 'List of line items data.', 'erp' ),
354 'type' => 'array',
355 'context' => [ 'view', 'edit' ],
356 'properties' => [
357 'ledger_id' => [
358 'description' => __( 'Ledger id.', 'erp' ),
359 'type' => 'integer',
360 'context' => [ 'view', 'edit' ],
361 ],
362 'particulars' => [
363 'description' => __( 'Line particulars.', 'erp' ),
364 'type' => 'string',
365 'context' => [ 'view', 'edit' ],
366 'arg_options' => [
367 'sanitize_callback' => 'sanitize_text_field',
368 ],
369 ],
370 'debit' => [
371 'description' => __( 'Debit balance.', 'erp' ),
372 'type' => 'number',
373 'context' => [ 'view', 'edit' ],
374 ],
375 'credit' => [
376 'description' => __( 'Credit balance.', 'erp' ),
377 'type' => 'number',
378 'context' => [ 'view', 'edit' ],
379 ],
380 ],
381 ],
382 'particulars' => [
383 'description' => __( 'Particulars for the resource.' ),
384 'type' => 'string',
385 'context' => [ 'edit' ],
386 'arg_options' => [
387 'sanitize_callback' => 'sanitize_text_field',
388 ],
389 ],
390 'attachments' => [
391 'description' => __( 'Attachments for the resource.' ),
392 'type' => 'object',
393 'context' => [ 'edit' ],
394 ],
395 ],
396 ];
397
398 return $schema;
399 }
400 }
401