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