CREATE SEQUENCE UPDATE_CNT_SEQ AS INT START WITH 1 ;

Update supplier_roles set update_cnt = NEXT VALUE FOR UPDATE_CNT_SEQ;

DROP SEQUENCE  UPDATE_CNT_SEQ RESTRICT;


CREATE SEQUENCE UPDATE_CNT_SEQ AS INT START WITH 1 ;
UPDATE contacts set update_cnt = ((SELECT coalesce(max(update_cnt),0) FROM contacts) + NEXT VALUE FOR UPDATE_CNT_SEQ)
WHERE contact_no IN (SELECT contact_no FROM supplier_roles);

DROP SEQUENCE  UPDATE_CNT_SEQ RESTRICT;



Update customer_treatments set update_cnt = treatment_id;


INSERT INTO pos_permissions (tenant_no,company_no,permission_cd,permission_nm,invisible,permission_group_cd,permission_desc,order_no)
SELECT tenant_no,company_no,'P_CASH_IN_8','Bonsperre aufheben',0,'P_CASH_IN_GROUP','',8
FROM companies c
WHERE NOT EXISTS(
    SELECT permission_cd
    FROM pos_permissions 
    WHERE permission_cd = 'P_CASH_IN_8'
    AND company_no = c.company_no

);

INSERT INTO pos_permissions (tenant_no,company_no,permission_cd,permission_nm,invisible,permission_group_cd,permission_desc,order_no)
SELECT tenant_no,company_no,'P_CASH_IN_9','Ausgaben buchen',0,'P_CASH_IN_GROUP','',9
FROM companies c
WHERE NOT EXISTS(
    SELECT permission_cd
    FROM pos_permissions 
    WHERE permission_cd = 'P_CASH_IN_9'
    AND company_no = c.company_no

);


INSERT INTO pos_permissions (tenant_no,company_no,permission_cd,permission_nm,invisible,permission_group_cd,permission_desc,order_no)
SELECT tenant_no,company_no,'P_CASH_IN_10','Kasse zählen',0,'P_CASH_IN_GROUP','',10
FROM companies c
WHERE NOT EXISTS(
    SELECT permission_cd
    FROM pos_permissions 
    WHERE permission_cd = 'P_CASH_IN_10'
    AND company_no = c.company_no

);



INSERT INTO employee_group_permissions (tenant_no,company_no,employee_group_cd,permission_cd)
SELECT tenant_no,company_no,'0','P_CASH_IN_8'
FROM companies c
WHERE EXISTS (
    SELECT * from employee_groups
    where employee_group_cd = '0' and employee_group_nm = 'Geschäftsführer'
    and company_no = c.company_no
);


INSERT INTO employee_group_permissions (tenant_no,company_no,employee_group_cd,permission_cd)
SELECT tenant_no,company_no,'0','P_CASH_IN_9'
FROM companies c
WHERE EXISTS (
    SELECT * from employee_groups
    where employee_group_cd = '0' and employee_group_nm = 'Geschäftsführer'
    and company_no = c.company_no
);



INSERT INTO employee_group_permissions (tenant_no,company_no,employee_group_cd,permission_cd)
SELECT tenant_no,company_no,'0','P_CASH_IN_10'
FROM companies c
WHERE EXISTS (
    SELECT * from employee_groups
    where employee_group_cd = '0' and employee_group_nm = 'Geschäftsführer'
    and company_no = c.company_no
);

