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