<?php
function InsertTransactionData($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $transactiontype, $date, $username, $filesattached, $last_modified_by, $narration, $userid, $reference_number, $dispatch_id, $billing_address, $shipping_address, $e_way_bill, $prefix_financial_year_bool, $financial_year_startdate, $transaction_numbering_system, $unique_id = null, $additional_prefix = '')
{

    if (in_array($transactiontype, $transaction_numbering_system['manual'])) {
        // check for existing transaction number
        $sql = "Select count(*) as total from act_transactiondata where unique_id = ? and workspace_id = ? and company_id = ? and branch_id = ? and book_id = ?";
        $check_transaction_uniqueid = $specific_db_conn->prepare($sql);
        $check_transaction_uniqueid->execute(array($unique_id, $workspace_id, $company_id, $branch_id, $book_id));
        $result_uniqueid = $check_transaction_uniqueid->fetchAll();
        if ($result_uniqueid[0]['total'] > 0) {
            $specific_db_conn->rollBack();
            errorreturn('Transaction number already exists.');
        }
        $unique_id = $unique_id;
        $financial_year = null;
        $transaction_type_suffix = null;
    } else if (in_array($transactiontype, $transaction_numbering_system['automatic'])) {

        if ($prefix_financial_year_bool == 1) {
            $financial_year = getFinancialYear($date, $financial_year_startdate);
        } else {
            $financial_year = null;
        }
        $transaction_type_suffix = extractFirstWords($transactiontype);
        if ($transaction_type_suffix == false or $transaction_type_suffix == -1) {
            $specific_db_conn->rollBack();
            errorreturn('Transaction type could not be identified.');
        }

        $sql_get_max_number = "SELECT MAX(CAST(unique_id AS UNSIGNED)) as unique_id from act_transactiondata where workspace_id = ? and company_id = ? and branch_id = ? and book_id = ? and transactiontype = ?";
        $array_params = [$workspace_id, $company_id, $branch_id, $book_id, $transactiontype];
        if ($financial_year != null) {
            $sql_get_max_number .=  "and financial_year = ?";
            $array_params[] = $financial_year;
        }
        $get_max_number = $specific_db_conn->prepare($sql_get_max_number);
        $get_max_number->execute($array_params);

        $get_max_number = $get_max_number->fetchAll();
        if (count($get_max_number) != 1) {
            $max_transaction_number = 0;
        } else {
            $max_transaction_number = intval($get_max_number[0]['unique_id']);
        }
        $unique_id = $max_transaction_number + 1;
    }

    $last_updated = null;
    $sql = "INSERT INTO act_transactiondata(workspace_id,company_id,branch_id,book_id, financial_year, unique_id, transaction_type_suffix, transactiontype,date,entry_by,filesattached,notified_ledger,time,last_updated,last_modified_by,narration,reference_number,dispatch_id,billing_address,shipping_address,transactionLock,lockedBy,e_way_bill) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    $stmt =  $specific_db_conn->prepare($sql);
    if (is_array($billing_address) and is_array($shipping_address) and isset($billing_address['id']) and isset($shipping_address['id'])) {
        $billing_address = $billing_address['id'];
        $shipping_address = $shipping_address['id'];
    } else {
        $billing_address = null;
        $shipping_address = null;
    }
    $stmt->execute(array($workspace_id, $company_id, $branch_id, $book_id, $financial_year, $unique_id, $transaction_type_suffix, $transactiontype, $date, $username, $filesattached, '1', time(), $last_updated, $last_modified_by, $narration, $reference_number, $dispatch_id, $billing_address, $shipping_address, '0', null, $e_way_bill));

    $transaction_id = $specific_db_conn->lastInsertId();

    if ($transaction_numbering_system == 'automatic') {
        if (!empty($additional_prefix) or !empty($financial_year)) {
            $unique_id = $additional_prefix . '' . $financial_year . '/' . $unique_id . '' . $transaction_type_suffix;
        } else {
            $unique_id = $unique_id . '' . $transaction_type_suffix;
        }
    }


    //return $unique_id;
    $result = array("transaction_id" => $transaction_id, "unique_id" => $unique_id);
    return $result;
}
