summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Company.pm
blob: b2eed72c182fbb65894a4f96349bc783b55a7db7 (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. $self->exec_method(funcname => 'entity_credit_save');
  49. $self->{threshold} = $self->format_amount(amount => $self->{threshold});
  50. $self->{dbh}->commit;
  51. }
  52. =over
  53. =item save_location
  54. This method saves an address for a company.
  55. =back
  56. =cut
  57. sub save_location {
  58. my $self = shift @_;
  59. $self->{country_id} = $self->{country_code};
  60. if($self->{credit_id}){
  61. $self->exec_method(funcname => 'eca__location_save');
  62. } else {
  63. my ($ref) = $self->exec_method(funcname => 'company__location_save');
  64. my @vals = values %$ref;
  65. $self->{location_id} = $vals[0];
  66. }
  67. $self->{dbh}->commit;
  68. }
  69. =over
  70. =item get_credit_id
  71. This method returns the current credit id from the screen.
  72. =back
  73. =cut
  74. sub get_credit_id {
  75. my $self = shift @_;
  76. my ($ref) = $self->exec_method(
  77. funcname => 'entity_credit_get_id'
  78. );
  79. $self->{credit_id} = $ref->{'entity_credit_get_id'};
  80. }
  81. =over
  82. =item get_metadata()
  83. This retrieves various information vor building the user interface. Among other
  84. things, it sets the following properties:
  85. $self->{ar_ap_acc_list} = qw(list of ar or ap accounts)
  86. $self->{cash_acc_list} = qw(list of cash accounts)
  87. =back
  88. =cut
  89. sub get_metadata {
  90. my $self = shift @_;
  91. @{$self->{ar_ap_acc_list}} =
  92. $self->exec_method(funcname => 'chart_get_ar_ap');
  93. for my $ref (@{$self->{ar_ap_acc_list}}){
  94. $ref->{text} = "$ref->{accno}--$ref->{description}";
  95. }
  96. @{$self->{cash_acc_list}} =
  97. $self->exec_method(funcname => 'chart_list_cash');
  98. for my $ref (@{$self->{cash_acc_list}}){
  99. $ref->{text} = "$ref->{accno}--$ref->{description}";
  100. }
  101. @{$self->{location_class_list}} =
  102. $self->exec_method(funcname => 'location_list_class');
  103. @{$self->{business_types}} =
  104. $self->exec_method(funcname => 'business_type__list');
  105. @{$self->{country_list}} =
  106. $self->exec_method(funcname => 'location_list_country');
  107. @{$self->{contact_class_list}} =
  108. $self->exec_method(funcname => 'entity_list_contact_class');
  109. }
  110. sub save_contact {
  111. my ($self) = @_;
  112. if ($self->{credit_id}){
  113. $self->exec_method(funcname => 'eca__save_contact');
  114. } else {
  115. $self->exec_method(funcname => 'company__save_contact');
  116. }
  117. $self->{dbh}->commit;
  118. }
  119. sub save_bank_account {
  120. my $self = shift @_;
  121. $self->exec_method(funcname => 'entity__save_bank_account');
  122. $self->{dbh}->commit;
  123. }
  124. sub save_notes {
  125. my $self = shift @_;
  126. if ($self->{credit_id} && $self->{note_class} eq '3'){
  127. $self->exec_method(funcname => 'eca__save_notes');
  128. } else {
  129. $self->exec_method(funcname => 'entity__save_notes');
  130. }
  131. $self->{dbh}->commit;
  132. }
  133. sub search {
  134. my ($self) = @_;
  135. @{$self->{search_results}} =
  136. $self->exec_method(funcname => 'company__search');
  137. return @{$self->{search_results}};
  138. }
  139. sub get_billing_info {
  140. my $self = shift @_;
  141. $self->set_entity_class();
  142. my ($ref) = $self->exec_method(funcname => 'company_get_billing_info');
  143. $self->merge($ref);
  144. }
  145. sub get {
  146. my $self = shift @_;
  147. $self->set_entity_class();
  148. my ($ref) = $self->exec_method(funcname => 'company_retrieve');
  149. $self->merge($ref);
  150. $self->{threshold} = $self->format_amount(amount => $self->{threshold});
  151. @{$self->{credit_list}} =
  152. $self->exec_method(funcname => 'entity__list_credit');
  153. for (@{$self->{credit_list}}){
  154. print STDERR "credit_id: $_->{credit_id}\n";
  155. if (($_->{credit_id} eq $self->{credit_id})
  156. or ($_->{meta_number} eq $self->{meta_number})){
  157. $self->merge($_);
  158. last;
  159. }
  160. }
  161. $self->{name} = $self->{legal_name};
  162. if ($self->{credit_id} and $self->{meta_number}){
  163. $self->get_credit_id;
  164. }
  165. if ($self->{credit_id}){
  166. @{$self->{locations}} = $self->exec_method(
  167. funcname => 'eca__list_locations');
  168. @{$self->{contacts}} = $self->exec_method(
  169. funcname => 'eca__list_contacts');
  170. @{$self->{notes}} = $self->exec_method(
  171. funcname => 'eca__list_notes');
  172. }
  173. else {
  174. @{$self->{locations}} = $self->exec_method(
  175. funcname => 'company__list_locations');
  176. @{$self->{contacts}} = $self->exec_method(
  177. funcname => 'company__list_contacts');
  178. @{$self->{notes}} = $self->exec_method(
  179. funcname => 'company__list_notes');
  180. }
  181. if ($self->{location_id}){
  182. for (@{$self->{locations}}){
  183. if ($_->{id} = $self->{location_id}){
  184. my $old_id = $self->{id};
  185. $self->merge($_);
  186. $self->{id} = $old_id;
  187. last;
  188. }
  189. }
  190. }
  191. if ($self->{contact_id}){
  192. for (@{$self->{contacts}}){
  193. if ($_->{id} = $self->{contact_id}){
  194. my $old_id = $self->{id};
  195. $self->merge($_);
  196. $self->{id} = $old_id;
  197. last;
  198. }
  199. }
  200. }
  201. @{$self->{bank_account}} = $self->exec_method(
  202. funcname => 'company__list_bank_account');
  203. };
  204. 1;