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