summaryrefslogtreecommitdiff
path: root/LedgerSMB/AA.pm
blob: d624eaa0ba3bae967861c5b34012c26b71b0a460 (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. use LedgerSMB::Sysconfig;
  37. sub post_transaction {
  38. my ($self, $myconfig, $form) = @_;
  39. # connect to database
  40. my $dbh = $form->{dbh};
  41. my $query;
  42. my $sth;
  43. my $null;
  44. ($null, $form->{department_id}) = split(/--/, $form->{department});
  45. $form->{department_id} *= 1;
  46. my $ml = 1;
  47. my $table = 'ar';
  48. my $buysell = 'buy';
  49. my $ARAP = 'AR';
  50. my $invnumber = "sinumber";
  51. my $keepcleared;
  52. if ($form->{vc} eq 'vendor') {
  53. $table = 'ap';
  54. $buysell = 'sell';
  55. $ARAP = 'AP';
  56. $ml = -1;
  57. $invnumber = "vinumber";
  58. }
  59. if ($form->{currency} eq $form->{defaultcurrency}) {
  60. $form->{exchangerate} = 1;
  61. } else {
  62. $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, $buysell);
  63. $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
  64. }
  65. my @taxaccounts = split / /, $form->{taxaccounts};
  66. my $tax = 0;
  67. my $fxtax = 0;
  68. my $amount;
  69. my $diff;
  70. my %tax = ();
  71. my $accno;
  72. # add taxes
  73. foreach $accno (@taxaccounts) {
  74. $fxtax += $tax{fxamount}{$accno} = $form->parse_amount($myconfig, $form->{"tax_$accno"});
  75. $tax += $tax{fxamount}{$accno};
  76. push @{ $form->{acc_trans}{taxes} }, {
  77. accno => $accno,
  78. amount => $tax{fxamount}{$accno},
  79. project_id => 'NULL',
  80. fx_transaction => 0 };
  81. $amount = $tax{fxamount}{$accno} * $form->{exchangerate};
  82. $tax{amount}{$accno} = $form->round_amount($amount - $diff, 2);
  83. $diff = $tax{amount}{$accno} - ($amount - $diff);
  84. $amount = $tax{amount}{$accno} - $tax{fxamount}{$accno};
  85. $tax += $amount;
  86. if ($form->{currency} ne $form->{defaultcurrency}) {
  87. push @{ $form->{acc_trans}{taxes} }, {
  88. accno => $accno,
  89. amount => $amount,
  90. project_id => 'NULL',
  91. fx_transaction => 1 };
  92. }
  93. }
  94. my %amount = ();
  95. my $fxinvamount = 0;
  96. for (1 .. $form->{rowcount}) {
  97. $fxinvamount += $amount{fxamount}{$_} = $form->parse_amount($myconfig, $form->{"amount_$_"})
  98. }
  99. $form->{taxincluded} *= 1;
  100. my $i;
  101. my $project_id;
  102. my $cleared = 0;
  103. $diff = 0;
  104. # deduct tax from amounts if tax included
  105. for $i (1 .. $form->{rowcount}) {
  106. if ($amount{fxamount}{$i}) {
  107. if ($form->{taxincluded}) {
  108. $amount = ($fxinvamount) ? $fxtax * $amount{fxamount}{$i} / $fxinvamount : 0;
  109. $amount{fxamount}{$i} -= $amount;
  110. }
  111. # multiply by exchangerate
  112. $amount = $amount{fxamount}{$i} * $form->{exchangerate};
  113. $amount{amount}{$i} = $form->round_amount($amount - $diff, 2);
  114. $diff = $amount{amount}{$i} - ($amount - $diff);
  115. ($null, $project_id) = split /--/, $form->{"projectnumber_$i"};
  116. $project_id ||= 'NULL';
  117. ($accno) = split /--/, $form->{"${ARAP}_amount_$i"};
  118. if ($keepcleared) {
  119. $cleared = ($form->{"cleared_$i"}) ? 1 : 0;
  120. }
  121. push @{ $form->{acc_trans}{lineitems} }, {
  122. accno => $accno,
  123. amount => $amount{fxamount}{$i},
  124. project_id => $project_id,
  125. description => $form->{"description_$i"},
  126. cleared => $cleared,
  127. fx_transaction => 0 };
  128. if ($form->{currency} ne $form->{defaultcurrency}) {
  129. $amount = $amount{amount}{$i} - $amount{fxamount}{$i};
  130. push @{ $form->{acc_trans}{lineitems} }, {
  131. accno => $accno,
  132. amount => $amount,
  133. project_id => $project_id,
  134. description => $form->{"description_$i"},
  135. cleared => $cleared,
  136. fx_transaction => 1 };
  137. }
  138. }
  139. }
  140. my $invnetamount = 0;
  141. for (@{ $form->{acc_trans}{lineitems} }) { $invnetamount += $_->{amount} }
  142. my $invamount = $invnetamount + $tax;
  143. # adjust paidaccounts if there is no date in the last row
  144. $form->{paidaccounts}--
  145. unless ($form->{"datepaid_$form->{paidaccounts}"});
  146. if ($form->{vc} ne "customer"){
  147. $form->{vc} = "vendor";
  148. }
  149. my $paid = 0;
  150. my $fxamount;
  151. $diff = 0;
  152. # add payments
  153. for $i (1 .. $form->{paidaccounts}) {
  154. $fxamount = $form->parse_amount($myconfig, $form->{"paid_$i"});
  155. if ($fxamount) {
  156. $paid += $fxamount;
  157. $paidamount = $fxamount * $form->{exchangerate};
  158. $amount = $form->round_amount($paidamount - $diff, 2);
  159. $diff = $amount - ($paidamount - $diff);
  160. $form->{datepaid} = $form->{"datepaid_$i"};
  161. $paid{fxamount}{$i} = $fxamount;
  162. $paid{amount}{$i} = $amount;
  163. }
  164. }
  165. $fxinvamount += $fxtax unless $form->{taxincluded};
  166. $fxinvamount = $form->round_amount($fxinvamount, 2);
  167. $invamount = $form->round_amount($invamount, 2);
  168. $paid = $form->round_amount($paid, 2);
  169. $paid = ($fxinvamount == $paid)
  170. ? $invamount
  171. : $form->round_amount($paid * $form->{exchangerate}, 2);
  172. $query = q|
  173. SELECT (SELECT value FROM defaults
  174. WHERE setting_key = 'fxgain_accno_id'),
  175. (SELECT value FROM defaults
  176. WHERE setting_key = 'fxloss_accno_id')|;
  177. my ($fxgain_accno_id, $fxloss_accno_id) = $dbh->selectrow_array($query);
  178. ($null, $form->{employee_id}) = split /--/, $form->{employee};
  179. unless ($form->{employee_id}) {
  180. ($form->{employee}, $form->{employee_id}) =
  181. $form->get_employee($dbh);
  182. }
  183. # check if id really exists
  184. if ($form->{id}) {
  185. my $id = $dbh->quote($form->{id});
  186. $keepcleared = 1;
  187. $query = qq|
  188. SELECT id
  189. FROM $table
  190. WHERE id = $id|;
  191. if ($dbh->selectrow_array($query)) {
  192. # delete detail records
  193. $query = qq|
  194. DELETE FROM acc_trans
  195. WHERE trans_id = $id|;
  196. $dbh->do($query) || $form->dberror($query);
  197. }
  198. } else {
  199. my $uid = localtime;
  200. $uid .= "$$";
  201. $query = qq|
  202. INSERT INTO $table (invnumber)
  203. VALUES ('$uid')|;
  204. $dbh->do($query) || $form->dberror($query);
  205. $query = qq|
  206. SELECT id FROM $table
  207. WHERE invnumber = '$uid'|;
  208. ($form->{id}) = $dbh->selectrow_array($query);
  209. }
  210. # record last payment date in ar/ap table
  211. $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
  212. my $datepaid = ($paid) ? qq|'$form->{datepaid}'| : 'NULL';
  213. $form->{invnumber} = $form->update_defaults($myconfig, $invnumber) unless $form->{invnumber};
  214. $query = qq|
  215. UPDATE $table
  216. SET invnumber = ?,
  217. ordnumber = ?,
  218. transdate = ?,
  219. $form->{vc}_id = ?,
  220. taxincluded = ?,
  221. amount = ?,
  222. duedate = ?,
  223. paid = ?,
  224. datepaid = ?,
  225. netamount = ?,
  226. curr = ?,
  227. notes = ?,
  228. department_id = ?,
  229. employee_id = ?,
  230. ponumber = ?
  231. WHERE id = ?
  232. |;
  233. my @queryargs = ($form->{invnumber}, $form->{ordnumber},
  234. $form->{transdate}, $form->{"$form->{vc}_id"},
  235. $form->{taxincluded}, $invamount, $form->{duedate}, $paid,
  236. $datepaid, $invnetamout, $form->{currency}, $form->{notes},
  237. $form->{department_id}, $form->{employee_id},
  238. $form->{ponumber}, $form->{id});
  239. $dbh->prepare($query)->execute(@queryargs) || $form->dberror($query);
  240. @queries = $form->run_custom_queries($table, 'INSERT');
  241. # update exchangerate
  242. my $buy = $form->{exchangerate};
  243. my $sell = 0;
  244. if ($form->{vc} eq 'vendor') {
  245. $buy = 0;
  246. $sell = $form->{exchangerate};
  247. }
  248. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  249. $form->update_exchangerate(
  250. $dbh, $form->{currency}, $form->{transdate},
  251. $buy, $sell);
  252. }
  253. my $ref;
  254. # add individual transactions
  255. foreach $ref (@{ $form->{acc_trans}{lineitems} }) {
  256. # insert detail records in acc_trans
  257. if ($ref->{amount}) {
  258. $query = qq|
  259. INSERT INTO acc_trans
  260. (trans_id, chart_id, amount,
  261. transdate, project_id, memo,
  262. fx_transaction, cleared)
  263. VALUES (?, (SELECT id FROM chart
  264. WHERE accno = ?),
  265. ? * ?, ?, ?, ?, ?, ?)|;
  266. @queryargs = ($form->{id}, $ref->{accno},
  267. $ref->{amount}, $ml, $form->{transdate},
  268. $ref->{project_id}, $ref->{description},
  269. $ref->{fx_transaction}, $ref->{cleared});
  270. $dbh->prepare($query)->execute(@queryargs)
  271. || $form->dberror($query);
  272. }
  273. }
  274. # save taxes
  275. foreach $ref (@{ $form->{acc_trans}{taxes} }) {
  276. if ($ref->{amount}) {
  277. $query = qq|
  278. INSERT INTO acc_trans
  279. (trans_id, chart_id, amount,
  280. transdate, fx_transaction)
  281. VALUES (?, (SELECT id FROM chart
  282. WHERE accno = ?),
  283. ? * ?, ?, ?)|;
  284. @queryargs = ($form->{id}, $ref->{accno},
  285. $ref->{amount}, $ml, $form->{transdate},
  286. $ref->{fx_transaction});
  287. $dbh->prepare($query)->execute(@queryargs)
  288. || $form->dberror($query);
  289. }
  290. }
  291. my $arap;
  292. # record ar/ap
  293. if (($arap = $invamount)) {
  294. ($accno) = split /--/, $form->{$ARAP};
  295. $query = qq|
  296. INSERT INTO acc_trans
  297. (trans_id, chart_id, amount, transdate)
  298. VALUES (?, (SELECT id FROM chart
  299. WHERE accno = '?'),
  300. ? * -1 * $ml, ?)|;
  301. @queryargs = ($form->{id}, $accno, $invamount,
  302. $form->{transdate});
  303. $dbh->prepare($query)->execute(@queryargs)
  304. || $form->dberror($query);
  305. }
  306. # if there is no amount force ar/ap
  307. if ($fxinvamount == 0) {
  308. $arap = 1;
  309. }
  310. my $exchangerate;
  311. # add paid transactions
  312. for $i (1 .. $form->{paidaccounts}) {
  313. if ($paid{fxamount}{$i}) {
  314. ($accno) = split(/--/, $form->{"${ARAP}_paid_$i"});
  315. $form->{"datepaid_$i"} = $form->{transdate} unless ($form->{"datepaid_$i"});
  316. $exchangerate = 0;
  317. if ($form->{currency} eq $form->{defaultcurrency}) {
  318. $form->{"exchangerate_$i"} = 1;
  319. } else {
  320. $exchangerate = $form->check_exchangerate(
  321. $myconfig, $form->{currency},
  322. $form->{"datepaid_$i"}, $buysell);
  323. $form->{"exchangerate_$i"} = ($exchangerate)
  324. ? $exchangerate
  325. : $form->parse_amount(
  326. $myconfig,
  327. $form->{"exchangerate_$i"});
  328. }
  329. # if there is no amount
  330. if ($fxinvamount == 0) {
  331. $form->{exchangerate} =
  332. $form->{"exchangerate_$i"};
  333. }
  334. # ar/ap amount
  335. if ($arap) {
  336. ($accno) = split /--/, $form->{$ARAP};
  337. # add ar/ap
  338. $query = qq|
  339. INSERT INTO acc_trans
  340. (trans_id, chart_id,
  341. amount,transdate)
  342. VALUES (?, (SELECT id FROM chart
  343. WHERE accno = ?),
  344. ? * $ml, ?)|;
  345. @queryargs = ($form->{id}, $paid{amount}{$i},
  346. $form->{"datepaid_$i"});
  347. $dbh->prepare($query)->execute(@queryargs)
  348. || $form->dberror($query);
  349. }
  350. $arap = $paid{amount}{$i};
  351. # add payment
  352. if ($paid{fxamount}{$i}) {
  353. ($accno) = split /--/, $form->{"${ARAP}_paid_$i"};
  354. my $cleared = ($form->{"cleared_$i"}) ? 1 : 0;
  355. $amount = $paid{fxamount}{$i};
  356. $query = qq|
  357. INSERT INTO acc_trans
  358. (trans_id, chart_id, amount,
  359. transdate, source, memo,
  360. cleared)
  361. VALUES (?, (SELECT id FROM chart
  362. WHERE accno = ?),
  363. ? * -1 * $ml, ?, ?, ?, ?)|;
  364. @queryargs = ($form->{id}, $accno, $amount,
  365. $form->{"datepaid_$i"},
  366. $form->{"source_$i"},
  367. $form->{"memo_$i"},
  368. $cleared);
  369. $dbh->prepare($query)->execute(@queryargs)
  370. || $form->dberror($query);
  371. if ($form->{currency}
  372. ne $form->{defaultcurrency}) {
  373. # exchangerate gain/loss
  374. $amount = ($form->round_amount(
  375. $paid{fxamount}{$i}
  376. * $form->{exchangerate},2) -
  377. $form->round_amount(
  378. $paid{fxamount}{$i}
  379. * $form->{"exchangerate_$i"},
  380. 2)) * -1;
  381. if ($amount) {
  382. my $accno_id = (($amount * $ml) > 0) ? $fxgain_accno_id : $fxloss_accno_id;
  383. $query = qq|
  384. INSERT INTO acc_trans
  385. (trans_id,
  386. chart_id,
  387. amount,
  388. transdate,
  389. fx_transaction,
  390. cleared)
  391. VALUES (?, ?,
  392. ? * $ml,
  393. ?, '1', ?)|;
  394. @queryargs = ($form->{id},
  395. $accno_id, $amount,
  396. $form->{"datepaid_$i"},
  397. $cleared);
  398. $sth = $dbh->prepare($query);
  399. $sth->execute(@queryargs)
  400. ||
  401. $form->dberror($query);
  402. }
  403. # exchangerate difference
  404. $amount = $paid{amount}{$i} - $paid{fxamount}{$i} + $amount;
  405. $query = qq|
  406. INSERT INTO acc_trans
  407. (trans_id, chart_id,
  408. amount,
  409. transdate,
  410. fx_transaction,
  411. cleared, source)
  412. VALUES (?, (SELECT id
  413. FROM chart
  414. WHERE accno
  415. = ?),
  416. ? * -1 * $ml, ?,
  417. '1', ?, ?)|;
  418. @queryargs = ($form->{id}, $accno,
  419. $amount, $form->{"datepaid_$i"},
  420. $cleared, $form->{"source_$i"});
  421. $sth = $dbh->prepare($query) ;
  422. $sth->execute(@queryargs)
  423. || $form->dberror($query);
  424. }
  425. # update exchangerate record
  426. $buy = $form->{"exchangerate_$i"};
  427. $sell = 0;
  428. if ($form->{vc} eq 'vendor') {
  429. $buy = 0;
  430. $sell = $form->{"exchangerate_$i"};
  431. }
  432. if (($form->{currency} ne
  433. $form->{defaultcurrency}) && !$exchangerate) {
  434. $form->update_exchangerate(
  435. $dbh, $form->{currency},
  436. $form->{"datepaid_$i"}, $buy,
  437. $sell);
  438. }
  439. }
  440. }
  441. }
  442. # save printed and queued
  443. $form->save_status($dbh);
  444. my %audittrail = ( tablename => $table,
  445. reference => $form->{invnumber},
  446. formname => 'transaction',
  447. action => 'posted',
  448. id => $form->{id} );
  449. $form->audittrail($dbh, "", \%audittrail);
  450. $form->save_recurring($dbh, $myconfig);
  451. my $rc = $dbh->commit;
  452. $rc;
  453. }
  454. sub delete_transaction {
  455. my ($self, $myconfig, $form) = @_;
  456. # connect to database, turn AutoCommit off
  457. my $dbh = $form->{dbh};
  458. my $table = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
  459. my %audittrail = ( tablename => $table,
  460. reference => $form->{invnumber},
  461. formname => 'transaction',
  462. action => 'deleted',
  463. id => $form->{id} );
  464. $form->audittrail($dbh, "", \%audittrail);
  465. my $query = qq|DELETE FROM $table WHERE id = $form->{id}|;
  466. $dbh->do($query) || $form->dberror($query);
  467. $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
  468. $dbh->prepare($query)->execute($form->{id}) || $form->dberror($query);
  469. # get spool files
  470. $query = qq|SELECT spoolfile
  471. FROM status
  472. WHERE trans_id = ?
  473. AND spoolfile IS NOT NULL|;
  474. my $sth = $dbh->prepare($query);
  475. $sth->execute($form->{id}) || $form->dberror($query);
  476. my $spoolfile;
  477. my @spoolfiles = ();
  478. while (($spoolfile) = $sth->fetchrow_array) {
  479. push @spoolfiles, $spoolfile;
  480. }
  481. $sth->finish;
  482. $query = qq|DELETE FROM status WHERE trans_id = ?|;
  483. $dbh->prepare($query)->execute($form->{id}) || $form->dberror($query);
  484. # commit
  485. my $rc = $dbh->commit;
  486. if ($rc) {
  487. foreach $spoolfile (@spoolfiles) {
  488. unlink "${LedgerSMB::Sysconfig::spool}/$spoolfile" if $spoolfile;
  489. }
  490. }
  491. $rc;
  492. }
  493. sub transactions {
  494. my ($self, $myconfig, $form) = @_;
  495. # connect to database
  496. my $dbh = $form->{dbh};
  497. my $null;
  498. my $var;
  499. my $paid = "a.paid";
  500. my $ml = 1;
  501. my $ARAP = 'AR';
  502. my $table = 'ar';
  503. my $buysell = 'buy';
  504. my $acc_trans_join;
  505. my $acc_trans_flds;
  506. if ($form->{vc} eq 'vendor') {
  507. $ml = -1;
  508. $ARAP = 'AP';
  509. $table = 'ap';
  510. $buysell = 'sell';
  511. }
  512. ($form->{transdatefrom}, $form->{transdateto}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  513. my @paidargs = ();
  514. if ($form->{outstanding}) {
  515. $paid = qq|
  516. SELECT SUM(ac.amount) * -1 * $ml
  517. FROM acc_trans ac
  518. JOIN chart c ON (c.id = ac.chart_id)
  519. WHERE ac.trans_id = a.id
  520. AND (c.link LIKE '%${ARAP}_paid%'
  521. OR c.link = '')|;
  522. if ($form->{transdateto}){
  523. $paid .= qq|
  524. AND ac.transdate <= ?|;
  525. push @paidargs, $form->{transdateto};
  526. }
  527. $form->{summary} = 1;
  528. }
  529. if (!$form->{summary}) {
  530. $acc_trans_flds = qq|
  531. , c.accno, ac.source,
  532. pr.projectnumber, ac.memo AS description,
  533. ac.amount AS linetotal,
  534. i.description AS linedescription|;
  535. $acc_trans_join = qq|
  536. JOIN acc_trans ac ON (a.id = ac.trans_id)
  537. JOIN chart c ON (c.id = ac.chart_id)
  538. LEFT JOIN project pr ON (pr.id = ac.project_id)
  539. LEFT JOIN invoice i ON (i.id = ac.invoice_id)|;
  540. }
  541. my $query = qq|
  542. SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
  543. a.duedate, a.netamount, a.amount, ($paid) AS paid,
  544. a.invoice, a.datepaid, a.terms, a.notes,
  545. a.shipvia, a.shippingpoint, e.name AS employee,
  546. vc.name,
  547. a.$form->{vc}_id, a.till, m.name AS manager, a.curr,
  548. ex.$buysell AS exchangerate,
  549. d.description AS department,
  550. a.ponumber $acc_trans_flds
  551. FROM $table a
  552. JOIN $form->{vc} vc ON (a.$form->{vc}_id = vc.id)
  553. LEFT JOIN employee e ON (a.employee_id = e.id)
  554. LEFT JOIN employee m ON (e.managerid = m.id)
  555. LEFT JOIN exchangerate ex ON (ex.curr = a.curr
  556. AND ex.transdate = a.transdate)
  557. LEFT JOIN department d ON (a.department_id = d.id)
  558. $acc_trans_join|;
  559. my %ordinal = ( id => 1,
  560. invnumber => 2,
  561. ordnumber => 3,
  562. transdate => 4,
  563. duedate => 5,
  564. datepaid => 10,
  565. shipvia => 13,
  566. shippingpoint => 14,
  567. employee => 15,
  568. name => 16,
  569. manager => 19,
  570. curr => 20,
  571. department => 22,
  572. ponumber => 23,
  573. accno => 24,
  574. source => 25,
  575. project => 26,
  576. description => 27);
  577. my @a = (transdate, invnumber, name);
  578. push @a, "employee" if $form->{l_employee};
  579. push @a, "manager" if $form->{l_manager};
  580. my $sortorder = $form->sort_order(\@a, \%ordinal);
  581. my $where = "1 = 1";
  582. if ($form->{"$form->{vc}_id"}) {
  583. $where .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  584. } else {
  585. if ($form->{$form->{vc}}) {
  586. $var = $dbh->quote(
  587. $form->like(lc $form->{$form->{vc}}));
  588. $where .= " AND lower(vc.name) LIKE $var";
  589. }
  590. }
  591. for (qw(department employee)) {
  592. if ($form->{$_}) {
  593. ($null, $var) = split /--/, $form->{$_};
  594. $var = $dbh->quote($var);
  595. $where .= " AND a.${_}_id = $var";
  596. }
  597. }
  598. for (qw(invnumber ordnumber)) {
  599. if ($form->{$_}) {
  600. $var = $dbh->quote($form->like(lc $form->{$_}));
  601. $where .= " AND lower(a.$_) LIKE $var";
  602. $form->{open} = $form->{closed} = 0;
  603. }
  604. }
  605. if ($form->{partsid}){
  606. my $parts_id = $dbh->quote($form->{parts_id});
  607. $where .= " AND a.id IN (select trans_id FROM invoice
  608. WHERE parts_id = $partsid)";
  609. }
  610. for (qw(ponumber shipvia notes)) {
  611. if ($form->{$_}) {
  612. $var = $dbh->quote($form->like(lc $form->{$_}));
  613. $where .= " AND lower(a.$_) LIKE $var";
  614. }
  615. }
  616. if ($form->{description}) {
  617. if ($acc_trans_flds) {
  618. $var = $dbh->quote(
  619. $form->like(lc $form->{description})
  620. );
  621. $where .= " AND lower(ac.memo) LIKE $var
  622. OR lower(i.description) LIKE $var";
  623. } else {
  624. $where .= " AND a.id = 0";
  625. }
  626. }
  627. if ($form->{source}) {
  628. if ($acc_trans_flds) {
  629. $var = $dbh->quote($form->like(lc $form->{source}));
  630. $where .= " AND lower(ac.source) LIKE $var";
  631. } else {
  632. $where .= " AND a.id = 0";
  633. }
  634. }
  635. my $transdatefrom = $dbh->quote($form->{transdatefrom});
  636. $where .= " AND a.transdate >= $transdatefrom"
  637. if $form->{transdatefrom};
  638. my $transdateto = $dbh->quote($form->{transdateto});
  639. $where .= " AND a.transdate <= $transdateto" if $form->{transdateto};
  640. if ($form->{open} || $form->{closed}) {
  641. unless ($form->{open} && $form->{closed}) {
  642. $where .= " AND a.amount != a.paid" if ($form->{open});
  643. $where .= " AND a.amount = a.paid" if ($form->{closed});
  644. }
  645. }
  646. if ($form->{till} ne "") {
  647. $where .= " AND a.invoice = '1'
  648. AND a.till = $form->{till}";
  649. if ($myconfig->{role} eq 'user') {
  650. my $login = $dbh->quote($form->{login});
  651. $where .= " AND e.login = $login";
  652. }
  653. }
  654. if ($form->{$ARAP}) {
  655. my ($accno) = split /--/, $form->{$ARAP};
  656. $accno = $dbh->quote($accno);
  657. $where .= qq|
  658. AND a.id IN (SELECT ac.trans_id
  659. FROM acc_trans ac
  660. JOIN chart c ON (c.id = ac.chart_id)
  661. WHERE a.id = ac.trans_id
  662. AND c.accno = $accno)|;
  663. }
  664. if ($form->{description}) {
  665. $var = $dbh->quote($form->like(lc $form->{description}));
  666. $where .= qq|
  667. AND (a.id IN (SELECT DISTINCT trans_id
  668. FROM acc_trans
  669. WHERE lower(memo) LIKE '$var')
  670. OR a.id IN
  671. (SELECT DISTINCT trans_id
  672. FROM invoice
  673. WHERE lower(description)
  674. LIKE '$var'))|;
  675. }
  676. $query .= "WHERE $where
  677. ORDER BY $sortorder";
  678. my $sth = $dbh->prepare($query);
  679. $sth->execute(@paidargs) || $form->dberror($query);
  680. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  681. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  682. if ($ref->{linetotal} <= 0) {
  683. $ref->{debit} = $ref->{linetotal} * -1;
  684. $ref->{credit} = 0;
  685. } else {
  686. $ref->{debit} = 0;
  687. $ref->{credit} = $ref->{linetotal};
  688. }
  689. if ($ref->{invoice}) {
  690. $ref->{description} ||= $ref->{linedescription};
  691. }
  692. if ($form->{outstanding}) {
  693. next if $form->round_amount($ref->{amount}, 2)
  694. == $form->round_amount($ref->{paid}, 2);
  695. }
  696. push @{ $form->{transactions} }, $ref;
  697. }
  698. $sth->finish;
  699. $dbh->commit;
  700. }
  701. # this is used in IS, IR to retrieve the name
  702. sub get_name {
  703. my ($self, $myconfig, $form) = @_;
  704. # sanitize $form->{vc}
  705. if ($form->{vc} ne 'customer'){
  706. $form->{vc} = 'vendor';
  707. } else {
  708. $form->{vc} = 'customer';
  709. }
  710. # connect to database
  711. my $dbh = $form->{dbh};
  712. my $dateformat = $myconfig->{dateformat};
  713. if ($myconfig->{dateformat} !~ /^y/) {
  714. my @a = split /\W/, $form->{transdate};
  715. $dateformat .= "yy" if (length $a[2] > 2);
  716. }
  717. if ($form->{transdate} !~ /\W/) {
  718. $dateformat = 'yyyymmdd';
  719. }
  720. my $duedate;
  721. $dateformat = $dbh->quote($dateformat);
  722. my $tdate = $dbh->quote($form->{transdate});
  723. $duedate = ($form->{transdate})
  724. ? "to_date($tdate, $dateformat)
  725. + c.terms"
  726. : "current_date + c.terms";
  727. $form->{"$form->{vc}_id"} *= 1;
  728. # get customer/vendor
  729. my $query = qq|
  730. SELECT c.name AS $form->{vc}, c.discount, c.creditlimit,
  731. c.terms, c.email, c.cc, c.bcc, c.taxincluded,
  732. c.address1, c.address2, c.city, c.state,
  733. c.zipcode, c.country, c.curr AS currency,
  734. c.language_code, $duedate AS duedate,
  735. c.notes AS intnotes,
  736. b.discount AS tradediscount,
  737. b.description AS business,
  738. e.name AS employee, e.id AS employee_id
  739. FROM $form->{vc} c
  740. LEFT JOIN business b ON (b.id = c.business_id)
  741. LEFT JOIN employee e ON (e.id = c.employee_id)
  742. WHERE c.id = ?|;
  743. @queryargs = ($form->{"$form->{vc}_id"});
  744. my $sth = $dbh->prepare($query);
  745. $sth->execute(@queryargs) || $form->dberror($query);
  746. $ref = $sth->fetchrow_hashref(NAME_lc);
  747. if ($form->{id}) {
  748. for (qw(currency employee employee_id intnotes)) {
  749. delete $ref->{$_};
  750. }
  751. }
  752. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  753. $sth->finish;
  754. my $buysell = ($form->{vc} eq 'customer') ? "buy" : "sell";
  755. # if no currency use defaultcurrency
  756. $form->{currency} =
  757. ($form->{currency})
  758. ? $form->{currency}
  759. : $form->{defaultcurrency};
  760. $form->{exchangerate} = 0
  761. if $form->{currency} eq $form->{defaultcurrency};
  762. if ($form->{transdate} && ($form->{currency}
  763. ne $form->{defaultcurrency})) {
  764. $form->{exchangerate} = $form->get_exchangerate(
  765. $dbh, $form->{currency}, $form->{transdate}, $buysell);
  766. }
  767. $form->{forex} = $form->{exchangerate};
  768. # if no employee, default to login
  769. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh)
  770. unless $form->{employee_id};
  771. my $arap = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
  772. my $ARAP = uc $arap;
  773. $form->{creditremaining} = $form->{creditlimit};
  774. $query = qq|
  775. SELECT SUM(amount - paid)
  776. FROM $arap
  777. WHERE $form->{vc}_id = ?|;
  778. $sth = $dbh->prepare($query);
  779. $sth->execute($form->{"$form->{vc}_id"})
  780. || $form->dberror($query);
  781. ($form->{creditremaining}) -= $sth->fetchrow_array;
  782. $sth->finish;
  783. if ($form->{vc} ne "customer"){
  784. $form->{vc} = 'vendor';
  785. }
  786. $query = qq|
  787. SELECT o.amount, (SELECT e.$buysell FROM exchangerate e
  788. WHERE e.curr = o.curr
  789. AND e.transdate = o.transdate)
  790. FROM oe o
  791. WHERE o.$form->{vc}_id = ?
  792. AND o.quotation = '0' AND o.closed = '0'|;
  793. $sth = $dbh->prepare($query);
  794. $sth->execute ($form->{"$form->{vc}_id"}) || $form->dberror($query);
  795. while (my ($amount, $exch) = $sth->fetchrow_array) {
  796. $exch = 1 unless $exch;
  797. $form->{creditremaining} -= $amount * $exch;
  798. }
  799. $sth->finish;
  800. # get shipto if we did not converted an order or invoice
  801. if (!$form->{shipto}) {
  802. for (
  803. qw(shiptoname shiptoaddress1 shiptoaddress2
  804. shiptocity shiptostate shiptozipcode
  805. shiptocountry shiptocontact shiptophone
  806. shiptofax shiptoemail)
  807. ) {
  808. delete $form->{$_}
  809. }
  810. ## needs fixing (SELECT *)
  811. $query = qq|
  812. SELECT *
  813. FROM shipto
  814. WHERE trans_id = $form->{"$form->{vc}_id"}|;
  815. $sth = $dbh->prepare($query);
  816. $sth->execute || $form->dberror($query);
  817. $ref = $sth->fetchrow_hashref(NAME_lc);
  818. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  819. $sth->finish;
  820. }
  821. # get taxes
  822. $query = qq|
  823. SELECT c.accno
  824. FROM chart c
  825. JOIN $form->{vc}tax ct ON (ct.chart_id = c.id)
  826. WHERE ct.$form->{vc}_id = ?|;
  827. $sth = $dbh->prepare($query);
  828. $sth->execute( $form->{"$form->{vc}_id"}) || $form->dberror($query);
  829. my %tax;
  830. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  831. $tax{$ref->{accno}} = 1;
  832. }
  833. $sth->finish;
  834. $transdate = $dbh->quote($form->{transdate});
  835. my $where = qq|AND (t.validto >= $transdate OR t.validto IS NULL)|
  836. if $form->{transdate};
  837. # get tax rates and description
  838. $query = qq|
  839. SELECT c.accno, c.description, t.rate, t.taxnumber
  840. FROM chart c
  841. JOIN tax t ON (c.id = t.chart_id)
  842. WHERE c.link LIKE '%${ARAP}_tax%'
  843. $where
  844. ORDER BY accno, validto|;
  845. $sth = $dbh->prepare($query);
  846. $sth->execute || $form->dberror($query);
  847. $form->{taxaccounts} = "";
  848. my %a = ();
  849. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  850. if ($tax{$ref->{accno}}) {
  851. if (not exists $a{$ref->{accno}}) {
  852. for (qw(rate description taxnumber)) {
  853. $form->{"$ref->{accno}_$_"} =
  854. $ref->{$_};
  855. }
  856. $form->{taxaccounts} .= "$ref->{accno} ";
  857. $a{$ref->{accno}} = 1;
  858. }
  859. }
  860. }
  861. $sth->finish;
  862. chop $form->{taxaccounts};
  863. # setup last accounts used for this customer/vendor
  864. if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
  865. $query = qq|
  866. SELECT c.accno, c.description, c.link, c.category,
  867. ac.project_id, p.projectnumber,
  868. a.department_id, d.description AS department
  869. FROM chart c
  870. JOIN acc_trans ac ON (ac.chart_id = c.id)
  871. JOIN $arap a ON (a.id = ac.trans_id)
  872. LEFT JOIN project p ON (ac.project_id = p.id)
  873. LEFT JOIN department d ON (d.id = a.department_id)
  874. WHERE a.$form->{vc}_id = ?
  875. AND a.id IN (SELECT max(id)
  876. FROM $arap
  877. WHERE $form->{vc}_id =
  878. ?)
  879. |;
  880. $sth = $dbh->prepare($query);
  881. $sth->execute($form->{"$form->{vc}_id"},
  882. $form->{"$form->{vc}_id"}) || $form->dberror($query);
  883. my $i = 0;
  884. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  885. $form->{department} = $ref->{department};
  886. $form->{department_id} = $ref->{department_id};
  887. if ($ref->{link} =~ /_amount/) {
  888. $i++;
  889. $form->{"$form->{ARAP}_amount_$i"} =
  890. "$ref->{accno}--$ref->{description}"
  891. if $ref->{accno};
  892. $form->{"projectnumber_$i"} =
  893. "$ref->{projectnumber}--" .
  894. "$ref->{project_id}"
  895. if $ref->{project_id};
  896. }
  897. if ($ref->{link} eq $form->{ARAP}) {
  898. $form->{$form->{ARAP}} =
  899. $form->{"$form->{ARAP}_1"} =
  900. "$ref->{accno}--".
  901. "$ref->{description}"
  902. if $ref->{accno};
  903. }
  904. }
  905. $sth->finish;
  906. $form->{rowcount} = $i if ($i && !$form->{type});
  907. }
  908. $dbh->commit;
  909. }
  910. 1;