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