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