diff --git a/app/Http/Controllers/BookingController.php b/app/Http/Controllers/BookingController.php index cd47742a..eb5e8ded 100644 --- a/app/Http/Controllers/BookingController.php +++ b/app/Http/Controllers/BookingController.php @@ -17,7 +17,7 @@ class BookingController extends Controller public function store(Request $request) { - $rates = Rate::all()->last(); + $rates = Rate::orderby('updated_at','desc')->first(); $term = $request->input('term'); $rate = 1; // default switch ($term) { @@ -129,7 +129,7 @@ class BookingController extends Controller public function uploadbankslip(Request $request, $id) { $booking = Booking::where('user_id',Auth::user()->id)->where('id',$id)->first(); - + /* Validation*/ if (!$booking) { @@ -175,7 +175,7 @@ class BookingController extends Controller public function uploadPurchaseOrder(Request $request, $id) { - $booking = Booking::where('user_id',Auth::user()->id)->where('id',$id)->first(); + $booking = Booking::where('user_id',Auth::user()->id)->where('id',$id)->first(); /* Validation */ if (!$booking) @@ -198,13 +198,12 @@ class BookingController extends Controller } /* End Validation */ - $filename = $user_input_file->getClientOriginalName(); $ext = $user_input_file->getClientOriginalExtension(); $input['image_url'] = $filename; $unique_name = 'purchase_order_' . md5($filename. time()); - $unique_image_url = $unique_name. '.' .$ext; + $unique_image_url = $unique_name. '.' .$ext; Storage::putFileAs( 'image_url',/*folder name*/ $user_input_file, $unique_image_url ); @@ -217,6 +216,7 @@ class BookingController extends Controller return response()->json(['message'=>'Success'],200); } + public function delete(Booking $book_id) { $booking = Booking::where('user_id', Auth::user())->where('id', $book_id)->firstOrFail(); @@ -224,5 +224,16 @@ class BookingController extends Controller return response()->json($book_id, 204); } - // TODO : cancel booking feature + public function cancel(Request $request, $book_id) + { + if (!$booking = Booking::where('user_id',Auth::user()->id)->where('id',$book_id)->first()) + { + return response()->json(['message'=>'Access denied'], 200); + } + + $booking->is_cancel = true; + $booking->save(); + + return response()->json(['message'=>'Success'],200); + } } diff --git a/database/factories/BookingFactory.php b/database/factories/BookingFactory.php new file mode 100644 index 00000000..f20d48ef --- /dev/null +++ b/database/factories/BookingFactory.php @@ -0,0 +1,40 @@ +define(App\Booking::class, function (Faker $faker) { + $rate_id = App\Rate::pluck('id')->toArray(); + $user_id = App\User::pluck('id')->toArray(); + + return [ + 'account_name' => $faker->name, + 'account_num' => $faker->unique()->randomDigit, + 'bank_name' => $faker->name, + 'bank_branch' => $faker->unique()->address, + 'company_name' => $faker->name, + 'company_address' => $faker->unique()->address, + 'bank_address' => $faker->unique()->address, + 'swift_code' => $faker->unique()->randomDigit, + 'cnap' => $faker->randomDigit, + 'term' => str_random(10), + 'amount'=> $faker->randomDigit, + + 'rate_id' => $faker->randomElement($rate_id), + 'user_id' => $faker->randomElement($user_id), + + 'rmb_book_amount' => $faker->randomDigit, + 'rmb_book_pay_method' => str_random(10), + 'rmb_book_account_name' => $faker->name, + 'rmb_book_acc_no' => $faker->randomDigit, + 'rmb_book_bank_branch' => $faker->unique()->address, + + 'usd_book_amount' => $faker->randomDigit, + 'usd_book_pay_method'=> str_random(10), + 'usd_book_company_name'=> $faker->name, + 'usd_book_company_address' => $faker->unique()->address, + 'usd_book_swift_code'=> $faker->unique()->randomDigit, + 'usd_book_cnap' => $faker->randomDigit, + 'usd_book_bank_branch' => $faker->unique()->address, + + ]; +}); diff --git a/database/migrations/2018_06_16_025657_add_cancel_booking_table.php b/database/migrations/2018_06_16_025657_add_cancel_booking_table.php new file mode 100644 index 00000000..b8abe8bb --- /dev/null +++ b/database/migrations/2018_06_16_025657_add_cancel_booking_table.php @@ -0,0 +1,34 @@ +boolean('is_cancel')->default('0'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + if (Schema::hasColumn('bookings', 'is_cancel')) { + Schema::table('bookings', function (Blueprint $table) { + // $table->dropColumn('is_cancel'); + }); + } + } +} diff --git a/routes/api.php b/routes/api.php index 8f55cbcd..56840ca9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -81,5 +81,6 @@ Route::delete('setting-supplier/{supplier}', 'SettingSupplierController@delete') Route::post('upload-china-bankslip', 'ChinaBankSlipController@store'); +Route::post('booking/{book_id}/cancel', 'BookingController@cancel'); Route::post('booking/{id}/upload-user-bankslip', 'BookingController@uploadbankslip'); Route::post('booking/{id}/upload-po', 'BookingController@uploadPurchaseOrder'); diff --git a/tests/Feature/BookingTest.php b/tests/Feature/BookingTest.php index 7ec79a24..5af1df68 100644 --- a/tests/Feature/BookingTest.php +++ b/tests/Feature/BookingTest.php @@ -18,77 +18,90 @@ use App\Booking; class BookingTest extends TestCase { protected $user; + protected $booking; public function setUp() { parent::setUp(); Artisan::call('db:seed'); $this->user = factory(User::class)->create(); + $this->booking = factory(Booking::class)->create([ + 'user_id' => $this->user->id + ]); } public function testPost() { $response = $this->actingAs($this->user) - ->postJson('/api/booking/', [ - 'account_name' => 'johnny', - 'account_num' => '1234567', - 'bank_name' => 'myabnak', - 'bank_branch' => 'banking', - 'company_name' => 'besaras', - 'company_address' => 'californina', - 'bank_address' => 'cyber', - 'swift_code' => '123wert', - 'cnap' => '2131rew', - 'term' => 'x2_cash', - 'amount' => '14000', - 'rmb_book_amount' => '2131', - 'rmb_book_pay_method' => '2131rea', - 'rmb_book_account_name' => '2131rew', - 'rmb_book_acc_no' => '2131', - 'rmb_book_bank_branch' => '2131rew', - 'usd_book_amount' => '2131', - 'usd_book_pay_method' => '2131rew', - 'usd_book_company_name' => '2131rew', - 'usd_book_company_address' => '2131rew', - 'usd_book_swift_code' => '2131rew', - 'usd_book_cnap' => '2131rew', - 'usd_book_bank_branch' => '2131rew', - 'verification_status' => '1' - ]); - $response->assertStatus(201); + ->postJson('/api/booking/', [ + 'account_name' => 'johnny', + 'account_num' => '1234567', + 'bank_name' => 'myabnak', + 'bank_branch' => 'banking', + 'company_name' => 'besaras', + 'company_address' => 'californina', + 'bank_address' => 'cyber', + 'swift_code' => '123wert', + 'cnap' => '2131rew', + 'term' => 'x2_cash', + 'amount' => '14000', + 'rmb_book_amount' => '2131', + 'rmb_book_pay_method' => '2131rea', + 'rmb_book_account_name' => '2131rew', + 'rmb_book_acc_no' => '2131', + 'rmb_book_bank_branch' => '2131rew', + 'usd_book_amount' => '2131', + 'usd_book_pay_method' => '2131rew', + 'usd_book_company_name' => '2131rew', + 'usd_book_company_address' => '2131rew', + 'usd_book_swift_code' => '2131rew', + 'usd_book_cnap' => '2131rew', + 'usd_book_bank_branch' => '2131rew', + 'verification_status' => '1' + ]) + ->assertStatus(201); } public function testuploadbankslip() { - $this->actingAs($this->user); Storage::fake('bankslip_url'); $bankslip_url = UploadedFile::fake()->image('comp.jpg'); + if(!file_exists($bankslip_url)){ $response->assertStatus(400); } - $response = $this->json('POST', '/api/booking/1/upload-user-bankslip', [ - 'bankslip_url' => $bankslip_url, - 'transfer_amount' => '9000' - ]); - $response->assertStatus(200); + $this->actingAs($this->user) + ->json('POST', '/api/booking/' . $this->booking->id . '/upload-user-bankslip', [ + 'bankslip_url' => $bankslip_url, + 'transfer_amount' => '9000' + ]) + ->assertSuccessful(); } public function testuploadPO() { - $this->actingAs($this->user); Storage::fake('image_url'); $image_url = UploadedFile::fake()->image('comp.jpg'); + if(!file_exists($image_url)){ $response->assertStatus(400); } - - $response = $this->json('POST', '/api/booking/1/upload-po', [ - 'image_url' => UploadedFile::fake()->image('comp.jpg'), - 'amount' => '12345' - ]); - $response->assertStatus(200); + + $this->actingAs($this->user) + ->json('POST', '/api/booking/' . $this->booking->id . '/upload-po', [ + 'image_url' => UploadedFile::fake()->image('comp.jpg'), + 'amount' => '12345' + ]) + ->assertSuccessful(); + } + public function testCancel() + { + $this->actingAs($this->user) + ->json('POST','/api/booking/' . $this->booking->id . '/cancel', []) + ->assertSuccessful(); + } } diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index e628820c..b7992649 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -4131,7 +4131,7 @@ return array( 'Tests\\Feature\\RegisterTest' => $baseDir . '/tests/Feature/RegisterTest.php', 'Tests\\Feature\\SettingsTest' => $baseDir . '/tests/Feature/SettingsTest.php', 'Tests\\TestCase' => $baseDir . '/tests/TestCase.php', - 'Tests\\Unit\\BookingTest\\BookingTest' => $baseDir . '/tests/Feature/BookingTest2.php', + 'Tests\\Unit\\BookingTest\\BookingTest' => $baseDir . '/tests/Feature/BookingTest.php', 'Text_Template' => $vendorDir . '/phpunit/php-text-template/src/Template.php', 'TheSeer\\Tokenizer\\Exception' => $vendorDir . '/theseer/tokenizer/src/Exception.php', 'TheSeer\\Tokenizer\\NamespaceUri' => $vendorDir . '/theseer/tokenizer/src/NamespaceUri.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index fdae4849..df5c5837 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4559,7 +4559,7 @@ class ComposerStaticInit6dcfe61ea7999ade723f072fea748b5a 'Tests\\Feature\\RegisterTest' => __DIR__ . '/../..' . '/tests/Feature/RegisterTest.php', 'Tests\\Feature\\SettingsTest' => __DIR__ . '/../..' . '/tests/Feature/SettingsTest.php', 'Tests\\TestCase' => __DIR__ . '/../..' . '/tests/TestCase.php', - 'Tests\\Unit\\BookingTest\\BookingTest' => __DIR__ . '/../..' . '/tests/Feature/BookingTest2.php', + 'Tests\\Unit\\BookingTest\\BookingTest' => __DIR__ . '/../..' . '/tests/Feature/BookingTest.php', 'Text_Template' => __DIR__ . '/..' . '/phpunit/php-text-template/src/Template.php', 'TheSeer\\Tokenizer\\Exception' => __DIR__ . '/..' . '/theseer/tokenizer/src/Exception.php', 'TheSeer\\Tokenizer\\NamespaceUri' => __DIR__ . '/..' . '/theseer/tokenizer/src/NamespaceUri.php',