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