/doc/plugins/teximg/

ipt>
summaryrefslogtreecommitdiff
path: root/LedgerSMB/AA.pm
blob: 89d48bab04a9aef3a5ab6927043ad44e65e2139b (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # Copyright (C) 2006
  5. # This work contains copyrighted information from a number of sources all used
  6. # with permission.
  7. #
  8. # This file contains source code included with or based on SQL-Ledger which
  9. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  10. # under the GNU General Public License version 2 or, at your option, any later
  11. # version. For a full list including contact information of contributors,
  12. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  13. #
  14. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  15. # Copyright (C) 2006
  16. #
  17. # Author: DWS Systems Inc.
  18. # Web: http://www.sql-ledger.org
  19. #
  20. # Contributors:
  21. #
  22. #
  23. # See COPYRIGHT file for copyright information
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # AR/AP backend routines
  31. # common routines
  32. #
  33. #======================================================================
  34. package AA;
  35. sub post_transaction {
  36. my ($self, $myconfig, $form) = @_;
  37. # connect to database
  38. my $dbh = $form->dbconnect_noauto($myconfig);
  39. my $query;
  40. my $sth;
  41. my $null;
  42. ($null, $form->{department_id}) = split(/--/, $form->{department});
  43. $form->{department_id} *= 1;
  44. my $ml = 1;
  45. my $table = 'ar';
  46. my $buysell = 'buy';
  47. my $ARAP = 'AR';
  48. my $invnumber = "sinumber";
  49. my $keepcleared;
  50. if ($form->{vc} eq 'vendor') {
  51. $table = 'ap';
  52. $buysell = 'sell';
  53. $ARAP = 'AP';
  54. $ml = -1;
  55. $invnumber = "vinumber";
  56. }
  57. if ($form->{currency} eq $form->{defaultcurrency}) {
  58. $form->{exchangerate} = 1;
  59. } else {
  60. $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, $buysell);
  61. $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
  62. }
  63. my @taxaccounts = split / /, $form->{taxaccounts};
  64. my $tax = 0;
  65. my $fxtax = 0;
  66. my $amount;
  67. my $diff;
  68. my %tax = ();
  69. my $accno;
  70. # add taxes
  71. foreach $accno (@taxaccounts) {
  72. $fxtax += $tax{fxamount}{$accno} = $form->parse_amount($myconfig, $form->{"tax_$accno"});
  73. $tax += $tax{fxamount}{$accno};
  74. push @{ $form->{acc_trans}{taxes} }, {
  75. accno => $accno,
  76. amount => $tax{fxamount}{$accno},
  77. project_id => 'NULL',
  78. fx_transaction => 0 };
  79. $amount = $tax{fxamount}{$accno} * $form->{exchangerate};
  80. $tax{amount}{$accno} = $form->round_amount($amount - $diff, 2);
  81. $diff = $tax{amount}{$accno} - ($amount - $diff);
  82. $amount = $tax{amount}{$accno} - $tax{fxamount}{$accno};
  83. $tax += $amount;
  84. if ($form->{currency} ne $form->{defaultcurrency}) {
  85. push @{ $form->{acc_trans}{taxes} }, {
  86. accno => $accno,
  87. amount => $amount,
  88. project_id => 'NULL',
  89. fx_transaction => 1 };
  90. }
  91. }
  92. my %amount = ();
  93. my $fxinvamount = 0;
  94. for (1 .. $form->{rowcount}) {
  95. $fxinvamount += $amount{fxamount}{$_} = $form->parse_amount($myconfig, $form->{"amount_$_"})
  96. }
  97. $form->{taxincluded} *= 1;
  98. my $i;
  99. my $project_id;
  100. my $cleared = 0;
  101. $diff = 0;
  102. # deduct tax from amounts if tax included
  103. for $i (1 .. $form->{rowcount}) {
  104. if ($amount{fxamount}{$i}) {
  105. if ($form->{taxincluded}) {
  106. $amount = ($fxinvamount) ? $fxtax * $amount{fxamount}{$i} / $fxinvamount : 0;
  107. $amount{fxamount}{$i} -= $amount;
  108. }
  109. # multiply by exchangerate
  110. $amount = $amount{fxamount}{$i} * $form->{exchangerate};
  111. $amount{amount}{$i} = $form->round_amount($amount - $diff, 2);
  112. $diff = $amount{amount}{$i} - ($amount - $diff);
  113. ($null, $project_id) = split /--/, $form->{"projectnumber_$i"};
  114. $project_id ||= 'NULL';
  115. ($accno) = split /--/, $form->{"${ARAP}_amount_$i"};
  116. if ($keepcleared) {
  117. $cleared = ($form->{"cleared_$i"}) ? 1 : 0;
  118. }
  119. push @{ $form->{acc_trans}{lineitems} }, {
  120. accno => $accno,
  121. amount => $amount{fxamount}{$i},
  122. project_id => $project_id,
  123. description => $form->{"description_$i"},
  124. cleared => $cleared,
  125. fx_transaction => 0 };
  126. if ($form->{currency} ne $form->{defaultcurrency}) {
  127. $amount = $amount{amount}{$i} - $amount{fxamount}{$i};
  128. push @{ $form->{acc_trans}{lineitems} }, {
  129. accno => $accno,
  130. amount => $amount,
  131. project_id => $project_id,
  132. description => $form->{"description_$i"},
  133. cleared => $cleared,
  134. fx_transaction => 1 };
  135. }
  136. }
  137. }
  138. my $invnetamount = 0;
  139. for (@{ $form->{acc_trans}{lineitems} }) { $invnetamount += $_->{amount} }
  140. my $invamount = $invnetamount + $tax;
  141. # adjust paidaccounts if there is no date in the last row
  142. $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
  143. my $paid = 0;
  144. my $fxamount;
  145. $diff = 0;
  146. # add payments
  147. for $i (1 .. $form->{paidaccounts}) {
  148. $fxamount = $form->parse_amount($myconfig, $form->{"paid_$i"});
  149. if ($fxamount) {
  150. $paid += $fxamount;
  151. $paidamount = $fxamount * $form->{exchangerate};
  152. $amount = $form->round_amount($paidamount - $diff, 2);
  153. $diff = $amount - ($paidamount - $diff);
  154. $form->{datepaid} = $form->{"datepaid_$i"};
  155. $paid{fxamount}{$i} = $fxamount;
  156. $paid{amount}{$i} = $amount;
  157. }
  158. }
  159. $fxinvamount += $fxtax unless $form->{taxincluded};
  160. $fxinvamount = $form->round_amount($fxinvamount, 2);
  161. $invamount = $form->round_amount($invamount, 2);
  162. $paid = $form->round_amount($paid, 2);
  163. $paid = ($fxinvamount == $paid) ? $invamount : $form->round_amount($paid * $form->{exchangerate}, 2);
  164. $query = q|SELECT fxgain_accno_id, fxloss_accno_id
  165. FROM defaults|;
  166. my ($fxgain_accno_id, $fxloss_accno_id) = $dbh->selectrow_array($query);
  167. ($null, $form->{employee_id}) = split /--/, $form->{employee};
  168. unless ($form->{employee_id}) {
  169. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
  170. }
  171. # check if id really exists
  172. if ($form->{id}) {
  173. $keepcleared = 1;
  174. $query = qq|SELECT id FROM $table
  175. WHERE id = $form->{id}|;
  176. if ($dbh->selectrow_array($query)) {
  177. # delete detail records
  178. $query = qq|DELETE FROM acc_trans
  179. WHERE trans_id = $form->{id}|;
  180. $dbh->do($query) || $form->dberror($query);
  181. }
  182. } else {
  183. my $uid = localtime;
  184. $uid .= "$$";
  185. $query = qq|INSERT INTO $table (invnumber)
  186. VALUES ('$uid')|;
  187. $dbh->do($query) || $form->dberror($query);
  188. $query = qq|SELECT id FROM $table
  189. WHERE invnumber = '$uid'|;
  190. ($form->{id}) = $dbh->selectrow_array($query);
  191. }
  192. # record last payment date in ar/ap table
  193. $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
  194. my $datepaid = ($paid) ? qq|'$form->{datepaid}'| : 'NULL';
  195. $form->{invnumber} = $form->update_defaults($myconfig, $invnumber) unless $form->{invnumber};
  196. $query = qq|UPDATE $table SET invnumber = |.$dbh->quote($form->{invnumber}).qq|,
  197. ordnumber = |.$dbh->quote($form->{ordnumber}).qq|,
  198. transdate = '$form->{transdate}',
  199. $form->{vc}_id = $form->{"$form->{vc}_id"},
  200. taxincluded = '$form->{taxincluded}',
  201. amount = $invamount,
  202. duedate = '$form->{duedate}',
  203. paid = $paid,
  204. datepaid = $datepaid,
  205. netamount = $invnetamount,
  206. curr = '$form->{currency}',
  207. notes = |.$dbh->quote($form->{notes}).qq|,
  208. department_id = $form->{department_id},
  209. employee_id = $form->{employee_id},
  210. ponumber = |.$dbh->quote($form->{ponumber}).qq|
  211. WHERE id = $form->{id}|;
  212. $dbh->do($query) || $form->dberror($query);
  213. # update exchangerate
  214. my $buy = $form->{exchangerate};
  215. my $sell = 0;
  216. if ($form->{vc} eq 'vendor') {
  217. $buy = 0;
  218. $sell = $form->{exchangerate};
  219. }
  220. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  221. $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $buy, $sell);
  222. }
  223. my $ref;
  224. # add individual transactions
  225. foreach $ref (@{ $form->{acc_trans}{lineitems} }) {
  226. # insert detail records in acc_trans
  227. if ($ref->{amount}) {
  228. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
  229. project_id, memo, fx_transaction, cleared)
  230. VALUES ($form->{id}, (SELECT id FROM chart
  231. WHERE accno = '$ref->{accno}'),
  232. $ref->{amount} * $ml, '$form->{transdate}',
  233. $ref->{project_id}, |.$dbh->quote($ref->{description}).qq|,
  234. '$ref->{fx_transaction}', '$ref->{cleared}')|;
  235. $dbh->do($query) || $form->dberror($query);
  236. }
  237. }
  238. # save taxes
  239. foreach $ref (@{ $form->{acc_trans}{taxes} }) {
  240. if ($ref->{amount}) {
  241. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  242. transdate, fx_transaction)
  243. VALUES ($form->{id},
  244. (SELECT id FROM chart
  245. WHERE accno = '$ref->{accno}'),
  246. $ref->{amount} * $ml, '$form->{transdate}',
  247. '$ref->{fx_transaction}')|;
  248. $dbh->do($query) || $form->dberror($query);
  249. }
  250. }
  251. my $arap;
  252. # record ar/ap
  253. if (($arap = $invamount)) {
  254. ($accno) = split /--/, $form->{$ARAP};
  255. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate)
  256. VALUES ($form->{id},
  257. (SELECT id FROM chart
  258. WHERE accno = '$accno'),
  259. $invamount * -1 * $ml, '$form->{transdate}')|;
  260. $dbh->do($query) || $form->dberror($query);
  261. }
  262. # if there is no amount force ar/ap
  263. if ($fxinvamount == 0) {
  264. $arap = 1;
  265. }
  266. my $exchangerate;
  267. # add paid transactions
  268. for $i (1 .. $form->{paidaccounts}) {
  269. if ($paid{fxamount}{$i}) {
  270. ($accno) = split(/--/, $form->{"${ARAP}_paid_$i"});
  271. $form->{"datepaid_$i"} = $form->{transdate} unless ($form->{"datepaid_$i"});
  272. $exchangerate = 0;
  273. if ($form->{currency} eq $form->{defaultcurrency}) {
  274. $form->{"exchangerate_$i"} = 1;
  275. } else {
  276. $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, $buysell);
  277. $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
  278. }
  279. # if there is no amount
  280. if ($fxinvamount == 0) {
  281. $form->{exchangerate} = $form->{"exchangerate_$i"};
  282. }
  283. # ar/ap amount
  284. if ($arap) {
  285. ($accno) = split /--/, $form->{$ARAP};
  286. # add ar/ap
  287. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,transdate)
  288. VALUES ($form->{id}, (SELECT id FROM chart
  289. WHERE accno = '$accno'),
  290. $paid{amount}{$i} * $ml, '$form->{"datepaid_$i"}')|;
  291. $dbh->do($query) || $form->dberror($query);
  292. }
  293. $arap = $paid{amount}{$i};
  294. # add payment
  295. if ($paid{fxamount}{$i}) {
  296. ($accno) = split /--/, $form->{"${ARAP}_paid_$i"};
  297. my $cleared = ($form->{"cleared_$i"}) ? 1 : 0;
  298. $amount = $paid{fxamount}{$i};
  299. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  300. transdate, source, memo, cleared)
  301. VALUES ($form->{id}, (SELECT id FROM chart
  302. WHERE accno = '$accno'),
  303. $amount * -1 * $ml, '$form->{"datepaid_$i"}', |
  304. .$dbh->quote($form->{"source_$i"}).qq|, |
  305. .$dbh->quote($form->{"memo_$i"}).qq|, '$cleared')|;
  306. $dbh->do($query) || $form->dberror($query);
  307. if ($form->{currency} ne $form->{defaultcurrency}) {
  308. # exchangerate gain/loss
  309. $amount = ($form->round_amount($paid{fxamount}{$i} * $form->{exchangerate},2) - $form->round_amount($paid{fxamount}{$i} * $form->{"exchangerate_$i"},2)) * -1;
  310. if ($amount) {
  311. my $accno_id = (($amount * $ml) > 0) ? $fxgain_accno_id : $fxloss_accno_id;
  312. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  313. transdate, fx_transaction, cleared)
  314. VALUES ($form->{id}, $accno_id,
  315. $amount * $ml, '$form->{"datepaid_$i"}', '1',
  316. '$cleared')|;
  317. $dbh->do($query) || $form->dberror($query);
  318. }
  319. # exchangerate difference
  320. $amount = $paid{amount}{$i} - $paid{fxamount}{$i} + $amount;
  321. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  322. transdate, fx_transaction, cleared, source)
  323. VALUES ($form->{id}, (SELECT id FROM chart
  324. WHERE accno = '$accno'),
  325. $amount * -1 * $ml, '$form->{"datepaid_$i"}', '1',
  326. '$cleared', |
  327. .$dbh->quote($form->{"source_$i"}).qq|)|;
  328. $dbh->do($query) || $form->dberror($query);
  329. }
  330. # update exchangerate record
  331. $buy = $form->{"exchangerate_$i"};
  332. $sell = 0;
  333. if ($form->{vc} eq 'vendor') {
  334. $buy = 0;
  335. $sell = $form->{"exchangerate_$i"};
  336. }
  337. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  338. $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $buy, $sell);
  339. }
  340. }
  341. }
  342. }
  343. # save printed and queued
  344. $form->save_status($dbh);
  345. my %audittrail = ( tablename => $table,
  346. reference => $form->{invnumber},
  347. formname => 'transaction',
  348. action => 'posted',
  349. id => $form->{id} );
  350. $form->audittrail($dbh, "", \%audittrail);
  351. $form->save_recurring($dbh, $myconfig);
  352. my $rc = $dbh->commit;
  353. $dbh->disconnect;
  354. $rc;
  355. }
  356. sub delete_transaction {
  357. my ($self, $myconfig, $form) = @_;