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