summaryrefslogtreecommitdiff
path: root/bin/io.pl
blob: 6bacd80d22aab50bf11c00dc9b7832364579ccca (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. my %hiddens;
  921. if ( $myconfig{role} !~ /(admin|manager)/ ) {
  922. $hiddens{bcc} = $form->{bcc};
  923. }
  924. if ( $form->{formname} =~ /(pick|packing|bin)_list/ ) {
  925. $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
  926. }
  927. $form->{oldmedia} = $form->{media};
  928. $form->{media} = "email";
  929. $form->{format} = "pdf";
  930. my $print_options = &print_options(\%hiddens);
  931. for (
  932. qw(email cc bcc subject message formname sendmode format language_code action nextsub)
  933. )
  934. {
  935. delete $form->{$_};
  936. }
  937. $hiddens{$_} = $form->{$_} for keys %$form;
  938. $hiddens{nextsub} = 'send_email';
  939. my @buttons = ({
  940. name => 'action',
  941. value => 'send_email',
  942. text => $locale->text('Continue'),
  943. });
  944. my $template = LedgerSMB::Template->new_UI(
  945. user => \%myconfig,
  946. locale => $locale,
  947. template => 'io-email',
  948. );
  949. $template->render({
  950. form => $form,
  951. print => $print_options,
  952. hiddens => \%hiddens,
  953. buttons => \@buttons,
  954. });
  955. }
  956. sub send_email {
  957. $old_form = new Form;
  958. for ( keys %$form ) { $old_form->{$_} = $form->{$_} }
  959. $old_form->{media} = $old_form->{oldmedia};
  960. &print_form($old_form);
  961. }
  962. sub print_options {
  963. my $hiddens = shift;
  964. my %options;
  965. $form->{sendmode} = "attachment";
  966. $form->{copies} = 1 unless $form->{copies};
  967. $form->{SM}{ $form->{sendmode} } = "selected";
  968. delete $form->{all_language};
  969. $form->all_languages;
  970. if ( ref $form->{all_language} eq 'ARRAY') {
  971. $options{lang} = {
  972. name => 'language_code',
  973. default_values => $form->{oldlanguage_code},
  974. options => [{text => ' ', value => ''}],
  975. };
  976. for my $lang (@{$form->{all_language}}) {
  977. push @{$options{lang}{options}}, {
  978. text => $lang->{description},
  979. value => $lang->{code},
  980. };
  981. }
  982. $hiddens->{oldlanguage_code} = $form->{oldlanguage_code};
  983. }
  984. $options{formname} = {
  985. name => 'formname',
  986. default_values => $form->{formname},
  987. options => [],
  988. };
  989. # SC: Option values extracted from other bin/ scripts
  990. if ($form->{type} eq 'sales_quotation') {
  991. push @{$options{formname}{options}}, {
  992. text => $locale->text('Quotation'),
  993. value => 'sales_quotation',
  994. };
  995. } elsif ($form->{type} eq 'request_quotation') {
  996. push @{$options{formname}{options}}, {
  997. text => $locale->text('RFQ'),
  998. value => 'request_quotation',
  999. };
  1000. } elsif ($form->{type} eq 'sales_order') {
  1001. push @{$options{formname}{options}}, {
  1002. text => $locale->text('Sales Order'),
  1003. value => 'sales_order',
  1004. };
  1005. push @{$options{formname}{options}}, {
  1006. text => $locale->text('Work Order'),
  1007. value => 'work_order',
  1008. };
  1009. push @{$options{formname}{options}}, {
  1010. text => $locale->text('Pick List'),
  1011. value => 'pick_list',
  1012. };
  1013. push @{$options{formname}{options}}, {
  1014. text => $locale->text('Packing List'),
  1015. value => 'packing_list',
  1016. };
  1017. } elsif ($form->{type} eq 'purchase_order') {
  1018. push @{$options{formname}{options}}, {
  1019. text => $locale->text('Purchase Order'),
  1020. value => 'purchase_order',
  1021. };
  1022. push @{$options{formname}{options}}, {
  1023. text => $locale->text('Bin List'),
  1024. value => 'bin_list',
  1025. };
  1026. } elsif ($form->{type} eq 'ship_order') {
  1027. push @{$options{formname}{options}}, {
  1028. text => $locale->text('Pick List'),
  1029. value => 'pick_list',
  1030. };
  1031. push @{$options{formname}{options}}, {
  1032. text => $locale->text('Packing List'),
  1033. value => 'packing_list',
  1034. };
  1035. } elsif ($form->{type} eq 'receive_order') {
  1036. push @{$options{formname}{options}}, {
  1037. text => $locale->text('Bin List'),
  1038. value => 'bin_list',
  1039. };
  1040. }
  1041. if ( $form->{media} eq 'email' ) {
  1042. $options{media} = {
  1043. name => 'sendmode',
  1044. options => [{
  1045. text => $locale->text('Attachment'),
  1046. value => 'attachment'}, {
  1047. text => $locale->text('In-line'),
  1048. value => 'inline'}
  1049. ]};
  1050. $options{media}{default_values} = 'attachment' if $form->{SM}{attachment};
  1051. $options{media}{default_values} = 'inline' if $form->{SM}{inline};
  1052. } else {
  1053. $options{media} = {
  1054. name => 'media',
  1055. default_values => $form->{media},
  1056. options => [{
  1057. text => $locale->text('Screen'),
  1058. value => 'screen'}
  1059. ]};
  1060. if ( %{LedgerSMB::Sysconfig::printer}
  1061. && ${LedgerSMB::Sysconfig::latex} )
  1062. {
  1063. for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  1064. push @{$options{media}{options}}, {text => $_, value => $_};
  1065. }
  1066. }
  1067. if ( ${LedgerSMB::Sysconfig::latex} )
  1068. {
  1069. push @{$options{media}{options}}, {text => $locale->text('Queue'),
  1070. value => 'queue'};
  1071. }
  1072. }
  1073. $options{format} = {
  1074. name => 'format',
  1075. default_values => $form->{selectformat},
  1076. options => [{text => 'HTML', value => 'html'}],
  1077. };
  1078. if ( ${LedgerSMB::Sysconfig::latex} ) {
  1079. push @{$options{format}{options}}, {
  1080. text => $locale->text('Postscript'),
  1081. value => 'postscript',
  1082. };
  1083. push @{$options{format}{options}}, {
  1084. text => $locale->text('PDF'),
  1085. value => 'pdf',
  1086. };
  1087. }
  1088. if ( %{LedgerSMB::Sysconfig::printer}
  1089. && ${LedgerSMB::Sysconfig::latex}
  1090. && $form->{media} ne 'email' )
  1091. {
  1092. $options{copies} = 1;
  1093. }
  1094. # $locale->text('Printed')
  1095. # $locale->text('E-mailed')
  1096. # $locale->text('Queued')
  1097. # $locale->text('Scheduled')
  1098. $options{status} = (
  1099. printed => 'Printed',
  1100. emailed => 'E-mailed',
  1101. queued => 'Queued',
  1102. recurring => 'Scheduled'
  1103. );
  1104. $options{groupby} = {};
  1105. $options{groupby}{groupprojectnumber} = "checked" if $form->{groupprojectnumber};
  1106. $options{groupby}{grouppartsgroup} = "checked" if $form->{grouppartsgroup};
  1107. $options{sortby} = {};
  1108. for (qw(runningnumber partnumber description bin)) {
  1109. $options{sortby}{$_} = "checked" if $form->{sortby} eq $_;
  1110. }
  1111. \%options;
  1112. }
  1113. sub print {
  1114. # if this goes to the printer pass through
  1115. if ( $form->{media} !~ /(screen|email)/ ) {
  1116. $form->error( $locale->text('Select txt, postscript or PDF!') )
  1117. if ( $form->{format} !~ /(txt|postscript|pdf)/ );
  1118. $old_form = new Form;
  1119. for ( keys %$form ) { $old_form->{$_} = $form->{$_} }
  1120. }
  1121. &print_form($old_form);
  1122. }
  1123. sub print_form {
  1124. my ($old_form) = @_;
  1125. my $inv = "inv";
  1126. my $due = "due";
  1127. my $numberfld = "sinumber";
  1128. my $display_form =
  1129. ( $form->{display_form} ) ? $form->{display_form} : "display_form";
  1130. if ( $form->{formname} eq "invoice" ) {
  1131. $form->{label} = $locale->text('Invoice');
  1132. }
  1133. if ( $form->{formname} eq 'sales_order' ) {
  1134. $inv = "ord";
  1135. $due = "req";
  1136. $form->{label} = $locale->text('Sales Order');
  1137. $numberfld = "sonumber";
  1138. $order = 1;
  1139. }
  1140. if ( $form->{formname} eq 'work_order' ) {
  1141. $inv = "ord";
  1142. $due = "req";
  1143. $form->{label} = $locale->text('Work Order');
  1144. $numberfld = "sonumber";
  1145. $order = 1;
  1146. }
  1147. if ( $form->{formname} eq 'packing_list' ) {
  1148. # we use the same packing list as from an invoice
  1149. $form->{label} = $locale->text('Packing List');
  1150. if ( $form->{type} ne 'invoice' ) {
  1151. $inv = "ord";
  1152. $due = "req";
  1153. $numberfld = "sonumber";
  1154. $order = 1;
  1155. $filled = 0;
  1156. for ( $i = 1 ; $i < $form->{rowcount} ; $i++ ) {
  1157. if ( $form->{"ship_$i"} ) {
  1158. $filled = 1;
  1159. last;
  1160. }
  1161. }
  1162. if ( !$filled ) {
  1163. for ( 1 .. $form->{rowcount} ) {
  1164. $form->{"ship_$_"} = $form->{"qty_$_"};
  1165. }
  1166. }
  1167. }
  1168. }
  1169. if ( $form->{formname} eq 'pick_list' ) {
  1170. $form->{label} = $locale->text('Pick List');
  1171. if ( $form->{type} ne 'invoice' ) {
  1172. $inv = "ord";
  1173. $due = "req";
  1174. $order = 1;
  1175. $numberfld = "sonumber";
  1176. }
  1177. }
  1178. if ( $form->{formname} eq 'purchase_order' ) {
  1179. $inv = "ord";
  1180. $due = "req";
  1181. $form->{label} = $locale->text('Purchase Order');
  1182. $numberfld = "ponumber";
  1183. $order = 1;
  1184. }
  1185. if ( $form->{formname} eq 'bin_list' ) {
  1186. $inv = "ord";
  1187. $due = "req";
  1188. $form->{label} = $locale->text('Bin List');
  1189. $numberfld = "ponumber";
  1190. $order = 1;
  1191. }
  1192. if ( $form->{formname} eq 'sales_quotation' ) {
  1193. $inv = "quo";
  1194. $due = "req";
  1195. $form->{label} = $locale->text('Quotation');
  1196. $numberfld = "sqnumber";
  1197. $order = 1;
  1198. }
  1199. if ( $form->{formname} eq 'request_quotation' ) {
  1200. $inv = "quo";
  1201. $due = "req";
  1202. $form->{label} = $locale->text('Quotation');
  1203. $numberfld = "rfqnumber";
  1204. $order = 1;
  1205. }
  1206. &validate_items;
  1207. $form->{"${inv}date"} = $form->{transdate};
  1208. $form->isblank( "email", $locale->text('E-mail address missing!') )
  1209. if ( $form->{media} eq 'email' );
  1210. $form->isblank( "${inv}date",
  1211. $locale->text( $form->{label} . ' Date missing!' ) );
  1212. # get next number
  1213. if ( !$form->{"${inv}number"} ) {
  1214. $form->{"${inv}number"} =
  1215. $form->update_defaults( \%myconfig, $numberfld );
  1216. if ( $form->{media} eq 'screen' ) {
  1217. &update;
  1218. exit;
  1219. }
  1220. }
  1221. # $locale->text('Invoice Number missing!')
  1222. # $locale->text('Invoice Date missing!')
  1223. # $locale->text('Packing List Number missing!')
  1224. # $locale->text('Packing List Date missing!')
  1225. # $locale->text('Order Number missing!')
  1226. # $locale->text('Order Date missing!')
  1227. # $locale->text('Quotation Number missing!')
  1228. # $locale->text('Quotation Date missing!')
  1229. &{"$form->{vc}_details"};
  1230. my @vars = ();
  1231. foreach $i ( 1 .. $form->{rowcount} ) {
  1232. push @vars,
  1233. (
  1234. "partnumber_$i", "description_$i",
  1235. "projectnumber_$i", "partsgroup_$i",
  1236. "serialnumber_$i", "bin_$i",
  1237. "unit_$i", "notes_$i"
  1238. );
  1239. }
  1240. for ( split / /, $form->{taxaccounts} ) { push @vars, "${_}_description" }
  1241. $ARAP = ( $form->{vc} eq 'customer' ) ? "AR" : "AP";
  1242. push @vars, $ARAP;
  1243. # format payment dates
  1244. for my $i ( 1 .. $form->{paidaccounts} - 1 ) {
  1245. if ( exists $form->{longformat} ) {
  1246. $form->{"datepaid_$i"} =
  1247. $locale->date( \%myconfig, $form->{"datepaid_$i"},
  1248. $form->{longformat} );
  1249. }
  1250. push @vars, "${ARAP}_paid_$i", "source_$i", "memo_$i";
  1251. }
  1252. $form->format_string(@vars);
  1253. ( $form->{employee} ) = split /--/, $form->{employee};
  1254. ( $form->{warehouse}, $form->{warehouse_id} ) = split /--/,
  1255. $form->{warehouse};
  1256. # this is a label for the subtotals
  1257. $form->{groupsubtotaldescription} = $locale->text('Subtotal')
  1258. if not exists $form->{groupsubtotaldescription};
  1259. delete $form->{groupsubtotaldescription} if $form->{deletegroupsubtotal};
  1260. $duedate = $form->{"${due}date"};
  1261. # create the form variables
  1262. if ($order) {
  1263. OE->order_details( \%myconfig, $form );
  1264. }
  1265. else {
  1266. IS->invoice_details( \%myconfig, $form );
  1267. }
  1268. if ( exists $form->{longformat} ) {
  1269. $form->{"${due}date"} = $duedate;
  1270. for ( "${inv}date", "${due}date", "shippingdate", "transdate" ) {
  1271. $form->{$_} =
  1272. $locale->date( \%myconfig, $form->{$_}, $form->{longformat} );
  1273. }
  1274. }
  1275. @vars =
  1276. qw(name address1 address2 city state zipcode country contact phone fax email);
  1277. $shipto = 1;
  1278. # if there is no shipto fill it in from billto
  1279. foreach $item (@vars) {
  1280. if ( $form->{"shipto$item"} ) {
  1281. $shipto = 0;
  1282. last;
  1283. }
  1284. }
  1285. if ($shipto) {
  1286. if ( $form->{formname} eq 'purchase_order'
  1287. || $form->{formname} eq 'request_quotation' )
  1288. {
  1289. $form->{shiptoname} = $myconfig{company};
  1290. $form->{shiptoaddress1} = $myconfig{address};
  1291. $form->{shiptoaddress1} =~ s/\\n/\n/g;
  1292. }
  1293. else {
  1294. if ( $form->{formname} !~ /bin_list/ ) {
  1295. for (@vars) { $form->{"shipto$_"} = $form->{$_} }
  1296. }
  1297. }
  1298. }
  1299. # some of the stuff could have umlauts so we translate them
  1300. push @vars,
  1301. qw(contact shiptoname shiptoaddress1 shiptoaddress2 shiptocity shiptostate shiptozipcode shiptocountry shiptocontact shiptoemail shippingpoint shipvia notes intnotes employee warehouse);
  1302. push @vars, ( "${inv}number", "${inv}date", "${due}date" );
  1303. for (qw(company address tel fax businessnumber)) {
  1304. $form->{$_} = $myconfig{$_};
  1305. }
  1306. $form->{address} =~ s/\\n/\n/g;
  1307. for (qw(name email)) { $form->{"user$_"} = $myconfig{$_} }
  1308. push @vars, qw(company address tel fax businessnumber username useremail);
  1309. for (qw(notes intnotes)) { $form->{$_} =~ s/^\s+//g }
  1310. # before we format replace <%var%>
  1311. for (qw(notes intnotes message)) {
  1312. $form->{$_} =~ s/<%(.*?)%>/$form->{$1}/g;
  1313. }
  1314. $form->format_string(@vars);
  1315. $form->{templates} = "$myconfig{templates}";
  1316. $form->{IN} = "$form->{formname}.$form->{format}";
  1317. if ( $form->{format} =~ /(postscript|pdf)/ ) {
  1318. $form->{IN} =~ s/$&$/tex/;
  1319. }
  1320. $form->{pre} = "<body bgcolor=#ffffff>\n<pre>" if $form->{format} eq 'txt';
  1321. my %output_options;
  1322. if ( $form->{media} !~ /(screen|queue|email)/ ) { # printing
  1323. $form->{OUT} = ${LedgerSMB::Sysconfig::printer}{ $form->{media} };
  1324. $form->{printmode} = '|-';
  1325. $form->{OUT} =~ s/<%(fax)%>/<%$form->{vc}$1%>/;
  1326. $form->{OUT} =~ s/<%(.*?)%>/$form->{$1}/g;
  1327. if ( $form->{printed} !~ /$form->{formname}/ ) {
  1328. $form->{printed} .= " $form->{formname}";
  1329. $form->{printed} =~ s/^ //;
  1330. $form->update_status( \%myconfig );
  1331. }
  1332. $old_form->{printed} = $form->{printed} if defined %$old_form;
  1333. %audittrail = (
  1334. tablename => ($order) ? 'oe' : lc $ARAP,
  1335. reference => $form->{"${inv}number"},
  1336. formname => $form->{formname},
  1337. action => 'printed',
  1338. id => $form->{id}
  1339. );
  1340. $old_form->{audittrail} .=
  1341. $form->audittrail( "", \%myconfig, \%audittrail )
  1342. if defined %$old_form;
  1343. } elsif ( $form->{media} eq 'email' ) {
  1344. $form->{subject} = qq|$form->{label} $form->{"${inv}number"}|
  1345. unless $form->{subject};
  1346. $form->{plainpaper} = 1;
  1347. if ( $form->{emailed} !~ /$form->{formname}/ ) {
  1348. $form->{emailed} .= " $form->{formname}";
  1349. $form->{emailed} =~ s/^ //;
  1350. # save status
  1351. $form->update_status( \%myconfig );
  1352. }
  1353. $now = scalar localtime;
  1354. $cc = $locale->text( 'Cc: [_1]', $form->{cc} ) . qq|\n| if $form->{cc};
  1355. $bcc = $locale->text( 'Bcc: [_1]', $form->{bcc} ) . qq|\n|
  1356. if $form->{bcc};
  1357. $output_options{subject} = $form->{subject};
  1358. $output_options{to} = $form->{email};
  1359. $output_options{cc} = $form->{cc};
  1360. $output_options{bcc} = $form->{bcc};
  1361. ##SC: XXX the from address needs fixing
  1362. $output_options{from} = $myconfig{email};
  1363. $output_options{notify} = 1 if $form->{read_receipt};
  1364. if ( defined %$old_form ) {
  1365. $old_form->{intnotes} = qq|$old_form->{intnotes}\n\n|
  1366. if $old_form->{intnotes};
  1367. $old_form->{intnotes} .=
  1368. qq|[email]\n|
  1369. . $locale->text( 'Date: [_1]', $now ) . qq|\n|
  1370. . $locale->text( 'To: [_1]', $form->{email} )
  1371. . qq|\n${cc}${bcc}|
  1372. . $locale->text( 'Subject: [_1]', $form->{subject} ) . qq|\n|;
  1373. $old_form->{intnotes} .= qq|\n| . $locale->text('Message') . qq|: |;
  1374. $old_form->{intnotes} .=
  1375. ( $form->{message} ) ? $form->{message} : $locale->text('sent');
  1376. $old_form->{message} = $form->{message};
  1377. $old_form->{emailed} = $form->{emailed};
  1378. $old_form->{format} = "postscript" if $myconfig{printer};
  1379. $old_form->{media} = $myconfig{printer};
  1380. $old_form->save_intnotes( \%myconfig, ($order) ? 'oe' : lc $ARAP );
  1381. }
  1382. %audittrail = (
  1383. tablename => ($order) ? 'oe' : lc $ARAP,
  1384. reference => $form->{"${inv}number"},
  1385. formname => $form->{formname},
  1386. action => 'emailed',
  1387. id => $form->{id}
  1388. );
  1389. $old_form->{audittrail} .=
  1390. $form->audittrail( "", \%myconfig, \%audittrail )
  1391. if defined %$old_form;
  1392. } elsif ( $form->{media} eq 'queue' ) {
  1393. %queued = split / /, $form->{queued};
  1394. if ( $filename = $queued{ $form->{formname} } ) {
  1395. $form->{queued} =~ s/$form->{formname} $filename//;
  1396. unlink "${LedgerSMB::Sysconfig::spool}/$filename";
  1397. $filename =~ s/\..*$//g;
  1398. }
  1399. else {
  1400. $filename = time;
  1401. $filename .= $$;
  1402. }
  1403. $filename .= ( $form->{format} eq 'postscript' ) ? '.ps' : '.pdf';
  1404. $form->{OUT} = "${LedgerSMB::Sysconfig::spool}/$filename";
  1405. $form->{printmode} = '>';
  1406. $form->{queued} .= " $form->{formname} $filename";
  1407. $form->{queued} =~ s/^ //;
  1408. # save status
  1409. $form->update_status( \%myconfig );
  1410. $old_form->{queued} = $form->{queued};
  1411. %audittrail = (
  1412. tablename => ($order) ? 'oe' : lc $ARAP,
  1413. reference => $form->{"${inv}number"},
  1414. formname => $form->{formname},
  1415. action => 'queued',
  1416. id => $form->{id}
  1417. );
  1418. $old_form->{audittrail} .=
  1419. $form->audittrail( "", \%myconfig, \%audittrail );
  1420. }
  1421. $form->format_string( "email", "cc", "bcc" );
  1422. $form->{fileid} = $form->{"${inv}number"};
  1423. $form->{fileid} =~ s/(\s|\W)+//g;
  1424. my $template = LedgerSMB::Template->new(
  1425. user => \%myconfig,
  1426. template => $form->{'formname'},
  1427. format => uc $form->{format},
  1428. method => $form->{media},
  1429. output_options => \%output_options,
  1430. );
  1431. try {
  1432. $template->render($form);
  1433. }
  1434. catch Error::Simple with {
  1435. my $E = shift;
  1436. $form->error( $E->stacktrace );
  1437. };
  1438. # if we got back here restore the previous form
  1439. if ( defined %$old_form ) {
  1440. $old_form->{"${inv}number"} = $form->{"${inv}number"};
  1441. # restore and display form
  1442. for ( keys %$old_form ) { $form->{$_} = $old_form->{$_} }
  1443. delete $form->{pre};
  1444. $form->{rowcount}--;
  1445. for (qw(exchangerate creditlimit creditremaining)) {
  1446. $form->{$_} = $form->parse_amount( \%myconfig, $form->{$_} );
  1447. }
  1448. for $i ( 1 .. $form->{paidaccounts} ) {
  1449. for (qw(paid exchangerate)) {
  1450. $form->{"${_}_$i"} =
  1451. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  1452. }
  1453. }
  1454. &{"$display_form"};
  1455. }
  1456. }
  1457. sub customer_details {
  1458. IS->customer_details( \%myconfig, \%$form );
  1459. }
  1460. sub vendor_details {
  1461. IR->vendor_details( \%myconfig, \%$form );
  1462. }
  1463. sub ship_to {
  1464. $title = $form->{title};
  1465. $form->{title} = $locale->text('Ship to');
  1466. for (qw(exchangerate creditlimit creditremaining)) {
  1467. $form->{$_} = $form->parse_amount( \%myconfig, $form->{$_} );
  1468. }
  1469. for ( 1 .. $form->{paidaccounts} ) {
  1470. $form->{"paid_$_"} =
  1471. $form->parse_amount( \%myconfig, $form->{"paid_$_"} );
  1472. }
  1473. # get details for name
  1474. &{"$form->{vc}_details"};
  1475. $number =
  1476. ( $form->{vc} eq 'customer' )
  1477. ? $locale->text('Customer Number')
  1478. : $locale->text('Vendor Number');
  1479. $nextsub =
  1480. ( $form->{display_form} ) ? $form->{display_form} : "display_form";
  1481. $form->{rowcount}--;
  1482. $form->header;
  1483. print qq|
  1484. <body>
  1485. <form method=post action=$form->{script}>
  1486. <table width=100%>
  1487. <tr>
  1488. <td>
  1489. <table>
  1490. <tr class=listheading>
  1491. <th class=listheading colspan=2 width=50%>|
  1492. . $locale->text('Billing Address')
  1493. . qq|</th>
  1494. <th class=listheading width=50%>|
  1495. . $locale->text('Shipping Address')
  1496. . qq|</th>
  1497. </tr>
  1498. <tr height="5"></tr>
  1499. <tr>
  1500. <th align=right nowrap>$number</th>
  1501. <td>$form->{"$form->{vc}number"}</td>
  1502. </tr>
  1503. <tr>
  1504. <th align=right nowrap>| . $locale->text('Company Name') . qq|</th>
  1505. <td>$form->{name}</td>
  1506. <td><input name=shiptoname size=35 maxlength=64 value="$form->{shiptoname}"></td>
  1507. </tr>
  1508. <tr>
  1509. <th align=right nowrap>| . $locale->text('Address') . qq|</th>
  1510. <td>$form->{address1}</td>
  1511. <td><input name=shiptoaddress1 size=35 maxlength=32 value="$form->{shiptoaddress1}"></td>
  1512. </tr>
  1513. <tr>
  1514. <th></th>
  1515. <td>$form->{address2}</td>
  1516. <td><input name=shiptoaddress2 size=35 maxlength=32 value="$form->{shiptoaddress2}"></td>
  1517. </tr>
  1518. <tr>
  1519. <th align=right nowrap>| . $locale->text('City') . qq|</th>
  1520. <td>$form->{city}</td>
  1521. <td><input name=shiptocity size=35 maxlength=32 value="$form->{shiptocity}"></td>
  1522. </tr>
  1523. <tr>
  1524. <th align=right nowrap>| . $locale->text('State/Province') . qq|</th>
  1525. <td>$form->{state}</td>
  1526. <td><input name=shiptostate size=35 maxlength=32 value="$form->{shiptostate}"></td>
  1527. </tr>
  1528. <tr>
  1529. <th align=right nowrap>| . $locale->text('Zip/Postal Code') . qq|</th>
  1530. <td>$form->{zipcode}</td>
  1531. <td><input name=shiptozipcode size=10 maxlength=10 value="$form->{shiptozipcode}"></td>
  1532. </tr>
  1533. <tr>
  1534. <th align=right nowrap>| . $locale->text('Country') . qq|</th>
  1535. <td>$form->{country}</td>
  1536. <td><input name=shiptocountry size=35 maxlength=32 value="$form->{shiptocountry}"></td>
  1537. </tr>
  1538. <tr>
  1539. <th align=right nowrap>| . $locale->text('Contact') . qq|</th>
  1540. <td>$form->{contact}</td>
  1541. <td><input name=shiptocontact size=35 maxlength=64 value="$form->{shiptocontact}"></td>
  1542. </tr>
  1543. <tr>
  1544. <th align=right nowrap>| . $locale->text('Phone') . qq|</th>
  1545. <td>$form->{"$form->{vc}phone"}</td>
  1546. <td><input name=shiptophone size=20 value="$form->{shiptophone}"></td>
  1547. </tr>
  1548. <tr>
  1549. <th align=right nowrap>| . $locale->text('Fax') . qq|</th>
  1550. <td>$form->{"$form->{vc}fax"}</td>
  1551. <td><input name=shiptofax size=20 value="$form->{shiptofax}"></td>
  1552. </tr>
  1553. <tr>
  1554. <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
  1555. <td>$form->{email}</td>
  1556. <td><input name=shiptoemail size=35 value="$form->{shiptoemail}"></td>
  1557. </tr>
  1558. </table>
  1559. </td>
  1560. </tr>
  1561. </table>
  1562. <input type=hidden name=nextsub value=$nextsub>
  1563. |;
  1564. # delete shipto
  1565. for (qw(action nextsub)) { delete $form->{$_} }
  1566. for (
  1567. qw(name address1 address2 city state zipcode country contact phone fax email)
  1568. )
  1569. {
  1570. delete $form->{"shipto$_"};
  1571. }
  1572. $form->{title} = $title;
  1573. $form->hide_form;
  1574. print qq|
  1575. <hr size=3 noshade>
  1576. <br>
  1577. <button class="submit" type="submit" name="action" value="continue">|
  1578. . $locale->text('Continue')
  1579. . qq|</button>
  1580. </form>
  1581. </body>
  1582. </html>
  1583. |;
  1584. }