summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Company.pm
blob: 23a56cecedd1fa848ade80f594038027d42e5db5 (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. $self->exec_method(funcname => 'company__location_save');
  61. $self->{dbh}->commit;
  62. }
  63. =over
  64. =item get_credit_id
  65. This method returns the current credit id from the screen.
  66. =back
  67. =cut
  68. sub get_credit_id {
  69. my $self = shift @_;
  70. my ($ref) = $self->exec_method(
  71. funcname => 'entity_credit_get_id'
  72. );
  73. $self->{credit_id} = $ref->{'entity_credit_get_id'};
  74. }
  75. =over
  76. =item get_metadata()
  77. This retrieves various information vor building the user interface. Among other
  78. things, it sets the following properties:
  79. $self->{ar_ap_acc_list} = qw(list of ar or ap accounts)
  80. $self->{cash_acc_list} = qw(list of cash accounts)
  81. =back
  82. =cut
  83. sub get_metadata {
  84. my $self = shift @_;
  85. @{$self->{ar_ap_acc_list}} =
  86. $self->exec_method(funcname => 'chart_get_ar_ap');
  87. for my $ref (@{$self->{ar_ap_acc_list}}){
  88. $ref->{text} = "$ref->{accno}--$ref->{description}";
  89. }
  90. @{$self->{cash_acc_list}} =
  91. $self->exec_method(funcname => 'chart_list_cash');
  92. for my $ref (@{$self->{cash_acc_list}}){
  93. $ref->{text} = "$ref->{accno}--$ref->{description}";
  94. }
  95. @{$self->{location_class_list}} =
  96. $self->exec_method(funcname => 'location_list_class');
  97. @{$self->{business_types}} =
  98. $self->exec_method(funcname => 'business_type__list');
  99. @{$self->{country_list}} =
  100. $self->exec_method(funcname => 'location_list_country');
  101. @{$self->{contact_class_list}} =
  102. $self->exec_method(funcname => 'entity_list_contact_class');
  103. @{$self->{credit_list}} =
  104. $self->exec_method(funcname => 'entity__list_credit');
  105. }
  106. sub save_contact {
  107. my ($self) = @_;
  108. $self->exec_method(funcname => 'company__save_contact');
  109. $self->{dbh}->commit;
  110. }
  111. sub save_bank_account {
  112. my $self = shift @_;
  113. $self->exec_method(funcname => 'entity__save_bank_account');
  114. $self->{dbh}->commit;
  115. }
  116. sub save_notes {
  117. my $self = shift @_;
  118. $self->exec_method(funcname => 'entity__save_notes');
  119. $self->{dbh}->commit;
  120. }
  121. sub search {
  122. my ($self) = @_;
  123. @{$self->{search_results}} =
  124. $self->exec_method(funcname => 'company__search');
  125. return @{$self->{search_results}};
  126. }
  127. sub get_billing_info {
  128. my $self = shift @_;
  129. $self->set_entity_class();
  130. my ($ref) = $self->exec_method(funcname => 'company_get_billing_info');
  131. $self->merge($ref);
  132. }
  133. sub get {
  134. my $self = shift @_;
  135. $self->set_entity_class();
  136. my ($ref) = $self->exec_method(funcname => 'company_retrieve');
  137. $self->merge($ref);
  138. $self->{threshold} = $self->format_amount(amount => $self->{threshold});
  139. $self->{name} = $self->{legal_name};
  140. @{$self->{locations}} = $self->exec_method(
  141. funcname => 'company__list_locations');
  142. @{$self->{contacts}} = $self->exec_method(
  143. funcname => 'company__list_contacts');
  144. @{$self->{bank_account}} = $self->exec_method(
  145. funcname => 'company__list_bank_account');
  146. @{$self->{notes}} = $self->exec_method(
  147. funcname => 'company__list_notes');
  148. };
  149. 1;