- #=====================================================================
- # LedgerSMB Small Medium Business Accounting
- # http://www.ledgersmb.org/
- #
- # Copyright (C) 2006
- # This work contains copyrighted information from a number of sources all used
- # with permission.
- #
- # This file contains source code included with or based on SQL-Ledger which
- # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
- # under the GNU General Public License version 2 or, at your option, any later
- # version. For a full list including contact information of contributors,
- # maintainers, and copyright holders, see the CONTRIBUTORS file.
- #
- # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
- # Copyright (c) 2001
- #
- # Author: DWS Systems Inc.
- # Web: http://www.sql-ledger.org
- #
- # Contributors:
- #
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- #======================================================================
- #
- # Inventory Control module
- #
- #======================================================================
- use LedgerSMB::IC;
- use LedgerSMB::Tax;
- require "bin/io.pl";
- 1;
- # end of main
- sub add {
- %label = (
- part => 'Part',
- service => 'Service',
- assembly => 'Assembly',
- labor => 'Labor/Overhead',
- );
- # $locale->text('Add Part')
- # $locale->text('Add Service')
- # $locale->text('Add Assembly')
- # $locale->text('Add Labor/Overhead')
- $label = "Add $label{$form->{item}}";
- $form->{title} = $locale->text($label);
- $form->{callback} =
- "$form->{script}?action=add&item=$form->{item}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}"
- unless $form->{callback};
- $form->{orphaned} = 1;
- if ( $form->{previousform} ) {
- $form->{callback} = "";
- }
- &link_part;
- &display_form;
- }
- sub edit {
- %label = (
- part => 'Part',
- service => 'Service',
- assembly => 'Assembly',
- labor => 'Labor/Overhead',
- );
- # $locale->text('Edit Part')
- # $locale->text('Edit Service')
- # $locale->text('Edit Assembly')
- # $locale->text('Edit Labor/Overhead')
- IC->get_part( \%myconfig, \%$form );
- $label = "Edit $label{$form->{item}}";
- $form->{title} = $locale->text($label);
- $form->{previousform} = $form->escape( $form->{previousform}, 1 )
- if $form->{previousform};
- &link_part;
- &display_form;
- }
- sub link_part {
- IC->create_links( "IC", \%myconfig, \%$form );
- # currencies
- $form->{selectcurrency} = "";
- for ( split /:/, $form->{currencies} ) {
- $form->{selectcurrency} .= "<option>$_\n";
- }
- # readonly
- if ( $form->{item} eq 'part' ) {
- $form->{readonly} = 1
- if $myconfig{acs} =~ /Goods \& Services--Add Part/;
- $form->error(
- $locale->text(
- 'Cannot create Part; Inventory account does not exist!')
- ) if !@{ $form->{IC_links}{IC} };
- $form->error(
- $locale->text('Cannot create Part; Income account does not exist!')
- ) if !@{ $form->{IC_links}{IC_sale} };
- $form->error(
- $locale->text('Cannot create Part; COGS account does not exist!') )
- if !@{ $form->{IC_links}{IC_cogs} };
- }
- if ( $form->{item} eq 'service' ) {
- $form->{readonly} = 1
- if $myconfig{acs} =~ /Goods \& Services--Add Service/;
- $form->error(
- $locale->text(
- 'Cannot create Service; Income account does not exist!')
- ) if !@{ $form->{IC_links}{IC_income} };
- $form->error(
- $locale->text(
- 'Cannot create Service; Expense account does not exist!')
- ) if !@{ $form->{IC_links}{IC_expense} };
- }
- if ( $form->{item} eq 'assembly' ) {
- $form->{readonly} = 1
- if $myconfig{acs} =~ /Goods \& Services--Add Assembly/;
- $form->error(
- $locale->text(
- 'Cannot create Assembly; Income account does not exist!')
- ) if !@{ $form->{IC_links}{IC_income} };
- }
- if ( $form->{item} eq 'labor' ) {
- $form->{readonly} = 1
- if $myconfig{acs} =~ /Goods \& Services--Add Labor\/Overhead/;
- $form->error(
- $locale->text(
- 'Cannot create Labor; Inventory account does not exist!')
- ) if !@{ $form->{IC_links}{IC} };
- $form->error(
- $locale->text('Cannot create Labor; COGS account does not exist!') )
- if !@{ $form->{IC_links}{IC_cogs} };
- }
- # parts, assemblies , labor and overhead have the same links
- $taxpart = ( $form->{item} eq 'service' ) ? "service" : "part";
- # build the popup menus
- $form->{taxaccounts} = "";
- foreach $key ( keys %{ $form->{IC_links} } ) {
- $form->{"select$key"} = "";
- foreach $ref ( @{ $form->{IC_links}{$key} } ) {
- # if this is a tax field
- if ( $key =~ /IC_tax/ ) {
- if ( $key =~ /$taxpart/ ) {
- $form->{taxaccounts} .= "$ref->{accno} ";
- $form->{"IC_tax_$ref->{accno}_description"} =
- "$ref->{accno}--$ref->{description}";
- if ( $form->{id} ) {
- if ( $form->{amount}{ $ref->{accno} } ) {
- $form->{"IC_tax_$ref->{accno}"} = "checked";
- }
- }
- else {
- $form->{"IC_tax_$ref->{accno}"} = "checked";
- }
- }
- }
- else {
- $form->{"select$key"} .=
- "<option>$ref->{accno}--$ref->{description}\n";
- }
- }
- }
- chop $form->{taxaccounts};
- if ( $form->{item} !~ /service/ ) {
- $form->{selectIC_inventory} = $form->{selectIC};
- $form->{selectIC_income} = $form->{selectIC_sale};
- $form->{selectIC_expense} = $form->{selectIC_cogs};
- $form->{IC_income} = $form->{IC_sale};
- $form->{IC_expense} = $form->{IC_cogs};
- }
- # set option
- for (qw(IC_inventory IC_income IC_expense)) {
- $form->{$_} =
- "$form->{amount}{$_}{accno}--$form->{amount}{$_}{description}"
- if $form->{amount}{$_}{accno};
- }
- delete $form->{IC_links};
- delete $form->{amount};
- $form->get_partsgroup( \%myconfig, { all => 1 } );
- if ( $form->{partsgroup} ) {
- $form->{partsgroup} =
- $form->quote( $form->{partsgroup} ) . "--$form->{partsgroup_id}";
- }
- if ( @{ $form->{all_partsgroup} } ) {
- $form->{selectpartsgroup} = qq|<option>\n|;
- for ( @{ $form->{all_partsgroup} } ) {
- $form->{selectpartsgroup} .=
- qq|<option value="|
- . $form->quote( $_->{partsgroup} )
- . qq|--$_->{id}">$_->{partsgroup}\n|;
- }
- delete $form->{all_partsgroup};
- }
- if ( $form->{item} eq 'assembly' ) {
- for ( 1 .. $form->{assembly_rows} ) {
- if ( $form->{"partsgroup_id_$_"} ) {
- $form->{"partsgroup_$_"} =
- qq|$form->{"partsgroup_$_"}--$form->{"partsgroup_id_$_"}|;
- }
- }
- $form->get_partsgroup( \%myconfig );
- if ( @{ $form->{all_partsgroup} } ) {
- $form->{selectassemblypartsgroup} = qq|<option>\n|;
- for ( @{ $form->{all_partsgroup} } ) {
- $form->{selectassemblypartsgroup} .=
- qq|<option value="$_->{partsgroup}--$_->{id}">$_->{partsgroup}\n|;
- }
- delete $form->{all_partsgroup};
- }
- }
- # setup make and models
- $i = 1;
- foreach $ref ( @{ $form->{makemodels} } ) {
- for (qw(make model)) { $form->{"${_}_$i"} = $ref->{$_} }
- $i++;
- }
- $form->{makemodel_rows} = $i - 1;
- delete $form->{makemodels};
- # setup vendors
- if ( @{ $form->{all_vendor} } ) {
- $form->{selectvendor} = "<option>\n";
- for ( @{ $form->{all_vendor} } ) {
- $form->{selectvendor} .=
- qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
- }
- delete $form->{all_vendor};
- }
- # vendor matrix
- $i = 1;
- foreach $ref ( @{ $form->{vendormatrix} } ) {
- $form->{"vendor_$i"} = qq|$ref->{name}--$ref->{id}|;
- for (qw(partnumber lastcost leadtime vendorcurr)) {
- $form->{"${_}_$i"} = $ref->{$_};
- }
- $i++;
- }
- $form->{vendor_rows} = $i - 1;
- delete $form->{vendormatrix};
- # setup customers and groups
- if ( @{ $form->{all_customer} } ) {
- $form->{selectcustomer} = "<option>\n";
- for ( @{ $form->{all_customer} } ) {
- $form->{selectcustomer} .=
- qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
- }
- delete $form->{all_customer};
- }
- if ( @{ $form->{all_pricegroup} } ) {
- $form->{selectpricegroup} = "<option>\n";
- for ( @{ $form->{all_pricegroup} } ) {
- $form->{selectpricegroup} .=
- qq|<option value="$_->{pricegroup}--$_->{id}">$_->{pricegroup}\n|;
- }
- delete $form->{all_pricegroup};
- }
- $i = 1;
- # customer matrix
- foreach $ref ( @{ $form->{customermatrix} } ) {
- $form->{"customer_$i"} = "$ref->{name}--$ref->{cid}" if $ref->{cid};
- $form->{"pricegroup_$i"} = "$ref->{pricegroup}--$ref->{gid}"
- if $ref->{gid};
- for (qw(validfrom validto pricebreak customerprice customercurr)) {
- $form->{"${_}_$i"} = $ref->{$_};
- }
- $i++;
- }
- $form->{customer_rows} = $i - 1;
- delete $form->{customermatrix};
- }
- sub form_header {
- if ( $form->{lastcost} > 0 ) {
- $markup =
- $form->round_amount(
- ( ( $form->{sellprice} / $form->{lastcost} - 1 ) * 100 ), 1 );
- $form->{markup} = $form->format_amount( \%myconfig, $markup, 1 );
- }
- ($dec) = ( $form->{sellprice} =~ /\.(\d+)/ );
- $dec = length $dec;
- $decimalplaces = ( $dec > 2 ) ? $dec : 2;
- for (qw(listprice sellprice)) {
- $form->{$_} =
- $form->format_amount( \%myconfig, $form->{$_}, $decimalplaces );
- }
- ($dec) = ( $form->{lastcost} =~ /\.(\d+)/ );
- $dec = length $dec;
- $decimalplaces = ( $dec > 2 ) ? $dec : 2;
- for (qw(lastcost avgcost)) {
- $form->{$_} =
- $form->format_amount( \%myconfig, $form->{$_}, $decimalplaces );
- }
- for (qw(weight rop stock)) {
- $form->{$_} = $form->format_amount( \%myconfig, $form->{$_} );
- }
- for (qw(partnumber description unit notes)) {
- $form->{$_} = $form->quote( $form->{$_} );
- }
- if ( ( $rows = $form->numtextrows( $form->{notes}, 40 ) ) < 2 ) {
- $rows = 2;
- }
- $notes =
- qq|<textarea name=notes rows=$rows cols=40 wrap=soft>$form->{notes}</textarea>|;
- if ( ( $rows = $form->numtextrows( $form->{description}, 40 ) ) > 1 ) {
- $description =
- qq|<textarea name="description" rows=$rows cols=40 wrap=soft>$form->{description}</textarea>|;
- }
- else {
- $description =
- qq|<input name="description" size="40" value="$form->{description}">|;
- }
- for ( split / /, $form->{taxaccounts} ) {
- $form->{"IC_tax_$_"} = ( $form->{"IC_tax_$_"} ) ? "checked" : "";
- }
- $form->{selectIC_inventory} = $form->{selectIC};
- # set option
- for (qw(IC_inventory IC_income IC_expense)) {
- if ( $form->{$_} ) {
- if ( $form->{orphaned} ) {
- $form->{"select$_"} =~ s/ selected//;
- $form->{"select$_"} =~
- s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
- }
- else {
- $form->{"select$_"} = qq|<option selected>$form->{$_}|;
- }
- }
- }
- if ( $form->{selectpartsgroup} ) {
- $form->{selectpartsgroup} =
- $form->unescape( $form->{selectpartsgroup} );
- $partsgroup =
- qq|<input type="hidden" name="selectpartsgroup" value="|
- . $form->escape( $form->{selectpartsgroup}, 1 ) . qq|">|;
- $form->{partsgroup} = $form->quote( $form->{partsgroup} );
- $form->{selectpartsgroup} =~
- s/(<option value="\Q$form->{partsgroup}\E")/$1 selected/;
- $partsgroup .=
- qq|\n<select name="partsgroup">$form->{selectpartsgroup}</select>|;
- $group = $locale->text('Group');
- }
- # tax fields
- foreach $item ( split / /, $form->{taxaccounts} ) {
- $tax .= qq|
- <input class="checkbox" type="checkbox" name="IC_tax_$item" value="1" $form->{"IC_tax_$item"}> <b>$form->{"IC_tax_${item}_description"}</b>
- <br><input type="hidden" name="IC_tax_${item}_description" value="$form->{"IC_tax_${item}_description"}">
- |;
- }
- $sellprice = qq|
- <tr>
- <th align="right" nowrap="true">| . $locale->text('Sell Price') . qq|</th>
- <td><input name="sellprice" size="11" value="$form->{sellprice}"></td>
- </tr>
- <tr>
- <th align="right" nowrap="true">| . $locale->text('List Price') . qq|</th>
- <td><input name="listprice" size="11" value="$form->{listprice}"></td>
- </tr>
- |;
- $avgcost = qq|
- <tr>
- <th align="right" nowrap="true">|
- . $locale->text('Average Cost')
- . qq|</th>
- <td><input type="hidden" name="avgcost" value="$form->{avgcost}">$form->{avgcost}</td>
- </tr>
- |;
- $lastcost = qq|
- <tr>
- <th align="right" nowrap="true">|
- . $locale->text('Last Cost')
- . qq|</th>
- <td><input name="lastcost" size="11" value="$form->{lastcost}"></td>
- </tr>
- <tr>
- <th align="right" nowrap="true">|
- . $locale->text('Markup')
|