summaryrefslogtreecommitdiff
path: root/LedgerSMB/ScriptLib/Company.pm
blob: 9b9b87c9ef7c91d6f721434b0f8e75b630ac796b (plain)
  1. package LedgerSMB::ScriptLib::Company;
  2. use LedgerSMB::Template;
  3. my $ec_labels = {
  4. 1 => 'Vendor',
  5. 2 => 'Customer',
  6. };
  7. =pod
  8. =head1 NAME
  9. LedgerSMB::ScriptLib::Company - LedgerSMB class defining the Controller
  10. functions, template instantiation and rendering for vendor and customer editing
  11. and display. This would also form the basis for other forms of company
  12. contacts.
  13. =head1 SYOPSIS
  14. This module is the UI controller for the vendor DB access; it provides the
  15. View interface, as well as defines the Save vendor.
  16. Save vendor/customer will update or create as needed.
  17. =head1 METHODS
  18. =over
  19. =item set_entity_class($request) returns int entity class
  20. Errors if not inherited. Inheriting classes MUST define this to set
  21. $entity_class appropriately.
  22. =back
  23. =cut
  24. sub set_entity_class {
  25. my ($request) = @_;
  26. $request->{_script_handle}->set_entity_class(@_) || $request->error(
  27. "Error: Cannot call LedgerSMB::ScriptLib::Company::set_entity_class " .
  28. "directly!");
  29. }
  30. =pod
  31. =over
  32. =item new_company($request)
  33. returns object inheriting LedgerSMB::DBObject::Company
  34. This too must be defined in classes that inherit this class.
  35. =back
  36. =cut
  37. sub new_company {
  38. my ($request) = @_;
  39. $request->{_script_handle}->new_company(@_) || $request->error(
  40. "Error: Cannot call LedgerSMB::ScriptLib::Company::new_company " .
  41. "directly!");
  42. }
  43. =pod
  44. =over
  45. =item get($self, $request, $user)
  46. Requires form var: id
  47. Extracts a single company from the database, using its company ID as the primary
  48. point of uniqueness. Shows (appropriate to user privileges) and allows editing
  49. of the company informations.
  50. =back
  51. =cut
  52. sub get {
  53. my ($request) = @_;
  54. my $company = new_company($request);
  55. set_entity_class($company);
  56. $company->get();
  57. $company->get_credit_id();
  58. $company->get_metadata();
  59. _render_main_screen($company);
  60. }
  61. =pod
  62. =over
  63. =item add_location
  64. Adds a location to the company as defined in the inherited object
  65. =back
  66. =cut
  67. sub add_location {
  68. my ($request) = @_;
  69. my $company = new_company($request);
  70. set_entity_class($company);
  71. $company->save_location();
  72. $company->get();
  73. $company->get_metadata();
  74. _render_main_screen($company);
  75. }
  76. =pod
  77. =over
  78. =item generate_control_code
  79. Sets $company->{control_code} equal to the next in the series of entity_control
  80. values
  81. =back
  82. =cut
  83. sub generate_control_code {
  84. my ($request) = @_;
  85. my $company = new_company($request);
  86. my ($ref) = $company->call_procedure(
  87. procname => 'setting_increment',
  88. args => ['entity_control']
  89. );
  90. ($company->{control_code}) = values %$ref;
  91. $company->{dbh}->commit;
  92. if ($company->{meta_number}){
  93. edit($company);
  94. } else {
  95. _render_main_screen($company);
  96. }
  97. }
  98. =pod
  99. =over
  100. =item add
  101. This method creates a blank screen for entering a company's information.
  102. =back
  103. =cut
  104. sub add {
  105. my ($request) = @_;
  106. my $company = new_company($request);
  107. set_entity_class($company);
  108. _render_main_screen($company);
  109. }
  110. =pod
  111. =over
  112. =item get_result($self, $request, $user)
  113. Requires form var: search_pattern
  114. Directly calls the database function search, and returns a set of all vendors
  115. found that match the search parameters. Search parameters search over address
  116. as well as vendor/Company name.
  117. =back
  118. =cut
  119. sub get_results {
  120. my ($request) = @_;
  121. my $company = new_company($request);
  122. set_entity_class($company);
  123. $company->{contact_info} =
  124. qq|{"%$request->{email}%","%$request->{phone}%"}|;
  125. my $results = $company->search();
  126. if ($company->{order_by}){
  127. # TODO: Set ordering logic
  128. };
  129. # URL Setup
  130. my $baseurl = "$request->{script}";
  131. my $search_url = "$base_url?action=get_results";
  132. my $get_url = "$base_url?action=get&account_class=$request->{account_class}";
  133. for (keys %$vendor){
  134. next if $_ eq 'order_by';
  135. $search_url .= "&$_=$form->{$_}";
  136. }
  137. # Column definitions for dynatable
  138. @columns = qw(legal_name meta_number business_type curr);
  139. my %column_heading;
  140. $column_heading{legal_name} = {
  141. text => $request->{_locale}->text('Name'),
  142. href => "$search_url&order_by=legal_name",
  143. };
  144. $column_heading{meta_number} = {
  145. text => $request->{_locale}->text('Vendor Number'),
  146. href => "$search_url&order_by=meta_number",
  147. };
  148. $column_heading{business_type} = {
  149. text => $request->{_locale}->text('Business Type'),
  150. href => "$search_url&order_by=business_type",
  151. };
  152. $column_heading{curr} = {
  153. text => $request->{_locale}->text('Currency'),
  154. href => "$search_url&order_by=curr",
  155. };
  156. my @rows;
  157. for $ref (@{$company->{search_results}}){
  158. push @rows,
  159. {legal_name => $ref->{legal_name},
  160. meta_number => {text => $ref->{meta_number},
  161. href => "$get_url&entity_id=$ref->{entity_id}" . "&meta_number=$ref->{meta_number}"
  162. },
  163. business_type => $ref->{business_type},
  164. curr => $ref->{curr},
  165. };
  166. }
  167. # CT: The CSV Report is broken. I get:
  168. # Not an ARRAY reference at
  169. # /usr/lib/perl5/site_perl/5.8.8/CGI/Simple.pm line 423
  170. # Disabling the button for now.
  171. my @buttons = (
  172. # {name => 'action',
  173. # value => 'csv_company_list',
  174. # text => $company->{_locale}->text('CSV Report'),
  175. # type => 'submit',
  176. # class => 'submit',
  177. # },
  178. {name => 'action',
  179. value => 'add',
  180. text => $company->{_locale}->text('Add Vendor'),
  181. type => 'submit',
  182. class => 'submit',
  183. }
  184. );
  185. my $template = LedgerSMB::Template->new(
  186. user => $user,
  187. path => 'UI' ,
  188. template => 'form-dynatable',
  189. locale => $company->{_locale},
  190. format => ($request->{FORMAT}) ? $request->{FORMAT} : 'HTML',
  191. );
  192. $template->render({
  193. form => $company,
  194. columns => \@columns,
  195. hiddens => $company,
  196. buttons => \@buttons,
  197. heading => \%column_heading,
  198. rows => \@rows,
  199. });
  200. }
  201. =pod
  202. =over
  203. =item csv_company_list($request)
  204. Generates CSV report (not working at present)
  205. =back
  206. =cut
  207. sub csv_company_list {
  208. my ($request) = @_;
  209. $request->{FORMAT} = 'CSV';
  210. get_results($request);
  211. }
  212. =pod
  213. =over
  214. =item save($self, $request, $user)
  215. Saves a company to the database. The function will update or insert a new
  216. company as needed, and will generate a new Company ID for the company if needed.
  217. =back
  218. =cut
  219. sub save {
  220. my ($request) = @_;
  221. my $company = new_company($request);
  222. $company->save();
  223. _render_main_screen($company);
  224. }
  225. =pod
  226. =over
  227. =item save_credit($request)
  228. This inserts or updates a credit account of the sort listed here.
  229. =back
  230. =cut
  231. sub save_credit {
  232. my ($request) = @_;
  233. my $company = new_company($request);
  234. $company->save_credit();
  235. $company->get();
  236. _render_main_screen($company);
  237. }
  238. =pod
  239. =over
  240. =item save_credit_new($request)
  241. This inserts a new credit account.
  242. =back
  243. =cut
  244. sub save_credit_new {
  245. my ($request) = @_;
  246. $request->{credit_id} = undef;
  247. save_credit($request);
  248. }
  249. =pod
  250. =over
  251. =item edit($request)
  252. Displays a company for editing. Needs the following to be set:
  253. entity_id, account_class, and meta_number.
  254. =back
  255. =cut
  256. sub edit{
  257. my $request = shift @_;
  258. my $company = LedgerSMB::DBObject::Vendor->new({base => $request});
  259. $company->get();
  260. _render_main_screen($company);
  261. }
  262. =pod
  263. =over
  264. =item PRIVATE _render_main_screen($company)
  265. Pulls relevant data from db and renders the data entry screen for it.
  266. =back
  267. =cut
  268. sub _render_main_screen{
  269. my $company = shift @_;
  270. $company->get_metadata();
  271. $company->{creditlimit} = "$company->{creditlimit}";
  272. $company->{discount} = "$company->{discount}";
  273. $company->{note_class_options} = [
  274. {label => 'Entity', value => 1},
  275. {label => $ec_labels->{"$company->{entity_class}"} . ' Account',
  276. value => 3},
  277. ];
  278. $company->{threshold} = $company->format_amount(amount => $company->{threshold});
  279. my $template = LedgerSMB::Template->new(
  280. user => $company->{_user},
  281. template => 'contact',
  282. locale => $company->{_locale},
  283. path => 'UI/Contact',
  284. format => 'HTML'
  285. );
  286. $template->render($company);
  287. }
  288. =pod
  289. =over
  290. =item search($request)
  291. Renders the search criteria screen.
  292. =back
  293. =cut
  294. sub search {
  295. my ($request) = @_;
  296. set_entity_class($request);
  297. my $template = LedgerSMB::Template->new(
  298. user => $request->{_user},
  299. template => 'search',
  300. locale => $request->{_locale},
  301. path => 'UI/Contact',
  302. format => 'HTML'
  303. );
  304. $template->render($request);
  305. }
  306. =pod
  307. =over
  308. =item save_contact($request)
  309. Saves contact info as per LedgerSMB::DBObject::Company::save_contact.
  310. =back
  311. =cut
  312. sub save_contact {
  313. my ($request) = @_;
  314. my $company = new_company($request);
  315. $company->save_contact();
  316. $company->get;
  317. _render_main_screen($company );
  318. }
  319. =pod
  320. =over
  321. =item save_contact_new($request)
  322. Saves contact info as a new line as per save_contact above.
  323. =cut
  324. sub save_contact_new{
  325. my ($request) = @_;
  326. delete $request->{old_contact};
  327. delete $request->{old_contact_class};
  328. save_contact($request);
  329. }
  330. =pod
  331. =over
  332. =item save_bank_account($request)
  333. Adds a bank account to a company and, if defined, an entity credit account.
  334. =back
  335. =cut
  336. sub save_bank_account {
  337. my ($request) = @_;
  338. my $company = new_company($request);
  339. $company->save_bank_account();
  340. $company->get;
  341. _render_main_screen($company );
  342. }
  343. sub save_notes {
  344. my ($request) = @_;
  345. my $company = new_company($company);
  346. $company->save_notes();
  347. $company->get();
  348. _render_main_screen($company );
  349. }
  350. 1;