summaryrefslogtreecommitdiff
path: root/LedgerSMB/IS.pm
blob: ad1f1c27f46e5b17e48d34771998a89bb731d774 (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. for (keys %$form) {
  743. if (UNIVERSAL::isa( $form->{$_}, 'Math::BigFloat' )){
  744. $form->{$_} = $form->{$_}->bstr();
  745. }
  746. }
  747. foreach $i ( 1 .. $form->{rowcount} ) {
  748. $form->{"qty_$i"} = $form->parse_amount( $myconfig, $form->{"qty_$i"} );
  749. if ( $form->{"qty_$i"} ) {
  750. $pth->execute( $form->{"id_$i"} );
  751. $ref = $pth->fetchrow_hashref(NAME_lc);
  752. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  753. $pth->finish;
  754. # project
  755. if ( $form->{"projectnumber_$i"} ) {
  756. ( $null, $project_id ) = split /--/,
  757. $form->{"projectnumber_$i"};
  758. }
  759. $project_id = $form->{"project_id_$i"}
  760. if $form->{"project_id_$i"};
  761. # keep entered selling price
  762. my $fxsellprice =
  763. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  764. my ($dec) = ( $fxsellprice =~ /\.(\d+)/ );
  765. $dec = length $dec;
  766. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  767. # undo discount formatting
  768. $form->{"discount_$i"} =
  769. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) / 100;
  770. # deduct discount
  771. $form->{"sellprice_$i"} = $fxsellprice -
  772. $form->round_amount( $fxsellprice * $form->{"discount_$i"},
  773. $decimalplaces );
  774. # linetotal
  775. my $fxlinetotal =
  776. $form->round_amount( $form->{"sellprice_$i"} * $form->{"qty_$i"},
  777. 2 );
  778. $amount = $fxlinetotal * $form->{exchangerate};
  779. my $linetotal = $form->round_amount( $amount, 2 );
  780. $fxdiff += $amount - $linetotal;
  781. @taxaccounts = Tax::init_taxes(
  782. $form,
  783. $form->{"taxaccounts_$i"},
  784. $form->{"taxaccounts"}
  785. );
  786. $ml = 1;
  787. $tax = Math::BigFloat->bzero();
  788. $fxtax = Math::BigFloat->bzero();
  789. if ( $form->{taxincluded} ) {
  790. $tax += $amount =
  791. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  792. $form->{"sellprice_$i"} -= $amount / $form->{"qty_$i"};
  793. $fxtax +=
  794. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  795. }
  796. else {
  797. $tax += $amount =
  798. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  799. $fxtax +=
  800. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  801. }
  802. for (@taxaccounts) {
  803. $form->{acc_trans}{ $form->{id} }{ $_->account }{amount} +=
  804. $_->value;
  805. }
  806. $grossamount = $form->round_amount( $linetotal, 2 );
  807. if ( $form->{taxincluded} ) {
  808. $amount = $form->round_amount( $tax, 2 );
  809. $linetotal -= $form->round_amount( $tax - $diff, 2 );
  810. $diff = ( $amount - $tax );
  811. }
  812. # add linetotal to income
  813. $amount = $form->round_amount( $linetotal, 2 );
  814. push @{ $form->{acc_trans}{lineitems} },
  815. {
  816. chart_id => $form->{"income_accno_id_$i"},
  817. amount => $amount,
  818. fxgrossamount => $fxlinetotal + $fxtax,
  819. grossamount => $grossamount,
  820. project_id => $project_id
  821. };
  822. $ndx = $#{ @{ $form->{acc_trans}{lineitems} } };
  823. $form->{"sellprice_$i"} =
  824. $form->round_amount(
  825. $form->{"sellprice_$i"} * $form->{exchangerate},
  826. $decimalplaces );
  827. if ( $form->{"inventory_accno_id_$i"}
  828. || $form->{"assembly_$i"} )
  829. {
  830. if ( $form->{"assembly_$i"} ) {
  831. # If the assembly consists of all
  832. # services, we don't keep inventory,
  833. # so we should not update it
  834. $query = qq|
  835. SELECT sum(
  836. p.inventory_accno_id),
  837. p.assembly
  838. FROM parts p
  839. JOIN assembly a
  840. ON (a.parts_id = p.id)
  841. WHERE a.id = ?
  842. GROUP BY p.assembly|;
  843. $sth = $dbh->prepare($query);
  844. $sth->execute( $form->{"id_$i"} )
  845. || $form->dberror($query);
  846. my ( $inv, $assembly ) = $sth->fetchrow_array;
  847. $sth->finish;
  848. if ( $inv || $assembly ) {
  849. $form->update_balance(
  850. $dbh, "parts", "onhand",
  851. qq|id = | . qq|$form->{"id_$i"}|,
  852. $form->{"qty_$i"} * -1
  853. ) unless $form->{shipped};
  854. }
  855. &process_assembly( $dbh, $form, $form->{"id_$i"},
  856. $form->{"qty_$i"}, $project_id );
  857. }
  858. else {
  859. $form->update_balance(
  860. $dbh, "parts", "onhand",
  861. qq|id = $form->{"id_$i"}|,
  862. $form->{"qty_$i"} * -1
  863. ) unless $form->{shipped};
  864. $allocated = cogs(
  865. $dbh, $form,
  866. $form->{"id_$i"}, $form->{"qty_$i"},
  867. $project_id, $form->{"sellprice_$i"},
  868. );
  869. }
  870. }
  871. # save detail record in invoice table
  872. $query = qq|
  873. INSERT INTO invoice (description)
  874. VALUES ('$uid')|;
  875. $dbh->do($query) || $form->dberror($query);
  876. $query = qq|
  877. SELECT id FROM invoice
  878. WHERE description = '$uid'|;
  879. ($invoice_id) = $dbh->selectrow_array($query);
  880. unless ( $form->{"deliverydate_$i"} ) {
  881. undef $form->{"deliverydate_$i"};
  882. }
  883. $query = qq|
  884. UPDATE invoice
  885. SET trans_id = ?,
  886. parts_id = ?,
  887. description = ?,
  888. qty = ?,
  889. sellprice = ?,
  890. fxsellprice = ?,
  891. discount = ?,
  892. allocated = ?,
  893. unit = ?,
  894. deliverydate = ?,
  895. project_id = ?,
  896. serialnumber = ?,
  897. notes = ?
  898. WHERE id = ?|;
  899. $sth = $dbh->prepare($query);
  900. $sth->execute(
  901. $form->{id}, $form->{"id_$i"},
  902. $form->{"description_$i"}, $form->{"qty_$i"},
  903. $form->{"sellprice_$i"}, $fxsellprice,
  904. $form->{"discount_$i"}, $allocated,
  905. $form->{"unit_$i"}, $form->{"deliverydate_$i"},
  906. $project_id, $form->{"serialnumber_$i"},
  907. $form->{"notes_$i"}, $invoice_id
  908. ) || $form->dberror($query);
  909. # add invoice_id
  910. $form->{acc_trans}{lineitems}[$ndx]->{invoice_id} = $invoice_id;
  911. }
  912. }
  913. $form->{paid} = 0;
  914. for $i ( 1 .. $form->{paidaccounts} ) {
  915. $form->{"paid_$i"} =
  916. $form->parse_amount( $myconfig, $form->{"paid_$i"} )->bstr();
  917. $form->{paid} += $form->{"paid_$i"};
  918. $form->{datepaid} = $form->{"datepaid_$i"}
  919. if ( $form->{"paid_$i"} );
  920. }
  921. # add lineitems + tax
  922. $amount = 0;
  923. $grossamount = 0;
  924. $fxgrossamount = 0;
  925. for ( @{ $form->{acc_trans}{lineitems} } ) {
  926. $amount += $_->{amount};
  927. $grossamount += $_->{grossamount};
  928. $fxgrossamount += $_->{fxgrossamount};
  929. }
  930. $invnetamount = $amount;
  931. $amount = 0;
  932. for ( split / /, $form->{taxaccounts} ) {
  933. $amount += $form->{acc_trans}{ $form->{id} }{$_}{amount} =
  934. $form->round_amount( $form->{acc_trans}{ $form->{id} }{$_}{amount},
  935. 2 );
  936. }
  937. $invamount = $invnetamount + $amount;
  938. $diff = 0;
  939. if ( $form->{taxincluded} ) {
  940. $diff = $form->round_amount( $grossamount - $invamount, 2 );
  941. $invamount += $diff;
  942. }
  943. $fxdiff = $form->round_amount( $fxdiff, 2 );
  944. $invnetamount += $fxdiff;
  945. $invamount += $fxdiff;
  946. if ( $form->round_amount( $form->{paid} - $fxgrossamount, 2 ) == 0 ) {
  947. $form->{paid} = $invamount;
  948. }
  949. else {
  950. $form->{paid} =
  951. $form->round_amount( $form->{paid} * $form->{exchangerate}, 2 );
  952. }
  953. foreach $ref ( sort { $b->{amount} <=> $a->{amount} }
  954. @{ $form->{acc_trans}{lineitems} } )
  955. {
  956. $amount = $ref->{amount} + $diff + $fxdiff;
  957. $query = qq|
  958. INSERT INTO acc_trans
  959. (trans_id, chart_id, amount,
  960. transdate, project_id, invoice_id)
  961. VALUES (?, ?, ?, ?, ?, ?)|;
  962. $sth = $dbh->prepare($query);
  963. $sth->execute( $form->{id}, $ref->{chart_id}, $amount,
  964. $form->{transdate}, $ref->{project_id}, $ref->{invoice_id} )
  965. || $form->dberror($query);
  966. $diff = 0;
  967. $fxdiff = 0;
  968. }
  969. $form->{receivables} = $invamount * -1;
  970. delete $form->{acc_trans}{lineitems};
  971. # update exchangerate
  972. if ( ( $form->{currency} ne $form->{defaultcurrency} ) && !$exchangerate ) {
  973. $form->update_exchangerate( $dbh, $form->{currency}, $form->{transdate},
  974. $form->{exchangerate}, 0 );
  975. }
  976. # record receivable
  977. if ( $form->{receivables} ) {
  978. ($accno) = split /--/, $form->{AR};
  979. $query = qq|
  980. INSERT INTO acc_trans
  981. (trans_id, chart_id, amount, transdate)
  982. VALUES (?, (SELECT id FROM chart WHERE accno = ?),
  983. ?, ?)|;
  984. $sth = $dbh->prepare($query);
  985. $sth->execute( $form->{id}, $accno, $form->{receivables},
  986. $form->{transdate} )
  987. || $form->dberror($query);
  988. }
  989. foreach my $trans_id ( keys %{ $form->{acc_trans} } ) {
  990. foreach my $accno ( keys %{ $form->{acc_trans}{$trans_id} } ) {
  991. $amount =
  992. $form->round_amount(
  993. $form->{acc_trans}{$trans_id}{$accno}{amount}, 2 );
  994. if ($amount) {
  995. $query = qq|
  996. INSERT INTO acc_trans
  997. (trans_id, chart_id, amount,
  998. transdate)
  999. VALUES (?, (SELECT id FROM chart
  1000. WHERE accno = ?),
  1001. ?, ?)|;
  1002. $sth = $dbh->prepare($query);
  1003. $sth->execute( $trans_id, $accno, $amount, $form->{transdate} )
  1004. || $form->dberror($query);
  1005. }
  1006. }
  1007. }
  1008. # if there is no amount but a payment record receivable
  1009. if ( $invamount == 0 ) {
  1010. $form->{receivables} = 1;
  1011. }
  1012. my $cleared = 0;
  1013. # record payments and offsetting AR
  1014. for $i ( 1 .. $form->{paidaccounts} ) {
  1015. if ( $form->{"paid_$i"} ) {
  1016. my ($accno) = split /--/, $form->{"AR_paid_$i"};
  1017. $form->{"datepaid_$i"} = $form->{transdate}
  1018. unless ( $form->{"datepaid_$i"} );
  1019. $form->{datepaid} = $form->{"datepaid_$i"};
  1020. $exchangerate = 0;
  1021. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  1022. $form->{"exchangerate_$i"} = 1;
  1023. }
  1024. else {
  1025. $exchangerate =
  1026. $form->check_exchangerate( $myconfig, $form->{currency},
  1027. $form->{"datepaid_$i"}, 'buy' );
  1028. $form->{"exchangerate_$i"} =
  1029. ($exchangerate)
  1030. ? $exchangerate
  1031. : $form->parse_amount( $myconfig,
  1032. $form->{"exchangerate_$i"} );
  1033. }
  1034. # record AR
  1035. $amount =
  1036. $form->round_amount( $form->{"paid_$i"} * $form->{exchangerate},
  1037. 2 );
  1038. if ( $form->{receivables} ) {
  1039. $query = qq|
  1040. INSERT INTO acc_trans
  1041. (trans_id, chart_id, amount,
  1042. transdate)
  1043. VALUES (?, (SELECT id FROM chart
  1044. WHERE accno = ?),
  1045. ?, ?)|;
  1046. $sth = $dbh->prepare($query);
  1047. $sth->execute( $form->{id}, $form->{AR}, $amount,
  1048. $form->{"datepaid_$i"} )
  1049. || $form->dberror($query);
  1050. }
  1051. # record payment
  1052. $amount = $form->{"paid_$i"} * -1;
  1053. if ($keepcleared) {
  1054. $cleared = ( $form->{"cleared_$i"} ) ? 1 : 0;
  1055. }
  1056. $query = qq|
  1057. INSERT INTO acc_trans
  1058. (trans_id, chart_id, amount,
  1059. transdate, source, memo, cleared)
  1060. VALUES (?, (SELECT id FROM chart
  1061. WHERE accno = ?),
  1062. ?, ?, ?, ?, ?)|;
  1063. $sth = $dbh->prepare($query);
  1064. $sth->execute( $form->{id}, $accno, $amount, $form->{"datepaid_$i"},
  1065. $form->{"source_$i"}, $form->{"memo_$i"}, $cleared )
  1066. || $form->dberror($query);
  1067. # exchangerate difference
  1068. $amount = $form->round_amount(
  1069. (
  1070. $form->round_amount(
  1071. $form->{"paid_$i"} * $form->{"exchangerate_$i"} -
  1072. $form->{"paid_$i"},
  1073. 2
  1074. )
  1075. ) * -1,
  1076. 2
  1077. );
  1078. if ($amount) {
  1079. $query = qq|
  1080. INSERT INTO acc_trans
  1081. (trans_id, chart_id, amount,
  1082. transdate, source,
  1083. fx_transaction, cleared)
  1084. VALUES (?, (SELECT id FROM chart
  1085. WHERE accno = ?),
  1086. ?, ?, ?, '1', ?)|;
  1087. $sth = $dbh->prepare($query);
  1088. $sth->execute( $form->{id}, $accno, $amount,
  1089. $form->{"datepaid_$i"},
  1090. $form->{"source_$i"}, $cleared )
  1091. || $form->dberror($query);
  1092. }
  1093. # gain/loss
  1094. $amount = $form->round_amount(
  1095. (
  1096. $form->round_amount(
  1097. $form->{"paid_$i"} * $form->{exchangerate}, 2 ) -
  1098. $form->round_amount(
  1099. $form->{"paid_$i"} * $form->{"exchangerate_$i"}, 2
  1100. )
  1101. ) * -1,
  1102. 2
  1103. );
  1104. if ($amount) {
  1105. my $accno_id =
  1106. ( $amount > 0 )
  1107. ? $fxgain_accno_id
  1108. : $fxloss_accno_id;
  1109. $query = qq|
  1110. INSERT INTO acc_trans (
  1111. trans_id, chart_id, amount,
  1112. transdate, fx_transaction,
  1113. cleared)
  1114. VALUES (?, ?, ?, ?, '1', ?)|;
  1115. $sth = $dbh->prepare($query);
  1116. $sth->execute( $form->{id}, $accno_id, $amount,
  1117. $form->{"datepaid_$i"}, $cleared )
  1118. || $form->dberror($query);
  1119. }
  1120. # update exchange rate
  1121. if ( ( $form->{currency} ne $form->{defaultcurrency} )
  1122. && !$exchangerate )
  1123. {
  1124. $form->update_exchangerate(
  1125. $dbh, $form->{currency},
  1126. $form->{"datepaid_$i"},
  1127. $form->{"exchangerate_$i"}, 0
  1128. );
  1129. }
  1130. }
  1131. }
  1132. # set values which could be empty to 0
  1133. $form->{terms} *= 1;
  1134. $form->{taxincluded} *= 1;
  1135. # save AR record
  1136. $query = qq|
  1137. UPDATE ar set
  1138. invnumber = ?,
  1139. ordnumber = ?,
  1140. quonumber = ?,
  1141. transdate = ?,
  1142. customer_id = ?,
  1143. amount = ?,
  1144. netamount = ?,
  1145. paid = ?,
  1146. datepaid = ?,
  1147. duedate = ?,
  1148. invoice = '1',
  1149. shippingpoint = ?,
  1150. shipvia = ?,
  1151. terms = ?,
  1152. notes = ?,
  1153. intnotes = ?,
  1154. taxincluded = ?,
  1155. curr = ?,
  1156. department_id = ?,
  1157. employee_id = ?,
  1158. till = ?,
  1159. language_code = ?,
  1160. ponumber = ?
  1161. WHERE id = ?
  1162. |;
  1163. $sth = $dbh->prepare($query);
  1164. $sth->execute(
  1165. $form->{invnumber}, $form->{ordnumber},
  1166. $form->{quonumber}, $form->{transdate},
  1167. $form->{customer_id}, $invamount,
  1168. $invnetamount, $form->{paid},
  1169. $form->{datepaid}, $form->{duedate},
  1170. $form->{shippingpoint}, $form->{shipvia},
  1171. $form->{terms}, $form->{notes},
  1172. $form->{intnotes}, $form->{taxincluded},
  1173. $form->{currency}, $form->{department_id},
  1174. $form->{employee_id}, $form->{till},
  1175. $form->{language_code}, $form->{ponumber},
  1176. $form->{id}
  1177. ) || $form->dberror($query);
  1178. # add shipto
  1179. $form->{name} = $form->{customer};
  1180. $form->{name} =~ s/--$form->{customer_id}//;
  1181. $form->add_shipto( $dbh, $form->{id} );
  1182. if ($invamount->is_nan) {
  1183. $dbh->rollback;
  1184. return;
  1185. }
  1186. # save printed, emailed and queued
  1187. $form->save_status($dbh);
  1188. my %audittrail = (
  1189. tablename => 'ar',
  1190. reference => $form->{invnumber},
  1191. formname => $form->{type},
  1192. action => 'posted',
  1193. id => $form->{id}
  1194. );
  1195. $form->audittrail( $dbh, "", \%audittrail );
  1196. $form->save_recurring( $dbh, $myconfig );
  1197. my $rc = $dbh->commit;
  1198. $rc;
  1199. }
  1200. sub process_assembly {
  1201. my ( $dbh2, $form, $id, $totalqty, $project_id ) = @_;
  1202. my $dbh = $form->{dbh};
  1203. my $query = qq|
  1204. SELECT a.parts_id, a.qty, p.assembly,
  1205. p.partnumber, p.description, p.unit,
  1206. p.inventory_accno_id, p.income_accno_id,
  1207. p.expense_accno_id
  1208. FROM assembly a
  1209. JOIN parts p ON (a.parts_id = p.id)
  1210. WHERE a.id = ?|;
  1211. my $sth = $dbh->prepare($query);
  1212. $sth->execute($id) || $form->dberror($query);
  1213. my $allocated;
  1214. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1215. $allocated = 0;
  1216. $ref->{inventory_accno_id} *= 1;
  1217. $ref->{expense_accno_id} *= 1;
  1218. # multiply by number of assemblies
  1219. $ref->{qty} *= $totalqty;
  1220. if ( $ref->{assembly} ) {
  1221. &process_assembly( $dbh, $form, $ref->{parts_id}, $ref->{qty},
  1222. $project_id );
  1223. next;
  1224. }
  1225. else {
  1226. if ( $ref->{inventory_accno_id} ) {
  1227. $allocated =
  1228. &cogs( $dbh, $form, $ref->{parts_id}, $ref->{qty},
  1229. $project_id );
  1230. }
  1231. }
  1232. $query = qq|
  1233. INSERT INTO invoice
  1234. (trans_id, description, parts_id, qty,
  1235. sellprice, fxsellprice, allocated,
  1236. assemblyitem, unit)
  1237. VALUES (?, ?, ?, ?, 0, 0, ?, 't', ?)|;
  1238. my $sth = $dbh->prepare($query);
  1239. $sth->execute( $form->{id}, $ref->{description}, $ref->{parts_id},
  1240. $ref->{qty}, $allocated, $ref->{unit} )
  1241. || $form->dberror($query);
  1242. }
  1243. $sth->finish;
  1244. }
  1245. sub cogs {
  1246. # This is nearly entirely rewritten since 1.2.8 based in part on the works
  1247. # of Victor Sterpu and Dieter Simader (see CONTRIBUTORS for more
  1248. # information). However, there are a number of areas where I have
  1249. # substantially rewritten the logic. This function is heavily annotated
  1250. # largely because COGS/invoices are still scheduled to be re-engineered in
  1251. # 1.4 so it is a good idea to have records of opinions in the code.-- CT
  1252. my ( $dbh2, $form, $id, $totalqty, $project_id, $sellprice) = @_;
  1253. my $dbh = $form->{dbh};
  1254. my $query;
  1255. my $allocated = 0;
  1256. if ($totalqty == 0) {
  1257. return 0;
  1258. }
  1259. elsif ($totalqty > 0) {
  1260. # If the quantity is positive, we do a standard FIFO COGS calculation.
  1261. # In this case, we are going to order the queue by transdate and trans_id
  1262. # as this is the best way of doing this perpetually. We don't want out
  1263. # of order entry to screw with the books. Of course if someone wants to
  1264. # implement LIFO, this would be the place to do it. -- CT
  1265. my $query = qq|
  1266. SELECT i.id, i.trans_id, i.qty, i.allocated, i.sellprice,
  1267. i.fxsellprice, p.inventory_accno_id,
  1268. p.expense_accno_id,
  1269. (i.qty * -1) - i.allocated AS available
  1270. FROM invoice i
  1271. JOIN parts p ON (i.parts_id = p.id)
  1272. JOIN ap a ON (i.trans_id = a.id)
  1273. WHERE i.parts_id = ? AND (i.qty + i.allocated) < 0
  1274. ORDER BY a.transdate, i.trans_id|;
  1275. my $sth = $dbh->prepare($query);
  1276. $sth->execute($id) || $form->dberror($query);
  1277. my $qty;
  1278. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1279. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1280. if ( $ref->{available} >= $totalqty ) {
  1281. $qty = $totalqty;
  1282. }
  1283. else {
  1284. $qty = $ref->{available};
  1285. }
  1286. $form->update_balance( $dbh, "invoice", "allocated",
  1287. qq|id = $ref->{id}|, $qty );
  1288. # total expenses and inventory
  1289. # sellprice is the cost of the item
  1290. my $linetotal = $form->round_amount($ref->{sellprice} * $qty, 2);
  1291. # add expense
  1292. push @{ $form->{acc_trans}{lineitems} },
  1293. {
  1294. chart_id => $ref->{expense_accno_id},
  1295. amount => $linetotal * -1,
  1296. project_id => $project_id,
  1297. invoice_id => $ref->{id}
  1298. };
  1299. # deduct inventory
  1300. push @{ $form->{acc_trans}{lineitems} },
  1301. {
  1302. chart_id => $ref->{inventory_accno_id},
  1303. amount => $linetotal,
  1304. project_id => $project_id,
  1305. invoice_id => $ref->{id}
  1306. };
  1307. # subtract from allocated
  1308. $allocated -= $qty;
  1309. last if ( ( $totalqty -= $qty ) <= 0 );
  1310. }
  1311. $sth->finish;
  1312. }
  1313. else {
  1314. # In this case, the quantity is negative. So we are looking at a
  1315. # reversing entry for partial COGS. The two workflows supported here
  1316. # are those involved in voiding an invoice or returning some items on it.
  1317. # If there are unallocated items for the current invoice at the end, we
  1318. # will throw an error until we have an understanding of other workflows
  1319. # that need to be supported. -- CT
  1320. #
  1321. # Note: Victor's original patch selected items to reverse based on
  1322. # sell price. This causes issues with restocking fees and the like so
  1323. # I am removing that restriction. This should be discussed more fully
  1324. # however. -- CT
  1325. $query = qq|
  1326. SELECT i.id, i.qty, i.allocated, a.transdate,
  1327. -1 * (i.allocated + i.qty) AS available,
  1328. p.expense_accno_id, p.inventory_accno_id
  1329. FROM invoice i
  1330. JOIN parts p ON (p.id = i.parts_id)
  1331. JOIN ar a ON (a.id = i.trans_id)
  1332. WHERE i.parts_id = ? AND (i.qty + i.allocated) > 0
  1333. ORDER BY transdate
  1334. |;
  1335. $sth = $dbh->prepare($query);
  1336. $sth->execute($id) || $form->dberror($query);
  1337. my $qty;
  1338. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1339. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  1340. if ($totalqty < $ref->{available}){
  1341. $qty = $ref->{available};
  1342. } else {
  1343. $qty = $totalqty;
  1344. }
  1345. # update allocated for sold item
  1346. $form->update_balance(
  1347. $dbh, "invoice", "allocated",
  1348. qq|id = $ref->{id}|, $qty
  1349. );
  1350. # Note: No COGS calculations on reversed short sale invoices.
  1351. # This merely prevents COGS calculations in the future agaisnt
  1352. # such short invoices. -- CT
  1353. $totalqty -= $qty;
  1354. $allocated -= $qty;
  1355. last if $totalqty == 0;
  1356. }
  1357. # If the total quantity is still less than zero, we must assume that
  1358. # this is just an invoice which has been voided or products returns
  1359. # but is not merely representing a voided short sale, and therefore
  1360. # we need to unallocate the items from AP. There has been some debate
  1361. # as to how to approach this, and I think it is safest to unallocate
  1362. # the most recently allocated AP items of the same type regardless of
  1363. # the relevant dates of the invoices. I can see cases where this
  1364. # might require adjustments, however. -- CT
  1365. if ($totalqty < 0){
  1366. $query = qq|
  1367. SELECT i.allocated, i.sellprice, p.inventory_accno_id,
  1368. p.expense_accno_id, i.id
  1369. FROM invoice i
  1370. JOIN parts p ON (i.parts_id = p.id)
  1371. JOIN ap a ON (i.trans_id = a.id)
  1372. WHERE allocated > 0
  1373. AND i.parts_id = ?
  1374. ORDER BY a.transdate DESC, a.id DESC
  1375. |;
  1376. my $sth = $dbh->prepare($query);
  1377. $sth->execute($id);
  1378. while (my $ref = $sth->fetchrow_hashref(NAME_lc)){
  1379. my $qty = $ref->{allocated} * -1;
  1380. $qty = ($qty < $totalqty) ? $totalqty : $qty;
  1381. my $linetotal = $qty*$ref->{sellprice};
  1382. push @{ $form->{acc_trans}{lineitems} },
  1383. {
  1384. chart_id => $ref->{expense_accno_id},
  1385. amount => $linetotal,
  1386. project_id => $project_id,
  1387. invoice_id => $ref->{id}
  1388. };
  1389. push @{ $form->{acc_trans}{lineitems} },
  1390. {
  1391. chart_id => $ref->{inventory_accno_id},
  1392. amount => -$linetotal,
  1393. project_id => $project_id,
  1394. invoice_id => $ref->{id}
  1395. };
  1396. $form->update_balance(
  1397. $dbh, "invoice", "allocated",
  1398. qq|id = $ref->{id}|, $qty
  1399. );
  1400. $totalqty -= $qty;
  1401. $allocated -= $qty;
  1402. last if $totalqty == 0;
  1403. }
  1404. }
  1405. # If we still have less than 0 total quantity, this is not a return
  1406. # or a void. Throw an error. If there are valid workflows that throw
  1407. # this error, they will require more work to address and will not work
  1408. # safely with the current system. -- CT
  1409. if ($totalqty < 0){
  1410. $form->error("Too many reversed items on an invoice");
  1411. }
  1412. elsif ($totalqty > 0){
  1413. $form->error("Unexpected and invalid quantity allocated.".
  1414. " Aborting.");
  1415. }
  1416. }
  1417. return $allocated;
  1418. }
  1419. sub reverse_invoice {
  1420. my ( $dbh2, $form ) = @_;
  1421. my $dbh = $form->{dbh};
  1422. my $query = qq|
  1423. SELECT id FROM ar
  1424. WHERE id = ?|;
  1425. my $sth;
  1426. $sth = $dbh->prepare($query);
  1427. $sth->execute( $form->{id} );
  1428. my ($id) = $sth->fetchrow_array;
  1429. return unless $id;
  1430. # reverse inventory items
  1431. my $query = qq|
  1432. SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.assembly,
  1433. p.inventory_accno_id
  1434. FROM invoice i
  1435. JOIN parts p ON (i.parts_id = p.id)
  1436. WHERE i.trans_id = ?|;
  1437. my $sth = $dbh->prepare($query);
  1438. $sth->execute( $form->{id} ) || $form->dberror($query);
  1439. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1440. if ( $ref->{inventory_accno_id} || $ref->{assembly} ) {
  1441. # if the invoice item is not an assemblyitem
  1442. # adjust parts onhand
  1443. if ( !$ref->{assemblyitem} ) {
  1444. # adjust onhand in parts table
  1445. $form->update_balance( $dbh, "parts", "onhand",
  1446. qq|id = $ref->{parts_id}|,
  1447. $ref->{qty} );
  1448. }
  1449. # loop if it is an assembly
  1450. next if ( $ref->{assembly} );
  1451. # de-allocated purchases
  1452. $query = qq|
  1453. SELECT id, trans_id, allocated
  1454. FROM invoice
  1455. WHERE parts_id = ?
  1456. AND allocated > 0
  1457. ORDER BY trans_id DESC|;
  1458. my $sth = $dbh->prepare($query);
  1459. $sth->execute( $ref->{parts_id} )
  1460. || $form->dberror($query);
  1461. while ( my $inhref = $sth->fetchrow_hashref(NAME_lc) ) {
  1462. $qty = $ref->{qty};
  1463. if ( ( $ref->{qty} - $inhref->{allocated} ) > 0 ) {
  1464. $qty = $inhref->{allocated};
  1465. }
  1466. # update invoice
  1467. $form->update_balance( $dbh, "invoice", "allocated",
  1468. qq|id = $inhref->{id}|,
  1469. $qty * -1 );
  1470. last if ( ( $ref->{qty} -= $qty ) <= 0 );
  1471. }
  1472. $sth->finish;
  1473. }
  1474. }
  1475. $sth->finish;
  1476. # delete acc_trans
  1477. $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
  1478. $sth = $dbh->prepare($query);
  1479. $sth->execute( $form->{id} ) || $form->dberror($query);
  1480. # delete invoice entries
  1481. $query = qq|DELETE FROM invoice WHERE trans_id = ?|;
  1482. $sth = $dbh->prepare($query);
  1483. $sth->execute( $form->{id} ) || $form->dberror($query);
  1484. $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
  1485. $sth = $dbh->prepare($query);
  1486. $sth->execute( $form->{id} ) || $form->dberror($query);
  1487. $dbh->commit;
  1488. }
  1489. sub delete_invoice {
  1490. my ( $self, $myconfig, $form ) = @_;
  1491. my $dbh = $form->{dbh};
  1492. my $sth;
  1493. &reverse_invoice( $dbh, $form );
  1494. my %audittrail = (
  1495. tablename => 'ar',
  1496. reference => $form->{invnumber},
  1497. formname => $form->{type},
  1498. action => 'deleted',
  1499. id => $form->{id}
  1500. );
  1501. $form->audittrail( $dbh, "", \%audittrail );
  1502. # delete AR record
  1503. my $query = qq|DELETE FROM ar WHERE id = ?|;
  1504. $sth = $dbh->prepare($query);
  1505. $sth->execute( $form->{id} ) || $form->dberror($query);
  1506. # delete spool files
  1507. $query = qq|
  1508. SELECT spoolfile FROM status
  1509. WHERE trans_id = ? AND spoolfile IS NOT NULL|;
  1510. $sth = $dbh->prepare($query);
  1511. $sth->execute( $form->{id} ) || $form->dberror($query);
  1512. my $spoolfile;
  1513. my @spoolfiles = ();
  1514. while ( ($spoolfile) = $sth->fetchrow_array ) {
  1515. push @spoolfiles, $spoolfile;
  1516. }
  1517. $sth->finish;
  1518. # delete status entries
  1519. $query = qq|DELETE FROM status WHERE trans_id = ?|;
  1520. $sth = $dbh->prepare($query);
  1521. $sth->execute( $form->{id} ) || $form->dberror($query);
  1522. my $rc = $dbh->commit;
  1523. if ($rc) {
  1524. foreach $spoolfile (@spoolfiles) {
  1525. unlink "${LedgerSMB::Sysconfig::spool}/$spoolfile"
  1526. if $spoolfile;
  1527. }
  1528. }
  1529. $rc;
  1530. }
  1531. sub retrieve_invoice {
  1532. my ( $self, $myconfig, $form ) = @_;
  1533. my $dbh = $form->{dbh};
  1534. my $query;
  1535. if ( $form->{id} ) {
  1536. # get default accounts and last invoice number
  1537. $query = qq|
  1538. SELECT value AS currencies FROM defaults
  1539. WHERE setting_key = 'curr'|;
  1540. }
  1541. else {
  1542. $query = qq|
  1543. SELECT value AS currencies, current_date AS transdate
  1544. FROM defaults
  1545. WHERE setting_key = 'curr'|;
  1546. }
  1547. my $sth = $dbh->prepare($query);
  1548. $sth->execute || $form->dberror($query);
  1549. my $ref = $sth->fetchrow_hashref(NAME_lc);
  1550. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  1551. $sth->finish;
  1552. if ( $form->{id} ) {
  1553. # retrieve invoice
  1554. $query = qq|
  1555. SELECT a.invnumber, a.ordnumber, a.quonumber,
  1556. a.transdate, a.paid,
  1557. a.shippingpoint, a.shipvia, a.terms, a.notes,
  1558. a.intnotes,
  1559. a.duedate, a.taxincluded, a.curr AS currency,
  1560. a.employee_id, e.name AS employee, a.till,
  1561. a.customer_id,
  1562. a.language_code, a.ponumber
  1563. FROM ar a
  1564. LEFT JOIN employee e ON (e.id = a.employee_id)
  1565. WHERE a.id = ?|;
  1566. $sth = $dbh->prepare($query);
  1567. $sth->execute( $form->{id} ) || $form->dberror($query);
  1568. $ref = $sth->fetchrow_hashref(NAME_lc);
  1569. $form->db_parse_numeric(sth=> $sth, hashref=>$ref_);
  1570. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  1571. $sth->finish;
  1572. # get shipto
  1573. $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
  1574. $sth = $dbh->prepare($query);
  1575. $sth->execute( $form->{id} ) || $form->dberror($query);
  1576. $ref = $sth->fetchrow_hashref(NAME_lc);
  1577. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  1578. $sth->finish;
  1579. # retrieve individual items
  1580. $query = qq|
  1581. SELECT i.description, i.qty, i.fxsellprice,
  1582. i.sellprice, i.discount, i.parts_id AS id,
  1583. i.unit, i.deliverydate, i.project_id,
  1584. pr.projectnumber, i.serialnumber, i.notes,
  1585. p.partnumber, p.assembly, p.bin,
  1586. pg.partsgroup, p.partsgroup_id,
  1587. p.partnumber AS sku, p.listprice, p.lastcost,
  1588. p.weight, p.onhand, p.inventory_accno_id,
  1589. p.income_accno_id, p.expense_accno_id,
  1590. t.description AS partsgrouptranslation
  1591. FROM invoice i
  1592. JOIN parts p ON (i.parts_id = p.id)
  1593. LEFT JOIN project pr ON (i.project_id = pr.id)
  1594. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1595. LEFT JOIN translation t
  1596. ON (t.trans_id = p.partsgroup_id
  1597. AND t.language_code
  1598. = ?)
  1599. WHERE i.trans_id = ?
  1600. AND NOT i.assemblyitem = '1'
  1601. ORDER BY i.id|;
  1602. $sth = $dbh->prepare($query);
  1603. $sth->execute( $form->{language_code}, $form->{id} )
  1604. || $form->dberror($query);
  1605. # foreign currency
  1606. &exchangerate_defaults( $dbh, $form );
  1607. # query for price matrix
  1608. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  1609. # taxes
  1610. $query = qq|
  1611. SELECT c.accno
  1612. FROM chart c
  1613. JOIN partstax pt ON (pt.chart_id = c.id)
  1614. WHERE pt.parts_id = ?|;
  1615. my $tth = $dbh->prepare($query) || $form->dberror($query);
  1616. my $taxrate;
  1617. my $ptref;
  1618. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1619. $form->db_parse_numeric(sth=>$sth, hashref => $ref);
  1620. my ($dec) = ( $ref->{fxsellprice} =~ /\.(\d+)/ );
  1621. $dec = length $dec;
  1622. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  1623. $tth->execute( $ref->{id} );
  1624. $ref->{taxaccounts} = "";
  1625. $taxrate = 0;
  1626. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  1627. $ref->{taxaccounts} .= "$ptref->{accno} ";
  1628. $taxrate += $form->{"$ptref->{accno}_rate"};
  1629. }
  1630. $tth->finish;
  1631. chop $ref->{taxaccounts};
  1632. # price matrix
  1633. $ref->{sellprice} =
  1634. ( $ref->{fxsellprice} * $form->{ $form->{currency} } );
  1635. PriceMatrix::price_matrix( $pmh, $ref, $form->{transdate},
  1636. $decimalplaces, $form, $myconfig );
  1637. $ref->{sellprice} = $ref->{fxsellprice};
  1638. $ref->{partsgroup} = $ref->{partsgrouptranslation}
  1639. if $ref->{partsgrouptranslation};
  1640. push @{ $form->{invoice_details} }, $ref;
  1641. }
  1642. $sth->finish;
  1643. }
  1644. @queries = $form->run_custom_queries( 'ar', 'SELECT' );
  1645. my $rc = $dbh->commit;
  1646. $rc;
  1647. }
  1648. sub retrieve_item {
  1649. my ( $self, $myconfig, $form ) = @_;
  1650. my $dbh = $form->{dbh};
  1651. my $i = $form->{rowcount};
  1652. my $null;
  1653. my $var;
  1654. my $where = "WHERE p.obsolete = '0' AND NOT p.income_accno_id IS NULL";
  1655. if ( $form->{"partnumber_$i"} ne "" ) {
  1656. $var = $dbh->quote( $form->like( lc $form->{"partnumber_$i"} ) );
  1657. $where .= " AND lower(p.partnumber) LIKE $var";
  1658. }
  1659. if ( $form->{"description_$i"} ne "" ) {
  1660. $var = $dbh->quote( $form->like( lc $form->{"description_$i"} ) );
  1661. if ( $form->{language_code} ne "" ) {
  1662. $where .= " AND lower(t1.description) LIKE $var";
  1663. }
  1664. else {
  1665. $where .= " AND lower(p.description) LIKE $var";
  1666. }
  1667. }
  1668. if ( $form->{"partsgroup_$i"} ne "" ) {
  1669. ( $null, $var ) = split /--/, $form->{"partsgroup_$i"};
  1670. if ( ! $var ) {
  1671. # search by partsgroup, this is for the POS
  1672. $where .=
  1673. qq| AND pg.partsgroup = |
  1674. . $dbh->quote( $form->{"partsgroup_$i"} );
  1675. }
  1676. else {
  1677. $var = $dbh->quote($var);
  1678. $where .= qq| AND p.partsgroup_id = $var|;
  1679. }
  1680. }
  1681. if ( $form->{"description_$i"} ne "" ) {
  1682. $where .= " ORDER BY 3";
  1683. }
  1684. else {
  1685. $where .= " ORDER BY 2";
  1686. }
  1687. my $query = qq|
  1688. SELECT p.id, p.partnumber, p.description, p.sellprice,
  1689. p.listprice, p.lastcost, p.unit, p.assembly, p.bin,
  1690. p.onhand, p.notes, p.inventory_accno_id,
  1691. p.income_accno_id, p.expense_accno_id, pg.partsgroup,
  1692. p.partsgroup_id, p.partnumber AS sku, p.weight,
  1693. t1.description AS translation,
  1694. t2.description AS grouptranslation
  1695. FROM parts p
  1696. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  1697. LEFT JOIN translation t1
  1698. ON (t1.trans_id = p.id AND t1.language_code = ?)
  1699. LEFT JOIN translation t2
  1700. ON (t2.trans_id = p.partsgroup_id
  1701. AND t2.language_code = ?)
  1702. $where|;
  1703. my $sth = $dbh->prepare($query);
  1704. $sth->execute( $form->{language_code}, $form->{language_code} )
  1705. || $form->dberror($query);
  1706. my $ref;
  1707. my $ptref;
  1708. # setup exchange rates
  1709. &exchangerate_defaults( $dbh, $form );
  1710. # taxes
  1711. $query = qq|
  1712. SELECT c.accno
  1713. FROM chart c
  1714. JOIN partstax pt ON (c.id = pt.chart_id)
  1715. WHERE pt.parts_id = ?|;
  1716. my $tth = $dbh->prepare($query) || $form->dberror($query);
  1717. # price matrix
  1718. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  1719. my $transdate = $form->datetonum( $myconfig, $form->{transdate} );
  1720. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1721. $form->db_parse_numeric(sth => $sth, hashref => $ref);
  1722. my ($dec) = ( $ref->{sellprice} =~ /\.(\d+)/ );
  1723. $dec = length $dec;
  1724. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  1725. # get taxes for part
  1726. $tth->execute( $ref->{id} );
  1727. $ref->{taxaccounts} = "";
  1728. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  1729. $ref->{taxaccounts} .= "$ptref->{accno} ";
  1730. }
  1731. $tth->finish;
  1732. chop $ref->{taxaccounts};
  1733. # get matrix
  1734. PriceMatrix::price_matrix( $pmh, $ref, $transdate, $decimalplaces,
  1735. $form, $myconfig );
  1736. $ref->{description} = $ref->{translation}
  1737. if $ref->{translation};
  1738. $ref->{partsgroup} = $ref->{grouptranslation}
  1739. if $ref->{grouptranslation};
  1740. push @{ $form->{item_list} }, $ref;
  1741. }
  1742. $sth->finish;
  1743. }
  1744. sub exchangerate_defaults {
  1745. my ( $dbh2, $form ) = @_;
  1746. $dbh = $form->{dbh};
  1747. my $var;
  1748. # get default currencies
  1749. my $query = qq|
  1750. SELECT substr(value,1,3), value FROM defaults
  1751. WHERE setting_key = 'curr'|;
  1752. my $eth = $dbh->prepare($query) || $form->dberror($query);
  1753. $eth->execute;
  1754. ( $form->{defaultcurrency}, $form->{currencies} ) = $eth->fetchrow_array;
  1755. $eth->finish;
  1756. $query = qq|
  1757. SELECT buy
  1758. FROM exchangerate
  1759. WHERE curr = ?
  1760. AND transdate = ?|;
  1761. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  1762. $query = qq/
  1763. SELECT max(transdate || ' ' || buy || ' ' || curr)
  1764. FROM exchangerate
  1765. WHERE curr = ?/;
  1766. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  1767. # get exchange rates for transdate or max
  1768. foreach $var ( split /:/, substr( $form->{currencies}, 4 ) ) {
  1769. $eth1->execute( $var, $form->{transdate} );
  1770. ( $form->{$var} ) = $eth1->fetchrow_array;
  1771. if ( !$form->{$var} ) {
  1772. $eth2->execute($var);
  1773. ( $form->{$var} ) = $eth2->fetchrow_array;
  1774. ( $null, $form->{$var} ) = split / /, $form->{$var};
  1775. $form->{$var} = 1 unless $form->{$var};
  1776. $eth2->finish;
  1777. }
  1778. $eth1->finish;
  1779. }
  1780. $form->{ $form->{currency} } = $form->{exchangerate}
  1781. if $form->{exchangerate};
  1782. $form->{ $form->{currency} } ||= 1;
  1783. $form->{ $form->{defaultcurrency} } = 1;
  1784. }
  1785. 1;