summaryrefslogtreecommitdiff
path: root/httemplate/edit/quick-charge.html
blob: 07501a4cf0f075bae1c06d852d36c65d63b9b8ce (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
% if ( $quotationnum && ! $cust_main ) {
<& /elements/header.html, mt('Quotation #[_1]: add one-time charge', $quotationnum) &>
% } else {
<& /elements/header-cust_main.html,
     view      => $quotationnum ? 'quotations' : 'packages',
     cust_main => $cust_main,
     etc       => $cgi->param('error') ? '' : 'onload="addRow()"',
&>
<h2><% $quotationnum
         ? emt('Quotation #[_1]: add one-time charge', $quotationnum)
         : mt('One-time charge')
    %></h2>
% }

<& /elements/init_calendar.html &>
<SCRIPT TYPE="text/javascript" SRC="<%$fsurl%>elements/xregexp-all.js"></SCRIPT>

<& /elements/error.html &>

<SCRIPT TYPE="text/javascript">

function enable_quick_charge (e) {

  if (    document.QuickChargeForm.amount.value
       && document.QuickChargeForm.pkg.value    ) {
    document.QuickChargeForm.submit.disabled = false;
  } else {
    document.QuickChargeForm.submit.disabled = true;
  }

% if ( $curuser->option('disable_enter_submit_onetimecharge') ) {

    var key;
    if (window.event)
          key = window.event.keyCode; //IE
    else

          key = e.which; //firefox, others

    return (key != 13);

% } else {
    return true;
% }

}

function validate_quick_charge () {
  var pkg = document.QuickChargeForm.pkg.value;
  var pkg_regex = XRegExp('^([\\p{L}\\p{N} \_\!\@\#\$\%\&\(\)\+\;\:\'\"\,\.\?\/\=\\-\\[\\]]*)$');
  var amount = document.QuickChargeForm.amount.value;
  var amount_regex = /^\s*\$?\s*(\d*(\.?\d{1,2}))\s*$/ ;
  var rval = true;

  if ( ! amount_regex.test(amount) ) {
    alert('Illegal amount - enter an amount to charge, for example, "5" or "43" or "21.46".');
    return false;
  }
  if ( String(pkg).length < 1 ) {
    rval = false;
  }
  if ( ! pkg_regex.test(pkg) ) {
    rval = false;
  }
  var i=0;
  for (i=0; i < rownum; i++) {
    if (! eval('pkg_regex.test(document.QuickChargeForm.description' + i + '.value)')){
      rval = false;
      break;
    }
  }
  if (rval == true) {
    return true;
  }

  if ( ! pkg ) {
    alert('Enter a description for the one-time charge');
    return false;
  }

  alert('Illegal description - spaces, letters, numbers, and the following punctuation characters are allowed: . , ! ? @ # $ % & ( ) - + ; : ' + "'" + ' " = [ ]' );
  return false;
}

function bill_now_changed (what) {
  var form = what.form;
  if ( what.checked ) {
    form.start_date_text.disabled = true;
    form.start_date.style.backgroundColor = '#dddddd';
    form.start_date_button.style.display = 'none';
    form.start_date_button_disabled.style.display = '';
    form.invoice_terms.disabled = false;
  } else {
    form.start_date_text.disabled = false;
    form.start_date.style.backgroundColor = '#ffffff';
    form.start_date_button.style.display = '';
    form.start_date_button_disabled.style.display = 'none';
    form.invoice_terms.disabled = true;
  }
}

</SCRIPT>

<P>
% if ( $prospect_main ) {
<& /elements/small_prospect_view.html, $prospect_main &>
% }
</P>

<FORM ACTION   = "process/quick-charge.cgi"
      NAME     = "QuickChargeForm"
      ID       = "QuickChargeForm"
      METHOD   = "POST"
      onSubmit = "document.QuickChargeForm.submit.disabled=true; return validate_quick_charge();"
>

<INPUT TYPE="hidden" NAME="custnum"     VALUE="<% $cust_main ? $cust_main->custnum : '' %>">
<INPUT TYPE="hidden" NAME="prospectnum" VALUE="<% $prospect_main ? $prospect_main->prospectnum : '' %>">
<INPUT TYPE="hidden" NAME="quotationnum" VALUE="<% $quotationnum %>">

<TABLE><TR><TD>

<FONT CLASS="fsinnerbox-title"><% mt('Charge') |h %>
<TABLE ID="QuickChargeTable" CLASS="fsinnerbox">

% if ( $cust_pkg ) { #modify one-time charge

<INPUT TYPE="hidden" NAME="pkgnum" VALUE="<% $cust_pkg->pkgnum %>">
% my $field = '/elements/tr-input-text.html';
% # don't allow changing these after the fact
% $field = '/elements/tr-fixed.html' if $billed;
<& $field,
  label  => mt('Amount to charge'),
  field  => 'amount',
  value  => sprintf('%.2f',$part_pkg->option('setup_fee')),
  size   => 8,
  prefix => $money_char,
&> 

% if ( $curuser->access_right('Edit package definition costs') ) {             
  <& $field,                                                                   
    label  => mt('Cost'),                                                      
    field  => 'setup_cost',                                                    
    value  => sprintf('%.2f',$part_pkg->setup_cost),                           
    size   => 8,                                                               
    prefix => $money_char,                                                     
  &>                                                                           
% } 

%   if ( $conf->exists('invoice-unitprice') ) {
<& $field,
  label => 'Quantity',
  field => 'quantity',
  value => $cust_pkg->quantity
&>
%   }

<& /elements/tr-select-pkg_class.html, 'curr_value' => $classnum  &>

% # crudely estimate whether any agent commission credits might exist
%   my @events = grep { $_->part_event->action =~ /credit/ }
%                $cust_pkg->cust_event;
%   if ( scalar @events ) {
<TR><TD></TD>
  <TD><INPUT TYPE="checkbox" NAME="adjust_commission" VALUE="Y" CHECKED>
<% emt('Adjust commission credits if necessary') %>
</TD>
</TR>
%   }

% #display the future or past charge date, but don't allow changes
% # XXX we probably _could_ let as-yet unbilled charges be rescheduled, but
% # there's no compelling need yet
%   if ( $billed ) {
      <& /elements/tr-fixed-date.html,
        label => emt('Billed on'),
        value => $cust_pkg->get('setup')
      &>
%   } else {
      <& /elements/tr-input-date-field.html,
        {
          name    => 'start_date',
          label   => emt('Will be billed'),
          value   => $cust_pkg->get('start_date'),
          format  => $date_format,
          noinit  => 1,
        }
      &>

      <& /elements/tr-checkbox.html,
        label => emt('Invoice this charge separately'),
        field => 'separate_bill',
        value => 'Y',
        curr_value => $cust_pkg->get('separate_bill'),
      &>
      <TR>
        <TH ALIGN="right"><% mt('Tax exempt') |h %> </TH>
        <TD><INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $cgi->param('setuptax') ? 'CHECKED' : '' %>></TD>
      </TR>

      <& /elements/tr-select-taxclass.html, 'curr_value' => $part_pkg->get('taxclass')  &>

      <& /elements/tr-select-taxproduct.html,
        'label' => emt('Tax product'),
        'curr_value' => $part_pkg->get('taxproductnum')
      &>
%   }

  </TABLE>
  </TD></TR></TABLE>

% } else { # new one-time charge

    <TR>
      <TH ALIGN="right"><% mt('Amount to charge') |h %> </TH>
      <TD>
        <% $money_char %><INPUT TYPE       = "text"
                                NAME       = "amount"
                                SIZE       = 8
                                VALUE      = "<% $amount %>"
                                onChange   = "return enable_quick_charge(event)"
                                onKeyPress = "return enable_quick_charge(event)"
                         >
      </TD>
    </TR>

%   if ( $conf->exists('invoice-unitprice') ) {
    <TR>
      <TH ALIGN="right"><% mt('Quantity') |h %> </TH>
      <TD>
        <INPUT TYPE       = "text"
               NAME       = "quantity"
               SIZE       = 4
               VALUE      = "<% $quantity %>"
               onKeyPress = "return enable_quick_charge(event)">
      </TD>
    </TR>
%   }

<& /elements/tr-select-pkg_class.html, 'curr_value' => $classnum  &>

<TR>
  <TH ALIGN="right"><% mt('Description') |h %> </TH>
  <TD>
    <INPUT TYPE       = "text"
           NAME       = "pkg"
           SIZE       = "50"
           MAXLENGTH  = "50"
           VALUE      = "<% $pkg %>"
           onChange   = "return enable_quick_charge(event)"
           onKeyPress = "return enable_quick_charge(event)"
    >
  </TD>
</TR>

% my $row = 0;
% # quotation details are handled by quotation_pkg_detail records, added via link from view/quotation.html
% # the details below get attached to the part_pkg record, and there's no way to edit that from quotations
% unless ($quotationnum) {
<TR>
  <TD></TD>
  <TH ALIGN="left"><% mt('Optional additional description (also printed on invoice):') |h %></TH>
</TR>

%   foreach (@description) {
    <TR>
      <TD></TD>
      <TD>
        <INPUT TYPE       = "text"
               NAME       = "description<% $row %>"
               SIZE       = "60"
               MAXLENGTH  = "65"
               VALUE      = "<% $_ |h %>"
               rownum     = "<% $row %>"
               onKeyPress = "return enable_quick_charge(event)"
               onKeyUp    = "return possiblyAddRow(event)"
        >
      </TD>
    </TR>
%     $row++;
%   }
% }

</TABLE>

</TD>


<SCRIPT TYPE="text/javascript">

  var rownum = <% $row %>;

  function possiblyAddRow(e) {

    if ( ( rownum - this.getAttribute('rownum') ) == 1 ) {
      addRow();
    }

%   if ( $curuser->option('disable_enter_submit_onetimecharge') ) {

      var key;
      if (window.event)
            key = window.event.keyCode; //IE
      else
            key = e.which; //firefox, others

      return (key != 13);

%   } else {
      return true;
%   }

  }

  function addRow() {

    var table = document.getElementById('QuickChargeTable');
    var tablebody = table.getElementsByTagName('tbody').item(0);

    var row = document.createElement('TR');

    var empty_cell = document.createElement('TD');
    row.appendChild(empty_cell);

    var description_cell = document.createElement('TD');

      //var description_input = document.createElement('INPUT');
      var di = document.createElement('INPUT');
      di.setAttribute('name', 'description'+rownum);
      di.setAttribute('id',   'description'+rownum);
      di.setAttribute('size', 60);
      di.setAttribute('maxLength', 65);
      di.setAttribute('rownum',   rownum);
      di.onkeyup = possiblyAddRow;
      di.onkeypress = enable_quick_charge;
      description_cell.appendChild(di);

    row.appendChild(description_cell);

    tablebody.appendChild(row);

    rownum++;

  }

</SCRIPT>


<TD>

  <FONT CLASS="fsinnerbox-title">Taxation</FONT>
  <TABLE CLASS="fsinnerbox">

  <TR>
    <TH ALIGN="right"><% mt('Tax exempt') |h %> </TH>
    <TD><INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y" <% $cgi->param('setuptax') ? 'CHECKED' : '' %>></TD>
  </TR>

  <& /elements/tr-select-taxclass.html, 'curr_value' => scalar($cgi->param('taxclass')) &>

  <& /elements/tr-select-taxproduct.html,
    'label'      => emt('Tax product'),
    'curr_value' => scalar($cgi->param('taxproductnum')),
  &>

  <& /elements/tr-select-taxoverride.html,
    'curr_value' => scalar($cgi->param('tax_override')),
  &>

  </TABLE>

% if ( $curuser->access_right('Edit package definition costs') ) {

    <BR>
    <FONT CLASS="fsinnerbox-title"><% mt('Cost Accounting') |h %>
    <TABLE ID="QuickChargeTable" CLASS="fsinnerbox">

    <& /elements/tr-input-text.html,
         label  => mt('Cost'),
         field  => 'setup_cost',
         value  => $setup_cost,
         size   => 8,
         prefix => $money_char,
    &> 

    </TABLE>

% }

</TD>


</TR></TABLE>


% if ( $curuser->access_right('Discount customer package') ) {
    <BR>
    <FONT CLASS="fsinnerbox-title"><% mt('Discounting') |h %></FONT>
    <TABLE CLASS="fsinnerbox">
      <& /elements/tr-select-pkg-discount.html,
           'setup_label'         => emt('Discount'),
           'disable_recur'       => 1,
           'disable_waive_setup' => 1,
      &>
    </TABLE>
% }


% unless ( $quotationnum ) {

    <BR>
    <FONT CLASS="fsinnerbox-title"><% mt('Invoicing') |h %></FONT>
    <TABLE CLASS="fsinnerbox">

    <TR>
      <TH ALIGN="right"><% mt('Invoice now') |h %></TH>
      <TD>
        <INPUT TYPE  = "checkbox"
               NAME  = "bill_now"
               VALUE = "1"
               <% $cgi->param('bill_now') ? 'CHECKED' : '' %>
               onClick  = "bill_now_changed(this);"
               onChange = "bill_now_changed(this);"
        >
        <FONT SIZE=-1><% mt('with terms') |h %></FONT>
        <& /elements/select-terms.html,
             'curr_value' => scalar($cgi->param('invoice_terms')),
             'disabled'   => ( $cgi->param('bill_now') ? 0 : 1 ),
             'agentnum'   => $cust_or_prospect->agentnum,
        &>
      </TD>
    </TR>

%   # false laziness w/misc/order_pkg.html
    <TR>
      <TH ALIGN="right"><% mt('Charge date') |h %> </TH>
      <TD>
        <INPUT TYPE  = "text"
               NAME  = "start_date"
               SIZE  = 32
               ID    = "start_date_text"
               VALUE = "<% $start_date %>"
               onKeyPress="return enable_quick_charge(event)"
               <% $cgi->param('bill_now')
                    ? 'STYLE = "background-color:#dddddd" DISABLED'
                    : ''
               %>
        >
        <IMG SRC   = "<%$fsurl%>images/calendar.png"
             ID    = "start_date_button"
             TITLE = "<% mt('Select date') |h %>"
             STYLE = "cursor:pointer<% $cgi->param('bill_now') ? ';display:none' : '' %>"
        >
        <IMG SRC   = "<%$fsurl%>images/calendar-disabled.png"
             ID    = "start_date_button_disabled"
             <% $cgi->param('bill_now') ? '' : 'STYLE="display:none"' %>
        >
        <FONT SIZE=-1>(<% mt('leave blank to charge immediately') |h %>)</FONT>
      </TD>
    </TR>

    <SCRIPT TYPE="text/javascript">
      Calendar.setup({
        inputField: "start_date_text",
        ifFormat:   "<% $date_format %>",
        button:     "start_date_button",
        align:      "BR"
      });
    </SCRIPT>

    <& /elements/tr-checkbox.html,
      label => emt('Invoice this charge separately'),
      field => 'separate_bill',
      value => 'Y'
    &>

%   if (  $cust_main->has_cust_payby_auto ) {
%     my $what = lc(FS::payby->shortname($cust_main->payby));
      <TR>
        <TD ALIGN="right"><% mt("Disable automatic $what charge") |h %> </TD>
        <TD COLSPAN=6><INPUT TYPE="checkbox" NAME="no_auto" VALUE="Y"></TD>
      </TR>
%   }

    </TABLE>

% }

% } # if !$cust_pkg

<BR>
% my $label = $cust_pkg
%             ? emt('Modify one-time charge')
%             : emt('Add one-time charge');
<INPUT TYPE="submit" ID="submit" NAME="submit" VALUE="<% $label %>" \
<% ($cgi->param('error') || $cust_pkg) ? '' :' DISABLED' %>>

</FORM>

%my %footer_args = (
%    formvalidation => {
%        QuickChargeForm => {
%            validate_fields => {
%                quantity => 'digits: true, min: 1, required: true',
%            },
%        },
%    },
%);
% if ( $quotationnum ) {
  <& /elements/footer.html, %footer_args &>
% } else {
  <& /elements/footer-cust_main.html, %footer_args &>
% }
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;

die "access denied"
  unless $curuser->access_right('One-time charge');

my $conf = new FS::Conf;
my $date_format = $conf->config('date_format') || '%m/%d/%Y';
my $money_char = $conf->config('money_char') || '$';

my( $cust_main, $cust_pkg, $prospect_main, $quotationnum ) = ( '', '', '', '' );
if ( $cgi->param('change_pkgnum') ) {
  # change an existing one-time charge
  die "access denied"
    unless $curuser->access_right('Modify one-time charge');

  $cgi->param('change_pkgnum') =~ /^(\d+)$/ or die "illegal pkgnum";
  $cust_pkg = FS::cust_pkg->by_key($1) or die "pkgnum $1 not found";
  $cust_main = $cust_pkg->cust_main;
} else {
  if ( $cgi->param('quotationnum') =~ /^(\d+)$/ ) {
    $quotationnum = $1;
  }
  if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
    $cust_main = FS::cust_main->by_key($1) or die "custnum $1 not found";
  }
  if ( $cgi->param('prospectnum') =~ /^(\d+)$/ ) {
    $prospect_main = FS::prospect_main->by_key($1) or die "prospectnum $1 not found";
  }
  die "custnum or prospectnum must be specified"
    unless $cust_main || $prospect_main;
}

my $cust_or_prospect = $cust_main || $prospect_main;

if ( $cust_main ) {
  my $custnum = $cust_main->custnum;
  # agent-virt
  if (!exists($curuser->agentnums_href->{$cust_main->agentnum})) {
    die "custnum $custnum not found";
  }
} elsif ( $prospect_main ) {
  my $prospectnum = $prospect_main->prospectnum;
  # agent-virt
  if (!exists($curuser->agentnums_href->{$prospect_main->agentnum})) {
    die "prospectnum $prospectnum not found";
  }
}

my $format = "%m/%d/%Y %T %z (%Z)"; #false laziness w/REAL_cust_pkg.cgi?
my $start_date = $cust_main ? $cust_main->next_bill_date : '';
$start_date = $start_date ? time2str($format, $start_date) : '';

my $amount = '';
if ( $cgi->param('amount') =~ /^\s*\$?\s*(\d+(\.\d{1,2})?)\s*$/ ) {
  $amount = $1;
}

my $setup_cost = '';
if ( $cgi->param('setup_cost') =~ /^\s*\$?\s*(\d+(\.\d{1,2})?)\s*$/ ) {
  $setup_cost = $1;
}

my $quantity = 1;
if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
  $quantity = $1;
}

$cgi->param('pkg') =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/ 
  or die 'illegal description';
my $pkg = $1;

my $default_terms;
if ( $cust_main && $cust_main->invoice_terms ) {
  $default_terms = emt("Customer default ([_1])", $cust_main->invoice_terms);
} else {
  $default_terms =
    emt( "Default ([_1])",
         ( $conf->config('invoice_default_terms', $cust_or_prospect->agentnum)
             || emt('Payable upon receipt')
         )
       );
}

my @description;
my %param = $cgi->Vars;
for (my $i = 0; exists($param{"description$i"}); $i++) {
  push @description, $param{"description$i"};
}

my $classnum;
if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
  $classnum = $1;
}

my $part_pkg;
my $billed = 0;

if ( $cust_pkg ) { # set defaults
  $part_pkg = $cust_pkg->part_pkg;
  $pkg ||= $part_pkg->pkg;
  $classnum ||= $part_pkg->classnum;
  if (!@description) {
    for (my $i = 0; $i < ($part_pkg->option('additional_count',1) || 0); $i++) 
    {
      push @description, $part_pkg->option("additional_info$i",1);
    }
  }
  $billed = $cust_pkg->get('setup') ? 1 : 0;
}

</%init>