text: -------------------------------------------
  -- Create partition before adding transaction
  -------------------------------------------
  
  -------------------------------------------
  -- Generated by: Database Migration
  -- Developer: KeenDev team
  -- Website: https://www.keendev.team
  -- Github: https://github.com/keendev-team
  -------------------------------------------
  
  create or replace function <%- migration.schemaName %>.<%- databaseItemsPrefix %>insert_transaction (
  	account char,
  	amount decimal,
  	code char default null,
  	datetime timestamp default null,
  	description varchar default null,
  	meta jsonb default null)
  
  	returns <%- migration.schemaName %>.<%- databaseItemsPrefix %>transaction_result
  	language plpgsql
  	AS $$
  
  	DECLARE
  		current_period int;
  		transaction_code char(8);
  		account_balance decimal;
  		transaction_time timestamp;
  
  		output <%- migration.schemaName %>.<%- databaseItemsPrefix %>transaction_result;
  
  	BEGIN
  		SELECT <%- migration.schemaName %>.<%- databaseItemsPrefix %>random_string(8) INTO transaction_code;
  		transaction_code = COALESCE(code, transaction_code);
  
  		transaction_time = COALESCE(datetime, now());
  		SELECT <%- migration.schemaName %>.<%- databaseItemsPrefix %>current_period(transaction_time) INTO current_period;
  
          -- Create current period partition
          PERFORM <%- migration.schemaName %>.<%- databaseItemsPrefix %>create_transaction_period(current_period);
  
  		SELECT <%- migration.schemaName %>.<%- databaseItemsPrefix %>get_balance(account) INTO account_balance;
  
  		insert into <%- migration.schemaName %>.<%- databaseItemsPrefix %>transaction
  			(period, timestamp, code, account_number, amount, balance, description, meta)
  		VALUES
  			(current_period, transaction_time, transaction_code, account, amount, account_balance + amount, description, meta);
  
  		output.code = transaction_code;
  		output.balance = account_balance + amount;
  
  		return output;
  	end;
  
  	$$;
  