summaryrefslogtreecommitdiff
path: root/LedgerSMB/OP.pm
blob: 13f9da01228b59c7d37d9b8ccd280d1521a72dde (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 $fxamount = $form->round_amount($amount * $form->{exchangerate}, 2);
  38. my ($paymentaccno) = split /--/, $form->{account};
  39. my ($null, $department_id) = split /--/, $form->{department};
  40. $department_id *= 1;
  41. my $uid = localtime;
  42. $uid .= "$$";
  43. # add AR/AP header transaction with a payment
  44. my $login = $dbh->quote($form->{login});
  45. $query = qq|
  46. INSERT INTO $form->{arap} (invnumber, employee_id)
  47. VALUES ('$uid', (SELECT id FROM employee
  48. WHERE login = $login))|;
  49. $dbh->do($query) || $form->dberror($query);
  50. $query = qq|SELECT id FROM $form->{arap} WHERE invnumber = '$uid'|;
  51. ($uid) = $dbh->selectrow_array($query);
  52. my $invnumber = $form->{invnumber};
  53. $invnumber = $form->update_defaults(
  54. $myconfig,
  55. ($form->{arap} eq 'ar')
  56. ? "sinumber"
  57. : "vinumber",
  58. $dbh) unless $invnumber;
  59. $query = qq|
  60. UPDATE $form->{arap}
  61. set invnumber = ?,
  62. $form->{vc}_id = ?,
  63. transdate = ?,
  64. datepaid = ?,
  65. duedate = ?,
  66. netamount = 0,
  67. amount = 0,
  68. paid = ?,
  69. curr = ?,
  70. department_id = ?
  71. WHERE id = ?|;
  72. $sth = $dbh->prepare($query);
  73. $sth->execute(
  74. $invnumber, $form->{"$form->{vc}_id"}, $form->{datepaid},
  75. $form->{datepaid}, $form->{datepaid}, $fxamount,
  76. $form->{currency}, $department_id, $uid
  77. ) || $form->dberror($query);
  78. # add AR/AP
  79. ($accno) = split /--/, $form->{$form->{ARAP}};
  80. $query = qq|
  81. INSERT INTO acc_trans (trans_id, chart_id, transdate, amount)
  82. VALUES (?, (SELECT id FROM chart
  83. WHERE accno = ?), ?, ?)|;
  84. $sth = $dbh->prepare($query);
  85. $sth->execute($uid, $accno, $form->{datepaid}, $fxamount * $ml)
  86. || $form->dberror($query);
  87. # add payment
  88. $query = qq|
  89. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  90. amount, source, memo)
  91. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  92. ?, ?, ?, ?)|;
  93. $sth = $dbh->prepare($query);
  94. $sth->execute(
  95. $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, $form->{source}
  108. ) || $form->dberror($query);
  109. }
  110. my %audittrail = (
  111. tablename => $form->{arap},
  112. reference => $invnumber,
  113. formname =>
  114. ($form->{arap} eq 'ar')
  115. ? 'deposit'
  116. : 'pre-payment',
  117. action => 'posted',
  118. id => $uid );
  119. $form->audittrail($dbh, "", \%audittrail);
  120. }
  121. 1;