summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Payment.pm
blob: 02f3aad59919785c004f42e96aa00758cde142b4 (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 list_overpayment_accounting
  242. This method lists all accounts that match the role specified in account_class property and
  243. are availible to store an overpayment / advanced payment / pre-payment.
  244. =back
  245. =cut
  246. sub list_overpayment_accounting {
  247. my ($self) = @_;
  248. @{$self->{overpayment_accounts}} = $self->exec_method( funcname => 'chart_list_overpayment');
  249. return @{$self->{overpayment_accounts}};
  250. }
  251. =item get_sources
  252. This method builds all the possible sources of money,
  253. in the future it will look inside the DB.
  254. =back
  255. =cut
  256. sub get_sources {
  257. my ($self, $locale) = @_;
  258. @{$self->{cash_sources}} = ($locale->text('cash'),
  259. $locale->text('check'),
  260. $locale->text('deposit'),
  261. $locale->text('other'));
  262. return @{$self->{cash_sources}};
  263. }
  264. =item get_exchange_rate(currency, date)
  265. This method gets the exchange rate for the specified currency and date
  266. =cut
  267. sub get_exchange_rate {
  268. my ($self) = shift @_;
  269. ($self->{currency}, $self->{date}) = @_;
  270. ($self->{exchangerate}) = $self->exec_method(funcname => 'currency_get_exchangerate');
  271. return $self->{exchangerate}->{currency_get_exchangerate};
  272. }
  273. =item get_default_currency
  274. This method gets the default currency
  275. =back
  276. =cut
  277. sub get_default_currency {
  278. my ($self) = shift @_;
  279. ($self->{default_currency}) = $self->call_procedure(procname => 'defaults_get_defaultcurrency');
  280. return $self->{default_currency}->{defaults_get_defaultcurrency};
  281. }
  282. =item get_current_date
  283. This method returns the system's current date
  284. =cut
  285. sub get_current_date {
  286. my ($self) = shift @_;
  287. return $self->{current_date};
  288. }
  289. =item get_vc_info
  290. This method returns the contact informatino for a customer or vendor according to
  291. $self->{account_class}
  292. =cut
  293. sub get_vc_info {
  294. my ($self) = @_;
  295. @{$self->{vendor_customer_info}} = $self->exec_method(funcname => 'payment_get_vc_info');
  296. return @{$self->{vendor_customer_info}};
  297. }
  298. =item get_payment_detail_data
  299. This method sets appropriate project, department, etc. fields.
  300. =cut
  301. sub get_payment_detail_data {
  302. my ($self) = @_;
  303. $self->get_metadata();
  304. my $source_inc;
  305. my $source_src;
  306. if (defined ($self->{source_start})){
  307. $self->{source_start} =~ /(\d*)\D*$/;
  308. $source_src = $1;
  309. if ($source_src) {
  310. $source_inc = $source_src;
  311. } else {
  312. $source_inc = 0;
  313. }
  314. }
  315. my $source_length = length($source_inc);
  316. @{$self->{contact_invoices}} = $self->exec_method(
  317. funcname => 'payment_get_all_contact_invoices');
  318. for my $inv (@{$self->{contact_invoices}}){
  319. if (defined $self->{source_start}){
  320. my $source = $self->{source_start};
  321. if (length($source_inc) < $source_length){
  322. $source_inc = sprintf('%0*s', $source_length, $source_inc);
  323. }
  324. $source =~ s/$source_src(\D*)$/$source_inc$1/;
  325. ++ $source_inc;
  326. $inv->{source} = $source;
  327. }
  328. my $tmp_invoices = $inv->{invoices};
  329. $inv->{invoices} = [];
  330. @{$inv->{invoices}} = $self->_parse_array($tmp_invoices);
  331. }
  332. $self->{dbh}->commit; # Commit locks
  333. }
  334. sub post_bulk {
  335. my ($self) = @_;
  336. my $total_count = 0;
  337. my ($ref) = $self->call_procedure(
  338. procname => 'setting_get',
  339. args => ['queue_payments'],
  340. );
  341. my $queue_payments = $ref->{setting_get};
  342. if ($queue_payments){
  343. my ($job_ref) = $self->exec_method(
  344. funcname => 'job__create'
  345. );
  346. $self->{job_id} = $job_ref->{job__create};
  347. ($self->{job}) = $self->exec_method(
  348. funcname => 'job__status'
  349. );
  350. }
  351. $self->{payment_date} = $self->{datepaid};
  352. for my $contact_row (1 .. $self->{contact_count}){
  353. my $contact_id = $self->{"contact_$contact_row"};
  354. next if (!$self->{"id_$contact_id"});
  355. my $invoice_array = "{}"; # Pg Array
  356. for my $invoice_row (1 .. $self->{"invoice_count_$contact_id"}){
  357. my $invoice_id = $self->{"invoice_${contact_id}_${invoice_row}"};
  358. my $pay_amount = ($self->{"paid_$contact_id"} eq 'all' )
  359. ? $self->{"net_$invoice_id"}
  360. : $self->{"payment_$invoice_id"};
  361. next if ! $pay_amount;
  362. $pay_amount = $pay_amount * 1;
  363. my $invoice_subarray = "{$invoice_id,$pay_amount}";
  364. if ($invoice_subarray !~ /^\{\d+\,\-?\d*\.?\d+\}$/){
  365. $self->error("Invalid subarray: $invoice_subarray");
  366. }
  367. $invoice_subarray =~ s/[^0123456789{},.-]//;
  368. if ($invoice_array eq '{}'){ # Omit comma
  369. $invoice_array = "{$invoice_subarray}";
  370. } else {
  371. $invoice_array =~ s/\}$/,$invoice_subarray\}/;
  372. }
  373. }
  374. $self->{transactions} = $invoice_array;
  375. $self->{source} = $self->{"source_$contact_id"};
  376. if ($queue_payments){
  377. $self->{batch_class} = 3;
  378. $self->exec_method(
  379. funcname => 'payment_bulk_queue'
  380. );
  381. } else {
  382. $self->exec_method(funcname => 'payment_bulk_post');
  383. }
  384. }
  385. $self->{queue_payments} = $queue_payments;
  386. return $self->{dbh}->commit;
  387. }
  388. sub check_job {
  389. my ($self) = @_;
  390. ($self->{job}) = $self->exec_method(funcname => 'job__status');
  391. }
  392. 1;