function clientoffer_validate($clientnegotiationid,$_POST,$offer_map)
{
d("*** clientoffer_validate ***");
d(clientnegotiationid, $clientnegotiationid);
d(_post, $_POST);
d(offer_map, $offer_map);
$errorkeys=array();
// CHECK THAT ALL REQUIRED FIELDS ARE FILLED OUT
foreach($offer_map as $k=>$fld)
{
if($fld[deprecated])
{
d(unseting,$fld);
unset($offer_map[$k]);
}
}
foreach($offer_map as $key => $val)
{
if($val[deprecated])
continue;
if ($key == clientoffersellertitle || $key == clientofferbuyertitle || $key == clientofferfinancingtype || $key == clientoffertitlechoice || $key == clientoffertitle || $key == clientofferrushofferresponse)
if(strlen($_POST[$key]) >= 200)
$errorkeys[]=$key;
if($val['customfieldtypeid'] == 38)
{
$_POST[$key] = db_number($_POST[$key]);
if (!is_numeric($_POST[$key]) || $_POST[$key] < 0)
$errorkeys[]=$key;
}
if($val['netpurchase'] == 'addable' && (!is_numeric($_POST[$key] ) || $_POST[$key] <0))
{
$errorkeys[]=$key;
}
elseif($val['netpurchase'] == 'subtractable' && (!is_numeric($_POST[$key] ) || $_POST[$key] <0))
{
$errorkeys[]=$key;
}
elseif($val['required'] && isset($val['required_unless'])){
if(!$val['required_unless'] && $_POST[$key] === ''){
$errorkeys[]=$key;
}
}
else if($val['required'] && $val['required_for']){
if($val['required_for']() && $_POST[$key] === ''){
$errorkeys[]=$key;
}
}
else if($val['required']){
if($_POST[$key] === ''){
$errorkeys[]=$key;
}
}
}
$clientcompanyclientid = db_firstval("select clientcompanyclientid from tblclientcompanyproperty where clientcompanypropertyid = ".db_number($_POST[clientcompanypropertyid]));
// only check that the netpurchase requires escalation if clientcompany
if (clientcompanyid() && $clientcompanyclientid )
{
$level = clientoffer_getescalation($clientcompanyclientid, $_POST, $offer_map);
if($level)
{
$amt = clientoffer_calcsum('netpurchase', $_POST, $offer_map);
$errorkeys['netpurchase']['error_message'] = "Net Purchase Amount($amt) requires escalation level '$level' and is not allowed. Please adjust one or more of the values in your counter offer.";
d(errorkeys, $errorkeys);
}
}
return $errorkeys;
}
function clientoffer_insert($clientnegotiationid,$_POST,$offer_map,$skipvalidation=0)
{
// CHECK THAT ALL REQUIRED FIELDS ARE FILLED OUT
if(!$skipvalidation) //only do this validation if we have not skipped it.
foreach($offer_map as $key => $val){
if($val[deprecated])
continue;
if($val['required'] && isset($val['required_unless'])){
if(!$val['required_unless'] && $_POST[$key] === ''){
d($key, $val);
echo $key;
return false;
}
}
else if($val['required'] && $val['required_for']){
if($val['required_for']() && $_POST[$key] === ''){
d($key, $val);
echo $key;
return false;
}
}
else if($val['required']){
if($_POST[$key] === ''){
d($key, $val);
echo $key;
return false;
}
}
}
$db_clientnegotiationid = db_number($clientnegotiationid);
// UPDATE NEGOTIATION WITH OFFER EXPIRATION
$sql = "update tblclientnegotiation
set clientnegotiationexpirationdate = " . (clientcompanyid() ? db_datetime($_POST['clientofferexpirationdate']) : "null") . "
where clientnegotiationid = " . $db_clientnegotiationid;
db_exec($sql);
if(companyid())
$offer_map['clientofferexpirationdate'] = null; // ONLY THE CLIENT SIDE CAN MODIFY OFFER EXPIRATION
$clientofferid = db_first("select nextval('tblclientoffer_clientofferid_seq') as id");
$clientofferid = $clientofferid['id'];
$sql = "insert
into tblclientoffer
(clientofferid,
clientoffercreatedby,
clientnegotiationid,
clientoffernetpurchaseamount\n";
foreach($offer_map as $key => $field)
{
if ($field['calc_type'] != 'percent')
$sql .= "," . $key . "\n";
}
$sql .= ")";
$sql .= "
values (" . $clientofferid . ",
" . loginid() . ",
" . $db_clientnegotiationid . ",
" . clientoffer_calcsum('netpurchase', $_POST, $offer_map);
foreach($offer_map as $key => $field)
{
//loop through all of the documents uploaded and save them
//d($field);
if($field['type'] == 'documentupload' && $_FILES[$key])
{
$retval = process_customfield_documentupload_type($clientofferid, clientoffer, $key, 'clientoffer', 0);
if($retval) //only set this if needed
$_POST[$key]=$retval;
}
}
foreach($offer_map as $key => $field)
{
$_POST[$key]=trim($_POST[$key]);
if($key == 'clientofferexpirationdate')
$sql .= "," . ($_POST[$key] != '' ? db_datetime($_POST[$key]) : "null")." ";
elseif($key == 'clientofferinspectiontime' || $key == 'clientofferentrareferralpct' || $key == 'clientofferentrareferralamount')
$sql .= "," . (strlen($_POST[$key]) != '' ? db_number(floor($_POST[$key])) : "null")." ";
elseif($field['type'] == 'number' || $field['customfieldtypeid'] == 38)
{
if ($field['highestandbestreplacement'] && $field['highestandbestreplacement'] == $_POST[$key])
$sql .= ", '".$_POST[$key]."' ";
else
$sql .= "," . (strlen($_POST[$key]) != '' ? db_number($_POST[$key]) : "null")." ";
}
else if (strpos($key, 'date') !== false)
$sql .= ", " . db_date($_POST[$key]) . " "; // this will convert any input into date/time for date fields. VL MTX 19924
elseif(strlen($_POST[$key]))
$sql.= ", '".db_col($_POST[$key])."' ";
else
$sql.= ", ".db_tick($_POST[$key])." ";
$sql.=" -- $key\n";
// $sql .= "," . ($_POST[$key] != '' ? db_tick($_POST[$key]) : "null");
}
$sql .= ")";
//d("squeel","
$sql
");
$ret = db_exec($sql,1);
//if we have a valid update, update the negotiation updated
//date to the lastest created date in the offers section
if ($ret === true)
clientnegotiation_updateddate($db_clientnegotiationid);
if(companyid())
{
//if the current user is a companyid() that means they are
//and if the negotiations was NOT just entered, that means they are submitting a folloup offer /counter
global $clientnegotiation_inserted;
if(!$clientnegotiation_inserted)//if the negotiation was just entered, dont run this trigger, this is only for counters (which are offers AFTER the first offer)
{
$neg = db_first("select clientcompanypropertyassignmentid from tblclientnegotiation where clientnegotiationid = ".db_number($clientnegotiationid));
//trigger the events for offers submitted
$event = get_workflowevent("propertyoffercountered");
//Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM
workflowevent($event[workfloweventshortname],clientoffer,$clientofferid); //trigger the event
//workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$neg['clientcompanypropertyassignmentid']); //trigger the event
}
}
elseif(clientcompanyid())
{
//if the current user is a clientcompanyid() that means they are
//and if the negotiations was NOT just entered, that means thee asset ma nagment company submitting a folloup offer /counter
global $clientnegotiation_inserted;
if(!$clientnegotiation_inserted)//if the negotiation was just entered, dont run this trigger, this is only for counters (which are offers AFTER the first offer)
{
$neg = db_first("select clientcompanypropertyassignmentid from tblclientnegotiation where clientnegotiationid = ".db_number($clientnegotiationid));
//trigger the events for offers submitted
//d(_POST,$_POST);
if($_POST[highestandbest])
$event = get_workflowevent("propertyofferhighestcountermade");
else
$event = get_workflowevent("propertyoffercountermade");
//d(event,$event);
//ddie($event);
//workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$neg['clientcompanypropertyassignmentid']); //trigger the event
//Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM
workflowevent($event[workfloweventshortname],clientoffer,$clientofferid); //trigger the event
}
}
if($ret)
return $clientofferid;
else
return false;
}
function clientoffer_get($clientnegotiationid = '', $clientofferid = '')
{
//ddie();
$sql = "select tblclientoffer.*
, (select 1 from tbllogin where loginid = tblclientoffer.clientoffercreatedby and companyid = ".companyid()." and clientcompanyid = ".clientcompanyid().") as mycompanyoffer
from tblclientoffer
join tblclientnegotiation using(clientnegotiationid)
where 0 = 0";
if(clientcompanyid())
$sql .= "
and clientcompanyid = " . clientcompanyid();
if(companyid())
$sql .= "
and companyid = " . companyid();
if($clientnegotiationid)
$sql .= "
and clientnegotiationid = " . db_number($clientnegotiationid);
if($clientofferid)
$sql .= "
and clientofferid = " . db_number($clientofferid);
$sql .= "
order by clientofferid";
//d($sql);
if($clientofferid)
$offers = db_first($sql);
else
$offers = db_query($sql);
if($offers)
return $offers;
else
return false;
}
function clientoffer_reject($negotiationid,$offerid)
{
$continue = true;
if(!clientnegotiation_reject($negotiationid))
$continue = false;
if($continue){
$sql = "update tblclientoffer
set clientofferrejecteddate = now(),
clientofferrejectedby = " . loginid() . "
where clientofferid = " . db_number($offerid);
if(db_exec($sql))
{
clientnegotiation_updateddate($negotiationid);
return true;
}
else
return false;
}
else
return false;
}
function clientoffer_retract($offerid)
{
//echo "";
$offer = clientoffer_get('',$offerid);
if(!$offer[mycompanyoffer])
return false;
$neg = db_first("select * from tblclientnegotiation where clientnegotiationid = $offer[clientnegotiationid]");
$event = get_workflowevent("propertyofferretracted");
d(event, $event);
//workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$neg['clientcompanypropertyassignmentid']); //trigger the event
//Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM
workflowevent($event[workfloweventshortname],clientoffer,$offerid); //trigger the event
$sql = "update tblclientoffer
set clientofferretracteddate = now(),
clientofferretractedby = " . loginid() . "
where clientofferid = " . db_number($offerid);
if(db_exec($sql))
{
clientnegotiation_updateddate($offer[clientnegotiationid]);
return true;
}
else
return false;
}
function clientoffer_expire($offerid)
{
$offer = clientoffer_get('',$offerid);
if(!$offer[mycompanyoffer])
return false;
$sql = "update tblclientoffer
set clientofferexpireddate = now(),
clientofferexpiredby = " . loginid() . "
where clientofferid = " . db_number($offerid);
$neg = db_first("select * from tblclientnegotiation where clientnegotiationid = $offer[clientnegotiationid]");
$event = get_workflowevent("propertyofferretracted");
//workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$neg['clientcompanypropertyassignmentid']); //trigger the event
//Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM
workflowevent($event[workfloweventshortname],clientoffer,$offerid); //trigger the event
if(db_exec($sql))
{
//clientnegotiation_updateddate($negotiationid);
return true;
}
else
return false;
}
function clientoffer_calcpercent($shortname, $netpurchase, $clientcompanypropertyid)
{
$percent = false;
if ($netpurchase)
{
$value = clientoffer_calcfactor($clientcompanypropertyid, $shortname);
if ($value > 0)
$percent = round(($netpurchase/$value*100), 2, PHP_ROUND_HALF_UP);
}
return $percent;
}
function clientoffer_calcfactor($clientcompanypropertyid, $shortname)
{
if ($clientcompanypropertyid)
return db_number(get_clientpropertyextensionentry($clientcompanypropertyid,$shortname));
else
return false;
}
function clientoffer_calcsum($shortname, $offer, $offer_map = '')
{
$sum = 0;
if (is_array($offer_map))
{
foreach ($offer_map as $key => $field)
{
if ($field[$shortname] == 'addable' || $field[$shortname] == 'subtractable')
{
$val = $offer[$key];
if ($field[$shortname] == 'addable')
{
if (!is_numeric($val) || $val < 0)
return 0;
}
else
$val = (is_numeric($val))?-$val:0;
$sum += $val;
}
}
}
return $sum;
}
function clientoffer_process($offer_map, $offer = '', $clientcompanyid = 0, $prev = '', $includecounteroffer = 0, $columnname = "")
{
$is_portalview = ($_SERVER[SCRIPT_NAME] == '/clientproperty.php');
if($clientcompanyid && $offer)
{
foreach($offer as $k => $v){
if(!strstr($k, 'client')){
$offer['client'.$k] = $v;
}
}
}
global $offer_count;
global $preferred_lender;
// HARD CODED PREFERRED LENDERS added the code below because public offers didn't have a $_GET['view'] and might not have an offer yet and so the property wasn't getting pulled correctly.
if($clientcompanyid) {
if($_GET['makeoffer']) {
$sql = "select clientcompanypropertyid from tblproperty where propertyid=".db_number($_GET['makeoffer']);
$qry = db_query($sql);
//d(__LINE__,$sql);
if($qry && $qry[0][clientcompanypropertyid])
$clientcompanypropertyid = $qry[0][clientcompanypropertyid];
else
$clientcompanypropertyid = clientnegotiation_attr($offer['clientnegotiationid'],'clientcompanypropertyid');
} else
$clientcompanypropertyid = ($_GET['view'] ? $_GET['view'] : clientnegotiation_attr($offer['clientnegotiationid'],'clientcompanypropertyid'));
} else
$clientcompanypropertyid = ($_GET['view'] ? $_GET['view'] : clientnegotiation_attr($offer['clientnegotiationid'],'clientcompanypropertyid'));
//d("clientcompanypropertyid: ",$clientcompanypropertyid);
if($clientcompanyid == 5){
$prop = get_clientcompanyproperty($clientcompanypropertyid);
$shore_states = array('AL','CA','FL','MD','MI','NC','OR','PA');
//d("prop: ",$prop);
$propstate=$prop[clientcompanypropertystate];
if(strlen($propstate) >2) {
$sql = "select code from tblstate where name='".trim($propstate)."'";
$qrystate = db_query($sql);
if($qrystate && $qrystate[0][code])
$propstate=$qrystate[0][code];
}
if(in_array($propstate,$shore_states))
{
$preferred_lender['name'] = 'Shore Mortgage';
$preferred_lender['address'] = '770 S. Adams Rd.';
$preferred_lender['city'] = 'Birmingham';
$preferred_lender['state'] = 'MI';
$preferred_lender['zip'] = '48009';
$preferred_lender['url'] = 'www.shoremortgage.com/home123';
$preferred_lender['phone'] = '866-258-7764 /801-736-7736';
}
else
{
$preferred_lender['name'] = 'Primary Residence Mortgage, Inc.';
$preferred_lender['address'] = '4750 W. Wiley Post Way, Suite 200';
$preferred_lender['city'] = 'Salt Lake City';
$preferred_lender['state'] = 'Utah';
$preferred_lender['zip'] = '84116';
$preferred_lender['url'] = 'www.pmimortgage.com';
}
}
else if($clientcompanyid == 7){
$preferred_lender['name'] = 'Idaho Independent Bank';
$preferred_lender['address'] = '560 E State St.';
$preferred_lender['city'] = 'Eagle';
$preferred_lender['state'] = 'Idaho';
$preferred_lender['zip'] = '83616';
}
if($offer)
{
if ($columnname)
echo "
$columnname
";
foreach($offer_map as $key => $field)
{
//d(__line__.$key,$field);
if($offer[$key] && function_exists("customfield_display_$field[type]"))
{
$fn = "customfield_display_$field[type]";
$offer[$key] = $fn($offer[$key], array(pretty=>1));
}
elseif($field[display_format_function])
{
$fn = $field[display_format_function];
$offer[$key] = $fn($offer[$key], array(pretty=>1));
}
}
//loop through and if any of the fields are 'deprecated' and do not have a value, remove them from the offer_map, that is our way of deleting things, but showing historically saved information
if ((companyid() || clientcompanyid()) && $is_portalview)
{
if ($_POST['clientnegotiation_counter'])
{
$createdby = get_login_info(loginid());
$createddate = dttime_format(date("Y-m-d H:i:s"));
}
else
{
if ($offer[clientoffercreatedby])
$createdby = get_login_info($offer[clientoffercreatedby]);
if ($offer[clientoffercreateddate])
$createddate = dttime_format($offer[clientoffercreateddate]);
}
?>
>
Created By
echo $createdby ?>
>
Created Date
echo $createddate ?>
}
}
foreach($offer_map as $key => $field)
{
if($field[deprecated] && !$offer[$key] && !strlen($offer[$key]) && !$field['calc_type'])
{//remove only ones that do not have ANY entry in it and is not calculated, even if it is only a 0, for new offers, the field would always be removed
unset($offer_map[$key]);
}
}
foreach($offer_map as $key => $field)
{
//d($key, $field);
if($field['clientcompanyids'])
$tmp_clientids = explode(',',$field['clientcompanyids']);
if(!$tmp_clientids)
$tmp_clientids = array();
//$clientcompanypropertyid = ($_GET['view'] ? $_GET['view'] : clientnegotiation_attr($offer['clientnegotiationid'],'clientcompanypropertyid'));
$restricted = $field['input_visible_for'];
$restriction_met = ($field['input_visible_for'] ? $field['input_visible_for']() : '');
// IF WE HAVE A MAP KEY THAT IS INTENDED FOR THE PUBLIC AND WE'RE NOT LOGGED IN
if($field['input_visible_for'] == 'companyid' && $field['clientcompany-publicoffer'] && !companyid() && !clientcompanyid())
$restriction_met = true;
$clientcompany_specific = isset($field['clientcompanyids']);
$clientcompany_specific_met = in_array(clientproperty_attr($clientcompanypropertyid,'clientcompanyid'),$tmp_clientids) || in_array($clientcompanyid, $tmp_clientids);
$addclass = $field['addclass'];
//d(!$restricted || $restriction_met);
//d(!$clientcompany_specific || $clientcompany_specific_met);
//d($key);
if((!$restricted || $restriction_met) && (!$clientcompany_specific || $clientcompany_specific_met))
{
global $customfield_viewonly;
$additional_class="";
if((!dttime_format($offer[$key]) && $offer[$key] != $prev[$key]) || (dttime_format($offer[$key]) && dttime_format($offer[$key]) != dttime_format($prev[$key]))) //MB - I added dttime format checking so that if the dates are the same but just in different formats the highlighting does not show
{
if($offer_count > 1)
$additional_class='highlight';
}
if($field['type']==documentupload && !$offer[$key]) //clear out the highlight if documents are not uploaded the second time
$additional_class="";
if($addclass)
$additional_class.=$addclass;
if($field['required'] == addoffer && $offer_count >1)
$field['required'] = "";
/*
///MB - i commented this out, we WANT to require this information
if($field['required'] == addoffer && $_POST[clientnegotiation_counter])
$field['required'] = "";
*/
//d(__line__,$key);
$isPreferredLenderKey = ($key == 'clientofferpreferredlender' ? true : false);
/*
if (1 == 2 && $isPreferredLenderKey)
{
d(isPreferredLenderKey,$isPreferredLenderKey);
d(preferred_lender,$preferred_lender);
d(offer,$offer);
d(field_type,$field['type']);
}
*/
if($isPreferredLenderKey && $preferred_lender || !$isPreferredLenderKey && (!$offer || $field['type'] != 'documentupload'))
{
?>
class=' echo $additional_class; ?>' datatype=' echo $key ?>'>
// This has been added per client request described in MTX task: 17013
if (strtolower($offer[clientofferfinancingtype]) == 'cash' && $_POST[clientnegotiation_counter])
{
$cs_status = $customfield_viewonly;
if ($key == 'clientofferfinancingrate' || $key == 'clientofferbuyerlendercontactid'
|| $key =='clientofferfinancingprequal' || $key=='clientofferpreferredlender' || $key =='clientofferfinancingrate')
{
$customfield_viewonly =1;
$cs_set=1;
}
}
if($offer && !$_POST['clientnegotiation_counter'])
{
$offer[$key] = str_replace("\n"," ",$offer[$key]); //display this formatted so that \n have a so it dislays in a browser
if($field['display'] == 'contact')
{
$contact = contact_get($offer[$key]);
echo "
** Please input n/a in all not applicable fields **
* Required
echo $queueapproval_display; //show the queueapproval_display if it is available
//dont show the accept button
if (!clientoffer_ishighestandbest($offer))
{
if(!$hideacceptbutton) { ?>
} ?>
if(clientcompanyid()) { ?>
if($status != 'Held') { ?>
} else { ?>
} ?>
}
}
?>