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