summaryrefslogtreecommitdiff
path: root/LedgerSMB/AA.pm
blob: 22f32474cc4ae2039e7c0911ed6d822cf144d653 (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 employees e ON (a.employee_id = e.id)
  597. LEFT JOIN employees 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 employees 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;