summaryrefslogtreecommitdiff
path: root/LedgerSMB/AA.pm
blob: f5a2564fa26b154a2d32dd461349ea23f1e007fc (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://sourceforge.net/projects/ledger-smb/
  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. sub post_transaction {
  37. my ($self, $myconfig, $form) = @_;
  38. # connect to database
  39. my $dbh = $form->dbconnect_noauto($myconfig);
  40. my $query;
  41. my $sth;
  42. my $null;
  43. ($null, $form->{department_id}) = split(/--/, $form->{department});
  44. $form->{department_id} *= 1;
  45. my $ml = 1;
  46. my $table = 'ar';
  47. my $buysell = 'buy';
  48. my $ARAP = 'AR';
  49. my $invnumber = "sinumber";
  50. my $keepcleared;
  51. if ($form->{vc} eq 'vendor') {
  52. $table = 'ap';
  53. $buysell = 'sell';
  54. $ARAP = 'AP';
  55. $ml = -1;
  56. $invnumber = "vinumber";
  57. }
  58. if ($form->{currency} eq $form->{defaultcurrency}) {
  59. $form->{exchangerate} = 1;
  60. } else {
  61. $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, $buysell);
  62. $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
  63. }
  64. my @taxaccounts = split / /, $form->{taxaccounts};
  65. my $tax = 0;
  66. my $fxtax = 0;
  67. my $amount;
  68. my $diff;
  69. my %tax = ();
  70. my $accno;
  71. # add taxes
  72. foreach $accno (@taxaccounts) {
  73. $fxtax += $tax{fxamount}{$accno} = $form->parse_amount($myconfig, $form->{"tax_$accno"});
  74. $tax += $tax{fxamount}{$accno};
  75. push @{ $form->{acc_trans}{taxes} }, {
  76. accno => $accno,
  77. amount => $tax{fxamount}{$accno},
  78. project_id => 'NULL',
  79. fx_transaction => 0 };
  80. $amount = $tax{fxamount}{$accno} * $form->{exchangerate};
  81. $tax{amount}{$accno} = $form->round_amount($amount - $diff, 2);
  82. $diff = $tax{amount}{$accno} - ($amount - $diff);
  83. $amount = $tax{amount}{$accno} - $tax{fxamount}{$accno};
  84. $tax += $amount;
  85. if ($form->{currency} ne $form->{defaultcurrency}) {
  86. push @{ $form->{acc_trans}{taxes} }, {
  87. accno => $accno,
  88. amount => $amount,
  89. project_id => 'NULL',
  90. fx_transaction => 1 };
  91. }
  92. }
  93. my %amount = ();
  94. my $fxinvamount = 0;
  95. for (1 .. $form->{rowcount}) {
  96. $fxinvamount += $amount{fxamount}{$_} = $form->parse_amount($myconfig, $form->{"amount_$_"})
  97. }
  98. $form->{taxincluded} *= 1;
  99. my $i;
  100. my $project_id;
  101. my $cleared = 0;
  102. $diff = 0;
  103. # deduct tax from amounts if tax included
  104. for $i (1 .. $form->{rowcount}) {
  105. if ($amount{fxamount}{$i}) {
  106. if ($form->{taxincluded}) {
  107. $amount = ($fxinvamount) ? $fxtax * $amount{fxamount}{$i} / $fxinvamount : 0;
  108. $amount{fxamount}{$i} -= $amount;
  109. }
  110. # multiply by exchangerate
  111. $amount = $amount{fxamount}{$i} * $form->{exchangerate};
  112. $amount{amount}{$i} = $form->round_amount($amount - $diff, 2);
  113. $diff = $amount{amount}{$i} - ($amount - $diff);
  114. ($null, $project_id) = split /--/, $form->{"projectnumber_$i"};
  115. $project_id ||= 'NULL';
  116. ($accno) = split /--/, $form->{"${ARAP}_amount_$i"};
  117. if ($keepcleared) {
  118. $cleared = ($form->{"cleared_$i"}) ? 1 : 0;
  119. }
  120. push @{ $form->{acc_trans}{lineitems} }, {
  121. accno => $accno,
  122. amount => $amount{fxamount}{$i},
  123. project_id => $project_id,
  124. description => $form->{"description_$i"},
  125. cleared => $cleared,
  126. fx_transaction => 0 };
  127. if ($form->{currency} ne $form->{defaultcurrency}) {
  128. $amount = $amount{amount}{$i} - $amount{fxamount}{$i};
  129. push @{ $form->{acc_trans}{lineitems} }, {
  130. accno => $accno,
  131. amount => $amount,
  132. project_id => $project_id,
  133. description => $form->{"description_$i"},
  134. cleared => $cleared,
  135. fx_transaction => 1 };
  136. }
  137. }
  138. }
  139. my $invnetamount = 0;
  140. for (@{ $form->{acc_trans}{lineitems} }) { $invnetamount += $_->{amount} }
  141. my $invamount = $invnetamount + $tax;
  142. # adjust paidaccounts if there is no date in the last row
  143. $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
  144. my $paid = 0;
  145. my $fxamount;
  146. $diff = 0;
  147. # add payments
  148. for $i (1 .. $form->{paidaccounts}) {
  149. $fxamount = $form->parse_amount($myconfig, $form->{"paid_$i"});
  150. if ($fxamount) {
  151. $paid += $fxamount;
  152. $paidamount = $fxamount * $form->{exchangerate};
  153. $amount = $form->round_amount($paidamount - $diff, 2);
  154. $diff = $amount - ($paidamount - $diff);
  155. $form->{datepaid} = $form->{"datepaid_$i"};
  156. $paid{fxamount}{$i} = $fxamount;
  157. $paid{amount}{$i} = $amount;
  158. }
  159. }
  160. $fxinvamount += $fxtax unless $form->{taxincluded};
  161. $fxinvamount = $form->round_amount($fxinvamount, 2);
  162. $invamount = $form->round_amount($invamount, 2);
  163. $paid = $form->round_amount($paid, 2);
  164. $paid = ($fxinvamount == $paid) ? $invamount : $form->round_amount($paid * $form->{exchangerate}, 2);
  165. $query = q|SELECT fxgain_accno_id, fxloss_accno_id
  166. FROM defaults|;
  167. my ($fxgain_accno_id, $fxloss_accno_id) = $dbh->selectrow_array($query);
  168. ($null, $form->{employee_id}) = split /--/, $form->{employee};
  169. unless ($form->{employee_id}) {
  170. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
  171. }
  172. # check if id really exists
  173. if ($form->{id}) {
  174. $keepcleared = 1;
  175. $query = qq|SELECT id FROM $table
  176. WHERE id = $form->{id}|;
  177. if ($dbh->selectrow_array($query)) {
  178. # delete detail records
  179. $query = qq|DELETE FROM acc_trans
  180. WHERE trans_id = $form->{id}|;
  181. $dbh->do($query) || $form->dberror($query);
  182. }
  183. } else {
  184. my $uid = localtime;
  185. $uid .= "$$";
  186. $query = qq|INSERT INTO $table (invnumber)
  187. VALUES ('$uid')|;
  188. $dbh->do($query) || $form->dberror($query);
  189. $query = qq|SELECT id FROM $table
  190. WHERE invnumber = '$uid'|;
  191. ($form->{id}) = $dbh->selectrow_array($query);
  192. }
  193. # record last payment date in ar/ap table
  194. $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
  195. my $datepaid = ($paid) ? qq|'$form->{datepaid}'| : 'NULL';
  196. $form->{invnumber} = $form->update_defaults($myconfig, $invnumber) unless $form->{invnumber};
  197. $query = qq|UPDATE $table SET invnumber = |.$dbh->quote($form->{invnumber}).qq|,
  198. ordnumber = |.$dbh->quote($form->{ordnumber}).qq|,
  199. transdate = '$form->{transdate}',
  200. $form->{vc}_id = $form->{"$form->{vc}_id"},
  201. taxincluded = '$form->{taxincluded}',
  202. amount = $invamount,
  203. duedate = '$form->{duedate}',
  204. paid = $paid,
  205. datepaid = $datepaid,
  206. netamount = $invnetamount,
  207. curr = '$form->{currency}',
  208. notes = |.$dbh->quote($form->{notes}).qq|,
  209. department_id = $form->{department_id},
  210. employee_id = $form->{employee_id},
  211. ponumber = |.$dbh->quote($form->{ponumber}).qq|
  212. WHERE id = $form->{id}|;
  213. $dbh->do($query) || $form->dberror($query);
  214. # update exchangerate
  215. my $buy = $form->{exchangerate};
  216. my $sell = 0;
  217. if ($form->{vc} eq 'vendor') {
  218. $buy = 0;
  219. $sell = $form->{exchangerate};
  220. }
  221. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  222. $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $buy, $sell);
  223. }
  224. my $ref;
  225. # add individual transactions
  226. foreach $ref (@{ $form->{acc_trans}{lineitems} }) {
  227. # insert detail records in acc_trans
  228. if ($ref->{amount}) {
  229. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
  230. project_id, memo, fx_transaction, cleared)
  231. VALUES ($form->{id}, (SELECT id FROM chart
  232. WHERE accno = '$ref->{accno}'),
  233. $ref->{amount} * $ml, '$form->{transdate}',
  234. $ref->{project_id}, |.$dbh->quote($ref->{description}).qq|,
  235. '$ref->{fx_transaction}', '$ref->{cleared}')|;
  236. $dbh->do($query) || $form->dberror($query);
  237. }
  238. }
  239. # save taxes
  240. foreach $ref (@{ $form->{acc_trans}{taxes} }) {
  241. if ($ref->{amount}) {
  242. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  243. transdate, fx_transaction)
  244. VALUES ($form->{id},
  245. (SELECT id FROM chart
  246. WHERE accno = '$ref->{accno}'),
  247. $ref->{amount} * $ml, '$form->{transdate}',
  248. '$ref->{fx_transaction}')|;
  249. $dbh->do($query) || $form->dberror($query);
  250. }
  251. }
  252. my $arap;
  253. # record ar/ap
  254. if (($arap = $invamount)) {
  255. ($accno) = split /--/, $form->{$ARAP};
  256. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate)
  257. VALUES ($form->{id},
  258. (SELECT id FROM chart
  259. WHERE accno = '$accno'),
  260. $invamount * -1 * $ml, '$form->{transdate}')|;
  261. $dbh->do($query) || $form->dberror($query);
  262. }
  263. # if there is no amount force ar/ap
  264. if ($fxinvamount == 0) {
  265. $arap = 1;
  266. }
  267. my $exchangerate;
  268. # add paid transactions
  269. for $i (1 .. $form->{paidaccounts}) {
  270. if ($paid{fxamount}{$i}) {
  271. ($accno) = split(/--/, $form->{"${ARAP}_paid_$i"});
  272. $form->{"datepaid_$i"} = $form->{transdate} unless ($form->{"datepaid_$i"});
  273. $exchangerate = 0;
  274. if ($form->{currency} eq $form->{defaultcurrency}) {
  275. $form->{"exchangerate_$i"} = 1;
  276. } else {
  277. $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, $buysell);
  278. $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
  279. }
  280. # if there is no amount
  281. if ($fxinvamount == 0) {
  282. $form->{exchangerate} = $form->{"exchangerate_$i"};
  283. }
  284. # ar/ap amount
  285. if ($arap) {
  286. ($accno) = split /--/, $form->{$ARAP};
  287. # add ar/ap
  288. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,transdate)
  289. VALUES ($form->{id}, (SELECT id FROM chart
  290. WHERE accno = '$accno'),
  291. $paid{amount}{$i} * $ml, '$form->{"datepaid_$i"}')|;
  292. $dbh->do($query) || $form->dberror($query);
  293. }
  294. $arap = $paid{amount}{$i};
  295. # add payment
  296. if ($paid{fxamount}{$i}) {
  297. ($accno) = split /--/, $form->{"${ARAP}_paid_$i"};
  298. my $cleared = ($form->{"cleared_$i"}) ? 1 : 0;
  299. $amount = $paid{fxamount}{$i};
  300. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  301. transdate, source, memo, cleared)
  302. VALUES ($form->{id}, (SELECT id FROM chart
  303. WHERE accno = '$accno'),
  304. $amount * -1 * $ml, '$form->{"datepaid_$i"}', |
  305. .$dbh->quote($form->{"source_$i"}).qq|, |
  306. .$dbh->quote($form->{"memo_$i"}).qq|, '$cleared')|;
  307. $dbh->do($query) || $form->dberror($query);
  308. if ($form->{currency} ne $form->{defaultcurrency}) {
  309. # exchangerate gain/loss
  310. $amount = ($form->round_amount($paid{fxamount}{$i} * $form->{exchangerate},2) - $form->round_amount($paid{fxamount}{$i} * $form->{"exchangerate_$i"},2)) * -1;
  311. if ($amount) {
  312. my $accno_id = (($amount * $ml) > 0) ? $fxgain_accno_id : $fxloss_accno_id;
  313. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  314. transdate, fx_transaction, cleared)
  315. VALUES ($form->{id}, $accno_id,
  316. $amount * $ml, '$form->{"datepaid_$i"}', '1',
  317. '$cleared')|;
  318. $dbh->do($query) || $form->dberror($query);
  319. }
  320. # exchangerate difference
  321. $amount = $paid{amount}{$i} - $paid{fxamount}{$i} + $amount;
  322. $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
  323. transdate, fx_transaction, cleared, source)
  324. VALUES ($form->{id}, (SELECT id FROM chart
  325. WHERE accno = '$accno'),
  326. $amount * -1 * $ml, '$form->{"datepaid_$i"}', '1',
  327. '$cleared', |
  328. .$dbh->quote($form->{"source_$i"}).qq|)|;
  329. $dbh->do($query) || $form->dberror($query);
  330. }
  331. # update exchangerate record
  332. $buy = $form->{"exchangerate_$i"};
  333. $sell = 0;
  334. if ($form->{vc} eq 'vendor') {
  335. $buy = 0;
  336. $sell = $form->{"exchangerate_$i"};
  337. }
  338. if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
  339. $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $buy, $sell);
  340. }
  341. }
  342. }
  343. }
  344. # save printed and queued
  345. $form->save_status($dbh);
  346. my %audittrail = ( tablename => $table,
  347. reference => $form->{invnumber},
  348. formname => 'transaction',
  349. action => 'posted',
  350. id => $form->{id} );
  351. $form->audittrail($dbh, "", \%audittrail);
  352. $form->save_recurring($dbh, $myconfig);
  353. my $rc = $dbh->commit;
  354. $dbh->disconnect;
  355. $rc;
  356. }
  357. sub delete_transaction {
  358. my ($self, $myconfig, $form) = @_;
  359. # connect to database, turn AutoCommit off
  360. my $dbh = $form->dbconnect_noauto($myconfig);
  361. my $table = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
  362. my %audittrail = ( tablename => $table,
  363. reference => $form->{invnumber},
  364. formname => 'transaction',
  365. action => 'deleted',
  366. id => $form->{id} );
  367. $form->audittrail($dbh, "", \%audittrail);
  368. my $query = qq|DELETE FROM $table WHERE id = $form->{id}|;
  369. $dbh->do($query) || $form->dberror($query);
  370. $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
  371. $dbh->do($query) || $form->dberror($query);
  372. # get spool files
  373. $query = qq|SELECT spoolfile
  374. FROM status
  375. WHERE trans_id = $form->{id}
  376. AND spoolfile IS NOT NULL|;
  377. my $sth = $dbh->prepare($query);
  378. $sth->execute || $form->dberror($query);
  379. my $spoolfile;
  380. my @spoolfiles = ();
  381. while (($spoolfile) = $sth->fetchrow_array) {
  382. push @spoolfiles, $spoolfile;
  383. }
  384. $sth->finish;
  385. $query = qq|DELETE FROM status WHERE trans_id = $form->{id}|;
  386. $dbh->do($query) || $form->dberror($query);
  387. # commit
  388. my $rc = $dbh->commit;
  389. $dbh->disconnect;
  390. if ($rc) {
  391. foreach $spoolfile (@spoolfiles) {
  392. unlink "$spool/$spoolfile" if $spoolfile;
  393. }
  394. }
  395. $rc;
  396. }
  397. sub transactions {
  398. my ($self, $myconfig, $form) = @_;
  399. # connect to database
  400. my $dbh = $form->dbconnect($myconfig);
  401. my $null;
  402. my $var;
  403. my $paid = "a.paid";
  404. my $ml = 1;
  405. my $ARAP = 'AR';
  406. my $table = 'ar';
  407. my $buysell = 'buy';
  408. my $acc_trans_join;
  409. my $acc_trans_flds;
  410. if ($form->{vc} eq 'vendor') {
  411. $ml = -1;
  412. $ARAP = 'AP';
  413. $table = 'ap';
  414. $buysell = 'sell';
  415. }
  416. ($form->{transdatefrom}, $form->{transdateto}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  417. if ($form->{outstanding}) {
  418. $paid = qq|SELECT SUM(ac.amount) * -1 * $ml
  419. FROM acc_trans ac
  420. JOIN chart c ON (c.id = ac.chart_id)
  421. WHERE ac.trans_id = a.id
  422. AND (c.link LIKE '%${ARAP}_paid%' OR c.link = '')|;
  423. $paid .= qq|
  424. AND ac.transdate <= '$form->{transdateto}'| if $form->{transdateto};
  425. $form->{summary} = 1;
  426. }
  427. if (!$form->{summary}) {
  428. $acc_trans_flds = qq|, c.accno, ac.source,
  429. pr.projectnumber, ac.memo AS description,
  430. ac.amount AS linetotal,
  431. i.description AS linedescription|;
  432. $acc_trans_join = qq| JOIN acc_trans ac ON (a.id = ac.trans_id)
  433. JOIN chart c ON (c.id = ac.chart_id)
  434. LEFT JOIN project pr ON (pr.id = ac.project_id)
  435. LEFT JOIN invoice i ON (i.id = ac.invoice_id)|;
  436. }
  437. my $query = qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
  438. a.duedate, a.netamount, a.amount, ($paid) AS paid,
  439. a.invoice, a.datepaid, a.terms, a.notes,
  440. a.shipvia, a.shippingpoint, e.name AS employee, vc.name,
  441. a.$form->{vc}_id, a.till, m.name AS manager, a.curr,
  442. ex.$buysell AS exchangerate, d.description AS department,
  443. a.ponumber $acc_trans_flds
  444. FROM $table a
  445. JOIN $form->{vc} vc ON (a.$form->{vc}_id = vc.id)
  446. LEFT JOIN employee e ON (a.employee_id = e.id)
  447. LEFT JOIN employee m ON (e.managerid = m.id)
  448. LEFT JOIN exchangerate ex ON (ex.curr = a.curr
  449. AND ex.transdate = a.transdate)
  450. LEFT JOIN department d ON (a.department_id = d.id)
  451. $acc_trans_join|;
  452. my %ordinal = ( id => 1,
  453. invnumber => 2,
  454. ordnumber => 3,
  455. transdate => 4,
  456. duedate => 5,
  457. datepaid => 10,
  458. shipvia => 13,
  459. shippingpoint => 14,
  460. employee => 15,
  461. name => 16,
  462. manager => 19,
  463. curr => 20,
  464. department => 22,
  465. ponumber => 23,
  466. accno => 24,
  467. source => 25,
  468. project => 26,
  469. description => 27);
  470. my @a = (transdate, invnumber, name);
  471. push @a, "employee" if $form->{l_employee};
  472. push @a, "manager" if $form->{l_manager};
  473. my $sortorder = $form->sort_order(\@a, \%ordinal);
  474. my $where = "1 = 1";
  475. if ($form->{"$form->{vc}_id"}) {
  476. $where .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  477. } else {
  478. if ($form->{$form->{vc}}) {
  479. $var = $form->like(lc $form->{$form->{vc}});
  480. $where .= " AND lower(vc.name) LIKE '$var'";
  481. }
  482. }
  483. for (qw(department employee)) {
  484. if ($form->{$_}) {
  485. ($null, $var) = split /--/, $form->{$_};
  486. $where .= " AND a.${_}_id = $var";
  487. }
  488. }
  489. for (qw(invnumber ordnumber)) {
  490. if ($form->{$_}) {
  491. $var = $form->like(lc $form->{$_});
  492. $where .= " AND lower(a.$_) LIKE '$var'";
  493. $form->{open} = $form->{closed} = 0;
  494. }
  495. }
  496. if ($form->{partsid}){
  497. $where .= " AND a.id IN (select trans_id FROM invoice
  498. WHERE parts_id = $form->{partsid})";
  499. }
  500. for (qw(ponumber shipvia notes)) {
  501. if ($form->{$_}) {
  502. $var = $form->like(lc $form->{$_});
  503. $where .= " AND lower(a.$_) LIKE '$var'";
  504. }
  505. }
  506. if ($form->{description}) {
  507. if ($acc_trans_flds) {
  508. $var = $form->like(lc $form->{description});
  509. $where .= " AND lower(ac.memo) LIKE '$var'
  510. OR lower(i.description) LIKE '$var'";
  511. } else {
  512. $where .= " AND a.id = 0";
  513. }
  514. }
  515. if ($form->{source}) {
  516. if ($acc_trans_flds) {
  517. $var = $form->like(lc $form->{source});
  518. $where .= " AND lower(ac.source) LIKE '$var'";
  519. } else {
  520. $where .= " AND a.id = 0";
  521. }
  522. }
  523. $where .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
  524. $where .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
  525. if ($form->{open} || $form->{closed}) {
  526. unless ($form->{open} && $form->{closed}) {
  527. $where .= " AND a.amount != a.paid" if ($form->{open});
  528. $where .= " AND a.amount = a.paid" if ($form->{closed});
  529. }
  530. }
  531. if ($form->{till} ne "") {
  532. $where .= " AND a.invoice = '1'
  533. AND a.till IS NOT NULL";
  534. if ($myconfig->{role} eq 'user') {
  535. $where .= " AND e.login = '$form->{login}'";
  536. }
  537. }
  538. if ($form->{$ARAP}) {
  539. my ($accno) = split /--/, $form->{$ARAP};
  540. $where .= qq|AND a.id IN (SELECT ac.trans_id
  541. FROM acc_trans ac
  542. JOIN chart c ON (c.id = ac.chart_id)
  543. WHERE a.id = ac.trans_id
  544. AND c.accno = '$accno')|;
  545. }
  546. if ($form->{description}) {
  547. $var = $form->like(lc $form->{description});
  548. $where .= qq| AND (a.id IN (SELECT DISTINCT trans_id
  549. FROM acc_trans
  550. WHERE lower(memo) LIKE '$var')
  551. OR a.id IN (SELECT DISTINCT trans_id
  552. FROM invoice
  553. WHERE lower(description) LIKE '$var'))|;
  554. }
  555. $query .= "WHERE $where
  556. ORDER BY $sortorder";
  557. my $sth = $dbh->prepare($query);
  558. $sth->execute || $form->dberror($query);
  559. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  560. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  561. if ($ref->{linetotal} <= 0) {
  562. $ref->{debit} = $ref->{linetotal} * -1;
  563. $ref->{credit} = 0;
  564. } else {
  565. $ref->{debit} = 0;
  566. $ref->{credit} = $ref->{linetotal};
  567. }
  568. if ($ref->{invoice}) {
  569. $ref->{description} ||= $ref->{linedescription};
  570. }
  571. if ($form->{outstanding}) {
  572. next if $form->round_amount($ref->{amount}, 2) == $form->round_amount($ref->{paid}, 2);
  573. }
  574. push @{ $form->{transactions} }, $ref;
  575. }
  576. $sth->finish;
  577. $dbh->disconnect;
  578. }
  579. # this is used in IS, IR to retrieve the name
  580. sub get_name {
  581. my ($self, $myconfig, $form) = @_;
  582. # connect to database
  583. my $dbh = $form->dbconnect($myconfig);
  584. my $dateformat = $myconfig->{dateformat};
  585. if ($myconfig->{dateformat} !~ /^y/) {
  586. my @a = split /\W/, $form->{transdate};
  587. $dateformat .= "yy" if (length $a[2] > 2);
  588. }
  589. if ($form->{transdate} !~ /\W/) {
  590. $dateformat = 'yyyymmdd';
  591. }
  592. my $duedate;
  593. if ($myconfig->{dbdriver} eq 'DB2') {
  594. $duedate = ($form->{transdate}) ? "date('$form->{transdate}') + c.terms DAYS" : "current_date + c.terms DAYS";
  595. } else {
  596. $duedate = ($form->{transdate}) ? "to_date('$form->{transdate}', '$dateformat') + c.terms" : "current_date + c.terms";
  597. }
  598. $form->{"$form->{vc}_id"} *= 1;
  599. # get customer/vendor
  600. my $query = qq|SELECT c.name AS $form->{vc}, c.discount, c.creditlimit, c.terms,
  601. c.email, c.cc, c.bcc, c.taxincluded,
  602. c.address1, c.address2, c.city, c.state,
  603. c.zipcode, c.country, c.curr AS currency, c.language_code,
  604. $duedate AS duedate, c.notes AS intnotes,
  605. b.discount AS tradediscount, b.description AS business,
  606. e.name AS employee, e.id AS employee_id
  607. FROM $form->{vc} c
  608. LEFT JOIN business b ON (b.id = c.business_id)
  609. LEFT JOIN employee e ON (e.id = c.employee_id)
  610. WHERE c.id = $form->{"$form->{vc}_id"}|;
  611. my $sth = $dbh->prepare($query);
  612. $sth->execute || $form->dberror($query);
  613. $ref = $sth->fetchrow_hashref(NAME_lc);
  614. if ($form->{id}) {
  615. for (qw(currency employee employee_id intnotes)) { delete $ref->{$_} }
  616. }
  617. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  618. $sth->finish;
  619. my $buysell = ($form->{vc} eq 'customer') ? "buy" : "sell";
  620. # if no currency use defaultcurrency
  621. $form->{currency} = ($form->{currency}) ? $form->{currency} : $form->{defaultcurrency};
  622. $form->{exchangerate} = 0 if $form->{currency} eq $form->{defaultcurrency};
  623. if ($form->{transdate} && ($form->{currency} ne $form->{defaultcurrency})) {
  624. $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate}, $buysell);
  625. }
  626. $form->{forex} = $form->{exchangerate};
  627. # if no employee, default to login
  628. ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh) unless $form->{employee_id};
  629. my $arap = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
  630. my $ARAP = uc $arap;
  631. $form->{creditremaining} = $form->{creditlimit};
  632. $query = qq|SELECT SUM(amount - paid)
  633. FROM $arap
  634. WHERE $form->{vc}_id = $form->{"$form->{vc}_id"}|;
  635. $sth = $dbh->prepare($query);
  636. $sth->execute || $form->dberror($query);
  637. ($form->{creditremaining}) -= $sth->fetchrow_array;
  638. $sth->finish;
  639. $query = qq|SELECT o.amount, (SELECT e.$buysell FROM exchangerate e
  640. WHERE e.curr = o.curr
  641. AND e.transdate = o.transdate)
  642. FROM oe o
  643. WHERE o.$form->{vc}_id = $form->{"$form->{vc}_id"}
  644. AND o.quotation = '0'
  645. AND o.closed = '0'|;
  646. $sth = $dbh->prepare($query);
  647. $sth->execute || $form->dberror($query);
  648. while (my ($amount, $exch) = $sth->fetchrow_array) {
  649. $exch = 1 unless $exch;
  650. $form->{creditremaining} -= $amount * $exch;
  651. }
  652. $sth->finish;
  653. # get shipto if we did not converted an order or invoice
  654. if (!$form->{shipto}) {
  655. for (qw(shiptoname shiptoaddress1 shiptoaddress2 shiptocity
  656. shiptostate shiptozipcode shiptocountry shiptocontact
  657. shiptophone shiptofax shiptoemail)) {
  658. delete $form->{$_}
  659. }
  660. ## needs fixing (SELECT *)
  661. $query = qq|SELECT *
  662. FROM shipto
  663. WHERE trans_id = $form->{"$form->{vc}_id"}|;
  664. $sth = $dbh->prepare($query);
  665. $sth->execute || $form->dberror($query);
  666. $ref = $sth->fetchrow_hashref(NAME_lc);
  667. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  668. $sth->finish;
  669. }
  670. # get taxes
  671. $query = qq|SELECT c.accno
  672. FROM chart c
  673. JOIN $form->{vc}tax ct ON (ct.chart_id = c.id)
  674. WHERE ct.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  675. $sth = $dbh->prepare($query);
  676. $sth->execute || $form->dberror($query);
  677. my %tax;
  678. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  679. $tax{$ref->{accno}} = 1;
  680. }
  681. $sth->finish;
  682. my $where = qq|AND (t.validto >= '$form->{transdate}' OR t.validto IS NULL)| if $form->{transdate};
  683. # get tax rates and description
  684. $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
  685. FROM chart c
  686. JOIN tax t ON (c.id = t.chart_id)
  687. WHERE c.link LIKE '%${ARAP}_tax%'
  688. $where
  689. ORDER BY accno, validto|;
  690. $sth = $dbh->prepare($query);
  691. $sth->execute || $form->dberror($query);
  692. $form->{taxaccounts} = "";
  693. my %a = ();
  694. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  695. if ($tax{$ref->{accno}}) {
  696. if (not exists $a{$ref->{accno}}) {
  697. for (qw(rate description taxnumber)) { $form->{"$ref->{accno}_$_"} = $ref->{$_} }
  698. $form->{taxaccounts} .= "$ref->{accno} ";
  699. $a{$ref->{accno}} = 1;
  700. }
  701. }
  702. }
  703. $sth->finish;
  704. chop $form->{taxaccounts};
  705. # setup last accounts used for this customer/vendor
  706. if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
  707. $query = qq|SELECT c.accno, c.description, c.link, c.category,
  708. ac.project_id, p.projectnumber, a.department_id,
  709. d.description AS department
  710. FROM chart c
  711. JOIN acc_trans ac ON (ac.chart_id = c.id)
  712. JOIN $arap a ON (a.id = ac.trans_id)
  713. LEFT JOIN project p ON (ac.project_id = p.id)
  714. LEFT JOIN department d ON (d.id = a.department_id)
  715. WHERE a.$form->{vc}_id = $form->{"$form->{vc}_id"}
  716. AND a.id IN (SELECT max(id)
  717. FROM $arap
  718. WHERE $form->{vc}_id = $form->{"$form->{vc}_id"})|;
  719. $sth = $dbh->prepare($query);
  720. $sth->execute || $form->dberror($query);
  721. my $i = 0;
  722. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  723. $form->{department} = $ref->{department};
  724. $form->{department_id} = $ref->{department_id};
  725. if ($ref->{link} =~ /_amount/) {
  726. $i++;
  727. $form->{"$form->{ARAP}_amount_$i"} = "$ref->{accno}--$ref->{description}" if $ref->{accno};
  728. $form->{"projectnumber_$i"} = "$ref->{projectnumber}--$ref->{project_id}" if $ref->{project_id};
  729. }
  730. if ($ref->{link} eq $form->{ARAP}) {
  731. $form->{$form->{ARAP}} = $form->{"$form->{ARAP}_1"} = "$ref->{accno}--$ref->{description}" if $ref->{accno};
  732. }
  733. }
  734. $sth->finish;
  735. $form->{rowcount} = $i if ($i && !$form->{type});
  736. }
  737. $dbh->disconnect;
  738. }
  739. 1;