$from) { if (trim($offerdata[$from]) != '') { $offerdata[$to] = $offerdata[$from]; $hasremap = true; } } //don't want to modify the map if we don't have any fields remapped already if ($hasremap) { $offerdata[contacttype] = 105; $offerdata[contactcreated] = "now()"; $offerdata[contactcreatedip] = $_SERVER['REMOTE_ADDR']; } return $offerdata; } function offer_format(&$offer) { if ($offer) { $fname = $offer['offeragentfname']; $lname = $offer['offeragentlname']; $name = $offer['offeragent']; if (trim($fname) != '' || trim($lname) != '') $name = trim("$fname $lname"); else if (trim($name) != '') { $lname = trim(list_last($name, " ")); $fname = trim(list_allbutlast($name, " ")); if ($fname == '') { $fname = $lname; $lname = ''; } } $offer['offeragent'] = $name; $offer['offeragentfname'] = $fname; $offer['offeragentlname'] = $lname; if ($offer['offeramount']) $offer['offeramount'] = db_number(preg_replace('/[^0-9\.]/', '', $offer['offeramount'])); if ($offer['offerearnestamount']) $offer['offerearnestamount'] = db_number(preg_replace('/[^0-9\.]/', '', $offer['offerearnestamount'])); } return $offer; } function offer_getcolumns() { global $offer_columns; if (!$offer_columns) { $sql = " select column_name, data_type from information_schema.columns where table_name='tbloffer' and column_name not in ( 'offerid', 'offerlastmodified', 'offerentered', 'offerreaddate', 'offerreadby' )"; $columns_qry = db_query($sql); $contact_columns = array(); foreach ($columns_qry as $data) { switch ($data[data_type]) { case 'timestamp without time zone': $contact_columns[$data[column_name]] = 'db_datetimenoadjust'; break; case 'date': $contact_columns[$data[column_name]] = 'db_date'; break; case 'integer': case 'numeric': case 'smallint': $contact_columns[$data[column_name]] = 'db_number'; break; case 'character varying': case 'text': default: $contact_columns[$data[column_name]] = 'db_tick'; } } } return $contact_columns; } function offer_set($offerdata, $offerid=0, $offerread=0, $ispublic=1) { $offerdata = offer_format($offerdata); //if we have been provided an offer id //then we have an update $has_data = false; if ($offerid > 0) { //validate the offer id $validate_sql = " select offerid from tbloffer where offerid = ".db_number($offerid) ; $offerid = db_firstval($validate_sql); //trying to update to an id that doesn't exist in the system if (!$offerid) return false; //make sure we are processing all valid columns foreach (offer_getcolumns() as $key => $db_func) { if (isset($offerdata[$key])) { $addondat .= " , {$key} = ".$db_func($offerdata[$key]); $has_data = true; unset($offerdata[$key]); } } if ($offerread) { $addondat .= " , offerreaddate = coalesce(offerreaddate,now()) , offerreadby = case when coalesce(offerreadby,0) = 0 then ".loginid()." else offerreadby end "; $has_data = true; } $sql = " update tbloffer set offerlastmodified = now() {$addondat} where offerid = {$offerid}"; if (!$ispublic) $sql .= " and propertyid in ( select propertyid from tblproperty where companyid = ".companyid()." )"; } else { $offerid = db_nextid('offer'); //make sure we are processing all valid columns foreach (offer_getcolumns() as $key => $db_func) { if (isset($offerdata[$key])) { $addonsql .= " , {$key} "; $addondat .= " , ".$db_func($offerdata[$key]); $has_data = true; unset($offerdata[$key]); } } if ($offerread) { $addonsql .= " , offerreaddate , offerreadby"; $addondat .= " , now() , ".loginid(); $has_data = true; } $sql = " insert into tbloffer ( offerid , offerentered , offerlastmodified {$addonsql} ) values ( {$offerid} , now() , now() {$addondat} )"; } //if we have something to process, process it d('offer_set sql', $sql); if ($has_data && $sql && db_exec($sql, 1)) return $offerid; return false; } function has_availablepropertyofferid($propertyid) { //no propertyid do nothing if (!$propertyid) return false; $sql = " select count(1) from tbloffer join tblavailablepropertyentry using (offerid, propertyid) where propertyid = ".db_number($propertyid)." and clientcompanyid = ".db_number(clientcompanyid())." and ( offerstatus ilike '%accepted%' or offerstatus ilike '%new%' or offerstatus ilike '%submitted%' or coalesce(offerstatus, '') = '' )"; //if we have an active offer on the property do nothing if (db_firstval($sql) > 0) return false; //otherwise return the next available offer id return true; } function update_extensionstooffervalue($propertyid, $newstatus, $checkstatus='under contract') { $propertyid=db_number($propertyid); if(!$propertyid || !$checkstatus) { developer_error('Update Extensions to Offer Value Error',"Missing propertyid:".$propertyid." or checkstatus:".$checkstatus); return; } if($checkstatus && strstr(strtolower($newstatus),$checkstatus)) { //d("status: ".$checkstatus); $sql="select * from tbloffer where propertyid=".$propertyid." and ( offerstatus is null or lower(offerstatus) not ilike '%rejected%' ) order by offerid "; //d('propertyoffersql',$sql); $propertyofferqry=db_query($sql); if(count($propertyofferqry)==1)//If there is only one offer not rejected then use that information to set some extensions. { $propertyoffer=$propertyofferqry[0]; $updatepropertyextensions_offerundercontract['propertyextensionofferacceptedamount']=$propertyoffer['offeracceptedamount']; if(!$propertyoffer['offeracceptedamount'] || ($propertyoffer['offeracceptedamount']=='0.00') || ($propertyoffer['offeracceptedamount']=='0.0') && $propertyoffer['offeramount']) $updatepropertyextensions_offerundercontract['propertyextensionofferacceptedamount']=$propertyoffer['offeramount']; $updatepropertyextensions_offerundercontract['propertyextensionbuyer1name']=$propertyoffer['offercustomername']; $updatepropertyextensions_offerundercontract['propertyextensionofferaccepteddate']=db_timestamp(time()); $updatepropertyextensions_offerundercontract['propertyextensionaccepteddate']=db_timestamp(time()); foreach($updatepropertyextensions_offerundercontract as $dbkey=>$dbval) { $entry=get_propertyextensionentry($propertyid,$dbkey); //d('$entry',$entry); if(!$entry || $entry=='0.00' || $entry=='0.0') property_ext_save($propertyid,$dbkey,$dbval); } } } //d('$updatepropertyextensions_offerundercontract',$updatepropertyextensions_offerundercontract); //d('$propertyofferqry',$propertyofferqry); }