summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: af81da1c03f535735bb59c77740931fb1be8d608 (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 = $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 (?, ?, ?, '$shippingdate', $form->{employee_id})|;
  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 )
  1765. || $form->dberror;
  1766. $sth->finish;
  1767. }
  1768. }
  1769. }
  1770. my $rc = $dbh->commit;
  1771. $rc;
  1772. }
  1773. sub get_soparts {
  1774. my ( $self, $myconfig, $form ) = @_;
  1775. # connect to database
  1776. my $dbh = $form->{dbh};
  1777. my $id;
  1778. my $ref;
  1779. # store required items from selected sales orders
  1780. my $query = qq|
  1781. SELECT p.id, oi.qty - oi.ship AS required, p.assembly
  1782. FROM orderitems oi
  1783. JOIN parts p ON (p.id = oi.parts_id)
  1784. WHERE oi.trans_id = ?|;
  1785. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1786. for ( my $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1787. if ( $form->{"ndx_$i"} ) {
  1788. $sth->execute( $form->{"ndx_$i"} );
  1789. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1790. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1791. &add_items_required( "", $dbh, $form, $ref->{id},
  1792. $ref->{required}, $ref->{assembly} );
  1793. }
  1794. $sth->finish;
  1795. }
  1796. }
  1797. $query = qq|SELECT current_date|;
  1798. ( $form->{transdate} ) = $dbh->selectrow_array($query);
  1799. # foreign exchange rates
  1800. &exchangerate_defaults( $dbh, $form );
  1801. $dbh->commit;
  1802. }
  1803. sub add_items_required {
  1804. my ( $self, $dbh, $form, $parts_id, $required, $assembly ) = @_;
  1805. my $query;
  1806. my $sth;
  1807. my $ref;
  1808. if ($assembly) {
  1809. $query = qq|
  1810. SELECT p.id, a.qty, p.assembly
  1811. FROM assembly a
  1812. JOIN parts p ON (p.id = a.parts_id)
  1813. WHERE a.id = ?|;
  1814. $sth = $dbh->prepare($query);
  1815. $sth->execute || $form->dberror($query);
  1816. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1817. $form->db_parse_numeric(sth=> $sth, hashref=> $ref);
  1818. &add_items_required( "", $dbh, $form, $ref->{id},
  1819. $required * $ref->{qty},
  1820. $ref->{assembly} );
  1821. }
  1822. $sth->finish;
  1823. }
  1824. else {
  1825. $query = qq|
  1826. SELECT partnumber, description, lastcost
  1827. FROM parts
  1828. WHERE id = ?|;
  1829. $sth = $dbh->prepare($query);
  1830. $sth->execute($parts_id) || $form->dberror($query);
  1831. $ref = $sth->fetchrow_hashref(NAME_lc);
  1832. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1833. for ( keys %$ref ) {
  1834. $form->{orderitems}{$parts_id}{$_} = $ref->{$_};
  1835. }
  1836. $sth->finish;
  1837. $form->{orderitems}{$parts_id}{required} += $required;
  1838. $query = qq|
  1839. SELECT pv.partnumber, pv.leadtime, pv.lastcost, pv.curr,
  1840. pv.vendor_id, v.name
  1841. FROM partsvendor pv
  1842. JOIN vendor v ON (v.id = pv.vendor_id)
  1843. WHERE pv.parts_id = ?|;
  1844. $sth = $dbh->prepare($query) || $form->dberror($query);
  1845. # get cost and vendor
  1846. $sth->execute($parts_id);
  1847. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1848. for ( keys %$ref ) {
  1849. $form->{orderitems}{$parts_id}{partsvendor}{ $ref->{vendor_id} }
  1850. {$_} = $ref->{$_};
  1851. }
  1852. }
  1853. $sth->finish;
  1854. }
  1855. }
  1856. sub generate_orders {
  1857. my ( $self, $myconfig, $form ) = @_;
  1858. my $i;
  1859. my %a;
  1860. my $query;
  1861. my $sth;
  1862. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1863. for (qw(qty lastcost)) {
  1864. $form->{"${_}_$i"} =
  1865. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  1866. }
  1867. if ( $form->{"qty_$i"} ) {
  1868. ( $vendor, $vendor_id ) =
  1869. split /--/, $form->{"vendor_$i"};
  1870. if ($vendor_id) {
  1871. $a{$vendor_id}{ $form->{"id_$i"} }{qty} += $form->{"qty_$i"};
  1872. for (qw(curr lastcost)) {
  1873. $a{$vendor_id}{ $form->{"id_$i"} }{$_} = $form->{"${_}_$i"};
  1874. }
  1875. }
  1876. }
  1877. }
  1878. # connect to database
  1879. my $dbh = $form->{dbh};
  1880. # foreign exchange rates
  1881. &exchangerate_defaults( $dbh, $form );
  1882. my $amount;
  1883. my $netamount;
  1884. my $curr = "";
  1885. my %tax;
  1886. my $taxincluded = 0;
  1887. my $vendor_id;
  1888. my $description;
  1889. my $unit;
  1890. my $sellprice;
  1891. foreach $vendor_id ( keys %a ) {
  1892. %tax = ();
  1893. my $ordnumber = $form->update_defaults( $myconfig, 'ponumber' );
  1894. $query = qq|
  1895. SELECT v.curr, v.taxincluded, t.rate, c.accno
  1896. FROM vendor v
  1897. LEFT JOIN vendortax vt ON (v.id = vt.vendor_id)
  1898. LEFT JOIN tax t ON (t.chart_id = vt.chart_id)
  1899. LEFT JOIN chart c ON (c.id = t.chart_id)
  1900. WHERE v.id = ?|;
  1901. $sth = $dbh->prepare($query);
  1902. $sth->execute($vendor_id) || $form->dberror($query);
  1903. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1904. $form->db_parse_numeric(sth=>$sth, hashref=> $ref);
  1905. $curr = $ref->{curr};
  1906. $taxincluded = $ref->{taxincluded};
  1907. $tax{ $ref->{accno} } = $ref->{rate};
  1908. }
  1909. $sth->finish;
  1910. $curr ||= $form->{defaultcurrency};
  1911. $taxincluded *= 1;
  1912. my $uid = localtime;
  1913. $uid .= "$$";
  1914. # TODO: Make this function insert as much as possible
  1915. $query = qq|
  1916. INSERT INTO oe (ordnumber)
  1917. VALUES ('$uid')|;
  1918. $dbh->do($query) || $form->dberror($query);
  1919. $query = qq|SELECT id FROM oe WHERE ordnumber = '$uid'|;
  1920. $sth = $dbh->prepare($query);
  1921. $sth->execute || $form->dberror($query);
  1922. my ($id) = $sth->fetchrow_array;
  1923. $sth->finish;
  1924. $amount = 0;
  1925. $netamount = 0;
  1926. foreach my $parts_id ( keys %{ $a{$vendor_id} } ) {
  1927. if ( ( $form->{$curr} * $form->{ $a{$vendor_id}{$parts_id}{curr} } )
  1928. > 0 )
  1929. {
  1930. $sellprice =
  1931. $a{$vendor_id}{$parts_id}{lastcost} / $form->{$curr} *
  1932. $form->{ $a{$vendor_id}{$parts_id}{curr} };
  1933. }
  1934. else {
  1935. $sellprice = $a{$vendor_id}{$parts_id}{lastcost};
  1936. }
  1937. $sellprice = $form->round_amount( $sellprice, 2 );
  1938. my $linetotal =
  1939. $form->round_amount( $sellprice * $a{$vendor_id}{$parts_id}{qty},
  1940. 2 );
  1941. $query = qq|
  1942. SELECT p.description, p.unit, c.accno
  1943. FROM parts p
  1944. LEFT JOIN partstax pt ON (p.id = pt.parts_id)
  1945. LEFT JOIN chart c ON (c.id = pt.chart_id)
  1946. WHERE p.id = ?|;
  1947. $sth = $dbh->prepare($query);
  1948. $sth->execute($parts_id) || $form->dberror($query);
  1949. my $rate = 0;
  1950. my $taxes = '';
  1951. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1952. $description = $ref->{description};
  1953. $unit = $ref->{unit};
  1954. $rate += $tax{ $ref->{accno} };
  1955. $taxes .= "$ref->{accno} ";
  1956. }
  1957. $sth->finish;
  1958. chop $taxes;
  1959. my @taxaccounts = Tax::init_taxes( $form, $taxes );
  1960. $netamount += $linetotal;
  1961. if ($taxincluded) {
  1962. $amount += $linetotal;
  1963. }
  1964. else {
  1965. $amount +=
  1966. $form->round_amount(
  1967. Tax::apply_taxes( \@taxaccounts, $form, $linetotal ), 2 );
  1968. }
  1969. $query = qq|
  1970. INSERT INTO orderitems
  1971. (trans_id, parts_id, description,
  1972. qty, ship, sellprice, unit)
  1973. VALUES
  1974. (?, ?, ?, ?, 0, ?, ?)|;
  1975. $sth = $dbh->prepare($query);
  1976. $sth->execute( $id, $parts_id, $description,
  1977. $a{vendor_id}{parts_id}{qty},
  1978. $sellprice, $unit )
  1979. || $form->dberror($query);
  1980. }
  1981. my $null;
  1982. my $employee_id;
  1983. my $department_id;
  1984. ( $null, $employee_id ) = $form->get_employee($dbh);
  1985. ( $null, $department_id ) = split /--/, $form->{department};
  1986. $department_id *= 1;
  1987. $query = qq|
  1988. UPDATE oe SET
  1989. ordnumber = ?,
  1990. transdate = current_date,
  1991. vendor_id = ?,
  1992. customer_id = 0,
  1993. amount = ?,
  1994. netamount = ?,
  1995. taxincluded = ?,
  1996. curr = ?,
  1997. employee_id = ?,
  1998. department_id = ?,
  1999. ponumber = ?
  2000. WHERE id = ?|;
  2001. $sth = $dbh->prepare($query);
  2002. $sth->execute(
  2003. $ordnumber, $vendor_id, $amount,
  2004. $netamount, $taxincluded, $curr,
  2005. $employee_id, $department_id, $form->{ponumber},
  2006. $id
  2007. ) || $form->dberror($query);
  2008. }
  2009. my $rc = $dbh->commit;
  2010. $rc;
  2011. }
  2012. sub consolidate_orders {
  2013. my ( $self, $myconfig, $form ) = @_;
  2014. # connect to database
  2015. my $dbh = $form->{dbh};
  2016. my $i;
  2017. my $id;
  2018. my $ref;
  2019. my %oe = ();
  2020. my $query = qq|SELECT * FROM oe WHERE id = ?|;
  2021. my $sth = $dbh->prepare($query) || $form->dberror($query);
  2022. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  2023. # retrieve order
  2024. if ( $form->{"ndx_$i"} ) {
  2025. $sth->execute( $form->{"ndx_$i"} );
  2026. $ref = $sth->fetchrow_hashref(NAME_lc);
  2027. $ref->{ndx} = $i;
  2028. $oe{oe}{ $ref->{curr} }{ $ref->{id} } = $ref;
  2029. $oe{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} }++;
  2030. $sth->finish;
  2031. }
  2032. }
  2033. $query = qq|SELECT * FROM orderitems WHERE trans_id = ?|;
  2034. $sth = $dbh->prepare($query) || $form->dberror($query);
  2035. foreach $curr ( keys %{ $oe{oe} } ) {
  2036. foreach $id (
  2037. sort { $oe{oe}{$curr}{$a}->{ndx} <=> $oe{oe}{$curr}{$b}->{ndx} }
  2038. keys %{ $oe{oe}{$curr} }
  2039. )
  2040. {
  2041. # retrieve order
  2042. $vc_id = $oe{oe}{$curr}{$id}->{"$form->{vc}_id"};
  2043. if ( $oe{vc}{ $oe{oe}{$curr}{$id}->{curr} }{$vc_id} > 1 ) {
  2044. push @{ $oe{orders}{$curr}{$vc_id} }, $id;
  2045. $sth->execute($id);
  2046. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  2047. push @{ $oe{orderitems}{$curr}{$id} }, $ref;
  2048. }
  2049. $sth->finish;
  2050. }
  2051. }
  2052. }
  2053. my $ordnumber = $form->{ordnumber};
  2054. my $numberfld = ( $form->{vc} eq 'customer' ) ? 'sonumber' : 'ponumber';
  2055. my ( $department, $department_id ) = $form->{department};
  2056. $department_id *= 1;
  2057. my $uid = localtime;
  2058. $uid .= "$$";
  2059. my @orderitems = ();
  2060. foreach $curr ( keys %{ $oe{orders} } ) {
  2061. foreach $vc_id ( sort { $a <=> $b } keys %{ $oe{orders}{$curr} } ) {
  2062. # the orders
  2063. @orderitems = ();
  2064. $form->{customer_id} = $form->{vendor_id} = 0;
  2065. $form->{"$form->{vc}_id"} = $vc_id;
  2066. $amount = 0;
  2067. $netamount = 0;
  2068. $ordnumber =
  2069. $form->update_defaults( $myconfig, $numberfld, $dbh );
  2070. my @orderids;
  2071. my $orderid_str = "";
  2072. foreach $id ( @{ $oe{orders}{$curr}{$vc_id} } ) {
  2073. push(@orderids, $id);
  2074. $orderid_str .= "?, ";
  2075. # header
  2076. $ref = $oe{oe}{$curr}{$id};
  2077. $amount += $ref->{amount};
  2078. $netamount += $ref->{netamount};
  2079. $id = $dbh->quote($id);
  2080. foreach $item ( @{ $oe{orderitems}{$curr}{$id} } ) {
  2081. push @orderitems, $item;
  2082. }
  2083. # close order
  2084. $query = qq|
  2085. UPDATE oe SET
  2086. closed = '1'
  2087. WHERE id = $id|;
  2088. $dbh->do($query) || $form->dberror($query);
  2089. # reset shipped
  2090. $query = qq|
  2091. UPDATE orderitems SET
  2092. ship = 0
  2093. WHERE trans_id = $id|;
  2094. $dbh->do($query) || $form->dberror($query);
  2095. }
  2096. #fixme: Change this
  2097. $query = qq|
  2098. INSERT INTO oe (ordnumber) VALUES ('$uid')|;
  2099. $dbh->do($query) || $form->dberror($query);
  2100. $query = qq|
  2101. SELECT id
  2102. FROM oe
  2103. WHERE ordnumber = '$uid'|;
  2104. ($id) = $dbh->selectrow_array($query);
  2105. $ref->{employee_id} *= 1;
  2106. $query = qq|
  2107. UPDATE oe SET
  2108. ordnumber = | . $dbh->quote($ordnumber) . qq|,
  2109. transdate = current_date,
  2110. vendor_id = $form->{vendor_id},
  2111. customer_id = $form->{customer_id},
  2112. amount = $amount,
  2113. netamount = $netamount,
  2114. reqdate = | . $form->dbquote( $ref->{reqdate}, SQL_DATE ) . qq|,
  2115. taxincluded = '$ref->{taxincluded}',
  2116. shippingpoint = | . $dbh->quote( $ref->{shippingpoint} ) . qq|,
  2117. notes = | . $dbh->quote( $ref->{notes} ) . qq|,
  2118. curr = '$curr',
  2119. employee_id = $ref->{employee_id},
  2120. intnotes = | . $dbh->quote( $ref->{intnotes} ) . qq|,
  2121. shipvia = | . $dbh->quote( $ref->{shipvia} ) . qq|,
  2122. language_code = '$ref->{language_code}',
  2123. ponumber = | . $dbh->quote( $form->{ponumber} ) . qq|,
  2124. department_id = $department_id
  2125. WHERE id = $id|;
  2126. $sth = $dbh->prepare($query);
  2127. $sth->execute() || $form->dberror($query);
  2128. $orderid_str =~ s/, $//;
  2129. # add items
  2130. $query = qq|
  2131. INSERT INTO orderitems
  2132. (trans_id, parts_id, description,
  2133. qty, sellprice, discount, unit, reqdate,
  2134. project_id, ship, serialnumber, notes)
  2135. SELECT ?, parts_id, description, qty,
  2136. sellprice, discount, unit, reqdate,
  2137. project_id, 0, description, qty
  2138. FROM orderitems
  2139. WHERE trans_id IN ($orderid_str)|;
  2140. $sth = $dbh->prepare($query);
  2141. $sth->execute($id, @orderids) || $form->dberror($query);
  2142. }
  2143. }
  2144. $rc = $dbh->commit;
  2145. $rc;
  2146. }
  2147. 1;