$file = "d:\ط¨ط±ظ†ط§ظ…ط¬ ظ…ط®ط§ط²ظ†\app_final.js"
$content = Get-Content $file -Raw
$injection = @"

  // Gather Subcontract Abstracts
  if (typeof currentSubcontractAbstracts !== 'undefined' && typeof currentSubcontracts !== 'undefined') {
    currentSubcontractAbstracts.forEach(ab => {
      if (ab.status === 'posted') {
        const contract = currentSubcontracts.find(sc => sc.id == ab.contract_id);
        if (contract && contract.subcontractor_id == contactId) {
          const net = parseFloat(ab.net_amount) || 0;
          records.push({
            date: ab.date,
            type: 'ظ…ط³طھط®ظ„طµ ظ…ظ‚ط§ظˆظ„ ط¨ط§ط·ظ†',
            ref: 'sub-' + ab.id,
            notes: 'ظ…ط³طھط®ظ„طµ ط±ظ‚ظ… ' + (ab.abstract_number || ''),
            debit: 0,
            credit: net
          });
        }
      }
    });
  }

  // Gather Client Abstracts
  if (typeof currentClientAbstracts !== 'undefined') {
    currentClientAbstracts.forEach(ab => {
      if (ab.status === 'posted') {
        let cid = '';
        if (ab.client_name) {
          let c = currentContacts.find(x => x.name.trim() === ab.client_name.trim());
          if (c) cid = c.id;
        }
        if (cid == contactId) {
          const net = parseFloat(ab.net_amount) || 0;
          records.push({
            date: ab.date,
            type: 'ظ…ط³طھط®ظ„طµ ط¹ظ…ظٹظ„',
            ref: 'cab-' + ab.id,
            notes: 'ظ…ط³طھط®ظ„طµ ط¹ظ…ظٹظ„ ' + (ab.id || ''),
            debit: net,
            credit: 0
          });
        }
      }
    });
  }
"@

$search = @"
    });
  }

  // Filter by date
"@

if ($content.Contains($search)) {
    $content = $content.Replace($search, "    });`r`n  }$injection`r`n`r`n  // Filter by date")
    Set-Content $file -Value $content -Encoding UTF8
    Write-Host "Injection successful!"
} else {
    Write-Host "Search string not found!"
}
