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