summaryrefslogtreecommitdiff
path: root/scripts/vendor.pl
blob: 7e5c537142332ae4041f4ef5fd936e712d373246 (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 get_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. _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. $vendor->{note_class_options} = [
  188. {label => 'Entity', value => 1},
  189. {label => 'Vendor Account', value => 3},
  190. ];
  191. $vendor->{threshold} = $vendor->format_amount(amount => $vendor->{threshold});
  192. my $template = LedgerSMB::Template->new(
  193. user => $vendor->{_user},
  194. template => 'contact',
  195. locale => $vendor->{_locale},
  196. path => 'UI/Contact',
  197. format => 'HTML'
  198. );
  199. $template->render($vendor);
  200. }
  201. sub search {
  202. my ($request) = @_;
  203. $request->{account_class} = 1;
  204. $request->{script} = 'vendor.pl';
  205. my $template = LedgerSMB::Template->new(
  206. user => $request->{_user},
  207. template => 'search',
  208. locale => $request->{_locale},
  209. path => 'UI/Contact',
  210. format => 'HTML'
  211. );
  212. $template->render($request);
  213. }
  214. sub save_contact {
  215. my ($request) = @_;
  216. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  217. $vendor->save_contact();
  218. $vendor->get;
  219. _render_main_screen($vendor );
  220. }
  221. sub save_bank_account {
  222. my ($request) = @_;
  223. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  224. $vendor->save_bank_account();
  225. $vendor->get;
  226. _render_main_screen($vendor );
  227. }
  228. sub save_notes {
  229. my ($request) = @_;
  230. my $vendor = LedgerSMB::DBObject::Vendor->new({base => $request});
  231. $vendor->save_notes();
  232. $vendor->get();
  233. _render_main_screen($vendor );
  234. }
  235. eval { do "scripts/custom/vendor.pl"};
  236. 1;