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