i have two table and one pivot table. settlement table is this:
$table->ulid();
$table->string('track_id', 36);
$table->string('payee_user_id', 36);
$table->string('terminal_type', 36)->nullable();
$table->string('terminal_provider', 36)->nullable();
$table->string('terminal_psp', 36)->nullable();
$table->string('terminal_id', 36)->nullable();
$table->string('acceptor_id', 36)->nullable();
$table->string('terminal_no', 36)->nullable();
$table->string('account_bank_id', 36)->nullable();
$table->string('account_id', 36)->nullable();
$table->string('account_deposit', 36)->nullable();
$table->string('account_iban', 36)->nullable();
$table->integer('count')->nullable();
$table->decimal('amount_settle', 15, 2);
$table->decimal('amount_wage', 15, 2);
$table->timestamps(6);
transaction table is this
$table->ulid();
$table->string('payee_user_id', 36);
$table->string('type', 36);
$table->string('terminal_id', 36);
$table->string('terminal_no', 36);
$table->string('terminal_type', 36);
$table->string('acceptor_id', 36);
$table->string('terminal_psp', 36);
$table->string('terminal_provider', 36);
$table->decimal('amount_settle', 15, 2);
$table->decimal('amount_wage', 15, 2);
$table->string('account_id', 36)->nullable();;
$table->string('account_bank_id', 36)->nullable();;
$table->string('account_deposit', 26)->nullable();
$table->string('account_iban', 26)->nullable();
$table->string('wallet_id', 36)->nullable();
$table->timestamp('paid_at', 6);
settlements_transactions table is this:
$table->string('transaction_id')->index();
$table->string('settlement_id')->index();
$table->foreign('transaction_id', 'fk_transaction_id')
->on('transactions')->references('id');
$table->foreign('settlement_id', 'fk_settlement_id')
->on('settlements')->references('id');
I am inserting group by of transaction in settlement table If I have not saved before.
$transactions = DB::table('transactions')
->where('paid_at', '<', $date)
->where('terminal_type', Transaction::PRIVATE)
->where('terminal_provider', Transaction::SHAPARAK)
->distinct()->selectRaw(
'account_iban,
account_deposit,
account_bank_id,
payee_user_id,
account_id,
terminal_psp,
terminal_id,
terminal_no,
acceptor_id,
account_id,
SUM(amount_settle) as amount_settle,
SUM(amount_wage) as amount_wage,
COUNT(id) as count'
)->groupBy(
'payee_user_id',
'account_id',
'account_iban',
'terminal_psp',
'terminal_no',
'terminal_id',
'acceptor_id',
'account_id',
'account_deposit',
'account_bank_id',
)->orderBy('payee_user_id')
->orderBy('account_id')
->orderBy('terminal_psp')
->orderBy('terminal_id')
->orderBy('account_id')
->orderBy('account_deposit')
->orderBy('account_bank_id')
->orderBy('account_iban')
->orderBy('terminal_no')
->orderBy('acceptor_id')
->get();
foreach ($transactions as $transaction) {
$exist = DB::table('settlements')
->where('payee_user_id', $transaction->payee_user_id)
->where('account_id', $transaction->account_id)
->where('account_deposit', $transaction->account_deposit)
->where('terminal_id', $transaction->terminal_id)
->where('terminal_psp', $transaction->terminal_psp)
->where('account_bank_id', $transaction->account_bank_id)
->where('terminal_provider', Transaction::SHAPARAK)
->where('terminal_type', Transaction::PRIVATE)
->where('settlements.created_at', '>=', Carbon::today())
->join('settlement_statuses', 'settlement_statuses.settlement_id', 'settlements.id')
->applySettlementLastStatusSubJoin('settlement_statuses')
->applySettlementLastAccepted('settlements')
->exists();
if ($exist) {
continue;
}
//insert in settlements table
}
I do not want to check exist in the loop. i need to get transactions group by result if not exist in settlement table.