summaryrefslogtreecommitdiff
path: root/LedgerSMB/AA.pm
blob: be1ac8eb67d98d729a3dc6475d987ff44a198c54 (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. }
  62. else {
  63. $exchangerate =
  64. $form->check_exchangerate( $myconfig, $form->{currency},
  65. $form->{transdate}, $buysell );
  66. $form->{exchangerate} =
  67. ($exchangerate)
  68. ? $exchangerate
  69. : $form->parse_amount( $myconfig, $form->{exchangerate} );
  70. }
  71. my @taxaccounts = split / /, $form->{taxaccounts};
  72. my $tax = 0;
  73. my $fxtax = 0;
  74. my $amount;
  75. my $diff;
  76. my %tax = ();
  77. my $accno;
  78. # add taxes
  79. foreach $accno (@taxaccounts) {
  80. $fxtax += $tax{fxamount}{$accno} =
  81. $form->parse_amount( $myconfig, $form->{"tax_$accno"} );
  82. $tax += $tax{fxamount}{$accno};
  83. push @{ $form->{acc_trans}{taxes} },
  84. {
  85. accno => $accno,
  86. amount => $tax{fxamount}{$accno},
  87. project_id => undef,
  88. fx_transaction => 0
  89. };
  90. $amount = $tax{fxamount}{$accno} * $form->{exchangerate};
  91. $tax{amount}{$accno} = $form->round_amount( $amount - $diff, 2 );
  92. $diff = $tax{amount}{$accno} - ( $amount - $diff );
  93. $amount = $tax{amount}{$accno} - $tax{fxamount}{$accno};
  94. $tax += $amount;
  95. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  96. push @{ $form->{acc_trans}{taxes} },
  97. {
  98. accno => $accno,
  99. amount => $amount,
  100. project_id => undef,
  101. fx_transaction => 1
  102. };
  103. }
  104. }
  105. my %amount = ();
  106. my $fxinvamount = 0;
  107. for ( 1 .. $form->{rowcount} ) {
  108. $fxinvamount += $amount{fxamount}{$_} =
  109. $form->parse_amount( $myconfig, $form->{"amount_$_"} );
  110. }
  111. $form->{taxincluded} *= 1;
  112. my $i;
  113. my $project_id;
  114. my $cleared = 0;
  115. $diff = 0;
  116. # deduct tax from amounts if tax included
  117. for $i ( 1 .. $form->{rowcount} ) {
  118. if ( $amount{fxamount}{$i} ) {
  119. if ( $form->{taxincluded} ) {
  120. $amount =
  121. ($fxinvamount)
  122. ? $fxtax * $amount{fxamount}{$i} / $fxinvamount
  123. : 0;
  124. $amount{fxamount}{$i} -= $amount;
  125. }
  126. # multiply by exchangerate
  127. $amount = $amount{fxamount}{$i} * $form->{exchangerate};
  128. $amount{amount}{$i} = $form->round_amount( $amount - $diff, 2 );
  129. $diff = $amount{amount}{$i} - ( $amount - $diff );
  130. ( $null, $project_id ) = split /--/, $form->{"projectnumber_$i"};
  131. $project_id ||= undef;
  132. ($accno) = split /--/, $form->{"${ARAP}_amount_$i"};
  133. if ($keepcleared) {
  134. $cleared = ( $form->{"cleared_$i"} ) ? 1 : 0;
  135. }
  136. push @{ $form->{acc_trans}{lineitems} },
  137. {
  138. accno => $accno,
  139. amount => $amount{fxamount}{$i},
  140. project_id => $project_id,
  141. description => $form->{"description_$i"},
  142. cleared => $cleared,
  143. fx_transaction => 0
  144. };
  145. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  146. $amount = $amount{amount}{$i} - $amount{fxamount}{$i};
  147. push @{ $form->{acc_trans}{lineitems} },
  148. {
  149. accno => $accno,
  150. amount => $amount,
  151. project_id => $project_id,
  152. description => $form->{"description_$i"},
  153. cleared => $cleared,
  154. fx_transaction => 1
  155. };
  156. }
  157. }
  158. }
  159. my $invnetamount = 0;
  160. for ( @{ $form->{acc_trans}{lineitems} } ) { $invnetamount += $_->{amount} }
  161. my $invamount = $invnetamount + $tax;
  162. # adjust paidaccounts if there is no date in the last row
  163. $form->{paidaccounts}--
  164. unless ( $form->{"datepaid_$form->{paidaccounts}"} );
  165. if ( $form->{vc} ne "customer" ) {
  166. $form->{vc} = "vendor";
  167. }
  168. my $paid = 0;
  169. my $fxamount;
  170. $diff = 0;
  171. # add payments
  172. for $i ( 1 .. $form->{paidaccounts} ) {
  173. $fxamount = $form->parse_amount( $myconfig, $form->{"paid_$i"} );
  174. if ($fxamount) {
  175. $paid += $fxamount;
  176. $paidamount = $fxamount * $form->{exchangerate};
  177. $amount = $form->round_amount( $paidamount - $diff, 2 );
  178. $diff = $amount - ( $paidamount - $diff );
  179. $form->{datepaid} = $form->{"datepaid_$i"};
  180. $paid{fxamount}{$i} = $fxamount;
  181. $paid{amount}{$i} = $amount;
  182. }
  183. }
  184. $fxinvamount += $fxtax unless $form->{taxincluded};
  185. $fxinvamount = $form->round_amount( $fxinvamount, 2 );
  186. $invamount = $form->round_amount( $invamount, 2 );
  187. $paid = $form->round_amount( $paid, 2 );
  188. $paid =
  189. ( $fxinvamount == $paid )
  190. ? $invamount
  191. : $form->round_amount( $paid * $form->{exchangerate}, 2 );
  192. $query = q|
  193. SELECT (SELECT value FROM defaults
  194. WHERE setting_key = 'fxgain_accno_id'),
  195. (SELECT value FROM defaults
  196. WHERE setting_key = 'fxloss_accno_id')|;
  197. my ( $fxgain_accno_id, $fxloss_accno_id ) = $dbh->selectrow_array($query);
  198. ( $null, $form->{employee_id} ) = split /--/, $form->{employee};
  199. unless ( $form->{employee_id} ) {
  200. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  201. }
  202. # check if id really exists
  203. if ( $form->{id} ) {
  204. my $id = $dbh->quote( $form->{id} );
  205. $keepcleared = 1;
  206. $query = qq|
  207. SELECT id
  208. FROM $table
  209. WHERE id = $id|;
  210. if ( $dbh->selectrow_array($query) ) {
  211. # delete detail records
  212. $query = qq|
  213. DELETE FROM acc_trans
  214. WHERE trans_id = $id|;
  215. $dbh->do($query) || $form->dberror($query);
  216. }
  217. }
  218. else {
  219. my $uid = localtime;
  220. $uid .= "$$";
  221. $query = qq|
  222. INSERT INTO $table (invnumber)
  223. VALUES ('$uid')|;
  224. $dbh->do($query) || $form->dberror($query);
  225. $query = qq|
  226. SELECT id FROM $table
  227. WHERE invnumber = '$uid'|;
  228. ( $form->{id} ) = $dbh->selectrow_array($query);
  229. }
  230. # record last payment date in ar/ap table
  231. $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
  232. my $datepaid = ($paid) ? qq|'$form->{datepaid}'| : 'NOW';
  233. $form->{invnumber} = $form->update_defaults( $myconfig, $invnumber )
  234. unless $form->{invnumber};
  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. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  727. if ( $ref->{linetotal} <= 0 ) {
  728. $ref->{debit} = $ref->{linetotal} * -1;
  729. $ref->{credit} = 0;
  730. }
  731. else {
  732. $ref->{debit} = 0;
  733. $ref->{credit} = $ref->{linetotal};
  734. }
  735. if ( $ref->{invoice} ) {
  736. $ref->{description} ||= $ref->{linedescription};
  737. }
  738. if ( $form->{outstanding} ) {
  739. next
  740. if $form->round_amount( $ref->{amount}, 2 ) ==
  741. $form->round_amount( $ref->{paid}, 2 );
  742. }
  743. push @{ $form->{transactions} }, $ref;
  744. }
  745. $sth->finish;
  746. $dbh->commit;
  747. }
  748. # this is used in IS, IR to retrieve the name
  749. sub get_name {
  750. my ( $self, $myconfig, $form ) = @_;
  751. # sanitize $form->{vc}
  752. if ( $form->{vc} ne 'customer' ) {
  753. $form->{vc} = 'vendor';
  754. }
  755. else {
  756. $form->{vc} = 'customer';
  757. }
  758. # connect to database
  759. my $dbh = $form->{dbh};
  760. my $dateformat = $myconfig->{dateformat};
  761. if ( $myconfig->{dateformat} !~ /^y/ ) {
  762. my @a = split /\W/, $form->{transdate};
  763. $dateformat .= "yy" if ( length $a[2] > 2 );
  764. }
  765. if ( $form->{transdate} !~ /\W/ ) {
  766. $dateformat = 'yyyymmdd';
  767. }
  768. my $duedate;
  769. $dateformat = $dbh->quote($dateformat);
  770. my $tdate = $dbh->quote( $form->{transdate} );
  771. $duedate = ( $form->{transdate} )
  772. ? "to_date($tdate, $dateformat)
  773. + c.terms"
  774. : "current_date + c.terms";
  775. $form->{"$form->{vc}_id"} *= 1;
  776. # get customer/vendor
  777. my $query = qq|
  778. SELECT c.name AS $form->{vc}, c.discount, c.creditlimit,
  779. c.terms, c.email, c.cc, c.bcc, c.taxincluded,
  780. c.address1, c.address2, c.city, c.state,
  781. c.zipcode, c.country, c.curr AS currency,
  782. c.language_code, $duedate AS duedate,
  783. c.notes AS intnotes,
  784. b.discount AS tradediscount,
  785. b.description AS business,
  786. e.name AS employee, e.id AS employee_id
  787. FROM $form->{vc} c
  788. LEFT JOIN business b ON (b.id = c.business_id)
  789. LEFT JOIN employee e ON (e.id = c.employee_id)
  790. WHERE c.id = ?|;
  791. @queryargs = ( $form->{"$form->{vc}_id"} );
  792. my $sth = $dbh->prepare($query);
  793. $sth->execute(@queryargs) || $form->dberror($query);
  794. $ref = $sth->fetchrow_hashref(NAME_lc);
  795. if ( $form->{id} ) {
  796. for (qw(currency employee employee_id intnotes)) {
  797. delete $ref->{$_};
  798. }
  799. }
  800. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  801. $sth->finish;
  802. my $buysell = ( $form->{vc} eq 'customer' ) ? "buy" : "sell";
  803. # if no currency use defaultcurrency
  804. $form->{currency} =
  805. ( $form->{currency} )
  806. ? $form->{currency}
  807. : $form->{defaultcurrency};
  808. $form->{exchangerate} = 0
  809. if $form->{currency} eq $form->{defaultcurrency};
  810. if ( $form->{transdate}
  811. && ( $form->{currency} ne $form->{defaultcurrency} ) )
  812. {
  813. $form->{exchangerate} =
  814. $form->get_exchangerate( $dbh, $form->{currency}, $form->{transdate},
  815. $buysell );
  816. }
  817. $form->{forex} = $form->{exchangerate};
  818. # if no employee, default to login
  819. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh)
  820. unless $form->{employee_id};
  821. my $arap = ( $form->{vc} eq 'customer' ) ? 'ar' : 'ap';
  822. my $ARAP = uc $arap;
  823. $form->{creditremaining} = $form->{creditlimit};
  824. $query = qq|
  825. SELECT SUM(amount - paid)
  826. FROM $arap
  827. WHERE $form->{vc}_id = ?|;
  828. $sth = $dbh->prepare($query);
  829. $sth->execute( $form->{"$form->{vc}_id"} )
  830. || $form->dberror($query);
  831. ( $form->{creditremaining} ) -= $sth->fetchrow_array;
  832. $sth->finish;
  833. if ( $form->{vc} ne "customer" ) {
  834. $form->{vc} = 'vendor';
  835. }
  836. $query = qq|
  837. SELECT o.amount, (SELECT e.$buysell FROM exchangerate e
  838. WHERE e.curr = o.curr
  839. AND e.transdate = o.transdate)
  840. FROM oe o
  841. WHERE o.$form->{vc}_id = ?
  842. AND o.quotation = '0' AND o.closed = '0'|;
  843. $sth = $dbh->prepare($query);
  844. $sth->execute( $form->{"$form->{vc}_id"} ) || $form->dberror($query);
  845. while ( my ( $amount, $exch ) = $sth->fetchrow_array ) {
  846. $exch = 1 unless $exch;
  847. $form->{creditremaining} -= $amount * $exch;
  848. }
  849. $sth->finish;
  850. # get shipto if we did not converted an order or invoice
  851. if ( !$form->{shipto} ) {
  852. for (
  853. qw(shiptoname shiptoaddress1 shiptoaddress2
  854. shiptocity shiptostate shiptozipcode
  855. shiptocountry shiptocontact shiptophone
  856. shiptofax shiptoemail)
  857. )
  858. {
  859. delete $form->{$_};
  860. }
  861. ## needs fixing (SELECT *)
  862. $query = qq|
  863. SELECT *
  864. FROM shipto
  865. WHERE trans_id = $form->{"$form->{vc}_id"}|;
  866. $sth = $dbh->prepare($query);
  867. $sth->execute || $form->dberror($query);
  868. $ref = $sth->fetchrow_hashref(NAME_lc);
  869. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  870. $sth->finish;
  871. }
  872. # get taxes
  873. $query = qq|
  874. SELECT c.accno
  875. FROM chart c
  876. JOIN $form->{vc}tax ct ON (ct.chart_id = c.id)
  877. WHERE ct.$form->{vc}_id = ?|;
  878. $sth = $dbh->prepare($query);
  879. $sth->execute( $form->{"$form->{vc}_id"} ) || $form->dberror($query);
  880. my %tax;
  881. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  882. $tax{ $ref->{accno} } = 1;
  883. }
  884. $sth->finish;
  885. $transdate = $dbh->quote( $form->{transdate} );
  886. my $where = qq|AND (t.validto >= $transdate OR t.validto IS NULL)|
  887. if $form->{transdate};
  888. # get tax rates and description
  889. $query = qq|
  890. SELECT c.accno, c.description, t.rate, t.taxnumber
  891. FROM chart c
  892. JOIN tax t ON (c.id = t.chart_id)
  893. WHERE c.link LIKE '%${ARAP}_tax%'
  894. $where
  895. ORDER BY accno, validto|;
  896. $sth = $dbh->prepare($query);
  897. $sth->execute || $form->dberror($query);
  898. $form->{taxaccounts} = "";
  899. my %a = ();
  900. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  901. if ( $tax{ $ref->{accno} } ) {
  902. if ( not exists $a{ $ref->{accno} } ) {
  903. for (qw(rate description taxnumber)) {
  904. $form->{"$ref->{accno}_$_"} = $ref->{$_};
  905. }
  906. $form->{taxaccounts} .= "$ref->{accno} ";
  907. $a{ $ref->{accno} } = 1;
  908. }
  909. }
  910. }
  911. $sth->finish;
  912. chop $form->{taxaccounts};
  913. # setup last accounts used for this customer/vendor
  914. if ( !$form->{id} && $form->{type} !~ /_(order|quotation)/ ) {
  915. $query = qq|
  916. SELECT c.accno, c.description, c.link, c.category,
  917. ac.project_id, p.projectnumber,
  918. a.department_id, d.description AS department
  919. FROM chart c
  920. JOIN acc_trans ac ON (ac.chart_id = c.id)
  921. JOIN $arap a ON (a.id = ac.trans_id)
  922. LEFT JOIN project p ON (ac.project_id = p.id)
  923. LEFT JOIN department d ON (d.id = a.department_id)
  924. WHERE a.$form->{vc}_id = ?
  925. AND a.id IN (SELECT max(id)
  926. FROM $arap
  927. WHERE $form->{vc}_id =
  928. ?)
  929. |;
  930. $sth = $dbh->prepare($query);
  931. $sth->execute( $form->{"$form->{vc}_id"}, $form->{"$form->{vc}_id"} )
  932. || $form->dberror($query);
  933. my $i = 0;
  934. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  935. $form->{department} = $ref->{department};
  936. $form->{department_id} = $ref->{department_id};
  937. if ( $ref->{link} =~ /_amount/ ) {
  938. $i++;
  939. $form->{"$form->{ARAP}_amount_$i"} =
  940. "$ref->{accno}--$ref->{description}"
  941. if $ref->{accno};
  942. $form->{"projectnumber_$i"} =
  943. "$ref->{projectnumber}--" . "$ref->{project_id}"
  944. if $ref->{project_id};
  945. }
  946. if ( $ref->{link} eq $form->{ARAP} ) {
  947. $form->{ $form->{ARAP} } = $form->{"$form->{ARAP}_1"} =
  948. "$ref->{accno}--" . "$ref->{description}"
  949. if $ref->{accno};
  950. }
  951. }
  952. $sth->finish;
  953. $form->{rowcount} = $i if ( $i && !$form->{type} );
  954. }
  955. $dbh->commit;
  956. }
  957. 1;