From 4661cca13675fdfaf974417507824ca600548891 Mon Sep 17 00:00:00 2001 From: Toby Batch Date: Thu, 24 Oct 2024 21:51:19 +0100 Subject: [PATCH 1/5] skunk: moves familes to anew centre --- app/Console/Commands/ArcSkunk.php | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 app/Console/Commands/ArcSkunk.php diff --git a/app/Console/Commands/ArcSkunk.php b/app/Console/Commands/ArcSkunk.php new file mode 100644 index 00000000..28aeef1f --- /dev/null +++ b/app/Console/Commands/ArcSkunk.php @@ -0,0 +1,57 @@ +first(); + $reg_associated_with_fam = $c + ->registrations() + ->withFullFamily() + ->get() + ->filter(function ($f) { + return in_array($f->family->centre_sequence, [4, 42, 44, 46, 39, 40, 41, 45]); + }); + $bundles = $reg_associated_with_fam->first()->bundles; + // "change disbursing_centre_id to new centre id where it is not null"; + foreach ($bundles as $bundle) { + if ($bundle->disbursing_centre_id == null) { + $bundle->disbursing_centre_id = $teviot_btg_id; + } + } + // "then there is a method to lock the family to a centre, on the family object, pass in a centre object, true is 2nd param to force"; + $centre = Centre::find($teviot_btg_id); + foreach ($reg_associated_with_fam as $reg) { + $reg->family->lockToCentre($centre, true); + } + return CommandAlias::SUCCESS; + } +} From 60e44cd57d50de5d673ef47e9f275dd40602fef4 Mon Sep 17 00:00:00 2001 From: Toby Batch Date: Thu, 31 Oct 2024 11:24:47 +0000 Subject: [PATCH 2/5] chore: debugging family move --- app/Console/Commands/ArcSkunk.php | 43 +- app/Console/Commands/MoveFamilies.php | 91 ++ .../Store/RegistrationController.php | 1314 +++++++++-------- 3 files changed, 775 insertions(+), 673 deletions(-) create mode 100644 app/Console/Commands/MoveFamilies.php diff --git a/app/Console/Commands/ArcSkunk.php b/app/Console/Commands/ArcSkunk.php index 28aeef1f..3cd36718 100644 --- a/app/Console/Commands/ArcSkunk.php +++ b/app/Console/Commands/ArcSkunk.php @@ -3,9 +3,12 @@ namespace App\Console\Commands; use App\Centre; +use App\Family; +use App\Voucher; use Illuminate\Console\Command; use Symfony\Component\Console\Command\Command as CommandAlias; +// docker compose -f .docker/docker-compose.yml exec service /opt/project/artisan arc:skunk class ArcSkunk extends Command { /** @@ -27,31 +30,27 @@ class ArcSkunk extends Command * * @return int */ - public function handle() + public function handle(): int { - $teviot_centre_id = 101; - $teviot_btg_id = 113; + $this->findVoucher(); + return CommandAlias::SUCCESS; + } - $c = Centre::where("id", $teviot_centre_id)->first(); - $reg_associated_with_fam = $c - ->registrations() - ->withFullFamily() - ->get() - ->filter(function ($f) { - return in_array($f->family->centre_sequence, [4, 42, 44, 46, 39, 40, 41, 45]); - }); - $bundles = $reg_associated_with_fam->first()->bundles; - // "change disbursing_centre_id to new centre id where it is not null"; - foreach ($bundles as $bundle) { - if ($bundle->disbursing_centre_id == null) { - $bundle->disbursing_centre_id = $teviot_btg_id; + public function findVoucher() + { + for ($i = 1109247; $i < 1109326; $i++) { + $v =Voucher::where("code", "SK" . $i)->first(); + if (!$v) { + $this->warn("Voucher SK" . $i . " not found"); + continue; + } + if ($v->reimbursedOn) { + $this->info("Voucher SK" . $i . " Reimbursed on " . $v->reimbursedOn->updated_at); + } else if ($v->paymentPendedOn) { + $this->info("Voucher SK" . $i . " Payment pending"); + } else { + $this->info("Voucher SK" . $i . " still live"); } } - // "then there is a method to lock the family to a centre, on the family object, pass in a centre object, true is 2nd param to force"; - $centre = Centre::find($teviot_btg_id); - foreach ($reg_associated_with_fam as $reg) { - $reg->family->lockToCentre($centre, true); - } - return CommandAlias::SUCCESS; } } diff --git a/app/Console/Commands/MoveFamilies.php b/app/Console/Commands/MoveFamilies.php new file mode 100644 index 00000000..ff03f96d --- /dev/null +++ b/app/Console/Commands/MoveFamilies.php @@ -0,0 +1,91 @@ +fixFamalams(); + return CommandAlias::SUCCESS; + } + + public function fixFamalams() + { + // TODO: If we get another request like this we should abstract this to a command that takes these as args + $from_centre_id = $this->argument('from_centre'); // 101; + $to_centre_id = $this->argument('to_centre'); // 113; + $family_list = explode(",", $this->argument('family_ids')); + + $this->info(sprintf("Moving families %s for centre %d to %d", implode(",", $family_list), $from_centre_id, $to_centre_id)); + + $from_centre = Centre::where("id", $from_centre_id)->first(); + $to_centre = Centre::find($to_centre_id); + $this->info(sprintf("From centre: %s, to centre: %s", $from_centre->name, $to_centre->name)); + + $reg_associated_with_fam = $from_centre + ->registrations() + ->withFullFamily() + ->get() + ->filter(function ($f) use ($family_list) { + return in_array($f->family->centre_sequence, $family_list); + }); + + foreach ($reg_associated_with_fam as $singleReg) { + /* @var Registration $singleReg */ + $this->info(sprintf( + "Registration: id='%s', family id=%s, centre id=%s, bundle count=%d", + $singleReg->id, + $singleReg->family->id, + $singleReg->centre->id, + count($singleReg->bundles), + )); + $bundles = $singleReg->bundles; + // "change disbursing_centre_id to new centre id where it is not null"; + foreach ($bundles as $bundle) { + $this->info(sprintf( + " Bundle: id=%s, distributing centre id=%s", + $bundle->id, + $bundle->disbursing_centre_id, + )); + if ($bundle->disbursing_centre_id != null && $bundle->disbursing_centre_id != $from_centre_id) { + $bundle->disbursing_centre_id = $to_centre_id; + $this->info(sprintf(" Updating bundle->disbursing_centre_id = %s", $to_centre_id)); + $bundle->save(); + } + } + $this->info(sprintf(" Locking family %s to centre %s", $singleReg->family->id, $to_centre->id)); + $singleReg->family->lockToCentre($to_centre, true); + $singleReg->family->save(); + $_family = Family::where("id", $singleReg->family->id)->first(); + \Log::info($_family); + } + } +} diff --git a/app/Http/Controllers/Store/RegistrationController.php b/app/Http/Controllers/Store/RegistrationController.php index ab3bef20..91935100 100644 --- a/app/Http/Controllers/Store/RegistrationController.php +++ b/app/Http/Controllers/Store/RegistrationController.php @@ -30,43 +30,43 @@ class RegistrationController extends Controller { - /** - * List all the Registrations (search-ably) - * - * This is a con. It only lists the registrations available to a User's CC's Sponsor - * This means a User can see the Registrations in his 'neighbour' CCs under a Sponsor - * - * Also, the view contains the search functionality. - * - * @param Request $request - * @return Factory|View - */ - public function index(Request $request) - { - // Masthead bit - /** @var User $user */ - $user = Auth::user(); - $programme = Auth::user()->centre->sponsor->programme; - $data = [ - "user_name" => $user->name, - "centre_name" => ($user->centre) ? $user->centre->name : null, - "programme" => $programme, - ]; - $programme = Auth::user()->centre->sponsor->programme; - - // Slightly roundabout method of getting the permitted centres to poll - $neighbour_centre_ids = $user - ->relevantCentres() - ->pluck('id') - ->toArray(); - - $family_name = $request->get('family_name'); - - // Fetch the list of primary carers, the first carer in the family. - $pri_carers = Carer::select([DB::raw('MIN(id) as min_id')]) - ->groupBy('family_id') - ->pluck('min_id') - ->toArray(); + /** + * List all the Registrations (search-ably) + * + * This is a con. It only lists the registrations available to a User's CC's Sponsor + * This means a User can see the Registrations in his 'neighbour' CCs under a Sponsor + * + * Also, the view contains the search functionality. + * + * @param Request $request + * @return Factory|View + */ + public function index(Request $request) + { + // Masthead bit + /** @var User $user */ + $user = Auth::user(); + $programme = Auth::user()->centre->sponsor->programme; + $data = [ + "user_name" => $user->name, + "centre_name" => ($user->centre) ? $user->centre->name : null, + "programme" => $programme, + ]; + $programme = Auth::user()->centre->sponsor->programme; + + // Slightly roundabout method of getting the permitted centres to poll + $neighbour_centre_ids = $user + ->relevantCentres() + ->pluck('id') + ->toArray(); + + $family_name = $request->get('family_name'); + + // Fetch the list of primary carers, the first carer in the family. + $pri_carers = Carer::select([DB::raw('MIN(id) as min_id')]) + ->groupBy('family_id') + ->pluck('min_id') + ->toArray(); $fuzzy = $request->get('fuzzy'); if ($fuzzy) { @@ -75,610 +75,622 @@ public function index(Request $request) $filtered_family_ids = $this->exactSearch($family_name, $pri_carers); } - //find the registrations - $q = Registration::query(); - if (!empty($neighbour_centre_ids)) { - $q = $q->whereIn('centre_id', $neighbour_centre_ids); - } + //find the registrations + $q = Registration::query(); + if (!empty($neighbour_centre_ids)) { + $q = $q->whereIn('centre_id', $neighbour_centre_ids); + } - // only for cc users with access to more than 1 centre - if (Auth::user()->centres->count() > 1) { - // get the centre_id from the masthead dropdown which is set by session (so we can filter reg selection) - $filtered_centre_id = session('CentreUserCurrentCentreId'); + // only for cc users with access to more than 1 centre + if (Auth::user()->centres->count() > 1) { + // get the centre_id from the masthead dropdown which is set by session (so we can filter reg selection) + $filtered_centre_id = session('CentreUserCurrentCentreId'); if ($filtered_centre_id && $filtered_centre_id != "all") { $q = $q->where('centre_id', '=', $filtered_centre_id); } } - if (!empty($filtered_family_ids)) { - $q = $q->whereIn('family_id', $filtered_family_ids) - // Somehow, whereIn re-orders the filtered array into numeric order. - // this would be the "cheap" solution, IF sqlite supported FIELD so we could test that. - // ->orderByRaw(DB::raw("FIELD(family_id, " .implode(',', $filtered_family_ids). ")")); - ; - } - - // Check if the request asks us to display inactive families - $q = $request->get('families_left') ? $q : $q->WhereActiveFamily(); - - // Check if the request should filter by centre - $q = $request->get('centre') ? $q->where('centre_id', $request->get('centre')) : $q; - - // This isn't ideal as it relies on getting all the families, then sorting them. - // However, the whereIn statements above destroy any sorted order on family_ids. - $reg_models = $q->WithFullFamily() - ->get() - ->values(); - - // Get any sorting direction from the request - $direction = $request->get('direction') ? $request->get('direction') : 'asc'; - - // Sort by desc if requested to, for anything else sort by asc as usual - if ($direction === 'desc') { - $reg_models = $reg_models->sortByDesc(function ($registration) { - return strtolower($registration->family->pri_carer); - }); - } else { - $reg_models = $reg_models->sortBy(function ($registration) { - return strtolower($registration->family->pri_carer); - }); - } - - // throw it into a paginator. - $page = LengthAwarePaginator::resolveCurrentPage(); - $perPage = 10; - $offset = ($page * $perPage) - $perPage; - $registrations = new LengthAwarePaginator( - $reg_models->slice($offset, $perPage), - $reg_models->count(), - $perPage, - $page, - [ - 'path' => LengthAwarePaginator::resolveCurrentPath(), - 'query' => array_except($request->query(), 'page'), - ] - ); - - $data = array_merge( - $data, - [ - 'registrations' => $registrations, - 'programme' => $programme, + if (!empty($filtered_family_ids)) { + $q = $q->whereIn('family_id', $filtered_family_ids) + // Somehow, whereIn re-orders the filtered array into numeric order. + // this would be the "cheap" solution, IF sqlite supported FIELD so we could test that. + // ->orderByRaw(DB::raw("FIELD(family_id, " .implode(',', $filtered_family_ids). ")")); + ; + } + + // Check if the request asks us to display inactive families + $q = $request->get('families_left') ? $q : $q->WhereActiveFamily(); + + // Check if the request should filter by centre + $q = $request->get('centre') ? $q->where('centre_id', $request->get('centre')) : $q; + +// DEBUG STUFF +// http://arcv-store.test:8001/registrations?_token=HgiYN3ijYL2slb9gnatGjTcsb0l139yQ7f6nRODd&family_name=habibah&fuzzy=0 +// $_family_name = habibah +// $_fuzzy = 0 +// $filtered_family_ids [10576] +// $_families_left = null +// $_centre = null + $_rm = $q->get(); + foreach ($_rm as $r) { + \Log::info($r); + } + + // This isn't ideal as it relies on getting all the families, then sorting them. + // However, the whereIn statements above destroy any sorted order on family_ids. + $reg_models = $q->WithFullFamily() + ->get() + ->values(); + + // Get any sorting direction from the request + $direction = $request->get('direction') ? $request->get('direction') : 'asc'; + + // Sort by desc if requested to, for anything else sort by asc as usual + if ($direction === 'desc') { + $reg_models = $reg_models->sortByDesc(function ($registration) { + return strtolower($registration->family->pri_carer); + }); + } else { + $reg_models = $reg_models->sortBy(function ($registration) { + return strtolower($registration->family->pri_carer); + }); + } + + // throw it into a paginator. + $page = LengthAwarePaginator::resolveCurrentPage(); + $perPage = 10; + $offset = ($page * $perPage) - $perPage; + $registrations = new LengthAwarePaginator( + $reg_models->slice($offset, $perPage), + $reg_models->count(), + $perPage, + $page, + [ + 'path' => LengthAwarePaginator::resolveCurrentPath(), + 'query' => array_except($request->query(), 'page'), + ] + ); + + $data = array_merge( + $data, + [ + 'registrations' => $registrations, + 'programme' => $programme, 'fuzzy' => (boolean)$fuzzy, - ] - ); - return view('store.index_registration', $data); - } - - /** - * Returns the registration page - * - * @return Factory|View - */ - public function create() - { - /** @var User $user */ - $user = Auth::user(); - - // Check if we verify, based on the currently logged in user's context - // as we have no registration to refer to yet. - $sponsor = $user->centre->sponsor; - $evaluator = EvaluatorFactory::make($sponsor->evaluations); - $sponsorsRequiresID = $evaluator->isVerifyingChildren(); - $programme = Auth::user()->centre->sponsor->programme; - - $data = [ - "user_name" => $user->name, - "centre_name" => ($user->centre) ? $user->centre->name : null, - "sponsorsRequiresID" => $sponsorsRequiresID, - "programme" => $programme, - 'leaver' => false, - ]; - return view('store.create_registration', $data); - } - - /** - * Show the Registration / Family edit form - * - * @param Registration $registration - * @return Factory|View - */ - public function edit(Registration $registration) - { - // Get User and Centre; - /** @var CentreUser $user */ - $user = Auth::user(); - $data = [ - 'user_name' => $user->name, - 'centre_name' => ($user->centre) ? $user->centre->name : null, - ]; - - // Get the registration, with deep eager-loaded Family (with Children and Carers) - if ($registration) { - $registration = Registration::withFullFamily()->find($registration->id); - } else { - abort(404, 'Registration not found.'); - } - $evaluations = $registration->centre->sponsor->evaluations; - $deferrable = false; - foreach ($evaluations as $key => $evaluation) { - if ($evaluation['name'] === 'ScottishChildCanDefer') { - $deferrable = true; - } - } - $can_change_defer = true; - $this_month = Carbon::now()->month; - if ($this_month > config('arc.scottish_school_month')) { - $can_change_defer = false; - } - - // Get the valuation - /** @var Valuation $valuation */ - $valuation = $registration->getValuation(); - - // Grab carers copy for shift)ing without altering family->carers - $carers = $registration->family->carers->all(); - $pri_carer = array_shift($carers); - $pri_carer_ethnicity = $pri_carer->ethnicity; - $pri_carer_language = $pri_carer->language; - - $evaluations["creditables"] = $registration->getEvaluator()->getPurposeFilteredEvaluations("credits"); - $evaluations["disqualifiers"] = $registration->getEvaluator()->getPurposeFilteredEvaluations("disqualifiers"); - $programme = Auth::user()->centre->sponsor->programme; - - - return view('store.edit_registration', array_merge( - $data, - [ - 'registration' => $registration, - 'family' => $registration->family, - 'pri_carer' => $pri_carer, - 'pri_carer_ethnicity' => $pri_carer_ethnicity, - 'pri_carer_language' => $pri_carer_language, - 'sec_carers' => $carers, - 'children' => $registration->family->children, - 'noticeReasons' => $valuation->getNoticeReasons(), - 'entitlement' => $valuation->getEntitlement(), - 'sponsorsRequiresID' => $registration->getEvaluator()->isVerifyingChildren(), - 'evaluations' => $evaluations, - 'deferrable' => $deferrable, - 'can_change_defer' => $can_change_defer, - 'programme' => $programme, - 'leaver' => false, - ] - )); - } - - /** - * Show the Registration / Family view form for a leaver - * - * @param Registration $registration - * @return Factory|View - */ - public function view(Registration $registration) - { - // Get User and Centre; - /** @var CentreUser $user */ - $user = Auth::user(); - $data = [ - 'user_name' => $user->name, - 'centre_name' => ($user->centre) ? $user->centre->name : null, - ]; - - // Get the registration, with deep eager-loaded Family (with Children and Carers) - if ($registration) { - $registration = Registration::withFullFamily()->find($registration->id); - } else { - abort(404, 'Registration not found.'); - } - $evaluations = $registration->centre->sponsor->evaluations; - - // Get the valuation - /** @var Valuation $valuation */ - $valuation = $registration->getValuation(); - - // Grab carers copy for shift)ing without altering family->carers - $carers = $registration->family->carers->all(); - - $evaluations["creditables"] = $registration->getEvaluator()->getPurposeFilteredEvaluations("credits"); - $evaluations["disqualifiers"] = $registration->getEvaluator()->getPurposeFilteredEvaluations("disqualifiers"); - $programme = Auth::user()->centre->sponsor->programme; - - return view('store.view_registration', array_merge( - $data, - [ - 'registration' => $registration, - 'family' => $registration->family, - 'pri_carer' => array_shift($carers), - 'children' => $registration->family->children, - 'entitlement' => $valuation->getEntitlement(), - 'programme' => $programme, - 'leaver' => true, - ] - )); - } - - /** - * Displays a printable version of the Registration. - * - * @param Registration $registration - * @return Response - */ - public function printOneIndividualFamilyForm(Registration $registration) - { - // Get User - $user = Auth::user(); - - // Get the registration, with deep eager-loaded Family (with Children and Carers) - if ($registration) { - $registration = Registration::withFullFamily()->find($registration->id); - } else { - abort(404, 'Registration not found.'); - } - - // Get the valuation - /** @var Valuation $valuation */ - $valuation = $registration->getValuation(); - - // Make a filename - $filename = 'Registration' . Carbon::now()->format('YmdHis') . '.pdf'; - - // Setup common data - $data = [ - 'user_name' => $user->name, - 'centre_name' => ($user->centre) ? $user->centre->name : null, - 'sheet_title' => 'Printable Family Sheet', - 'sheet_header' => 'Family Collection Sheet', - ]; - - $data['regs'][] = [ - 'centre' => $registration->centre, - 'family' => $registration->family, - 'pri_carer' => $registration->family->pri_carer, - 'children' => $registration->family->children, - 'noticeReasons' => $valuation->getNoticeReasons(), - 'creditReasons' => $valuation->getCreditReasons(), - 'entitlement' => $valuation->getEntitlement(), - ]; - - // throw at a PDF - $pdf = PDF::loadView('store.printables.family', $data); - $pdf->setPaper('A4', 'landscape'); - return @$pdf->download($filename); - } - - /** - * Displays a printable version of the Registration. - * - * @return RedirectResponse|Response - */ - public function printBatchIndividualFamilyForms() - { - // Get the user and Centre - $user = Auth::user(); - $centre = ($user->centre) ? $user->centre : null; - - // Cope if User has no Centre. - if (!$centre) { - Log::info('User ' . $user->id . " has no Centre"); - // Send me back to dashboard - return redirect() - ->route('store.dashboard') - ->withErrors(['error_message' => 'User has no Centre']); - } - // Get the registrations this User's centre is directly responsible for - $registrations = $centre->registrations() - ->whereActiveFamily() - ->withFullFamily() - ->get() - ->sortBy(function ($registration) { - // Need strtolower because case comparison sucks. - return strtolower($registration->family->pri_carer); - }); - - if ($registrations->count() < 1) { - return redirect() - ->route('store.dashboard') - ->with('error_message', 'No Registrations in that centre.'); - } - - // Make a filename - $filename = 'Registrations_' . Carbon::now()->format('YmdHis') . '.pdf'; - - // Set up the common view data. - $data = [ - 'user_name' => $user->name, - 'centre_name' => ($user->centre) ? $user->centre->name : null, - 'sheet_title' => 'Printable Family Sheet', - 'sheet_header' => 'Family Collection Sheet', - ]; - - // Stack the registration batch into the data - foreach ($registrations as $registration) { - // Get the valuation - $valuation = $registration->getValuation(); - - $data['regs'][] = [ - 'centre' => $centre, - 'family' => $registration->family, - 'pri_carer' => $registration->family->pri_carer, - 'children' => $registration->family->children, - 'noticeReasons' => $valuation->getNoticeReasons(), - 'creditReasons' => $valuation->getCreditReasons(), - 'entitlement' => $valuation->getEntitlement(), - ]; - } - - // throw it at a PDF. - $pdf = PDF::loadView( - 'store.printables.family', - $data - ); - $pdf->setPaper('A4', 'landscape'); - return @$pdf->download($filename); - } - - /** - * Stores an incoming Registration. - * - * @param StoreNewRegistrationRequest $request - * @return RedirectResponse - * @throws Throwable $e - */ - public function store(StoreNewRegistrationRequest $request): RedirectResponse - { - // Create Carers, merge primary carer - $carers = array_map( - function ($carer) use ($request) { - return new Carer([ - 'name' => $carer, - 'ethnicity' => $request->get("pri_carer_ethnicity"), - 'language' => $request->get("pri_carer_language") - ]); - }, - array_merge( - (array)$request->get('pri_carer'), - (array)$request->get('new_carers') - ) - ); - - // Create Children - $children = $this->makeChildrenFromInput( - (array)$request->get('children') - ); - - $hsbs = $request->get('eligibility-hsbs'); - $eligible_from = null; - if ($hsbs === 'healthy-start-receiving') { - $eligible_from = Carbon::now(); - } - - // Create Registration - $registration = new Registration([ - 'consented_on' => Carbon::now(), - 'eligibility_hsbs' => $request->get('eligibility-hsbs'), - 'eligibility_nrpf' => $request->get('eligibility-nrpf'), - 'eligible_from' => $eligible_from, - ]); - - // Duplicate families are fine at this point. - $family = new Family(); - - // Set the RVID using the User's Centre. - $family->lockToCentre(Auth::user()->centre); - - // Try to transact, so we can roll it back - try { - DB::transaction(function () use ($registration, $family, $carers, $children) { - $family->save(); - $family->carers()->saveMany($carers); - $family->children()->saveMany($children); - $registration->family()->associate($family); - $registration->centre()->associate(Auth::user()->centre); - $registration->save(); - }); - } catch (Exception $e) { - // Oops! Log that - Log::error('Bad transaction for ' . __CLASS__ . '@' . __METHOD__ . ' by service user ' . Auth::id()); - Log::error($e->getTraceAsString()); - // Throw it back to the user - return redirect()->route('store.registration.create')->withErrors('Registration failed.'); - } - // Or return the success - Log::info('Registration ' . $registration->id . ' created by service user ' . Auth::id()); - // and go to the edit page for the new registration - return redirect() - ->route('store.registration.edit', ['registration' => $registration->id]) - ->with('message', 'Registration created.'); - } - - /** - * Makes children from input data - * @param array $children - * @return Child[] - */ - private function makeChildrenFromInput(array $children = []): array - { - return array_map( - function ($child) { - // Note: Carbon uses different time formats than laravel validation - // For crazy reasons known only to the creators of Carbon, when no day provided, - // createFromFormat - defaults to 31 - which bumps to next month if not a real day. - // So we want '2013-02-01' not '2013-02-31'... - $month_of_birth = Carbon::createFromFormat('Y-m-d', $child['dob'] . '-01'); - - // Check and set verified, or null - $verified = null; - if (array_key_exists('verified', $child)) { - $verified = (bool)$child['verified']; - } - - // Check and set deferred, or null - $deferred = 0; - if (array_key_exists('deferred', $child)) { - $deferred = (bool)$child['deferred']; - } - - // Check and set is_pri_carer, or null - $is_pri_carer = null; - if (array_key_exists('is_pri_carer', $child)) { - $is_pri_carer = (bool)$child['is_pri_carer']; - } - - return new Child([ - 'born' => $month_of_birth->isPast(), - 'dob' => $month_of_birth->toDateTimeString(), - 'verified' => $verified, - 'deferred' => $deferred, - 'is_pri_carer' => $is_pri_carer, - ]); - }, - $children - ); - } - - /** - * Update a Registration - * - * @param StoreUpdateRegistrationRequest $request - * @param Registration $registration - * @return RedirectResponse - */ - public function update(StoreUpdateRegistrationRequest $request, Registration $registration): RedirectResponse - { - $amendedCarers = []; - - // Fetch eligibility - $eligibility_hsbs = $request->get('eligibility-hsbs'); - $eligibility_nrpf = $request->get('eligibility-nrpf'); - $eligible_from = $registration->eligible_from; - $deferred = $request->get('deferred'); - - //Prevent the date changing if you're just editing a different field - if ($registration->eligible_from === null && $eligibility_hsbs === 'healthy-start-receiving') { - $eligible_from = Carbon::now(); - } else { - if ($eligibility_hsbs !== 'healthy-start-receiving') { - $eligible_from = null; - } - } - - // NOTE: Following refactor where we needed to retain Carer ids. - // Possible that we might want to add flag to carer to distinguish Main from Secondary, - // to simplify method below for sorting and updating carer entries. - - // Update primary carer. - $carerInput = (array)$request->get("pri_carer"); - $carerEthnicity = $request->get("pri_carer_ethnicity"); - $carerLanguage = $request->get('pri_carer_language'); - $carerKey = key($carerInput); - $carer = Carer::find($carerKey); - if ($carer->name !== $carerInput[$carer->id]) { - $carer->name = $carerInput[$carer->id]; - $amendedCarers[] = $carer; - } - if ($carerEthnicity !== null && $carerEthnicity !== $carerEthnicity[$carer->id]) { - $carer->ethnicity = $carerEthnicity[$carer->id]; - $amendedCarers[] = $carer; - } - if ($carerLanguage !== null && $carerLanguage !== $carerLanguage[$carer->id]) { - $carer->language = $carerLanguage[$carer->id]; - $amendedCarers[] = $carer; - } - - // Find secondary carers id's in the DB - $carersInput = (array)$request->get("sec_carers"); - $carersKeys = $registration->family->carers->pluck("id")->toArray(); - // remove carerKey from that; - if (($key = array_search($carerKey, $carersKeys)) !== false) { - unset($carersKeys[$key]); - } - - // Those in the DB, not in the input can be scheduled for deletion; - $carersInputKeys = array_keys($carersInput); - $carersKeysToDelete = array_diff($carersKeys, $carersInputKeys); - - // Get the secondary carers. - $carers = Carer::whereIn("id", $carersInputKeys)->get(); - - // roll though those and amend them if necessary. - foreach ($carers as $carer) { - if ($carer->name !== $carersInput[$carer->id]) { - $carer->name = $carersInput[$carer->id]; - $amendedCarers[] = $carer; - } - } - - // Create new carers - $newCarers = array_map( - static function ($new_carer) { - return new Carer(['name' => $new_carer]); - }, - (array)$request->get('new_carers') - ); - - // Create New Children - $children = $this->makeChildrenFromInput( - (array)$request->get('children') - ); - - $family = $registration->family; - - // Try to transact, so we can roll it back - try { - DB::transaction(function () use ( - $registration, - $family, - $amendedCarers, - $newCarers, - $carersKeysToDelete, - $children, - $eligibility_hsbs, - $eligibility_nrpf, - $eligible_from - ) { - - // delete the missing carers - Carer::whereIn('id', $carersKeysToDelete)->get()->each(function ($carer) { - $carer->delete(); - }); - - // delete the children. still messy. - $family->children()->delete(); - - // save the new ones! - $family->carers()->saveMany($newCarers); - $family->children()->saveMany($children); - - // save changes to the changed names - collect($amendedCarers)->each( - function (Carer $model) { - $model->save(); - } - ); - - // update eligibility - $registration->eligibility_hsbs = $eligibility_hsbs; - $registration->eligibility_nrpf = $eligibility_nrpf; - $registration->eligible_from = $eligible_from; - - // save changes to registration. - $registration->save(); - }); - } catch (Throwable $e) { - // Oops! Log that - Log::error('Bad transaction for ' . __CLASS__ . '@' . __METHOD__ . ' by service user ' . Auth::id()); - Log::error($e->getTraceAsString()); - // Throw it back to the user - return redirect()->route('store.registration.edit', ['registration' => $registration->id])->withErrors('Registration update failed.'); - } - // Or return the success - Log::info('Registration ' . $registration->id . ' updated by service user ' . Auth::id()); - // and go back to edit page for the changed registration - return redirect() - ->route('store.registration.edit', ['registration' => $registration->id]) - ->with('message', 'Registration updated.'); - } + ] + ); + return view('store.index_registration', $data); + } + + /** + * Returns the registration page + * + * @return Factory|View + */ + public function create() + { + /** @var User $user */ + $user = Auth::user(); + + // Check if we verify, based on the currently logged in user's context + // as we have no registration to refer to yet. + $sponsor = $user->centre->sponsor; + $evaluator = EvaluatorFactory::make($sponsor->evaluations); + $sponsorsRequiresID = $evaluator->isVerifyingChildren(); + $programme = Auth::user()->centre->sponsor->programme; + + $data = [ + "user_name" => $user->name, + "centre_name" => ($user->centre) ? $user->centre->name : null, + "sponsorsRequiresID" => $sponsorsRequiresID, + "programme" => $programme, + 'leaver' => false, + ]; + return view('store.create_registration', $data); + } + + /** + * Show the Registration / Family edit form + * + * @param Registration $registration + * @return Factory|View + */ + public function edit(Registration $registration) + { + // Get User and Centre; + /** @var CentreUser $user */ + $user = Auth::user(); + $data = [ + 'user_name' => $user->name, + 'centre_name' => ($user->centre) ? $user->centre->name : null, + ]; + + // Get the registration, with deep eager-loaded Family (with Children and Carers) + if ($registration) { + $registration = Registration::withFullFamily()->find($registration->id); + } else { + abort(404, 'Registration not found.'); + } + $evaluations = $registration->centre->sponsor->evaluations; + $deferrable = false; + foreach ($evaluations as $key => $evaluation) { + if ($evaluation['name'] === 'ScottishChildCanDefer') { + $deferrable = true; + } + } + $can_change_defer = true; + $this_month = Carbon::now()->month; + if ($this_month > config('arc.scottish_school_month')) { + $can_change_defer = false; + } + + // Get the valuation + /** @var Valuation $valuation */ + $valuation = $registration->getValuation(); + + // Grab carers copy for shift)ing without altering family->carers + $carers = $registration->family->carers->all(); + $pri_carer = array_shift($carers); + $pri_carer_ethnicity = $pri_carer->ethnicity; + $pri_carer_language = $pri_carer->language; + + $evaluations["creditables"] = $registration->getEvaluator()->getPurposeFilteredEvaluations("credits"); + $evaluations["disqualifiers"] = $registration->getEvaluator()->getPurposeFilteredEvaluations("disqualifiers"); + $programme = Auth::user()->centre->sponsor->programme; + + + return view('store.edit_registration', array_merge( + $data, + [ + 'registration' => $registration, + 'family' => $registration->family, + 'pri_carer' => $pri_carer, + 'pri_carer_ethnicity' => $pri_carer_ethnicity, + 'pri_carer_language' => $pri_carer_language, + 'sec_carers' => $carers, + 'children' => $registration->family->children, + 'noticeReasons' => $valuation->getNoticeReasons(), + 'entitlement' => $valuation->getEntitlement(), + 'sponsorsRequiresID' => $registration->getEvaluator()->isVerifyingChildren(), + 'evaluations' => $evaluations, + 'deferrable' => $deferrable, + 'can_change_defer' => $can_change_defer, + 'programme' => $programme, + 'leaver' => false, + ] + )); + } + + /** + * Show the Registration / Family view form for a leaver + * + * @param Registration $registration + * @return Factory|View + */ + public function view(Registration $registration) + { + // Get User and Centre; + /** @var CentreUser $user */ + $user = Auth::user(); + $data = [ + 'user_name' => $user->name, + 'centre_name' => ($user->centre) ? $user->centre->name : null, + ]; + + // Get the registration, with deep eager-loaded Family (with Children and Carers) + if ($registration) { + $registration = Registration::withFullFamily()->find($registration->id); + } else { + abort(404, 'Registration not found.'); + } + $evaluations = $registration->centre->sponsor->evaluations; + + // Get the valuation + /** @var Valuation $valuation */ + $valuation = $registration->getValuation(); + + // Grab carers copy for shift)ing without altering family->carers + $carers = $registration->family->carers->all(); + + $evaluations["creditables"] = $registration->getEvaluator()->getPurposeFilteredEvaluations("credits"); + $evaluations["disqualifiers"] = $registration->getEvaluator()->getPurposeFilteredEvaluations("disqualifiers"); + $programme = Auth::user()->centre->sponsor->programme; + + return view('store.view_registration', array_merge( + $data, + [ + 'registration' => $registration, + 'family' => $registration->family, + 'pri_carer' => array_shift($carers), + 'children' => $registration->family->children, + 'entitlement' => $valuation->getEntitlement(), + 'programme' => $programme, + 'leaver' => true, + ] + )); + } + + /** + * Displays a printable version of the Registration. + * + * @param Registration $registration + * @return Response + */ + public function printOneIndividualFamilyForm(Registration $registration) + { + // Get User + $user = Auth::user(); + + // Get the registration, with deep eager-loaded Family (with Children and Carers) + if ($registration) { + $registration = Registration::withFullFamily()->find($registration->id); + } else { + abort(404, 'Registration not found.'); + } + + // Get the valuation + /** @var Valuation $valuation */ + $valuation = $registration->getValuation(); + + // Make a filename + $filename = 'Registration' . Carbon::now()->format('YmdHis') . '.pdf'; + + // Setup common data + $data = [ + 'user_name' => $user->name, + 'centre_name' => ($user->centre) ? $user->centre->name : null, + 'sheet_title' => 'Printable Family Sheet', + 'sheet_header' => 'Family Collection Sheet', + ]; + + $data['regs'][] = [ + 'centre' => $registration->centre, + 'family' => $registration->family, + 'pri_carer' => $registration->family->pri_carer, + 'children' => $registration->family->children, + 'noticeReasons' => $valuation->getNoticeReasons(), + 'creditReasons' => $valuation->getCreditReasons(), + 'entitlement' => $valuation->getEntitlement(), + ]; + + // throw at a PDF + $pdf = PDF::loadView('store.printables.family', $data); + $pdf->setPaper('A4', 'landscape'); + return @$pdf->download($filename); + } + + /** + * Displays a printable version of the Registration. + * + * @return RedirectResponse|Response + */ + public function printBatchIndividualFamilyForms() + { + // Get the user and Centre + $user = Auth::user(); + $centre = ($user->centre) ? $user->centre : null; + + // Cope if User has no Centre. + if (!$centre) { + Log::info('User ' . $user->id . " has no Centre"); + // Send me back to dashboard + return redirect() + ->route('store.dashboard') + ->withErrors(['error_message' => 'User has no Centre']); + } + // Get the registrations this User's centre is directly responsible for + $registrations = $centre->registrations() + ->whereActiveFamily() + ->withFullFamily() + ->get() + ->sortBy(function ($registration) { + // Need strtolower because case comparison sucks. + return strtolower($registration->family->pri_carer); + }); + + if ($registrations->count() < 1) { + return redirect() + ->route('store.dashboard') + ->with('error_message', 'No Registrations in that centre.'); + } + + // Make a filename + $filename = 'Registrations_' . Carbon::now()->format('YmdHis') . '.pdf'; + + // Set up the common view data. + $data = [ + 'user_name' => $user->name, + 'centre_name' => ($user->centre) ? $user->centre->name : null, + 'sheet_title' => 'Printable Family Sheet', + 'sheet_header' => 'Family Collection Sheet', + ]; + + // Stack the registration batch into the data + foreach ($registrations as $registration) { + // Get the valuation + $valuation = $registration->getValuation(); + + $data['regs'][] = [ + 'centre' => $centre, + 'family' => $registration->family, + 'pri_carer' => $registration->family->pri_carer, + 'children' => $registration->family->children, + 'noticeReasons' => $valuation->getNoticeReasons(), + 'creditReasons' => $valuation->getCreditReasons(), + 'entitlement' => $valuation->getEntitlement(), + ]; + } + + // throw it at a PDF. + $pdf = PDF::loadView( + 'store.printables.family', + $data + ); + $pdf->setPaper('A4', 'landscape'); + return @$pdf->download($filename); + } + + /** + * Stores an incoming Registration. + * + * @param StoreNewRegistrationRequest $request + * @return RedirectResponse + * @throws Throwable $e + */ + public function store(StoreNewRegistrationRequest $request): RedirectResponse + { + // Create Carers, merge primary carer + $carers = array_map( + function ($carer) use ($request) { + return new Carer([ + 'name' => $carer, + 'ethnicity' => $request->get("pri_carer_ethnicity"), + 'language' => $request->get("pri_carer_language") + ]); + }, + array_merge( + (array)$request->get('pri_carer'), + (array)$request->get('new_carers') + ) + ); + + // Create Children + $children = $this->makeChildrenFromInput( + (array)$request->get('children') + ); + + $hsbs = $request->get('eligibility-hsbs'); + $eligible_from = null; + if ($hsbs === 'healthy-start-receiving') { + $eligible_from = Carbon::now(); + } + + // Create Registration + $registration = new Registration([ + 'consented_on' => Carbon::now(), + 'eligibility_hsbs' => $request->get('eligibility-hsbs'), + 'eligibility_nrpf' => $request->get('eligibility-nrpf'), + 'eligible_from' => $eligible_from, + ]); + + // Duplicate families are fine at this point. + $family = new Family(); + + // Set the RVID using the User's Centre. + $family->lockToCentre(Auth::user()->centre); + + // Try to transact, so we can roll it back + try { + DB::transaction(function () use ($registration, $family, $carers, $children) { + $family->save(); + $family->carers()->saveMany($carers); + $family->children()->saveMany($children); + $registration->family()->associate($family); + $registration->centre()->associate(Auth::user()->centre); + $registration->save(); + }); + } catch (Exception $e) { + // Oops! Log that + Log::error('Bad transaction for ' . __CLASS__ . '@' . __METHOD__ . ' by service user ' . Auth::id()); + Log::error($e->getTraceAsString()); + // Throw it back to the user + return redirect()->route('store.registration.create')->withErrors('Registration failed.'); + } + // Or return the success + Log::info('Registration ' . $registration->id . ' created by service user ' . Auth::id()); + // and go to the edit page for the new registration + return redirect() + ->route('store.registration.edit', ['registration' => $registration->id]) + ->with('message', 'Registration created.'); + } + + /** + * Makes children from input data + * @param array $children + * @return Child[] + */ + private function makeChildrenFromInput(array $children = []): array + { + return array_map( + function ($child) { + // Note: Carbon uses different time formats than laravel validation + // For crazy reasons known only to the creators of Carbon, when no day provided, + // createFromFormat - defaults to 31 - which bumps to next month if not a real day. + // So we want '2013-02-01' not '2013-02-31'... + $month_of_birth = Carbon::createFromFormat('Y-m-d', $child['dob'] . '-01'); + + // Check and set verified, or null + $verified = null; + if (array_key_exists('verified', $child)) { + $verified = (bool)$child['verified']; + } + + // Check and set deferred, or null + $deferred = 0; + if (array_key_exists('deferred', $child)) { + $deferred = (bool)$child['deferred']; + } + + // Check and set is_pri_carer, or null + $is_pri_carer = null; + if (array_key_exists('is_pri_carer', $child)) { + $is_pri_carer = (bool)$child['is_pri_carer']; + } + + return new Child([ + 'born' => $month_of_birth->isPast(), + 'dob' => $month_of_birth->toDateTimeString(), + 'verified' => $verified, + 'deferred' => $deferred, + 'is_pri_carer' => $is_pri_carer, + ]); + }, + $children + ); + } + + /** + * Update a Registration + * + * @param StoreUpdateRegistrationRequest $request + * @param Registration $registration + * @return RedirectResponse + */ + public function update(StoreUpdateRegistrationRequest $request, Registration $registration): RedirectResponse + { + $amendedCarers = []; + + // Fetch eligibility + $eligibility_hsbs = $request->get('eligibility-hsbs'); + $eligibility_nrpf = $request->get('eligibility-nrpf'); + $eligible_from = $registration->eligible_from; + $deferred = $request->get('deferred'); + + //Prevent the date changing if you're just editing a different field + if ($registration->eligible_from === null && $eligibility_hsbs === 'healthy-start-receiving') { + $eligible_from = Carbon::now(); + } else { + if ($eligibility_hsbs !== 'healthy-start-receiving') { + $eligible_from = null; + } + } + + // NOTE: Following refactor where we needed to retain Carer ids. + // Possible that we might want to add flag to carer to distinguish Main from Secondary, + // to simplify method below for sorting and updating carer entries. + + // Update primary carer. + $carerInput = (array)$request->get("pri_carer"); + $carerEthnicity = $request->get("pri_carer_ethnicity"); + $carerLanguage = $request->get('pri_carer_language'); + $carerKey = key($carerInput); + $carer = Carer::find($carerKey); + if ($carer->name !== $carerInput[$carer->id]) { + $carer->name = $carerInput[$carer->id]; + $amendedCarers[] = $carer; + } + if ($carerEthnicity !== null && $carerEthnicity !== $carerEthnicity[$carer->id]) { + $carer->ethnicity = $carerEthnicity[$carer->id]; + $amendedCarers[] = $carer; + } + if ($carerLanguage !== null && $carerLanguage !== $carerLanguage[$carer->id]) { + $carer->language = $carerLanguage[$carer->id]; + $amendedCarers[] = $carer; + } + + // Find secondary carers id's in the DB + $carersInput = (array)$request->get("sec_carers"); + $carersKeys = $registration->family->carers->pluck("id")->toArray(); + // remove carerKey from that; + if (($key = array_search($carerKey, $carersKeys)) !== false) { + unset($carersKeys[$key]); + } + + // Those in the DB, not in the input can be scheduled for deletion; + $carersInputKeys = array_keys($carersInput); + $carersKeysToDelete = array_diff($carersKeys, $carersInputKeys); + + // Get the secondary carers. + $carers = Carer::whereIn("id", $carersInputKeys)->get(); + + // roll though those and amend them if necessary. + foreach ($carers as $carer) { + if ($carer->name !== $carersInput[$carer->id]) { + $carer->name = $carersInput[$carer->id]; + $amendedCarers[] = $carer; + } + } + + // Create new carers + $newCarers = array_map( + static function ($new_carer) { + return new Carer(['name' => $new_carer]); + }, + (array)$request->get('new_carers') + ); + + // Create New Children + $children = $this->makeChildrenFromInput( + (array)$request->get('children') + ); + + $family = $registration->family; + + // Try to transact, so we can roll it back + try { + DB::transaction(function () use ( + $registration, + $family, + $amendedCarers, + $newCarers, + $carersKeysToDelete, + $children, + $eligibility_hsbs, + $eligibility_nrpf, + $eligible_from + ) { + + // delete the missing carers + Carer::whereIn('id', $carersKeysToDelete)->get()->each(function ($carer) { + $carer->delete(); + }); + + // delete the children. still messy. + $family->children()->delete(); + + // save the new ones! + $family->carers()->saveMany($newCarers); + $family->children()->saveMany($children); + + // save changes to the changed names + collect($amendedCarers)->each( + function (Carer $model) { + $model->save(); + } + ); + + // update eligibility + $registration->eligibility_hsbs = $eligibility_hsbs; + $registration->eligibility_nrpf = $eligibility_nrpf; + $registration->eligible_from = $eligible_from; + + // save changes to registration. + $registration->save(); + }); + } catch (Throwable $e) { + // Oops! Log that + Log::error('Bad transaction for ' . __CLASS__ . '@' . __METHOD__ . ' by service user ' . Auth::id()); + Log::error($e->getTraceAsString()); + // Throw it back to the user + return redirect()->route('store.registration.edit', ['registration' => $registration->id])->withErrors('Registration update failed.'); + } + // Or return the success + Log::info('Registration ' . $registration->id . ' updated by service user ' . Auth::id()); + // and go back to edit page for the changed registration + return redirect() + ->route('store.registration.edit', ['registration' => $registration->id]) + ->with('message', 'Registration updated.'); + } private function exactSearch($family_name, $pri_carers): array { @@ -711,24 +723,24 @@ private function exactSearch($family_name, $pri_carers): array private function fuzzySearch($family_name, $pri_carers): array { - // Get the current database driver - $connection = config('database.default'); - $driver = config("database.connections.{$connection}.driver"); - - if ($driver == 'mysql') { - // We can use Searchy for mysql; defaults to "fuzzy" search; - // results are a collection of basic objects, but we can still "pluck()" - $filtered_family_ids = Searchy::search('carers') - ->fields('name') - ->query($family_name) - ->getQuery() - ->whereIn('id', $pri_carers) - ->pluck('family_id') - ->toArray(); - } else { - // We may not be able to use Searchy, so we default to unfuzzy. + // Get the current database driver + $connection = config('database.default'); + $driver = config("database.connections.{$connection}.driver"); + + if ($driver == 'mysql') { + // We can use Searchy for mysql; defaults to "fuzzy" search; + // results are a collection of basic objects, but we can still "pluck()" + $filtered_family_ids = Searchy::search('carers') + ->fields('name') + ->query($family_name) + ->getQuery() + ->whereIn('id', $pri_carers) + ->pluck('family_id') + ->toArray(); + } else { + // We may not be able to use Searchy, so we default to unfuzzy. $filtered_family_ids = $this->exactSearch($family_name, $pri_carers); - } + } return $filtered_family_ids; } From c559152ea250d1044aa3fb98e76a33ad0579ba85 Mon Sep 17 00:00:00 2001 From: Toby Batch Date: Thu, 31 Oct 2024 16:49:21 +0000 Subject: [PATCH 3/5] chore: more work debugging move --- .docker/docker-compose.yml | 24 +++++++++++++++---- app/Console/Commands/MoveFamilies.php | 11 +++++---- .../Store/RegistrationController.php | 7 +++++- package.json | 3 ++- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/.docker/docker-compose.yml b/.docker/docker-compose.yml index 6294304e..f5787c39 100644 --- a/.docker/docker-compose.yml +++ b/.docker/docker-compose.yml @@ -1,15 +1,19 @@ -version: '3.5' services: sqldb: image: mysql:5.7 environment: - - MYSQL_DATABASE=arcv + - MYSQL_DATABASE=arcv_service - MYSQL_USER=arcvuser - MYSQL_PASSWORD=arcvpassword - MYSQL_ROOT_PASSWORD=changemeplease + volumes: + - ./initdb.d:/docker-entrypoint-initdb.d + - mysql-data:/var/lib/mysql command: --default-storage-engine innodb restart: unless-stopped + ports: + - 3336:3306 healthcheck: test: mysqladmin -p$$MYSQL_ROOT_PASSWORD ping -h localhost interval: 20s @@ -24,6 +28,8 @@ services: volumes: - ./nginx_default.conf:/etc/nginx/conf.d/default.conf - ../:/opt/project:ro + restart: unless-stopped + depends_on: [service] service: image: arc-service:dev @@ -37,7 +43,7 @@ services: - DB_CONNECTION=mysql - DB_HOST=sqldb - DB_PORT=3306 - - DB_DATABASE=arcv + - DB_DATABASE=arcv_service - DB_USERNAME=arcvuser - DB_PASSWORD=arcvpassword - LOG_CHANNEL=stderr @@ -52,11 +58,21 @@ services: - "arcv-service.test:host-gateway" - "arcv-store.test:host-gateway" - "arcv-market.test:host-gateway" + restart: unless-stopped + depends_on: [sqldb] mailer: image: schickling/mailcatcher ports: - "${MAILER_ADMIN_PORT:-2080}:1080" + restart: unless-stopped volumes: - service_public: \ No newline at end of file + service_public: + mysql-data: + +# docker compose -f .docker/docker-compose.yml up -d +# docker compose -f .docker/docker-compose.yml logs -f +# docker compose -f .docker/docker-compose.yml exec service bash +# docker compose -f .docker/docker-compose.yml exec service /opt/project/artisan +# docker compose -f .docker/docker-compose.yml exec sqldb mysql -uarcvuser -parcvpassword arcv_service diff --git a/app/Console/Commands/MoveFamilies.php b/app/Console/Commands/MoveFamilies.php index ff03f96d..62385464 100644 --- a/app/Console/Commands/MoveFamilies.php +++ b/app/Console/Commands/MoveFamilies.php @@ -17,14 +17,14 @@ class MoveFamilies extends Command * * @var string */ - protected $signature = 'arc:skunk {from_centre} {to_centre} {family_ids} '; + protected $signature = 'arc:move:families {from_centre} {to_centre} {family_ids} '; /** * The console command description. * * @var string */ - protected $description = 'Move families between centres, e.g. ./artisan arc:skunk 103 101 4,42,44,46,39,40,41,45'; + protected $description = 'Move families between centres, e.g. ./artisan arc:move:families 103 101 4,42,44,46,39,40,41,45'; /** * Execute the console command. @@ -57,6 +57,9 @@ public function fixFamalams() ->filter(function ($f) use ($family_list) { return in_array($f->family->centre_sequence, $family_list); }); + if (count($reg_associated_with_fam) == 0) { + $this->warn("No matching registrations found"); + } foreach ($reg_associated_with_fam as $singleReg) { /* @var Registration $singleReg */ @@ -84,8 +87,8 @@ public function fixFamalams() $this->info(sprintf(" Locking family %s to centre %s", $singleReg->family->id, $to_centre->id)); $singleReg->family->lockToCentre($to_centre, true); $singleReg->family->save(); - $_family = Family::where("id", $singleReg->family->id)->first(); - \Log::info($_family); + $singleReg->centre = $to_centre; + $singleReg->save(); } } } diff --git a/app/Http/Controllers/Store/RegistrationController.php b/app/Http/Controllers/Store/RegistrationController.php index 91935100..58e9e2ad 100644 --- a/app/Http/Controllers/Store/RegistrationController.php +++ b/app/Http/Controllers/Store/RegistrationController.php @@ -113,7 +113,12 @@ public function index(Request $request) // $_centre = null $_rm = $q->get(); foreach ($_rm as $r) { - \Log::info($r); + \Log::info( + sprintf( + "Reg id: %d, family id: %d, centre id: %d", + $r->id, $r->family_id, $r->centre_id + ) + ); } // This isn't ideal as it relies on getting all the families, then sorting them. diff --git a/package.json b/package.json index 6fdb03c5..8454aa98 100644 --- a/package.json +++ b/package.json @@ -29,5 +29,6 @@ ">2%", "ie 11", "last 3 versions" - ] + ], + "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" } From 03c7489e6862f64bb7050d0229a15b0fe562fbaa Mon Sep 17 00:00:00 2001 From: Toby Batch Date: Thu, 31 Oct 2024 17:27:39 +0000 Subject: [PATCH 4/5] feat: set the registration centre to the new centre --- app/Console/Commands/MoveFamilies.php | 6 ++-- app/Console/Commands/UnbundleVouchers.php | 37 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 app/Console/Commands/UnbundleVouchers.php diff --git a/app/Console/Commands/MoveFamilies.php b/app/Console/Commands/MoveFamilies.php index 62385464..2caa9f92 100644 --- a/app/Console/Commands/MoveFamilies.php +++ b/app/Console/Commands/MoveFamilies.php @@ -24,7 +24,7 @@ class MoveFamilies extends Command * * @var string */ - protected $description = 'Move families between centres, e.g. ./artisan arc:move:families 103 101 4,42,44,46,39,40,41,45'; + protected $description = 'Move families between centres, e.g. ./artisan arc:move:families 101 113 4,42,44,46,39,40,41,45'; /** * Execute the console command. @@ -80,14 +80,14 @@ public function fixFamalams() )); if ($bundle->disbursing_centre_id != null && $bundle->disbursing_centre_id != $from_centre_id) { $bundle->disbursing_centre_id = $to_centre_id; - $this->info(sprintf(" Updating bundle->disbursing_centre_id = %s", $to_centre_id)); +// $this->info(sprintf(" Updating bundle->disbursing_centre_id = %s", $to_centre_id)); $bundle->save(); } } $this->info(sprintf(" Locking family %s to centre %s", $singleReg->family->id, $to_centre->id)); $singleReg->family->lockToCentre($to_centre, true); $singleReg->family->save(); - $singleReg->centre = $to_centre; + $singleReg->centre_id = $to_centre->id; $singleReg->save(); } } diff --git a/app/Console/Commands/UnbundleVouchers.php b/app/Console/Commands/UnbundleVouchers.php new file mode 100644 index 00000000..54ba3a1b --- /dev/null +++ b/app/Console/Commands/UnbundleVouchers.php @@ -0,0 +1,37 @@ + Date: Thu, 31 Oct 2024 18:12:52 +0000 Subject: [PATCH 5/5] feat: added unbundle command --- app/Console/Commands/UnbundleVouchers.php | 36 +++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/UnbundleVouchers.php b/app/Console/Commands/UnbundleVouchers.php index 54ba3a1b..c95f3677 100644 --- a/app/Console/Commands/UnbundleVouchers.php +++ b/app/Console/Commands/UnbundleVouchers.php @@ -2,8 +2,8 @@ namespace App\Console\Commands; -use App\Centre; -use App\Registration; +use App\Bundle; +use App\Voucher; use Illuminate\Console\Command; use Symfony\Component\Console\Command\Command as CommandAlias; @@ -31,7 +31,39 @@ class UnbundleVouchers extends Command */ public function handle(): int { + // grab vouchers + $vouchers = []; + for ($i = $this->argument('start_id'); $i <= (intval($this->argument('end_id'))); $i++) { + $vouchers[] = Voucher::where('code', $this->argument('prefix') . $i)->first(); + } + // get bundle ids + $bundle_ids = []; + foreach ($vouchers as $v) { + $bundle_ids[] = $v->bundle_id; + } + $bundle_ids = array_unique($bundle_ids); + + // warn that we will destroy these bundles, other vouchers in the bundle will be unbundled + $this->warn(sprintf("This will destroy the bundles with the IDs %s", implode(", ", $bundle_ids))); + if (!$this->confirm('Are you sure you want to proceed?')) { + // Code to execute if the user confirms + $this->info('Run back home then little petal...'); + } + + // foreach voucher, null the bundle id + foreach ($vouchers as $v) { + $v->bundle_id = null; + $this->info('Nulling bundle on ' . $v->code); + $v->save(); + } + + // foreach bundle delete the bundle + foreach ($bundle_ids as $bundle_id) { + $b = Bundle::where('id', $bundle_id); + $this->info('Deleting bundle ' . $bundle_id); + $b->delete(); + } return CommandAlias::SUCCESS; } }