0 && $page > 0) { return true; } return false; } /** * Get all resource in the model within the module; if start and limit is specified then add in query; use relationship * @param string $module * @param string $model_name * @param integer $start * @param integer $limit * @param array $relationship * @param array $grouping * @param array $condition * @param string $generalSearch * @return array */ public static function showListResource($module = '', $model_name = '', $page_size = 0, $page = 0, $sorting = [], $relationship = [], $grouping = [], $condition = [], $dateRange = [], $generalSearch = NULL) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; if (empty($sorting)) { $model = $source::with($relationship); } else { $model = $source::with($relationship)->orderBy($sorting['field'], $sorting['sort_order']); } if(!empty($generalSearch)){ $model->search($generalSearch); } if(!empty($dateRange)){ foreach ($dateRange as $where){ if(!empty($where['field']) && !empty($where['from']) && !empty($where['to'])){ $fieldName = $where['field']; $from = $where['from']; $to = $where['to'] . " 23:59"; $model->whereBetween($fieldName,[$from,$to]); } } } foreach ($condition as $where){ if(!empty($where['field']) && !empty($where['operator']) && !empty($where['value'])){ $fieldName = $where['field']; $operator = $where['operator']; $value = $where['value']; $model->where($fieldName,$operator,$value); } } if (empty($grouping)) { $model = $model->get(); } else { $model = $model->get()->groupBy(function($grouping){ return Carbon::parse($grouping['group_by'])->format('Y'); }); } $pagination = self::validatePagination($page_size, $page); if ($pagination == true) { // $model = $source::skip($start)->take($limit)->get(); $model = $source::paginate($page_size); if (count($relationship)) { // $model = $source::with($relationship)->take($limit)->get(); $model = $source::with($relationship)->paginate($page_size); } } if (count($model) > 0) { $response = [ 'code' => '200', 'status' => "SC001", 'data' => $model->toArray() ]; } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Get all resource in the model within the module; if start and limit is specified then add in query; use relationship * @param string $module * @param string $model_name * @param integer $start * @param integer $limit * @param array $relationship * @return array */ public static function showAllResource($module = '', $model_name = '', $page_size = 0, $page = 0, $sorting = [], $relationship = [], $grouping = [], $condition = []) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = $source::with($relationship); $pagination = self::validatePagination($page_size, $page); if(!empty($condition)) { foreach ($condition as $where){ if(!empty($where['field']) && !empty($where['operator']) && !empty($where['value'])){ $fieldName = $where['field']; $operator = $where['operator']; $value = $where['value']; $model->where($fieldName,$operator,$value); } } } if (!empty($sorting)) { $model = $model->orderBy($sorting['field'], $sorting['sort_order']); if($pagination == true){ $model = $model->orderBy($sorting['field'], $sorting['sort_order'])->paginate($page_size); } } if (count($model) > 0) { $response = [ 'code' => '200', 'status' => "SC001", 'data' => !empty($sorting) ? $model->toArray() : $model->get()->toArray() ]; } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Get soft deleted resource in the model within the module using the resource id as identifier; use relationship * @param string $module * @param string $model_name * @param integer $resource_id * @param string $resource_field * @param array $relationship * @return array */ public static function showAllDeletedResource($module = '', $model_name = '', $start = 0, $limit = 0, $relationship = []) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = $source::onlyTrashed()->get(); $pagination = self::validatePagination($start, $limit); if ($pagination == true) { $model = $source::onlyTrashed()->skip($start)->take($limit)->get(); if (count($relationship)) { $model = $source::onlyTrashed()->with($relationship)->take($limit)->get(); } } if (count($model) > 0) { $response = [ 'code' => '200', 'status' => "SC001", 'data' => $model->toArray() ]; } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Get resource in the model within the module using the resource id as identifier; use relationship * @param string $module * @param string $model_name * @param integer $resource_id * @param string $resource_field * @param array $relationship * @return array */ public static function showResource($module ='', $model_name ='', $resource_id = 0, $resource_field = '', $relationship = []) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = $source::where($resource_field, '=', $resource_id)->first(); if (count($relationship)) { $model = $source::with($relationship)->where($resource_field, '=', $resource_id)->first(); // $model = $source::with($relationship)->find($resource_id); } if ($model) { $response = [ 'code' => '200', 'status' => "SC001", 'data' => ($module == "Shipment") ? $model->makeVisible('shipment_id')->toArray() : $model->toArray() ]; } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Get soft deleted resource in the model within the module using the resource id as identifier; use relationship * @param string $module * @param string $model_name * @param integer $resource_id * @param string $resource_field * @param array $relationship * @return array */ public static function showDeletedResource($module ='', $model_name ='', $resource_id = 0, $resource_field = '', $relationship = []) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = $source::onlyTrashed()->where($resource_field, $resource_id)->first(); if (count($relationship)) { $model = $source::onlyTrashed()->with($relationship)->where($resource_field, '=', $resource_id)->first(); } if ($model) { $response = [ 'code' => '200', 'status' => "SC001", 'data' => $model->toArray() ]; } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Store resource in the model within the module using the request parameters as fields and values * @param Reqeust $request_instance * @param string $module * @param string $model_name * @param array $request * @param integer $full_response * @return array */ public static function storeResource(Request $request_instance, $module = '', $model_name = '', $request = [], $full_response = 0, $is_log = 0, $created_by = 0) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = new $source; $resource_id = $model->getKeyName(); foreach ($request as $field => $value) { if (Schema::hasColumn($model->getTable(), $field)) { $model->{$field} = ($field == 'password' ? Hash::make($value) : $value); // $model->{$field} = ($field == 'password' ? bcrypt($value) : $value); } } if ($model->save()) { if ($is_log > 0) { $user_access = UserParser::getUserFromUserAccessByToken($request_instance->header('X-Auth-Token')); // Save resource log self::storeResourceLog($model_name, $model->getTable(), $model->$resource_id, ($user_access['code'] == '200' ? $user_access['data']['user_id'] : $model->$resource_id) ); } // Prepare response body $response['code'] = '201'; $response['status'] = "SC001"; if ($full_response > 0) { $response['data'] = $model->toArray(); } else { $response['id'] = $model->$resource_id; } } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while storing new resource.' ]; } return $response; } /** * Store resource log mapped into model * @param string $model_name * @param string $table_name * @param integer $resource_id * @return array */ public static function storeResourceLog($model_name = '', $table_name = '', $resource_id = 0, $created_by = 0) { $source = "\App\Modules\Utility\Models\ResourceLog"; $model = new $source; // Set field values $model->resource_model = $model_name; $model->resource_table = $table_name; $model->resource_id = $resource_id; $model->created_by = $created_by; // Save new resource log if ($model->save()) { $response = [ 'code' => '201', 'status' => "SC001", 'data' => $model->toArray() ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while storing log' ]; } return $response; } /** * Store mutltiple resource in the model within the module using the request parameters as fields and values * @param string $module * @param string $model_name * @param array $request * @param integer $full_response * @return array */ public static function storeMultipleResource(Request $request_instance, $module = '', $model_name = '', $request = [], $full_response = 0, $is_log = 0) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; foreach ($request as $resource) { $model = new $source; $resource_id = $model->getKeyName(); foreach ($resource as $field => $value) { $model->{$field} = ($field == 'password' ? Hash::make($value) : $value); // $model->{$field} = ($field == 'password' ? bcrypt($value) : $value); } $user_id = AuthHelper::getUserDataUsingAuthToken($request_instance->header('X-Auth-Token')); $truck_personnel_id = AuthHelper::getTruckPersonnelDataUsingAuthToken($request_instance->header('X-Auth-Token')); $model->created_by = $user_id != 0 ? $user_id : $truck_personnel_id; $model->updated_by = $user_id != 0 ? $user_id : $truck_personnel_id; $model->created_at = Carbon::now()->toDateTimeString(); $model->updated_at = Carbon::now()->toDateTimeString(); if ($model->save()) { if ($is_log > 0) { $user_access = UserParser::getUserFromUserAccessByToken($request_instance->header('X-Auth-Token')); // Save resource log self::storeResourceLog($model_name, $model->getTable(), $model->$resource_id, ($user_access['code'] == '200' ? $user_access['data']['user_id'] : $model->$resource_id) ); } if ($full_response > 0) { $data[] = $model->toArray(); } else { $data[] = $model->$resource_id; } } else { $response = [ 'code' => '400', 'status' => "ER000", 'message' => 'An error occurred while storing multiple resource.' ]; break; } } if (count($data) > 0) { $response['code'] = '201'; $response['status'] = "SC001"; $response['data'] = $data; } return $response; } /** * Update resource in the model within the module using the resource id as identifier as request parameters as fields and values * @param string $module * @param string $model * @param integer $resource_id * @param array $request * @param integer $full_response * @return array */ public static function updateResource(Request $request_instance, $module = '', $model_name = '', $resource_id = 0, $request = [], $full_response = 0, $is_log = 0) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; if ($model = $source::find($resource_id)) { $deactivated_at = ''; $reference_key = $model->getKeyName(); foreach ($request as $field => $value) { if (Schema::hasColumn($model->getTable(), $field)) { $model->{$field} = ($field == 'password' ? Hash::make($value) : $value); // Check if resource status is being deactiveated if ($field == 'is_active') { if ($value == 0) { $deactivated_at = Carbon::now(); } } // $model->{$field} = ($field == 'password' ? bcrypt($value) : $value); } // $model->{$field} = ($field == 'password' ? bcrypt($value) : $value); } if ($model->save()) { if ($is_log > 0) { $user_access = UserParser::getUserFromUserAccessByToken($request_instance->header('X-Auth-Token')); // Update resource log mapped into model self::updateResourceLog($model_name, $model->getTable(), $model->$reference_key, $user_access['data']['user_id'], 'modified_by', $deactivated_at); } // Prepare response body $response['code'] = '200'; $response['status'] = "SC001"; if ($full_response > 0) { $response['data'] = $model->toArray(); } else { $response['id'] = $model->$reference_key; } } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while updating resource.' ]; } } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Update resource in the model using the resource id and resource field as identifier using the request parameters as values * @param string $module * @param string $model * @param integer $resource_id * @param array $request * @param string $resource_field * @param integer $full_response * @return array */ public static function updateResourceByFieldValue(Request $request_instance, $module = '', $model_name = '', $resource_id = 0, $request = [], $resource_field = '', $full_response = 0) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = $source::where($resource_field, $resource_id)->first(); if ($model) { $reference_key = $model->getKeyName(); foreach ($request as $field => $value) { if (Schema::hasColumn($model->getTable(), $field)) { $model->{$field} = ($field == 'password' ? Hash::make($value) : $value); } } if ($model->save()) { // Prepare response body $response['code'] = '200'; $response['status'] = "SC001"; if ($full_response > 0) { $response['data'] = $model->toArray(); } else { $response['id'] = $model->$reference_key; } } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while updating resource.' ]; } } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found' ]; } return $response; } /** * Update resource in the model using the resource id, resource field , resource header id and resource header field as identifier using the request parameters as values * @param string $module * @param string $model * @param integer $resource_id * @param integer $resource_header_id * @param array $request * @param string $resource_field * @param string $resource_header_field * @param integer $full_response * @return array */ public static function updateResourceDetailByFieldValue(Request $request_instance, $module = '', $model_name = '', $resource_id = "", $resource_header_id = 0, $request = [], $resource_field = '',$resource_header_field = '', $full_response = 0) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = $source::where($resource_field, $resource_id) ->where($resource_header_field,$resource_header_id)->first(); if ($model) { $reference_key = $model->getKeyName(); foreach ($request as $field => $value) { $field = ($field == 'reason_uuid') ? 'reason_id' : $field; if (Schema::hasColumn($model->getTable(), $field)) { if($field == 'password'){ $model->{$field} = Hash::make($value); }elseif($field == 'reason_id'){ $reason_id = self::getResourceId($request_instance, 'Reason' , 'Reason' , 'reason_uuid', $value); $model->{$field} = $reason_id; }else{ $model->{$field} = $value; } } } if ($model->save()) { // Prepare response body $response['code'] = '200'; $response['status'] = "SC001"; if ($full_response > 0) { $response['data'] = $model->toArray(); } else { $response['id'] = $model->$reference_key; } } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while updating resource.' ]; } } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found' ]; } return $response; } /** * Update resource log mapped into model * @param string $model_name * @param string $table_name * @param integer $resource_id * @param integer $modified_by * @param string $deactivated_at * @return array */ public static function updateResourceLog($model_name = '', $table_name = '', $resource_id = 0, $modified_by = 0, $reference_field = '', $deactivated_at = '') { $source = "\App\Modules\Utility\Models\ResourceLog"; $model = $source::whereResourceModel($model_name)->whereResourceTable($table_name)->whereResourceId($resource_id)->first(); // Check if resource log is found if ($model) { // Set field values $model->{$reference_field} = $modified_by; $model->deactivated_at = ($deactivated_at != '' ? $deactivated_at : null); // Save new resource log if ($model->save()) { $response = [ 'code' => '201', 'status' => "SC001", 'data' => $model->toArray() ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while storing log' ]; } } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource log not found' ]; } return $response; } /** * Soft delete resource in the model within the module using the resource id as identifier * @param string $module * @param string $model_name * @param integer $resource_id * @return array */ public static function deleteResource(Request $request_instance, $module = '', $model_name = '', $resource_id = 0, $resource_field = "", $is_log = 0) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; if ($model = $source::where($resource_field , "=", $resource_id)) { if ($model->delete()) { if ($is_log > 0) { $reference_key = $model->getKeyName(); $user_access = UserParser::getUserFromUserAccessByToken($request_instance->header('X-Auth-Token')); // Update resource log mapped into model self::updateResourceLog($model_name, $model->getTable(), $model->$reference_key, $user_access['data']['user_id'], 'deleted_by'); } $response = [ 'code' => '200', 'status' => "SC001", 'message' => 'Resource deleted.', 'data' => $model ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while deleting resource.' ]; } } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Restore soft deleted resource in the model within the module using the resource id as identifier * @param string $module * @param string $model_name * @param integer $resource_id * @return array */ public static function restoreResource($module = '', $model_name = '', $resource_id = 0, $resource_field = '') { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; if ($model = $source::onlyTrashed()->where($resource_field, $resource_id)) { if ($model->restore()) { $response = [ 'code' => '200', 'status' => "SC001", 'data' => $resource_id ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while deleting resource.' ]; } } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Force delete resource in the model within the module using the resource id as identifier * @param string $module * @param string $model_name * @param integer $resource_id * @return array */ public static function forceDeleteResource($module = '', $model_name = '', $resource_id = 0, $resource_field = '', $uses_soft_delete = 1) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = (($uses_soft_delete == 1) ? $source::withTrashed()->where($resource_field, $resource_id) : $source::where($resource_field, $resource_id)); if ($model) { if ($model->forceDelete()) { $response = [ 'code' => '200', 'status' => "SC001", 'data' => $resource_id ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while deleting resource.' ]; } } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Force delete batch resources in the model within the module using the resource id as identifier * @param string $module * @param string $model_name * @param integer $resource_id * @param integer $where_in * @return array */ public static function forceBatchDeleteResource($module = '', $model_name = '', $resource_id = 0, $resource_field = '', $where_in = 1, $uses_soft_delete = 1) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; if ($where_in == 1) { $model = (($uses_soft_delete == 1) ? $source::withTrashed()->whereIn($resource_field, $resource_id) : $source::whereIn($resource_field, $resource_id)); } else { // delete Ids not labaled $model = (($uses_soft_delete == 1) ? $source::withTrashed()->whereNotIn($resource_field, $resource_id) : $source::whereNotIn($resource_field, $resource_id)); } if ($model) { if ($model->forceDelete()) { $response = [ 'code' => '200', 'status' => "SC001", 'data' => $resource_id ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'An error occurred while deleting resource.' ]; } } else { $response = [ 'code' => '404', 'status' => "NF002", 'message' => 'Resource not found.' ]; } return $response; } /** * Batch update resources (configuration) in the model within the module using the request parameters as fields and values * @param Reqeust $request_instance * @param string $table * @param string $reference_field * @param string $update_field * @param array $request * @param integer $is_logs * @return array */ public static function batchUpdate(Request $request_instance, $table ='', $reference_field = '', $update_field = '', $request = [], $is_log = 0) { // build raw sql $sql = "UPDATE $table SET $update_field = CASE [case] END"; $case = ""; $batch_updated = []; // loop thru input foreach($request as $ref => $value){ if(!is_array($value)) { $case .= "WHEN $reference_field = '". $ref . "' THEN '" . $value ."' "; array_push($batch_updated, "'".$ref."'"); } } // Replace [case] $sql = str_replace("[case]", $case, $sql); $sql .= " WHERE configuration IN(". implode(',', $batch_updated).")"; $model = count($batch_updated) > 0 ? DB::update(DB::raw($sql)) : 0; if ($model > 0) { if ($is_log > 0) { $user_access = UserParser::getUserFromUserAccessByToken($request_instance->header('X-Auth-Token')); // Save resource log self::storeResourceLog($model_name, $table, $reference_field, ($user_access['code'] == '200' ? $user_access['data']['user_id'] : $reference_field) ); } // Prepare response body $response = [ 'code' => '200', 'status' => "SC001", 'message' => 'Resources updated.' ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'No changes made.' ]; } return $response; } /** * Batch update resources (single field/column) in the model within the module using the request parameters as fields and values * @param Request $request_instance * @param string $table * @param string $reference_field * @param string $update_field * @param array $request * @param integer $is_log * @return array */ public static function batchUpdateResource(Request $request_instance, $module = '', $model = '', $ref_field = '', $update_field = '', $comp_op = '', $request = [], $is_log = 0) { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model; // Instantiate model and get table name $table = (new $source())->getTable(); // build sql using laravel's query builder foreach($request as $key => $arr_value) { $resource = DB::table($table)->where($ref_field, $comp_op, $request[$key][$ref_field])->update([$update_field => $request[$key][$update_field]]); Log::info("update ".$table." :". $resource); } if ($resource > 0) { if ($is_log > 0) { $user_access = UserParser::getUserFromUserAccessByToken($request_instance->header('X-Auth-Token')); // Save resource log self::storeResourceLog($model, $table, $ref_field, ($user_access['code'] == '200' ? $user_access['data']['user_id'] : $ref_field) ); } // Prepare response body $response = [ 'code' => '200', 'status' => "SC001", 'message' => 'Resources updated.' ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'No changes made.' ]; } return $response; } /** * Batch update resources (single field/column) in the model within the module using the request parameters as fields and values * @param Request $request_instance * @param string $table * @param string $reference_field * @param string $update_field * @param array $request * @param integer $is_log * @return array */ public static function batchUpdateWithDetailResource(Request $request_instance, $module = '', $model = '', $model_detail = '', $ref_field = '', $update_field = '', $comp_op = '', $request = [], $resource_detail_field = "", $detail_ref_field = "", $is_log = 0) { //get user_id based on X-Authorization $trucker_id = AuthHelper::getTruckPersonnelDataUsingAuthToken($request_instance->header('X-Auth-Token')); $resource_detail = 0; $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model; // Instantiate model and get table name $table = (new $source())->getTable(); // build sql using laravel's query builder foreach($request as $key => $arr_value) { if(!empty($update_field)){ $resource = DB::table($table)->where($ref_field, $comp_op, $request[$key][$ref_field])->update([$update_field => $request[$key][$update_field]]); }else{ $resource = 1; } $reference_id = DB::table($table)->where($ref_field, $comp_op, $request[$key][$ref_field])->first(); $ref_column = $table."_id"; if($resource > 0){ foreach($request[$key][$resource_detail_field] as $key_detail => $arr_detail){ $resource_detail = self::updateResourceDetailByFieldValue($request_instance, $module, $model_detail, $arr_detail[$detail_ref_field], $reference_id->$ref_column, $arr_detail, $detail_ref_field , $ref_column, 1); } //for excess if(!empty($request[$key]['excess'])){ foreach($request[$key]['excess'] as $key_excess => $arr_excess){ $arr_excess['created_by'] = $trucker_id; $arr_excess['shipment_delivery_id'] = $reference_id->$ref_column; //var_dump($arr_excess);die(); $save_excess = self::storeResource($request_instance, $module, $model_detail, $arr_excess, 1, 0, $trucker_id); } }else{ $save_excess = 0; } } } if ($resource_detail['code'] == '200' && $save_excess !== NULL) { if ($is_log > 0) { $user_access = UserParser::getUserFromUserAccessByToken($request_instance->header('X-Auth-Token')); // Save resource log self::storeResourceLog($model, $table, $ref_field, ($user_access['code'] == '200' ? $user_access['data']['user_id'] : $ref_field) ); } // Prepare response body $response = [ 'code' => '200', 'status' => "SC001", 'message' => 'Resources updated.' ]; } else { $response = [ 'code' => '500', 'status' => "ER000", 'message' => 'No changes made.' ]; } return $response; } public static function getResourceId(Request $request_instance, $module = '', $model_name = '', $resource_field = '', $resource_id = '') { $source = "\App\Modules\\".$module."\\".$module."\Models\\".$model_name; $model = $source::where($resource_field, $resource_id)->first(); if ($model) { $reference_key = $model->getKeyName(); } return $model[$reference_key]; } }