<?php
//insert userupload transaction function

function awsS3UserUploadTransactionInsert($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $userid, $created_by, $size, $file_extension, $filesattached, $directoryPathMain, $transaction_id, $originalFileName)
{


    if ($filesattached == null) {
    } else {

        // Generate the image name
        $image_name = $transaction_id . "." . $file_extension;
        $keyName = $directoryPathMain . $image_name;

        $party_1 =  null;
        $party_2 =  null;
        $party_3 =  null;
        $date_of_document = null;
        $category_file_upload = 'transaction_attachment';
        $reminder_date =  null;
        $last_modified_by = null;
        $termination_document_date =  null;
        //$tags =  null;
        $tags = null;
        $share_mode =  'public';
        $share_with =  null;

        // run an updatequery that just updates the path of file in db at the specific row
        $sql = "UPDATE  act_transactiondata SET filesattached = ? WHERE id = ? and workspace_id = ? and company_id = ? and branch_id= ? and book_id = ?";
        $specific_db_conn->prepare($sql)->execute(array($keyName, $transaction_id, $workspace_id, $company_id, $branch_id, $book_id));

        awsS3UserUploadInsert(
            $GLOBALS['s3_finware_bucket'],
            $GLOBALS['finware_bucketName'],
            $specific_db_conn,
            $workspace_id,
            $company_id,
            $branch_id,
            $book_id,
            $userid,
            $filesattached,
            $originalFileName,
            $keyName,
            $size,
            strtolower($file_extension),
            $tags,
            $party_1,
            $party_2,
            $party_3,
            $date_of_document,
            $category_file_upload,
            $transaction_id,
            $reminder_date,
            $created_by,
            $last_modified_by,
            $termination_document_date,
            $share_mode,
            $share_with,
            1,
            1
        );
    }
}

function awsS3UserUploadTransactionUpdate($specific_db_conn, $userid, $workspace_id, $company_id, $branch_id, $book_id, $size, $file_extension, $filesattached, $directoryPathMain, $existing_filesattached, $transaction_id, $originalFileName)
{
    //echo $originalFileName;
    $image_name = $transaction_id . "." . $file_extension;
    $keyName = $directoryPathMain . "" . $image_name;
    $party_1 =  null;
    $party_2 =  null;
    $party_3 =  null;
    $date_of_document = null;
    $category_file_upload = 'transaction_attachment';
    $reminder_date =  null;
    $last_modified_by = null;
    $termination_document_date =  null;
    //$tags =  null;
    $tags = null;
    $share_mode =  'public';
    $share_with =  null;
    // get user upload id

    $sql_get_userupload_id = "Select id from user_upload where workspace_id = ? and path = ? and category = ? and category_unique_id = ?";
    $get_useruploadid = $specific_db_conn->prepare($sql_get_userupload_id);
    $get_useruploadid->execute(array($workspace_id, $existing_filesattached, 'transaction_attachment', $transaction_id));
    $existing_upload_id = $get_useruploadid->fetchAll();

    if (count($existing_upload_id) == 1) {
        awsS3UserUploadUpdate(
            $GLOBALS['s3_finware_bucket'],
            $GLOBALS['finware_bucketName'],
            $specific_db_conn,
            $workspace_id,
            $userid,
            $filesattached,
            $originalFileName,
            $keyName,
            $size,
            strtolower($file_extension),
            $tags,
            $party_1,
            $party_2,
            $party_3,
            $date_of_document,
            $category_file_upload,
            $reminder_date,
            $last_modified_by,
            $termination_document_date,
            $share_mode,
            $share_with,
            $existing_upload_id[0]['id'],
            1,
            1
        );
        // run an update query that just updates the path of file in db at the specific row
        $sql = "UPDATE act_transactiondata SET filesattached = ? WHERE id = ? and workspace_id = ? and company_id = ? and branch_id= ? and book_id = ?";
        $specific_db_conn->prepare($sql)->execute(array($keyName, $transaction_id, $workspace_id, $company_id, $branch_id, $book_id));
    } else {
        // do nothing
    }
}
