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