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