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