use SL::CVar;
use SL::DBUtils;
+use SL::TransNumber;
use strict;
my $priceupdate = ', priceupdate = current_date';
if ($form->{id}) {
+ my $trans_number = SL::TransNumber->new(type => $form->{item}, dbh => $dbh, number => $form->{partnumber}, id => $form->{id});
+ if (!$trans_number->is_unique) {
+ $::lxdebug->leave_sub;
+ return 3;
+ }
# get old price
$query = qq|SELECT sellprice, weight FROM parts WHERE id = ?|;
$priceupdate = '' if (all { $previous_values->{$_} == $form->{$_} } qw(sellprice lastcost listprice));
} else {
- my ($count) = selectrow_query($form, $dbh, qq|SELECT COUNT(*) FROM parts WHERE partnumber = ?|, $form->{partnumber});
- if ($count) {
- $main::lxdebug->leave_sub();
+ my $trans_number = SL::TransNumber->new(type => $form->{item}, dbh => $dbh, number => $form->{partnumber}, save => 1);
+
+ if ($form->{partnumber} && !$trans_number->is_unique) {
+ $::lxdebug->leave_sub;
return 3;
}
+ $form->{partnumber} = $trans_number->create_unique;
+
($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('id')|);
- do_query($form, $dbh, qq|INSERT INTO parts (id, partnumber, unit) VALUES (?, '', '')|, $form->{id});
+ do_query($form, $dbh, qq|INSERT INTO parts (id, partnumber, unit) VALUES (?, ?, '')|, $form->{id}, $form->{partnumber});
$form->{orphaned} = 1;
- if ($form->{partnumber} eq "" && $form->{"item"} eq "service") {
- $form->{partnumber} = $form->update_defaults($myconfig, "servicenumber");
- }
- if ($form->{partnumber} eq "" && $form->{"item"} ne "service") {
- $form->{partnumber} = $form->update_defaults($myconfig, "articlenumber");
- }
-
}
my $partsgroup_id = 0;
scalar => [ qw(type id number save dbh dbh_provided business_id) ],
);
-my @SUPPORTED_TYPES = qw(invoice credit_note customer vendor sales_delivery_order purchase_delivery_order sales_order purchase_order sales_quotation request_quotation);
+my @SUPPORTED_TYPES = qw(invoice credit_note customer vendor sales_delivery_order purchase_delivery_order sales_order purchase_order sales_quotation request_quotation part service assembly);
sub new {
my $class = shift;
$filters{where} = 'NOT COALESCE(quotation, FALSE)';
$filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
- } else {
+ } elsif ($type =~ /_quotation$/) {
$filters{trans_number} = "quonumber";
$filters{numberfield} = $type eq 'sales_quotation' ? "sqnumber" : "rfqnumber";
$filters{table} = "oe";
$filters{where} = 'COALESCE(quotation, FALSE)';
$filters{where} .= $type =~ /^sales/ ? ' AND (customer_id IS NOT NULL)' : ' AND (vendor_id IS NOT NULL)';
+
+ } elsif ($type =~ /part|service|assembly/) {
+ $filters{trans_number} = "partnumber";
+ $filters{numberfield} = $type eq 'service' ? 'servicenumber' : 'articlenumber';
+ $filters{table} = "parts";
+ $filters{where} = 'COALESCE(inventory_accno_id, 0) ' . ($type eq 'service' ? '=' : '<>') . ' 0';
}
return %filters;
push @values, conv_i($self->id);
}
- my $where_str = @where ? join(' AND ', map { "($_)" } @where) : '';
+ my $where_str = @where ? ' AND ' . join(' AND ', map { "($_)" } @where) : '';
my $query = <<SQL;
SELECT $filters{trans_number}
FROM $filters{table}