summaryrefslogtreecommitdiff
path: root/LedgerSMB/OP.pm
blob: 525717ff2a2d52cebeb29b6cdbb8108f02aeb1f4 (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. $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;