public function deleteAction()
 {
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
     }
     $id = $this->_request->getParam('objid');
     $messages['message'] = '';
     $actionflag = 3;
     if ($id) {
         $businessunitsmodel = new Default_Model_Businessunits();
         $checkdeptexistance = $businessunitsmodel->checkdeptstobusinessunits($id);
         if ($checkdeptexistance == 0) {
             $menumodel = new Default_Model_Menu();
             $data = array('isactive' => 0, 'modifieddate' => gmdate("Y-m-d H:i:s"));
             $where = array('id=?' => $id);
             $Id = $businessunitsmodel->SaveorUpdateBusinessUnits($data, $where);
             if ($Id == 'update') {
                 $menuidArr = $menumodel->getMenuObjID('/businessunits');
                 $menuID = $menuidArr[0]['id'];
                 $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $id);
                 $messages['message'] = 'Business unit deleted successfully.';
                 $messages['msgtype'] = 'success';
             } else {
                 $messages['message'] = 'Business unit cannot be deleted.';
                 $messages['msgtype'] = 'error';
             }
         } else {
             $messages['message'] = 'Since departments are associated with this business unit, you cannot delete the business unit.';
             $messages['msgtype'] = 'error';
         }
     } else {
         $messages['message'] = 'Business unit cannot be deleted.';
     }
     $this->_helper->json($messages);
 }
 public function savebusinessunitAction()
 {
     $ajaxContext = $this->_helper->getHelper('AjaxContext');
     $ajaxContext->addActionContext('savebusinessunit', 'json')->initContext();
     $this->_helper->layout->disableLayout();
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
         $loginuserRole = $auth->getStorage()->read()->emprole;
         $loginuserGroup = $auth->getStorage()->read()->group_id;
     }
     $businessunitsmodel = new Default_Model_Businessunits();
     $orgInfoModel = new Default_Model_Organisationinfo();
     $systempreferencemodel = new Default_Model_Sitepreference();
     $organisationData = $orgInfoModel->getOrganisationInfo();
     $sitePreferenceData = $systempreferencemodel->SitePreferanceData();
     $result['result'] = '';
     $result['id'] = '';
     $result['unitname'] = '';
     $result['address'] = '';
     $date = new Zend_Date();
     try {
         $unitname = trim($this->_request->getParam('bunit'));
         $address = $this->_request->getParam('streetAddress');
         $isUnitExist = $businessunitsmodel->checkDuplicateUnitName($unitname);
         if (!empty($isUnitExist)) {
             if ($isUnitExist[0]['count'] > 0) {
                 $result['msg'] = 'Name already exists.';
                 $result['id'] = '';
                 $result['unitname'] = '';
                 $result['address'] = '';
             } else {
                 $menumodel = new Default_Model_Menu();
                 $actionflag = '';
                 $tableid = '';
                 if (strlen(trim($unitname)) > 3) {
                     $unitCode = strtoupper(substr(trim($unitname), 0, 4));
                 } else {
                     $unitCode = strtoupper(trim($unitname));
                 }
                 $data = array('unitname' => $unitname, 'unitcode' => $unitCode, 'startdate' => NULL, 'country' => !empty($organisationData) ? $organisationData[0]['country'] : NULL, 'state' => !empty($organisationData) ? $organisationData[0]['state'] : NULL, 'city' => !empty($organisationData) ? $organisationData[0]['city'] : NULL, 'address1' => $address, 'timezone' => !empty($sitePreferenceData) ? $sitePreferenceData[0]['timezoneid'] : NULL, 'createdby' => $loginUserId, 'createddate' => $date->get('yyyy-MM-dd HH:mm:ss'), 'modifiedby' => $loginUserId, 'modifieddate' => $date->get('yyyy-MM-dd HH:mm:ss'));
                 $where = '';
                 $actionflag = 1;
                 $Id = $businessunitsmodel->SaveorUpdateBusinessUnits($data, $where);
                 if ($Id) {
                     $menuID = BUSINESSUNITS;
                     $logresult = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $Id);
                     $result['msg'] = 'success';
                     $result['id'] = $Id;
                     $result['unitname'] = $unitname;
                     $result['address'] = $address;
                 } else {
                     $result['msg'] = 'error';
                     $result['id'] = '';
                     $result['unitname'] = '';
                     $result['address'] = '';
                 }
             }
         }
     } catch (Exception $e) {
         $result['msg'] = $e->getMessage();
         $result['id'] = '';
         $result['unitname'] = '';
         $result['address'] = '';
     }
     $this->_helper->json($result);
 }
 public function save($servicedeskconfform)
 {
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
     }
     $servicedeskconfmodel = new Default_Model_Servicedeskconf();
     $employeemodel = new Default_Model_Employee();
     $departmentsmodel = new Default_Model_Departments();
     $businessunitsmodel = new Default_Model_Businessunits();
     $msgarray = array();
     $resultArr = array();
     $empstring = '';
     $prevBusinessunit = '';
     $errorflag = "true";
     $id = $this->_request->getParam('id');
     $businessunit_id = $this->_request->getParam('businessunit_id');
     $service_desk_flag = $this->_request->getParam('service_desk_flag');
     $department_id = $this->_request->getParam('department_id');
     $service_desk_id = $this->_request->getParam('service_desk_id');
     $request_recievers = $this->_request->getParam('request_recievers');
     $approvingauthority = $this->_request->getParam('approvingauthority');
     $approver_1 = $this->_request->getParam('approver_1');
     $approver_2 = $this->_request->getParam('approver_2');
     $approver_3 = $this->_request->getParam('approver_3');
     $cc_mail_recievers = $this->_request->getParam('cc_mail_recievers');
     $attachment = $this->_request->getParam('attachment');
     $description = trim($this->_request->getParam('description'));
     /** Start
      * Validating Request reciever and CC reciever
      * If both have same values then throwing an error
      */
     if (!empty($request_recievers)) {
         if (!empty($cc_mail_recievers)) {
             $resultArr = array_intersect($request_recievers, $cc_mail_recievers);
         }
     }
     if (!empty($resultArr)) {
         $msgarray['cc_mail_recievers'] = 'Executors and request viewers cannot be same.';
         $errorflag = "false";
     }
     /**
      End validating Request reciever and CC reciever
     */
     /** Start
      * Validating selection of department if implementaion is department wise
      */
     if ($service_desk_flag == 0) {
         if ($department_id == '') {
             $msgarray['department_id'] = 'Please select department.';
             $errorflag = "false";
         }
     }
     /**
      End validating selection of department
     */
     /** Start
      * Validating approver level selection.
      */
     if ($approvingauthority != '') {
         if ($approvingauthority == 1) {
             if ($approver_1 == '') {
                 $msgarray['approver_1'] = 'Please select approver 1.';
                 $errorflag = "false";
             }
         } else {
             if ($approvingauthority == 2) {
                 if ($approver_1 == '') {
                     $msgarray['approver_1'] = 'Please select approver 1.';
                     $errorflag = "false";
                 }
                 if ($approver_2 == '') {
                     $msgarray['approver_2'] = 'Please select approver 2.';
                     $errorflag = "false";
                 }
             } else {
                 if ($approver_1 == '') {
                     $msgarray['approver_1'] = 'Please select approver 1.';
                     $errorflag = "false";
                 }
                 if ($approver_2 == '') {
                     $msgarray['approver_2'] = 'Please select approver 2.';
                     $errorflag = "false";
                 }
                 if ($approver_3 == '') {
                     $msgarray['approver_3'] = 'Please select approver 3.';
                     $errorflag = "false";
                 }
             }
         }
     }
     /**
      * End Approver level selection
      */
     /** Start
          Validating unique service desk department
         */
     if ($businessunit_id != '' && $service_desk_id != '' && $id == '') {
         $serviceconfArr = $servicedeskconfmodel->checkuniqueServiceConfData($businessunit_id, $service_desk_flag, $service_desk_id, $department_id);
         if (!empty($serviceconfArr)) {
             if ($serviceconfArr[0]['count'] > 0) {
                 $msgarray['service_desk_id'] = 'Please select a different department.';
                 $errorflag = "false";
             }
         }
     }
     /** End
      * Validating uniques service desk department
      */
     /**
      * Validating if pending requests are there when changing the implementation
      */
     if ($businessunit_id != '') {
         $implementationdata = $businessunitsmodel->getSingleUnitData($businessunit_id);
         if (!empty($implementationdata)) {
             $prevBusinessunit = $implementationdata['service_desk_flag'];
         }
         if ($service_desk_flag != '') {
             if ($prevBusinessunit != $service_desk_flag) {
                 $pendingRequestdata = $servicedeskconfmodel->getPendingServiceReqData($businessunit_id);
                 if (!empty($pendingRequestdata)) {
                     if ($pendingRequestdata[0]['count'] > 0) {
                         $msgarray['service_desk_flag'] = 'Applicability cannot be changed as requests are in pending state.';
                         $errorflag = "false";
                     }
                 }
             }
         }
     }
     /**
      * End validating pending request checking 
      */
     $db = Zend_Db_Table::getDefaultAdapter();
     $db->beginTransaction();
     if ($servicedeskconfform->isValid($this->_request->getPost()) && $errorflag == 'true') {
         try {
             $menumodel = new Default_Model_Menu();
             $actionflag = '';
             $tableid = '';
             $req_rec_string = '';
             $cc_string = '';
             $req_rec_string = implode(',', $request_recievers);
             if (!empty($cc_mail_recievers)) {
                 $cc_string = implode(',', $cc_mail_recievers);
             }
             $data = array('businessunit_id' => $businessunit_id, 'department_id' => $department_id != '' ? $department_id : NULL, 'service_desk_flag' => $service_desk_flag, 'service_desk_id' => $service_desk_id, 'request_recievers' => $req_rec_string, 'cc_mail_recievers' => $cc_string != '' ? $cc_string : NULL, 'approver_1' => $approver_1 != '' ? $approver_1 : NULL, 'approver_2' => $approver_2 != '' ? $approver_2 : NULL, 'approver_3' => $approver_3 != '' ? $approver_3 : NULL, 'attachment' => $attachment != '' ? $attachment : NULL, 'description' => $description != '' ? $description : NULL, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"));
             if ($id != '') {
                 $where = array('id=?' => $id);
                 $actionflag = 2;
             } else {
                 $data['createdby'] = $loginUserId;
                 $data['createddate'] = gmdate("Y-m-d H:i:s");
                 $data['isactive'] = 1;
                 $where = '';
                 $actionflag = 1;
             }
             $budata = array('service_desk_flag' => $service_desk_flag, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"));
             $buwhere = array('id=?' => $businessunit_id);
             /* If implementation wise is changed then all the previous records are inactivated and new record is inserted 
              * Else normal update - insert will take place. 
              */
             if ($prevBusinessunit != $service_desk_flag) {
                 $prevBUData = array('isactive' => 0, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"));
                 $prevBUwhere = array('businessunit_id=?' => $businessunit_id);
                 /* Inactivating previous records */
                 $prevBUId = $servicedeskconfmodel->SaveorUpdateServiceConfData($prevBUData, $prevBUwhere);
                 $data['createdby'] = $loginUserId;
                 $data['createddate'] = gmdate("Y-m-d H:i:s");
                 $data['isactive'] = 1;
                 $where = '';
                 $actionflag = 1;
                 /* Inserting new record */
                 $Id = $servicedeskconfmodel->SaveorUpdateServiceConfData($data, $where);
             } else {
                 /* Insert or update based on action */
                 $Id = $servicedeskconfmodel->SaveorUpdateServiceConfData($data, $where);
             }
             /* Updating service desk flag in business unit table */
             $BId = $businessunitsmodel->SaveorUpdateBusinessUnits($budata, $buwhere);
             if ($Id == 'update') {
                 $tableid = $id;
                 $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Setting updated successfully."));
             } else {
                 $tableid = $Id;
                 $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Setting added successfully."));
             }
             $menuidArr = $menumodel->getMenuObjID('/servicedeskconf');
             $menuID = $menuidArr[0]['id'];
             $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $tableid);
             $db->commit();
             $this->_redirect('servicedeskconf');
         } catch (Exception $e) {
             $db->rollBack();
             $msgarray['service_desk_id'] = "Something went wrong, please try again.";
             return $msgarray;
         }
     } else {
         $messages = $servicedeskconfform->getMessages();
         foreach ($messages as $key => $val) {
             foreach ($val as $key2 => $val2) {
                 $msgarray[$key] = $val2;
                 break;
             }
         }
         if (isset($businessunit_id) && $businessunit_id != '') {
             $employeeData = $employeemodel->getEmployeesForServiceDesk($businessunit_id, $department_id);
             $servicedeskData = $servicedeskconfmodel->getActiveServiceDepartments($businessunit_id, $department_id);
             if ($id == '') {
                 $servicedeskconfform->service_desk_id->clearMultiOptions();
             }
             $servicedeskconfform->request_recievers->clearMultiOptions();
             $servicedeskconfform->cc_mail_recievers->clearMultiOptions();
             if (!empty($employeeData)) {
                 $servicedeskconfform->request_recievers->addMultiOption('', 'Select executor');
                 $servicedeskconfform->cc_mail_recievers->addMultiOption('', 'Select request viewer');
                 foreach ($employeeData as $empres) {
                     $servicedeskconfform->request_recievers->addMultiOption($empres['user_id'], utf8_encode($empres['userfullname']));
                     $servicedeskconfform->cc_mail_recievers->addMultiOption($empres['user_id'], utf8_encode($empres['userfullname']));
                 }
             }
             if ($id == '') {
                 if (!empty($servicedeskData)) {
                     $servicedeskconfform->service_desk_id->addMultiOption('', 'Select category');
                     foreach ($servicedeskData as $servicedeskres) {
                         $servicedeskconfform->service_desk_id->addMultiOption($servicedeskres['id'], utf8_encode($servicedeskres['service_desk_name']));
                     }
                 }
             }
             if ($service_desk_flag == 0) {
                 $departmentlistArr = $departmentsmodel->getDepartmentList($businessunit_id);
                 if (!empty($departmentlistArr)) {
                     foreach ($departmentlistArr as $departmentlist) {
                         $servicedeskconfform->department_id->addMultiOption($departmentlist['id'], utf8_encode($departmentlist['deptname']));
                     }
                 }
             }
             if (isset($service_desk_id) && $service_desk_id != 0 && $service_desk_id != '' && $id == '') {
                 $servicedeskconfform->setDefault('service_desk_id', $service_desk_id);
             }
             if (isset($department_id) && $department_id != 0 && $department_id != '') {
                 $servicedeskconfform->setDefault('department_id', $department_id);
             }
         }
         if (isset($approvingauthority) && $approvingauthority != '') {
             if ($approvingauthority == 1) {
                 $approver1Data = $employeemodel->getApproverForServiceDesk($businessunit_id, $department_id, $empstring);
                 if (!empty($approver1Data)) {
                     foreach ($approver1Data as $app1res) {
                         $servicedeskconfform->approver_1->addMultiOption($app1res['user_id'], utf8_encode($app1res['userfullname']));
                     }
                 }
             } else {
                 if ($approvingauthority == 2) {
                     $approver1Data = $employeemodel->getApproverForServiceDesk($businessunit_id, $department_id, $empstring);
                     if (!empty($approver1Data)) {
                         foreach ($approver1Data as $app1res) {
                             $servicedeskconfform->approver_1->addMultiOption($app1res['user_id'], utf8_encode($app1res['userfullname']));
                         }
                     }
                     if (isset($approver_1) && $approver_1 != '') {
                         $empstring = $approver_1;
                         $approver2Data = $employeemodel->getApproverForServiceDesk($businessunit_id, $department_id, $empstring);
                         if (!empty($approver2Data)) {
                             foreach ($approver2Data as $app2res) {
                                 $servicedeskconfform->approver_2->addMultiOption($app2res['user_id'], utf8_encode($app2res['userfullname']));
                             }
                         }
                     }
                 } else {
                     $approver1Data = $employeemodel->getApproverForServiceDesk($businessunit_id, $department_id, $empstring);
                     if (!empty($approver1Data)) {
                         foreach ($approver1Data as $app1res) {
                             $servicedeskconfform->approver_1->addMultiOption($app1res['user_id'], utf8_encode($app1res['userfullname']));
                         }
                     }
                     if (isset($approver_1) && $approver_1 != '') {
                         $empstring = $approver_1;
                         $approver2Data = $employeemodel->getApproverForServiceDesk($businessunit_id, $department_id, $empstring);
                         if (!empty($approver2Data)) {
                             foreach ($approver2Data as $app2res) {
                                 $servicedeskconfform->approver_2->addMultiOption($app2res['user_id'], utf8_encode($app2res['userfullname']));
                             }
                         }
                     }
                     if (isset($approver_2) && $approver_2 != '') {
                         $empstring = $empstring . ',' . $approver_2;
                         $approver3Data = $employeemodel->getApproverForServiceDesk($businessunit_id, $department_id, $empstring);
                         if (!empty($approver3Data)) {
                             foreach ($approver3Data as $app3res) {
                                 $servicedeskconfform->approver_3->addMultiOption($app3res['user_id'], utf8_encode($app3res['userfullname']));
                             }
                         }
                     }
                 }
             }
             if (isset($approver_1) && $approver_1 != 0 && $approver_1 != '') {
                 $servicedeskconfform->setDefault('approver_1', $approver_1);
             }
             if (isset($approver_2) && $approver_2 != 0 && $approver_2 != '') {
                 $servicedeskconfform->setDefault('approver_2', $approver_2);
             }
             if (isset($approver_3) && $approver_3 != 0 && $approver_3 != '') {
                 $servicedeskconfform->setDefault('approver_3', $approver_3);
             }
         }
         return $msgarray;
     }
 }