Skip to content

Commit

Permalink
fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
armanmasangkay committed Feb 21, 2022
1 parent 1585959 commit 2f0a329
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 27 deletions.
18 changes: 17 additions & 1 deletion app/Http/Controllers/ConsumerDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,32 @@

use App\Models\Transaction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ConsumerDashboardController extends Controller
{
public function dashboard()
{
$from=now()->subYear(1);
$to=now();
$transactions=Transaction::whereBetween('created_at',[$from,$to])->get();
$transactions=Transaction::whereBetween('created_at',[$from,$to])
->where('customer_id',Auth::guard("accounts")->user()->account_number)
->get();
return view("consumer-portal.dashboard",[
"transactions"=>$transactions
]);
}

public function latestBill()
{
$transaction=Transaction::where('customer_id',Auth::guard("accounts")->user()->account_number)
->orderBy('created_at','desc')
->first();


//TODO: Get the latest Bill
return view("consumer-portal.latest",[
'transaction'=>$transaction
]);
}
}
31 changes: 28 additions & 3 deletions app/Http/Controllers/ConsumerLedgerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Transaction;
use App\Models\Payments;
use App\Models\Surcharge;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
Expand Down Expand Up @@ -117,8 +118,8 @@ public function store(Request $request)
$validator = Validator::make($request->all(), [
'customer_id' => 'required',
'current_transaction_id' => 'required',
'current_month' => 'required',
'next_month' => 'required',
// 'current_month' => 'required',
// 'next_month' => 'required',
'reading_date' => 'required',
'reading_meter' => 'required|numeric|gt:0',
'consumption' => 'required|numeric',
Expand All @@ -136,13 +137,37 @@ public function store(Request $request)
return response()->json(['created' => false, 'msg' => 'Current meter reading should not be less than the previous meter reading.']);
}

$latestTransaction=Transaction::where('customer_id',$request->customer_id)->orderBy('reading_date','desc')->first();

$lastReadingDate=Carbon::parse($latestTransaction->reading_date);
$inputtedReadingDate=Carbon::parse($request->reading_date);

$isInputtedReadingDateNotOnPast=$lastReadingDate->diffInDays($inputtedReadingDate,false)>=0;

$isWithinValidRange=$lastReadingDate->addDays(28)->diffInDays($inputtedReadingDate,false)>=0 & $lastReadingDate->addDays(7)->diffInDays($inputtedReadingDate,false)<=0;

if(!$isInputtedReadingDateNotOnPast){
return response()->json(['created' => false,
'msg' => 'You must input a reading date that is similar or later than the previous reading date '. $latestTransaction->reading_date
]);
}

if(!$isWithinValidRange){
return response()->json(['created' => false,
'msg' => 'You can only add a bill between '. $lastReadingDate->subDays(7)->toDateString() . " and " . $lastReadingDate->addDays(7)->toDateString()
]);
}

$periodCoveredFrom= $lastReadingDate->subDays(35)->toFormattedDateString();
$periodCoveredFromTo= $inputtedReadingDate->toFormattedDateString();

$this->waterbill->getConnectionType($request->customer_id);
$this->waterbill->getCurrentBalance($request->customer_id);
$this->waterbill->computeBillConsumption($request->reading_meter);

$fillable=[
'customer_id' => $this->waterbill->balance != null ? $this->waterbill->balance->customer_id : $request->customer_id,
'period_covered' => $request->current_month.'-'.$request->next_month,
'period_covered' => $periodCoveredFrom.'-'.$periodCoveredFromTo,
'reading_date' => date('Y-m-d', strtotime($request->reading_date)),
'reading_meter' => $request->reading_meter,
'reading_consumption' => $this->waterbill->computed_total['meter_consumption'],
Expand Down
85 changes: 85 additions & 0 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

use App\Models\Customer;
use App\Models\Service;
use App\Models\Surcharge;
use App\Models\Transaction;
use App\Models\User;
use App\Models\WaterRate;
use Carbon\Carbon;
use Illuminate\Http\Request;

class DashboardController extends Controller
Expand All @@ -19,8 +23,89 @@ private function constructArray($title, $count, $url, $icon)
];
}


/*
This will look for customers that exceeds 35 days from their previous reading date
and automatically create a bill for them
*/

private function autoGenerateBill()
{
$activeCustomers=Customer::where('connection_status',Customer::ACTIVE)->get();

foreach($activeCustomers as $customer){


$lastTransaction=$customer->getLatestTransaction();

$numOfTransactionsToCreate=ceil(Carbon::parse($lastTransaction->reading_date)->diffInDays(now())/35)+1;

for($i=0;$i<$numOfTransactionsToCreate;$i++){
$lastTransaction=$customer->getLatestTransaction();
// dd($lastTransaction->reading_date);

if(!is_null($lastTransaction)){

$lastReadingDate= Carbon::parse($lastTransaction->reading_date);

$isPast35Days=$lastReadingDate->diffInDays(now())>35;
$residentialAndInstitutionalRate=WaterRate::where("type","Residential")->first()->min_rate;
$commercialRate=WaterRate::where("type","Commercial")->first()->min_rate;
$billingAmount=$lastTransaction->customer->connection_type==Customer::CT_RESIDENTIAL_INSTITUTIONAL?$residentialAndInstitutionalRate:$commercialRate;
$surchargeRate=Surcharge::all()->first()->rate;

if($isPast35Days){

$periodCoveredFrom=Carbon::parse($lastTransaction->reading_date);
$periodCoveredFromStr=Carbon::parse($lastTransaction->reading_date)->toFormattedDateString();
$periodCoveredTo=$periodCoveredFrom->addDays(35);
$periodCoveredToStr=$periodCoveredTo->toFormattedDateString();
$periodCovered=$periodCoveredFromStr . "-" . $periodCoveredToStr;
$readingDate=$periodCoveredTo->toDateString();
$meterReading=$lastTransaction->reading_meter;

$newSurchage=$lastTransaction->billing_amount*$surchargeRate;
$lastTransaction->billing_surcharge=$newSurchage;
$lastTransaction->billing_total=$lastTransaction->billing_total+$newSurchage;
$lastTransaction->balance=$lastTransaction->balance+$newSurchage;
$lastTransaction->update();

Transaction::create([
'customer_id'=>$customer->account_number,
'period_covered'=>$periodCovered,
'reading_date'=>$readingDate,
'reading_meter'=>$meterReading,
'reading_consumption'=>0,
'billing_amount'=>$billingAmount,
'billing_surcharge'=>0,
'billing_meter_ips'=>$lastTransaction->billing_meter_ips,
'billing_total'=>$billingAmount,
'payment_or_no',
'payment_date',
'payment_amount',
'balance'=>$lastTransaction->balance+$billingAmount,
'user_id',
'posted_by'
]);
}

}




}


}

}


public function index()
{
$this->autoGenerateBill();

$customerCount = Customer::count();

$userCount = User::count();
Expand Down
10 changes: 9 additions & 1 deletion app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Customer extends Model

public const ACTIVE = "active";
public const INACTIVE = "inactive";
const CT_RESIDENTIAL_INSTITUTIONAL="residential";
const CT_COMMERCIAL="commercial";

public function isOrgAccount()
{
Expand Down Expand Up @@ -100,7 +102,13 @@ public function hasActiveConnection()
return $this->connection_status==="active";
}

public static function count(){
public static function count()
{
return self::all()->count();
}

public function getLatestTransaction()
{
return $this->transactions()->orderBy('reading_date','desc')->first();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function up()
$table->double('billing_surcharge')->nullable();
$table->double('billing_meter_ips')->nullable()->default('0.00');
$table->double('billing_total')->nullable();
$table->foreignId('posted_by')->references('id')->on('users')->onDelete('cascade');
$table->foreignId('posted_by')->nullable()->references('id')->on('users')->onDelete('cascade');
$table->string('payment_or_no')->nullable();
$table->date('payment_date')->nullable();
$table->double('payment_amount')->nullable();
$table->double('balance');
$table->foreignId('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreignId('user_id')->nullable()->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
$table->foreign('customer_id')->references('account_number')->on('customers')->onDelete('cascade');
});
Expand Down
1 change: 0 additions & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function run()
if(env('APP_DEBUG') == true){
$this->call([
UserSeeder::class,
CustomerSeeder::class,
WaterRateSeeder::class,
SurchargeSeeder::class,
ServicesListSeeder::class,
Expand Down
6 changes: 4 additions & 2 deletions public/assets/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@
}

.text-muted, .text-secondary{
color: #6c757d !important;
color: #1b1b1b !important;
}
.text-gray{
color: #6c757d;
}

.navbar ul li a:active{
color: #3f3f3f !important;
background-color: transparent !important;
Expand Down
4 changes: 3 additions & 1 deletion resources/views/consumer-portal/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
<td>{{$transaction->getBillingTotalFormatted()}}</td>
<td>{{$transaction->getOutstandingBalanceFormatted()}}</td>
<td>{{$transaction->getNameOfBillCreator()}}</td>
<!-- FIXME: Retrieve payment details -->
<td>{{$transaction->payment_or_no}}</td>
<td>{{$transaction->payment_date}}</td>
<td>{{$transaction->payment_amount}}</td>
<!-- FIXME:: Retrieve the name of the person who process the payment -->
<td>{{$transaction->getNameOfBillCreator()}}</td>
//TODO:: Continue working with the data. Make sure that it shows the oldest first to the newest


</tr>
@empty
Expand Down
8 changes: 8 additions & 0 deletions resources/views/consumer-portal/latest.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@extends('layout.main-alt')

@section('title','Latest Bill')

@section('content')


@endsection
2 changes: 1 addition & 1 deletion resources/views/consumer-portal/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
No account yet? <a href="{{route('consumer.signup.index')}}">Sign up</a>
</small>
</div>
<div class="card-footer text-muted">
<div class="card-footer text-gray">
<small>Don't share your password to anyone</small>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/consumer-portal/signup.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<div class="card p-md-2 shadow-sm">
<div class="card-body">
<p class="mb-3">
<small class="text-muted">Fields with <span class="text-danger">*</span> are required</small>
<small class="text-gray">Fields with <span class="text-danger">*</span> are required</small>
</p>
<form method="POST" action="{{route('consumer.signup.store')}}" enctype="multipart/form-data">
@csrf
Expand Down
31 changes: 22 additions & 9 deletions resources/views/layout/main-alt.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,34 @@

</head>
<body class="bg-light bg-gradient">

<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container-fluid">
<a class="navbar-brand" href="{{route('consumer.dashboard')}}">Consumer Portal</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse d-lg-flex flex-row justify-content-end" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="{{route('consumer.signout')}}">
<i class="fas fa-sign-out-alt"></i> Sign out
</a>
</li>

<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav d-lg-flex flex-col justify-content-between" style="width:100%">
<div class="d-lg-flex flex-row">
<li class="nav-item">
<a class="nav-link {{ request()->routeIs('consumer.dashboard')?"active":"" }}" aria-current="page" href="{{route('consumer.dashboard')}}">
<i class="fas fa-home"></i> Home
</a>
</li>
<li class="nav-item">
<a class="nav-link {{ request()->routeIs('consumer.latest')?"active":"" }}" aria-current="page" href="{{route('consumer.latest')}}">
<i class="far fa-eye"></i> View Latest Bill
</a>
</li>
</div>
<div>
<li class="nav-item">
<a class="nav-link" aria-current="page" href="{{route('consumer.signout')}}">
<i class="fas fa-sign-out-alt"></i> Sign out
</a>
</li>
</div>

</ul>
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions resources/views/templates/newBillingModal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</div>
</div>
<div class='row px-md-2 mb-2 mt-2'>
<div class='col-6 col-md-3 col-lg-2 col-xl-1 mt-2 pe-md-1 ps-md-1 ps-lg-1 pe-sm-1 pe-1'>
{{-- <div class='col-6 col-md-3 col-lg-2 col-xl-1 mt-2 pe-md-1 ps-md-1 ps-lg-1 pe-sm-1 pe-1'>
<small class='text-primary'>{{ isset($last_date) ? \Carbon\Carbon::parse($last_date)->format('M, Y') : '' }}</small>
<select name='current_month' id='current-month' class='form-select border-0' disabled style="font-weight: bold; font-size: 20px;">
@if(isset($customer))
Expand All @@ -68,7 +68,7 @@
@endfor
@endif
</select>
</div>
</div> --}}

<div class='col-6 col-md-6 col-lg-4 col-xl-2 mt-2 mt-lg-2 mt-md-2 px-lg-1 pe-sm-1 ps-md-1 pe-1'>
<small class='text-muted'>Meter Reading <span class="text-danger"><strong>*</strong></span></small>
Expand Down Expand Up @@ -96,12 +96,12 @@
<input style="font-weight: bold; font-size: 20px;" class="form-control-plaintext ps-2" type='number' id="total" name="total" placeholder='0.00' min=0 readonly>
</div>
</div>
<div class="row mt-3">
{{-- <div class="row mt-3">
<div class="col-md-12 pe-md-0 d-flex justify-content-start align-items-center">
<input name="override" id="override" type="checkbox">
<label class='text-muted ms-2'>Allow override period covered date ?</label>
</div>
</div>
</div> --}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><i data-feather="x"></i> Close</button>
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,5 @@
Route::get('/consumer/signout',[ConsumerAuthController::class,'signout'])->name('consumer.signout');

// -Dashboard
Route::get('/consumer/dashboard',[ConsumerDashboardController::class,'dashboard'])->name('consumer.dashboard')->middleware('auth.account');
Route::get('/consumer/dashboard',[ConsumerDashboardController::class,'dashboard'])->name('consumer.dashboard')->middleware('auth.account');
Route::get('/consumer/latest',[ConsumerDashboardController::class,'latestBill'])->name('consumer.latest')->middleware('auth.account');

0 comments on commit 2f0a329

Please sign in to comment.