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