- #=====================================================================
- # 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').";".$locale->text('Inventory account does not exist!')) if ! @{ $form->{IC_links}{IC} };
- $form->error($locale->text('Cannot create Part').";".$locale->text('Income account does not exist!')) if ! @{ $form->{IC_links}{IC_sale} };
- $form->error($locale->text('Cannot create Part').";".$locale->text('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').";".$locale->text('Income account does not exist!')) if ! @{ $form->{IC_links}{IC_income} };
- $form->error($locale->text('Cannot create Service').";".$locale->text('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').";".$locale->text('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').";".$locale->text('Inventory account does not exist!')) if ! @{ $form->{IC_links}{IC} };
- $form->error($locale->text('Cannot create Labor').";".$locale->text('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').qq| %</th>
- <td><input name=markup size=5 value=$form->{markup}></td>
- <input type=hidden name=oldmarkup value=$markup>
- </tr>
- |;
- if ($form->{item} =~ /(part|assembly)/) {
- $n = ($form->{onhand} > 0) ? "1" : "0";
- $onhand = qq|
- <tr>
- <th align="right" nowrap>|.$locale->text('On Hand').qq|</th>
- <th align=left nowrap class="plus$n"> |.$form->format_amount(\%myconfig, $form->{onhand}).qq|</th>
- </tr>
- |;
- $rop = qq|
- <tr>
- <th align="right" nowrap="true">|.$locale->text('ROP').qq|</th>
- <td><input name=rop size=10 value=$form->{rop}></td>
- </tr>
- |;
-
- $bin = qq|
- <tr>
- <th align="right" nowrap="true">|.$locale->text('Bin').qq|</th>
- <td><input name=bin size=10 value="$form->{bin}"></td>
- </tr>
- |;
-
- $imagelinks = qq|
- <tr>
- <td>
- <table width=100%>
- <tr>
- <th align=right nowrap>|.$locale->text('Image').qq|</th>
- <td><input name=image size=40 value="$form->{image}"></td>
- <th align=right nowrap>|.$locale->text('Microfiche').qq|</th>
- <td><input name=microfiche size=20 value="$form->{microfiche}"></td>
- </tr>
- <tr>
- <th align=right nowrap>|.$locale->text('Drawing').qq|</th>
- <td><input name=drawing size=40 value="$form->{drawing}"></td>
- </tr>
- </table>
- </td>
- </tr>
- |;
- }
- if ($form->{item} eq "part") {
- $linkaccounts = qq|
- <tr>
- <th align=right>|.$locale->text('Inventory').qq|</th>
- <td><select name=IC_inventory>$form->{selectIC_inventory}</select></td>
- <input name=selectIC type=hidden value="$form->{selectIC}">
- </tr>
- <tr>
- <th align=right>|.$locale->text('Income').qq|</th>
- <td><select name=IC_income>$form->{selectIC_income}</select></td>
- <input name=selectIC_income type=hidden value="$form->{selectIC_income}">
- </tr>
- <tr>
- <th align=right>|.$locale->text('COGS').qq|</th>
- <td><select name=IC_expense>$form->{selectIC_expense}</select></td>
- <input name=selectIC_expense type=hidden value="$form->{selectIC_expense}">
- </tr>
- |;
-
- if ($tax) {
- $linkaccounts .= qq|
- <tr>
- <th align=right>|.$locale->text('Tax').qq|</th>
- <td>$tax</td>
- </tr>
- |;
- }
-
- $weight = qq|
- <tr>
- <th align="right" nowrap="true">|.$locale->text('Weight').qq|</th>
- <td>
- <table>
- <tr>
- <td>
- <input name=weight size=10 value=$form->{weight}>
- </td>
- <th>
-
- $form->{weightunit}
- <input type=hidden name=weightunit value=$form->{weightunit}>
- </th>
- </tr>
- </table>
- </td>
- </tr>
- |;
-
- }
- if ($form->{item} eq "assembly") {
- $avgcost = "";
-
- if ($form->{project_id}) {
- $weight = qq|
- <tr>
- <th align="right" nowrap="true">|.$locale->text('Weight').qq|</th>
- <td>
- <table>
- <tr>
- <td>
- <input name=weight size=10 value=$form->{weight}>
- </td>
- <th>
-
- $form->{weightunit}
- <input type=hidden name=weightunit value=$form->{weightunit}>
- </th>
- </tr>
- </table>
- </td>
- </tr>
- |;
- } else {
-
- $weight = qq|
- <tr>
- <th align="right" nowrap="true">|.$locale->text('Weight').qq|</th>
- <td>
- <table>
- <tr>
- <td>
- $form->{weight}
- <input type=hidden name=weight value=$form->{weight}>
- </td>
- <th>
-
- $form->{weightunit}
- <input type=hidden name=weightunit value=$form->{weightunit}>
- </th>
- </tr>
- </table>
- </td>
- </tr>
- |;
- }
-
- if ($form->{project_id}) {
- $lastcost = "";
- $avgcost = "";
- $onhand = "";
- $rop = "";
- $form->{isassemblyitem} = 1;
- } else {
- $stock = qq|
- <tr>
- <th align="right" nowrap>|.$locale->text('Stock').qq|</th>
- <td><input name=stock size=10 value=$form->{stock}></td>
- </tr>
- |;
- $lastcost = qq|
- <tr>
- <th align="right" nowrap="true">|.$locale->text('Last Cost').qq|</th>
- <td><input type=hidden name=lastcost value=$form->{lastcost}>$form->{lastcost}</td>
- </tr>
- <tr>
- <th align="right" nowrap="true">|.$locale->text('Markup').qq| %</th>
- <td><input name=markup size=5 value=$form->{markup}></td>
- <input type=hidden name=oldmarkup value=$markup>
- </tr>
- |;
- }
- $linkaccounts = qq|
- <tr>
- <th align=right>|.$locale->text('Income').qq|</th>
- <td><select name=IC_income>$form->{selectIC_income}</select></td>
- <input name=selectIC_income type=hidden value="$form->{selectIC_income}">
- </tr>
- |;
-
- if ($tax) {
- $linkaccounts .= qq|
- <tr>
- <th align=right>|.$locale->text('Tax').qq|</th>
- <td>$tax</td>
- </tr>
- |;
- }
-
- }
-
- if ($form->{item} eq "service") {
- $avgcost = "";
- $linkaccounts = qq|
- <tr>
- <th align=right>|.$locale->text('Income').qq|</th>
- <td><select name=IC_income>$form->{selectIC_income}</select></td>
- <input name=selectIC_income type=hidden value="$form->{selectIC_income}">
- </tr>
- <tr>
- <th align=right>|.$locale->text('Expense').qq|</th>
- <td><select name=IC_expense>$form->{selectIC_expense}</select></td>
- <input name=selectIC_expense type=hidden value="$form->{selectIC_expense}">
- </tr>
- |;
-
- if ($tax) {
- $linkaccounts .= qq|
- <tr>
- <th align=right>|.$locale->text('Tax').qq|</th>
- <td>$tax</td>
- </tr>
- |;
- }
- }
- if ($form->{item} eq 'labor') {
- $avgcost = "";
- $n = ($form->{onhand} > 0) ? "1" : "0";
- $onhand = qq|
- <tr>
- <th align="right" nowrap>|.$locale->text('On Hand').qq|</th>
- <th align=left nowrap class="plus$n"> |.$form->format_amount(\%myconfig, $form->{onhand}).qq|</th>
- </tr>
- |;
-
- $linkaccounts = qq|
- <tr>
- <th align=right>|.$locale->text('Labor/Overhead').qq|</th>
- <td><select name=IC_inventory>$form->{selectIC_inventory}</select></td>
- <input name=selectIC type=hidden value="$form->{selectIC}">
- </tr>
- <tr>
- <th align=right>|.$locale->text('COGS').qq|</th>
- <td><select name=IC_expense>$form->{selectIC_expense}</select></td>
- <input name=selectIC_expense type=hidden value="$form->{selectIC_expense}">
- </tr>
- |;
-
- }
- if ($form->{id}) {
- $checked = ($form->{obsolete}) ? "checked" : "";
- $obsolete = qq|
- <tr>
- <th align="right" nowrap="true">|.$locale->text('Obsolete').qq|</th>
- <td><input name=obsolete type=checkbox class=checkbox value=1 $checked></td>
- </tr>
- |;
- $obsolete = "<input type=hidden name=obsolete value=$form->{obsolete}>" if $form->{project_id};
- }
- # type=submit $locale->text('Edit Part')
- # type=submit $locale->text('Edit Service')
- # type=submit $locale->text('Edit Assembly')
- $form->header;
- print qq|
- <body>
- <form method=post action="$form->{script}">
- |;
- $form->hide_form(qw(id item title makemodel alternate onhand orphaned taxaccounts rowcount baseassembly project_id));
-
- print qq|
- <table width="100%">
- <tr>
- <th class=listtop>$form->{title}</th>
- </tr>
- <tr height="5"></tr>
- <tr>
- <td>
- <table width="100%">
- <tr valign=top>
- <th align=left>|.$locale->text('Number').qq|</th>
- <th align=left>|.$locale->text('Description').qq|</th>
- <th align=left>$group</th>
- </tr>
- <tr valign=top>
- <td><input name=partnumber value="$form->{partnumber}" size=20></td>
|