summaryrefslogtreecommitdiff
path: root/LedgerSMB/CT.pm
blob: fcc0b32c6c127c03dd696db7691ad04fed36a870 (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. $form->db_parse_numeric(sth => $sth, hashref => $ref);
  550. $tth->execute( $ref->{id} );
  551. while ( ($item) = $tth->fetchrow_array ) {
  552. $ref->{taxaccount} .= "$item ";
  553. }
  554. $tth->finish;
  555. chop $ref->{taxaccount};
  556. $ref->{address} = "";
  557. for (qw(address1 address2 city state zipcode country)) {
  558. $ref->{address} .= "$ref->{$_} ";
  559. }
  560. push @{ $form->{CT} }, $ref;
  561. }
  562. $sth->finish;
  563. $dbh->commit;
  564. }
  565. sub get_history {
  566. my ( $self, $myconfig, $form ) = @_;
  567. # connect to database
  568. my $dbh = $form->{dbh};
  569. my $query;
  570. my $where = "1 = 1";
  571. $form->{sort} = "partnumber" unless $form->{sort};
  572. my $sortorder = $form->{sort};
  573. my %ordinal = ();
  574. my $var;
  575. my $table;
  576. # setup ASC or DESC
  577. $form->sort_order();
  578. if ( $form->{"$form->{db}number"} ne "" ) {
  579. $var = $dbh->quote( $form->like( lc $form->{"$form->{db}number"} ) );
  580. $where .= " AND lower(ct.$form->{db}number) LIKE $var";
  581. }
  582. if ( $form->{address} ne "" ) {
  583. $var = $dbh->quote( $form->like( lc $form->{address} ) );
  584. $where .= " AND lower(ct.address1) ILIKE $var";
  585. }
  586. for (qw(name contact email phone notes city state zipcode country)) {
  587. if ( $form->{$_} ne "" ) {
  588. $var = $dbh->quote( $form->like( lc $form->{$_} ) );
  589. $where .= " AND lower(ct.$_) LIKE $var";
  590. }
  591. }
  592. if ( $form->{employee} ne "" ) {
  593. $var = $dbh->quote($form->like(lc $form->{employee}));
  594. $where .= " AND lower(e.name) LIKE $var";
  595. }
  596. $transwhere .=
  597. " AND a.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  598. if $form->{transdatefrom};
  599. $transwhere .= " AND a.transdate <= " . $dbh->quote( $form->{transdateto} )
  600. if $form->{transdateto};
  601. if ( $form->{open} || $form->{closed} ) {
  602. unless ( $form->{open} && $form->{closed} ) {
  603. if ( $form->{type} eq 'invoice' ) {
  604. $where .= " AND a.amount != a.paid"
  605. if $form->{open};
  606. $where .= " AND a.amount = a.paid"
  607. if $form->{closed};
  608. }
  609. else {
  610. $where .= " AND a.closed = '0'"
  611. if $form->{open};
  612. $where .= " AND a.closed = '1'"
  613. if $form->{closed};
  614. }
  615. }
  616. }
  617. my $invnumber = 'invnumber';
  618. my $deldate = 'deliverydate';
  619. my $buysell;
  620. my $sellprice = "sellprice";
  621. if ( $form->{db} eq 'customer' ) {
  622. $buysell = "buy";
  623. if ( $form->{type} eq 'invoice' ) {
  624. $where .= qq|
  625. AND a.invoice = '1' AND i.assemblyitem = '0'|;
  626. $table = 'ar';
  627. $sellprice = "fxsellprice";
  628. }
  629. else {
  630. $table = 'oe';
  631. if ( $form->{type} eq 'order' ) {
  632. $invnumber = 'ordnumber';
  633. $where .= qq| AND a.quotation = '0'|;
  634. }
  635. else {
  636. $invnumber = 'quonumber';
  637. $where .= qq| AND a.quotation = '1'|;
  638. }
  639. $deldate = 'reqdate';
  640. }
  641. }
  642. if ( $form->{db} eq 'vendor' ) {
  643. $buysell = "sell";
  644. if ( $form->{type} eq 'invoice' ) {
  645. $where .= qq| AND a.invoice = '1' AND i.assemblyitem = '0'|;
  646. $table = 'ap';
  647. $sellprice = "fxsellprice";
  648. }
  649. else {
  650. $table = 'oe';
  651. if ( $form->{type} eq 'order' ) {
  652. $invnumber = 'ordnumber';
  653. $where .= qq| AND a.quotation = '0'|;
  654. }
  655. else {
  656. $invnumber = 'quonumber';
  657. $where .= qq| AND a.quotation = '1'|;
  658. }
  659. $deldate = 'reqdate';
  660. }
  661. }
  662. my $invjoin = qq| JOIN invoice i ON (i.trans_id = a.id)|;
  663. if ( $form->{type} eq 'order' ) {
  664. $invjoin = qq| JOIN orderitems i ON (i.trans_id = a.id)|;
  665. }
  666. if ( $form->{type} eq 'quotation' ) {
  667. $invjoin = qq| JOIN orderitems i ON (i.trans_id = a.id)|;
  668. $where .= qq| AND a.quotation = '1'|;
  669. }
  670. %ordinal = (
  671. partnumber => 9,
  672. description => 12,
  673. "$deldate" => 16,
  674. serialnumber => 17,
  675. projectnumber => 18
  676. );
  677. $sortorder =
  678. "2 $form->{direction}, 1, 11, $ordinal{$sortorder} $form->{direction}";
  679. $query = qq|
  680. SELECT ct.id AS ctid, ct.name, ct.address1,
  681. ct.address2, ct.city, ct.state,
  682. p.id AS pid, p.partnumber, a.id AS invid,
  683. a.$invnumber, a.curr, i.description,
  684. i.qty, i.$sellprice AS sellprice, i.discount,
  685. i.$deldate, i.serialnumber, pr.projectnumber,
  686. e.name AS employee, ct.zipcode, ct.country, i.unit,
  687. (SELECT $buysell
  688. FROM exchangerate ex
  689. WHERE a.curr = ex.curr
  690. AND a.transdate = ex.transdate) AS exchangerate
  691. FROM $form->{db} ct
  692. JOIN $table a ON (a.$form->{db}_id = ct.id)
  693. $invjoin
  694. JOIN parts p ON (p.id = i.parts_id)
  695. LEFT JOIN project pr ON (pr.id = i.project_id)
  696. LEFT JOIN employee e ON (e.id = a.employee_id)
  697. WHERE $where
  698. ORDER BY $sortorder|;
  699. my $sth = $dbh->prepare($query);
  700. $sth->execute || $form->dberror($query);
  701. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  702. $ref->{address} = "";
  703. $ref->{exchangerate} ||= 1;
  704. for (qw(address1 address2 city state zipcode country)) {
  705. $ref->{address} .= "$ref->{$_} ";
  706. }
  707. $ref->{id} = $ref->{ctid};
  708. push @{ $form->{CT} }, $ref;
  709. }
  710. $sth->finish;
  711. $dbh->commit;
  712. }
  713. sub pricelist {
  714. my ( $self, $myconfig, $form ) = @_;
  715. # connect to database
  716. my $dbh = $form->{dbh};
  717. my $query;
  718. if ( $form->{db} eq 'customer' ) {
  719. $query = qq|SELECT p.id, p.partnumber, p.description,
  720. p.sellprice, pg.partsgroup, p.partsgroup_id,
  721. m.pricebreak, m.sellprice,
  722. m.validfrom, m.validto, m.curr
  723. FROM partscustomer m
  724. JOIN parts p ON (p.id = m.parts_id)
  725. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  726. WHERE m.customer_id = ?
  727. ORDER BY partnumber|;
  728. }
  729. if ( $form->{db} eq 'vendor' ) {
  730. $query = qq|SELECT p.id, p.partnumber AS sku, p.description,
  731. pg.partsgroup, p.partsgroup_id,
  732. m.partnumber, m.leadtime, m.lastcost, m.curr
  733. FROM partsvendor m
  734. JOIN parts p ON (p.id = m.parts_id)
  735. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  736. WHERE m.vendor_id = ?
  737. ORDER BY p.partnumber|;
  738. }
  739. my $sth;
  740. my $ref;
  741. if ( $form->{id} ) {
  742. $sth = $dbh->prepare($query);
  743. $sth->execute( $form->{id} ) || $form->dberror($query);
  744. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  745. push @{ $form->{all_partspricelist} }, $ref;
  746. }
  747. $sth->finish;
  748. }
  749. $query = qq|SELECT value FROM defaults where setting_key = 'curr'|;
  750. ( $form->{currencies} ) = $dbh->selectrow_array($query);
  751. $query = qq|SELECT id, partsgroup
  752. FROM partsgroup
  753. ORDER BY partsgroup|;
  754. $sth = $dbh->prepare($query);
  755. $sth->execute || $self->dberror($query);
  756. $form->{all_partsgroup} = ();
  757. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  758. push @{ $form->{all_partsgroup} }, $ref;
  759. }
  760. $sth->finish;
  761. $dbh->commit;
  762. }
  763. sub save_pricelist {
  764. my ( $self, $myconfig, $form ) = @_;
  765. my $dbh = $form->{dbh};
  766. my $query = qq|
  767. DELETE FROM parts$form->{db}
  768. WHERE $form->{db}_id = ?|;
  769. $sth = $dbh->prepare($query);
  770. $sth->execute( $form->{id} ) || $form->dberror($query);
  771. foreach $i ( 1 .. $form->{rowcount} ) {
  772. if ( $form->{"id_$i"} ) {
  773. if ( $form->{db} eq 'customer' ) {
  774. for (qw(pricebreak sellprice)) {
  775. $form->{"${_}_$i"} =
  776. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  777. }
  778. $query = qq|
  779. INSERT INTO parts$form->{db}
  780. (parts_id, customer_id,
  781. pricebreak, sellprice,
  782. validfrom, validto, curr)
  783. VALUES (?, ?, ?, ?, ?, ?, ?)|;
  784. @queryargs = (
  785. $form->{"id_$i"}, $form->{id},
  786. $form->{"pricebreak_$i"}, $form->{"sellprice_$i"},
  787. $form->{"validfrom_$i"}, $form->{"validto_$i"},
  788. $form->{"curr_$i"}
  789. );
  790. }
  791. else {
  792. for (qw(leadtime lastcost)) {
  793. $form->{"${_}_$i"} =
  794. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  795. }
  796. $query = qq|
  797. INSERT INTO parts$form->{db}
  798. (parts_id, vendor_id,
  799. partnumber, lastcost,
  800. leadtime, curr)
  801. VALUES (?, ?, ?, ?, ?, ?)|;
  802. @queryargs = (
  803. $form->{"id_$i"}, $form->{id},
  804. $form->{"partnumber_$i"}, $form->{"lastcost_$i"},
  805. $form->{"leadtime_$i"}, $form->{"curr_$i"}
  806. );
  807. }
  808. $sth = $dbh->prepare($query);
  809. $sth->execute(@queryargs) || $form->dberror($query);
  810. }
  811. }
  812. $_ = $dbh->commit;
  813. }
  814. sub retrieve_item {
  815. my ( $self, $myconfig, $form ) = @_;
  816. # connect to database
  817. my $dbh = $form->{dbh};
  818. my $i = $form->{rowcount};
  819. my $var;
  820. my $null;
  821. my $where = "WHERE p.obsolete = '0'";
  822. if ( $form->{db} eq 'vendor' ) {
  823. # parts, services, labor
  824. $where .= " AND p.assembly = '0'";
  825. }
  826. if ( $form->{db} eq 'customer' ) {
  827. # parts, assemblies, services
  828. $where .= " AND p.income_accno_id > 0";
  829. }
  830. if ( $form->{"partnumber_$i"} ne "" ) {
  831. $var = $dbh->quote( $form->like( lc $form->{"partnumber_$i"} ) );
  832. $where .= " AND lower(p.partnumber) LIKE $var";
  833. }
  834. if ( $form->{"description_$i"} ne "" ) {
  835. $var = $dbh->quote( $form->like( lc $form->{"description_$i"} ) );
  836. $where .= " AND lower(p.description) LIKE $var";
  837. }
  838. if ( $form->{"partsgroup_$i"} ne "" ) {
  839. ( $null, $var ) = split /--/, $form->{"partsgroup_$i"};
  840. $var = $dbh->quote($var);
  841. $where .= qq| AND p.partsgroup_id = $var|;
  842. }
  843. my $query = qq|
  844. SELECT p.id, p.partnumber, p.description, p.sellprice,
  845. p.lastcost, p.unit, pg.partsgroup, p.partsgroup_id
  846. FROM parts p
  847. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  848. $where
  849. ORDER BY partnumber|;
  850. my $sth = $dbh->prepare($query);
  851. $sth->execute || $form->dberror($query);
  852. my $ref;
  853. $form->{item_list} = ();
  854. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  855. push @{ $form->{item_list} }, $ref;
  856. }
  857. $sth->finish;
  858. $dbh->commit;
  859. }
  860. 1;