summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Company.pm
blob: 4d11d858a2fed9e6d5bfe82eb938349ba8390696 (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->{entity_classes}} =
  93. $self->exec_method(funcname => 'entity__list_classes');
  94. @{$self->{ar_ap_acc_list}} =
  95. $self->exec_method(funcname => 'chart_get_ar_ap');
  96. for my $ref (@{$self->{ar_ap_acc_list}}){
  97. $ref->{text} = "$ref->{accno}--$ref->{description}";
  98. }
  99. @{$self->{cash_acc_list}} =
  100. $self->exec_method(funcname => 'chart_list_cash');
  101. for my $ref (@{$self->{cash_acc_list}}){
  102. $ref->{text} = "$ref->{accno}--$ref->{description}";
  103. }
  104. @{$self->{location_class_list}} =
  105. $self->exec_method(funcname => 'location_list_class');
  106. @{$self->{business_types}} =
  107. $self->exec_method(funcname => 'business_type__list');
  108. @{$self->{country_list}} =
  109. $self->exec_method(funcname => 'location_list_country');
  110. @{$self->{contact_class_list}} =
  111. $self->exec_method(funcname => 'entity_list_contact_class');
  112. }
  113. sub save_contact {
  114. my ($self) = @_;
  115. if ($self->{credit_id}){
  116. $self->exec_method(funcname => 'eca__save_contact');
  117. } else {
  118. $self->exec_method(funcname => 'company__save_contact');
  119. }
  120. $self->{dbh}->commit;
  121. }
  122. sub save_bank_account {
  123. my $self = shift @_;
  124. $self->exec_method(funcname => 'entity__save_bank_account');
  125. $self->{dbh}->commit;
  126. }
  127. sub save_notes {
  128. my $self = shift @_;
  129. if ($self->{credit_id} && $self->{note_class} eq '3'){
  130. $self->exec_method(funcname => 'eca__save_notes');
  131. } else {
  132. $self->exec_method(funcname => 'entity__save_notes');
  133. }
  134. $self->{dbh}->commit;
  135. }
  136. sub search {
  137. my ($self) = @_;
  138. @{$self->{search_results}} =
  139. $self->exec_method(funcname => 'company__search');
  140. return @{$self->{search_results}};
  141. }
  142. sub get_billing_info {
  143. my $self = shift @_;
  144. $self->set_entity_class();
  145. my ($ref) = $self->exec_method(funcname => 'company_get_billing_info');
  146. $self->merge($ref);
  147. }
  148. sub get {
  149. my $self = shift @_;
  150. $self->set_entity_class();
  151. my ($ref) = $self->exec_method(funcname => 'company_retrieve');
  152. $self->merge($ref);
  153. $self->{threshold} = $self->format_amount(amount => $self->{threshold});
  154. @{$self->{credit_list}} =
  155. $self->exec_method(funcname => 'entity__list_credit');
  156. for (@{$self->{credit_list}}){
  157. print STDERR "credit_id: $_->{credit_id}\n";
  158. if (($_->{credit_id} eq $self->{credit_id})
  159. or ($_->{meta_number} eq $self->{meta_number})
  160. or ($_->{id} eq $self->{credit_id})){
  161. $self->merge($_);
  162. last;
  163. }
  164. }
  165. $self->{name} = $self->{legal_name};
  166. if ($self->{credit_id} and $self->{meta_number}){
  167. $self->get_credit_id;
  168. }
  169. if ($self->{credit_id}){
  170. @{$self->{locations}} = $self->exec_method(
  171. funcname => 'eca__list_locations');
  172. @{$self->{contacts}} = $self->exec_method(
  173. funcname => 'eca__list_contacts');
  174. @{$self->{notes}} = $self->exec_method(
  175. funcname => 'eca__list_notes');
  176. }
  177. else {
  178. @{$self->{locations}} = $self->exec_method(
  179. funcname => 'company__list_locations');
  180. @{$self->{contacts}} = $self->exec_method(
  181. funcname => 'company__list_contacts');
  182. @{$self->{notes}} = $self->exec_method(
  183. funcname => 'company__list_notes');
  184. }
  185. if ($self->{location_id}){
  186. for (@{$self->{locations}}){
  187. if ($_->{id} = $self->{location_id}){
  188. my $old_id = $self->{id};
  189. $self->merge($_);
  190. $self->{id} = $old_id;
  191. last;
  192. }
  193. }
  194. }
  195. if ($self->{contact_id}){
  196. for (@{$self->{contacts}}){
  197. if ($_->{id} = $self->{contact_id}){
  198. my $old_id = $self->{id};
  199. $self->merge($_);
  200. $self->{id} = $old_id;
  201. last;
  202. }
  203. }
  204. }
  205. @{$self->{bank_account}} = $self->exec_method(
  206. funcname => 'company__list_bank_account');
  207. };
  208. 1;