summaryrefslogtreecommitdiff
path: root/LedgerSMB/AA.pm
blob: 835dbe0d4c670a2abb9f8819fa914ffdbd8c08ba (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. my $query;
  65. my $sth;
  66. my $null;
  67. ( $null, $form->{department_id} ) = split( /--/, $form->{department} );
  68. $form->{department_id} *= 1;
  69. my $ml = 1;
  70. my $table = 'ar';
  71. my $buysell = 'buy';
  72. my $ARAP = 'AR';
  73. my $invnumber = "sinumber";
  74. my $keepcleared;
  75. if ( $form->{vc} eq 'vendor' ) {
  76. $table = 'ap';
  77. $buysell = 'sell';
  78. $ARAP = 'AP';
  79. $ml = -1;
  80. $invnumber = "vinumber";
  81. }
  82. $form->{invnumber} = $form->update_defaults( $myconfig, $invnumber )
  83. unless $form->{invnumber};
  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_credit_account = ?,
  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, vc.meta_number,
  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.manager_id = 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 => 20,
  674. curr => 21,
  675. department => 23,
  676. ponumber => 24,
  677. accno => 25,
  678. source => 26,
  679. project => 27,
  680. description => 28
  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->{"meta_number"}){
  688. $where .= " AND vc.meta_number = " . $dbh->quote($form->{meta_number});
  689. }
  690. if ( $form->{"$form->{vc}_id"} ) {
  691. $form->{entity_id} = $form->{$form->{vc}."_id"};
  692. $where .= qq| AND a.entity_id = $form->{entity_id}|;
  693. }
  694. else {
  695. if ( $form->{ $form->{vc} } ) {
  696. $var = $dbh->quote( $form->like( lc $form->{ $form->{vc} } ) );
  697. $where .= " AND lower(vce.name) LIKE $var";
  698. }
  699. }
  700. for (qw(department employee)) {
  701. if ( $form->{$_} ) {
  702. ( $null, $var ) = split /--/, $form->{$_};
  703. $var = $dbh->quote($var);
  704. $where .= " AND a.${_}_id = $var";
  705. }
  706. }
  707. for (qw(invnumber ordnumber)) {
  708. if ( $form->{$_} ) {
  709. $var = $dbh->quote( $form->like( lc $form->{$_} ) );
  710. $where .= " AND lower(a.$_) LIKE $var";
  711. $form->{open} = $form->{closed} = 0;
  712. }
  713. }
  714. if ( $form->{partsid} ) {
  715. my $partsid = $dbh->quote( $form->{partsid} );
  716. $where .= " AND a.id IN (select trans_id FROM invoice
  717. WHERE parts_id = $partsid)";
  718. }
  719. for (qw(ponumber shipvia notes)) {
  720. if ( $form->{$_} ) {
  721. $var = $dbh->quote( $form->like( lc $form->{$_} ) );
  722. $where .= " AND lower(a.$_) LIKE $var";
  723. }
  724. }
  725. if ( $form->{description} ) {
  726. if ($acc_trans_flds) {
  727. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  728. $where .= " AND lower(ac.memo) LIKE $var
  729. OR lower(i.description) LIKE $var";
  730. }
  731. else {
  732. $where .= " AND a.id = 0";
  733. }
  734. }
  735. if ( $form->{source} ) {
  736. if ($acc_trans_flds) {
  737. $var = $dbh->quote( $form->like( lc $form->{source} ) );
  738. $where .= " AND lower(ac.source) LIKE $var";
  739. }
  740. else {
  741. $where .= " AND a.id = 0";
  742. }
  743. }
  744. my $transdatefrom = $dbh->quote( $form->{transdatefrom} );
  745. $where .= " AND a.transdate >= $transdatefrom"
  746. if $form->{transdatefrom};
  747. my $transdateto = $dbh->quote( $form->{transdateto} );
  748. $where .= " AND a.transdate <= $transdateto" if $form->{transdateto};
  749. if ( $form->{open} || $form->{closed} ) {
  750. unless ( $form->{open} && $form->{closed} ) {
  751. $where .= " AND a.amount != a.paid" if ( $form->{open} );
  752. $where .= " AND a.amount = a.paid" if ( $form->{closed} );
  753. }
  754. }
  755. if ( $form->{till} ne "" ) {
  756. $where .= " AND a.invoice = '1'
  757. AND a.till = $form->{till}";
  758. if ( $myconfig->{role} eq 'user' ) {
  759. my $login = $dbh->quote( $form->{login} );
  760. $where .= " AND e.entity_id = (select entity_id from users where username = $login";
  761. }
  762. }
  763. if ( $form->{$ARAP} ) {
  764. my ($accno) = split /--/, $form->{$ARAP};
  765. $accno = $dbh->quote($accno);
  766. $where .= qq|
  767. AND a.id IN (SELECT ac.trans_id
  768. FROM acc_trans ac
  769. JOIN chart c ON (c.id = ac.chart_id)
  770. WHERE a.id = ac.trans_id
  771. AND c.accno = $accno)|;
  772. }
  773. if ( $form->{description} ) {
  774. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  775. $where .= qq|
  776. AND (a.id IN (SELECT DISTINCT trans_id
  777. FROM acc_trans
  778. WHERE lower(memo) LIKE $var)
  779. OR a.id IN
  780. (SELECT DISTINCT trans_id
  781. FROM invoice
  782. WHERE lower(description)
  783. LIKE $var))|;
  784. }
  785. if ($form->{invoice_type}) {
  786. if ( $form->{invoice_type} == 2 ) {
  787. $where .= qq|
  788. AND a.on_hold = 'f'
  789. |;
  790. }
  791. if ($form->{invoice_type} == 3) {
  792. $where .= qq|
  793. AND a.on_hold = 't'
  794. |;
  795. }
  796. }
  797. # the third state, all invoices, sets no explicit toggles. It just selects them all, as normal.
  798. $query .= "WHERE $where
  799. ORDER BY $sortorder";
  800. my $sth = $dbh->prepare($query);
  801. $sth->execute(@paidargs) || $form->dberror($query);
  802. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  803. $form->db_parse_numeric(sth => $sth, hashref => $ref);
  804. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  805. if ( $ref->{linetotal} <= 0 ) {
  806. $ref->{debit} = $ref->{linetotal} * -1;
  807. $ref->{credit} = 0;
  808. }
  809. else {
  810. $ref->{debit} = 0;
  811. $ref->{credit} = $ref->{linetotal};
  812. }
  813. if ( $ref->{invoice} ) {
  814. $ref->{description} ||= $ref->{linedescription};
  815. }
  816. if ( $form->{outstanding} ) {
  817. next
  818. if $form->round_amount( $ref->{amount}, 2 ) ==
  819. $form->round_amount( $ref->{paid}, 2 );
  820. }
  821. push @{ $form->{transactions} }, $ref;
  822. }
  823. $sth->finish;
  824. $dbh->commit;
  825. }
  826. # this is used in IS, IR to retrieve the name
  827. sub get_name {
  828. my ( $self, $myconfig, $form ) = @_;
  829. # sanitize $form->{vc}
  830. if ( $form->{vc} ne 'customer' ) {
  831. $form->{vc} = 'vendor';
  832. }
  833. else {
  834. $form->{vc} = 'customer';
  835. }
  836. # connect to database
  837. my $dbh = $form->{dbh};
  838. my $dateformat = $myconfig->{dateformat};
  839. if ( $myconfig->{dateformat} !~ /^y/ ) {
  840. my @a = split /\W/, $form->{transdate};
  841. $dateformat .= "yy" if ( length $a[2] > 2 );
  842. }
  843. if ( $form->{transdate} !~ /\W/ ) {
  844. $dateformat = 'yyyymmdd';
  845. }
  846. my $duedate;
  847. $dateformat = $dbh->quote($dateformat);
  848. my $tdate = $dbh->quote( $form->{transdate} );
  849. $duedate = ( $form->{transdate} )
  850. ? "to_date($tdate, $dateformat)
  851. + c.terms"
  852. : "current_date + c.terms";
  853. $form->{"$form->{vc}_id"} *= 1;
  854. # get customer/vendor
  855. my $query = qq|
  856. SELECT entity.name AS $form->{vc}, c.discount,
  857. c.creditlimit,
  858. c.terms, c.taxincluded,
  859. c.curr AS currency,
  860. c.language_code, $duedate AS duedate,
  861. b.discount AS tradediscount,
  862. b.description AS business
  863. FROM entity_credit_account c
  864. JOIN entity ON (entity.id = c.entity_id)
  865. LEFT JOIN business b ON (b.id = c.business_id)
  866. WHERE c.id = ?|;
  867. # TODO: Add location join
  868. @queryargs = ( $form->{"$form->{vc}_id"} );
  869. my $sth = $dbh->prepare($query);
  870. $sth->execute(@queryargs) || $form->dberror($query);
  871. $ref = $sth->fetchrow_hashref(NAME_lc);
  872. $form->db_parse_numeric(sth => $sth, hashref => $ref);
  873. if ( $form->{id} ) {
  874. for (qw(currency employee employee_id intnotes)) {
  875. delete $ref->{$_};
  876. }
  877. }
  878. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  879. $sth->finish;
  880. # TODO: Retrieve contact records
  881. my $buysell = ( $form->{vc} eq 'customer' ) ? "buy" : "sell";
  882. # if no currency use defaultcurrency
  883. $form->{currency} =
  884. ( $form->{currency} )
  885. ? $form->{currency}
  886. : $form->{defaultcurrency};
  887. $form->{exchangerate} = 0
  888. if $form->{currency} eq $form->{defaultcurrency};
  889. if ( $form->{transdate}
  890. && ( $form->{currency} ne $form->{defaultcurrency} ) )
  891. {
  892. $form->{exchangerate} =
  893. $form->get_exchangerate( $dbh, $form->{currency}, $form->{transdate},
  894. $buysell );
  895. }
  896. $form->{forex} = $form->{exchangerate};
  897. # if no employee, default to login
  898. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh)
  899. unless $form->{employee_id};
  900. my $arap = ( $form->{vc} eq 'customer' ) ? 'ar' : 'ap';
  901. my $ARAP = uc $arap;
  902. $form->{creditremaining} = $form->{creditlimit};
  903. $query = qq|
  904. SELECT SUM(amount - paid)
  905. FROM $arap
  906. WHERE id = ?|;
  907. $sth = $dbh->prepare($query);
  908. $sth->execute( $form->{"$form->{vc}_id"} )
  909. || $form->dberror($query);
  910. ( $form->{creditremaining} ) -= $sth->fetchrow_array;
  911. $sth->finish;
  912. if ( $form->{vc} ne "customer" ) {
  913. $form->{vc} = 'vendor';
  914. }
  915. $query = qq|
  916. SELECT o.amount, (SELECT e.$buysell FROM exchangerate e
  917. WHERE e.curr = o.curr
  918. AND e.transdate = o.transdate)
  919. FROM oe o
  920. WHERE o.entity_id =
  921. (select entity_id from $form->{vc} WHERE id = ?)
  922. AND o.quotation = '0' AND o.closed = '0'|;
  923. $sth = $dbh->prepare($query);
  924. $sth->execute( $form->{"$form->{vc}_id"} ) || $form->dberror($query);
  925. while ( my @ref = $sth->fetchrow_array ) {
  926. $form->db_parse_numeric(sth => $sth, arrayref => \@ref);
  927. my ($amount, $exch) = @ref;
  928. $exch = 1 unless $exch;
  929. $form->{creditremaining} -= $amount * $exch;
  930. }
  931. $sth->finish;
  932. # get shipto if we did not converted an order or invoice
  933. if ( !$form->{shipto} ) {
  934. for (
  935. qw(shiptoname shiptoaddress1 shiptoaddress2
  936. shiptocity shiptostate shiptozipcode
  937. shiptocountry shiptocontact shiptophone
  938. shiptofax shiptoemail)
  939. )
  940. {
  941. delete $form->{$_};
  942. }
  943. ## needs fixing (SELECT *)
  944. $query = qq|
  945. SELECT *
  946. FROM shipto
  947. WHERE trans_id = $form->{"$form->{vc}_id"}|;
  948. $sth = $dbh->prepare($query);
  949. $sth->execute || $form->dberror($query);
  950. $ref = $sth->fetchrow_hashref(NAME_lc);
  951. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  952. $sth->finish;
  953. }
  954. # get taxes
  955. $query = qq|
  956. SELECT c.accno
  957. FROM chart c
  958. JOIN $form->{vc}tax ct ON (ct.chart_id = c.id)
  959. WHERE ct.$form->{vc}_id = ?|;
  960. $sth = $dbh->prepare($query);
  961. $sth->execute( $form->{"$form->{vc}_id"} ) || $form->dberror($query);
  962. my %tax;
  963. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  964. $tax{ $ref->{accno} } = 1;
  965. }
  966. $sth->finish;
  967. $transdate = $dbh->quote( $form->{transdate} );
  968. my $where = qq|AND (t.validto >= $transdate OR t.validto IS NULL)|
  969. if $form->{transdate};
  970. # get tax rates and description
  971. $query = qq|
  972. SELECT c.accno, c.description, t.rate, t.taxnumber
  973. FROM chart c
  974. JOIN tax t ON (c.id = t.chart_id)
  975. WHERE c.link LIKE '%${ARAP}_tax%'
  976. $where
  977. ORDER BY accno, validto|;
  978. $sth = $dbh->prepare($query);
  979. $sth->execute || $form->dberror($query);
  980. $form->{taxaccounts} = "";
  981. my %a = ();
  982. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  983. $form->db_parse_numeric(sth => $sth, hashref => $hashref);
  984. if ( $tax{ $ref->{accno} } ) {
  985. if ( not exists $a{ $ref->{accno} } ) {
  986. for (qw(rate description taxnumber)) {
  987. $form->{"$ref->{accno}_$_"} = $ref->{$_};
  988. }
  989. $form->{taxaccounts} .= "$ref->{accno} ";
  990. $a{ $ref->{accno} } = 1;
  991. }
  992. }
  993. }
  994. $sth->finish;
  995. chop $form->{taxaccounts};
  996. # setup last accounts used for this customer/vendor
  997. if ( !$form->{id} && $form->{type} !~ /_(order|quotation)/ ) {
  998. $query = qq|
  999. SELECT c.accno, c.description, c.link,
  1000. c.category,
  1001. ac.project_id,
  1002. a.department_id
  1003. FROM chart c
  1004. JOIN acc_trans ac ON (ac.chart_id = c.id)
  1005. JOIN $arap a ON (a.id = ac.trans_id)
  1006. WHERE a.entity_id = ?
  1007. AND a.id = (SELECT max(id)
  1008. FROM $arap
  1009. WHERE entity_id =
  1010. ?)
  1011. |;
  1012. $sth = $dbh->prepare($query);
  1013. $sth->execute( $form->{"$form->{vc}_id"}, $form->{"$form->{vc}_id"} )
  1014. || $form->dberror($query);
  1015. my $i = 0;
  1016. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1017. $form->{department_id} = $ref->{department_id};
  1018. if ( $ref->{link} =~ /_amount/ ) {
  1019. $i++;
  1020. $form->{"$form->{ARAP}_amount_$i"} =
  1021. "$ref->{accno}--$ref->{description}"
  1022. if $ref->{accno};
  1023. $form->{"projectnumber_$i"} =
  1024. "$ref->{projectnumber}--" . "$ref->{project_id}"
  1025. if $ref->{project_id};
  1026. }
  1027. if ( $ref->{link} eq $form->{ARAP} ) {
  1028. $form->{ $form->{ARAP} } = $form->{"$form->{ARAP}_1"} =
  1029. "$ref->{accno}--" . "$ref->{description}"
  1030. if $ref->{accno};
  1031. }
  1032. }
  1033. $sth->finish;
  1034. $query = "select description from department where id = ?";
  1035. $sth = $dbh->prepare($query);
  1036. $sth->execute($form->{department_id});
  1037. ($form->{department}) = $sth->fetchrow_array;
  1038. $form->{rowcount} = $i if ( $i && !$form->{type} );
  1039. }
  1040. $dbh->commit;
  1041. }
  1042. 1;