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