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