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