summaryrefslogtreecommitdiff
path: root/scripts/customer.pl
blob: bef642ec0b550e7902bf81f7da658320a24c31ad (plain)
  1. #!/usr/bin/perl
  2. =pod
  3. =head1 NAME
  4. LedgerSMB::Scripts::customer - LedgerSMB class defining the Controller
  5. functions, template instantiation and rendering for customer editing and display.
  6. =head1 SYOPSIS
  7. This module is the UI controller for the customer DB access; it provides the
  8. View interface, as well as defines the Save customer.
  9. Save customer will update or create as needed.
  10. =head1 METHODS
  11. =cut
  12. package LedgerSMB::Scripts::customer;
  13. use LedgerSMB::Template;
  14. use LedgerSMB::DBObject::Customer;
  15. require 'lsmb-request.pl';
  16. =pod
  17. =over
  18. =item get($self, $request, $user)
  19. Requires form var: id
  20. Extracts a single customer from the database, using its company ID as the primary
  21. point of uniqueness. Shows (appropriate to user privileges) and allows editing
  22. of the customer informations.
  23. =back
  24. =cut
  25. sub get {
  26. my ($request) = @_;
  27. my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all');
  28. $customer->set( entity_class=> '2' );
  29. my $result = $customer->get();
  30. my $template = LedgerSMB::Template->new( user => $user,
  31. template => 'Customer/customer', language => $user->{language},
  32. format => 'HTML');
  33. $template->render($results);
  34. }
  35. sub add_location {
  36. my ($request) = @_;
  37. my $customer = LedgerSMB::DBObject::Customer->new({base => $request, copy => 'all'});
  38. $customer->set( entity_class=> '2' );
  39. $customer->save_location();
  40. $customer->get();
  41. $customer->get_metadata();
  42. my $template = LedgerSMB::Template->new(
  43. user => $user,
  44. template => 'customer',
  45. path => 'UI/Customer',
  46. locale => $request->{_locale},
  47. format => 'HTML');
  48. $request->{script} = 'Customer/customer';
  49. $template->render($customer);
  50. }
  51. =pod
  52. =over
  53. =item add
  54. This method creates a blank screen for entering a customer's information.
  55. =back
  56. =cut
  57. sub add {
  58. my ($request) = @_;
  59. my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all');
  60. $customer->set(entity_class=>2);
  61. $customer->get_metadata();
  62. my $template = LedgerSMB::Template->new(
  63. user => $user,
  64. template => 'customer',
  65. path => 'UI/Customer',
  66. locale => $request->{_locale},
  67. format => 'HTML');
  68. $request->{script} = 'Customer/customer';
  69. $template->render($customer);
  70. }
  71. =pod
  72. =over
  73. =item search($self, $request, $user)
  74. Requires form var: search_pattern
  75. Directly calls the database function search, and returns a set of all customers
  76. found that match the search parameters. Search parameters search over address
  77. as well as customer/Company name.
  78. =back
  79. =cut
  80. sub search {
  81. my ($request) = @_;
  82. if ($request->type() eq 'POST') {
  83. # assume it's asking us to do the search, now
  84. my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all');
  85. $customer->set(entity_class=>2);
  86. my $results = $customer->search($customer->{search_pattern});
  87. my $template = LedgerSMB::Template->new( user => $user,
  88. template => 'Customer/customer', language => $user->{language},
  89. format => 'HTML');
  90. $template->render($results);
  91. }
  92. else {
  93. # grab the happy search page out.
  94. my $template = LedgerSMB::Template->new(
  95. user => $user,
  96. path => 'UI/Customer' ,
  97. template => 'customer_search',
  98. locale => $request->{_locale},
  99. format => 'HTML');
  100. $template->render();
  101. }
  102. }
  103. =pod
  104. =over
  105. =item save($self, $request, $user)
  106. Saves a customer to the database. The function will update or insert a new
  107. customer as needed, and will generate a new Company ID for the customer if needed.
  108. =back
  109. =cut
  110. sub save {
  111. my ($request) = @_;
  112. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  113. $customer->save();
  114. $customer->get_metadata();
  115. my $template = LedgerSMB::Template->new(
  116. user => $request->{_user},
  117. template => 'customer',
  118. locale => $request->{_locale},
  119. path => 'UI/Customer',
  120. format => 'HTML'
  121. );
  122. $template->render($customer);
  123. }
  124. 1;