summaryrefslogtreecommitdiff
path: root/LedgerSMB/CT.pm
blob: f3df8113cf115aba1612b8d5baeea07b0fee6b44 (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. if (!$form->{startdate}){
  268. undef $form->{startdate};
  269. }
  270. if (!$form->{enddate}){
  271. undef $form->{enddate};
  272. }
  273. $sth->execute(
  274. $form->{customernumber}, $form->{name}, $form->{address1},
  275. $form->{address2}, $form->{city}, $form->{state},
  276. $form->{zipcode}, $form->{country}, $form->{contact},
  277. $form->{phone}, $form->{fax}, $form->{email}, $form->{cc},
  278. $form->{bcc}, $form->{notes}, $form->{discount},
  279. $form->{creditlimit}, $form->{terms}, $form->{taxincluded},
  280. $business_id, $form->{taxnumber}, $form->{sic_code},
  281. $form->{iban}, $form->{bic}, $employee_id, $pricegroup_id,
  282. $language_code,
  283. $form->{curr}, $form->{startdate}, $form->{enddate},
  284. $form->{id})
  285. || $form->dberror($query);
  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 (! $dbh->selectrow_array($query)) {
  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. } else {
  349. my $uid = localtime;
  350. $uid .= "$$";
  351. $query = qq|INSERT INTO vendor (name)
  352. VALUES ('$uid')|;
  353. $dbh->do($query) || $form->dberror($query);
  354. $query = qq|SELECT id
  355. FROM vendor
  356. WHERE name = '$uid'|;
  357. ($form->{id}) = $dbh->selectrow_array($query);
  358. }
  359. my $employee_id;
  360. ($null, $employee_id) = split /--/, $form->{employee};
  361. $employee_id *= 1;
  362. my $pricegroup_id;
  363. ($null, $pricegroup_id) = split /--/, $form->{pricegroup};
  364. $pricegroup_id *= 1;
  365. my $business_id;
  366. ($null, $business_id) = split /--/, $form->{business};
  367. $business_id *= 1;
  368. my $language_code;
  369. ($null, $language_code) = split /--/, $form->{language};
  370. $form->{vendornumber} = $form->update_defaults($myconfig, "vendornumber", $dbh) if ! $form->{vendornumber};
  371. $query = qq|
  372. UPDATE vendor
  373. SET vendornumber = ?,
  374. name = ?,
  375. address1 = ?,
  376. address2 = ?,
  377. city = ?,
  378. state = ?,
  379. zipcode = ?,
  380. country = ?,
  381. contact = ?,
  382. phone = ?,
  383. fax = ?,
  384. email = ?,
  385. cc = ?,
  386. bcc = ?,
  387. notes = ?,
  388. discount = ?,
  389. creditlimit = ?,
  390. terms = ?,
  391. taxincluded = ?,
  392. gifi_accno = ?,
  393. business_id = ?,
  394. taxnumber = ?,
  395. sic_code = ?,
  396. iban = ?,
  397. bic = ?,
  398. employee_id = ?,
  399. language_code = ?,
  400. pricegroup_id = ?,
  401. curr = ?,
  402. startdate = ?,
  403. enddate = ?
  404. WHERE id = ?|;
  405. $sth = $dbh->prepare($query);
  406. $sth->execute(
  407. $form->{vendornumber}, $form->{name}, $form->{address1},
  408. $form->{address2}, $form->{city}, $form->{state},
  409. $form->{zipcode}, $form->{country}, $form->{contact},
  410. $form->{phone}, $form->{fax}, $form->{email}, $form->{cc},
  411. $form->{bcc}, $form->{notes}, $form->{discount},
  412. $form->{creditlimit}, $form->{terms}, $form->{taxincluded},
  413. $form->{gifi_accno}, $business_id, $form->{taxnumber},
  414. $form->{sic_code}, $form->{iban}, $form->{bic}, $employee_id,
  415. $language_code, $pricegroup_id,
  416. $form->{curr}, $form->{startdate}, $form->{enddate},
  417. $form->{id})
  418. || $form->dberror($query);
  419. # save taxes
  420. foreach $item (split / /, $form->{taxaccounts}) {
  421. if ($form->{"tax_$item"}) {
  422. $query = qq|
  423. INSERT INTO vendortax (vendor_id, chart_id)
  424. VALUES (?, (SELECT id
  425. FROM chart
  426. WHERE accno = ?))|;
  427. $sth = $dbh->prepare($query);
  428. $sth->execute($form->{id}, $item)
  429. || $form->dberror($query);
  430. }
  431. }
  432. # add shipto
  433. $form->add_shipto($dbh, $form->{id});
  434. $dbh->commit;
  435. }
  436. sub delete {
  437. my ($self, $myconfig, $form) = @_;
  438. # connect to database
  439. my $dbh = $form->{dbh};
  440. # delete customer/vendor
  441. my $query = qq|DELETE FROM $form->{db}
  442. WHERE id = ?|;
  443. $sth = $dbh->prepare($query);
  444. $sth->execute($form->{id}) || $form->dberror($query);
  445. $dbh->commit;
  446. }
  447. sub search {
  448. my ($self, $myconfig, $form) = @_;
  449. # connect to database
  450. my $dbh = $form->dbconnect($myconfig);
  451. my $where = "1 = 1";
  452. $form->{sort} = ($form->{sort}) ? $form->{sort} : "name";
  453. my @a = qw(name);
  454. my $sortorder = $form->sort_order(\@a);
  455. my $var;
  456. my $item;
  457. @a = ("$form->{db}number");
  458. push @a, qw(name contact city state zipcode country notes phone email);
  459. if ($form->{employee}) {
  460. $var = $form->like(lc $form->{employee});
  461. $where .= " AND lower(e.name) LIKE '$var'";
  462. }
  463. foreach $item (@a) {
  464. if ($form->{$item} ne "") {
  465. $var = $form->like(lc $form->{$item});
  466. $where .= " AND lower(ct.$item) LIKE '$var'";
  467. }
  468. }
  469. if ($form->{address} ne "") {
  470. $var = $dbh->quote($form->like(lc $form->{address}));
  471. $where .= " AND (lower(ct.address1) LIKE $var OR lower(ct.address2) LIKE '$var')";
  472. }
  473. if ($form->{startdatefrom}) {
  474. $where .= " AND ct.startdate >= ".
  475. $dbh->quote($form->{startdatefrom});
  476. }
  477. if ($form->{startdateto}) {
  478. $where .= " AND ct.startdate <= ".
  479. $dbh->quote($form->{startdateto});
  480. }
  481. if ($form->{status} eq 'active') {
  482. $where .= " AND ct.enddate IS NULL";
  483. }
  484. if ($form->{status} eq 'inactive') {
  485. $where .= " AND ct.enddate <= current_date";
  486. }
  487. if ($form->{status} eq 'orphaned') {
  488. $where .= qq|
  489. AND ct.id NOT IN (SELECT o.$form->{db}_id
  490. FROM oe o, $form->{db} vc
  491. WHERE vc.id = o.$form->{db}_id)|;
  492. if ($form->{db} eq 'customer') {
  493. $where .= qq| AND ct.id NOT IN (SELECT a.customer_id
  494. FROM ar a, customer vc
  495. WHERE vc.id = a.customer_id)|;
  496. }
  497. if ($form->{db} eq 'vendor') {
  498. $where .= qq| AND ct.id NOT IN (SELECT a.vendor_id
  499. FROM ap a, vendor vc
  500. WHERE vc.id = a.vendor_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 employee e ON (ct.employee_id = e.id)
  511. LEFT JOIN employee 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. my ($ar, $union, $module);
  521. $query = "";
  522. my $transwhere;
  523. my $openarap = "";
  524. my $openoe = "";
  525. if ($form->{open} || $form->{closed}) {
  526. unless ($form->{open} && $form->{closed}) {
  527. $openarap = " AND a.amount != a.paid"
  528. if $form->{open};
  529. $openarap = " AND a.amount = a.paid"
  530. if $form->{closed};
  531. $openoe = " AND o.closed = '0'"
  532. if $form->{open};
  533. $openoe = " AND o.closed = '1'"
  534. if $form->{closed};
  535. }
  536. }
  537. if ($form->{l_transnumber}) {
  538. $ar = ($form->{db} eq 'customer') ? 'ar' : 'ap';
  539. $module = $ar;
  540. $transwhere = "";
  541. $transwhere .= " AND a.transdate >= "
  542. .$dbh->quote($form->{transdatefrom})
  543. if $form->{transdatefrom};
  544. $transwhere .= " AND a.transdate <= ".
  545. $dbh->quote($form->{transdateto})
  546. if $form->{transdateto};
  547. $query = qq|
  548. SELECT ct.*, b.description AS business,
  549. a.invnumber, a.ordnumber,
  550. a.quonumber,
  551. a.id AS invid, '$ar' AS module,
  552. 'invoice' AS formtype,
  553. (a.amount = a.paid) AS closed,
  554. a.amount,
  555. a.netamount, e.name AS employee,
  556. m.name AS manager
  557. FROM $form->{db} ct
  558. JOIN $ar a ON (a.$form->{db}_id = ct.id)
  559. LEFT JOIN business b ON (ct.business_id = b.id)
  560. LEFT JOIN employee e ON (a.employee_id = e.id)
  561. LEFT JOIN employee m ON (m.id = e.managerid)
  562. WHERE $where
  563. AND a.invoice = '0'
  564. $transwhere
  565. $openarap |;
  566. $union = qq| UNION |;
  567. }
  568. if ($form->{l_invnumber}) {
  569. $ar = ($form->{db} eq 'customer') ? 'ar' : 'ap';
  570. $module = ($ar eq 'ar') ? 'is' : 'ir';
  571. $transwhere = "";
  572. $transwhere .= " AND a.transdate >= ".
  573. $dbh->quote($form->{transdatefrom})
  574. if $form->{transdatefrom};
  575. $transwhere .= " AND a.transdate <= ".
  576. $dbh->quote($form->{transdateto})
  577. if $form->{transdateto};
  578. $query .= qq|
  579. $union
  580. SELECT ct.*, b.description AS business,
  581. a.invnumber, a.ordnumber, a.quonumber,
  582. a.id AS invid,
  583. '$module' AS module,
  584. 'invoice' AS formtype,
  585. (a.amount = a.paid) AS closed,
  586. a.amount, a.netamount,
  587. e.name AS employee, m.name AS manager
  588. FROM $form->{db} ct
  589. JOIN $ar a ON (a.$form->{db}_id = ct.id)
  590. LEFT JOIN business b ON (ct.business_id = b.id)
  591. LEFT JOIN employee e ON (a.employee_id = e.id)
  592. LEFT JOIN employee m ON (m.id = e.managerid)
  593. WHERE $where
  594. AND a.invoice = '1'
  595. $transwhere
  596. $openarap |;
  597. $union = qq| UNION|;
  598. }
  599. if ($form->{l_ordnumber}) {
  600. $transwhere = "";
  601. $transwhere .= " AND o.transdate >= ".
  602. $dbh->quote($form->{transdatefrom})
  603. if $form->{transdatefrom};
  604. $transwhere .= " AND o.transdate <= ".
  605. $dbh->quote($form->{transdateto})
  606. if $form->{transdateto};
  607. $query .= qq|
  608. $union
  609. SELECT ct.*, b.description AS business,
  610. ' ' AS invnumber, o.ordnumber,
  611. o.quonumber, o.id AS invid,
  612. 'oe' AS module, 'order' AS formtype,
  613. o.closed, o.amount, o.netamount,
  614. e.name AS employee, m.name AS manager
  615. FROM $form->{db} ct
  616. JOIN oe o ON (o.$form->{db}_id = ct.id)
  617. LEFT JOIN business b ON (ct.business_id = b.id)
  618. LEFT JOIN employee e ON (o.employee_id = e.id)
  619. LEFT JOIN employee m ON (m.id = e.managerid)
  620. WHERE $where
  621. AND o.quotation = '0'
  622. $transwhere
  623. $openoe |;
  624. $union = qq| UNION|;
  625. }
  626. if ($form->{l_quonumber}) {
  627. $transwhere = "";
  628. $transwhere .= " AND o.transdate >= ".
  629. $dbh->quote($form->{transdatefrom})
  630. if $form->{transdatefrom};
  631. $transwhere .= " AND o.transdate <= ".
  632. $dbh->quote($form->{transdateto})
  633. if $form->{transdateto};
  634. $query .= qq|
  635. $union
  636. SELECT ct.*, b.description AS business,
  637. ' ' AS invnumber, o.ordnumber,
  638. o.quonumber, o.id AS invid,
  639. 'oe' AS module,
  640. 'quotation' AS formtype,
  641. o.closed, o.amount, o.netamount,
  642. e.name AS employee, m.name AS manager
  643. FROM $form->{db} ct
  644. JOIN oe o ON (o.$form->{db}_id = ct.id)
  645. LEFT JOIN business b ON (ct.business_id = b.id)
  646. LEFT JOIN employee e ON (o.employee_id = e.id)
  647. LEFT JOIN employee m ON (m.id = e.managerid)
  648. WHERE $where
  649. AND o.quotation = '1'
  650. $transwhere
  651. $openoe |;
  652. }
  653. $sortorder .= ", invid";
  654. }
  655. $query .= qq| ORDER BY $sortorder|;
  656. my $sth = $dbh->prepare($query);
  657. $sth->execute || $form->dberror($query);
  658. # accounts
  659. $query = qq|
  660. SELECT c.accno
  661. FROM chart c
  662. JOIN $form->{db}tax t ON (t.chart_id = c.id)
  663. WHERE t.$form->{db}_id = ?|;
  664. my $tth = $dbh->prepare($query);
  665. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  666. $tth->execute($ref->{id});
  667. while (($item) = $tth->fetchrow_array) {
  668. $ref->{taxaccount} .= "$item ";
  669. }
  670. $tth->finish;
  671. chop $ref->{taxaccount};
  672. $ref->{address} = "";
  673. for (qw(address1 address2 city state zipcode country)) {
  674. $ref->{address} .= "$ref->{$_} ";
  675. }
  676. push @{ $form->{CT} }, $ref;
  677. }
  678. $sth->finish;
  679. $dbh->commit;
  680. }
  681. sub get_history {
  682. my ($self, $myconfig, $form) = @_;
  683. # connect to database
  684. my $dbh = $form->dbconnect($myconfig);
  685. my $query;
  686. my $where = "1 = 1";
  687. $form->{sort} = "partnumber" unless $form->{sort};
  688. my $sortorder = $form->{sort};
  689. my %ordinal = ();
  690. my $var;
  691. my $table;
  692. # setup ASC or DESC
  693. $form->sort_order();
  694. if ($form->{"$form->{db}number"} ne "") {
  695. $var = $dbh->($form->like(lc $form->{"$form->{db}number"}));
  696. $where .= " AND lower(ct.$form->{db}number) LIKE $var";
  697. }
  698. if ($form->{address} ne "") {
  699. $var = $dbh->quote($form->like(lc $form->{address}));
  700. $where .= " AND lower(ct.address1) LIKE $var";
  701. }
  702. for (qw(name contact email phone notes city state zipcode country)) {
  703. if ($form->{$_} ne "") {
  704. $var = $dbh->quote($form->like(lc $form->{$_}));
  705. $where .= " AND lower(ct.$_) LIKE $var";
  706. }
  707. }
  708. if ($form->{employee} ne "") {
  709. $var = $form->like(lc $form->{employee});
  710. $where .= " AND lower(e.name) LIKE '$var'";
  711. }
  712. $transwhere .= " AND a.transdate >= ".
  713. $dbh->quote($form->{transdatefrom})
  714. if $form->{transdatefrom};
  715. $transwhere .= " AND a.transdate <= ".
  716. $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. } else {
  726. $where .= " AND a.closed = '0'"
  727. if $form->{open};
  728. $where .= " AND a.closed = '1'"
  729. if $form->{closed};
  730. }
  731. }
  732. }
  733. my $invnumber = 'invnumber';
  734. my $deldate = 'deliverydate';
  735. my $buysell;
  736. my $sellprice = "sellprice";
  737. if ($form->{db} eq 'customer') {
  738. $buysell = "buy";
  739. if ($form->{type} eq 'invoice') {
  740. $where .= qq|
  741. AND a.invoice = '1' AND i.assemblyitem = '0'|;
  742. $table = 'ar';
  743. $sellprice = "fxsellprice";
  744. } else {
  745. $table = 'oe';
  746. if ($form->{type} eq 'order') {
  747. $invnumber = 'ordnumber';
  748. $where .= qq| AND a.quotation = '0'|;
  749. } else {
  750. $invnumber = 'quonumber';
  751. $where .= qq| AND a.quotation = '1'|;
  752. }
  753. $deldate = 'reqdate';
  754. }
  755. }
  756. if ($form->{db} eq 'vendor') {
  757. $buysell = "sell";
  758. if ($form->{type} eq 'invoice') {
  759. $where .= qq| AND a.invoice = '1' AND i.assemblyitem = '0'|;
  760. $table = 'ap';
  761. $sellprice = "fxsellprice";
  762. } else {
  763. $table = 'oe';
  764. if ($form->{type} eq 'order') {
  765. $invnumber = 'ordnumber';
  766. $where .= qq| AND a.quotation = '0'|;
  767. } else {
  768. $invnumber = 'quonumber';
  769. $where .= qq| AND a.quotation = '1'|;
  770. }
  771. $deldate = 'reqdate';
  772. }
  773. }
  774. my $invjoin = qq| JOIN invoice i ON (i.trans_id = a.id)|;
  775. if ($form->{type} eq 'order') {
  776. $invjoin = qq| JOIN orderitems i ON (i.trans_id = a.id)|;
  777. }
  778. if ($form->{type} eq 'quotation') {
  779. $invjoin = qq| JOIN orderitems i ON (i.trans_id = a.id)|;
  780. $where .= qq| AND a.quotation = '1'|;
  781. }
  782. %ordinal = ( partnumber => 9,
  783. description => 12,
  784. "$deldate" => 16,
  785. serialnumber => 17,
  786. projectnumber => 18 );
  787. $sortorder = "2 $form->{direction}, 1, 11, $ordinal{$sortorder} $form->{direction}";
  788. $query = qq|
  789. SELECT ct.id AS ctid, ct.name, ct.address1,
  790. ct.address2, ct.city, ct.state,
  791. p.id AS pid, p.partnumber, a.id AS invid,
  792. a.$invnumber, a.curr, i.description,
  793. i.qty, i.$sellprice AS sellprice, i.discount,
  794. i.$deldate, i.serialnumber, pr.projectnumber,
  795. e.name AS employee, ct.zipcode, ct.country, i.unit,
  796. (SELECT $buysell
  797. FROM exchangerate ex
  798. WHERE a.curr = ex.curr
  799. AND a.transdate = ex.transdate) AS exchangerate
  800. FROM $form->{db} ct
  801. JOIN $table a ON (a.$form->{db}_id = ct.id)
  802. $invjoin
  803. JOIN parts p ON (p.id = i.parts_id)
  804. LEFT JOIN project pr ON (pr.id = i.project_id)
  805. LEFT JOIN employee e ON (e.id = a.employee_id)
  806. WHERE $where
  807. ORDER BY $sortorder|;
  808. my $sth = $dbh->prepare($query);
  809. $sth->execute || $form->dberror($query);
  810. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  811. $ref->{address} = "";
  812. $ref->{exchangerate} ||= 1;
  813. for (qw(address1 address2 city state zipcode country)) { $ref->{address} .= "$ref->{$_} " }
  814. $ref->{id} = $ref->{ctid};
  815. push @{ $form->{CT} }, $ref;
  816. }
  817. $sth->finish;
  818. $dbh->commit;
  819. }
  820. sub pricelist {
  821. my ($self, $myconfig, $form) = @_;
  822. # connect to database
  823. my $dbh = $form->{dbh};
  824. my $query;
  825. if ($form->{db} eq 'customer') {
  826. $query = qq|SELECT p.id, p.partnumber, p.description,
  827. p.sellprice, pg.partsgroup, p.partsgroup_id,
  828. m.pricebreak, m.sellprice,
  829. m.validfrom, m.validto, m.curr
  830. FROM partscustomer m
  831. JOIN parts p ON (p.id = m.parts_id)
  832. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  833. WHERE m.customer_id = ?
  834. ORDER BY partnumber|;
  835. }
  836. if ($form->{db} eq 'vendor') {
  837. $query = qq|SELECT p.id, p.partnumber AS sku, p.description,
  838. pg.partsgroup, p.partsgroup_id,
  839. m.partnumber, m.leadtime, m.lastcost, m.curr
  840. FROM partsvendor 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.vendor_id = ?
  844. ORDER BY p.partnumber|;
  845. }
  846. my $sth;
  847. my $ref;
  848. if ($form->{id}) {
  849. $sth = $dbh->prepare($query);
  850. $sth->execute($form->{id}) || $form->dberror($query);
  851. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  852. push @{ $form->{all_partspricelist} }, $ref;
  853. }
  854. $sth->finish;
  855. }
  856. $query = qq|SELECT curr FROM defaults|;
  857. ($form->{currencies}) = $dbh->selectrow_array($query);
  858. $query = qq|SELECT id, partsgroup
  859. FROM partsgroup
  860. ORDER BY partsgroup|;
  861. $sth = $dbh->prepare($query);
  862. $sth->execute || $self->dberror($query);
  863. $form->{all_partsgroup} = ();
  864. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  865. push @{ $form->{all_partsgroup} }, $ref;
  866. }
  867. $sth->finish;
  868. $dbh->connect;
  869. }
  870. sub save_pricelist {
  871. my ($self, $myconfig, $form) = @_;
  872. my $dbh = $form->dbconnect_noauto($myconfig);
  873. my $query = qq|
  874. DELETE FROM parts$form->{db}
  875. WHERE $form->{db}_id = ?}|;
  876. $sth = $dbh->prepare($query);
  877. $sth->execute($form->{id}) || $form->dberror($query);
  878. foreach $i (1 .. $form->{rowcount}) {
  879. if ($form->{"id_$i"}) {
  880. if ($form->{db} eq 'customer') {
  881. for (qw(pricebreak sellprice)) {
  882. $form->{"${_}_$i"} =
  883. $form->parse_amount(
  884. $myconfig,
  885. $form->{"${_}_$i"});
  886. }
  887. $query = qq|
  888. INSERT INTO parts$form->{db}
  889. (parts_id, customer_id,
  890. pricebreak, sellprice,
  891. validfrom, validto, curr)
  892. VALUES (?, ?, ?, ?, ?, ?, ?)|;
  893. @queryargs = ($form->{"id_$i"}, $form->{id},
  894. $form->{"pricebreak_$i"},
  895. $form->{"sellprice_$i"},
  896. $form->{"validfrom_$i"},
  897. $form->{"validto_$i"},
  898. $form->{"curr_$i"});
  899. } else {
  900. for (qw(leadtime lastcost)) {
  901. $form->{"${_}_$i"} =
  902. $form->parse_amount(
  903. $myconfig,
  904. $form->{"${_}_$i"})
  905. }
  906. $query = qq|
  907. INSERT INTO parts$form->{db}
  908. (parts_id, vendor_id,
  909. partnumber, lastcost,
  910. leadtime, curr)
  911. VALUES (?, ?, ?, ?, ?, ?)|;
  912. @queryargs = ($form->{"id_$i"}, $form->{id},
  913. $form->{"partnumber_$i"},
  914. $form->{"lastcost_$i"},
  915. $form->{"leadtime_$i"},
  916. $form->{"curr_$i"});
  917. }
  918. $sth = $dbh->prepare($query);
  919. $sth->execute(@queryargs) || $form->dberror($query);
  920. }
  921. }
  922. $_ = $dbh->commit;
  923. }
  924. sub retrieve_item {
  925. my ($self, $myconfig, $form) = @_;
  926. # connect to database
  927. my $dbh = $form->dbconnect($myconfig);
  928. my $i = $form->{rowcount};
  929. my $var;
  930. my $null;
  931. my $where = "WHERE p.obsolete = '0'";
  932. if ($form->{db} eq 'vendor') {
  933. # parts, services, labor
  934. $where .= " AND p.assembly = '0'";
  935. }
  936. if ($form->{db} eq 'customer') {
  937. # parts, assemblies, services
  938. $where .= " AND p.income_accno_id > 0";
  939. }
  940. if ($form->{"partnumber_$i"} ne "") {
  941. $var = $dbh->quote($form->like(lc $form->{"partnumber_$i"}));
  942. $where .= " AND lower(p.partnumber) LIKE $var";
  943. }
  944. if ($form->{"description_$i"} ne "") {
  945. $var = $dbh->quote($form->like(lc $form->{"description_$i"}));
  946. $where .= " AND lower(p.description) LIKE $var";
  947. }
  948. if ($form->{"partsgroup_$i"} ne "") {
  949. ($null, $var) = split /--/, $form->{"partsgroup_$i"};
  950. $var = $dbh->quote($var);
  951. $where .= qq| AND p.partsgroup_id = $var|;
  952. }
  953. my $query = qq|
  954. SELECT p.id, p.partnumber, p.description, p.sellprice,
  955. p.lastcost, p.unit, pg.partsgroup, p.partsgroup_id
  956. FROM parts p
  957. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  958. $where
  959. ORDER BY partnumber|;
  960. my $sth = $dbh->prepare($query);
  961. $sth->execute || $form->dberror($query);
  962. my $ref;
  963. $form->{item_list} = ();
  964. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  965. push @{ $form->{item_list} }, $ref;
  966. }
  967. $sth->finish;
  968. $dbh->commit;
  969. }
  970. 1;