summaryrefslogtreecommitdiff
path: root/LedgerSMB/IS.pm
blob: 5cc991c67494bfe7283fc534310f8f3d95c5ce69 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2000
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors: Jim Rawlings <jim@your-dba.com>
  23. #`
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # Inventory invoicing module
  31. #
  32. #======================================================================
  33. package IS;
  34. use LedgerSMB::Tax;
  35. use LedgerSMB::PriceMatrix;
  36. use LedgerSMB::Sysconfig;
  37. sub invoice_details {
  38. use LedgerSMB::CP;
  39. my ( $self, $myconfig, $form ) = @_;
  40. $form->{duedate} = $form->{transdate} unless ( $form->{duedate} );
  41. # connect to database
  42. my $dbh = $form->{dbh};
  43. my $query = qq|
  44. SELECT ?::date - ?::date
  45. AS terms, value
  46. FROM defaults
  47. WHERE setting_key = 'weightunit'|;
  48. my $sth = $dbh->prepare($query);
  49. $sth->execute( $form->{duedate}, $form->{transdate} )
  50. || $form->dberror($query);
  51. ( $form->{terms}, $form->{weightunit} ) = $sth->fetchrow_array;
  52. $sth->finish;
  53. # this is for the template
  54. $form->{invdate} = $form->{transdate};
  55. my $tax = 0;
  56. my $item;
  57. my $i;
  58. my @sortlist = ();
  59. my $projectnumber;
  60. my $projectdescription;
  61. my $projectnumber_id;
  62. my $translation;
  63. my $partsgroup;
  64. my @taxaccounts;
  65. my %taxaccounts;
  66. my $tax;
  67. my $taxrate;
  68. my $taxamount;
  69. my %translations;
  70. $query = qq|
  71. SELECT p.description, t.description
  72. FROM project p
  73. LEFT JOIN translation t
  74. ON (t.trans_id = p.id
  75. AND t.language_code = ?)
  76. WHERE id = ?|;
  77. my $prh = $dbh->prepare($query) || $form->dberror($query);
  78. $query = qq|
  79. SELECT inventory_accno_id, income_accno_id,
  80. expense_accno_id, assembly, weight FROM parts
  81. WHERE id = ?|;
  82. my $pth = $dbh->prepare($query) || $form->dberror($query);
  83. my $sortby;
  84. # sort items by project and partsgroup
  85. for $i ( 1 .. $form->{rowcount} - 1 ) {
  86. # account numbers
  87. $pth->execute( $form->{"id_$i"} );
  88. $ref = $pth->fetchrow_hashref(NAME_lc);
  89. $form->db_parse_numeric(sth=>$pth, hashref=>$ref);
  90. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  91. $pth->finish;
  92. $projectnumber_id = 0;
  93. $projectnumber = "";
  94. $form->{partsgroup} = "";
  95. $form->{projectnumber} = "";
  96. if ( $form->{groupprojectnumber} || $form->{grouppartsgroup} ) {
  97. $inventory_accno_id =
  98. ( $form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"} )
  99. ? "1"
  100. : "";
  101. if ( $form->{groupprojectnumber} ) {
  102. ( $projectnumber, $projectnumber_id ) =
  103. split /--/, $form->{"projectnumber_$i"};
  104. }
  105. if ( $form->{grouppartsgroup} ) {
  106. ( $form->{partsgroup} ) =
  107. split /--/, $form->{"partsgroup_$i"};
  108. }
  109. if ( $projectnumber_id && $form->{groupprojectnumber} ) {
  110. if ( $translation{$projectnumber_id} ) {
  111. $form->{projectnumber} = $translation{$projectnumber_id};
  112. }
  113. else {
  114. # get project description
  115. $prh->execute( $projectnumber_id, $form->{language_code} );
  116. ( $projectdescription, $translation ) =
  117. $prh->fetchrow_array;
  118. $prh->finish;
  119. $form->{projectnumber} =
  120. ($translation)
  121. ? "$projectnumber, $translation"
  122. : "$projectnumber, " . "$projectdescription";
  123. $translation{$projectnumber_id} = $form->{projectnumber};
  124. }
  125. }
  126. if ( $form->{grouppartsgroup} && $form->{partsgroup} ) {
  127. $form->{projectnumber} .= " / "
  128. if $projectnumber_id;
  129. $form->{projectnumber} .= $form->{partsgroup};
  130. }
  131. $form->format_string(projectnumber);
  132. }
  133. $sortby = qq|$projectnumber$form->{partsgroup}|;
  134. if ( $form->{sortby} ne 'runningnumber' ) {
  135. for (qw(partnumber description bin)) {
  136. $sortby .= $form->{"${_}_$i"}
  137. if $form->{sortby} eq $_;
  138. }
  139. }
  140. push @sortlist,
  141. [
  142. $i,
  143. qq|$projectnumber$form->{partsgroup}| . qq|$inventory_accno_id|,
  144. $form->{projectnumber},
  145. $projectnumber_id,
  146. $form->{partsgroup},
  147. $sortby
  148. ];
  149. }
  150. # sort the whole thing by project and group
  151. @sortlist = sort { $a->[5] cmp $b->[5] } @sortlist;
  152. my $runningnumber = 1;
  153. my $sameitem = "";
  154. my $subtotal;
  155. my $k = scalar @sortlist;
  156. my $j = 0;
  157. foreach $item (@sortlist) {
  158. $i = $item->[0];
  159. $j++;
  160. # heading
  161. if ( $form->{groupprojectnumber} || $form->{grouppartsgroup} ) {
  162. if ( $item->[1] ne $sameitem ) {
  163. $sameitem = $item->[1];
  164. $ok = 0;
  165. if ( $form->{groupprojectnumber} ) {
  166. $ok = $form->{"projectnumber_$i"};
  167. }
  168. if ( $form->{grouppartsgroup} ) {
  169. $ok = $form->{"partsgroup_$i"}
  170. unless $ok;
  171. }
  172. if ($ok) {
  173. if ( $form->{"inventory_accno_id_$i"}
  174. || $form->{"assembly_$i"} )
  175. {
  176. push( @{ $form->{part} }, "" );
  177. push( @{ $form->{service} }, NULL );
  178. }
  179. else {
  180. push( @{ $form->{part} }, NULL );
  181. push( @{ $form->{service} }, "" );
  182. }
  183. push( @{ $form->{description} }, $item->[2] );
  184. for (
  185. qw(taxrates runningnumber number
  186. sku serialnumber bin qty ship
  187. unit deliverydate projectnumber
  188. sellprice listprice netprice
  189. discount discountrate linetotal
  190. weight itemnotes)
  191. )
  192. {
  193. push( @{ $form->{$_} }, "" );
  194. }
  195. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  196. }
  197. }
  198. }
  199. $form->{"qty_$i"} = $form->parse_amount( $myconfig, $form->{"qty_$i"} );
  200. if ( $form->{"qty_$i"} ) {
  201. $form->{totalqty} += $form->{"qty_$i"};
  202. $form->{totalship} += $form->{"qty_$i"};
  203. $form->{totalweight} +=
  204. ( $form->{"qty_$i"} * $form->{"weight_$i"} );
  205. $form->{totalweightship} +=
  206. ( $form->{"qty_$i"} * $form->{"weight_$i"} );
  207. # add number, description and qty to $form->{number}...
  208. push( @{ $form->{runningnumber} }, $runningnumber++ );
  209. push( @{ $form->{number} }, $form->{"partnumber_$i"} );
  210. push( @{ $form->{sku} }, $form->{"sku_$i"} );
  211. push( @{ $form->{serialnumber} }, $form->{"serialnumber_$i"} );
  212. push( @{ $form->{bin} }, $form->{"bin_$i"} );
  213. push( @{ $form->{description} }, $form->{"description_$i"} );
  214. push( @{ $form->{itemnotes} }, $form->{"notes_$i"} );
  215. push(
  216. @{ $form->{qty} },
  217. $form->format_amount( $myconfig, $form->{"qty_$i"} )
  218. );
  219. push(
  220. @{ $form->{ship} },
  221. $form->format_amount( $myconfig, $form->{"qty_$i"} )
  222. );
  223. push( @{ $form->{unit} }, $form->{"unit_$i"} );
  224. push( @{ $form->{deliverydate} }, $form->{"deliverydate_$i"} );
  225. push( @{ $form->{projectnumber} }, $form->{"projectnumber_$i"} );
  226. push( @{ $form->{sellprice} }, $form->{"sellprice_$i"} );
  227. push( @{ $form->{listprice} }, $form->{"listprice_$i"} );
  228. push(
  229. @{ $form->{weight} },
  230. $form->format_amount(
  231. $myconfig, $form->{"weight_$i"} * $form->{"qty_$i"}
  232. )
  233. );
  234. my $sellprice =
  235. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  236. my ($dec) = ( $sellprice =~ /\.(\d+)/ );
  237. $dec = length $dec;
  238. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  239. my $discount = $form->round_amount(
  240. $sellprice *
  241. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) /
  242. 100,
  243. $decimalplaces
  244. );
  245. # keep a netprice as well, (sellprice - discount)
  246. $form->{"netprice_$i"} = $sellprice - $discount;
  247. my $linetotal =
  248. $form->round_amount( $form->{"qty_$i"} * $form->{"netprice_$i"},
  249. 2 );
  250. if ( $form->{"inventory_accno_id_$i"}
  251. || $form->{"assembly_$i"} )
  252. {
  253. push( @{ $form->{part} }, $form->{"sku_$i"} );
  254. push( @{ $form->{service} }, NULL );
  255. $form->{totalparts} += $linetotal;
  256. }
  257. else {
  258. push( @{ $form->{service} }, $form->{"sku_$i"} );
  259. push( @{ $form->{part} }, NULL );
  260. $form->{totalservices} += $linetotal;
  261. }
  262. push(
  263. @{ $form->{netprice} },
  264. ( $form->{"netprice_$i"} )
  265. ? $form->format_amount( $myconfig, $form->{"netprice_$i"},
  266. $decimalplaces )
  267. : " "
  268. );
  269. $discount =
  270. ($discount)
  271. ? $form->format_amount( $myconfig, $discount * -1,
  272. $decimalplaces )
  273. : " ";
  274. push( @{ $form->{discount} }, $discount );
  275. push(
  276. @{ $form->{discountrate} },
  277. $form->format_amount( $myconfig, $form->{"discount_$i"} )
  278. );
  279. $form->{total} += $linetotal;
  280. # this is for the subtotals for grouping
  281. $subtotal += $linetotal;
  282. $form->{"linetotal_$i"} =
  283. $form->format_amount( $myconfig, $linetotal, 2 );
  284. $form->{"linetotal_$i"} = '0.00' unless $form->{"linetotal_$i"};
  285. push( @{ $form->{linetotal} }, $form->{"linetotal_$i"} );
  286. @taxaccounts = Tax::init_taxes(
  287. $form,
  288. $form->{"taxaccounts_$i"},
  289. $form->{"taxaccounts"}
  290. );
  291. my $ml = 1;
  292. my @taxrates = ();
  293. $tax = 0;
  294. if ( $form->{taxincluded} ) {
  295. $taxamount =
  296. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  297. $taxbase = ( $linetotal - $taxamount );
  298. $tax += Tax::extract_taxes( \@taxaccounts, $form, $linetotal );
  299. }
  300. else {
  301. $taxamount =
  302. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  303. $tax += Tax::apply_taxes( \@taxaccounts, $form, $linetotal );
  304. }
  305. foreach $item (@taxaccounts) {
  306. push @taxrates, 100 * $item->rate;
  307. $taxaccounts{ $item->account } += $item->value;
  308. if ( $form->{taxincluded} ) {
  309. $taxbase{ $item->account } += $taxbase;
  310. }
  311. else {
  312. $taxbase{ $item->account } += $linetotal;
  313. }
  314. }
  315. push(
  316. @{ $form->{lineitems} },
  317. {
  318. amount => $linetotal,
  319. tax => $form->round_amount( $tax, 2 )
  320. }
  321. );
  322. push( @{ $form->{taxrates} },
  323. join ' ', sort { $a <=> $b } @taxrates );
  324. if ( $form->{"assembly_$i"} ) {
  325. $form->{stagger} = -1;
  326. &assembly_details( $myconfig, $form, $dbh, $form->{"id_$i"},
  327. $oid{ $myconfig->{dbdriver} },
  328. $form->{"qty_$i"} );
  329. }
  330. }
  331. # add subtotal
  332. if ( $form->{groupprojectnumber} || $form->{grouppartsgroup} ) {
  333. if ($subtotal) {
  334. if ( $j < $k ) {
  335. # look at next item
  336. if ( $sortlist[$j]->[1] ne $sameitem ) {
  337. if ( $form->{"inventory_accno_id_$j"}
  338. || $form->{"assembly_$i"} )
  339. {
  340. push( @{ $form->{part} }, "" );
  341. push( @{ $form->{service} }, NULL );
  342. }
  343. else {
  344. push( @{ $form->{service} }, "" );
  345. push( @{ $form->{part} }, NULL );
  346. }
  347. for (
  348. qw(taxrates
  349. runningnumber number sku
  350. serialnumber bin qty
  351. ship unit deliverydate
  352. projectnumber sellprice
  353. listprice netprice
  354. discount discountrate
  355. weight itemnotes)
  356. )
  357. {
  358. push( @{ $form->{$_} }, "" );
  359. }
  360. push(
  361. @{ $form->{description} },
  362. $form->{groupsubtotaldescription}
  363. );
  364. push(
  365. @{ $form->{lineitems} },
  366. {
  367. amount => 0,
  368. tax => 0
  369. }
  370. );
  371. if ( $form->{groupsubtotaldescription} ne "" ) {
  372. push(
  373. @{ $form->{linetotal} },
  374. $form->format_amount( $myconfig, $subtotal, 2 )
  375. );
  376. }
  377. else {
  378. push( @{ $form->{linetotal} }, "" );
  379. }
  380. $subtotal = 0;
  381. }
  382. }
  383. else {
  384. # got last item
  385. if ( $form->{groupsubtotaldescription} ne "" ) {
  386. if ( $form->{"inventory_accno_id_$j"}
  387. || $form->{"assembly_$i"} )
  388. {
  389. push( @{ $form->{part} }, "" );
  390. push( @{ $form->{service} }, NULL );
  391. }
  392. else {
  393. push( @{ $form->{service} }, "" );
  394. push( @{ $form->{part} }, NULL );
  395. }
  396. for (
  397. qw(taxrates
  398. runningnumber number sku
  399. serialnumber bin qty
  400. ship unit deliverydate
  401. projectnumber sellprice
  402. listprice netprice
  403. discount discountrate
  404. weight itemnotes)
  405. )
  406. {
  407. push( @{ $form->{$_} }, "" );
  408. }
  409. push(
  410. @{ $form->{description} },
  411. $form->{groupsubtotaldescription}
  412. );
  413. push(
  414. @{ $form->{linetotal} },
  415. $form->format_amount( $myconfig, $subtotal, 2 )
  416. );
  417. push(
  418. @{ $form->{lineitems} },
  419. {
  420. amount => 0,
  421. tax => 0
  422. }
  423. );
  424. }
  425. }
  426. }
  427. }
  428. }
  429. $tax = 0;
  430. foreach my $item ( sort keys %taxaccounts ) {
  431. if ( $form->round_amount( $taxaccounts{$item}, 2 ) ) {
  432. $tax += $taxamount = $form->round_amount( $taxaccounts{$item}, 2 );
  433. push(
  434. @{ $form->{taxbaseinclusive} },
  435. $form->{"${item}_taxbaseinclusive"} =
  436. $form->format_amount( $myconfig, $taxbase{$item} + $tax, 2 )
  437. );
  438. push(
  439. @{ $form->{taxbase} },
  440. $form->{"${item}_taxbase"} =
  441. $form->format_amount( $myconfig, $taxbase{$item}, 2 )
  442. );
  443. push(
  444. @{ $form->{tax} },
  445. $form->{"${item}_tax"} =
  446. $form->format_amount( $myconfig, $taxamount, 2 )
  447. );
  448. push( @{ $form->{taxdescription} },
  449. $form->{"${item}_description"} );
  450. $form->{"${item}_taxrate"} =
  451. $form->format_amount( $myconfig, $form->{"${item}_rate"} * 100 );
  452. push( @{ $form->{taxrate} }, $form->{"${item}_taxrate"} );
  453. push( @{ $form->{taxnumber} }, $form->{"${item}_taxnumber"} );
  454. }
  455. }
  456. # adjust taxes for lineitems
  457. my $total = 0;
  458. for ( @{ $form->{lineitems} } ) {
  459. $total += $_->{tax};
  460. }
  461. if ( $form->round_amount( $total, 2 ) != $form->round_amount( $tax, 2 ) ) {
  462. # get largest amount
  463. for ( reverse sort { $a->{tax} <=> $b->{tax} } @{ $form->{lineitems} } )
  464. {
  465. $_->{tax} -= $total - $tax;
  466. last;
  467. }
  468. }
  469. $i = 1;
  470. for ( @{ $form->{lineitems} } ) {
  471. push(
  472. @{ $form->{linetax} },
  473. $form->format_amount( $myconfig, $_->{tax}, 2, "" )
  474. );
  475. }
  476. for $i ( 1 .. $form->{paidaccounts} ) {
  477. if ( $form->{"paid_$i"} ) {
  478. push( @{ $form->{payment} }, $form->{"paid_$i"} );
  479. my ( $accno, $description ) = split /--/, $form->{"AR_paid_$i"};
  480. push( @{ $form->{paymentaccount} }, $description );
  481. push( @{ $form->{paymentdate} }, $form->{"datepaid_$i"} );
  482. push( @{ $form->{paymentsource} }, $form->{"source_$i"} );
  483. push( @{ $form->{paymentmemo} }, $form->{"memo_$i"} );
  484. $form->{paid} +=
  485. $form->parse_amount( $myconfig, $form->{"paid_$i"} );
  486. }
  487. }
  488. for (qw(totalparts totalservices)) {
  489. $form->{$_} = $form->format_amount( $myconfig, $form->{$_}, 2 );
  490. }
  491. for (qw(totalqty totalship totalweight)) {
  492. $form->{$_} = $form->format_amount( $myconfig, $form->{$_} );
  493. }
  494. $form->{subtotal} = $form->format_amount( $myconfig, $form->{total}, 2 );
  495. $form->{subtotal} = '0.00' unless $form->{subtotal};
  496. $form->{invtotal} =
  497. ( $form->{taxincluded} ) ? $form->{total} : $form->{total} + $tax;
  498. my $c;
  499. if ( $form->{language_code} ne "" ) {
  500. $c = new CP $form->{language_code};
  501. }
  502. else {
  503. $c = new CP $myconfig->{countrycode};
  504. }
  505. $c->init;
  506. my $whole;
  507. ( $whole, $form->{decimal} ) = split /\./, $form->{invtotal};
  508. $form->{decimal} .= "00";
  509. $form->{decimal} = substr( $form->{decimal}, 0, 2 );
  510. $form->{text_decimal} = $c->num2text( $form->{decimal} * 1 );
  511. $form->{text_amount} = $c->num2text($whole);
  512. $form->{integer_amount} = $form->format_amount( $myconfig, $whole );
  513. $form->format_string(qw(text_amount text_decimal));
  514. $form->{total} =
  515. $form->format_amount( $myconfig, $form->{invtotal} - $form->{paid}, 2 );
  516. $form->{invtotal} = $form->format_amount( $myconfig, $form->{invtotal}, 2 );
  517. $form->{paid} = $form->format_amount( $myconfig, $form->{paid}, 2 );
  518. $dbh->commit;
  519. }
  520. sub assembly_details {
  521. my ( $myconfig, $form, $dbh2, $id, $oid, $qty ) = @_;
  522. $dbh = $form->{dbh};
  523. my $sm = "";
  524. my $spacer;
  525. $form->{stagger}++;
  526. if ( $form->{format} eq 'html' ) {
  527. $spacer = "&nbsp;" x ( 3 * ( $form->{stagger} - 1 ) )
  528. if $form->{stagger} > 1;
  529. }
  530. if ( $form->{format} =~ /(postscript|pdf)/ ) {
  531. if ( $form->{stagger} > 1 ) {
  532. $spacer = ( $form->{stagger} - 1 ) * 3;
  533. $spacer = '\rule{' . $spacer . 'mm}{0mm}';
  534. }
  535. }
  536. # get parts and push them onto the stack
  537. my $sortorder = "";
  538. if ( $form->{grouppartsgroup} ) {
  539. $sortorder = qq|ORDER BY pg.partsgroup|;
  540. }
  541. my $query = qq|
  542. SELECT p.partnumber, p.description, p.unit, a.qty,
  543. pg.partsgroup, p.partnumber AS sku
  544. FROM assembly a
  545. JOIN parts p ON (a.parts_id = p.id)
  546. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  547. WHERE a.bom = '1'
  548. AND a.id = ?
  549. $sortorder|;
  550. my $sth = $dbh->prepare($query);
  551. $sth->execute($id) || $form->dberror($query);
  552. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  553. for (qw(partnumber description partsgroup)) {
  554. $form->{"a_$_"} = $ref->{$_};
  555. $form->format_string("a_$_");
  556. }
  557. if ( $form->{grouppartsgroup} && $ref->{partsgroup} ne $sm ) {
  558. for (
  559. qw(taxrates runningnumber number sku
  560. serialnumber unit qty ship bin deliverydate
  561. projectnumber sellprice listprice netprice
  562. discount discountrate linetotal weight
  563. itemnotes)
  564. )
  565. {
  566. push( @{ $form->{$_} }, "" );
  567. }
  568. $sm =
  569. ( $form->{"a_partsgroup"} )
  570. ? $form->{"a_partsgroup"}
  571. : "--";
  572. push( @{ $form->{description} }, "$spacer$sm" );
  573. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  574. }
  575. if ( $form->{stagger} ) {
  576. push(
  577. @{ $form->{description} },
  578. $form->format_amount( $myconfig,
  579. $ref->{qty} * $form->{"qty_$i"} )
  580. . qq| -- $form->{"a_partnumber"}|
  581. . qq|, $form->{"a_description"}|
  582. );
  583. for (
  584. qw(taxrates runningnumber number sku
  585. serialnumber unit qty ship bin deliverydate
  586. projectnumber sellprice listprice netprice
  587. discount discountrate linetotal weight
  588. itemnotes)
  589. )
  590. {
  591. push( @{ $form->{$_} }, "" );
  592. }
  593. }
  594. else {
  595. push( @{ $form->{description} }, qq|$form->{"a_description"}| );
  596. push( @{ $form->{number} }, $form->{"a_partnumber"} );
  597. push( @{ $form->{sku} }, $form->{"a_partnumber"} );
  598. for (
  599. qw(taxrates runningnumber ship serialnumber
  600. reqdate projectnumber sellprice listprice
  601. netprice discount discountrate linetotal weight
  602. itemnotes)
  603. )
  604. {
  605. push( @{ $form->{$_} }, "" );
  606. }
  607. }
  608. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  609. push(
  610. @{ $form->{qty} },
  611. $form->format_amount( $myconfig, $ref->{qty} * $qty )
  612. );
  613. for (qw(unit bin)) {
  614. $form->{"a_$_"} = $ref->{$_};
  615. $form->format_string("a_$_");
  616. push( @{ $form->{$_} }, $form->{"a_$_"} );
  617. }
  618. }
  619. $sth->finish;
  620. $form->{stagger}--;
  621. }
  622. sub project_description {
  623. my ( $self, $dbh2, $id ) = @_;
  624. $dbh = $form->{dbh};
  625. my $query = qq|
  626. SELECT description
  627. FROM project
  628. WHERE id = ?|;
  629. $sth = $dbh->prepare($query);
  630. $sth->execute($id);
  631. ($_) = $sth->fetchrow_array;
  632. $_;
  633. }
  634. sub customer_details {
  635. my ( $self, $myconfig, $form ) = @_;
  636. my $dbh = $form->{dbh};
  637. # get rest for the customer
  638. my $query = qq|
  639. SELECT customernumber, name, address1, address2, city,
  640. state, zipcode, country,
  641. contact, phone as customerphone, fax as customerfax,
  642. taxnumber AS customertaxnumber, sic_code AS sic, iban,
  643. bic, startdate, enddate
  644. FROM customer
  645. WHERE id = ?|;
  646. my $sth = $dbh->prepare($query);
  647. $sth->execute( $form->{customer_id} ) || $form->dberror($query);
  648. $ref = $sth->fetchrow_hashref(NAME_lc);
  649. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  650. $sth->finish;
  651. }
  652. sub post_invoice {
  653. my ( $self, $myconfig, $form ) = @_;
  654. my $dbh = $form->{dbh};
  655. $form->{invnumber} = $form->update_defaults( $myconfig, "sinumber", $dbh )
  656. unless $form->{invnumber};
  657. my $query;
  658. my $sth;
  659. my $null;
  660. my $project_id;
  661. my $exchangerate = 0;
  662. my $keepcleared = 0;
  663. %$form->{acc_trans} = ();
  664. ( $null, $form->{employee_id} ) = split /--/, $form->{employee};
  665. unless ( $form->{employee_id} ) {
  666. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  667. }
  668. ( $null, $form->{department_id} ) = split( /--/, $form->{department} );
  669. $form->{department_id} *= 1;
  670. $query = qq|
  671. SELECT (SELECT value FROM defaults
  672. WHERE setting_key = 'fxgain_accno_id')
  673. AS fxgain_accno_id,
  674. (SELECT value FROM defaults
  675. WHERE setting_key = 'fxloss_accno_id')
  676. AS fxloss_accno_id|;
  677. my ( $fxgain_accno_id, $fxloss_accno_id ) = $dbh->selectrow_array($query);
  678. $query = qq|
  679. SELECT p.assembly, p.inventory_accno_id,
  680. p.income_accno_id, p.expense_accno_id, p.project_id
  681. FROM parts p
  682. WHERE p.id = ?|;
  683. my $pth = $dbh->prepare($query) || $form->dberror($query);
  684. if ( $form->{id} ) {
  685. $keepcleared = 1;
  686. $query = qq|SELECT id FROM ar WHERE id = ?|;
  687. $sth = $dbh->prepare($query);
  688. $sth->execute( $form->{id} );
  689. if ( $sth->fetchrow_array ) {
  690. &reverse_invoice( $dbh, $form );
  691. }
  692. else {
  693. $query = qq|INSERT INTO ar (id, customer_id) VALUES (?, ?)|;
  694. $sth = $dbh->prepare($query);
  695. $sth->execute( $form->{id}, $form->{customer_id} )
  696. || $form->dberror($query);
  697. }
  698. }
  699. my $uid = localtime;
  700. $uid .= "$$";
  701. if ( !$form->{id} ) {
  702. $query = qq|
  703. INSERT INTO ar (invnumber, customer_id, employee_id)
  704. VALUES ('$uid', ?, ?)|;
  705. $sth = $dbh->prepare($query);
  706. $sth->execute( $form->{customer_id}, $form->{employee_id} )
  707. || $form->dberror($query);
  708. $query = qq|SELECT id FROM ar WHERE invnumber = '$uid'|;
  709. $sth = $dbh->prepare($query);
  710. $sth->execute || $form->dberror($query);
  711. ( $form->{id} ) = $sth->fetchrow_array;
  712. $sth->finish;
  713. @queries = $form->run_custom_queries( 'ar', 'INSERT' );
  714. }
  715. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  716. $form->{exchangerate} = 1;
  717. }
  718. else {
  719. $exchangerate =
  720. $form->check_exchangerate( $myconfig, $form->{currency},
  721. $form->{transdate}, 'buy' );
  722. }
  723. $form->{exchangerate} =
  724. ($exchangerate)
  725. ? $exchangerate
  726. : $form->parse_amount( $myconfig, $form->{exchangerate} );
  727. my $i;
  728. my $item;
  729. my $allocated = 0;
  730. my $taxrate;
  731. my $tax;
  732. my $fxtax;
  733. my @taxaccounts;
  734. my $amount;
  735. my $grossamount;
  736. my $invamount = 0;
  737. my $invnetamount = 0;
  738. my $diff = 0;
  739. my $ml;
  740. my $invoice_id;
  741. my $ndx;
  742. foreach $i ( 1 .. $form->{rowcount} ) {
  743. $form->{"qty_$i"} = $form->parse_amount( $myconfig, $form->{"qty_$i"} );
  744. if ( $form->{"qty_$i"} ) {
  745. $pth->execute( $form->{"id_$i"} );
  746. $ref = $pth->fetchrow_hashref(NAME_lc);
  747. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  748. $pth->finish;
  749. # project
  750. if ( $form->{"projectnumber_$i"} ) {
  751. ( $null, $project_id ) = split /--/,
  752. $form->{"projectnumber_$i"};
  753. }
  754. $project_id = $form->{"project_id_$i"}
  755. if $form->{"project_id_$i"};
  756. # keep entered selling price
  757. my $fxsellprice =
  758. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  759. my ($dec) = ( $fxsellprice =~ /\.(\d+)/ );
  760. $dec = length $dec;
  761. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  762. # undo discount formatting
  763. $form->{"discount_$i"} =
  764. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) / 100;
  765. # deduct discount
  766. $form->{"sellprice_$i"} = $fxsellprice -
  767. $form->round_amount( $fxsellprice * $form->{"discount_$i"},
  768. $decimalplaces );
  769. # linetotal
  770. my $fxlinetotal =
  771. $form->round_amount( $form->{"sellprice_$i"} * $form->{"qty_$i"},
  772. 2 );
  773. $amount = $fxlinetotal * $form->{exchangerate};
  774. my $linetotal = $form->round_amount( $amount, 2 );
  775. $fxdiff += $amount - $linetotal;
  776. @taxaccounts = Tax::init_taxes(
  777. $form,
  778. $form->{"taxaccounts_$i"},
  779. $form->{"taxaccounts"}
  780. );
  781. $ml = 1;
  782. $tax = Math::BigFloat->bzero();
  783. $fxtax = Math::BigFloat->bzero();
  784. if ( $form->{taxincluded} ) {
  785. $tax += $amount =
  786. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  787. $form->{"sellprice_$i"} -= $amount / $form->{"qty_$i"};
  788. $fxtax +=
  789. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  790. }
  791. else {
  792. $tax += $amount =
  793. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  794. $fxtax +=
  795. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  796. }
  797. for (@taxaccounts) {
  798. $form->{acc_trans}{ $form->{id} }{ $_->account }{amount} +=
  799. $_->value;
  800. }
  801. $grossamount = $form->round_amount( $linetotal, 2 );
  802. if ( $form->{taxincluded} ) {
  803. $amount = $form->round_amount( $tax, 2 );
  804. $linetotal -= $form->round_amount( $tax - $diff, 2 );
  805. $diff = ( $amount - $tax );
  806. }
  807. # add linetotal to income
  808. $amount = $form->round_amount( $linetotal, 2 );
  809. push @{ $form->{acc_trans}{lineitems} },
  810. {
  811. chart_id => $form->{"income_accno_id_$i"},
  812. amount => $amount,
  813. fxgrossamount => $fxlinetotal + $fxtax,
  814. grossamount => $grossamount,
  815. project_id => $project_id
  816. };
  817. $ndx = $#{ @{ $form->{acc_trans}{lineitems} } };
  818. $form->{"sellprice_$i"} =
  819. $form->round_amount(
  820. $form->{"sellprice_$i"} * $form->{exchangerate},
  821. $decimalplaces );
  822. if ( $form->{"inventory_accno_id_$i"}
  823. || $form->{"assembly_$i"} )
  824. {
  825. if ( $form->{"assembly_$i"} ) {
  826. # If the assembly consists of all
  827. # services, we don't keep inventory,
  828. # so we should not update it
  829. $query = qq|
  830. SELECT sum(
  831. p.inventory_accno_id),
  832. p.assembly
  833. FROM parts p
  834. JOIN assembly a
  835. ON (a.parts_id = p.id)
  836. WHERE a.id = ?
  837. GROUP BY p.assembly|;
  838. $sth = $dbh->prepare($query);
  839. $sth->execute( $form->{"id_$i"} )
  840. || $form->dberror($query);
  841. my ( $inv, $assembly ) = $sth->fetchrow_array;
  842. $sth->finish;
  843. if ( $inv || $assembly ) {
  844. $form->update_balance(
  845. $dbh, "parts", "onhand",
  846. qq|id = | . qq|$form->{"id_$i"}|,
  847. $form->{"qty_$i"} * -1
  848. ) unless $form->{shipped};
  849. }
  850. &process_assembly( $dbh, $form, $form->{"id_$i"},
  851. $form->{"qty_$i"}, $project_id );
  852. }
  853. else {
  854. $form->update_balance(
  855. $dbh, "parts", "onhand",
  856. qq|id = $form->{"id_$i"}|,
  857. $form->{"qty_$i"} * -1
  858. ) unless $form->{shipped};
  859. $allocated = cogs(
  860. $dbh, $form,
  861. $form->{"id_$i"}, $form->{"qty_$i"},
  862. $project_id, $form->{"sellprice_$i"},
  863. );
  864. }
  865. }
  866. # save detail record in invoice table
  867. $query = qq|
  868. INSERT INTO invoice (description)
  869. VALUES ('$uid')|;
  870. $dbh->do($query) || $form->dberror($query);
  871. $query = qq|
  872. SELECT id FROM invoice
  873. WHERE description = '$uid'|;
  874. ($invoice_id) = $dbh->selectrow_array($query);
  875. unless ( $form->{"deliverydate_$i"} ) {
  876. undef $form->{"deliverydate_$i"};
  877. }
  878. $query = qq|
  879. UPDATE invoice
  880. SET trans_id = ?,
  881. parts_id = ?,
  882. description = ?,
  883. qty = ?,
  884. sellprice = ?,
  885. fxsellprice = ?,
  886. discount = ?,
  887. allocated = ?,
  888. unit = ?,
  889. deliverydate = ?,
  890. project_id = ?,
  891. serialnumber = ?,
  892. notes = ?
  893. WHERE id = ?|;
  894. $sth = $dbh->prepare($query);
  895. $sth->execute(
  896. $form->{id}, $form->{"id_$i"},
  897. $form->{"description_$i"}, $form->{"qty_$i"},
  898. $form->{"sellprice_$i"}, $fxsellprice,
  899. $form->{"discount_$i"}, $allocated,
  900. $form->{"unit_$i"}, $form->{"deliverydate_$i"},
  901. $project_id, $form->{"serialnumber_$i"},
  902. $form->{"notes_$i"}, $invoice_id
  903. ) || $form->dberror($query);
  904. # add invoice_id
  905. $form->{acc_trans}{lineitems}[$ndx]->{invoice_id} = $invoice_id;
  906. }
  907. }
  908. $form->{paid} = 0;
  909. for $i ( 1 .. $form->{paidaccounts} ) {
  910. $form->{"paid_$i"} =
  911. $form->parse_amount( $myconfig, $form->{"paid_$i"} );
  912. $form->{paid} += $form->{"paid_$i"};
  913. $form->{datepaid} = $form->{"datepaid_$i"}
  914. if ( $form->{"paid_$i"} );
  915. }
  916. # add lineitems + tax
  917. $amount = 0;
  918. $grossamount = 0;
  919. $fxgrossamount = 0;
  920. for ( @{ $form->{acc_trans}{lineitems} } ) {
  921. $amount += $_->{amount};
  922. $grossamount += $_->{grossamount};
  923. $fxgrossamount += $_->{fxgrossamount};
  924. }
  925. $invnetamount = $amount;
  926. $amount = 0;
  927. for ( split / /, $form->{taxaccounts} ) {
  928. $amount += $form->{acc_trans}{ $form->{id} }{$_}{amount} =
  929. $form->round_amount( $form->{acc_trans}{ $form->{id} }{$_}{amount},
  930. 2 );
  931. }
  932. $invamount = $invnetamount + $amount;
  933. $diff = 0;
  934. if ( $form->{taxincluded} ) {
  935. $diff = $form->round_amount( $grossamount - $invamount, 2 );
  936. $invamount += $diff;
  937. }
  938. $fxdiff = $form->round_amount( $fxdiff, 2 );
  939. $invnetamount += $fxdiff;
  940. $invamount += $fxdiff;
  941. if ( $form->round_amount( $form->{paid} - $fxgrossamount, 2 ) == 0 ) {
  942. $form->{paid} = $invamount;
  943. }
  944. else {
  945. $form->{paid} =
  946. $form->round_amount( $form->{paid} * $form->{exchangerate}, 2 );
  947. }
  948. foreach $ref ( sort { $b->{amount} <=> $a->{amount} }
  949. @{ $form->{acc_trans}{lineitems} } )
  950. {
  951. $amount = $ref->{amount} + $diff + $fxdiff;
  952. $query = qq|
  953. INSERT INTO acc_trans
  954. (trans_id, chart_id, amount,
  955. transdate, project_id, invoice_id)
  956. VALUES (?, ?, ?, ?, ?, ?)|;
  957. $sth = $dbh->prepare($query);
  958. $sth->execute( $form->{id}, $ref->{chart_id}, $amount,
  959. $form->{transdate}, $ref->{project_id}, $ref->{invoice_id} )
  960. || $form->dberror($query);
  961. $diff = 0;
  962. $fxdiff = 0;
  963. }
  964. $form->{receivables} = $invamount * -1;
  965. delete $form->{acc_trans}{lineitems};
  966. # update exchangerate
  967. if ( ( $form->{currency} ne $form->{defaultcurrency} ) && !$exchangerate ) {
  968. $form->update_exchangerate( $dbh, $form->{currency}, $form->{transdate},
  969. $form->{exchangerate}, 0 );
  970. }
  971. # record receivable
  972. if ( $form->{receivables} ) {
  973. ($accno) = split /--/, $form->{AR};
  974. $query = qq|
  975. INSERT INTO acc_trans
  976. (trans_id, chart_id, amount, transdate)
  977. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  978. ?, ?)|;
  979. $sth = $dbh->prepare($query);
  980. $sth->execute( $form->{id}, $accno, $form->{receivables},
  981. $form->{transdate} )
  982. || $form->dberror($query);
  983. }
  984. foreach my $trans_id ( keys %{ $form->{acc_trans} } ) {
  985. foreach my $accno ( keys %{ $form->{acc_trans}{$trans_id} } ) {
  986. $amount =
  987. $form->round_amount(
  988. $form->{acc_trans}{$trans_id}{$accno}{amount}, 2 );
  989. if ($amount) {
  990. $query = qq|
  991. INSERT INTO acc_trans
  992. (trans_id, chart_id, amount,
  993. transdate)
  994. VALUES (?, (SELECT id FROM chart
  995. WHERE accno = ?),
  996. ?, ?)|;
  997. $sth = $dbh->prepare($query);
  998. $sth->execute( $trans_id, $accno, $amount, $form->{transdate} )
  999. || $form->dberror($query);
  1000. }
  1001. }
  1002. }
  1003. # if there is no amount but a payment record receivable
  1004. if ( $invamount == 0 ) {
  1005. $form->{receivables} = 1;
  1006. }
  1007. my $cleared = 0;
  1008. # record payments and offsetting AR
  1009. for $i ( 1 .. $form->{paidaccounts} ) {
  1010. if ( $form->{"paid_$i"} ) {
  1011. my ($accno) = split /--/, $form->{"AR_paid_$i"};
  1012. $form->{"datepaid_$i"} = $form->{transdate}
  1013. unless ( $form->{"datepaid_$i"} );
  1014. $form->{datepaid} = $form->{"datepaid_$i"};
  1015. $exchangerate = 0;
  1016. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  1017. $form->{"exchangerate_$i"} = 1;
  1018. }
  1019. else {
  1020. $exchangerate =
  1021. $form->check_exchangerate( $myconfig, $form->{currency},
  1022. $form->{"datepaid_$i"}, 'buy' );
  1023. $form->{"exchangerate_$i"} =
  1024. ($exchangerate)
  1025. ? $exchangerate
  1026. : $form->parse_amount( $myconfig,
  1027. $form->{"exchangerate_$i"} );
  1028. }
  1029. # record AR
  1030. $amount =
  1031. $form->round_amount( $form->{"paid_$i"} * $form->{exchangerate},
  1032. 2 );
  1033. if ( $form->{receivables} ) {
  1034. $query = qq|
  1035. INSERT INTO acc_trans
  1036. (trans_id, chart_id, amount,
  1037. transdate)
  1038. VALUES (?, (SELECT id FROM chart
  1039. WHERE accno = ?),
  1040. ?, ?)|;
  1041. $sth = $dbh->prepare($query);
  1042. $sth->execute( $form->{id}, $form->{AR}, $amount,
  1043. $form->{"datepaid_$i"} )
  1044. || $form->dberror($query);
  1045. }
  1046. # record payment
  1047. $amount = $form->{"paid_$i"} * -1;
  1048. if ($keepcleared) {
  1049. $cleared = ( $form->{"cleared_$i"} ) ? 1 : 0;
  1050. }
  1051. $query = qq|
  1052. INSERT INTO acc_trans
  1053. (trans_id, chart_id, amount,
  1054. transdate, source, memo, cleared)
  1055. VALUES (?, (SELECT id FROM chart
  1056. WHERE accno = ?),
  1057. ?, ?, ?, ?, ?)|;
  1058. $sth = $dbh->prepare($query);
  1059. $sth->execute( $form->{id}, $accno, $amount, $form->{"datepaid_$i"},
  1060. $form->{"source_$i"}, $form->{"memo_$i"}, $cleared )
  1061. || $form->dberror($query);
  1062. # exchangerate difference
  1063. $amount = $form->round_amount(
  1064. (
  1065. $form->round_amount(
  1066. $form->{"paid_$i"} * $form->{"exchangerate_$i"} -
  1067. $form->{"paid_$i"},
  1068. 2
  1069. )
  1070. ) * -1,
  1071. 2
  1072. );
  1073. if ($amount) {
  1074. $query = qq|
  1075. INSERT INTO acc_trans
  1076. (trans_id, chart_id, amount,
  1077. transdate, source,
  1078. fx_transaction, cleared)
  1079. VALUES (?, (SELECT id FROM chart
  1080. WHERE accno = ?),
  1081. ?, ?, ?, '1', ?)|;
  1082. $sth = $dbh->prepare($query);
  1083. $sth->execute( $form->{id}, $accno, $amount,
  1084. $form->{"datepaid_$i"},
  1085. $form->{"source_$i"}, $cleared )
  1086. || $form->dberror($query);
  1087. }
  1088. # gain/loss
  1089. $amount = $form->round_amount(
  1090. (
  1091. $form->round_amount(
  1092. $form->{"paid_$i"} * $form->{exchangerate}, 2 ) -
  1093. $form->round_amount(
  1094. $form->{"paid_$i"} * $form->{"exchangerate_$i"}, 2
  1095. )
  1096. ) * -1,
  1097. 2
  1098. );
  1099. if ($amount) {
  1100. my $accno_id =
  1101. ( $amount > 0 )
  1102. ? $fxgain_accno_id
  1103. : $fxloss_accno_id;
  1104. $query = qq|
  1105. INSERT INTO acc_trans (
  1106. trans_id, chart_id, amount,
  1107. transdate, fx_transaction,
  1108. cleared)
  1109. VALUES (?, ?, ?, ?, '1', ?)|;
  1110. $sth = $dbh->prepare($query);
  1111. $sth->execute( $form->{id}, $accno_id, $amount,
  1112. $form->{"datepaid_$i"}, $cleared )
  1113. || $form->dberror($query);
  1114. }
  1115. # update exchange rate
  1116. if ( ( $form->{currency} ne $form->{defaultcurrency} )
  1117. && !$exchangerate )
  1118. {
  1119. $form->update_exchangerate(
  1120. $dbh, $form->{currency},
  1121. $form->{"datepaid_$i"},
  1122. $form->{"exchangerate_$i"}, 0
  1123. );
  1124. }
  1125. }
  1126. }
  1127. # set values which could be empty to 0
  1128. $form->{terms} *= 1;
  1129. $form->{taxincluded} *= 1;
  1130. # save AR record
  1131. $query = qq|
  1132. UPDATE ar set
  1133. invnumber = ?,
  1134. ordnumber = ?,
  1135. quonumber = ?,
  1136. transdate = ?,
  1137. customer_id = ?,
  1138. amount = ?,
  1139. netamount = ?,
  1140. paid = ?,
  1141. datepaid = ?,
  1142. duedate = ?,
  1143. invoice = '1',
  1144. shippingpoint = ?,
  1145. shipvia = ?,
  1146. terms = ?,
  1147. notes = ?,
  1148. intnotes = ?,
  1149. taxincluded = ?,
  1150. curr = ?,
  1151. department_id = ?,
  1152. employee_id = ?,
  1153. till = ?,
  1154. language_code = ?,
  1155. ponumber = ?
  1156. WHERE id = ?
  1157. |;
  1158. $sth = $dbh->prepare($query);
  1159. $sth->execute(
  1160. $form->{invnumber}, $form->{ordnumber},
  1161. $form->{quonumber}, $form->{transdate},
  1162. $form->{customer_id}, $invamount,
  1163. $invnetamount, $form->{paid},
  1164. $form->{datepaid}, $form->{duedate},
  1165. $form->{shippingpoint}, $form->{shipvia},
  1166. $form->{terms}, $form->{notes},
  1167. $form->{intnotes}, $form->{taxincluded},
  1168. $form->{currency}, $form->{department_id},
  1169. $form->{employee_id}, $form->{till},
  1170. $form->{language_code}, $form->{ponumber},
  1171. $form->{id}
  1172. ) || $form->dberror($query);
  1173. # add shipto
  1174. $form->{name} = $form->{customer};
  1175. $form->{name} =~ s/--$form->{customer_id}//;
  1176. $form->add_shipto( $dbh, $form->{id} );
  1177. if ($invamount->is_nan) {
  1178. $dbh->rollback;
  1179. return;
  1180. }
  1181. # save printed, emailed and queued
  1182. $form->save_status($dbh);
  1183. my %audittrail = (
  1184. tablename => 'ar',
  1185. reference => $form->{invnumber},
  1186. formname => $form->{type},
  1187. action => 'posted',
  1188. id => $form->{id}
  1189. );
  1190. $form->audittrail( $dbh, "", \%audittrail );
  1191. $form->save_recurring( $dbh, $myconfig );
  1192. my $rc = $dbh->commit;
  1193. $rc;
  1194. }
  1195. sub process_assembly {
  1196. my ( $dbh2, $form, $id, $totalqty, $project_id ) = @_;
  1197. my $dbh = $form->{dbh};
  1198. my $query = qq|
  1199. SELECT a.parts_id, a.qty, p.assembly,
  1200. p.partnumber, p.description, p.unit,
  1201. p.inventory_accno_id, p.income_accno_id,
  1202. p.expense_accno_id
  1203. FROM assembly a
  1204. JOIN parts p ON (a.parts_id = p.id)
  1205. WHERE a.id = ?|;
  1206. my $sth = $dbh->prepare($query);
  1207. $sth->execute($id) || $form->dberror($query);
  1208. my $allocated;
  1209. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1210. $allocated = 0;
  1211. $ref->{inventory_accno_id} *= 1;
  1212. $ref->{expense_accno_id} *= 1;
  1213. # multiply by number of assemblies
  1214. $ref->{qty} *= $totalqty;
  1215. if ( $ref->{assembly} ) {
  1216. &process_assembly( $dbh, $form, $ref->{parts_id}, $ref->{qty},
  1217. $project_id );
  1218. next;
  1219. }
  1220. else {
  1221. if ( $ref->{inventory_accno_id} ) {
  1222. $allocated =
  1223. &cogs( $dbh, $form, $ref->{parts_id}, $ref->{qty},
  1224. $project_id );
  1225. }
  1226. }
  1227. $query = qq|
  1228. INSERT INTO invoice
  1229. (trans_id, description, parts_id, qty,
  1230. sellprice, fxsellprice, allocated,
  1231. assemblyitem, unit)
  1232. VALUES (?, ?, ?, ?, 0, 0, ?, 't', ?)|;
  1233. my $sth = $dbh->prepare($query);
  1234. $sth->execute( $form->{id}, $ref->{description}, $ref->{parts_id},
  1235. $ref->{qty}, $allocated, $ref->{unit} )
  1236. || $form->dberror($query);
  1237. }
  1238. $sth->finish;
  1239. }
  1240. sub cogs {
  1241. # This is nearly entirely rewritten since 1.2.8 based in part on the works
  1242. # of Victor Sterpu and Dieter Simader (see CONTRIBUTORS for more
  1243. # information). However, there are a number of areas where I have
  1244. # substantially rewritten the logic. This function is heavily annotated
  1245. # largely because COGS/invoices are still scheduled to be re-engineered in
  1246. # 1.4 so it is a good idea to have records of opinions in the code.-- CT
  1247. my ( $dbh2, $form, $id, $totalqty, $project_id, $sellprice) = @_;
  1248. my $dbh = $form->{dbh};
  1249. my $query;
  1250. my $allocated = 0;
  1251. if ($totalqty == 0) {
  1252. return 0;
  1253. }
  1254. elsif ($totalqty > 0) {
  1255. # If the quantity is positive, we do a standard FIFO COGS calculation.
  1256. # In this case, we are going to order the queue by transdate and trans_id
  1257. # as this is the best way of doing this perpetually. We don't want out
  1258. # of order entry to screw with the books. Of course if someone wants to
  1259. # implement LIFO, this would be the place to do it. -- CT
  1260. my $query = qq|
  1261. SELECT i.id, i.trans_id, i.qty, i.allocated, i.sellprice,
  1262. i.fxsellprice, p.inventory_accno_id,
  1263. p.expense_accno_id,
  1264. (i.qty * -1) - i.allocated AS available
  1265. FROM invoice i
  1266. JOIN parts p ON (i.parts_id = p.id)
  1267. JOIN ap a ON (i.trans_id = a.id)
  1268. WHERE i.parts_id = ? AND (i.qty + i.allocated) < 0
  1269. ORDER BY a.transdate, i.trans_id|;
  1270. my $sth = $dbh->prepare($query);
  1271. $sth->execute($id) || $form->dberror($query);
  1272. my $qty;
  1273. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1274. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1275. if ( $ref->{available} >= $totalqty ) {
  1276. $qty = $totalqty;
  1277. }
  1278. else {
  1279. $qty = $ref->{available};
  1280. }
  1281. $form->update_balance( $dbh, "invoice", "allocated",
  1282. qq|id = $ref->{id}|, $qty );
  1283. # total expenses and inventory
  1284. # sellprice is the cost of the item
  1285. my $linetotal = $form->round_amount($ref->{sellprice} * $qty, 2);
  1286. # add expense
  1287. push @{ $form->{acc_trans}{lineitems} },
  1288. {
  1289. chart_id => $ref->{expense_accno_id},
  1290. amount => $linetotal * -1,
  1291. project_id => $project_id,
  1292. invoice_id => $ref->{id}
  1293. };
  1294. # deduct inventory
  1295. push @{ $form->{acc_trans}{lineitems} },
  1296. {
  1297. chart_id => $ref->{inventory_accno_id},
  1298. amount => $linetotal,
  1299. project_id => $project_id,
  1300. invoice_id => $ref->{id}
  1301. };
  1302. # subtract from allocated
  1303. $allocated -= $qty;
  1304. last if ( ( $totalqty -= $qty ) <= 0 );
  1305. }
  1306. $sth->finish;
  1307. }
  1308. else {
  1309. # In this case, the quantity is negative. So we are looking at a
  1310. # reversing entry for partial COGS. The two workflows supported here
  1311. # are those involved in voiding an invoice or returning some items on it.
  1312. # If there are unallocated items for the current invoice at the end, we
  1313. # will throw an error until we have an understanding of other workflows
  1314. # that need to be supported. -- CT
  1315. #
  1316. # Note: Victor's original patch selected items to reverse based on
  1317. # sell price. This causes issues with restocking fees and the like so
  1318. # I am removing that restriction. This should be discussed more fully
  1319. # however. -- CT
  1320. $query = qq|
  1321. SELECT i.id, i.qty, i.allocated, a.transdate,
  1322. -1 * (i.allocated + i.qty) AS available,
  1323. p.expense_accno_id, p.inventory_accno_id
  1324. FROM invoice i
  1325. JOIN parts p ON (p.id = i.parts_id)
  1326. JOIN ar a ON (a.id = i.trans_id)
  1327. WHERE i.parts_id = ? AND (i.qty + i.allocated) > 0
  1328. ORDER BY transdate
  1329. |;
  1330. $sth = $dbh->prepare($query);
  1331. $sth->execute($id) || $form->dberror($query);
  1332. my $qty;
  1333. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1334. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  1335. if ($totalqty < $ref->{available}){
  1336. $qty = $ref->{available};
  1337. } else {
  1338. $qty = $totalqty;
  1339. }
  1340. # update allocated for sold item
  1341. $form->update_balance(
  1342. $dbh, "invoice", "allocated",
  1343. qq|id = $ref->{id}|, $qty
  1344. );
  1345. # Note: No COGS calculations on reversed short sale invoices.
  1346. # This merely prevents COGS calculations in the future agaisnt
  1347. # such short invoices. -- CT
  1348. $totalqty -= $qty;
  1349. $allocated -= $qty;
  1350. last if $totalqty == 0;
  1351. }
  1352. # If the total quantity is still less than zero, we must assume that
  1353. # this is just an invoice which has been voided or products returns
  1354. # but is not merely representing a voided short sale, and therefore
  1355. # we need to unallocate the items from AP. There has been some debate
  1356. # as to how to approach this, and I think it is safest to unallocate
  1357. # the most recently allocated AP items of the same type regardless of
  1358. # the relevant dates of the invoices. I can see cases where this
  1359. # might require adjustments, however. -- CT
  1360. if ($totalqty < 0){
  1361. $query = qq|
  1362. SELECT i.allocated, i.sellprice, p.inventory_accno_id,
  1363. p.expense_accno_id, i.id
  1364. FROM invoice i
  1365. JOIN parts p ON (i.parts_id = p.id)
  1366. JOIN ap a ON (i.trans_id = a.id)
  1367. WHERE allocated > 0
  1368. AND i.parts_id = ?
  1369. ORDER BY a.transdate DESC, a.id DESC
  1370. |;
  1371. my $sth = $dbh->prepare($query);
  1372. $sth->execute($id);
  1373. while (my $ref = $sth->fetchrow_hashref(NAME_lc)){
  1374. my $qty = $ref->{allocated} * -1;
  1375. $qty = ($qty < $totalqty) ? $totalqty : $qty;
  1376. my $linetotal = $qty*$ref->{sellprice};
  1377. push @{ $form->{acc_trans}{lineitems} },
  1378. {
  1379. chart_id => $ref->{expense_accno_id},
  1380. amount => $linetotal,
  1381. project_id => $project_id,
  1382. invoice_id => $ref->{id}
  1383. };
  1384. push @{ $form->{acc_trans}{lineitems} },
  1385. {
  1386. chart_id => $ref->{inventory_accno_id},
  1387. amount => -$linetotal,
  1388. project_id => $project_id,
  1389. invoice_id => $ref->{id}
  1390. };
  1391. $form->update_balance(
  1392. $dbh, "invoice", "allocated",
  1393. qq|id = $ref->{id}|, $qty
  1394. );
  1395. $totalqty -= $qty;
  1396. $allocated -= $qty;
  1397. last if $totalqty == 0;
  1398. }
  1399. }
  1400. # If we still have less than 0 total quantity, this is not a return
  1401. # or a void. Throw an error. If there are valid workflows that throw
  1402. # this error, they will require more work to address and will not work
  1403. # safely with the current system. -- CT
  1404. if ($totalqty < 0){
  1405. $form->error("Too many reversed items on an invoice");
  1406. }
  1407. elsif ($totalqty > 0){
  1408. $form->error("Unexpected and invalid quantity allocated.".
  1409. " Aborting.");
  1410. }
  1411. }
  1412. return $allocated;
  1413. }
  1414. sub reverse_invoice {
  1415. my ( $dbh2, $form ) = @_;
  1416. my $dbh = $form->{dbh};
  1417. my $query = qq|
  1418. SELECT id FROM ar
  1419. WHERE id = ?|;
  1420. my $sth;
  1421. $sth = $dbh->prepare($query);
  1422. $sth->execute( $form->{id} );
  1423. my ($id) = $sth->fetchrow_array;
  1424. return unless $id;
  1425. # reverse inventory items
  1426. my $query = qq|
  1427. SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly,
  1428. p.inventory_accno_id
  1429. FROM invoice i
  1430. JOIN parts p ON (i.parts_id = p.id)
  1431. WHERE i.trans_id = ?|;
  1432. my $sth = $dbh->prepare($query);
  1433. $sth->execute( $form->{id} ) || $form->dberror($query);
  1434. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1435. if ( $ref->{inventory_accno_id} || $ref->{assembly} ) {
  1436. # if the invoice item is not an assemblyitem
  1437. # adjust parts onhand
  1438. if ( !$ref->{assemblyitem} ) {
  1439. # adjust onhand in parts table
  1440. $form->update_balance( $dbh, "parts", "onhand",
  1441. qq|id = $ref->{parts_id}|,
  1442. $ref->{qty} );
  1443. }
  1444. # loop if it is an assembly
  1445. next if ( $ref->{assembly} );
  1446. # de-allocated purchases
  1447. $query = qq|
  1448. SELECT id, trans_id, allocated
  1449. FROM invoice
  1450. WHERE parts_id = ?
  1451. AND allocated > 0
  1452. ORDER BY trans_id DESC|;
  1453. my $sth = $dbh->prepare($query);
  1454. $sth->execute( $ref->{parts_id} )
  1455. || $form->dberror($query);
  1456. while ( my $inhref = $sth->fetchrow_hashref(NAME_lc) ) {
  1457. $qty = $ref->{qty};
  1458. if ( ( $ref->{qty} - $inhref->{allocated} ) > 0 ) {
  1459. $qty = $inhref->{allocated};
  1460. }
  1461. # update invoice
  1462. $form->update_balance( $dbh, "invoice", "allocated",
  1463. qq|id = $inhref->{id}|,
  1464. $qty * -1 );
  1465. last if ( ( $ref->{qty} -= $qty ) <= 0 );
  1466. }
  1467. $sth->finish;
  1468. }
  1469. }
  1470. $sth->finish;
  1471. # delete acc_trans
  1472. $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
  1473. $sth = $dbh->prepare($query);
  1474. $sth->execute( $form->{id} ) || $form->dberror($query);
  1475. # delete invoice entries
  1476. $query = qq|DELETE FROM invoice WHERE trans_id = ?|;
  1477. $sth = $dbh->prepare($query);
  1478. $sth->execute( $form->{id} ) || $form->dberror($query);
  1479. $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
  1480. $sth = $dbh->prepare($query);
  1481. $sth->execute( $form->{id} ) || $form->dberror($query);
  1482. $dbh->commit;
  1483. }
  1484. sub delete_invoice {
  1485. my ( $self, $myconfig, $form ) = @_;
  1486. my $dbh = $form->{dbh};
  1487. my $sth;
  1488. &reverse_invoice( $dbh, $form );
  1489. my %audittrail = (
  1490. tablename => 'ar',
  1491. reference => $form->{invnumber},
  1492. formname => $form->{type},
  1493. action => 'deleted',
  1494. id => $form->{id}
  1495. );
  1496. $form->audittrail( $dbh, "", \%audittrail );
  1497. # delete AR record
  1498. my $query = qq|DELETE FROM ar WHERE id = ?|;
  1499. $sth = $dbh->prepare($query);
  1500. $sth->execute( $form->{id} ) || $form->dberror($query);
  1501. # delete spool files
  1502. $query = qq|
  1503. SELECT spoolfile FROM status
  1504. WHERE trans_id = ? AND spoolfile IS NOT NULL|;
  1505. $sth = $dbh->prepare($query);
  1506. $sth->execute( $form->{id} ) || $form->dberror($query);
  1507. my $spoolfile;
  1508. my @spoolfiles = ();
  1509. while ( ($spoolfile) = $sth->fetchrow_array ) {
  1510. push @spoolfiles, $spoolfile;
  1511. }
  1512. $sth->finish;
  1513. # delete status entries
  1514. $query = qq|DELETE FROM status WHERE trans_id = ?|;
  1515. $sth = $dbh->prepare($query);
  1516. $sth->execute( $form->{id} ) || $form->dberror($query);
  1517. my $rc = $dbh->commit;
  1518. if ($rc) {
  1519. foreach $spoolfile (@spoolfiles) {
  1520. unlink "${LedgerSMB::Sysconfig::spool}/$spoolfile"
  1521. if $spoolfile;
  1522. }
  1523. }
  1524. $rc;
  1525. }
  1526. sub retrieve_invoice {
  1527. my ( $self, $myconfig, $form ) = @_;
  1528. my $dbh = $form->{dbh};
  1529. my $query;
  1530. if ( $form->{id} ) {
  1531. # get default accounts and last invoice number
  1532. $query = qq|
  1533. SELECT value AS currencies FROM defaults
  1534. WHERE setting_key = 'curr'|;
  1535. }
  1536. else {
  1537. $query = qq|
  1538. SELECT value AS currencies, current_date AS transdate
  1539. FROM defaults
  1540. WHERE setting_key = 'curr'|;
  1541. }
  1542. my $sth = $dbh->prepare($query);
  1543. $sth->execute || $form->dberror($query);
  1544. my $ref = $sth->fetchrow_hashref(NAME_lc);
  1545. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  1546. $sth->finish;
  1547. if ( $form->{id} ) {
  1548. # retrieve invoice
  1549. $query = qq|
  1550. SELECT a.invnumber, a.ordnumber, a.quonumber,
  1551. a.transdate, a.paid,
  1552. a.shippingpoint, a.shipvia, a.terms, a.notes,
  1553. a.intnotes,
  1554. a.duedate, a.taxincluded, a.curr AS currency,
  1555. a.employee_id, e.name AS employee, a.till,
  1556. a.customer_id,
  1557. a.language_code, a.ponumber
  1558. FROM ar a
  1559. LEFT JOIN employee e ON (e.id = a.employee_id)
  1560. WHERE a.id = ?|;
  1561. $sth = $dbh->prepare($query);
  1562. $sth->execute( $form->{id} ) || $form->dberror($query);
  1563. $ref = $sth->fetchrow_hashref(NAME_lc);
  1564. $form->db_parse_numeric(sth=> $sth, hashref=>$ref_);
  1565. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  1566. $sth->finish;
  1567. # get shipto
  1568. $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
  1569. $sth = $dbh->prepare($query);
  1570. $sth->execute( $form->{id} ) || $form->dberror($query);
  1571. $ref = $sth->fetchrow_hashref(NAME_lc);
  1572. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  1573. $sth->finish;
  1574. # retrieve individual items
  1575. $query = qq|
  1576. SELECT i.description, i.qty, i.fxsellprice,
  1577. i.sellprice, i.discount, i.parts_id AS id,
  1578. i.unit, i.deliverydate, i.project_id,
  1579. pr.projectnumber, i.serialnumber, i.notes,
  1580. p.partnumber, p.assembly, p.bin,
  1581. pg.partsgroup, p.partsgroup_id,
  1582. p.partnumber AS sku, p.listprice, p.lastcost,
  1583. p.weight, p.onhand, p.inventory_accno_id,
  1584. p.income_accno_id, p.expense_accno_id,
  1585. t.description AS partsgrouptranslation
  1586. FROM invoice i
  1587. JOIN parts p ON (i.parts_id = p.id)
  1588. LEFT JOIN project pr ON (i.project_id = pr.id)
  1589. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1590. LEFT JOIN translation t
  1591. ON (t.trans_id = p.partsgroup_id
  1592. AND t.language_code
  1593. = ?)
  1594. WHERE i.trans_id = ?
  1595. AND NOT i.assemblyitem = '1'
  1596. ORDER BY i.id|;
  1597. $sth = $dbh->prepare($query);
  1598. $sth->execute( $form->{language_code}, $form->{id} )
  1599. || $form->dberror($query);
  1600. # foreign currency
  1601. &exchangerate_defaults( $dbh, $form );
  1602. # query for price matrix
  1603. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  1604. # taxes
  1605. $query = qq|
  1606. SELECT c.accno
  1607. FROM chart c
  1608. JOIN partstax pt ON (pt.chart_id = c.id)
  1609. WHERE pt.parts_id = ?|;
  1610. my $tth = $dbh->prepare($query) || $form->dberror($query);
  1611. my $taxrate;
  1612. my $ptref;
  1613. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1614. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  1615. my ($dec) = ( $ref->{fxsellprice} =~ /\.(\d+)/ );
  1616. $dec = length $dec;
  1617. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  1618. $tth->execute( $ref->{id} );
  1619. $ref->{taxaccounts} = "";
  1620. $taxrate = 0;
  1621. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  1622. $ref->{taxaccounts} .= "$ptref->{accno} ";
  1623. $taxrate += $form->{"$ptref->{accno}_rate"};
  1624. }
  1625. $tth->finish;
  1626. chop $ref->{taxaccounts};
  1627. # price matrix
  1628. $ref->{sellprice} =
  1629. ( $ref->{fxsellprice} * $form->{ $form->{currency} } );
  1630. PriceMatrix::price_matrix( $pmh, $ref, $form->{transdate},
  1631. $decimalplaces, $form, $myconfig );
  1632. $ref->{sellprice} = $ref->{fxsellprice};
  1633. $ref->{partsgroup} = $ref->{partsgrouptranslation}
  1634. if $ref->{partsgrouptranslation};
  1635. push @{ $form->{invoice_details} }, $ref;
  1636. }
  1637. $sth->finish;
  1638. }
  1639. @queries = $form->run_custom_queries( 'ar', 'SELECT' );
  1640. my $rc = $dbh->commit;
  1641. $rc;
  1642. }
  1643. sub retrieve_item {
  1644. my ( $self, $myconfig, $form ) = @_;
  1645. my $dbh = $form->{dbh};
  1646. my $i = $form->{rowcount};
  1647. my $null;
  1648. my $var;
  1649. my $where = "WHERE p.obsolete = '0' AND NOT p.income_accno_id IS NULL";
  1650. if ( $form->{"partnumber_$i"} ne "" ) {
  1651. $var = $dbh->quote( $form->like( lc $form->{"partnumber_$i"} ) );
  1652. $where .= " AND lower(p.partnumber) LIKE $var";
  1653. }
  1654. if ( $form->{"description_$i"} ne "" ) {
  1655. $var = $dbh->quote( $form->like( lc $form->{"description_$i"} ) );
  1656. if ( $form->{language_code} ne "" ) {
  1657. $where .= " AND lower(t1.description) LIKE $var";
  1658. }
  1659. else {
  1660. $where .= " AND lower(p.description) LIKE $var";
  1661. }
  1662. }
  1663. if ( $form->{"partsgroup_$i"} ne "" ) {
  1664. ( $null, $var ) = split /--/, $form->{"partsgroup_$i"};
  1665. if ( ! $var ) {
  1666. # search by partsgroup, this is for the POS
  1667. $where .=
  1668. qq| AND pg.partsgroup = |
  1669. . $dbh->quote( $form->{"partsgroup_$i"} );
  1670. }
  1671. else {
  1672. $var = $dbh->quote($var);
  1673. $where .= qq| AND p.partsgroup_id = $var|;
  1674. }
  1675. }
  1676. if ( $form->{"description_$i"} ne "" ) {
  1677. $where .= " ORDER BY 3";
  1678. }
  1679. else {
  1680. $where .= " ORDER BY 2";
  1681. }
  1682. my $query = qq|
  1683. SELECT p.id, p.partnumber, p.description, p.sellprice,
  1684. p.listprice, p.lastcost, p.unit, p.assembly, p.bin,
  1685. p.onhand, p.notes, p.inventory_accno_id,
  1686. p.income_accno_id, p.expense_accno_id, pg.partsgroup,
  1687. p.partsgroup_id, p.partnumber AS sku, p.weight,
  1688. t1.description AS translation,
  1689. t2.description AS grouptranslation
  1690. FROM parts p
  1691. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  1692. LEFT JOIN translation t1
  1693. ON (t1.trans_id = p.id AND t1.language_code = ?)
  1694. LEFT JOIN translation t2
  1695. ON (t2.trans_id = p.partsgroup_id
  1696. AND t2.language_code = ?)
  1697. $where|;
  1698. my $sth = $dbh->prepare($query);
  1699. $sth->execute( $form->{language_code}, $form->{language_code} )
  1700. || $form->dberror($query);
  1701. my $ref;
  1702. my $ptref;
  1703. # setup exchange rates
  1704. &exchangerate_defaults( $dbh, $form );
  1705. # taxes
  1706. $query = qq|
  1707. SELECT c.accno
  1708. FROM chart c
  1709. JOIN partstax pt ON (c.id = pt.chart_id)
  1710. WHERE pt.parts_id = ?|;
  1711. my $tth = $dbh->prepare($query) || $form->dberror($query);
  1712. # price matrix
  1713. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  1714. my $transdate = $form->datetonum( $myconfig, $form->{transdate} );
  1715. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1716. $form->db_parse_numeric(sth => $sth, hashref => $ref);
  1717. my ($dec) = ( $ref->{sellprice} =~ /\.(\d+)/ );
  1718. $dec = length $dec;
  1719. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  1720. # get taxes for part
  1721. $tth->execute( $ref->{id} );
  1722. $ref->{taxaccounts} = "";
  1723. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  1724. $ref->{taxaccounts} .= "$ptref->{accno} ";
  1725. }
  1726. $tth->finish;
  1727. chop $ref->{taxaccounts};
  1728. # get matrix
  1729. PriceMatrix::price_matrix( $pmh, $ref, $transdate, $decimalplaces,
  1730. $form, $myconfig );
  1731. $ref->{description} = $ref->{translation}
  1732. if $ref->{translation};
  1733. $ref->{partsgroup} = $ref->{grouptranslation}
  1734. if $ref->{grouptranslation};
  1735. push @{ $form->{item_list} }, $ref;
  1736. }
  1737. $sth->finish;
  1738. }
  1739. sub exchangerate_defaults {
  1740. my ( $dbh2, $form ) = @_;
  1741. $dbh = $form->{dbh};
  1742. my $var;
  1743. # get default currencies
  1744. my $query = qq|
  1745. SELECT substr(value,1,3), value FROM defaults
  1746. WHERE setting_key = 'curr'|;
  1747. my $eth = $dbh->prepare($query) || $form->dberror($query);
  1748. $eth->execute;
  1749. ( $form->{defaultcurrency}, $form->{currencies} ) = $eth->fetchrow_array;
  1750. $eth->finish;
  1751. $query = qq|
  1752. SELECT buy
  1753. FROM exchangerate
  1754. WHERE curr = ?
  1755. AND transdate = ?|;
  1756. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  1757. $query = qq/
  1758. SELECT max(transdate || ' ' || buy || ' ' || curr)
  1759. FROM exchangerate
  1760. WHERE curr = ?/;
  1761. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  1762. # get exchange rates for transdate or max
  1763. foreach $var ( split /:/, substr( $form->{currencies}, 4 ) ) {
  1764. $eth1->execute( $var, $form->{transdate} );
  1765. ( $form->{$var} ) = $eth1->fetchrow_array;
  1766. if ( !$form->{$var} ) {
  1767. $eth2->execute($var);
  1768. ( $form->{$var} ) = $eth2->fetchrow_array;
  1769. ( $null, $form->{$var} ) = split / /, $form->{$var};
  1770. $form->{$var} = 1 unless $form->{$var};
  1771. $eth2->finish;
  1772. }
  1773. $eth1->finish;
  1774. }
  1775. $form->{ $form->{currency} } = $form->{exchangerate}
  1776. if $form->{exchangerate};
  1777. $form->{ $form->{currency} } ||= 1;
  1778. $form->{ $form->{defaultcurrency} } = 1;
  1779. }
  1780. 1;