Service cancellation and ordering

This commit is contained in:
Deon George
2021-09-29 14:57:25 +10:00
parent b2e45fcaee
commit f7439172b6
11 changed files with 637 additions and 299 deletions

View File

@@ -2,21 +2,211 @@
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
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\Mail\{CancelRequest,ChangeRequest};
use App\Models\Service;
class ServiceController extends Controller
{
/* SERVICE WORKFLOW METHODS */
/**
* Cancel a request to cancel a service
*
* @param Service $o
* @return bool
*/
private function action_cancel_cancel(Service $o): bool
{
if (! $o->order_info)
$o->order_info = collect();
$o->order_info->put('cancel_cancel',Carbon::now()->format('Y-m-d H:i:s'));
$o->order_status = 'ACTIVE';
return $o->save();
}
/**
* Cancel a request to change a service
*
* @param Service $o
* @return bool
*/
private function action_change_cancel(Service $o): bool
{
if (! $o->order_info)
$o->order_info = collect();
$o->order_info->put('change_cancel',Carbon::now()->format('Y-m-d H:i:s'));
$o->order_status = 'ACTIVE';
return $o->save();
}
/**
* Action to change a service order_status to another stage
* This is a generic function that can redirect the user to a page that is required to completed to enter
* the new stage
*
* @param Service $o
* @param string $stage
* @return \Illuminate\Contracts\Foundation\Application|RedirectResponse|\Illuminate\Routing\Redirector
*/
private function action_request_enter_redirect(Service $o,string $stage)
{
return redirect(sprintf('u/service/%d/%s',$o->id,strtolower($stage)));
}
/* OTHER METHODS */
/**
* Process a request to cancel a service
*
* @param Request $request
* @param Service $o
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|RedirectResponse|\Illuminate\Routing\Redirector
*/
public function cancel_request(Request $request,Service $o)
{
if ($request->post()) {
$request->validate([
'date_end'=>'required|date',
]);
if (! $o->order_info)
$o->order_info = collect();
$o->date_end = $request->date_end;
$o->order_info->put('cancel_note',$request->notes);
$o->order_status = 'CANCEL-REQUEST';
$o->save();
//@todo Get email from DB.
Mail::to('help@graytech.net.au')
->queue((new CancelRequest($o,$request->notes))->onQueue('email'));
return redirect('u/service/'.$o->id)->with('success','Cancellation lodged');
}
return view('u.service.cancel_request')
->with('o',$o);
}
/**
* Change the status of a service
*
* @note This route is protected by middleware @see ServicePolicy::progress()
* It is assumed that the next stage is valid for the services current stage - validated in ServicePolicy::progress()
* @param Service $o
* @param string $stage
* @return RedirectResponse
*/
public function change(Service $o,string $stage): RedirectResponse
{
// While stage has a string value, that indicates the next stage we want to go to
// If stage is NULL, the current stage hasnt been completed
// If stage is FALSE, then the current stage failed, and may optionally be directed to another stage.
while ($stage) {
// Check that stage is a valid next action for the user currently performing it
//$current = $this->getStageParameters($this->order_status);
$next = $o->getStageParameters($stage);
// If valid, call the method to confirm that the current stage is complete
if ($x=$next->get('enter_method')) {
if (! method_exists($this,$x))
abort(500,sprintf('ENTER_METHOD [%s]defined doesnt exist',$x));
Log::debug(sprintf('Running ENTER_METHOD [%s] on Service [%d] to go to stage [%s]',$x,$o->id,$stage));
// @todo Should call exit_method of the current stage first, to be sure we can change
try {
$result = $this->{$x}($o,$stage);
// If we have a form to complete, we need to return with a URL, so we can catch that with an Exception
} catch (HttpException $e) {
if ($e->getStatusCode() == 301)
return ($e->getMessage());
}
// An Error Condition
if (is_null($result))
return redirect()->to('u/service/'.$o->id);
elseif ($result instanceof RedirectResponse)
return $result;
// The service cannot enter the next stage
elseif (! $result)
abort(500,'Current Method FAILED: '.$result);
} else {
$o->order_status = $stage;
$o->save();
}
// If valid, call the method to start the next stage
$stage = ''; // @todo this is temporary, we havent written the code to automatically jump to the next stage if wecan
}
return redirect()->to('u/service/'.$o->id);
}
/**
* Process a request to cancel a service
*
* @param Request $request
* @param Service $o
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|RedirectResponse|\Illuminate\Routing\Redirector
*/
public function change_request(Request $request,Service $o)
{
if ($request->post()) {
$request->validate([
'change_date'=>'required|date',
'notes'=>'required|min:10',
]);
if (! $o->order_info)
$o->order_info = collect();
$o->order_info->put('change_note',$request->notes);
$o->order_info->put('change_date',$request->change_date);
$o->order_status = 'CHANGE-REQUEST';
$o->save();
//@todo Get email from DB.
Mail::to('help@graytech.net.au')
->queue((new ChangeRequest($o,$request->notes))->onQueue('email'));
return redirect('u/service/'.$o->id)->with('success','Upgrade requested');
}
switch (get_class($o->type)) {
default:
return view('u.service.change_request')
->with('o',$o);
}
}
/**
* Edit a domain service details
*
* @param Request $request
* @param Service $o
* @return \Illuminate\Http\RedirectResponse
* @return RedirectResponse
* @todo revalidate
*/
public function domain_edit(Request $request,Service $o)
{
@@ -49,6 +239,7 @@ class ServiceController extends Controller
* List all the domains managed by the user
*
* @return View
* @todo revalidate
*/
public function domain_list(): View
{
@@ -62,95 +253,4 @@ class ServiceController extends Controller
return view('r.service.domain.list')
->with('o',$o);
}
/**
* Update a service
*
* @note: Route Middleware protects this path
* @param Request $request
* @param Service $o
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
*/
public function update(Request $request,Service $o)
{
switch ($o->order_status) {
case 'CANCEL-REQUEST':
if ($request->post()) {
if (! $request->post('date_end'))
return redirect()->back()->withErrors('Cancellation Date not provided');
$o->date_end = $request->post('date_end');
foreach (['cancel_notes'] as $key) {
if ($request->post($key))
$o->setOrderInfo($key,$request->post($key));
}
$o->order_status='CANCEL-PENDING';
$o->save();
return redirect()->to(url('u/service',$o->id))->with('updated','Service cancellation submitted.');
}
return $this->update_request_cancel($o);
case 'ORDER-SENT':
if ($request->post()) {
foreach (['reference','notes'] as $key) {
$o->setOrderInfo($key,$request->post($key));
}
$o->save();
foreach ($request->post($o->stype) as $k=>$v) {
$o->type->{$k} = $v;
}
$o->type->save();
return redirect()->to(url('u/service',$o->id))->with('updated','Order sent notes updated.');
}
return $this->update_order_status($o);
case 'PROVISION-PLANNED':
if ($request->post()) {
foreach (['provision_notes'] as $key) {
$o->setOrderInfo($key,$request->post($key));
}
$o->date_start = $request->post('date_start');
$o->save();
foreach ($request->post($o->stype) as $k=>$v) {
$o->type->{$k} = $v;
}
$o->type->save();
return redirect()->to(url('u/service',$o->id))->with('updated','Order sent notes updated.');
}
return $this->update_provision_planned($o);
default:
abort(499,'Not yet implemented');
}
}
private function update_order_status(Service $o)
{
return View('r.service.order.sent',['o'=>$o]);
}
private function update_request_cancel(Service $o)
{
return View('u.service.order.cancel',['o'=>$o]);
}
private function update_provision_planned(Service $o)
{
return View('r.service.order.provision_plan',['o'=>$o]);
}
}