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