->query(function (Builder $query, array $data, $livewire): Builder {
$partnerId = $livewire->ownerRecord->id;
$date = $data['date'];
if (isset($date)) {
$date = explode(' ', $date);
$date = $date[0];
///*dd(*/return $query->where('date', $date)->where('partner_id', $partnerId)->withTrashed()/*->get())*/;
$query = $query->withTrashed()->selectRaw('plan_partner_payments.*, @cumulative_balance:=@cumulative_balance + plan_partner_payments.credit-plan_partner_payments.debit AS balance')
->fromRaw('(
SELECT -1 as id, -1 as plan_partner_id, -1 as plan_id, partner_id, DATE_SUB(\'' . $date . '\', INTERVAL 1 YEAR) as date,
CASE WHEN SUM(credit)-SUM(debit) < 0 THEN SUM(credit)-SUM(debit) ELSE 0 END as debit,
CASE WHEN SUM(credit)-SUM(debit) >= 0 THEN SUM(credit)-SUM(debit) ELSE 0 END as credit
FROM plan_partner_payments
WHERE partner_id = ' . $partnerId . ' AND date < DATE_SUB(\'' . $date . '\', INTERVAL 1 YEAR)
UNION ALL
SELECT id, plan_partner_id, plan_id, partner_id, date, debit, credit
FROM plan_partner_payments
WHERE partner_id = ' . $partnerId . ' AND date = \'' . $date . '\'
) plan_partner_payments
JOIN (SELECT @cumulative_balance:=0) r');
}
return $query;
}),