In Cases module i created a custom function for auto assignment of responsible user. In database Auto assignment function is working properly data updated every save but in Cases module, Responsible person does not change in Assigned To field. Reply asap
Here is the code:
<?php
/**
* BS method
*/
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class SaveEndDate
{
function action_save_date($bean,$event,$arguments)
{
//echo 'hiiii';
global $db;
//$workflowbean=BeanFactory::newBean('SLA_workflow');
$cat=$bean->category_c;
$moduleBean=BeanFactory::getBean('workf_SLA_workflow',0);
$beanList = $moduleBean->get_list(
//Order by the accounts name
'name',
"category_c='$cat'"
//Only accounts with industry 'Media
);
//echo '<pre>';
$sla = $beanList['list']['0']->sla_c;
$sla=$sla/8;
// echo '<pre>';
// print_r($bean);
// die;
// here is an example
$start = $bean->date_entered;
//die;
$holidayBean=BeanFactory::getBean('HOLI_Holidays',0);
$holidayList = $holidayBean->get_list(
//Order by the accounts name
'name'
//Only accounts with industry 'Media
);
// echo "<pre>";
// print_r($holidayList);
// die();
// $holidayDate = $holidayList->Array['list'][0]['holiday_date'];
// $holidayDate2 = $holidayDate->Arra
// echo "<pre>";
// print_r($holidayDate);
// die();
$holiday = array();
foreach ($holidayList['list'] as $value) {
$H_DATE=date("Y-m-d", strtotime($value->holiday_date));
array_push($holiday,$H_DATE);
}
$deadline=SaveEndDate::getWDays($start,$holiday, $sla);
//die;
$bean->deadline_c=$deadline;
$bean->save();
//START
require_once('data/SugarBean.php');
$sugarbean = new SugarBean();
$moduleUser = BeanFactory::getBean('Users',0);
$userList = $moduleUser->get_list(
'name'
);
$user = array();
$id = array();
foreach ($userList['list'] as $value) {
$userName = $value->user_name;
$userID = $value->id;
array_push($user,$userName);
// usort($user, 'strnatcasecmp');
array_push($id,$userID);
$finalArray = array_combine($id, $user);
natcasesort($finalArray);
}
$query = "SELECT user_name FROM last_assigned ORDER BY id DESC LIMIT 1";
$result = $db->query($query,true);
$row = $db->fetchByAssoc($result);
$last_user = $row['user_name'];
if(empty($row))
{
// echo "hiiii";
$user = reset($finalArray);
$userid = array_search($user,$finalArray);
$insquery = "INSERT INTO last_assigned (user_id,user_name,dept_role) VALUES ('$userid','$user','Helpdesk')";
$insresult = $sugarbean->db->query($insquery,true);
// echo $user;
$bean->assigned_user_id=$userid;
$bean->save();
}
else
{
// echo "hellooo";
$keys = array_keys($finalArray);
if($keys !== false)
{
$values = array_values($finalArray);
$offset = array_search($last_user, $values);
if ($offset !== false && isset($values[++$offset])) {
$element = array_slice($finalArray, $offset, 1, true);
$next_user_key = key($element);
$next_user_name = current($element);
}
$updquery = "UPDATE last_assigned SET user_id = '$next_user_key', user_name = '$next_user_name' WHERE dept_role = 'Helpdesk'";
$updresult = $sugarbean->db->query($updquery,true);
// echo $next_user_name;
$bean->assigned_user_id=$next_user_key;
$bean->save();
}
}
// die;
// $bean->save();
}
//END OF CODE
public function getWDays($startDate,$holiday,$wDays)
{
// using + weekdays excludes weekends
// echo $startDate;
// echo "<br>";
// echo "<br>";
$new_date = date('Y-m-d h:i:s', strtotime("{$startDate} +{$wDays} weekdays"));
//die;
foreach ($holiday as $h_day)
{
# code...
$holiday_ts = strtotime($h_day);
// if holiday falls between start date and new date, then account for it
if ($holiday_ts >= strtotime($startDate) && $holiday_ts <= strtotime($new_date))
{
// check if the holiday falls on a working day
$h = date('w', $holiday_ts);
if ($h != 0 && $h != 6 )
{
// holiday falls on a working day, add an extra working day
$new_date = date('Y-m-d h:i:s', strtotime("{$new_date} + 1 weekdays"));
}
}
}
return $new_date;
}
}
?>