me='generator' content='cgit v1.2.3'/>
summaryrefslogtreecommitdiff
path: root/bin/ic.pl
blob: fc940b3935b6cb30eb12963291751410fffa1b96 (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) 2001
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors:
  22. #
  23. #
  24. # This program is free software; you can redistribute it and/or modify
  25. # it under the terms of the GNU General Public License as published by
  26. # the Free Software Foundation; either version 2 of the License, or
  27. # (at your option) any later version.
  28. #
  29. # This program is distributed in the hope that it will be useful,
  30. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. # GNU General Public License for more details.
  33. # You should have received a copy of the GNU General Public License
  34. # along with this program; if not, write to the Free Software
  35. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. #======================================================================
  37. #
  38. # Inventory Control module
  39. #
  40. #======================================================================
  41. use LedgerSMB::IC;
  42. use LedgerSMB::Tax;
  43. require "bin/io.pl";
  44. 1;
  45. # end of main
  46. sub add {
  47. %label = (
  48. part => 'Part',
  49. service => 'Service',
  50. assembly => 'Assembly',
  51. labor => 'Labor/Overhead',
  52. );
  53. # $locale->text('Add Part')
  54. # $locale->text('Add Service')
  55. # $locale->text('Add Assembly')
  56. # $locale->text('Add Labor/Overhead')
  57. $label = "Add $label{$form->{item}}";
  58. $form->{title} = $locale->text($label);
  59. $form->{callback} =
  60. "$form->{script}?action=add&item=$form->{item}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}"
  61. unless $form->{callback};
  62. $form->{orphaned} = 1;
  63. if ( $form->{previousform} ) {
  64. $form->{callback} = "";
  65. }
  66. &link_part;
  67. &display_form;
  68. }
  69. sub edit {
  70. %label = (
  71. part => 'Part',
  72. service => 'Service',
  73. assembly => 'Assembly',
  74. labor => 'Labor/Overhead',
  75. );
  76. # $locale->text('Edit Part')
  77. # $locale->text('Edit Service')
  78. # $locale->text('Edit Assembly')
  79. # $locale->text('Edit Labor/Overhead')
  80. IC->get_part( \%myconfig, \%$form );
  81. $label = "Edit $label{$form->{item}}";
  82. $form->{title} = $locale->text($label);
  83. $form->{previousform} = $form->escape( $form->{previousform}, 1 )
  84. if $form->{previousform};
  85. &link_part;
  86. &display_form;
  87. }
  88. sub link_part {
  89. IC->create_links( "IC", \%myconfig, \%$form );
  90. # currencies
  91. $form->{selectcurrency} = "";
  92. for ( split /:/, $form->{currencies} ) {
  93. $form->{selectcurrency} .= "<option>$_\n";
  94. }
  95. # readonly
  96. if ( $form->{item} eq 'part' ) {
  97. $form->{readonly} = 1
  98. if $myconfig{acs} =~ /Goods \& Services--Add Part/;
  99. $form->error(
  100. $locale->text(
  101. 'Cannot create Part; Inventory account does not exist!')
  102. ) if !@{ $form->{IC_links}{IC} };
  103. $form->error(
  104. $locale->text('Cannot create Part; Income account does not exist!')
  105. ) if !@{ $form->{IC_links}{IC_sale} };
  106. $form->error(
  107. $locale->text('Cannot create Part; COGS account does not exist!') )
  108. if !@{ $form->{IC_links}{IC_cogs} };
  109. }
  110. if ( $form->{item} eq 'service' ) {
  111. $form->{readonly} = 1
  112. if $myconfig{acs} =~ /Goods \& Services--Add Service/;
  113. $form->error(
  114. $locale->text(
  115. 'Cannot create Service; Income account does not exist!')
  116. ) if !@{ $form->{IC_links}{IC_income} };
  117. $form->error(
  118. $locale->text(
  119. 'Cannot create Service; Expense account does not exist!')
  120. ) if !@{ $form->{IC_links}{IC_expense} };
  121. }
  122. if ( $form->{item} eq 'assembly' ) {
  123. $form->{readonly} = 1
  124. if $myconfig{acs} =~ /Goods \& Services--Add Assembly/;
  125. $form->error(
  126. $locale->text(
  127. 'Cannot create Assembly; Income account does not exist!')
  128. ) if !@{ $form->{IC_links}{IC_income} };
  129. }
  130. if ( $form->{item} eq 'labor' ) {
  131. $form->{readonly} = 1
  132. if $myconfig{acs} =~ /Goods \& Services--Add Labor\/Overhead/;
  133. $form->error(
  134. $locale->text(
  135. 'Cannot create Labor; Inventory account does not exist!')
  136. ) if !@{ $form->{IC_links}{IC} };
  137. $form->error(
  138. $locale->text('Cannot create Labor; COGS account does not exist!') )
  139. if !@{ $form->{IC_links}{IC_cogs} };
  140. }
  141. # parts, assemblies , labor and overhead have the same links
  142. $taxpart = ( $form->{item} eq 'service' ) ? "service" : "part";
  143. # build the popup menus
  144. $form->{taxaccounts} = "";
  145. foreach $key ( keys %{ $form->{IC_links} } ) {
  146. $form->{"select$key"} = "";
  147. foreach $ref ( @{ $form->{IC_links}{$key} } ) {
  148. # if this is a tax field
  149. if ( $key =~ /IC_tax/ ) {
  150. if ( $key =~ /$taxpart/ ) {
  151. $form->{taxaccounts} .= "$ref->{accno} ";
  152. $form->{"IC_tax_$ref->{accno}_description"} =
  153. "$ref->{accno}--$ref->{description}";
  154. if ( $form->{id} ) {
  155. if ( $form->{amount}{ $ref->{accno} } ) {
  156. $form->{"IC_tax_$ref->{accno}"} = "checked";
  157. }
  158. }
  159. else {
  160. $form->{"IC_tax_$ref->{accno}"} = "checked";
  161. }
  162. }
  163. }
  164. else {
  165. $form->{"select$key"} .=
  166. "<option>$ref->{accno}--$ref->{description}\n";
  167. }
  168. }
  169. }
  170. chop $form->{taxaccounts};
  171. if ( $form->{item} !~ /service/ ) {
  172. $form->{selectIC_inventory} = $form->{selectIC};
  173. $form->{selectIC_income} = $form->{selectIC_sale};
  174. $form->{selectIC_expense} = $form->{selectIC_cogs};
  175. $form->{IC_income} = $form->{IC_sale};
  176. $form->{IC_expense} = $form->{IC_cogs};
  177. }
  178. # set option
  179. for (qw(IC_inventory IC_income IC_expense)) {
  180. $form->{$_} =
  181. "$form->{amount}{$_}{accno}--$form->{amount}{$_}{description}"
  182. if $form->{amount}{$_}{accno};
  183. }
  184. delete $form->{IC_links};
  185. delete $form->{amount};
  186. $form->get_partsgroup( \%myconfig, { all => 1 } );
  187. if ( $form->{partsgroup} ) {
  188. $form->{partsgroup} =
  189. $form->quote( $form->{partsgroup} ) . "--$form->{partsgroup_id}";
  190. }
  191. if ( @{ $form->{all_partsgroup} } ) {
  192. $form->{selectpartsgroup} = qq|<option>\n|;
  193. for ( @{ $form->{all_partsgroup} } ) {
  194. $form->{selectpartsgroup} .=
  195. qq|<option value="|
  196. . $form->quote( $_->{partsgroup} )
  197. . qq|--$_->{id}">$_->{partsgroup}\n|;
  198. }
  199. delete $form->{all_partsgroup};
  200. }
  201. if ( $form->{item} eq 'assembly' ) {
  202. for ( 1 .. $form->{assembly_rows} ) {
  203. if ( $form->{"partsgroup_id_$_"} ) {
  204. $form->{"partsgroup_$_"} =
  205. qq|$form->{"partsgroup_$_"}--$form->{"partsgroup_id_$_"}|;
  206. }
  207. }
  208. $form->get_partsgroup( \%myconfig );
  209. if ( @{ $form->{all_partsgroup} } ) {
  210. $form->{selectassemblypartsgroup} = qq|<option>\n|;
  211. for ( @{ $form->{all_partsgroup} } ) {
  212. $form->{selectassemblypartsgroup} .=
  213. qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n|;
  214. }
  215. delete $form->{all_partsgroup};
  216. }
  217. }
  218. # setup make and models
  219. $i = 1;
  220. foreach $ref ( @{ $form->{makemodels} } ) {
  221. for (qw(make model)) { $form->{"${_}_$i"} = $ref->{$_} }
  222. $i++;
  223. }
  224. $form->{makemodel_rows} = $i - 1;
  225. delete $form->{makemodels};
  226. # setup vendors
  227. if ( @{ $form->{all_vendor} } ) {
  228. $form->{selectvendor} = "<option>\n";
  229. for ( @{ $form->{all_vendor} } ) {
  230. $form->{selectvendor} .=
  231. qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
  232. }
  233. delete $form->{all_vendor};
  234. }
  235. # vendor matrix
  236. $i = 1;
  237. foreach $ref ( @{ $form->{vendormatrix} } ) {
  238. $form->{"vendor_$i"} = qq|$ref->{name}--$ref->{id}|;
  239. for (qw(partnumber lastcost leadtime vendorcurr)) {
  240. $form->{"${_}_$i"} = $ref->{$_};
  241. }
  242. $i++;
  243. }
  244. $form->{vendor_rows} = $i - 1;
  245. delete $form->{vendormatrix};
  246. # setup customers and groups
  247. if ( @{ $form->{all_customer} } ) {
  248. $form->{selectcustomer} = "<option>\n";
  249. for ( @{ $form->{all_customer} } ) {
  250. $form->{selectcustomer} .=
  251. qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
  252. }
  253. delete $form->{all_customer};
  254. }
  255. if ( @{ $form->{all_pricegroup} } ) {
  256. $form->{selectpricegroup} = "<option>\n";
  257. for ( @{ $form->{all_pricegroup} } ) {
  258. $form->{selectpricegroup} .=
  259. qq|<option value="$_->{pricegroup}--$_->{id}">$_->{pricegroup}\n|;
  260. }
  261. delete $form->{all_pricegroup};
  262. }
  263. $i = 1;
  264. # customer matrix
  265. foreach $ref ( @{ $form->{customermatrix} } ) {
  266. $form->{"customer_$i"} = "$ref->{name}--$ref->{cid}" if $ref->{cid};
  267. $form->{"pricegroup_$i"} = "$ref->{pricegroup}--$ref->{gid}"
  268. if $ref->{gid};
  269. for (qw(validfrom validto pricebreak customerprice customercurr)) {
  270. $form->{"${_}_$i"} = $ref->{$_};
  271. }
  272. $i++;
  273. }
  274. $form->{customer_rows} = $i - 1;
  275. delete $form->{customermatrix};
  276. }
  277. sub form_header {
  278. if ( $form->{lastcost} > 0 ) {
  279. $markup =
  280. $form->round_amount(
  281. ( ( $form->{sellprice} / $form->{lastcost} - 1 ) * 100 ), 1 );
  282. $form->{markup} = $form->format_amount( \%myconfig, $markup, 1 );
  283. }
  284. ($dec) = ( $form->{sellprice} =~ /\.(\d+)/ );
  285. $dec = length $dec;
  286. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  287. for (qw(listprice sellprice)) {
  288. $form->{$_} =
  289. $form->format_amount( \%myconfig, $form->{$_}, $decimalplaces );
  290. }
  291. ($dec) = ( $form->{lastcost} =~ /\.(\d+)/ );
  292. $dec = length $dec;
  293. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  294. for (qw(lastcost avgcost)) {
  295. $form->{$_} =
  296. $form->format_amount( \%myconfig, $form->{$_}, $decimalplaces );
  297. }
  298. for (qw(weight rop stock)) {
  299. $form->{$_} = $form->format_amount( \%myconfig, $form->{$_} );
  300. }
  301. for (qw(partnumber description unit notes)) {
  302. $form->{$_} = $form->quote( $form->{$_} );
  303. }
  304. if ( ( $rows = $form->numtextrows( $form->{notes}, 40 ) ) < 2 ) {
  305. $rows = 2;
  306. }
  307. $notes =
  308. qq|<textarea name=notes rows=$rows cols=40 wrap=soft>$form->{notes}</textarea>|;
  309. if ( ( $rows = $form->numtextrows( $form->{description}, 40 ) ) > 1 ) {
  310. $description =
  311. qq|<textarea name="description" rows=$rows cols=40 wrap=soft>$form->{description}</textarea>|;
  312. }
  313. else {
  314. $description =
  315. qq|<input name="description" size="40" value="$form->{description}">|;
  316. }
  317. for ( split / /, $form->{taxaccounts} ) {
  318. $form->{"IC_tax_$_"} = ( $form->{"IC_tax_$_"} ) ? "checked" : "";
  319. }
  320. $form->{selectIC_inventory} = $form->{selectIC};
  321. # set option
  322. for (qw(IC_inventory IC_income IC_expense)) {
  323. if ( $form->{$_} ) {
  324. if ( $form->{orphaned} ) {
  325. $form->{"select$_"} =~ s/ selected//;
  326. $form->{"select$_"} =~
  327. s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  328. }
  329. else {
  330. $form->{"select$_"} = qq|<option selected>$form->{$_}|;
  331. }
  332. }
  333. }
  334. if ( $form->{selectpartsgroup} ) {
  335. $form->{selectpartsgroup} =
  336. $form->unescape( $form->{selectpartsgroup} );
  337. $partsgroup =
  338. qq|<input type="hidden" name="selectpartsgroup" value="|
  339. . $form->escape( $form->{selectpartsgroup}, 1 ) . qq|">|;
  340. $form->{partsgroup} = $form->quote( $form->{partsgroup} );
  341. $form->{selectpartsgroup} =~
  342. s/(<option value="\Q$form->{partsgroup}\E")/$1 selected/;
  343. $partsgroup .=
  344. qq|\n<select name="partsgroup">$form->{selectpartsgroup}</select>|;
  345. $group = $locale->text('Group');
  346. }
  347. # tax fields
  348. foreach $item ( split / /, $form->{taxaccounts} ) {
  349. $tax .= qq|
  350. <input class="checkbox" type="checkbox" name="IC_tax_$item" value="1" $form->{"IC_tax_$item"}>&nbsp;<b>$form->{"IC_tax_${item}_description"}</b>
  351. <br><input type="hidden" name="IC_tax_${item}_description" value="$form->{"IC_tax_${item}_description"}">
  352. |;
  353. }
  354. $sellprice = qq|
  355. <tr>
  356. <th align="right" nowrap="true">| . $locale->text('Sell Price') . qq|</th>
  357. <td><input name="sellprice" size="11" value="$form->{sellprice}"></td>
  358. </tr>
  359. <tr>
  360. <th align="right" nowrap="true">| . $locale->text('List Price') . qq|</th>
  361. <td><input name="listprice" size="11" value="$form->{listprice}"></td>
  362. </tr>
  363. |;
  364. $avgcost = qq|
  365. <tr>
  366. <th align="right" nowrap="true">|
  367. . $locale->text('Average Cost')
  368. . qq|</th>
  369. <td><input type="hidden" name="avgcost" value="$form->{avgcost}">$form->{avgcost}</td>
  370. </tr>
  371. |;
  372. $lastcost = qq|
  373. <tr>
  374. <th align="right" nowrap="true">|
  375. . $locale->text('Last Cost')
  376. . qq|</th>
  377. <td><input name="lastcost" size="11" value="$form->{lastcost}"></td>
  378. </tr>
  379. <tr>
  380. <th align="right" nowrap="true">|
  381. . $locale->text('Markup')