summaryrefslogtreecommitdiff
path: root/LedgerSMB/ScriptLib/Company.pm
blob: 1c9f232153606f3ff0322cbfdf69c65f053a8083 (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 entity_control_code meta_number credit_description
  139. business_type curr);
  140. my %column_heading;
  141. $column_heading{legal_name} = {
  142. text => $request->{_locale}->text('Name'),
  143. href => "$search_url&order_by=legal_name",
  144. };
  145. $column_heading{entity_control_code} = {
  146. text => $request->{_locale}->text('Control Code'),
  147. href => "$search_url&order_by=entity_control_code",
  148. };
  149. $column_heading{legal_name} = {
  150. text => $request->{_locale}->text('Name'),
  151. href => "$search_url&order_by=legal_name",
  152. };
  153. $column_heading{meta_number} = {
  154. text => $request->{_locale}->text('Vendor Number'),
  155. href => "$search_url&order_by=meta_number",
  156. };
  157. $column_heading{credit_description} = {
  158. text => $request->{_locale}->text('Description'),
  159. href => "$search_url&order_by=credit_description",
  160. };
  161. $column_heading{business_type} = {
  162. text => $request->{_locale}->text('Business Type'),
  163. href => "$search_url&order_by=business_type",
  164. };
  165. $column_heading{curr} = {
  166. text => $request->{_locale}->text('Currency'),
  167. href => "$search_url&order_by=curr",
  168. };
  169. my @rows;
  170. for $ref (@{$company->{search_results}}){
  171. push @rows,
  172. {legal_name => $ref->{legal_name},
  173. entity_control_code => $ref->{entity_control_code},
  174. credit_description => $ref->{credit_description},
  175. meta_number => {text => $ref->{meta_number},
  176. href => "$get_url&entity_id=$ref->{entity_id}" . "&meta_number=$ref->{meta_number}"
  177. },
  178. business_type => $ref->{business_type},
  179. curr => $ref->{curr},
  180. };
  181. }
  182. # CT: The CSV Report is broken. I get:
  183. # Not an ARRAY reference at
  184. # /usr/lib/perl5/site_perl/5.8.8/CGI/Simple.pm line 423
  185. # Disabling the button for now.
  186. my @buttons = (
  187. # {name => 'action',
  188. # value => 'csv_company_list',
  189. # text => $company->{_locale}->text('CSV Report'),
  190. # type => 'submit',
  191. # class => 'submit',
  192. # },
  193. {name => 'action',
  194. value => 'add',
  195. text => $company->{_locale}->text('Add Vendor'),
  196. type => 'submit',
  197. class => 'submit',
  198. }
  199. );
  200. my $template = LedgerSMB::Template->new(
  201. user => $user,
  202. path => 'UI' ,
  203. template => 'form-dynatable',
  204. locale => $company->{_locale},
  205. format => ($request->{FORMAT}) ? $request->{FORMAT} : 'HTML',
  206. );
  207. $template->render({
  208. form => $company,
  209. columns => \@columns,
  210. hiddens => $company,
  211. buttons => \@buttons,
  212. heading => \%column_heading,
  213. rows => \@rows,
  214. });
  215. }
  216. =pod
  217. =over
  218. =item csv_company_list($request)
  219. Generates CSV report (not working at present)
  220. =back
  221. =cut
  222. sub csv_company_list {
  223. my ($request) = @_;
  224. $request->{FORMAT} = 'CSV';
  225. get_results($request);
  226. }
  227. =pod
  228. =over
  229. =item save($self, $request, $user)
  230. Saves a company to the database. The function will update or insert a new
  231. company as needed, and will generate a new Company ID for the company if needed.
  232. =back
  233. =cut
  234. sub save {
  235. my ($request) = @_;
  236. my $company = new_company($request);
  237. $company->save();
  238. _render_main_screen($company);
  239. }
  240. =pod
  241. =over
  242. =item save_credit($request)
  243. This inserts or updates a credit account of the sort listed here.
  244. =back
  245. =cut
  246. sub save_credit {
  247. my ($request) = @_;
  248. my $company = new_company($request);
  249. $company->save_credit();
  250. $company->get();
  251. _render_main_screen($company);
  252. }
  253. =pod
  254. =over
  255. =item save_credit_new($request)
  256. This inserts a new credit account.
  257. =back
  258. =cut
  259. sub save_credit_new {
  260. my ($request) = @_;
  261. $request->{credit_id} = undef;
  262. save_credit($request);
  263. }
  264. =pod
  265. =over
  266. =item edit($request)
  267. Displays a company for editing. Needs the following to be set:
  268. entity_id, account_class, and meta_number.
  269. =back
  270. =cut
  271. sub edit{
  272. my $request = shift @_;
  273. my $company = LedgerSMB::DBObject::Vendor->new({base => $request});
  274. $company->get();
  275. _render_main_screen($company);
  276. }
  277. =pod
  278. =over
  279. =item PRIVATE _render_main_screen($company)
  280. Pulls relevant data from db and renders the data entry screen for it.
  281. =back
  282. =cut
  283. sub _render_main_screen{
  284. my $company = shift @_;
  285. $company->get_metadata();
  286. $company->{creditlimit} = "$company->{creditlimit}";
  287. $company->{discount} = "$company->{discount}";
  288. $company->{note_class_options} = [
  289. {label => 'Entity', value => 1},
  290. {label => $ec_labels->{"$company->{entity_class}"} . ' Account',
  291. value => 3},
  292. ];
  293. $company->{threshold} = $company->format_amount(amount => $company->{threshold});
  294. my $template = LedgerSMB::Template->new(
  295. user => $company->{_user},
  296. template => 'contact',
  297. locale => $company->{_locale},
  298. path => 'UI/Contact',
  299. format => 'HTML'
  300. );
  301. $template->render($company);
  302. }
  303. =pod
  304. =over
  305. =item search($request)
  306. Renders the search criteria screen.
  307. =back
  308. =cut
  309. sub search {
  310. my ($request) = @_;
  311. set_entity_class($request);
  312. my $template = LedgerSMB::Template->new(
  313. user => $request->{_user},
  314. template => 'search',
  315. locale => $request->{_locale},
  316. path => 'UI/Contact',
  317. format => 'HTML'
  318. );
  319. $template->render($request);
  320. }
  321. =pod
  322. =over
  323. =item save_contact($request)
  324. Saves contact info as per LedgerSMB::DBObject::Company::save_contact.
  325. =back
  326. =cut
  327. sub save_contact {
  328. my ($request) = @_;
  329. my $company = new_company($request);
  330. $company->save_contact();
  331. $company->get;
  332. _render_main_screen($company );
  333. }
  334. =pod
  335. =over
  336. =item save_contact_new($request)
  337. Saves contact info as a new line as per save_contact above.
  338. =cut
  339. sub save_contact_new{
  340. my ($request) = @_;
  341. delete $request->{old_contact};
  342. delete $request->{old_contact_class};
  343. save_contact($request);
  344. }
  345. =pod
  346. =over
  347. =item save_bank_account($request)
  348. Adds a bank account to a company and, if defined, an entity credit account.
  349. =back
  350. =cut
  351. sub save_bank_account {
  352. my ($request) = @_;
  353. my $company = new_company($request);
  354. $company->save_bank_account();
  355. $company->get;
  356. _render_main_screen($company );
  357. }
  358. sub save_notes {
  359. my ($request) = @_;
  360. my $company = new_company($request);
  361. $company->save_notes();
  362. $company->get();
  363. _render_main_screen($company );
  364. }
  365. 1;