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