summaryrefslogtreecommitdiff
path: root/LedgerSMB/OP.pm
blob: f559208682a369e5d5d2bc9cd2cec4f173b32079 (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. $invnumber =
  38. $form->update_defaults( $myconfig, ( $form->{arap} eq 'ar' )
  39. ? "sinumber"
  40. : "vinumber", $dbh )
  41. unless $invnumber;
  42. my $fxamount = $form->round_amount( $amount * $form->{exchangerate}, 2 );
  43. my ($paymentaccno) = split /--/, $form->{account};
  44. my ( $null, $department_id ) = split /--/, $form->{department};
  45. $department_id *= 1;
  46. my $uid = localtime;
  47. $uid .= "$$";
  48. # add AR/AP header transaction with a payment
  49. my $login = $dbh->quote( $form->{login} );
  50. $query = qq|
  51. INSERT INTO $form->{arap} (invnumber, employee_id)
  52. VALUES ('$uid', (SELECT id FROM employee
  53. WHERE login = $login))|;
  54. $dbh->do($query) || $form->dberror($query);
  55. $query = qq|SELECT id FROM $form->{arap} WHERE invnumber = '$uid'|;
  56. ($uid) = $dbh->selectrow_array($query);
  57. my $invnumber = $form->{invnumber};
  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. $sth = $dbh->prepare($query);
  86. $sth->execute( $uid, $accno, $form->{datepaid}, $fxamount * $ml )
  87. || $form->dberror($query);
  88. # add payment
  89. $query = qq|
  90. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  91. amount, source, memo)
  92. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  93. ?, ?, ?, ?)|;
  94. $sth = $dbh->prepare($query);
  95. $sth->execute( $uid, $paymentaccno, $form->{datepaid}, $amount * $ml * -1,
  96. $form->{source}, $form->{memo} )
  97. || $form->dberror($query);
  98. # add exchangerate difference
  99. if ( $fxamount != $amount ) {
  100. $query = qq|
  101. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  102. amount, cleared, fx_transaction, source)
  103. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  104. ?, ?, '1', '1', ?)|;
  105. $sth = $dbh->prepare($query);
  106. $sth->execute( $uid, $paymentaccno, $form->{datepaid},
  107. ( $fxamount - $amount ) * $ml * -1,
  108. $form->{source} )
  109. || $form->dberror($query);
  110. }
  111. my %audittrail = (
  112. tablename => $form->{arap},
  113. reference => $invnumber,
  114. formname => ( $form->{arap} eq 'ar' )
  115. ? 'deposit'
  116. : 'pre-payment',
  117. action => 'posted',
  118. id => $uid
  119. );
  120. $form->audittrail( $dbh, "", \%audittrail );
  121. }
  122. 1;