|
|
@@ -1633,7 +1633,7 @@ private function upsertAccount(Import $import, array $mapped, $mappings, bool $h
|
|
|
|
|
|
|
|
|
|
|
|
$existing = Account::query()
|
|
|
|
$existing = Account::query()
|
|
|
|
->where('contract_id', $contractId)
|
|
|
|
->where('contract_id', $contractId)
|
|
|
|
->where('reference', $reference)
|
|
|
|
//->where('reference', $reference)
|
|
|
|
->where('active', 1)
|
|
|
|
->where('active', 1)
|
|
|
|
->first();
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
|
@@ -1656,6 +1656,10 @@ private function upsertAccount(Import $import, array $mapped, $mappings, bool $h
|
|
|
|
$value = $acc[$field] ?? null;
|
|
|
|
$value = $acc[$field] ?? null;
|
|
|
|
if (in_array($field, ['balance_amount', 'initial_amount'], true) && is_string($value)) {
|
|
|
|
if (in_array($field, ['balance_amount', 'initial_amount'], true) && is_string($value)) {
|
|
|
|
$value = $this->normalizeDecimal($value);
|
|
|
|
$value = $this->normalizeDecimal($value);
|
|
|
|
|
|
|
|
// Ensure the normalized value is numeric, otherwise default to 0
|
|
|
|
|
|
|
|
if ($value === '' || $value === '-' || ! is_numeric($value)) {
|
|
|
|
|
|
|
|
$value = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Convert empty string to 0 for amount fields
|
|
|
|
// Convert empty string to 0 for amount fields
|
|
|
|
if (in_array($field, ['balance_amount', 'initial_amount'], true) && ($value === '' || $value === null)) {
|
|
|
|
if (in_array($field, ['balance_amount', 'initial_amount'], true) && ($value === '' || $value === null)) {
|
|
|
@@ -1689,8 +1693,12 @@ private function upsertAccount(Import $import, array $mapped, $mappings, bool $h
|
|
|
|
if ($existing) {
|
|
|
|
if ($existing) {
|
|
|
|
// Build non-null changes for account fields
|
|
|
|
// Build non-null changes for account fields
|
|
|
|
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
|
|
|
|
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
|
|
|
|
// Track balance change
|
|
|
|
// Track balance change - normalize in case DB has malformed data
|
|
|
|
$oldBalance = (float) ($existing->balance_amount ?? 0);
|
|
|
|
$rawBalance = $existing->balance_amount ?? 0;
|
|
|
|
|
|
|
|
if (is_string($rawBalance) && $rawBalance !== '') {
|
|
|
|
|
|
|
|
$rawBalance = $this->normalizeDecimal($rawBalance);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$oldBalance = is_numeric($rawBalance) ? (float) $rawBalance : 0;
|
|
|
|
// Note: meta merging for contracts is handled in upsertContractChain, not here
|
|
|
|
// Note: meta merging for contracts is handled in upsertContractChain, not here
|
|
|
|
if (! empty($changes)) {
|
|
|
|
if (! empty($changes)) {
|
|
|
|
$existing->fill($changes);
|
|
|
|
$existing->fill($changes);
|
|
|
@@ -1699,7 +1707,11 @@ private function upsertAccount(Import $import, array $mapped, $mappings, bool $h
|
|
|
|
|
|
|
|
|
|
|
|
// If balance_amount changed and this wasn't caused by a payment (we are in account upsert), log an activity with before/after
|
|
|
|
// If balance_amount changed and this wasn't caused by a payment (we are in account upsert), log an activity with before/after
|
|
|
|
if (array_key_exists('balance_amount', $changes)) {
|
|
|
|
if (array_key_exists('balance_amount', $changes)) {
|
|
|
|
$newBalance = (float) ($existing->balance_amount ?? 0);
|
|
|
|
$rawNewBalance = $existing->balance_amount ?? 0;
|
|
|
|
|
|
|
|
if (is_string($rawNewBalance) && $rawNewBalance !== '') {
|
|
|
|
|
|
|
|
$rawNewBalance = $this->normalizeDecimal($rawNewBalance);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$newBalance = is_numeric($rawNewBalance) ? (float) $rawNewBalance : 0;
|
|
|
|
if ($newBalance !== $oldBalance) {
|
|
|
|
if ($newBalance !== $oldBalance) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
$contractId = $existing->contract_id;
|
|
|
|
$contractId = $existing->contract_id;
|
|
|
@@ -3194,7 +3206,7 @@ private function upsertAddress(int $personId, array $addrData, $mappings): array
|
|
|
|
->first();*/
|
|
|
|
->first();*/
|
|
|
|
|
|
|
|
|
|
|
|
// Build search query combining address, post_code and city
|
|
|
|
// Build search query combining address, post_code and city
|
|
|
|
$searchParts = [$addrData['post_code']];
|
|
|
|
$searchParts = [$addrData['address']];
|
|
|
|
if (!empty($addrData['post_code'])) {
|
|
|
|
if (!empty($addrData['post_code'])) {
|
|
|
|
$searchParts[] = $addrData['post_code'];
|
|
|
|
$searchParts[] = $addrData['post_code'];
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -3204,7 +3216,7 @@ private function upsertAddress(int $personId, array $addrData, $mappings): array
|
|
|
|
|
|
|
|
|
|
|
|
$searchQuery = implode(' ', $searchParts);
|
|
|
|
$searchQuery = implode(' ', $searchParts);
|
|
|
|
// Use fulltext search (GIN index optimized)
|
|
|
|
// Use fulltext search (GIN index optimized)
|
|
|
|
$existing = PersonAddress::where('person_id', $personId)
|
|
|
|
$existing = PersonAddress::query()->where('person_id', $personId)
|
|
|
|
->whereRaw("search_vector @@ plainto_tsquery('simple', ?)", [$searchQuery])
|
|
|
|
->whereRaw("search_vector @@ plainto_tsquery('simple', ?)", [$searchQuery])
|
|
|
|
->first();
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
|
|