summaryrefslogtreecommitdiff
path: root/LedgerSMB/OP.pm
blob: ed03d4241f6a3bcae32c47dccb9144c6d059a6ec (plain)
  1. =head1 NAME
  2. OP
  3. =head1 SYNOPSIS
  4. This module provides an overpayment function used by CP.pm
  5. =head1 COPYRIGHT
  6. #====================================================================
  7. # LedgerSMB
  8. # Small Medium Business Accounting software
  9. # http://www.ledgersmb.org/
  10. #
  11. # Copyright (C) 2006
  12. # This work contains copyrighted information from a number of sources
  13. # all used with permission.
  14. #
  15. # This file contains source code included with or based on SQL-Ledger
  16. # which is Copyright Dieter Simader and DWS Systems Inc. 2000-2005
  17. # and licensed under the GNU General Public License version 2 or, at
  18. # your option, any later version. For a full list including contact
  19. # information of contributors, maintainers, and copyright holders,
  20. # see the CONTRIBUTORS file.
  21. #
  22. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  23. # Copyright (C) 2001
  24. #
  25. # Author: DWS Systems Inc.
  26. # Web: http://www.sql-ledger.org
  27. #
  28. # Contributors:
  29. #
  30. #====================================================================
  31. #
  32. # This file has undergone whitespace cleanup.
  33. #
  34. #====================================================================
  35. #
  36. # Overpayment function
  37. # used in AR, AP, IS, IR, OE, CP
  38. #
  39. #====================================================================
  40. =head1 METHODS
  41. =over
  42. =cut
  43. package OP;
  44. =item OP::overpayment('', $myconfig, $form, $dbh, $amount, $ml);
  45. Adds entries to acc_trans and ar or ap relating to overpayments found while
  46. processing payments with CP->post_payment and CP->post_payments. Also adds an
  47. audit trail entry with a formname of 'deposit' if it is on an AR transaction
  48. or 'pre-payment' if the transaction is an AP transaction. $amount is the
  49. overpayment amount without the exchange rate applied.
  50. $form attributes used by this function:
  51. =over
  52. =item $form->{arap}
  53. Possible values are 'ar' and 'ap'. Indicates part of the transaction type.
  54. =item $form->{ARAP}
  55. Possible values are 'AR' and 'AP'. Should be the upper case variant of the
  56. value in $form->{arap}.
  57. =item $form->{vc}
  58. Possible values are 'vendor' and 'customer'. Should be 'customer' if
  59. $form->{arap} is 'ar' and 'vendor' if $form->{arap} is 'ap'.
  60. =item $form->{invnumber}
  61. Invoice number for the transaction being processed.
  62. =item $form->{exchangerate}
  63. Exchange rate used in the transaction.
  64. =item $form->{currency}
  65. Currency used by the transaction.
  66. =item $form->{account}
  67. Of the form 'accno--description'. Used to obtain the account number of the
  68. payment account.
  69. =item $form->{$form->{ARAP}}
  70. Of the form 'accno--description'. Used to obtain the account number of the
  71. non-payment account.
  72. =item $form->{department}
  73. Of the form 'description--department_id'. Used to obtain the id of the
  74. department involved in the transaction.
  75. =item $form->{$form->{vc}_id}
  76. The id of the customer or vendor involved in the overpayment.
  77. =item $form->{datepaid}
  78. The date to enter in as all the date fields related to the overpayment entries.
  79. =item $form->{source}
  80. Payment source
  81. =item $form->{memo}
  82. Payment memo
  83. =item $form->{approved}
  84. If this is false but defined, add a voucher entry. Otherwise, is set to true.
  85. =item $form->{batch_id}
  86. Batch id for a voucher, only used if $form->{approved} is false but defined.
  87. =back
  88. =cut
  89. sub overpayment {
  90. my ( $self, $myconfig, $form, $dbh, $amount, $ml ) = @_;
  91. my $invnumber = $form->{invnumber};
  92. $invnumber =
  93. $form->update_defaults( $myconfig, ( $form->{arap} eq 'ar' )
  94. ? "sinumber"
  95. : "vinumber", $dbh )
  96. unless $invnumber;
  97. my $fxamount = $form->round_amount( $amount * $form->{exchangerate}, 2 );
  98. my ($paymentaccno) = split /--/, $form->{account};
  99. my ( $null, $department_id ) = split /--/, $form->{department};
  100. $department_id *= 1;
  101. my $uid = localtime;
  102. $uid .= "$$";
  103. # add AR/AP header transaction with a payment
  104. my $login = $dbh->quote( $form->{login} );
  105. $query = qq|
  106. INSERT INTO $form->{arap} (invnumber, employee_id)
  107. VALUES ('$uid', (SELECT id FROM employee
  108. WHERE login = $login))|;
  109. $dbh->do($query) || $form->dberror($query);
  110. $query = qq|SELECT id FROM $form->{arap} WHERE invnumber = '$uid'|;
  111. ($uid) = $dbh->selectrow_array($query);
  112. $query = qq|
  113. UPDATE $form->{arap}
  114. set invnumber = ?,
  115. $form->{vc}_id = ?,
  116. transdate = ?,
  117. datepaid = ?,
  118. duedate = ?,
  119. netamount = 0,
  120. amount = 0,
  121. paid = ?,
  122. curr = ?,
  123. department_id = ?
  124. WHERE id = ?|;
  125. $sth = $dbh->prepare($query);
  126. $sth->execute(
  127. $invnumber, $form->{"$form->{vc}_id"},
  128. $form->{datepaid}, $form->{datepaid},
  129. $form->{datepaid}, $fxamount,
  130. $form->{currency}, $department_id,
  131. $uid
  132. ) || $form->dberror($query);
  133. # add AR/AP
  134. ($accno) = split /--/, $form->{ $form->{ARAP} };
  135. $query = qq|
  136. INSERT INTO acc_trans (trans_id, chart_id, transdate, amount,
  137. approved)
  138. VALUES (?, (SELECT id FROM chart
  139. WHERE accno = ?), ?, ?, ?)|;
  140. if (not defined $form->{approved}){
  141. $form->{approved} = 1;
  142. }
  143. if (!$form->{approved}){
  144. if (not defined $form->{batch_id}){
  145. $form->error($locale->text('Batch ID Missing'));
  146. }
  147. $query = qq|
  148. INSERT INTO voucher (batch_id, trans_id) VALUES (?, ?)|;
  149. $sth = $dbh->prepare($query);
  150. $sth->execute($form->{batch_id}, $uid) ||
  151. $form->dberror($query);
  152. }
  153. $sth = $dbh->prepare($query);
  154. $sth->execute( $uid, $accno, $form->{datepaid}, $fxamount * $ml,
  155. $form->{approved} )
  156. || $form->dberror($query);
  157. # add payment
  158. $query = qq|
  159. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  160. amount, source, memo)
  161. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  162. ?, ?, ?, ?)|;
  163. $sth = $dbh->prepare($query);
  164. $sth->execute( $uid, $paymentaccno, $form->{datepaid}, $amount * $ml * -1,
  165. $form->{source}, $form->{memo} )
  166. || $form->dberror($query);
  167. # add exchangerate difference
  168. if ( $fxamount != $amount ) {
  169. $query = qq|
  170. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  171. amount, cleared, fx_transaction, source)
  172. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  173. ?, ?, '1', '1', ?)|;
  174. $sth = $dbh->prepare($query);
  175. $sth->execute( $uid, $paymentaccno, $form->{datepaid},
  176. ( $fxamount - $amount ) * $ml * -1,
  177. $form->{source} )
  178. || $form->dberror($query);
  179. }
  180. my %audittrail = (
  181. tablename => $form->{arap},
  182. reference => $invnumber,
  183. formname => ( $form->{arap} eq 'ar' )
  184. ? 'deposit'
  185. : 'pre-payment',
  186. action => 'posted',
  187. id => $uid
  188. );
  189. $form->audittrail( $dbh, "", \%audittrail );
  190. }
  191. 1;
  192. =back