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