From 3fbd2f93c8521a1d7a06112158fa1c74febbb34a Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 8 Apr 2018 17:36:37 +0200 Subject: [PATCH] Code for #1324 --- app/Console/Commands/UpgradeDatabase.php | 6 ++-- app/Http/Controllers/BillController.php | 10 +++++++ app/Http/Controllers/RuleController.php | 2 +- app/Repositories/Bill/BillRepository.php | 29 +++++++++++++++++++ .../Bill/BillRepositoryInterface.php | 12 ++++++++ resources/views/list/bills.twig | 14 ++++++--- 6 files changed, 66 insertions(+), 7 deletions(-) diff --git a/app/Console/Commands/UpgradeDatabase.php b/app/Console/Commands/UpgradeDatabase.php index 7f437cbf47..d842c27ce1 100644 --- a/app/Console/Commands/UpgradeDatabase.php +++ b/app/Console/Commands/UpgradeDatabase.php @@ -119,9 +119,11 @@ class UpgradeDatabase extends Command } // loop bills. - $order = 1; + $order = 1; + /** @var Collection $collection */ + $collection = $user->bills()->where('active', 1)->get(); /** @var Bill $bill */ - foreach ($user->bills()->get() as $bill) { + foreach ($collection as $bill) { if ($bill->match !== 'MIGRATED_TO_RULES') { $rule = Rule::create( [ diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php index 29b4770bdb..3f020a4e27 100644 --- a/app/Http/Controllers/BillController.php +++ b/app/Http/Controllers/BillController.php @@ -199,6 +199,16 @@ class BillController extends Controller } ); + // add info about rules: + $rules = $repository->getRulesForBills($paginator->getCollection()); + $bills = $bills->map( + function (array $bill) use ($rules) { + $bill['rules'] = $rules[$bill['id']] ?? []; + + return $bill; + } + ); + $paginator->setPath(route('bills.index')); return view('bills.index', compact('bills', 'paginator')); diff --git a/app/Http/Controllers/RuleController.php b/app/Http/Controllers/RuleController.php index f4be740679..87be71d5d0 100644 --- a/app/Http/Controllers/RuleController.php +++ b/app/Http/Controllers/RuleController.php @@ -369,7 +369,7 @@ class RuleController extends Controller // redirect to new bill creation. if ((int)$request->get('bill_id') > 0) { - return redirect(route('bills.create')); + return redirect($this->getPreviousUri('bills.create.uri')); } diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php index cba39e1599..14fb2f4bf8 100644 --- a/app/Repositories/Bill/BillRepository.php +++ b/app/Repositories/Bill/BillRepository.php @@ -367,6 +367,35 @@ class BillRepository implements BillRepositoryInterface return $journals; } + /** + * Return all rules related to the bills in the collection, in an associative array: + * 5= billid + * + * 5 => [['id' => 1, 'title' => 'Some rule'],['id' => 2, 'title' => 'Some other rule']] + * + * @param Collection $collection + * + * @return array + */ + public function getRulesForBills(Collection $collection): array + { + $rules = $this->user->rules() + ->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id') + ->where('rule_actions.action_type', 'link_to_bill') + ->get(['rules.id', 'rules.title', 'rule_actions.action_value']); + $array = []; + foreach ($rules as $rule) { + $array[$rule->action_value] = $array[$rule->action_value] ?? []; + $array[$rule->action_value][] = ['id' => $rule->id, 'title' => $rule->title]; + } + $return = []; + foreach ($collection as $bill) { + $return[$bill->id] = $array[$bill->name] ?? []; + } + + return $return; + } + /** * @param Bill $bill * @param Carbon $date diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php index cd4bfe3cd7..19cd785ace 100644 --- a/app/Repositories/Bill/BillRepositoryInterface.php +++ b/app/Repositories/Bill/BillRepositoryInterface.php @@ -140,6 +140,18 @@ interface BillRepositoryInterface */ public function getPossiblyRelatedJournals(Bill $bill): Collection; + /** + * Return all rules related to the bills in the collection, in an associative array: + * 5= billid + * + * 5 => [['id' => 1, 'title' => 'Some rule'],['id' => 2, 'title' => 'Some other rule']] + * + * @param Collection $collection + * + * @return array + */ + public function getRulesForBills(Collection $collection): array; + /** * @param Bill $bill * @param Carbon $date diff --git a/resources/views/list/bills.twig b/resources/views/list/bills.twig index aa1759cd3b..cdb320ac7b 100644 --- a/resources/views/list/bills.twig +++ b/resources/views/list/bills.twig @@ -21,8 +21,8 @@ {% for entry in bills %} {% if entry.active %} {% set count = count + 1 %} - {% set sum_min = sum_min + entry.amount_min %} - {% set sum_max = sum_min + entry.amount_max %} + {% set sum_min = sum_min + entry.amount_min %} + {% set sum_max = sum_min + entry.amount_max %} {% set expected_total = expected_total + ((entry.amount_min + entry.amount_max) / 2) %} {% endif %} @@ -43,7 +43,13 @@ - (rules) + {% if entry.rules|length > 0 %} + + {% endif %} @@ -127,7 +133,7 @@   - {# calculate total#} + {# calculate total#} {% if count > 0 %} {% set avg_min = (sum_min / count) %} {% set avg_max = (sum_max / count) %}