public function newMultiUnit($updatesA, $unit, $database) { $echo = "Entering newMultiUnit ........................................ "; File::append($this->logfilename, $echo . "\r\n"); //dd($updatesA, $unit); // check if multiple units // get sqM for specific unit $a = explode(" & ", $updatesA['strComplexNo']); $sqM = explode(" & ", $updatesA['strSqMeters']); $found = in_array($unit, $a); if ($found) { $place = array_search($unit, $a); $updatesA['strSqMeters'] = $sqM[$place]; } $now = \Carbon\Carbon::now('Africa/Johannesburg')->toDateTimeString(); $updatesA['id'] = null; $updatesA['strKey'] = $updatesA['strKey']; $updatesA['numStreetNo'] = $updatesA['strStreetNo']; $updatesA['numComplexNo'] = $updatesA['strComplexNo']; $updatesA['created_at'] = $now; // dd($updatesA, $a, $found, $place, $a[$place], $sqM[$place]); // delete older ones $delOld = Property::on($database)->where('strKey', $updatesA['strKey'])->where(DB::raw("STR_TO_DATE(dtmRegDate, '%Y-%m-%d')"), '<', Date($updatesA['dtmRegDate']))->delete(); $echo = "..DELETE OLD ............ " . $updatesA['strKey'] . " - " . $delOld; File::append($this->logfilename, $echo . "\r\n"); // insert new property record Property::on($database)->insert($updatesA); // add owner record if not existing // $hascontact = Owner::on($database)->select('id')->where('strIDNumber', '=', $updatesA['strIdentity']); if ($hascontact->count() == 0) { // update details from admin contacts $admin_contacts = "farmbook_admin"; $otf = new \App\Database\OTF(['database' => $admin_contacts]); $db = DB::connection($admin_contacts); $owner_details = Contact::on($admin_contacts)->select('*')->where('strIDNumber', $updatesA['strIdentity'])->first(); // contact details found in admin contacts if (sizeof($owner_details) == 1) { $uid = $owner_details->strIDNumber; // set database back $dbname = $database; $otf = new \App\Database\OTF(['database' => $dbname]); $db = DB::connection($dbname); // update contact details $owner = Owner::on($database)->insert(array('strIDNumber' => $owner_details->strIDNumber, 'NAME' => $owner_details->NAME, 'TITLE' => $owner_details->TITLE, 'INITIALS' => $owner_details->INITIALS, 'strSurname' => $owner_details->strSurname, 'strFirstName' => $owner_details->strFirstName, 'strHomePhoneNo' => $owner_details->strHomePhoneNo, 'strWorkPhoneNo' => $owner_details->strWorkPhoneNo, 'strCellPhoneNo' => $owner_details->strCellPhoneNo, 'EMAIL' => $owner_details->EMAIL, 'created_at' => $now, 'updated_at' => $now)); // update from prop rec } else { $dbname = $database; $otf = new \App\Database\OTF(['database' => $dbname]); $db = DB::connection($dbname); $owner = Owner::on($database)->insert(array('strIDNumber' => $updatesA['strIdentity'], 'NAME' => $updatesA['strOwners'], 'created_at' => $now)); } } else { // dont wipe old details } // add note // $hasnote = Note::on($database)->select('id')->where('strKey', '=', $updatesA['strKey'])->get(); if ($hasnote->count() == 0) { //$note = Note::on($database)->insert(array('strKey' => $updatesA[$x]['strKey'], 'numErf' => $updatesA[$x]['numErf'], 'memNotes' => "\n" . $now . ' ' . $updatesA[$x]['strOwners'] . ' - New Owner.', 'created_at' => $now)); $note = Note::on($database)->insert(array('strKey' => $updatesA['strKey'], 'numErf' => $updatesA['numErf'], 'created_at' => $now)); } else { $note = Note::on($database)->where('strKey', '=', $updatesA['strKey'])->update(array('memNotes' => DB::raw('concat(memNotes, " \\n' . $now . ' ' . $updatesA['strOwners'] . ' - New Owner.")'), 'updated_at' => $now)); } $echo = "Finishing newMultiUnit ........................................ "; File::append($this->logfilename, $echo . "\r\n"); }