summaryrefslogtreecommitdiff
path: root/httemplate/edit/did_order.html
blob: 6912117de21e4a5a3ec9d2ed2ebf27ee643e7e63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<% include( 'elements/edit.html',
              'fields' => [
                            { field => 'vendornum',
                              type => 'select-table',
                              name_col => 'vendorname',
                              table => 'did_vendor',
                              disable_empty => 1,
                            },
                            { field => 'vendor_order_id',
                              type => 'hidden',
                            },
                            { field => 'confirmed',
                              type => 'hidden',
                            },
                            { field => 'received',
                              type => 'hidden',
                            },
                            { field => 'submitted',
                              type => 'hidden',
                            },
                            { field => 'stock_or_customer',
                              type => 'radio',
                              options => [ 'Stock', 'Customer', ],
                              onchange => 'stockcust_changed',
                              curr_value_callback => $stock_or_customer,
                            },
                            { field => 'custnum',
                              type => 'search-cust_main',
                            },
                            { type => 'tablebreak-tr-title',
                              value => 'Order Items',
                            },
                            { 'field' => 'orderitemnum',
                              'type' => 'did_order_item',
                              'o2m_table' => 'did_order_item',
                              'm2_label' => 'Item',
                              'm2_error_callback' => $m2_error_callback,
                            },
                        ],
              'labels' => { 
                            'ordernum'        => 'Order',
                            'vendornum'       => 'Vendor',
                            'vendor_order_id' => 'Vendor Order #',
                            'custnum'         => '',
                            'confirmed'       => 'Confirmation Date',
                            'orderitemnum'     => 'Item',
                          },
              'viewall_dir' => 'browse',
              'table' => 'did_order',
              'name' => 'Bulk DID Order',
              'field_callback' => $field_callback,
              'html_bottom' => $javascript,
              'body_etc'        => 'onLoad="stockcust_changed()"',
           )
%>
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Import');

my $field_callback = sub {
  my ($cgi, $object, $field_hashref ) = @_;
  if ($object->ordernum) {
    $field_hashref->{type} = 'text'
        if $field_hashref->{field} eq 'vendor_order_id';
    $field_hashref->{type} = 'input-date-field'
        if $field_hashref->{field} eq 'confirmed';
  }
};

my $m2_error_callback = sub {
  my($cgi, $object) = @_;

  #process_o2m fields in process/did_order.html
  my @fields = qw( msanum npa latanum ratecenternum state quantity );
  my @gfields = ( '', map "_$_", @fields );

  map {
        if ( /^orderitemnum(\d+)$/ ) {
          my $num = $1;
          if ( grep $cgi->param("orderitemnum$num$_"), @gfields ) {
            my $x = new FS::did_order_item {
              'orderitemnum' => scalar($cgi->param("orderitemnum$num")),
              map { $_ => scalar($cgi->param("orderitemnum${num}_$_")) } @fields,
            };
            $x;
          } else {
            ();
          }
        } else {
          ();
        }
      }
      $cgi->param;
};

my $stock_or_customer = sub{
    my($cgi,$object,$field) = @_;
    return 'Customer' if $object->custnum;
    'Stock';
};

my $javascript = <<END;
    <SCRIPT TYPE="text/javascript">
        function stockcust_changed() {
            var f = document.edit_topform;
            var custnum = f.custnum;
            var custnum_search = f.custnum_search;
            var custnum_select = f.custnum_select;
            if ( f.stock_or_customer_Stock.checked ) {
                custnum_search.disabled = true;
                custnum_select.disabled = true;
                custnum.value = '';
            }
            else if ( f.stock_or_customer_Customer.checked ) {
                custnum_search.disabled = false;
                custnum_select.disabled = false;
            }
        }

        /* o2m or something else is broken, can't put this in the actual
            component because random JS and other crap is rendered 
            onto the final output page */
        function ratecenter_changed(rc) {
            var idbase = rc.id.substring(0,rc.id.indexOf('_'));
            var div = document.getElementById(idbase+'_rc_div');
            var input = document.getElementById(idbase+'_rc_new');
            if(rc.options[rc.selectedIndex].value == '0') {
                div.style.display = 'inline';
            }
            else {
                div.style.display = 'none';
            }
            input.value = '';
        }
    </SCRIPT>
END

</%init>