summaryrefslogtreecommitdiff
path: root/LedgerSMB/AA.pm
blob: 63076ab18526f39a8f1f213f28a0f0224c21d99d (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->get_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"});
  418. $sth = $dbh->prepare($query) ;
  419. $sth->execute(@queryargs)
  420. || $form->dberror($query);
  421. }
  422. # update exchangerate record
  423. $buy = $form->{"exchangerate_$i"};
  424. $sell = 0;
  425. if ($form->{vc} eq 'vendor') {
  426. $buy = 0;
  427. $sell = $form->{"exchangerate_$i"};
  428. }
  429. if (($form->{currency} ne
  430. $form->{defaultcurrency}) && !$exchangerate) {
  431. $form->update_exchangerate(
  432. $dbh, $form->{currency},
  433. $form->{"datepaid_$i"}, $buy,
  434. $sell);
  435. }
  436. }
  437. }
  438. }
  439. # save printed and queued
  440. $form->save_status($dbh);
  441. my %audittrail = ( tablename => $table,
  442. reference => $form->{invnumber},
  443. formname => 'transaction',
  444. action => 'posted',
  445. id => $form->{id} );
  446. $form->audittrail($dbh, "", \%audittrail);
  447. $form->save_recurring($dbh, $myconfig);
  448. my $rc = $dbh->commit;
  449. $rc;
  450. }
  451. sub delete_transaction {
  452. my ($self, $myconfig, $form) = @_;
  453. # connect to database, turn AutoCommit off
  454. my $dbh = $form->dbconnect_noauto($myconfig);
  455. my $table = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
  456. my %audittrail = ( tablename => $table,
  457. reference => $form->{invnumber},
  458. formname => 'transaction',
  459. action => 'deleted',
  460. id => $form->{id} );
  461. $form->audittrail($dbh, "", \%audittrail);
  462. my $query = qq|DELETE FROM $table WHERE id = $form->{id}|;
  463. $dbh->do($query) || $form->dberror($query);
  464. $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
  465. $dbh->do($query) || $form->dberror($query);
  466. # get spool files
  467. $query = qq|SELECT spoolfile
  468. FROM status
  469. WHERE trans_id = $form->{id}
  470. AND spoolfile IS NOT NULL|;
  471. my $sth = $dbh->prepare($query);
  472. $sth->execute || $form->dberror($query);
  473. my $spoolfile;
  474. my @spoolfiles = ();
  475. while (($spoolfile) = $sth->fetchrow_array) {
  476. push @spoolfiles, $spoolfile;
  477. }
  478. $sth->finish;
  479. $query = qq|DELETE FROM status WHERE trans_id = $form->{id}|;
  480. $dbh->do($query) || $form->dberror($query);
  481. # commit
  482. my $rc = $dbh->commit;
  483. $dbh->disconnect;
  484. if ($rc) {
  485. foreach $spoolfile (@spoolfiles) {
  486. unlink "$spool/$spoolfile" if $spoolfile;
  487. }
  488. }
  489. $rc;
  490. }
  491. sub transactions {
  492. my ($self, $myconfig, $form) = @_;
  493. # connect to database
  494. my $dbh = $form->dbconnect($myconfig);
  495. my $null;
  496. my $var;
  497. my $paid = "a.paid";
  498. my $ml = 1;
  499. my $ARAP = 'AR';
  500. my $table = 'ar';
  501. my $buysell = 'buy';
  502. my $acc_trans_join;
  503. my $acc_trans_flds;
  504. if ($form->{vc} eq 'vendor') {
  505. $ml = -1;
  506. $ARAP = 'AP';
  507. $table = 'ap';
  508. $buysell = 'sell';
  509. }
  510. ($form->{transdatefrom}, $form->{transdateto}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  511. if ($form->{outstanding}) {
  512. $paid = qq|SELECT SUM(ac.amount) * -1 * $ml
  513. FROM acc_trans ac
  514. JOIN chart c ON (c.id = ac.chart_id)
  515. WHERE ac.trans_id = a.id
  516. AND (c.link LIKE '%${ARAP}_paid%' OR c.link = '')|;
  517. $paid .= qq|
  518. AND ac.transdate <= '$form->{transdateto}'| if $form->{transdateto};
  519. $form->{summary} = 1;
  520. }
  521. if (!$form->{summary}) {
  522. $acc_trans_flds = qq|, c.accno, ac.source,
  523. pr.projectnumber, ac.memo AS description,
  524. ac.amount AS linetotal,
  525. i.description AS linedescription|;
  526. $acc_trans_join = qq| JOIN acc_trans ac ON (a.id = ac.trans_id)
  527. JOIN chart c ON (c.id = ac.chart_id)
  528. LEFT JOIN project pr ON (pr.id = ac.project_id)
  529. LEFT JOIN invoice i ON (i.id = ac.invoice_id)|;
  530. }
  531. my $query = qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
  532. a.duedate, a.netamount, a.amount, ($paid) AS paid,
  533. a.invoice, a.datepaid, a.terms, a.notes,
  534. a.shipvia, a.shippingpoint, e.name AS employee, vc.name,
  535. a.$form->{vc}_id, a.till, m.name AS manager, a.curr,
  536. ex.$buysell AS exchangerate, d.description AS department,
  537. a.ponumber $acc_trans_flds
  538. FROM $table a
  539. JOIN $form->{vc} vc ON (a.$form->{vc}_id = vc.id)
  540. LEFT JOIN employee e ON (a.employee_id = e.id)
  541. LEFT JOIN employee m ON (e.managerid = m.id)
  542. LEFT JOIN exchangerate ex ON (ex.curr = a.curr
  543. AND ex.transdate = a.transdate)
  544. LEFT JOIN department d ON (a.department_id = d.id)
  545. $acc_trans_join|;
  546. my %ordinal = ( id => 1,
  547. invnumber => 2,
  548. ordnumber => 3,
  549. transdate => 4,
  550. duedate => 5,
  551. datepaid => 10,
  552. shipvia => 13,
  553. shippingpoint => 14,
  554. employee => 15,
  555. name => 16,
  556. manager => 19,
  557. curr => 20,
  558. department => 22,
  559. ponumber => 23,
  560. accno => 24,
  561. source => 25,
  562. project => 26,
  563. description => 27);
  564. my @a = (transdate, invnumber, name);
  565. push @a, "employee" if $form->{l_employee};
  566. push @a, "manager" if $form->{l_manager};
  567. my $sortorder = $form->sort_order(\@a, \%ordinal);
  568. my $where = "1 = 1";
  569. if ($form->{"$form->{vc}_id"}) {
  570. $where .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  571. } else {
  572. if ($form->{$form->{vc}}) {
  573. $var = $form->like(lc $form->{$form->{vc}});
  574. $where .= " AND lower(vc.name) LIKE '$var'";
  575. }
  576. }
  577. for (qw(department employee)) {
  578. if ($form->{$_}) {
  579. ($null, $var) = split /--/, $form->{$_};
  580. $where .= " AND a.${_}_id = $var";
  581. }
  582. }
  583. for (qw(invnumber ordnumber)) {
  584. if ($form->{$_}) {
  585. $var = $form->like(lc $form->{$_});
  586. $where .= " AND lower(a.$_) LIKE '$var'";
  587. $form->{open} = $form->{closed} = 0;
  588. }
  589. }
  590. if ($form->{partsid}){
  591. $where .= " AND a.id IN (select trans_id FROM invoice
  592. WHERE parts_id = $form->{partsid})";
  593. }
  594. for (qw(ponumber shipvia notes)) {
  595. if ($form->{$_}) {
  596. $var = $form->like(lc $form->{$_});
  597. $where .= " AND lower(a.$_) LIKE '$var'";
  598. }
  599. }
  600. if ($form->{description}) {
  601. if ($acc_trans_flds) {
  602. $var = $form->like(lc $form->{description});
  603. $where .= " AND lower(ac.memo) LIKE '$var'
  604. OR lower(i.description) LIKE '$var'";
  605. } else {
  606. $where .= " AND a.id = 0";
  607. }
  608. }
  609. if ($form->{source}) {
  610. if ($acc_trans_flds) {
  611. $var = $form->like(lc $form->{source});
  612. $where .= " AND lower(ac.source) LIKE '$var'";
  613. } else {
  614. $where .= " AND a.id = 0";
  615. }
  616. }
  617. $where .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
  618. $where .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
  619. if ($form->{open} || $form->{closed}) {
  620. unless ($form->{open} && $form->{closed}) {
  621. $where .= " AND a.amount != a.paid" if ($form->{open});
  622. $where .= " AND a.amount = a.paid" if ($form->{closed});
  623. }
  624. }
  625. if ($form->{till} ne "") {
  626. $where .= " AND a.invoice = '1'
  627. AND a.till IS NOT NULL";
  628. if ($myconfig->{role} eq 'user') {
  629. $where .= " AND e.login = '$form->{login}'";
  630. }
  631. }
  632. if ($form->{$ARAP}) {
  633. my ($accno) = split /--/, $form->{$ARAP};
  634. $where .= qq|AND a.id IN (SELECT ac.trans_id
  635. FROM acc_trans ac
  636. JOIN chart c ON (c.id = ac.chart_id)
  637. WHERE a.id = ac.trans_id
  638. AND c.accno = '$accno')|;
  639. }
  640. if ($form->{description}) {
  641. $var = $form->like(lc $form->{description});
  642. $where .= qq| AND (a.id IN (SELECT DISTINCT trans_id
  643. FROM acc_trans
  644. WHERE lower(memo) LIKE '$var')
  645. OR a.id IN (SELECT DISTINCT trans_id
  646. FROM invoice
  647. WHERE lower(description) LIKE '$var'))|;
  648. }
  649. $query .= "WHERE $where
  650. ORDER BY $sortorder";
  651. my $sth = $dbh->prepare($query);
  652. $sth->execute || $form->dberror($query);
  653. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  654. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  655. if ($ref->{linetotal} <= 0) {
  656. $ref->{debit} = $ref->{linetotal} * -1;
  657. $ref->{credit} = 0;
  658. } else {
  659. $ref->{debit} = 0;
  660. $ref->{credit} = $ref->{linetotal};
  661. }
  662. if ($ref->{invoice}) {
  663. $ref->{description} ||= $ref->{linedescription};
  664. }
  665. if ($form->{outstanding}) {
  666. next if $form->round_amount($ref->{amount}, 2) == $form->round_amount($ref->{paid}, 2);
  667. }
  668. push @{ $form->{transactions} }, $ref;
  669. }
  670. $sth->finish;
  671. $dbh->disconnect;
  672. }
  673. # this is used in IS, IR to retrieve the name
  674. sub get_name {
  675. my ($self, $myconfig, $form) = @_;
  676. # connect to database
  677. my $dbh = $form->dbconnect($myconfig);
  678. my $dateformat = $myconfig->{dateformat};
  679. if ($myconfig->{dateformat} !~ /^y/) {
  680. my @a = split /\W/, $form->{transdate};
  681. $dateformat .= "yy" if (length $a[2] > 2);
  682. }
  683. if ($form->{transdate} !~ /\W/) {
  684. $dateformat = 'yyyymmdd';
  685. }
  686. my $duedate;
  687. if ($myconfig->{dbdriver} eq 'DB2') {
  688. $duedate = ($form->{transdate}) ? "date('$form->{transdate}') + c.terms DAYS" : "current_date + c.terms DAYS";
  689. } else {
  690. $duedate = ($form->{transdate}) ? "to_date('$form->{transdate}', '$dateformat') + c.terms" : "current_date + c.terms";
  691. }
  692. $form->{"$form->{vc}_id"} *= 1;
  693. # get customer/vendor
  694. my $query = qq|SELECT c.name AS $form->{vc}, c.discount, c.creditlimit, c.terms,
  695. c.email, c.cc, c.bcc, c.taxincluded,
  696. c.address1, c.address2, c.city, c.state,
  697. c.zipcode, c.country, c.curr AS currency, c.language_code,
  698. $duedate AS duedate, c.notes AS intnotes,
  699. b.discount AS tradediscount, b.description AS business,
  700. e.name AS employee, e.id AS employee_id
  701. FROM $form->{vc} c
  702. LEFT JOIN business b ON (b.id = c.business_id)
  703. LEFT JOIN employee e ON (e.id = c.employee_id)
  704. WHERE c.id = $form->{"$form->{vc}_id"}|;
  705. my $sth = $dbh->prepare($query);
  706. $sth->execute || $form->dberror($query);
  707. $ref = $sth->fetchrow_hashref(NAME_lc);
  708. if ($form->{id}) {
  709. for (qw(currency employee employee_id intnotes)) { delete $ref->{$_} }
  710. }
  711. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  712. $sth->finish;
  713. my $buysell = ($form->{vc} eq 'customer') ? "buy" : "sell";
  714. # if no currency use defaultcurrency
  715. $form->{currency} = ($form->{currency}) ? $form->{currency} : $form->{defaultcurrency};
  716. $form->{exchangerate} = 0 if $form->{currency} eq $form->{defaultcurrency};
  717. if ($form->{transdate} && ($form->{currency} ne $form->{defaultcurrency})) {
  718. $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate}, $buysell);
  719. }
  720. $form->{forex} = $form->{exchangerate};
  721. # if no employee, default to login
  722. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh) unless $form->{employee_id};
  723. my $arap = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
  724. my $ARAP = uc $arap;
  725. $form->{creditremaining} = $form->{creditlimit};
  726. $query = qq|SELECT SUM(amount - paid)
  727. FROM $arap
  728. WHERE $form->{vc}_id = $form->{"$form->{vc}_id"}|;
  729. $sth = $dbh->prepare($query);
  730. $sth->execute || $form->dberror($query);
  731. ($form->{creditremaining}) -= $sth->fetchrow_array;
  732. $sth->finish;
  733. $query = qq|SELECT o.amount, (SELECT e.$buysell FROM exchangerate e
  734. WHERE e.curr = o.curr
  735. AND e.transdate = o.transdate)
  736. FROM oe o
  737. WHERE o.$form->{vc}_id = $form->{"$form->{vc}_id"}
  738. AND o.quotation = '0'
  739. AND o.closed = '0'|;
  740. $sth = $dbh->prepare($query);
  741. $sth->execute || $form->dberror($query);
  742. while (my ($amount, $exch) = $sth->fetchrow_array) {
  743. $exch = 1 unless $exch;
  744. $form->{creditremaining} -= $amount * $exch;
  745. }
  746. $sth->finish;
  747. # get shipto if we did not converted an order or invoice
  748. if (!$form->{shipto}) {
  749. for (qw(shiptoname shiptoaddress1 shiptoaddress2 shiptocity
  750. shiptostate shiptozipcode shiptocountry shiptocontact
  751. shiptophone shiptofax shiptoemail)) {
  752. delete $form->{$_}
  753. }
  754. ## needs fixing (SELECT *)
  755. $query = qq|SELECT *
  756. FROM shipto
  757. WHERE trans_id = $form->{"$form->{vc}_id"}|;
  758. $sth = $dbh->prepare($query);
  759. $sth->execute || $form->dberror($query);
  760. $ref = $sth->fetchrow_hashref(NAME_lc);
  761. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  762. $sth->finish;
  763. }
  764. # get taxes
  765. $query = qq|SELECT c.accno
  766. FROM chart c
  767. JOIN $form->{vc}tax ct ON (ct.chart_id = c.id)
  768. WHERE ct.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  769. $sth = $dbh->prepare($query);
  770. $sth->execute || $form->dberror($query);
  771. my %tax;
  772. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  773. $tax{$ref->{accno}} = 1;
  774. }
  775. $sth->finish;
  776. my $where = qq|AND (t.validto >= '$form->{transdate}' OR t.validto IS NULL)| if $form->{transdate};
  777. # get tax rates and description
  778. $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
  779. FROM chart c
  780. JOIN tax t ON (c.id = t.chart_id)
  781. WHERE c.link LIKE '%${ARAP}_tax%'
  782. $where
  783. ORDER BY accno, validto|;
  784. $sth = $dbh->prepare($query);
  785. $sth->execute || $form->dberror($query);
  786. $form->{taxaccounts} = "";
  787. my %a = ();
  788. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  789. if ($tax{$ref->{accno}}) {
  790. if (not exists $a{$ref->{accno}}) {
  791. for (qw(rate description taxnumber)) { $form->{"$ref->{accno}_$_"} = $ref->{$_} }
  792. $form->{taxaccounts} .= "$ref->{accno} ";
  793. $a{$ref->{accno}} = 1;
  794. }
  795. }
  796. }
  797. $sth->finish;
  798. chop $form->{taxaccounts};
  799. # setup last accounts used for this customer/vendor
  800. if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
  801. $query = qq|SELECT c.accno, c.description, c.link, c.category,
  802. ac.project_id, p.projectnumber, a.department_id,
  803. d.description AS department
  804. FROM chart c
  805. JOIN acc_trans ac ON (ac.chart_id = c.id)
  806. JOIN $arap a ON (a.id = ac.trans_id)
  807. LEFT JOIN project p ON (ac.project_id = p.id)
  808. LEFT JOIN department d ON (d.id = a.department_id)
  809. WHERE a.$form->{vc}_id = $form->{"$form->{vc}_id"}
  810. AND a.id IN (SELECT max(id)
  811. FROM $arap
  812. WHERE $form->{vc}_id = $form->{"$form->{vc}_id"})|;
  813. $sth = $dbh->prepare($query);
  814. $sth->execute || $form->dberror($query);
  815. my $i = 0;
  816. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  817. $form->{department} = $ref->{department};
  818. $form->{department_id} = $ref->{department_id};
  819. if ($ref->{link} =~ /_amount/) {
  820. $i++;
  821. $form->{"$form->{ARAP}_amount_$i"} = "$ref->{accno}--$ref->{description}" if $ref->{accno};
  822. $form->{"projectnumber_$i"} = "$ref->{projectnumber}--$ref->{project_id}" if $ref->{project_id};
  823. }
  824. if ($ref->{link} eq $form->{ARAP}) {
  825. $form->{$form->{ARAP}} = $form->{"$form->{ARAP}_1"} = "$ref->{accno}--$ref->{description}" if $ref->{accno};
  826. }
  827. }
  828. $sth->finish;
  829. $form->{rowcount} = $i if ($i && !$form->{type});
  830. }
  831. $dbh->disconnect;
  832. }
  833. 1;