summaryrefslogtreecommitdiff
path: root/LedgerSMB/OP.pm
blob: 65b8da315b255ed0f44616609ce91b80f87decdf (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # See COPYRIGHT file for copyright information
  6. #======================================================================
  7. #
  8. # This file has NOT undergone whitespace cleanup.
  9. #
  10. #======================================================================
  11. #
  12. # Overpayment function
  13. # used in AR, AP, IS, IR, OE, CP
  14. #
  15. #======================================================================
  16. package OP;
  17. sub overpayment {
  18. my ($self, $myconfig, $form, $dbh, $amount, $ml) = @_;
  19. my $fxamount = $form->round_amount($amount * $form->{exchangerate}, 2);
  20. my ($paymentaccno) = split /--/, $form->{account};
  21. my ($null, $department_id) = split /--/, $form->{department};
  22. $department_id *= 1;
  23. my $uid = localtime;
  24. $uid .= "$$";
  25. # add AR/AP header transaction with a payment
  26. $query = qq|INSERT INTO $form->{arap} (invnumber, employee_id)
  27. VALUES ('$uid', (SELECT id FROM employee
  28. WHERE login = '$form->{login}'))|;
  29. $dbh->do($query) || $form->dberror($query);
  30. $query = qq|SELECT id FROM $form->{arap}
  31. WHERE invnumber = '$uid'|;
  32. ($uid) = $dbh->selectrow_array($query);
  33. my $invnumber = $form->{invnumber};
  34. $invnumber = $form->update_defaults($myconfig, ($form->{arap} eq 'ar') ? "sinumber" : "vinumber", $dbh) unless $invnumber;
  35. $query = qq|UPDATE $form->{arap} set
  36. invnumber = |.$dbh->quote($invnumber).qq|,
  37. $form->{vc}_id = $form->{"$form->{vc}_id"},
  38. transdate = '$form->{datepaid}',
  39. datepaid = '$form->{datepaid}',
  40. duedate = '$form->{datepaid}',
  41. netamount = 0,
  42. amount = 0,
  43. paid = $fxamount,
  44. curr = '$form->{currency}',
  45. department_id = $department_id
  46. WHERE id = $uid|;
  47. $dbh->do($query) || $form->dberror($query);
  48. # add AR/AP
  49. ($accno) = split /--/, $form->{$form->{ARAP}};
  50. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount)
  51. VALUES ($uid, (SELECT id FROM chart
  52. WHERE accno = '$accno'),
  53. '$form->{datepaid}', $fxamount * $ml)|;
  54. $dbh->do($query) || $form->dberror($query);
  55. # add payment
  56. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
  57. amount, source, memo)
  58. VALUES ($uid, (SELECT id FROM chart
  59. WHERE accno = '$paymentaccno'),
  60. '$form->{datepaid}', $amount * $ml * -1, |
  61. .$dbh->quote($form->{source}).qq|, |
  62. .$dbh->quote($form->{memo}).qq|)|;
  63. $dbh->do($query) || $form->dberror($query);
  64. # add exchangerate difference
  65. if ($fxamount != $amount) {
  66. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
  67. amount, cleared, fx_transaction, source)
  68. VALUES ($uid, (SELECT id FROM chart
  69. WHERE accno = '$paymentaccno'),
  70. '$form->{datepaid}', ($fxamount - $amount) * $ml * -1,
  71. '1', '1', |
  72. .$dbh->quote($form->{source}).qq|)|;
  73. $dbh->do($query) || $form->dberror($query);
  74. }
  75. my %audittrail = ( tablename => $form->{arap},
  76. reference => $invnumber,
  77. formname => ($form->{arap} eq 'ar') ? 'deposit' : 'pre-payment',
  78. action => 'posted',
  79. id => $uid );
  80. $form->audittrail($dbh, "", \%audittrail);
  81. }
  82. 1;