summaryrefslogtreecommitdiff
path: root/LedgerSMB/CT.pm
blob: 6aa0745923e50b485202d2d8581540d0e57145f5 (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. if ( $form->{id} ) {
  170. $query = qq|
  171. DELETE FROM $form->{vc}tax
  172. WHERE entity_id =
  173. (select entity_id from $form->{vc}
  174. WHERE id = ?)|;
  175. $sth = $dbh->prepare($query);
  176. $sth->execute( $form->{id} ) || $form->dberror($query);
  177. $query = qq|
  178. SELECT id
  179. FROM $form->{vc}
  180. WHERE id = ?|;
  181. $sth = $dbh->prepare($query);
  182. $sth->execute( $form->{id} ) || $form->dberror($query);
  183. if ( $sth->fetchrow_array ) {
  184. $sth->finish;
  185. $query = qq|
  186. UPDATE $form->{vc}
  187. SET discount = ?
  188. taxincluded = ?
  189. creditlimit = ?
  190. terms = ?
  191. $form->{vc}number = ?
  192. cc = ?
  193. bcc = ?
  194. business_id = ?
  195. sic_code = ?
  196. language_code = ?
  197. pricegroup_id = ?
  198. curr = ?
  199. startdate = ?
  200. enddate = ?
  201. invoice_notes = ?
  202. bic = ?
  203. iban = ?
  204. WHERE id = ?|;
  205. $sth = $dbh->prepare($query);
  206. $sth->execute(
  207. $form->{discount}, $form->{taxincluded}, $form->{creditlimit},
  208. $form->{terms}, $form->{"$form->{vc}number"}, $form->{cc},
  209. $form->{bcc}, $form->{business_id}, $form->{sic_code},
  210. $form->{language_code}, $form->{pricegroup_id},
  211. $form->{curr}, $form->{startdate}, $form->{enddate},
  212. $form->{invoice_notes}, $form->{bic}, $form->{iban}, $form->{id}
  213. ) || $form->dberror(__FILE__.":".__LINE__.":$query");
  214. $updated = 1;
  215. }
  216. }
  217. if (!$updated){
  218. # Creating Entity
  219. ($form->{entity_id}) = $dbh->selectrow_array("SELECT nextval('entity_id_seq')");
  220. $query = qq|INSERT INTO entity (id, name, entity_class) VALUES (?, ?)|;
  221. $sth = $dbh->prepare($query);
  222. $sth->execute($form->{entity_id}, $form->{name}, $form->{entity_class});
  223. $sth->finish;
  224. # Creating LOCATION
  225. ($form->{location_id}) =
  226. $dbh->selectrow_array("SELECT nextval('location_id_seq')");
  227. $query = qq|
  228. INSERT INTO location
  229. (id, line_one, line_two, city_province, mail_code,
  230. country_id, location_class,created)
  231. VALUES
  232. (?, ?, ?, ?,
  233. (SELECT id FROM country
  234. WHERE short_name = ? OR name = ?),
  235. 1, current_date)
  236. |; # location class 1 is Billing. This is a sensible default.
  237. $sth = $dbh->prepare($query);
  238. $sth->execute($form->{location_id}, $form->{address1}, $form->{address2},
  239. "$form->{city}, $form->{state}", $form->{zipcode},
  240. $form->{country}, $form->{country}
  241. ) || $form->dberror($query);
  242. #Creating company
  243. # Removed entity_class_id ~Aurynn
  244. # removed primary_location_id ~Aurynn
  245. $query = qq|
  246. INSERT INTO company
  247. (entity_id, legal_name, tax_id)
  248. VALUES
  249. (?, ?, ?)
  250. |;
  251. $sth = $dbh->prepare($query) || $form->dberror($query);
  252. $sth->execute($form->{entity_id}, # $form->{entity_class}, # removed entity_class_id ~Aurynn
  253. $form->{name},
  254. # $form->{location_id}, # removed by ~aurynn
  255. $form->{taxnumber});
  256. #Creating customer record
  257. $query = qq|
  258. INSERT INTO customer
  259. (entity_id, discount, taxincluded, creditlimit, terms,
  260. customernumber, cc, bcc, business_id, sic_code,
  261. language_code, pricegroup_id, curr, startdate,
  262. enddate, invoice_notes, bic, iban)
  263. VALUES (?, ?, ?, ?, ?,
  264. ?, ?, ?, ?, ?,
  265. ?, ?, ?, ?,
  266. ?, ?, ?, ?)|;
  267. $sth = $dbh->prepare($query);
  268. $sth->execute(
  269. $form->{entity_id}, $form->{discount}, $form->{taxincluded},
  270. $form->{creditlimit},
  271. $form->{terms}, $form->{"$form->{vc}number"}, $form->{cc},
  272. $form->{bcc}, $form->{business_id}, $form->{sic_code},
  273. $form->{language_code}, $form->{pricegroup_id}, $form->{curr},
  274. $form->{startdate} || undef, $form->{enddate} || undef,
  275. $form->{invoice_notes},
  276. $form->{bic}, $form->{iban}
  277. ) || $form->dberror($query);
  278. }
  279. }
  280. sub save_customer {
  281. my ( $self, $myconfig, $form ) = @_;
  282. # connect to databaseµ
  283. my $dbh = $form->{dbh};
  284. my $query;
  285. my $sth;
  286. my $null;
  287. # remove double spaces
  288. $form->{name} =~ s/ / /g;
  289. # remove double minus and minus at the end
  290. $form->{name} =~ s/--+/-/g;
  291. $form->{name} =~ s/-+$//;
  292. # assign value discount, terms, creditlimit
  293. $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
  294. $form->{discount} /= 100;
  295. $form->{terms} *= 1;
  296. $form->{taxincluded} *= 1;
  297. $form->{creditlimit} =
  298. $form->parse_amount( $myconfig, $form->{creditlimit} );
  299. if ( !$form->{creditlimit} ) {
  300. $form->{creditlimit} = 0;
  301. }
  302. &_save_vc($form);
  303. # save taxes
  304. foreach $item ( split / /, $form->{taxaccounts} ) {
  305. if ( $form->{"tax_$item"} ) {
  306. $query = qq|
  307. INSERT INTO customertax (customer_id, chart_id)
  308. VALUES (?, (SELECT id
  309. FROM chart
  310. WHERE accno = ?))|;
  311. $sth = $dbh->prepare($query);
  312. $sth->execute( $form->{id}, $item )
  313. || $form->dberror($query);
  314. }
  315. }
  316. # add shipto
  317. $form->add_shipto( $dbh, $form->{id} );
  318. $dbh->commit;
  319. }
  320. sub save_vendor {
  321. my ( $self, $myconfig, $form ) = @_;
  322. # connect to database
  323. my $dbh = $form->{dbh};
  324. my $query;
  325. my $sth;
  326. my $null;
  327. # remove double spaces
  328. $form->{name} =~ s/ / /g;
  329. # remove double minus and minus at the end
  330. $form->{name} =~ s/--+/-/g;
  331. $form->{name} =~ s/-+$//;
  332. $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
  333. $form->{discount} /= 100;
  334. $form->{terms} *= 1;
  335. $form->{taxincluded} *= 1;
  336. $form->{creditlimit} =
  337. $form->parse_amount( $myconfig, $form->{creditlimit} );
  338. &_save_vc($form);
  339. # save taxes
  340. foreach $item ( split / /, $form->{taxaccounts} ) {
  341. if ( $form->{"tax_$item"} ) {
  342. $query = qq|
  343. INSERT INTO vendortax (vendor_id, chart_id)
  344. VALUES (?, (SELECT id
  345. FROM chart
  346. WHERE accno = ?))|;
  347. $sth = $dbh->prepare($query);
  348. $sth->execute( $form->{id}, $item )
  349. || $form->dberror($query);
  350. }
  351. }
  352. # add shipto
  353. $form->add_shipto( $dbh, $form->{id} );
  354. $dbh->commit;
  355. }
  356. sub delete {
  357. my ( $self, $myconfig, $form ) = @_;
  358. # connect to database
  359. my $dbh = $form->{dbh};
  360. # delete customer/vendor
  361. my $query = qq|DELETE FROM $form->{db}
  362. WHERE id = ?|;
  363. $sth = $dbh->prepare($query);
  364. $sth->execute( $form->{id} ) || $form->dberror($query);
  365. $dbh->commit;
  366. }
  367. sub search {
  368. my ( $self, $myconfig, $form ) = @_;
  369. # connect to database
  370. my $dbh = $form->{dbh};
  371. my $where = "1 = 1";
  372. $form->{sort} = ( $form->{sort} ) ? $form->{sort} : "name";
  373. my @a = qw(name);
  374. my $sortorder = $form->sort_order( \@a );
  375. my $var;
  376. my $item;
  377. @a = ("$form->{db}number");
  378. push @a, qw(name contact city state zipcode country notes phone email);
  379. if ( $form->{employee} ) {
  380. $var = $form->like( lc $form->{employee} );
  381. $where .= " AND lower(e.name) LIKE '$var'";
  382. }
  383. foreach $item (@a) {
  384. if ( $form->{$item} ne "" ) {
  385. $var = $form->like( lc $form->{$item} );
  386. $where .= " AND lower(ct.$item) LIKE '$var'";
  387. }
  388. }
  389. if ( $form->{address} ne "" ) {
  390. $var = $dbh->quote( $form->like( lc $form->{address} ) );
  391. $where .=
  392. " AND (lower(ct.address1) ILIKE $var)";
  393. }
  394. if ( $form->{startdatefrom} ) {
  395. $where .=
  396. " AND ct.startdate >= " . $dbh->quote( $form->{startdatefrom} );
  397. }
  398. if ( $form->{startdateto} ) {
  399. $where .= " AND ct.startdate <= " . $dbh->quote( $form->{startdateto} );
  400. }
  401. if ( $form->{status} eq 'active' ) {
  402. $where .= " AND ct.enddate IS NULL";
  403. }
  404. if ( $form->{status} eq 'inactive' ) {
  405. $where .= " AND ct.enddate <= current_date";
  406. }
  407. if ( $form->{status} eq 'orphaned' ) {
  408. $where .= qq|
  409. AND ct.id NOT IN (SELECT o.$form->{db}_id
  410. FROM oe o, $form->{db} vc
  411. WHERE vc.id = o.$form->{db}_id)|;
  412. if ( $form->{db} =~ /(^customer$|^vendor$)/ ) {
  413. $where .= qq| AND ct.id NOT IN (SELECT a.entity_id
  414. FROM ar a, customer vc
  415. WHERE vc.entity_id = a.entity_id)|;
  416. }
  417. $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
  418. }
  419. my $query = qq|
  420. SELECT ct.*, b.description AS business,
  421. e.name AS employee, g.pricegroup,
  422. l.description AS language, m.name AS manager
  423. FROM $form->{db} ct
  424. LEFT JOIN business b ON (ct.business_id = b.id)
  425. LEFT JOIN employee e ON (ct.employee_id = e.id)
  426. LEFT JOIN employee m ON (m.id = e.managerid)
  427. LEFT JOIN pricegroup g ON (ct.pricegroup_id = g.id)
  428. LEFT JOIN language l ON (l.code = ct.language_code)
  429. WHERE $where|;
  430. # redo for invoices, orders and quotations
  431. if ( $form->{l_transnumber}
  432. || $form->{l_invnumber}
  433. || $form->{l_ordnumber}
  434. || $form->{l_quonumber} )
  435. {
  436. my ( $ar, $union, $module );
  437. $query = "";
  438. my $transwhere;
  439. my $openarap = "";
  440. my $openoe = "";
  441. if ( $form->{open} || $form->{closed} ) {
  442. unless ( $form->{open} && $form->{closed} ) {
  443. $openarap = " AND a.amount != a.paid"
  444. if $form->{open};
  445. $openarap = " AND a.amount = a.paid"
  446. if $form->{closed};
  447. $openoe = " AND o.closed = '0'"
  448. if $form->{open};
  449. $openoe = " AND o.closed = '1'"
  450. if $form->{closed};
  451. }
  452. }
  453. if ( $form->{l_transnumber} ) {
  454. $ar = ( $form->{db} eq 'customer' ) ? 'ar' : 'ap';
  455. $module = $ar;
  456. $transwhere = "";
  457. $transwhere .=
  458. " AND a.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  459. if $form->{transdatefrom};
  460. $transwhere .=
  461. " AND a.transdate <= " . $dbh->quote( $form->{transdateto} )
  462. if $form->{transdateto};
  463. $query = qq|
  464. SELECT ct.*, b.description AS business,
  465. a.invnumber, a.ordnumber,
  466. a.quonumber,
  467. a.id AS invid, '$ar' AS module,
  468. 'invoice' AS formtype,
  469. (a.amount = a.paid) AS closed,
  470. a.amount,
  471. a.netamount, e.name AS employee,
  472. m.name AS manager
  473. FROM $form->{db} ct
  474. JOIN $ar a ON (a.$form->{db}_id = ct.id)
  475. LEFT JOIN business b ON (ct.business_id = b.id)
  476. LEFT JOIN employee e ON (a.employee_id = e.id)
  477. LEFT JOIN employee m ON (m.id = e.managerid)
  478. WHERE $where
  479. AND a.invoice = '0'
  480. $transwhere
  481. $openarap |;
  482. $union = qq| UNION |;
  483. }
  484. if ( $form->{l_invnumber} ) {
  485. $ar = ( $form->{db} eq 'customer' ) ? 'ar' : 'ap';
  486. $module = ( $ar eq 'ar' ) ? 'is' : 'ir';
  487. $transwhere = "";
  488. $transwhere .=
  489. " AND a.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  490. if $form->{transdatefrom};
  491. $transwhere .=
  492. " AND a.transdate <= " . $dbh->quote( $form->{transdateto} )
  493. if $form->{transdateto};
  494. $query .= qq|
  495. $union
  496. SELECT ct.*, b.description AS business,
  497. a.invnumber, a.ordnumber, a.quonumber,
  498. a.id AS invid,
  499. '$module' AS module,
  500. 'invoice' AS formtype,
  501. (a.amount = a.paid) AS closed,
  502. a.amount, a.netamount,
  503. e.name AS employee, m.name AS manager
  504. FROM $form->{db} ct
  505. JOIN $ar a ON (a.$form->{db}_id = ct.id)
  506. LEFT JOIN business b ON (ct.business_id = b.id)
  507. LEFT JOIN employee e ON (a.employee_id = e.id)
  508. LEFT JOIN employee m ON (m.id = e.managerid)
  509. WHERE $where
  510. AND a.invoice = '1'
  511. $transwhere
  512. $openarap |;
  513. $union = qq| UNION|;
  514. }
  515. if ( $form->{l_ordnumber} ) {
  516. $transwhere = "";
  517. $transwhere .=
  518. " AND o.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  519. if $form->{transdatefrom};
  520. $transwhere .=
  521. " AND o.transdate <= " . $dbh->quote( $form->{transdateto} )
  522. if $form->{transdateto};
  523. $query .= qq|
  524. $union
  525. SELECT ct.*, b.description AS business,
  526. ' ' AS invnumber, o.ordnumber,
  527. o.quonumber, o.id AS invid,
  528. 'oe' AS module, 'order' AS formtype,
  529. o.closed, o.amount, o.netamount,
  530. e.name AS employee, m.name AS manager
  531. FROM $form->{db} ct
  532. JOIN oe o ON (o.$form->{db}_id = ct.id)
  533. LEFT JOIN business b ON (ct.business_id = b.id)
  534. LEFT JOIN employee e ON (o.employee_id = e.id)
  535. LEFT JOIN employee m ON (m.id = e.managerid)
  536. WHERE $where
  537. AND o.quotation = '0'
  538. $transwhere
  539. $openoe |;
  540. $union = qq| UNION|;
  541. }
  542. if ( $form->{l_quonumber} ) {
  543. $transwhere = "";
  544. $transwhere .=
  545. " AND o.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  546. if $form->{transdatefrom};
  547. $transwhere .=
  548. " AND o.transdate <= " . $dbh->quote( $form->{transdateto} )
  549. if $form->{transdateto};
  550. $query .= qq|
  551. $union
  552. SELECT ct.*, b.description AS business,
  553. ' ' AS invnumber, o.ordnumber,
  554. o.quonumber, o.id AS invid,
  555. 'oe' AS module,
  556. 'quotation' AS formtype,
  557. o.closed, o.amount, o.netamount,
  558. e.name AS employee, m.name AS manager
  559. FROM $form->{db} ct
  560. JOIN oe o ON (o.$form->{db}_id = ct.id)
  561. LEFT JOIN business b ON (ct.business_id = b.id)
  562. LEFT JOIN employee e ON (o.employee_id = e.id)
  563. LEFT JOIN employee m ON (m.id = e.managerid)
  564. WHERE $where
  565. AND o.quotation = '1'
  566. $transwhere
  567. $openoe |;
  568. }
  569. $sortorder .= ", invid";
  570. }
  571. $query .= qq| ORDER BY $sortorder|;
  572. my $sth = $dbh->prepare($query);
  573. $sth->execute || $form->dberror($query);
  574. # accounts
  575. $query = qq|
  576. SELECT c.accno
  577. FROM chart c
  578. JOIN $form->{db}tax t ON (t.chart_id = c.id)
  579. WHERE t.$form->{db}_id = ?|;
  580. my $tth = $dbh->prepare($query);
  581. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  582. $tth->execute( $ref->{id} );
  583. while ( ($item) = $tth->fetchrow_array ) {
  584. $ref->{taxaccount} .= "$item ";
  585. }
  586. $tth->finish;
  587. chop $ref->{taxaccount};
  588. $ref->{address} = "";
  589. for (qw(address1 address2 city state zipcode country)) {
  590. $ref->{address} .= "$ref->{$_} ";
  591. }
  592. push @{ $form->{CT} }, $ref;
  593. }
  594. $sth->finish;
  595. $dbh->commit;
  596. }
  597. sub get_history {
  598. my ( $self, $myconfig, $form ) = @_;
  599. # connect to database
  600. my $dbh = $form->{dbh};
  601. my $query;
  602. my $where = "1 = 1";
  603. $form->{sort} = "partnumber" unless $form->{sort};
  604. my $sortorder = $form->{sort};
  605. my %ordinal = ();
  606. my $var;
  607. my $table;
  608. # setup ASC or DESC
  609. $form->sort_order();
  610. if ( $form->{"$form->{db}number"} ne "" ) {
  611. $var = $dbh->( $form->like( lc $form->{"$form->{db}number"} ) );
  612. $where .= " AND lower(ct.$form->{db}number) LIKE $var";
  613. }
  614. if ( $form->{address} ne "" ) {
  615. $var = $dbh->quote( $form->like( lc $form->{address} ) );
  616. $where .= " AND lower(ct.address1) ILIKE $var";
  617. }
  618. for (qw(name contact email phone notes city state zipcode country)) {
  619. if ( $form->{$_} ne "" ) {
  620. $var = $dbh->quote( $form->like( lc $form->{$_} ) );
  621. $where .= " AND lower(ct.$_) LIKE $var";
  622. }
  623. }
  624. if ( $form->{employee} ne "" ) {
  625. $var = $form->like( lc $form->{employee} );
  626. $where .= " AND lower(e.name) LIKE '$var'";
  627. }
  628. $transwhere .=
  629. " AND a.transdate >= " . $dbh->quote( $form->{transdatefrom} )
  630. if $form->{transdatefrom};
  631. $transwhere .= " AND a.transdate <= " . $dbh->quote( $form->{transdateto} )
  632. if $form->{transdateto};
  633. if ( $form->{open} || $form->{closed} ) {
  634. unless ( $form->{open} && $form->{closed} ) {
  635. if ( $form->{type} eq 'invoice' ) {
  636. $where .= " AND a.amount != a.paid"
  637. if $form->{open};
  638. $where .= " AND a.amount = a.paid"
  639. if $form->{closed};
  640. }
  641. else {
  642. $where .= " AND a.closed = '0'"
  643. if $form->{open};
  644. $where .= " AND a.closed = '1'"
  645. if $form->{closed};
  646. }
  647. }
  648. }
  649. my $invnumber = 'invnumber';
  650. my $deldate = 'deliverydate';
  651. my $buysell;
  652. my $sellprice = "sellprice";
  653. if ( $form->{db} eq 'customer' ) {
  654. $buysell = "buy";
  655. if ( $form->{type} eq 'invoice' ) {
  656. $where .= qq|
  657. AND a.invoice = '1' AND i.assemblyitem = '0'|;
  658. $table = 'ar';
  659. $sellprice = "fxsellprice";
  660. }
  661. else {
  662. $table = 'oe';
  663. if ( $form->{type} eq 'order' ) {
  664. $invnumber = 'ordnumber';
  665. $where .= qq| AND a.quotation = '0'|;
  666. }
  667. else {
  668. $invnumber = 'quonumber';
  669. $where .= qq| AND a.quotation = '1'|;
  670. }
  671. $deldate = 'reqdate';
  672. }
  673. }
  674. if ( $form->{db} eq 'vendor' ) {
  675. $buysell = "sell";
  676. if ( $form->{type} eq 'invoice' ) {
  677. $where .= qq| AND a.invoice = '1' AND i.assemblyitem = '0'|;
  678. $table = 'ap';
  679. $sellprice = "fxsellprice";
  680. }
  681. else {
  682. $table = 'oe';
  683. if ( $form->{type} eq 'order' ) {
  684. $invnumber = 'ordnumber';
  685. $where .= qq| AND a.quotation = '0'|;
  686. }
  687. else {
  688. $invnumber = 'quonumber';
  689. $where .= qq| AND a.quotation = '1'|;
  690. }
  691. $deldate = 'reqdate';
  692. }
  693. }
  694. my $invjoin = qq| JOIN invoice i ON (i.trans_id = a.id)|;
  695. if ( $form->{type} eq 'order' ) {
  696. $invjoin = qq| JOIN orderitems i ON (i.trans_id = a.id)|;
  697. }
  698. if ( $form->{type} eq 'quotation' ) {
  699. $invjoin = qq| JOIN orderitems i ON (i.trans_id = a.id)|;
  700. $where .= qq| AND a.quotation = '1'|;
  701. }
  702. %ordinal = (
  703. partnumber => 9,
  704. description => 12,
  705. "$deldate" => 16,
  706. serialnumber => 17,
  707. projectnumber => 18
  708. );
  709. $sortorder =
  710. "2 $form->{direction}, 1, 11, $ordinal{$sortorder} $form->{direction}";
  711. $query = qq|
  712. SELECT ct.id AS ctid, ct.name, ct.address1,
  713. ct.address2, ct.city, ct.state,
  714. p.id AS pid, p.partnumber, a.id AS invid,
  715. a.$invnumber, a.curr, i.description,
  716. i.qty, i.$sellprice AS sellprice, i.discount,
  717. i.$deldate, i.serialnumber, pr.projectnumber,
  718. e.name AS employee, ct.zipcode, ct.country, i.unit,
  719. (SELECT $buysell
  720. FROM exchangerate ex
  721. WHERE a.curr = ex.curr
  722. AND a.transdate = ex.transdate) AS exchangerate
  723. FROM $form->{db} ct
  724. JOIN $table a ON (a.$form->{db}_id = ct.id)
  725. $invjoin
  726. JOIN parts p ON (p.id = i.parts_id)
  727. LEFT JOIN project pr ON (pr.id = i.project_id)
  728. LEFT JOIN employee e ON (e.id = a.employee_id)
  729. WHERE $where
  730. ORDER BY $sortorder|;
  731. my $sth = $dbh->prepare($query);
  732. $sth->execute || $form->dberror($query);
  733. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  734. $ref->{address} = "";
  735. $ref->{exchangerate} ||= 1;
  736. for (qw(address1 address2 city state zipcode country)) {
  737. $ref->{address} .= "$ref->{$_} ";
  738. }
  739. $ref->{id} = $ref->{ctid};
  740. push @{ $form->{CT} }, $ref;
  741. }
  742. $sth->finish;
  743. $dbh->commit;
  744. }
  745. sub pricelist {
  746. my ( $self, $myconfig, $form ) = @_;
  747. # connect to database
  748. my $dbh = $form->{dbh};
  749. my $query;
  750. if ( $form->{db} eq 'customer' ) {
  751. $query = qq|SELECT p.id, p.partnumber, p.description,
  752. p.sellprice, pg.partsgroup, p.partsgroup_id,
  753. m.pricebreak, m.sellprice,
  754. m.validfrom, m.validto, m.curr
  755. FROM partscustomer m
  756. JOIN parts p ON (p.id = m.parts_id)
  757. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  758. WHERE m.customer_id = ?
  759. ORDER BY partnumber|;
  760. }
  761. if ( $form->{db} eq 'vendor' ) {
  762. $query = qq|SELECT p.id, p.partnumber AS sku, p.description,
  763. pg.partsgroup, p.partsgroup_id,
  764. m.partnumber, m.leadtime, m.lastcost, m.curr
  765. FROM partsvendor m
  766. JOIN parts p ON (p.id = m.parts_id)
  767. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  768. WHERE m.vendor_id = ?
  769. ORDER BY p.partnumber|;
  770. }
  771. my $sth;
  772. my $ref;
  773. if ( $form->{id} ) {
  774. $sth = $dbh->prepare($query);
  775. $sth->execute( $form->{id} ) || $form->dberror($query);
  776. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  777. push @{ $form->{all_partspricelist} }, $ref;
  778. }
  779. $sth->finish;
  780. }
  781. $query = qq|SELECT value FROM defaults where setting_key = 'curr'|;
  782. ( $form->{currencies} ) = $dbh->selectrow_array($query);
  783. $query = qq|SELECT id, partsgroup
  784. FROM partsgroup
  785. ORDER BY partsgroup|;
  786. $sth = $dbh->prepare($query);
  787. $sth->execute || $self->dberror($query);
  788. $form->{all_partsgroup} = ();
  789. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  790. push @{ $form->{all_partsgroup} }, $ref;
  791. }
  792. $sth->finish;
  793. $dbh->commit;
  794. }
  795. sub save_pricelist {
  796. my ( $self, $myconfig, $form ) = @_;
  797. my $dbh = $form->{dbh};
  798. my $query = qq|
  799. DELETE FROM parts$form->{db}
  800. WHERE $form->{db}_id = ?}|;
  801. $sth = $dbh->prepare($query);
  802. $sth->execute( $form->{id} ) || $form->dberror($query);
  803. foreach $i ( 1 .. $form->{rowcount} ) {
  804. if ( $form->{"id_$i"} ) {
  805. if ( $form->{db} eq 'customer' ) {
  806. for (qw(pricebreak sellprice)) {
  807. $form->{"${_}_$i"} =
  808. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  809. }
  810. $query = qq|
  811. INSERT INTO parts$form->{db}
  812. (parts_id, customer_id,
  813. pricebreak, sellprice,
  814. validfrom, validto, curr)
  815. VALUES (?, ?, ?, ?, ?, ?, ?)|;
  816. @queryargs = (
  817. $form->{"id_$i"}, $form->{id},
  818. $form->{"pricebreak_$i"}, $form->{"sellprice_$i"},
  819. $form->{"validfrom_$i"}, $form->{"validto_$i"},
  820. $form->{"curr_$i"}
  821. );
  822. }
  823. else {
  824. for (qw(leadtime lastcost)) {
  825. $form->{"${_}_$i"} =
  826. $form->parse_amount( $myconfig, $form->{"${_}_$i"} );
  827. }
  828. $query = qq|
  829. INSERT INTO parts$form->{db}
  830. (parts_id, vendor_id,
  831. partnumber, lastcost,
  832. leadtime, curr)
  833. VALUES (?, ?, ?, ?, ?, ?)|;
  834. @queryargs = (
  835. $form->{"id_$i"}, $form->{id},
  836. $form->{"partnumber_$i"}, $form->{"lastcost_$i"},
  837. $form->{"leadtime_$i"}, $form->{"curr_$i"}
  838. );
  839. }
  840. $sth = $dbh->prepare($query);
  841. $sth->execute(@queryargs) || $form->dberror($query);
  842. }
  843. }
  844. $_ = $dbh->commit;
  845. }
  846. sub retrieve_item {
  847. my ( $self, $myconfig, $form ) = @_;
  848. # connect to database
  849. my $dbh = $form->{dbh};
  850. my $i = $form->{rowcount};
  851. my $var;
  852. my $null;
  853. my $where = "WHERE p.obsolete = '0'";
  854. if ( $form->{db} eq 'vendor' ) {
  855. # parts, services, labor
  856. $where .= " AND p.assembly = '0'";
  857. }
  858. if ( $form->{db} eq 'customer' ) {
  859. # parts, assemblies, services
  860. $where .= " AND p.income_accno_id > 0";
  861. }
  862. if ( $form->{"partnumber_$i"} ne "" ) {
  863. $var = $dbh->quote( $form->like( lc $form->{"partnumber_$i"} ) );
  864. $where .= " AND lower(p.partnumber) LIKE $var";
  865. }
  866. if ( $form->{"description_$i"} ne "" ) {
  867. $var = $dbh->quote( $form->like( lc $form->{"description_$i"} ) );
  868. $where .= " AND lower(p.description) LIKE $var";
  869. }
  870. if ( $form->{"partsgroup_$i"} ne "" ) {
  871. ( $null, $var ) = split /--/, $form->{"partsgroup_$i"};
  872. $var = $dbh->quote($var);
  873. $where .= qq| AND p.partsgroup_id = $var|;
  874. }
  875. my $query = qq|
  876. SELECT p.id, p.partnumber, p.description, p.sellprice,
  877. p.lastcost, p.unit, pg.partsgroup, p.partsgroup_id
  878. FROM parts p
  879. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  880. $where
  881. ORDER BY partnumber|;
  882. my $sth = $dbh->prepare($query);
  883. $sth->execute || $form->dberror($query);
  884. my $ref;
  885. $form->{item_list} = ();
  886. while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  887. push @{ $form->{item_list} }, $ref;
  888. }
  889. $sth->finish;
  890. $dbh->commit;
  891. }
  892. 1;