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