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