summaryrefslogtreecommitdiff
path: root/bin/io.pl
blob: e4188ac230de0650da6cbe76c068283a7748233a (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) 2002
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation; either version 2 of the License, or
  25. # (at your option) any later version.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. # You should have received a copy of the GNU General Public License
  32. # along with this program; if not, write to the Free Software
  33. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. #######################################################################
  35. #
  36. # common routines used in is, ir, oe
  37. #
  38. #######################################################################
  39. use LedgerSMB::Tax;
  40. use LedgerSMB::Sysconfig;
  41. # any custom scripts for this one
  42. if ( -f "bin/custom/io.pl" ) {
  43. eval { require "bin/custom/io.pl"; };
  44. }
  45. if ( -f "bin/custom/$form->{login}_io.pl" ) {
  46. eval { require "bin/custom/$form->{login}_io.pl"; };
  47. }
  48. 1;
  49. # end of main
  50. # this is for our long dates
  51. # $locale->text('January')
  52. # $locale->text('February')
  53. # $locale->text('March')
  54. # $locale->text('April')
  55. # $locale->text('May ')
  56. # $locale->text('June')
  57. # $locale->text('July')
  58. # $locale->text('August')
  59. # $locale->text('September')
  60. # $locale->text('October')
  61. # $locale->text('November')
  62. # $locale->text('December')
  63. # this is for our short month
  64. # $locale->text('Jan')
  65. # $locale->text('Feb')
  66. # $locale->text('Mar')
  67. # $locale->text('Apr')
  68. # $locale->text('May')
  69. # $locale->text('Jun')
  70. # $locale->text('Jul')
  71. # $locale->text('Aug')
  72. # $locale->text('Sep')
  73. # $locale->text('Oct')
  74. # $locale->text('Nov')
  75. # $locale->text('Dec')
  76. sub _calc_taxes {
  77. for $i (1 .. $form->{rowcount}){
  78. my $linetotal =
  79. $form->parse_amount(\%myconfig, $form->{"sellprice_$i"})
  80. * $form->parse_amount(\%myconfig, $form->{"qty_$i"})
  81. * (1 - $form->parse_amount(\%myconfig, $form->{"discount_$i"})
  82. / 100);
  83. @taxaccounts = Tax::init_taxes(
  84. $form, $form->{"taxaccounts_$i"},
  85. $form->{'taxaccounts'}
  86. );
  87. my $tax;
  88. my $fxtax;
  89. my $amount;
  90. if ( $form->{taxincluded} ) {
  91. $tax += $amount =
  92. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
  93. $form->{"sellprice_$i"} -= $amount / $form->{"qty_$i"};
  94. }
  95. else {
  96. $tax += $amount =
  97. Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 0 );
  98. $fxtax +=
  99. Tax::calculate_taxes( \@taxaccounts, $form, $fxlinetotal, 0 )
  100. if $fxlinetotal;
  101. }
  102. for (@taxaccounts) {
  103. $form->{taxes}{$_->account} = 0 if ! $form->{taxes}{$_->account};
  104. $form->{taxes}{$_->account} += $_->value;
  105. }
  106. }
  107. }
  108. sub display_row {
  109. my $numrows = shift;
  110. @column_index = qw(runningnumber partnumber description qty);
  111. if ( $form->{type} eq "sales_order" ) {
  112. push @column_index, "ship";
  113. $column_data{ship} =
  114. qq|<th class="listheading" align="center" width="auto">|
  115. . $locale->text('Ship')
  116. . qq|</th>|;
  117. }
  118. if ( $form->{type} eq "purchase_order" ) {
  119. push @column_index, "ship";
  120. $column_data{ship} =
  121. qq|<th class="listheading" align="center" width="auto">|
  122. . $locale->text('Recd')
  123. . qq|</th>|;
  124. }
  125. for (qw(projectnumber partsgroup)) {
  126. $form->{"select$_"} = $form->unescape( $form->{"select$_"} )
  127. if $form->{"select$_"};
  128. }
  129. if ( $form->{language_code} ne $form->{oldlanguage_code} ) {
  130. # rebuild partsgroup
  131. $l{language_code} = $form->{language_code};
  132. $l{searchitems} = 'nolabor' if $form->{vc} eq 'customer';
  133. $form->get_partsgroup( \%myconfig, \%l );
  134. if ( @{ $form->{all_partsgroup} } ) {
  135. $form->{selectpartsgroup} = "<option>\n";
  136. foreach $ref ( @{ $form->{all_partsgroup} } ) {
  137. if ( $ref->{translation} ) {
  138. $form->{selectpartsgroup} .=
  139. qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{translation}\n|;
  140. }
  141. else {
  142. $form->{selectpartsgroup} .=
  143. qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{partsgroup}\n|;
  144. }
  145. }
  146. }
  147. $form->{oldlanguage_code} = $form->{language_code};
  148. }
  149. push @column_index, @{LedgerSMB::Sysconfig::io_lineitem_columns};
  150. my $colspan = $#column_index + 1;
  151. $form->{invsubtotal} = 0;
  152. for ( split / /, $form->{taxaccounts} ) { $form->{"${_}_base"} = 0 }
  153. $column_data{runningnumber} =
  154. qq|<th class="listheading" nowrap>| . $locale->text('Item') . qq|</th>|;
  155. $column_data{partnumber} =
  156. qq|<th class="listheading" nowrap>| . $locale->text('Number') . qq|</th>|;
  157. $column_data{description} =
  158. qq|<th class="listheading" nowrap>|
  159. . $locale->text('Description')
  160. . qq|</th>|;
  161. $column_data{qty} =
  162. qq|<th class="listheading" nowrap>| . $locale->text('Qty') . qq|</th>|;
  163. $column_data{unit} =
  164. qq|<th class="listheading" nowrap>| . $locale->text('Unit') . qq|</th>|;
  165. $column_data{sellprice} =
  166. qq|<th class="listheading" nowrap>| . $locale->text('Price') . qq|</th>|;
  167. $column_data{discount} = qq|<th class="listheading">%</th>|;
  168. $column_data{linetotal} =
  169. qq|<th class="listheading" nowrap>| . $locale->text('Extended') . qq|</th>|;
  170. $column_data{bin} =
  171. qq|<th class="listheading" nowrap>| . $locale->text('Bin') . qq|</th>|;
  172. $column_data{onhand} =
  173. qq|<th class="listheading" nowrap>| . $locale->text('OH') . qq|</th>|;
  174. print qq|
  175. <tr>
  176. <td>
  177. <table width="100%">
  178. <tr class="listheading">|;
  179. for (@column_index) { print "\n$column_data{$_}" }
  180. print qq|
  181. </tr>
  182. |;
  183. $deliverydate = $locale->text('Delivery Date');
  184. $serialnumber = $locale->text('Serial No.');
  185. $projectnumber = $locale->text('Project');
  186. $group = $locale->text('Group');
  187. $sku = $locale->text('SKU');
  188. $delvar = 'deliverydate';
  189. if ( $form->{type} =~ /_(order|quotation)$/ ) {
  190. $reqdate = $locale->text('Required by');
  191. $delvar = 'reqdate';
  192. }
  193. $exchangerate = $form->parse_amount( \%myconfig, $form->{exchangerate} );
  194. $exchangerate = ($exchangerate) ? $exchangerate : 1;
  195. $spc = substr( $myconfig{numberformat}, -3, 1 );
  196. for $i ( 1 .. $numrows ) {
  197. if ( $spc eq '.' ) {
  198. ( $null, $dec ) = split /\./, $form->{"sellprice_$i"};
  199. }
  200. else {
  201. ( $null, $dec ) = split /,/, $form->{"sellprice_$i"};
  202. }
  203. $dec = length $dec;
  204. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  205. # undo formatting
  206. for (qw(qty oldqty ship discount sellprice)) {
  207. $form->{"${_}_$i"} =
  208. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  209. }
  210. if ( $form->{"qty_$i"} != $form->{"oldqty_$i"} ) {
  211. # check pricematrix
  212. @a = split / /, $form->{"pricematrix_$i"};
  213. if ( scalar @a > 2 ) {
  214. foreach $item (@a) {
  215. ( $q, $p ) = split /:/, $item;
  216. if ( ( $p * 1 ) && ( $form->{"qty_$i"} >= ( $q * 1 ) ) ) {
  217. ($dec) = ( $p =~ /\.(\d+)/ );
  218. $dec = length $dec;
  219. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  220. $form->{"sellprice_$i"} =
  221. $form->round_amount( $p / $exchangerate,
  222. $decimalplaces );
  223. }
  224. }
  225. }
  226. }
  227. $discount =
  228. $form->round_amount(
  229. $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100,
  230. $decimalplaces );
  231. $linetotal = $form->round_amount( $form->{"sellprice_$i"} - $discount,
  232. $decimalplaces );
  233. $linetotal = $form->round_amount( $linetotal * $form->{"qty_$i"}, 2 );
  234. if (
  235. ( $rows = $form->numtextrows( $form->{"description_$i"}, 46, 6 ) ) >
  236. 1 )
  237. {
  238. $form->{"description_$i"} =
  239. $form->quote( $form->{"description_$i"} );
  240. $column_data{description} =
  241. qq|<td><textarea name="description_$i" rows="$rows" cols="46" wrap="soft">$form->{"description_$i"}</textarea></td>|;
  242. }
  243. else {
  244. $form->{"description_$i"} =
  245. $form->quote( $form->{"description_$i"} );
  246. $column_data{description} =
  247. qq|<td><input name="description_$i" size="48" value="$form->{"description_$i"}"></td>|;
  248. }
  249. for (qw(partnumber sku unit)) {
  250. $form->{"${_}_$i"} = $form->quote( $form->{"${_}_$i"} );
  251. }
  252. $skunumber = qq|
  253. <p><b>$sku</b> $form->{"sku_$i"}|
  254. if ( $form->{vc} eq 'vendor' && $form->{"sku_$i"} );
  255. if ( $form->{selectpartsgroup} ) {
  256. if ( $i < $numrows ) {
  257. $partsgroup = qq|
  258. <b>$group</b>
  259. <input type="hidden" name="partsgroup_$i" value="$form->{"partsgroup_$i"}">|;
  260. ( $form->{"partsgroup_$i"} ) = split /--/,
  261. $form->{"partsgroup_$i"};
  262. $partsgroup .= $form->{"partsgroup_$i"};
  263. $partsgroup = "" unless $form->{"partsgroup_$i"};
  264. }
  265. }
  266. $delivery = qq|
  267. <td colspan="2" nowrap>
  268. <b>${$delvar}</b>
  269. <input name="${delvar}_$i" size="11" title="$myconfig{dateformat}" value="$form->{"${delvar}_$i"}"></td>
  270. |;
  271. $column_data{runningnumber} =
  272. qq|<td><input name="runningnumber_$i" size="3" value="$i"></td>|;
  273. $column_data{partnumber} =
  274. qq|<td><input name="partnumber_$i" size="15" value="$form->{"partnumber_$i"}" accesskey="$i" title="[Alt-$i]">$skunumber</td>|;
  275. $column_data{qty} =
  276. qq|<td align="right"><input name="qty_$i" title="$form->{"onhand_$i"}" size="5" value="|
  277. . $form->format_amount( \%myconfig, $form->{"qty_$i"} )
  278. . qq|"></td>|;
  279. $column_data{ship} =
  280. qq|<td align="right"><input name="ship_$i" size="5" value="|
  281. . $form->format_amount( \%myconfig, $form->{"ship_$i"} )
  282. . qq|"></td>|;
  283. $column_data{unit} =
  284. qq|<td><input name="unit_$i" size="5" maxlength="5" value="$form->{"unit_$i"}" /></td>|;
  285. $column_data{sellprice} =
  286. qq|<td align="right"><input name="sellprice_$i" size="9" value="|
  287. . $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  288. $decimalplaces )
  289. . qq|"></td>|;
  290. $column_data{discount} =
  291. qq|<td align="right"><input name="discount_$i" size="3" value="|
  292. . $form->format_amount( \%myconfig, $form->{"discount_$i"} )
  293. . qq|"></td>|;
  294. $column_data{linetotal} =
  295. qq|<td align="right">|
  296. . $form->format_amount( \%myconfig, $linetotal, 2 )
  297. . qq|</td>|;
  298. $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
  299. $column_data{onhand} = qq|<td>$form->{"onhand_$i"}</td>|;
  300. print qq|
  301. <tr valign="top">|;
  302. for (@column_index) {
  303. print "\n$column_data{$_}";
  304. }
  305. print qq|
  306. </tr>
  307. <input type="hidden" name="oldqty_$i" value="$form->{"qty_$i"}">
  308. |;
  309. for (
  310. qw(orderitems_id id bin weight listprice lastcost taxaccounts pricematrix sku onhand assembly inventory_accno_id income_accno_id expense_accno_id)
  311. )
  312. {
  313. $form->hide_form("${_}_$i");
  314. }
  315. $form->{selectprojectnumber} =~ s/ selected//;
  316. $form->{selectprojectnumber} =~
  317. s/(<option value="\Q$form->{"projectnumber_$i"}\E")/$1 selected/;
  318. $project = qq|
  319. <b>$projectnumber</b>
  320. <select name="projectnumber_$i">$form->{selectprojectnumber}</select>
  321. | if $form->{selectprojectnumber};
  322. if ( ( $rows = $form->numtextrows( $form->{"notes_$i"}, 46, 6 ) ) > 1 )
  323. {
  324. $form->{"notes_$i"} = $form->quote( $form->{"notes_$i"} );
  325. $notes =
  326. qq|<td><textarea name="notes_$i" rows="$rows" cols="46" wrap="soft">$form->{"notes_$i"}</textarea></td>|;
  327. }
  328. else {
  329. $form->{"notes_$i"} = $form->quote( $form->{"notes_$i"} );
  330. $notes =
  331. qq|<td><input name="notes_$i" size="48" value="$form->{"notes_$i"}"></td>|;
  332. }
  333. $serial = qq|
  334. <td colspan="6" nowrap><b>$serialnumber</b> <input name="serialnumber_$i" value="$form->{"serialnumber_$i"}"></td>|
  335. if $form->{type} !~ /_quotation/;
  336. if ( $i == $numrows ) {
  337. $partsgroup = "";
  338. if ( $form->{selectpartsgroup} ) {
  339. $partsgroup = qq|
  340. <b>$group</b>
  341. <select name="partsgroup_$i">$form->{selectpartsgroup}</select>
  342. |;
  343. }
  344. $serial = "";
  345. $project = "";
  346. $delivery = "";
  347. $notes = "";
  348. }
  349. # print second and third row
  350. print qq|
  351. <tr valign="top">
  352. $delivery
  353. $notes
  354. $serial
  355. </tr>
  356. <tr valign="top">
  357. <td colspan="$colspan">
  358. $project
  359. $partsgroup
  360. </td>
  361. </tr>
  362. <tr>
  363. <td colspan="$colspan"><hr size="1" noshade></td>
  364. </tr>
  365. |;
  366. $skunumber = "";
  367. for ( split / /, $form->{"taxaccounts_$i"} ) {
  368. $form->{"${_}_base"} += $linetotal;
  369. }
  370. $form->{invsubtotal} += $linetotal;
  371. }
  372. print qq|
  373. </table>
  374. </td>
  375. </tr>
  376. |;
  377. $form->hide_form(qw(audittrail));
  378. print qq|
  379. <input type="hidden" name="oldcurrency" value="$form->{currency}">
  380. <input type="hidden" name="selectpartsgroup" value="|
  381. . $form->escape( $form->{selectpartsgroup}, 1 ) . qq|">
  382. <input type="hidden" name="selectprojectnumber" value="|
  383. . $form->escape( $form->{selectprojectnumber}, 1 ) . qq|">
  384. |;
  385. }
  386. sub select_item {
  387. if ( $form->{vc} eq "vendor" ) {
  388. @column_index =
  389. qw(ndx partnumber sku description partsgroup onhand sellprice);
  390. }
  391. else {
  392. @column_index =
  393. qw(ndx partnumber description partsgroup onhand sellprice);
  394. }
  395. $column_data{ndx} = qq|<th>&nbsp;</th>|;
  396. $column_data{partnumber} =
  397. qq|<th class="listheading">| . $locale->text('Number') . qq|</th>|;
  398. $column_data{sku} =
  399. qq|<th class="listheading">| . $locale->text('SKU') . qq|</th>|;
  400. $column_data{description} =
  401. qq|<th class="listheading">| . $locale->text('Description') . qq|</th>|;
  402. $column_data{partsgroup} =
  403. qq|<th class="listheading">| . $locale->text('Group') . qq|</th>|;
  404. $column_data{sellprice} =
  405. qq|<th class="listheading">| . $locale->text('Price') . qq|</th>|;
  406. $column_data{onhand} =
  407. qq|<th class="listheading">| . $locale->text('Qty') . qq|</th>|;
  408. $exchangerate = ( $form->{exchangerate} ) ? $form->{exchangerate} : 1;
  409. # list items with radio button on a form
  410. $form->header;
  411. $title = $locale->text('Select items');
  412. print qq|
  413. <body>
  414. <form method="post" action="$form->{script}">
  415. <table width="100%">
  416. <tr>
  417. <th class="listtop">$title</th>
  418. </tr>
  419. <tr height="5"></tr>
  420. <tr>
  421. <td>$option</td>
  422. </tr>
  423. <tr>
  424. <td>
  425. <table width="100%">
  426. <tr class="listheading">|;
  427. for (@column_index) { print "\n$column_data{$_}" }
  428. print qq|
  429. </tr>
  430. |;
  431. my $i = 0;
  432. foreach $ref ( @{ $form->{item_list} } ) {
  433. $i++;
  434. for (qw(sku partnumber description unit notes partsgroup)) {
  435. $ref->{$_} = $form->quote( $ref->{$_} );
  436. }
  437. $column_data{ndx} =
  438. qq|<td><input name="ndx_$i" class="checkbox" type="checkbox" value="$i"></td>|;
  439. for (qw(partnumber sku description partsgroup)) {
  440. $column_data{$_} = qq|<td>$ref->{$_}&nbsp;</td>|;
  441. }
  442. $column_data{sellprice} = qq|<td align="right">|
  443. . $form->format_amount( \%myconfig, $ref->{sellprice} / $exchangerate,
  444. 2, "&nbsp;" )
  445. . qq|</td>|;
  446. $column_data{onhand} =
  447. qq|<td align="right">|
  448. . $form->format_amount( \%myconfig, $ref->{onhand}, '', "&nbsp;" )
  449. . qq|</td>|;
  450. $j++;
  451. $j %= 2;
  452. print qq|
  453. <tr class="listrow$j">|;
  454. for (@column_index) {
  455. print "\n$column_data{$_}";
  456. }
  457. print qq|
  458. </tr>
  459. |;
  460. for (
  461. qw(partnumber sku description partsgroup partsgroup_id bin weight sellprice listprice lastcost onhand unit assembly taxaccounts inventory_accno_id income_accno_id expense_accno_id pricematrix id notes)
  462. )
  463. {
  464. print
  465. qq|<input type="hidden" name="new_${_}_$i" value="$ref->{$_}">\n|;
  466. }
  467. }
  468. print qq|
  469. </table>
  470. </td>
  471. </tr>
  472. <tr>
  473. <td><hr size="3" noshade></td>
  474. </tr>
  475. </table>
  476. <input name="lastndx" type="hidden" value="$i">
  477. |;
  478. # delete variables
  479. for (qw(nextsub item_list)) { delete $form->{$_} }
  480. $form->{action} = "item_selected";
  481. $form->hide_form;
  482. print qq|
  483. <input type="hidden" name="nextsub" value="item_selected">
  484. <br>
  485. <button class="submit" type="submit" name="action" value="continue">|
  486. . $locale->text('Continue')
  487. . qq|</button>
  488. </form>
  489. </body>
  490. </html>
  491. |;
  492. }
  493. sub item_selected {
  494. $i = $form->{rowcount} - 1;
  495. $i = $form->{assembly_rows} - 1 if ( $form->{item} eq 'assembly' );
  496. $qty =
  497. ( $form->{"qty_$form->{rowcount}"} )
  498. ? $form->{"qty_$form->{rowcount}"}
  499. : 1;
  500. for $j ( 1 .. $form->{lastndx} ) {
  501. if ( $form->{"ndx_$j"} ) {
  502. $i++;
  503. $form->{"qty_$i"} = $qty;
  504. $form->{"discount_$i"} = $form->{discount} * 100;
  505. $form->{"reqdate_$i"} = $form->{reqdate}
  506. if $form->{type} !~ /_quotation/;
  507. for (
  508. qw(id partnumber sku description listprice lastcost bin unit weight assembly taxaccounts pricematrix onhand notes inventory_accno_id income_accno_id expense_accno_id)
  509. )
  510. {
  511. $form->{"${_}_$i"} = $form->{"new_${_}_$j"};
  512. }
  513. $form->{"sellprice_$i"} = $form->{"new_sellprice_$j"}
  514. if not $form->{"sellprice_$i"};
  515. $form->{"partsgroup_$i"} =
  516. qq|$form->{"new_partsgroup_$j"}--$form->{"new_partsgroup_id_$j"}|;
  517. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  518. $dec = length $dec;
  519. $decimalplaces1 = ( $dec > 2 ) ? $dec : 2;
  520. ($dec) = ( $form->{"lastcost_$i"} =~ /\.(\d+)/ );
  521. $dec = length $dec;
  522. $decimalplaces2 = ( $dec > 2 ) ? $dec : 2;
  523. # if there is an exchange rate adjust sellprice
  524. if ( ( $form->{exchangerate} * 1 ) ) {
  525. for (qw(sellprice listprice lastcost)) {
  526. $form->{"${_}_$i"} /= $form->{exchangerate};
  527. }
  528. # don't format list and cost
  529. $form->{"sellprice_$i"} =
  530. $form->round_amount( $form->{"sellprice_$i"},
  531. $decimalplaces1 );
  532. }
  533. # this is for the assembly
  534. if ( $form->{item} eq 'assembly' ) {
  535. $form->{"adj_$i"} = 1;
  536. for (qw(sellprice listprice weight)) {
  537. $form->{$_} =
  538. $form->parse_amount( \%myconfig, $form->{$_} );
  539. }
  540. $form->{sellprice} +=
  541. ( $form->{"sellprice_$i"} * $form->{"qty_$i"} );
  542. $form->{weight} += ( $form->{"weight_$i"} * $form->{"qty_$i"} );
  543. }
  544. $amount =
  545. $form->{"sellprice_$i"} * ( 1 - $form->{"discount_$i"} / 100 ) *
  546. $form->{"qty_$i"};
  547. for ( split / /, $form->{"taxaccounts_$i"} ) {
  548. $form->{"${_}_base"} += $amount;
  549. }
  550. if ( !$form->{taxincluded} ) {
  551. my @taxlist = Tax::init_taxes( $form, $form->{"taxaccounts_$i"},
  552. $form->{taxaccounts} );
  553. $amount += Tax::calculate_taxes( \@taxlist, $form, $amount, 0 );
  554. }
  555. $form->{creditremaining} -= $amount;
  556. $form->{"runningnumber_$i"} = $i;
  557. # format amounts
  558. if ( $form->{item} ne 'assembly' ) {
  559. for (qw(sellprice listprice)) {
  560. $form->{"${_}_$i"} =
  561. $form->format_amount( \%myconfig, $form->{"${_}_$i"},
  562. $decimalplaces1 );
  563. }
  564. $form->{"lastcost_$i"} =
  565. $form->format_amount( \%myconfig, $form->{"lastcost_$i"},
  566. $decimalplaces2 );
  567. }
  568. $form->{"discount_$i"} =
  569. $form->format_amount( \%myconfig, $form->{"discount_$i"} );
  570. }
  571. }
  572. $form->{rowcount} = $i;
  573. $form->{assembly_rows} = $i if ( $form->{item} eq 'assembly' );
  574. $form->{focus} = "description_$i";
  575. # delete all the new_ variables
  576. for $i ( 1 .. $form->{lastndx} ) {
  577. for (
  578. qw(id partnumber sku description sellprice listprice lastcost bin unit weight assembly taxaccounts pricematrix onhand notes inventory_accno_id income_accno_id expense_accno_id)
  579. )
  580. {
  581. delete $form->{"new_${_}_$i"};
  582. }
  583. }
  584. for (qw(ndx lastndx nextsub)) { delete $form->{$_} }
  585. &display_form;
  586. }
  587. sub new_item {
  588. if ( $form->{language_code} && $form->{"description_$form->{rowcount}"} ) {
  589. $form->error( $locale->text('Translation not on file!') );
  590. }
  591. # change callback
  592. $form->{old_callback} = $form->escape( $form->{callback}, 1 );
  593. $form->{callback} =
  594. $form->escape( "$form->{script}?action=display_form", 1 );
  595. # delete action
  596. delete $form->{action};
  597. # save all other form variables in a previousform variable
  598. if ( !$form->{previousform} ) {
  599. foreach $key ( keys %$form ) {
  600. # escape ampersands
  601. $form->{$key} =~ s/&/%26/g;
  602. $form->{previousform} .= qq|$key=$form->{$key}&|;
  603. }
  604. chop $form->{previousform};
  605. $form->{previousform} = $form->escape( $form->{previousform}, 1 );
  606. }
  607. $i = $form->{rowcount};
  608. for (qw(partnumber description)) {
  609. $form->{"${_}_$i"} = $form->quote( $form->{"${_}_$i"} );
  610. }
  611. $form->header;
  612. print qq|
  613. <body>
  614. <h4 class="error">| . $locale->text('Item not on file!') . qq|</h4>|;
  615. if ( $myconfig{acs} !~
  616. /(Goods \& Services--Add Part|Goods \& Services--Add Service)/ )
  617. {
  618. print qq|
  619. <h4>| . $locale->text('What type of item is this?') . qq|</h4>
  620. <form method="post" action="ic.pl">
  621. <p>
  622. <input class="radio" type="radio" name="item" value="part" checked>&nbsp;|
  623. . $locale->text('Part') . qq|<br>
  624. <input class="radio" type="radio" name="item" value="service">&nbsp;|
  625. . $locale->text('Service')
  626. . qq|
  627. <input type="hidden" name="partnumber" value="$form->{"partnumber_$i"}">
  628. <input type="hidden" name="description" value="$form->{"description_$i"}">
  629. <input type="hidden" name="nextsub" value="add">
  630. <input type="hidden" name="action" value="add">
  631. |;
  632. $form->hide_form(qw(previousform rowcount path login sessionid));
  633. print qq|
  634. <p>
  635. <button class="submit" type="submit" name="action" value="continue">|
  636. . $locale->text('Continue')
  637. . qq|</button>
  638. </form>
  639. |;
  640. }
  641. print qq|
  642. </body>
  643. </html>
  644. |;
  645. }
  646. sub display_form {
  647. # if we have a display_form
  648. if ( $form->{display_form} ) {
  649. &{"$form->{display_form}"};
  650. exit;
  651. }
  652. &form_header;
  653. $numrows = ++$form->{rowcount};
  654. $subroutine = "display_row";
  655. if ( $form->{item} eq 'part' ) {
  656. # create makemodel rows
  657. &makemodel_row( ++$form->{makemodel_rows} );
  658. &vendor_row( ++$form->{vendor_rows} );
  659. $numrows = ++$form->{customer_rows};
  660. $subroutine = "customer_row";
  661. }
  662. if ( $form->{item} eq 'assembly' ) {
  663. # create makemodel rows
  664. &makemodel_row( ++$form->{makemodel_rows} );
  665. $numrows = ++$form->{customer_rows};
  666. $subroutine = "customer_row";
  667. }
  668. if ( $form->{item} eq 'service' ) {
  669. &vendor_row( ++$form->{vendor_rows} );
  670. $numrows = ++$form->{customer_rows};
  671. $subroutine = "customer_row";
  672. }
  673. if ( $form->{item} eq 'labor' ) {
  674. $numrows = 0;
  675. }
  676. # create rows
  677. &{$subroutine}($numrows) if $numrows;
  678. &form_footer;
  679. }
  680. sub check_form {
  681. my @a = ();
  682. my $count = 0;
  683. my $i;
  684. my $j;
  685. my @flds =
  686. qw(id runningnumber partnumber description partsgroup qty ship unit sellprice discount oldqty orderitems_id bin weight listprice lastcost taxaccounts pricematrix sku onhand assembly inventory_accno_id income_accno_id expense_accno_id notes reqdate deliverydate serialnumber projectnumber);
  687. # remove any makes or model rows
  688. if ( $form->{item} eq 'part' ) {
  689. for (qw(listprice sellprice lastcost avgcost weight rop markup)) {
  690. $form->{$_} = $form->parse_amount( \%myconfig, $form->{$_} );
  691. }
  692. &calc_markup;
  693. @flds = qw(make model);
  694. $count = 0;
  695. @a = ();
  696. for $i ( 1 .. $form->{makemodel_rows} ) {
  697. if ( ( $form->{"make_$i"} ne "" ) || ( $form->{"model_$i"} ne "" ) )
  698. {
  699. push @a, {};
  700. $j = $#a;
  701. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  702. $count++;
  703. }
  704. }
  705. $form->redo_rows( \@flds, \@a, $count, $form->{makemodel_rows} );
  706. $form->{makemodel_rows} = $count;
  707. &check_vendor;
  708. &check_customer;
  709. }
  710. if ( $form->{item} eq 'service' ) {
  711. for (qw(sellprice listprice lastcost avgcost markup)) {
  712. $form->{$_} = $form->parse_amount( \%myconfig, $form->{$_} );
  713. }
  714. &calc_markup;
  715. &check_vendor;
  716. &check_customer;
  717. }
  718. if ( $form->{item} eq 'assembly' ) {
  719. if ( !$form->{project_id} ) {
  720. $form->{sellprice} = 0;
  721. $form->{listprice} = 0;
  722. $form->{lastcost} = 0;
  723. $form->{weight} = 0;
  724. }
  725. for (qw(rop stock markup)) {
  726. $form->{$_} = $form->parse_amount( \%myconfig, $form->{$_} );
  727. }
  728. @flds =
  729. qw(id qty unit bom adj partnumber description sellprice listprice lastcost weight assembly runningnumber partsgroup);
  730. $count = 0;
  731. @a = ();
  732. for $i ( 1 .. ( $form->{assembly_rows} - 1 ) ) {
  733. if ( $form->{"qty_$i"} ) {
  734. push @a, {};
  735. my $j = $#a;
  736. $form->{"qty_$i"} =
  737. $form->parse_amount( \%myconfig, $form->{"qty_$i"} );
  738. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  739. if ( !$form->{project_id} ) {
  740. for (qw(sellprice listprice weight lastcost)) {
  741. $form->{$_} +=
  742. ( $form->{"${_}_$i"} * $form->{"qty_$i"} );
  743. }
  744. }
  745. $count++;
  746. }
  747. }
  748. if ( $form->{markup} && $form->{markup} != $form->{oldmarkup} ) {
  749. $form->{sellprice} = 0;
  750. &calc_markup;
  751. }
  752. for (qw(sellprice lastcost listprice)) {
  753. $form->{$_} = $form->round_amount( $form->{$_}, 2 );
  754. }
  755. $form->redo_rows( \@flds, \@a, $count, $form->{assembly_rows} );
  756. $form->{assembly_rows} = $count;
  757. $count = 0;
  758. @flds = qw(make model);
  759. @a = ();
  760. for $i ( 1 .. ( $form->{makemodel_rows} ) ) {
  761. if ( ( $form->{"make_$i"} ne "" ) || ( $form->{"model_$i"} ne "" ) )
  762. {
  763. push @a, {};
  764. my $j = $#a;
  765. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  766. $count++;
  767. }
  768. }
  769. $form->redo_rows( \@flds, \@a, $count, $form->{makemodel_rows} );
  770. $form->{makemodel_rows} = $count;
  771. &check_customer;
  772. }
  773. if ( $form->{type} ) {
  774. # this section applies to invoices and orders
  775. # remove any empty numbers
  776. $count = 0;
  777. @a = ();
  778. if ( $form->{rowcount} ) {
  779. for $i ( 1 .. $form->{rowcount} - 1 ) {
  780. if ( $form->{"partnumber_$i"} ) {
  781. push @a, {};
  782. my $j = $#a;
  783. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  784. $count++;
  785. }
  786. }
  787. $form->redo_rows( \@flds, \@a, $count, $form->{rowcount} );
  788. $form->{rowcount} = $count;
  789. $form->{creditremaining} -= &invoicetotal;
  790. }
  791. }
  792. &display_form;
  793. }
  794. sub calc_markup {
  795. if ( $form->{markup} ) {
  796. if ( $form->{markup} != $form->{oldmarkup} ) {
  797. if ( $form->{lastcost} ) {
  798. $form->{sellprice} =
  799. $form->{lastcost} * ( 1 + $form->{markup} / 100 );
  800. $form->{sellprice} =
  801. $form->round_amount( $form->{sellprice}, 2 );
  802. }
  803. else {
  804. $form->{lastcost} =
  805. $form->{sellprice} / ( 1 + $form->{markup} / 100 );
  806. $form->{lastcost} = $form->round_amount( $form->{lastcost}, 2 );
  807. }
  808. }
  809. }
  810. else {
  811. if ( $form->{lastcost} ) {
  812. $form->{markup} =
  813. $form->round_amount(
  814. ( ( 1 - $form->{sellprice} / $form->{lastcost} ) * 100 ), 1 );
  815. }
  816. $form->{markup} = "" if $form->{markup} == 0;
  817. }
  818. }
  819. sub invoicetotal {
  820. $form->{oldinvtotal} = 0;
  821. # add all parts and deduct paid
  822. for ( split / /, $form->{taxaccounts} ) { $form->{"${_}_base"} = 0 }
  823. my ( $amount, $sellprice, $discount, $qty );
  824. for $i ( 1 .. $form->{rowcount} ) {
  825. $sellprice = $form->parse_amount( \%myconfig, $form->{"sellprice_$i"} );
  826. $discount = $form->parse_amount( \%myconfig, $form->{"discount_$i"} );
  827. $qty = $form->parse_amount( \%myconfig, $form->{"qty_$i"} );
  828. $amount = $sellprice * ( 1 - $discount / 100 ) * $qty;
  829. for ( split / /, $form->{"taxaccounts_$i"} ) {
  830. $form->{"${_}_base"} += $amount;
  831. }
  832. $form->{oldinvtotal} += $amount;
  833. }
  834. if ( !$form->{taxincluded} ) {
  835. my @taxlist = Tax::init_taxes( $form, $form->{taxaccounts} );
  836. $form->{oldinvtotal} +=
  837. Tax::calculate_taxes( \@taxlist, $form, $amount, 0 );
  838. }
  839. $form->{oldtotalpaid} = 0;
  840. for $i ( 1 .. $form->{paidaccounts} ) {
  841. $form->{oldtotalpaid} += $form->{"paid_$i"};
  842. }
  843. # return total
  844. ( $form->{oldinvtotal} - $form->{oldtotalpaid} );
  845. }
  846. sub validate_items {
  847. # check if items are valid
  848. if ( $form->{rowcount} == 1 ) {
  849. &update;
  850. exit;
  851. }
  852. for $i ( 1 .. $form->{rowcount} - 1 ) {
  853. $form->isblank( "partnumber_$i",
  854. $locale->text( 'Number missing in Row [_1]', $i ) );
  855. }
  856. }
  857. sub purchase_order {
  858. $form->{title} = $locale->text('Add Purchase Order');
  859. $form->{vc} = 'vendor';
  860. $form->{type} = 'purchase_order';
  861. $buysell = 'sell';
  862. &create_form;
  863. }
  864. sub sales_order {
  865. $form->{title} = $locale->text('Add Sales Order');
  866. $form->{vc} = 'customer';
  867. $form->{type} = 'sales_order';
  868. $buysell = 'buy';
  869. &create_form;
  870. }
  871. sub rfq {
  872. $form->{title} = $locale->text('Add Request for Quotation');
  873. $form->{vc} = 'vendor';
  874. $form->{type} = 'request_quotation';
  875. $buysell = 'sell';
  876. &create_form;
  877. }
  878. sub quotation {
  879. $form->{title} = $locale->text('Add Quotation');
  880. $form->{vc} = 'customer';
  881. $form->{type} = 'sales_quotation';
  882. $buysell = 'buy';
  883. &create_form;
  884. }
  885. sub create_form {
  886. for (qw(id printed emailed queued)) { delete $form->{$_} }
  887. $form->{script} = 'oe.pl';
  888. $form->{shipto} = 1;
  889. $form->{rowcount}-- if $form->{rowcount};
  890. $form->{rowcount} = 0 if !$form->{"$form->{vc}_id"};
  891. do "bin/$form->{script}";
  892. for ( "$form->{vc}", "currency" ) { $form->{"select$_"} = "" }
  893. for (
  894. qw(currency employee department intnotes notes language_code taxincluded)
  895. )
  896. {
  897. $temp{$_} = $form->{$_};
  898. }
  899. &order_links;
  900. for ( keys %temp ) { $form->{$_} = $temp{$_} if $temp{$_} }
  901. $form->{exchangerate} = "";
  902. $form->{forex} = "";
  903. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  904. $form->{exchangerate} = $exchangerate
  905. if (
  906. $form->{forex} = (
  907. $exchangerate = $form->check_exchangerate(
  908. \%myconfig, $form->{currency},
  909. $form->{transdate}, $buysell
  910. )
  911. )
  912. );
  913. }
  914. &prepare_order;
  915. &display_form;
  916. }
  917. sub e_mail {
  918. $bcc = qq|<input type="hidden" name="bcc" value="$form->{bcc}">|;
  919. if ( $myconfig{role} =~ /(admin|manager)/ ) {
  920. $bcc = qq|
  921. <th align="right" nowrap="true">| . $locale->text('Bcc') . qq|</th>
  922. <td><input name="bcc" size="30" value="$form->{bcc}"></td>
  923. |;
  924. }
  925. if ( $form->{formname} =~ /(pick|packing|bin)_list/ ) {
  926. $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
  927. }
  928. $name = $form->{ $form->{vc} };
  929. $name =~ s/--.*//g;
  930. $title = $locale->text('E-mail') . " $name";
  931. $form->header;
  932. print qq|
  933. <body>
  934. <form method="post" action="$form->{script}">
  935. <table width="100%">
  936. <tr class="listtop">
  937. <th class="listtop">$title</th>
  938. </tr>
  939. <tr height="5"></tr>
  940. <tr>
  941. <td>
  942. <table width="100%">
  943. <tr>
  944. <th align="right" nowrap>| . $locale->text('E-mail') . qq|</th>
  945. <td><input name="email" size="30" value="$form->{email}"></td>
  946. <th align="right" nowrap>| . $locale->text('Cc') . qq|</th>
  947. <td><input name="cc" size="30" value="$form->{cc}"></td>
  948. </tr>
  949. <tr>
  950. <th align="right" nowrap>| . $locale->text('Subject') . qq|</th>
  951. <td><input name="subject" size="30" value="$form->{subject}"></td>
  952. $bcc
  953. </tr>
  954. </table>
  955. </td>
  956. </tr>
  957. <tr>
  958. <td>
  959. <table width="100%">
  960. <tr>
  961. <th align="left" nowrap>| . $locale->text('Message') . qq|</th>
  962. </tr>
  963. <tr>
  964. <td><textarea name="message" rows="15" cols="60" wrap="soft">$form->{message}</textarea></td>
  965. </tr>
  966. </table>
  967. </td>
  968. </tr>
  969. <tr>
  970. <td>
  971. |;
  972. $form->{oldmedia} = $form->{media};
  973. $form->{media} = "email";
  974. $form->{format} = "pdf";
  975. &print_options;
  976. for (
  977. qw(email cc bcc subject message formname sendmode format language_code action nextsub)
  978. )
  979. {
  980. delete $form->{$_};
  981. }
  982. $form->hide_form;
  983. print qq|
  984. </td>
  985. </tr>
  986. <tr>
  987. <td><hr size="3" noshade></td>
  988. </tr>
  989. </table>
  990. <input type="hidden" name="nextsub" value="send_email">
  991. <br>
  992. <button name="action" class="submit" type="submit" value="continue">|
  993. . $locale->text('Continue')
  994. . qq|</button>
  995. </form>
  996. </body>
  997. </html>
  998. |;
  999. }
  1000. sub send_email {
  1001. $old_form = new Form;
  1002. for ( keys %$form ) { $old_form->{$_} = $form->{$_} }
  1003. $old_form->{media} = $old_form->{oldmedia};
  1004. &print_form($old_form);
  1005. }
  1006. sub print_options {
  1007. $form->{sendmode} = "attachment";
  1008. $form->{copies} = 1 unless $form->{copies};
  1009. $form->{SM}{ $form->{sendmode} } = "selected";
  1010. if ( $form->{selectlanguage} ) {
  1011. $form->{"selectlanguage"} =
  1012. $form->unescape( $form->{"selectlanguage"} );
  1013. $form->{"selectlanguage"} =~ s/ selected//;
  1014. $form->{"selectlanguage"} =~
  1015. s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  1016. $lang = qq|<select name="language_code">$form->{selectlanguage}</select>
  1017. <input type="hidden" name="oldlanguage_code" value="$form->{oldlanguage_code}">
  1018. <input type="hidden" name="selectlanguage" value="|
  1019. . $form->escape( $form->{selectlanguage}, 1 ) . qq|">|;
  1020. }
  1021. $form->{selectformname} = $form->unescape( $form->{selectformname} );
  1022. $form->{selectformname} =~ s/ selected//;
  1023. $form->{selectformname} =~
  1024. s/(<option value="\Q$form->{formname}\E")/$1 selected/;
  1025. $type = qq|<select name="formname">$form->{selectformname}</select>
  1026. <input type="hidden" name="selectformname" value="|
  1027. . $form->escape( $form->{selectformname}, 1 ) . qq|">|;
  1028. if ( $form->{media} eq 'email' ) {
  1029. $media = qq|<select name="sendmode">
  1030. <option value="attachment" $form->{SM}{attachment}>|
  1031. . $locale->text('Attachment') . qq|
  1032. <option value="inline" $form->{SM}{inline}>|
  1033. . $locale->text('In-line')
  1034. . qq|</select>|;
  1035. }
  1036. else {
  1037. $media = qq|<select name="media">
  1038. <option value="screen">| . $locale->text('Screen');
  1039. if ( %{LedgerSMB::Sysconfig::printer}
  1040. && ${LedgerSMB::Sysconfig::latex} )
  1041. {
  1042. for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  1043. $media .= qq|
  1044. <option value="$_">$_|;
  1045. }
  1046. }
  1047. if ( ${LedgerSMB::Sysconfig::latex} ) {
  1048. $media .= qq|
  1049. <option value="queue">| . $locale->text('Queue');
  1050. }
  1051. $media .= qq|</select>|;
  1052. # set option selected
  1053. $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
  1054. }
  1055. $form->{selectformat} = qq|<option value="html">html\n|;
  1056. # <option value="txt">|.$locale->text('Text');
  1057. if ( ${LedgerSMB::Sysconfig::latex} ) {
  1058. $form->{selectformat} .= qq|
  1059. <option value="postscript">| . $locale->text('Postscript') . qq|
  1060. <option value="pdf">| . $locale->text('PDF');
  1061. }
  1062. $format = qq|<select name="format">$form->{selectformat}</select>|;
  1063. $format =~ s/(<option value="\Q$form->{format}\E")/$1 selected/;
  1064. $format .= qq|
  1065. <input type="hidden" name="selectformat" value="|
  1066. . $form->escape( $form->{selectformat}, 1 ) . qq|">|;
  1067. print qq|
  1068. <table width="100%">
  1069. <tr>
  1070. <td>$type</td>
  1071. <td>$lang</td>
  1072. <td>$format</td>
  1073. <td>$media</td>
  1074. |;
  1075. if ( %{LedgerSMB::Sysconfig::printer}
  1076. && ${LedgerSMB::Sysconfig::latex}
  1077. && $form->{media} ne 'email' )
  1078. {
  1079. print qq|
  1080. <td nowrap>| . $locale->text('Copies') . qq|
  1081. <input name="copies" size="2" value="$form->{copies}"></td>
  1082. |;
  1083. }
  1084. # $locale->text('Printed')
  1085. # $locale->text('E-mailed')
  1086. # $locale->text('Queued')
  1087. # $locale->text('Scheduled')
  1088. %status = (
  1089. printed => 'Printed',
  1090. emailed => 'E-mailed',
  1091. queued => 'Queued',
  1092. recurring => 'Scheduled'
  1093. );
  1094. print qq|<td align="right" width="90%">|;
  1095. for (qw(printed emailed queued recurring)) {
  1096. if ( $form->{$_} =~ /$form->{formname}/ ) {
  1097. print $locale->text( $status{$_} ) . qq|<br>|;
  1098. }
  1099. }
  1100. print qq|
  1101. </td>
  1102. </tr>
  1103. |;
  1104. $form->{groupprojectnumber} = "checked" if $form->{groupprojectnumber};
  1105. $form->{grouppartsgroup} = "checked" if $form->{grouppartsgroup};
  1106. for (qw(runningnumber partnumber description bin)) {
  1107. $sortby{$_} = "checked" if $form->{sortby} eq $_;
  1108. }
  1109. print qq|
  1110. <tr>
  1111. <td colspan="6">| . $locale->text('Group by') . qq| ->
  1112. <input name="groupprojectnumber" type="checkbox" class="checkbox" $form->{groupprojectnumber}>
  1113. | . $locale->text('Project') . qq|
  1114. <input name="grouppartsgroup" type="checkbox" class="checkbox" $form->{grouppartsgroup}>
  1115. | . $locale->text('Group') . qq|
  1116. </td>
  1117. </tr>
  1118. <tr>
  1119. <td colspan="6">| . $locale->text('Sort by') . qq| ->
  1120. <input name="sortby" type="radio" class="radio" value="runningnumber" $sortby{runningnumber}>
  1121. | . $locale->text('Item') . qq|
  1122. <input name="sortby" type="radio" class="radio" value="partnumber" $sortby{partnumber}>
  1123. | . $locale->text('Number') . qq|
  1124. <input name="sortby" type="radio" class="radio" value="description" $sortby{description}>
  1125. | . $locale->text('Description') . qq|
  1126. <input name="sortby" type="radio" class="radio" value="bin" $sortby{bin}>
  1127. | . $locale->text('Bin') . qq|
  1128. </td>
  1129. </tr>
  1130. </table>
  1131. |;
  1132. }
  1133. sub print {
  1134. # if this goes to the printer pass through
  1135. if ( $form->{media} !~ /(screen|email)/ ) {
  1136. $form->error( $locale->text('Select txt, postscript or PDF!') )
  1137. if ( $form->{format} !~ /(txt|postscript|pdf)/ );
  1138. $old_form = new Form;
  1139. for ( keys %$form ) { $old_form->{$_} = $form->{$_} }
  1140. }
  1141. &print_form($old_form);
  1142. }
  1143. sub print_form {
  1144. my ($old_form) = @_;
  1145. $inv = "inv";
  1146. $due = "due";
  1147. $numberfld = "sinumber";
  1148. $display_form =
  1149. ( $form->{display_form} ) ? $form->{display_form} : "display_form";
  1150. if ( $form->{formname} eq "invoice" ) {
  1151. $form->{label} = $locale->text('Invoice');
  1152. }
  1153. if ( $form->{formname} eq 'sales_order' ) {
  1154. $inv = "ord";
  1155. $due = "req";
  1156. $form->{label} = $locale->text('Sales Order');
  1157. $numberfld = "sonumber";
  1158. $order = 1;
  1159. }
  1160. if ( $form->{formname} eq 'work_order' ) {
  1161. $inv = "ord";
  1162. $due = "req";
  1163. $form->{label} = $locale->text('Work Order');
  1164. $numberfld = "sonumber";
  1165. $order = 1;
  1166. }
  1167. if ( $form->{formname} eq 'packing_list' ) {
  1168. # we use the same packing list as from an invoice
  1169. $form->{label} = $locale->text('Packing List');
  1170. if ( $form->{type} ne 'invoice' ) {
  1171. $inv = "ord";
  1172. $due = "req";
  1173. $numberfld = "sonumber";
  1174. $order = 1;
  1175. $filled = 0;
  1176. for ( $i = 1 ; $i < $form->{rowcount} ; $i++ ) {
  1177. if ( $form->{"ship_$i"} ) {
  1178. $filled = 1;
  1179. last;
  1180. }
  1181. }
  1182. if ( !$filled ) {
  1183. for ( 1 .. $form->{rowcount} ) {
  1184. $form->{"ship_$_"} = $form->{"qty_$_"};
  1185. }
  1186. }
  1187. }
  1188. }
  1189. if ( $form->{formname} eq 'pick_list' ) {
  1190. $form->{label} = $locale->text('Pick List');
  1191. if ( $form->{type} ne 'invoice' ) {
  1192. $inv = "ord";
  1193. $due = "req";
  1194. $order = 1;
  1195. $numberfld = "sonumber";
  1196. }
  1197. }
  1198. if ( $form->{formname} eq 'purchase_order' ) {
  1199. $inv = "ord";
  1200. $due = "req";
  1201. $form->{label} = $locale->text('Purchase Order');
  1202. $numberfld = "ponumber";
  1203. $order = 1;
  1204. }
  1205. if ( $form->{formname} eq 'bin_list' ) {
  1206. $inv = "ord";
  1207. $due = "req";
  1208. $form->{label} = $locale->text('Bin List');
  1209. $numberfld = "ponumber";
  1210. $order = 1;
  1211. }
  1212. if ( $form->{formname} eq 'sales_quotation' ) {
  1213. $inv = "quo";
  1214. $due = "req";
  1215. $form->{label} = $locale->text('Quotation');
  1216. $numberfld = "sqnumber";
  1217. $order = 1;
  1218. }
  1219. if ( $form->{formname} eq 'request_quotation' ) {
  1220. $inv = "quo";
  1221. $due = "req";
  1222. $form->{label} = $locale->text('Quotation');
  1223. $numberfld = "rfqnumber";
  1224. $order = 1;
  1225. }
  1226. &validate_items;
  1227. $form->{"${inv}date"} = $form->{transdate};
  1228. $form->isblank( "email", $locale->text('E-mail address missing!') )
  1229. if ( $form->{media} eq 'email' );
  1230. $form->isblank( "${inv}date",
  1231. $locale->text( $form->{label} . ' Date missing!' ) );
  1232. # get next number
  1233. if ( !$form->{"${inv}number"} ) {
  1234. $form->{"${inv}number"} =
  1235. $form->update_defaults( \%myconfig, $numberfld );
  1236. if ( $form->{media} eq 'screen' ) {
  1237. &update;
  1238. exit;
  1239. }
  1240. }
  1241. # $locale->text('Invoice Number missing!')
  1242. # $locale->text('Invoice Date missing!')
  1243. # $locale->text('Packing List Number missing!')
  1244. # $locale->text('Packing List Date missing!')
  1245. # $locale->text('Order Number missing!')
  1246. # $locale->text('Order Date missing!')
  1247. # $locale->text('Quotation Number missing!')
  1248. # $locale->text('Quotation Date missing!')
  1249. &{"$form->{vc}_details"};
  1250. my @vars = ();
  1251. foreach $i ( 1 .. $form->{rowcount} ) {
  1252. push @vars,
  1253. (
  1254. "partnumber_$i", "description_$i",
  1255. "projectnumber_$i", "partsgroup_$i",
  1256. "serialnumber_$i", "bin_$i",
  1257. "unit_$i", "notes_$i"
  1258. );
  1259. }
  1260. for ( split / /, $form->{taxaccounts} ) { push @vars, "${_}_description" }
  1261. $ARAP = ( $form->{vc} eq 'customer' ) ? "AR" : "AP";
  1262. push @vars, $ARAP;
  1263. # format payment dates
  1264. for $i ( 1 .. $form->{paidaccounts} - 1 ) {
  1265. if ( exists $form->{longformat} ) {
  1266. $form->{"datepaid_$i"} =
  1267. $locale->date( \%myconfig, $form->{"datepaid_$i"},
  1268. $form->{longformat} );
  1269. }
  1270. push @vars, "${ARAP}_paid_$i", "source_$i", "memo_$i";
  1271. }
  1272. $form->format_string(@vars);
  1273. ( $form->{employee} ) = split /--/, $form->{employee};
  1274. ( $form->{warehouse}, $form->{warehouse_id} ) = split /--/,
  1275. $form->{warehouse};
  1276. # this is a label for the subtotals
  1277. $form->{groupsubtotaldescription} = $locale->text('Subtotal')
  1278. if not exists $form->{groupsubtotaldescription};
  1279. delete $form->{groupsubtotaldescription} if $form->{deletegroupsubtotal};
  1280. $duedate = $form->{"${due}date"};
  1281. # create the form variables
  1282. if ($order) {
  1283. OE->order_details( \%myconfig, $form );
  1284. }
  1285. else {
  1286. IS->invoice_details( \%myconfig, $form );
  1287. }
  1288. if ( exists $form->{longformat} ) {
  1289. $form->{"${due}date"} = $duedate;
  1290. for ( "${inv}date", "${due}date", "shippingdate", "transdate" ) {
  1291. $form->{$_} =
  1292. $locale->date( \%myconfig, $form->{$_}, $form->{longformat} );
  1293. }
  1294. }
  1295. @vars =
  1296. qw(name address1 address2 city state zipcode country contact phone fax email);
  1297. $shipto = 1;
  1298. # if there is no shipto fill it in from billto
  1299. foreach $item (@vars) {
  1300. if ( $form->{"shipto$item"} ) {
  1301. $shipto = 0;
  1302. last;
  1303. }
  1304. }
  1305. if ($shipto) {
  1306. if ( $form->{formname} eq 'purchase_order'
  1307. || $form->{formname} eq 'request_quotation' )
  1308. {
  1309. $form->{shiptoname} = $myconfig{company};
  1310. $form->{shiptoaddress1} = $myconfig{address};
  1311. $form->{shiptoaddress1} =~ s/\\n/\n/g;
  1312. }
  1313. else {
  1314. if ( $form->{formname} !~ /bin_list/ ) {
  1315. for (@vars) { $form->{"shipto$_"} = $form->{$_} }
  1316. }
  1317. }
  1318. }
  1319. # some of the stuff could have umlauts so we translate them
  1320. push @vars,
  1321. qw(contact shiptoname shiptoaddress1 shiptoaddress2 shiptocity shiptostate shiptozipcode shiptocountry shiptocontact shiptoemail shippingpoint shipvia notes intnotes employee warehouse);
  1322. push @vars, ( "${inv}number", "${inv}date", "${due}date" );
  1323. for (qw(company address tel fax businessnumber)) {
  1324. $form->{$_} = $myconfig{$_};
  1325. }
  1326. $form->{address} =~ s/\\n/\n/g;
  1327. for (qw(name email)) { $form->{"user$_"} = $myconfig{$_} }
  1328. push @vars, qw(company address tel fax businessnumber username useremail);
  1329. for (qw(notes intnotes)) { $form->{$_} =~ s/^\s+//g }
  1330. # before we format replace <%var%>
  1331. for (qw(notes intnotes message)) {
  1332. $form->{$_} =~ s/<%(.*?)%>/$form->{$1}/g;
  1333. }
  1334. $form->format_string(@vars);
  1335. $form->{templates} = "$myconfig{templates}";
  1336. $form->{IN} = "$form->{formname}.$form->{format}";
  1337. if ( $form->{format} =~ /(postscript|pdf)/ ) {
  1338. $form->{IN} =~ s/$&$/tex/;
  1339. }
  1340. $form->{pre} = '<body bgcolor="#ffffff">\n<pre>' if $form->{format} eq 'txt';
  1341. if ( $form->{media} !~ /(screen|queue|email)/ ) { # printer
  1342. $form->{OUT} = ${LedgerSMB::Sysconfig::printer}{ $form->{media} };
  1343. $form->{printmode} = '|-';
  1344. $form->{OUT} =~ s/<%(fax)%>/<%$form->{vc}$1%>/;
  1345. $form->{OUT} =~ s/<%(.*?)%>/$form->{$1}/g;
  1346. if ( $form->{printed} !~ /$form->{formname}/ ) {
  1347. $form->{printed} .= " $form->{formname}";
  1348. $form->{printed} =~ s/^ //;
  1349. $form->update_status( \%myconfig );
  1350. }
  1351. $old_form->{printed} = $form->{printed} if defined %$old_form;
  1352. %audittrail = (
  1353. tablename => ($order) ? 'oe' : lc $ARAP,
  1354. reference => $form->{"${inv}number"},
  1355. formname => $form->{formname},
  1356. action => 'printed',
  1357. id => $form->{id}
  1358. );
  1359. $old_form->{audittrail} .=
  1360. $form->audittrail( "", \%myconfig, \%audittrail )
  1361. if defined %$old_form;
  1362. } elsif ( $form->{media} eq 'email' ) {
  1363. $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
  1364. unless $form->{subject};
  1365. $form->{plainpaper} = 1;
  1366. $form->{OUT} = "${LedgerSMB::Sysconfig::sendmail}";
  1367. $form->{printmode} = '|-';
  1368. if ( $form->{emailed} !~ /$form->{formname}/ ) {
  1369. $form->{emailed} .= " $form->{formname}";
  1370. $form->{emailed} =~ s/^ //;
  1371. # save status
  1372. $form->update_status( \%myconfig );
  1373. }
  1374. $now = scalar localtime;
  1375. $cc = $locale->text( 'Cc: [_1]', $form->{cc} ) . qq|\n| if $form->{cc};
  1376. $bcc = $locale->text( 'Bcc: [_1]', $form->{bcc} ) . qq|\n|
  1377. if $form->{bcc};
  1378. if ( defined %$old_form ) {
  1379. $old_form->{intnotes} = qq|$old_form->{intnotes}\n\n|
  1380. if $old_form->{intnotes};
  1381. $old_form->{intnotes} .=
  1382. qq|[email]\n|
  1383. . $locale->text( 'Date: [_1]', $now ) . qq|\n|
  1384. . $locale->text( 'To: [_1]', $form->{email} )
  1385. . qq|\n${cc}${bcc}|
  1386. . $locale->text( 'Subject: [_1]', $form->{subject} ) . qq|\n|;
  1387. $old_form->{intnotes} .= qq|\n| . $locale->text('Message') . qq|: |;
  1388. $old_form->{intnotes} .=
  1389. ( $form->{message} ) ? $form->{message} : $locale->text('sent');
  1390. $old_form->{message} = $form->{message};
  1391. $old_form->{emailed} = $form->{emailed};
  1392. $old_form->{format} = "postscript" if $myconfig{printer};
  1393. $old_form->{media} = $myconfig{printer};
  1394. $old_form->save_intnotes( \%myconfig, ($order) ? 'oe' : lc $ARAP );
  1395. }
  1396. %audittrail = (
  1397. tablename => ($order) ? 'oe' : lc $ARAP,
  1398. reference => $form->{"${inv}number"},
  1399. formname => $form->{formname},
  1400. action => 'emailed',
  1401. id => $form->{id}
  1402. );
  1403. $old_form->{audittrail} .=
  1404. $form->audittrail( "", \%myconfig, \%audittrail )
  1405. if defined %$old_form;
  1406. } elsif ( $form->{media} eq 'queue' ) {
  1407. %queued = split / /, $form->{queued};
  1408. if ( $filename = $queued{ $form->{formname} } ) {
  1409. $form->{queued} =~ s/$form->{formname} $filename//;
  1410. unlink "${LedgerSMB::Sysconfig::spool}/$filename";
  1411. $filename =~ s/\..*$//g;
  1412. }
  1413. else {
  1414. $filename = time;
  1415. $filename .= $$;
  1416. }
  1417. $filename .= ( $form->{format} eq 'postscript' ) ? '.ps' : '.pdf';
  1418. $form->{OUT} = "${LedgerSMB::Sysconfig::spool}/$filename";
  1419. $form->{printmode} = '>';
  1420. $form->{queued} .= " $form->{formname} $filename";
  1421. $form->{queued} =~ s/^ //;
  1422. # save status
  1423. $form->update_status( \%myconfig );
  1424. $old_form->{queued} = $form->{queued};
  1425. %audittrail = (
  1426. tablename => ($order) ? 'oe' : lc $ARAP,
  1427. reference => $form->{"${inv}number"},
  1428. formname => $form->{formname},
  1429. action => 'queued',
  1430. id => $form->{id}
  1431. );
  1432. $old_form->{audittrail} .=
  1433. $form->audittrail( "", \%myconfig, \%audittrail );
  1434. }
  1435. $form->format_string( "email", "cc", "bcc" );
  1436. $form->{fileid} = $form->{"${inv}number"};
  1437. $form->{fileid} =~ s/(\s|\W)+//g;
  1438. $form->parse_template( \%myconfig, ${LedgerSMB::Sysconfig::userspath} );
  1439. # if we got back here restore the previous form
  1440. if ( defined %$old_form ) {
  1441. $old_form->{"${inv}number"} = $form->{"${inv}number"};
  1442. # restore and display form
  1443. for ( keys %$old_form ) { $form->{$_} = $old_form->{$_} }
  1444. delete $form->{pre};
  1445. $form->{rowcount}--;
  1446. for (qw(exchangerate creditlimit creditremaining)) {
  1447. $form->{$_} = $form->parse_amount( \%myconfig, $form->{$_} );
  1448. }
  1449. for $i ( 1 .. $form->{paidaccounts} ) {
  1450. for (qw(paid exchangerate)) {
  1451. $form->{"${_}_$i"} =
  1452. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  1453. }
  1454. }
  1455. &{"$display_form"};
  1456. }
  1457. }
  1458. sub customer_details {
  1459. IS->customer_details( \%myconfig, \%$form );
  1460. }
  1461. sub vendor_details {
  1462. IR->vendor_details( \%myconfig, \%$form );
  1463. }
  1464. sub ship_to {
  1465. $title = $form->{title};
  1466. $form->{title} = $locale->text('Ship to');
  1467. for (qw(exchangerate creditlimit creditremaining)) {
  1468. $form->{$_} = $form->parse_amount( \%myconfig, $form->{$_} );
  1469. }
  1470. for ( 1 .. $form->{paidaccounts} ) {
  1471. $form->{"paid_$_"} =
  1472. $form->parse_amount( \%myconfig, $form->{"paid_$_"} );
  1473. }
  1474. # get details for name
  1475. &{"$form->{vc}_details"};
  1476. $number =
  1477. ( $form->{vc} eq 'customer' )
  1478. ? $locale->text('Customer Number')
  1479. : $locale->text('Vendor Number');
  1480. $nextsub =
  1481. ( $form->{display_form} ) ? $form->{display_form} : "display_form";
  1482. $form->{rowcount}--;
  1483. $form->header;
  1484. print qq|
  1485. <body>
  1486. <form method="post" action="$form->{script}">
  1487. <table width="100%">
  1488. <tr>
  1489. <td>
  1490. <table>
  1491. <tr class="listheading">
  1492. <th class="listheading" colspan="2" width="50%">|
  1493. . $locale->text('Billing Address')
  1494. . qq|</th>
  1495. <th class="listheading" width="50%">|
  1496. . $locale->text('Shipping Address')
  1497. . qq|</th>
  1498. </tr>
  1499. <tr height="5"></tr>
  1500. <tr>
  1501. <th align="right" nowrap>$number</th>
  1502. <td>$form->{"$form->{vc}number"}</td>
  1503. </tr>
  1504. <tr>
  1505. <th align="right" nowrap>| . $locale->text('Company Name') . qq|</th>
  1506. <td>$form->{name}</td>
  1507. <td><input name="shiptoname" size="35" maxlength="64" value="$form->{shiptoname}"></td>
  1508. </tr>
  1509. <tr>
  1510. <th align="right" nowrap>| . $locale->text('Address') . qq|</th>
  1511. <td>$form->{address1}</td>
  1512. <td><input name="shiptoaddress1" size="35" maxlength="32" value="$form->{shiptoaddress1}"></td>
  1513. </tr>
  1514. <tr>
  1515. <th></th>
  1516. <td>$form->{address2}</td>
  1517. <td><input name="shiptoaddress2" size="35" maxlength="32" value="$form->{shiptoaddress2}"></td>
  1518. </tr>
  1519. <tr>
  1520. <th align="right" nowrap>| . $locale->text('City') . qq|</th>
  1521. <td>$form->{city}</td>
  1522. <td><input name="shiptocity" size="35" maxlength="32" value="$form->{shiptocity}"></td>
  1523. </tr>
  1524. <tr>
  1525. <th align="right" nowrap>| . $locale->text('State/Province') . qq|</th>
  1526. <td>$form->{state}</td>
  1527. <td><input name="shiptostate" size="35" maxlength="32" value="$form->{shiptostate}"></td>
  1528. </tr>
  1529. <tr>
  1530. <th align="right" nowrap>| . $locale->text('Zip/Postal Code') . qq|</th>
  1531. <td>$form->{zipcode}</td>
  1532. <td><input name="shiptozipcode" size="10" maxlength="10" value="$form->{shiptozipcode}"></td>
  1533. </tr>
  1534. <tr>
  1535. <th align="right" nowrap>| . $locale->text('Country') . qq|</th>
  1536. <td>$form->{country}</td>
  1537. <td><input name="shiptocountry" size="35" maxlength="32" value="$form->{shiptocountry}"></td>
  1538. </tr>
  1539. <tr>
  1540. <th align="right" nowrap>| . $locale->text('Contact') . qq|</th>
  1541. <td>$form->{contact}</td>
  1542. <td><input name="shiptocontact" size="35" maxlength="64" value="$form->{shiptocontact}"></td>
  1543. </tr>
  1544. <tr>
  1545. <th align="right" nowrap>| . $locale->text('Phone') . qq|</th>
  1546. <td>$form->{"$form->{vc}phone"}</td>
  1547. <td><input name="shiptophone" size="20" value="$form->{shiptophone}"></td>
  1548. </tr>
  1549. <tr>
  1550. <th align="right" nowrap>| . $locale->text('Fax') . qq|</th>
  1551. <td>$form->{"$form->{vc}fax"}</td>
  1552. <td><input name="shiptofax" size="20" value="$form->{shiptofax}"></td>
  1553. </tr>
  1554. <tr>
  1555. <th align="right" nowrap>| . $locale->text('E-mail') . qq|</th>
  1556. <td>$form->{email}</td>
  1557. <td><input name="shiptoemail" size="35" value="$form->{shiptoemail}"></td>
  1558. </tr>
  1559. </table>
  1560. </td>
  1561. </tr>
  1562. </table>
  1563. <input type="hidden" name="nextsub" value="$nextsub">
  1564. |;
  1565. # delete shipto
  1566. for (qw(action nextsub)) { delete $form->{$_} }
  1567. for (
  1568. qw(name address1 address2 city state zipcode country contact phone fax email)
  1569. )
  1570. {
  1571. delete $form->{"shipto$_"};
  1572. }
  1573. $form->{title} = $title;
  1574. $form->hide_form;
  1575. print qq|
  1576. <hr size="3" noshade>
  1577. <br>
  1578. <button class="submit" type="submit" name="action" value="continue">|
  1579. . $locale->text('Continue')
  1580. . qq|</button>
  1581. </form>
  1582. </body>
  1583. </html>
  1584. |;
  1585. }