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