Transaction

mail@pastecode.io avatar
unknown
php
5 months ago
37 kB
6
Indexable
<?php

namespace App\Http\Services;

use App\Amount;
use App\Exceptions\CustomException;
use App\Merchant;
use App\Transaction;
use Carbon\Carbon;
use DateTime;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TransactionService
{

    public function inquiryRecurringTransaction($merchantId, $vaNumber)
    {
        Log::info("Inquiry reccuring Transaction");
        Log::info("merchant Id : " . $merchantId);
        Log::info("vaNumber : " . $vaNumber);

        $data_transaction = Transaction::join('merchant', 'transaction.id_merchant', '=', 'merchant.id')
            ->where('merchant_id', '=', $merchantId)
            ->where('va', 'like', '%' . $vaNumber . '%')
            ->where('amount_type', 0)
            ->where('is_recurring', 'Y')
            ->get();

        Log::info("Data transaction recurring : " . json_encode($data_transaction));

        if (!($data_transaction->isEmpty())) {
            foreach ($data_transaction as $transaction) {
                $validityDate = Carbon::parse($transaction->validity);
                $validityMonthYear = Carbon::createFromFormat('Y-m-d H:i:s', $transaction->validity);
                $validityMonthYear = $validityMonthYear->format('Y-m');
                $validityMonthYear = Carbon::createFromFormat('Y-m', $validityMonthYear);

                $currentDate = Carbon::now();
                $currentMounthYear = $currentDate->format('Y-m');
                $currentMounthYear = Carbon::createFromFormat('Y-m', $currentMounthYear);

                // Periksa apakah bulan saat ini sudah bulan yang baru berdasarkan validityDate
                if ($validityMonthYear->lessThan($currentMounthYear)) {
                    // Hitung selisih bulan antara validity dan current date
                    $newCurrentDate = $currentDate->copy()->startOfMonth();
                    $newValidityDate = $validityDate->copy()->startOfMonth();

                    // dd($newValidityDate, $newCurrentDate);
                    // $monthsDifference = $newValidityDate->diffInMonths($newCurrentDate);
                    // dd($monthsDifference);
                    // Log::info("monthDiff before : " . $monthsDifference);

                    $newExpiryDate = Carbon::now()->startOfMonth()->addDays(26)->setTime(0, 0, 0);

                    $accumulation = 0;
                    if ($transaction->paid_date != null) {

                        // for ($i=0; $i < $monthsDifference; $i++) { 
                        //     $accumulation = $accumulation + $transaction->initial_bill;
                        // }

                        //accumulation = (initial bill * month_diff) + sisa_bill;
                        // $accumulation = ($transaction->initial_bill * $monthsDifference) + $transaction->bill;

                        while ($newCurrentDate->greaterThan($newValidityDate)) {
                            $accumulation = $accumulation + $transaction->initial_bill;
                            $newValidityDate->addMonth();
                        }

                        $accumulation = $accumulation + $transaction->bill;

                        $updateData = [
                            'status' => '10',
                            'validity' => $newExpiryDate->format('Y-m-d H:i:s'),
                            // 'paid_date' => NULL,
                            // 'va_paid' => NULL,
                            'jumlah' => $accumulation,
                            'bill' => $accumulation,
                            'last_paid' => NULL
                        ];

                        Log::info("accumulation : " . $accumulation);
                    } else {
                        // for ($i=0; $i <= $monthsDifference; $i++) { 
                        //     $accumulation = $accumulation + $transaction->jumlah;
                        // }

                        // $monthsDifference = $monthsDifference + 1;
                        // $accumulation = $transaction->jumlah * $monthsDifference;

                        while ($newCurrentDate->greaterThanOrEqualTo($newValidityDate)) {
                            $accumulation = $accumulation + $transaction->jumlah;
                            $newValidityDate->addMonth();
                        }

                        $updateData = [
                            'status' => '10',
                            'validity' => $newExpiryDate->format('Y-m-d H:i:s'),
                            // 'paid_date' => NULL,
                            // 'va_paid' => NULL,
                            'jumlah' => $accumulation,
                            'bill' => $accumulation,
                            'initial_bill' => $transaction->jumlah
                        ];

                        Log::info("monthsDiff : " . $accumulation);
                        Log::info("paid_date : " . $transaction->paid_date);
                    }

                    Log::info("last paid : " . $transaction->last_paid);
                    Log::info("currentDate : " . $currentDate);
                    Log::info("validityDate : " . $validityDate);
                    Log::info("akumulasi : " . $accumulation);

                    $this->renewRecurringTransaction($transaction->id_merchant, $vaNumber, $updateData);

                    $transaction->status = '10';

                    Log::info('Pembaharuan transaksi VA: ' . $transaction->id);
                    Log::info('validity before update: ' . $validityDate);
                    Log::info('validity after update: ' . $newExpiryDate->format('Y-m-d H:i:s'));
                    Log::info('status : ' . '10');
                }

                //periksa apakah transaksi sudah terbayarkan di bulan ini
                // if($transaction->status == '11') {
                //     Log::info('VA recurring already paid');
                //     throw new CustomException('00', 'Paid');
                // }                
            }
        }

        $this->checkExpired($merchantId, $vaNumber);
    }

    public function checkExpired($merchantId, $vaNumber)
    {
        $data_transaction = Transaction::join('merchant', 'transaction.id_merchant', '=', 'merchant.id')
            ->where('merchant_id', '=', $merchantId)
            ->where('va', 'like', '%' . $vaNumber . '%')
            ->where('amount_type', 0)
            ->where('is_recurring', 'Y')
            ->get();

        if (!($data_transaction->isEmpty())) {
            foreach ($data_transaction as $transaction) {
                $validityDate = Carbon::parse($transaction->validity);
                $validityMonthYear = Carbon::createFromFormat('Y-m-d H:i:s', $transaction->validity);
                $validityMonthYear = $validityMonthYear->format('Y-m');
                $validityMonthYear = Carbon::createFromFormat('Y-m', $validityMonthYear);

                $currentDate = Carbon::now();
                $currentMounthYear = $currentDate->format('Y-m');
                $currentMounthYear = Carbon::createFromFormat('Y-m', $currentMounthYear);

                // Periksa apakah tanggal saat ini sudah lewat tanggal validity dan masih dalam bulan yang sama
                if ($currentDate->gt($validityDate) && $currentDate->isSameMonth($validityDate)) {
                    $updateData = [
                        'status' => '12'
                    ];
                    $this->renewRecurringTransaction($transaction->id_merchant, $vaNumber, $updateData);
                    throw new CustomException('01', 'Expired');
                    Log::info('Pembaharuan transaksi VA: ' . $transaction->id);
                    Log::info('status : ' . '12');
                    Log::info('currentDate : ' . $currentDate);
                    Log::info('validityDate : ' . $validityDate);
                }
            }
        }
    }

    public function renewRecurringTransaction($id_merchant, $vaNumber, $updateData)
    {
        Transaction::where('id_merchant', '=', $id_merchant)
            ->where('va', 'like', '%' . $vaNumber . '%')
            ->where('is_recurring', 'Y')
            ->update($updateData);
    }

    public function renewTransaction($id_merchant, $vaNumber, $updateData)
    {
        Transaction::where('id_merchant', '=', $id_merchant)
            ->where('va', 'like', '%' . $vaNumber . '%')
            ->whereNull('parent_id')
            ->update($updateData);
    }

    public function insertTransaction($insertData)
    {
        try {
            $newTransaction = new Transaction();
            foreach ($insertData as $key => $value) {
                $newTransaction->$key = $value;
            }
            $newTransaction->save();
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function getTransaction($vaNumber, $merchantId)
    {

        Log::info("vaNumber : " . $vaNumber);
        Log::info("merchant Id : " . $merchantId);

        $transaction = DB::table('transaction')
            ->join('merchant', 'merchant.id', '=', 'transaction.id_merchant')
            ->where('transaction.va', 'like', '%' . $vaNumber . '%')
            ->where('merchant.merchant_id', $merchantId)
            ->select('transaction.*', 'merchant.name')
            ->first();

        
        return $transaction;
    }

    private function prepareInsertData($data_trans, $amount, $reqAmount, $accountNo, $bill)
    {
        return [
            'id_merchant' => $data_trans->id_merchant,
            'invoice' => $data_trans->invoice,
            'nama' => $data_trans->nama,
            'email' => $data_trans->email,
            'no_hp' => $data_trans->no_hp,
            'description' => $data_trans->description,
            'va' => $data_trans->va,
            'jumlah' => $amount,
            'validity' => $data_trans->validity,
            'status' => 1,
            'date' => $data_trans->date,
            'inserted_by' => $data_trans->inserted_by,
            'remarks' => $data_trans->remarks,
            'penalty_code' => $data_trans->penalty_code,
            'total_penalty' => $data_trans->total_penalty,
            'input_method' => $data_trans->input_method,
            'amount_type' => $data_trans->amount_type,
            'is_notif' => $data_trans->is_notif,
            'bill' => $bill,
            'initial_bill' => $bill,
            'va_paid' => $accountNo,
            'last_paid' => $reqAmount,
            'operator_id' => $data_trans->operator_id,
            'filetransaction_data_id' => $data_trans->filetransaction_data_id,
            'filetransaction_id' => $data_trans->filetransaction_id,
            'is_recurring' => NULL,
            'paid_date' => now(),
            'parent_id' => $data_trans->id
        ];
    }

    /* partial payment */
    public function partialPayment($data_trans, $amount, $accountNo)
    {

        $succes = 20;
        $not_found = 0;
        $less_amount = 21;
        $exeed_amount = 22;
        $not_match_bill = 23;
        $less_minimum = 24;
        
        if (empty($data_trans)) {
            Log::info("The transaction data is empty or null.");
            return $not_found;
        }

        if (!isset($data_trans->payment_method)) {
            Log::info("Payment method is not defined or is null.");
            return $not_found;
        }

        if (!isset($data_trans->value_type)) {
            Log::info("Value type is not defined or is null.");
            return $not_found;
        }

        // if (intval($data_trans->jumlah) == 1) {
        //     Log::info("VA already paid");
        //     return 3;
        // }

        // Ubah tanggal validity menjadi timestamp
        // $validityTimestamp = strtotime($data_trans->validity);
        // // Dapatkan timestamp dari waktu saat ini
        // $currentTimestamp = time();
        // if ($validityTimestamp < $currentTimestamp) {
        //     Log::info("Transaction expired");
        //     // Misalnya, kembalikan kode untuk menandakan bahwa transaksi kedaluwarsa
        //     return 0;
        // }

        /**
         * validasi berdasarkan parent partial
         * jumlah = bill + last_paid
         * nominal = jumlah * partial1 / 100
         * bill = jumlah - last_paid
         * 
         */

        if ($data_trans->payment_method == 'partial' && $data_trans->value_type == 'percentage') {
            Log::info("UPDATE | Partial Percentage");
            // $total_bill = $data_trans->bill;

            $jumlah = intval($data_trans->jumlah);

            $lastThreeDigits = substr($jumlah, -3);

            // Ganti 3 angka terakhir dengan 000
            $jumlah = $jumlah - $lastThreeDigits;
            // $jumlah = $jumlah + 1000;

            Log::info("last three digit : " . $lastThreeDigits);
            Log::info("jumlah : " . $jumlah);


            $partial1 = 0;
            $partial2 = 0;
            $partial3 = 0;
            $partial4 = 0;

            if (!empty($data_trans->partial1)) {
                $partial1 = $jumlah * $data_trans->partial1 / 100;
            }

            if (!empty($data_trans->partial2)) {
                $partial2 = $jumlah * $data_trans->partial2 / 100;
            }

            if (!empty($data_trans->partial3)) {
                $partial3 = $jumlah * $data_trans->partial3 / 100;
            }

            if (!empty($data_trans->partial4)) {
                $partial4 = $jumlah * $data_trans->partial4 / 100;
            }

            if (!empty($partial4)) {
                $partial4 = $partial4 + $lastThreeDigits;
            } elseif (!empty($partial3)) {
                $partial3 = $partial3 + $lastThreeDigits;
            } elseif (!empty($partial2)) {
                $partial2 = $partial2 + $lastThreeDigits;
            } elseif (!empty($partial1)) {
                $partial1 = $partial1 + $lastThreeDigits;
            }

            $updateData = [
                'paid_date' => now(),
                'va_paid' => $accountNo,
                'last_paid' => $amount
            ];

            //partial 1
            if (empty($data_trans->last_paid)) {
                Log::info("Partial : " . $data_trans->partial1);
                Log::info("amount : " . $amount);
                Log::info("partial 1 : " . $partial1);
                // if ($amount != $partial1) {
                //     Log::info("Invalid amount");
                //     return 1;
                // }

                if($amount < $partial1){
                    Log::info("The amount entered is less than the required partial payment amount.");
                    return $less_amount;
                }

                if($amount > $partial1){
                    Log::info("The amount entered exceeds the partial payment amount.");
                    return $exeed_amount;
                }

                
                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $amount, $accountNo, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                if ($updateData['bill'] == 0) {
                    $updateStatus = ['status' => 1];
                    $this->renewTransaction($data_trans->id_merchant, $accountNo, $updateStatus);
                }
                return $succes;
            }

            //partial 2
            $total = $data_trans->bill + $partial1;
            if ($data_trans->jumlah == $total) {
                // if ($amount != $partial2) {
                //     Log::info("Invalid amount");
                //     return 1;
                // }
                if($amount < $partial2){
                    Log::info("The amount entered is less than the required partial payment amount.");
                    return $less_amount;
                }

                if($amount > $partial2){
                    Log::info("The amount entered exceeds the partial payment amount.");
                    return $exeed_amount;
                }
                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $amount, $accountNo, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                if ($updateData['bill'] == 0) {
                    $updateStatus = ['status' => 1];
                    $this->renewTransaction($data_trans->id_merchant, $accountNo, $updateStatus);
                }


                return $succes;
            }

            //partial 3
            $compare_partial3 = $data_trans->bill - $partial4;
            if ($compare_partial3 == $partial3) {
                // if ($amount != $partial3) {
                //     Log::info("Invalid amount");
                //     return 1;
                // }
                if($amount < $partial3){
                    Log::info("The amount entered is less than the required partial payment amount.");
                    return $less_amount;
                }

                if($amount > $partial3){
                    Log::info("The amount entered exceeds the partial payment amount.");
                    return $exeed_amount;
                }
                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $amount, $accountNo, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                if ($updateData['bill'] == 0) {
                    $updateStatus = ['status' => 1];
                    $this->renewTransaction($data_trans->id_merchant, $accountNo, $updateStatus);
                }
                return $succes;
            }

            //partial 4
            if ($data_trans->bill == $partial4) {
                // if ($amount != $partial4) {
                //     Log::info("Invalid amount");
                //     return 1;
                // }
                if($amount < $partial4){
                    Log::info("The amount entered is less than the required partial payment amount.");
                    return $less_amount;
                }

                if($amount > $partial4){
                    Log::info("The amount entered exceeds the partial payment amount.");
                    return $exeed_amount;
                }
                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $amount, $accountNo, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                if ($updateData['bill'] == 0) {
                    $updateStatus = ['status' => 1];
                    $this->renewTransaction($data_trans->id_merchant, $accountNo, $updateStatus);
                }
                return $succes;
            }

            return $not_found;
        }

        if ($data_trans->payment_method == 'partial' && $data_trans->value_type == 'flat') {
            Log::info("UPDATE | Partial flat");
            $partial1 = $data_trans->partial1;
            $partial2 = $data_trans->partial2;
            $partial3 = $data_trans->partial3;
            $partial4 = $data_trans->partial4;
            $updateData = [
                'paid_date' => now(),
                'va_paid' => $accountNo,
                'last_paid' => $amount
            ];

            //partial 1
            if (empty($data_trans->last_paid)) {
                // if ($amount != $partial1) {
                //     Log::info("Invalid amount");
                //     return 1;
                // }
                if($amount < $partial1){
                    Log::info("The amount entered is less than the required partial payment amount.");
                    return $less_amount;
                }

                if($amount > $partial1){
                    Log::info("The amount entered exceeds the partial payment amount.");
                    return $exeed_amount;
                }
                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $amount, $accountNo, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                if ($updateData['bill'] == 0) {
                    $updateStatus = ['status' => 1];
                    $this->renewTransaction($data_trans->id_merchant, $accountNo, $updateStatus);
                }
                return $succes;
            }

            //partial 2
            $total = $data_trans->bill + $partial1;
            if ($data_trans->jumlah == $total) {
                // if ($amount != $partial2) {
                //     Log::info("Invalid amount");
                //     return 1;
                // }
                if($amount < $partial2){
                    Log::info("The amount entered is less than the required partial payment amount.");
                    return $less_amount;
                }

                if($amount > $partial2){
                    Log::info("The amount entered exceeds the partial payment amount.");
                    return $exeed_amount;
                }
                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $amount, $accountNo, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                if ($updateData['bill'] == 0) {
                    $updateStatus = ['status' => 1];
                    $this->renewTransaction($data_trans->id_merchant, $accountNo, $updateStatus);
                }
                return $succes;
            }

            //partial 3
            $compare_partial3 = $data_trans->bill - $partial4;
            if ($compare_partial3 == $partial3) {
                // if ($amount != $partial3) {
                //     Log::info("Invalid amount");
                //     return 1;
                // }
                if($amount < $partial3){
                    Log::info("The amount entered is less than the required partial payment amount.");
                    return $less_amount;
                }

                if($amount > $partial3){
                    Log::info("The amount entered exceeds the partial payment amount.");
                    return $exeed_amount;
                }
                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $amount, $accountNo, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                if ($updateData['bill'] == 0) {
                    $updateStatus = ['status' => 1];
                    $this->renewTransaction($data_trans->id_merchant, $accountNo, $updateStatus);
                }
                return $succes;
            }

            //partial 4
            if ($data_trans->bill == $partial4) {
                // if ($amount != $partial4) {
                //     Log::info("Invalid amount");
                //     return 1;
                // }
                if($amount < $partial4){
                    Log::info("The amount entered is less than the required partial payment amount.");
                    return $less_amount;
                }

                if($amount > $partial4){
                    Log::info("The amount entered exceeds the partial payment amount.");
                    return $exeed_amount;
                }
                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $amount, $accountNo, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                if ($updateData['bill'] == 0) {
                    $updateStatus = ['status' => 1];
                    $this->renewTransaction($data_trans->id_merchant, $accountNo, $updateStatus);
                }
                return $succes;
            }

            return $not_found;
        }

        if ($data_trans->payment_method == 'minimum' && $data_trans->value_type == 'percentage') {
            Log::info("UPDATE | Minimum percentage");
            $updateData = [
                'paid_date' => now(),
                'va_paid' => $accountNo,
                'last_paid' => $amount
            ];

            //Validasi bill trx kurang dari minimum maka pembayaran harus sama dengan bill trx
            $minimum = $data_trans->jumlah * $data_trans->minimum / 100;
            if ($data_trans->bill <= $minimum) {

                $amount = (int)$amount;
                $bill = (int)$data_trans->bill;

                if ($amount !== $bill) {
                    Log::info("Invalid amount. The amount does not match the bill.");
                    return $not_match_bill;
                }

                $updateData['bill'] = $data_trans->bill - $amount;
                $updateData['status'] = 1;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $accountNo, $amount, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                return $succes;
            } else if($amount == $data_trans->bill){
                //Langsung bayar lunas
                $updateData['bill'] = $data_trans->bill - $amount;
                $updateData['status'] = 1;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $accountNo, $amount, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                // $this->insertTransaction($insertData, $accountNo);
                return $succes;
            } else {
                //Validasi amount < minimum maka resp invalid amount
                if ($amount < $minimum) {
                    Log::info("Amount entered less than the minimum payment");
                    return $less_minimum;
                }

                if($amount > $data_trans->bill){
                    $updateData['status'] = 1;
                }

                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $accountNo, $amount, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                return $succes;
            }
        }

        if ($data_trans->payment_method == 'minimum' && $data_trans->value_type == 'flat') {
            Log::info("UPDATE | Minimum flat");
            $updateData = [
                'paid_date' => now(),
                'va_paid' => $accountNo,
                'last_paid' => $amount
            ];

            $minimum = $data_trans->minimum;
            if ($data_trans->bill <= $minimum) {
                $amount = (int)$amount;
                $bill = (int)$data_trans->bill;
                if ($amount !== $bill) {
                    Log::info("Invalid amount. The amount does not match the bill.");
                    return $not_match_bill;
                }
                $updateData['bill'] = $data_trans->bill - $amount;
                $updateData['status'] = 1;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $accountNo, $amount, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                return $succes;
            } else if($amount == $data_trans->bill){
                //Langsung bayar lunas
                $updateData['bill'] = $data_trans->bill - $amount;
                $updateData['status'] = 1;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $accountNo, $amount, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                // $this->insertTransaction($insertData, $accountNo);
                return $succes;
            } else {
                //Validasi amount < minimum maka resp invalid amount
                if ($amount < $minimum) {
                    Log::info("Amount entered less than the minimum payment");
                    return $less_minimum;
                }

                if($amount > $data_trans->bill){
                    $updateData['status'] = 1;
                }

                $updateData['bill'] = $data_trans->bill - $amount;
                $insertData = $this->prepareInsertData($data_trans, $data_trans->jumlah, $accountNo, $amount, $updateData['bill']);
                $this->renewTransaction($data_trans->id_merchant, $accountNo, $data_trans->bill);
                $this->insertTransaction($insertData, $accountNo);
                return $succes;
            }
        }

        return $not_found;
    }

    public function inquiryPartial($transaction)
    {
        $data = [];
        if ($transaction->payment_method == 'partial' && $transaction->value_type == 'percentage') {
            Log::info("Partial | Percentage");

            $jumlah = intval($transaction->jumlah);

            $lastThreeDigits = substr($jumlah, -3);

            // Ganti 3 angka terakhir dengan 000
            $jumlah = $jumlah - $lastThreeDigits;
            // $amount = $amount + 1000;

            $partial1 = 0;
            $partial2 = 0;
            $partial3 = 0;
            $partial4 = 0;

            if (!empty($transaction->partial1)) {
                $partial1 = $jumlah * $transaction->partial1 / 100;
            }

            if (!empty($transaction->partial2)) {
                $partial2 = $jumlah * $transaction->partial2 / 100;
            }

            if (!empty($transaction->partial3)) {
                $partial3 = $jumlah * $transaction->partial3 / 100;
            }

            if (!empty($transaction->partial4)) {
                $partial4 = $jumlah * $transaction->partial4 / 100;
            }

            if (!empty($partial4)) {
                $partial4 = $partial4 + $lastThreeDigits;
            } elseif (!empty($partial3)) {
                $partial3 = $partial3 + $lastThreeDigits;
            } elseif (!empty($partial2)) {
                $partial2 = $partial2 + $lastThreeDigits;
            } elseif (!empty($partial1)) {
                $partial1 = $partial1 + $lastThreeDigits;
            }

            //partial 1
            if (empty($transaction->last_paid)) {
                $data['partialPayment'] = $partial1;
            }

            //partial 2
            $total = $transaction->bill + $partial1;
            if ($transaction->jumlah == $total) {
                $data['partialPayment'] = $partial2;
            }

            //partial 3
            $compare_partial3 = $transaction->bill - $partial4;
            if ($compare_partial3 == $partial3) {
                $data['partialPayment'] = $partial3;
            }

            //partial 4
            if ($transaction->bill == $partial4) {
                $data['partialPayment'] = $partial4;
            }

            $data['responseMessage'] = "Pending";
        }

        if ($transaction->payment_method == 'partial' && $transaction->value_type == 'flat') {
            Log::info("Partial | Flat");
            $partial1 = 0;
            $partial2 = 0;
            $partial3 = 0;
            $partial4 = 0;

            if (!empty($transaction->partial1)) {
                $partial1 = $transaction->partial1;
            }

            if (!empty($transaction->partial2)) {
                $partial2 = $transaction->partial2;
            }

            if (!empty($transaction->partial3)) {
                $partial3 = $transaction->partial3;
            }

            if (!empty($transaction->partial4)) {
                $partial4 = $transaction->partial4;
            }

            //partial 1
            if (empty($transaction->last_paid)) {
                $data['partialPayment'] = intval($partial1);
            }

            //partial 2
            $total = $transaction->bill + $partial1;
            if ($transaction->jumlah == $total) {
                $data['partialPayment'] = intval($partial2);
            }

            //partial 3
            $compare_partial3 = $transaction->bill - $partial4;
            if ($compare_partial3 == $partial3) {
                $data['partialPayment'] = intval($partial3);
            }

            //partial 4
            if ($transaction->bill == $partial4) {
                $data['partialPayment'] = intval($partial4);
            }

            $data['responseMessage'] = "Pending";
        }

        if ($transaction->payment_method == 'minimum' && $transaction->value_type == 'percentage') {
            Log::info("Minimum | Percentage");

            $minimum = $transaction->jumlah * $transaction->minimum / 100;


            // if (empty($transaction->last_paid)) {
            //     // $data['minimumPayment'] = $minimum;
            //     $data['partialPayment'] = $minimum;
            // } 
            // else {
            //     // $data['minimumPayment'] = 0;
            //     $data['minimumPayment'] = 0;

            // }
            if ($transaction->bill < $minimum) {
                $data['partialPayment'] = $transaction->bill;
                $data['responseMessage'] = "Pending";
            } else {
                $data['partialPayment'] = $transaction->bill;
                $data['responseMessage'] = "Pending. Minimum payment: $minimum";
            }
        }

        if ($transaction->payment_method == 'minimum' && $transaction->value_type == 'flat') {
            Log::info("Minimum | Flat");

            $minimum = $transaction->minimum;

            // if (empty($transaction->last_paid)) {
            //     $data['partialPayment'] = intval($minimum);
            // } 
            // else {
            //     $data['minimumPayment'] = 0;
            // }

            if ($transaction->bill < $minimum) {
                $data['partialPayment'] = $transaction->bill;
                $data['responseMessage'] = "Pending";
            } else {
                $data['partialPayment'] = $transaction->bill;
                $data['responseMessage'] = "Pending. Minimum payment: $minimum";
            }
        }

        return $data;
    }

    // public function processDataInquiryPartial($trans)
    // {
    //     // 1. Memeriksa apakah data kosong atau null
    //     if (empty($trans)) {
    //         return null;
    //     }

    //     Log::info($trans);

    //     // 2. Melakukan pemrosesan data menggunakan loop for
    //     $data = [];
    //     foreach ($trans as $transaction) {
    //         Log::info($transaction);
    //         // 3. Memanggil function inquiryPartial untuk setiap data dalam loop
    //         $result = $this->inquiryPartial($transaction);
    //         // 4. Menambahkan hasil pemrosesan ke dalam array $data
    //         $data[] = $result;
    //     }

    //     return $data;
    // }
}
Leave a Comment