summaryrefslogtreecommitdiff
path: root/LedgerSMB/OP.pm
blob: 4b8d9121655b3444dd479ef8a427af028d150dcf (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2001
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # Overpayment function
  31. # used in AR, AP, IS, IR, OE, CP
  32. #
  33. #======================================================================
  34. package OP;
  35. sub overpayment {
  36. my ( $self, $myconfig, $form, $dbh, $amount, $ml ) = @_;
  37. my $invnumber = $form->{invnumber};
  38. $invnumber =
  39. $form->update_defaults( $myconfig, ( $form->{arap} eq 'ar' )
  40. ? "sinumber"
  41. : "vinumber", $dbh )
  42. unless $invnumber;
  43. my $fxamount = $form->round_amount( $amount * $form->{exchangerate}, 2 );
  44. my ($paymentaccno) = split /--/, $form->{account};
  45. my ( $null, $department_id ) = split /--/, $form->{department};
  46. $department_id *= 1;
  47. my $uid = localtime;
  48. $uid .= "$$";
  49. # add AR/AP header transaction with a payment
  50. my $login = $dbh->quote( $form->{login} );
  51. $query = qq|
  52. INSERT INTO $form->{arap} (invnumber, employee_id)
  53. VALUES ('$uid', (SELECT id FROM employee
  54. WHERE login = $login))|;
  55. $dbh->do($query) || $form->dberror($query);
  56. $query = qq|SELECT id FROM $form->{arap} WHERE invnumber = '$uid'|;
  57. ($uid) = $dbh->selectrow_array($query);
  58. $query = qq|
  59. UPDATE $form->{arap}
  60. set invnumber = ?,
  61. $form->{vc}_id = ?,
  62. transdate = ?,
  63. datepaid = ?,
  64. duedate = ?,
  65. netamount = 0,
  66. amount = 0,
  67. paid = ?,
  68. curr = ?,
  69. department_id = ?
  70. WHERE id = ?|;
  71. $sth = $dbh->prepare($query);
  72. $sth->execute(
  73. $invnumber, $form->{"$form->{vc}_id"},
  74. $form->{datepaid}, $form->{datepaid},
  75. $form->{datepaid}, $fxamount,
  76. $form->{currency}, $department_id,
  77. $uid
  78. ) || $form->dberror($query);
  79. # add AR/AP
  80. ($accno) = split /--/, $form->{ $form->{ARAP} };
  81. $query = qq|
  82. INSERT INTO acc_trans (trans_id, chart_id, transdate, amount)
  83. VALUES (?, (SELECT id FROM chart
  84. WHERE accno = ?), ?, ?, ?)|;
  85. if (not defined $form->{approved}){
  86. $form->{approved} = 1;
  87. }
  88. if (!$form->{approved}){
  89. if (not defined $form->{batch_id}){
  90. $form->error($locale->text('Batch ID Missing'));
  91. }
  92. $query = qq|
  93. INSERT INTO voucher (batch_id, trans_id) VALUES (?, ?)|;
  94. $sth = $dbh->prepare($query);
  95. $sth->execute($form->{batch_id}, $uid) ||
  96. $form->dberror($query);
  97. }
  98. $sth = $dbh->prepare($query);
  99. $sth->execute( $uid, $accno, $form->{datepaid}, $fxamount * $ml,
  100. $form->{approved} )
  101. || $form->dberror($query);
  102. # add payment
  103. $query = qq|
  104. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  105. amount, source, memo)
  106. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  107. ?, ?, ?, ?)|;
  108. $sth = $dbh->prepare($query);
  109. $sth->execute( $uid, $paymentaccno, $form->{datepaid}, $amount * $ml * -1,
  110. $form->{source}, $form->{memo} )
  111. || $form->dberror($query);
  112. # add exchangerate difference
  113. if ( $fxamount != $amount ) {
  114. $query = qq|
  115. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  116. amount, cleared, fx_transaction, source)
  117. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  118. ?, ?, '1', '1', ?)|;
  119. $sth = $dbh->prepare($query);
  120. $sth->execute( $uid, $paymentaccno, $form->{datepaid},
  121. ( $fxamount - $amount ) * $ml * -1,
  122. $form->{source} )
  123. || $form->dberror($query);
  124. }
  125. my %audittrail = (
  126. tablename => $form->{arap},
  127. reference => $invnumber,
  128. formname => ( $form->{arap} eq 'ar' )
  129. ? 'deposit'
  130. : 'pre-payment',
  131. action => 'posted',
  132. id => $uid
  133. );
  134. $form->audittrail( $dbh, "", \%audittrail );
  135. }
  136. 1;