SET @requestdesk_module_id := (SELECT `id` FROM `ohrm_module` WHERE name = 'Request Desk');

SET @next_wf_order = (SELECT max(order_no)
                      FROM ohrm_workflow);


INSERT INTO `ohrm_workflow`(`id`,`name`, `label`, `available_to`, `default_available_to`, `module_id`,`order_no`) VALUES
('REQUEST_PROCESS_APPROVE','Request Approve Process Workflow','Request Approve Process Workflow',NULL,2,@requestdesk_module_id,@next_wf_order+1),
('REQUEST_PROCESS_ACTOR_APPROVER','Request Approve Actor Workflow','Request Approve Actor Workflow',1,2,@requestdesk_module_id,@next_wf_order+2);

-- Main Request entity workflow state machine
INSERT INTO ohrm_workflow_state_machine (workflow, state, role, action, resulting_state)
VALUES ('REQUEST_PROCESS_APPROVE', 'INITIAL', 'SYSTEM', 'INITIATE', 'IN PROGRESS'),
       ('REQUEST_PROCESS_APPROVE', 'IN PROGRESS', 'SYSTEM', 'COMPLETE APPROVAL', 'COMPLETED'),
       ('REQUEST_PROCESS_APPROVE', 'IN PROGRESS', 'SYSTEM', 'REJECT', 'INITIAL'),
       ('REQUEST_PROCESS_APPROVE', 'IN PROGRESS', 'SYSTEM', 'ROLLBACK', 'INITIAL'),
       ('REQUEST_PROCESS_APPROVE', 'INITIAL', 'SYSTEM', 'NON APPROVE SUBMIT', 'COMPLETED');

-- Request approval process workflow state machine
INSERT INTO `ohrm_workflow_state_machine`(`workflow`, `state`, `role`, `action`, `resulting_state`) VALUES
('REQUEST','Saved','Requester','SUBMIT','Submitted'),
('REQUEST_WITHOUT_APPROVE','Saved','Requester','SUBMIT','In Progress'),
('REQUEST','Saved','Requester','SAVE','Saved'),
('REQUEST_WITHOUT_APPROVE','Saved','Requester','SAVE','Saved'),

('REQUEST','Submitted','RequestApprover','APPROVE','In Progress'),
('REQUEST','Submitted','RequestApprover','REJECT','Rejected'),
('REQUEST','Submitted','Admin','APPROVE','In Progress'),
('REQUEST','Submitted','Admin','REJECT','Rejected'),

('REQUEST','Submitted','Requester','WITHDRAW','Withdrawn'),
('REQUEST','Submitted','Admin','WITHDRAW','Withdrawn'),
('REQUEST','Submitted','Requester','SAVE','Submitted'),
('REQUEST','Submitted','Admin','SAVE','Submitted'),

('REQUEST','In Progress','Requester','WITHDRAW','Withdrawn'),
('REQUEST_WITHOUT_APPROVE','In Progress','Requester','WITHDRAW','Withdrawn'),
('REQUEST','In Progress','Admin','WITHDRAW','Withdrawn'),
('REQUEST_WITHOUT_APPROVE','In Progress','Admin','WITHDRAW','Withdrawn'),
('REQUEST','In Progress','RequestApprover','APPROVE','In Progress'),
('REQUEST','In Progress','Admin','APPROVE','In Progress'),
('REQUEST','In Progress','RequestApprover','REJECT','Rejected'),
('REQUEST','In Progress','Admin','REJECT','Rejected'),
('REQUEST','In Progress','SYSTEM','APPROVE','Approved'),
('REQUEST','In Progress','SYSTEM','REJECT','Rejected'),
('REQUEST','In Progress','RequestOwner','SAVE','In Progress'),
('REQUEST','In Progress','Admin','SAVE','In Progress'),
('REQUEST_WITHOUT_APPROVE','In Progress','RequestOwner','RESOLVE','Completed'),
('REQUEST_WITHOUT_APPROVE','In Progress','Admin','RESOLVE','Completed'),
('REQUEST_WITHOUT_APPROVE','In Progress','RequestOwner','REQUEST INFO','Request Info Required'),
('REQUEST_WITHOUT_APPROVE','In Progress','Admin','REQUEST INFO','Request Info Required'),

('REQUEST','Approved','RequestOwner','RESOLVE','Completed'),
('REQUEST','Approved','Admin','RESOLVE','Completed'),
('REQUEST','Approved','Requester','WITHDRAW','Withdrawn'),
('REQUEST','Approved','Admin','WITHDRAW','Withdrawn'),
('REQUEST','Approved','RequestOwner','REQUEST INFO','Request Info Required'),
('REQUEST','Approved','Admin','REQUEST INFO','Request Info Required'),

('REQUEST','Request Info Required','Requester','SUBMIT','Approved'),
('REQUEST','Request Info Required','Admin','SUBMIT','Approved'),
('REQUEST_WITHOUT_APPROVE','Request Info Required','Requester','SUBMIT','In Progress'),
('REQUEST_WITHOUT_APPROVE','Request Info Required','Admin','SUBMIT','In Progress'),
('REQUEST','Request Info Required','Requester','SAVE','Request Info Required'),
('REQUEST','Request Info Required','Admin','SAVE','Request Info Required'),
('REQUEST_WITHOUT_APPROVE','Request Info Required','Requester','SAVE','Request Info Required'),
('REQUEST_WITHOUT_APPROVE','Request Info Required','Admin','SAVE','Request Info Required'),

('REQUEST','Completed','Admin','REOPEN','Approved'),
('REQUEST_WITHOUT_APPROVE','Completed','Admin','REOPEN','In Progress'),
('REQUEST','Completed','RequestOwner','REOPEN','Approved'),
('REQUEST_WITHOUT_APPROVE','Completed','RequestOwner','REOPEN','In Progress'),

('REQUEST','Any State','AnyRole','Add Comment','Intial State'),
('REQUEST','Any State','AnyRole','Edit Comment','Intial State');

-- Request approval process actor workflow state machine
INSERT INTO ohrm_workflow_state_machine (workflow, state, role, action, resulting_state)
VALUES ('REQUEST_PROCESS_ACTOR_APPROVER', 'INITIAL', 'SYSTEM', 'INITIATE', 'INITIATED'),
       ('REQUEST_PROCESS_ACTOR_APPROVER', 'INITIATED', 'RequestApprover', 'APPROVE', 'APPROVED'),
       ('REQUEST_PROCESS_ACTOR_APPROVER', 'INITIATED', 'Admin', 'APPROVE', 'APPROVED'),
       ('REQUEST_PROCESS_ACTOR_APPROVER', 'INITIATED', 'RequestApprover', 'REJECT', 'INITIAL'),
       ('REQUEST_PROCESS_ACTOR_APPROVER', 'INITIATED', 'Admin', 'REJECT', 'INITIAL'),
       ('REQUEST_PROCESS_ACTOR_APPROVER', 'INITIATED', 'SYSTEM', 'ROLLBACK', 'INITIAL');

INSERT INTO ohrm_advanced_workflow_entity (name, workflow_id, description, options)
VALUES ('Request', 'REQUEST', 'Support for Approve processes for Request entity', '');

INSERT INTO ohrm_advanced_workflow_entity_status (entity, status_id, name, description, `sequence`, options)
VALUES ('Request', 1, 'Saved', 'Request is saved', 1, ''),
       ('Request', 2, 'Submitted', 'Request is submitted', 2, ''),
       ('Request', 3, 'Withdrawn', 'Request is withdrawn.', 3, ''),
       ('Request', 4, 'Request Info Required', 'More information required.', 4, ''),
       ('Request', 5, 'In Progress', 'Request is in progress.', 5, ''),
       ('Request', 6, 'Approved', 'Request is approved', 6, ''),
       ('Request', 7, 'Rejected', 'Request is rejected', 7, ''),
       ('Request', 8, 'Completed', 'Request is completed', 8, '');

INSERT INTO ohrm_advanced_workflow_entity_process (entity, name, workflow_id, description, options)
VALUES ('Request', 'Approve', 'REQUEST_PROCESS_APPROVE', 'Approve process for Request entity', '');

SET @approve_process_id := (SELECT id
                            FROM ohrm_advanced_workflow_entity_process
                            WHERE entity = 'Request'
                              AND name = 'Approve');

INSERT INTO ohrm_advanced_workflow_entity_process_status (entity_process_id, status_id, name, description, `sequence`, options)
VALUES (@approve_process_id, 1, 'INITIAL', 'Initial state - before request submitted', 1, ''),
       (@approve_process_id, 2, 'INITIATED', 'Request created. No approver has started approving', 2, ''),
       (@approve_process_id, 3, 'IN PROGRESS', 'Request is under approval process. At least one approver has approved', 3, ''),
       (@approve_process_id, 4, 'COMPLETED', 'Request is approved by all the approvers', 4, '');

INSERT INTO ohrm_advanced_workflow_entity_process_actor_type (entity_process_id, name, user_role, workflow_id, processor_class, description)
VALUES (@approve_process_id, 'RequestApprover', 'RequestApprover', 'REQUEST_PROCESS_ACTOR_APPROVER', 'AdvancedWorkflowEmployeeActorTypeToEmployeeMapper',
        'Request approver employees');

SET @approver_actor_type_id := (SELECT id
                                FROM ohrm_advanced_workflow_entity_process_actor_type
                                WHERE entity_process_id = @approve_process_id
                                  AND name = 'RequestApprover');

INSERT INTO ohrm_advanced_workflow_entity_process_actor_type_status (actor_type_id, status_id, name, description, `sequence`, options)
VALUES (@approver_actor_type_id, 1, 'INITIAL', 'Initial state - Cannot approve yet.', 1, ''),
       (@approver_actor_type_id, 2, 'INITIATED', 'Approval process is enabled. All reviewers have completed reviewing', 2, ''),
       (@approver_actor_type_id, 3, 'APPROVED', 'Approved', 3, '');


