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