};
$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 });
}
};
+ 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();