summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Company.pm
blob: a0cce9141727c168789bb4e8dc3e65692a124cc4 (plain)
  1. =head1 NAME
  2. LedgerSMB::DBObject::Company.pm, LedgerSMB Base Class for Customers/Vendors
  3. =head1 SYNOPSIS
  4. This library contains the base utility functions for creating, saving, and
  5. retrieving customers and vendors.
  6. =cut
  7. package LedgerSMB::DBObject::Company;
  8. use base qw(LedgerSMB::DBObject);
  9. use strict;
  10. =head1 METHODS
  11. =over
  12. =item $company->set_entity_class()
  13. This is a stub for a private method that subclasses are expected to overwrite.
  14. It will be set to the account class of the entity (1 for vendor, 2 for customer,
  15. etc).
  16. =back
  17. =cut
  18. sub set_entity_class {
  19. my $self = shift @_;
  20. if (!defined $self->{entity_class}){
  21. $self->error("Entity ID Not Set and No Entity Class Defined!");
  22. }
  23. }
  24. =over
  25. =item save()
  26. This stores the company record including a credit accoun tin the database.
  27. TODO: Separate company from credit account storage.
  28. =back
  29. =cut
  30. sub save {
  31. my $self = shift @_;
  32. $self->set_entity_class();
  33. my ($ref) = $self->exec_method(funcname => 'company_save');
  34. $self->{entity_id} = (values %$ref)[0];
  35. $self->get;
  36. $self->get_metadata;
  37. $self->{dbh}->commit;
  38. }
  39. =over
  40. =item save_credit
  41. This method saves the credit account for the company.
  42. =back
  43. =cut
  44. sub save_credit {
  45. my $self = shift @_;
  46. $self->set_entity_class();
  47. $self->{threshold} = $self->parse_amount(amount => $self->{threshold});
  48. my ($ref) = $self->exec_method(funcname => 'entity_credit_save');
  49. $self->{credit_id} = (values %$ref)[0];
  50. $self->{threshold} = $self->format_amount(amount => $self->{threshold});
  51. $self->{dbh}->commit;
  52. }
  53. =over
  54. =item save_location
  55. This method saves an address for a company.
  56. =back
  57. =cut
  58. sub save_location {
  59. my $self = shift @_;
  60. $self->{country_id} = $self->{country_code};
  61. if($self->{credit_id}){
  62. $self->exec_method(funcname => 'eca__location_save');
  63. } else {
  64. my ($ref) = $self->exec_method(funcname => 'company__location_save');
  65. my @vals = values %$ref;
  66. $self->{location_id} = $vals[0];
  67. }
  68. $self->{dbh}->commit;
  69. }
  70. =over
  71. =item get_credit_id
  72. This method returns the current credit id from the screen.
  73. =back
  74. =cut
  75. sub get_credit_id {
  76. my $self = shift @_;
  77. my ($ref) = $self->exec_method(
  78. funcname => 'entity_credit_get_id'
  79. );
  80. $self->{credit_id} = $ref->{'entity_credit_get_id'};
  81. }
  82. =over
  83. =item get_metadata()
  84. This retrieves various information vor building the user interface. Among other
  85. things, it sets the following properties:
  86. $self->{ar_ap_acc_list} = qw(list of ar or ap accounts)
  87. $self->{cash_acc_list} = qw(list of cash accounts)
  88. =back
  89. =cut
  90. sub get_metadata {
  91. my $self = shift @_;
  92. @{$self->{ar_ap_acc_list}} =
  93. $self->exec_method(funcname => 'chart_get_ar_ap');
  94. for my $ref (@{$self->{ar_ap_acc_list}}){
  95. $ref->{text} = "$ref->{accno}--$ref->{description}";
  96. }
  97. @{$self->{cash_acc_list}} =
  98. $self->exec_method(funcname => 'chart_list_cash');
  99. for my $ref (@{$self->{cash_acc_list}}){
  100. $ref->{text} = "$ref->{accno}--$ref->{description}";
  101. }
  102. @{$self->{location_class_list}} =
  103. $self->exec_method(funcname => 'location_list_class');
  104. @{$self->{business_types}} =
  105. $self->exec_method(funcname => 'business_type__list');
  106. @{$self->{country_list}} =
  107. $self->exec_method(funcname => 'location_list_country');
  108. @{$self->{contact_class_list}} =
  109. $self->exec_method(funcname => 'entity_list_contact_class');
  110. }
  111. sub save_contact {
  112. my ($self) = @_;
  113. if ($self->{credit_id}){
  114. $self->exec_method(funcname => 'eca__save_contact');
  115. } else {
  116. $self->exec_method(funcname => 'company__save_contact');
  117. }
  118. $self->{dbh}->commit;
  119. }
  120. sub save_bank_account {
  121. my $self = shift @_;
  122. $self->exec_method(funcname => 'entity__save_bank_account');
  123. $self->{dbh}->commit;
  124. }
  125. sub save_notes {
  126. my $self = shift @_;
  127. if ($self->{credit_id} && $self->{note_class} eq '3'){
  128. $self->exec_method(funcname => 'eca__save_notes');
  129. } else {
  130. $self->exec_method(funcname => 'entity__save_notes');
  131. }
  132. $self->{dbh}->commit;
  133. }
  134. sub search {
  135. my ($self) = @_;
  136. @{$self->{search_results}} =
  137. $self->exec_method(funcname => 'company__search');
  138. return @{$self->{search_results}};
  139. }
  140. sub get_billing_info {
  141. my $self = shift @_;
  142. $self->set_entity_class();
  143. my ($ref) = $self->exec_method(funcname => 'company_get_billing_info');
  144. $self->merge($ref);
  145. }
  146. sub get {
  147. my $self = shift @_;
  148. $self->set_entity_class();
  149. my ($ref) = $self->exec_method(funcname => 'company_retrieve');
  150. $self->merge($ref);
  151. $self->{threshold} = $self->format_amount(amount => $self->{threshold});
  152. @{$self->{credit_list}} =
  153. $self->exec_method(funcname => 'entity__list_credit');
  154. for (@{$self->{credit_list}}){
  155. print STDERR "credit_id: $_->{credit_id}\n";
  156. if (($_->{credit_id} eq $self->{credit_id})
  157. or ($_->{meta_number} eq $self->{meta_number})
  158. or ($_->{id} eq $self->{credit_id})){
  159. $self->merge($_);
  160. last;
  161. }
  162. }
  163. $self->{name} = $self->{legal_name};
  164. if ($self->{credit_id} and $self->{meta_number}){
  165. $self->get_credit_id;
  166. }
  167. if ($self->{credit_id}){
  168. @{$self->{locations}} = $self->exec_method(
  169. funcname => 'eca__list_locations');
  170. @{$self->{contacts}} = $self->exec_method(
  171. funcname => 'eca__list_contacts');
  172. @{$self->{notes}} = $self->exec_method(
  173. funcname => 'eca__list_notes');
  174. }
  175. else {
  176. @{$self->{locations}} = $self->exec_method(
  177. funcname => 'company__list_locations');
  178. @{$self->{contacts}} = $self->exec_method(
  179. funcname => 'company__list_contacts');
  180. @{$self->{notes}} = $self->exec_method(
  181. funcname => 'company__list_notes');
  182. }
  183. if ($self->{location_id}){
  184. for (@{$self->{locations}}){
  185. if ($_->{id} = $self->{location_id}){
  186. my $old_id = $self->{id};
  187. $self->merge($_);
  188. $self->{id} = $old_id;
  189. last;
  190. }
  191. }
  192. }
  193. if ($self->{contact_id}){
  194. for (@{$self->{contacts}}){
  195. if ($_->{id} = $self->{contact_id}){
  196. my $old_id = $self->{id};
  197. $self->merge($_);
  198. $self->{id} = $old_id;
  199. last;
  200. }
  201. }
  202. }
  203. @{$self->{bank_account}} = $self->exec_method(
  204. funcname => 'company__list_bank_account');
  205. };
  206. 1;