summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: 295e736c9aadf81c3e66292d49522b251df85836 (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) 2001
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup
  27. #
  28. #======================================================================
  29. #
  30. # Order entry module
  31. # Quotation
  32. #
  33. #======================================================================
  34. package OE;
  35. use LedgerSMB::Tax;
  36. use LedgerSMB::Sysconfig;
  37. sub transactions {
  38. my ( $self, $myconfig, $form ) = @_;
  39. # connect to database
  40. my $dbh = $form->{dbh};
  41. my $query;
  42. my $null;
  43. my $var;
  44. my $ordnumber = 'ordnumber';
  45. my $quotation = '0';
  46. my $department;
  47. my $rate = ( $form->{vc} eq 'customer' ) ? 'buy' : 'sell';
  48. ( $form->{transdatefrom}, $form->{transdateto} ) =
  49. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  50. if $form->{year} && $form->{month};
  51. if ( $form->{type} =~ /_quotation$/ ) {
  52. $quotation = '1';
  53. $ordnumber = 'quonumber';
  54. }
  55. my $number = $form->like( lc $form->{$ordnumber} );
  56. my $name = $form->like( lc $form->{ $form->{vc} } );
  57. my @dptargs = ();
  58. for (qw(department employee)) {
  59. if ( $form->{$_} ) {
  60. ( $null, $var ) = split /--/, $form->{$_};
  61. $department .= " AND o.${_}_id = ?";
  62. push @dptargs, $var;
  63. }
  64. }
  65. if ( $form->{vc} ne 'customer' ) { # Sanitize $form->{vc}
  66. $form->{vc} = 'vendor';
  67. }
  68. my $query = qq|
  69. SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
  70. o.amount, ct.name, o.netamount, o.$form->{vc}_id,
  71. ex.$rate AS exchangerate, o.closed, o.quonumber,
  72. o.shippingpoint, o.shipvia, e.name AS employee,
  73. m.name AS manager, o.curr, o.ponumber
  74. FROM oe o
  75. JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
  76. LEFT JOIN employee e ON (o.employee_id = e.id)
  77. LEFT JOIN employee m ON (e.managerid = m.id)
  78. LEFT JOIN exchangerate ex
  79. ON (ex.curr = o.curr AND ex.transdate = o.transdate)
  80. WHERE o.quotation = ?
  81. $department|;
  82. my @queryargs = @dptargs;
  83. unshift @queryargs, $quotation;
  84. my %ordinal = (
  85. id => 1,
  86. ordnumber => 2,
  87. transdate => 3,
  88. reqdate => 4,
  89. name => 6,
  90. quonumber => 11,
  91. shipvia => 13,
  92. employee => 14,
  93. manager => 15,
  94. curr => 16,
  95. ponumber => 17
  96. );
  97. my @a = ( transdate, $ordnumber, name );
  98. push @a, "employee" if $form->{l_employee};
  99. if ( $form->{type} !~ /(ship|receive)_order/ ) {
  100. push @a, "manager" if $form->{l_manager};
  101. }
  102. my $sortorder = $form->sort_order( \@a, \%ordinal );
  103. # build query if type eq (ship|receive)_order
  104. if ( $form->{type} =~ /(ship|receive)_order/ ) {
  105. my ( $warehouse, $warehouse_id ) = split /--/, $form->{warehouse};
  106. $query = qq|
  107. SELECT DISTINCT o.id, o.ordnumber, o.transdate,
  108. o.reqdate, o.amount, ct.name, o.netamount,
  109. o.$form->{vc}_id, ex.$rate AS exchangerate,
  110. o.closed, o.quonumber, o.shippingpoint,
  111. o.shipvia, e.name AS employee, o.curr,
  112. o.ponumber
  113. FROM oe o
  114. JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
  115. JOIN orderitems oi ON (oi.trans_id = o.id)
  116. JOIN parts p ON (p.id = oi.parts_id)|;
  117. if ( $warehouse_id && $form->{type} eq 'ship_order' ) {
  118. $query .= qq|
  119. JOIN inventory i ON (oi.parts_id = i.parts_id)
  120. |;
  121. }
  122. $query .= qq|
  123. LEFT JOIN employee e ON (o.employee_id = e.id)
  124. LEFT JOIN exchangerate ex
  125. ON (ex.curr = o.curr
  126. AND ex.transdate = o.transdate)
  127. WHERE o.quotation = '0'
  128. AND (p.inventory_accno_id > 0 OR p.assembly = '1')
  129. AND oi.qty != oi.ship
  130. $department|;
  131. @queryargs = @dptargs; #reset @queryargs
  132. if ( $warehouse_id && $form->{type} eq 'ship_order' ) {
  133. $query .= qq|
  134. AND i.warehouse_id = ?
  135. AND (
  136. SELECT SUM(i.qty)
  137. FROM inventory i
  138. WHERE oi.parts_id = i.parts_id
  139. AND i.warehouse_id = ?
  140. ) > 0|;
  141. push( @queryargs, $warehouse_id, $warehouse_id );
  142. }
  143. }
  144. if ( $form->{"$form->{vc}_id"} ) {
  145. $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  146. }
  147. elsif ( $form->{ $form->{vc} } ne "" ) {
  148. $query .= " AND lower(ct.name) LIKE ?";
  149. push @queryargs, $name;
  150. }
  151. if ( $form->{$ordnumber} ne "" ) {
  152. $ordnumber = ($ordnumber eq 'ordnumber') ? 'ordnumber' : 'quonumber';
  153. $query .= " AND lower($ordnumber) LIKE ?";
  154. push @queryargs, $number;
  155. $form->{open} = 1;
  156. $form->{closed} = 1;
  157. }
  158. if ( $form->{ponumber} ne "" ) {
  159. $query .= " AND lower(ponumber) LIKE '%' || lower(?) || '%'";
  160. push @queryargs, $form->{ponumber};
  161. }
  162. if ( !$form->{open} && !$form->{closed} ) {
  163. $query .= " AND o.id = 0";
  164. }
  165. elsif ( !( $form->{open} && $form->{closed} ) ) {
  166. $query .=
  167. ( $form->{open} ) ? " AND o.closed = '0'" : " AND o.closed = '1'";
  168. }
  169. if ( $form->{shipvia} ne "" ) {
  170. $var = $form->like( lc $form->{shipvia} );
  171. $query .= " AND lower(o.shipvia) LIKE ?";
  172. push @queryargs, $var;
  173. }
  174. if ( $form->{description} ne "" ) {
  175. $var = $dbh->quote($form->like( lc $form->{description} ));
  176. $query .= " AND o.id IN (SELECT DISTINCT trans_id
  177. FROM orderitems
  178. WHERE lower(description) LIKE $var)";
  179. push @queryargs, $var;
  180. }
  181. if ( $form->{transdatefrom} ) {
  182. $query .= " AND o.transdate >= ?";
  183. push @queryargs, $form->{transdatefrom};
  184. }
  185. if ( $form->{transdateto} ) {
  186. $query .= " AND o.transdate <= ?";
  187. push @queryargs, $form->{transdateto};
  188. }
  189. $query .= " ORDER by $sortorder";
  190. my $sth = $dbh->prepare($query);
  191. $sth->execute(@queryargs) || $form->dberror($query);
  192. my %oid = ();
  193. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  194. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  195. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  196. if ( $ref->{id} != $oid{id}{ $ref->{id} } ) {
  197. push @{ $form->{OE} }, $ref;
  198. $oid{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} }++;
  199. }
  200. $oid{id}{ $ref->{id} } = $ref->{id};
  201. }
  202. $sth->finish;
  203. $dbh->commit;
  204. if ( $form->{type} =~ /^consolidate_/ ) {
  205. @a = ();
  206. foreach $ref ( @{ $form->{OE} } ) {
  207. push @a, $ref
  208. if $oid{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} } > 1;
  209. }
  210. @{ $form->{OE} } = @a;
  211. }
  212. }
  213. sub save {
  214. my ( $self, $myconfig, $form ) = @_;
  215. my $ordnumber;
  216. my $quotation;
  217. my $numberfld;
  218. if ( $form->{type} =~ /_order$/ ) {
  219. $quotation = "0";
  220. $ordnumber = "ordnumber";
  221. $numberfld =
  222. ( $form->{vc} eq 'customer' )
  223. ? "sonumber"
  224. : "ponumber";
  225. }
  226. else {
  227. $quotation = "1";
  228. $ordnumber = "quonumber";
  229. $numberfld =
  230. ( $form->{vc} eq 'customer' )
  231. ? "sqnumber"
  232. : "rfqnumber";
  233. }
  234. $form->{"$ordnumber"} =
  235. $form->update_defaults( $myconfig, $numberfld, $dbh )
  236. unless $form->{"$ordnumber"};
  237. $form->db_prepare_vars(
  238. "quonumber", "transdate", "vendor_id", "customer_id",
  239. "reqdate", "taxincluded", "shippingpoint", "shipvia",
  240. "currency", "department_id", "employee_id", "language_code",
  241. "ponumber", "terms"
  242. );
  243. # connect to database, turn off autocommit
  244. my $dbh = $form->{dbh};
  245. my @queryargs;
  246. $form->{vc} = ( $form->{vc} eq 'customer' ) ? 'customer' : 'vendor';
  247. my $query;
  248. my $sth;
  249. my $null;
  250. my $exchangerate = 0;
  251. ( $null, $form->{employee_id} ) = split /--/, $form->{employee};
  252. if ( !$form->{employee_id} ) {
  253. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  254. $form->{employee} = "$form->{employee}--$form->{employee_id}";
  255. }
  256. my $ml = ( $form->{type} eq 'sales_order' ) ? 1 : -1;
  257. $query = qq|
  258. SELECT p.assembly, p.project_id
  259. FROM parts p WHERE p.id = ?|;
  260. my $pth = $dbh->prepare($query) || $form->dberror($query);
  261. if ( $form->{id} ) {
  262. $query = qq|SELECT id FROM oe WHERE id = $form->{id}|;
  263. if ( $dbh->selectrow_array($query) ) {
  264. &adj_onhand( $dbh, $form, $ml )
  265. if $form->{type} =~ /_order$/;
  266. $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
  267. $sth = $dbh->prepare($query);
  268. $sth->execute( $form->{id} ) || $form->dberror($query);
  269. $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
  270. $sth = $dbh->prepare($query);
  271. $sth->execute( $form->{id} ) || $form->dberror($query);
  272. }
  273. else { # id is not in the database
  274. delete $form->{id};
  275. }
  276. }
  277. my $did_insert = 0;
  278. if ( !$form->{id} ) {
  279. $query = qq|SELECT nextval('id')|;
  280. $sth = $dbh->prepare($query);
  281. $sth->execute || $form->dberror($query);
  282. ( $form->{id} ) = $sth->fetchrow_array;
  283. $sth->finish;
  284. my $uid = localtime;
  285. $uid .= "$$";
  286. if ( !$form->{reqdate} ) {
  287. $form->{reqdate} = undef;
  288. }
  289. if ( !$form->{transdate} ) {
  290. $form->{transdate} = "now";
  291. }
  292. if ( ( $form->{closed} ne 't' ) and ( $form->{closed} ne "1" ) ) {
  293. $form->{closed} = 'f';
  294. }
  295. # $form->{id} is safe because it is only pulled *from* the db.
  296. $query = qq|
  297. INSERT INTO oe
  298. (id, ordnumber, quonumber, transdate, vendor_id,
  299. customer_id, reqdate, shippingpoint, shipvia,
  300. notes, intnotes, curr, closed, department_id,
  301. employee_id, language_code, ponumber, terms,
  302. quotation)
  303. VALUES
  304. ($form->{id}, ?, ?, ?, ?,
  305. ?, ?, ?, ?,
  306. ?, ?, ?, ?, ?,
  307. ?, ?, ?, ?, ?)|;
  308. @queryargs = (
  309. $form->{ordnumber}, $form->{quonumber},
  310. $form->{transdate}, $form->{vendor_id},
  311. $form->{customer_id}, $form->{reqdate},
  312. $form->{shippingpoint}, $form->{shipvia},
  313. $form->{notes}, $form->{intnotes},
  314. $form->{currency}, $form->{closed},
  315. $form->{department_id}, $form->{employee_id},
  316. $form->{language_code}, $form->{ponumber},
  317. $form->{terms}, $quotation
  318. );
  319. $sth = $dbh->prepare($query);
  320. $sth->execute(@queryargs) || $form->dberror($query);
  321. $sth->finish;
  322. @queries = $form->run_custom_queries( 'oe', 'INSERT' );
  323. }
  324. my $amount;
  325. my $linetotal;
  326. my $discount;
  327. my $project_id;
  328. my $taxrate;
  329. my $taxamount;
  330. my $fxsellprice;
  331. my %taxbase;
  332. my @taxaccounts;
  333. my %taxaccounts;
  334. my $netamount = 0;
  335. my $rowcount = $form->{rowcount};
  336. for my $i ( 1 .. $rowcount ) {
  337. $form->{"ship_$i"} = 0 unless $form->{"ship_$i"};
  338. $form->db_prepare_vars( "orderitems_id_$i", "id_$i", "description_$i",
  339. "project_id_$i" );
  340. for (qw(qty ship)) {
  341. $form->{"${_}_$i"} =
  342. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  343. }
  344. $form->{"discount_$i"} =
  345. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) / 100;
  346. $form->{"sellprice_$i"} =
  347. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  348. if ( $form->{"qty_$i"} ) {
  349. $pth->execute( $form->{"id_$i"} );
  350. $ref = $pth->fetchrow_hashref(NAME_lc);
  351. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  352. $pth->finish;
  353. $fxsellprice = $form->{"sellprice_$i"};
  354. my ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  355. $dec = length $dec;
  356. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  357. $discount =
  358. $form->round_amount(
  359. $form->{"sellprice_$i"} * $form->{"discount_$i"},
  360. $decimalplaces );
  361. $form->{"sellprice_$i"} =
  362. $form->round_amount( $form->{"sellprice_$i"} - $discount,
  363. $decimalplaces );
  364. $linetotal =
  365. $form->round_amount( $form->{"sellprice_$i"} * $form->{"qty_$i"},
  366. 2 );
  367. @taxaccounts = Tax::init_taxes( $form, $form->{"taxaccounts_$i"},
  368. $form->{taxaccounts} );
  369. if ( $form->{taxincluded} ) {
  370. $taxamount =
  371. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  372. $form->{"sellprice_$i"} =
  373. Tax::extract_taxes( \@taxaccounts, $form,
  374. $form->{"sellprice_$i"} );
  375. $taxbase =
  376. Tax::extract_taxes( \@taxaccounts, $form, $linetotal );
  377. }
  378. else {
  379. $taxamount =
  380. Tax::apply_taxes( \@taxaccounts, $form, $linetotal );
  381. $taxbase = $linetotal;
  382. }
  383. if ( @taxaccounts && $form->round_amount( $taxamount, 2 ) == 0 ) {
  384. if ( $form->{taxincluded} ) {
  385. foreach $item (@taxaccounts) {
  386. $taxamount = $form->round_amount( $item->value, 2 );
  387. $taxaccounts{ $item->account } += $taxamount;
  388. $taxdiff += $taxamount;
  389. $taxbase{ $item->account } += $taxbase;
  390. }
  391. $taxaccounts{ $taxaccounts[0]->account } += $taxdiff;
  392. }
  393. else {
  394. foreach $item (@taxaccounts) {
  395. $taxaccounts{ $item->account } += $item->value;
  396. $taxbase{ $item->account } += $taxbase;
  397. }
  398. }
  399. }
  400. else {
  401. foreach $item (@taxaccounts) {
  402. $taxaccounts{ $item->account } += $item->value;
  403. $taxbase{ $item->account } += $taxbase;
  404. }
  405. }
  406. $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
  407. if ( $form->{"projectnumber_$i"} ne "" ) {
  408. ( $null, $project_id ) = split /--/,
  409. $form->{"projectnumber_$i"};
  410. }
  411. $project_id = $form->{"project_id_$i"}
  412. if $form->{"project_id_$i"};
  413. if ( !$form->{"reqdate_$i"} ) {
  414. $form->{"reqdate_$i"} = undef;
  415. }
  416. @queryargs = ();
  417. # save detail record in orderitems table
  418. $query = qq|INSERT INTO orderitems (|;
  419. $query .= qq|
  420. trans_id, parts_id, description, qty, sellprice,
  421. discount, unit, reqdate, project_id, ship,
  422. serialnumber, notes)
  423. VALUES (|;
  424. $query .= qq| ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
  425. $sth = $dbh->prepare($query);
  426. push( @queryargs,
  427. $form->{id}, $form->{"id_$i"},
  428. $form->{"description_$i"}, $form->{"qty_$i"},
  429. $fxsellprice, $form->{"discount_$i"},
  430. $form->{"unit_$i"}, $form->{"reqdate_$i"},
  431. $project_id, $form->{"ship_$i"},
  432. $form->{"serialnumber_$i"}, $form->{"notes_$i"} );
  433. $sth->execute(@queryargs) || $form->dberror($query);
  434. $form->{"sellprice_$i"} = $fxsellprice;
  435. }
  436. $form->{"discount_$i"} *= 100;
  437. }
  438. # set values which could be empty
  439. for (qw(vendor_id customer_id taxincluded closed quotation)) {
  440. $form->{$_} *= 1;
  441. }
  442. # add up the tax
  443. my $tax = 0;
  444. for ( keys %taxaccounts ) { $tax += $taxaccounts{$_} }
  445. $amount = $form->round_amount( $netamount + $tax, 2 );
  446. $netamount = $form->round_amount( $netamount, 2 );
  447. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  448. $form->{exchangerate} = 1;
  449. }
  450. else {
  451. $exchangerate =
  452. $form->check_exchangerate( $myconfig, $form->{currency},
  453. $form->{transdate},
  454. ( $form->{vc} eq 'customer' ) ? 'buy' : 'sell' );
  455. }
  456. $form->{exchangerate} =
  457. ($exchangerate)
  458. ? $exchangerate
  459. : $form->parse_amount( $myconfig, $form->{exchangerate} );
  460. ( $null, $form->{department_id} ) = split( /--/, $form->{department} );
  461. for (qw(department_id terms)) { $form->{$_} *= 1 }
  462. if ($did_insert) {
  463. $query = qq|
  464. UPDATE oe SET
  465. amount = ?,
  466. netamount = ?,
  467. taxincluded = ?
  468. WHERE id = ?|;
  469. @queryargs = ( $amount, $netamount, $form->{taxincluded}, $form->{id} );
  470. }
  471. else {
  472. # save OE record
  473. $query = qq|
  474. UPDATE oe set
  475. ordnumber = ?,
  476. quonumber = ?,
  477. transdate = ?,
  478. vendor_id = ?,
  479. customer_id = ?,
  480. amount = ?,
  481. netamount = ?,
  482. reqdate = ?,
  483. taxincluded = ?,
  484. shippingpoint = ?,
  485. shipvia = ?,
  486. notes = ?,
  487. intnotes = ?,
  488. curr = ?,
  489. closed = ?,
  490. quotation = ?,
  491. department_id = ?,
  492. employee_id = ?,
  493. language_code = ?,
  494. ponumber = ?,
  495. terms = ?
  496. WHERE id = ?|;
  497. if ( !$form->{reqdate} ) {
  498. $form->{reqdate} = undef;
  499. }
  500. @queryargs = (
  501. $form->{ordnumber}, $form->{quonumber},
  502. $form->{transdate}, $form->{vendor_id},
  503. $form->{customer_id}, $amount,
  504. $netamount, $form->{reqdate},
  505. $form->{taxincluded}, $form->{shippingpoint},
  506. $form->{shipvia}, $form->{notes},
  507. $form->{intnotes}, $form->{currency},
  508. $form->{closed}, $quotation,
  509. $form->{department_id}, $form->{employee_id},
  510. $form->{language_code}, $form->{ponumber},
  511. $form->{terms}, $form->{id}
  512. );
  513. }
  514. $sth = $dbh->prepare($query);
  515. $sth->execute(@queryargs) || $form->dberror($query);
  516. if ( $amount->is_nan ) {
  517. $dbh->rollback;
  518. return;
  519. }
  520. if ( !$did_insert ) {
  521. @queries = $form->run_custom_queries( 'oe', 'UPDATE' );
  522. }
  523. $form->{ordtotal} = $amount;
  524. # add shipto
  525. $form->{name} = $form->{ $form->{vc} };
  526. $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
  527. $form->add_shipto( $dbh, $form->{id} );
  528. # save printed, emailed, queued
  529. $form->save_status($dbh);
  530. if ( ( $form->{currency} ne $form->{defaultcurrency} ) && !$exchangerate ) {
  531. if ( $form->{vc} eq 'customer' ) {
  532. $form->update_exchangerate( $dbh, $form->{currency},
  533. $form->{transdate}, $form->{exchangerate}, 0 );
  534. }
  535. if ( $form->{vc} eq 'vendor' ) {
  536. $form->update_exchangerate( $dbh, $form->{currency},
  537. $form->{transdate}, 0, $form->{exchangerate} );
  538. }
  539. }
  540. if ( $form->{type} =~ /_order$/ ) {
  541. # adjust onhand
  542. &adj_onhand( $dbh, $form, $ml * -1 );
  543. &adj_inventory( $dbh, $myconfig, $form );
  544. }
  545. my %audittrail = (
  546. tablename => 'oe',
  547. reference => ( $form->{type} =~ /_order$/ )
  548. ? $form->{ordnumber}
  549. : $form->{quonumber},
  550. formname => $form->{type},
  551. action => 'saved',
  552. id => $form->{id}
  553. );
  554. $form->audittrail( $dbh, "", \%audittrail );
  555. $form->save_recurring( $dbh, $myconfig );
  556. my $rc = $dbh->commit;
  557. $rc;
  558. }
  559. sub delete {
  560. my ( $self, $myconfig, $form ) = @_;
  561. # connect to database
  562. my $dbh = $form->{dbh};
  563. # delete spool files
  564. my $query = qq|
  565. SELECT spoolfile FROM status
  566. WHERE trans_id = ?
  567. AND spoolfile IS NOT NULL|;
  568. $sth = $dbh->prepare($query);
  569. $sth->execute( $form->{id} ) || $form->dberror($query);
  570. my $spoolfile;
  571. my @spoolfiles = ();
  572. while ( ($spoolfile) = $sth->fetchrow_array ) {
  573. push @spoolfiles, $spoolfile;
  574. }
  575. $sth->finish;
  576. $query = qq|
  577. SELECT o.parts_id, o.ship, p.inventory_accno_id, p.assembly
  578. FROM orderitems o
  579. JOIN parts p ON (p.id = o.parts_id)
  580. WHERE trans_id = ?|;
  581. $sth = $dbh->prepare($query);
  582. $sth->execute( $form->{id} ) || $form->dberror($query);
  583. if ( $form->{type} =~ /_order$/ ) {
  584. $ml = ( $form->{type} eq 'purchase_order' ) ? -1 : 1;
  585. while ( my ( $id, $ship, $inv, $assembly ) = $sth->fetchrow_array ) {
  586. $form->update_balance( $dbh, "parts", "onhand", "id = $id",
  587. $ship * $ml )
  588. if ( $inv || $assembly );
  589. }
  590. }
  591. $sth->finish;
  592. # delete inventory
  593. $query = qq|DELETE FROM inventory WHERE trans_id = ?|;
  594. $sth = $dbh->prepare($query);
  595. $sth->execute( $form->{id} ) || $form->dberror($query);
  596. $sth->finish;
  597. # delete status entries
  598. $query = qq|DELETE FROM status WHERE trans_id = ?|;
  599. $sth = $dbh->prepare($query);
  600. $sth->execute( $form->{id} ) || $form->dberror($query);
  601. $sth->finish;
  602. # delete OE record
  603. $query = qq|DELETE FROM oe WHERE id = ?|;
  604. $sth = $dbh->prepare($query);
  605. $sth->execute( $form->{id} ) || $form->dberror($query);
  606. $sth->finish;
  607. # delete individual entries
  608. $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
  609. $sth->finish;
  610. $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
  611. $sth = $dbh->prepare($query);
  612. $sth->execute( $form->{id} ) || $form->dberror($query);
  613. $sth->finish;
  614. my %audittrail = (
  615. tablename => 'oe',
  616. reference => ( $form->{type} =~ /_order$/ )
  617. ? $form->{ordnumber}
  618. : $form->{quonumber},
  619. formname => $form->{type},
  620. action => 'deleted',
  621. id => $form->{id}
  622. );
  623. $form->audittrail( $dbh, "", \%audittrail );
  624. my $rc = $dbh->commit;
  625. if ($rc) {
  626. foreach $spoolfile (@spoolfiles) {
  627. unlink "${LedgerSMB::Sysconfig::spool}/$spoolfile" if $spoolfile;
  628. }
  629. }
  630. $rc;
  631. }
  632. sub retrieve {
  633. use LedgerSMB::PriceMatrix;
  634. my ( $self, $myconfig, $form ) = @_;
  635. # connect to database
  636. my $dbh = $form->{dbh};
  637. my $query;
  638. my $sth;
  639. my $var;
  640. my $ref;
  641. $query = qq|
  642. SELECT value, current_date FROM defaults
  643. WHERE setting_key = 'curr'|;
  644. ( $form->{currencies}, $form->{transdate} ) = $dbh->selectrow_array($query);
  645. if ( $form->{id} ) {
  646. # retrieve order
  647. $query = qq|
  648. SELECT o.ordnumber, o.transdate, o.reqdate, o.terms,
  649. o.taxincluded, o.shippingpoint, o.shipvia,
  650. o.notes, o.intnotes, o.curr AS currency,
  651. e.name AS employee, o.employee_id,
  652. o.$form->{vc}_id, vc.name AS $form->{vc},
  653. o.amount AS invtotal, o.closed, o.reqdate,
  654. o.quonumber, o.department_id,
  655. d.description AS department, o.language_code,
  656. o.ponumber
  657. FROM oe o
  658. JOIN $form->{vc} vc ON (o.$form->{vc}_id = vc.id)
  659. LEFT JOIN employee e ON (o.employee_id = e.id)
  660. LEFT JOIN department d ON (o.department_id = d.id)
  661. WHERE o.id = ?|;
  662. $sth = $dbh->prepare($query);
  663. $sth->execute( $form->{id} ) || $form->dberror($query);
  664. $ref = $sth->fetchrow_hashref(NAME_lc);
  665. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  666. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  667. $sth->finish;
  668. $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
  669. $sth = $dbh->prepare($query);
  670. $sth->execute( $form->{id} ) || $form->dberror($query);
  671. $ref = $sth->fetchrow_hashref(NAME_lc);
  672. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  673. $sth->finish;
  674. # get printed, emailed and queued
  675. $query = qq|
  676. SELECT s.printed, s.emailed, s.spoolfile, s.formname
  677. FROM status s
  678. WHERE s.trans_id = ?|;
  679. $sth = $dbh->prepare($query);
  680. $sth->execute( $form->{id} ) || $form->dberror($query);
  681. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  682. $form->{printed} .= "$ref->{formname} "
  683. if $ref->{printed};
  684. $form->{emailed} .= "$ref->{formname} "
  685. if $ref->{emailed};
  686. $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
  687. if $ref->{spoolfile};
  688. }
  689. $sth->finish;
  690. for (qw(printed emailed queued)) { $form->{$_} =~ s/ +$//g }
  691. # retrieve individual items
  692. $query = qq|
  693. SELECT o.id AS orderitems_id, p.partnumber, p.assembly,
  694. o.description, o.qty, o.sellprice,
  695. o.parts_id AS id, o.unit, o.discount, p.bin,
  696. o.reqdate, o.project_id, o.ship, o.serialnumber,
  697. o.notes, pr.projectnumber, pg.partsgroup,
  698. p.partsgroup_id, p.partnumber AS sku,
  699. p.listprice, p.lastcost, p.weight, p.onhand,
  700. p.inventory_accno_id, p.income_accno_id,
  701. p.expense_accno_id, t.description
  702. AS partsgrouptranslation
  703. FROM orderitems o
  704. JOIN parts p ON (o.parts_id = p.id)
  705. LEFT JOIN project pr ON (o.project_id = pr.id)
  706. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  707. LEFT JOIN translation t
  708. ON (t.trans_id = p.partsgroup_id
  709. AND t.language_code = ?)
  710. WHERE o.trans_id = ?
  711. ORDER BY o.id|;
  712. $sth = $dbh->prepare($query);
  713. $sth->execute( $form->{language_code}, $form->{id} )
  714. || $form->dberror($query);
  715. # foreign exchange rates
  716. &exchangerate_defaults( $dbh, $form );
  717. # query for price matrix
  718. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  719. # taxes
  720. $query = qq|
  721. SELECT c.accno FROM chart c
  722. JOIN partstax pt ON (pt.chart_id = c.id)
  723. WHERE pt.parts_id = ?|;
  724. my $tth = $dbh->prepare($query) || $form->dberror($query);
  725. my $taxrate;
  726. my $ptref;
  727. my $sellprice;
  728. my $listprice;
  729. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  730. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  731. ($decimalplaces) = ( $ref->{sellprice} =~ /\.(\d+)/ );
  732. $decimalplaces = length $decimalplaces;
  733. $decimalplaces = ( $decimalplaces > 2 ) ? $decimalplaces : 2;
  734. $tth->execute( $ref->{id} );
  735. $ref->{taxaccounts} = "";
  736. $taxrate = 0;
  737. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  738. $ref->{taxaccounts} .= "$ptref->{accno} ";
  739. $taxrate += $form->{"$ptref->{accno}_rate"};
  740. }
  741. $tth->finish;
  742. chop $ref->{taxaccounts};
  743. # preserve price
  744. $sellprice = $ref->{sellprice};
  745. # multiply by exchangerate
  746. $ref->{sellprice} =
  747. $form->round_amount(
  748. $ref->{sellprice} * $form->{ $form->{currency} },
  749. $decimalplaces );
  750. for (qw(listprice lastcost)) {
  751. $ref->{$_} =
  752. $form->round_amount(
  753. $ref->{$_} / $form->{ $form->{currency} },
  754. $decimalplaces );
  755. }
  756. # partnumber and price matrix
  757. PriceMatrix::price_matrix( $pmh, $ref, $form->{transdate},
  758. $decimalplaces, $form, $myconfig );
  759. $ref->{sellprice} = $sellprice;
  760. $ref->{partsgroup} = $ref->{partsgrouptranslation}
  761. if $ref->{partsgrouptranslation};
  762. push @{ $form->{form_details} }, $ref;
  763. }
  764. $sth->finish;
  765. # get recurring transaction
  766. $form->get_recurring;
  767. @queries = $form->run_custom_queries( 'oe', 'SELECT' );
  768. }
  769. else {
  770. # get last name used
  771. $form->lastname_used( $myconfig, $dbh, $form->{vc} )
  772. unless $form->{"$form->{vc}_id"};
  773. delete $form->{notes};
  774. }
  775. $dbh->commit;
  776. }
  777. sub exchangerate_defaults {
  778. my ( $dbh2, $form ) = @_;
  779. $dbh = $form->{dbh};
  780. my $var;
  781. my $buysell = ( $form->{vc} eq "customer" ) ? "buy" : "sell";
  782. # get default currencies
  783. my $query = qq|
  784. SELECT substr(value,1,3), value FROM defaults
  785. WHERE setting_key = 'curr'|;
  786. ( $form->{defaultcurrency}, $form->{currencies} ) =
  787. $dbh->selectrow_array($query);
  788. $query = qq|
  789. SELECT $buysell
  790. FROM exchangerate
  791. WHERE curr = ?
  792. AND transdate = ?|;
  793. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  794. $query = qq~
  795. SELECT max(transdate || ' ' || $buysell || ' ' || curr)
  796. FROM exchangerate
  797. WHERE curr = ?~;
  798. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  799. # get exchange rates for transdate or max
  800. foreach $var ( split /:/, substr( $form->{currencies}, 4 ) ) {
  801. $eth1->execute( $var, $form->{transdate} );
  802. my @exchangelist;
  803. @exchangelist = $eth1->fetchrow_array;
  804. $form->db_parse_numeric(sth=>$eth1, arrayref=>\@exchangelist);
  805. $form->{$var} = shift @array;
  806. if ( !$form->{$var} ) {
  807. $eth2->execute($var);
  808. @exchangelist = $eth2->fetchrow_array;
  809. $form->db_parse_numeric(sth=>$eth2, arrayref=>\@exchangelist);
  810. ( $form->{$var} ) = @exchangelist;
  811. ( $null, $form->{$var} ) = split / /, $form->{$var};
  812. $form->{$var} = 1 unless $form->{$var};
  813. $eth2->finish;
  814. }
  815. $eth1->finish;
  816. }
  817. $form->{ $form->{currency} } = $form->{exchangerate}
  818. if $form->{exchangerate};
  819. $form->{ $form->{currency} } ||= 1;
  820. $form->{ $form->{defaultcurrency} } = 1;
  821. }
  822. sub order_details {
  823. use LedgerSMB::CP;
  824. my ( $self, $myconfig, $form ) = @_;
  825. # connect to database
  826. my $dbh = $form->{dbh};
  827. my $query;
  828. my $sth;
  829. my $item;
  830. my $i;
  831. my @sortlist = ();
  832. my $projectnumber;
  833. my $projectdescription;
  834. my $projectnumber_id;
  835. my $translation;
  836. my $partsgroup;
  837. my @queryargs;
  838. my @taxaccounts;
  839. my %taxaccounts; # I don't think this works.
  840. my $tax;
  841. my $taxrate;
  842. my $taxamount;
  843. my %translations;
  844. my $language_code = $form->{dbh}->quote( $form->{language_code} );
  845. $query = qq|
  846. SELECT p.description, t.description
  847. FROM project p
  848. LEFT JOIN translation t ON (t.trans_id = p.id AND
  849. t.language_code = $language_code)
  850. WHERE id = ?|;
  851. my $prh = $dbh->prepare($query) || $form->dberror($query);
  852. $query = qq|
  853. SELECT inventory_accno_id, income_accno_id,
  854. expense_accno_id, assembly FROM parts
  855. WHERE id = ?|;
  856. my $pth = $dbh->prepare($query) || $form->dberror($query);
  857. my $sortby;
  858. # sort items by project and partsgroup
  859. for $i ( 1 .. $form->{rowcount} ) {
  860. if ( $form->{"id_$i"} ) {
  861. # account numbers
  862. $pth->execute( $form->{"id_$i"} );
  863. $ref = $pth->fetchrow_hashref(NAME_lc);
  864. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  865. $pth->finish;
  866. $projectnumber_id = 0;
  867. $projectnumber = "";
  868. $form->{partsgroup} = "";
  869. $form->{projectnumber} = "";
  870. if ( $form->{groupprojectnumber}
  871. || $form->{grouppartsgroup} )
  872. {
  873. $inventory_accno_id =
  874. ( $form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"} )
  875. ? "1"
  876. : "";
  877. if ( $form->{groupprojectnumber} ) {
  878. ( $projectnumber, $projectnumber_id ) =
  879. split /--/, $form->{"projectnumber_$i"};
  880. }
  881. if ( $form->{grouppartsgroup} ) {
  882. ( $form->{partsgroup} ) = split /--/,
  883. $form->{"partsgroup_$i"};
  884. }
  885. if ( $projectnumber_id
  886. && $form->{groupprojectnumber} )
  887. {
  888. if ( $translation{$projectnumber_id} ) {
  889. $form->{projectnumber} =
  890. $translation{$projectnumber_id};
  891. }
  892. else {
  893. # get project description
  894. $prh->execute($projectnumber_id);
  895. ( $projectdescription, $translation ) =
  896. $prh->fetchrow_array;
  897. $prh->finish;
  898. $form->{projectnumber} =
  899. ($translation)
  900. ? "$projectnumber, \n" . "$translation"
  901. : "$projectnumber, \n" . "$projectdescription";
  902. $translation{$projectnumber_id} =
  903. $form->{projectnumber};
  904. }
  905. }
  906. if ( $form->{grouppartsgroup}
  907. && $form->{partsgroup} )
  908. {
  909. $form->{projectnumber} .= " / "
  910. if $projectnumber_id;
  911. $form->{projectnumber} .= $form->{partsgroup};
  912. }
  913. $form->format_string(projectnumber);
  914. }
  915. $sortby = qq|$projectnumber$form->{partsgroup}|;
  916. if ( $form->{sortby} ne 'runningnumber' ) {
  917. for (qw(partnumber description bin)) {
  918. $sortby .= $form->{"${_}_$i"}
  919. if $form->{sortby} eq $_;
  920. }
  921. }
  922. push @sortlist,
  923. [
  924. $i,
  925. "$projectnumber$form->{partsgroup}" . "$inventory_accno_id",
  926. $form->{projectnumber},
  927. $projectnumber_id,
  928. $form->{partsgroup},
  929. $sortby
  930. ];
  931. }
  932. }
  933. delete $form->{projectnumber};
  934. # sort the whole thing by project and group
  935. @sortlist = sort { $a->[5] cmp $b->[5] } @sortlist;
  936. # if there is a warehouse limit picking
  937. if ( $form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/ ) {
  938. # run query to check for inventory
  939. $query = qq|
  940. SELECT sum(qty) AS qty FROM inventory
  941. WHERE parts_id = ? AND warehouse_id = ?|;
  942. $sth = $dbh->prepare($query) || $form->dberror($query);
  943. for $i ( 1 .. $form->{rowcount} ) {
  944. $sth->execute( $form->{"id_$i"}, $form->{warehouse_id} )
  945. || $form->dberror;
  946. my @qtylist = $sth->fetchrow_array;
  947. $form->db_parse_numeric(sth=>$sth, arrayref=>\@qtylist);
  948. ($qty) = @qtylist; $sth->fetchrow_array;
  949. $sth->finish;
  950. $form->{"qty_$i"} = 0 if $qty == 0;
  951. if ( $form->parse_amount( $myconfig, $form->{"ship_$i"} ) > $qty ) {
  952. $form->{"ship_$i"} = $form->format_amount( $myconfig, $qty );
  953. }
  954. }
  955. }
  956. my $runningnumber = 1;
  957. my $sameitem = "";
  958. my $subtotal;
  959. my $k = scalar @sortlist;
  960. my $j = 0;
  961. foreach $item (@sortlist) {
  962. $i = $item->[0];
  963. $j++;
  964. if ( $form->{groupprojectnumber} || $form->{grouppartsgroup} ) {
  965. if ( $item->[1] ne $sameitem ) {
  966. $sameitem = $item->[1];
  967. $ok = 0;
  968. if ( $form->{groupprojectnumber} ) {
  969. $ok = $form->{"projectnumber_$i"};
  970. }
  971. if ( $form->{grouppartsgroup} ) {
  972. $ok = $form->{"partsgroup_$i"}
  973. unless $ok;
  974. }
  975. if ($ok) {
  976. if ( $form->{"inventory_accno_id_$i"}
  977. || $form->{"assembly_$i"} )
  978. {
  979. push( @{ $form->{part} }, "" );
  980. push( @{ $form->{service} }, NULL );
  981. }
  982. else {
  983. push( @{ $form->{part} }, NULL );
  984. push( @{ $form->{service} }, "" );
  985. }
  986. push( @{ $form->{description} }, $item->[2] );
  987. for (
  988. qw(taxrates runningnumber
  989. number sku qty ship unit bin
  990. serialnumber requiredate
  991. projectnumber sellprice
  992. listprice netprice discount
  993. discountrate linetotal weight
  994. itemnotes)
  995. )
  996. {
  997. push( @{ $form->{$_} }, "" );
  998. }
  999. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  1000. }
  1001. }
  1002. }
  1003. $form->{"qty_$i"} = $form->parse_amount( $myconfig, $form->{"qty_$i"} );
  1004. $form->{"ship_$i"} =
  1005. $form->parse_amount( $myconfig, $form->{"ship_$i"} );
  1006. if ( $form->{"qty_$i"} ) {
  1007. $form->{discount} = [] if ref $form->{discount} ne 'ARRAY';
  1008. $form->{totalqty} += $form->{"qty_$i"};
  1009. $form->{totalship} += $form->{"ship_$i"};
  1010. $form->{totalweight} +=
  1011. ( $form->{"weight_$i"} * $form->{"qty_$i"} );
  1012. $form->{totalweightship} +=
  1013. ( $form->{"weight_$i"} * $form->{"ship_$i"} );
  1014. # add number, description and qty to $form->{number}
  1015. push( @{ $form->{runningnumber} }, $runningnumber++ );
  1016. push( @{ $form->{number} }, qq|$form->{"partnumber_$i"}| );
  1017. push( @{ $form->{sku} }, qq|$form->{"sku_$i"}| );
  1018. push( @{ $form->{description} }, qq|$form->{"description_$i"}| );
  1019. push( @{ $form->{itemnotes} }, $form->{"notes_$i"} );
  1020. push(
  1021. @{ $form->{qty} },
  1022. $form->format_amount( $myconfig, $form->{"qty_$i"} )
  1023. );
  1024. push(
  1025. @{ $form->{ship} },
  1026. $form->format_amount( $myconfig, $form->{"ship_$i"} )
  1027. );
  1028. push( @{ $form->{unit} }, qq|$form->{"unit_$i"}| );
  1029. push( @{ $form->{bin} }, qq|$form->{"bin_$i"}| );
  1030. push( @{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}| );
  1031. push( @{ $form->{requiredate} }, qq|$form->{"reqdate_$i"}| );
  1032. push( @{ $form->{projectnumber} },
  1033. qq|$form->{"projectnumber_$i"}| );
  1034. push( @{ $form->{sellprice} }, $form->{"sellprice_$i"} );
  1035. push( @{ $form->{listprice} }, $form->{"listprice_$i"} );
  1036. push(
  1037. @{ $form->{weight} },
  1038. $form->format_amount(
  1039. $myconfig, $form->{"weight_$i"} * $form->{"ship_$i"}
  1040. )
  1041. );
  1042. my $sellprice =
  1043. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  1044. my ($dec) = ( $sellprice =~ /\.(\d+)/ );
  1045. $dec = length $dec;
  1046. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  1047. my $discount = $form->round_amount(
  1048. $sellprice *
  1049. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) /
  1050. 100,
  1051. $decimalplaces
  1052. );
  1053. # keep a netprice as well, (sellprice - discount)
  1054. $form->{"netprice_$i"} = $sellprice - $discount;
  1055. my $linetotal =
  1056. $form->round_amount( $form->{"qty_$i"} * $form->{"netprice_$i"},
  1057. 2 );
  1058. if ( $form->{"inventory_accno_id_$i"}
  1059. || $form->{"assembly_$i"} )
  1060. {
  1061. push( @{ $form->{part} }, $form->{"sku_$i"} );
  1062. push( @{ $form->{service} }, NULL );
  1063. $form->{totalparts} += $linetotal;
  1064. }
  1065. else {
  1066. push( @{ $form->{service} }, $form->{"sku_$i"} );
  1067. push( @{ $form->{part} }, NULL );
  1068. $form->{totalservices} += $linetotal;
  1069. }
  1070. push(
  1071. @{ $form->{netprice} },
  1072. ( $form->{"netprice_$i"} )
  1073. ? $form->format_amount( $myconfig, $form->{"netprice_$i"},
  1074. $decimalplaces )
  1075. : " "
  1076. );
  1077. $discount =
  1078. ($discount)
  1079. ? $form->format_amount( $myconfig, $discount * -1,
  1080. $decimalplaces )
  1081. : " ";
  1082. push( @{ $form->{discount} }, $discount );
  1083. push(
  1084. @{ $form->{discountrate} },
  1085. $form->format_amount( $myconfig, $form->{"discount_$i"} )
  1086. );
  1087. $form->{ordtotal} += $linetotal;
  1088. # this is for the subtotals for grouping
  1089. $subtotal += $linetotal;
  1090. $form->{"linetotal_$i"} =
  1091. $form->format_amount( $myconfig, $linetotal, 2 );
  1092. push( @{ $form->{linetotal} }, $form->{"linetotal_$i"} );
  1093. @taxaccounts = Tax::init_taxes( $form, $form->{"taxaccounts_$i"},
  1094. $form->{'taxaccounts'} );
  1095. my $ml = 1;
  1096. my @taxrates = ();
  1097. $tax = 0;
  1098. $taxamount =
  1099. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  1100. $taxbase = Tax::extract_taxes( \@taxaccounts, $form, $linetotal );
  1101. foreach $item (@taxaccounts) {
  1102. push @taxrates, Math::BigFloat->new(100) * $item->rate;
  1103. if ( $form->{taxincluded} ) {
  1104. $taxaccounts{ $item->account } += $item->value;
  1105. $taxbase{ $item->account } += $taxbase;
  1106. }
  1107. else {
  1108. Tax::apply_taxes( \@taxaccounts, $form, $linetotal );
  1109. $taxbase{ $item->account } += $linetotal;
  1110. $taxaccounts{ $item->account } += $item->value;
  1111. }
  1112. }
  1113. if ( $form->{taxincluded} ) {
  1114. $tax +=
  1115. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  1116. }
  1117. else {
  1118. $tax +=
  1119. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  1120. }
  1121. push(
  1122. @{ $form->{lineitems} },
  1123. {
  1124. amount => $linetotal,
  1125. tax => $form->round_amount( $tax, 2 )
  1126. }
  1127. );
  1128. push( @{ $form->{taxrates} },
  1129. join ' ', sort { $a <=> $b } @taxrates );
  1130. if ( $form->{"assembly_$i"} ) {
  1131. $form->{stagger} = -1;
  1132. &assembly_details( $myconfig, $form, $dbh, $form->{"id_$i"},
  1133. $oid{ $myconfig->{dbdriver} },
  1134. $form->{"qty_$i"} );
  1135. }
  1136. }
  1137. # add subtotal
  1138. if ( $form->{groupprojectnumber} || $form->{grouppartsgroup} ) {
  1139. if ($subtotal) {
  1140. if ( $j < $k ) {
  1141. # look at next item
  1142. if ( $sortlist[$j]->[1] ne $sameitem ) {
  1143. if ( $form->{"inventory_accno_id_$i"}
  1144. || $form->{"assembly_$i"} )
  1145. {
  1146. push( @{ $form->{part} }, "" );
  1147. push( @{ $form->{service} }, NULL );
  1148. }
  1149. else {
  1150. push( @{ $form->{service} }, "" );
  1151. push( @{ $form->{part} }, NULL );
  1152. }
  1153. for (
  1154. qw(
  1155. taxrates runningnumber
  1156. number sku qty ship unit
  1157. bin serialnumber
  1158. requiredate
  1159. projectnumber sellprice
  1160. listprice netprice
  1161. discount discountrate
  1162. weight itemnotes)
  1163. )
  1164. {
  1165. push( @{ $form->{$_} }, "" );
  1166. }
  1167. push(
  1168. @{ $form->{description} },
  1169. $form->{groupsubtotaldescription}
  1170. );
  1171. push(
  1172. @{ $form->{lineitems} },
  1173. {
  1174. amount => 0,
  1175. tax => 0
  1176. }
  1177. );
  1178. if ( $form->{groupsubtotaldescription} ne "" ) {
  1179. push(
  1180. @{ $form->{linetotal} },
  1181. $form->format_amount( $myconfig, $subtotal, 2 )
  1182. );
  1183. }
  1184. else {
  1185. push( @{ $form->{linetotal} }, "" );
  1186. }
  1187. $subtotal = 0;
  1188. }
  1189. }
  1190. else {
  1191. # got last item
  1192. if ( $form->{groupsubtotaldescription} ne "" ) {
  1193. if ( $form->{"inventory_accno_id_$i"}
  1194. || $form->{"assembly_$i"} )
  1195. {
  1196. push( @{ $form->{part} }, "" );
  1197. push( @{ $form->{service} }, NULL );
  1198. }
  1199. else {
  1200. push( @{ $form->{service} }, "" );
  1201. push( @{ $form->{part} }, NULL );
  1202. }
  1203. for (
  1204. qw(
  1205. taxrates runningnumber
  1206. number sku qty ship unit
  1207. bin serialnumber
  1208. requiredate
  1209. projectnumber sellprice
  1210. listprice netprice
  1211. discount discountrate
  1212. weight itemnotes)
  1213. )
  1214. {
  1215. push( @{ $form->{$_} }, "" );
  1216. }
  1217. push(
  1218. @{ $form->{description} },
  1219. $form->{groupsubtotaldescription}
  1220. );
  1221. push(
  1222. @{ $form->{linetotal} },
  1223. $form->format_amount( $myconfig, $subtotal, 2 )
  1224. );
  1225. push(
  1226. @{ $form->{lineitems} },
  1227. {
  1228. amount => 0,
  1229. tax => 0
  1230. }
  1231. );
  1232. }
  1233. }
  1234. }
  1235. }
  1236. }
  1237. $tax = 0;
  1238. foreach $item ( sort keys %taxaccounts ) {
  1239. if ( $form->round_amount( $taxaccounts{$item}, 2 ) ) {
  1240. $tax += $taxamount = $form->round_amount( $taxaccounts{$item}, 2 );
  1241. push(
  1242. @{ $form->{taxbaseinclusive} },
  1243. $form->{"${item}_taxbaseinclusive"} =
  1244. $form->round_amount( $taxbase{$item} + $tax, 2 )
  1245. );
  1246. push(
  1247. @{ $form->{taxbase} },
  1248. $form->{"${item}_taxbase"} =
  1249. $form->format_amount( $myconfig, $taxbase{$item}, 2 )
  1250. );
  1251. push(
  1252. @{ $form->{tax} },
  1253. $form->{"${item}_tax"} =
  1254. $form->format_amount( $myconfig, $taxamount, 2 )
  1255. );
  1256. push( @{ $form->{taxdescription} },
  1257. $form->{"${item}_description"} );
  1258. $form->{"${item}_taxrate"} =
  1259. $form->format_amount( $myconfig, $form->{"${item}_rate"} * 100 );
  1260. push( @{ $form->{taxrate} }, $form->{"${item}_taxrate"} );
  1261. push( @{ $form->{taxnumber} }, $form->{"${item}_taxnumber"} );
  1262. }
  1263. }
  1264. # adjust taxes for lineitems
  1265. my $total = 0;
  1266. for ( @{ $form->{lineitems} } ) {
  1267. $total += $_->{tax};
  1268. }
  1269. if ( $form->round_amount( $total, 2 ) != $form->round_amount( $tax, 2 ) ) {
  1270. # get largest amount
  1271. for ( reverse sort { $a->{tax} <=> $b->{tax} } @{ $form->{lineitems} } )
  1272. {
  1273. $_->{tax} -= $total - $tax;
  1274. last;
  1275. }
  1276. }
  1277. $i = 1;
  1278. for ( @{ $form->{lineitems} } ) {
  1279. push(
  1280. @{ $form->{linetax} },
  1281. $form->format_amount( $myconfig, $_->{tax}, 2, "" )
  1282. );
  1283. }
  1284. for (qw(totalparts totalservices)) {
  1285. $form->{$_} = $form->format_amount( $myconfig, $form->{$_}, 2 );
  1286. }
  1287. for (qw(totalqty totalship totalweight)) {
  1288. $form->{$_} = $form->format_amount( $myconfig, $form->{$_} );
  1289. }
  1290. $form->{subtotal} = $form->format_amount( $myconfig, $form->{ordtotal}, 2 );
  1291. $form->{ordtotal} =
  1292. ( $form->{taxincluded} )
  1293. ? $form->{ordtotal}
  1294. : $form->{ordtotal} + $tax;
  1295. my $c;
  1296. if ( $form->{language_code} ne "" ) {
  1297. $c = new CP $form->{language_code};
  1298. }
  1299. else {
  1300. $c = new CP $myconfig->{countrycode};
  1301. }
  1302. $c->init;
  1303. my $whole;
  1304. ( $whole, $form->{decimal} ) = split /\./, $form->{ordtotal};
  1305. $form->{decimal} .= "00";
  1306. $form->{decimal} = substr( $form->{decimal}, 0, 2 );
  1307. $form->{text_decimal} = $c->num2text( $form->{decimal} * 1 );
  1308. $form->{text_amount} = $c->num2text($whole);
  1309. $form->{integer_amount} = $form->format_amount( $myconfig, $whole );
  1310. # format amounts
  1311. $form->{quototal} = $form->{ordtotal} =
  1312. $form->format_amount( $myconfig, $form->{ordtotal}, 2 );
  1313. $form->format_string(qw(text_amount text_decimal));
  1314. $query = qq|
  1315. SELECT value FROM defaults
  1316. WHERE setting_key = 'weightunit'|;
  1317. ( $form->{weightunit} ) = $dbh->selectrow_array($query);
  1318. $dbh->commit;
  1319. }
  1320. sub assembly_details {
  1321. my ( $myconfig, $form, $dbh, $id, $oid, $qty ) = @_;
  1322. my $sm = "";
  1323. my $spacer;
  1324. $form->{stagger}++;
  1325. if ( $form->{format} eq 'html' ) {
  1326. $spacer = "&nbsp;" x ( 3 * ( $form->{stagger} - 1 ) )
  1327. if $form->{stagger} > 1;
  1328. }
  1329. if ( $form->{format} =~ /(postscript|pdf)/ ) {
  1330. if ( $form->{stagger} > 1 ) {
  1331. $spacer = ( $form->{stagger} - 1 ) * 3;
  1332. $spacer = '\rule{' . $spacer . 'mm}{0mm}';
  1333. }
  1334. }
  1335. # get parts and push them onto the stack
  1336. my $sortorder = "";
  1337. if ( $form->{grouppartsgroup} ) {
  1338. $sortorder = qq|ORDER BY pg.partsgroup, a.id|;
  1339. }
  1340. else {
  1341. $sortorder = qq|ORDER BY a.id|;
  1342. }
  1343. my $where =
  1344. ( $form->{formname} eq 'work_order' )
  1345. ? "1 = 1"
  1346. : "a.bom = '1'";
  1347. my $query = qq|
  1348. SELECT p.partnumber, p.description, p.unit, a.qty,
  1349. pg.partsgroup, p.partnumber AS sku, p.assembly, p.id,
  1350. p.bin
  1351. FROM assembly a
  1352. JOIN parts p ON (a.parts_id = p.id)
  1353. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1354. WHERE $where
  1355. AND a.id = ?
  1356. $sortorder|;
  1357. my $sth = $dbh->prepare($query);
  1358. $sth->execute($id) || $form->dberror($query);
  1359. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1360. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1361. for (qw(partnumber description partsgroup)) {
  1362. $form->{"a_$_"} = $ref->{$_};
  1363. $form->format_string("a_$_");
  1364. }
  1365. if ( $form->{grouppartsgroup} && $ref->{partsgroup} ne $sm ) {
  1366. for (
  1367. qw(
  1368. taxrates number sku unit qty runningnumber ship
  1369. bin serialnumber requiredate projectnumber
  1370. sellprice listprice netprice discount
  1371. discountrate linetotal weight itemnotes)
  1372. )
  1373. {
  1374. push( @{ $form->{$_} }, "" );
  1375. }
  1376. $sm = ( $form->{"a_partsgroup"} ) ? $form->{"a_partsgroup"} : "";
  1377. push( @{ $form->{description} }, "$spacer$sm" );
  1378. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  1379. }
  1380. if ( $form->{stagger} ) {
  1381. push(
  1382. @{ $form->{description} },
  1383. qq|$spacer$form->{"a_partnumber"}, |
  1384. . qq|$form->{"a_description"}|
  1385. );
  1386. for (
  1387. qw(
  1388. taxrates number sku runningnumber ship
  1389. serialnumber requiredate projectnumber
  1390. sellprice listprice netprice discount
  1391. discountrate linetotal weight itemnotes)
  1392. )
  1393. {
  1394. push( @{ $form->{$_} }, "" );
  1395. }
  1396. }
  1397. else {
  1398. push( @{ $form->{description} }, qq|$form->{"a_description"}| );
  1399. push( @{ $form->{sku} }, $form->{"a_partnumber"} );
  1400. push( @{ $form->{number} }, $form->{"a_partnumber"} );
  1401. for (
  1402. qw(
  1403. taxrates runningnumber ship serialnumber
  1404. requiredate projectnumber sellprice listprice
  1405. netprice discount discountrate linetotal weight
  1406. itemnotes)
  1407. )
  1408. {
  1409. push( @{ $form->{$_} }, "" );
  1410. }
  1411. }
  1412. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  1413. push(
  1414. @{ $form->{qty} },
  1415. $form->format_amount( $myconfig, $ref->{qty} * $qty )
  1416. );
  1417. for (qw(unit bin)) {
  1418. $form->{"a_$_"} = $ref->{$_};
  1419. $form->format_string("a_$_");
  1420. push( @{ $form->{$_} }, $form->{"a_$_"} );
  1421. }
  1422. if ( $ref->{assembly} && $form->{formname} eq 'work_order' ) {
  1423. &assembly_details( $myconfig, $form, $dbh, $ref->{id}, $oid,
  1424. $ref->{qty} * $qty );
  1425. }
  1426. }
  1427. $sth->finish;
  1428. $form->{stagger}--;
  1429. }
  1430. sub project_description {
  1431. my ( $self, $dbh, $id ) = @_;
  1432. my $query = qq|
  1433. SELECT description
  1434. FROM project
  1435. WHERE id = $id|;
  1436. ($_) = $dbh->selectrow_array($query);
  1437. $_;
  1438. }
  1439. sub get_warehouses {
  1440. my ( $self, $myconfig, $form ) = @_;
  1441. my $dbh = $form->{dbh};
  1442. # setup warehouses
  1443. my $query = qq|
  1444. SELECT id, description
  1445. FROM warehouse
  1446. ORDER BY 2|;
  1447. my $sth = $dbh->prepare($query);
  1448. $sth->execute || $form->dberror($query);
  1449. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1450. push @{ $form->{all_warehouse} }, $ref;
  1451. }
  1452. $sth->finish;
  1453. }
  1454. sub save_inventory {
  1455. my ( $self, $myconfig, $form ) = @_;
  1456. my ( $null, $warehouse_id ) = split /--/, $form->{warehouse};
  1457. $warehouse_id *= 1;
  1458. my $ml = ( $form->{type} eq 'ship_order' ) ? -1 : 1;
  1459. my $dbh = $form->{dbh};
  1460. my $sth;
  1461. my $wth;
  1462. my $serialnumber;
  1463. my $ship;
  1464. my ( $null, $employee_id ) = split /--/, $form->{employee};
  1465. ( $null, $employee_id ) = $form->get_employee($dbh) if !$employee_id;
  1466. $query = qq|
  1467. SELECT serialnumber, ship
  1468. FROM orderitems
  1469. WHERE trans_id = ?
  1470. AND id = ?
  1471. FOR UPDATE|;
  1472. $sth = $dbh->prepare($query) || $form->dberror($query);
  1473. $query = qq|
  1474. SELECT sum(qty)
  1475. FROM inventory
  1476. WHERE parts_id = ?
  1477. AND warehouse_id = ?|;
  1478. $wth = $dbh->prepare($query) || $form->dberror($query);
  1479. for my $i ( 1 .. $form->{rowcount} ) {
  1480. $ship =
  1481. ( abs( $form->{"ship_$i"} ) > abs( $form->{"qty_$i"} ) )
  1482. ? $form->{"qty_$i"}
  1483. : $form->{"ship_$i"};
  1484. if ( $warehouse_id && $form->{type} eq 'ship_order' ) {
  1485. $wth->execute( $form->{"id_$i"}, $warehouse_id )
  1486. || $form->dberror;
  1487. @qtylist = $wth->fetchrow_array;
  1488. $form->db_parse_numeric(sth=>$wth, arrayref=>\@qtylist);
  1489. ($qty) = @qtylist;
  1490. $wth->finish;
  1491. if ( $ship > $qty ) {
  1492. $ship = $qty;
  1493. }
  1494. }
  1495. if ($ship) {
  1496. if ( !$form->{shippingdate} ) {
  1497. $form->{shippingdate} = undef;
  1498. }
  1499. $ship *= $ml;
  1500. $query = qq|
  1501. INSERT INTO inventory
  1502. (parts_id, warehouse_id, qty, trans_id,
  1503. orderitems_id, shippingdate,
  1504. employee_id)
  1505. VALUES
  1506. (?, ?, ?, ?, ?, ?, ?)|;
  1507. $sth2 = $dbh->prepare($query);
  1508. $sth2->execute( $form->{"id_$i"}, $warehouse_id, $ship,
  1509. $form->{"id"}, $form->{"orderitems_id_$i"},
  1510. $form->{shippingdate}, $employee_id )
  1511. || $form->dberror($query);
  1512. $sth2->finish;
  1513. # add serialnumber, ship to orderitems
  1514. $sth->execute( $form->{id}, $form->{"orderitems_id_$i"} )
  1515. || $form->dberror;
  1516. ( $serialnumber, $ship ) = $sth->fetchrow_array;
  1517. $sth->finish;
  1518. $serialnumber .= " " if $serialnumber;
  1519. $serialnumber .= qq|$form->{"serialnumber_$i"}|;
  1520. $ship += $form->{"ship_$i"};
  1521. $query = qq|
  1522. UPDATE orderitems SET
  1523. serialnumber = ?,
  1524. ship = ?,
  1525. reqdate = ?
  1526. WHERE trans_id = ?
  1527. AND id = ?|;
  1528. $sth2 = $dbh->prepare($query);
  1529. $sth2->execute( $serialnumber, $ship, $form->{shippingdate},
  1530. $form->{id}, $form->{"orderitems_id_$i"} )
  1531. || $form->dberror($query);
  1532. $sth2->finish;
  1533. # update order with ship via
  1534. $query = qq|
  1535. UPDATE oe SET
  1536. shippingpoint = ?,
  1537. shipvia = ?
  1538. WHERE id = ?|;
  1539. $sth2 = $dbh->prepare($query);
  1540. $sth2->execute( $form->{shippingpoint},
  1541. $form->{shipvia}, $form->{id} )
  1542. || $form->dberror($query);
  1543. $sth2->finish;
  1544. # update onhand for parts
  1545. $form->update_balance(
  1546. $dbh, "parts", "onhand",
  1547. qq|id = $form->{"id_$i"}|,
  1548. $form->{"ship_$i"} * $ml
  1549. );
  1550. }
  1551. }
  1552. my $rc = $dbh->commit;
  1553. $rc;
  1554. }
  1555. sub adj_onhand {
  1556. my ( $dbh, $form, $ml ) = @_;
  1557. my $query = qq|
  1558. SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
  1559. FROM orderitems oi
  1560. JOIN parts p ON (p.id = oi.parts_id)
  1561. WHERE oi.trans_id = ?|;
  1562. my $sth = $dbh->prepare($query);
  1563. $sth->execute( $form->{id} ) || $form->dberror($query);
  1564. $query = qq|
  1565. SELECT sum(p.inventory_accno_id), p.assembly
  1566. FROM parts p
  1567. JOIN assembly a ON (a.parts_id = p.id)
  1568. WHERE a.id = ?
  1569. GROUP BY p.assembly|;
  1570. my $ath = $dbh->prepare($query) || $form->dberror($query);
  1571. my $ref;
  1572. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1573. if ( $ref->{inventory_accno_id} || $ref->{assembly} ) {
  1574. # do not update if assembly consists of all services
  1575. if ( $ref->{assembly} ) {
  1576. $ath->execute( $ref->{parts_id} )
  1577. || $form->dberror($query);
  1578. my ( $inv, $assembly ) = $ath->fetchrow_array;
  1579. $ath->finish;
  1580. next unless ( $inv || $assembly );
  1581. }
  1582. # adjust onhand in parts table
  1583. $form->update_balance(
  1584. $dbh, "parts", "onhand",
  1585. qq|id = $ref->{parts_id}|,
  1586. $ref->{ship} * $ml
  1587. );
  1588. }
  1589. }
  1590. $sth->finish;
  1591. }
  1592. sub adj_inventory {
  1593. my ( $dbh, $myconfig, $form ) = @_;
  1594. # increase/reduce qty in inventory table
  1595. my $query = qq|
  1596. SELECT oi.id, oi.parts_id, oi.ship
  1597. FROM orderitems oi
  1598. WHERE oi.trans_id = ?|;
  1599. my $sth = $dbh->prepare($query);
  1600. $sth->execute( $form->{id} ) || $form->dberror($query);
  1601. my $id = $dbh->quote( $form->{id} );
  1602. $query = qq|
  1603. SELECT qty,
  1604. (SELECT SUM(qty) FROM inventory
  1605. WHERE trans_id = $id
  1606. AND orderitems_id = ?) AS total
  1607. FROM inventory
  1608. WHERE trans_id = $id
  1609. AND orderitems_id = ?|;
  1610. my $ith = $dbh->prepare($query) || $form->dberror($query);
  1611. my $qty;
  1612. my $ml = ( $form->{type} =~ /(ship|sales)_order/ ) ? -1 : 1;
  1613. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1614. $ith->execute( $ref->{id}, $ref->{id} ) || $form->dberror($query);
  1615. my $ship = $ref->{ship};
  1616. while ( my $inv = $ith->fetchrow_hashref(NAME_lc) ) {
  1617. if ( ( $qty = ( ( $inv->{total} * $ml ) - $ship ) ) >= 0 ) {
  1618. $qty = $inv->{qty} * $ml
  1619. if ( $qty > ( $inv->{qty} * $ml ) );
  1620. $form->update_balance(
  1621. $dbh, "inventory", "qty",
  1622. qq|$oid{$myconfig->{dbdriver}} | . qq|= $inv->{oid}|,
  1623. $qty * -1 * $ml
  1624. );
  1625. $ship -= $qty;
  1626. }
  1627. }
  1628. $ith->finish;
  1629. }
  1630. $sth->finish;
  1631. # delete inventory entries if qty = 0
  1632. $query = qq|
  1633. DELETE FROM inventory
  1634. WHERE trans_id = ?
  1635. AND qty = 0|;
  1636. $sth = $dbh->prepare($query);
  1637. $sth->execute( $form->{id} ) || $form->dberror($query);
  1638. }
  1639. sub get_inventory {
  1640. my ( $self, $myconfig, $form ) = @_;
  1641. my $where;
  1642. my $query;
  1643. my $null;
  1644. my $fromwarehouse_id;
  1645. my $towarehouse_id;
  1646. my $var;
  1647. my $dbh = $form->{dbh};
  1648. if ( $form->{partnumber} ne "" ) {
  1649. $var = $dbh->quote( $form->like( lc $form->{partnumber} ) );
  1650. $where .= "
  1651. AND lower(p.partnumber) LIKE $var";
  1652. }
  1653. if ( $form->{description} ne "" ) {
  1654. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  1655. $where .= "
  1656. AND lower(p.description) LIKE $var";
  1657. }
  1658. if ( $form->{partsgroup} ne "" ) {
  1659. ( $null, $var ) = split /--/, $form->{partsgroup};
  1660. $var = $dbh->quote($var);
  1661. $where .= "
  1662. AND pg.id = $var";
  1663. }
  1664. ( $null, $fromwarehouse_id ) = split /--/, $form->{fromwarehouse};
  1665. $fromwarehouse_id = $dbh->quote($fromwarehouse_id);
  1666. ( $null, $towarehouse_id ) = split /--/, $form->{towarehouse};
  1667. $towarehouse_id = $dbh->quote($towarehouse_id);
  1668. my %ordinal = (
  1669. partnumber => 2,
  1670. description => 3,
  1671. partsgroup => 5,
  1672. warehouse => 6,
  1673. );
  1674. my @a = ( partnumber, warehouse );
  1675. my $sortorder = $form->sort_order( \@a, \%ordinal );
  1676. if ($fromwarehouse_id) {
  1677. if ($towarehouse_id) {
  1678. $where .= "
  1679. AND NOT i.warehouse_id = $towarehouse_id";
  1680. }
  1681. $query = qq|
  1682. SELECT p.id, p.partnumber, p.description,
  1683. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1684. pg.partsgroup, w.description AS warehouse,
  1685. i.warehouse_id
  1686. FROM inventory i
  1687. JOIN parts p ON (p.id = i.parts_id)
  1688. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1689. JOIN warehouse w ON (w.id = i.warehouse_id)
  1690. WHERE i.warehouse_id = $fromwarehouse_id
  1691. $where
  1692. GROUP BY p.id, p.partnumber, p.description,
  1693. pg.partsgroup, w.description, i.warehouse_id
  1694. ORDER BY $sortorder|;
  1695. }
  1696. else {
  1697. if ($towarehouse_id) {
  1698. $query = qq|
  1699. SELECT p.id, p.partnumber, p.description,
  1700. p.onhand,
  1701. (SELECT SUM(qty)
  1702. FROM inventory i
  1703. WHERE i.parts_id = p.id) AS qty,
  1704. pg.partsgroup, '' AS warehouse,
  1705. 0 AS warehouse_id
  1706. FROM parts p
  1707. LEFT JOIN partsgroup pg
  1708. ON (p.partsgroup_id = pg.id)
  1709. WHERE p.onhand > 0
  1710. $where
  1711. UNION|;
  1712. }
  1713. $query .= qq|
  1714. SELECT p.id, p.partnumber, p.description,
  1715. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1716. pg.partsgroup, w.description AS warehouse,
  1717. i.warehouse_id
  1718. FROM inventory i
  1719. JOIN parts p ON (p.id = i.parts_id)
  1720. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1721. JOIN warehouse w ON (w.id = i.warehouse_id)
  1722. WHERE i.warehouse_id != $towarehouse_id
  1723. $where
  1724. GROUP BY p.id, p.partnumber, p.description,
  1725. pg.partsgroup, w.description, i.warehouse_id
  1726. ORDER BY $sortorder|;
  1727. }
  1728. my $sth = $dbh->prepare($query);
  1729. $sth->execute || $form->dberror($query);
  1730. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1731. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1732. $ref->{qty} = $ref->{onhand} - $ref->{qty};
  1733. push @{ $form->{all_inventory} }, $ref if $ref->{qty} > 0;
  1734. }
  1735. $sth->finish;
  1736. $dbh->commit;
  1737. }
  1738. sub transfer {
  1739. my ( $self, $myconfig, $form ) = @_;
  1740. my $dbh = $form->{dbh};
  1741. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  1742. my @a = localtime;
  1743. $a[5] += 1900;
  1744. $a[4]++;
  1745. $a[4] = substr( "0$a[4]", -2 );
  1746. $a[3] = substr( "0$a[3]", -2 );
  1747. $shippingdate = "$a[5]$a[4]$a[3]";
  1748. my %total = ();
  1749. my $query = qq|
  1750. INSERT INTO inventory
  1751. (warehouse_id, parts_id, qty, shippingdate, employee_id)
  1752. VALUES (?, ?, ?, ?, ?)|;
  1753. $sth = $dbh->prepare($query) || $form->dberror($query);
  1754. my $qty;
  1755. for my $i ( 1 .. $form->{rowcount} ) {
  1756. $qty = $form->parse_amount( $myconfig, $form->{"transfer_$i"} );
  1757. $qty = $form->{"qty_$i"} if ( $qty > $form->{"qty_$i"} );
  1758. if ( $qty > 0 ) {
  1759. # to warehouse
  1760. if ( $form->{warehouse_id} ) {
  1761. $sth->execute( $form->{warehouse_id}, $form->{"id_$i"}, $qty,
  1762. $shippingdate, $form->{employee_id} )
  1763. || $form->dberror;
  1764. $sth->finish;
  1765. }
  1766. # from warehouse
  1767. if ( $form->{"warehouse_id_$i"} ) {
  1768. $sth->execute( $form->{"warehouse_id_$i"},
  1769. $form->{"id_$i"}, $qty * -1, $shippingdate,
  1770. $form->{employee_id})
  1771. || $form->dberror;
  1772. $sth->finish;
  1773. }
  1774. }
  1775. }
  1776. my $rc = $dbh->commit;
  1777. $rc;
  1778. }
  1779. sub get_soparts {
  1780. my ( $self, $myconfig, $form ) = @_;
  1781. # connect to database
  1782. my $dbh = $form->{dbh};
  1783. my $id;
  1784. my $ref;
  1785. # store required items from selected sales orders
  1786. my $query = qq|
  1787. SELECT p.id, oi.qty - oi.ship AS required, p.assembly
  1788. FROM orderitems oi
  1789. JOIN parts p ON (p.id = oi.parts_id)
  1790. WHERE oi.trans_id = ?|;
  1791. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1792. for ( my $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1793. if ( $form->{"ndx_$i"} ) {
  1794. $sth->execute( $form->{"ndx_$i"} );
  1795. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1796. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1797. &add_items_required( "", $dbh, $form, $ref->{id},
  1798. $ref->{required}, $ref->{assembly} );
  1799. }
  1800. $sth->finish;
  1801. }
  1802. }
  1803. $query = qq|SELECT current_date|;
  1804. ( $form->{transdate} ) = $dbh->selectrow_array($query);
  1805. # foreign exchange rates
  1806. &exchangerate_defaults( $dbh, $form );
  1807. $dbh->commit;
  1808. }
  1809. sub add_items_required {
  1810. my ( $self, $dbh, $form, $parts_id, $required, $assembly ) = @_;
  1811. my $query;
  1812. my $sth;
  1813. my $ref;
  1814. if ($assembly) {
  1815. $query = qq|
  1816. SELECT p.id, a.qty, p.assembly
  1817. FROM assembly a
  1818. JOIN parts p ON (p.id = a.parts_id)
  1819. WHERE a.id = ?|;
  1820. $sth = $dbh->prepare($query);
  1821. $sth->execute($parts_id) || $form->dberror($query);
  1822. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  1823. $form->db_parse_numeric(sth=> $sth, hashref=> $ref);
  1824. &add_items_required( "", $dbh, $form, $ref->{id},
  1825. $required * $ref->{qty},
  1826. $ref->{assembly} );
  1827. }
  1828. $sth->finish;
  1829. }
  1830. else {
  1831. $query = qq|
  1832. SELECT partnumber, description, lastcost
  1833. FROM parts
  1834. WHERE id = ?|;
  1835. $sth = $dbh->prepare($query);
  1836. $sth->execute($parts_id) || $form->dberror($query);
  1837. $ref = $sth->fetchrow_hashref(NAME_lc);
  1838. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1839. for ( keys %$ref ) {
  1840. $form->{orderitems}{$parts_id}{$_} = $ref->{$_};
  1841. }
  1842. $sth->finish;
  1843. $form->{orderitems}{$parts_id}{required} += $required;
  1844. $query = qq|
  1845. SELECT pv.partnumber, pv.leadtime, pv.lastcost, pv.curr,
  1846. pv.vendor_id, v.name
  1847. FROM partsvendor pv
  1848. JOIN vendor v ON (v.id = pv.vendor_id)
  1849. WHERE pv.parts_id = ?|;
  1850. $sth = $dbh->prepare($query) || $form->dberror($query);
  1851. # get cost and vendor
  1852. $sth->execute($parts_id);
  1853. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1854. for ( keys %$ref ) {
  1855. $form->{orderitems}{$parts_id}{partsvendor}{ $ref->{vendor_id} }
  1856. {$_} = $ref->{$_};
  1857. }
  1858. }
  1859. $sth->finish;
  1860. }
  1861. }
  1862. sub generate_orders {
  1863. my ( $self, $myconfig, $form ) = @_;
  1864. my $i;
  1865. my %a;
  1866. my $query;
  1867. my $sth;
  1868. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1869. for (qw(qty lastcost)) {
  1870. $form->{"${_}_$i"} =
  1871. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  1872. }
  1873. if ( $form->{"qty_$i"} ) {
  1874. ( $vendor, $vendor_id ) =
  1875. split /--/, $form->{"vendor_$i"};
  1876. if ($vendor_id) {
  1877. $a{$vendor_id}{ $form->{"id_$i"} }{qty} += $form->{"qty_$i"};
  1878. for (qw(curr lastcost)) {
  1879. $a{$vendor_id}{ $form->{"id_$i"} }{$_} = $form->{"${_}_$i"};
  1880. }
  1881. }
  1882. }
  1883. }
  1884. # connect to database
  1885. my $dbh = $form->{dbh};
  1886. # foreign exchange rates
  1887. &exchangerate_defaults( $dbh, $form );
  1888. my $amount;
  1889. my $netamount;
  1890. my $curr = "";
  1891. my %tax;
  1892. my $taxincluded = 0;
  1893. my $vendor_id;
  1894. my $description;
  1895. my $unit;
  1896. my $sellprice;
  1897. foreach $vendor_id ( keys %a ) {
  1898. %tax = ();
  1899. my $ordnumber = $form->update_defaults( $myconfig, 'ponumber' );
  1900. $query = qq|
  1901. SELECT v.curr, v.taxincluded, t.rate, c.accno
  1902. FROM vendor v
  1903. LEFT JOIN vendortax vt ON (v.id = vt.vendor_id)
  1904. LEFT JOIN tax t ON (t.chart_id = vt.chart_id)
  1905. LEFT JOIN chart c ON (c.id = t.chart_id)
  1906. WHERE v.id = ?|;
  1907. $sth = $dbh->prepare($query);
  1908. $sth->execute($vendor_id) || $form->dberror($query);
  1909. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1910. $form->db_parse_numeric(sth=>$sth, hashref=> $ref);
  1911. $curr = $ref->{curr};
  1912. $taxincluded = $ref->{taxincluded};
  1913. $tax{ $ref->{accno} } = $ref->{rate};
  1914. }
  1915. $sth->finish;
  1916. $curr ||= $form->{defaultcurrency};
  1917. $taxincluded *= 1;
  1918. my $uid = localtime;
  1919. $uid .= "$$";
  1920. # TODO: Make this function insert as much as possible
  1921. $query = qq|
  1922. INSERT INTO oe (ordnumber)
  1923. VALUES ('$uid')|;
  1924. $dbh->do($query) || $form->dberror($query);
  1925. $query = qq|SELECT id FROM oe WHERE ordnumber = '$uid'|;
  1926. $sth = $dbh->prepare($query);
  1927. $sth->execute || $form->dberror($query);
  1928. my ($id) = $sth->fetchrow_array;
  1929. $sth->finish;
  1930. $amount = 0;
  1931. $netamount = 0;
  1932. foreach my $parts_id ( keys %{ $a{$vendor_id} } ) {
  1933. if ( ( $form->{$curr} * $form->{ $a{$vendor_id}{$parts_id}{curr} } )
  1934. > 0 )
  1935. {
  1936. $sellprice =
  1937. $a{$vendor_id}{$parts_id}{lastcost} / $form->{$curr} *
  1938. $form->{ $a{$vendor_id}{$parts_id}{curr} };
  1939. }
  1940. else {
  1941. $sellprice = $a{$vendor_id}{$parts_id}{lastcost};
  1942. }
  1943. $sellprice = $form->round_amount( $sellprice, 2 );
  1944. my $linetotal =
  1945. $form->round_amount( $sellprice * $a{$vendor_id}{$parts_id}{qty},
  1946. 2 );
  1947. $query = qq|
  1948. SELECT p.description, p.unit, c.accno
  1949. FROM parts p
  1950. LEFT JOIN partstax pt ON (p.id = pt.parts_id)
  1951. LEFT JOIN chart c ON (c.id = pt.chart_id)
  1952. WHERE p.id = ?|;
  1953. $sth = $dbh->prepare($query);
  1954. $sth->execute($parts_id) || $form->dberror($query);
  1955. my $rate = 0;
  1956. my $taxes = '';
  1957. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1958. $description = $ref->{description};
  1959. $unit = $ref->{unit};
  1960. $rate += $tax{ $ref->{accno} };
  1961. $taxes .= "$ref->{accno} ";
  1962. }
  1963. $sth->finish;
  1964. chop $taxes;
  1965. my @taxaccounts = Tax::init_taxes( $form, $taxes );
  1966. $netamount += $linetotal;
  1967. if ($taxincluded) {
  1968. $amount += $linetotal;
  1969. }
  1970. else {
  1971. $amount +=
  1972. $form->round_amount(
  1973. Tax::apply_taxes( \@taxaccounts, $form, $linetotal ), 2 );
  1974. }
  1975. $query = qq|
  1976. INSERT INTO orderitems
  1977. (trans_id, parts_id, description,
  1978. qty, ship, sellprice, unit)
  1979. VALUES
  1980. (?, ?, ?, ?, 0, ?, ?)|;
  1981. $sth = $dbh->prepare($query);
  1982. $sth->execute( $id, $parts_id, $description,
  1983. $a{$vendor_id}{$parts_id}{qty},
  1984. $sellprice, $unit )
  1985. || $form->dberror($query);
  1986. }
  1987. my $null;
  1988. my $employee_id;
  1989. my $department_id;
  1990. ( $null, $employee_id ) = $form->get_employee($dbh);
  1991. ( $null, $department_id ) = split /--/, $form->{department};
  1992. $department_id *= 1;
  1993. $query = qq|
  1994. UPDATE oe SET
  1995. ordnumber = ?,
  1996. transdate = current_date,
  1997. vendor_id = ?,
  1998. customer_id = 0,
  1999. amount = ?,
  2000. netamount = ?,
  2001. taxincluded = ?,
  2002. curr = ?,
  2003. employee_id = ?,
  2004. department_id = ?,
  2005. ponumber = ?
  2006. WHERE id = ?|;
  2007. $sth = $dbh->prepare($query);
  2008. $sth->execute(
  2009. $ordnumber, $vendor_id, $amount,
  2010. $netamount, $taxincluded, $curr,
  2011. $employee_id, $department_id, $form->{ponumber},
  2012. $id
  2013. ) || $form->dberror($query);
  2014. }
  2015. my $rc = $dbh->commit;
  2016. $rc;
  2017. }
  2018. sub consolidate_orders {
  2019. my ( $self, $myconfig, $form ) = @_;
  2020. # connect to database
  2021. my $dbh = $form->{dbh};
  2022. my $i;
  2023. my $id;
  2024. my $ref;
  2025. my %oe = ();
  2026. my $query = qq|SELECT * FROM oe WHERE id = ?|;
  2027. my $sth = $dbh->prepare($query) || $form->dberror($query);
  2028. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  2029. # retrieve order
  2030. if ( $form->{"ndx_$i"} ) {
  2031. $sth->execute( $form->{"ndx_$i"} );
  2032. $ref = $sth->fetchrow_hashref(NAME_lc);
  2033. $ref->{ndx} = $i;
  2034. $oe{oe}{ $ref->{curr} }{ $ref->{id} } = $ref;
  2035. $oe{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} }++;
  2036. $sth->finish;
  2037. }
  2038. }
  2039. $query = qq|SELECT * FROM orderitems WHERE trans_id = ?|;
  2040. $sth = $dbh->prepare($query) || $form->dberror($query);
  2041. foreach $curr ( keys %{ $oe{oe} } ) {
  2042. foreach $id (
  2043. sort { $oe{oe}{$curr}{$a}->{ndx} <=> $oe{oe}{$curr}{$b}->{ndx} }
  2044. keys %{ $oe{oe}{$curr} }
  2045. )
  2046. {
  2047. # retrieve order
  2048. $vc_id = $oe{oe}{$curr}{$id}->{"$form->{vc}_id"};
  2049. if ( $oe{vc}{ $oe{oe}{$curr}{$id}->{curr} }{$vc_id} > 1 ) {
  2050. push @{ $oe{orders}{$curr}{$vc_id} }, $id;
  2051. $sth->execute($id);
  2052. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  2053. push @{ $oe{orderitems}{$curr}{$id} }, $ref;
  2054. }
  2055. $sth->finish;
  2056. }
  2057. }
  2058. }
  2059. my $ordnumber = $form->{ordnumber};
  2060. my $numberfld = ( $form->{vc} eq 'customer' ) ? 'sonumber' : 'ponumber';
  2061. my ( $department, $department_id ) = $form->{department};
  2062. $department_id *= 1;
  2063. my $uid = localtime;
  2064. $uid .= "$$";
  2065. my @orderitems = ();
  2066. foreach $curr ( keys %{ $oe{orders} } ) {
  2067. foreach $vc_id ( sort { $a <=> $b } keys %{ $oe{orders}{$curr} } ) {
  2068. # the orders
  2069. @orderitems = ();
  2070. $form->{customer_id} = $form->{vendor_id} = 0;
  2071. $form->{"$form->{vc}_id"} = $vc_id;
  2072. $amount = 0;
  2073. $netamount = 0;
  2074. $ordnumber =
  2075. $form->update_defaults( $myconfig, $numberfld, $dbh );
  2076. my @orderids;
  2077. my $orderid_str = "";
  2078. foreach $id ( @{ $oe{orders}{$curr}{$vc_id} } ) {
  2079. push(@orderids, $id);
  2080. $orderid_str .= "?, ";
  2081. # header
  2082. $ref = $oe{oe}{$curr}{$id};
  2083. $amount += $ref->{amount};
  2084. $netamount += $ref->{netamount};
  2085. $id = $dbh->quote($id);
  2086. foreach $item ( @{ $oe{orderitems}{$curr}{$id} } ) {
  2087. push @orderitems, $item;
  2088. }
  2089. # close order
  2090. $query = qq|
  2091. UPDATE oe SET
  2092. closed = '1'
  2093. WHERE id = $id|;
  2094. $dbh->do($query) || $form->dberror($query);
  2095. # reset shipped
  2096. $query = qq|
  2097. UPDATE orderitems SET
  2098. ship = 0
  2099. WHERE trans_id = $id|;
  2100. $dbh->do($query) || $form->dberror($query);
  2101. }
  2102. #fixme: Change this
  2103. $query = qq|
  2104. INSERT INTO oe (ordnumber) VALUES ('$uid')|;
  2105. $dbh->do($query) || $form->dberror($query);
  2106. $query = qq|
  2107. SELECT id
  2108. FROM oe
  2109. WHERE ordnumber = '$uid'|;
  2110. ($id) = $dbh->selectrow_array($query);
  2111. $ref->{employee_id} *= 1;
  2112. $query = qq|
  2113. UPDATE oe SET
  2114. ordnumber = | . $dbh->quote($ordnumber) . qq|,
  2115. transdate = current_date,
  2116. vendor_id = $form->{vendor_id},
  2117. customer_id = $form->{customer_id},
  2118. amount = $amount,
  2119. netamount = $netamount,
  2120. reqdate = | . $form->dbquote( $ref->{reqdate}, SQL_DATE ) . qq|,
  2121. taxincluded = '$ref->{taxincluded}',
  2122. shippingpoint = | . $dbh->quote( $ref->{shippingpoint} ) . qq|,
  2123. notes = | . $dbh->quote( $ref->{notes} ) . qq|,
  2124. curr = '$curr',
  2125. employee_id = $ref->{employee_id},
  2126. intnotes = | . $dbh->quote( $ref->{intnotes} ) . qq|,
  2127. shipvia = | . $dbh->quote( $ref->{shipvia} ) . qq|,
  2128. language_code = '$ref->{language_code}',
  2129. ponumber = | . $dbh->quote( $form->{ponumber} ) . qq|,
  2130. department_id = $department_id
  2131. WHERE id = $id|;
  2132. $sth = $dbh->prepare($query);
  2133. $sth->execute() || $form->dberror($query);
  2134. $orderid_str =~ s/, $//;
  2135. # add items
  2136. $query = qq|
  2137. INSERT INTO orderitems
  2138. (trans_id, parts_id, description,
  2139. qty, sellprice, discount, unit, reqdate,
  2140. project_id, ship, serialnumber, notes)
  2141. SELECT ?, parts_id, description, qty,
  2142. sellprice, discount, unit, reqdate,
  2143. project_id, 0, description, qty
  2144. FROM orderitems
  2145. WHERE trans_id IN ($orderid_str)|;
  2146. $sth = $dbh->prepare($query);
  2147. $sth->execute($id, @orderids) || $form->dberror($query);
  2148. }
  2149. }
  2150. $rc = $dbh->commit;
  2151. $rc;
  2152. }
  2153. 1;