From b96e2aa78bc836226660fd5a9491c08023b5283f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 13 Jul 2020 09:29:44 +1000 Subject: [PATCH] Bulk actions for tax rates --- .../Controllers/OpenAPI/TaxRateSchema.php | 1 + app/Http/Controllers/TaxRateController.php | 85 ++++++++++++++++++- app/Services/Invoice/AutoBillInvoice.php | 13 +-- app/Transformers/TaxRateTransformer.php | 1 + ..._065301_add_token_id_to_activity_table.php | 4 + routes/api.php | 1 + 6 files changed, 92 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/OpenAPI/TaxRateSchema.php b/app/Http/Controllers/OpenAPI/TaxRateSchema.php index 17ae45587f00..5e0b7f882896 100644 --- a/app/Http/Controllers/OpenAPI/TaxRateSchema.php +++ b/app/Http/Controllers/OpenAPI/TaxRateSchema.php @@ -6,5 +6,6 @@ * @OA\Property(property="id", type="string", example="Opnel5aKBz", description="______"), * @OA\Property(property="name", type="string", example="GST", description="______"), * @OA\Property(property="rate", type="number", example="10", description="______"), + * @OA\Property(property="is_deleted", type="boolean", example=true, description="______"), * ) */ diff --git a/app/Http/Controllers/TaxRateController.php b/app/Http/Controllers/TaxRateController.php index d007b15ff6fc..995c988f6576 100644 --- a/app/Http/Controllers/TaxRateController.php +++ b/app/Http/Controllers/TaxRateController.php @@ -12,13 +12,14 @@ namespace App\Http\Controllers; use App\Factory\TaxRateFactory; +use App\Http\Requests\TaxRate\CreateTaxRateRequest; use App\Http\Requests\TaxRate\DestroyTaxRateRequest; use App\Http\Requests\TaxRate\EditTaxRateRequest; use App\Http\Requests\TaxRate\ShowTaxRateRequest; use App\Http\Requests\TaxRate\StoreTaxRateRequest; use App\Http\Requests\TaxRate\UpdateTaxRateRequest; -use App\Http\Requests\TaxRate\CreateTaxRateRequest; use App\Models\TaxRate; +use App\Repositories\BaseRepository; use App\Transformers\TaxRateTransformer; use Illuminate\Http\Request; @@ -32,9 +33,13 @@ class TaxRateController extends BaseController protected $entity_transformer = TaxRateTransformer::class; - public function __construct() + protected $base_repo; + + public function __construct(BaseRepository $base_repo) { parent::__construct(); + + $this->base_repo = $base_repo; } /** @@ -354,8 +359,82 @@ class TaxRateController extends BaseController */ public function destroy(DestroyTaxRateRequest $request, TaxRate $tax_rate) { + $tax_rate->is_deleted = true; + $tax_rate->save(); $tax_rate->delete(); - return response()->json([], 200); + return $this->itemResponse($tax_rate); } + + + /** + * Perform bulk actions on the list view + * + * @param BulkTaxRateRequest $request + * @return \Illuminate\Http\Response + * + * + * @OA\Post( + * path="/api/v1/tax_rates/bulk", + * operationId="bulkTaxRates", + * tags={"tax_rates"}, + * summary="Performs bulk actions on an array of TaxRates", + * description="", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter(ref="#/components/parameters/index"), + * @OA\RequestBody( + * description="Tax Rates", + * required=true, + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema( + * type="array", + * @OA\Items( + * type="integer", + * description="Array of hashed IDs to be bulk 'actioned", + * example="[0,1,2,3]", + * ), + * ) + * ) + * ), + * @OA\Response( + * response=200, + * description="The TaxRate List response", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/Webhook"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function bulk() + { + $action = request()->input('action'); + + $ids = request()->input('ids'); + + $tax_rate = TaxRate::withTrashed()->find($this->transformKeys($ids)); + + + $tax_rate->each(function ($tax_rat, $key) use ($action) { + if (auth()->user()->can('edit', $tax_rate)) { + $this->base_repo->{$action}($tax_rate); + } + }); + + return $this->listResponse(TaxRate::withTrashed()->whereIn('id', $this->transformKeys($ids))); + } + } diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index ee8f3baed3e6..397336a1855d 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -43,10 +43,9 @@ class AutoBillInvoice extends AbstractService return $this->invoice; if($this->invoice->balance > 0) - $gateway_token = $this->getGateway($this->invoice->balance); - - // else - // return $this->invoice->service()->markPaid()->save(); + $gateway_token = $this->getGateway($this->invoice->balance); + else + return $this->invoice->service()->markPaid()->save(); if(!$gateway_token) return $this->invoice; @@ -81,12 +80,6 @@ class AutoBillInvoice extends AbstractService return $gateway_token; } - // return collect($gateway_tokens)->filter(function ($token) use ($amount){ - - // return $this->validGatewayLimits($token, $amount); - - // })->first(); - } /** diff --git a/app/Transformers/TaxRateTransformer.php b/app/Transformers/TaxRateTransformer.php index b408923a1548..771a0ede28c5 100644 --- a/app/Transformers/TaxRateTransformer.php +++ b/app/Transformers/TaxRateTransformer.php @@ -18,6 +18,7 @@ class TaxRateTransformer extends EntityTransformer 'id' => (string) $this->encodePrimaryKey($tax_rate->id), 'name' => (string) $tax_rate->name, 'rate' => (float) $tax_rate->rate, + 'is_deleted' => (bool) $tax_rate->is_deleted, 'updated_at' => (int)$tax_rate->updated_at, 'archived_at' => (int)$tax_rate->deleted_at, 'created_at' => (int)$tax_rate->created_at, diff --git a/database/migrations/2020_07_08_065301_add_token_id_to_activity_table.php b/database/migrations/2020_07_08_065301_add_token_id_to_activity_table.php index b2e87940799b..ed2ff8940e6e 100644 --- a/database/migrations/2020_07_08_065301_add_token_id_to_activity_table.php +++ b/database/migrations/2020_07_08_065301_add_token_id_to_activity_table.php @@ -16,6 +16,10 @@ class AddTokenIdToActivityTable extends Migration Schema::table('activities', function (Blueprint $table) { $table->unsignedInteger('token_id')->nullable(); }); + + Schema::table('tax_rates', function (Blueprint $table) { + $table->boolean('is_deleted')->default(0); + }); } /** diff --git a/routes/api.php b/routes/api.php index 650d953553d1..86573edd40e4 100644 --- a/routes/api.php +++ b/routes/api.php @@ -117,6 +117,7 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('group_settings/bulk', 'GroupSettingController@bulk'); Route::resource('tax_rates', 'TaxRateController');// name = (tasks. index / create / show / update / destroy / edit + Route::post('tax_rates/bulk', 'TaxRateController@bulk')->name('tax_rates.bulk'); Route::post('refresh', 'Auth\LoginController@refresh');