summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Payment.pm
blob: 11e5ebfd9ac1c96ece4a3bbf4b82016387d324f0 (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 text_amount {
  45. use LedgerSMB::Num2text;
  46. print STDERR "num2text\n";
  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} = $processed_invoices;
  192. }
  193. return @{$self->{contacts}};
  194. }
  195. =over
  196. =item list_open_projects
  197. This method gets the current date attribute, and provides a list of open
  198. projects. The list is attached to $self->{projects} and returned.
  199. =back
  200. =cut
  201. sub list_open_projects {
  202. my ($self) = @_;
  203. @{$self->{projects}} = $self->call_procedure(
  204. procname => 'project_list_open', args => [$self->{current_date}]
  205. );
  206. return @{$self->{projects}};
  207. }
  208. =over
  209. =item list_departments
  210. This method gets the type of document as a parameter, and provides a list of departments
  211. of the required type.
  212. The list is attached to $self->{departments} and returned.
  213. =back
  214. =cut
  215. sub list_departments {
  216. my ($self) = shift @_;
  217. my @args = @_;
  218. @{$self->{departments}} = $self->call_procedure(
  219. procname => 'department_list',
  220. args => \@args
  221. );
  222. return @{$self->{departments}};
  223. }
  224. =item list_open_vc
  225. This method gets the type of vc (vendor or customer) as a parameter, and provides a list of departments
  226. of the required type.
  227. The list is attached to $self->{departments} and returned.
  228. =back
  229. =cut
  230. sub list_departments {
  231. my ($self) = shift @_;
  232. my @args = @_;
  233. @{$self->{departments}} = $self->call_procedure(
  234. procname => 'department_list',
  235. args => \@args
  236. );
  237. return @{$self->{departments}};
  238. }
  239. =item get_open_currencies
  240. This method gets a list of the open currencies inside the database, it requires that
  241. $self->{account_class} (must be 1 or 2) exist to work.
  242. =back
  243. =cut
  244. sub get_open_currencies {
  245. my ($self) = shift @_;
  246. @{$self->{openCurrencies}} = $self->exec_method( funcname => 'payments_get_open_currencies');
  247. return @{$self->{openCurrencies}};
  248. }
  249. =item list_accounting
  250. This method lists all accounts that match the role specified in account_class property and
  251. are availible to store the payment or receipts.
  252. =back
  253. =cut
  254. sub list_accounting {
  255. my ($self) = @_;
  256. @{$self->{pay_accounts}} = $self->exec_method( funcname => 'chart_list_cash');
  257. return @{$self->{pay_accounts}};
  258. }
  259. =item list_overpayment_accounting
  260. This method lists all accounts that match the role specified in account_class property and
  261. are availible to store an overpayment / advanced payment / pre-payment.
  262. =back
  263. =cut
  264. sub list_overpayment_accounting {
  265. my ($self) = @_;
  266. @{$self->{overpayment_accounts}} = $self->exec_method( funcname => 'chart_list_overpayment');
  267. return @{$self->{overpayment_accounts}};
  268. }
  269. =item get_sources
  270. This method builds all the possible sources of money,
  271. in the future it will look inside the DB.
  272. =back
  273. =cut
  274. sub get_sources {
  275. my ($self, $locale) = @_;
  276. @{$self->{cash_sources}} = ($locale->text('cash'),
  277. $locale->text('check'),
  278. $locale->text('deposit'),
  279. $locale->text('other'));
  280. return @{$self->{cash_sources}};
  281. }
  282. =item get_exchange_rate(currency, date)
  283. This method gets the exchange rate for the specified currency and date
  284. =cut
  285. sub get_exchange_rate {
  286. my ($self) = shift @_;
  287. ($self->{currency}, $self->{date}) = @_;
  288. ($self->{exchangerate}) = $self->exec_method(funcname => 'currency_get_exchangerate');
  289. return $self->{exchangerate}->{currency_get_exchangerate};
  290. }
  291. =item get_default_currency
  292. This method gets the default currency
  293. =back
  294. =cut
  295. sub get_default_currency {
  296. my ($self) = shift @_;
  297. ($self->{default_currency}) = $self->call_procedure(procname => 'defaults_get_defaultcurrency');
  298. return $self->{default_currency}->{defaults_get_defaultcurrency};
  299. }
  300. =item get_current_date
  301. This method returns the system's current date
  302. =cut
  303. sub get_current_date {
  304. my ($self) = shift @_;
  305. return $self->{current_date};
  306. }
  307. =item get_vc_info
  308. This method returns the contact informatino for a customer or vendor according to
  309. $self->{account_class}
  310. =cut
  311. sub get_vc_info {
  312. my ($self) = @_;
  313. @{$self->{vendor_customer_info}} = $self->exec_method(funcname => 'payment_get_vc_info');
  314. return @{$self->{vendor_customer_info}};
  315. }
  316. =item get_payment_detail_data
  317. This method sets appropriate project, department, etc. fields.
  318. =cut
  319. sub get_payment_detail_data {
  320. my ($self) = @_;
  321. $self->get_metadata();
  322. my $source_inc;
  323. my $source_src;
  324. if (defined ($self->{source_start})){
  325. $self->{source_start} =~ /(\d*)\D*$/;
  326. $source_src = $1;
  327. if ($source_src) {
  328. $source_inc = $source_src;
  329. } else {
  330. $source_inc = 0;
  331. }
  332. }
  333. my $source_length = length($source_inc);
  334. @{$self->{contact_invoices}} = $self->exec_method(
  335. funcname => 'payment_get_all_contact_invoices');
  336. for my $inv (@{$self->{contact_invoices}}){
  337. if (defined $self->{source_start}){
  338. my $source = $self->{source_start};
  339. if (length($source_inc) < $source_length){
  340. $source_inc = sprintf('%0*s', $source_length, $source_inc);
  341. }
  342. $source =~ s/$source_src(\D*)$/$source_inc$1/;
  343. ++ $source_inc;
  344. $inv->{source} = $source;
  345. }
  346. my $tmp_invoices = $inv->{invoices};
  347. $inv->{invoices} = [];
  348. @{$inv->{invoices}} = $self->_parse_array($tmp_invoices);
  349. }
  350. $self->{dbh}->commit; # Commit locks
  351. }
  352. sub post_bulk {
  353. my ($self) = @_;
  354. my $total_count = 0;
  355. my ($ref) = $self->call_procedure(
  356. procname => 'setting_get',
  357. args => ['queue_payments'],
  358. );
  359. my $queue_payments = $ref->{setting_get};
  360. if ($queue_payments){
  361. my ($job_ref) = $self->exec_method(
  362. funcname => 'job__create'
  363. );
  364. $self->{job_id} = $job_ref->{job__create};
  365. ($self->{job}) = $self->exec_method(
  366. funcname => 'job__status'
  367. );
  368. }
  369. $self->{payment_date} = $self->{datepaid};
  370. for my $contact_row (1 .. $self->{contact_count}){
  371. my $contact_id = $self->{"contact_$contact_row"};
  372. next if (!$self->{"id_$contact_id"});
  373. my $invoice_array = "{}"; # Pg Array
  374. for my $invoice_row (1 .. $self->{"invoice_count_$contact_id"}){
  375. my $invoice_id = $self->{"invoice_${contact_id}_${invoice_row}"};
  376. my $pay_amount = ($self->{"paid_$contact_id"} eq 'all' )
  377. ? $self->{"net_$invoice_id"}
  378. : $self->{"payment_$invoice_id"};
  379. next if ! $pay_amount;
  380. $pay_amount = $pay_amount * 1;
  381. my $invoice_subarray = "{$invoice_id,$pay_amount}";
  382. if ($invoice_subarray !~ /^\{\d+\,\-?\d*\.?\d+\}$/){
  383. $self->error("Invalid subarray: $invoice_subarray");
  384. }
  385. $invoice_subarray =~ s/[^0123456789{},.-]//;
  386. if ($invoice_array eq '{}'){ # Omit comma
  387. $invoice_array = "{$invoice_subarray}";
  388. } else {
  389. $invoice_array =~ s/\}$/,$invoice_subarray\}/;
  390. }
  391. }
  392. $self->{transactions} = $invoice_array;
  393. $self->{source} = $self->{"source_$contact_id"};
  394. if ($queue_payments){
  395. $self->{batch_class} = 3;
  396. $self->exec_method(
  397. funcname => 'payment_bulk_queue'
  398. );
  399. } else {
  400. $self->exec_method(funcname => 'payment_bulk_post');
  401. }
  402. }
  403. $self->{queue_payments} = $queue_payments;
  404. return $self->{dbh}->commit;
  405. }
  406. sub check_job {
  407. my ($self) = @_;
  408. ($self->{job}) = $self->exec_method(funcname => 'job__status');
  409. }
  410. =item post_payment
  411. This method uses payment_post to store a payment (not a bulk payment) on the database.
  412. =cut
  413. sub post_payment {
  414. my ($self) = @_;
  415. $self->exec_method(funcname => 'payment_post');
  416. $self->{dbh}->commit();
  417. }
  418. 1;