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