Пример #1
0
 /**
  * 
  * @param type $requisitionform
  * @param type $data
  * @return type
  */
 public function save($requisitionform, $data)
 {
     $ipreporting_id = "";
     $technicalskill_id = "";
     $nontechnicalskill_id = "";
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
         $loginuserGroup = $auth->getStorage()->read()->group_id;
     }
     $requi_model = new Default_Model_Requisition();
     $user_model = new Default_Model_Usermanagement();
     $jobtitleModel = new Default_Model_Jobtitles();
     $appr_mail = '';
     $appr_per = '';
     if ($requisitionform->isValid($this->_request->getPost())) {
         $trDb = Zend_Db_Table::getDefaultAdapter();
         // starting transaction
         $trDb->beginTransaction();
         try {
             $id = (int) $this->_getParam('id', null);
             $requisition_code = $this->_getParam('requisition_code', null);
             $onboard_date = $this->_getParam('onboard_date', null);
             $business_unit = $this->_getParam('business_unit', null);
             $department = $this->_getParam('department', null);
             $jobtitle = $this->_getParam('jobtitle', null);
             $position_id = $this->_getParam('position_id', null);
             $reporting_id = $this->_getParam('reporting_id', null);
             $req_no_positions = $this->_getParam('req_no_positions', null);
             $jobdescription = $this->_getParam('jobdescription', null);
             $req_skills = $this->_getParam('req_skills', null);
             $req_qualification = $this->_getParam('req_qualification', null);
             $req_exp_years = $this->_getParam('req_exp_years', null);
             $emp_type = $this->_getParam('emp_type', null);
             $req_priority = $this->_getParam('req_priority', null);
             $additional_info = $this->_getParam('additional_info', null);
             $req_status = $this->_getParam('req_status', null);
             $approver1 = $this->_getParam('approver1', null);
             $approver2 = $this->_getParam('approver2', null);
             $approver3 = $this->_getParam('approver3', null);
             $aflag = $this->_getParam('aflag', null);
             $edit_flag = $this->_getParam('edit_flag', null);
             $edit_order = $this->_getParam('edit_order', null);
             $requ_jd = $this->_getParam('requ_jd', null);
             $billable = $this->_getParam('billable', null);
             $billablefor = $this->_getParam('billablefor', null);
             $billable_empname = $this->_getParam('billable_empname', null);
             $interviewroundscount = $this->_getParam('interviewroundscount', null);
             $technology = $this->_getParam('technology', null);
             try {
                 $ipbusiness_unit = 1;
                 $ipdepartment = 1;
                 $ipreporting_id = $this->_getParam('ipreporting_id', null);
                 $technicalskill_id = $this->_getParam('technicalskill_id', null);
                 $nontechnicalskill_id = $this->_getParam('nontechnicalskill_id', null);
             } catch (Exception $abc) {
             }
             $aorder = $this->_getParam('aorder', null);
             //1= approver1,2=approver2,3=approver3
             if ($aflag != '' && $aflag == 'approver') {
                 if ($req_status == 3) {
                     //for rejected
                     $data = array('modifiedby' => trim($loginUserId), 'modifiedon' => gmdate("Y-m-d H:i:s"), 'appstatus' . $aorder => $req_status, 'req_status' => $req_status);
                 } else {
                     //for approved
                     if ($aorder == 1) {
                         if ($approver2 != '') {
                             $data = array('appstatus1' => $req_status, 'req_status' => $req_status);
                             $appr_mail = $approver1;
                             $appr_per = $approver1;
                         } else {
                             $data = array('appstatus1' => $req_status, 'req_status' => $req_status);
                             $appr_mail = 'approved';
                         }
                     } else {
                         if ($aorder == 2) {
                             if ($approver3 != '') {
                                 $data = array('appstatus2' => $req_status, 'req_status' => $req_status);
                                 $appr_mail = $approver2;
                                 $appr_per = $approver2;
                             } else {
                                 $data = array('appstatus2' => $req_status, 'req_status' => $req_status);
                                 $appr_mail = 'approved';
                             }
                         }
                     }
                     $data['modifiedby'] = trim($loginUserId);
                     $data['modifiedon'] = gmdate("Y-m-d H:i:s");
                 }
             } else {
                 $data = array('requisition_code' => trim($requisition_code), 'onboard_date' => sapp_Global::change_date(trim($onboard_date), 'database'), 'position_id' => trim($position_id), 'reporting_id' => trim($reporting_id), 'businessunit_id' => trim($business_unit), 'department_id' => trim($department), 'req_no_positions' => trim($req_no_positions), 'jobdescription' => trim($jobdescription), 'jobtitle' => trim($jobtitle), 'req_skills' => trim($req_skills), 'req_qualification' => trim($req_qualification), 'req_exp_years' => trim($req_exp_years), 'emp_type' => trim($emp_type), 'req_priority' => trim($req_priority), 'additional_info' => trim($additional_info), 'billable' => trim($billable), 'interviewroundscount' => trim($interviewroundscount), 'technology' => trim($technology), 'approver1' => $approver1, 'approver2' => $approver2 == '' ? null : $approver2, 'approver3' => $approver3 == '' ? null : $approver3, 'req_status' => 'Initiated', 'appstatus1' => 'Initiated', 'appstatus2' => null, 'appstatus3' => null, 'isactive' => 1, 'createdby' => trim($loginUserId), 'modifiedby' => trim($loginUserId), 'createdon' => gmdate("Y-m-d H:i:s"), 'modifiedon' => gmdate("Y-m-d H:i:s"), 'upload_jd' => trim($requ_jd), 'billable_for' => trim($billablefor), 'billable_empname' => trim($billable_empname));
                 if ($loginuserGroup == MANAGER_GROUP) {
                     $data['reporting_id'] = $loginUserId;
                 }
                 if ($edit_flag != '' && $edit_flag == 'yes') {
                     if ($edit_order == 1) {
                         $data = array('modifiedby' => trim($loginUserId), 'modifiedon' => gmdate("Y-m-d H:i:s"), 'approver2' => $approver2 == '' ? null : $approver2, 'approver3' => $approver3 == '' ? null : $approver3, 'appstatus2' => 'Initiated');
                         if ($approver2 == '') {
                             $data['appstatus2'] = null;
                         } else {
                             $appr_per = $edit_order;
                             $appr_mail = $approver2;
                         }
                     }
                     if ($edit_order == 2) {
                         $data = array('modifiedby' => trim($loginUserId), 'modifiedon' => gmdate("Y-m-d H:i:s"), 'approver3' => $approver3 == '' ? null : $approver3, 'appstatus3' => 'Initiated');
                         if ($approver3 == '') {
                             $data['appstatus3'] = null;
                         } else {
                             $appr_per = $edit_order;
                             $appr_mail = $approver3;
                         }
                     }
                 }
             }
             $where = "";
             $actionflag = 1;
             //for mailing
             if ($id == '') {
                 //for requisition code
                 $identity_code_model = new Default_Model_Identitycodes();
                 $identity_codes = $identity_code_model->getIdentitycodesRecord();
                 $irequistion_code = isset($identity_codes[0]) ? $identity_codes[0]['requisition_code'] : "";
                 if ($irequistion_code != '') {
                     $req_id = $requi_model->getMaxReqCode($irequistion_code . "/");
                 } else {
                     $req_id = '';
                 }
                 $data['requisition_code'] = $req_id;
                 //end of requisition code
                 $report_person_data = $user_model->getUserDataById($data['reporting_id']);
                 $approver1_person_data = $user_model->getUserDataById($approver1);
                 $Raisedby_person_data = $user_model->getUserDataById($data['createdby']);
                 $jobttlArr = $jobtitleModel->getsingleJobTitleData(trim($jobtitle));
                 if (!empty($jobttlArr) && $jobttlArr != 'norows') {
                     $jobtitlename = ' - ' . $jobttlArr[0]['jobtitlename'];
                 } else {
                     $jobtitlename = '';
                 }
                 //$hrgroupemail = $requi_model->getgroupemailid("REQ_HR", $business_unit);
                 //if (isset($hrgroupemail)) {
                 //HR Group
                 $mail_arr[0]['name'] = 'HR';
                 $mail_arr[0]['email'] = constant('REQ_HR_0');
                 $mail_arr[0]['type'] = 'HR';
                 //}
                 //$mgmtgroupemail = $requi_model->getgroupemailid("REQ_MGMT", $business_unit);
                 //if (isset($mgmtgroupemail)) {
                 //HR Group
                 $mail_arr[1]['name'] = 'Management';
                 $mail_arr[1]['email'] = constant('REQ_MGMT_0');
                 $mail_arr[1]['type'] = 'Management';
                 //}
                 $mail_arr[2]['name'] = $Raisedby_person_data['userfullname'];
                 $mail_arr[2]['email'] = $Raisedby_person_data['emailaddress'];
                 $mail_arr[2]['type'] = 'RaiseNew';
                 $mail_arr[3]['name'] = $approver1_person_data['userfullname'];
                 $mail_arr[3]['email'] = $approver1_person_data['emailaddress'];
                 $mail_arr[3]['type'] = 'Approver';
                 $appr_str = "";
                 $appr_str = $approver1_person_data['userfullname'];
                 $req_selected_skills = sprintf("Technical Skills (%s), Non-Technical Skills (%s).", $requi_model->getSkillNamesByIds(implode(",", $technicalskill_id), "Technical"), $requi_model->getSkillNamesByIds(implode(",", $nontechnicalskill_id), "Non-Technical"));
                 for ($ii = 0; $ii < count($mail_arr); $ii++) {
                     $base_url = 'http://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getBaseUrl();
                     $view = $this->getHelper('ViewRenderer')->view;
                     $this->view->emp_name = $mail_arr[$ii]['name'];
                     $this->view->base_url = $base_url;
                     $this->view->type = $mail_arr[$ii]['type'];
                     $this->view->jobtitle = $jobtitlename;
                     $this->view->requisition_code = $requisition_code;
                     $this->view->approver_str = $appr_str;
                     $this->view->raised_name = $Raisedby_person_data['userfullname'];
                     if ($mail_arr[$ii]['type'] == "RaiseNew" || $mail_arr[$ii]['type'] == 'Raise' || $mail_arr[$ii]['type'] == 'RaiseNew' || $mail_arr[$ii]['type'] == 'HR' || $mail_arr[$ii]['type'] == 'Management' || $mail_arr[$ii]['type'] == 'Approver' || $mail_arr[$ii]['type'] == 'ReportingManager') {
                         //values for new email template
                         $bu_dept = $requi_model->getBusinessUnitDepartmentName(trim($department));
                         $this->view->department_name = $bu_dept['DepartmentName'];
                         $this->view->businessunit_name = $bu_dept['BusinessUnitName'];
                         $this->view->noofopenings = trim($req_no_positions);
                         $this->view->position_name = trim($jobtitlename);
                         $this->view->skills = trim($req_selected_skills);
                         $this->view->application_name = APPLICATION_NAME;
                     }
                     $text = $view->render('mailtemplates/requisition.phtml');
                     $options['subject'] = APPLICATION_NAME . ': ' . $requisition_code . '-Requisition for approval';
                     $options['header'] = 'Requisition Status';
                     $options['toEmail'] = $mail_arr[$ii]['email'];
                     $options['toName'] = $mail_arr[$ii]['name'];
                     $options['message'] = $text;
                     //var_dump($text);
                     //$options['cron'] = 'yes';
                     if ($options['toEmail'] != '') {
                         if ($mail_arr[$ii]['type'] == "RaiseNew" || $mail_arr[$ii]['type'] == 'Raise' || $mail_arr[$ii]['type'] == 'RaiseNew' || $mail_arr[$ii]['type'] == 'HR' || $mail_arr[$ii]['type'] == 'Management' || $mail_arr[$ii]['type'] == 'Approver' || $mail_arr[$ii]['type'] == 'ReportingManager') {
                             sapp_Global::_sendEmail($options, "New", REQUISITIONMODULESENDEMAIL);
                         } else {
                             sapp_Global::_sendEmail($options, '', REQUISITIONMODULESENDEMAIL);
                         }
                     }
                 }
             }
             if ($id != '') {
                 unset($data['createdby']);
                 unset($data['createdon']);
                 unset($data['isactive']);
                 $where = "id = " . $id;
                 $tableid = $id;
                 $actionflag = 2;
             }
             $result = $requi_model->SaveorUpdateRequisitionData($data, $where);
             //log approvals by approver
             if ($loginUserId == $approver1) {
                 $requisition_data1 = $requi_model->getRequisitionDataById($id);
                 $result1 = $requi_model->insert_requisition_approvalLog($id, $requisition_data1['requisition_code'], $req_status, $loginUserId, gmdate("Y-m-d H:i:s"));
             }
             /*
              * To Insert Interview Panel & Skill details when a new Requisition created
              */
             $currReqIDinDB = $id;
             if ($currReqIDinDB <= 0) {
                 $currReqIDinDB = $result;
             }
             $requisition_data2 = $requi_model->getRequisitionDataById($currReqIDinDB);
             $result1 = $requi_model->insert_requisition_interview_panel($requisition_data2['requisition_code'], $ipbusiness_unit, $ipdepartment, $ipreporting_id, $requ_jd);
             $result2 = $requi_model->insert_requisition_skills($requisition_data2['requisition_code'], $technicalskill_id, "Technical");
             $result3 = $requi_model->insert_requisition_skills($requisition_data2['requisition_code'], $nontechnicalskill_id, "Non-Technical");
             if ($id == '') {
                 $tableid = $result;
             }
             if ($result != '') {
                 if ($actionflag == 2) {
                     //start of mailing
                     $requisition_data = $requi_model->getRequisitionDataById($id);
                     $report_person_data = $user_model->getUserDataById($requisition_data['reporting_id']);
                     $st_arr = array('0' => 'Select status', '2' => 'Approved', '3' => 'Rejected');
                     if ($req_status == 3 || $appr_mail == 'approved') {
                         //for rejected
                         $approver1_person_data = $user_model->getUserDataById($requisition_data['approver1']);
                         $Raisedby_person_data = $user_model->getUserDataById($requisition_data['createdby']);
                         $jobttlArr = $jobtitleModel->getsingleJobTitleData(trim($requisition_data['jobtitle']));
                         if (!empty($jobttlArr) && $jobttlArr != 'norows') {
                             $jobtitlename = ' - ' . $jobttlArr[0]['jobtitlename'];
                         } else {
                             $jobtitlename = '';
                         }
                         /* $mail_arr[0]['name'] = 'HR';
                            $mail_arr[0]['email'] = defined('REQ_HR_' . $requisition_data['businessunit_id']) ? constant('REQ_HR_' . $requisition_data['businessunit_id']) : "";
                            $mail_arr[0]['type'] = 'HR';
                            $mail_arr[1]['name'] = 'Management';
                            $mail_arr[1]['email'] = defined('REQ_MGMT_' . $requisition_data['businessunit_id']) ? constant('REQ_MGMT_' . $requisition_data['businessunit_id']) : "";
                            $mail_arr[1]['type'] = 'Management'; */
                         //$hrgroupemail = $requi_model->getgroupemailid("REQ_HR", $requisition_data['businessunit_id']);
                         //if (isset($hrgroupemail)) {
                         //HR Group
                         $mail_arr[0]['name'] = 'HR';
                         $mail_arr[0]['email'] = constant('REQ_HR_0');
                         $mail_arr[0]['type'] = 'HR';
                         //}
                         //$mgmtgroupemail = $requi_model->getgroupemailid("REQ_MGMT", $requisition_data['businessunit_id']);
                         //if (isset($mgmtgroupemail)) {
                         //HR Group
                         $mail_arr[1]['name'] = 'Management';
                         $mail_arr[1]['email'] = constant('REQ_MGMT_0');
                         $mail_arr[1]['type'] = 'Management';
                         //}
                         $mail_arr[2]['name'] = $Raisedby_person_data['userfullname'];
                         $mail_arr[2]['email'] = $Raisedby_person_data['emailaddress'];
                         $mail_arr[2]['type'] = 'Raise';
                         $mail_arr[3]['name'] = $approver1_person_data['userfullname'];
                         $mail_arr[3]['email'] = $approver1_person_data['emailaddress'];
                         $mail_arr[3]['type'] = 'Approver';
                         // Check if the reporting person and raised person are same - Requisition raised by Manager case
                         if ($requisition_data['reporting_id'] != $requisition_data['createdby']) {
                             $mail_arr[4]['name'] = $report_person_data['userfullname'];
                             $mail_arr[4]['email'] = $report_person_data['emailaddress'];
                             $mail_arr[4]['type'] = 'ReportingManager';
                         }
                         $appr_str = "";
                         $appr_str = $approver1_person_data['userfullname'];
                         if ($requisition_data['approver2'] != '') {
                             $approver2_person_data = $user_model->getUserDataById($requisition_data['approver2']);
                             $appr_str .= ", " . $approver2_person_data['userfullname'];
                             $mail_arr[5]['name'] = $approver2_person_data['userfullname'];
                             $mail_arr[5]['email'] = $approver2_person_data['emailaddress'];
                             $mail_arr[5]['type'] = 'Approver';
                         }
                         if ($requisition_data['approver3'] != '') {
                             $approver3_person_data = $user_model->getUserDataById($requisition_data['approver3']);
                             $appr_str .= " and " . $approver3_person_data['userfullname'];
                             $mail_arr[6]['name'] = $approver3_person_data['userfullname'];
                             $mail_arr[6]['email'] = $approver3_person_data['emailaddress'];
                             $mail_arr[6]['type'] = 'Approver';
                         }
                         $mail = array();
                         for ($ii = 0; $ii < count($mail_arr); $ii++) {
                             $base_url = 'http://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getBaseUrl();
                             $view = $this->getHelper('ViewRenderer')->view;
                             $this->view->emp_name = !empty($mail_arr[$ii]['name']) ? $mail_arr[$ii]['name'] : '';
                             $this->view->base_url = $base_url;
                             $this->view->type = !empty($mail_arr[$ii]['type']) ? $mail_arr[$ii]['type'] : '';
                             $this->view->jobtitle = $jobtitlename;
                             $this->view->requisition_code = $requisition_data['requisition_code'];
                             $this->view->approver_str = $appr_str;
                             $this->view->raised_name = $Raisedby_person_data['userfullname'];
                             $this->view->req_status = $st_arr[$req_status];
                             $this->view->reporting_manager = $report_person_data['userfullname'];
                             $text = $view->render('mailtemplates/changedrequisition.phtml');
                             $options['subject'] = strtolower($st_arr[$req_status]) == 'approved' ? APPLICATION_NAME . ': ' . $requisition_data['requisition_code'] . '-Requisition is approved' : APPLICATION_NAME . ': Requisition is rejected';
                             $options['header'] = 'Requisition Status';
                             $options['toEmail'] = !empty($mail_arr[$ii]['email']) ? $mail_arr[$ii]['email'] : '';
                             $options['toName'] = !empty($mail_arr[$ii]['name']) ? $mail_arr[$ii]['name'] : '';
                             $options['message'] = $text;
                             $mail[$ii] = $options;
                             //$options['cron'] = 'yes';
                             if ($options['toEmail'] != '') {
                                 sapp_Global::_sendEmail($options, "by_approver1", REQUISITIONMODULESENDEMAIL);
                             } else {
                                 sapp_Global::_sendEmail($options, '', REQUISITIONMODULESENDEMAIL);
                             }
                         }
                     } else {
                         if ($req_status == 2) {
                             //Approver
                             $approver_person_data = $user_model->getUserDataById($appr_mail);
                             $mail_arr[0]['name'] = $approver_person_data['userfullname'];
                             $mail_arr[0]['email'] = $approver_person_data['emailaddress'];
                             $mail_arr[0]['type'] = 'Approver';
                             //HR Group
                             $mail_arr[1]['name'] = 'HR';
                             $mail_arr[1]['email'] = constant('REQ_HR_0');
                             $mail_arr[1]['type'] = 'HR';
                             //Management Group
                             $mail_arr[2]['name'] = 'Management';
                             $mail_arr[2]['email'] = constant('REQ_MGMT_0');
                             $mail_arr[2]['type'] = 'Management';
                             //Requester
                             $Raisedby_person_data = $user_model->getUserDataById($requisition_data['createdby']);
                             $mail_arr[3]['name'] = $Raisedby_person_data['userfullname'];
                             $mail_arr[3]['email'] = $Raisedby_person_data['emailaddress'];
                             $mail_arr[3]['type'] = 'Raise';
                             if ($edit_flag == 'yes') {
                                 $approved_by_data = $user_model->getUserDataById($requisition_data['approver' . $appr_per]);
                                 $req_status = 2;
                             } else {
                                 $approved_by_data = $user_model->getUserDataById($appr_per);
                             }
                             $Raisedby_person_data = $user_model->getUserDataById($requisition_data['createdby']);
                             $appr_str = $approved_by_data['userfullname'];
                             for ($ii = 0; $ii < count($mail_arr); $ii++) {
                                 $base_url = 'http://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getBaseUrl();
                                 $view = $this->getHelper('ViewRenderer')->view;
                                 $this->view->emp_name = $mail_arr[$ii]['name'];
                                 $this->view->base_url = $base_url;
                                 $this->view->type = $mail_arr[$ii]['type'];
                                 $this->view->requisition_code = $requisition_data['requisition_code'];
                                 $this->view->req_status = $st_arr[$req_status];
                                 $this->view->raised_name = $Raisedby_person_data['userfullname'];
                                 $this->view->approver_str = $appr_str;
                                 $text = $view->render('mailtemplates/changedrequisition.phtml');
                                 $options['subject'] = strtolower($st_arr[$req_status]) == 'approved' ? APPLICATION_NAME . ':' . $requisition_data['requisition_code'] . '-Requisition is approved' : APPLICATION_NAME . ': Requisition is rejected';
                                 $options['header'] = 'Requisition Status';
                                 $options['toEmail'] = $mail_arr[$ii]['email'];
                                 $options['toName'] = $mail_arr[$ii]['name'];
                                 $options['message'] = $text;
                                 //$options['cron'] = 'yes';
                                 if ($options['toEmail'] != '') {
                                     sapp_Global::_sendEmail($options, "by_approver2", REQUISITIONMODULESENDEMAIL);
                                 } else {
                                     sapp_Global::_sendEmail($options, '', REQUISITIONMODULESENDEMAIL);
                                 }
                             }
                         }
                     }
                     //end of mailing
                 }
                 $menumodel = new Default_Model_Menu();
                 $objidArr = $menumodel->getMenuObjID('/requisition');
                 $objID = $objidArr[0]['id'];
                 $result = sapp_Global::logManager($objID, $actionflag, $loginUserId, $tableid);
                 if ($id != '') {
                     $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Resource requisition updated successfully."));
                 } else {
                     $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Resource requisition added successfully."));
                 }
                 $trDb->commit();
                 $this->_redirect('/requisition');
             }
         } catch (Exception $e) {
             $trDb->rollBack();
             $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Something went wrong, please try again later."));
             $this->_redirect('/requisition');
         }
     } else {
         $messages = $requisitionform->getMessages();
         foreach ($messages as $key => $val) {
             foreach ($val as $key2 => $val2) {
                 $msgarray[$key] = $val2;
                 break;
             }
         }
         return $msgarray;
     }
 }