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