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