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