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