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