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