<?php


$credittotal = 0.00;
$credittotal += $cr_amount;
$additionaldata = array();
$additionaldata = ['id', 'ledger_name', 'ledger_parent_group', 'ledger_main_ledgerhead'];

$ledger_detail = GetBasicLedgerNameDetail($specific_db_conn, $ledger_name, $workspace_id, $company_id, $branch_id, $book_id, $additionaldata);

$transaction_record_rows[] = array(
    'ledgerid' => $ledger_detail[0]['id'], 'relative_amount' => - (floatval($credittotal)),
    'ledger_main_ledgerhead' => $ledger_detail[3]['ledger_main_ledgerhead'], 'ledger_parent_group' => $ledger_detail[2]['ledger_parent_group'],
    'amount' => abs(floatval($credittotal)), 'balancetype' => 'cr', 'instrument_details' => null, 'denominations' => null
);

validateTransactionTypeRecords($transactiontype, 'cr', $ledger_detail[3]['ledger_main_ledgerhead']);

$debittotal = 0.00;
$debittotal += $dr_amount;

$ledger_detail = GetBasicLedgerNameDetail($specific_db_conn, $ledger_name, $workspace_id, $company_id, $branch_id, $book_id, $additionaldata);

$transaction_record_rows[] = array(
    'ledgerid' => $ledger_detail[0]['id'], 'relative_amount' => - (floatval($credittotal)),
    'ledger_main_ledgerhead' => $ledger_detail[3]['ledger_main_ledgerhead'], 'ledger_parent_group' => $ledger_detail[2]['ledger_parent_group'],
    'amount' => abs(floatval($credittotal)), 'balancetype' => 'dr', 'instrument_details' => null, 'denominations' => null
);
// print_r($transaction_record_rows);die();
validateTransactionTypeRecords($transactiontype, 'dr', $ledger_detail[3]['ledger_main_ledgerhead']);
// echo $dr_amount;
// echo $credittotal;
// echo '<pre>';
// echo $debittotal;
if ($credittotal != $debittotal) {

    errorreturn('Credit and Debit total do not match.');
}


try {

    $unique_id =  InsertTransactionData($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $transactiontype, $transaction_number, $transaction_date, $username, $filesattached, $last_modified_by, $narration, $userid, $reference_number, $dispatch_id, $billing_address, $shipping_address);


    //prepare query for transaction record insert
    $sql = "INSERT INTO act_transactionrecord(workspace_id,company_id,branch_id,book_id,unique_id,ledgerid,main_ledgerhead,parent_ledgerhead,amount,balancetype,date)
                               VALUES(?,?,?,?,?,?,?,?,?,?,?)";
    $insert_into_transactionrecord =  $specific_db_conn->prepare($sql);


    foreach ($transaction_record_rows as $row1) {

        $ledgerid = $row1['ledgerid'];
        $main_ledgerhead = $row1['ledger_main_ledgerhead'];
        $parent_ledgerhead = $row1['ledger_parent_group'];
        $amount = $row1['amount'];
        $relative_amount = $row1['relative_amount'];
        $balancetype = $row1['balancetype'];

        $insert_into_transactionrecord->execute(array($workspace_id, $company_id, $branch_id, $book_id, $unique_id, $ledgerid, $main_ledgerhead, $parent_ledgerhead, $amount, $balancetype, $transaction_date));

        // use update ledgerbalance here

        UpdateLedgerBalance($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $ledgerid, $relative_amount, $transaction_date, $main_ledgerhead, $parent_ledgerhead);
    }

    $transactionrecord_id = $conn->lastInsertId();

    successreturn(array('unique_id' => $unique_id, 'successmsg' => "Transaction saved successfully."));
} catch (Exception $e) {
    echo "Error : " . $e->getMessage();
}
