diff --git a/app/Http/Controllers/BookingController.php b/app/Http/Controllers/BookingController.php index 1ab412a6..dcc1c17e 100644 --- a/app/Http/Controllers/BookingController.php +++ b/app/Http/Controllers/BookingController.php @@ -13,6 +13,7 @@ use App\Invoice; use App\SettingCredit; use App\SettingTaxRate; use App\SettingBillingCharge; +use App\Notification; use Auth; use Validator; use DateTime; @@ -80,7 +81,7 @@ class BookingController extends Controller $user = Auth::user(); $bookings = Booking::select('user_id', 'id', 'created_at', 'admin_status', 'rate', 'term', 'amount', 'bia', 'verification_status') ->orderby('updated_at','desc') - ->get(); + ->paginate(10); // reformat to fit frontend structure foreach ($bookings as $booking) { @@ -248,7 +249,9 @@ class BookingController extends Controller $term = $request->input('term'); $amount = $request->input("amount"); // in RMB $rates = Rate::orderby('updated_at','desc')->first(); - $creditLimit = SettingCredit::orderby('updated_at','desc')->first(); + $creditLimit = SettingCredit::orderby('updated_at','desc')->first(); + $taxrate = SettingTaxRate::latest()->first()->tax_rate; + $billingChargeRate = SettingBillingCharge::latest()->first()->billing_charge_rate; if (!$creditLimit){ return response()->json("Credit limit not set, please contact admin", 405); @@ -290,27 +293,31 @@ class BookingController extends Controller } // Service charge (RMB) : if amount higher than 10,000, no service charge. if amount lower than 10,000, 20.00 service charge - if ($amount >= 10000 && ($term == 'x2_cash' || $term == 'x2_cheque' || $term == 'x2_ba')) { + if ($amount >= 10000 && ($term == 'x2_cash' || $term == 'x2_cheque' || $term == 'x2_ba')) { $svcharge = 0; } else { $svcharge = 20.00; } - - // initial rmb to myr amount - $RMBinMYR = (($amount + $svcharge) / $rate); - - // TODO : Should be remove this ? - // gst 0% - $gst = 0 * $RMBinMYR; - - // Sales tax : 10 % - $sales_tax = 0.10 * $RMBinMYR; - - // Billing fees : 1.5% - $billing = 0.015 * $RMBinMYR; - - // Bank in amount - $bia = round($RMBinMYR + $gst + $sales_tax + $billing, 2); + + $subtotal = round(($amount + $svcharge) / $rate,2); + + // TODO : refactor + if ($taxrate !=0){ + $taxAmount = round($subtotal * $taxrate - $subtotal,2); + } + else{ + $taxAmount = 0; + } + + if ($billingChargeRate !=0){ + $billingChargeAmount = round(($amount + $svcharge) * $billingChargeRate - ($amount + $svcharge), 2); + } + else{ + $billingChargeAmount = 0; + } + + $billingChargeAmount = round((($amount / $rate) * $billingChargeRate) - ($amount / $rate), 2); + $bankin_amount = round($subtotal + $taxAmount + $billingChargeAmount , 2); $booking = new Booking(); $booking->account_name = $request->input('account_name'); @@ -323,9 +330,9 @@ class BookingController extends Controller $booking->term = $term; $booking->rate_id = $rates->id; $booking->rate = $rate; - $booking->rmb_book_amount = $request->input('amount'); + $booking->rmb_book_amount = $request->input('amount'); // BIG TODO : amount change variable name to rmb_book_amount $booking->status = 2; - $booking->bia = $bia; + $booking->bia = $bankin_amount; // BID TODO : change bia viariable name // $booking->user_id = $user->id; $booking->user()->associate($user); @@ -456,9 +463,16 @@ class BookingController extends Controller $booking->admin_status = 2; $booking->save(); + // Add notification message to admin + $adminNotification = new Notification; + $adminNotification->detail = "A bankslip is uploaded for booking " . $booking->id; + $adminNotification->link = "/booking/" . $booking->id . "/verification"; + $adminNotification->user_id = User::withRole('admin')->first()->id; + $adminNotification->save(); + $user_bankslip->transfer_amount = $request->input('transfer_amount'); $user_bankslip->save(); - return response()->json(["message"=> "Success"],200); + return response()->json($adminNotification,200); } public function uploadPurchaseOrder(Request $request, $id) @@ -570,7 +584,6 @@ class BookingController extends Controller if($amount > $creditLimit->rmb_credit_limit){ return response()->json(["message" => "Exceed credit limit, please contact our sales team for larger quantitiy"], 400); } - switch ($term) { case "x1_cash": @@ -604,16 +617,31 @@ class BookingController extends Controller ], 422); break; } - - if (($amount >= 10000) && ($term == 'x2_cash' || $term == 'x2_cheque' || $term == 'x2_ba')) { + + if ($amount >= 10000 && ($term == 'x2_cash' || $term == 'x2_cheque' || $term == 'x2_ba')) { $svcharge = 0; } else { $svcharge = 20.00; } - $subtotal = round($amount + $svcharge / $rate,2); - $taxAmount = round($subtotal * $taxrate,2); - $billingChargeAmount = round(($amount + $svcharge) * $billingChargeRate, 2); + $subtotal = round(($amount + $svcharge) / $rate,2); + + // TODO : refactor + if ($taxrate !=0){ + $taxAmount = round($subtotal * $taxrate - $subtotal,2); + } + else{ + $taxAmount = 0; + } + + if ($billingChargeRate !=0){ + $billingChargeAmount = round(($amount + $svcharge) * $billingChargeRate - ($amount + $svcharge), 2); + } + else{ + $billingChargeAmount = 0; + } + + $billingChargeAmount = round((($amount / $rate) * $billingChargeRate) - ($amount / $rate), 2); $bankin_amount = round($subtotal + $taxAmount + $billingChargeAmount , 2); return response()->json([ @@ -630,6 +658,8 @@ class BookingController extends Controller 'taxAmount' => $taxAmount, 'billingChargeAmount' => $billingChargeAmount, 'bankin_amount' => $bankin_amount, + 'acc_name' => $request->input('acc_name'), + 'acc_no' => $request->input('acc_no') ], 200); } @@ -660,11 +690,26 @@ class BookingController extends Controller // update booking status $booking->status = 4; - if($booking->term !== "x1_ba" || $booking->term !== "x2_cheque") + if($booking->term !== "x1_ba" || $booking->term !== "x2_cheque"){ $booking->admin_status = 3; - else + // Add notification message to user + $userNotification = new Notification; + $userNotification->detail = "Your bankslip for booking " . $booking->id . " is approved."; + $userNotification->link = "/booking/" . $booking->id . "/transfer"; + $userNotification->user_id = $booking->user_id; + $userNotification->save(); + } + else{ $booking->admin_status = 5; + // Add notification message to user + $userNotification = new Notification; + $userNotification->detail = "Your bankslip for booking " . $booking->id . " is approved."; + $userNotification->link = "/booking/" . $booking->id . "/supplier"; + $userNotification->user_id = $booking->user_id; + $userNotification->save(); + } $booking->save(); + return response()->json(['message'=>'Success'],200); } @@ -760,6 +805,11 @@ class BookingController extends Controller $booking->admin_status = 5; // upload china bankslip if($booking->save()){ + $userNotification = new Notification; + $userNotification->detail = "Supplier booking report for booking " . $booking->id . " is completed."; + $userNotification->link = "/booking/" . $booking->id . "/transfer"; + $userNotification->user_id = $booking->user_id; + $userNotification->save(); return response()->json(['message'=>"Success"],200); } } @@ -768,16 +818,21 @@ class BookingController extends Controller $booking = Booking::where('user_id',Auth::user()->id)->where('id',$id)->first(); $purchase_order = $booking->purchaseOrder()->first(); - return($purchase_order); if(!$purchase_order){ return response()->json(["message"=> "Please upload your purchase order"],400); } - $booking->status = 6; // wait invoice $booking->admin_status = 6; // upload invoice $booking->save(); - return response()->json(['message'=>'Success'],200); + + $adminNotification = new Notification; + $adminNotification->detail = "Purchase Order for booking " . $booking->id . " is uploaded."; + $adminNotification->link = "/booking/" . $booking->id . "/upload-invoice"; + $adminNotification->user_id = User::withRole('admin')->first()->id; + $adminNotification->save(); + + return response()->json($purchase_order,200); } public function confirmInvoice(Request $request, $id) @@ -794,6 +849,11 @@ class BookingController extends Controller $booking->admin_status = 7; // completed if($booking->save()){ + $adminNotification = new Notification; + $adminNotification->detail = "Invoice for booking " . $booking->id . " is uploaded."; + $adminNotification->link = "/booking/" . $booking->id . "/complete"; + $adminNotification->user_id = $booking->user_id; + $adminNotification->save(); return response()->json(['message'=>"Success"],200); } } diff --git a/app/Http/Controllers/ChinaBankSlipController.php b/app/Http/Controllers/ChinaBankSlipController.php index eabfe4a5..58aad3a0 100644 --- a/app/Http/Controllers/ChinaBankSlipController.php +++ b/app/Http/Controllers/ChinaBankSlipController.php @@ -4,6 +4,8 @@ namespace App\Http\Controllers; use App\ChinaBankSlip; use App\Booking; +use App\User; +use App\Notification; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; use Validator; @@ -80,6 +82,13 @@ class ChinaBankSlipController extends Controller $booking->admin_status = 6; $booking->save(); + $userNotification = new Notification; + $userNotification->detail = "Your china bankslip for booking " . $booking->id . " is uploaded."; + $userNotification->link = "/booking/" . $booking->id . "/upload-po"; + $userNotification->user_id = $booking->user_id; + $userNotification->save(); + + return response()->json(["message"=> "Success"],200); } diff --git a/app/Http/Controllers/NotificationController.php b/app/Http/Controllers/NotificationController.php new file mode 100644 index 00000000..00c8e722 --- /dev/null +++ b/app/Http/Controllers/NotificationController.php @@ -0,0 +1,65 @@ +orderby('updated_at','desc'); + } + + public function show(Notification $notification) + { + return $notification; + } + + public function store(Request $request) + { + $notification = Notification::create($request->all()); + + return response()->json($notification, 201); + } + + public function update(Request $request, Notification $notification) + { + $notification->update($request->all()); + + return response()->json($notification, 200); + } + + public function delete(Notification $notification) + { + $notification->delete(); + + return response()->json(null, 204); + } + + public function getUserMessage() + { + $user_all_notification = Notification::Where("user_id",Auth::user()->id)->orderby('updated_at','desc')->get(); + + + return response()->json([ + "message" =>$user_all_notification, + "unread_message" =>Notification::Where([ + ["user_id",Auth::user()->id], + ["is_read",0] + ])->orderby('updated_at','desc')->get() + ],200); + } + + public function readNotification($notification_id){ + $affected = DB::table('notifications') + ->Where('id', $notification_id) + ->update(array('is_read' => 1)); + return response()->json("Read message", 200); + } + + +} \ No newline at end of file diff --git a/app/Notification.php b/app/Notification.php new file mode 100644 index 00000000..0ac7488f --- /dev/null +++ b/app/Notification.php @@ -0,0 +1,77 @@ +created_at; + + date_default_timezone_set('Asia/Kuala_Lumpur'); //Change as per your default time + $str = strtotime($created_time); + $today = strtotime(date('Y-m-d H:i:s')); + + // It returns the time difference in Seconds... + $time_differnce = $today-$str; + + // To Calculate the time difference in Years... + $years = 60*60*24*365; + + // To Calculate the time difference in Months... + $months = 60*60*24*30; + + // To Calculate the time difference in Days... + $days = 60*60*24; + + // To Calculate the time difference in Hours... + $hours = 60*60; + + // To Calculate the time difference in Minutes... + $minutes = 60; + + if(intval($time_differnce/$years) > 1) + { + return intval($time_differnce/$years)." years ago"; + }else if(intval($time_differnce/$years) > 0) + { + return intval($time_differnce/$years)." year ago"; + }else if(intval($time_differnce/$months) > 1) + { + return intval($time_differnce/$months)." months ago"; + }else if(intval(($time_differnce/$months)) > 0) + { + return intval(($time_differnce/$months))." month ago"; + }else if(intval(($time_differnce/$days)) > 1) + { + return intval(($time_differnce/$days))." days ago"; + }else if (intval(($time_differnce/$days)) > 0) + { + return intval(($time_differnce/$days))." day ago"; + }else if (intval(($time_differnce/$hours)) > 1) + { + return intval(($time_differnce/$hours))." hours ago"; + }else if (intval(($time_differnce/$hours)) > 0) + { + return intval(($time_differnce/$hours))." hour ago"; + }else if (intval(($time_differnce/$minutes)) > 1) + { + return intval(($time_differnce/$minutes))." minutes ago"; + }else if (intval(($time_differnce/$minutes)) > 0) + { + return intval(($time_differnce/$minutes))." minute ago"; + }else if (intval(($time_differnce)) > 1) + { + return intval(($time_differnce))." seconds ago"; + }else + { + return "few seconds ago"; + } + } + +} diff --git a/database/migrations/2018_07_21_102920_create_notification_table.php b/database/migrations/2018_07_21_102920_create_notification_table.php new file mode 100644 index 00000000..7e3be228 --- /dev/null +++ b/database/migrations/2018_07_21_102920_create_notification_table.php @@ -0,0 +1,39 @@ +increments('id'); + $table->string('detail'); + $table->string('link'); + $table->unsignedInteger('user_id'); + $table->boolean('is_read')->default(0); + $table->timestamps(); + + $table->foreign('user_id') + ->references('id')->on('users') + ->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('notifications'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index ebc04e05..e0ba98e9 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -28,6 +28,7 @@ class DatabaseSeeder extends Seeder $this->call(SettingActiveBankSeeder::class); $this->call(SettingTaxRateSeeder::class); $this->call(SettingBillingChargeSeeder::class); + $this->call(NotificationSeeder::class); DB::statement('SET FOREIGN_KEY_CHECKS=1;'); } } diff --git a/database/seeds/NotificationSeeder.php b/database/seeds/NotificationSeeder.php new file mode 100644 index 00000000..ac238997 --- /dev/null +++ b/database/seeds/NotificationSeeder.php @@ -0,0 +1,35 @@ +first(); + $admin = User::where("email", "admin@example.com")->first(); + + DB::table('notifications')->insert([ + 'detail' => 'This is an user seeder notification', + 'user_id' => $user->id, + 'link' => 'http://www.google.com', + 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), + 'updated_at' => Carbon::now()->format('Y-m-d H:i:s'), + ]); + + DB::table('notifications')->insert([ + 'detail' => 'This is an admin seeder notification', + 'user_id' => $admin->id, + 'link' => 'http://www.google.com', + 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), + 'updated_at' => Carbon::now()->format('Y-m-d H:i:s'), + ]); + } +} diff --git a/resources/assets/js/components/Navbar.vue b/resources/assets/js/components/Navbar.vue index 9d8d5729..e01004de 100644 --- a/resources/assets/js/components/Navbar.vue +++ b/resources/assets/js/components/Navbar.vue @@ -12,30 +12,34 @@
- -