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