From cbb8b43138227cefb2d22dd6806176dfa4d93515 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 2 May 2017 21:00:16 +0700 Subject: [PATCH] Added ProductUnit Management feature and add unit_id to product attribute --- app/Http/Controllers/ProductsController.php | 5 +- app/Http/Controllers/UnitsController.php | 63 ++++++++++++ app/Product.php | 7 +- app/Providers/AppServiceProvider.php | 6 ++ app/Unit.php | 11 +++ database/factories/ModelFactory.php | 21 ++-- .../2017_04_09_013901_create_products_table.php | 3 +- ...017_05_02_211915_create_product_units_table.php | 32 ++++++ resources/views/layouts/partials/top-nav.blade.php | 5 +- resources/views/products/partials/forms.blade.php | 4 + resources/views/units/index.blade.php | 35 +++++++ resources/views/units/partials/forms.blade.php | 33 +++++++ routes/web.php | 7 +- tests/Feature/ManageProductsTest.php | 9 ++ tests/Feature/ManageUnitsTest.php | 108 +++++++++++++++++++++ 15 files changed, 330 insertions(+), 19 deletions(-) create mode 100644 app/Http/Controllers/UnitsController.php create mode 100644 app/Unit.php create mode 100644 database/migrations/2017_05_02_211915_create_product_units_table.php create mode 100644 resources/views/units/index.blade.php create mode 100644 resources/views/units/partials/forms.blade.php create mode 100644 tests/Feature/ManageUnitsTest.php diff --git a/app/Http/Controllers/ProductsController.php b/app/Http/Controllers/ProductsController.php index a23d1a4..fbd2104 100644 --- a/app/Http/Controllers/ProductsController.php +++ b/app/Http/Controllers/ProductsController.php @@ -30,9 +30,10 @@ class ProductsController extends Controller 'name' => 'required|max:20', 'cash_price' => 'required|numeric', 'credit_price' => 'nullable|numeric', + 'unit_id' => 'required|numeric', ]); - Product::create($request->only('name','cash_price','credit_price')); + Product::create($request->only('name','cash_price','credit_price','unit_id')); flash(trans('product.created'), 'success'); @@ -49,7 +50,7 @@ class ProductsController extends Controller $routeParam = $request->only('q'); - $product = Product::findOrFail($productId)->update($request->only('name','cash_price','credit_price')); + $product = Product::findOrFail($productId)->update($request->only('name','cash_price','credit_price','unit_id')); flash(trans('product.updated'), 'success'); diff --git a/app/Http/Controllers/UnitsController.php b/app/Http/Controllers/UnitsController.php new file mode 100644 index 0000000..7c34245 --- /dev/null +++ b/app/Http/Controllers/UnitsController.php @@ -0,0 +1,63 @@ +get('action'), ['edit','delete']) && $request->has('id')) + $editableUnit = Unit::find($request->get('id')); + + return view('units.index', compact('units','editableUnit')); + } + + public function store(Request $request) + { + $this->validate($request, [ + 'name' => 'required|max:20', + ]); + + Unit::create($request->only('name')); + + flash(trans('unit.created'), 'success'); + + return redirect()->route('units.index'); + } + + public function update(Request $request, $unitId) + { + $this->validate($request, [ + 'name' => 'required|max:20', + ]); + + $unit = Unit::findOrFail($unitId)->update($request->only('name')); + + flash(trans('unit.updated'), 'success'); + + return redirect()->route('units.index'); + } + + public function destroy(Request $request, Unit $unit) + { + $this->validate($request, [ + 'unit_id' => 'required|exists:product_units,id|not_exists:products,unit_id', + ], [ + 'unit_id.not_exists' => trans('unit.undeleted') + ]); + + if ($request->get('unit_id') == $unit->id && $unit->delete()) { + flash(trans('unit.deleted'), 'success'); + return redirect()->route('units.index'); + } + + flash(trans('unit.undeleted'), 'error'); + return back(); + } +} diff --git a/app/Product.php b/app/Product.php index 27a9ef1..b85d0e3 100644 --- a/app/Product.php +++ b/app/Product.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model; class Product extends Model { - protected $fillable = ['name','cash_price','credit_price']; + protected $fillable = ['name','cash_price','credit_price','unit_id']; public function getPrice($type = 'cash') { @@ -17,4 +17,9 @@ class Product extends Model return $this->cash_price; } + + public function unit() + { + return $this->belongsTo(Unit::class); + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 9d001f6..332188f 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -14,6 +14,12 @@ class AppServiceProvider extends ServiceProvider public function boot() { require_once app_path().'/Helpers/helpers.php'; + \Validator::extend('not_exists', function($attribute, $value, $parameters) + { + return \DB::table($parameters[0]) + ->where($parameters[1], $value) + ->count() < 1; + }); } /** diff --git a/app/Unit.php b/app/Unit.php new file mode 100644 index 0000000..8d98a37 --- /dev/null +++ b/app/Unit.php @@ -0,0 +1,11 @@ +define(App\User::class, function (Faker\Generator $faker) { return [ @@ -25,7 +14,17 @@ $factory->define(App\User::class, function (Faker\Generator $faker) { $factory->define(App\Product::class, function (Faker\Generator $faker) { return [ 'name' => $faker->name, + 'unit_id' => function() { + return factory(App\Unit::class)->create()->id; + }, 'cash_price' => 2000, 'credit_price' => 1000, ]; }); + +/* @var \Illuminate\Database\Eloquent\Factory $factory */ +$factory->define(App\Unit::class, function (Faker\Generator $faker) { + return [ + 'name' => $faker->name, + ]; +}); diff --git a/database/migrations/2017_04_09_013901_create_products_table.php b/database/migrations/2017_04_09_013901_create_products_table.php index a10ac6d..fe2aad4 100644 --- a/database/migrations/2017_04_09_013901_create_products_table.php +++ b/database/migrations/2017_04_09_013901_create_products_table.php @@ -15,7 +15,8 @@ class CreateProductsTable extends Migration { Schema::create('products', function (Blueprint $table) { $table->increments('id'); - $table->string('name'); + $table->string('name', 60); + $table->unsignedInteger('unit_id'); $table->unsignedInteger('cash_price'); $table->unsignedInteger('credit_price')->nullable(); $table->timestamps(); diff --git a/database/migrations/2017_05_02_211915_create_product_units_table.php b/database/migrations/2017_05_02_211915_create_product_units_table.php new file mode 100644 index 0000000..ff7747c --- /dev/null +++ b/database/migrations/2017_05_02_211915_create_product_units_table.php @@ -0,0 +1,32 @@ +increments('id'); + $table->string('name', 20); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('product_units'); + } +} diff --git a/resources/views/layouts/partials/top-nav.blade.php b/resources/views/layouts/partials/top-nav.blade.php index 019adf6..526b462 100644 --- a/resources/views/layouts/partials/top-nav.blade.php +++ b/resources/views/layouts/partials/top-nav.blade.php @@ -28,9 +28,8 @@ -
  • - {{ link_to_route('products.index', trans('product.list')) }} -
  • +
  • {{ link_to_route('products.index', trans('product.list')) }}
  • +
  • {{ link_to_route('units.index', trans('unit.list')) }}