Enabled changing broadband services and adjusting invoices

This commit is contained in:
Deon George 2022-06-12 18:32:54 +10:00
parent c8162b8eb9
commit c1080481ec
8 changed files with 379 additions and 5 deletions

View File

@ -6,14 +6,16 @@ use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\View\View;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Http\Requests\ServiceChangeRequest;
use App\Mail\{CancelRequest,ChangeRequest};
use App\Models\Service;
use App\Models\{Charge,Product,Service};
class ServiceController extends Controller
{
@ -69,6 +71,23 @@ class ServiceController extends Controller
/* OTHER METHODS */
public function change_pending(ServiceChangeRequest $request,Service $o)
{
if ($request->post()) {
foreach ($this->service_change_charges($request,$o) as $co)
$co->save();
$o->product_id = Arr::get($request->broadband,'product_id');
$o->order_status = 'ACTIVE';
$o->save();
return redirect()->to(url('u/service',[$o->id]));
}
return view('a.service.change_pending')
->with('o',$o);
}
/**
* Process a request to cancel a service
*
@ -283,6 +302,87 @@ class ServiceController extends Controller
->with('o',$o);
}
private function service_change_charges(Request $request,Service $o): Collection
{
$charges = collect();
$po = Product::findOrFail(Arr::get($request->broadband,'product_id'));
$start_at = Carbon::create(Arr::get($request->broadband,'start_at'));
// Get the invoiced items covering the start_at date
foreach ($o->invoice_items->filter(function($item) use ($start_at) {
return ($item->start_at < $start_at) && ($item->stop_at > $start_at) && ($item->item_type === 0);
}) as $iio)
{
// Reverse the original charge
$co = new Charge;
$co->active = TRUE;
$co->service_id = $o->id;
$co->account_id = $o->account_id;
$co->sweep_type = 6;
$co->product_id = $iio->product_id;
$co->description = 'Plan Upgrade Adjustment';
$co->user_id = Auth::id();
$co->type = $iio->item_type;
$co->start_at = $start_at;
$co->stop_at = $iio->stop_at;
$co->amount = $iio->price_base;
$co->taxable = TRUE; // @todo this should be determined
$co->quantity = -1*$start_at->diff($iio->stop_at)->days/$iio->start_at->diff($iio->stop_at)->days;
$charges->push($co);
// Add the new charge
$co = new Charge;
$co->active = TRUE;
$co->service_id = $o->id;
$co->account_id = $o->account_id;
$co->sweep_type = 6;
$co->product_id = $po->id;
$co->description = 'Plan Upgrade Adjustment';
$co->user_id = Auth::id();
$co->type = $iio->item_type;
$co->start_at = $start_at;
$co->stop_at = $iio->stop_at;
$co->amount = $po->base_charge;
$co->taxable = TRUE; // @todo this should be determined
$co->quantity = $start_at->diff($iio->stop_at)->days/$iio->start_at->diff($iio->stop_at)->days;
$charges->push($co);
}
// Add any fee
if (Arr::get($request->broadband,'change_fee')) {
$co = new Charge;
$co->active = TRUE;
$co->service_id = $o->id;
$co->account_id = $o->account_id;
$co->sweep_type = 6;
$co->product_id = $po->id;
$co->description = 'Plan Upgrade Fee';
$co->user_id = Auth::id();
$co->type = 3;
$co->start_at = $start_at;
$co->stop_at = $start_at;
$co->amount = Arr::get($request->broadband,'change_fee');
$co->taxable = TRUE; // @todo this should be determined
$co->quantity = 1;
$charges->push($co);
}
return $charges;
}
/**
* This is an API method, that works with service change - to return the new charges as a result of changing a service
*
* @param Request $request
* @param Service $o
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function service_change_charges_display(Request $request,Service $o)
{
return view('a.charge.service_change')
->with('charges',$this->service_change_charges($request,$o));
}
/**
* Update details about a service
*

View File

@ -0,0 +1,38 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ServiceChangeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->route('o')->serviceUserAuthorised(Auth::user());
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
* @todo This is specific to broadband - this needs to be more generic.
*/
public function rules(Request $request)
{
if (! $request->isMethod('post'))
return [];
return [
'broadband.product_id' => 'required|exists:products,id',
'broadband.change_fee' => 'nullable|numeric',
'broadband.start_at' => 'required|date', // @todo Check that it is not more than 1 billing cycle ago, and not future.
];
}
}

View File

@ -12,6 +12,7 @@ use App\Traits\SiteID;
* + Charge Date should not be null
* + Attributes should be a collection array
* + type should not be null
* + It would be useful, given an array of Charges to call a function that renders them into invoice format. This may provide consistence and be the single view of how charges do look on an invoice.
*/
class Charge extends Model
{
@ -22,7 +23,9 @@ class Charge extends Model
];
protected $dates = [
'charged_at',
'start_at',
'stop_at',
'charged_at', // @todo Dont remember what this attribute is for
];
public const sweep = [
@ -75,6 +78,6 @@ class Charge extends Model
public function getTypeNameAttribute(): string
{
return Arr::get(InvoiceItem::type,$this->attribute('type'));
return Arr::get(InvoiceItem::type,$this->type);
}
}

View File

@ -30,7 +30,9 @@ class InvoiceItem extends Model
// Array of items that can be updated with PushNew
protected $pushable = ['taxes'];
// @todo Change these to CONSTS so it's easier to reference through out the code
public const type = [
0 => 'Service Charge',
1 => 'Hardware', // *
2 => 'Service Relocation Fee', // * Must have corresponding SERVICE_ID
3 => 'Service Change', // * Must have corresponding SERVICE_ID

View File

@ -31,6 +31,7 @@ use App\Traits\SiteID;
* + billing_charge : Charge for this service each invoice period // @todo change to "charge"
* + billing_interval : The period that this service is billed for by default
* + billing_interval_string : The period that this service is billed for by default as a name
* + billed_to : When this service has been billed to // @todo rename all references to invoice_to
* + category : The type of service this is, eg: broadband, phone
* + contract_term : The term that this service must be active
* + contract_end : The date that the contract ends for this service
@ -100,6 +101,7 @@ class Service extends Model implements IDs
* to see if it can proceed further if not, it'll wait here for user/admin intervention
*
* @var array
* @todo This needs an overhaul, its not implemented correctly.
*/
private const ACTION_PROGRESS = [
// Order Submitted @todo redo
@ -256,6 +258,14 @@ class Service extends Model implements IDs
'enter_method'=>'action_request_enter_redirect',
'title'=>'Change Service',
],
'CHANGE-PENDING' => [
'next'=>[
'ACTIVE'=>['wholesaler'],
],
'enter_method'=>'action_request_enter_redirect',
'title'=>'Activate Change',
],
];
/* INTERFACES */
@ -1221,8 +1231,8 @@ class Service extends Model implements IDs
$o->quantity = $oo->quantity;
$o->item_type = $oo->type;
$o->price_base = $oo->amount;
$o->start_at = $oo->date_charge;
$o->stop_at = $oo->date_charge;
$o->start_at = $oo->start_at;
$o->stop_at = $oo->stop_at;
$o->module_id = 30; // @todo This shouldnt be hard coded
$o->module_ref = $oo->id;
$o->site_id = 1; // @todo

View File

@ -0,0 +1,33 @@
<div class="row">
<div class="col-12">
<div class="card card-info">
<div class="card-header">
<h3 class="card-title">Pending Transactions</h3>
</div>
<div class="card-body">
<table class="table table-sm">
<thead>
<tr>
<th>Service Charge</th>
<th class="text-right">Quantity</th>
<th class="text-right">Rate</th>
<th class="text-right">Amount</th>
</tr>
</thead>
<tbody>
@foreach ($charges as $co)
<tr>
<td>{{ $co->type_name }}</td>
<td class="text-right">{{ number_format($co->quantity,2) }}</td>
<td class="text-right">${{ number_format($co->amount,2) }}</td>
<td class="text-right">${{ number_format($co->amount*$co->quantity,2) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,180 @@
@extends('adminlte::layouts.app')
@section('htmlheader_title')
Change Service #{{ $o->id }}
@endsection
@section('contentheader_title')
Change Service #{{ $o->id }} - WARNING - this is only for Broadband for now
@endsection
@section('contentheader_description')
{{ $o->sid }}
@endsection
<!-- $o = App\Models\Service::class -->
@section('main-content')
<div class="row">
<div class="col-6">
<form role="form" method="POST" enctype="multipart/form-data">
<div class="card card-dark">
{{ csrf_field() }}
<div class="card-header">
<h3 class="card-title">Service Information</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-3">
<!-- SERVICE NUMBER -->
<div class="form-group">
<label for="service_number" class="col-form-label text-right">Service Number</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-phone"></i></span>
</div>
<input type="text" class="form-control" name="broadband[service_number]" value="{{ $o->name_short ?? '' }}" disabled>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-3">
<!-- PRODUCT -->
<div class="form-group has-validation">
<label for="product_id">Type</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-fw fa-hashtag"></i></span>
</div>
<select class="form-control @error('broadband.product_id') is-invalid @enderror" id="product_id" name="broadband[product_id]" required>
<!-- @todo TO DO LIMIT THIS TO OF THE SAME OFFERING TYPE AND SORT BY NAME -->
@foreach (\App\Models\Product::get() as $po)
@if ($po->category !== $o->product->category) @continue @endif
<option value="{{ $po->id }}" {{ $po->id == old('broadband.product_id',$po->exists ? \Illuminate\Support\Arr::get($o->order_info,'change_product_id') : NULL) ? 'selected' : '' }}>{{ $po->name }}</option>
@endforeach
</select>
<span class="invalid-feedback" role="alert">
@error('broadband.product_id')
{{ $message }}
@else
Type is required.
@enderror
</span>
</div>
<span class="input-helper">Product - {{ $o->product->category_name }}.</span>
</div>
</div>
</div>
<div class="row">
<div class="col-3">
<!-- CHANGE DATE -->
<div class="form-group has-validation">
<label for="start_at" class="col-form-label text-right">Change Date</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
</div>
<input type="date" class="form-control @error('broadband.start_at') is-invalid @enderror" id="start_at" name="broadband[start_at]" value="{{ \Illuminate\Support\Arr::get($o->order_info,'change_date') ?? '' }}" required>
<span class="invalid-feedback" role="alert">
@error('broadband.start_at')
{{ $message }}
@else
Type is required.
@enderror
</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-3">
<!-- CHANGE FEE -->
<div class="form-group has-validation">
<label for="change_fee" class="col-form-label text-right">Change Fee</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-dollar-sign"></i></span>
</div>
<input type="text" class="form-control @error('broadband.change_fee') is-invalid @enderror" id="change_fee" name="broadband[change_fee]" value="0">
<span class="invalid-feedback" role="alert">
@error('broadband.change_fee')
{{ $message }}
@enderror
</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<a href="{{ url('/home') }}" class="btn btn-danger">Cancel</a>
@can('wholesaler')
<button type="submit" name="submit" class="btn btn-success mr-0 float-right">@if ($site->exists)Save @else Add @endif</button>
@endcan
</div>
</div>
</div>
</div>
</form>
</div>
<div class="col-6">
@include('a.service.widgets.internal')
</div>
</div>
<div id="transactions"></div>
@endsection
@section('page-scripts')
@css(select2)
@js(select2,autofocus)
<script type="text/javascript">
function pendingtrans() {
var pid = $('#product_id').val();
var start = $('#start_at').val();
var fee = $('#change_fee').val();
$("div[id=transactions]").empty();
$.ajax({
type: 'POST',
dataType: 'html',
data: {broadband: {product_id: pid,start_at: start,change_fee: fee}},
cache: false,
url: '{{ url('r/service_change_charges',[$o->id]) }}',
timeout: 2000,
error: function(x) {
spinner.toggleClass('d-none').toggleClass('fa-spin');
alert('Failed to submit');
},
success: function(data) {
$("div[id=transactions]").append(data);
}
});
}
$(document).ready(function() {
$('#product_id')
.select2()
.on('change',function() {
pendingtrans();
});
$('#start_at').on('change',function() {
pendingtrans();
});
$('#change_fee').on('change',function() {
pendingtrans();
});
pendingtrans();
});
</script>
@append

View File

@ -87,6 +87,10 @@ Route::group(['middleware'=>['theme:adminlte-be','auth','role:reseller'],'prefix
// Charges on an account
Route::get('charges/{o}',[AdminController::class,'charge_pending_account'])
->where('o','[0-9]+');
// Reseller API calls
Route::post('service_change_charges/{o}',[ServiceController::class,'service_change_charges_display'])
->where('o','[0-9]+');
});
// Our User Routes
@ -118,6 +122,10 @@ Route::group(['middleware'=>['theme:adminlte-be','auth'],'prefix'=>'u'],function
Route::match(['get','post'],'service/{o}/change-request',[ServiceController::class,'change_request'])
->where('o','[0-9]+')
->middleware('can:progress,o,"change-request"');
// @todo This shouldnt be a user privilege.
Route::match(['get','post'],'service/{o}/change-pending',[ServiceController::class,'change_pending'])
->where('o','[0-9]+')
->middleware('can:progress,o,"change-pending"');
Route::get('service/{o}/change/{status}',[ServiceController::class,'change'])
->where('o','[0-9]+')
->middleware('can:progress,o,status');