summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Payment.pm
blob: f13a3d42b5c3bcb47824b89767e51ab9af3764cb (plain)
  1. =head1 NAME
  2. LedgerSMB::Payment: Payment Handling Back-end Routines for LedgerSMB
  3. =head1 SYNOPSIS
  4. Provides the functions for generating the data structures payments made in
  5. LedgerSMB. This module currently handles only basic payment logic, and does
  6. handle overpayment logic, though these features will be moved into this module
  7. in the near future.
  8. =head1 COPYRIGHT
  9. Copyright (c) 2007 The LedgerSMB Core Team. Licensed under the GNU General
  10. Public License version 2 or at your option any later version. Please see the
  11. included COPYRIGHT and LICENSE files for more information.
  12. =cut
  13. package LedgerSMB::DBObject::Payment;
  14. use base qw(LedgerSMB::DBObject);
  15. use strict;
  16. use Math::BigFloat lib => 'GMP';
  17. our $VERSION = '0.1.0';
  18. =head1 METHODS
  19. =over
  20. =item LedgerSMB::DBObject::Payment->new()
  21. Inherited from LedgerSMB::DBObject. Please see that documnetation for details.
  22. =item $oayment->get_open_accounts()
  23. This function returns a list of open accounts depending on the
  24. $payment->{account_class} property. If this property is 1, it returns a list
  25. of vendor accounts, for 2, a list of customer accounts are returned.
  26. The returned list of hashrefs is stored in the $payment->{accounts} property.
  27. Each hashref has the following keys: id (entity id), name, and entity_class.
  28. An account is considered open if there are outstanding, unpaid invoices
  29. attached to it. Customer/vendor payment threshold is not considered for this
  30. calculation.
  31. =back
  32. =cut
  33. sub get_open_accounts {
  34. my ($self) = @_;
  35. @{$self->{accounts}} =
  36. $self->exec_method(funcname => 'payment_get_open_accounts');
  37. return @{$self->{accounts}};
  38. }
  39. =over
  40. =item $oayment->get_all_accounts()
  41. This function returns a list of open or closed accounts depending on the
  42. $payment->{account_class} property. If this property is 1, it returns a list
  43. of vendor accounts, for 2, a list of customer accounts are returned.
  44. The returned list of hashrefs is stored in the $payment->{accounts} property.
  45. Each hashref has the following keys: id (entity id), name, and entity_class.
  46. =back
  47. =cut
  48. sub get_all_accounts {
  49. my ($self) = @_;
  50. @{$self->{accounts}} =
  51. $self->exec_method(funcname => 'payment_get_all_accounts');
  52. return @{$self->{accounts}};
  53. }
  54. =over
  55. =item $oayment->get_open_invoices()
  56. This function returns a list of open invoices depending on the
  57. $payment->{account_class}, $payment->{entity_id}, and $payment->{currency}
  58. properties. Account classes follow the conventions above. This list is hence
  59. specific to a customer or vendor and currency as well.
  60. The returned list of hashrefs is stored in the $payment->{open_invoices}
  61. property. Each hashref has the following keys: id (entity id), name, and
  62. entity_class.
  63. =back
  64. =cut
  65. sub get_open_invoices {
  66. my ($self) = @_;
  67. @{$self->{open_invoices}} =
  68. $self->exec_method(funcname => 'payment_get_open_invoices');
  69. return @{$self->{open_invoices}};
  70. }
  71. =over
  72. =item $oayment->get_all_contact_invoices()
  73. This function returns a list of open accounts depending on the
  74. $payment->{account_class} property. If this property is 1, it returns a list
  75. of vendor accounts, for 2, a list of customer accounts are returned. Attached
  76. to each account is a list of open invoices. The data structure is somewhat
  77. complex.
  78. Each item in the list has the following keys: contact_id, contact_name, \
  79. account_number, total_due, and invoices.
  80. The invoices entry is a reference to an array of hashrefs. Each of these
  81. hashrefs has the following keys: invoice_id, invnumber, invoice_date, amount,
  82. discount, and due.
  83. These are filtered based on the (required) properties:
  84. $payment->{account_class}, $payment->{business_type}, $payment->{date_from},
  85. $payment->{date_to}, and $payment->{ar_ap_accno}.
  86. The $payment->{ar_ap_accno} property is used to filter out by AR or AP account.
  87. The following can also be optionally passed: $payment->{batch_id}. If this is
  88. patched, vouchers in the current batch will be picked up as well.
  89. The returned list of hashrefs is stored in the $payment->{contact} property.
  90. Each hashref has the following keys: id (entity id), name, and entity_class.
  91. =back
  92. =cut
  93. sub get_all_contact_invoices {
  94. my ($self) = @_;
  95. @{$self->{contacts}} =
  96. $self->exec_method(funcname => 'payment_get_all_contact_invoices');
  97. # When arrays of complex types are supported by all versions of Postgres
  98. # that this application supports, we should look at doing type conversions
  99. # in DBObject so this sort of logic is unncessary. -- CT
  100. for my $contact (@{$self->{contacts}}){
  101. my @invoices = $self->parse_array($contact->{invoices});
  102. my $processed_invoices = [];
  103. for my $invoice (@invoices){
  104. my $new_invoice = {};
  105. for (qw(invoice_id invnumber invoice_date amount discount due)){
  106. $new_invoice->{$_} = shift @$invoice;
  107. if ($_ =~ /^(amount|discount|due)$/){
  108. $new_invoice->{$_} =
  109. Math::BigFloat->new($new_invoice->{$_});
  110. }
  111. }
  112. push(@$processed_invoices, $new_invoice);
  113. }
  114. $contact->{invoice} = $processed_invoices;
  115. }
  116. return @{$self->{contacts}};
  117. }
  118. =over
  119. =item list_open_projects
  120. This method gets the current date attribute, and provides a list of open
  121. projects. The list is attached to $self->{projects} and returned.
  122. =back
  123. =cut
  124. sub list_open_projects {
  125. my ($self) = @_;
  126. my ($date) = $self->{dbh}->selectrow_array('select current_date');
  127. @{$self->{projects}} = $self->call_procedure(
  128. procname => 'project_list_open', args => [$date]
  129. );
  130. return @{$self->{projects}};
  131. }
  132. =over
  133. =item list_departments
  134. This method gets the type of document as a parameter, and provides a list of departments
  135. of the required type.
  136. The list is attached to $self->{departments} and returned.
  137. =back
  138. =cut
  139. sub list_departments {
  140. my ($self) = shift @_;
  141. my @args = @_;
  142. @{$self->{departments}} = $self->call_procedure(
  143. procname => 'department_list',
  144. args => \@args
  145. );
  146. return @{$self->{departments}};
  147. }
  148. =item list_open_vc
  149. This method gets the type of vc (vendor or customer) as a parameter, and provides a list of departments
  150. of the required type.
  151. The list is attached to $self->{departments} and returned.
  152. =back
  153. =cut
  154. sub list_departments {
  155. my ($self) = shift @_;
  156. my @args = @_;
  157. @{$self->{departments}} = $self->call_procedure(
  158. procname => 'department_list',
  159. args => \@args
  160. );
  161. return @{$self->{departments}};
  162. }
  163. =item get_open_currencies
  164. This method gets a list of the open currencies inside the database, it requires that
  165. $self->{account_class} (must be 1 or 2) exist to work.
  166. =back
  167. =cut
  168. sub get_open_currencies {
  169. my ($self) = shift @_;
  170. @{$self->{openCurrencies}} = $self->exec_method( funcname => 'payments_get_open_currencies');
  171. return @{$self->{openCurrencies}};
  172. }
  173. 1;