summaryrefslogtreecommitdiff
path: root/LedgerSMB/OP.pm
blob: 477445260f5931fbbe80b7d2566d469891c8f58c (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 NOT 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. $query = qq|INSERT INTO $form->{arap} (invnumber, employee_id)
  45. VALUES ('$uid', (SELECT id FROM employee
  46. WHERE login = '$form->{login}'))|;
  47. $dbh->do($query) || $form->dberror($query);
  48. $query = qq|SELECT id FROM $form->{arap}
  49. WHERE invnumber = '$uid'|;
  50. ($uid) = $dbh->selectrow_array($query);
  51. my $invnumber = $form->{invnumber};
  52. $invnumber = $form->update_defaults($myconfig, ($form->{arap} eq 'ar') ? "sinumber" : "vinumber", $dbh) unless $invnumber;
  53. $query = qq|UPDATE $form->{arap} set
  54. invnumber = |.$dbh->quote($invnumber).qq|,
  55. $form->{vc}_id = $form->{"$form->{vc}_id"},
  56. transdate = '$form->{datepaid}',
  57. datepaid = '$form->{datepaid}',
  58. duedate = '$form->{datepaid}',
  59. netamount = 0,
  60. amount = 0,
  61. paid = $fxamount,
  62. curr = '$form->{currency}',
  63. department_id = $department_id
  64. WHERE id = $uid|;
  65. $dbh->do($query) || $form->dberror($query);
  66. # add AR/AP
  67. ($accno) = split /--/, $form->{$form->{ARAP}};
  68. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount)
  69. VALUES ($uid, (SELECT id FROM chart
  70. WHERE accno = '$accno'),
  71. '$form->{datepaid}', $fxamount * $ml)|;
  72. $dbh->do($query) || $form->dberror($query);
  73. # add payment
  74. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
  75. amount, source, memo)
  76. VALUES ($uid, (SELECT id FROM chart
  77. WHERE accno = '$paymentaccno'),
  78. '$form->{datepaid}', $amount * $ml * -1, |
  79. .$dbh->quote($form->{source}).qq|, |
  80. .$dbh->quote($form->{memo}).qq|)|;
  81. $dbh->do($query) || $form->dberror($query);
  82. # add exchangerate difference
  83. if ($fxamount != $amount) {
  84. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
  85. amount, cleared, fx_transaction, source)
  86. VALUES ($uid, (SELECT id FROM chart
  87. WHERE accno = '$paymentaccno'),
  88. '$form->{datepaid}', ($fxamount - $amount) * $ml * -1,
  89. '1', '1', |
  90. .$dbh->quote($form->{source}).qq|)|;
  91. $dbh->do($query) || $form->dberror($query);
  92. }
  93. my %audittrail = ( tablename => $form->{arap},
  94. reference => $invnumber,
  95. formname => ($form->{arap} eq 'ar') ? 'deposit' : 'pre-payment',
  96. action => 'posted',
  97. id => $uid );
  98. $form->audittrail($dbh, "", \%audittrail);
  99. }
  100. 1;