Grouping by charge types

4 years ago

jaseofspades88

Groups the allCharges by their respective type and sums their amount

Code

$quote = App\Pricing\Models\ClientQuote::withoutGlobalScopes()->find(24);

$quote
    ->allCharges()
    ->groupBy('description')
    ->map(function ($collection) {
        $item = $collection->first();
        $item->amount = $collection->sum('amount');
        return $item;
    })
    ->values();

Output

=> App\Pricing\Models\ClientQuote {#2373
     id: 80,
     result: null,
     current_step: "shipping-date",
     order_id: null,
     brand_id: 1,
     delivery_service_id: null,
     booker_id: 301,
     client_account_id: 109,
     purpose: "gift",
     from_country_code: "GB",
     to_country_code: "FR",
     service_type: null,
     client_type: null,
     journey_type: "return",
     client_travelling: null,
     client_first_name: "John",
     client_last_name: "Smith",
     client_email: "[email protected]",
     client_phone_number: "01204123456",
     client_country_code: "GB",
     agreed_to_terms: 1,
     booking_for_someone_else: 0,
     booker_first_name: null,
     booker_last_name: null,
     booker_email: null,
     booker_phone_number: null,
     booker_country_code: null,
     booker_terms_of_use: null,
     shipping_option: "express",
     outbound_type: "collect_by",
     outbound_delivery_date: "2020-09-25 00:00:00",
     outbound_collect_by_date: "2020-09-23 00:00:00",
     return_type: "deliver_by",
     return_delivery_date: "2020-09-25 00:00:00",
     return_collect_by_date: "2020-09-23 00:00:00",
     paid: 1,
     cached_at: null,
     arrives_at: null,
     created_at: "2020-09-23 16:20:36",
     updated_at: "2020-09-23 17:15:46",
   }
HELO: Local email testing for your desktop!