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