Skip to content

Instantly share code, notes, and snippets.

@akhdaniel
Last active April 5, 2021 09:05
Show Gist options
  • Save akhdaniel/79218d4c078a8bbde5ba6d94871b405b to your computer and use it in GitHub Desktop.
Save akhdaniel/79218d4c078a8bbde5ba6d94871b405b to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION public.vit_duplicate_account_journal(v_account_journal_id INTEGER)
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
DECLARE
v_account_journal record;
v_company_id INTEGER;
v_sequence_id INTEGER;
v_rel RECORD;
BEGIN
FOR v_rel IN SELECT * FROM account_journal_res_branch_rel where account_journal_id=v_account_journal_id
LOOP
-- select account_journal
SELECT * FROM account_journal WHERE id = v_rel.account_journal_id INTO v_account_journal;
-- select company_id
select id from res_company where code=(select substring(name,1,5) from res_branch where id=v_rel.res_branch_id) INTO v_company_id;
-- insert sequence
INSERT INTO ir_sequence (name, implementation, company_id, padding, step)
VALUES ('SEQ '|| rel.account_journal_id::TEXT, 'standard', v_company_id, 4, 1) INTO v_sequence_id;
-- insert into acocunt journal
INSERT into account_journal(name, type, code, company_id, sequence_id)
VALUES(
v_account_journal.name,
v_account_journal.type,
v_account_journal.code,
v_company_id,
v_sequence_id);
END LOOP;
END;
$BODY$;
SELECT vit_duplicate_account_journal(2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment