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