summaryrefslogtreecommitdiff
path: root/scripts/customer.pl
blob: cf1f734ea9435dc01c8206525ed7987817e67d02 (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 => 'contact', language => $user->{language},
  32. path => 'UI/Contact',
  33. format => 'HTML');
  34. $template->render($results);
  35. }
  36. sub add_location {
  37. my ($request) = @_;
  38. my $customer = LedgerSMB::DBObject::Customer->new({base => $request, copy => 'all'});
  39. $customer->set( entity_class=> '2' );
  40. $customer->save_location();
  41. $customer->get();
  42. $customer->get_metadata();
  43. _render_main_screen($customer);
  44. }
  45. =pod
  46. =over
  47. =item add
  48. This method creates a blank screen for entering a customer's information.
  49. =back
  50. =cut
  51. sub add {
  52. my ($request) = @_;
  53. my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all');
  54. $customer->set( entity_class=> '2' );
  55. _render_main_screen($customer);
  56. }
  57. =pod
  58. =over
  59. =item search($self, $request, $user)
  60. Requires form var: search_pattern
  61. Directly calls the database function search, and returns a set of all customers
  62. found that match the search parameters. Search parameters search over address
  63. as well as customer/Company name.
  64. =back
  65. =cut
  66. sub search {
  67. my ($request) = @_;
  68. if ($request->type() eq 'POST') {
  69. # assume it's asking us to do the search, now
  70. my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all');
  71. $customer->set(entity_class=>2);
  72. my $results = $customer->search($customer->{search_pattern});
  73. my $template = LedgerSMB::Template->new( user => $user,
  74. template => 'Contact/customer', language => $user->{language},
  75. format => 'HTML');
  76. $template->render($results);
  77. }
  78. else {
  79. # grab the happy search page out.
  80. my $template = LedgerSMB::Template->new(
  81. user => $user,
  82. path => 'UI/Contact' ,
  83. template => 'customer_search',
  84. locale => $request->{_locale},
  85. format => 'HTML');
  86. $template->render();
  87. }
  88. }
  89. =pod
  90. =over
  91. =item save($self, $request, $user)
  92. Saves a customer to the database. The function will update or insert a new
  93. customer as needed, and will generate a new Company ID for the customer if needed.
  94. =back
  95. =cut
  96. sub save {
  97. my ($request) = @_;
  98. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  99. $customer->save();
  100. _render_main_screen($customer);
  101. }
  102. sub edit{
  103. my $request = shift @_;
  104. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  105. $customer->get();
  106. _render_main_screen($customer);
  107. }
  108. sub _render_main_screen{
  109. my $customer = shift @_;
  110. $customer->get_metadata();
  111. $customer->{creditlimit} = "$customer->{creditlimit}";
  112. $customer->{discount} = "$customer->{discount}";
  113. $customer->{script} = "customer.pl";
  114. my $template = LedgerSMB::Template->new(
  115. user => $customer->{_user},
  116. template => 'contact',
  117. locale => $customer->{_locale},
  118. path => 'UI/Contact',
  119. format => 'HTML'
  120. );
  121. $template->render($customer);
  122. }
  123. sub save_contact {
  124. my ($request) = @_;
  125. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  126. $customer->save_contact();
  127. $customer->get;
  128. _render_main_screen($customer);
  129. }
  130. sub save_bank_account {
  131. my ($request) = @_;
  132. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  133. $customer->save_bank_account();
  134. $customer->get;
  135. _render_main_screen($customer);
  136. }
  137. 1;