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