summaryrefslogtreecommitdiff
path: root/bin/ct.pl
blob: 91f13eb414d9352bb21e7fb80a0f2e5b50776797 (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # http://www.ledgersmb.org/
  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) 2001
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors: Reed White <alta@alta-research.com>
  22. #
  23. #
  24. #
  25. # This program is free software; you can redistribute it and/or modify
  26. # it under the terms of the GNU General Public License as published by
  27. # the Free Software Foundation; either version 2 of the License, or
  28. # (at your option) any later version.
  29. #
  30. # This program is distributed in the hope that it will be useful,
  31. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. # GNU General Public License for more details.
  34. # You should have received a copy of the GNU General Public License
  35. # along with this program; if not, write to the Free Software
  36. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  37. #======================================================================
  38. #
  39. # customer/vendor module
  40. #
  41. #======================================================================
  42. use LedgerSMB::CT;
  43. use LedgerSMB::Template;
  44. 1;
  45. # end of main
  46. sub add {
  47. $form->{title} = "Add";
  48. # $locale->text('Add Customer')
  49. # $locale->text('Add Vendor')
  50. $form->{callback} =
  51. "$form->{script}?action=add&db=$form->{db}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}"
  52. unless $form->{callback};
  53. CT->create_links( \%myconfig, \%$form );
  54. &form_header;
  55. &form_footer;
  56. }
  57. sub history {
  58. # $locale->text('Customer History')
  59. # $locale->text('Vendor History')
  60. my %hiddens;
  61. my @buttons;
  62. $history = 1;
  63. $label = ucfirst $form->{db};
  64. $label .= " History";
  65. if ( $form->{db} eq 'customer' ) {
  66. $form->{invlabel} = $locale->text('Sales Invoices');
  67. $form->{ordlabel} = $locale->text('Sales Orders');
  68. $form->{quolabel} = $locale->text('Quotations');
  69. } else {
  70. $form->{invlabel} = $locale->text('Vendor Invoices');
  71. $form->{ordlabel} = $locale->text('Purchase Orders');
  72. $form->{quolabel} = $locale->text('Request for Quotations');
  73. }
  74. $form->{title} = $locale->text($label);
  75. $form->{nextsub} = "list_history";
  76. &search_name(\%hiddens, \@buttons);
  77. ##SC: Temporary removal
  78. ## if ( $form->{lynx} ) {
  79. ## require "bin/menu.pl";
  80. ## &menubar;
  81. ## }
  82. my $template = LedgerSMB::Template->new_UI(
  83. user => \%myconfig,
  84. locale => $locale,
  85. template => 'ct-search',
  86. );
  87. $template->render({
  88. form => $form,
  89. user => \%myconfig,
  90. hiddens => \%hiddens,
  91. buttons => \@buttons,
  92. });
  93. }
  94. sub transactions {
  95. if ( $form->{db} eq 'customer' ) {
  96. $form->{translabel} = $locale->text('AR Transactions');
  97. $form->{invlabel} = $locale->text('Sales Invoices');
  98. $form->{ordlabel} = $locale->text('Sales Orders');
  99. $form->{quolabel} = $locale->text('Quotations');
  100. } else {
  101. $form->{translabel} = $locale->text('AP Transactions');
  102. $form->{invlabel} = $locale->text('Vendor Invoices');
  103. $form->{ordlabel} = $locale->text('Purchase Orders');
  104. $form->{quolabel} = $locale->text('Request for Quotations');
  105. }
  106. }
  107. sub include_in_report {
  108. $label = ucfirst $form->{db};
  109. my @fields = ();
  110. push @fields, {
  111. name => 'l_ndx',
  112. label => $locale->text('No.'),
  113. };
  114. push @fields, {
  115. name => 'l_id',
  116. label => $locale->text('ID'),
  117. };
  118. push @fields, {
  119. name => "l_$form->{db}number",
  120. label => $locale->text($label . ' Number'),
  121. };
  122. push @fields, {
  123. name => 'l_name',
  124. label => $locale->text('Company Name'),
  125. $form->{l_name} => $form->{l_name},
  126. };
  127. push @fields, {
  128. name => 'l_contact',
  129. label => $locale->text('Contact'),
  130. $form->{l_contact} => $form->{l_contact},
  131. };
  132. push @fields, {
  133. name => 'l_email',
  134. label => $locale->text('E-mail'),
  135. $form->{l_email} => $form->{l_email},
  136. };
  137. push @fields, {
  138. name => 'l_address',
  139. label => $locale->text('Address'),
  140. };
  141. push @fields, {
  142. name => 'l_city',
  143. label => $locale->text('City'),
  144. };
  145. push @fields, {
  146. name => 'l_state',
  147. label => $locale->text('State/Province'),
  148. };
  149. push @fields, {
  150. name => 'l_zipcode',
  151. label => $locale->text('Zip/Postal Code'),
  152. };
  153. push @fields, {
  154. name => 'l_country',
  155. label => $locale->text('Country'),
  156. };
  157. push @fields, {
  158. name => 'l_phone',
  159. label => $locale->text('Phone'),
  160. $form->{l_phone} => $form->{l_phone},
  161. };
  162. push @fields, {
  163. name => 'l_fax',
  164. label => $locale->text('Fax'),
  165. };
  166. push @fields, {
  167. name => 'l_cc',
  168. label => $locale->text('Cc'),
  169. };
  170. if ( $myconfig{role} =~ /(admin|manager)/ ) {
  171. push @fields, {
  172. name => 'l_bcc',
  173. label => $locale->text('Bcc'),
  174. };
  175. }
  176. push @fields, {
  177. name => 'l_notes',
  178. label => $locale->text('Notes'),
  179. };
  180. push @fields, {
  181. name => 'l_discount',
  182. label => $locale->text('Discount'),
  183. };
  184. push @fields, {
  185. name => 'l_taxaccount',
  186. label => $locale->text('Tax Account'),
  187. };
  188. push @fields, {
  189. name => 'l_taxnumber',
  190. label => $locale->text('Tax Number'),
  191. };
  192. if ( $form->{db} eq 'customer' ) {
  193. push @fields, {
  194. name => 'l_employee',
  195. label => $locale->text('Salesperson'),
  196. };
  197. push @fields, {
  198. name => 'l_manager',
  199. label => $locale->text('Manager'),
  200. };
  201. push @fields, {
  202. name => 'l_pricegroup',
  203. label => $locale->text('Pricegroup'),
  204. };
  205. } else {
  206. push @fields, {
  207. name => 'l_employee',
  208. label => $locale->text('Employee'),
  209. };
  210. push @fields, {
  211. name => 'l_manager',
  212. label => $locale->text('Manager'),
  213. };
  214. push @fields, {
  215. name => 'l_gifi_accno',
  216. label => $locale->text('GIFI'),
  217. };
  218. }
  219. push @fields, {
  220. name => 'l_sic_code',
  221. label => $locale->text('SIC'),
  222. };
  223. push @fields, {
  224. name => 'l_iban',
  225. label => $locale->text('IBAN'),
  226. };
  227. push @fields, {
  228. name => 'l_bic',
  229. label => $locale->text('BIC'),
  230. };
  231. push @fields, {
  232. name => 'l_business',
  233. label => $locale->text('Type of Business'),
  234. };
  235. push @fields, {
  236. name => 'l_terms',
  237. label => $locale->text('Terms'),
  238. };
  239. push @fields, {
  240. name => 'l_langauge',
  241. label => $locale->text('Language'),
  242. };
  243. push @fields, {
  244. name => 'l_startdate',
  245. label => $locale->text('Startdate'),
  246. };
  247. push @fields, {
  248. name => 'l_enddate',
  249. label => $locale->text('Enddate'),
  250. };
  251. $form->{includes} = [];
  252. my $i = 0;
  253. while (@fields) {
  254. push @{$form->{includes}}, [];
  255. for ( 1 .. 5 ) {
  256. push @{$form->{includes}[$i]}, shift(@fields);
  257. if ($form->{includes}[$i][$_ - 1]) {
  258. $form->{includes}[$i][$_ - 1]{type} = 'checkbox';
  259. $form->{includes}[$i][$_ - 1]{value} = 'Y';
  260. }
  261. }
  262. $i++;
  263. }
  264. }
  265. sub search {
  266. # $locale->text('Customers')
  267. # $locale->text('Vendors')
  268. my %hiddens;
  269. my @buttons;
  270. $form->{title} = $locale->text('Search') unless $form->{title};
  271. for (qw(name contact phone email)) { $form->{"l_$_"} = 'checked' }
  272. $form->{nextsub} = "list_names";
  273. &transactions;
  274. &include_in_report;
  275. &search_name(\%hiddens, \@buttons);
  276. ##SC: Temporary removal
  277. ## if ( $form->{lynx} ) {
  278. ## require "bin/menu.pl";
  279. ## &menubar;
  280. ## }
  281. my $template = LedgerSMB::Template->new_UI(
  282. user => \%myconfig,
  283. locale => $locale,
  284. template => 'ct-search',
  285. );
  286. $template->render({
  287. form => $form,
  288. user => \%myconfig,
  289. hiddens => \%hiddens,
  290. buttons => \@buttons,
  291. });
  292. }
  293. sub search_name {
  294. my $hiddens = shift;
  295. my $buttons = shift;
  296. my $label = ucfirst $form->{db};
  297. $form->{label} = $label;
  298. $hiddens->{$_} = $form->{$_} foreach qw(db nextsub path login sessionid);
  299. push @{$buttons}, {
  300. name => 'action',
  301. value => 'continue',
  302. text => $locale->text('Continue'),
  303. };
  304. }
  305. sub list_names {
  306. my %hiddens;
  307. my @buttons;
  308. my @options;
  309. CT->search( \%myconfig, \%$form );
  310. my $href =
  311. "$form->{script}?action=list_names&direction=$form->{direction}&oldsort=$form->{oldsort}&db=$form->{db}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&status=$form->{status}&l_subtotal=$form->{l_subtotal}";
  312. $form->sort_order();
  313. my $callback =
  314. "$form->{script}?action=list_names&direction=$form->{direction}&oldsort=$form->{oldsort}&db=$form->{db}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&status=$form->{status}&l_subtotal=$form->{l_subtotal}";
  315. my @columns = $form->sort_columns( id, name, "$form->{db}number", address,
  316. city, state, zipcode, country,
  317. contact, phone, fax, email,
  318. cc, bcc, employee, manager,
  319. notes, discount, terms, taxaccount,
  320. taxnumber, gifi_accno, sic_code, business,
  321. pricegroup, language, iban, bic,
  322. startdate, enddate, invnumber, invamount,
  323. invtax, invtotal, ordnumber, ordamount,
  324. ordtax, ordtotal, quonumber, quoamount,
  325. quotax, quototal
  326. );
  327. unshift @columns, "ndx";
  328. $form->{l_invnumber} = "Y" if $form->{l_transnumber};
  329. foreach my $item (qw(inv ord quo)) {
  330. if ( $form->{"l_${item}number"} ) {
  331. for (qw(amount tax total)) {
  332. $form->{"l_$item$_"} = $form->{"l_$_"};
  333. }
  334. $removeemployee = 1;
  335. $openclosed = 1;
  336. }
  337. }
  338. $form->{open} = $form->{closed} = "" if !$openclosed;
  339. my @column_index;
  340. foreach my $item (@columns) {
  341. if ( $form->{"l_$item"} eq "Y" ) {
  342. push @column_index, $item;
  343. # add column to href and callback
  344. $callback .= "&l_$item=Y";
  345. $href .= "&l_$item=Y";
  346. }
  347. }
  348. foreach my $item (qw(amount tax total transnumber)) {
  349. if ( $form->{"l_$item"} eq "Y" ) {
  350. $callback .= "&l_$item=Y";
  351. $href .= "&l_$item=Y";
  352. }
  353. }
  354. if ( $form->{status} eq 'all' ) {
  355. push @options, $locale->text('All');
  356. } elsif ( $form->{status} eq 'orphaned' ) {
  357. push @options, $locale->text('Orphaned');
  358. } elsif ( $form->{status} eq 'active' ) {
  359. push @options, $locale->text('Active');
  360. } elsif ( $form->{status} eq 'inactive' ) {
  361. push @options, $locale->text('Inactive');
  362. }
  363. if ( $form->{name} ) {
  364. $callback .= "&name=" . $form->escape( $form->{name}, 1 );
  365. $href .= "&name=" . $form->escape( $form->{name} );
  366. push @options, $locale->text('Name: [_1]', $form->{name});
  367. }
  368. if ( $form->{address} ) {
  369. $callback .= "&address=" . $form->escape( $form->{address}, 1 );
  370. $href .= "&address=" . $form->escape( $form->{address} );
  371. push @options, $locale->text('Address: [_1]', $form->{address});
  372. }
  373. if ( $form->{city} ) {
  374. $callback .= "&city=" . $form->escape( $form->{city}, 1 );
  375. $href .= "&city=" . $form->escape( $form->{city} );
  376. push @options, $locale->text('City: [_1]', $form->{city});
  377. }
  378. if ( $form->{state} ) {
  379. $callback .= "&state=" . $form->escape( $form->{state}, 1 );
  380. $href .= "&state=" . $form->escape( $form->{state} );
  381. push @options, $locale->text('State: [_1]', $form->{state});
  382. }
  383. if ( $form->{zipcode} ) {
  384. $callback .= "&zipcode=" . $form->escape( $form->{zipcode}, 1 );
  385. $href .= "&zipcode=" . $form->escape( $form->{zipcode} );
  386. push @options, $locale->text('Zip/Postal Code: [_1]', $form->{zipcode});
  387. }
  388. if ( $form->{country} ) {
  389. $callback .= "&country=" . $form->escape( $form->{country}, 1 );
  390. $href .= "&country=" . $form->escape( $form->{country} );
  391. push @options, $locale->text('Country: [_1]', $form->{country});
  392. }
  393. if ( $form->{contact} ) {
  394. $callback .= "&contact=" . $form->escape( $form->{contact}, 1 );
  395. $href .= "&contact=" . $form->escape( $form->{contact} );
  396. push @options, $locale->text('Contact: [_1]', $form->{contact});
  397. }
  398. if ( $form->{employee} ) {
  399. $callback .= "&employee=" . $form->escape( $form->{employee}, 1 );
  400. $href .= "&employee=" . $form->escape( $form->{employee} );
  401. if ( $form->{db} eq 'customer' ) {
  402. push @options, $locale->text('Salesperson: [_1]', $form->{employee});
  403. } elsif ( $form->{db} eq 'vendor' ) {
  404. push @options, $locale->text('Employee: [_1]', $form->{employee});
  405. }
  406. }
  407. my $fromdate = "";
  408. my $todate = "";
  409. if ( $form->{startdatefrom} ) {
  410. $callback .= "&startdatefrom=$form->{startdatefrom}";
  411. $href .= "&startdatefrom=$form->{startdatefrom}";
  412. $fromdate = $locale->date( \%myconfig, $form->{startdatefrom}, 1 );
  413. }
  414. if ( $form->{startdateto} ) {
  415. $callback .= "&startdateto=$form->{startdateto}";
  416. $href .= "&startdateto=$form->{startdateto}";
  417. $todate = $locale->date( \%myconfig, $form->{startdateto}, 1 );
  418. }
  419. if ( $fromdate || $todate ) {
  420. push @options,
  421. $locale->text('Startdate [_1] - [_2]', $fromdate, $todate);
  422. }
  423. if ( $form->{notes} ) {
  424. $callback .= "&notes=" . $form->escape( $form->{notes}, 1 );
  425. $href .= "&notes=" . $form->escape( $form->{notes} );
  426. push @options, $locale->text('Notes: [_1]', $form->{notes});
  427. }
  428. if ( $form->{"$form->{db}number"} ) {
  429. $callback .=
  430. qq|&$form->{db}number=|
  431. . $form->escape( $form->{"$form->{db}number"}, 1 );
  432. $href .=
  433. "&$form->{db}number=" . $form->escape( $form->{"$form->{db}number"} );
  434. push @options,
  435. $locale->text('Number: [_1]', $form->{"$form->{db}number"});
  436. }
  437. if ( $form->{phone} ) {
  438. $callback .= "&phone=" . $form->escape( $form->{phone}, 1 );
  439. $href .= "&phone=" . $form->escape( $form->{phone} );
  440. push @options, $locale->text('Phone: [_1]', $form->{phone});
  441. }
  442. if ( $form->{email} ) {
  443. $callback .= "&email=" . $form->escape( $form->{email}, 1 );
  444. $href .= "&email=" . $form->escape( $form->{email} );
  445. push @options, $locale->text('E-mail: [_1]', $form->{email});
  446. }
  447. if ( $form->{transdatefrom} ) {
  448. $callback .= "&transdatefrom=$form->{transdatefrom}";
  449. $href .= "&transdatefrom=$form->{transdatefrom}";
  450. push @options, $locale->text('From [_1]',
  451. $locale->date( \%myconfig, $form->{transdatefrom}, 1 ));
  452. }
  453. if ( $form->{transdateto} ) {
  454. $callback .= "&transdateto=$form->{transdateto}";
  455. $href .= "&transdateto=$form->{transdateto}";
  456. if ( $form->{transdatefrom} ) {
  457. pop @options;
  458. push @options, $locale->text('From [_1] To [_2]',
  459. $locale->date( \%myconfig, $form->{transdatefrom}, 1 ),
  460. $locale->date( \%myconfig, $form->{transdateto}, 1 ));
  461. } else {
  462. push @options, $locale->text('To [_1]',
  463. $locale->date( \%myconfig, $form->{transdateto}, 1 ));
  464. }
  465. }
  466. if ( $form->{open} ) {
  467. $callback .= "&open=$form->{open}";
  468. $href .= "&open=$form->{open}";
  469. push @options, $locale->text('Open');
  470. }
  471. if ( $form->{closed} ) {
  472. $callback .= "&closed=$form->{closed}";
  473. $href .= "&closed=$form->{closed}";
  474. push @options, $locale->text('Closed');
  475. }
  476. $form->{callback} = "$callback&sort=$form->{sort}";
  477. $callback = $form->escape( $form->{callback} );
  478. my %column_header;
  479. $column_header{ndx} = ' ';
  480. $column_header{id} = $locale->text('ID');
  481. $column_header{"$form->{db}number"} = {
  482. href => "$href&sort=$form->{db}number",
  483. text => $locale->text('Number')
  484. };
  485. $column_header{name} = {
  486. href => "$href&sort=name",
  487. text => $locale->text('Name')
  488. };
  489. $column_header{address} = $locale->text('Address');
  490. $column_header{city} = {
  491. href => "$href&sort=city",
  492. text => $locale->text('City')
  493. };
  494. $column_header{state} = {
  495. href => "$href&sort=state",
  496. text => $locale->text('State/Province')
  497. };
  498. $column_header{zipcode} = {
  499. href => "$href&sort=zipcode",
  500. text => $locale->text('Zip/Postal Code')
  501. };
  502. $column_header{country} = {
  503. href => "$href&sort=country",
  504. text => $locale->text('Country')
  505. };
  506. $column_header{contact} = {
  507. href => "$href&sort=contact",
  508. text => $locale->text('Contact'),
  509. };
  510. $column_header{phone} = {
  511. href => "$href&sort=phone",
  512. text => $locale->text('Phone')
  513. };
  514. $column_header{fax} = {
  515. href => "$href&sort=fax",
  516. text => $locale->text('Fax')
  517. };
  518. $column_header{email} = {
  519. href => "$href&sort=email",
  520. text => $locale->text('E-mail')
  521. };
  522. $column_header{cc} = {
  523. href => "$href&sort=cc",
  524. text => $locale->text('Cc')
  525. };
  526. $column_header{bcc} = {
  527. href => "$href&sort=cc",
  528. text => $locale->text('Bcc')
  529. };
  530. $column_header{notes} = {
  531. href => "$href&sort=notes",
  532. text => $locale->text('Notes')
  533. };
  534. $column_header{discount} = '%';
  535. $column_header{terms} = $locale->text('Terms');
  536. $column_header{taxnumber} = {
  537. href => "$href&sort=taxnumber",
  538. text => $locale->text('Tax Number')
  539. };
  540. $column_header{taxaccount} = $locale->text('Tax Account');
  541. $column_header{gifi_accno} = {
  542. href => "$href&sort=gifi_accno",
  543. text => $locale->text('GIFI')
  544. };
  545. $column_header{sic_code} = {
  546. href => "$href&sort=sic_code",
  547. text => $locale->text('SIC')
  548. };
  549. $column_header{business} = {
  550. href => "$href&sort=business",
  551. text => $locale->text('Type of Business')
  552. };
  553. $column_header{iban} = $locale->text('IBAN');
  554. $column_header{bic} = $locale->text('BIC');
  555. $column_header{startdate} = {
  556. href => "$href&sort=startdate",
  557. text => $locale->text('Startdate')
  558. };
  559. $column_header{enddate} = {
  560. href => "$href&sort=enddate",
  561. text => $locale->text('Enddate')
  562. };
  563. $column_header{invnumber} = {
  564. href => "$href&sort=invnumber",
  565. text => $locale->text('Invoice')
  566. };
  567. $column_header{ordnumber} = {
  568. href => "$href&sort=ordnumber",
  569. text => $locale->text('Order')
  570. };
  571. $column_header{quonumber} = {
  572. href => "$href&sort=quonumber",
  573. text => $locale->text('Quotation')
  574. };
  575. if ( $form->{db} eq 'customer' ) {
  576. $column_header{employee} = {
  577. href => "$href&sort=employee",
  578. text => $locale->text('Salesperson')
  579. };
  580. } else {
  581. $column_header{employee} = {
  582. href => "$href&sort=employee",
  583. text => $locale->text('Employee')
  584. };
  585. }
  586. $column_header{manager} = {
  587. href => "$href&sort=manager",
  588. text => $locale->text('Manager')
  589. };
  590. $column_header{pricegroup} = {
  591. href => "$href&sort=pricegroup",
  592. text => $locale->text('Pricegroup')
  593. };
  594. $column_header{language} = {
  595. href => "$href&sort=language",
  596. text => $locale->text('Language')
  597. };
  598. $amount = $locale->text('Amount');
  599. $tax = $locale->text('Tax');
  600. $total = $locale->text('Total');
  601. $column_header{invamount} = $amount;
  602. $column_header{ordamount} = $amount;
  603. $column_header{quoamount} = $amount;
  604. $column_header{invtax} = $tax;
  605. $column_header{ordtax} = $tax;
  606. $column_header{quotax} = $tax;
  607. $column_header{invtotal} = $total;
  608. $column_header{ordtotal} = $total;
  609. $column_header{quototal} = $total;
  610. if ( $form->{status} ) {
  611. $label = ucfirst $form->{db} . "s";
  612. $form->{title} = $locale->text($label);
  613. } else {
  614. $label = ucfirst $form->{db};
  615. $form->{title} = $locale->text( $label . " Transactions" );
  616. }
  617. my $ordertype =
  618. ( $form->{db} eq 'customer' ) ? 'sales_order' : 'purchase_order';
  619. my $quotationtype =
  620. ( $form->{db} eq 'customer' ) ? 'sales_quotation' : 'request_quotation';
  621. $subtotal = 0;
  622. my $i = 0;
  623. my @rows;
  624. foreach my $ref ( @{ $form->{CT} } ) {
  625. if ( $ref->{ $form->{sort} } ne $sameitem && $form->{l_subtotal} ) {
  626. # append subtotal
  627. if ($subtotal) {
  628. push @rows, &list_subtotal;
  629. }
  630. }
  631. my %column_data;
  632. if ( $ref->{id} eq $sameid ) {
  633. for (@column_index) { $column_data{$_} = ' ' }
  634. } else {
  635. $i++;
  636. $ref->{notes} =~ s/\r?\n/<br>/g;
  637. for (@column_index) {
  638. $column_data{$_} = $ref->{$_};
  639. }
  640. $column_data{ndx} = $i;
  641. if ( $ref->{ $form->{sort} } eq $sameitem ) {
  642. $column_data{ $form->{sort} } = ' ';
  643. }
  644. $column_data{address} =
  645. "$ref->{address1} $ref->{address2}";
  646. $column_data{name} = {
  647. text => $ref->{name},
  648. href => "$form->{script}?action=edit&id=$ref->{id}&db=$form->{db}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&status=$form->{status}&callback=$callback",
  649. };
  650. $email = "";
  651. if ( $form->{sort} =~ /(email|cc)/ ) {
  652. if ( $ref->{ $form->{sort} } ne $sameitem ) {
  653. $email = 1;
  654. }
  655. } else {
  656. $email = 1;
  657. }
  658. if ($email) {
  659. foreach $item (qw(email cc bcc)) {
  660. if ( $ref->{$item} ) {
  661. $email = $ref->{$item};
  662. $column_data{$item} = {
  663. href => "mailto:$ref->{$item}",
  664. text => $email,
  665. };
  666. }
  667. }
  668. }
  669. }
  670. if ( $ref->{formtype} eq 'invoice' ) {
  671. $column_data{invnumber} = {
  672. href => "$ref->{module}.pl?action=edit&id=$ref->{invid}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback",
  673. text => $ref->{invnumber},
  674. };
  675. $column_data{invamount} =
  676. $form->format_amount( \%myconfig, $ref->{netamount}, 2, ' ' );
  677. $column_data{invtax} = $form->format_amount( \%myconfig,
  678. $ref->{amount} - $ref->{netamount}, 2, ' ' );
  679. $column_data{invtotal} =
  680. $form->format_amount( \%myconfig, $ref->{amount}, 2, ' ' );
  681. $invamountsubtotal += $ref->{netamount};
  682. $invtaxsubtotal += ( $ref->{amount} - $ref->{netamount} );
  683. $invtotalsubtotal += $ref->{amount};
  684. $subtotal = 1;
  685. } elsif ( $ref->{formtype} eq 'order' ) {
  686. $column_data{ordnumber} = {
  687. href => "$ref->{module}.pl?action=edit&id=$ref->{invid}&type=$ordertype&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback",
  688. text => $ref->{ordnumber},
  689. };
  690. $column_data{ordamount} =
  691. $form->format_amount( \%myconfig, $ref->{netamount}, 2, ' ' );
  692. $column_data{ordtax} = $form->format_amount( \%myconfig,
  693. $ref->{amount} - $ref->{netamount}, 2, ' ' );
  694. $column_data{ordtotal} =
  695. $form->format_amount( \%myconfig, $ref->{amount}, 2, ' ' );
  696. $ordamountsubtotal += $ref->{netamount};
  697. $ordtaxsubtotal += ( $ref->{amount} - $ref->{netamount} );
  698. $ordtotalsubtotal += $ref->{amount};
  699. $subtotal = 1;
  700. } elsif ( $ref->{formtype} eq 'quotation' ) {
  701. $column_data{quonumber} = {
  702. href => "$ref->{module}.pl?action=edit&id=$ref->{invid}&type=$quotationtype&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback",
  703. text => $ref->{quonumber},
  704. };
  705. $column_data{quoamount} =
  706. $form->format_amount( \%myconfig, $ref->{netamount}, 2, ' ' );
  707. $column_data{quotax} = $form->format_amount( \%myconfig,
  708. $ref->{amount} - $ref->{netamount}, 2, ' ' );
  709. $column_data{quototal} =
  710. $form->format_amount( \%myconfig, $ref->{amount}, 2, ' ' );
  711. $quoamountsubtotal += $ref->{netamount};
  712. $quotaxsubtotal += ( $ref->{amount} - $ref->{netamount} );
  713. $quototalsubtotal += $ref->{amount};
  714. $subtotal = 1;
  715. }
  716. if ( $sameid ne "$ref->{id}" ) {
  717. if ( $form->{l_discount} ) {
  718. $column_data{discount} =
  719. $form->format_amount( \%myconfig, $ref->{discount} * 100,
  720. "", ' ' );
  721. }
  722. if ( $form->{l_terms} ) {
  723. $column_data{terms} =
  724. $form->format_amount( \%myconfig, $ref->{terms}, "", ' ' );
  725. }
  726. }
  727. $j++;
  728. $j %= 2;
  729. $column_data{i} = $j;
  730. $sameitem = $ref->{$form->{sort}};
  731. $sameid = $ref->{id};
  732. push @rows, \%column_data;
  733. }
  734. if ( $form->{l_subtotal} && $subtotal ) {
  735. push @rows, &list_subtotal;
  736. }
  737. $i = 1;
  738. if ( $myconfig{acs} !~ /AR--AR/ ) {
  739. if ( $form->{db} eq 'customer' ) {
  740. $button{'AR--Customers--Add Customer'}{code} = {
  741. name => 'action',
  742. value => 'add_customer',
  743. text => $locale->text('Add Customer'),
  744. };
  745. $button{'AR--Customers--Add Customer'}{order} = $i++;
  746. }
  747. }
  748. if ( $myconfig{acs} !~ /AP--AP/ ) {
  749. if ( $form->{db} eq 'vendor' ) {
  750. $button{'AP--Vendors--Add Vendor'}{code} = {
  751. name => 'action',
  752. value => 'add_vendor',
  753. text => $locale->text('Add Vendor'),
  754. };
  755. $button{'AP--Vendors--Add Vendor'}{order} = $i++;
  756. }
  757. }
  758. foreach $item ( split /;/, $myconfig{acs} ) {
  759. delete $button{$item};
  760. }
  761. $hiddens{$_} = $form->{$_} foreach qw(callback db path login sessionid);
  762. if ( $form->{status} ) {
  763. foreach $item ( sort { $a->{order} <=> $b->{order} } %button ) {
  764. push @buttons, $item->{code};
  765. }
  766. }
  767. ##SC: Temporary removal
  768. ## if ( $form->{lynx} ) {
  769. ## require "bin/menu.pl";
  770. ## &menubar;
  771. ## }
  772. my $template = LedgerSMB::Template->new_UI(
  773. user => \%myconfig,
  774. locale => $locale,
  775. template => 'form-dynatable',
  776. );
  777. $template->render({
  778. form => $form,
  779. user => \%myconfig,
  780. hiddens => \%hiddens,
  781. buttons => \@buttons,
  782. options => \@options,
  783. rows => \@rows,
  784. columns => \@column_index,
  785. heading => \%column_header,
  786. });
  787. }
  788. sub list_subtotal {
  789. my %column_data;
  790. $column_data{invamount} =
  791. $form->format_amount( \%myconfig, $invamountsubtotal, 2, ' ' );
  792. $column_data{invtax} =
  793. $form->format_amount( \%myconfig, $invtaxsubtotal, 2, ' ' );
  794. $column_data{invtotal} =
  795. $form->format_amount( \%myconfig, $invtotalsubtotal, 2, ' ' );
  796. $invamountsubtotal = 0;
  797. $invtaxsubtotal = 0;
  798. $invtotalsubtotal = 0;
  799. $column_data{ordamount} =
  800. $form->format_amount( \%myconfig, $ordamountsubtotal, 2, ' ' );
  801. $column_data{ordtax} =
  802. $form->format_amount( \%myconfig, $ordtaxsubtotal, 2, ' ' );
  803. $column_data{ordtotal} =
  804. $form->format_amount( \%myconfig, $ordtotalsubtotal, 2, ' ' );
  805. $ordamountsubtotal = 0;
  806. $ordtaxsubtotal = 0;
  807. $ordtotalsubtotal = 0;
  808. $column_data{quoamount} =
  809. $form->format_amount( \%myconfig, $quoamountsubtotal, 2, ' ' );
  810. $column_data{quotax} =
  811. $form->format_amount( \%myconfig, $quotaxsubtotal, 2, ' ' );
  812. $column_data{quototal} =
  813. $form->format_amount( \%myconfig, $quototalsubtotal, 2, ' ' );
  814. $quoamountsubtotal = 0;
  815. $quotaxsubtotal = 0;
  816. $quototalsubtotal = 0;
  817. $column_data{class} = 'subtotal';
  818. \%column_data;
  819. }
  820. sub list_history {
  821. CT->get_history( \%myconfig, \%$form );
  822. $href =
  823. "$form->{script}?action=list_history&direction=$form->{direction}&oldsort=$form->{oldsort}&db=$form->{db}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&history=$form->{history}";
  824. $form->sort_order();
  825. $callback =
  826. "$form->{script}?action=list_history&direction=$form->{direction}&oldsort=$form->{oldsort}&db=$form->{db}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&history=$form->{history}";
  827. $form->{l_fxsellprice} = $form->{l_curr};
  828. @columns = $form->sort_columns( partnumber, description, qty, unit,
  829. sellprice, fxsellprice, curr, discount,
  830. deliverydate, projectnumber, serialnumber
  831. );
  832. if ( $form->{history} eq 'summary' ) {
  833. @columns =
  834. $form->sort_columns( partnumber, description, qty, unit, sellprice,
  835. curr );
  836. }
  837. foreach $item (@columns) {
  838. if ( $form->{"l_$item"} eq "Y" ) {
  839. push @column_index, $item;
  840. # add column to href and callback
  841. $callback .= "&l_$item=Y";
  842. $href .= "&l_$item=Y";
  843. }
  844. }
  845. if ( $form->{history} eq 'detail' ) {
  846. $option = $locale->text('Detail');
  847. }
  848. if ( $form->{history} eq 'summary' ) {
  849. $option .= $locale->text('Summary');
  850. }
  851. if ( $form->{name} ) {
  852. $callback .= "&name=" . $form->escape( $form->{name}, 1 );
  853. $href .= "&name=" . $form->escape( $form->{name} );
  854. $option .= "\n<br>" . $locale->text('Name') . " : $form->{name}";
  855. }
  856. if ( $form->{contact} ) {
  857. $callback .= "&contact=" . $form->escape( $form->{contact}, 1 );
  858. $href .= "&contact=" . $form->escape( $form->{contact} );
  859. $option .= "\n<br>" . $locale->text('Contact') . " : $form->{contact}";
  860. }
  861. if ( $form->{"$form->{db}number"} ) {
  862. $callback .=
  863. qq|&$form->{db}number=|
  864. . $form->escape( $form->{"$form->{db}number"}, 1 );
  865. $href .=
  866. "&$form->{db}number=" . $form->escape( $form->{"$form->{db}number"} );
  867. $option .= "\n<br>"
  868. . $locale->text('Number')
  869. . qq| : $form->{"$form->{db}number"}|;
  870. }
  871. if ( $form->{email} ) {
  872. $callback .= "&email=" . $form->escape( $form->{email}, 1 );
  873. $href .= "&email=" . $form->escape( $form->{email} );
  874. $option .= "\n<br>" . $locale->text('E-mail') . " : $form->{email}";
  875. }
  876. if ( $form->{transdatefrom} ) {
  877. $callback .= "&transdatefrom=$form->{transdatefrom}";
  878. $href .= "&transdatefrom=$form->{transdatefrom}";
  879. $option .= "\n<br>" if ($option);
  880. $option .=
  881. $locale->text('From') . "&nbsp;"
  882. . $locale->date( \%myconfig, $form->{transdatefrom}, 1 );
  883. }
  884. if ( $form->{transdateto} ) {
  885. $callback .= "&transdateto=$form->{transdateto}";
  886. $href .= "&transdateto=$form->{transdateto}";
  887. if ( $form->{transdatefrom} ) {
  888. $option .= " ";
  889. }
  890. else {
  891. $option .= "\n<br>" if ($option);
  892. }
  893. $option .=
  894. $locale->text('To') . "&nbsp;"
  895. . $locale->date( \%myconfig, $form->{transdateto}, 1 );
  896. }
  897. if ( $form->{open} ) {
  898. $callback .= "&open=$form->{open}";
  899. $href .= "&open=$form->{open}";
  900. $option .= "\n<br>" if ($option);
  901. $option .= $locale->text('Open');
  902. }
  903. if ( $form->{closed} ) {
  904. $callback .= "&closed=$form->{closed}";
  905. $href .= "&closed=$form->{closed}";
  906. $option .= "\n<br>" if ($option);
  907. $option .= $locale->text('Closed');
  908. }
  909. $form->{callback} = "$callback&sort=$form->{sort}";
  910. $callback = $form->escape( $form->{callback} );
  911. $column_header{partnumber} =
  912. qq|<th><a class=listheading href=$href&sort=partnumber>|
  913. . $locale->text('Part Number')
  914. . qq|</a></th>|;
  915. $column_header{description} =
  916. qq|<th><a class=listheading href=$href&sort=description>|
  917. . $locale->text('Description')
  918. . qq|</a></th>|;
  919. if ( $form->{history} eq 'summary' ) {
  920. $column_header{sellprice} =
  921. qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
  922. }
  923. else {
  924. $column_header{sellprice} =
  925. qq|<th class=listheading>| . $locale->text('Sell Price') . qq|</th>|;
  926. }
  927. $column_header{fxsellprice} = qq|<th>&nbsp;</th>|;
  928. $column_header{curr} =
  929. qq|<th class=listheading>| . $locale->text('Curr') . qq|</th>|;
  930. $column_header{discount} =
  931. qq|<th class=listheading>| . $locale->text('Discount') . qq|</th>|;
  932. $column_header{qty} =
  933. qq|<th class=listheading>| . $locale->text('Qty') . qq|</th>|;
  934. $column_header{unit} =
  935. qq|<th class=listheading>| . $locale->text('Unit') . qq|</th>|;
  936. $column_header{deliverydate} =
  937. qq|<th><a class=listheading href=$href&sort=deliverydate>|
  938. . $locale->text('Delivery Date')
  939. . qq|</a></th>|;
  940. $column_header{projectnumber} =
  941. qq|<th><a class=listheading href=$href&sort=projectnumber>|
  942. . $locale->text('Project Number')
  943. . qq|</a></th>|;
  944. $column_header{serialnumber} =
  945. qq|<th><a class=listheading href=$href&sort=serialnumber>|
  946. . $locale->text('Serial Number')
  947. . qq|</a></th>|;
  948. # $locale->text('Customer History')
  949. # $locale->text('Vendor History')
  950. $label = ucfirst $form->{db};
  951. $form->{title} = $locale->text( $label . " History" );
  952. $colspan = $#column_index + 1;
  953. $form->header;
  954. print qq|
  955. <body>
  956. <table width=100%>
  957. <tr>
  958. <th class=listtop>$form->{title}</th>
  959. </tr>
  960. <tr height="5"></tr>
  961. <tr>
  962. <td>$option</td>
  963. </tr>
  964. <tr>
  965. <td>
  966. <table width=100%>
  967. <tr class=listheading>
  968. |;
  969. for (@column_index) { print "$column_header{$_}\n" }
  970. print qq|
  971. </tr>
  972. |;
  973. $module = 'oe';
  974. if ( $form->{db} eq 'customer' ) {
  975. $invlabel = $locale->text('Sales Invoice');
  976. $ordlabel = $locale->text('Sales Order');
  977. $quolabel = $locale->text('Quotation');
  978. $ordertype = 'sales_order';
  979. $quotationtype = 'sales_quotation';
  980. if ( $form->{type} eq 'invoice' ) {
  981. $module = 'is';
  982. }
  983. }
  984. else {
  985. $invlabel = $locale->text('Vendor Invoice');
  986. $ordlabel = $locale->text('Purchase Order');
  987. $quolabel = $locale->text('RFQ');
  988. $ordertype = 'purchase_order';
  989. $quotationtype = 'request_quotation';
  990. if ( $form->{type} eq 'invoice' ) {
  991. $module = 'ir';
  992. }
  993. }
  994. $ml = ( $form->{db} eq 'vendor' ) ? -1 : 1;
  995. foreach $ref ( @{ $form->{CT} } ) {
  996. if ( $ref->{id} ne $sameid ) {
  997. # print the header
  998. print qq|
  999. <tr class=listheading>
  1000. <th colspan=$colspan><a class=listheading href=$form->{script}?action=edit&id=$ref->{ctid}&db=$form->{db}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ref->{name} $ref->{address}</a></th>
  1001. </tr>
  1002. |;
  1003. }
  1004. if ( $form->{type} ne 'invoice' ) {
  1005. $ref->{fxsellprice} = $ref->{sellprice};
  1006. $ref->{sellprice} *= $ref->{exchangerate};
  1007. }
  1008. if ( $form->{history} eq 'detail' and $ref->{invid} ne $sameinvid ) {
  1009. # print inv, ord, quo number
  1010. $i++;
  1011. $i %= 2;
  1012. print qq|
  1013. <tr class=listrow$i>
  1014. |;
  1015. if ( $form->{type} eq 'invoice' ) {
  1016. print
  1017. qq|<th align=left colspan=$colspan><a href=${module}.pl?action=edit&id=$ref->{invid}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$invlabel $ref->{invnumber} / $ref->{employee}</a></th>|;
  1018. }
  1019. if ( $form->{type} eq 'order' ) {
  1020. print
  1021. qq|<th align=left colspan=$colspan><a href=${module}.pl?action=edit&id=$ref->{invid}&type=$ordertype&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ordlabel $ref->{ordnumber} / $ref->{employee}</a></th>|;
  1022. }
  1023. if ( $form->{type} eq 'quotation' ) {
  1024. print
  1025. qq|<th align=left colspan=$colspan><a href=${module}.pl?action=edit&id=$ref->{invid}&type=$quotationtype&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$quolabel $ref->{quonumber} / $ref->{employee}</a></th>|;
  1026. }
  1027. print qq|
  1028. </tr>
  1029. |;
  1030. }
  1031. for (@column_index) { $column_data{$_} = "<td>$ref->{$_}&nbsp;</td>" }
  1032. if ( $form->{l_curr} ) {
  1033. $column_data{fxsellprice} =
  1034. qq|<td align=right>|
  1035. . $form->format_amount( \%myconfig, $ref->{fxsellprice}, 2 )
  1036. . "</td>";
  1037. }
  1038. $column_data{sellprice} =
  1039. qq|<td align=right>|
  1040. . $form->format_amount( \%myconfig, $ref->{sellprice}, 2 ) . "</td>";
  1041. $column_data{qty} =
  1042. qq|<td align=right>|
  1043. . $form->format_amount( \%myconfig, $ref->{qty} * $ml ) . "</td>";
  1044. $column_data{discount} = qq|<td align=right>|
  1045. . $form->format_amount( \%myconfig, $ref->{discount} * 100,
  1046. "", "&nbsp;" )
  1047. . "</td>";
  1048. $column_data{partnumber} =
  1049. qq|<td><a href=ic.pl?action=edit&id=$ref->{pid}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ref->{partnumber}</td>|;
  1050. $i++;
  1051. $i %= 2;
  1052. print qq|
  1053. <tr class=listrow$i>
  1054. |;
  1055. for (@column_index) { print "$column_data{$_}\n" }
  1056. print qq|
  1057. </tr>
  1058. |;
  1059. $sameid = $ref->{id};
  1060. $sameinvid = $ref->{invid};
  1061. }
  1062. print qq|
  1063. </table>
  1064. </td>
  1065. </tr>
  1066. <tr>
  1067. <td><hr size=3 noshade></td>
  1068. </tr>
  1069. </table>
  1070. |;
  1071. if ( $form->{lynx} ) {
  1072. require "bin/menu.pl";
  1073. &menubar;
  1074. }
  1075. print qq|
  1076. </form>
  1077. </body>
  1078. </html>
  1079. |;
  1080. }
  1081. sub edit {
  1082. # $locale->text('Edit Customer')
  1083. # $locale->text('Edit Vendor')
  1084. CT->create_links( \%myconfig, \%$form );
  1085. for ( keys %$form ) { $form->{$_} = $form->quote( $form->{$_} ) }
  1086. $form->{title} = "Edit";
  1087. # format discount
  1088. $form->{discount} *= 100;
  1089. &form_header;
  1090. &form_footer;
  1091. }
  1092. sub form_header {
  1093. $form->{taxincluded} = ( $form->{taxincluded} ) ? "checked" : "";
  1094. $form->{creditlimit} =
  1095. $form->format_amount( \%myconfig, $form->{creditlimit}, 0 );
  1096. $form->{discount} =
  1097. $form->format_amount( \%myconfig, $form->{discount}, "" );
  1098. $form->{terms} = $form->format_amount( \%myconfig, $form->{terms}, "" );
  1099. if ( $form->{currencies} ) {
  1100. # currencies
  1101. $form->{selectcurrency} = {
  1102. name => 'curr',
  1103. options => [],
  1104. default_values => $form->{curr},
  1105. };
  1106. push @{$form->{selectcurrency}{options}}, {
  1107. text => $_,
  1108. value => $_,
  1109. } foreach split /:/, $form->{currencies};
  1110. }
  1111. $form->{taxable} = [];
  1112. foreach my $item ( split / /, $form->{taxaccounts} ) {
  1113. my $temp_tax = {
  1114. name => "tax_$item",
  1115. value => 1,
  1116. type => 'checkbox',
  1117. label => $form->{tax}{$item}{description}
  1118. };
  1119. $temp_tax->{checked} = 'checked' if $form->{tax}{$item}{taxable};
  1120. push @{$form->{taxable}}, $temp_tax;
  1121. }
  1122. if (ref $form->{all_business} eq 'ARRAY') {
  1123. $form->{selectbusiness} = {
  1124. name => 'business',
  1125. options => [{text => '', value => ''}],
  1126. };
  1127. push @{$form->{selectbusiness}{options}}, {
  1128. text => $_->{description},
  1129. value => "$_->{description}--$_->{id}",
  1130. } foreach @{$form->{all_business}};
  1131. $form->{selectbusiness}{default_values} =
  1132. "$form->{business}--$form->{business_id}";
  1133. }
  1134. if (ref $form->{all_pricegroup} eq 'ARRAY' && $form->{db} eq 'customer') {
  1135. $form->{selectpricegroup} = {
  1136. name => 'pricegroup',
  1137. options => [{text => '', value => ''}],
  1138. };
  1139. push @{$form->{selectpricegroup}{options}}, {
  1140. name => $_->{pricegroup},
  1141. value => "$_->{pricegroup}--$_->{id}",
  1142. } foreach @{$form->{all_pricegroup}};
  1143. $form->{selectpricegroup}{default_values} =
  1144. "$form->{pricegroup}--$form->{pricegroup_id}";
  1145. }
  1146. if ( @{ $form->{all_language} } ) {
  1147. $form->{selectlanguage} = {
  1148. name => 'language',
  1149. options => [{text => '', value => ''}],
  1150. };
  1151. push @{$form->{selectlanguage}{options}}, {
  1152. text => $_->{description},
  1153. value => "$_->{description}--$_->{code}",
  1154. } foreach @{$form->{all_language}};
  1155. $form->{selectlangauge}{default_values} =
  1156. "$form->{language}--$form->{language_code}";
  1157. }
  1158. $form->{selectemployee} = {
  1159. name => 'employee',
  1160. options => [{text => '', value => ''}],
  1161. default_values => "$form->{employee}--$form->{employee_id}",
  1162. };
  1163. push @{$form->{selectemployee}{options}}, {
  1164. text => $_->{name},
  1165. value => "$_->{name}--$_->{id}",
  1166. } foreach @{$form->{all_employee}};
  1167. if (ref $form->{all_employee} eq 'ARRAY') {
  1168. if ( $myconfig{role} eq 'user' && $form->{id} ) {
  1169. $hiddens{employee} = "$form->{employee}--$form->{employee_id}";
  1170. }
  1171. }
  1172. # $locale->text('Customer Number')
  1173. # $locale->text('Vendor Number')
  1174. $label = ucfirst $form->{db};
  1175. $form->{label} = $label;
  1176. $form->{title} = $locale->text("$form->{title} $label");
  1177. }
  1178. sub form_footer {
  1179. # type=submit $locale->text('Save')
  1180. # type=submit $locale->text('Save as new')
  1181. # type=submit $locale->text('AR Transaction')
  1182. # type=submit $locale->text('Sales Invoice')
  1183. # type=submit $locale->text('Sales Order')
  1184. # type=submit $locale->text('Quotation')
  1185. # type=submit $locale->text('AP Transaction')
  1186. # type=submit $locale->text('Vendor Invoice')
  1187. # type=submit $locale->text('Purchase Order')
  1188. # type=submit $locale->text('RFQ')
  1189. # type=submit $locale->text('Pricelist')
  1190. # type=submit $locale->text('Delete')
  1191. # type=submit $locale->text('POS')
  1192. my %hiddens;
  1193. my @buttons;
  1194. %button = (
  1195. 'save' => { ndx => 1, key => 'S', value => $locale->text('Save') },
  1196. 'save_as_new' =>
  1197. { ndx => 2, key => 'N', value => $locale->text('Save as new') },
  1198. 'ar_transaction' =>
  1199. { ndx => 7, key => 'A', value => $locale->text('AR Transaction') },
  1200. 'ap_transaction' =>
  1201. { ndx => 8, key => 'A', value => $locale->text('AP Transaction') },
  1202. 'sales_invoice' =>
  1203. { ndx => 9, key => 'I', value => $locale->text('Sales Invoice') },
  1204. 'pos' => { ndx => 10, key => 'C', value => $locale->text('POS') },
  1205. 'sales_order' =>
  1206. { ndx => 11, key => 'O', value => $locale->text('Sales Order') },
  1207. 'quotation' =>
  1208. { ndx => 12, key => 'Q', value => $locale->text('Quotation') },
  1209. 'vendor_invoice' =>
  1210. { ndx => 13, key => 'I', value => $locale->text('Vendor Invoice') },
  1211. 'purchase_order' =>
  1212. { ndx => 14, key => 'O', value => $locale->text('Purchase Order') },
  1213. 'rfq' => { ndx => 15, key => 'Q', value => $locale->text('RFQ') },
  1214. 'pricelist' =>
  1215. { ndx => 16, key => 'P', value => $locale->text('Pricelist') },
  1216. 'delete' => { ndx => 17, key => 'D', value => $locale->text('Delete') },
  1217. );
  1218. my %blist = ();
  1219. if ( $form->{db} eq 'customer' ) {
  1220. if ( $myconfig{acs} !~ /AR--Customers--Add Customer/ ) {
  1221. $blist{'save'} = 1;
  1222. if ( $form->{id} ) {
  1223. $blist{'save_as_new'} = 1;
  1224. if ( $form->{status} eq 'orphaned' ) {
  1225. $blist{'delete'} = 1;
  1226. }
  1227. }
  1228. }
  1229. if ( $myconfig{acs} !~ /AR--AR/ ) {
  1230. if ( $myconfig{acs} !~ /AR--Add Transaction/ ) {
  1231. $blist{'ar_transaction'} = 1;
  1232. }
  1233. if ( $myconfig{acs} !~ /AR--Sales Invoice/ ) {
  1234. $blist{'sales_invoice'} = 1;
  1235. }
  1236. }
  1237. if ( $myconfig{acs} !~ /POS--POS/ ) {
  1238. if ( $myconfig{acs} !~ /POS--Sale/ ) {
  1239. $blist{'pos'} = 1;
  1240. }
  1241. }
  1242. if ( $myconfig{acs} !~ /Order Entry--Order Entry/ ) {
  1243. if ( $myconfig{acs} !~ /Order Entry--Sales Order/ ) {
  1244. $blist{'sales_order'} = 1;
  1245. }
  1246. }
  1247. if ( $myconfig{acs} !~ /Quotations--Quotations/ ) {
  1248. if ( $myconfig{acs} !~ /Quotations--Quotation/ ) {
  1249. $blist{'quotation'} = 1;
  1250. }
  1251. }
  1252. }
  1253. if ( $form->{db} eq 'vendor' ) {
  1254. if ( $myconfig{acs} !~ /AP--Vendors--Add Vendor/ ) {
  1255. $blist{'save'} = 1;
  1256. if ( $form->{id} ) {
  1257. $blist{'save_as_new'} = 1;
  1258. if ( $form->{status} eq 'orphaned' ) {
  1259. $blist{'delete'} = 1;
  1260. }
  1261. }
  1262. }
  1263. if ( $myconfig{acs} !~ /AP--AP/ ) {
  1264. if ( $myconfig{acs} !~ /AP--Add Transaction/ ) {
  1265. $blist{'ap_transaction'} = 1;
  1266. }
  1267. if ( $myconfig{acs} !~ /AP--Vendor Invoice/ ) {
  1268. $blist{'vendor_invoice'} = 1;
  1269. }
  1270. }
  1271. if ( $myconfig{acs} !~ /Order Entry--Order Entry/ ) {
  1272. if ( $myconfig{acs} !~ /Order Entry--Purchase Order/ ) {
  1273. $blist{'purchase_order'} = 1;
  1274. }
  1275. }
  1276. if ( $myconfig{acs} !~ /Quotations--Quotations/ ) {
  1277. if ( $myconfig{acs} !~ /Quotations--RFQ/ ) {
  1278. $blist{'rfq'} = 1;
  1279. }
  1280. }
  1281. }
  1282. if ( $myconfig{acs} !~ /Goods & Services--Goods & Services/ ) {
  1283. $myconfig{acs} =~
  1284. s/(Goods & Services--)Add (Service|Assembly).*;/$1--Add Part/g;
  1285. if ( $myconfig{acs} !~ /Goods & Services--Add Part/ ) {
  1286. $blist{'pricelist'} = 1;
  1287. }
  1288. }
  1289. $hiddens{$_} = $form->{$_} foreach
  1290. qw(id taxaccounts path login sessionid callback db);
  1291. for ( keys %button ) { delete $button{$_} if !$blist{$_} }
  1292. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button ) {
  1293. push @buttons, {
  1294. name => 'action',
  1295. value => $_,
  1296. accesskey => $button{$_}{key},
  1297. title => "$button{$name}{value} [Alt-$button{$_}{key}]",
  1298. text => $button{$_}{value},
  1299. };
  1300. }
  1301. ##SC: Temporary removal
  1302. ## if ( $form->{lynx} ) {
  1303. ## require "bin/menu.pl";
  1304. ## &menubar;
  1305. ## }
  1306. $form->{dbnumber} = "$form->{db}number";
  1307. my $template = LedgerSMB::Template->new_UI(
  1308. user => \%myconfig,
  1309. locale => $locale,
  1310. template => 'ct-form',
  1311. );
  1312. $template->render({
  1313. form => $form,
  1314. user => \%myconfig,
  1315. hiddens => \%hiddens,
  1316. buttons => \@buttons,
  1317. options => \@options,
  1318. });
  1319. }
  1320. sub pricelist {
  1321. $form->isblank( "name", $locale->text('Name missing!') );
  1322. $form->{display_form} ||= "display_pricelist";
  1323. CT->pricelist( \%myconfig, \%$form );
  1324. foreach $ref ( @{ $form->{"all_partspricelist"} } ) {
  1325. $i++;
  1326. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  1327. }
  1328. $form->{rowcount} = $i;
  1329. # currencies
  1330. @curr = split /:/, $form->{currencies};
  1331. $form->{selectcurrency} = [];
  1332. for (@curr) {push @{$form->{selectcurrency}}, {text => $_, value => $_}}
  1333. if (ref $form->{all_partsgroup} eq 'ARRAY') {
  1334. $form->{selectpartsgroup} = "";
  1335. foreach $ref ( @{ $form->{all_partsgroup} } ) {
  1336. $form->{selectpartsgroup} .= qq|$ref->{partsgroup}--$ref->{id}\n|;
  1337. }
  1338. }
  1339. for (qw(currencies all_partsgroup all_partspricelist)) {
  1340. delete $form->{$_};
  1341. }
  1342. foreach $i ( 1 .. $form->{rowcount} ) {
  1343. if ( $form->{db} eq 'customer' ) {
  1344. $form->{"pricebreak_$i"} =
  1345. $form->format_amount( \%myconfig, $form->{"pricebreak_$i"} );
  1346. $form->{"sellprice_$i"} =
  1347. $form->format_amount( \%myconfig, $form->{"sellprice_$i"}, 2 );
  1348. }
  1349. if ( $form->{db} eq 'vendor' ) {
  1350. $form->{"leadtime_$i"} =
  1351. $form->format_amount( \%myconfig, $form->{"leadtime_$i"} );
  1352. $form->{"lastcost_$i"} =
  1353. $form->format_amount( \%myconfig, $form->{"lastcost_$i"}, 2 );
  1354. }
  1355. }
  1356. $form->{rowcount}++;
  1357. &{"$form->{db}_pricelist"};
  1358. }
  1359. sub customer_pricelist {
  1360. my @flds =
  1361. qw(runningnumber id partnumber description sellprice unit partsgroup pricebreak curr validfrom validto);
  1362. $form->{rowcount}--;
  1363. # remove empty rows
  1364. if ( $form->{rowcount} ) {
  1365. foreach $i ( 1 .. $form->{rowcount} ) {
  1366. for (qw(pricebreak sellprice)) {
  1367. $form->{"${_}_$i"} =
  1368. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  1369. }
  1370. ( $a, $b ) = split /\./, $form->{"pricebreak_$i"};
  1371. $a = length $a;
  1372. $b = length $b;
  1373. $whole = ( $whole > $a ) ? $whole : $a;
  1374. $dec = ( $dec > $b ) ? $dec : $b;
  1375. }
  1376. $pad1 = '0' x $whole;
  1377. $pad2 = '0' x $dec;
  1378. foreach $i ( 1 .. $form->{rowcount} ) {
  1379. ( $a, $b ) = split /\./, $form->{"pricebreak_$i"};
  1380. $a = substr( "$pad1$a", -$whole );
  1381. $b = substr( "$b$pad2", 0, $dec );
  1382. $ndx{qq|$form->{"partnumber_$i"}_$form->{"id_$i"}_$a$b|} = $i;
  1383. }
  1384. $i = 1;
  1385. for ( sort keys %ndx ) { $form->{"runningnumber_$ndx{$_}"} = $i++ }
  1386. foreach $i ( 1 .. $form->{rowcount} ) {
  1387. if ( $form->{"partnumber_$i"} && $form->{"sellprice_$i"} ) {
  1388. if ( $form->{"id_$i"} eq $sameid ) {
  1389. $j = $i + 1;
  1390. next
  1391. if ( $form->{"id_$j"} eq $sameid
  1392. && !$form->{"pricebreak_$i"} );
  1393. }
  1394. push @a, {};
  1395. $j = $#a;
  1396. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  1397. $count++;
  1398. }
  1399. $sameid = $form->{"id_$i"};
  1400. }
  1401. $form->redo_rows( \@flds, \@a, $count, $form->{rowcount} );
  1402. $form->{rowcount} = $count;
  1403. }
  1404. $form->{rowcount}++;
  1405. if ( $form->{display_form} ) {
  1406. &{"$form->{display_form}"};
  1407. }
  1408. }
  1409. sub vendor_pricelist {
  1410. my @flds =
  1411. qw(runningnumber id sku partnumber description lastcost unit partsgroup curr leadtime);
  1412. $form->{rowcount}--;
  1413. # remove empty rows
  1414. if ( $form->{rowcount} ) {
  1415. foreach $i ( 1 .. $form->{rowcount} ) {
  1416. for (qw(leadtime lastcost)) {
  1417. $form->{"${_}_$i"} =
  1418. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  1419. }
  1420. $var =
  1421. ( $form->{"partnumber_$i"} )
  1422. ? $form->{"sku_$i"}
  1423. : qq|_$form->{"sku_$i"}|;
  1424. $ndx{$var} = $i;
  1425. }
  1426. $i = 1;
  1427. for ( sort keys %ndx ) { $form->{"runningnumber_$ndx{$_}"} = $i++ }
  1428. foreach $i ( 1 .. $form->{rowcount} ) {
  1429. if ( $form->{"sku_$i"} ) {
  1430. push @a, {};
  1431. $j = $#a;
  1432. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  1433. $count++;
  1434. }
  1435. }
  1436. $form->redo_rows( \@flds, \@a, $count, $form->{rowcount} );
  1437. $form->{rowcount} = $count;
  1438. }
  1439. $form->{rowcount}++;
  1440. if ( $form->{display_form} ) {
  1441. &{"$form->{display_form}"};
  1442. }
  1443. }
  1444. sub display_pricelist {
  1445. my %hiddens;
  1446. my $buttons = &pricelist_footer;
  1447. delete $form->{action};
  1448. $hiddens{$_} = $form->{$_} foreach sort keys %$form;
  1449. &pricelist_header(\%hiddens, $buttons);
  1450. }
  1451. sub pricelist_header {
  1452. my $hiddens = shift;
  1453. my $buttons = shift;
  1454. $form->{title} = $form->{name};
  1455. my @column_index;
  1456. my %column_header;
  1457. if ( $form->{db} eq 'customer' ) {
  1458. @column_index = qw(partnumber description);
  1459. push @column_index, "partsgroup" if $form->{selectpartsgroup};
  1460. push @column_index, qw(pricebreak sellprice curr validfrom validto);
  1461. $column_header{pricebreak} = $locale->text('Break');
  1462. $column_header{sellprice} = $locale->text('Sell Price');
  1463. $column_header{validfrom} = $locale->text('From');
  1464. $column_header{validto} = $locale->text('To');
  1465. }
  1466. if ( $form->{db} eq 'vendor' ) {
  1467. @column_index = qw(sku partnumber description);
  1468. push @column_index, "partsgroup" if $form->{selectpartsgroup};
  1469. push @column_index, qw(lastcost curr leadtime);
  1470. $column_header{sku} = $locale->text('SKU');
  1471. $column_header{leadtime} = $locale->text('Leadtime');
  1472. $column_header{lastcost} = $locale->text('Cost');
  1473. }
  1474. $column_header{partnumber} = $locale->text('Number');
  1475. $column_header{description} = $locale->text('Description'); #80% width
  1476. $column_header{partsgroup} = $locale->text('Group');
  1477. $column_header{curr} = $locale->text('Curr');
  1478. $sameid = "";
  1479. my @rows;
  1480. foreach my $i ( 1 .. $form->{rowcount} ) {
  1481. my %column_data;
  1482. if ( $form->{selectpartsgroup} ) {
  1483. if ( $i < $form->{rowcount} ) {
  1484. ($partsgroup) = split /--/, $form->{"partsgroup_$i"};
  1485. $hiddens->{"partsgroup_$i"} = $form->{"partsgroup_$i"};
  1486. $column_data{partsgroup} = $partsgroup;
  1487. }
  1488. }
  1489. if ( $i < $form->{rowcount} ) {
  1490. if ( $form->{"id_$i"} eq $sameid ) {
  1491. for (qw(partnumber description partsgroup)) {
  1492. $hiddens->{"${_}_$i"} = $form->{"${_}_$i"};
  1493. $column_data{$_} = ' ';
  1494. }
  1495. } else {
  1496. $column_data{sku} = {input => {
  1497. name => "sku_$i",
  1498. value => $form->{"sku_$i"}
  1499. }};
  1500. $column_data{partnumber} = {input => {
  1501. name => "partnumber_$i",
  1502. value => $form->{"partnumber_$i"}
  1503. }};
  1504. $column_data{description} =$form->{"description_$i"};
  1505. $hiddens->{"description_$i"} = $form->{"description_$i"};
  1506. }
  1507. $hiddens->{"id_$i"} = $form->{"id_$i"};
  1508. } else {
  1509. if ( $form->{db} eq 'customer' ) {
  1510. $column_data{partnumber} = {input => {
  1511. name => "partnumber_$i",
  1512. value => $form->{"partnumber_$i"}
  1513. }};
  1514. } else {
  1515. $column_data{partnumber} = ' ';
  1516. }
  1517. $hiddens->{"id_$i"} = $form->{"id_$i"};
  1518. $column_data{sku} = {input => {
  1519. name => "sku_$i",
  1520. value => $form->{"sku_$i"}
  1521. }};
  1522. $column_data{partnumber} = {input => {
  1523. name => "partnumber_$i",
  1524. value => $form->{"partnumber_$i"}
  1525. }};
  1526. if ( $form->{selectpartsgroup} ) {
  1527. @selectpartsgroup = ({text => '', value => ''});
  1528. foreach $line ( split /\n/, $form->{selectpartsgroup} ) {
  1529. push @selectpartsgroup, {
  1530. text => (split /--/, $line)[0],
  1531. value => $line,
  1532. };
  1533. }
  1534. $column_data{partsgroup} = {'select' => {
  1535. name => "partnumber_$i",
  1536. options => \@selectpartsgroup,
  1537. }};
  1538. }
  1539. }
  1540. if ( $form->{db} eq 'customer' ) {
  1541. $column_data{pricebreak} = {input => {
  1542. name => "pricebreak_$i",
  1543. size => 5,
  1544. value => $form->format_amount(\%myconfig, $form->{"pricebreak_$i"})
  1545. }};
  1546. $column_data{sellprice} = {input => {
  1547. name => "sellprice_$i",
  1548. size => 10,
  1549. value => $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, 2)
  1550. }};
  1551. $column_data{validfrom} = {input => {
  1552. name => "validfrom_$i",
  1553. size => 11,
  1554. value => $form->{"validfrom_$i"},
  1555. }};
  1556. $column_data{validto} = {input => {
  1557. name => "validto_$i",
  1558. size => 11,
  1559. value => $form->{"validto_$i"},
  1560. }};
  1561. }
  1562. if ( $form->{db} eq 'vendor' ) {
  1563. $column_data{leadtime} = {input => {
  1564. name => "leadtime_$i",
  1565. size => 5,
  1566. value => $form->format_amount(\%myconfig, $form->{"leadtime_$i"})
  1567. }};
  1568. $column_data{lastcost} = {input => {
  1569. name => "lastcost_$i",
  1570. size => 10,
  1571. value => $form->format_amount( \%myconfig, $form->{"lastcost_$i"}, 2 )
  1572. }};
  1573. }
  1574. $column_data{curr} = {'select' => {
  1575. name => "curr_$i",
  1576. options => $form->{selectcurrency},
  1577. default_values => $form->{"curr_$i"},
  1578. }};
  1579. $sameid = $form->{"id_$i"};
  1580. push @rows, \%column_data;
  1581. }
  1582. # delete variables
  1583. foreach $i ( 1 .. $form->{rowcount} ) {
  1584. for ( @column_index, "id" ) { delete $form->{"${_}_$i"} }
  1585. }
  1586. for (qw(title titlebar script none)) { delete $form->{$_} }
  1587. my $template = LedgerSMB::Template->new_UI(
  1588. user => \%myconfig,
  1589. locale => $locale,
  1590. template => 'form-dynatable',
  1591. );
  1592. $template->render({
  1593. form => $form,
  1594. user => \%myconfig,
  1595. hiddens => $hiddens,
  1596. buttons => $buttons,
  1597. options => \@options,
  1598. rows => \@rows,
  1599. columns => \@column_index,
  1600. heading => \%column_header,
  1601. });
  1602. }
  1603. sub pricelist_footer {
  1604. # type=submit $locale->text('Update')
  1605. # type=submit $locale->text('Save Pricelist')
  1606. %button = (
  1607. 'update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  1608. 'save_pricelist' =>
  1609. { ndx => 3, key => 'S', value => $locale->text('Save Pricelist') },
  1610. );
  1611. my @buttons;
  1612. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button ) {
  1613. push @buttons, {
  1614. name => 'action',
  1615. value => $_,
  1616. accesskey => $button{$_}{key},
  1617. title => "$button{$name}{value} [Alt-$button{$_}{key}]",
  1618. text => $button{$_}{value},
  1619. };
  1620. }
  1621. \@buttons;
  1622. }
  1623. sub update {
  1624. $i = $form->{rowcount};
  1625. $additem = 0;
  1626. if ( $form->{db} eq 'customer' ) {
  1627. $additem = 1
  1628. if !(( $form->{"partnumber_$i"} eq "" )
  1629. && ( $form->{"description_$i"} eq "" )
  1630. && ( $form->{"partsgroup_$i"} eq "" ) );
  1631. }
  1632. if ( $form->{db} eq 'vendor' ) {
  1633. if (
  1634. !(
  1635. ( $form->{"sku_$i"} eq "" )
  1636. && ( $form->{"description_$i"} eq "" )
  1637. && ( $form->{"partsgroup_$i"} eq "" )
  1638. )
  1639. )
  1640. {
  1641. $additem = 1;
  1642. $form->{"partnumber_$i"} = $form->{"sku_$i"};
  1643. }
  1644. }
  1645. if ($additem) {
  1646. CT->retrieve_item( \%myconfig, \%$form );
  1647. $rows = scalar @{ $form->{item_list} };
  1648. if ( $rows > 0 ) {
  1649. if ( $rows > 1 ) {
  1650. &select_item;
  1651. exit;
  1652. } else {
  1653. $sellprice = $form->{"sellprice_$i"};
  1654. $pricebreak = $form->{"pricebreak_$i"};
  1655. $lastcost = $form->{"lastcost_$i"};
  1656. for (qw(partnumber description)) {
  1657. $form->{item_list}[0]{$_} =
  1658. $form->quote( $form->{item_list}[0]{$_} );
  1659. }
  1660. for ( keys %{ $form->{item_list}[0] } ) {
  1661. $form->{"${_}_$i"} = $form->{item_list}[0]{$_};
  1662. }
  1663. if ( $form->{db} eq 'customer' ) {
  1664. if ($sellprice) {
  1665. $form->{"sellprice_$i"} = $sellprice;
  1666. }
  1667. $form->{"sellprice_$i"} =
  1668. $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  1669. 2 );
  1670. $form->{"pricebreak_$i"} = $pricebreak;
  1671. } else {
  1672. foreach $j ( 1 .. $form->{rowcount} - 1 ) {
  1673. if ( $form->{"sku_$j"} eq $form->{"partnumber_$i"} ) {
  1674. $form->error(
  1675. $locale->text('Item already on pricelist!') );
  1676. }
  1677. }
  1678. if ($lastcost) {
  1679. $form->{"lastcost_$i"} = $lastcost;
  1680. }
  1681. $form->{"lastcost_$i"} =
  1682. $form->format_amount( \%myconfig, $form->{"lastcost_$i"},
  1683. 2 );
  1684. $form->{"sku_$i"} = $form->{"partnumber_$i"};
  1685. # delete $form->{"partnumber_$i"};
  1686. }
  1687. $form->{rowcount}++;
  1688. }
  1689. } else {
  1690. $form->error( $locale->text('Item not on file!') );
  1691. }
  1692. }
  1693. &{"$form->{db}_pricelist"};
  1694. }
  1695. sub select_item {
  1696. my %hiddens;
  1697. my @column_index =
  1698. qw(ndx partnumber description partsgroup unit sellprice lastcost);
  1699. my %column_data;
  1700. $column_data{ndx} = ' ';
  1701. $column_data{partnumber} = locale->text('Number');
  1702. $column_data{description} = $locale->text('Description');
  1703. $column_data{partsgroup} = $locale->text('Group');
  1704. $column_data{unit} = $locale->text('Unit');
  1705. $column_data{sellprice} = $locale->text('Sell Price');
  1706. $column_data{lastcost} = $locale->text('Cost');
  1707. $form->{title} = $locale->text('Select items');
  1708. my $i = 0;
  1709. my @rows;
  1710. foreach $ref ( @{ $form->{item_list} } ) {
  1711. $i++;
  1712. my %column_data;
  1713. for (qw(partnumber description unit)) {
  1714. $ref->{$_} = $form->quote( $ref->{$_} );
  1715. }
  1716. $column_data{ndx} = {input => {
  1717. name => "ndx_$i",
  1718. type => 'checkbox',
  1719. value => $i,
  1720. }};
  1721. $column_data{$_} = $ref->{$_} foreach
  1722. qw(partnumber description partsgroup unit);
  1723. $column_data{sellprice} =
  1724. $form->format_amount( \%myconfig, $ref->{sellprice}, 2, ' ' );
  1725. $column_data{lastcost} =
  1726. $form->format_amount( \%myconfig, $ref->{lastcost}, 2, ' ' );
  1727. $j++;
  1728. $j %= 2;
  1729. $column_data{i} = $j;
  1730. push @rows, \%column_data;
  1731. $hiddens{"new_${_}_$i"} = $ref->{$_} foreach
  1732. qw(partnumber description partsgroup partsgroup_id sellprice lastcost unit id);
  1733. }
  1734. # delete action variable
  1735. for (qw(nextsub item_list)) { delete $form->{$_} }
  1736. $form->{action} = "item_selected";
  1737. $hiddens{$_} = $form->{$_} foreach sort keys %$form;
  1738. $hiddens{nextsub} = 'item_selected';
  1739. $hiddens{lastndx} = $i;
  1740. my @buttons = ({
  1741. name => 'action',
  1742. value => 'item_selected',
  1743. text => $locale->text('Continue'),
  1744. });
  1745. my $template = LedgerSMB::Template->new_UI(
  1746. user => \%myconfig,
  1747. locale => $locale,
  1748. template => 'form-dynatable',
  1749. );
  1750. $template->render({
  1751. form => $form,
  1752. user => \%myconfig,
  1753. hiddens => \%hiddens,
  1754. buttons => \@buttons,
  1755. options => \@options,
  1756. rows => \@rows,
  1757. columns => \@column_index,
  1758. heading => \%column_header,
  1759. });
  1760. }
  1761. sub item_selected {
  1762. # add rows
  1763. $i = $form->{rowcount};
  1764. %id = ();
  1765. for $i ( 1 .. $form->{rowcount} - 1 ) {
  1766. $id{ $form->{"id_$i"} } = 1;
  1767. }
  1768. for $j ( 1 .. $form->{lastndx} ) {
  1769. if ( $form->{"ndx_$j"} ) {
  1770. if ( $id{ $form->{"new_id_$j"} } ) {
  1771. next if $form->{db} eq 'vendor';
  1772. }
  1773. for (qw(id partnumber description unit sellprice lastcost)) {
  1774. $form->{"${_}_$i"} = $form->{"new_${_}_$j"};
  1775. }
  1776. $form->{"partsgroup_$i"} =
  1777. qq|$form->{"new_partsgroup_$j"}--$form->{"new_partsgroup_id_$j"}|;
  1778. $form->{"sku_$i"} = $form->{"new_partnumber_$j"};
  1779. $i++;
  1780. }
  1781. }
  1782. $form->{rowcount} = $i;
  1783. # delete all the new_ variables
  1784. for $i ( 1 .. $form->{lastndx} ) {
  1785. for (
  1786. qw(id partnumber description unit sellprice lastcost partsgroup partsgroup_id)
  1787. )
  1788. {
  1789. delete $form->{"new_${_}_$i"};
  1790. }
  1791. delete $form->{"ndx_$i"};
  1792. }
  1793. for (qw(ndx lastndx nextsub)) { delete $form->{$_} }
  1794. &{"$form->{db}_pricelist"};
  1795. }
  1796. sub save_pricelist {
  1797. &{"CT::save_$form->{db}"}( "", \%myconfig, \%$form );
  1798. $callback = $form->{callback};
  1799. $form->{callback} = "$form->{script}?action=edit";
  1800. for (qw(db id login path sessionid)) {
  1801. $form->{callback} .= "&$_=$form->{$_}";
  1802. }
  1803. $form->{callback} .= "&callback=" . $form->escape( $callback, 1 );
  1804. if ( CT->save_pricelist( \%myconfig, \%$form ) ) {
  1805. $form->redirect;
  1806. }
  1807. else {
  1808. $form->error( $locale->text('Could not save pricelist!') );
  1809. }
  1810. }
  1811. sub add_transaction {
  1812. $form->isblank( "name", $locale->text("Name missing!") );
  1813. &{"CT::save_$form->{db}"}( "", \%myconfig, \%$form );
  1814. $form->{callback} = $form->escape( $form->{callback}, 1 );
  1815. $name = $form->escape( $form->{name}, 1 );
  1816. $form->{callback} =
  1817. "$form->{script}?login=$form->{login}&path=$form->{path}&sessionid=$form->{sessionid}&action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
  1818. $form->redirect;
  1819. }
  1820. sub ap_transaction {
  1821. $form->{script} = "ap.pl";
  1822. $form->{type} = "ap_transaction";
  1823. &add_transaction;
  1824. }
  1825. sub ar_transaction {
  1826. $form->{script} = "ar.pl";
  1827. $form->{type} = "ar_transaction";
  1828. &add_transaction;
  1829. }
  1830. sub sales_invoice {
  1831. $form->{script} = "is.pl";
  1832. $form->{type} = "invoice";
  1833. &add_transaction;
  1834. }
  1835. sub pos {
  1836. $form->{script} = "ps.pl";
  1837. $form->{type} = "pos_invoice";
  1838. &add_transaction;
  1839. }
  1840. sub vendor_invoice {
  1841. $form->{script} = "ir.pl";
  1842. $form->{type} = "invoice";
  1843. &add_transaction;
  1844. }
  1845. sub rfq {
  1846. $form->{script} = "oe.pl";
  1847. $form->{type} = "request_quotation";
  1848. &add_transaction;
  1849. }
  1850. sub quotation {
  1851. $form->{script} = "oe.pl";
  1852. $form->{type} = "sales_quotation";
  1853. &add_transaction;
  1854. }
  1855. sub sales_order {
  1856. $form->{script} = "oe.pl";
  1857. $form->{type} = "sales_order";
  1858. &add_transaction;
  1859. }
  1860. sub purchase_order {
  1861. $form->{script} = "oe.pl";
  1862. $form->{type} = "purchase_order";
  1863. &add_transaction;
  1864. }
  1865. sub save_as_new {
  1866. delete $form->{id};
  1867. &save;
  1868. }
  1869. sub save {
  1870. # $locale->text('Customer saved!')
  1871. # $locale->text('Vendor saved!')
  1872. $msg = ucfirst $form->{db};
  1873. $msg .= " saved!";
  1874. $form->isblank( "name", $locale->text("Name missing!") );
  1875. &{"CT::save_$form->{db}"}( "", \%myconfig, \%$form );
  1876. $form->redirect( $locale->text($msg) );
  1877. }
  1878. sub delete {
  1879. # $locale->text('Customer deleted!')
  1880. # $locale->text('Cannot delete customer!')
  1881. # $locale->text('Vendor deleted!')
  1882. # $locale->text('Cannot delete vendor!')
  1883. CT->delete( \%myconfig, \%$form );
  1884. $msg = ucfirst $form->{db};
  1885. $msg .= " deleted!";
  1886. $form->redirect( $locale->text($msg) );
  1887. }
  1888. sub continue { &{ $form->{nextsub} } }
  1889. sub add_customer { &add }
  1890. sub add_vendor { &add }