summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: 5c253f5061a138810b4930c33a5a5cadf4e5914d (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 ?";
  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 employee 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. $ship =
  1464. ( abs( $form->{"ship_$i"} ) > abs( $form->{"qty_$i"} ) )
  1465. ? $form->{"qty_$i"}
  1466. : $form->{"ship_$i"};
  1467. if ( $warehouse_id && $form->{type} eq 'ship_order' ) {
  1468. $wth->execute( $form->{"id_$i"}, $warehouse_id )
  1469. || $form->dberror;
  1470. ($qty) = $wth->fetchrow_array;
  1471. $wth->finish;
  1472. if ( $ship > $qty ) {
  1473. $ship = $qty;
  1474. }
  1475. }
  1476. if ($ship) {
  1477. if ( !$form->{shippingdate} ) {
  1478. $form->{shippingdate} = undef;
  1479. }
  1480. $ship *= $ml;
  1481. $query = qq|
  1482. INSERT INTO inventory
  1483. (parts_id, warehouse_id, qty, trans_id,
  1484. orderitems_id, shippingdate,
  1485. employee_id)
  1486. VALUES
  1487. (?, ?, ?, ?, ?, ?, ?)|;
  1488. $sth2 = $dbh->prepare($query);
  1489. $sth2->execute( $form->{"id_$i"}, $warehouse_id, $ship,
  1490. $form->{"id"}, $form->{"orderitems_id_$i"},
  1491. $form->{shippingdate}, $employee_id )
  1492. || $form->dberror($query);
  1493. $sth2->finish;
  1494. # add serialnumber, ship to orderitems
  1495. $sth->execute( $form->{id}, $form->{"orderitems_id_$i"} )
  1496. || $form->dberror;
  1497. ( $serialnumber, $ship ) = $sth->fetchrow_array;
  1498. $sth->finish;
  1499. $serialnumber .= " " if $serialnumber;
  1500. $serialnumber .= qq|$form->{"serialnumber_$i"}|;
  1501. $ship += $form->{"ship_$i"};
  1502. $query = qq|
  1503. UPDATE orderitems SET
  1504. serialnumber = ?,
  1505. ship = ?,
  1506. reqdate = ?
  1507. WHERE trans_id = ?
  1508. AND id = ?|;
  1509. $sth2 = $dbh->prepare($query);
  1510. $sth2->execute( $serialnumber, $ship, $form->{shippingdate},
  1511. $form->{id}, $form->{"orderitems_id_$i"} )
  1512. || $form->dberror($query);
  1513. $sth2->finish;
  1514. # update order with ship via
  1515. $query = qq|
  1516. UPDATE oe SET
  1517. shippingpoint = ?,
  1518. shipvia = ?
  1519. WHERE id = ?|;
  1520. $sth2 = $dbh->prepare($query);
  1521. $sth2->execute( $form->{shippingpoint},
  1522. $form->{shipvia}, $form->{id} )
  1523. || $form->dberror($query);
  1524. $sth2->finish;
  1525. # update onhand for parts
  1526. $form->update_balance(
  1527. $dbh, "parts", "onhand",
  1528. qq|id = $form->{"id_$i"}|,
  1529. $form->{"ship_$i"} * $ml
  1530. );
  1531. }
  1532. }
  1533. my $rc = $dbh->commit;
  1534. $rc;
  1535. }
  1536. sub adj_onhand {
  1537. my ( $dbh, $form, $ml ) = @_;
  1538. my $query = qq|
  1539. SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
  1540. FROM orderitems oi
  1541. JOIN parts p ON (p.id = oi.parts_id)
  1542. WHERE oi.trans_id = ?|;
  1543. my $sth = $dbh->prepare($query);
  1544. $sth->execute( $form->{id} ) || $form->dberror($query);
  1545. $query = qq|
  1546. SELECT sum(p.inventory_accno_id), p.assembly
  1547. FROM parts p
  1548. JOIN assembly a ON (a.parts_id = p.id)
  1549. WHERE a.id = ?
  1550. GROUP BY p.assembly|;
  1551. my $ath = $dbh->prepare($query) || $form->dberror($query);
  1552. my $ref;
  1553. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1554. if ( $ref->{inventory_accno_id} || $ref->{assembly} ) {
  1555. # do not update if assembly consists of all services
  1556. if ( $ref->{assembly} ) {
  1557. $ath->execute( $ref->{parts_id} )
  1558. || $form->dberror($query);
  1559. my ( $inv, $assembly ) = $ath->fetchrow_array;
  1560. $ath->finish;
  1561. next unless ( $inv || $assembly );
  1562. }
  1563. # adjust onhand in parts table
  1564. $form->update_balance(
  1565. $dbh, "parts", "onhand",
  1566. qq|id = $ref->{parts_id}|,
  1567. $ref->{ship} * $ml
  1568. );
  1569. }
  1570. }
  1571. $sth->finish;
  1572. }
  1573. sub adj_inventory {
  1574. my ( $dbh, $myconfig, $form ) = @_;
  1575. # increase/reduce qty in inventory table
  1576. my $query = qq|
  1577. SELECT oi.id, oi.parts_id, oi.ship
  1578. FROM orderitems oi
  1579. WHERE oi.trans_id = ?|;
  1580. my $sth = $dbh->prepare($query);
  1581. $sth->execute( $form->{id} ) || $form->dberror($query);
  1582. my $id = $dbh->quote( $form->{id} );
  1583. $query = qq|
  1584. SELECT qty,
  1585. (SELECT SUM(qty) FROM inventory
  1586. WHERE trans_id = $id
  1587. AND orderitems_id = ?) AS total
  1588. FROM inventory
  1589. WHERE trans_id = $id
  1590. AND orderitems_id = ?|;
  1591. my $ith = $dbh->prepare($query) || $form->dberror($query);
  1592. my $qty;
  1593. my $ml = ( $form->{type} =~ /(ship|sales)_order/ ) ? -1 : 1;
  1594. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1595. $ith->execute( $ref->{id}, $ref->{id} ) || $form->dberror($query);
  1596. my $ship = $ref->{ship};
  1597. while ( my $inv = $ith->fetchrow_hashref(NAME_lc) ) {
  1598. if ( ( $qty = ( ( $inv->{total} * $ml ) - $ship ) ) >= 0 ) {
  1599. $qty = $inv->{qty} * $ml
  1600. if ( $qty > ( $inv->{qty} * $ml ) );
  1601. $form->update_balance(
  1602. $dbh, "inventory", "qty",
  1603. qq|$oid{$myconfig->{dbdriver}} | . qq|= $inv->{oid}|,
  1604. $qty * -1 * $ml
  1605. );
  1606. $ship -= $qty;
  1607. }
  1608. }
  1609. $ith->finish;
  1610. }
  1611. $sth->finish;
  1612. # delete inventory entries if qty = 0
  1613. $query = qq|
  1614. DELETE FROM inventory
  1615. WHERE trans_id = ?
  1616. AND qty = 0|;
  1617. $sth = $dbh->prepare($query);
  1618. $sth->execute( $form->{id} ) || $form->dberror($query);
  1619. }
  1620. sub get_inventory {
  1621. my ( $self, $myconfig, $form ) = @_;
  1622. my $where;
  1623. my $query;
  1624. my $null;
  1625. my $fromwarehouse_id;
  1626. my $towarehouse_id;
  1627. my $var;
  1628. my $dbh = $form->{dbh};
  1629. if ( $form->{partnumber} ne "" ) {
  1630. $var = $dbh->quote( $form->like( lc $form->{partnumber} ) );
  1631. $where .= "
  1632. AND lower(p.partnumber) LIKE '$var'";
  1633. }
  1634. if ( $form->{description} ne "" ) {
  1635. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  1636. $where .= "
  1637. AND lower(p.description) LIKE '$var'";
  1638. }
  1639. if ( $form->{partsgroup} ne "" ) {
  1640. ( $null, $var ) = split /--/, $form->{partsgroup};
  1641. $var = $dbh->quote($var);
  1642. $where .= "
  1643. AND pg.id = $var";
  1644. }
  1645. ( $null, $fromwarehouse_id ) = split /--/, $form->{fromwarehouse};
  1646. $fromwarehouse_id = $dbh->quote($fromwarehouse_id);
  1647. ( $null, $towarehouse_id ) = split /--/, $form->{towarehouse};
  1648. $towarehouse_id = $dbh->quote($towarehouse_id);
  1649. my %ordinal = (
  1650. partnumber => 2,
  1651. description => 3,
  1652. partsgroup => 5,
  1653. warehouse => 6,
  1654. );
  1655. my @a = ( partnumber, warehouse );
  1656. my $sortorder = $form->sort_order( \@a, \%ordinal );
  1657. if ($fromwarehouse_id) {
  1658. if ($towarehouse_id) {
  1659. $where .= "
  1660. AND NOT i.warehouse_id = $towarehouse_id";
  1661. }
  1662. $query = qq|
  1663. SELECT p.id, p.partnumber, p.description,
  1664. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1665. pg.partsgroup, w.description AS warehouse,
  1666. i.warehouse_id
  1667. FROM inventory i
  1668. JOIN parts p ON (p.id = i.parts_id)
  1669. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1670. JOIN warehouse w ON (w.id = i.warehouse_id)
  1671. WHERE i.warehouse_id = $fromwarehouse_id
  1672. $where
  1673. GROUP BY p.id, p.partnumber, p.description,
  1674. pg.partsgroup, w.description, i.warehouse_id
  1675. ORDER BY $sortorder|;
  1676. }
  1677. else {
  1678. if ($towarehouse_id) {
  1679. $query = qq|
  1680. SELECT p.id, p.partnumber, p.description,
  1681. p.onhand,
  1682. (SELECT SUM(qty)
  1683. FROM inventory i
  1684. WHERE i.parts_id = p.id) AS qty,
  1685. pg.partsgroup, '' AS warehouse,
  1686. 0 AS warehouse_id
  1687. FROM parts p
  1688. LEFT JOIN partsgroup pg
  1689. ON (p.partsgroup_id = pg.id)
  1690. WHERE p.onhand > 0
  1691. $where
  1692. UNION|;
  1693. }
  1694. $query .= qq|
  1695. SELECT p.id, p.partnumber, p.description,
  1696. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1697. pg.partsgroup, w.description AS warehouse,
  1698. i.warehouse_id
  1699. FROM inventory i
  1700. JOIN parts p ON (p.id = i.parts_id)
  1701. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1702. JOIN warehouse w ON (w.id = i.warehouse_id)
  1703. WHERE i.warehouse_id != $towarehouse_id
  1704. $where
  1705. GROUP BY p.id, p.partnumber, p.description,
  1706. pg.partsgroup, w.description, i.warehouse_id
  1707. ORDER BY $sortorder|;
  1708. }
  1709. my $sth = $dbh->prepare($query);
  1710. $sth->execute || $form->dberror($query);
  1711. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1712. $ref->{qty} = $ref->{onhand} - $ref->{qty};
  1713. push @{ $form->{all_inventory} }, $ref if $ref->{qty} > 0;
  1714. }
  1715. $sth->finish;
  1716. $dbh->commit;
  1717. }
  1718. sub transfer {
  1719. my ( $self, $myconfig, $form ) = @_;
  1720. my $dbh = $form->{dbh};
  1721. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  1722. my @a = localtime;
  1723. $a[5] += 1900;
  1724. $a[4]++;
  1725. $a[4] = substr( "0$a[4]", -2 );
  1726. $a[3] = substr( "0$a[3]", -2 );
  1727. $shippingdate = "$a[5]$a[4]$a[3]";
  1728. my %total = ();
  1729. my $query = qq|
  1730. INSERT INTO inventory
  1731. (warehouse_id, parts_id, qty, shippingdate, employee_id)
  1732. VALUES (?, ?, ?, '$shippingdate', $form->{employee_id})|;
  1733. $sth = $dbh->prepare($query) || $form->dberror($query);
  1734. my $qty;
  1735. for my $i ( 1 .. $form->{rowcount} ) {
  1736. $qty = $form->parse_amount( $myconfig, $form->{"transfer_$i"} );
  1737. $qty = $form->{"qty_$i"} if ( $qty > $form->{"qty_$i"} );
  1738. if ( $qty > 0 ) {
  1739. # to warehouse
  1740. if ( $form->{warehouse_id} ) {
  1741. $sth->execute( $form->{warehouse_id}, $form->{"id_$i"}, $qty,
  1742. $shippingdate, $form->{employee_id} )
  1743. || $form->dberror;
  1744. $sth->finish;
  1745. }
  1746. # from warehouse
  1747. if ( $form->{"warehouse_id_$i"} ) {
  1748. $sth->execute( $form->{"warehouse_id_$i"},
  1749. $form->{"id_$i"}, $qty * -1 )
  1750. || $form->dberror;
  1751. $sth->finish;
  1752. }
  1753. }
  1754. }
  1755. my $rc = $dbh->commit;
  1756. $rc;
  1757. }
  1758. sub get_soparts {
  1759. my ( $self, $myconfig, $form ) = @_;
  1760. # connect to database
  1761. my $dbh = $form->{dbh};
  1762. my $id;
  1763. my $ref;
  1764. # store required items from selected sales orders
  1765. my $query = qq|
  1766. SELECT p.id, oi.qty - oi.ship AS required, p.assembly
  1767. FROM orderitems oi
  1768. JOIN parts p ON (p.id = oi.parts_id)
  1769. WHERE oi.trans_id = ?|;
  1770. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1771. for ( my $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1772. if ( $form->{"ndx_$i"} ) {
  1773. $sth->execute( $form->{"ndx_$i"} );
  1774. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1775. &add_items_required( "", $dbh, $form, $ref->{id},
  1776. $ref->{required}, $ref->{assembly} );
  1777. }
  1778. $sth->finish;
  1779. }
  1780. }
  1781. $query = qq|SELECT current_date|;
  1782. ( $form->{transdate} ) = $dbh->selectrow_array($query);
  1783. # foreign exchange rates
  1784. &exchangerate_defaults( $dbh, $form );
  1785. $dbh->commit;
  1786. }
  1787. sub add_items_required {
  1788. my ( $self, $dbh, $form, $parts_id, $required, $assembly ) = @_;
  1789. my $query;
  1790. my $sth;
  1791. my $ref;
  1792. if ($assembly) {
  1793. $query = qq|
  1794. SELECT p.id, a.qty, p.assembly
  1795. FROM assembly a
  1796. JOIN parts p ON (p.id = a.parts_id)
  1797. WHERE a.id = ?|;
  1798. $sth = $dbh->prepare($query);
  1799. $sth->execute || $form->dberror($query);
  1800. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1801. &add_items_required( "", $dbh, $form, $ref->{id},
  1802. $required * $ref->{qty},
  1803. $ref->{assembly} );
  1804. }
  1805. $sth->finish;
  1806. }
  1807. else {
  1808. $query = qq|
  1809. SELECT partnumber, description, lastcost
  1810. FROM parts
  1811. WHERE id = ?|;
  1812. $sth = $dbh->prepare($query);
  1813. $sth->execute($parts_id) || $form->dberror($query);
  1814. $ref = $sth->fetchrow_hashref(NAME_lc);
  1815. for ( keys %$ref ) {
  1816. $form->{orderitems}{$parts_id}{$_} = $ref->{$_};
  1817. }
  1818. $sth->finish;
  1819. $form->{orderitems}{$parts_id}{required} += $required;
  1820. $query = qq|
  1821. SELECT pv.partnumber, pv.leadtime, pv.lastcost, pv.curr,
  1822. pv.vendor_id, v.name
  1823. FROM partsvendor pv
  1824. JOIN vendor v ON (v.id = pv.vendor_id)
  1825. WHERE pv.parts_id = ?|;
  1826. $sth = $dbh->prepare($query) || $form->dberror($query);
  1827. # get cost and vendor
  1828. $sth->execute($parts_id);
  1829. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1830. for ( keys %$ref ) {
  1831. $form->{orderitems}{$parts_id}{partsvendor}{ $ref->{vendor_id} }
  1832. {$_} = $ref->{$_};
  1833. }
  1834. }
  1835. $sth->finish;
  1836. }
  1837. }
  1838. sub generate_orders {
  1839. my ( $self, $myconfig, $form ) = @_;
  1840. my $i;
  1841. my %a;
  1842. my $query;
  1843. my $sth;
  1844. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1845. for (qw(qty lastcost)) {
  1846. $form->{"${_}_$i"} =
  1847. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  1848. }
  1849. if ( $form->{"qty_$i"} ) {
  1850. ( $vendor, $vendor_id ) =
  1851. split /--/, $form->{"vendor_$i"};
  1852. if ($vendor_id) {
  1853. $a{$vendor_id}{ $form->{"id_$i"} }{qty} += $form->{"qty_$i"};
  1854. for (qw(curr lastcost)) {
  1855. $a{$vendor_id}{ $form->{"id_$i"} }{$_} = $form->{"${_}_$i"};
  1856. }
  1857. }
  1858. }
  1859. }
  1860. # connect to database
  1861. my $dbh = $form->{dbh};
  1862. # foreign exchange rates
  1863. &exchangerate_defaults( $dbh, $form );
  1864. my $amount;
  1865. my $netamount;
  1866. my $curr = "";
  1867. my %tax;
  1868. my $taxincluded = 0;
  1869. my $vendor_id;
  1870. my $description;
  1871. my $unit;
  1872. my $sellprice;
  1873. foreach $vendor_id ( keys %a ) {
  1874. %tax = ();
  1875. $query = qq|
  1876. SELECT v.curr, v.taxincluded, t.rate, c.accno
  1877. FROM vendor v
  1878. LEFT JOIN vendortax vt ON (v.id = vt.vendor_id)
  1879. LEFT JOIN tax t ON (t.chart_id = vt.chart_id)
  1880. LEFT JOIN chart c ON (c.id = t.chart_id)
  1881. WHERE v.id = ?|;
  1882. $sth = $dbh->prepare($query);
  1883. $sth->execute($vendor_id) || $form->dberror($query);
  1884. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1885. $curr = $ref->{curr};
  1886. $taxincluded = $ref->{taxincluded};
  1887. $tax{ $ref->{accno} } = $ref->{rate};
  1888. }
  1889. $sth->finish;
  1890. $curr ||= $form->{defaultcurrency};
  1891. $taxincluded *= 1;
  1892. my $uid = localtime;
  1893. $uid .= "$$";
  1894. # TODO: Make this function insert as much as possible
  1895. $query = qq|
  1896. INSERT INTO oe (ordnumber)
  1897. VALUES ('$uid')|;
  1898. $dbh->do($query) || $form->dberror($query);
  1899. $query = qq|SELECT id FROM oe WHERE ordnumber = '$uid'|;
  1900. $sth = $dbh->prepare($query);
  1901. $sth->execute || $form->dberror($query);
  1902. my ($id) = $sth->fetchrow_array;
  1903. $sth->finish;
  1904. $amount = 0;
  1905. $netamount = 0;
  1906. foreach my $parts_id ( keys %{ $a{$vendor_id} } ) {
  1907. if ( ( $form->{$curr} * $form->{ $a{$vendor_id}{$parts_id}{curr} } )
  1908. > 0 )
  1909. {
  1910. $sellprice =
  1911. $a{$vendor_id}{$parts_id}{lastcost} / $form->{$curr} *
  1912. $form->{ $a{$vendor_id}{$parts_id}{curr} };
  1913. }
  1914. else {
  1915. $sellprice = $a{$vendor_id}{$parts_id}{lastcost};
  1916. }
  1917. $sellprice = $form->round_amount( $sellprice, 2 );
  1918. my $linetotal =
  1919. $form->round_amount( $sellprice * $a{$vendor_id}{$parts_id}{qty},
  1920. 2 );
  1921. $query = qq|
  1922. SELECT p.description, p.unit, c.accno
  1923. FROM parts p
  1924. LEFT JOIN partstax pt ON (p.id = pt.parts_id)
  1925. LEFT JOIN chart c ON (c.id = pt.chart_id)
  1926. WHERE p.id = ?|;
  1927. $sth = $dbh->prepare($query);
  1928. $sth->execute($parts_id) || $form->dberror($query);
  1929. my $rate = 0;
  1930. my $taxes = '';
  1931. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1932. $description = $ref->{description};
  1933. $unit = $ref->{unit};
  1934. $rate += $tax{ $ref->{accno} };
  1935. $taxes .= "$ref->{accno} ";
  1936. }
  1937. $sth->finish;
  1938. chop $taxes;
  1939. my @taxaccounts = Tax::init_taxes( $form, $taxes );
  1940. $netamount += $linetotal;
  1941. if ($taxincluded) {
  1942. $amount += $linetotal;
  1943. }
  1944. else {
  1945. $amount +=
  1946. $form->round_amount(
  1947. Tax::apply_taxes( \@taxaccounts, $form, $linetotal ), 2 );
  1948. }
  1949. $query = qq|
  1950. INSERT INTO orderitems
  1951. (trans_id, parts_id, description,
  1952. qty, ship, sellprice, unit)
  1953. VALUES
  1954. (?, ?, ?, ?, 0, ?, ?)|;
  1955. $sth = $dbh->prepare($query);
  1956. $sth->execute( $id, $parts_id, $description,
  1957. $a{vendor_id}{parts_id}{qty},
  1958. $sellprice, $unit )
  1959. || $form->dberror($query);
  1960. }
  1961. my $ordnumber = $form->update_defaults( $myconfig, 'ponumber' );
  1962. my $null;
  1963. my $employee_id;
  1964. my $department_id;
  1965. ( $null, $employee_id ) = $form->get_employee($dbh);
  1966. ( $null, $department_id ) = split /--/, $form->{department};
  1967. $department_id *= 1;
  1968. $query = qq|
  1969. UPDATE oe SET
  1970. ordnumber = ?,
  1971. transdate = current_date,
  1972. vendor_id = ?,
  1973. customer_id = 0,
  1974. amount = ?,
  1975. netamount = ?,
  1976. taxincluded = ?,
  1977. curr = ?,
  1978. employee_id = ?,
  1979. department_id = ?,
  1980. ponumber = ?
  1981. WHERE id = ?|;
  1982. $sth = $dbh->prepare($query);
  1983. $sth->execute(
  1984. $ordnumber, $vendor_id, $amount,
  1985. $netamount, $taxincluded, $curr,
  1986. $employee_id, $department_id, $form->{ponumber},
  1987. $id
  1988. ) || $form->dberror($query);
  1989. }
  1990. my $rc = $dbh->commit;
  1991. $rc;
  1992. }
  1993. sub consolidate_orders {
  1994. my ( $self, $myconfig, $form ) = @_;
  1995. # connect to database
  1996. my $dbh = $form->{dbh};
  1997. my $i;
  1998. my $id;
  1999. my $ref;
  2000. my %oe = ();
  2001. my $query = qq|SELECT * FROM oe WHERE id = ?|;
  2002. my $sth = $dbh->prepare($query) || $form->dberror($query);
  2003. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  2004. # retrieve order
  2005. if ( $form->{"ndx_$i"} ) {
  2006. $sth->execute( $form->{"ndx_$i"} );
  2007. $ref = $sth->fetchrow_hashref(NAME_lc);
  2008. $ref->{ndx} = $i;
  2009. $oe{oe}{ $ref->{curr} }{ $ref->{id} } = $ref;
  2010. $oe{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} }++;
  2011. $sth->finish;
  2012. }
  2013. }
  2014. $query = qq|SELECT * FROM orderitems WHERE trans_id = ?|;
  2015. $sth = $dbh->prepare($query) || $form->dberror($query);
  2016. foreach $curr ( keys %{ $oe{oe} } ) {
  2017. foreach $id (
  2018. sort { $oe{oe}{$curr}{$a}->{ndx} <=> $oe{oe}{$curr}{$b}->{ndx} }
  2019. keys %{ $oe{oe}{$curr} }
  2020. )
  2021. {
  2022. # retrieve order
  2023. $vc_id = $oe{oe}{$curr}{$id}->{"$form->{vc}_id"};
  2024. if ( $oe{vc}{ $oe{oe}{$curr}{$id}->{curr} }{$vc_id} > 1 ) {
  2025. push @{ $oe{orders}{$curr}{$vc_id} }, $id;
  2026. $sth->execute($id);
  2027. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  2028. push @{ $oe{orderitems}{$curr}{$id} }, $ref;
  2029. }
  2030. $sth->finish;
  2031. }
  2032. }
  2033. }
  2034. my $ordnumber = $form->{ordnumber};
  2035. my $numberfld = ( $form->{vc} eq 'customer' ) ? 'sonumber' : 'ponumber';
  2036. my ( $department, $department_id ) = $form->{department};
  2037. $department_id *= 1;
  2038. my $uid = localtime;
  2039. $uid .= "$$";
  2040. my @orderitems = ();
  2041. foreach $curr ( keys %{ $oe{orders} } ) {
  2042. foreach $vc_id ( sort { $a <=> $b } keys %{ $oe{orders}{$curr} } ) {
  2043. # the orders
  2044. @orderitems = ();
  2045. $form->{customer_id} = $form->{vendor_id} = 0;
  2046. $form->{"$form->{vc}_id"} = $vc_id;
  2047. $amount = 0;
  2048. $netamount = 0;
  2049. foreach $id ( @{ $oe{orders}{$curr}{$vc_id} } ) {
  2050. # header
  2051. $ref = $oe{oe}{$curr}{$id};
  2052. $amount += $ref->{amount};
  2053. $netamount += $ref->{netamount};
  2054. $id = $dbh->quote($id);
  2055. foreach $item ( @{ $oe{orderitems}{$curr}{$id} } ) {
  2056. push @orderitems, $item;
  2057. }
  2058. # close order
  2059. $query = qq|
  2060. UPDATE oe SET
  2061. closed = '1'
  2062. WHERE id = $id|;
  2063. $dbh->do($query) || $form->dberror($query);
  2064. # reset shipped
  2065. $query = qq|
  2066. UPDATE orderitems SET
  2067. ship = 0
  2068. WHERE trans_id = $id|;
  2069. $dbh->do($query) || $form->dberror($query);
  2070. }
  2071. $ordnumber ||=
  2072. $form->update_defaults( $myconfig, $numberfld, $dbh );
  2073. #fixme: Change this
  2074. $query = qq|
  2075. INSERT INTO oe (ordnumber) VALUES ('$uid')|;
  2076. $dbh->do($query) || $form->dberror($query);
  2077. $query = qq|
  2078. SELECT id
  2079. FROM oe
  2080. WHERE ordnumber = '$uid'|;
  2081. ($id) = $dbh->selectrow_array($query);
  2082. $ref->{employee_id} *= 1;
  2083. $query = qq|
  2084. UPDATE oe SET
  2085. ordnumber = | . $dbh->quote($ordnumber) . qq|,
  2086. transdate = current_date,
  2087. vendor_id = $form->{vendor_id},
  2088. customer_id = $form->{customer_id},
  2089. amount = $amount,
  2090. netamount = $netamount,
  2091. reqdate = | . $form->dbquote( $ref->{reqdate}, SQL_DATE ) . qq|,
  2092. taxincluded = '$ref->{taxincluded}',
  2093. shippingpoint = | . $dbh->quote( $ref->{shippingpoint} ) . qq|,
  2094. notes = | . $dbh->quote( $ref->{notes} ) . qq|,
  2095. curr = '$curr',
  2096. employee_id = $ref->{employee_id},
  2097. intnotes = | . $dbh->quote( $ref->{intnotes} ) . qq|,
  2098. shipvia = | . $dbh->quote( $ref->{shipvia} ) . qq|,
  2099. language_code = '$ref->{language_code}',
  2100. ponumber = | . $dbh->quote( $form->{ponumber} ) . qq|,
  2101. department_id = $department_id
  2102. WHERE id = $id|;
  2103. $sth = $dbh->prepare($query);
  2104. $sth->execute() || $form->dberror($query);
  2105. # add items
  2106. foreach $item (@orderitems) {
  2107. for (
  2108. qw(
  2109. qty sellprice discount project_id ship)
  2110. )
  2111. {
  2112. $item->{$_} *= 1;
  2113. }
  2114. $query = qq|
  2115. INSERT INTO orderitems
  2116. (trans_id, parts_id, description,
  2117. qty, sellprice, discount, unit, reqdate,
  2118. project_id, ship, serialnumber, notes)
  2119. VALUES
  2120. (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
  2121. $sth = $dbh->prepare($query);
  2122. $sth->execute(
  2123. $id, $item->{parts_id},
  2124. $item->{description}, $item->{qty},
  2125. $item->{sellprice}, $item->{discount},
  2126. $item->{unit}, $form->{reqdate},
  2127. $item->{project_id}, $item->{ship},
  2128. $item->{serialnumber}, $item->{notes}
  2129. ) || $form->dberror($query);
  2130. }
  2131. }
  2132. }
  2133. $rc = $dbh->commit;
  2134. $rc;
  2135. }
  2136. 1;