}
}) || die($db->error);
- $self->{contact} = SL::DB::Contact->new();
+ $self->{contact} = $self->_new_contact_object;
}
$self->action_edit();
};
$data->{contact_cvars} = {
- map(
- {
- if ( $_->config->type eq 'number' ) {
- $_->config->name => $::form->format_amount(\%::myconfig, $_->value, -2);
- } else {
- $_->config->name => $_->value;
- }
+ map {
+ my $cvar = $_;
+ my $result = { type => $cvar->config->type };
+
+ if ($cvar->config->type eq 'number') {
+ $result->{value} = $::form->format_amount(\%::myconfig, $cvar->value, -2);
+
+ } elsif ($result->{type} =~ m{customer|vendor|part}) {
+ my $object = $cvar->value;
+ my $method = $result->{type} eq 'part' ? 'description' : 'name';
+
+ $result->{id} = int($cvar->number_value) || undef;
+ $result->{value} = $object ? $object->$method // '' : '';
+
+ } else {
+ $result->{value} = $cvar->value;
}
- grep(
- { $_->is_valid; }
- @{$self->{contact}->cvars_by_config}
- )
- )
+
+ ( $cvar->config->name => $result )
+
+ } grep { $_->is_valid } @{ $self->{contact}->cvars_by_config }
};
$self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 });
$self->{cv} = SL::DB::Customer->new(id => $::form->{cv}->{id})->load();
}
} else {
- if ( $self->is_vendor() ) {
- $self->{cv} = SL::DB::Vendor->new();
- } else {
- $self->{cv} = SL::DB::Customer->new();
- }
+ $self->{cv} = $self->_new_customer_vendor_object;
}
$self->{cv}->assign_attributes(%{$::form->{cv}});
if ( $::form->{contact}->{cp_id} ) {
$self->{contact} = SL::DB::Contact->new(cp_id => $::form->{contact}->{cp_id})->load();
} else {
- $self->{contact} = SL::DB::Contact->new();
+ $self->{contact} = $self->_new_contact_object;
}
$self->{contact}->assign_attributes(%{$::form->{contact}});
die($::locale->text('Error'));
}
} else {
- $self->{contact} = SL::DB::Contact->new();
+ $self->{contact} = $self->_new_contact_object;
}
}
sub _create_customer_vendor {
my ($self) = @_;
- if ( $self->is_vendor() ) {
- $self->{cv} = SL::DB::Vendor->new();
- } else {
- $self->{cv} = SL::DB::Customer->new();
- }
+ $self->{cv} = $self->_new_customer_vendor_object;
$self->{cv}->currency_id($::instance_conf->get_currency_id());
$self->{note} = SL::DB::Note->new();
$self->{shipto} = SL::DB::Shipto->new();
- $self->{contact} = SL::DB::Contact->new();
+ $self->{contact} = $self->_new_contact_object;
}
sub _pre_render {
);
}
+sub _new_customer_vendor_object {
+ my ($self) = @_;
+
+ my $class = 'SL::DB::' . ($self->is_vendor ? 'Vendor' : 'Customer');
+ return $class->new(
+ contacts => [],
+ shipto => [],
+ custom_variables => [],
+ );
+}
+
+sub _new_contact_object {
+ my ($self) = @_;
+
+ return SL::DB::Contact->new(custom_variables => []);
+}
+
1;
require SL::DB::Customer;
my $id = int($self->number_value);
- return $id ? SL::DB::Customer->new(id => $id)->load() : 0;
+ return $id ? SL::DB::Customer->new(id => $id)->load() : undef;
} elsif ( $type eq 'vendor' ) {
require SL::DB::Vendor;
my $id = int($self->number_value);
- return $id ? SL::DB::Vendor->new(id => $id)->load() : 0;
+ return $id ? SL::DB::Vendor->new(id => $id)->load() : undef;
} elsif ( $type eq 'part' ) {
require SL::DB::Part;
my $id = int($self->number_value);
- return $id ? SL::DB::Part->new(id => $id)->load() : 0;
+ return $id ? SL::DB::Part->new(id => $id)->load() : undef;
}
goto &text_value; # text, textfield, date and select
sub customer_vendor_picker {
my ($self, $name, $value, %params) = @_;
- $value = SL::DB::Manager::Customer->find_by(id => $value) if $value && !ref $value;
+ croak 'Unknown "type" parameter' unless $params{type} =~ m{^(?:customer|vendor)$};
+ croak 'Unknown value class' if $value && (ref($value) !~ m{^SL::DB::(?:Customer|Vendor)$});
+
+ if ($value && !ref $value) {
+ my $class = $params{type} eq 'customer' ? 'SL::DB::Manager::Customer' : 'SL::DB::Manager::Vendor';
+ $value = $class->find_by(id => $value);
+ }
+
my $id = delete($params{id}) || $self->name_to_id($name);
my $fat_set_item = delete $params{fat_set_item};
}
};
+ this.setCustomVariablesFromAJAJ = function(cvars) {
+ for (var key in cvars) {
+ var cvar = cvars[key];
+ var $ctrl = $('#contact_cvars_'+ key);
+
+ console.log($ctrl, cvar);
+
+ if (cvar.type == 'bool')
+ $ctrl.prop('checked', cvar.value == 1 ? 'checked' : '');
+
+ else if ((cvar.type == 'customer') || (cvar.type == 'vendor'))
+ kivi.CustomerVendorPicker($ctrl).set_item({ id: cvar.id, name: cvar.value });
+
+ else if (cvar.type == 'part')
+ kivi.PartPicker($ctrl).set_item({ id: cvar.id, name: cvar.value });
+
+ else
+ $ctrl.val(cvar.value);
+ }
+ };
+
this.selectContact = function(params) {
var contactId = $('#contact_cp_id').val();
for(var key in contact)
$(document.getElementById('contact_'+ key)).val(contact[key])
- var cvars = data.contact_cvars;
- for(var key in cvars)
- $(document.getElementById('contact_cvars_'+ key)).val(cvars[key]);
+ kivi.CustomerVendor.setCustomVariablesFromAJAJ(data.contact_cvars);
if ( contactId )
$('#action_delete_contact').show();
[%- ELSIF ( var.config.type == 'timestamp' ) %]
[% L.input_tag(var_name, var.value) %]
[%- ELSIF ( var.config.type == 'customer' ) %]
- [% L.customer_picker(var_name, var.value) %]
+ [% L.customer_vendor_picker(var_name, var.value, type='customer') %]
[%- ELSIF ( var.config.type == 'vendor' ) %]
- [% L.vendor_selector(var_name, var.value) %]
+ [% L.customer_vendor_picker(var_name, var.value, type='vendor') %]
[%- ELSIF ( var.config.type == 'part' ) %]
- [% L.part_selector(var_name, var.value) %]
+ [% L.part_picker(var_name, var.value) %]
[%- ELSIF ( var.config.type == 'select' ) %]
[% L.select_tag(var_name, var.config.processed_options, default = var.value) %]
[%- ELSIF ( var.config.type == 'number' ) %]
<div id="custom_variables">
<p>
<table>
- [% FOREACH var = cv_cvars %]
+ [% FOREACH var = SELF.cv.cvars_by_config %]
<tr>
<th align="left" valign="top" nowrap>[% var.config.description | html %]</th>