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