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