summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Payment.pm
blob: 533a9cb858d333df5a56677d6327c1bd91139f55 (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 $payment->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 __validate__ {
  34. my ($self) = shift @_;
  35. #FIRST WE CHECK IF THE MAIN PROPERTY 'account_class' IS SET
  36. if (!$self->{account_class}) {
  37. $self->error("account_class must be set")
  38. };
  39. #NOW WE SET THE CURRENT DATE
  40. ($self->{current_date}) = $self->{dbh}->selectrow_array('select current_date');
  41. }
  42. sub get_open_accounts {
  43. my ($self) = @_;
  44. @{$self->{accounts}} =
  45. $self->exec_method(funcname => 'payment_get_open_accounts');
  46. return @{$self->{accounts}};
  47. }
  48. =over
  49. =item $payment->get_all_accounts()
  50. This function returns a list of open or closed accounts depending on the
  51. $payment->{account_class} property. If this property is 1, it returns a list
  52. of vendor accounts, for 2, a list of customer accounts are returned.
  53. The returned list of hashrefs is stored in the $payment->{accounts} property.
  54. Each hashref has the following keys: id (entity id), name, and entity_class.
  55. =back
  56. =cut
  57. sub get_all_accounts {
  58. my ($self) = @_;
  59. @{$self->{accounts}} =
  60. $self->exec_method(funcname => 'payment_get_all_accounts');
  61. return @{$self->{accounts}};
  62. }
  63. =over
  64. =item $payment->get_open_invoices()
  65. This function returns a list of open invoices depending on the
  66. $payment->{account_class}, $payment->{entity_id}, and $payment->{curr}
  67. properties. Account classes follow the conventions above. This list is hence
  68. specific to a customer or vendor and currency as well.
  69. The returned list of hashrefs is stored in the $payment->{open_invoices}
  70. property. Each hashref has the following keys: id (entity id), name, and
  71. entity_class.
  72. =back
  73. =cut
  74. sub get_open_invoices {
  75. my ($self) = @_;
  76. @{$self->{open_invoices}} =
  77. $self->exec_method(funcname => 'payment_get_open_invoices');
  78. return @{$self->{open_invoices}};
  79. }
  80. =over
  81. =item $oayment->get_all_contact_invoices()
  82. This function returns a list of open accounts depending on the
  83. $payment->{account_class} property. If this property is 1, it returns a list
  84. of vendor accounts, for 2, a list of customer accounts are returned. Attached
  85. to each account is a list of open invoices. The data structure is somewhat
  86. complex.
  87. Each item in the list has the following keys: contact_id, contact_name, \
  88. account_number, total_due, and invoices.
  89. The invoices entry is a reference to an array of hashrefs. Each of these
  90. hashrefs has the following keys: invoice_id, invnumber, invoice_date, amount,
  91. discount, and due.
  92. These are filtered based on the (required) properties:
  93. $payment->{account_class}, $payment->{business_type}, $payment->{date_from},
  94. $payment->{date_to}, and $payment->{ar_ap_accno}.
  95. The $payment->{ar_ap_accno} property is used to filter out by AR or AP account.
  96. The following can also be optionally passed: $payment->{batch_id}. If this is
  97. patched, vouchers in the current batch will be picked up as well.
  98. The returned list of hashrefs is stored in the $payment->{contact} property.
  99. Each hashref has the following keys: id (entity id), name, and entity_class.
  100. =back
  101. =cut
  102. sub get_all_contact_invoices {
  103. my ($self) = @_;
  104. @{$self->{contacts}} =
  105. $self->exec_method(funcname => 'payment_get_all_contact_invoices');
  106. # When arrays of complex types are supported by all versions of Postgres
  107. # that this application supports, we should look at doing type conversions
  108. # in DBObject so this sort of logic is unncessary. -- CT
  109. for my $contact (@{$self->{contacts}}){
  110. my @invoices = $self->parse_array($contact->{invoices});
  111. my $processed_invoices = [];
  112. for my $invoice (@invoices){
  113. my $new_invoice = {};
  114. for (qw(invoice_id invnumber invoice_date amount discount due)){
  115. $new_invoice->{$_} = shift @$invoice;
  116. if ($_ =~ /^(amount|discount|due)$/){
  117. $new_invoice->{$_} =
  118. Math::BigFloat->new($new_invoice->{$_});
  119. }
  120. }
  121. push(@$processed_invoices, $new_invoice);
  122. }
  123. $contact->{invoice} = $processed_invoices;
  124. }
  125. return @{$self->{contacts}};
  126. }
  127. =over
  128. =item list_open_projects
  129. This method gets the current date attribute, and provides a list of open
  130. projects. The list is attached to $self->{projects} and returned.
  131. =back
  132. =cut
  133. sub list_open_projects {
  134. my ($self) = @_;
  135. @{$self->{projects}} = $self->call_procedure(
  136. procname => 'project_list_open', args => [$self->{current_date}]
  137. );
  138. return @{$self->{projects}};
  139. }
  140. =over
  141. =item list_departments
  142. This method gets the type of document as a parameter, and provides a list of departments
  143. of the required type.
  144. The list is attached to $self->{departments} and returned.
  145. =back
  146. =cut
  147. sub list_departments {
  148. my ($self) = shift @_;
  149. my @args = @_;
  150. @{$self->{departments}} = $self->call_procedure(
  151. procname => 'department_list',
  152. args => \@args
  153. );
  154. return @{$self->{departments}};
  155. }
  156. =item list_open_vc
  157. This method gets the type of vc (vendor or customer) as a parameter, and provides a list of departments
  158. of the required type.
  159. The list is attached to $self->{departments} and returned.
  160. =back
  161. =cut
  162. sub list_departments {
  163. my ($self) = shift @_;
  164. my @args = @_;
  165. @{$self->{departments}} = $self->call_procedure(
  166. procname => 'department_list',
  167. args => \@args
  168. );
  169. return @{$self->{departments}};
  170. }
  171. =item get_open_currencies
  172. This method gets a list of the open currencies inside the database, it requires that
  173. $self->{account_class} (must be 1 or 2) exist to work.
  174. =back
  175. =cut
  176. sub get_open_currencies {
  177. my ($self) = shift @_;
  178. @{$self->{openCurrencies}} = $self->exec_method( funcname => 'payments_get_open_currencies');
  179. return @{$self->{openCurrencies}};
  180. }
  181. =item list_accounting
  182. This method lists all accounts that match the role specified in account_class property and
  183. are availible to store the payment or receipts.
  184. =back
  185. =cut
  186. sub list_accounting {
  187. my ($self) = @_;
  188. @{$self->{pay_accounts}} = $self->exec_method( funcname => 'chart_list_cash');
  189. return @{$self->{pay_accounts}};
  190. }
  191. =item get_sources
  192. This method builds all the possible sources of money,
  193. in the future it will look inside the DB.
  194. =back
  195. =cut
  196. sub get_sources {
  197. my ($self, $locale) = @_;
  198. @{$self->{cash_sources}} = ($locale->text('cash'),
  199. $locale->text('check'),
  200. $locale->text('deposit'),
  201. $locale->text('other'));
  202. return @{$self->{cash_sources}};
  203. }
  204. =item get_exchange_rate(currency, date)
  205. This method gets the exchange rate for the specified currency and date
  206. =cut
  207. sub get_exchange_rate {
  208. my ($self) = shift @_;
  209. ($self->{currency}, $self->{date}) = @_;
  210. ($self->{exchangerate}) = $self->exec_method(funcname => 'currency_get_exchangerate');
  211. return $self->{exchangerate}->{currency_get_exchangerate};
  212. }
  213. =item get_default_currency
  214. This method gets the default currency
  215. =back
  216. =cut
  217. sub get_default_currency {
  218. my ($self) = shift @_;
  219. ($self->{default_currency}) = $self->call_procedure(procname => 'defaults_get_defaultcurrency');
  220. return $self->{default_currency}->{defaults_get_defaultcurrency};
  221. }
  222. =item get_current_date
  223. This method returns the system's current date
  224. =cut
  225. sub get_current_date {
  226. my ($self) = shift @_;
  227. return $self->{current_date};
  228. }
  229. =item get_vc_info
  230. This method returns the contact informatino for a customer or vendor according to
  231. $self->{account_class}
  232. =cut
  233. sub get_vc_info {
  234. my ($self) = @_;
  235. #@{$self->{vendor_customer_info}} = $self->call_procedure(procname => 'vendor_customer_info');
  236. #return @{$self->{vendor_customer_info}};
  237. }
  238. 1;