0) { $tmleft=$exdate - strtotime(dt_format(time())); $taskdays=round($tmleft/86400,0); if($taskdays<1) $taskdays=1; if(dt_format($exdate)==dt_format(time())) // if they said expire today set taskdays = 0 $taskdays=0; } if(!isset($taskdays)) $taskdays = 3; $clientcompanypropertyassignmentid=clientcompanypropertyassignment_insert($type,$id,$clientcompanypropertyid,$assignmentmethodid,$clientcompanypropertyassignmentexpiremarkeddate, $clientcompanypropertyassignmenttsecondarystaffid, $externaluserinviteid, $assignmentaliasclientstaffid); if($type!=="externaluser") { if(is_array($_SESSION[login][roles])) $role = array_shift(array_keys($_SESSION[login][roles])); if(!$role) $role = 0; clienttask_create($role,$clientcompanypropertyid, $clientcompanypropertyassignmentid,"Task Accept/Decline","Accept or Decline the Assignment.",$taskdays,0,"","","","",$clientpretaskid); workflowevent('propertyassignmentcreated',clientcompanypropertyassignment,$clientcompanypropertyassignmentid); } //stop future calls to clientcompanypropertyassignment_insert $clientcompanypropertyassignment_insert_callable=""; return $clientcompanypropertyassignmentid; } function clientcompanypropertyassignment_get($clientcompanypropertyid = '', $assignmentmethodid = '', $clientcompanypropertyassignmentid = '', $include_status = 0, $staffid = '', $offersenabled = ''){ $sql = "select * from tblclientcompanypropertyassignment a join vw_assigntypes using(accountstaffid, staffid, externaluserid,assignmentaliasclientstaffid) join tblassignmentmethod using (assignmentmethodid)"; // SPECIFIC ASSIGNMENT if(companyid()) $sql.=" join tbllogin using(staffid) "; if($clientcompanypropertyassignmentid) $sql .= " where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid); // SPECIFIC PROPERTY else { if (!is_array($clientcompanypropertyid)) { $sql .= " where clientcompanypropertyid = " . db_number($clientcompanypropertyid); } else if (is_array($clientcompanypropertyid) && !empty($clientcompanypropertyid)) { $sql .=" where clientcompanypropertyid in (".implode(",",$clientcompanypropertyid).") "; } } // SPECIFIC METHOD if($assignmentmethodid) $sql .= " and assignmentmethodid = " . db_number($assignmentmethodid); // COMPANY SPECIFIC if(companyid()) $sql .= " and tbllogin.companyid = " . companyid(); //For genesis portal skip selecting assignments that were pulled over 30 days ago. if(client_portal() && companyid()) $sql .= " and ( a.clientcompanypropertyassignmentrevokeddate is null or ( a.clientcompanypropertyassignmentrevokeddate is not null and (a.clientcompanypropertyassignmentrevokeddate + interval '30 days') > now() ) )" ; // SPECIFIC STAFF MEMBER if(accountid() && has_customization("corporateaccount") ) { //this corporate account gets to see all properties for all assigments for all subs $sql.=" and accountstaffid in (select accountstaffid from tblaccountstaff join tblaccount using(accountid) where 0 = 0 "; if( !account_hasparent(accountid())) $sql.=" and accountparentaccountid = ".accountid()." "; else $sql.=" and accountid = ".accountid()." "; $sql.= " ) "; } elseif(accountstaffid()) { $sql .= " and accountstaffid in ( select accountstaffid from tblaccountstaff where officelocationid in ( select officelocationid from tblaccountstaff where accountstaffid = ".accountstaffid()." ) )"; } if(externaluserid()) $sql .= " and a.externaluserid = " . externaluserid(); if($staffid) $sql .= " and a.staffid = " . db_number($staffid); // ONLY OFFERS ENABLED if($offersenabled) $sql .= " and assignmentmethodoffersenabled = 1"; if(clientstaffid()) $sql .= " and assignmentaliasclientstaffid = " . clientstaffid(); if (is_array($clientcompanypropertyid) && !empty($clientcompanypropertyid)) $assignments = db_query($sql,0,1,clientcompanypropertyassignmentid); // vlade's pending code not used in most places else $assignments = db_query($sql); // STATUS BASED ON DATES IN ROW if($include_status) foreach($assignments as $key => $assignment) $assignments[$key]['clientcompanypropertyassignmentstatus'] = clientcompanypropertyassignment_status($assignment['clientcompanypropertyassignmentid']); if($assignments) return $assignments; else return false; } function clientcompanypropertyassignment_get_withnames($clientcompanypropertyid = '', $assignmentmethodid = '', $clientcompanypropertyassignmentid = '', $include_status = 0, $staffid = '', $offersenabled = ''){ $sql = "select * from tblclientcompanypropertyassignment join vw_assigntypes using(staffid, accountstaffid, externaluserid, assignmentaliasclientstaffid) join tblassignmentmethod using (assignmentmethodid) join tbllogin using (loginid) " ; // SPECIFIC ASSIGNMENT if($clientcompanypropertyassignmentid) $sql .= " where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid); // SPECIFIC PROPERTY else $sql .= " where clientcompanypropertyid = " . db_number($clientcompanypropertyid); // SPECIFIC METHOD if($assignmentmethodid) $sql .= " and assignmentmethodid = " . db_number($assignmentmethodid); // COMPANY SPECIFIC if(companyid()) $sql .= " and companyid = " . companyid(); if(client_portal() && companyid()) $sql .= " and ( clientcompanypropertyassignmentrevokeddate is null or ( clientcompanypropertyassignmentrevokeddate is not null and (clientcompanypropertyassignmentrevokeddate + interval '30 days') > now() ) )" ; if(accountstaffid()) $sql .= " and tblclientcompanypropertyassignment.accountstaffid = " . accountstaffid(); if(externaluserid()) $sql .= " and tblclientcompanypropertyassignment.externaluserid = " . externaluserid(); // SPECIFIC STAFF MEMBER if($staffid) $sql .= " and tbllogin.staffid = " . db_number($staffid); // ONLY OFFERS ENABLED if($offersenabled) $sql .= " and assignmentmethodoffersenabled = 1"; if(clientstaffid()) $sql .= " and assignmentaliasclientstaffid = " . clientstaffid(); $assignments = db_query($sql); // STATUS BASED ON DATES IN ROW if($include_status) foreach($assignments as $key => $assignment) $assignments[$key]['clientcompanypropertyassignmentstatus'] = clientcompanypropertyassignment_status($assignment['clientcompanypropertyassignmentid']); if($assignments) return $assignments; else return false; } function clientcompanypropertyassignment_review($loginid){ $sql = "select distinct (clientcompanypropertyid), clientcompanypropertyaddress, staffid from tblclientcompanypropertyassignment join tblclientcompanyproperty using(clientcompanypropertyid) join tbllogin using(staffid) where loginid = " . $loginid . " and clientcompanypropertyassignmentaccepteddate is null and clientcompanypropertyassignmentdenieddate is null and clientcompanypropertyassignmentrevokeddate is null and clientcompanypropertyassignmentcompleteddate is null"; $properties = db_query($sql); if($properties) return $properties; else return false; } // ASSIGNMENT MAY BE AN ASSIGNMENT OR AN ASSIGNMENTID function clientcompanypropertyassignment_status($assignment, $staffid = ''){ if(!is_array($assignment)){ $assignment = clientcompanypropertyassignment_get('','',$assignment, 0, $staffid); $assignment = array_pop($assignment); } if($assignment) if($assignment['clientcompanypropertyassignmentrevokeddate']) return 'Pulled'; //Pulled is the industry term else if($assignment['clientcompanypropertyassignmentcompleteddate']) return 'Completed'; else if($assignment['clientcompanypropertyassignmentdenieddate']) return 'Declined'; else if($assignment['clientcompanypropertyassignmentaccepteddate']) return 'Accepted'; else if($assignment['clientcompanypropertyassignmentexpired']) return 'Expired'; else return 'Pending Acceptance'; else return false; } function clientcompanypropertyassignment_isactive($clientcompanypropertyassignmentid){ $assignment = $clientcompanypropertyassignmentid; if(!is_array($assignment)) $assignment = clientcompanypropertyassignment_get('','',$clientcompanypropertyassignmentid); if($assignment['clientcompanypropertyassignmentrevokeddate']) return '0'; else if($assignment['clientcompanypropertyassignmentcompleteddate']) return 0; else if($assignment['clientcompanypropertyassignmentdenieddate']) return 0; else if($assignment['clientcompanypropertyassignmentaccepteddate']) return 1; else return 0; } function clientcompanypropertyassignment_activegroup($clientcompanypropertyassignmentid){ $assignment = $clientcompanypropertyassignmentid; if(!is_array($assignment)) $assignment = clientcompanypropertyassignment_get('','',$clientcompanypropertyassignmentid); if($assignment['clientcompanypropertyassignmentrevokeddate']) return 'Closed'; else if($assignment['clientcompanypropertyassignmentcompleteddate']) return 'Closed'; else if($assignment['clientcompanypropertyassignmentdenieddate']) return 'Closed'; else if($assignment['clientcompanypropertyassignmentexpired']) return 'Expired'; else if($assignment['clientcompanypropertyassignmentaccepteddate']) return 'Active'; else return 'Pending'; } function clientcompanypropertyassignment_accept($clientcompanypropertyassignmentid) { $sql = "update tblclientcompanypropertyassignment set clientcompanypropertyassignmentaccepteddate = now() where clientcompanypropertyassignmentaccepteddate is null --only 'accept it once' and clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid); $ret = db_exec($sql,1); $sql = "update tblclienttask set clienttaskcompleteddate = now(), clienttaskcompletedby = ".loginid()." where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid)." and clienttasktitle ilike 'Task Accept/Decline'"; //only mark tasks that are accept and declien tasks complete db_exec($sql,1); $sql = "select * from tblclientcompanypropertyassignment where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid); $asgnqry = db_first($sql,0,1); $sql = " select * from tblclientcompanypropertyassignment where clientcompanypropertyid = " . db_number($asgnqry[clientcompanypropertyid]) . " and assignmentmethodid = " . db_number($asgnqry[assignmentmethodid]) . " and clientcompanypropertyassignmentaccepteddate is not null order by clientcompanypropertyassignmentid "; $revokedqry = db_first($sql,1); if($revokedqry[clientcompanypropertyassignmentrevokeddate]) { ///track assignments that are sent because it has been pulled and reassigned, so we can reassign any tasks over to the new person and tirgger the workflow event. workflowevent('propertypulledreassignedaccepted',clientcompanypropertyassignment,$clientcompanypropertyassignmentid); $sql = "select * from tblclienttask join tblclientcompanypropertyassignment using(clientcompanypropertyassignmentid) where tblclienttask.clientcompanypropertyid = ".$revokedqry[clientcompanypropertyid]." and assignmentmethodid = ".$revokedqry[assignmentmethodid]." and clienttaskcompleteddate is null and clienttasktitle <> 'Task Accept/Decline'"; $ctaskqry = db_query($sql,0,1); if($ctaskqry) { foreach($ctaskqry as $k => $ctask) { $roleid = $_SESSION[login][roles][1][roleid]; if(!$roleid) $roleid = 1; clienttask_create($roleid,$ctask[clientcompanypropertyid],$clientcompanypropertyassignmentid,$ctask[clienttasktitle], $ctask[clienttaskdescription],3,$ctask[tasktemplateid],"","","",array(),db_number($ctask['clientpretaskid']) ); } } } else { //this event should happen even if a task has previously been revoked workflowevent('propertyassignmentaccepted','clientcompanypropertyassignment',$clientcompanypropertyassignmentid); $sql="select clientpretaskid from tblclientpretask where clientcompanypropertyassignmentid=" . db_number($clientcompanypropertyassignmentid) . " AND clientpretaskcancelleddate IS NULL AND clientpretaskexpirationdate > now()"; $clientpretasks=db_query($sql); if($clientpretasks) { foreach($clientpretasks as $clienttask) workflowevent('clientpretaskaccepted','clientpretask',$clienttask['clientpretaskid']); } } if($ret) return true; else return false; } function clientcompanypropertyassignment_deny($clientcompanypropertyassignmentid, $deniedby = -1){ if($deniedby == -1) $deniedby = loginid(); // DECLINE ASSIGNMENT $sql = "update tblclientcompanypropertyassignment set clientcompanypropertyassignmentdenieddate = now(), clientcompanypropertyassignmentdeniedby = " . db_number($deniedby) . " where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid); db_exec($sql,1); // COMPLETE TASK $sql = "update tblclienttask set clienttaskcompleteddate = now(), clienttaskcompletedby = " . loginid() . " where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid); db_exec($sql,1); workflowevent('propertyassignmentdeclined',clientcompanypropertyassignment,$clientcompanypropertyassignmentid); // Assign to secondary? clientcompanypropertyassignment_assigntosecondaryagent($clientcompanypropertyassignmentid); return true; } function clientcompanypropertyassignment_expire($clientcompanypropertyassignmentid) { $sql = "select clientcompanypropertyassignmentexpired from tblclientcompanypropertyassignment where clientcompanypropertyassignmentid = ".db_number($clientcompanypropertyassignmentid); $expirearr = db_first($sql); $sql = "update tblclientcompanypropertyassignment set clientcompanypropertyassignmentexpired = now(), clientcompanypropertyassignmentexpiredby = " .db_number(loginid()). " where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid)." and clientcompanypropertyassignmentcompleteddate is null"; db_exec($sql,1); $sql = "update tblclienttask set clienttaskrevokeddate = now() where clientcompanypropertyassignmentid = ".db_number($clientcompanypropertyassignmentid); db_exec($sql,1); // Check expiration or we duplicate duplicate the secondary assignment if (!$expirearr[clientcompanypropertyassignmentexpired]) clientcompanypropertyassignment_assigntosecondaryagent($clientcompanypropertyassignmentid); workflowevent('propertyassignmentexpired',clientcompanypropertyassignment,$clientcompanypropertyassignmentid); return; } function clientcompanypropertyassignment_assigntosecondaryagent($clientcompanypropertyassignmentid) { if (!is_numeric($clientcompanypropertyassignmentid) || $clientcompanypropertyassignmentid < 1) ddie('$clientcompanypropertyassignmentid is empty'); $sql = "select * from tblclientcompanypropertyassignment where clientcompanypropertyassignmentid = ".db_number($clientcompanypropertyassignmentid); $assignarr = db_first($sql); if ($assignarr[clientcompanypropertyassignmenttsecondarystaffid]) { $expiredate = strtotime(date("Y-m-d") . " +1 week"); $clientcompanypropertyassignmentid = clientcompanypropertyassignment_create($assignarr[clientcompanypropertyassignmenttsecondarystaffid],$assignarr[clientcompanypropertyid],$assignarr[assignmentmethodid], $expiredate); $staff = get_staff($assignarr[clientcompanypropertyassignmenttsecondarystaffid]); set_message("The assignment has been assigned to secondary agent ($staff[stafffname] $staff[stafflname])."); $sql = "select * from tblloginrole where loginid = ".loginid(); $roleqry = db_first($sql); $roleid = $roleqry[roleid]; $taskname = "Task Accept/Decline"; $taskdescr = "Accept or Decline the assignment."; $taskdays = 3; $tasktemplateid = 0; clienttask_create($roleid,$assignarr[clientcompanypropertyid], $assignarr[assignmentmethodid],$taskname, $taskdescr,$taskdays,$tasktemplateid); } } function clientcompanypropertyassignment_revoke($clientcompanypropertyassignmentid) { $sql = "update tblclientcompanypropertyassignment set clientcompanypropertyassignmentrevokeddate = now(), clientcompanypropertyassignmentrevokedby = " . loginid() . " where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid)." and clientcompanypropertyassignmentcompleteddate is null"; db_exec($sql,1); $sql = "update tblclienttask set clienttaskrevokeddate = now() where clientcompanypropertyassignmentid = ".db_number($clientcompanypropertyassignmentid); db_exec($sql,1); workflowevent('propertyassignmentrevoked',clientcompanypropertyassignment,$clientcompanypropertyassignmentid); return true; } function clientcompanypropertyassignment_expiremarked($clientcompanypropertyassignmentid) { $sql = "update tblclientcompanypropertyassignment set clientcompanypropertyassignmentexpired = now() where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid)." and clientcompanypropertyassignmentexpiremarkeddate is not null and clientcompanypropertyassignmentexpired is null"; db_exec($sql,1); //possibly going to add a work flow event return; } function clientcompanypropertyassignment_complete($clientcompanypropertyassignmentid){ $sql = "update tblclientcompanypropertyassignment set clientcompanypropertyassignmentcompleteddate = now(), clientcompanypropertyassignmentmarkedcompleteby = " . loginid() . " where clientcompanypropertyassignmentid = " . db_number($clientcompanypropertyassignmentid); workflowevent('propertyassignmentcompleted',clientcompanypropertyassignment,$clientcompanypropertyassignmentid); if(db_exec($sql)) return true; else return false; } // VERIFY THE LOGGED IN USER'S COMPANY HAS THIS LISTING [AND IT HAS BEEN ACCEPTED][AND IT IS ABLE TO WRITE OFFERS] function clientcompanypropertyassignment_companyhaslisting($clientcompanypropertyid, $accepted = '', $offersenabled = '') { if(clientcompanyid()) return true; $sql = "select * from tblclientcompanypropertyassignment a join tblassignmentmethod using (assignmentmethodid)"; if (companyid()) $sql .="\n left join tbllogin using (staffid)"; else if (accountid()) $sql .="\n left join tbllogin using (accountstaffid)"; $sql .="\n where clientcompanypropertyid = " . db_number($clientcompanypropertyid) ; if(companyid()) $sql.= " and companyid = " . companyid(); if(accountid() && has_customization("corporateaccount") ) { //this corporate account gets to see all properties for all assigments for all subs $sql.=" and a.accountstaffid in (select accountstaffid from tblaccountstaff join tblaccount using(accountid) where 0 = 0 "; if( !account_hasparent(accountid())) $sql.=" and accountparentaccountid = ".accountid()." "; else $sql.=" and accountid = ".accountid()." "; $sql.= " ) "; } elseif(accountstaffid()) { $sql.= " and a.accountstaffid in ( select accountstaffid from tblaccountstaff where officelocationid in ( select officelocationid from tblaccountstaff where accountstaffid = ".accountstaffid()." ) )"; } if(externaluserid()) $sql.= " and a.externaluserid = " . externaluserid(); if($accepted) $sql .= " and clientcompanypropertyassignmentaccepteddate is not null"; if($offersenabled) $sql .= " and assignmentmethodoffersenabled = 1"; $exists = db_query($sql); if($exists) return true; else return false; } function clientcompanyproperty_getassignmentmethodcurrentuser($clientcompanypropertyid) { if(!$clientcompanypropertyid || is_array($clientcompanypropertyid)) ddie("Error: Invalid clientcompanypropertyid"); if(accountstaffid()) $stafftablename="accountstaff"; elseif(externaluserid()) $stafftablename="externaluser"; else $stafftablename="staff"; $sql = "select assignmentmethodid from tblclientcompanypropertyassignment join tblclientcompanyproperty using (clientcompanypropertyid) join tblassignmentmethod using(assignmentmethodid) join tbl".$stafftablename." using(".$stafftablename."id) join tbllogin using(".$stafftablename."id) where clientcompanypropertyid = ".$clientcompanypropertyid." and clientcompanypropertyassignmentrevokeddate is null and clientcompanypropertyassignmentcompleteddate is null and clientcompanypropertyassignmentexpired is null and clientcompanypropertyassignmentaccepteddate is not null"; if(accountstaffid()) { $sql .= " and tbllogin.accountstaffid in ( select accountstaffid from tblaccountstaff where officelocationid in ( select officelocationid from tblaccountstaff where accountstaffid = ".accountstaffid()." ) )"; } elseif(externaluserid()) $sql.=" and tbllogin.externaluserid = ".externaluserid(); else $sql.=" and tbllogin.companyid = ".companyid(); $sql.= " and clientcompanypropertyassignmentdenieddate is null"; $qry=db_query($sql,1,0,assignmentmethodid); $assignmentids=''; if(is_array($qry)) $assignmentids=implode(",",array_keys($qry)); return $assignmentids; } ?>