summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Payment.pm
blob: bf50719067f9aef8df21b4050ff0fa25083a2ecb (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->{currencies} = [];
  50. for my $c (@{$self->{openCurrencies}}){
  51. push @{$self->{currencies}}, $c->{payments_get_open_currencies};
  52. }
  53. @{$self->{businesses}} = $self->exec_method(
  54. funcname => 'business_type__list'
  55. );
  56. @{$self->{debt_accounts}} = $self->exec_method(
  57. funcname => 'chart_get_ar_ap');
  58. @{$self->{cash_accounts}} = $self->exec_method(
  59. funcname => 'chart_list_cash');
  60. for my $ref(@{$self->{cash_accounts}}){
  61. $ref->{text} = "$ref->{accno}--$ref->{description}";
  62. }
  63. }
  64. sub search {
  65. my ($self) = @_;
  66. if ($self->{meta_number} && !$self->{credit_id}){
  67. my ($ref) = $self->exec_method(
  68. funcname => 'entity_credit_get_id_by_meta_number'
  69. );
  70. my @keys = keys %$ref;
  71. my $key = shift @keys;
  72. $self->{credit_id} = $ref->{$key};
  73. }
  74. @{$self->{search_results}} = $self->exec_method(
  75. funcname => 'payment__search'
  76. );
  77. return @{$self->{search_results}};
  78. }
  79. sub get_open_accounts {
  80. my ($self) = @_;
  81. @{$self->{accounts}} =
  82. $self->exec_method(funcname => 'payment_get_open_accounts');
  83. return @{$self->{accounts}};
  84. }
  85. =over
  86. =item $payment->get_all_accounts()
  87. This function returns a list of open or closed accounts depending on the
  88. $payment->{account_class} property. If this property is 1, it returns a list
  89. of vendor accounts, for 2, a list of customer accounts are returned.
  90. The returned list of hashrefs is stored in the $payment->{accounts} property.
  91. Each hashref has the following keys: id (entity id), name, and entity_class.
  92. =back
  93. =cut
  94. sub get_all_accounts {
  95. my ($self) = @_;
  96. @{$self->{accounts}} =
  97. $self->exec_method(funcname => 'payment_get_all_accounts');
  98. return @{$self->{accounts}};
  99. }
  100. =over
  101. =item $payment->reverse()
  102. This function reverses a payment. A payment is defined as one source
  103. ($payment->{source}) to one cash account ($payment->{cash_accno}) to one date
  104. ($payment->{date_paid}) to one vendor/customer ($payment->{credit_id},
  105. $payment->{account_class}). This reverses the entries with that source.
  106. =back
  107. =cut
  108. sub reverse {
  109. my ($self) = @_;
  110. $self->exec_method(funcname => 'payment__reverse');
  111. return $self->{dbh}->commit;
  112. }
  113. =over
  114. =item $payment->get_open_invoices()
  115. This function returns a list of open invoices depending on the
  116. $payment->{account_class}, $payment->{entity_id}, and $payment->{curr}
  117. properties. Account classes follow the conventions above. This list is hence
  118. specific to a customer or vendor and currency as well.
  119. The returned list of hashrefs is stored in the $payment->{open_invoices}
  120. property. Each hashref has the following keys: id (entity id), name, and
  121. entity_class.
  122. =back
  123. =cut
  124. sub get_open_invoices {
  125. my ($self) = @_;
  126. @{$self->{open_invoices}} =
  127. $self->exec_method(funcname => 'payment_get_open_invoices');
  128. return @{$self->{open_invoices}};
  129. }
  130. =over
  131. =item $payment->get_all_contact_invoices()
  132. This function returns a list of open accounts depending on the
  133. $payment->{account_class} property. If this property is 1, it returns a list
  134. of vendor accounts, for 2, a list of customer accounts are returned. Attached
  135. to each account is a list of open invoices. The data structure is somewhat
  136. complex.
  137. Each item in the list has the following keys: contact_id, contact_name, \
  138. account_number, total_due, and invoices.
  139. The invoices entry is a reference to an array of hashrefs. Each of these
  140. hashrefs has the following keys: invoice_id, invnumber, invoice_date, amount,
  141. discount, and due.
  142. These are filtered based on the (required) properties:
  143. $payment->{account_class}, $payment->{business_type}, $payment->{date_from},
  144. $payment->{date_to}, and $payment->{ar_ap_accno}.
  145. The $payment->{ar_ap_accno} property is used to filter out by AR or AP account.
  146. The following can also be optionally passed: $payment->{batch_id}. If this is
  147. patched, vouchers in the current batch will be picked up as well.
  148. The returned list of hashrefs is stored in the $payment->{contact} property.
  149. Each hashref has the following keys: id (entity id), name, and entity_class.
  150. =back
  151. =cut
  152. sub get_all_contact_invoices {
  153. my ($self) = @_;
  154. @{$self->{contacts}} =
  155. $self->exec_method(funcname => 'payment_get_all_contact_invoices');
  156. # When arrays of complex types are supported by all versions of Postgres
  157. # that this application supports, we should look at doing type conversions
  158. # in DBObject so this sort of logic is unncessary. -- CT
  159. for my $contact (@{$self->{contacts}}){
  160. my @invoices = $self->parse_array($contact->{invoices});
  161. my $processed_invoices = [];
  162. for my $invoice (@invoices){
  163. my $new_invoice = {};
  164. for (qw(invoice_id invnumber invoice_date amount discount due)){
  165. $new_invoice->{$_} = shift @$invoice;
  166. if ($_ =~ /^(amount|discount|due)$/){
  167. $new_invoice->{$_} =
  168. Math::BigFloat->new($new_invoice->{$_});
  169. }
  170. }
  171. push(@$processed_invoices, $new_invoice);
  172. }
  173. $contact->{invoice} = $processed_invoices;
  174. }
  175. return @{$self->{contacts}};
  176. }
  177. =over
  178. =item list_open_projects
  179. This method gets the current date attribute, and provides a list of open
  180. projects. The list is attached to $self->{projects} and returned.
  181. =back
  182. =cut
  183. sub list_open_projects {
  184. my ($self) = @_;
  185. @{$self->{projects}} = $self->call_procedure(
  186. procname => 'project_list_open', args => [$self->{current_date}]
  187. );
  188. return @{$self->{projects}};
  189. }
  190. =over
  191. =item list_departments
  192. This method gets the type of document as a parameter, and provides a list of departments
  193. of the required type.
  194. The list is attached to $self->{departments} and returned.
  195. =back
  196. =cut
  197. sub list_departments {
  198. my ($self) = shift @_;
  199. my @args = @_;
  200. @{$self->{departments}} = $self->call_procedure(
  201. procname => 'department_list',
  202. args => \@args
  203. );
  204. return @{$self->{departments}};
  205. }
  206. =item list_open_vc
  207. This method gets the type of vc (vendor or customer) as a parameter, and provides a list of departments
  208. of the required type.
  209. The list is attached to $self->{departments} and returned.
  210. =back
  211. =cut
  212. sub list_departments {
  213. my ($self) = shift @_;
  214. my @args = @_;
  215. @{$self->{departments}} = $self->call_procedure(
  216. procname => 'department_list',
  217. args => \@args
  218. );
  219. return @{$self->{departments}};
  220. }
  221. =item get_open_currencies
  222. This method gets a list of the open currencies inside the database, it requires that
  223. $self->{account_class} (must be 1 or 2) exist to work.
  224. =back
  225. =cut
  226. sub get_open_currencies {
  227. my ($self) = shift @_;
  228. @{$self->{openCurrencies}} = $self->exec_method( funcname => 'payments_get_open_currencies');
  229. return @{$self->{openCurrencies}};
  230. }
  231. =item list_accounting
  232. This method lists all accounts that match the role specified in account_class property and
  233. are availible to store the payment or receipts.
  234. =back
  235. =cut
  236. sub list_accounting {
  237. my ($self) = @_;
  238. @{$self->{pay_accounts}} = $self->exec_method( funcname => 'chart_list_cash');
  239. return @{$self->{pay_accounts}};
  240. }
  241. =item get_sources
  242. This method builds all the possible sources of money,
  243. in the future it will look inside the DB.
  244. =back
  245. =cut
  246. sub get_sources {
  247. my ($self, $locale) = @_;
  248. @{$self->{cash_sources}} = ($locale->text('cash'),
  249. $locale->text('check'),
  250. $locale->text('deposit'),
  251. $locale->text('other'));
  252. return @{$self->{cash_sources}};
  253. }
  254. =item get_exchange_rate(currency, date)
  255. This method gets the exchange rate for the specified currency and date
  256. =cut
  257. sub get_exchange_rate {
  258. my ($self) = shift @_;
  259. ($self->{currency}, $self->{date}) = @_;
  260. ($self->{exchangerate}) = $self->exec_method(funcname => 'currency_get_exchangerate');
  261. return $self->{exchangerate}->{currency_get_exchangerate};
  262. }
  263. =item get_default_currency
  264. This method gets the default currency
  265. =back
  266. =cut
  267. sub get_default_currency {
  268. my ($self) = shift @_;
  269. ($self->{default_currency}) = $self->call_procedure(procname => 'defaults_get_defaultcurrency');
  270. return $self->{default_currency}->{defaults_get_defaultcurrency};
  271. }
  272. =item get_current_date
  273. This method returns the system's current date
  274. =cut
  275. sub get_current_date {
  276. my ($self) = shift @_;
  277. return $self->{current_date};
  278. }
  279. =item get_vc_info
  280. This method returns the contact informatino for a customer or vendor according to
  281. $self->{account_class}
  282. =cut
  283. sub get_vc_info {
  284. my ($self) = @_;
  285. #@{$self->{vendor_customer_info}} = $self->call_procedure(procname => 'vendor_customer_info');
  286. #return @{$self->{vendor_customer_info}};
  287. }
  288. =item get_payment_detail_data
  289. This method sets appropriate project, department, etc. fields.
  290. =cut
  291. sub get_payment_detail_data {
  292. my ($self) = @_;
  293. $self->get_metadata();
  294. my $source_inc;
  295. my $source_src;
  296. if (defined ($self->{source_start})){
  297. $self->{source_start} =~ /(\d*)\D*$/;
  298. $source_src = $1;
  299. if ($source_src) {
  300. $source_inc = $source_src;
  301. } else {
  302. $source_inc = 0;
  303. }
  304. }
  305. my $source_length = length($source_inc);
  306. @{$self->{contact_invoices}} = $self->exec_method(
  307. funcname => 'payment_get_all_contact_invoices');
  308. for my $inv (@{$self->{contact_invoices}}){
  309. if (defined $self->{source_start}){
  310. my $source = $self->{source_start};
  311. if (length($source_inc) < $source_length){
  312. $source_inc = sprintf('%0*s', $source_length, $source_inc);
  313. }
  314. $source =~ s/$source_src(\D*)$/$source_inc$1/;
  315. ++ $source_inc;
  316. $inv->{source} = $source;
  317. }
  318. my $tmp_invoices = $inv->{invoices};
  319. $inv->{invoices} = [];
  320. @{$inv->{invoices}} = $self->_parse_array($tmp_invoices);
  321. }
  322. $self->{dbh}->commit; # Commit locks
  323. }
  324. sub post_bulk {
  325. my ($self) = @_;
  326. my $total_count = 0;
  327. my ($ref) = $self->call_procedure(
  328. procname => 'setting_get',
  329. args => ['queue_payments'],
  330. );
  331. my $queue_payments = $ref->{setting_get};
  332. if ($queue_payments){
  333. my ($job_ref) = $self->exec_method(
  334. funcname => 'job__create'
  335. );
  336. $self->{job_id} = $job_ref->{job__create};
  337. ($self->{job}) = $self->exec_method(
  338. funcname => 'job__status'
  339. );
  340. }
  341. $self->{payment_date} = $self->{datepaid};
  342. for my $contact_row (1 .. $self->{contact_count}){
  343. my $contact_id = $self->{"contact_$contact_row"};
  344. next if (!$self->{"id_$contact_id"});
  345. my $invoice_array = "{}"; # Pg Array
  346. for my $invoice_row (1 .. $self->{"invoice_count_$contact_id"}){
  347. my $invoice_id = $self->{"invoice_${contact_id}_${invoice_row}"};
  348. my $pay_amount = ($self->{"paid_$contact_id"} eq 'all' )
  349. ? $self->{"net_$invoice_id"}
  350. : $self->{"payment_$invoice_id"};
  351. next if ! $pay_amount;
  352. $pay_amount = $pay_amount * 1;
  353. my $invoice_subarray = "{$invoice_id,$pay_amount}";
  354. if ($invoice_subarray !~ /^\{\d+\,\-?\d*\.?\d+\}$/){
  355. $self->error("Invalid subarray: $invoice_subarray");
  356. }
  357. $invoice_subarray =~ s/[^0123456789{},.]//;
  358. if ($invoice_array eq '{}'){ # Omit comma
  359. $invoice_array = "{$invoice_subarray}";
  360. } else {
  361. $invoice_array =~ s/\}$/,$invoice_subarray\}/;
  362. }
  363. }
  364. $self->{transactions} = $invoice_array;
  365. $self->{source} = $self->{"source_$contact_id"};
  366. if ($queue_payments){
  367. $self->{batch_class} = 3;
  368. $self->exec_method(
  369. funcname => 'payment_bulk_queue'
  370. );
  371. } else {
  372. $self->exec_method(funcname => 'payment_bulk_post');
  373. }
  374. }
  375. $self->{queue_payments} = $queue_payments;
  376. return $self->{dbh}->commit;
  377. }
  378. sub check_job {
  379. my ($self) = @_;
  380. ($self->{job}) = $self->exec_method(funcname => 'job__status');
  381. }
  382. 1;