summaryrefslogtreecommitdiff
path: root/LedgerSMB/OE.pm
blob: e274cc03d5254d99e4ae4b3cdc5240570d76d201 (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->{totalqty} += $form->{"qty_$i"};
  1009. $form->{totalship} += $form->{"ship_$i"};
  1010. $form->{totalweight} +=
  1011. ( $form->{"weight_$i"} * $form->{"qty_$i"} );
  1012. $form->{totalweightship} +=
  1013. ( $form->{"weight_$i"} * $form->{"ship_$i"} );
  1014. # add number, description and qty to $form->{number}
  1015. push( @{ $form->{runningnumber} }, $runningnumber++ );
  1016. push( @{ $form->{number} }, qq|$form->{"partnumber_$i"}| );
  1017. push( @{ $form->{sku} }, qq|$form->{"sku_$i"}| );
  1018. push( @{ $form->{description} }, qq|$form->{"description_$i"}| );
  1019. push( @{ $form->{itemnotes} }, $form->{"notes_$i"} );
  1020. push(
  1021. @{ $form->{qty} },
  1022. $form->format_amount( $myconfig, $form->{"qty_$i"} )
  1023. );
  1024. push(
  1025. @{ $form->{ship} },
  1026. $form->format_amount( $myconfig, $form->{"ship_$i"} )
  1027. );
  1028. push( @{ $form->{unit} }, qq|$form->{"unit_$i"}| );
  1029. push( @{ $form->{bin} }, qq|$form->{"bin_$i"}| );
  1030. push( @{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}| );
  1031. push( @{ $form->{requiredate} }, qq|$form->{"reqdate_$i"}| );
  1032. push( @{ $form->{projectnumber} },
  1033. qq|$form->{"projectnumber_$i"}| );
  1034. push( @{ $form->{sellprice} }, $form->{"sellprice_$i"} );
  1035. push( @{ $form->{listprice} }, $form->{"listprice_$i"} );
  1036. push(
  1037. @{ $form->{weight} },
  1038. $form->format_amount(
  1039. $myconfig, $form->{"weight_$i"} * $form->{"ship_$i"}
  1040. )
  1041. );
  1042. my $sellprice =
  1043. $form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
  1044. my ($dec) = ( $sellprice =~ /\.(\d+)/ );
  1045. $dec = length $dec;
  1046. my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  1047. my $discount = $form->round_amount(
  1048. $sellprice *
  1049. $form->parse_amount( $myconfig, $form->{"discount_$i"} ) /
  1050. 100,
  1051. $decimalplaces
  1052. );
  1053. # keep a netprice as well, (sellprice - discount)
  1054. $form->{"netprice_$i"} = $sellprice - $discount;
  1055. my $linetotal =
  1056. $form->round_amount( $form->{"qty_$i"} * $form->{"netprice_$i"},
  1057. 2 );
  1058. if ( $form->{"inventory_accno_id_$i"}
  1059. || $form->{"assembly_$i"} )
  1060. {
  1061. push( @{ $form->{part} }, $form->{"sku_$i"} );
  1062. push( @{ $form->{service} }, NULL );
  1063. $form->{totalparts} += $linetotal;
  1064. }
  1065. else {
  1066. push( @{ $form->{service} }, $form->{"sku_$i"} );
  1067. push( @{ $form->{part} }, NULL );
  1068. $form->{totalservices} += $linetotal;
  1069. }
  1070. push(
  1071. @{ $form->{netprice} },
  1072. ( $form->{"netprice_$i"} )
  1073. ? $form->format_amount( $myconfig, $form->{"netprice_$i"},
  1074. $decimalplaces )
  1075. : " "
  1076. );
  1077. $discount =
  1078. ($discount)
  1079. ? $form->format_amount( $myconfig, $discount * -1,
  1080. $decimalplaces )
  1081. : " ";
  1082. push( @{ $form->{discount} }, $discount );
  1083. push(
  1084. @{ $form->{discountrate} },
  1085. $form->format_amount( $myconfig, $form->{"discount_$i"} )
  1086. );
  1087. $form->{ordtotal} += $linetotal;
  1088. # this is for the subtotals for grouping
  1089. $subtotal += $linetotal;
  1090. $form->{"linetotal_$i"} =
  1091. $form->format_amount( $myconfig, $linetotal, 2 );
  1092. push( @{ $form->{linetotal} }, $form->{"linetotal_$i"} );
  1093. @taxaccounts = Tax::init_taxes( $form, $form->{"taxaccounts_$i"} );
  1094. my $ml = 1;
  1095. my @taxrates = ();
  1096. $tax = 0;
  1097. $taxamount =
  1098. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  1099. $taxbase = Tax::extract_taxes( \@taxaccounts, $form, $linetotal );
  1100. foreach $item (@taxaccounts) {
  1101. push @taxrates, Math::BigFloat->new(100) * $item->rate;
  1102. if ( $form->{taxincluded} ) {
  1103. $taxaccounts{ $item->account } += $item->value;
  1104. $taxbase{ $item->account } += $taxbase;
  1105. }
  1106. else {
  1107. Tax::apply_taxes( \@taxaccounts, $form, $linetotal );
  1108. $taxbase{ $item->account } += $linetotal;
  1109. $taxaccounts{ $item->account } += $item->value;
  1110. }
  1111. }
  1112. if ( $form->{taxincluded} ) {
  1113. $tax +=
  1114. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  1115. }
  1116. else {
  1117. $tax +=
  1118. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  1119. }
  1120. push(
  1121. @{ $form->{lineitems} },
  1122. {
  1123. amount => $linetotal,
  1124. tax => $form->round_amount( $tax, 2 )
  1125. }
  1126. );
  1127. push( @{ $form->{taxrates} },
  1128. join ' ', sort { $a <=> $b } @taxrates );
  1129. if ( $form->{"assembly_$i"} ) {
  1130. $form->{stagger} = -1;
  1131. &assembly_details( $myconfig, $form, $dbh, $form->{"id_$i"},
  1132. $oid{ $myconfig->{dbdriver} },
  1133. $form->{"qty_$i"} );
  1134. }
  1135. }
  1136. # add subtotal
  1137. if ( $form->{groupprojectnumber} || $form->{grouppartsgroup} ) {
  1138. if ($subtotal) {
  1139. if ( $j < $k ) {
  1140. # look at next item
  1141. if ( $sortlist[$j]->[1] ne $sameitem ) {
  1142. if ( $form->{"inventory_accno_id_$i"}
  1143. || $form->{"assembly_$i"} )
  1144. {
  1145. push( @{ $form->{part} }, "" );
  1146. push( @{ $form->{service} }, NULL );
  1147. }
  1148. else {
  1149. push( @{ $form->{service} }, "" );
  1150. push( @{ $form->{part} }, NULL );
  1151. }
  1152. for (
  1153. qw(
  1154. taxrates runningnumber
  1155. number sku qty ship unit
  1156. bin serialnumber
  1157. requiredate
  1158. projectnumber sellprice
  1159. listprice netprice
  1160. discount discountrate
  1161. weight itemnotes)
  1162. )
  1163. {
  1164. push( @{ $form->{$_} }, "" );
  1165. }
  1166. push(
  1167. @{ $form->{description} },
  1168. $form->{groupsubtotaldescription}
  1169. );
  1170. push(
  1171. @{ $form->{lineitems} },
  1172. {
  1173. amount => 0,
  1174. tax => 0
  1175. }
  1176. );
  1177. if ( $form->{groupsubtotaldescription} ne "" ) {
  1178. push(
  1179. @{ $form->{linetotal} },
  1180. $form->format_amount( $myconfig, $subtotal, 2 )
  1181. );
  1182. }
  1183. else {
  1184. push( @{ $form->{linetotal} }, "" );
  1185. }
  1186. $subtotal = 0;
  1187. }
  1188. }
  1189. else {
  1190. # got last item
  1191. if ( $form->{groupsubtotaldescription} ne "" ) {
  1192. if ( $form->{"inventory_accno_id_$i"}
  1193. || $form->{"assembly_$i"} )
  1194. {
  1195. push( @{ $form->{part} }, "" );
  1196. push( @{ $form->{service} }, NULL );
  1197. }
  1198. else {
  1199. push( @{ $form->{service} }, "" );
  1200. push( @{ $form->{part} }, NULL );
  1201. }
  1202. for (
  1203. qw(
  1204. taxrates runningnumber
  1205. number sku qty ship unit
  1206. bin serialnumber
  1207. requiredate
  1208. projectnumber sellprice
  1209. listprice netprice
  1210. discount discountrate
  1211. weight itemnotes)
  1212. )
  1213. {
  1214. push( @{ $form->{$_} }, "" );
  1215. }
  1216. push(
  1217. @{ $form->{description} },
  1218. $form->{groupsubtotaldescription}
  1219. );
  1220. push(
  1221. @{ $form->{linetotal} },
  1222. $form->format_amount( $myconfig, $subtotal, 2 )
  1223. );
  1224. push(
  1225. @{ $form->{lineitems} },
  1226. {
  1227. amount => 0,
  1228. tax => 0
  1229. }
  1230. );
  1231. }
  1232. }
  1233. }
  1234. }
  1235. }
  1236. $tax = 0;
  1237. foreach $item ( sort keys %taxaccounts ) {
  1238. if ( $form->round_amount( $taxaccounts{$item}, 2 ) ) {
  1239. $tax += $taxamount = $form->round_amount( $taxaccounts{$item}, 2 );
  1240. push(
  1241. @{ $form->{taxbaseinclusive} },
  1242. $form->{"${item}_taxbaseinclusive"} =
  1243. $form->round_amount( $taxbase{$item} + $tax, 2 )
  1244. );
  1245. push(
  1246. @{ $form->{taxbase} },
  1247. $form->{"${item}_taxbase"} =
  1248. $form->format_amount( $myconfig, $taxbase{$item}, 2 )
  1249. );
  1250. push(
  1251. @{ $form->{tax} },
  1252. $form->{"${item}_tax"} =
  1253. $form->format_amount( $myconfig, $taxamount, 2 )
  1254. );
  1255. push( @{ $form->{taxdescription} },
  1256. $form->{"${item}_description"} );
  1257. $form->{"${item}_taxrate"} =
  1258. $form->format_amount( $myconfig, $form->{"${item}_rate"} * 100 );
  1259. push( @{ $form->{taxrate} }, $form->{"${item}_taxrate"} );
  1260. push( @{ $form->{taxnumber} }, $form->{"${item}_taxnumber"} );
  1261. }
  1262. }
  1263. # adjust taxes for lineitems
  1264. my $total = 0;
  1265. for ( @{ $form->{lineitems} } ) {
  1266. $total += $_->{tax};
  1267. }
  1268. if ( $form->round_amount( $total, 2 ) != $form->round_amount( $tax, 2 ) ) {
  1269. # get largest amount
  1270. for ( reverse sort { $a->{tax} <=> $b->{tax} } @{ $form->{lineitems} } )
  1271. {
  1272. $_->{tax} -= $total - $tax;
  1273. last;
  1274. }
  1275. }
  1276. $i = 1;
  1277. for ( @{ $form->{lineitems} } ) {
  1278. push(
  1279. @{ $form->{linetax} },
  1280. $form->format_amount( $myconfig, $_->{tax}, 2, "" )
  1281. );
  1282. }
  1283. for (qw(totalparts totalservices)) {
  1284. $form->{$_} = $form->format_amount( $myconfig, $form->{$_}, 2 );
  1285. }
  1286. for (qw(totalqty totalship totalweight)) {
  1287. $form->{$_} = $form->format_amount( $myconfig, $form->{$_} );
  1288. }
  1289. $form->{subtotal} = $form->format_amount( $myconfig, $form->{ordtotal}, 2 );
  1290. $form->{ordtotal} =
  1291. ( $form->{taxincluded} )
  1292. ? $form->{ordtotal}
  1293. : $form->{ordtotal} + $tax;
  1294. my $c;
  1295. if ( $form->{language_code} ne "" ) {
  1296. $c = new CP $form->{language_code};
  1297. }
  1298. else {
  1299. $c = new CP $myconfig->{countrycode};
  1300. }
  1301. $c->init;
  1302. my $whole;
  1303. ( $whole, $form->{decimal} ) = split /\./, $form->{ordtotal};
  1304. $form->{decimal} .= "00";
  1305. $form->{decimal} = substr( $form->{decimal}, 0, 2 );
  1306. $form->{text_decimal} = $c->num2text( $form->{decimal} * 1 );
  1307. $form->{text_amount} = $c->num2text($whole);
  1308. $form->{integer_amount} = $form->format_amount( $myconfig, $whole );
  1309. # format amounts
  1310. $form->{quototal} = $form->{ordtotal} =
  1311. $form->format_amount( $myconfig, $form->{ordtotal}, 2 );
  1312. $form->format_string(qw(text_amount text_decimal));
  1313. $query = qq|
  1314. SELECT value FROM defaults
  1315. WHERE setting_key = 'weightunit'|;
  1316. ( $form->{weightunit} ) = $dbh->selectrow_array($query);
  1317. $dbh->commit;
  1318. }
  1319. sub assembly_details {
  1320. my ( $myconfig, $form, $dbh, $id, $oid, $qty ) = @_;
  1321. my $sm = "";
  1322. my $spacer;
  1323. $form->{stagger}++;
  1324. if ( $form->{format} eq 'html' ) {
  1325. $spacer = "&nbsp;" x ( 3 * ( $form->{stagger} - 1 ) )
  1326. if $form->{stagger} > 1;
  1327. }
  1328. if ( $form->{format} =~ /(postscript|pdf)/ ) {
  1329. if ( $form->{stagger} > 1 ) {
  1330. $spacer = ( $form->{stagger} - 1 ) * 3;
  1331. $spacer = '\rule{' . $spacer . 'mm}{0mm}';
  1332. }
  1333. }
  1334. # get parts and push them onto the stack
  1335. my $sortorder = "";
  1336. if ( $form->{grouppartsgroup} ) {
  1337. $sortorder = qq|ORDER BY pg.partsgroup, a.id|;
  1338. }
  1339. else {
  1340. $sortorder = qq|ORDER BY a.id|;
  1341. }
  1342. my $where =
  1343. ( $form->{formname} eq 'work_order' )
  1344. ? "1 = 1"
  1345. : "a.bom = '1'";
  1346. my $query = qq|
  1347. SELECT p.partnumber, p.description, p.unit, a.qty,
  1348. pg.partsgroup, p.partnumber AS sku, p.assembly, p.id,
  1349. p.bin
  1350. FROM assembly a
  1351. JOIN parts p ON (a.parts_id = p.id)
  1352. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1353. WHERE $where
  1354. AND a.id = ?
  1355. $sortorder|;
  1356. my $sth = $dbh->prepare($query);
  1357. $sth->execute($id) || $form->dberror($query);
  1358. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1359. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1360. for (qw(partnumber description partsgroup)) {
  1361. $form->{"a_$_"} = $ref->{$_};
  1362. $form->format_string("a_$_");
  1363. }
  1364. if ( $form->{grouppartsgroup} && $ref->{partsgroup} ne $sm ) {
  1365. for (
  1366. qw(
  1367. taxrates number sku unit qty runningnumber ship
  1368. bin serialnumber requiredate projectnumber
  1369. sellprice listprice netprice discount
  1370. discountrate linetotal weight itemnotes)
  1371. )
  1372. {
  1373. push( @{ $form->{$_} }, "" );
  1374. }
  1375. $sm = ( $form->{"a_partsgroup"} ) ? $form->{"a_partsgroup"} : "";
  1376. push( @{ $form->{description} }, "$spacer$sm" );
  1377. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  1378. }
  1379. if ( $form->{stagger} ) {
  1380. push(
  1381. @{ $form->{description} },
  1382. qq|$spacer$form->{"a_partnumber"}, |
  1383. . qq|$form->{"a_description"}|
  1384. );
  1385. for (
  1386. qw(
  1387. taxrates number sku runningnumber ship
  1388. serialnumber requiredate projectnumber
  1389. sellprice listprice netprice discount
  1390. discountrate linetotal weight itemnotes)
  1391. )
  1392. {
  1393. push( @{ $form->{$_} }, "" );
  1394. }
  1395. }
  1396. else {
  1397. push( @{ $form->{description} }, qq|$form->{"a_description"}| );
  1398. push( @{ $form->{sku} }, $form->{"a_partnumber"} );
  1399. push( @{ $form->{number} }, $form->{"a_partnumber"} );
  1400. for (
  1401. qw(
  1402. taxrates runningnumber ship serialnumber
  1403. requiredate projectnumber sellprice listprice
  1404. netprice discount discountrate linetotal weight
  1405. itemnotes)
  1406. )
  1407. {
  1408. push( @{ $form->{$_} }, "" );
  1409. }
  1410. }
  1411. push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
  1412. push(
  1413. @{ $form->{qty} },
  1414. $form->format_amount( $myconfig, $ref->{qty} * $qty )
  1415. );
  1416. for (qw(unit bin)) {
  1417. $form->{"a_$_"} = $ref->{$_};
  1418. $form->format_string("a_$_");
  1419. push( @{ $form->{$_} }, $form->{"a_$_"} );
  1420. }
  1421. if ( $ref->{assembly} && $form->{formname} eq 'work_order' ) {
  1422. &assembly_details( $myconfig, $form, $dbh, $ref->{id}, $oid,
  1423. $ref->{qty} * $qty );
  1424. }
  1425. }
  1426. $sth->finish;
  1427. $form->{stagger}--;
  1428. }
  1429. sub project_description {
  1430. my ( $self, $dbh, $id ) = @_;
  1431. my $query = qq|
  1432. SELECT description
  1433. FROM project
  1434. WHERE id = $id|;
  1435. ($_) = $dbh->selectrow_array($query);
  1436. $_;
  1437. }
  1438. sub get_warehouses {
  1439. my ( $self, $myconfig, $form ) = @_;
  1440. my $dbh = $form->{dbh};
  1441. # setup warehouses
  1442. my $query = qq|
  1443. SELECT id, description
  1444. FROM warehouse
  1445. ORDER BY 2|;
  1446. my $sth = $dbh->prepare($query);
  1447. $sth->execute || $form->dberror($query);
  1448. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1449. push @{ $form->{all_warehouse} }, $ref;
  1450. }
  1451. $sth->finish;
  1452. }
  1453. sub save_inventory {
  1454. my ( $self, $myconfig, $form ) = @_;
  1455. my ( $null, $warehouse_id ) = split /--/, $form->{warehouse};
  1456. $warehouse_id *= 1;
  1457. my $ml = ( $form->{type} eq 'ship_order' ) ? -1 : 1;
  1458. my $dbh = $form->{dbh};
  1459. my $sth;
  1460. my $wth;
  1461. my $serialnumber;
  1462. my $ship;
  1463. my ( $null, $employee_id ) = split /--/, $form->{employee};
  1464. ( $null, $employee_id ) = $form->get_employee($dbh) if !$employee_id;
  1465. $query = qq|
  1466. SELECT serialnumber, ship
  1467. FROM orderitems
  1468. WHERE trans_id = ?
  1469. AND id = ?
  1470. FOR UPDATE|;
  1471. $sth = $dbh->prepare($query) || $form->dberror($query);
  1472. $query = qq|
  1473. SELECT sum(qty)
  1474. FROM inventory
  1475. WHERE parts_id = ?
  1476. AND warehouse_id = ?|;
  1477. $wth = $dbh->prepare($query) || $form->dberror($query);
  1478. for my $i ( 1 .. $form->{rowcount} ) {
  1479. $form->{"ship_$i"} = 0 unless $form->{"ship_$i"};
  1480. $ship =
  1481. ( abs( $form->{"ship_$i"} ) > abs( $form->{"qty_$i"} ) )
  1482. ? $form->{"qty_$i"}
  1483. : $form->{"ship_$i"};
  1484. if ( $warehouse_id && $form->{type} eq 'ship_order' ) {
  1485. $wth->execute( $form->{"id_$i"}, $warehouse_id )
  1486. || $form->dberror;
  1487. @qtylist = $wth->fetchrow_array;
  1488. $form->db_parse_numeric(sth=>$wth, arrayref=>\@qtylist);
  1489. ($qty) = @qtylist;
  1490. $wth->finish;
  1491. if ( $ship > $qty ) {
  1492. $ship = $qty;
  1493. }
  1494. }
  1495. if ($ship) {
  1496. if ( !$form->{shippingdate} ) {
  1497. $form->{shippingdate} = undef;
  1498. }
  1499. $ship *= $ml;
  1500. $query = qq|
  1501. INSERT INTO inventory
  1502. (parts_id, warehouse_id, qty, trans_id,
  1503. orderitems_id, shippingdate,
  1504. employee_id)
  1505. VALUES
  1506. (?, ?, ?, ?, ?, ?, ?)|;
  1507. $sth2 = $dbh->prepare($query);
  1508. $sth2->execute( $form->{"id_$i"}, $warehouse_id, $ship,
  1509. $form->{"id"}, $form->{"orderitems_id_$i"},
  1510. $form->{shippingdate}, $employee_id )
  1511. || $form->dberror($query);
  1512. $sth2->finish;
  1513. # add serialnumber, ship to orderitems
  1514. $sth->execute( $form->{id}, $form->{"orderitems_id_$i"} )
  1515. || $form->dberror;
  1516. ( $serialnumber, $ship ) = $sth->fetchrow_array;
  1517. $sth->finish;
  1518. $serialnumber .= " " if $serialnumber;
  1519. $serialnumber .= qq|$form->{"serialnumber_$i"}|;
  1520. $ship += $form->{"ship_$i"};
  1521. $query = qq|
  1522. UPDATE orderitems SET
  1523. serialnumber = ?,
  1524. ship = ?,
  1525. reqdate = ?
  1526. WHERE trans_id = ?
  1527. AND id = ?|;
  1528. $sth2 = $dbh->prepare($query);
  1529. $sth2->execute( $serialnumber, $ship, $form->{shippingdate},
  1530. $form->{id}, $form->{"orderitems_id_$i"} )
  1531. || $form->dberror($query);
  1532. $sth2->finish;
  1533. # update order with ship via
  1534. $query = qq|
  1535. UPDATE oe SET
  1536. shippingpoint = ?,
  1537. shipvia = ?
  1538. WHERE id = ?|;
  1539. $sth2 = $dbh->prepare($query);
  1540. $sth2->execute( $form->{shippingpoint},
  1541. $form->{shipvia}, $form->{id} )
  1542. || $form->dberror($query);
  1543. $sth2->finish;
  1544. # update onhand for parts
  1545. $form->update_balance(
  1546. $dbh, "parts", "onhand",
  1547. qq|id = $form->{"id_$i"}|,
  1548. $form->{"ship_$i"} * $ml
  1549. );
  1550. }
  1551. }
  1552. my $rc = $dbh->commit;
  1553. $rc;
  1554. }
  1555. sub adj_onhand {
  1556. my ( $dbh, $form, $ml ) = @_;
  1557. my $query = qq|
  1558. SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
  1559. FROM orderitems oi
  1560. JOIN parts p ON (p.id = oi.parts_id)
  1561. WHERE oi.trans_id = ?|;
  1562. my $sth = $dbh->prepare($query);
  1563. $sth->execute( $form->{id} ) || $form->dberror($query);
  1564. $query = qq|
  1565. SELECT sum(p.inventory_accno_id), p.assembly
  1566. FROM parts p
  1567. JOIN assembly a ON (a.parts_id = p.id)
  1568. WHERE a.id = ?
  1569. GROUP BY p.assembly|;
  1570. my $ath = $dbh->prepare($query) || $form->dberror($query);
  1571. my $ref;
  1572. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1573. if ( $ref->{inventory_accno_id} || $ref->{assembly} ) {
  1574. # do not update if assembly consists of all services
  1575. if ( $ref->{assembly} ) {
  1576. $ath->execute( $ref->{parts_id} )
  1577. || $form->dberror($query);
  1578. my ( $inv, $assembly ) = $ath->fetchrow_array;
  1579. $ath->finish;
  1580. next unless ( $inv || $assembly );
  1581. }
  1582. # adjust onhand in parts table
  1583. $form->update_balance(
  1584. $dbh, "parts", "onhand",
  1585. qq|id = $ref->{parts_id}|,
  1586. $ref->{ship} * $ml
  1587. );
  1588. }
  1589. }
  1590. $sth->finish;
  1591. }
  1592. sub adj_inventory {
  1593. my ( $dbh, $myconfig, $form ) = @_;
  1594. # increase/reduce qty in inventory table
  1595. my $query = qq|
  1596. SELECT oi.id, oi.parts_id, oi.ship
  1597. FROM orderitems oi
  1598. WHERE oi.trans_id = ?|;
  1599. my $sth = $dbh->prepare($query);
  1600. $sth->execute( $form->{id} ) || $form->dberror($query);
  1601. my $id = $dbh->quote( $form->{id} );
  1602. $query = qq|
  1603. SELECT qty,
  1604. (SELECT SUM(qty) FROM inventory
  1605. WHERE trans_id = $id
  1606. AND orderitems_id = ?) AS total
  1607. FROM inventory
  1608. WHERE trans_id = $id
  1609. AND orderitems_id = ?|;
  1610. my $ith = $dbh->prepare($query) || $form->dberror($query);
  1611. my $qty;
  1612. my $ml = ( $form->{type} =~ /(ship|sales)_order/ ) ? -1 : 1;
  1613. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1614. $ith->execute( $ref->{id}, $ref->{id} ) || $form->dberror($query);
  1615. my $ship = $ref->{ship};
  1616. while ( my $inv = $ith->fetchrow_hashref(NAME_lc) ) {
  1617. if ( ( $qty = ( ( $inv->{total} * $ml ) - $ship ) ) >= 0 ) {
  1618. $qty = $inv->{qty} * $ml
  1619. if ( $qty > ( $inv->{qty} * $ml ) );
  1620. $form->update_balance(
  1621. $dbh, "inventory", "qty",
  1622. qq|$oid{$myconfig->{dbdriver}} | . qq|= $inv->{oid}|,
  1623. $qty * -1 * $ml
  1624. );
  1625. $ship -= $qty;
  1626. }
  1627. }
  1628. $ith->finish;
  1629. }
  1630. $sth->finish;
  1631. # delete inventory entries if qty = 0
  1632. $query = qq|
  1633. DELETE FROM inventory
  1634. WHERE trans_id = ?
  1635. AND qty = 0|;
  1636. $sth = $dbh->prepare($query);
  1637. $sth->execute( $form->{id} ) || $form->dberror($query);
  1638. }
  1639. sub get_inventory {
  1640. my ( $self, $myconfig, $form ) = @_;
  1641. my $where;
  1642. my $query;
  1643. my $null;
  1644. my $fromwarehouse_id;
  1645. my $towarehouse_id;
  1646. my $var;
  1647. my $dbh = $form->{dbh};
  1648. if ( $form->{partnumber} ne "" ) {
  1649. $var = $dbh->quote( $form->like( lc $form->{partnumber} ) );
  1650. $where .= "
  1651. AND lower(p.partnumber) LIKE $var";
  1652. }
  1653. if ( $form->{description} ne "" ) {
  1654. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  1655. $where .= "
  1656. AND lower(p.description) LIKE $var";
  1657. }
  1658. if ( $form->{partsgroup} ne "" ) {
  1659. ( $null, $var ) = split /--/, $form->{partsgroup};
  1660. $var = $dbh->quote($var);
  1661. $where .= "
  1662. AND pg.id = $var";
  1663. }
  1664. ( $null, $fromwarehouse_id ) = split /--/, $form->{fromwarehouse};
  1665. $fromwarehouse_id = $dbh->quote($fromwarehouse_id);
  1666. ( $null, $towarehouse_id ) = split /--/, $form->{towarehouse};
  1667. $towarehouse_id = $dbh->quote($towarehouse_id);
  1668. my %ordinal = (
  1669. partnumber => 2,
  1670. description => 3,
  1671. partsgroup => 5,
  1672. warehouse => 6,
  1673. );
  1674. my @a = ( partnumber, warehouse );
  1675. my $sortorder = $form->sort_order( \@a, \%ordinal );
  1676. if ($fromwarehouse_id) {
  1677. if ($towarehouse_id) {
  1678. $where .= "
  1679. AND NOT i.warehouse_id = $towarehouse_id";
  1680. }
  1681. $query = qq|
  1682. SELECT p.id, p.partnumber, p.description,
  1683. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1684. pg.partsgroup, w.description AS warehouse,
  1685. i.warehouse_id
  1686. FROM inventory i
  1687. JOIN parts p ON (p.id = i.parts_id)
  1688. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1689. JOIN warehouse w ON (w.id = i.warehouse_id)
  1690. WHERE i.warehouse_id = $fromwarehouse_id
  1691. $where
  1692. GROUP BY p.id, p.partnumber, p.description,
  1693. pg.partsgroup, w.description, i.warehouse_id
  1694. ORDER BY $sortorder|;
  1695. }
  1696. else {
  1697. if ($towarehouse_id) {
  1698. $query = qq|
  1699. SELECT p.id, p.partnumber, p.description,
  1700. p.onhand,
  1701. (SELECT SUM(qty)
  1702. FROM inventory i
  1703. WHERE i.parts_id = p.id) AS qty,
  1704. pg.partsgroup, '' AS warehouse,
  1705. 0 AS warehouse_id
  1706. FROM parts p
  1707. LEFT JOIN partsgroup pg
  1708. ON (p.partsgroup_id = pg.id)
  1709. WHERE p.onhand > 0
  1710. $where
  1711. UNION|;
  1712. }
  1713. $query .= qq|
  1714. SELECT p.id, p.partnumber, p.description,
  1715. sum(i.qty) * 2 AS onhand, sum(i.qty) AS qty,
  1716. pg.partsgroup, w.description AS warehouse,
  1717. i.warehouse_id
  1718. FROM inventory i
  1719. JOIN parts p ON (p.id = i.parts_id)
  1720. LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
  1721. JOIN warehouse w ON (w.id = i.warehouse_id)
  1722. WHERE i.warehouse_id != $towarehouse_id
  1723. $where
  1724. GROUP BY p.id, p.partnumber, p.description,
  1725. pg.partsgroup, w.description, i.warehouse_id
  1726. ORDER BY $sortorder|;
  1727. }
  1728. my $sth = $dbh->prepare($query);
  1729. $sth->execute || $form->dberror($query);
  1730. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1731. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1732. $ref->{qty} = $ref->{onhand} - $ref->{qty};
  1733. push @{ $form->{all_inventory} }, $ref if $ref->{qty} > 0;
  1734. }
  1735. $sth->finish;
  1736. $dbh->commit;
  1737. }
  1738. sub transfer {
  1739. my ( $self, $myconfig, $form ) = @_;
  1740. my $dbh = $form->{dbh};
  1741. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  1742. my @a = localtime;
  1743. $a[5] += 1900;
  1744. $a[4]++;
  1745. $a[4] = substr( "0$a[4]", -2 );
  1746. $a[3] = substr( "0$a[3]", -2 );
  1747. $shippingdate = "$a[5]$a[4]$a[3]";
  1748. my %total = ();
  1749. my $query = qq|
  1750. INSERT INTO inventory
  1751. (warehouse_id, parts_id, qty, shippingdate, employee_id)
  1752. VALUES (?, ?, ?, ?, ?)|;
  1753. $sth = $dbh->prepare($query) || $form->dberror($query);
  1754. my $qty;
  1755. for my $i ( 1 .. $form->{rowcount} ) {
  1756. $qty = $form->parse_amount( $myconfig, $form->{"transfer_$i"} );
  1757. $qty = $form->{"qty_$i"} if ( $qty > $form->{"qty_$i"} );
  1758. if ( $qty > 0 ) {
  1759. # to warehouse
  1760. if ( $form->{warehouse_id} ) {
  1761. $sth->execute( $form->{warehouse_id}, $form->{"id_$i"}, $qty,
  1762. $shippingdate, $form->{employee_id} )
  1763. || $form->dberror;
  1764. $sth->finish;
  1765. }
  1766. # from warehouse
  1767. if ( $form->{"warehouse_id_$i"} ) {
  1768. $sth->execute( $form->{"warehouse_id_$i"},
  1769. $form->{"id_$i"}, $qty * -1, $shippingdate,
  1770. $form->{employee_id})
  1771. || $form->dberror;
  1772. $sth->finish;
  1773. }
  1774. }
  1775. }
  1776. my $rc = $dbh->commit;
  1777. $rc;
  1778. }
  1779. sub get_soparts {
  1780. my ( $self, $myconfig, $form ) = @_;
  1781. # connect to database
  1782. my $dbh = $form->{dbh};
  1783. my $id;
  1784. my $ref;
  1785. # store required items from selected sales orders
  1786. my $query = qq|
  1787. SELECT p.id, oi.qty - oi.ship AS required, p.assembly
  1788. FROM orderitems oi
  1789. JOIN parts p ON (p.id = oi.parts_id)
  1790. WHERE oi.trans_id = ?|;
  1791. my $sth = $dbh->prepare($query) || $form->dberror($query);
  1792. for ( my $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1793. if ( $form->{"ndx_$i"} ) {
  1794. $sth->execute( $form->{"ndx_$i"} );
  1795. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1796. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1797. &add_items_required( "", $dbh, $form, $ref->{id},
  1798. $ref->{required}, $ref->{assembly} );
  1799. }
  1800. $sth->finish;
  1801. }
  1802. }
  1803. $query = qq|SELECT current_date|;
  1804. ( $form->{transdate} ) = $dbh->selectrow_array($query);
  1805. # foreign exchange rates
  1806. &exchangerate_defaults( $dbh, $form );
  1807. $dbh->commit;
  1808. }
  1809. sub add_items_required {
  1810. my ( $self, $dbh, $form, $parts_id, $required, $assembly ) = @_;
  1811. my $query;
  1812. my $sth;
  1813. my $ref;
  1814. if ($assembly) {
  1815. $query = qq|
  1816. SELECT p.id, a.qty, p.assembly
  1817. FROM assembly a
  1818. JOIN parts p ON (p.id = a.parts_id)
  1819. WHERE a.id = ?|;
  1820. $sth = $dbh->prepare($query);
  1821. $sth->execute($parts_id) || $form->dberror($query);
  1822. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1823. $form->db_parse_numeric(sth=> $sth, hashref=> $ref);
  1824. &add_items_required( "", $dbh, $form, $ref->{id},
  1825. $required * $ref->{qty},
  1826. $ref->{assembly} );
  1827. }
  1828. $sth->finish;
  1829. }
  1830. else {
  1831. $query = qq|
  1832. SELECT partnumber, description, lastcost
  1833. FROM parts
  1834. WHERE id = ?|;
  1835. $sth = $dbh->prepare($query);
  1836. $sth->execute($parts_id) || $form->dberror($query);
  1837. $ref = $sth->fetchrow_hashref(NAME_lc);
  1838. $form->db_parse_numeric(sth=>$sth, hashref=>$ref);
  1839. for ( keys %$ref ) {
  1840. $form->{orderitems}{$parts_id}{$_} = $ref->{$_};
  1841. }
  1842. $sth->finish;
  1843. $form->{orderitems}{$parts_id}{required} += $required;
  1844. $query = qq|
  1845. SELECT pv.partnumber, pv.leadtime, pv.lastcost, pv.curr,
  1846. pv.vendor_id, v.name
  1847. FROM partsvendor pv
  1848. JOIN vendor v ON (v.id = pv.vendor_id)
  1849. WHERE pv.parts_id = ?|;
  1850. $sth = $dbh->prepare($query) || $form->dberror($query);
  1851. # get cost and vendor
  1852. $sth->execute($parts_id);
  1853. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1854. for ( keys %$ref ) {
  1855. $form->{orderitems}{$parts_id}{partsvendor}{ $ref->{vendor_id} }
  1856. {$_} = $ref->{$_};
  1857. }
  1858. }
  1859. $sth->finish;
  1860. }
  1861. }
  1862. sub generate_orders {
  1863. my ( $self, $myconfig, $form ) = @_;
  1864. my $i;
  1865. my %a;
  1866. my $query;
  1867. my $sth;
  1868. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  1869. for (qw(qty lastcost)) {
  1870. $form->{"${_}_$i"} =
  1871. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  1872. }
  1873. if ( $form->{"qty_$i"} ) {
  1874. ( $vendor, $vendor_id ) =
  1875. split /--/, $form->{"vendor_$i"};
  1876. if ($vendor_id) {
  1877. $a{$vendor_id}{ $form->{"id_$i"} }{qty} += $form->{"qty_$i"};
  1878. for (qw(curr lastcost)) {
  1879. $a{$vendor_id}{ $form->{"id_$i"} }{$_} = $form->{"${_}_$i"};
  1880. }
  1881. }
  1882. }
  1883. }
  1884. # connect to database
  1885. my $dbh = $form->{dbh};
  1886. # foreign exchange rates
  1887. &exchangerate_defaults( $dbh, $form );
  1888. my $amount;
  1889. my $netamount;
  1890. my $curr = "";
  1891. my %tax;
  1892. my $taxincluded = 0;
  1893. my $vendor_id;
  1894. my $description;
  1895. my $unit;
  1896. my $sellprice;
  1897. foreach $vendor_id ( keys %a ) {
  1898. %tax = ();
  1899. $query = qq|
  1900. SELECT v.curr, v.taxincluded, t.rate, c.accno
  1901. FROM vendor v
  1902. LEFT JOIN vendortax vt ON (v.id = vt.vendor_id)
  1903. LEFT JOIN tax t ON (t.chart_id = vt.chart_id)
  1904. LEFT JOIN chart c ON (c.id = t.chart_id)
  1905. WHERE v.id = ?|;
  1906. $sth = $dbh->prepare($query);
  1907. $sth->execute($vendor_id) || $form->dberror($query);
  1908. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1909. $form->db_parse_numeric(sth=>$sth, hashref=> $ref);
  1910. $curr = $ref->{curr};
  1911. $taxincluded = $ref->{taxincluded};
  1912. $tax{ $ref->{accno} } = $ref->{rate};
  1913. }
  1914. $sth->finish;
  1915. $curr ||= $form->{defaultcurrency};
  1916. $taxincluded *= 1;
  1917. my $uid = localtime;
  1918. $uid .= "$$";
  1919. # TODO: Make this function insert as much as possible
  1920. $query = qq|
  1921. INSERT INTO oe (ordnumber)
  1922. VALUES ('$uid')|;
  1923. $dbh->do($query) || $form->dberror($query);
  1924. $query = qq|SELECT id FROM oe WHERE ordnumber = '$uid'|;
  1925. $sth = $dbh->prepare($query);
  1926. $sth->execute || $form->dberror($query);
  1927. my ($id) = $sth->fetchrow_array;
  1928. $sth->finish;
  1929. $amount = 0;
  1930. $netamount = 0;
  1931. foreach my $parts_id ( keys %{ $a{$vendor_id} } ) {
  1932. if ( ( $form->{$curr} * $form->{ $a{$vendor_id}{$parts_id}{curr} } )
  1933. > 0 )
  1934. {
  1935. $sellprice =
  1936. $a{$vendor_id}{$parts_id}{lastcost} / $form->{$curr} *
  1937. $form->{ $a{$vendor_id}{$parts_id}{curr} };
  1938. }
  1939. else {
  1940. $sellprice = $a{$vendor_id}{$parts_id}{lastcost};
  1941. }
  1942. $sellprice = $form->round_amount( $sellprice, 2 );
  1943. my $linetotal =
  1944. $form->round_amount( $sellprice * $a{$vendor_id}{$parts_id}{qty},
  1945. 2 );
  1946. $query = qq|
  1947. SELECT p.description, p.unit, c.accno
  1948. FROM parts p
  1949. LEFT JOIN partstax pt ON (p.id = pt.parts_id)
  1950. LEFT JOIN chart c ON (c.id = pt.chart_id)
  1951. WHERE p.id = ?|;
  1952. $sth = $dbh->prepare($query);
  1953. $sth->execute($parts_id) || $form->dberror($query);
  1954. my $rate = 0;
  1955. my $taxes = '';
  1956. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  1957. $description = $ref->{description};
  1958. $unit = $ref->{unit};
  1959. $rate += $tax{ $ref->{accno} };
  1960. $taxes .= "$ref->{accno} ";
  1961. }
  1962. $sth->finish;
  1963. chop $taxes;
  1964. my @taxaccounts = Tax::init_taxes( $form, $taxes );
  1965. $netamount += $linetotal;
  1966. if ($taxincluded) {
  1967. $amount += $linetotal;
  1968. }
  1969. else {
  1970. $amount +=
  1971. $form->round_amount(
  1972. Tax::apply_taxes( \@taxaccounts, $form, $linetotal ), 2 );
  1973. }
  1974. $query = qq|
  1975. INSERT INTO orderitems
  1976. (trans_id, parts_id, description,
  1977. qty, ship, sellprice, unit)
  1978. VALUES
  1979. (?, ?, ?, ?, 0, ?, ?)|;
  1980. $sth = $dbh->prepare($query);
  1981. $sth->execute( $id, $parts_id, $description,
  1982. $a{$vendor_id}{$parts_id}{qty},
  1983. $sellprice, $unit )
  1984. || $form->dberror($query);
  1985. }
  1986. my $ordnumber = $form->update_defaults( $myconfig, 'ponumber' );
  1987. my $null;
  1988. my $employee_id;
  1989. my $department_id;
  1990. ( $null, $employee_id ) = $form->get_employee($dbh);
  1991. ( $null, $department_id ) = split /--/, $form->{department};
  1992. $department_id *= 1;
  1993. $query = qq|
  1994. UPDATE oe SET
  1995. ordnumber = ?,
  1996. transdate = current_date,
  1997. entity_id = ?
  1998. amount = ?,
  1999. netamount = ?,
  2000. taxincluded = ?,
  2001. curr = ?,
  2002. employee_id = ?,
  2003. department_id = ?,
  2004. ponumber = ?
  2005. WHERE id = ?|;
  2006. $sth = $dbh->prepare($query);
  2007. $sth->execute(
  2008. $ordnumber, $vendor_id, $amount,
  2009. $netamount, $taxincluded, $curr,
  2010. $employee_id, $department_id, $form->{ponumber},
  2011. $id
  2012. ) || $form->dberror($query);
  2013. }
  2014. my $rc = $dbh->commit;
  2015. $rc;
  2016. }
  2017. sub consolidate_orders {
  2018. my ( $self, $myconfig, $form ) = @_;
  2019. # connect to database
  2020. my $dbh = $form->{dbh};
  2021. my $i;
  2022. my $id;
  2023. my $ref;
  2024. my %oe = ();
  2025. my $query = qq|SELECT * FROM oe WHERE id = ?|;
  2026. my $sth = $dbh->prepare($query) || $form->dberror($query);
  2027. for ( $i = 1 ; $i <= $form->{rowcount} ; $i++ ) {
  2028. # retrieve order
  2029. if ( $form->{"ndx_$i"} ) {
  2030. $sth->execute( $form->{"ndx_$i"} );
  2031. $ref = $sth->fetchrow_hashref(NAME_lc);
  2032. $ref->{ndx} = $i;
  2033. $oe{oe}{ $ref->{curr} }{ $ref->{id} } = $ref;
  2034. $oe{vc}{ $ref->{curr} }{ $ref->{"$form->{vc}_id"} }++;
  2035. $sth->finish;
  2036. }
  2037. }
  2038. $query = qq|SELECT * FROM orderitems WHERE trans_id = ?|;
  2039. $sth = $dbh->prepare($query) || $form->dberror($query);
  2040. foreach $curr ( keys %{ $oe{oe} } ) {
  2041. foreach $id (
  2042. sort { $oe{oe}{$curr}{$a}->{ndx} <=> $oe{oe}{$curr}{$b}->{ndx} }
  2043. keys %{ $oe{oe}{$curr} }
  2044. )
  2045. {
  2046. # retrieve order
  2047. $vc_id = $oe{oe}{$curr}{$id}->{"$form->{vc}_id"};
  2048. if ( $oe{vc}{ $oe{oe}{$curr}{$id}->{curr} }{$vc_id} > 1 ) {
  2049. push @{ $oe{orders}{$curr}{$vc_id} }, $id;
  2050. $sth->execute($id);
  2051. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  2052. push @{ $oe{orderitems}{$curr}{$id} }, $ref;
  2053. }
  2054. $sth->finish;
  2055. }
  2056. }
  2057. }
  2058. my $ordnumber = $form->{ordnumber};
  2059. my $numberfld = ( $form->{vc} eq 'customer' ) ? 'sonumber' : 'ponumber';
  2060. my ( $department, $department_id ) = $form->{department};
  2061. $department_id *= 1;
  2062. my $uid = localtime;
  2063. $uid .= "$$";
  2064. my @orderitems = ();
  2065. foreach $curr ( keys %{ $oe{orders} } ) {
  2066. foreach $vc_id ( sort { $a <=> $b } keys %{ $oe{orders}{$curr} } ) {
  2067. # the orders
  2068. @orderitems = ();
  2069. $form->{entity_id} = $vc_id;
  2070. $amount = 0;
  2071. $netamount = 0;
  2072. my @orderids;
  2073. my $orderid_str = "";
  2074. foreach $id ( @{ $oe{orders}{$curr}{$vc_id} } ) {
  2075. push(@orderids, $id);
  2076. $orderid_str .= "?, ";
  2077. # header
  2078. $ref = $oe{oe}{$curr}{$id};
  2079. $amount += $ref->{amount};
  2080. $netamount += $ref->{netamount};
  2081. $id = $dbh->quote($id);
  2082. foreach $item ( @{ $oe{orderitems}{$curr}{$id} } ) {
  2083. push @orderitems, $item;
  2084. }
  2085. # close order
  2086. $query = qq|
  2087. UPDATE oe SET
  2088. closed = '1'
  2089. WHERE id = $id|;
  2090. $dbh->do($query) || $form->dberror($query);
  2091. # reset shipped
  2092. $query = qq|
  2093. UPDATE orderitems SET
  2094. ship = 0
  2095. WHERE trans_id = $id|;
  2096. $dbh->do($query) || $form->dberror($query);
  2097. }
  2098. $ordnumber ||=
  2099. $form->update_defaults( $myconfig, $numberfld, $dbh );
  2100. #fixme: Change this
  2101. $query = qq|
  2102. INSERT INTO oe (ordnumber) VALUES ('$uid')|;
  2103. $dbh->do($query) || $form->dberror($query);
  2104. $query = qq|
  2105. SELECT id
  2106. FROM oe
  2107. WHERE ordnumber = '$uid'|;
  2108. ($id) = $dbh->selectrow_array($query);
  2109. $ref->{employee_id} *= 1;
  2110. $query = qq|
  2111. UPDATE oe SET
  2112. ordnumber = | . $dbh->quote($ordnumber) . qq|,
  2113. transdate = current_date,
  2114. entity_id = | .
  2115. $dbh->quote($form->{entity_id}).qq|,
  2116. amount = $amount,
  2117. netamount = $netamount,
  2118. reqdate = | . $form->dbquote( $ref->{reqdate}, SQL_DATE ) . qq|,
  2119. taxincluded = '$ref->{taxincluded}',
  2120. shippingpoint = | . $dbh->quote( $ref->{shippingpoint} ) . qq|,
  2121. notes = | . $dbh->quote( $ref->{notes} ) . qq|,
  2122. curr = '$curr',
  2123. employee_id = $ref->{employee_id},
  2124. intnotes = | . $dbh->quote( $ref->{intnotes} ) . qq|,
  2125. shipvia = | . $dbh->quote( $ref->{shipvia} ) . qq|,
  2126. language_code = '$ref->{language_code}',
  2127. ponumber = | . $dbh->quote( $form->{ponumber} ) . qq|,
  2128. department_id = $department_id
  2129. WHERE id = $id|;
  2130. $sth = $dbh->prepare($query);
  2131. $sth->execute() || $form->dberror($query);
  2132. $orderid_str =~ s/, $//;
  2133. # add items
  2134. $query = qq|
  2135. INSERT INTO orderitems
  2136. (trans_id, parts_id, description,
  2137. qty, sellprice, discount, unit, reqdate,
  2138. project_id, ship, serialnumber, notes)
  2139. SELECT ?, parts_id, description,
  2140. qty, sellprice, discount, unit, reqdate,
  2141. project_id, ship, serialnumber, notes
  2142. FROM orderitems
  2143. WHERE trans_id IN ($orderid_str)|;
  2144. $sth = $dbh->prepare($query);
  2145. $sth->execute($id, @orderids) || $form->dberror($query);
  2146. }
  2147. }
  2148. $rc = $dbh->commit;
  2149. $rc;
  2150. }
  2151. 1;