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