summaryrefslogtreecommitdiff
path: root/scripts/customer.pl
blob: fb1297bab13e691d6a1c5d83a819e2cf5e433530 (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(
  32. template => 'contact',
  33. user => $request->{_user},
  34. locale => $request->{_locale},
  35. path => 'UI/Contact',
  36. format => 'HTML');
  37. $template->render($results);
  38. }
  39. sub add_location {
  40. my ($request) = @_;
  41. my $customer = LedgerSMB::DBObject::Customer->new({base => $request, copy => 'all'});
  42. $customer->set( entity_class=> '2' );
  43. $customer->save_location();
  44. $customer->get();
  45. $customer->get_metadata();
  46. _render_main_screen($customer);
  47. }
  48. =pod
  49. =over
  50. =item add
  51. This method creates a blank screen for entering a customer's information.
  52. =back
  53. =cut
  54. sub add {
  55. my ($request) = @_;
  56. my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all');
  57. $customer->set( entity_class=> '2' );
  58. _render_main_screen($customer);
  59. }
  60. =pod
  61. =over
  62. =item search($self, $request, $user)
  63. Requires form var: search_pattern
  64. Directly calls the database function search, and returns a set of all customers
  65. found that match the search parameters. Search parameters search over address
  66. as well as customer/Company name.
  67. =back
  68. =cut
  69. sub search {
  70. my ($request) = @_;
  71. $request->{script} = "customer.pl";
  72. $request->{account_class} = 2;
  73. my $template = LedgerSMB::Template->new(
  74. user => $request->{_user},
  75. path => 'UI/Contact' ,
  76. template => 'search',
  77. locale => $request->{_locale},
  78. format => 'HTML');
  79. $template->render($request);
  80. }
  81. =pod
  82. =item get_result($self, $request, $user)
  83. Requires form var: search_pattern
  84. Directly calls the database function search, and returns a set of all vendors
  85. found that match the search parameters. Search parameters search over address
  86. as well as vendor/Company name.
  87. =back
  88. =cut
  89. sub get_results {
  90. my ($request) = @_;
  91. my $customer = LedgerSMB::DBObject::Customer->new(base => $request, copy => 'all');
  92. $customer->set(entity_class=>2);
  93. $customer->{contact_info} = qq|{"%$request->{email}%","%$request->{phone}%"}|;
  94. my $results = $customer->search();
  95. if ($customer->{order_by}){
  96. # TODO: Set ordering logic
  97. };
  98. # URL Setup
  99. my $baseurl = "$request->{script}";
  100. my $search_url = "$base_url?action=get_results";
  101. my $get_url = "$base_url?action=get&account_class=$request->{account_class}";
  102. for (keys %$vendor){
  103. next if $_ eq 'order_by';
  104. $search_url .= "&$_=$form->{$_}";
  105. }
  106. # Column definitions for dynatable
  107. @columns = qw(legal_name meta_number business_type curr);
  108. my %column_heading;
  109. $column_heading{legal_name} = {
  110. text => $request->{_locale}->text('Name'),
  111. href => "$search_url&order_by=legal_name",
  112. };
  113. $column_heading{meta_number} = {
  114. text => $request->{_locale}->text('Customer Number'),
  115. href => "$search_url&order_by=meta_number",
  116. };
  117. $column_heading{business_type} = {
  118. text => $request->{_locale}->text('Business Type'),
  119. href => "$search_url&order_by=business_type",
  120. };
  121. $column_heading{curr} = {
  122. text => $request->{_locale}->text('Currency'),
  123. href => "$search_url&order_by=curr",
  124. };
  125. my @rows;
  126. for $ref (@{$customer->{search_results}}){
  127. push @rows,
  128. {legal_name => $ref->{legal_name},
  129. meta_number => {text => $ref->{meta_number},
  130. href => "$get_url&entity_id=$ref->{entity_id}" . "&meta_number=$ref->{meta_number}"
  131. },
  132. business_type => $ref->{business_type},
  133. curr => $ref->{curr},
  134. };
  135. }
  136. # CT: The CSV Report is broken. I get:
  137. # Not an ARRAY reference at
  138. # /usr/lib/perl5/site_perl/5.8.8/CGI/Simple.pm line 423
  139. # Disabling the button for now.
  140. my @buttons = (
  141. # {name => 'action',
  142. # value => 'csv_vendor_list',
  143. # text => $vendor->{_locale}->text('CSV Report'),
  144. # type => 'submit',
  145. # class => 'submit',
  146. # },
  147. {name => 'action',
  148. value => 'add',
  149. text => $customer->{_locale}->text('Add Customer'),
  150. type => 'submit',
  151. class => 'submit',
  152. }
  153. );
  154. my $template = LedgerSMB::Template->new(
  155. user => $user,
  156. path => 'UI' ,
  157. template => 'form-dynatable',
  158. locale => $customer->{_locale},
  159. format => ($request->{FORMAT}) ? $request->{FORMAT} : 'HTML',
  160. );
  161. $template->render({
  162. form => $customer,
  163. columns => \@columns,
  164. hiddens => $vendor,
  165. buttons => \@buttons,
  166. heading => \%column_heading,
  167. rows => \@rows,
  168. });
  169. }
  170. =pod
  171. =over
  172. =item save($self, $request, $user)
  173. Saves a customer to the database. The function will update or insert a new
  174. customer as needed, and will generate a new Company ID for the customer if needed.
  175. =back
  176. =cut
  177. sub save {
  178. my ($request) = @_;
  179. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  180. $customer->save();
  181. _render_main_screen($customer);
  182. }
  183. sub save_credit {
  184. my ($request) = @_;
  185. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  186. $customer->save_credit();
  187. $customer->get();
  188. _render_main_screen($customer);
  189. }
  190. sub save_credit_new {
  191. my ($request) = @_;
  192. $request->{credit_id} = undef;
  193. save_credit($request);
  194. }
  195. sub edit{
  196. my $request = shift @_;
  197. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  198. $customer->get();
  199. $customer->get_credit_id();
  200. _render_main_screen($customer);
  201. }
  202. sub _render_main_screen{
  203. my $customer = shift @_;
  204. $customer->get_metadata();
  205. $customer->{creditlimit} = "$customer->{creditlimit}";
  206. $customer->{discount} = "$customer->{discount}";
  207. $customer->{script} = "customer.pl";
  208. my $template = LedgerSMB::Template->new(
  209. user => $customer->{_user},
  210. template => 'contact',
  211. locale => $customer->{_locale},
  212. path => 'UI/Contact',
  213. format => 'HTML'
  214. );
  215. $template->render($customer);
  216. }
  217. sub save_contact {
  218. my ($request) = @_;
  219. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  220. $customer->save_contact();
  221. $customer->get;
  222. _render_main_screen($customer);
  223. }
  224. sub save_bank_account {
  225. my ($request) = @_;
  226. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  227. $customer->save_bank_account();
  228. $customer->get;
  229. _render_main_screen($customer);
  230. }
  231. sub save_notes {
  232. my ($request) = @_;
  233. my $customer = LedgerSMB::DBObject::Customer->new({base => $request});
  234. $customer->save_notes();
  235. $customer->get();
  236. _render_main_screen($customer);
  237. }
  238. eval { do "scripts/custom/customer.pl"};
  239. 1;