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