summaryrefslogtreecommitdiff
path: root/LedgerSMB/CT.pm
blob: b51f48bf22a2c32b71738203d0219adc61badf32 (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) 2000
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #
  25. #======================================================================
  26. #
  27. # This file has undergone whitespace cleanup.
  28. #
  29. #======================================================================
  30. #
  31. # backend code for customers and vendors
  32. #
  33. #======================================================================
  34. package CT;
  35. sub create_links {
  36. my ( $self, $myconfig, $form ) = @_;
  37. my $dbh = $form->{dbh};
  38. my $query;
  39. my $sth;
  40. my $ref;
  41. my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
  42. my $ARAP = uc $arap;
  43. if ( $form->{id} ) {
  44. $query = qq|
  45. SELECT ct.*, b.description AS business, s.*,
  46. e.name AS employee,
  47. g.pricegroup AS pricegroup,
  48. l.description AS language, ct.curr
  49. FROM $form->{db} ct
  50. LEFT JOIN business b ON (ct.business_id = b.id)
  51. LEFT JOIN shipto s ON (ct.id = s.trans_id)
  52. LEFT JOIN employee e ON (ct.employee_id = e.id)
  53. LEFT JOIN pricegroup g ON (g.id = ct.pricegroup_id)
  54. LEFT JOIN language l ON (l.code = ct.language_code)
  55. WHERE ct.id = ?|;
  56. $sth = $dbh->prepare($query);
  57. $sth->execute( $form->{id} ) || $form->dberror($query);
  58. $ref = $sth->fetchrow_hashref(NAME_lc);
  59. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  60. $sth->finish;
  61. # check if it is orphaned
  62. $query = qq|
  63. SELECT a.id
  64. FROM $arap a
  65. JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
  66. WHERE ct.id = ?
  67. UNION
  68. SELECT a.id
  69. FROM oe a
  70. JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
  71. WHERE ct.id = ?|;
  72. $sth = $dbh->prepare($query);
  73. $sth->execute( $form->{id}, $form->{id} )
  74. || $form->dberror($query);
  75. unless ( $sth->fetchrow_array ) {
  76. $form->{status} = "orphaned";
  77. }
  78. $sth->finish;
  79. # get taxes for customer/vendor
  80. $query = qq|
  81. SELECT c.accno
  82. FROM chart c
  83. JOIN $form->{db}tax t ON (t.chart_id = c.id)
  84. WHERE t.$form->{db}_id = ?|;
  85. $sth = $dbh->prepare($query);
  86. $sth->execute( $form->{id} ) || $form->dberror($query);
  87. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  88. $form->{tax}{ $ref->{accno} }{taxable} = 1;
  89. }
  90. $sth->finish;
  91. }
  92. else {
  93. ( $form->{employee}, $form->{employee_id} ) = $form->get_employee($dbh);
  94. $query = qq|SELECT current_date|;
  95. ( $form->{startdate} ) = $dbh->selectrow_array($query);
  96. }
  97. # get tax labels
  98. $query = qq|
  99. SELECT DISTINCT c.accno, c.description
  100. FROM chart c
  101. JOIN tax t ON (t.chart_id = c.id)
  102. WHERE c.link LIKE ?
  103. ORDER BY c.accno|;
  104. $sth = $dbh->prepare($query);
  105. $sth->execute("%${ARAP}_tax%") || $form->dberror($query);
  106. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  107. $form->{taxaccounts} .= "$ref->{accno} ";
  108. $form->{tax}{ $ref->{accno} }{description} = $ref->{description};
  109. }
  110. $sth->finish;
  111. chop $form->{taxaccounts};
  112. # get business types ## needs fixing, this is bad (SELECT * ...) with order by 2. Yuck
  113. $query = qq|
  114. SELECT *
  115. FROM business
  116. ORDER BY 2|;
  117. $sth = $dbh->prepare($query);
  118. $sth->execute || $form->dberror($query);
  119. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  120. push @{ $form->{all_business} }, $ref;
  121. }
  122. $sth->finish;
  123. # employees/salespersons
  124. $form->all_employees( $myconfig, $dbh, undef,
  125. ( $form->{vc} eq 'customer' )
  126. ? 1
  127. : 0 );
  128. # get language ## needs fixing, this is bad (SELECT * ...) with order by 2. Yuck
  129. $query = qq|
  130. SELECT *
  131. FROM language
  132. ORDER BY 2|;
  133. $sth = $dbh->prepare($query);
  134. $sth->execute || $form->dberror($query);
  135. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  136. push @{ $form->{all_language} }, $ref;
  137. }
  138. $sth->finish;
  139. # get pricegroups ## needs fixing, this is bad (SELECT * ...) with order by 2. Yuck
  140. $query = qq|
  141. SELECT *
  142. FROM pricegroup
  143. ORDER BY 2|;
  144. $sth = $dbh->prepare($query);
  145. $sth->execute || $form->dberror($query);
  146. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  147. push @{ $form->{all_pricegroup} }, $ref;
  148. }
  149. $sth->finish;
  150. # get currencies
  151. $query = qq|
  152. SELECT value AS currencies
  153. FROM defaults
  154. WHERE setting_key = 'curr'|;
  155. ( $form->{currencies} ) = $dbh->selectrow_array($query);
  156. $dbh->commit;
  157. }
  158. sub _save_vc {
  159. ($form) = @_;
  160. my $dbh = $form->{dbh};
  161. my $updated = 0;
  162. if ($form->{vc} eq 'customer'){
  163. $form->{vc} = 'customer';
  164. $form->{entity_class} = 2;
  165. } else {
  166. $form->{vc} = 'vendor';
  167. $form->{entity_class} = 1;
  168. }
  169. # this should really all be replaced by an upsert.
  170. if ( $form->{id} ) {
  171. # This module is depricated, so we are just going to throw an error here
  172. $form->error("Updating $form->{vc} not supported. ".
  173. "Please wait for us to move to the new codebase.");
  174. }
  175. if (!$updated){
  176. # Creating Entity
  177. ($form->{entity_id}) = $dbh->selectrow_array("SELECT nextval('entity_id_seq')");
  178. $query = qq|INSERT INTO entity (id, name, entity_class) VALUES (?, ?,?)|;
  179. $sth = $dbh->prepare($query);
  180. $sth->execute($form->{entity_id}, $form->{name}, $form->{entity_class});
  181. $sth->finish;
  182. # Creating LOCATION
  183. ($form->{location_id}) =
  184. $dbh->selectrow_array("SELECT nextval('location_id_seq')");
  185. $query = qq|
  186. INSERT INTO location
  187. (id, line_one, line_two, city_province, mail_code,
  188. country_id, location_class, created)
  189. VALUES
  190. (?, ?, ?, ?, ?,
  191. (SELECT id FROM country
  192. WHERE short_name = ? OR name = ?),
  193. 1, current_date)
  194. |; # location class 1 is Billing. This is a sensible default.
  195. $sth = $dbh->prepare($query);
  196. $sth->execute($form->{location_id}, $form->{address1}, $form->{address2},
  197. "$form->{city}, $form->{state}", $form->{zipcode},
  198. $form->{country}, $form->{country}
  199. ) || $form->dberror($query);
  200. #Creating company
  201. # Removed entity_class_id,
  202. # removed primary_location_id,
  203. # added sic_code ~Aurynn
  204. $query = qq|
  205. INSERT INTO company
  206. (entity_id, legal_name, tax_id, sic_code)
  207. VALUES
  208. (?, ?, ?, ?)
  209. |;
  210. $sth = $dbh->prepare($query) || $form->dberror($query);
  211. $sth->execute($form->{entity_id}, # $form->{entity_class}, # removed entity_class_id ~Aurynn
  212. $form->{name},
  213. # $form->{location_id}, # removed by ~aurynn
  214. $form->{taxnumber},
  215. $form->{sic_code});
  216. # Creating entity_metadata record, replacing customer and vendor.
  217. $query = qq|
  218. INSERT INTO entity_credit_account
  219. (entity_id, entity_class, discount, taxincluded, creditlimit,
  220. terms, meta_number, cc, bcc, business_id,
  221. language_code, pricegroup_id, curr, startdate,
  222. enddate)
  223. VALUES (?, ?, ?, ?, ?,
  224. ?, ?, ?, ?,
  225. ?, ?, ?, ?,
  226. ?, ?)|;
  227. $sth = $dbh->prepare($query);
  228. $sth->execute(
  229. $form->{entity_id}, $form->{entity_class}, $form->{discount},
  230. $form->{taxincluded}, $form->{creditlimit},
  231. $form->{terms}, $form->{"$form->{db}number"}, $form->{cc},
  232. $form->{bcc}, $form->{business_id},
  233. $form->{language_code}, $form->{pricegroup_id}, $form->{curr},
  234. $form->{startdate} || undef, $form->{enddate} || undef,
  235. ) || $form->dberror($query);
  236. $query = qq|
  237. INSERT INTO entity_bank_account (entity_id, bic, iban)
  238. VALUES (?,?,?)
  239. |;
  240. $sth = $dbh->prepare($query);
  241. $sth->execute($form->{entity_id}, $form->{bic}, $form->{iban}) ||
  242. $form->dberror($query);
  243. }
  244. }
  245. sub save_customer {
  246. my ( $self, $myconfig, $form ) = @_;
  247. # connect to databaseµ
  248. my $dbh = $form->{dbh};
  249. my $query;
  250. my $sth;
  251. my $null;
  252. $form->{vc} = 'customer';
  253. # remove double spaces
  254. $form->{name} =~ s/ / /g;
  255. # remove double minus and minus at the end
  256. $form->{name} =~ s/--+/-/g;
  257. $form->{name} =~ s/-+$//;
  258. # assign value discount, terms, creditlimit
  259. $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
  260. $form->{discount} /= 100;
  261. $form->{terms} *= 1;
  262. $form->{taxincluded} *= 1;
  263. $form->{creditlimit} =
  264. $form->parse_amount( $myconfig, $form->{creditlimit} );
  265. if ( !$form->{creditlimit} ) {
  266. $form->{creditlimit} = 0;
  267. }
  268. &_save_vc($form);
  269. # save taxes
  270. foreach $item ( split / /, $form->{taxaccounts} ) {
  271. if ( $form->{"tax_$item"} ) {
  272. $query = qq|
  273. INSERT INTO customertax (customer_id, chart_id)
  274. VALUES (currval('entity_credit_account_id_seq'),
  275. (SELECT id
  276. FROM chart
  277. WHERE accno = ?))|;
  278. $sth = $dbh->prepare($query);
  279. $sth->execute( $item )
  280. || $form->dberror($query);
  281. }
  282. }
  283. # add shipto
  284. $form->add_shipto( $dbh, $form->{id} );
  285. $dbh->commit;
  286. }
  287. sub save_vendor {
  288. my ( $self, $myconfig, $form ) = @_;
  289. # connect to database
  290. my $dbh = $form->{dbh};
  291. my $query;
  292. my $sth;
  293. my $null;
  294. # remove double spaces
  295. $form->{name} =~ s/ / /g;
  296. # remove double minus and minus at the end
  297. $form->{name} =~ s/--+/-/g;
  298. $form->{name} =~ s/-+$//;
  299. $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
  300. $form->{discount} /= 100;
  301. $form->{terms} *= 1;
  302. $form->{taxincluded} *= 1;
  303. $form->{creditlimit} =
  304. $form->parse_amount( $myconfig, $form->{creditlimit} );
  305. &_save_vc($form);
  306. # save taxes
  307. foreach $item ( split / /, $form->{taxaccounts} ) {
  308. if ( $form->{"tax_$item"} ) {
  309. $query = qq|
  310. INSERT INTO vendortax (vendor_id, chart_id)
  311. VALUES (?, (SELECT id
  312. FROM chart
  313. WHERE accno = ?))|;
  314. $sth = $dbh->prepare($query);
  315. $sth->execute( $form->{id}, $item )
  316. || $form->dberror($query);
  317. }
  318. }
  319. # add shipto
  320. $form->add_shipto( $dbh, $form->{id} );
  321. $dbh->commit;
  322. }
  323. sub delete {
  324. my ( $self, $myconfig, $form ) = @_;
  325. # connect to database
  326. my $dbh = $form->{dbh};
  327. # delete customer/vendor
  328. my $query = qq|DELETE FROM $form->{db}
  329. WHERE id = ?|;
  330. $sth = $dbh->prepare($query);
  331. $sth->execute( $form->{id} ) || $form->dberror($query);
  332. $dbh->commit;
  333. }
  334. sub search {
  335. my ( $self, $myconfig, $form ) = @_;
  336. # connect to database
  337. my $dbh = $form->{dbh};
  338. my $where = "1 = 1";
  339. $form->{sort} = ( $form->{sort} ) ? $form->{sort} : "name";
  340. my @a = qw(name);
  341. my $sortorder = $form->sort_order( \@a );
  342. my $var;
  343. my $item;
  344. @a = ("$form->{db}number");
  345. push @a, qw(name contact city state zipcode country notes phone email);
  346. if ( $form->{employee} ) {
  347. $var = $dbh->quote($form->like(lc $form->{employee}));
  348. $where .= " AND lower(e.name) LIKE $var";
  349. }
  350. foreach $item (@a) {
  351. if ( $form->{$item} ne "" ) {
  352. $var = $dbh->quote($form->like( lc $form->{$item}) );
  353. $where .= " AND lower(ct.$item) LIKE $var";
  354. }
  355. }
  356. if ( $form->{address} ne "" ) {
  357. $var = $dbh->quote( $form->like( lc $form->{address} ) );
  358. $where .=
  359. " AND (lower(ct.address1) ILIKE $var)";
  360. }
  361. if ( $form->{startdatefrom} ) {
  362. $where .=
  363. " AND ct.startdate >= " . $dbh->quote( $form->{startdatefrom} );
  364. }
  365. if ( $form->{startdateto} ) {
  366. $where .= " AND ct.startdate <= " . $dbh->quote( $form->{startdateto} );
  367. }
  368. if ( $form->{status} eq 'active' ) {
  369. $where .= " AND ct.enddate IS NULL";
  370. }
  371. if ( $form->{status} eq 'inactive' ) {
  372. $where .= " AND ct.enddate <= current_date";
  373. }
  374. if ( $form->{status} eq 'orphaned' ) {
  375. $where .= qq|
  376. AND ct.id NOT IN (SELECT o.$form->{db}_id
  377. FROM oe o, $form->{db} vc
  378. WHERE vc.id = o.$form->{db}_id)|;
  379. if ( $form->{db} =~ /(^customer$|^vendor$)/ ) {
  380. $where .= qq| AND ct.id NOT IN (SELECT a.entity_id
  381. FROM ar a, customer vc
  382. WHERE vc.entity_id = a.entity_id)|;
  383. }
  384. $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
  385. }
  386. my $query = qq|
  387. SELECT ct.*, b.description AS business,
  388. e.name AS employee, g.pricegroup,
  389. l.description AS language, m.name AS manager
  390. FROM $form->{db} ct
  391. LEFT JOIN business b ON (ct.business_id = b.id)
  392. LEFT JOIN employee e ON (ct.employee_id = e.id)
  393. LEFT JOIN employee m ON (m.id = e.managerid)
  394. LEFT JOIN pricegroup g ON (ct.pricegroup_id = g.id)
  395. LEFT JOIN language l ON (l.code = ct.language_code)
  396. WHERE $where|;
  397. # redo for invoices, orders and quotations
  398. if ( $form->{l_transnumber}
  399. || $form->{l_invnumber}
  400. || $form->{l_ordnumber}
  401. || $form->{l_quonumber} )
  402. {
  403. my ( $ar, $union, $module );
  404. $query = "";
  405. my $transwhere;
  406. my $openarap = "";
  407. my $openoe = "";
  408. if ( $form->{open} || $form->{closed} ) {
  409. unless ( $form->{open} && $form->{closed} ) {
  410. $openarap = " AND a.amount != a.paid"
  411. if $form->{open};
  412. $openarap = " AND a.amount = a.paid"
  413. if $form->{closed};
  414. $openoe = " AND o.closed = '0'"
  415. if $form->{open};
  416. $openoe = " AND o.closed = '1'"
  417. if $form->{closed};
  418. }
  419. }
  420. if ( $form->{l_transnumber} ) {
  421. $ar = ( $form->{db} eq 'customer' ) ? 'ar' : 'ap';
  422. $module = $ar;
  423. $transwhere = "";
  424. $transwhere .=
  425. " AND a.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  426. if $form->{transdatefrom};
  427. $transwhere .=
  428. " AND a.transdate <= " . $dbh->quote( $form->{transdateto} )
  429. if $form->{transdateto};
  430. $query = qq|
  431. SELECT ct.*, b.description AS business,
  432. a.invnumber, a.ordnumber,
  433. a.quonumber,
  434. a.id AS invid, '$ar' AS module,
  435. 'invoice' AS formtype,
  436. (a.amount = a.paid) AS closed,
  437. a.amount,
  438. a.netamount, e.name AS employee,
  439. m.name AS manager
  440. FROM $form->{db} ct
  441. JOIN $ar a ON (a.$form->{db}_id = ct.id)
  442. LEFT JOIN business b ON (ct.business_id = b.id)
  443. LEFT JOIN employee e ON (a.employee_id = e.id)
  444. LEFT JOIN employee m ON (m.id = e.managerid)
  445. WHERE $where
  446. AND a.invoice = '0'
  447. $transwhere
  448. $openarap |;
  449. $union = qq| UNION |;
  450. }
  451. if ( $form->{l_invnumber} ) {
  452. $ar = ( $form->{db} eq 'customer' ) ? 'ar' : 'ap';
  453. $module = ( $ar eq 'ar' ) ? 'is' : 'ir';
  454. $transwhere = "";
  455. $transwhere .=
  456. " AND a.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  457. if $form->{transdatefrom};
  458. $transwhere .=
  459. " AND a.transdate <= " . $dbh->quote( $form->{transdateto} )
  460. if $form->{transdateto};
  461. $query .= qq|
  462. $union
  463. SELECT ct.*, b.description AS business,
  464. a.invnumber, a.ordnumber, a.quonumber,
  465. a.id AS invid,
  466. '$module' AS module,
  467. 'invoice' AS formtype,
  468. (a.amount = a.paid) AS closed,
  469. a.amount, a.netamount,
  470. e.name AS employee, m.name AS manager
  471. FROM $form->{db} ct
  472. JOIN $ar a ON (a.$form->{db}_id = ct.id)
  473. LEFT JOIN business b ON (ct.business_id = b.id)
  474. LEFT JOIN employee e ON (a.employee_id = e.id)
  475. LEFT JOIN employee m ON (m.id = e.managerid)
  476. WHERE $where
  477. AND a.invoice = '1'
  478. $transwhere
  479. $openarap |;
  480. $union = qq| UNION|;
  481. }
  482. if ( $form->{l_ordnumber} ) {
  483. $transwhere = "";
  484. $transwhere .=
  485. " AND o.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  486. if $form->{transdatefrom};
  487. $transwhere .=
  488. " AND o.transdate <= " . $dbh->quote( $form->{transdateto} )
  489. if $form->{transdateto};
  490. $query .= qq|
  491. $union
  492. SELECT ct.*, b.description AS business,
  493. ' ' AS invnumber, o.ordnumber,
  494. o.quonumber, o.id AS invid,
  495. 'oe' AS module, 'order' AS formtype,
  496. o.closed, o.amount, o.netamount,
  497. e.name AS employee, m.name AS manager
  498. FROM $form->{db} ct
  499. JOIN oe o ON (o.$form->{db}_id = ct.id)
  500. LEFT JOIN business b ON (ct.business_id = b.id)
  501. LEFT JOIN employee e ON (o.employee_id = e.id)
  502. LEFT JOIN employee m ON (m.id = e.managerid)
  503. WHERE $where
  504. AND o.quotation = '0'
  505. $transwhere
  506. $openoe |;
  507. $union = qq| UNION|;
  508. }
  509. if ( $form->{l_quonumber} ) {
  510. $transwhere = "";
  511. $transwhere .=
  512. " AND o.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  513. if $form->{transdatefrom};
  514. $transwhere .=
  515. " AND o.transdate <= " . $dbh->quote( $form->{transdateto} )
  516. if $form->{transdateto};
  517. $query .= qq|
  518. $union
  519. SELECT ct.*, b.description AS business,
  520. ' ' AS invnumber, o.ordnumber,
  521. o.quonumber, o.id AS invid,
  522. 'oe' AS module,
  523. 'quotation' AS formtype,
  524. o.closed, o.amount, o.netamount,
  525. e.name AS employee, m.name AS manager
  526. FROM $form->{db} ct
  527. JOIN oe o ON (o.$form->{db}_id = ct.id)
  528. LEFT JOIN business b ON (ct.business_id = b.id)
  529. LEFT JOIN employee e ON (o.employee_id = e.id)
  530. LEFT JOIN employee m ON (m.id = e.managerid)
  531. WHERE $where
  532. AND o.quotation = '1'
  533. $transwhere
  534. $openoe |;
  535. }
  536. $sortorder .= ", invid";
  537. }
  538. $query .= qq| ORDER BY $sortorder|;
  539. my $sth = $dbh->prepare($query);
  540. $sth->execute || $form->dberror($query);
  541. # accounts
  542. $query = qq|
  543. SELECT c.accno
  544. FROM chart c
  545. JOIN $form->{db}tax t ON (t.chart_id = c.id)
  546. WHERE t.$form->{db}_id = ?|;
  547. my $tth = $dbh->prepare($query);
  548. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  549. $tth->execute( $ref->{id} );
  550. while ( ($item) = $tth->fetchrow_array ) {
  551. $ref->{taxaccount} .= "$item ";
  552. }
  553. $tth->finish;
  554. chop $ref->{taxaccount};
  555. $ref->{address} = "";
  556. for (qw(address1 address2 city state zipcode country)) {
  557. $ref->{address} .= "$ref->{$_} ";
  558. }
  559. push @{ $form->{CT} }, $ref;
  560. }
  561. $sth->finish;
  562. $dbh->commit;
  563. }
  564. sub get_history {
  565. my ( $self, $myconfig, $form ) = @_;
  566. # connect to database
  567. my $dbh = $form->{dbh};
  568. my $query;
  569. my $where = "1 = 1";
  570. $form->{sort} = "partnumber" unless $form->{sort};
  571. my $sortorder = $form->{sort};
  572. my %ordinal = ();
  573. my $var;
  574. my $table;
  575. # setup ASC or DESC
  576. $form->sort_order();
  577. if ( $form->{"$form->{db}number"} ne "" ) {
  578. $var = $dbh->quote( $form->like( lc $form->{"$form->{db}number"} ) );
  579. $where .= " AND lower(ct.$form->{db}number) LIKE $var";
  580. }
  581. if ( $form->{address} ne "" ) {
  582. $var = $dbh->quote( $form->like( lc $form->{address} ) );
  583. $where .= " AND lower(ct.address1) ILIKE $var";
  584. }
  585. for (qw(name contact email phone notes city state zipcode country)) {
  586. if ( $form->{$_} ne "" ) {
  587. $var = $dbh->quote( $form->like( lc $form->{$_} ) );
  588. $where .= " AND lower(ct.$_) LIKE $var";
  589. }
  590. }
  591. if ( $form->{employee} ne "" ) {
  592. $var = $dbh->quote($form->like(lc $form->{employee}));
  593. $where .= " AND lower(e.name) LIKE $var";
  594. }
  595. $transwhere .=
  596. " AND a.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  597. if $form->{transdatefrom};
  598. $transwhere .= " AND a.transdate <= " . $dbh->quote( $form->{transdateto} )
  599. if $form->{transdateto};
  600. if ( $form->{open} || $form->{closed} ) {
  601. unless ( $form->{open} && $form->{closed} ) {
  602. if ( $form->{type} eq 'invoice' ) {
  603. $where .= " AND a.amount != a.paid"
  604. if $form->{open};
  605. $where .= " AND a.amount = a.paid"
  606. if $form->{closed};
  607. }
  608. else {
  609. $where .= " AND a.closed = '0'"
  610. if $form->{open};
  611. $where .= " AND a.closed = '1'"
  612. if $form->{closed};
  613. }
  614. }
  615. }
  616. my $invnumber = 'invnumber';
  617. my $deldate = 'deliverydate';
  618. my $buysell;
  619. my $sellprice = "sellprice";
  620. if ( $form->{db} eq 'customer' ) {
  621. $buysell = "buy";
  622. if ( $form->{type} eq 'invoice' ) {
  623. $where .= qq|
  624. AND a.invoice = '1' AND i.assemblyitem = '0'|;
  625. $table = 'ar';
  626. $sellprice = "fxsellprice";
  627. }
  628. else {
  629. $table = 'oe';
  630. if ( $form->{type} eq 'order' ) {
  631. $invnumber = 'ordnumber';
  632. $where .= qq| AND a.quotation = '0'|;
  633. }
  634. else {
  635. $invnumber = 'quonumber';
  636. $where .= qq| AND a.quotation = '1'|;
  637. }
  638. $deldate = 'reqdate';
  639. }
  640. }
  641. if ( $form->{db} eq 'vendor' ) {
  642. $buysell = "sell";
  643. if ( $form->{type} eq 'invoice' ) {
  644. $where .= qq| AND a.invoice = '1' AND i.assemblyitem = '0'|;
  645. $table = 'ap';
  646. $sellprice = "fxsellprice";
  647. }
  648. else {
  649. $table = 'oe';
  650. if ( $form->{type} eq 'order' ) {
  651. $invnumber = 'ordnumber';
  652. $where .= qq| AND a.quotation = '0'|;
  653. }
  654. else {
  655. $invnumber = 'quonumber';
  656. $where .= qq| AND a.quotation = '1'|;
  657. }
  658. $deldate = 'reqdate';
  659. }
  660. }
  661. my $invjoin = qq| JOIN invoice i ON (i.trans_id = a.id)|;
  662. if ( $form->{type} eq 'order' ) {
  663. $invjoin = qq| JOIN orderitems i ON (i.trans_id = a.id)|;
  664. }
  665. if ( $form->{type} eq 'quotation' ) {
  666. $invjoin = qq| JOIN orderitems i ON (i.trans_id = a.id)|;
  667. $where .= qq| AND a.quotation = '1'|;
  668. }
  669. %ordinal = (
  670. partnumber => 9,
  671. description => 12,
  672. "$deldate" => 16,
  673. serialnumber => 17,
  674. projectnumber => 18
  675. );
  676. $sortorder =
  677. "2 $form->{direction}, 1, 11, $ordinal{$sortorder} $form->{direction}";
  678. $query = qq|
  679. SELECT ct.id AS ctid, ct.name, ct.address1,
  680. ct.address2, ct.city, ct.state,
  681. p.id AS pid, p.partnumber, a.id AS invid,
  682. a.$invnumber, a.curr, i.description,
  683. i.qty, i.$sellprice AS sellprice, i.discount,
  684. i.$deldate, i.serialnumber, pr.projectnumber,
  685. e.name AS employee, ct.zipcode, ct.country, i.unit,
  686. (SELECT $buysell
  687. FROM exchangerate ex
  688. WHERE a.curr = ex.curr
  689. AND a.transdate = ex.transdate) AS exchangerate
  690. FROM $form->{db} ct
  691. JOIN $table a ON (a.$form->{db}_id = ct.id)
  692. $invjoin
  693. JOIN parts p ON (p.id = i.parts_id)
  694. LEFT JOIN project pr ON (pr.id = i.project_id)
  695. LEFT JOIN employee e ON (e.id = a.employee_id)
  696. WHERE $where
  697. ORDER BY $sortorder|;
  698. my $sth = $dbh->prepare($query);
  699. $sth->execute || $form->dberror($query);
  700. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  701. $ref->{address} = "";
  702. $ref->{exchangerate} ||= 1;
  703. for (qw(address1 address2 city state zipcode country)) {
  704. $ref->{address} .= "$ref->{$_} ";
  705. }
  706. $ref->{id} = $ref->{ctid};
  707. push @{ $form->{CT} }, $ref;
  708. }
  709. $sth->finish;
  710. $dbh->commit;
  711. }
  712. sub pricelist {
  713. my ( $self, $myconfig, $form ) = @_;
  714. # connect to database
  715. my $dbh = $form->{dbh};
  716. my $query;
  717. if ( $form->{db} eq 'customer' ) {
  718. $query = qq|SELECT p.id, p.partnumber, p.description,
  719. p.sellprice, pg.partsgroup, p.partsgroup_id,
  720. m.pricebreak, m.sellprice,
  721. m.validfrom, m.validto, m.curr
  722. FROM partscustomer m
  723. JOIN parts p ON (p.id = m.parts_id)
  724. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  725. WHERE m.customer_id = ?
  726. ORDER BY partnumber|;
  727. }
  728. if ( $form->{db} eq 'vendor' ) {
  729. $query = qq|SELECT p.id, p.partnumber AS sku, p.description,
  730. pg.partsgroup, p.partsgroup_id,
  731. m.partnumber, m.leadtime, m.lastcost, m.curr
  732. FROM partsvendor m
  733. JOIN parts p ON (p.id = m.parts_id)
  734. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  735. WHERE m.vendor_id = ?
  736. ORDER BY p.partnumber|;
  737. }
  738. my $sth;
  739. my $ref;
  740. if ( $form->{id} ) {
  741. $sth = $dbh->prepare($query);
  742. $sth->execute( $form->{id} ) || $form->dberror($query);
  743. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  744. push @{ $form->{all_partspricelist} }, $ref;
  745. }
  746. $sth->finish;
  747. }
  748. $query = qq|SELECT value FROM defaults where setting_key = 'curr'|;
  749. ( $form->{currencies} ) = $dbh->selectrow_array($query);
  750. $query = qq|SELECT id, partsgroup
  751. FROM partsgroup
  752. ORDER BY partsgroup|;
  753. $sth = $dbh->prepare($query);
  754. $sth->execute || $self->dberror($query);
  755. $form->{all_partsgroup} = ();
  756. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  757. push @{ $form->{all_partsgroup} }, $ref;
  758. }
  759. $sth->finish;
  760. $dbh->commit;
  761. }
  762. sub save_pricelist {
  763. my ( $self, $myconfig, $form ) = @_;
  764. my $dbh = $form->{dbh};
  765. my $query = qq|
  766. DELETE FROM parts$form->{db}
  767. WHERE $form->{db}_id = ?}|;
  768. $sth = $dbh->prepare($query);
  769. $sth->execute( $form->{id} ) || $form->dberror($query);
  770. foreach $i ( 1 .. $form->{rowcount} ) {
  771. if ( $form->{"id_$i"} ) {
  772. if ( $form->{db} eq 'customer' ) {
  773. for (qw(pricebreak sellprice)) {
  774. $form->{"${_}_$i"} =
  775. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  776. }
  777. $query = qq|
  778. INSERT INTO parts$form->{db}
  779. (parts_id, customer_id,
  780. pricebreak, sellprice,
  781. validfrom, validto, curr)
  782. VALUES (?, ?, ?, ?, ?, ?, ?)|;
  783. @queryargs = (
  784. $form->{"id_$i"}, $form->{id},
  785. $form->{"pricebreak_$i"}, $form->{"sellprice_$i"},
  786. $form->{"validfrom_$i"}, $form->{"validto_$i"},
  787. $form->{"curr_$i"}
  788. );
  789. }
  790. else {
  791. for (qw(leadtime lastcost)) {
  792. $form->{"${_}_$i"} =
  793. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  794. }
  795. $query = qq|
  796. INSERT INTO parts$form->{db}
  797. (parts_id, vendor_id,
  798. partnumber, lastcost,
  799. leadtime, curr)
  800. VALUES (?, ?, ?, ?, ?, ?)|;
  801. @queryargs = (
  802. $form->{"id_$i"}, $form->{id},
  803. $form->{"partnumber_$i"}, $form->{"lastcost_$i"},
  804. $form->{"leadtime_$i"}, $form->{"curr_$i"}
  805. );
  806. }
  807. $sth = $dbh->prepare($query);
  808. $sth->execute(@queryargs) || $form->dberror($query);
  809. }
  810. }
  811. $_ = $dbh->commit;
  812. }
  813. sub retrieve_item {
  814. my ( $self, $myconfig, $form ) = @_;
  815. # connect to database
  816. my $dbh = $form->{dbh};
  817. my $i = $form->{rowcount};
  818. my $var;
  819. my $null;
  820. my $where = "WHERE p.obsolete = '0'";
  821. if ( $form->{db} eq 'vendor' ) {
  822. # parts, services, labor
  823. $where .= " AND p.assembly = '0'";
  824. }
  825. if ( $form->{db} eq 'customer' ) {
  826. # parts, assemblies, services
  827. $where .= " AND p.income_accno_id > 0";
  828. }
  829. if ( $form->{"partnumber_$i"} ne "" ) {
  830. $var = $dbh->quote( $form->like( lc $form->{"partnumber_$i"} ) );
  831. $where .= " AND lower(p.partnumber) LIKE $var";
  832. }
  833. if ( $form->{"description_$i"} ne "" ) {
  834. $var = $dbh->quote( $form->like( lc $form->{"description_$i"} ) );
  835. $where .= " AND lower(p.description) LIKE $var";
  836. }
  837. if ( $form->{"partsgroup_$i"} ne "" ) {
  838. ( $null, $var ) = split /--/, $form->{"partsgroup_$i"};
  839. $var = $dbh->quote($var);
  840. $where .= qq| AND p.partsgroup_id = $var|;
  841. }
  842. my $query = qq|
  843. SELECT p.id, p.partnumber, p.description, p.sellprice,
  844. p.lastcost, p.unit, pg.partsgroup, p.partsgroup_id
  845. FROM parts p
  846. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  847. $where
  848. ORDER BY partnumber|;
  849. my $sth = $dbh->prepare($query);
  850. $sth->execute || $form->dberror($query);
  851. my $ref;
  852. $form->{item_list} = ();
  853. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  854. push @{ $form->{item_list} }, $ref;
  855. }
  856. $sth->finish;
  857. $dbh->commit;
  858. }
  859. 1;