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