$field){ $sql .= "," . $key . "\n"; } $sql .= ")"; $sql .= " values (" . $clientnegotiationid . ", " . loginid() . ", " . db_number($_POST['clientcompanyid']) . ", " . db_number($_POST['clientcompanypropertyid']) . ", " . companyid() . ", " . db_number($_POST['clientcompanypropertyassignmentid']) . ", " . db_number($_POST['staffid']) . "\n"; foreach($negotiation_map as $key => $field){ if($field['type'] == 'number') $sql .= "," . ($_POST[$key] ? db_number($_POST[$key]) . "\n" : "null\n"); else $sql .= "," . ($_POST[$key] ? "'" . db_col($_POST[$key]) . "'\n" : "null\n"); } $sql .= ")"; d(execsql,$sql); $event = get_workflowevent("propertyoffersubmitted"); workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$_POST['clientcompanypropertyassignmentid']); //trigger the event /* //Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM $sql="select clientofferid from tblclientoffer join tbllogin on (clientoffercreatedby=loginid) where clientnegotiationid=".db_number($clientnegotiationid)." and clientcompanyid=0 order by clientofferid desc limit 1"; $clientofferid=db_firstval($sql); workflowevent($event[workfloweventshortname],clientoffer,$clientofferid); //trigger the event */ if(db_exec($sql,1)) { clientnegotiation_updateddate($clientnegotiationid); return $clientnegotiationid; } else return false; } function clientnegotiation_status($negotiation){ if(!is_array($negotiation)) { //d(___ccc____, $negotiation); $negotiation = clientnegotiation_get('', $negotiation); $negotiation = $negotiation[0]; } if($negotiation['status'] || strlen($negotiation['status']) > 0) return $negotiation['status']; if($negotiation['clientnegotiationrejecteddate']){ return 'Rejected'; }else if($negotiation['clientnegotiationholddate']){ return 'Held'; }else if($negotiation['clientnegotiationaccepteddate']){ return 'Accepted'; }else if(isset($negotiation['numberofcounters'] ) && !$negotiation['numberofcounters']){ return 'New'; }else if(isset($negotiation['numberofcounters'] ) && $negotiation['numberofcounters'] > 0){ return 'Negotiating'; }else if(is_array($negotiation['offers']) && count($negotiation['offers']) == 1){ return 'New'; }else if(is_array($negotiation['offers']) && count($negotiation['offers']) > 1){ return 'Negotiating'; }else if($negotiation['offers'] == 1){ return 'New'; }else if($negotiation['offers'] > 1){ return 'Negotiating'; }else{ return 'Negotiation w/ no offers'; } } function clientnegotiation_get($clientcompanypropertyid = '', $clientnegotiationid = '', $include_status = 0, $status = ''){ if($status) $include_status = 1; //if($_GET['s']) //d($_GET); $sql = "select tblclientnegotiation.*, (case when clientnegotiationaccepteddate is not null then 'Accepted' when clientnegotiationexpirationdate < now() then 'Expired' else null end) as status, clientnegotiationbuyeraddress,clientnegotiationfinancingtype from tblclientnegotiation join tblclientcompanyproperty using(clientcompanypropertyid) where 0=0 "; if(clientcompanyid()) { $sql.=" and tblclientcompanyproperty.clientcompanyid = " . clientcompanyid() ; //this restriction makes it so that users can only see their 'subs' negotiations. $tmpclientstaffid2 = get_rolepropertyrestriction_stafflist(); if($tmpclientstaffid2) { $sql.=" and exists (select 1 from tblclientcompanypropertyrole where clientcompanypropertyid = tblclientcompanyproperty.clientcompanypropertyid and clientstaffid in($tmpclientstaffid2)) "; } } if(companyid()) { $sql.=" and clientcompanypropertyhide is null and exists ( select 1 from tblclientcompanypropertyassignment ccpa where 0=0 and ccpa.clientcompanypropertyid=tblclientcompanyproperty.clientcompanypropertyid and staffid in ( select staffid from tbllogin where companyid = ".companyid()." and staffid > 0 ) and ( clientcompanypropertyassignmentrevokeddate is null or ( clientcompanypropertyassignmentrevokeddate is not null and (clientcompanypropertyassignmentrevokeddate + interval '30 days') > now() ) ) limit 1 ) and companyid = " . companyid(); } // we do not have any rules that define whether the accountstaff get to see any offers //when we do it will probably be related to assignments if(!$clientnegotiationid && !$clientcompanypropertyid) if(accountstaffid() || externaluserid()) "";//$sql.= ' and break sql so that I can see that I should not be here'; if($clientcompanypropertyid) $sql .= " and clientcompanypropertyid = " . db_number($clientcompanypropertyid); if($clientnegotiationid) $sql .= " and clientnegotiationid = " . db_number($clientnegotiationid); /* if($_GET['s']) { $sql.="and ( clientnegotiationagency ilike '%".$_GET['s']."%' or clientnegotiationoffertype ilike '%".$_GET['s']."%' or clientnegotiationbuyerfname ilike '%".$_GET['s']."%' or clientnegotiationbuyerlname ilike '%".$_GET['s']."%' or clientnegotiationbuyeraddress ilike '%".$_GET['s']."%' or clientnegotiationfinancingtype ilike '%".$_GET['s']."%' or clientnegotiationbuyerfullname ilike '%".$_GET['s']."%' ) "; } */ //d(NEGOTIATIONS,$sql); $negotiations = db_query($sql); //d(htmlpre($sql),$negotiations); if($negotiations) { foreach($negotiations as $negotiation_key => $negotiation){ $negotiations[$negotiation_key][countercount]=0; // GET ALL OFFERS FOR THIS NEGOTIATION $offers = clientoffer_get($negotiation['clientnegotiationid']); if($offers){ foreach($offers as $offer_key => $offer){ // GET ALL EXTENSIONS FOR THIS OFFER $offers[$offer_key]['extensions'] = clientofferextensionentry_get($offer['clientofferid']); if(!$offer[clientofferretracteddate]) $negotiations[$negotiation_key][countercount]++; //increment the offercount, only if the offer has not been retracted } if($negotiations[$negotiation_key][countercount]) $negotiations[$negotiation_key][countercount] --; } // ADD OFFERS TO NEGOTIATION $negotiations[$negotiation_key]['offers'] = $offers; //$negotiations[$negotiation_key]['clientnegotiationfinancingtype'] = $offers[$negotiation_key]['clientofferfinancingtype']; //$negotiations[$negotiation_key]['clientnegotiationbuyeraddress'] = $offers[$negotiation_key]['offeragentcompanyaddress']; // ADD STATUS TO NEGOTIATION if($include_status) $negotiations[$negotiation_key]['status'] = clientnegotiation_status($negotiations[$negotiation_key]); //d($negotiations[$negotiation_key]['status']); } return $negotiations; } else return array(); } function clientnegotiation_accept($negotiationid){ $db_clientnegotiationid = db_number($negotiationid); $sql = "update tblclientnegotiation set clientnegotiationaccepteddate = now(), clientnegotiationacceptedby = " . loginid() . ", clientnegotiationholddate = null, clientnegotiationholdby = null, clientnegotiationholdreason = null where clientnegotiationid = " . $db_clientnegotiationid; if(db_exec($sql)) { $sql = "select clientcompanypropertyassignmentid from tblclientnegotiation where clientnegotiationid = " . $db_clientnegotiationid; $assignmentid = db_first($sql); $event = get_workflowevent("propertyofferaccepted"); //workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$assignmentid[clientcompanypropertyassignmentid]); //trigger the event //Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM $sql="select clientofferid from tblclientoffer join tbllogin on (clientoffercreatedby=loginid) where clientnegotiationid=".$db_clientnegotiationid." and clientcompanyid=0 order by clientofferid desc limit 1"; $clientofferid=db_firstval($sql); workflowevent($event[workfloweventshortname],clientoffer,$clientofferid); //trigger the event clientnegotiation_updateddate($db_clientnegotiationid); return true; } else { return false; } } function clientnegotiation_unaccept($negotiationid, $clientcompanypropertyid, $clientnegotiationbuyername) { $db_clientnegotiationid = db_number($negotiationid); $sql = "select clientnegotiationaccepteddate, clientnegotiationacceptedby from tblclientnegotiation where clientnegotiationid = ".$db_clientnegotiationid; $result = db_first($sql); if ($result) { $sql = "update tblclientnegotiation set clientnegotiationaccepteddate = null, clientnegotiationacceptedby = 0, clientnegotiationholddate = null, clientnegotiationholdby = null, clientnegotiationholdreason = null where clientnegotiationid = ".$db_clientnegotiationid; if(db_exec($sql)) { create_note($clientcompanypropertyid, 'clientcompanyproperty', "The offer that was accepted by ".get_login_info($result['clientnegotiationacceptedby'])." on ".dttime_format($result['clientnegotiationaccepteddate'])." for $clientnegotiationbuyername was canceled.", '', '', 0, 'offers'); clientnegotiation_updateddate($db_clientnegotiationid); return true; } else { return false; } } } function clientnegotiation_hold($negotiationid,$holdreason){ $db_clientnegotiationid = db_number($negotiationid); $sql = "update tblclientnegotiation set clientnegotiationholddate = now(), clientnegotiationholdby = " . loginid() . ", clientnegotiationholdreason = " . db_tick($holdreason) . " where clientnegotiationid = " . $db_clientnegotiationid; //ddie($sql); if(db_exec($sql)) { $sql = "select clientcompanypropertyassignmentid from tblclientnegotiation where clientnegotiationid = " . $db_clientnegotiationid; $assignmentid = db_first($sql); $event = get_workflowevent("propertyofferhold"); //workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$assignmentid[clientcompanypropertyassignmentid]); //trigger the event //Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM $sql="select clientofferid from tblclientoffer join tbllogin on (clientoffercreatedby=loginid) where clientnegotiationid=".$db_clientnegotiationid." and clientcompanyid=0 order by clientofferid desc limit 1"; $clientofferid=db_firstval($sql); workflowevent($event[workfloweventshortname],clientoffer,$clientofferid); //trigger the event clientnegotiation_updateddate($db_clientnegotiationid); return true; } else return false; } function clientnegotiation_unhold($negotiationid){ $sql = "update tblclientnegotiation set clientnegotiationholddate = null, clientnegotiationholdby = null, clientnegotiationupdateddate = now(), clientnegotiationupdatedby = " . loginid() . ", clientnegotiationholdreason = null where clientnegotiationid = " . db_number($negotiationid); if(db_exec($sql)) { $sql = "select clientcompanypropertyassignmentid from tblclientnegotiation where clientnegotiationid = " . db_number($negotiationid); $assignmentid = db_first($sql); $event = get_workflowevent("propertyofferunhold"); //workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$assignmentid[clientcompanypropertyassignmentid]); //trigger the event //Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM $sql="select clientofferid from tblclientoffer join tbllogin on (clientoffercreatedby=loginid) where clientnegotiationid=".db_number($negotiationid)." and clientcompanyid=0 order by clientofferid desc limit 1"; $clientofferid=db_firstval($sql); workflowevent($event[workfloweventshortname],clientoffer,$clientofferid); //trigger the event clientnegotiation_updateddate($negotiationid); return true; } else return false; } function clientnegotiation_reject($negotiationid){ $sql = "update tblclientnegotiation set clientnegotiationrejecteddate = now(), clientnegotiationrejectedby = " . loginid() . ", clientnegotiationholddate = null, clientnegotiationholdby = null, clientnegotiationholdreason = null where clientnegotiationid = " . db_number($negotiationid); //ddie($sql); if(db_exec($sql)) { $sql = "select clientcompanypropertyassignmentid from tblclientnegotiation where clientnegotiationid = " . db_number($negotiationid); //d($sql); $assignmentid = db_first($sql); $event = get_workflowevent("propertyofferdeclined"); //workflowevent($event[workfloweventshortname],clientcompanypropertyassignment,$assignmentid[clientcompanypropertyassignmentid]); //trigger the event //Modified offer workflowevents so that they can pull offer info via clientofferid. 2-2-2013 MDM $sql="select clientofferid from tblclientoffer join tbllogin on (clientoffercreatedby=loginid) where clientnegotiationid=".db_number($negotiationid)." and clientcompanyid=0 order by clientofferid desc limit 1"; $clientofferid=db_firstval($sql); workflowevent($event[workfloweventshortname],clientoffer,$clientofferid); //trigger the event clientnegotiation_updateddate($negotiationid); return true; } else { return false; } } function clientnegotiation_needsresponse($negotiation){ // psuedo code for ::: count($negotiation['offers']) % 2 == ((companyid() ? 0 : 1)) //if client company and offer count is odd // then this might be true //elseif company and offer count is even // then this might be true //else // false //d(clientnegotiation_needsresponse, $negotiation); if(is_array($negotiation[offers])) { foreach($negotiation[offers] as $offer) if(!$offer[clientofferretracteddate]) { $offercount++; $lastoffer = $offer; } $myoffer = $lastoffer[mycompanyoffer]; } else { $offercount = $negotiation[offers]; $myoffer = ($negotiation[offers] % 2 == (companyid() ? 1 : 0)); //calculate whether the number of offers means that the offer the client side or companyside } if($negotiation['status'] == 'New' && clientcompanyid() && $offercount) { //d("new offer"); return true; }//get the last offer so we can see whose 'turn' it is if($negotiation['status'] == 'Negotiating' && !$myoffer && $offercount) { //d("negotiating offer"); return true; } //d(notneedresponse); return false; } function clientnegotiation_validate($clientnegotiationid,$arr,$negotiation_map) { $errorkeys=array(); // CHECK THAT ALL REQUIRED FIELDS ARE FILLED OUT foreach($negotiation_map as $key => $val){ if ($key == clientoffersellertitle || $key == clientofferbuyertitle || $key == clientofferfinancingtype || $key == clientoffertitlechoice || $key == clientoffertitle || $key == clientofferrushofferresponse) if(strlen($arr[$key]) >= 200) $errorkeys[]=$key; if($val['required'] && isset($val['required_unless'])) { //d(" if($val[required_unless]) \$evald=true; else \$evald=false;",$val['required_unless']); eval(" if($val[required_unless]) \$evald=true; else \$evald=false;"); if(!$evald && !$arr[$key]) { $errorkeys[]=$key; } } else if($val['required'] && $val['required_for']){ if($val['required_for']() && $arr[$key] === ''){ $errorkeys[]=$key; } } else if($val['required']){ if($arr[$key] === ''){ $errorkeys[]=$key; } } } return $errorkeys; } function clientnegotiation_updateddate($clientnegotiationid = 0, $clientofferid = 0) { $ret = false; $db_clientofferid = db_number($clientofferid); $db_clientnegotiationid = db_number($clientnegotiationid); //if we don't have a negotiation id, but do have a client offer id if (!$db_clientnegotiationid && $db_clientofferid) { //try and get our negotiation id $sql = " select clientnegotiationid from tblclientoffer where clientofferid = ".$db_clientofferid; $db_clientnegotiationid = $db_firstval($sql); } //if we have a negotiation id if ($db_clientnegotiationid) { //update our updated by date $upd_sql = " update tblclientnegotiation set clientnegotiationupdateddate = NOW() , clientnegotiationupdatedby = ".loginid()." where clientnegotiationid = $db_clientnegotiationid"; $ret = db_exec($upd_sql,1); } return $ret; } function clientnegotiation_getmap() { $negotiation_map['clientnegotiationagency']['label'] = "Agency Confirmation"; $negotiation_map['clientnegotiationagency']['customfieldtypeid'] = 73; $negotiation_map['clientnegotiationagency']['required'] = true; $negotiation_map['clientnegotiationagency']['default'] = "Listing & Selling Agent Different"; $negotiation_map['clientnegotiationsellingagentcontactid']['label'] = "Selling Agent"; $negotiation_map['clientnegotiationsellingagentcontactid']['customfieldtypeid'] = 58; $negotiation_map['clientnegotiationsellingagentcontactid']['extra1']['contacttypeid'] = '45,47,57'; $negotiation_map['clientnegotiationsellingagentcontactid']['type'] = "number"; $negotiation_map['clientnegotiationsellingagentcontactid']['display'] = "contact"; $negotiation_map['clientnegotiationsellingagentcontactid']['required'] = true; $negotiation_map['clientnegotiationsellingagentcontactid']['required_unless'] = '$arr[clientnegotiationagency] != "Listing & Selling Agent Different"'; $negotiation_map['clientnegotiationoffertype']['label'] = "Offer Type"; $negotiation_map['clientnegotiationoffertype']['customfieldtypeid'] = 74; $negotiation_map['clientnegotiationoffertype']['required'] = true; $negotiation_map['clientnegotiationbuyerfullname']['label'] = "Buyer(s) Full Name"; $negotiation_map['clientnegotiationbuyerfullname']['required'] = true; //$negotiation_map['clientnegotiationbuyerfname']['label'] = "Buyer First Name"; //$negotiation_map['clientnegotiationbuyerfname']['required'] = true; //$negotiation_map['clientnegotiationbuyerlname']['label'] = "Buyer Last Name"; //$negotiation_map['clientnegotiationbuyerlname']['required'] = true; $negotiation_map['clientnegotiationbuyerphone']['label'] = "Buyer Phone"; $negotiation_map['clientnegotiationbuyerphone']['required'] = true; $negotiation_map['clientnegotiationbuyeraddress']['label'] = "Buyer Address"; $negotiation_map['clientnegotiationbuyerattorneycontactid']['label'] = "Buyer Attorney"; $negotiation_map['clientnegotiationbuyerattorneycontactid']['customfieldtypeid'] = 58; $negotiation_map['clientnegotiationbuyerattorneycontactid']['extra1']['contacttypeid'] = '23'; $negotiation_map['clientnegotiationbuyerattorneycontactid']['type'] = "number"; $negotiation_map['clientnegotiationbuyerattorneycontactid']['display'] = "contact"; $negotiation_map['clientnegotiationbuyerlendercontactid']['label'] = "Buyer Lender"; $negotiation_map['clientnegotiationbuyerlendercontactid']['customfieldtypeid'] = 8; $negotiation_map['clientnegotiationbuyerlendercontactid']['extra1']['contacttypeid'] = '6'; $negotiation_map['clientnegotiationbuyerlendercontactid']['type'] = "number"; $negotiation_map['clientnegotiationbuyerlendercontactid']['display'] = "contact"; $negotiation_map['clientnegotiationbuyerlendercontactid']['legacy'] = true; $negotiation_map['clientnegotiationfinancingprequal']['label'] = "Financing Prequal"; $negotiation_map['clientnegotiationfinancingprequal']['customfieldtypeid'] = 47; $negotiation_map['clientnegotiationfinancingprequal']['legacy'] = true; $negotiation_map['clientnegotiationfinancingprequalnumber']['label'] = "Financing Prequal Number"; $negotiation_map['clientnegotiationfinancingprequalnumber']['legacy'] = true; $negotiation_map['clientnegotiationfinancingtype']['label'] = "Financing Type"; $negotiation_map['clientnegotiationfinancingtype']['customfieldtypeid'] = 59; $negotiation_map['clientnegotiationfinancingtype']['legacy'] = true; $negotiation_map['clientnegotiationfinancingrate']['label'] = "Financing Rate"; $negotiation_map['clientnegotiationfinancingrate']['type'] = "number"; $negotiation_map['clientnegotiationfinancingrate']['legacy'] = true; return $negotiation_map; } ?>