/doc/bugs/aggregate_plugin_errors/

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