summaryrefslogtreecommitdiff
path: root/LedgerSMB/OP.pm
blob: 836ef92ca079a2c801355dfcb4fd024eaf70b011 (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 =
  54. $form->update_defaults( $myconfig, ( $form->{arap} eq 'ar' )
  55. ? "sinumber"
  56. : "vinumber", $dbh )
  57. unless $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. if (not defined $form->{approved}){
  86. $form->{approved} = 1;
  87. }
  88. $sth = $dbh->prepare($query);
  89. $sth->execute( $uid, $accno, $form->{datepaid}, $fxamount * $ml,
  90. $form->{approved} )
  91. || $form->dberror($query);
  92. # add payment
  93. $query = qq|
  94. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  95. amount, source, memo)
  96. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  97. ?, ?, ?, ?)|;
  98. $sth = $dbh->prepare($query);
  99. $sth->execute( $uid, $paymentaccno, $form->{datepaid}, $amount * $ml * -1,
  100. $form->{source}, $form->{memo} )
  101. || $form->dberror($query);
  102. # add exchangerate difference
  103. if ( $fxamount != $amount ) {
  104. $query = qq|
  105. INSERT INTO acc_trans (trans_id, chart_id, transdate,
  106. amount, cleared, fx_transaction, source)
  107. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  108. ?, ?, '1', '1', ?)|;
  109. $sth = $dbh->prepare($query);
  110. $sth->execute( $uid, $paymentaccno, $form->{datepaid},
  111. ( $fxamount - $amount ) * $ml * -1,
  112. $form->{source} )
  113. || $form->dberror($query);
  114. }
  115. my %audittrail = (
  116. tablename => $form->{arap},
  117. reference => $invnumber,
  118. formname => ( $form->{arap} eq 'ar' )
  119. ? 'deposit'
  120. : 'pre-payment',
  121. action => 'posted',
  122. id => $uid
  123. );
  124. $form->audittrail( $dbh, "", \%audittrail );
  125. }
  126. 1;