summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: 5b83c786b0bb57a60b3636b12ef71173470763eb (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2001
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has undergone whitespace cleanup
  27. #
  28. #======================================================================
  29. #
  30. # Order entry module
  31. # Quotation
  32. #
  33. #======================================================================
  34. package OE;
  35. use LedgerSMB::Tax;
  36. use LedgerSMB::Sysconfig;
  37. sub transactions {
  38. my ( $self, $myconfig, $form ) = @_;
  39. # connect to database
  40. my $dbh = $form->{dbh};
  41. my $query;
  42. my $null;
  43. my $var;
  44. my $ordnumber = 'ordnumber';
  45. my $quotation = '0';
  46. my $department;
  47. my $rate = ( $form->{vc} eq 'customer' ) ? 'buy' : 'sell';
  48. ( $form->{transdatefrom}, $form->{transdateto} ) =
  49. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  50. if $form->{year} && $form->{month};
  51. if ( $form->{type} =~ /_quotation$/ ) {
  52. $quotation = '1';
  53. $ordnumber = 'quonumber';
  54. }
  55. my $number = $form->like( lc $form->{$ordnumber} );
  56. my $name = $form->like( lc $form->{ $form->{vc} } );
  57. my @dptargs = ();
  58. for (qw(department employee)) {
  59. if ( $form->{$_} ) {
  60. ( $null, $var ) = split /--/, $form->{$_};
  61. $department .= " AND o.${_}_id = ?";
  62. push @dptargs, $var;
  63. }
  64. }
  65. if ( $form->{vc} ne 'customer' ) { # Sanitize $form->{vc}
  66. $form->{vc} = 'vendor';
  67. }
  68. my $query = qq|
  69. SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
  70. o.amount, ct.name, o.netamount, o.$form->{vc}_id,
  71. ex.$rate AS exchangerate, o.closed, o.quonumber,
  72. o.shippingpoint, o.shipvia, e.name AS employee,
  73. m.name AS manager, o.curr, o.ponumber
  74. FROM oe o
  75. JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
  76. LEFT JOIN employee e ON (o.employee_id = e.id)
  77. LEFT JOIN employee m ON (e.managerid = m.id)
  78. LEFT JOIN exchangerate ex
  79. ON (ex.curr = o.curr AND ex.transdate = o.transdate)
  80. WHERE o.quotation = ?
  81. $department|;
  82. my @queryargs = @dptargs;
  83. unshift @queryargs, $quotation;
  84. my %ordinal = (
  85. id => 1,
  86. ordnumber => 2,
  87. transdate => 3,
  88. reqdate => 4,
  89. name => 6,
  90. quonumber => 11,
  91. shipvia => 13,
  92. employee => 14,
  93. manager => 15,
  94. curr => 16,
  95. ponumber => 17
  96. );
  97. my @a = ( transdate, $ordnumber, name );
  98. push @a, "employee" if $form->{l_employee};
  99. if ( $form->{type} !~ /(ship|receive)_order/ ) {
  100. push @a, "manager" if $form->{l_manager};
  101. }
  102. my $sortorder = $form->sort_order( \@a, \%ordinal );
  103. # build query if type eq (ship|receive)_order
  104. if ( $form->{type} =~ /(ship|receive)_order/ ) {
  105. my ( $warehouse, $warehouse_id ) = split /--/, $form->{warehouse};
  106. $query = qq|
  107. SELECT DISTINCT o.id, o.ordnumber, o.transdate,
  108. o.reqdate, o.amount, ct.name, o.netamount,
  109. o.$form->{vc}_id, ex.$rate AS exchangerate,
  110. o.closed, o.quonumber, o.shippingpoint,
  111. o.shipvia, e.name AS employee, o.curr,
  112. o.ponumber
  113. FROM oe o
  114. JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
  115. JOIN orderitems oi ON (oi.trans_id = o.id)
  116. JOIN parts p ON (p.id = oi.parts_id)|;
  117. if ( $warehouse_id && $form->{type} eq 'ship_order' ) {
  118. $query .= qq|
  119. JOIN inventory i ON (oi.parts_id = i.parts_id)
  120. |;
  121. }
  122. $query .= qq|
  123. LEFT JOIN employee e ON (o.employee_id = e.id)
  124. LEFT JOIN exchangerate ex
  125. ON (ex.curr = o.curr
  126. AND ex.transdate = o.transdate)
  127. WHERE o.quotation = '0'
  128. AND (p.inventory_accno_id > 0 OR p.assembly = '1')
  129. AND oi.qty != oi.ship
  130. $department|;
  131. @queryargs = @dptargs; #reset @queryargs
  132. if ( $warehouse_id && $form->{type} eq 'ship_order' ) {
  133. $query .= qq|
  134. AND i.warehouse_id = ?
  135. AND (
  136. SELECT SUM(i.qty)
  137. FROM inventory i
  138. WHERE oi.parts_id = i.parts_id
  139. AND i.warehouse_id = ?
  140. ) > 0|;
  141. push( @queryargs, $warehouse_id, $warehouse_id );
  142. }
  143. }
  144. if ( $form->{"$form->{vc}_id"} ) {
  145. $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  146. }
  147. elsif ( $form->{ $form->{vc} } ne "" ) {
  148. $query .= " AND lower(ct.name) LIKE ?";
  149. push @queryargs, $name;
  150. }
  151. if ( $form->{$ordnumber} ne "" ) {
  152. $ordnumber = ($ordnumber eq 'ordnumber') ? 'ordnumber' : 'quonumber';
  153. $query .= " AND lower($ordnumber) LIKE ?";
  154. push @queryargs, $number;
  155. $form->{open} = 1;
  156. $form->{closed} = 1;
  157. }
  158. if ( $form->{ponumber} ne "" ) {
  159. $query .= " AND lower(ponumber) LIKE '%' || lower(?) || '%'";
  160. push @queryargs, $form->{ponumber};
  161. }
  162. if ( !$form->{open} && !$form->{closed} ) {
  163. $query .= " AND o.id = 0";
  164. }
  165. elsif ( !( $form->{open} && $form->{closed} ) ) {
  166. $query .=
  167. ( $form->{open} ) ? " AND o.closed = '0'" : " AND o.closed = '1'";
  168. }
  169. if ( $form->{shipvia} ne "" ) {
  170. $var = $form->like( lc $form->{shipvia} );
  171. $query .= " AND lower(o.shipvia) LIKE ?";
  172. push @queryargs, $var;
  173. }
  174. if ( $form->{description} ne "" ) {
  175. $var = $dbh->quote($form->like( lc $form->{description} ));
  176. $query .= " AND o.id IN (SELECT DISTINCT trans_id
  177. FROM orderitems
  178. WHERE lower(description) LIKE $var)";
  179. push @queryargs, $var;
  180. }
  181. if ( $form->{transdatefrom} ) {
  182. $query .= " AND o.transdate >= ?";
  183. push @queryargs, $form->{transdatefrom};
  184. }
  185. if ( $form->{transdateto} ) {
  186. $query .= " AND o.transdate <= ?";
  187. push @queryargs, $form->{transdateto};
  188. }
  189. $query .= " ORDER by $sortorder";
  190. my $sth = $dbh->prepare($query);
  191. $sth->execute(@queryargs) || $form->dberror($query);
  192. my %oid = ();
  193. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  194. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  195. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  196. if ( $ref->{id} != $oid{id}{ $ref->{id} } ) {
  197. push @{ $form->{OE} }, $ref;
  198. $oid{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} }++;
  199. }
  200. $oid{id}{ $ref->{id} } = $ref->{id};
  201. }
  202. $sth->finish;
  203. $dbh->commit;
  204. if ( $form->{type} =~ /^consolidate_/ ) {
  205. @a = ();
  206. foreach $ref ( @{ $form->{OE} } ) {
  207. push @a, $ref
  208. if $oid{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} } > 1;
  209. }
  210. @{ $form->{OE} } = @a;
  211. }
  212. }
  213. sub save {
  214. my ( $self, $myconfig, $form ) = @_;
  215. my $ordnumber;
  216. my $quotation;
  217. my $numberfld;
  218. if ( $form->{type} =~ /_order$/ ) {
  219. $quotation = "0";
  220. $ordnumber = "ordnumber";
  221. $numberfld =
  222. ( $form->{vc} eq 'customer' )
  223. ? "sonumber"
  224. : "ponumber";
  225. }
  226. else {
  227. $quotation = "1";
  228. $ordnumber = "quonumber";
  229. $numberfld =
  230. ( $form->{vc} eq 'customer' )
  231. ? "sqnumber"
  232. : "rfqnumber";
  233. }
  234. $form->{"$ordnumber"} =
  235. $form->update_defaults( $myconfig, $numberfld, $dbh )
  236. unless $form->{"$ordnumber"};
  237. $form->db_prepare_vars(
  238. "quonumber", "transdate", "vendor_id", "customer_id",
  239. "reqdate", "taxincluded", "shippingpoint", "shipvia",
  240. "currency", "department_id", "employee_id", "language_code",
  241. "ponumber", "terms"
  242. );
  243. # connect to database, turn off autocommit
  244. my $dbh = $form->{dbh};
  245. my @queryargs;
  246. $form->{vc} = ( $form->{vc} eq 'customer' ) ? 'customer' : 'vendor';
  247. my $query;
  248. my $sth;
  249. my $null;
  250. my $exchangerate = 0;
  251. ( $null, $form->{employee_id} ) = split /--/, $form->{employee};
  252. if ( !$form->{employee_id} ) {
  253. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  254. $form->{employee} = "$form->{employee}--$form->{employee_id}";
  255. }
  256. my $ml = ( $form->{type} eq 'sales_order' ) ? 1 : -1;
  257. $query = qq|
  258. SELECT p.assembly, p.project_id
  259. FROM parts p WHERE p.id = ?|;
  260. my $pth = $dbh->prepare($query) || $form->dberror($query);
  261. if ( $form->{id} ) {
  262. $query = qq|SELECT id FROM oe WHERE id = $form->{id}|;
  263. if ( $dbh->selectrow_array($query) ) {
  264. &adj_onhand( $dbh, $form, $ml )
  265. if $form->{type} =~ /_order$/;
  266. $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
  267. $sth = $dbh->prepare($query);
  268. $sth->execute( $form->{id} ) || $form->dberror($query);
  269. $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
  270. $sth = $dbh->prepare($query);
  271. $sth->execute( $form->{id} ) || $form->dberror($query);
  272. }
  273. else { # id is not in the database
  274. delete $form->{id};
  275. }
  276. }
  277. my $did_insert = 0;
  278. if ( !$form->{id} ) {
  279. $query = qq|SELECT nextval('id')|;
  280. $sth = $dbh->prepare($query);
  281. $sth->execute || $form->dberror($query);
  282. ( $form->{id} ) = $sth->fetchrow_array;
  283. $sth->finish;
  284. my $uid = localtime;
  285. $uid .= "$$";
  286. if ( !$form->{reqdate} ) {
  287. $form->{reqdate} = undef;
  288. }
  289. if ( !$form->{transdate} ) {
  290. $form->{transdate} = "now";
  291. }
  292. if ( ( $form->{closed} ne 't' ) and ( $form->{closed} ne "1" ) ) {
  293. $form->{closed} = 'f';
  294. }
  295. # $form->{id} is safe because it is only pulled *from* the db.
  296. $query = qq|
  297. INSERT INTO oe
  298. (id, ordnumber, quonumber, transdate, vendor_id,
  299. customer_id, reqdate, shippingpoint, shipvia,
  300. notes, intnotes, curr, closed, department_id,
  301. employee_id, language_code, ponumber, terms,
  302. quotation)
  303. VALUES
  304. ($form->{id}, ?, ?, ?, ?,
  305. ?, ?, ?, ?,
  306. ?, ?, ?, ?, ?,
  307. ?, ?, ?, ?, ?)|;
  308. @queryargs = (
  309. $form->{ordnumber}, $form->{quonumber},
  310. $form->{transdate}, $form->{vendor_id},
  311. $form->{customer_id}, $form->{reqdate},
  312. $form->{shippingpoint}, $form->{shipvia},
  313. $form->{notes}, $form->{intnotes},
  314. $form->{currency}, $form->{closed},
  315. $form->{department_id}, $form->{employee_id},
  316. $form->{language_code}, $form->{ponumber},
  317. $form->{terms}, $quotation
  318. );
  319. $sth = $dbh->prepare($query);
  320. $sth->execute(@queryargs) || $form->dberror($query);
  321. $sth->finish;
  322. @queries = $form->run_custom_queries( 'oe', 'INSERT' );
  323. }
  324. my $amount;
  325. my $linetotal;
  326. my $discount;
  327. my $project_id;
  328. my $taxrate;
  329. my $taxamount;
  330. my $fxsellprice;
  331. my %taxbase;
  332. my @taxaccounts;
  333. my %taxaccounts;
  334. my $netamount = 0;
  335. my $rowcount = $form->{rowcount};
  336. for my $i ( 1 .. $rowcount ) {
  337. $form->{"ship_$i"} = 0 unless $form->{"ship_$i"};
  338. $form->db_prepare_vars( "orderitems_id_$i", "id_$i", "description_$i",
  339. "project_id_$i" );
  340. for (qw(qty ship)) {
  341. $form->{"${_}_$i"} =
  342. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  343. }
  344. $form->{"discount_$i"} =
  345. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) / 100;
  346. $form->{"sellprice_$i"} =
  347. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  348. if ( $form->{"qty_$i"} ) {
  349. $pth->execute( $form->{"id_$i"} );
  350. $ref = $pth->fetchrow_hashref(NAME_lc);
  351. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  352. $pth->finish;
  353. $fxsellprice = $form->{"sellprice_$i"};
  354. my ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  355. $dec = length $dec;
  356. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  357. $discount =
  358. $form->round_amount(
  359. $form->{"sellprice_$i"} * $form->{"discount_$i"},
  360. $decimalplaces );
  361. $form->{"sellprice_$i"} =
  362. $form->round_amount( $form->{"sellprice_$i"} - $discount,
  363. $decimalplaces );
  364. $linetotal =
  365. $form->round_amount( $form->{"sellprice_$i"} * $form->{"qty_$i"},
  366. 2 );
  367. @taxaccounts = Tax::init_taxes( $form, $form->{"taxaccounts_$i"},
  368. $form->{taxaccounts} );
  369. if ( $form->{taxincluded} ) {
  370. $taxamount =
  371. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  372. $form->{"sellprice_$i"} =
  373. Tax::extract_taxes( \@taxaccounts, $form,
  374. $form->{"sellprice_$i"} );
  375. $taxbase =
  376. Tax::extract_taxes( \@taxaccounts, $form, $linetotal );
  377. }
  378. else {
  379. $taxamount =
  380. Tax::apply_taxes( \@taxaccounts, $form, $linetotal );
  381. $taxbase = $linetotal;
  382. }
  383. if ( @taxaccounts && $form->round_amount( $taxamount, 2 ) == 0 ) {
  384. if ( $form->{taxincluded} ) {
  385. foreach $item (@taxaccounts) {
  386. $taxamount = $form->round_amount( $item->value, 2 );
  387. $taxaccounts{ $item->account } += $taxamount;
  388. $taxdiff += $taxamount;
  389. $taxbase{ $item->account } += $taxbase;
  390. }
  391. $taxaccounts{ $taxaccounts[0]->account } += $taxdiff;
  392. }
  393. else {
  394. foreach $item (@taxaccounts) {
  395. $taxaccounts{ $item->account } += $item->value;
  396. $taxbase{ $item->account } += $taxbase;
  397. }
  398. }
  399. }
  400. else {
  401. foreach $item (@taxaccounts) {
  402. $taxaccounts{ $item->account } += $item->value;
  403. $taxbase{ $item->account } += $taxbase;
  404. }
  405. }
  406. $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
  407. if ( $form->{"projectnumber_$i"} ne "" ) {
  408. ( $null, $project_id ) = split /--/,
  409. $form->{"projectnumber_$i"};
  410. }
  411. $project_id = $form->{"project_id_$i"}
  412. if $form->{"project_id_$i"};
  413. if ( !$form->{"reqdate_$i"} ) {
  414. $form->{"reqdate_$i"} = undef;
  415. }
  416. @queryargs = ();
  417. # save detail record in orderitems table
  418. $query = qq|INSERT INTO orderitems (|;
  419. $query .= qq|
  420. trans_id, parts_id, description, qty, sellprice,
  421. discount, unit, reqdate, project_id, ship,
  422. serialnumber, notes)
  423. VALUES (|;
  424. $query .= qq| ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
  425. $sth = $dbh->prepare($query);
  426. push( @queryargs,
  427. $form->{id}, $form->{"id_$i"},
  428. $form->{"description_$i"}, $form->{"qty_$i"},
  429. $fxsellprice, $form->{"discount_$i"},
  430. $form->{"unit_$i"}, $form->{"reqdate_$i"},
  431. $project_id, $form->{"ship_$i"},
  432. $form->{"serialnumber_$i"}, $form->{"notes_$i"} );
  433. $sth->execute(@queryargs) || $form->dberror($query);
  434. $form->{"sellprice_$i"} = $fxsellprice;
  435. }
  436. $form->{"discount_$i"} *= 100;
  437. }
  438. # set values which could be empty
  439. for (qw(vendor_id customer_id taxincluded closed quotation)) {
  440. $form->{$_} *= 1;
  441. }
  442. # add up the tax
  443. my $tax = 0;
  444. for ( keys %taxaccounts ) { $tax += $taxaccounts{$_} }
  445. $amount = $form->round_amount( $netamount + $tax, 2 );
  446. $netamount = $form->round_amount( $netamount, 2 );
  447. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  448. $form->{exchangerate} = 1;
  449. }
  450. else {
  451. $exchangerate =
  452. $form->check_exchangerate( $myconfig, $form->{currency},
  453. $form->{transdate},
  454. ( $form->{vc} eq 'customer' ) ? 'buy' : 'sell' );
  455. }
  456. $form->{exchangerate} =
  457. ($exchangerate)
  458. ? $exchangerate
  459. : $form->parse_amount( $myconfig, $form->{exchangerate} );
  460. ( $null, $form->{department_id} ) = split( /--/, $form->{department} );
  461. for (qw(department_id terms)) { $form->{$_} *= 1 }
  462. if ($did_insert) {
  463. $query = qq|
  464. UPDATE oe SET
  465. amount = ?,
  466. netamount = ?,
  467. taxincluded = ?
  468. WHERE id = ?|;
  469. @queryargs = ( $amount, $netamount, $form->{taxincluded}, $form->{id} );
  470. }
  471. else {
  472. # save OE record
  473. $query = qq|
  474. UPDATE oe set
  475. ordnumber = ?,
  476. quonumber = ?,
  477. transdate = ?,
  478. vendor_id = ?,
  479. customer_id = ?,
  480. amount = ?,
  481. netamount = ?,
  482. reqdate = ?,
  483. taxincluded = ?,
  484. shippingpoint = ?,
  485. shipvia = ?,
  486. notes = ?,
  487. intnotes = ?,
  488. curr = ?,
  489. closed = ?,
  490. quotation = ?,
  491. department_id = ?,
  492. employee_id = ?,
  493. language_code = ?,
  494. ponumber = ?,
  495. terms = ?
  496. WHERE id = ?|;
  497. if ( !$form->{reqdate} ) {
  498. $form->{reqdate} = undef;
  499. }
  500. @queryargs = (
  501. $form->{ordnumber}, $form->{quonumber},
  502. $form->{transdate}, $form->{vendor_id},
  503. $form->{customer_id}, $amount,
  504. $netamount, $form->{reqdate},
  505. $form->{taxincluded}, $form->{shippingpoint},
  506. $form->{shipvia}, $form->{notes},
  507. $form->{intnotes}, $form->{currency},
  508. $form->{closed}, $quotation,
  509. $form->{department_id}, $form->{employee_id},
  510. $form->{language_code}, $form->{ponumber},
  511. $form->{terms}, $form->{id}
  512. );
  513. }
  514. $sth = $dbh->prepare($query);
  515. $sth->execute(@queryargs) || $form->dberror($query);
  516. if ( $amount->is_nan ) {
  517. $dbh->rollback;
  518. return;
  519. }
  520. if ( !$did_insert ) {
  521. @queries = $form->run_custom_queries( 'oe', 'UPDATE' );
  522. }
  523. $form->{ordtotal} = $amount;
  524. # add shipto
  525. $form->{name} = $form->{ $form->{vc} };
  526. $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
  527. $form->add_shipto( $dbh, $form->{id} );
  528. # save printed, emailed, queued
  529. $form->save_status($dbh);
  530. if ( ( $form->{currency} ne $form->{defaultcurrency} ) && !$exchangerate ) {
  531. if ( $form->{vc} eq 'customer' ) {
  532. $form->update_exchangerate( $dbh, $form->{currency},
  533. $form->{transdate}, $form->{exchangerate}, 0 );
  534. }
  535. if ( $form->{vc} eq 'vendor' ) {
  536. $form->update_exchangerate( $dbh, $form->{currency},
  537. $form->{transdate}, 0, $form->{exchangerate} );
  538. }
  539. }
  540. if ( $form->{type} =~ /_order$/ ) {
  541. # adjust onhand
  542. &adj_onhand( $dbh, $form, $ml * -1 );
  543. &adj_inventory( $dbh, $myconfig, $form );
  544. }
  545. my %audittrail = (
  546. tablename => 'oe',
  547. reference => ( $form->{type} =~ /_order$/ )
  548. ? $form->{ordnumber}
  549. : $form->{quonumber},
  550. formname => $form->{type},
  551. action => 'saved',
  552. id => $form->{id}
  553. );
  554. $form->audittrail( $dbh, "", \%audittrail );
  555. $form->save_recurring( $dbh, $myconfig );
  556. my $rc = $dbh->commit;
  557. $rc;
  558. }
  559. sub delete {
  560. my ( $self, $myconfig, $form ) = @_;
  561. # connect to database
  562. my $dbh = $form->{dbh};
  563. # delete spool files
  564. my $query = qq|
  565. SELECT spoolfile FROM status
  566. WHERE trans_id = ?
  567. AND spoolfile IS NOT NULL|;
  568. $sth = $dbh->prepare($query);
  569. $sth->execute( $form->{id} ) || $form->dberror($query);
  570. my $spoolfile;
  571. my @spoolfiles = ();
  572. while ( ($spoolfile) = $sth->fetchrow_array ) {
  573. push @spoolfiles, $spoolfile;
  574. }
  575. $sth->finish;
  576. $query = qq|
  577. SELECT o.parts_id, o.ship, p.inventory_accno_id, p.assembly
  578. FROM orderitems o
  579. JOIN parts p ON (p.id = o.parts_id)
  580. WHERE trans_id = ?|;
  581. $sth = $dbh->prepare($query);
  582. $sth->execute( $form->{id} ) || $form->dberror($query);
  583. if ( $form->{type} =~ /_order$/ ) {
  584. $ml = ( $form->{type} eq 'purchase_order' ) ? -1 : 1;
  585. while ( my ( $id, $ship, $inv, $assembly ) = $sth->fetchrow_array ) {
  586. $form->update_balance( $dbh, "parts", "onhand", "id = $id",
  587. $ship * $ml )
  588. if ( $inv || $assembly );
  589. }
  590. }
  591. $sth->finish;
  592. # delete inventory
  593. $query = qq|DELETE FROM inventory WHERE trans_id = ?|;
  594. $sth = $dbh->prepare($query);
  595. $sth->execute( $form->{id} ) || $form->dberror($query);
  596. $sth->finish;
  597. # delete status entries
  598. $query = qq|DELETE FROM status WHERE trans_id = ?|;
  599. $sth = $dbh->prepare($query);
  600. $sth->execute( $form->{id} ) || $form->dberror($query);
  601. $sth->finish;
  602. # delete OE record
  603. $query = qq|DELETE FROM oe WHERE id = ?|;
  604. $sth = $dbh->prepare($query);
  605. $sth->execute( $form->{id} ) || $form->dberror($query);
  606. $sth->finish;
  607. # delete individual entries
  608. $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
  609. $sth->finish;
  610. $query = qq|DELETE FROM shipto WHERE trans_id = ?|;
  611. $sth = $dbh->prepare($query);
  612. $sth->execute( $form->{id} ) || $form->dberror($query);
  613. $sth->finish;
  614. my %audittrail = (
  615. tablename => 'oe',
  616. reference => ( $form->{type} =~ /_order$/ )
  617. ? $form->{ordnumber}
  618. : $form->{quonumber},
  619. formname => $form->{type},
  620. action => 'deleted',
  621. id => $form->{id}
  622. );
  623. $form->audittrail( $dbh, "", \%audittrail );
  624. my $rc = $dbh->commit;
  625. if ($rc) {
  626. foreach $spoolfile (@spoolfiles) {
  627. unlink "${LedgerSMB::Sysconfig::spool}/$spoolfile" if $spoolfile;
  628. }
  629. }
  630. $rc;
  631. }
  632. sub retrieve {
  633. use LedgerSMB::PriceMatrix;
  634. my ( $self, $myconfig, $form ) = @_;
  635. # connect to database
  636. my $dbh = $form->{dbh};
  637. my $query;
  638. my $sth;
  639. my $var;
  640. my $ref;
  641. $query = qq|
  642. SELECT value, current_date FROM defaults
  643. WHERE setting_key = 'curr'|;
  644. ( $form->{currencies}, $form->{transdate} ) = $dbh->selectrow_array($query);
  645. if ( $form->{id} ) {
  646. # retrieve order
  647. $query = qq|
  648. SELECT o.ordnumber, o.transdate, o.reqdate, o.terms,
  649. o.taxincluded, o.shippingpoint, o.shipvia,
  650. o.notes, o.intnotes, o.curr AS currency,
  651. e.name AS employee, o.employee_id,
  652. o.$form->{vc}_id, vc.name AS $form->{vc},
  653. o.amount AS invtotal, o.closed, o.reqdate,
  654. o.quonumber, o.department_id,
  655. d.description AS department, o.language_code,
  656. o.ponumber
  657. FROM oe o
  658. JOIN $form->{vc} vc ON (o.$form->{vc}_id = vc.id)
  659. LEFT JOIN employee e ON (o.employee_id = e.id)
  660. LEFT JOIN department d ON (o.department_id = d.id)
  661. WHERE o.id = ?|;
  662. $sth = $dbh->prepare($query);
  663. $sth->execute( $form->{id} ) || $form->dberror($query);
  664. $ref = $sth->fetchrow_hashref(NAME_lc);
  665. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  666. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  667. $sth->finish;
  668. $query = qq|SELECT * FROM shipto WHERE trans_id = ?|;
  669. $sth = $dbh->prepare($query);
  670. $sth->execute( $form->{id} ) || $form->dberror($query);
  671. $ref = $sth->fetchrow_hashref(NAME_lc);
  672. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  673. $sth->finish;
  674. # get printed, emailed and queued
  675. $query = qq|
  676. SELECT s.printed, s.emailed, s.spoolfile, s.formname
  677. FROM status s
  678. WHERE s.trans_id = ?|;
  679. $sth = $dbh->prepare($query);
  680. $sth->execute( $form->{id} ) || $form->dberror($query);
  681. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  682. $form->{printed} .= "$ref->{formname} "
  683. if $ref->{printed};
  684. $form->{emailed} .= "$ref->{formname} "
  685. if $ref->{emailed};
  686. $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
  687. if $ref->{spoolfile};
  688. }
  689. $sth->finish;
  690. for (qw(printed emailed queued)) { $form->{$_} =~ s/ +$//g }
  691. # retrieve individual items
  692. $query = qq|
  693. SELECT o.id AS orderitems_id, p.partnumber, p.assembly,
  694. o.description, o.qty, o.sellprice,
  695. o.parts_id AS id, o.unit, o.discount, p.bin,
  696. o.reqdate, o.project_id, o.ship, o.serialnumber,
  697. o.notes, pr.projectnumber, pg.partsgroup,
  698. p.partsgroup_id, p.partnumber AS sku,
  699. p.listprice, p.lastcost, p.weight, p.onhand,
  700. p.inventory_accno_id, p.income_accno_id,
  701. p.expense_accno_id, t.description
  702. AS partsgrouptranslation
  703. FROM orderitems o
  704. JOIN parts p ON (o.parts_id = p.id)
  705. LEFT JOIN project pr ON (o.project_id = pr.id)
  706. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  707. LEFT JOIN translation t
  708. ON (t.trans_id = p.partsgroup_id
  709. AND t.language_code = ?)
  710. WHERE o.trans_id = ?
  711. ORDER BY o.id|;
  712. $sth = $dbh->prepare($query);
  713. $sth->execute( $form->{language_code}, $form->{id} )
  714. || $form->dberror($query);
  715. # foreign exchange rates
  716. &exchangerate_defaults( $dbh, $form );
  717. # query for price matrix
  718. my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
  719. # taxes
  720. $query = qq|
  721. SELECT c.accno FROM chart c
  722. JOIN partstax pt ON (pt.chart_id = c.id)
  723. WHERE pt.parts_id = ?|;
  724. my $tth = $dbh->prepare($query) || $form->dberror($query);
  725. my $taxrate;
  726. my $ptref;
  727. my $sellprice;
  728. my $listprice;
  729. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  730. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  731. ($decimalplaces) = ( $ref->{sellprice} =~ /\.(\d+)/ );
  732. $decimalplaces = length $decimalplaces;
  733. $decimalplaces = ( $decimalplaces > 2 ) ? $decimalplaces : 2;
  734. $tth->execute( $ref->{id} );
  735. $ref->{taxaccounts} = "";
  736. $taxrate = 0;
  737. while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
  738. $ref->{taxaccounts} .= "$ptref->{accno} ";
  739. $taxrate += $form->{"$ptref->{accno}_rate"};
  740. }
  741. $tth->finish;
  742. chop $ref->{taxaccounts};
  743. # preserve price
  744. $sellprice = $ref->{sellprice};
  745. # multiply by exchangerate
  746. $ref->{sellprice} =
  747. $form->round_amount(
  748. $ref->{sellprice} * $form->{ $form->{currency} },
  749. $decimalplaces );
  750. for (qw(listprice lastcost)) {
  751. $ref->{$_} =
  752. $form->round_amount(
  753. $ref->{$_} / $form->{ $form->{currency} },
  754. $decimalplaces );
  755. }
  756. # partnumber and price matrix
  757. PriceMatrix::price_matrix( $pmh, $ref, $form->{transdate},
  758. $decimalplaces, $form, $myconfig );
  759. $ref->{sellprice} = $sellprice;
  760. $ref->{partsgroup} = $ref->{partsgrouptranslation}
  761. if $ref->{partsgrouptranslation};
  762. push @{ $form->{form_details} }, $ref;
  763. }
  764. $sth->finish;
  765. # get recurring transaction
  766. $form->get_recurring;
  767. @queries = $form->run_custom_queries( 'oe', 'SELECT' );
  768. }
  769. else {
  770. # get last name used
  771. $form->lastname_used( $myconfig, $dbh, $form->{vc} )
  772. unless $form->{"$form->{vc}_id"};
  773. delete $form->{notes};
  774. }
  775. $dbh->commit;
  776. }
  777. sub exchangerate_defaults {
  778. my ( $dbh2, $form ) = @_;
  779. $dbh = $form->{dbh};
  780. my $var;
  781. my $buysell = ( $form->{vc} eq "customer" ) ? "buy" : "sell";
  782. # get default currencies
  783. my $query = qq|
  784. SELECT substr(value,1,3), value FROM defaults
  785. WHERE setting_key = 'curr'|;
  786. ( $form->{defaultcurrency}, $form->{currencies} ) =
  787. $dbh->selectrow_array($query);
  788. $query = qq|
  789. SELECT $buysell
  790. FROM exchangerate
  791. WHERE curr = ?
  792. AND transdate = ?|;
  793. my $eth1 = $dbh->prepare($query) || $form->dberror($query);
  794. $query = qq~
  795. SELECT max(transdate || ' ' || $buysell || ' ' || curr)
  796. FROM exchangerate
  797. WHERE curr = ?~;
  798. my $eth2 = $dbh->prepare($query) || $form->dberror($query);
  799. # get exchange rates for transdate or max
  800. foreach $var ( split /:/, substr( $form->{currencies}, 4 ) ) {
  801. $eth1->execute( $var, $form->{transdate} );
  802. my @exchangelist;
  803. @exchangelist = $eth1->fetchrow_array;
  804. $form->db_parse_numeric(sth=>$eth1, arrayref=>\@exchangelist);
  805. $form->{$var} = shift @array;
  806. if ( !$form->{$var} ) {
  807. $eth2->execute($var);
  808. @exchangelist = $eth2->fetchrow_array;
  809. $form->db_parse_numeric(sth=>$eth2, arrayref=>\@exchangelist);
  810. ( $form->{$var} ) = @exchangelist;
  811. ( $null, $form->{$var} ) = split / /, $form->{$var};
  812. $form->{$var} = 1 unless $form->{$var};
  813. $eth2->finish;
  814. }
  815. $eth1->finish;
  816. }
  817. $form->{ $form->{currency} } = $form->{exchangerate}
  818. if $form->{exchangerate};
  819. $form->{ $form->{currency} } ||= 1;
  820. $form->{ $form->{defaultcurrency} } = 1;
  821. }
  822. sub order_details {
  823. use LedgerSMB::CP;
  824. my ( $self, $myconfig, $form ) = @_;
  825. # connect to database
  826. my $dbh = $form->{dbh};
  827. my $query;
  828. my $sth;
  829. my $item;
  830. my $i;
  831. my @sortlist = ();
  832. my $projectnumber;
  833. my $projectdescription;
  834. my $projectnumber_id;
  835. my $translation;
  836. my $partsgroup;
  837. my @queryargs;
  838. my @taxaccounts;
  839. my %taxaccounts; # I don't think this works.
  840. my $tax;
  841. my $taxrate;
  842. my $taxamount;
  843. my %translations;
  844. my $language_code = $form->{dbh}->quote( $form->{language_code} );
  845. $query = qq|
  846. SELECT p.description, t.description
  847. FROM project p
  848. LEFT JOIN translation t ON (t.trans_id = p.id AND
  849. t.language_code = $language_code)
  850. WHERE id = ?|;
  851. my $prh = $dbh->prepare($query) || $form->dberror($query);
  852. $query = qq|
  853. SELECT inventory_accno_id, income_accno_id,
  854. expense_accno_id, assembly FROM parts
  855. WHERE id = ?|;
  856. my $pth = $dbh->prepare($query) || $form->dberror($query);
  857. my $sortby;
  858. # sort items by project and partsgroup
  859. for $i ( 1 .. $form->{rowcount} ) {
  860. if ( $form->{"id_$i"} ) {
  861. # account numbers
  862. $pth->execute( $form->{"id_$i"} );
  863. $ref = $pth->fetchrow_hashref(NAME_lc);
  864. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  865. $pth->finish;
  866. $projectnumber_id = 0;
  867. $projectnumber = "";
  868. $form->{partsgroup} = "";
  869. $form->{projectnumber} = "";
  870. if ( $form->{groupprojectnumber}
  871. || $form->{grouppartsgroup} )
  872. {
  873. $inventory_accno_id =
  874. ( $form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"} )
  875. ? "1"
  876. : "";
  877. if ( $form->{groupprojectnumber} ) {
  878. ( $projectnumber, $projectnumber_id ) =
  879. split /--/, $form->{"projectnumber_$i"};
  880. }
  881. if ( $form->{grouppartsgroup} ) {
  882. ( $form->{partsgroup} ) = split /--/,
  883. $form->{"partsgroup_$i"};
  884. }
  885. if ( $projectnumber_id
  886. && $form->{groupprojectnumber} )
  887. {
  888. if ( $translation{$projectnumber_id} ) {
  889. $form->{projectnumber} =
  890. $translation{$projectnumber_id};
  891. }
  892. else {
  893. # get project description
  894. $prh->execute($projectnumber_id);
  895. ( $projectdescription, $translation ) =
  896. $prh->fetchrow_array;
  897. $prh->finish;
  898. $form->{projectnumber} =
  899. ($translation)
  900. ? "$projectnumber, \n" . "$translation"
  901. : "$projectnumber, \n" . "$projectdescription";
  902. $translation{$projectnumber_id} =
  903. $form->{projectnumber};
  904. }
  905. }
  906. if ( $form->{grouppartsgroup}
  907. && $form->{partsgroup} )
  908. {
  909. $form->{projectnumber} .= " / "
  910. if $projectnumber_id;
  911. $form->{projectnumber} .= $form->{partsgroup};
  912. }
  913. $form->format_string(projectnumber);
  914. }
  915. $sortby = qq|$projectnumber$form->{partsgroup}|;
  916. if ( $form->{sortby} ne 'runningnumber' ) {
  917. for (qw(partnumber description bin)) {
  918. $sortby .= $form->{"${_}_$i"}
  919. if $form->{sortby} eq $_;
  920. }
  921. }
  922. push @sortlist,
  923. [
  924. $i,
  925. "$projectnumber$form->{partsgroup}" . "$inventory_accno_id",
  926. $form->{projectnumber},
  927. $projectnumber_id,
  928. $form->{partsgroup},
  929. $sortby
  930. ];
  931. }
  932. }
  933. delete $form->{projectnumber};
  934. # sort the whole thing by project and group
  935. @sortlist = sort { $a->[5] cmp $b->[5] } @sortlist;
  936. # if there is a warehouse limit picking
  937. if ( $form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/ ) {
  938. # run query to check for inventory
  939. $query = qq|
  940. SELECT sum(qty) AS qty FROM inventory
  941. WHERE parts_id = ? AND warehouse_id = ?|;
  942. $sth = $dbh->prepare($query) || $form->dberror($query);
  943. for $i ( 1 .. $form->{rowcount} ) {
  944. $sth->execute( $form->{"id_$i"}, $form->{warehouse_id} )
  945. || $form->dberror;
  946. my @qtylist = $sth->fetchrow_array;
  947. $form->db_parse_numeric(sth=>$sth, arrayref=>\@qtylist);
  948. ($qty) = @qtylist; $sth->fetchrow_array;
  949. $sth->finish;
  950. $form->{"qty_$i"} = 0 if $qty == 0;
  951. if ( $form->parse_amount( $myconfig, $form->{"ship_$i"} ) > $qty ) {
  952. $form->{"ship_$i"} = $form->format_amount( $myconfig, $qty );
  953. }
  954. }
  955. }
  956. my $runningnumber = 1;
  957. my $sameitem = "";
  958. my $subtotal;
  959. my $k = scalar @sortlist;
  960. my $j = 0;
  961. foreach $item (@sortlist) {
  962. $i = $item->[0];
  963. $j++;
  964. if ( $form->{groupprojectnumber} || $form->{grouppartsgroup} ) {
  965. if ( $item->[1] ne $sameitem ) {
  966. $sameitem = $item->[1];
  967. $ok = 0;
  968. if ( $form->{groupprojectnumber} ) {
  969. $ok = $form->{"projectnumber_$i"};
  970. }
  971. if ( $form->{grouppartsgroup} ) {
  972. $ok = $form->{"partsgroup_$i"}
  973. unless $ok;
  974. }
  975. if ($ok) {
  976. if ( $form->{"inventory_accno_id_$i"}
  977. || $form->{"assembly_$i"} )
  978. {
  979. push( @{ $form->{part} }, "" );
  980. push( @{ $form->{service} }, NULL );
  981. }
  982. else {
  983. push( @{ $form->{part} }, NULL );
  984. push( @{ $form->{service} }, "" );
  985. }
  986. push( @{ $form->{description} }, $item->[2] );
  987. for (
  988. qw(taxrates runningnumber
  989. number sku qty ship unit bin
  990. serialnumber requiredate
  991. projectnumber sellprice
  992. listprice netprice discount
  993. discountrate linetotal weight
  994. itemnotes)
  995. )
  996. {
  997. push( @{ $form->{$_} }, "" );
  998. }
  999. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  1000. }
  1001. }
  1002. }
  1003. $form->{"qty_$i"} = $form->parse_amount( $myconfig, $form->{"qty_$i"} );
  1004. $form->{"ship_$i"} =
  1005. $form->parse_amount( $myconfig, $form->{"ship_$i"} );
  1006. if ( $form->{"qty_$i"} ) {
  1007. $form->{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) {
  1676. if ($towarehouse_id) {
  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. JOIN warehouse w ON (w.id = i.warehouse_id)
  1689. WHERE i.warehouse_id = $fromwarehouse_id
  1690. $where
  1691. GROUP BY p.id, p.partnumber, p.description,
  1692. pg.partsgroup, w.description, i.warehouse_id
  1693. ORDER BY $sortorder|;
  1694. }
  1695. else {
  1696. if ($towarehouse_id) {
  1697. $query = qq|
  1698. SELECT p.id, p.partnumber, p.description,
  1699. p.onhand,
  1700. (SELECT SUM(qty)
  1701. FROM inventory i
  1702. WHERE i.parts_id = p.id) AS qty,
  1703. pg.partsgroup, '' AS warehouse,
  1704. 0 AS warehouse_id
  1705. FROM parts p
  1706. LEFT JOIN partsgroup pg
  1707. ON (p.partsgroup_id = pg.id)
  1708. WHERE p.onhand > 0
  1709. $where
  1710. UNION|;
  1711. }
  1712. $query .= qq|
  1713. SELECT p.id, p.partnumber, p.description,
  1714. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1715. pg.partsgroup, w.description AS warehouse,
  1716. i.warehouse_id
  1717. FROM inventory i
  1718. JOIN parts p ON (p.id = i.parts_id)
  1719. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1720. JOIN warehouse w ON (w.id = i.warehouse_id)
  1721. WHERE i.warehouse_id != $towarehouse_id
  1722. $where
  1723. GROUP BY p.id, p.partnumber, p.description,
  1724. pg.partsgroup, w.description, i.warehouse_id
  1725. ORDER BY $sortorder|;
  1726. }
  1727. my $sth = $dbh->prepare($query);
  1728. $sth->execute || $form->dberror($query);
  1729. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1730. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1731. $ref->{qty} = $ref->{onhand} - $ref->{qty};
  1732. push @{ $form->{all_inventory} }, $ref if $ref->{qty} > 0;
  1733. }
  1734. $sth->finish;
  1735. $dbh->commit;
  1736. }
  1737. sub transfer {
  1738. my ( $self, $myconfig, $form ) = @_;
  1739. my $dbh = $form->{dbh};
  1740. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  1741. my @a = localtime;
  1742. $a[5] += 1900;
  1743. $a[4]++;
  1744. $a[4] = substr( "0$a[4]", -2 );
  1745. $a[3] = substr( "0$a[3]", -2 );
  1746. $shippingdate = "$a[5]$a[4]$a[3]";
  1747. my %total = ();
  1748. my $query = qq|
  1749. INSERT INTO inventory
  1750. (warehouse_id, parts_id, qty, shippingdate, employee_id)
  1751. VALUES (?, ?, ?, ?, ?)|;
  1752. $sth = $dbh->prepare($query) || $form->dberror($query);
  1753. my $qty;
  1754. for my $i ( 1 .. $form->{rowcount} ) {
  1755. $qty = $form->parse_amount( $myconfig, $form->{"transfer_$i"} );
  1756. $qty = $form->{"qty_$i"} if ( $qty > $form->{"qty_$i"} );
  1757. if ( $qty > 0 ) {
  1758. # to warehouse
  1759. if ( $form->{warehouse_id} ) {
  1760. $sth->execute( $form->{warehouse_id}, $form->{"id_$i"}, $qty,
  1761. $shippingdate, $form->{employee_id} )
  1762. || $form->dberror;
  1763. $sth->finish;
  1764. }
  1765. # from warehouse
  1766. if ( $form->{"warehouse_id_$i"} ) {
  1767. $sth->execute( $form->{"warehouse_id_$i"},
  1768. $form->{"id_$i"}, $qty * -1, $shippingdate,
  1769. $form->{employee_id})
  1770. || $form->dberror;
  1771. $sth->finish;
  1772. }
  1773. }
  1774. }
  1775. my $rc = $dbh->commit;
  1776. $rc;
  1777. }
  1778. sub get_soparts {
  1779. my ( $self, $myconfig, $form ) = @_;
  1780. # connect to database
  1781. my $dbh = $form->{dbh};
  1782. my $id;
  1783. my $ref;
  1784. # store required items from selected sales orders
  1785. my $query = qq|
  1786. SELECT p.id, oi.qty - oi.ship AS required, p.assembly
  1787. FROM orderitems oi
  1788. JOIN parts p ON (p.id = oi.parts_id)
  1789. WHERE oi.trans_id = ?|;
  1790. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1791. for ( my $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1792. if ( $form->{"ndx_$i"} ) {
  1793. $sth->execute( $form->{"ndx_$i"} );
  1794. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1795. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1796. &add_items_required( "", $dbh, $form, $ref->{id},
  1797. $ref->{required}, $ref->{assembly} );
  1798. }
  1799. $sth->finish;
  1800. }
  1801. }
  1802. $query = qq|SELECT current_date|;
  1803. ( $form->{transdate} ) = $dbh->selectrow_array($query);
  1804. # foreign exchange rates
  1805. &exchangerate_defaults( $dbh, $form );
  1806. $dbh->commit;
  1807. }
  1808. sub add_items_required {
  1809. my ( $self, $dbh, $form, $parts_id, $required, $assembly ) = @_;
  1810. my $query;
  1811. my $sth;
  1812. my $ref;
  1813. if ($assembly) {
  1814. $query = qq|
  1815. SELECT p.id, a.qty, p.assembly
  1816. FROM assembly a
  1817. JOIN parts p ON (p.id = a.parts_id)
  1818. WHERE a.id = ?|;
  1819. $sth = $dbh->prepare($query);
  1820. $sth->execute($parts_id) || $form->dberror($query);
  1821. while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
  1822. $form->db_parse_numeric(sth=> $sth, hashref=> $ref);
  1823. &add_items_required( "", $dbh, $form, $ref->{id},
  1824. $required * $ref->{qty},
  1825. $ref->{assembly} );
  1826. }
  1827. $sth->finish;
  1828. }
  1829. else {
  1830. $query = qq|
  1831. SELECT partnumber, description, lastcost
  1832. FROM parts
  1833. WHERE id = ?|;
  1834. $sth = $dbh->prepare($query);
  1835. $sth->execute($parts_id) || $form->dberror($query);
  1836. $ref = $sth->fetchrow_hashref(NAME_lc);
  1837. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1838. for ( keys %$ref ) {
  1839. $form->{orderitems}{$parts_id}{$_} = $ref->{$_};
  1840. }
  1841. $sth->finish;
  1842. $form->{orderitems}{$parts_id}{required} += $required;
  1843. $query = qq|
  1844. SELECT pv.partnumber, pv.leadtime, pv.lastcost, pv.curr,
  1845. pv.vendor_id, v.name
  1846. FROM partsvendor pv
  1847. JOIN vendor v ON (v.id = pv.vendor_id)
  1848. WHERE pv.parts_id = ?|;
  1849. $sth = $dbh->prepare($query) || $form->dberror($query);
  1850. # get cost and vendor
  1851. $sth->execute($parts_id);
  1852. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1853. for ( keys %$ref ) {
  1854. $form->{orderitems}{$parts_id}{partsvendor}{ $ref->{vendor_id} }
  1855. {$_} = $ref->{$_};
  1856. }
  1857. }
  1858. $sth->finish;
  1859. }
  1860. }
  1861. sub generate_orders {
  1862. my ( $self, $myconfig, $form ) = @_;
  1863. my $i;
  1864. my %a;
  1865. my $query;
  1866. my $sth;
  1867. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1868. for (qw(qty lastcost)) {
  1869. $form->{"${_}_$i"} =
  1870. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  1871. }
  1872. if ( $form->{"qty_$i"} ) {
  1873. ( $vendor, $vendor_id ) =
  1874. split /--/, $form->{"vendor_$i"};
  1875. if ($vendor_id) {
  1876. $a{$vendor_id}{ $form->{"id_$i"} }{qty} += $form->{"qty_$i"};
  1877. for (qw(curr lastcost)) {
  1878. $a{$vendor_id}{ $form->{"id_$i"} }{$_} = $form->{"${_}_$i"};
  1879. }
  1880. }
  1881. }
  1882. }
  1883. # connect to database
  1884. my $dbh = $form->{dbh};
  1885. # foreign exchange rates
  1886. &exchangerate_defaults( $dbh, $form );
  1887. my $amount;
  1888. my $netamount;
  1889. my $curr = "";
  1890. my %tax;
  1891. my $taxincluded = 0;
  1892. my $vendor_id;
  1893. my $description;
  1894. my $unit;
  1895. my $sellprice;
  1896. foreach $vendor_id ( keys %a ) {
  1897. %tax = ();
  1898. my $ordnumber = $form->update_defaults( $myconfig, 'ponumber' );
  1899. $query = qq|
  1900. SELECT v.curr, v.taxincluded, t.rate, c.accno
  1901. FROM vendor v
  1902. LEFT JOIN vendortax vt ON (v.id = vt.vendor_id)
  1903. LEFT JOIN tax t ON (t.chart_id = vt.chart_id)
  1904. LEFT JOIN chart c ON (c.id = t.chart_id)
  1905. WHERE v.id = ?|;
  1906. $sth = $dbh->prepare($query);
  1907. $sth->execute($vendor_id) || $form->dberror($query);
  1908. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1909. $form->db_parse_numeric(sth=>$sth, hashref=> $ref);
  1910. $curr = $ref->{curr};
  1911. $taxincluded = $ref->{taxincluded};
  1912. $tax{ $ref->{accno} } = $ref->{rate};
  1913. }
  1914. $sth->finish;
  1915. $curr ||= $form->{defaultcurrency};
  1916. $taxincluded *= 1;
  1917. my $uid = localtime;
  1918. $uid .= "$$";
  1919. # TODO: Make this function insert as much as possible
  1920. $query = qq|
  1921. INSERT INTO oe (ordnumber)
  1922. VALUES ('$uid')|;
  1923. $dbh->do($query) || $form->dberror($query);
  1924. $query = qq|SELECT id FROM oe WHERE ordnumber = '$uid'|;
  1925. $sth = $dbh->prepare($query);
  1926. $sth->execute || $form->dberror($query);
  1927. my ($id) = $sth->fetchrow_array;
  1928. $sth->finish;
  1929. $amount = 0;
  1930. $netamount = 0;
  1931. foreach my $parts_id ( keys %{ $a{$vendor_id} } ) {
  1932. if ( ( $form->{$curr} * $form->{ $a{$vendor_id}{$parts_id}{curr} } )
  1933. > 0 )
  1934. {
  1935. $sellprice =
  1936. $a{$vendor_id}{$parts_id}{lastcost} / $form->{$curr} *
  1937. $form->{ $a{$vendor_id}{$parts_id}{curr} };
  1938. }
  1939. else {
  1940. $sellprice = $a{$vendor_id}{$parts_id}{lastcost};
  1941. }
  1942. $sellprice = $form->round_amount( $sellprice, 2 );
  1943. my $linetotal =
  1944. $form->round_amount( $sellprice * $a{$vendor_id}{$parts_id}{qty},
  1945. 2 );
  1946. $query = qq|
  1947. SELECT p.description, p.unit, c.accno
  1948. FROM parts p
  1949. LEFT JOIN partstax pt ON (p.id = pt.parts_id)
  1950. LEFT JOIN chart c ON (c.id = pt.chart_id)
  1951. WHERE p.id = ?|;
  1952. $sth = $dbh->prepare($query);
  1953. $sth->execute($parts_id) || $form->dberror($query);
  1954. my $rate = 0;
  1955. my $taxes = '';
  1956. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1957. $description = $ref->{description};
  1958. $unit = $ref->{unit};
  1959. $rate += $tax{ $ref->{accno} };
  1960. $taxes .= "$ref->{accno} ";
  1961. }
  1962. $sth->finish;
  1963. chop $taxes;
  1964. my @taxaccounts = Tax::init_taxes( $form, $taxes );
  1965. $netamount += $linetotal;
  1966. if ($taxincluded) {
  1967. $amount += $linetotal;
  1968. }
  1969. else {
  1970. $amount +=
  1971. $form->round_amount(
  1972. Tax::apply_taxes( \@taxaccounts, $form, $linetotal ), 2 );
  1973. }
  1974. $query = qq|
  1975. INSERT INTO orderitems
  1976. (trans_id, parts_id, description,
  1977. qty, ship, sellprice, unit)
  1978. VALUES
  1979. (?, ?, ?, ?, 0, ?, ?)|;
  1980. $sth = $dbh->prepare($query);
  1981. $sth->execute( $id, $parts_id, $description,
  1982. $a{$vendor_id}{$parts_id}{qty},
  1983. $sellprice, $unit )
  1984. || $form->dberror($query);
  1985. }
  1986. my $null;
  1987. my $employee_id;
  1988. my $department_id;
  1989. ( $null, $employee_id ) = $form->get_employee($dbh);
  1990. ( $null, $department_id ) = split /--/, $form->{department};
  1991. $department_id *= 1;
  1992. $query = qq|
  1993. UPDATE oe SET
  1994. ordnumber = ?,
  1995. transdate = current_date,
  1996. vendor_id = ?,
  1997. customer_id = 0,
  1998. amount = ?,
  1999. netamount = ?,
  2000. taxincluded = ?,
  2001. curr = ?,
  2002. employee_id = ?,
  2003. department_id = ?,
  2004. ponumber = ?
  2005. WHERE id = ?|;
  2006. $sth = $dbh->prepare($query);
  2007. $sth->execute(
  2008. $ordnumber, $vendor_id, $amount,
  2009. $netamount, $taxincluded, $curr,
  2010. $employee_id, $department_id, $form->{ponumber},
  2011. $id
  2012. ) || $form->dberror($query);
  2013. }
  2014. my $rc = $dbh->commit;
  2015. $rc;
  2016. }
  2017. sub consolidate_orders {
  2018. my ( $self, $myconfig, $form ) = @_;
  2019. # connect to database
  2020. my $dbh = $form->{dbh};
  2021. my $i;
  2022. my $id;
  2023. my $ref;
  2024. my %oe = ();
  2025. my $query = qq|SELECT * FROM oe WHERE id = ?|;
  2026. my $sth = $dbh->prepare($query) || $form->dberror($query);
  2027. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  2028. # retrieve order
  2029. if ( $form->{"ndx_$i"} ) {
  2030. $sth->execute( $form->{"ndx_$i"} );
  2031. $ref = $sth->fetchrow_hashref(NAME_lc);
  2032. $ref->{ndx} = $i;
  2033. $oe{oe}{ $ref->{curr} }{ $ref->{id} } = $ref;
  2034. $oe{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} }++;
  2035. $sth->finish;
  2036. }
  2037. }
  2038. $query = qq|SELECT * FROM orderitems WHERE trans_id = ?|;
  2039. $sth = $dbh->prepare($query) || $form->dberror($query);
  2040. foreach $curr ( keys %{ $oe{oe} } ) {
  2041. foreach $id (
  2042. sort { $oe{oe}{$curr}{$a}->{ndx} <=> $oe{oe}{$curr}{$b}->{ndx} }
  2043. keys %{ $oe{oe}{$curr} }
  2044. )
  2045. {
  2046. # retrieve order
  2047. $vc_id = $oe{oe}{$curr}{$id}->{"$form->{vc}_id"};
  2048. if ( $oe{vc}{ $oe{oe}{$curr}{$id}->{curr} }{$vc_id} > 1 ) {
  2049. push @{ $oe{orders}{$curr}{$vc_id} }, $id;
  2050. $sth->execute($id);
  2051. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  2052. push @{ $oe{orderitems}{$curr}{$id} }, $ref;
  2053. }
  2054. $sth->finish;
  2055. }
  2056. }
  2057. }
  2058. my $ordnumber = $form->{ordnumber};
  2059. my $numberfld = ( $form->{vc} eq 'customer' ) ? 'sonumber' : 'ponumber';
  2060. my ( $department, $department_id ) = $form->{department};
  2061. $department_id *= 1;
  2062. my $uid = localtime;
  2063. $uid .= "$$";
  2064. my @orderitems = ();
  2065. foreach $curr ( keys %{ $oe{orders} } ) {
  2066. foreach $vc_id ( sort { $a <=> $b } keys %{ $oe{orders}{$curr} } ) {
  2067. # the orders
  2068. @orderitems = ();
  2069. $form->{customer_id} = $form->{vendor_id} = 0;
  2070. $form->{"$form->{vc}_id"} = $vc_id;
  2071. $amount = 0;
  2072. $netamount = 0;
  2073. $ordnumber =
  2074. $form->update_defaults( $myconfig, $numberfld, $dbh );
  2075. my @orderids;
  2076. my $orderid_str = "";
  2077. foreach $id ( @{ $oe{orders}{$curr}{$vc_id} } ) {
  2078. push(@orderids, $id);
  2079. $orderid_str .= "?, ";
  2080. # header
  2081. $ref = $oe{oe}{$curr}{$id};
  2082. $amount += $ref->{amount};
  2083. $netamount += $ref->{netamount};
  2084. $id = $dbh->quote($id);
  2085. foreach $item ( @{ $oe{orderitems}{$curr}{$id} } ) {
  2086. push @orderitems, $item;
  2087. }
  2088. # close order
  2089. $query = qq|
  2090. UPDATE oe SET
  2091. closed = '1'
  2092. WHERE id = $id|;
  2093. $dbh->do($query) || $form->dberror($query);
  2094. # reset shipped
  2095. $query = qq|
  2096. UPDATE orderitems SET
  2097. ship = 0
  2098. WHERE trans_id = $id|;
  2099. $dbh->do($query) || $form->dberror($query);
  2100. }
  2101. #fixme: Change this
  2102. $query = qq|
  2103. INSERT INTO oe (ordnumber) VALUES ('$uid')|;
  2104. $dbh->do($query) || $form->dberror($query);
  2105. $query = qq|
  2106. SELECT id
  2107. FROM oe
  2108. WHERE ordnumber = '$uid'|;
  2109. ($id) = $dbh->selectrow_array($query);
  2110. $ref->{employee_id} *= 1;
  2111. $query = qq|
  2112. UPDATE oe SET
  2113. ordnumber = | . $dbh->quote($ordnumber) . qq|,
  2114. transdate = current_date,
  2115. vendor_id = $form->{vendor_id},
  2116. customer_id = $form->{customer_id},
  2117. amount = $amount,
  2118. netamount = $netamount,
  2119. reqdate = | . $form->dbquote( $ref->{reqdate}, SQL_DATE ) . qq|,
  2120. taxincluded = '$ref->{taxincluded}',
  2121. shippingpoint = | . $dbh->quote( $ref->{shippingpoint} ) . qq|,
  2122. notes = | . $dbh->quote( $ref->{notes} ) . qq|,
  2123. curr = '$curr',
  2124. employee_id = $ref->{employee_id},
  2125. intnotes = | . $dbh->quote( $ref->{intnotes} ) . qq|,
  2126. shipvia = | . $dbh->quote( $ref->{shipvia} ) . qq|,
  2127. language_code = '$ref->{language_code}',
  2128. ponumber = | . $dbh->quote( $form->{ponumber} ) . qq|,
  2129. department_id = $department_id
  2130. WHERE id = $id|;
  2131. $sth = $dbh->prepare($query);
  2132. $sth->execute() || $form->dberror($query);
  2133. $orderid_str =~ s/, $//;
  2134. # add items
  2135. $query = qq|
  2136. INSERT INTO orderitems
  2137. (trans_id, parts_id, description,
  2138. qty, sellprice, discount, unit, reqdate,
  2139. project_id, ship, serialnumber, notes)
  2140. SELECT ?, parts_id, description, qty,
  2141. sellprice, discount, unit, reqdate,
  2142. project_id, 0, description, qty
  2143. FROM orderitems
  2144. WHERE trans_id IN ($orderid_str)|;
  2145. $sth = $dbh->prepare($query);
  2146. $sth->execute($id, @orderids) || $form->dberror($query);
  2147. }
  2148. }
  2149. $rc = $dbh->commit;
  2150. $rc;
  2151. }
  2152. 1;