| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\API; |
| 3 |
|
| 4 |
use WP_REST_Server; |
| 5 |
use WP_REST_Response; |
| 6 |
use WP_Error; |
| 7 |
|
| 8 |
class Journals_Controller extends REST_Controller { |
| 9 |
/** |
| 10 |
* Endpoint namespace. |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $namespace = 'erp/v1'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Route base. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $rest_base = 'accounting/journals'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Register the routes for the objects of the controller. |
| 25 |
*/ |
| 26 |
public function register_routes() { |
| 27 |
register_rest_route( $this->namespace, '/' . $this->rest_base, [ |
| 28 |
[ |
| 29 |
'methods' => WP_REST_Server::READABLE, |
| 30 |
'callback' => [ $this, 'get_journals' ], |
| 31 |
'args' => $this->get_collection_params(), |
| 32 |
'permission_callback' => function ( $request ) { |
| 33 |
return current_user_can( 'erp_ac_view_journal' ); |
| 34 |
}, |
| 35 |
], |
| 36 |
[ |
| 37 |
'methods' => WP_REST_Server::CREATABLE, |
| 38 |
'callback' => [ $this, 'create_journal' ], |
| 39 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 40 |
'permission_callback' => function ( $request ) { |
| 41 |
return current_user_can( 'erp_ac_create_journal' ); |
| 42 |
}, |
| 43 |
], |
| 44 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 45 |
] ); |
| 46 |
|
| 47 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [ |
| 48 |
[ |
| 49 |
'methods' => WP_REST_Server::READABLE, |
| 50 |
'callback' => [ $this, 'get_journal' ], |
| 51 |
'args' => [ |
| 52 |
'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
| 53 |
], |
| 54 |
'permission_callback' => function ( $request ) { |
| 55 |
return current_user_can( 'erp_ac_view_journal' ); |
| 56 |
}, |
| 57 |
], |
| 58 |
[ |
| 59 |
'methods' => WP_REST_Server::EDITABLE, |
| 60 |
'callback' => [ $this, 'update_journal' ], |
| 61 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 62 |
'permission_callback' => function ( $request ) { |
| 63 |
return current_user_can( 'erp_ac_create_journal' ); |
| 64 |
}, |
| 65 |
], |
| 66 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 67 |
] ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get a collection of journals |
| 72 |
* |
| 73 |
* @param WP_REST_Request $request |
| 74 |
* |
| 75 |
* @return WP_Error|WP_REST_Response |
| 76 |
*/ |
| 77 |
public function get_journals( $request ) { |
| 78 |
$args = [ |
| 79 |
'number' => $request['per_page'], |
| 80 |
'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ), |
| 81 |
'type' => 'journal', |
| 82 |
]; |
| 83 |
|
| 84 |
$items = erp_ac_get_all_transaction( ['type' => 'journal', 'join' => ['journals'], 'with_ledger' => true] ); |
| 85 |
$total_items = erp_ac_get_transaction_count( ['type' => 'journal'] ); |
| 86 |
|
| 87 |
$formated_items = []; |
| 88 |
foreach ( $items as $item ) { |
| 89 |
$additional_fields = []; |
| 90 |
|
| 91 |
$data = $this->prepare_item_for_response( $item, $request, $additional_fields ); |
| 92 |
$formated_items[] = $this->prepare_response_for_collection( $data ); |
| 93 |
} |
| 94 |
|
| 95 |
$response = rest_ensure_response( $formated_items ); |
| 96 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 97 |
|
| 98 |
return $response; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get a specific journal |
| 103 |
* |
| 104 |
* @param WP_REST_Request $request |
| 105 |
* |
| 106 |
* @return WP_Error|WP_REST_Response |
| 107 |
*/ |
| 108 |
public function get_journal( $request ) { |
| 109 |
$id = (int) $request['id']; |
| 110 |
$item = erp_ac_get_transaction( $id, ['type' => 'journal', 'join' => ['journals'], 'with_ledger' => true, 'output_by' => 'object'] ); |
| 111 |
|
| 112 |
if ( empty( $id ) || empty( $item->id ) || $item->type != 'journal' ) { |
| 113 |
return new WP_Error( 'rest_journal_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] ); |
| 114 |
} |
| 115 |
|
| 116 |
$additional_fields = []; |
| 117 |
|
| 118 |
$item = $this->prepare_item_for_response( $item, $request, $additional_fields ); |
| 119 |
$response = rest_ensure_response( $item ); |
| 120 |
|
| 121 |
return $response; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Create a journal |
| 126 |
* |
| 127 |
* @param WP_REST_Request $request |
| 128 |
* |
| 129 |
* @return WP_Error|WP_REST_Request |
| 130 |
*/ |
| 131 |
public function create_journal( $request ) { |
| 132 |
$trans_data = $this->prepare_item_for_database( $request ); |
| 133 |
$items = $request['items']; |
| 134 |
|
| 135 |
$id = erp_ac_new_journal( $trans_data, $items ); |
| 136 |
|
| 137 |
if ( is_wp_error( $id ) ) { |
| 138 |
return $id; |
| 139 |
} |
| 140 |
|
| 141 |
$transaction = erp_ac_get_transaction( $id, ['type' => 'journal', 'join' => ['journals'], 'with_ledger' => true, 'output_by' => 'object'] ); |
| 142 |
|
| 143 |
$request->set_param( 'context', 'edit' ); |
| 144 |
$response = $this->prepare_item_for_response( $transaction, $request ); |
| 145 |
$response = rest_ensure_response( $response ); |
| 146 |
$response->set_status( 201 ); |
| 147 |
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) ); |
| 148 |
|
| 149 |
return $response; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Prepare a single item for create or update |
| 154 |
* |
| 155 |
* @param WP_REST_Request $request Request object. |
| 156 |
* |
| 157 |
* @return array $prepared_item |
| 158 |
*/ |
| 159 |
protected function prepare_item_for_database( $request ) { |
| 160 |
$prepared_item = []; |
| 161 |
|
| 162 |
$prepared_item['type'] = 'journal'; |
| 163 |
$prepared_item['ref'] = isset( $request['reference'] ) ? sanitize_text_field( $request['reference'] ) : ''; |
| 164 |
$prepared_item['issue_date'] = isset( $request['issue_date'] ) ? sanitize_text_field( $request['issue_date'] ) : ''; |
| 165 |
$prepared_item['summary'] = isset( $request['summary'] ) ? wp_kses_post( $request['summary'] ) : ''; |
| 166 |
|
| 167 |
return $prepared_item; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Prepare a single user output for response |
| 172 |
* |
| 173 |
* @param object $item |
| 174 |
* @param WP_REST_Request $request Request object. |
| 175 |
* @param array $additional_fields (optional) |
| 176 |
* |
| 177 |
* @return WP_REST_Response $response Response data. |
| 178 |
*/ |
| 179 |
public function prepare_item_for_response( $item, $request, $additional_fields = [] ) { |
| 180 |
$data = [ |
| 181 |
'id' => (int) $item->id, |
| 182 |
'reference' => $item->ref, |
| 183 |
'summary' => $item->summary, |
| 184 |
'issue_date' => $item->issue_date, |
| 185 |
'items' => $this->format_transaction_items( $item->journals ), |
| 186 |
'total' => (float) $item->total, |
| 187 |
'trans_total' => (float) $item->trans_total, |
| 188 |
]; |
| 189 |
|
| 190 |
if ( isset( $request['include'] ) ) { |
| 191 |
$include_params = explode( ',', str_replace( ' ', '', $request['include'] ) ); |
| 192 |
|
| 193 |
if ( in_array( 'created_by', $include_params ) ) { |
| 194 |
$data['created_by'] = $this->get_user( intval( $item->created_by ) ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
$data = array_merge( $data, $additional_fields ); |
| 199 |
|
| 200 |
// Wrap the data in a response object |
| 201 |
$response = rest_ensure_response( $data ); |
| 202 |
|
| 203 |
$response = $this->add_links( $response, $item ); |
| 204 |
|
| 205 |
return $response; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Format the transaction's items |
| 210 |
* |
| 211 |
* @param array $items |
| 212 |
* |
| 213 |
* @return array |
| 214 |
*/ |
| 215 |
protected function format_transaction_items( $items ) { |
| 216 |
return array_map( function( $item ) { |
| 217 |
return [ |
| 218 |
'id' => (int) $item['id'], |
| 219 |
'debit' => (float) $item['debit'], |
| 220 |
'credit' => (float) $item['credit'], |
| 221 |
'ledger_id' => (int) $item['ledger']['id'], |
| 222 |
'ledger_name' => isset( $item['ledger']['name'] ) ? $item['ledger']['name'] : '', |
| 223 |
]; |
| 224 |
}, $items ); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Get the User's schema, conforming to JSON Schema |
| 229 |
* |
| 230 |
* @return array |
| 231 |
*/ |
| 232 |
public function get_item_schema() { |
| 233 |
$schema = [ |
| 234 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 235 |
'title' => 'journal', |
| 236 |
'type' => 'object', |
| 237 |
'properties' => [ |
| 238 |
'id' => [ |
| 239 |
'description' => __( 'Unique identifier for the resource.' ), |
| 240 |
'type' => 'integer', |
| 241 |
'context' => [ 'embed', 'view', 'edit' ], |
| 242 |
'readonly' => true, |
| 243 |
], |
| 244 |
'reference' => [ |
| 245 |
'description' => __( 'Reference for the resource.' ), |
| 246 |
'type' => 'string', |
| 247 |
'context' => [ 'edit' ], |
| 248 |
'arg_options' => [ |
| 249 |
'sanitize_callback' => 'sanitize_text_field', |
| 250 |
], |
| 251 |
], |
| 252 |
'summary' => [ |
| 253 |
'description' => __( 'Summary for the resource.' ), |
| 254 |
'type' => 'string', |
| 255 |
'context' => [ 'edit' ], |
| 256 |
'arg_options' => [ |
| 257 |
'sanitize_callback' => 'sanitize_text_field', |
| 258 |
], |
| 259 |
'required' => true, |
| 260 |
], |
| 261 |
'issue_date' => [ |
| 262 |
'description' => __( 'Issue date for the resource.' ), |
| 263 |
'type' => 'string', |
| 264 |
'context' => [ 'edit' ], |
| 265 |
'arg_options' => [ |
| 266 |
'sanitize_callback' => 'sanitize_text_field', |
| 267 |
], |
| 268 |
'required' => true, |
| 269 |
], |
| 270 |
'items' => [ |
| 271 |
'description' => __( 'Items for the resource.' ), |
| 272 |
'type' => 'array', |
| 273 |
'context' => [ 'edit' ], |
| 274 |
'required' => true, |
| 275 |
], |
| 276 |
], |
| 277 |
]; |
| 278 |
|
| 279 |
return $schema; |
| 280 |
} |
| 281 |
} |