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