summaryrefslogtreecommitdiff
path: root/LedgerSMB/RESTXML/Document/Customer_Search.pm
blob: 9f2fb3d6e0bf5fc5090a72a133f908d940b784d4 (plain)
  1. package LedgerSMB::RESTXML::Document::Customer_Search;
  2. use strict;
  3. use warnings;
  4. use base qw(LedgerSMB::RESTXML::Document::Base);
  5. use LedgerSMB::Log;
  6. sub handle_get {
  7. my ( $self, $args ) = @_;
  8. my $user = $args->{user};
  9. my $dbh = $args->{dbh};
  10. my $handler = $args->{handler};
  11. my $query = $handler->read_query();
  12. my %terms;
  13. for my $field ( $query->param() ) {
  14. # TODO: BIG GAPING HOLE HERE.
  15. $terms{$field} = $query->param($field);
  16. }
  17. if ( $terms{_keyword} ) {
  18. %terms = (
  19. name => $terms{_keyword},
  20. customernumber => $terms{_keyword},
  21. contact => $terms{_keyword}
  22. );
  23. }
  24. my $sql =
  25. 'SELECT id,name,phone,customernumber FROM customer WHERE '
  26. . join( ' OR ', map { "$_ like ?" } sort keys %terms );
  27. my $res = $dbh->prepare($sql);
  28. $res->execute( map { "$terms{$_}\%" } sort keys %terms )
  29. or return $handler->error( $dbh->errstr );
  30. my @rows;
  31. my $row;
  32. push @rows, $row while $row = $res->fetchrow_hashref();
  33. $res->finish();
  34. $handler->respond(
  35. XML::Twig::Elt->new(
  36. 'Customer_Search_Response',
  37. { 'xmlns:xlink' => "http://www.w3.org/1999/xlink" },
  38. map {
  39. $self->hash_to_twig(
  40. {
  41. name => 'Customer',
  42. root_attr => { 'xlink:href' => "Customer/$_->{id}" },
  43. hash => $_
  44. }
  45. );
  46. } @rows
  47. )
  48. );
  49. }
  50. 1;