}
function clientpropertyassignment_tabs($enabled_pages,$clientpropertyid, $url = ''){
// ENABLED_PAGES COMES FROM clientproperty.php
// GLOBAL VARIABLE TO CHECK IF TABS ARE DISPLAYED
global $clientpropertyassignment_tabs_displayed;
// IF THEY ARE DISPLAYED, WE'RE DONE, ELSE SET THE VARIABLE
if($clientpropertyassignment_tabs_displayed)
return false;
else
$clientpropertyassignment_tabs_displayed = 1;
$clientcompany_tabs['active'] = 'Active Assignments';
$clientcompany_tabs['completed'] = 'Completed Assignments';
$clientcompany_tabs['pulled'] = 'Pulled Assignments';
$clientcompany_tabs['expired'] = 'Expired Assignments';
foreach($clientcompany_tabs as $key => $tab)
if(!$enabled_pages[$key])
unset($clientcompany_tabs[$key]);
?>
}
function display_clientcompanyproperty_selectbox($name,$default,$defaulttext='',$js='')
{
if($name)
$default = $_POST[$name];
$sql = "
select clientcompanypropertyid as id
, clientcompanypropertyaddress as value
from tblclientcompanyproperty
where 0=0
and (clientcompanypropertycloseddate is null or clientcompanypropertyid = ".db_number($default)." )
";
if(clientcompanyid())
$sql.=" and clientcompanyid = ".clientcompanyid();
$qry = db_query($sql);
if($js)
$js = "onchange='$js'";
$box = customfield_return('sqldropdown',$name,$default,$qry,$defaulttext,$js);
return $box;
}
function clientcompanyproperty_get_columndaysdisplay($clientcompanypropertyid,$columnname)
{
$days = clientcompanyproperty_get_columndays($clientcompanypropertyid,$columnname);
if($days!=="")
return "(".db_number($days)." days)";
return "";
}
function clientcompanyproperty_get_columndays($clientcompanypropertyid,$columnname)
{
if(!db_number($clientcompanypropertyid))
return '';
$sql = "select ((now()::date + '0 day'::interval)::date - attributehistorycreateddate::date) as days, *
from tblattributehistory
where clientcompanypropertyid = ".db_number($clientcompanypropertyid)."
and attributehistorycolumn = '".db_col($columnname)."'
order by attributehistoryid desc
limit 1";
$qry = db_first($sql);
if($qry)
{
$qry[days] = str_replace("days","","$qry[days]");
$qry[days] = str_replace("day","","$qry[days]");
$qry[days] = trim($qry[days] );
return "$qry[days]";
}
return "";
}
function clientproperty_attr($clientcompanypropertyid, $attr){
$sql = "select " . $attr . "
from tblclientcompanyproperty
where clientcompanypropertyid = " . db_number($clientcompanypropertyid);
$ccproperty = db_first($sql);
if($ccproperty)
return $ccproperty[$attr];
else
return false;
}
function get_clientcompanypropertyrole_clientstaffid_offers($clientcompanypropertyid, $roledisplayinofferslist = 0){
$sql = "select *
from tblclientcompanypropertyrole
join tblrole using (roleid)
where clientcompanypropertyid = " . db_number($clientcompanypropertyid) . "
and roleshowonoffers = 1";
if ($roledisplayinofferslist)
$sql .= " and roledisplayinofferslist = 1 ";
$staffids = db_query($sql);
if($staffids)
return $staffids;
else
return array();
}
function get_clientcompanypropertyrole_clientstaff($clientcompanypropertyid, $roledisplayinofferslist = 0){
$sql = "select tblclientstaff.*, roleid
from tblclientcompanypropertyrole
join tblrole using (roleid)
join tblclientstaff using (clientstaffid)
where clientcompanypropertyid = " . db_number($clientcompanypropertyid) . "
and roleshowonoffers = 1";
if ($roledisplayinofferslist)
$sql .= " and roledisplayinofferslist = 1 ";
$staffids = db_query($sql);
if($staffids)
return $staffids;
else
return array();
}
function get_clientcompanypropertyrole_roles_offerlist($clientcompanypropertyidarr){
$sql = "select *
from tblrole
where clientcompanyid in
(
select clientcompanyid
from tblclientcompanyproperty
where clientcompanypropertyid in ( ".implode(",", $clientcompanypropertyidarr)." )
)
and roledisplayinofferslist = 1";
$roleids = db_query($sql);
if($roleids)
return $roleids;
else
return array();
}
function get_clientcompanypropertyrole_clientstaffid($clientcompanypropertyid,$roleid)
{
if(!$roleid)
return 0;
$sql = "select *
from tblclientcompanypropertyrole
where clientcompanypropertyid = " . db_number($clientcompanypropertyid) . "
and roleid = " . db_number($roleid);
$existqry = db_first($sql,0,1);
if($existqry)
return $existqry[clientstaffid];
$sql = "select clientstaffid
from tblclientcompanypropertyrole
where clientcompanypropertyid = " . db_number($clientcompanypropertyid);
$cstaffqry = db_query($sql,0,1);
//get all staff in a role with this property
foreach($cstaffqry as $k => $cid)
$clientstaffparent[] = $cid[clientstaffid];
//loop through each and find the boss of the one assigned
while($clientstaffparent)
{// get any parents that have roles with the ability to to view their subs
$sql = "select * from tblclientstaff
join tbllogin using(clientstaffid)
join tblloginrole using(loginid)
join tblrole using(roleid)
where clientstaffid in ( ".implode(",",$clientstaffparent).")
and rolepropertyrestrictions = ".db_tick(VISIBLETOSUBS)."";
$ccstaffqry = db_query($sql);
//get a list of all of the parents
$clientstaffparent=array();
foreach($ccstaffqry as $staff)
{
if($staff[roleid] == $roleid)
return $staff[clientstaffid];
if($staff[clientstaffparent]>0)
$clientstaffparent[] = $staff[clientstaffparent];
}
if($ii++>100)
ddie("TOO MANY LOOPS");
}
return 0;
}
function update_clientcompanypropertyrole($clientcompanypropertyid,$roleid,$clientstaffid)
{
$sql = "
select *
from tblclientcompanypropertyrole
where clientcompanypropertyid = ".db_number($clientcompanypropertyid)."
and roleid = ".db_number($roleid);
$existqry = db_first($sql,0,1);
if(!$clientstaffid && !$existqry )
return false; //not updated
if($existqry && (db_number($existqry[clientstaffid]) == db_number($clientstaffid)))
{
return false; //not updated
}
$sql = "select rolename from tblrole where roleid = ".db_number($roleid);
$rolenameqry = db_first($sql,0,1);
$rolename = $rolenameqry[rolename];
$sql = "select clientstafffname||' '||clientstafflname as clientstaffname from tblclientstaff where clientstaffid = ".db_number($clientstaffid);
$cstaffnameqry = db_first($sql,0,1);
$clientstaffname = $cstaffnameqry[clientstaffname];
if($existqry)
{
$sql = "
update tblclientcompanypropertyrole
set clientstaffid = " . db_number($clientstaffid)."
where clientcompanypropertyid = ".db_number($clientcompanypropertyid)."
and roleid = ".db_number($roleid);
}
else
{
$sql = "
insert into tblclientcompanypropertyrole
(
clientcompanypropertyid
, roleid
, clientstaffid
)
values
(
".db_number($clientcompanypropertyid)."
, ".db_number($roleid)."
, ".db_number($clientstaffid)."
)";
}
db_exec($sql,1);
$sql = "
insert into tblattributehistory
(attributehistorycolumn, attributehistoryvalue, clientcompanypropertyid, attributehistorycreatedby)
values(".db_tick($rolename).",".db_tick($clientstaffname).",".db_number($clientcompanypropertyid).", ".loginid().")";
db_exec($sql);
$event = get_workflowevent("propertyrolechanged");
workflowevent($event[workfloweventshortname],clientcompanyproperty,$clientcompanypropertyid); //trigger the event
}
function update_clientcompanypropertycrole($clientcompanypropertyid,$croleid,$contactid)
{
$sql = "select * from tblclientcompanypropertycrole where clientcompanypropertyid = ".db_number($clientcompanypropertyid)." and
croleid = ".db_number($croleid);
$existqry = db_first($sql,0,1);
if($existqry && $existqry[contactid] == $contactid)
{
return false; //not updated
}
$sql = "select crolename from tblcrole where croleid = ".db_number($croleid);
$rolenameqry = db_first($sql,0,1);
$rolename = $rolenameqry[crolename];
$sql = "select contactcname from tblcontact where contactid = ".db_number($contactid);
$c = db_first($sql,0,1);
$contactcname = $c[contactcname];
if($existqry)
{
$sql = "update tblclientcompanypropertycrole set contactid = " . db_number($contactid)."
where clientcompanypropertyid = ".db_number($clientcompanypropertyid)."
and croleid = ".db_number($croleid);
db_exec($sql,1);
}
else
{
$sql = "insert into tblclientcompanypropertycrole (
clientcompanypropertyid
,croleid
,contactid
)values(
".db_number($clientcompanypropertyid)."
,".db_number($croleid)."
,".db_number($contactid)."
)";
db_exec($sql,1);
}
$sql = "insert into tblattributehistory (attributehistorycolumn, attributehistoryvalue, clientcompanypropertyid, attributehistorycreatedby)
values(".db_tick($rolename)."
,".db_tick($contactcname)."
,".db_number($clientcompanypropertyid)."
, ".loginid().")";
db_exec($sql);
$event = get_workflowevent("propertycrolechanged"); // as of 12/26/ there is no event that triggers when a user changes the contact for a client
if($event)
workflowevent($event[workfloweventshortname],clientcompanyproperty,$clientcompanypropertyid); //trigger the event
}
function clientcompanyproperty_set_column($clientcompanypropertyid,$columnname,$updateval)
{
if($updateval===0||$updateval==="0") //if the value is set to 0, go ahead and update it to be 0 (which is different than updating it to
;
elseif(!$updateval)
return false;
if(is_array($updateval))//make this so that they can just pass in an array and we'll extract the value if there is one
{
if(!isset($updateval[$columnname])) //if we dont find a field here we need to show a problem
return false;
$updateval = $updateval[$columnname];
}
$existval = clientcompanyproperty_get_column($clientcompanypropertyid,$columnname);
if($existval===false)
return null; //this is an invalid property, skip
if(trim($updateval)==trim($existval))
{
//d("Existing column not changed","trim($newval)===trim($existval)");
return false; //not updated
}
//an update neeeds to happen
$sql = "update tblclientcompanyproperty set $columnname = '".db_col($updateval)."' where clientcompanypropertyid = ".db_number($clientcompanypropertyid);
db_exec($sql,1);
if(trim($columnname) == "clientcompanyclientid") // we want the name not the id for tblattributehistory
{
$sql = "select clientcompanyclientname from tblclientcompanyclient where clientcompanyclientid = ".db_number($updateval);
$nameqry = db_first($sql,0,1);
$updateval = $nameqry[clientcompanyclientname];
}
if(trim($columnname) == "clientcompanypropertylistingagent_clientid") // we want the name not the id for tblattributehistory
{
$sql = "select clientcompanyname as clientcompanypropertylistingagent_clientname from tblclient where clientid = ".db_number($updateval);
$nameqry = db_first($sql,0,1);
$updateval = $nameqry[clientcompanypropertylistingagent_clientname];
}
if($columnname == "portfolioid") // we want the name not the id for tblattributehistory
{
$sql = "select portfolioname from tblportfolio where portfolioid = ".db_number($updateval);
$nameqry = db_first($sql,0,1);
$updateval = $nameqry[portfolioname];
}
$sql = "insert into tblattributehistory (attributehistorycolumn, attributehistoryvalue, clientcompanypropertyid, attributehistorycreatedby)
values('$columnname','".db_col($updateval)."',$clientcompanypropertyid, ".loginid().")";
db_exec($sql);
//find an event based on the name of the field that is being 'changed' if we find the event, trigger it.
$tmpcolumnname=$columnname;
$event = get_workflowevent($tmpcolumnname."change");
if(!$event)
{
$tmpcolumnname=str_replace("clientcompany","",$tmpcolumnname);
$event = get_workflowevent($tmpcolumnname."change");
}
if($event)
workflowevent($event[workfloweventshortname],clientcompanyproperty,$clientcompanypropertyid); //trigger the event
}
/**
* @name clientcompanyproperty_set_propertyextension
* @param integer $clientcompanypropertyid
* @param string | array $propertyextensionkey
* @param unknown $updateval
* @param string | array $extra1
*/
function clientcompanyproperty_set_propertyextension($clientcompanypropertyid,$propertyextensionkey,$updateval,$extra1="")
{
/* $extra1 is supposed to be array and this is how its used
* key is tblattributehistory column name
* value inside the key is value that will be inserted in that column
* this function will not validate does column exists or not but it will try to guess dataformat
* @example: $extra1[clienttaskid]=1123 this will insert 1123 into column clienttaskid
* ARRAY RULES:
* You can pass ONLY 1 extra1 column for ALL extensions
*/
if (!db_number($clientcompanypropertyid))
developer_error("A blank clientcompanypropertyid was passed into function clientcompanyproperty_set_propertyextension", "Please investigate why this is happening.");
if (!$propertyextensionkey)
{
//added this check because blank parameters are getting passed in and causing errors with array_keys below
developer_error("A blank propertyextensionkey was passed into function clientcompanyproperty_set_propertyextension"
, "An update to a property extension was attempted without the property extension key needed to update it.");
return;
}
if (is_array($extra1)){
if($extra1[forcetrackchanges])
$forcetrackchanges=1;
unset($extra1[forcetrackchanges]);
}
if (is_array($extra1))
{
$attrcolumnname = strtolower (array_shift(array_keys ($extra1)));
$attrcolumnval = $extra1[$attrcolumnname];
}
$looparr = $propertyextensionkey;
if(!is_array($looparr))
$looparr = array($propertyextensionkey=>$updateval);
foreach ($looparr as $key=>$val)
$dbsafearr[db_col($key)] = $val;
$existing = db_query("select propertyextensionentryid, propertyextensionkey from tblpropertyextensionentry where propertyextensionkey in ('".implode("','",array_keys($dbsafearr))."') and clientcompanypropertyid = ".db_number($clientcompanypropertyid),1,0,propertyextensionkey);
$event_propertyattributechanged = get_workflowevent(propertyattributechanged);
foreach ($looparr as $propertyextensionkey=>$value)
{
//The following if statement was added to prevent the db error caused when inserting or updating because the propertyextensionentryvalue is limited to 5000 characters. MAC 10/17/2013
if($value && (strlen($value) >5000))
$value=substr($value,0,5000);
if ($existing[$propertyextensionkey])
{
if(trim($value) === "")
$sql .= " DELETE from tblpropertyextensionentry where propertyextensionentryid=".db_number($existing[$propertyextensionkey][propertyextensionentryid]).";\n";
else
$sql .= " UPDATE tblpropertyextensionentry set propertyextensionentryvalue='".db_col($value)."' where propertyextensionentryid=".db_number($existing[$propertyextensionkey][propertyextensionentryid]).";\n";
}
elseif(trim($value) !== "")
{
$sql .= " INSERT INTO tblpropertyextensionentry (propertyid,clientcompanypropertyid,propertyextensionkey,propertyextensionentryvalue)
VALUES (0,".db_number($clientcompanypropertyid).",'".db_col($propertyextensionkey)."','".db_col($value)."');\n";
}
if(!$event_propertyattributechanged)
$event_propertyattributechanged = get_workflowevent(propertyattributechanged);
if (clientcompanyproperty_propertyattributechanged($clientcompanypropertyid,$propertyextensionkey,$value, $event_propertyattributechanged))
$changed =1;
else
unset($changed);
// now we update history table ..
$inserthistory=0;
if($forcetrackchanges && $changed)
$inserthistory=1;
if(!$inserthistory && $changed && !isset($trackchangehistory))
$trackchangehistory =get_propertyextensions_byconfigtypematch("trackchangehistory",'y');
if($trackchangehistory[$propertyextensionkey] && $changed)
$inserthistory=1;
//other critiera for determining if the history should be inserted goes here.
if(!$inserthistory && !isset($valuationkeys) && $changed)
$valuationkeys =get_propertyextensions_byconfigtypematch("valuationdata",'y');
if($valuationkeys[$propertyextensionkey] && $changed)
$inserthistory=1;
if($inserthistory)
{
$sql .= " INSERT INTO tblattributehistory (attributehistorycolumn
, attributehistoryvalue
, clientcompanypropertyid
, attributehistorycreatedby";
if ($attrcolumnname)
$sql .= " , ".db_col($attrcolumnname)." ";
$sql .= " )
VALUES ('".db_col($propertyextensionkey)."'
,'".db_col($value)."'
,".db_number($clientcompanypropertyid)."
,".db_number(loginid());
if ($attrcolumnname){
$sql .= " ,".(preg_match("/id$/",$attrcolumnname)? db_number($attrcolumnval) : db_col($attrcolumnval));
}
$sql .=" );\n";
}
}
if($sql)
$sql ="BEGIN;\n".$sql."\nCOMMIT;";
if($sql)
{
db_exec($sql,1); // This is commented out untill we are sure that this works
unset($sql);
}
}
function clientcompanyproperty_propertyattributechanged($clientcompanypropertyid,$columnname,$updateval, $event )
{
if(!$updateval)
return false;
if(is_array($updateval))//make this so that they can just pass in an array and we'll extract the value if there is one
{
if(!isset($updateval[$columnname])) //if we dont find a field here we need to show a problem
return false;
$updateval = $updateval[$columnname];
}
$existval = clientcompanyproperty_get_propertyextension($clientcompanypropertyid,$columnname);
//need to do some formatting changes to make sure that dollar formatted values dont end up forceing a false change (74000!=$74,000.00)
$check_updateval = strip_dollar_format($updateval);
$check_existval = strip_dollar_format($existval);
if($check_updateval && is_numeric($check_updateval) && is_numeric($check_existval))
{
if(dollar_format($check_updateval) == dollar_format($check_existval) )
return false;
}
if(trim($updateval)===trim($existval))
return false; //not updated
if($event)
workflowevent($event[workfloweventshortname],clientcompanyproperty,$clientcompanypropertyid, $columnname, $updateval); //trigger the event
return true;
}
function clientcompanyproperty_get_column($clientcompanypropertyid,$columnname)
{
$sql="select * from tblclientcompanyproperty where clientcompanypropertyid = ".db_number($clientcompanypropertyid);
$qry = db_first($sql);
if(!$qry)
return false; //invalid propertyid, return false
if(!array_key_exists($columnname,$qry)) //there is no column with this name in this table
ddie("Invalid Call to clientcompanyproperty_get_column($clientcompanypropertyid,$columnname)",$qry);
return $qry[$columnname];
}
function clientcompanyproperty_get_propertyextension($clientcompanypropertyid,$propertyextensionkey)
{
$sql="select propertyextensionentryvalue
from tblpropertyextensionentry
where clientcompanypropertyid = ".db_number($clientcompanypropertyid)." and propertyextensionkey = ".db_tick($propertyextensionkey);
$qry = db_firstval($sql);
if(!$qry)
return false; //invalid propertyid, return false
return $qry;
}
function clientcompanyproperty_assignmentfilter_selector($fieldname,$val)
{
if(is_array($val))
$val = $val[$fieldname];
if($_POST[$fieldname]) //if the form was posted, lets use the value from the posted form
$val = $val[$fieldname];
if(!$val)
$val=0;
$disp=none;
if($val)
{
$disp='';
$foundvalue=1;
list($cnt,$status,$assignmentmethodid) = explode("|",$val);
}
//this variable needs to have all three in order to be able to search by
//this function returns and array of values so that it can be used in several ways.
// form field
// sql to use for restricting to properties matching
// text used to display in english what has been searched for.
ob_start();
//build options for display in text and input
$ids=array();
if($foundvalue)
{
$sql ="
select clientcompanypropertyid
, case when clientcompanypropertyassignmentcompleteddate is not null then 'complete'
when clientcompanypropertyassignmentrevokeddate is not null then 'pulled'
when clientcompanypropertyassignmentdenieddate is not null then 'declined'
when clientcompanypropertyassignmentexpired is not null then 'expired'
when clientcompanypropertyassignmentaccepteddate is not null then 'active' else 'pending'
end as status
from tblclientcompanypropertyassignment
join tblclientcompanyproperty using(clientcompanypropertyid)
where 0=0
and clientcompanyid = ".clientcompanyid()."
";
if($assignmentmethodid)
$sql .=" and assignmentmethodid = ".db_number($assignmentmethodid) ;
if($status)
{
$sql = " select * from ($sql) t where status in ('".implode("','",explode(",",$status))."')";
}
$sql = " select clientcompanypropertyid from ($sql) t";
if($cnt)
$sql = "select clientcompanypropertyid from tblclientcompanyproperty where clientcompanyid = ".clientcompanyid()." and clientcompanypropertyid not in ($sql) ";
$ids = array_keys(db_query($sql,1,0,clientcompanypropertyid));
if(!$ids) //if we are supposed to have a value, we can't return an empty or it will not filter it correctly.
$ids=-1;
}
$statusopts[active] = "Active";
$statusopts[pending] = "Pending";
$statusopts[complete] = "Completed";
$statusopts[pulled] = "Pulled";
$statusopts[expired] = "Expired";
$statusopts[declined] = "Declined";
$cntopts[0] = "With at least one";
$cntopts[1] = "Without any";
$qry = db_query("select * from tblassignmentmethod where clientcompanyid = ".clientcompanyid()." and assignmentmethodinactivedate is null order by assignmentmethodname");
$assnopts[0] = "All";
foreach($qry as $opt)
$assnopts[$opt[assignmentmethodid]]=$opt[assignmentmethodname] ;
$statusarr=array();
if($status)
{
$statusarr=explode(",",$status);
$statusdisplay = implode(' or ',$statusarr) ;
}
if($assignmentmethodid)
$assndisplay = $assnopts[$assignmentmethodid] ;
if(!$cntopts[$cnt]) $cnt=0;
if($assndisplay) $assndisplay=" ".$assndisplay ;
if($statusdisplay) $statusdisplay=" ".$statusdisplay ;
$text = "Properties: ".$cntopts[$cnt].$statusdisplay.$assndisplay." assignment";
if($cnt)
$text.="s";
if(!$foundvalue)
$text = 'No filters based on assignments';
echo "
";
$smallinput = ob_get_clean();
$arr[formfield]=$input;
$arr[displaytext]=$text;
$arr[smallformfield]=$smallinput;
$arr[sql]=$sql;
$arr[clientcompanypropertyids]=$ids;
$arr[assignmentmethodid] = $assignmentmethodid;
return $arr;
}
/**
* @name get_activeassignedproperties
* @param unknown $assignmentid
* @return unknown
* @uses functions_db.php
*/
function get_activeassignedproperties($assignedstaffid,$clientcompanyid=0,$assignedaccountstaffid=0){
if ($assignedaccountstaffid)
$addsql = " accountstaffid";
else
$addsql = " staffid";
$sql = "select ".$addsql.", count (distinct(clientcompanypropertyid)) as properties
from tblclientcompanypropertyassignment
where 0=0
";
if ($assignedstaffid)
$sql .=" and staffid in( ".db_number_list($assignedstaffid).")";
if ($assignedaccountstaffid)
$sql .=" and accountstaffid in (".db_number_list($assignedaccountstaffid).")";
$sql .=" and clientcompanypropertyassignmentaccepteddate is not null
and clientcompanypropertyassignmentdenieddate is null
and clientcompanypropertyassignmentrevokeddate is null
and clientcompanypropertyassignmentcompleteddate is null
and clientcompanypropertyassignmentexpired is null";
if ($clientcompanyid >0)
$sql .=" and clientcompanypropertyid in (select clientcompanypropertyid from tblclientcompanyproperty where clientcompanypropertyid=tblclientcompanypropertyassignment.clientcompanypropertyid and clientcompanyid =".db_number($clientcompanyid).") ";
$sql .=" group by ".$addsql."
order by ".$addsql;
$out = db_query($sql);
return $out;
}
function check_historyforcategory($clientcompanypropertyid,$categoryid,$tabcategoryid, $taskonly=0){
if (db_number($clientcompanypropertyid) == 0 || (db_number($categoryid) ==0 && db_number($tabcategoryid) == 0))
return false; // bad data
$sql = "select * from tblattributehistory
where attributehistorycolumn in ( select propertyextensionkey
from tblpropertyextension
where propertyextensionid in ( select propertyextensionid
from tbl".($tabcategoryid ? "tabcategorypropertyextension" : "propertyextension" );
if ($tabcategoryid)
$sql .=" where propertytabcategoryid = ".db_number($tabcategoryid)." ";
if ($categoryid)
$sql .=" where propertycategoryid = ".db_number($categoryid);
$sql .=" )
)
and clientcompanypropertyid =".db_number($clientcompanypropertyid);
if($taskonly)
$sql.= " and clienttaskid > 0 ";
return db_query($sql);
}
function clientcompanyproperty_croles($clientcompanypropertyid, $includeinactive=0)
{
$sql = "select *
from tblclientcompanypropertycrole
join tblcrole using(croleid)
join tblcontact using(contactid)
where clientcompanypropertyid = ".db_number($clientcompanypropertyid)."
and clientcompanypropertyid > 0
";
if(!$includeinactive)
$sql.=" and croleinactive is NULL";
$sql.=" order by crolename ";
$qry = db_query($sql,1,0,croleid);
return $qry;
}
function clientcompanyclient_croles($clientcompanyclientid,$includeinactive=0)
{
$sql = "select *
from tblclientcompanyclientcrole
join tblcrole using(croleid)
where clientcompanyclientid = ".db_number($clientcompanyclientid)."
and clientcompanyclientid > 0
";
if(!$includeinactive)
$sql.=" and croleinactive is NULL";
$sql.=" order by crolename ";
$qry = db_query($sql,1,0,croleid);
return $qry;
}
function clientcompanyproperty_pageactions($clientcompanyid)
{
$arr[10][cenbam]=array(page=>'clientproperty_bulkupdate.php', button=> '', label=> 'Safeguard Update' );
$arr[10][cenbam][updatemap]["reorekeydate"] = propertyextensionsafeguardrekeydate;
$arr[10][cenbam][updatemap]["reotrashoutdate"] = propertyextensionsafeguardtrashoutdate;
$arr[10][cenbam][updatemap]["reowinterizedate"] = propertyextensionsafeguardwinterizationdate;
$arr[10][cenbam][updatemap]["lastgrasscutdate"] = propertyextensiongrasscutdate;
$arr[10][cenbam][updatefrom]=array('loan'=>propertyextensionloannumber);
$arr[7] = $arr[10];
$arr= $arr[$clientcompanyid];
if(!$arr)
return array();
return $arr;
}
function clientcompanyproperty_pageaction_run($clientcompanyid, $pageaction)
{
$actions = clientcompanyproperty_pageactions($clientcompanyid);
$action = $actions[$pageaction];
if(!$action)
ddie("Invalid pageaction: $pageaction");
if(!file_exists($action[page]))
return false;
extract($action);
include($action[page]);
return true;
}
function clientproperty_ext_save($clientcompanypropertyid,$key,$value,$noupdate_keys='')
{
if (trim($clientcompanypropertyid) == '' || empty($key) )
return false;
if (is_array($value))
developer_error('Array passed in for clientproperty_ext_save variable $value', 'There was an array passed in for the parameter $value which is not set up to be handled inside of this function. This will cause issues with the trim function later on, and more importantly is not handled with the database. This message is included to trace back to the file that called this function');
$keys=$key;
if(is_array($keys) && $value!==false)
developer_error("Invalid call to clientproperty_ext_save, if you pass an array in to \$key then you must pass the boolean false into \$value");
if(!is_array($keys))
$keys = array($key=>$value);
$value = "$value";
$extens = db_query("select propertyextensionkey as key, propertyextensionentryid id from tblpropertyextensionentry where clientcompanypropertyid = {$clientcompanypropertyid} and propertyextensionkey in ('".implode("','",array_keys($keys))."')",1,0,'key');
foreach($keys as $key=>$value)
{
$extenid = $extens[$key][id];
if ($extenid)
{
//we are removing the entry if blank
//to help keep the table size down
//if we have data here and we have
//instructions to not update the field
if ($noupdate_keys[$key] && $extens[$key])
continue;
else if (trim($value) == '')
{
$sql .= ";
delete from tblpropertyextensionentry
where propertyextensionentryid = {$extenid}";
}
//otherwise update
else
{
$sql .= ";
update tblpropertyextensionentry
set propertyextensionentryvalue = ".db_tick($value)."
where propertyextensionentryid = {$extenid}";
}
}
else
{
//don't insert new blank values
//to help keep table sizes down
if (trim($value) == '')
continue;
if (!is_string($value) && !is_numeric($value) )
continue;
$sql .= ";
insert into tblpropertyextensionentry (clientcompanypropertyid, propertyextensionkey, propertyextensionentryvalue)
values ($clientcompanypropertyid, ".db_tick($key).", ".db_tick($value).")";
}
}
if($sql)
$rval = db_exec($sql, 1);
return $rval;
}
function clientcompanyproperty_ext_save($clientcompanypropertyid,$key,$value)
{
$sql = "insert into tblpropertyextensionextentry (clientcompanypropertyid, propertyextensionkey)
select $clientcompanypropertyid, '$key'
where not exists (
select 1 from tblpropertyextensionextentry
where clientcompanypropertyid = $clientcompanypropertyid
and propertyextensionkey = '$key'
)
";
db_exec($sql,1);
$sql = "update tblpropertyextensionextentry
set propertyextensionentryextvalue= ".db_tick($value)."
where clientcompanypropertyid = $clientcompanypropertyid
and propertyextensionkey = '$key' ";
db_exec($sql,1);
}
function clientcompanyproperty_ext_merge($propertyarr)
{
if($propertyarr[clientcompanypropertyid])
$arr[] = $propertyarr;
else
$arr = $propertyarr;
foreach($arr as $k=>$property)
$propids[] = $property[clientcompanypropertyid];
$sql = "select * from tblpropertyextensionextentry where clientcompanypropertyid in (".db_number_list($propids).")";
$qry = db_query($sql);
foreach($qry as $row)
$aarr[$row[clientcompanypropertyid]][$row[propertyextensionkey]]=$row[propertyextensionentryextvalue];
foreach($propertyarr as &$property)
if($aarr[$property[clientcompanypropertyid]])
$property = array_merge($property,$aarr[$property[clientcompanypropertyid]]);
return $propertyarr;
}
function hoa_name($hoaid)
{
$sql = "select hoaname from tblhoa where hoaid = ".db_number($hoaid);
$hoa = db_first($sql);
return $hoa[hoaname];
}
function clientcompanyproperty_getadditionaldata($clientcompanypropertyid)
{
$sql = "select *
, case
when staffid> 0 then (select companyname from tblcompany join tbllogin lc using(companyid) where staffid = c.staffid)
when accountstaffid> 0 then (select accountname from tblaccount join tblaccountstaff lc using(accountid) where accountstaffid = c.accountstaffid)
when externaluserid> 0 then (select externalusercompanyname from tblexternaluser where externaluserid = c.externaluserid)
end as companyname
from tblclientcompanypropertyassignment a
join tblclientcompanyproperty b using(clientcompanypropertyid)
join vw_assigntypes c using(accountstaffid, staffid, externaluserid,assignmentaliasclientstaffid)
join tblassignmentmethod d using (assignmentmethodid)
where clientcompanypropertyid = $clientcompanypropertyid
and d.clientcompanyid=b.clientcompanyid
and clientcompanypropertyassignmentexpired is null
and clientcompanypropertyassignmentrevokeddate is null
and clientcompanypropertyassignmentaccepteddate is not null
and clientcompanypropertyassignmentaccepteddate in
(
select max (clientcompanypropertyassignmentaccepteddate) as clientcompanypropertyassignmentaccepteddate
from tblclientcompanypropertyassignment ta
join tblclientcompanyproperty tb using(clientcompanypropertyid)
join vw_assigntypes tc using(accountstaffid, staffid, externaluserid,assignmentaliasclientstaffid)
join tblassignmentmethod td using (assignmentmethodid)
where clientcompanypropertyid = $clientcompanypropertyid
and td.clientcompanyid=tb.clientcompanyid
and clientcompanypropertyassignmentexpired is null
and clientcompanypropertyassignmentrevokeddate is null
and clientcompanypropertyassignmentaccepteddate is not null
group by assignmentmethodid
)";
$sql2 = "select assignmentmethodname
,assignmentmethodname as assignname
,assignmentmethodname as assignphone
,assignmentmethodname as assigncell
,assignmentmethodname as assignemail
,assignmentmethodname as assigncompanyname
from tblassignmentmethod where assignmentmethodinactivedate is null and clientcompanyid = ".clientcompanyid();
if($clientcompanypropertyid<0)
{
$sql = $sql2;
}
$assigns = db_query($sql);
foreach($assigns as $assign)
{
$ids[]=$assign[assignmentmethodid];
$amname=$assign[assignmentmethodname];
$amname = strtolower($amname);
$amname = str_replace(' ','',$amname);
$amname = str_replace("'",'',$amname);
$amname = str_replace('"','',$amname);
$kam = 'assigned'.$amname;
$replaceqrynewarr[$kam.name]=$assign[assignname];
$replaceqrynewarr[$kam.phone]=$assign[assignphone];
$replaceqrynewarr[$kam.cell]=$assign[assigncell];
$replaceqrynewarr[$kam.email]=$assign[assignemail];
$replaceqrynewarr[$kam.companyname]=$assign[assignemail];
if(isset($assign[staffid]))
$replaceqrynewarr[$kam.companyname]=get_assignedcompanyname($assign[staffid],$assign[accountstaffid],$assign[externaluserid],$assign['assignmentaliasclientstaffid']);
}
return $replaceqrynewarr;
}
function clientcompanyproperty_get_attributehistory($clientcompanypropertyid)
{
$db_clientcompanypropertyid = db_number($clientcompanypropertyid);
//get attribute history if we have one
if ($db_clientcompanypropertyid)
{
$title = "The date and time this field was last changed on.";
$sql = "
select attributehistorycolumn, max(attributehistorycreateddate) as attributehistorycreateddate
from tblattributehistory
where 0=0";
if ($db_clientcompanypropertyid)
$sql .= "
and clientcompanypropertyid = $db_clientcompanypropertyid";
$sql .= "
group by attributehistorycolumn";
if ($sql)
$attr_history = db_query($sql, 1, 0, attributehistorycolumn);
foreach ($attr_history as $key => $value)
$attr_history[$key][formatteddate] = "".dttime_format($attr_history[$key][attributehistorycreateddate])."";
return $attr_history;
}
}
function prep_vhtmlreport($propertyid,$html){
global $property;
exec( "find " . $_SERVER['DOCUMENT_ROOT'] . '/clientcompanylogos/ -name "' . clientcompanyid() . '.*"',$logopath);
$property = db_first ("select * from tblclientcompanyproperty where clientcompanypropertyid = ".db_number($propertyid));
$property[logopath]=str_replace($_SERVER['DOCUMENT_ROOT'] ."/","",array_shift($logopath));
$property = array_merge($property,db_first("select * from tblclientcompany where clientcompanyid =".clientcompanyid()));
$tmp= get_clientcompanyclient($property[clientcompanyclientid]);
$property[clientcompanyclientid] =$tmp[clientcompanyclientname];
unset($tmp);
$property[clientcompanypropertyupdated] = array_shift(explode(".",$property[clientcompanypropertyupdated]));
$property[clientcompanypropertyupdatedby] = get_loginusername($property[clientcompanypropertyupdatedby]);
$sql = "select propertyextensionentryvalue, propertyextensionkey, clientcompanypropertyid
from tblpropertyextensionentry
where clientcompanypropertyid =".db_number($propertyid);
$extensions = db_query($sql,1,0,propertyextensionkey);
foreach ($extensions as $key=>$extension){
$property[$key]=$extension[propertyextensionentryvalue];
}
$sql = "select name from tblrole
left join (select * from tblclientcompanypropertyrole
where clientcompanypropertyid = ".db_number($propertyid).") r using (roleid)
join (select clientstafffname|| ' '|| clientstafflname as name,clientstaffid from tblclientstaff) as clientstaff using (clientstaffid)
where clientcompanyid = ".db_number(clientcompanyid())."
and rolepropertyworking = 1
and rolename ilike 'Asset Manager'
order by rolename";
$property[assetmanager] = db_firstval($sql);
$property[propertyextensiondaysonmarket] = get_daystillnow($property[propertyextensionlistdate]);
$property[received] = dt_format(array_shift(explode(".",$property[clientcompanypropertycreated])));
$property[propertyextensiondaysininventory] = get_daystillnow($property[clientcompanypropertycreated]);
$property[propertyextensionyearbuilt] = get_yearstillnow($property[propertyextensionyearbuilt]);
$property[clientcompanypropertyapprtobpo] = apprtobpoval($property[propertyextensioninitialappraisalvalue],$property[propertyextensioninitialbpovalue]);
foreach ($property as $key=>$val){
if ((strstr($key,"value") || strstr($key,"price")) && is_numeric($val))
$property[$key]=number_format($val);
}
ob_start();
$property = array_merge($property,clientcompanyproperty_getadditionaldata($propertyid));
$null=ob_end_clean();
unset($null);
return $html;
}
/**
* get_marketingreports
* @param unknown $propertyid
*/
function get_marketingreports($propertyid){
$doctype = get_documenttype_byname('marketingreport');
$doctypeid = $doctype[documenttypeid];
$reportdocs = db_query("select documentid,documentname,documenttitle, documentloadedby,documentdateloaded from tbldocument where documenttypeid=".db_number($doctypeid)." and clientcompanypropertyid =".db_number($propertyid)." order by documentid desc");
foreach ($reportdocs as $key=>$document){
$filepath = get_document_full_path(db_firstval("select documentid from tbldocument where documentid=".db_number($document[documentid])));
$reportdocs[$key][filepath]="document/".end(explode("/document/",$filepath));
$reportdocs[$key][comment] = db_firstval("select marketingreportscomment from tblmarketingreportscomment where documentid=".db_number($document[documentid]));
}
unset ($doctype,$doctypeid,$filepath);
return $reportdocs;
}
/**
* @name apprtobpoval
* @param unknown $appraisal
* @param unknown $bpoval
* @return number
* @comment This function returns positive or negative %
*/
function apprtobpoval ($appraisal,$bpoval){
if ($bpoval>$appraisal){
$tmp=$appraisal;
$appraisal = db_number($bpoval);
$bpoval = db_number($tmp);
}
if ($appraisal >0){
$r=$bpoval/$appraisal;
$result = round($r,2);
$result = $result * 100;
}else
$result =0;
if (!$tmp)
$result = 0-$result;
return $result;
}
function marketingreportui(){
?>
Marketing Report
View Report
Create Report
if ($_SESSION[open_report]){
?>
unset ($_SESSION[open_report]);
}else{
echo " ";
}?>
}
function clientcompanyproperty_assignedtossp($clientcompanypropertyid)
{
if(!db_number($clientcompanypropertyid))
return FALSE;
$sql="select 1 from tblclientcompanyproperty
join tblclientcompanypropertyassignment using(clientcompanypropertyid)
join tblassignmentmethod using (assignmentmethodid)
where 0=0
and assignmentmethodactivessp=1
and clientcompanypropertyassignmentaccepteddate is not null
and clientcompanypropertyassignmentdenieddate is null
and clientcompanypropertyassignmentrevokeddate is null
and clientcompanypropertyid=".db_number($clientcompanypropertyid);
$assignedtossp=db_firstval($sql);
if($assignedtossp)
return TRUE;
return FALSE;
}
/**
* @name delegate
* @param integer $clientcompanypropertyid
* @param integer $sisterclientcompanyid
* @return boolean based on if delegated or not.
*/
function delegate($clientcompanypropertyid, $sisterclientcompanyid)
{
if(!db_number($clientcompanypropertyid) || !db_number($sisterclientcompanyid))
return FALSE;
if(isdelegatedtosister($clientcompanypropertyid, $sisterclientcompanyid))
return FALSE;
$sql = "insert into tbldelegation (delegationcreateddate, delegationcreatedby, delegationfromclientcompanypropertyid, delegationtoclientcompanypropertyid, delegationsisterclientcompanyid)
values (now(), ".loginid().", ".db_number($clientcompanypropertyid).", 0, ".db_number($sisterclientcompanyid).")";
db_exec($sql);
return TRUE;
}
/**
* @name isdelegatedtosister
* @param integer $clientcompanypropertyid
* @param integer $sisterclientcompanyid
* @return 1 or null based on if param $clientcompanypropertyid has delegated to param $sisterclientcompanyid.
* @comment This function is not being used anywhere as of 03/03/2015. - PB
*/
function isdelegatedtosister($clientcompanypropertyid, $sisterclientcompanyid)
{
$sql = "select 1 as isdelegatedtosister from tbldelegation
where delegationfromclientcompanypropertyid = ".db_number($clientcompanypropertyid)." and
delegationsisterclientcompanyid = ".db_number($sisterclientcompanyid);
$isdelegatedtosister = db_firstval($sql);
return $isdelegatedtosister;
}
/**
* @name isdelegatedandisnotremoved
* @param integer $clientcompanypropertyid
* @param integer $sisterclientcompanyid
* @return return
* @comment This function returns retval
*/
function isdelegatedandisnotremoved($clientcompanypropertyid, $sisterclientcompanyid)
{
$sql = "select 1 as isdelegatedandisnotremoved from tbldelegation
where delegationfromclientcompanypropertyid = ".db_number($clientcompanypropertyid)." and
delegationsisterclientcompanyid = ".db_number($sisterclientcompanyid)." and
delegationpulleddate is null
and delegationcompleteddate is null";
$isdelegatedandisnotremoved = db_firstval($sql);
return $isdelegatedandisnotremoved;
}
function get_delegationstatus($clientcompanypropertyid)
{
if(isdelegationproperty($clientcompanypropertyid))
return "Yes";
else
return "No";
}
/**
* @name delegationpropertysync
* @param integer $clientcompanypropertyid
* @return boolean based on success of function
* @comment This function syncs delegation properties in two ways: 1) if param $clientcompanypropertyid is a newly delegated property, createdelegatedproperty() is called; 2) otherwise,
* @comment all related delegation properties are sync'd across Basic Information and Property Attributes (i.e. property extensions).
* @comment Namely, data is sync'd in tblclientcompanyproperty, tblclientcompanypropertyextension, & tblpropertyextensionentry.
*/
function delegationpropertysync($clientcompanypropertyid)
{
if(!$clientcompanypropertyid)
return FALSE;
if(!isdelegationproperty($clientcompanypropertyid))
return FALSE;
//Check if newly delegated property.
$sql = "select * from tbldelegation where delegationfromclientcompanypropertyid = ".db_number($clientcompanypropertyid)." and delegationtoclientcompanypropertyid = 0";
$qry = db_first($sql);
if($qry)//Newly delegated property.
{
$newclientcompanypropertyid = createdelegatedproperty($clientcompanypropertyid, $qry[delegationsisterclientcompanyid]);
$sql = "update tbldelegation set delegationtoclientcompanypropertyid = $newclientcompanypropertyid where delegationid = ".$qry[delegationid];
db_exec($sql);
return TRUE;
}
$allrelateddelegationpropertiesinfo = array();
//Pull row to sync from.
$sql = "select * from tblclientcompanyproperty where clientcompanypropertyid = ".db_number($clientcompanypropertyid);
$row = db_first($sql);
$rootclientcompanypropertyid = getrootdelegationclientcompanypropertyid($clientcompanypropertyid);
$sql = "select clientcompanyid from tblclientcompanyproperty where clientcompanypropertyid = ".db_number($rootclientcompanypropertyid);
$rootclientcompanyid = db_firstval($sql);
$rootclientcompanypropertyinfo = array('delegationtoclientcompanypropertyid'=>$rootclientcompanypropertyid, 'delegationsisterclientcompanyid'=>$rootclientcompanyid);
array_push($allrelateddelegationpropertiesinfo, $rootclientcompanypropertyinfo);//Make an entry so that the root 'from' property can be looped through like all the 'to' properties.
getallchildrendelegationpropertiesinfo($rootclientcompanypropertyid, $allrelateddelegationpropertiesinfo);
$sql = "select 1 as hasclientcompanypropertyextension from tblclientcompanypropertyextension where clientcompanyid = ".db_number($row[clientcompanyid])." limit 1";
$ispropertyextensiondatatosync = db_firstval($sql);
if($ispropertyextensiondatatosync)
{ //Get property extension data to sync from.
$sql = "select propertyextensionkey, propertyextensionentryvalue from tblpropertyextensionentry
where propertyextensionkey in
(
select propertyextensionkey from tblclientcompanypropertyextension join tblpropertyextension using (propertyextensionid)
where clientcompanyid = ".db_number($row[clientcompanyid])."
)
and clientcompanypropertyid = ".db_number($clientcompanypropertyid);
$propertyextensionentrydatatosync = db_query($sql);
}
$sql = '';
foreach($allrelateddelegationpropertiesinfo as $relatedclientcompanypropertyinfo)
{
if($relatedclientcompanypropertyinfo[delegationtoclientcompanypropertyid] != $clientcompanypropertyid && $relatedclientcompanypropertyinfo[delegationtoclientcompanypropertyid] != 0)//Skip originating property and newly delegated properties (which should never get here).
{
$sql .= "update tblclientcompanyproperty
set
clientcompanypropertyaddress=". ($row[clientcompanypropertyaddress] ? "'".db_col($row[clientcompanypropertyaddress])."'" : "null") .",
clientcompanypropertyaddress2=". ($row[clientcompanypropertyaddress2] ? "'".db_col($row[clientcompanypropertyaddress2])."'" : "null") .",
clientcompanypropertycity=". ($row[clientcompanypropertycity] ? "'".db_col($row[clientcompanypropertycity])."'" : "null") .",
clientcompanypropertystate=". ($row[clientcompanypropertystate] ? "'".db_col($row[clientcompanypropertystate])."'" : "null") .",
clientcompanypropertyzip=". ($row[clientcompanypropertyzip] ? "'".db_col($row[clientcompanypropertyzip])."'" : "null") .","."
clientcompanypropertyupdated=". ($row[clientcompanypropertyupdated] ? "'".db_col($row[clientcompanypropertyupdated])."'" : "null") .",
clientcompanypropertyupdatedby=".db_number($row[clientcompanypropertyupdatedby]).",
clientcompanypropertyupdatedip=". ($row[clientcompanypropertyupdatedip] ? "'".db_col($row[clientcompanypropertyupdatedip])."'" : "null") .",
clientcompanypropertycounty=". ($row[clientcompanypropertycounty] ? "'".db_col($row[clientcompanypropertycounty])."'" : "null") .",
portfolioid=".db_number($row[portfolioid]).",
clientcompanypropertystatus=". ($row[clientcompanypropertystatus] ? "'".db_col($row[clientcompanypropertystatus])."'" : "null") .",
clientcompanypropertyassignedto=".db_number($row[clientcompanypropertyassignedto]).",
clientcompanyclientid=".db_number($row[clientcompanyclientid]).",
clientcompanypropertyassignmenttype=". ($row[clientcompanypropertyassignmenttype] ? "'".db_col($row[clientcompanypropertyassignmenttype])."'" : "null") .",
clientcompanypropertycloseddate=". ($row[clientcompanypropertycloseddate] ? "'".db_col($row[clientcompanypropertycloseddate])."'" : "null") .",
clientcompanypropertyclosedby=".db_number($row[clientcompanypropertyclosedby]).",
clientcompanypropertystatus2=". ($row[clientcompanypropertystatus2] ? "'".db_col($row[clientcompanypropertystatus2])."'" : "null") .",
clientcompanypropertytype=". ($row[clientcompanypropertytype] ? "'".db_col($row[clientcompanypropertytype])."'" : "null") .",
clientcompanyclientsubid=".db_number($row[clientcompanyclientsubid]).",
clientcompanypropertyrevieweddate=". ($row[clientcompanypropertyrevieweddate] ? "'".db_col($row[clientcompanypropertyrevieweddate])."'" : "null") .",
clientcompanypropertynotebiddingboard=".db_number($row[clientcompanypropertynotebiddingboard]).",
clientcompanypropertynotebiddingboardawardeddate=". ($row[clientcompanypropertynotebiddingboardawardeddate] ? "'".db_col($row[clientcompanypropertynotebiddingboardawardeddate])."'" : "null") .",
clientcompanypropertyoriginalcreatedate=". ($row[clientcompanypropertyoriginalcreatedate] ? "'".db_col($row[clientcompanypropertyoriginalcreatedate])."'" : "null") .",
clientcompanypropertylistingagent_clientid=".db_number($row[clientcompanypropertylistingagent_clientid]).",
clientcompanypropertysellerlistingagent_contactid=".db_number($row[clientcompanypropertysellerlistingagent_contactid])."
where clientcompanypropertyid = ".db_number($relatedclientcompanypropertyinfo[delegationtoclientcompanypropertyid]).";
";
if($ispropertyextensiondatatosync)
{
foreach($propertyextensionentrydatatosync as $propertyextensionentrydata)
{
$haspropertyextensionsql = "select clientcompanypropertyextensionid as haspropertyextension from tblclientcompanypropertyextension join tblpropertyextension using (propertyextensionid)
where clientcompanyid = ".db_number($relatedclientcompanypropertyinfo[delegationsisterclientcompanyid])."
and propertyextensionkey = '".db_col($propertyextensionentrydata[propertyextensionkey])."'";
$haspropertyextension = db_firstval($haspropertyextensionsql);
//If not in tblclientcompanypropertyextension, insert it.
if(!$haspropertyextension)
{
$sql .= " insert into tblclientcompanypropertyextension
(
propertyextensionid,
clientcompanyid,
clientcompanypropertyextensiondisplayintable,
clientcompanypropertyextensionrank,
clientcompanypropertyextensionvisibletocompany
)
select
propertyextensionid,
".db_number($relatedclientcompanypropertyinfo[delegationsisterclientcompanyid]).",
clientcompanypropertyextensiondisplayintable,
clientcompanypropertyextensionrank,
clientcompanypropertyextensionvisibletocompany
from tblclientcompanypropertyextension
where
clientcompanyid = ".db_number($row[clientcompanyid])." and propertyextensionid in
( select propertyextensionid from tblpropertyextension where propertyextensionkey = '".db_col($propertyextensionentrydata[propertyextensionkey])."' );
";
}
$haspropertyextensionentrysql = " select 1 as haspropertyextensionentry from tblpropertyextensionentry
where propertyextensionkey = '".db_col($propertyextensionentrydata[propertyextensionkey])."'
and clientcompanypropertyid = ".db_number($relatedclientcompanypropertyinfo[delegationtoclientcompanypropertyid]);
$haspropertyextensionentry = db_firstval($haspropertyextensionentrysql);
//If not in tblpropertyextensionentry, insert into it.
if(!$haspropertyextensionentry)
{
$sql .= " insert into tblpropertyextensionentry
(
propertyextensionentryvalue,
propertyextensionkey,
clientcompanypropertyid
)
values
(
". ($propertyextensionentrydata[propertyextensionentryvalue] ? "'".db_col($propertyextensionentrydata[propertyextensionentryvalue])."'" : "null") .",
". ($propertyextensionentrydata[propertyextensionkey] ? "'".db_col($propertyextensionentrydata[propertyextensionkey])."'" : "null") .",
". db_number($relatedclientcompanypropertyinfo[delegationtoclientcompanypropertyid])."
);
";
}
//Otherwise, update it.
else
{
$sql .= " update tblpropertyextensionentry
set propertyextensionentryvalue = ". ($propertyextensionentrydata[propertyextensionentryvalue] ? "'".db_col($propertyextensionentrydata[propertyextensionentryvalue])."'" : "null") ."
where propertyextensionkey = '". db_col($propertyextensionentrydata[propertyextensionkey])."'
and clientcompanypropertyid = ". db_number($relatedclientcompanypropertyinfo[delegationtoclientcompanypropertyid]).";
";
}
}
}
}
}//end foreach $allrelateddelegationpropertiesinfo
if($sql)
{
$qry = db_exec($sql, 1);
return $qry;
}
}
/**
* @name createdelegatedproperty
* @param integer $clientcompanypropertyid
* @param integer $sisterclientcompanyid
* @return integer $newclientcompanypropertyid
* @comment This function inserts a new delegated property into tblclientcompanyproperty.
*/
function createdelegatedproperty($clientcompanypropertyid, $sisterclientcompanyid)
{
$sql = "insert into tblclientcompanyproperty
(
clientcompanypropertyaddress,
clientcompanypropertyaddress2,
clientcompanypropertycity,
clientcompanypropertystate,
clientcompanypropertyzip,
clientcompanypropertycreated,
clientcompanypropertycreatedby,
clientcompanypropertycreatedip,
clientcompanypropertyupdated,
clientcompanypropertyupdatedby,
clientcompanypropertyupdatedip,
clientcompanypropertycounty,
clientcompanyid,
portfolioid,
clientcompanypropertystatus,
clientcompanypropertyassignedto,
clientcompanyclientid,
clientcompanypropertyassignmenttype,
clientcompanypropertycloseddate,
clientcompanypropertyclosedby,
clientcompanypropertystatus2,
clientcompanypropertytype,
clientcompanyclientsubid,
clientcompanypropertyrevieweddate,
clientcompanypropertynotebiddingboard,
clientcompanypropertynotebiddingboardawardeddate,
clientcompanypropertyoriginalcreatedate,
clientcompanypropertylistingagent_clientid,
clientcompanypropertysellerlistingagent_contactid
)
select
clientcompanypropertyaddress,
clientcompanypropertyaddress2,
clientcompanypropertycity,
clientcompanypropertystate,
clientcompanypropertyzip,
clientcompanypropertycreated,
clientcompanypropertycreatedby,
clientcompanypropertycreatedip,
clientcompanypropertyupdated,
clientcompanypropertyupdatedby,
clientcompanypropertyupdatedip,
clientcompanypropertycounty,
".db_number($sisterclientcompanyid).",
portfolioid,
clientcompanypropertystatus,
clientcompanypropertyassignedto,
clientcompanyclientid,
clientcompanypropertyassignmenttype,
clientcompanypropertycloseddate,
clientcompanypropertyclosedby,
clientcompanypropertystatus2,
clientcompanypropertytype,
clientcompanyclientsubid,
clientcompanypropertyrevieweddate,
clientcompanypropertynotebiddingboard,
clientcompanypropertynotebiddingboardawardeddate,
clientcompanypropertyoriginalcreatedate,
clientcompanypropertylistingagent_clientid,
clientcompanypropertysellerlistingagent_contactid
from tblclientcompanyproperty
where clientcompanypropertyid=".db_number($clientcompanypropertyid)."
returning clientcompanypropertyid";
$newclientcompanypropertyid = db_firstval($sql);
return $newclientcompanypropertyid;
}
/**
* @name getallchildrendelegationpropertiesinfo
* @param integer $delegationfromclientcompanypropertyid
* @param array &$allrelateddelegationpropertiesinfo (passed by reference)
* @return updates &$allrelateddelegationpropertiesinfo with data for all delegation properties that are children of param $delegationfromclientcompanypropertyid
*/
function getallchildrendelegationpropertiesinfo($delegationfromclientcompanypropertyid, &$allrelateddelegationpropertiesinfo)
{
$delegationtoclientcompanypropertiesinfo = getdelegationchildren($delegationfromclientcompanypropertyid);
if($delegationtoclientcompanypropertiesinfo)
{
foreach($delegationtoclientcompanypropertiesinfo as $delegationtoclientcompanypropertyinfo)
{
array_push($allrelateddelegationpropertiesinfo, $delegationtoclientcompanypropertyinfo);
getallchildrendelegationpropertiesinfo($delegationtoclientcompanypropertyinfo[delegationtoclientcompanypropertyid], $allrelateddelegationpropertiesinfo);
}
}
}
/**
* @name getdelegationchildren
* @param integer $delegationfromclientcompanypropertyid
* @return all rows from tbldelegation for param $delegationfromclientcompanypropertyid
* @comment This function returns the clientcompanypropertyid's children.
*/
function getdelegationchildren($delegationfromclientcompanypropertyid)
{
$sql = "select delegationid, delegationfromclientcompanypropertyid, delegationtoclientcompanypropertyid, delegationsisterclientcompanyid from tbldelegation
where delegationfromclientcompanypropertyid = ".db_number($delegationfromclientcompanypropertyid)."
and delegationpulleddate is null";
$qry = db_query($sql);
return($qry);
}
/**
* @name getrootdelegationclientcompanypropertyid
* @param integer $clientcompanypropertyid
* @return root clientcompanypropertyid for delegation property param $clientcompanypropertyid
*/
function getrootdelegationclientcompanypropertyid($clientcompanypropertyid)
{
$retval = $clientcompanypropertyid;
do {
$delegationtorow = getdelegationparent($retval);
if($delegationtorow)
{
$retval = $delegationtorow[delegationfromclientcompanypropertyid];//Walk backwards from 'to' to 'from'.
}
} while ($delegationtorow);
return $retval;
}
/**
* @name getdelegationparent
* @param integer $clientcompanypropertyid
* @return unique row from tbldelegation for where delegationtoclientcompanypropertyid == $clientcompanypropertyid
* @comment This function returns the clientcompanypropertyid's parent.
*/
function getdelegationparent($clientcompanypropertyid)
{
$sql = "select * from tbldelegation
where delegationtoclientcompanypropertyid = ".db_number($clientcompanypropertyid)."
and delegationpulleddate is null";
$qry = db_first($sql);
return $qry;
}
/**
* @name isdelegationproperty
* @param integer $clientcompanypropertyid
* @return boolean based on if $clientcompanypropertyid is a delegation property
*/
function isdelegationproperty($clientcompanypropertyid)
{
$sql = "select 1 as isdelegationproperty from tbldelegation
where (delegationfromclientcompanypropertyid = ".db_number($clientcompanypropertyid)." or delegationtoclientcompanypropertyid = ".db_number($clientcompanypropertyid).")
and (delegationpulleddate is null and delegationcompleteddate is null)";
$qry = db_firstval($sql);
return $qry ? true : false;
}
/**
* @name removedelegation
* @param integer $clientcompanypropertyid
* @param integer $sisterclientcompanyid
* @return boolean based on success of query execution
* @comment This function removes delegation for the values passed in AND all the children thereof.
*/
function removedelegation($clientcompanypropertyid, $sisterclientcompanyid)
{
if(!db_number($clientcompanypropertyid) || !db_number($sisterclientcompanyid))
return FALSE;
if(!isdelegatedandisnotremoved($clientcompanypropertyid, $sisterclientcompanyid))
return FALSE;
$allchilddelegationpropertiesinfo = array();
$sql = "select delegationtoclientcompanypropertyid from tbldelegation where delegationfromclientcompanypropertyid = " . $clientcompanypropertyid . " and delegationsisterclientcompanyid = " . $sisterclientcompanyid;
$delegationtoclientcompanypropertyid = db_firstval($sql);
$originalclientcompanypropertyinfo = array('delegationfromclientcompanypropertyid'=>$clientcompanypropertyid, 'delegationtoclientcompanypropertyid'=>$delegationtoclientcompanypropertyid, 'delegationsisterclientcompanyid'=>$sisterclientcompanyid);
array_push($allchilddelegationpropertiesinfo, $originalclientcompanypropertyinfo);//Make an entry to add the original from property's info.
getallchildrendelegationpropertiesinfo($delegationtoclientcompanypropertyid, $allchilddelegationpropertiesinfo);
$tableswithclientcompanypropertyidandclientcompanyid = gettableswithclientcompanypropertyidandclientcompanyid();
foreach($allchilddelegationpropertiesinfo as $clientcompanypropertyinfo)
{
pulldelegationandtables($clientcompanypropertyinfo[delegationtoclientcompanypropertyid], $tableswithclientcompanypropertyidandclientcompanyid);
}
return true;
}
function pulldelegation($delegationtoclientcompanypropertyid, $tableswithclientcompanypropertyidandclientcompanyid = null)
{
pulldelegationandtables($delegationtoclientcompanypropertyid, $tableswithclientcompanypropertyidandclientcompanyid, false);
}
function pulldelegationandtables($delegationtoclientcompanypropertyid, $tableswithclientcompanypropertyidandclientcompanyid = null, $updatetableswithclientcompanypropertyidandclientcompanyid = true)
{
$sql = "
update tbldelegation
set delegationpulleddate=now(), delegationpulledby=".loginid()."
where delegationtoclientcompanypropertyid = ".db_number($delegationtoclientcompanypropertyid).";
";
if($updatetableswithclientcompanypropertyidandclientcompanyid)
{
if(!$tableswithclientcompanypropertyidandclientcompanyid)
{
$tableswithclientcompanypropertyidandclientcompanyid = gettableswithclientcompanypropertyidandclientcompanyid();
}
foreach($tableswithclientcompanypropertyidandclientcompanyid as $table)
{
$sql .= "
update " . $table . "
set clientcompanyid = (-clientcompanyid)
where clientcompanypropertyid = ".db_number($delegationtoclientcompanypropertyid).";
";
}
}
db_exec($sql,1);//doing each child's sql separately
}
function gettableswithclientcompanypropertyidandclientcompanyid()
{
//Get all tables that have clientcompanypropertyid field and clientcompanyid field, so we can negate the clientcompanyid field.
$clientcompanypropertyidtables = copy_gettablescontaining(clientcompanyproperty);
$clientcompanypropertyidtablesarray = array();
foreach($clientcompanypropertyidtables as $table)
array_push($clientcompanypropertyidtablesarray, $table[table_name]);
$clientcompanyidtables = copy_gettablescontaining(clientcompany);
$clientcompanyidtablesarray = array();
foreach($clientcompanyidtables as $table)
array_push($clientcompanyidtablesarray, $table[table_name]);
$tableswithclientcompanypropertyidandclientcompanyid = array(tblclientcompanyproperty);
foreach($clientcompanypropertyidtablesarray as $clientcompanypropertyidtable)
{
if(in_array($clientcompanypropertyidtable, $clientcompanyidtablesarray))
array_push($tableswithclientcompanypropertyidandclientcompanyid, $clientcompanypropertyidtable);
}
return $tableswithclientcompanypropertyidandclientcompanyid;
}
/**
* @name getclientcompanysisterscsv
* @return returns clientcompanysistercsv for clientcompanyid().
*/
function getclientcompanysisterscsv()
{
$clientcompanysisterscsv = '';
$sql = "select clientcompanysisters from tblclientcompany where clientcompanyid=".clientcompanyid();
$clientcompanysisterscsv = db_firstval($sql);
return $clientcompanysisterscsv;
}
/**
* @name hasdelegatedproperty
* @return return boolean based on if clientcompanyid() has delegated property.
*/
function hasdelegatedproperty()
{
$hasdelegatedproperty = '';
$clientcompanysisterscsv = getclientcompanysisterscsv();
if($clientcompanysisterscsv)
{
$sql = "select * from tbldelegation
join tblclientcompanyproperty on (tbldelegation.delegationfromclientcompanypropertyid = tblclientcompanyproperty.clientcompanypropertyid)
where tblclientcompanyproperty.clientcompanyid = " . clientcompanyid() . "
and delegationpulleddate is null
and delegationcompleteddate is null";
$hasdelegatedproperty = db_firstval($sql) ? true : false;
}
else
{
$hasdelegatedproperty = null;
}
return $hasdelegatedproperty;
}
function get_sisterssharingmyvendornetwork($clientcompanyid)
{
if(!$clientcompanyid || !($clientcompanyid>0))
return;
$sql="select clientcompanyid ,clientcompanysisters
from tblcustomizationentry
join tblcustomization using (customizationid)
join tblclientcompany using (clientcompanyid)
where 0=0
and customizationshortname='sharemyvendornetworkwithsister'
and customizationentryselected=1
";
$clientcompanysharingmynetworkwithsister=db_query($sql);
$sisterssharingmyvendornetwork="";
if($clientcompanysharingmynetworkwithsister)
{
foreach($clientcompanysharingmynetworkwithsister as $sharinginfo)
{
$clientcompanysisters=explode(',', $sharinginfo['clientcompanysisters']);
if(in_array(db_number($clientcompanyid),$clientcompanysisters))
$sisterssharingmyvendornetwork.=",".$sharinginfo['clientcompanyid'];
}
}
$sisterssharingmyvendornetwork=trim($sisterssharingmyvendornetwork, ',');
return $sisterssharingmyvendornetwork;
}
function get_amcvendorlist ($state="",$city="",$zipcode="",$clientcompanyid=0,$externalusertypeid=0,$accountid=0){
$vendors = array();
$city = db_col(trim($city));
$state = db_col(trim($state));
if (!$state)
unset ($state);
if (!$city)
unset ($city);
$clientcompanyid = db_number($clientcompanyid);
if (!$clientcompanyid)
$clientcompanyid = clientcompanyid();
$sql ="";
if ($state)
$sql ="CREATE TEMP TABLE tt_stcheck AS(SELECT REGEXP_SPLIT_TO_TABLE((CAST (stateid as CHAR(2)) || ',' || name||',' || code ), E',+')
FROM tblstate
WHERE code ILIKE '%" . db_col($state) . " %'
or name ILIKE '%" . db_col($state) . "%'
);";
$sql .= "\n SELECT *
, (SELECT loginid from tbllogin where tbllogin.accountstaffid=tblaccountstaff.accountstaffid) as loginid
, COALESCE (accountstaffnetworkstatus ,0) as accountstaffnetworkstatus
, COALESCE (tblaccountstaffnetwork.clientcompanyid, 0) as accountstaffnetwork_clientcompanyid
, (SELECT officelocationid as firstofficelocation from tblofficelocation where accountid=tblaccount.accountid order by officelocationid asc limit 1) as firstofficelocation
from tblaccount
join tblaccountpackage using(accountpackageid)
join tblaccountstaff using (accountid)
left join tblaccountstaffnetwork using (accountstaffid)
join tblofficelocation using(officelocationid)
where accountstaffid in (select distinct tblaccountstaff.accountstaffid
from tblaccount
join (select *, tblaccountstaff.accountid as aid from tblaccountstaff
join tblofficelocation using(officelocationid)
where 0=0
and accountstaffdisplayvendorprofile IN (-1, 1)
) as tblaccountstaff on (aid = tblaccount.accountid)
left join tblaccountstaffnetwork on (tblaccountstaffnetwork.accountstaffid = tblaccountstaff.accountstaffid and clientcompanyid = " . $clientcompanyid . ")
left join tblservicearea using (officelocationid)
where 0=0";
if ($city)
$sql .="\n and (city ilike '%" . $city . "%' or officelocationcity ilike '%" . $city . "%')";
if ($state)
$sql .="\n and (state = ANY(select * from tt_stcheck)
or officelocationstate = ANY (select * from tt_stcheck)
)";
$sql .="
and accountpaidthru >= current_date
and (accountstaffnetworkstatus is null or accountstaffnetworkstatus != -1)
)";
if (!is_developer())
$sql .= "\n and accountdeveloper =0";
if ($accountid)
$sql .= "\n and accountid in (" . db_number_list($accountid) . ")";
if ($externalusertypeid)
$sql .= "\n and externalusertypeid=" . db_number($externalusertypeid);
if ($zipcode)
$sql .= "\n and officelocationsvcziplist like '%".db_col($zipcode)."%'";
$vendors = db_query ($sql,0,1);
return $vendors;
}
function copy_property_form ($clientcompanypropertyid,$cprop=array())
{
$sql = "select propertyaddress from tblproperty where clientcompanypropertyid = ". db_number($clientcompanypropertyid) . " and companyid = " . companyid();
$cpropaddress = db_first($sql);
if (!$cpropaddress)
{
$cpropaddress = db_first("select clientcompanypropertyaddress || ', ' || clientcompanypropertystate as propertyaddress, clientcompanyid from tblclientcompanyproperty where clientcompanypropertyid = ".$clientcompanypropertyid);
//d($cpropaddress);
$clientcompanyid = $cpropaddress[clientcompanyid];
$clientqry = db_query("select clientcompanyname as value,clientid as id, clienttypename as label
from tblclient
join tblclienttype
using (clienttypeid)
where tblclient.companyid = '".db_number(companyid())."'
and clienttypeseller = 1
and clientinactive is null
and clienttypeid > 0
order by label,clientcompanyname");
if(!$clientqry||$_POST[create_clientid])
{ //if there are no clients, or they prompted to insert the client again, insert a new client and reselect
$clientcompany = get_clientcompany($clientcompanyid);
d($clientcompanyid,$clientcompany);
$sql="insert into tblclient
(
clientcompanyname
, clientaddress
, clientcity
, clientstate
, clientzip
, clientphone
, clientemail
, clientfax
, companyid
, clienttypeid
)
values
(
".db_tick($clientcompany[clientcompanyname])."
,".db_tick($clientcompany[clientcompanyaddress])."
,".db_tick($clientcompany[clientcompanycity])."
,".db_tick($clientcompany[clientcompanystate])."
,".db_tick($clientcompany[clientcompanyzip])."
,".db_tick($clientcompany[clientcompanyphone])."
,".db_tick($clientcompany[clientcompanyemail])."
,".db_tick($clientcompany[clientcompanyfax])."
, ".companyid()."
, 7 -- automatically insert the new client as an REO ClientType
)";
db_exec($sql,1);
$clientqry = db_query("select clientcompanyname as value,clientid as id, clienttypename as label
from tblclient
join tblclienttype
using (clienttypeid)
where tblclient.companyid = '".db_number(companyid())."'
and clienttypeseller = 1
and clientinactive is null
and clienttypeid > 0
order by label,clientcompanyname");
}
$tmp_sql=array();
foreach ($clientqry as $row)
{
$sql_label[$row[label]]=$row[label];
}
$i=0;
$f=0;
if($sql_label){
foreach ($sql_label as $lbl)
{
$tmp_sql[$i][id]="";
$tmp_sql[$i][value]=$lbl;
$tmp_sql[$i][label]=1;
$i++;
foreach ($clientqry as $row)
{
if ($clientqry[$f][label] == $lbl)
{
$tmp_sql[$i][id]=$clientqry[$f][id];
$tmp_sql[$i][value]=$clientqry[$f][value];
$i++;
$f++;
}
}
}
}
if(companyid())
{
$clientqry=$tmp_sql;
echo "