summaryrefslogtreecommitdiff
path: root/scripts/vendor.pl
blob: 2119d46c3bd1530eef5fe2fa38c1002f5a374688 (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}" . "&meta_number=$ref->{meta_number}"
  106. },
  107. business_type => $ref->{business_type},
  108. curr => $ref->{curr},
  109. };
  110. }
  111. # CT: The CSV Report is broken. I get:
  112. # Not an ARRAY reference at
  113. # /usr/lib/perl5/site_perl/5.8.8/CGI/Simple.pm line 423
  114. # Disabling the button for now.
  115. my @buttons = (
  116. # {name => 'action',
  117. # value => 'csv_vendor_list',
  118. # text => $vendor->{_locale}->text('CSV Report'),
  119. # type => 'submit',
  120. # class => 'submit',
  121. # },
  122. {name => 'action',
  123. value => 'add',
  124. text => $vendor->{_locale}->text('Add Vendor'),
  125. type => 'submit',
  126. class => 'submit',
  127. }
  128. );
  129. my $template = LedgerSMB::Template->new(
  130. user => $user,
  131. path => 'UI' ,
  132. template => 'form-dynatable',
  133. locale => $vendor->{_locale},
  134. format => ($request->{FORMAT}) ? $request->{FORMAT} : 'HTML',
  135. );
  136. $template->render({
  137. form => $vendor,
  138. columns => \@columns,
  139. hiddens => $vendor,
  140. buttons => \@buttons,
  141. heading => \%column_heading,
  142. rows => \@rows,
  143. });
  144. }
  145. sub csv_vendor_list {
  146. my ($request) = @_;
  147. $request->{FORMAT} = 'CSV';
  148. get_results($request);
  149. }
  150. =pod
  151. =over
  152. =item save($self, $request, $user)
  153. Saves a vendor to the database. The function will update or insert a new
  154. vendor as needed, and will generate a new Company ID for the vendor if needed.
  155. =back
  156. =cut
  157. sub save {
  158. my ($request) = @_;
  159. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  160. $vendor->save();
  161. _render_main_screen($vendor);
  162. }
  163. sub save_credit {
  164. my ($request) = @_;
  165. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  166. $vendor->save_credit();
  167. $vendor->get();
  168. _render_main_screen($vendor);
  169. }
  170. sub save_credit_new {
  171. my ($request) = @_;
  172. $request->{credit_id} = undef;
  173. save_credit($request);
  174. }
  175. sub edit{
  176. my $request = shift @_;
  177. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  178. $vendor->get();
  179. $vendor->get_credit_id();
  180. _render_main_screen($vendor);
  181. }
  182. sub _render_main_screen{
  183. my $vendor = shift @_;
  184. $vendor->get_metadata();
  185. $vendor->{creditlimit} = "$vendor->{creditlimit}";
  186. $vendor->{discount} = "$vendor->{discount}";
  187. $vendor->{script} = "vendor.pl";
  188. my $template = LedgerSMB::Template->new(
  189. user => $vendor->{_user},
  190. template => 'contact',
  191. locale => $vendor->{_locale},
  192. path => 'UI/Contact',
  193. format => 'HTML'
  194. );
  195. $template->render($vendor);
  196. }
  197. sub search {
  198. my ($request) = @_;
  199. $request->{account_class} = 1;
  200. $request->{script} = 'vendor.pl';
  201. my $template = LedgerSMB::Template->new(
  202. user => $request->{_user},
  203. template => 'search',
  204. locale => $request->{_locale},
  205. path => 'UI/Contact',
  206. format => 'HTML'
  207. );
  208. $template->render($request);
  209. }
  210. sub save_contact {
  211. my ($request) = @_;
  212. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  213. $vendor->save_contact();
  214. $vendor->get;
  215. _render_main_screen($vendor );
  216. }
  217. sub save_bank_account {
  218. my ($request) = @_;
  219. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  220. $vendor->save_bank_account();
  221. $vendor->get;
  222. _render_main_screen($vendor );
  223. }
  224. sub save_notes {
  225. my ($request) = @_;
  226. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  227. $vendor->save_notes();
  228. $vendor->get();
  229. _render_main_screen($vendor );
  230. }
  231. eval { do "scripts/custom/vendor.pl"};
  232. 1;