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