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