Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Pman.Dialog.XtupleCreditMemo.js
1 //<script type="text/javascript">
2
3 // Auto generated file - created by app.Builder.js- do not edit directly (at present!)
4
5 Pman.Dialog.XtupleCreditMemo = {
6
7     dialog : false,
8     callback:  false,
9
10     show : function(data, cb)
11     {
12         if (!this.dialog) {
13             this.create();
14         }
15
16         this.callback = cb;
17         this.data = data;
18         this.dialog.show(this.data._el);
19         if (this.form) {
20            this.form.reset();
21            this.form.setValues(data);
22            this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });
23         }
24
25     },
26
27     create : function()
28     {
29         var _this = this;
30         this.dialog = Roo.factory({
31             xtype: 'LayoutDialog',
32             xns: Roo,
33             listeners : {
34                 show : function () {
35                        this.layout.getRegion('center').showPanel(0);
36                 }
37             },
38             closable : true,
39             collapsible : false,
40             height : 450,
41             modal : true,
42             resizable : true,
43             title : "Edit / Create Credit Memo",
44             width : 950,
45             items : [
46                 {
47                     xtype: 'ContentPanel',
48                     xns: Roo,
49                     listeners : {
50                         activate : function (_self)
51                         {
52                             // we need to reload to find out the subtotal.
53                             if (!_this.data || !_this.data.cmhead_id) {
54                                 return;
55                             }
56                             
57                             new Pman.Request({
58                                 method : 'GET',
59                                 url : baseURL + '/Roo/cmhead',
60                                 params : { 
61                                     _id : _this.data.cmhead_id
62                                 },
63                                 success : function(res) {
64                                     // update the items and tax values...?
65                         
66                                     _this.form.findField('cmhead_taxfree_value').setValue(res.data.cmhead_taxfree_value);
67                                     _this.form.findField('cmhead_tax_value').setValue(res.data.cmhead_tax_value * 1);            
68                                     _this.form.findField('cmhead_taxable_value').setValue(res.data.cmhead_taxable_value * 1);  
69                                     _this.form.findField('cmhead_misc_per').update();
70                                     _this.form.findField('cmhead_total').recalc();
71                                 
72                                 }
73                             });
74                         }
75                     },
76                     region : 'center',
77                     title : "Credit Details",
78                     items : [
79                         {
80                             xtype: 'Form',
81                             xns: Roo.form,
82                             listeners : {
83                                 actioncomplete : function(_self,action)
84                                 {
85                                     if (action.type == 'setdata') {
86                                         _this.saveBtn.show();
87                                         
88                                         if (_this.data.cmhead_id) {
89                                            this.load({ method: 'GET', params: { '_id' : _this.data.cmhead_id }});
90                                            
91                                            return;
92                                         }
93                                         _this.dialog.setTitle("Add New Credit Memo");
94                                        
95                                         _this.form.findField('cmhead_docdate').setValue(new Date());
96                                         
97                                        return;
98                                     }
99                                     if (action.type == 'load') {
100                                          
101                                         _this.data = action.result.data;
102                                         
103                                         if(_this.data.cmhead_posted){
104                                             _this.saveBtn.hide();
105                                         }
106                                         _this.dataloading = true;
107                                         if (_this.data.cmhead_shipto_cntct_id == _this.data.cmhead_billto_cntct_id) {
108                                             this.findField('_shipto_same').setValue(1);
109                                                       Roo.log('set shipto 1');
110                                         } else {
111                                 //          this.findField('_shipto_same').setValue(0);
112                                                       Roo.log('set shipto 0');
113                                           }
114                                         _this.dataloading = false;          
115                                         
116                                         
117                                         this.findField('billto_address').update();
118                                 //        this.findField('shipto_address').update();        
119                                         _this.dialog.setTitle("Edit Credit Memo Order - " + this.findField('cmhead_number').getValue());
120                                         
121                                       _this.form.findField('cmhead_misc_per').update();
122                                         _this.form.findField('cmhead_total').recalc();
123                                         return;
124                                     }
125                                     if (action.type =='submit') {
126                                     
127                                 
128                                         var id = _this.form.findField('cmhead_id').getValue() * 1;
129                                         if (id < 1) {
130                                 
131                                             _this.data.cmhead_id = action.result.data.cmhead_id;
132                                              this.load({ method: 'GET', params: { '_id' : _this.data.cmhead_id }});
133                                             return;
134                                         }
135                                     
136                                         _this.dialog.hide();
137                                         
138                                          if (_this.callback) {
139                                             _this.callback.call(_this, _this.form.getValues());
140                                          }
141                                          _this.form.reset();
142                                          return;
143                                     }
144                                 },
145                                 rendered : function (form)
146                                 {
147                                     _this.form= form;
148                                 },
149                                 actionfailed : function (_self, action)
150                                 {
151                                     if (action.failureType == 'client') {
152                                         Roo.MessageBox.alert("Error", "Fill in all the required fields");
153                                     }
154                                     if (action.failureType == 'server') {    
155                                         Roo.log(action);
156                                         Roo.MessageBox.alert("Error", action.result.errorMsg);
157                                     }
158                                     _this.dialog.layout.getRegion('center').showPanel(0);
159                                 }
160                             },
161                             method : 'POST',
162                             style : 'margin:10px;',
163                             url : baseURL + '/Roo/cmhead.php',
164                             items : [
165                                 {
166                                     xtype: 'Column',
167                                     xns: Roo.form,
168                                     width : '435',
169                                     items : [
170                                         {
171                                             xtype: 'FieldSet',
172                                             xns: Roo.form,
173                                             labelWidth : 120,
174                                             legend : "Credit",
175                                             style : 'width:420px',
176                                             items : [
177                                                 {
178                                                     xtype: 'Column',
179                                                     xns: Roo.form,
180                                                     labelWidth : 100,
181                                                     width : 410,
182                                                     items : [
183                                                         {
184                                                             xtype: 'TextField',
185                                                             xns: Roo.form,
186                                                             allowBlank : true,
187                                                             emptyText : "Automatic",
188                                                             fieldLabel : 'Ref #',
189                                                             name : 'cmhead_number',
190                                                             readOnly : true,
191                                                             width : 150
192                                                         },
193                                                         {
194                                                             xtype: 'TextField',
195                                                             xns: Roo.form,
196                                                             editable : false,
197                                                             fieldLabel : 'Customer',
198                                                             name : 'cmhead_cust_id_cust_name',
199                                                             readOnly : true,
200                                                             width : 300
201                                                         },
202                                                         {
203                                                             xtype: 'ComboBox',
204                                                             xns: Roo.form,
205                                                             allowBlank : false,
206                                                             displayField : 'location_name',
207                                                             editable : true,
208                                                             emptyText : "select location",
209                                                             fieldLabel : 'Return stock to',
210                                                             forceSelection : true,
211                                                             hiddenName : 'cmhead_location_id',
212                                                             listWidth : 400,
213                                                             loadingText : "Searching...",
214                                                             minChars : 2,
215                                                             name : 'cmhead_location_id_location_name',
216                                                             pageSize : 200,
217                                                             qtip : "Select terms",
218                                                             queryParam : 'query[location_name]',
219                                                             selectOnFocus : true,
220                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{location_name}</b> {location_descrip}</div>',
221                                                             triggerAction : 'all',
222                                                             typeAhead : true,
223                                                             valueField : 'location_id',
224                                                             width : 300,
225                                                             store : {
226                                                                 xtype: 'Store',
227                                                                 xns: Roo.data,
228                                                                 listeners : {
229                                                                     beforeload : function (_self, o){
230                                                                         o.params = o.params || {};
231                                                                         // set more here
232                                                                          o.params.location_netable = 1;
233                                                                          o.params._notinternalcompany = 1;
234                                                                          o.params.location_restrict = 0;
235                                                                     }
236                                                                 },
237                                                                 remoteSort : true,
238                                                                 sortInfo : { direction : 'ASC', field: 'location_name' },
239                                                                 proxy : {
240                                                                     xtype: 'HttpProxy',
241                                                                     xns: Roo.data,
242                                                                     method : 'GET',
243                                                                     url : baseURL + '/Roo/location.php'
244                                                                 },
245                                                                 reader : {
246                                                                     xtype: 'JsonReader',
247                                                                     xns: Roo.data,
248                                                                     id : 'location_id',
249                                                                     root : 'data',
250                                                                     totalProperty : 'total',
251                                                                     fields : [{'name':'location_id','type':'int'},'location_name']
252                                                                 }
253                                                             }
254                                                         },
255                                                         {
256                                                             xtype: 'DateField',
257                                                             xns: Roo.form,
258                                                             allowBlank : false,
259                                                             fieldLabel : 'Date',
260                                                             format : 'Y-m-d',
261                                                             name : 'cmhead_docdate',
262                                                             width : 100
263                                                         },
264                                                         {
265                                                             xtype: 'ComboBox',
266                                                             xns: Roo.form,
267                                                             displayField : 'salesrep_name',
268                                                             editable : false,
269                                                             emptyText : "Select salesrep",
270                                                             fieldLabel : 'Staff I.C.',
271                                                             forceSelection : true,
272                                                             hiddenName : 'cmhead_salesrep_id',
273                                                             listWidth : 400,
274                                                             loadingText : "Searching...",
275                                                             minChars : 2,
276                                                             name : 'cmhead_salesrep_id_salesrep_name',
277                                                             pageSize : 20,
278                                                             qtip : "Select salesrep",
279                                                             queryParam : 'query[salesrep_name]',
280                                                             selectOnFocus : true,
281                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{salesrep_name}</b> </div>',
282                                                             triggerAction : 'all',
283                                                             typeAhead : true,
284                                                             valueField : 'salesrep_id',
285                                                             width : 300,
286                                                             store : {
287                                                                 xtype: 'Store',
288                                                                 xns: Roo.data,
289                                                                 listeners : {
290                                                                     beforeload : function (_self, o){
291                                                                         o.params = o.params || {};
292                                                                         // set more here
293                                                                     }
294                                                                 },
295                                                                 remoteSort : true,
296                                                                 sortInfo : { direction : 'ASC', field: 'salesrep_name' },
297                                                                 proxy : {
298                                                                     xtype: 'HttpProxy',
299                                                                     xns: Roo.data,
300                                                                     method : 'GET',
301                                                                     url : baseURL + '/Roo/salesrep.php'
302                                                                 },
303                                                                 reader : {
304                                                                     xtype: 'JsonReader',
305                                                                     xns: Roo.data,
306                                                                     id : 'salesrep_id',
307                                                                     root : 'data',
308                                                                     totalProperty : 'total',
309                                                                     fields : [{'name':'salesrep_id','type':'int'},'salesrep_name']
310                                                                 }
311                                                             }
312                                                         },
313                                                         {
314                                                             xtype: 'TextArea',
315                                                             xns: Roo.form,
316                                                             fieldLabel : 'Comments',
317                                                             height : 80,
318                                                             name : 'cmhead_comments',
319                                                             width : 300
320                                                         }
321                                                     ]
322                                                 }
323                                             ]
324                                         }
325                                     ]
326                                 },
327                                 {
328                                     xtype: 'Column',
329                                     xns: Roo.form,
330                                     style : 'margin-left:10px',
331                                     width : 450,
332                                     items : [
333                                         {
334                                             xtype: 'FieldSet',
335                                             xns: Roo.form,
336                                             legend : "Price Details",
337                                             style : 'width:420px',
338                                             labelWidth : '50',
339                                             items : [
340                                                 {
341                                                     xtype: 'Column',
342                                                     xns: Roo.form,
343                                                     labelAlign : 'right',
344                                                     labelWidth : 300,
345                                                     width : 420,
346                                                     items : [
347                                                         {
348                                                             xtype: 'Row',
349                                                             xns: Roo.form,
350                                                             labelAlign : 'right',
351                                                             labelWidth : 300,
352                                                             width : 500,
353                                                             items : [
354                                                                 {
355                                                                     xtype: 'NumberField',
356                                                                     xns: Roo.form,
357                                                                     allowDecimals : true,
358                                                                     cls : 'roo-align-right',
359                                                                     decimalPrecision : 3,
360                                                                     fieldLabel : 'Taxable Value',
361                                                                     name : 'cmhead_taxable_value',
362                                                                     readOnly : true,
363                                                                     width : 100
364                                                                 },
365                                                                 {
366                                                                     xtype: 'NumberField',
367                                                                     xns: Roo.form,
368                                                                     allowDecimals : true,
369                                                                     cls : 'roo-align-right',
370                                                                     decimalPrecision : 3,
371                                                                     fieldLabel : 'Tax Free Value',
372                                                                     name : 'cmhead_taxfree_value',
373                                                                     readOnly : true,
374                                                                     width : 100
375                                                                 }
376                                                             ]
377                                                         },
378                                                         {
379                                                             xtype: 'Row',
380                                                             xns: Roo.form,
381                                                             labelAlign : 'top',
382                                                             labelSeparator : '&nbsp;',
383                                                             width : 500,
384                                                             items : [
385                                                                 {
386                                                                     xtype: 'ComboBox',
387                                                                     xns: Roo.form,
388                                                                     allowBlank : false,
389                                                                     displayField : 'curr_name',
390                                                                     editable : false,
391                                                                     emptyText : "Select Currency",
392                                                                     fieldLabel : 'Currency',
393                                                                     forceSelection : true,
394                                                                     hiddenName : 'cmhead_curr_id',
395                                                                     listWidth : 400,
396                                                                     loadingText : "Searching...",
397                                                                     minChars : 2,
398                                                                     name : 'cmhead_curr_id_curr_name',
399                                                                     pageSize : 20,
400                                                                     qtip : "Select Currency",
401                                                                     queryParam : 'query[curr_name]',
402                                                                     selectOnFocus : true,
403                                                                     tpl : '<div class="x-grid-cell-text x-btn button"><b>{curr_name}</b> </div>',
404                                                                     triggerAction : 'all',
405                                                                     typeAhead : true,
406                                                                     valueField : 'curr_id',
407                                                                     width : 285,
408                                                                     store : {
409                                                                         xtype: 'Store',
410                                                                         xns: Roo.data,
411                                                                         listeners : {
412                                                                             beforeload : function (_self, o){
413                                                                                 o.params = o.params || {};
414                                                                                 // set more here
415                                                                                
416                                                                             }
417                                                                         },
418                                                                         remoteSort : true,
419                                                                         sortInfo : { direction : 'ASC', field: 'curr_symbol' },
420                                                                         proxy : {
421                                                                             xtype: 'HttpProxy',
422                                                                             xns: Roo.data,
423                                                                             method : 'GET',
424                                                                             url : baseURL + '/Roo/curr_symbol.php'
425                                                                         },
426                                                                         reader : {
427                                                                             xtype: 'JsonReader',
428                                                                             xns: Roo.data,
429                                                                             id : 'curr_id',
430                                                                             root : 'data',
431                                                                             totalProperty : 'total',
432                                                                             fields : [{'name':'curr_id','type':'int'},'curr_symbol']
433                                                                         }
434                                                                     }
435                                                                 },
436                                                                 {
437                                                                     xtype: 'NumberField',
438                                                                     xns: Roo.form,
439                                                                     listeners : {
440                                                                         keyup : function (_self, e)
441                                                                         {
442                                                                             _this.form.findField('cmhead_total').recalc();
443                                                                         }
444                                                                     },
445                                                                     allowDecimals : true,
446                                                                     cls : 'roo-align-right',
447                                                                     decimalPrecision : 3,
448                                                                     fieldLabel : 'Freight',
449                                                                     name : 'cmhead_freight',
450                                                                     width : 100
451                                                                 }
452                                                             ]
453                                                         },
454                                                         {
455                                                             xtype: 'Row',
456                                                             xns: Roo.form,
457                                                             labelAlign : 'top',
458                                                             labelSeparator : '&nbsp;',
459                                                             width : 500,
460                                                             items : [
461                                                                 {
462                                                                     xtype: 'TextField',
463                                                                     xns: Roo.form,
464                                                                     fieldLabel : 'Discount Description',
465                                                                     name : 'cmhead_misc_descrip',
466                                                                     width : 230
467                                                                 },
468                                                                 {
469                                                                     xtype: 'NumberField',
470                                                                     xns: Roo.form,
471                                                                     listeners : {
472                                                                         keyup : function (_self, e)
473                                                                         {
474                                                                             var m = _this.form.findField('cmhead_misc');
475                                                                             var pv =  _this.form.findField('cmhead_value').getValue();
476                                                                             var n = this.getValue();
477                                                                             var discount = parseInt(n * pv * 0.01);
478                                                                             m.setValue(discount * -1.000);
479                                                                              
480                                                                             
481                                                                             _this.form.findField('cmhead_total').recalc();
482                                                                             var val = discount * -1.0;
483                                                                              if (val > 0) {
484                                                                                 _this.form.findField('cohead_misc_descrip').setValue("Discount of " + val.toFixed(1)+'%');
485                                                                             }
486                                                                         }
487                                                                     },
488                                                                     allowDecimals : true,
489                                                                     cls : 'roo-align-right',
490                                                                     decimalPrecision : 1,
491                                                                     fieldLabel : '%',
492                                                                     name : 'cmhead_misc_per',
493                                                                     width : 35,
494                                                                     update : function() {
495                                                                         var m = _this.form.findField('cmhead_misc_per');
496                                                                         var pv =  parseFloat(_this.form.findField('cmhead_taxfree_value').getValue()) + 
497                                                                                 parseFloat(_this.form.findField('cmhead_taxable_value').getValue()) ;
498                                                                         var discount = parseFloat(_this.form.findField('cmhead_misc').getValue());
499                                                                         
500                                                                         if (discount > 0.0) {
501                                                                             this.setValue(0);        
502                                                                             return;
503                                                                         }
504                                                                         if (pv < 0) {
505                                                                             this.setValue(0);        
506                                                                             return;
507                                                                         }
508                                                                         var val = ((discount) / pv) * -100;
509                                                                         
510                                                                         //Roo.log("update discount?" + val);
511                                                                         this.setValue(val.toFixed(1));
512                                                                         
513                                                                          if (val > 0.0) {
514                                                                             _this.form.findField('cmhead_misc_descrip').setValue("Discount of " + val.toFixed(1)+'%');
515                                                                         
516                                                                         }
517                                                                         
518                                                                        
519                                                                     }
520                                                                 },
521                                                                 {
522                                                                     xtype: 'NumberField',
523                                                                     xns: Roo.form,
524                                                                     listeners : {
525                                                                         keyup : function (_self, e)
526                                                                         {
527                                                                            _this.form.findField('cmhead_misc_per').update();
528                                                                             _this.form.findField('cmhead_total').recalc();
529                                                                         }
530                                                                     },
531                                                                     allowDecimals : true,
532                                                                     cls : 'roo-align-right',
533                                                                     decimalPrecision : 3,
534                                                                     fieldLabel : '&nbsp;',
535                                                                     name : 'cmhead_misc',
536                                                                     width : 100
537                                                                 }
538                                                             ]
539                                                         },
540                                                         {
541                                                             xtype: 'Row',
542                                                             xns: Roo.form,
543                                                             labelAlign : 'top',
544                                                             labelSeparator : '&nbsp;',
545                                                             width : 500,
546                                                             items : [
547                                                                 {
548                                                                     xtype: 'ComboBox',
549                                                                     xns: Roo.form,
550                                                                     listeners : {
551                                                                         select : function (combo, record, index)
552                                                                         {
553                                                                             _this.form.findField('cmhead_tax_value').setValue(
554                                                                                 record.data.taxzone_rate * _this.form.findField('cmhead_taxable_value').getValue()
555                                                                             );
556                                                                         }
557                                                                     },
558                                                                     allowBlank : false,
559                                                                     displayField : 'taxzone_descrip',
560                                                                     editable : false,
561                                                                     emptyText : "Select tax zone",
562                                                                     fieldLabel : 'Tax Zone',
563                                                                     forceSelection : true,
564                                                                     hiddenName : 'cmhead_taxzone_id',
565                                                                     listWidth : 400,
566                                                                     loadingText : "Searching...",
567                                                                     minChars : 2,
568                                                                     name : 'cmhead_taxzone_id_taxzone_descrip',
569                                                                     pageSize : 20,
570                                                                     qtip : "Select tax zone",
571                                                                     queryParam : 'query[taxzone_descrip]',
572                                                                     selectOnFocus : true,
573                                                                     tpl : '<div class="x-grid-cell-text x-btn button"><b>{taxzone_descrip}</b> </div>',
574                                                                     triggerAction : 'all',
575                                                                     typeAhead : true,
576                                                                     valueField : 'taxzone_id',
577                                                                     width : 285,
578                                                                     store : {
579                                                                         xtype: 'Store',
580                                                                         xns: Roo.data,
581                                                                         listeners : {
582                                                                             beforeload : function (_self, o){
583                                                                                 o.params = o.params || {};
584                                                                                 // set more here
585                                                                                 
586                                                                                 o.params.with_date = _this.form.findField('cmhead_docdate').getValue().format('Y-m-d');
587                                                                                 Roo.log("with date?" + o.params.with_date);
588                                                                                 
589                                                                                 
590                                                                             }
591                                                                         },
592                                                                         remoteSort : true,
593                                                                         sortInfo : { direction : 'ASC', field: 'taxzone_descrip' },
594                                                                         proxy : {
595                                                                             xtype: 'HttpProxy',
596                                                                             xns: Roo.data,
597                                                                             method : 'GET',
598                                                                             url : baseURL + '/Roo/taxzone.php'
599                                                                         },
600                                                                         reader : {
601                                                                             xtype: 'JsonReader',
602                                                                             xns: Roo.data,
603                                                                             id : 'taxzone_id',
604                                                                             root : 'data',
605                                                                             totalProperty : 'total',
606                                                                             fields : [{'name':'taxzone_id','type':'int'},'taxzone_descrip']
607                                                                         }
608                                                                     }
609                                                                 },
610                                                                 {
611                                                                     xtype: 'NumberField',
612                                                                     xns: Roo.form,
613                                                                     allowDecimals : true,
614                                                                     cls : 'roo-align-right',
615                                                                     decimalPrecision : 3,
616                                                                     fieldLabel : 'Tax',
617                                                                     name : 'cmhead_tax_value',
618                                                                     readOnly : true,
619                                                                     width : 100
620                                                                 }
621                                                             ]
622                                                         },
623                                                         {
624                                                             xtype: 'Row',
625                                                             xns: Roo.form,
626                                                             labelAlign : 'right',
627                                                             labelWidth : 300,
628                                                             width : 500,
629                                                             items : [
630                                                                 {
631                                                                     xtype: 'NumberField',
632                                                                     xns: Roo.form,
633                                                                     allowDecimals : true,
634                                                                     cls : 'roo-align-right',
635                                                                     decimalPrecision : 3,
636                                                                     fieldLabel : 'Total',
637                                                                     name : 'cmhead_total',
638                                                                     readOnly : true,
639                                                                     width : 100,
640                                                                     recalc : function() {
641                                                                         var vals = _this.form.getValues();
642                                                                         this.setValue(
643                                                                             (1*vals.cmhead_taxable_value) + 
644                                                                             (1*vals.cmhead_taxfree_value) + 
645                                                                             (1*vals.cmhead_freight) + 
646                                                                             (1*vals.cmhead_tax_value) + 
647                                                                             (1*vals.cmhead_misc)
648                                                                         );
649                                                                     }
650                                                                 }
651                                                             ]
652                                                         }
653                                                     ]
654                                                 }
655                                             ]
656                                         },
657                                         {
658                                             xtype: 'FieldSet',
659                                             xns: Roo.form,
660                                             legend : "Billing",
661                                             style : 'width:420px',
662                                             items : [
663                                                 {
664                                                     xtype: 'Column',
665                                                     xns: Roo.form,
666                                                     width : '420',
667                                                     labelWidth : '50',
668                                                     items : [
669                                                         {
670                                                             xtype: 'ComboBox',
671                                                             xns: Roo.form,
672                                                             listeners : {
673                                                                 beforeselect : function (combo, record, index)
674                                                                 {
675                                                                     // set _this.data values ..
676                                                                     
677                                                                     // just add everything...
678                                                                     for(var i in record.data) {
679                                                                       //  Roo.log('cmhead_billto_cntct_id_' + i +' ='  + record.data[i]);
680                                                                         _this.data['cmhead_billto_cntct_id_' + i] = record.data[i];
681                                                                     }
682                                                                 
683                                                                     _this.form.findField('billto_address').update();
684                                                                     
685                                                                  
686                                                                 },
687                                                                 add : function (combo)
688                                                                 {
689                                                                   
690                                                                   Pman.Dialog.XtupleQuickContact.show( 
691                                                                             {
692                                                                               _id : id,
693                                                                               customer_id : _this.form.findField('cmhead_cust_id').getValue()
694                                                                             },
695                                                                             
696                                                                             function (data) {
697                                                                                
698                                                                                 for(var i in  data) {
699                                                                                     
700                                                                                     _this.data['cmhead_billto_cntct_id_' + i] =  data[i];
701                                                                                 }
702                                                                                 
703                                                                                 _this.form.findField('billto_address').update();
704                                                                                 // fill in the select box..
705                                                                                 _this.form.setValues( {
706                                                                                     cmhead_billto_cntct_id : data.cntct_id,
707                                                                                     cmhead_billto_cntct_id_cntct_name : data.cntct_first_name + ' '+ 
708                                                                                             data.cntct_last_name
709                                                                                     
710                                                                                 });
711                                                                             }
712                                                                         );
713                                                                         //  Pman.Dialog.XtupleCustomer.show(
714                                                                             //{ cust_id : _this.form.findField('cohead_cust_id').getValue() }, 
715                                                                             //function(data) {
716                                                                         // refresh the data in the pulldown..
717                                                                     //    }); 
718                                                                 
719                                                                 }
720                                                             },
721                                                             allowBlank : false,
722                                                             alwaysQuery : true,
723                                                             displayField : 'cntct_name',
724                                                             editable : true,
725                                                             emptyText : "Select Contact",
726                                                             fieldLabel : 'Bill To (select)',
727                                                             forceSelection : true,
728                                                             hiddenName : 'cmhead_billto_cntct_id',
729                                                             listWidth : 400,
730                                                             loadingText : "Searching...",
731                                                             minChars : 2,
732                                                             name : 'cmhead_billto_cntct_id_cntct_name',
733                                                             pageSize : 20,
734                                                             qtip : "Select Contact",
735                                                             queryParam : 'query[cntct_name]',
736                                                             selectOnFocus : true,
737                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{cntct_name}</b> {cntct_addr_id_addr_line1}</div>',
738                                                             triggerAction : 'all',
739                                                             typeAhead : true,
740                                                             valueField : 'cntct_id',
741                                                             width : 300,
742                                                             store : {
743                                                                 xtype: 'Store',
744                                                                 xns: Roo.data,
745                                                                 listeners : {
746                                                                     beforeload : function (_self, o){
747                                                                         o.params = o.params || {};
748                                                                         // set more here
749                                                                         o.params._customer_id = _this.data.cmhead_cust_id;
750                                                                     }
751                                                                 },
752                                                                 remoteSort : true,
753                                                                 sortInfo : { direction : 'ASC', field: 'cntct_name' },
754                                                                 proxy : {
755                                                                     xtype: 'HttpProxy',
756                                                                     xns: Roo.data,
757                                                                     method : 'GET',
758                                                                     url : baseURL + '/Roo/cntct.php'
759                                                                 },
760                                                                 reader : {
761                                                                     xtype: 'JsonReader',
762                                                                     xns: Roo.data,
763                                                                     id : 'cntct_id',
764                                                                     root : 'data',
765                                                                     totalProperty : 'total',
766                                                                     fields : [{'name':'cntct_id','type':'int'},'cntct_name']
767                                                                 }
768                                                             }
769                                                         },
770                                                         {
771                                                             xtype: 'TextArea',
772                                                             xns: Roo.form,
773                                                             listeners : {
774                                                                 render : function (_self)
775                                                                 {
776                                                                    Roo.log(this.el)
777                                                                    
778                                                                    
779                                                                    
780                                                                    this.el.on('click', function() { 
781                                                                        var id = _this.form.findField('cmhead_billto_cntct_id').getValue();
782                                                                         Pman.Dialog.XtupleQuickContact.show( 
783                                                                             {
784                                                                               _id : id,
785                                                                               customer_id : _this.form.findField('cmhead_cust_id').getValue()
786                                                                             },
787                                                                             
788                                                                             function (data) {
789                                                                             
790                                                                                 for(var i in  data) {
791                                                                                     
792                                                                                     _this.data['cmhead_billto_cntct_id_' + i] =  data[i];
793                                                                                 }
794                                                                                 
795                                                                                 _this.form.findField('billto_address').update();
796                                                                                 // fill in the select box..
797                                                                                 _this.form.setValues( {
798                                                                                     cmhead_billto_cntct_id : data.cntct_id,
799                                                                                     cmhead_billto_cntct_id_cntct_name : data.cntct_first_name + ' '+ 
800                                                                                             data.cntct_last_name
801                                                                                 });
802                                                                             }
803                                                                         );
804                                                                      });
805                                                                 }
806                                                             },
807                                                             fieldLabel : 'or enter Address',
808                                                             name : 'billto_address',
809                                                             readOnly : true,
810                                                             update : function() {
811                                                             
812                                                                 var c = ['first_name', 'last_name' ] ;
813                                                                 var a = [ 'line1', 'line2', 'line3', 'city', 'state', 'country' ];
814                                                                 var v = [];
815                                                                 Roo.each(c, function(e) {
816                                                                     if (_this.data['cmhead_billto_cntct_id_cntct_' +e] &&
817                                                                         _this.data['cmhead_billto_cntct_id_cntct_' +e].length) {
818                                                                         v.push(_this.data['cmhead_billto_cntct_id_cntct_' +e]);
819                                                                     }
820                                                                 });
821                                                                 Roo.each(a, function(e) {
822                                                                     if (_this.data['cmhead_billto_cntct_id_cntct_addr_id_addr_' +e] &&
823                                                                         _this.data['cmhead_billto_cntct_id_cntct_addr_id_addr_' +e].length) {
824                                                                         v.push(_this.data['cmhead_billto_cntct_id_cntct_addr_id_addr_' +e]);
825                                                                     }
826                                                                 });
827                                                                 this.setValue(v.join("\n"));
828                                                             }
829                                                         }
830                                                     ]
831                                                 }
832                                             ]
833                                         }
834                                     ]
835                                 },
836                                 {
837                                     xtype: 'Hidden',
838                                     xns: Roo.form,
839                                     name : 'cmhead_id'
840                                 },
841                                 {
842                                     xtype: 'Hidden',
843                                     xns: Roo.form,
844                                     name : 'cmhead_billto_addr_id'
845                                 },
846                                 {
847                                     xtype: 'Hidden',
848                                     xns: Roo.form,
849                                     name : 'cmhead_posted'
850                                 },
851                                 {
852                                     xtype: 'Hidden',
853                                     xns: Roo.form,
854                                     name : 'cmhead_cust_id'
855                                 },
856                                 {
857                                     xtype: 'Hidden',
858                                     xns: Roo.form,
859                                     name : 'taxzone_rate'
860                                 },
861                                 {
862                                     xtype: 'Hidden',
863                                     xns: Roo.form,
864                                     name : 'has_item'
865                                 }
866                             ]
867                         }
868                     ]
869                 },
870                 {
871                     xtype: 'GridPanel',
872                     xns: Roo,
873                     listeners : {
874                         deactivate : function (_self)
875                         {
876                             if(_this.grid){
877                                 _this.grid.stopEditing();
878                             }
879                         },
880                         activate : function (_self)
881                         {
882                         
883                             _this.panel = this;
884                             
885                             try { if (MODULE.isBuilder) {
886                                 return;
887                             } } catch(e) { }
888                             
889                             var id = _this.form.findField('cmhead_id').getValue() * 1;
890                             if (id < 1) {
891                                 Roo.MessageBox.alert("Save First", "Save the order first, before adding items");
892                                 _this.dialog.layout.getRegion('center').showPanel(0);
893                                 return;
894                             }
895                             if (_this.grid) {
896                                 _this.grid.ds.load({});
897                             }
898                         
899                         }
900                     },
901                     background : true,
902                     fitContainer : true,
903                     fitToframe : true,
904                     region : 'center',
905                     tableName : 'coitem',
906                     title : "Credit Items",
907                     grid : {
908                         xtype: 'EditorGrid',
909                         xns: Roo.grid,
910                         listeners : {
911                             render : function() 
912                             {
913                                 _this.grid = this; 
914                                 _this.hasQuery = 0;
915                                 //_this.dialog = Pman.Dialog.FILL_IN
916                                 if (_this.panel.active) {
917                                    this.ds.load({});
918                                 }
919                             },
920                             afteredit : function (e)
921                             {
922                                 //Roo.log('afteredit');
923                                 //Roo.log(e);
924                                 
925                                 if (e.field == 'item_number' || e.originalValue == e.value) {
926                                     // afterselect handles this...
927                                     return;
928                                 }
929                                 var r = e.record;
930                                 
931                                 if ( r && r.data.cmitem_id) {
932                                     // as we disable update to the display on the ajax callback to 
933                                     // allow editing flow to continue, and not refresh - we can only update
934                                     // these values after something has actually been edited.
935                                      r.set('coitem_id', r.data.cmitem_id);
936                                 }
937                                 
938                                 var rate = 0;
939                                 if(r.data.cmitem_taxtype_id_taxtype_name == 'Taxable' ){
940                                     rate = _this.form.findField('taxzone_rate').getValue();
941                                 }
942                                 if(e.field == 'cmitem_tax_unitprice'){
943                                     r.set('cmitem_unitprice', r.data.cmitem_tax_unitprice * 1 / (1 + rate * 1));
944                             //        fields.push('cmitem_unitprice');
945                                 }
946                                 if(e.field == 'cmitem_unitprice'){
947                                     r.set('cmitem_tax_unitprice', r.data.cmitem_unitprice * (1 + rate * 1) );
948                             //        fields.push('cmitem_tax_unitprice');
949                                 }
950                                 if(e.field == 'cmitem_taxtype_id'){
951                                     r.set('cmitem_tax_unitprice', r.data.cmitem_unitprice * (1 + rate * 1) );
952                             //        fields.push('cmitem_tax_unitprice');
953                                             
954                                 }
955                                 r.set('cmitem_tax_listprice', r.data.cmitem_item_listprice * (1 + rate * 1) );
956                             //    fields.push('cmitem_tax_listprice');
957                                 r.set('cmitem_line_value', r.data.cmitem_qtycredit * r.data.cmitem_unitprice );
958                             //    fields.push('cmitem_line_value');
959                                 r.set('cmitem_line_tax_value', r.data.cmitem_qtycredit * r.data.cmitem_tax_unitprice );
960                             //    fields.push('cmitem_line_tax_value'); 
961                                 
962                             //    fields.push('cmitem_id');
963                                 
964                                 _this.hasQuery += 1;
965                                 
966                                  var doupdate = function() { 
967                                    if (!_this.itemsUpdating) {
968                                         Roo.log('doupdate...');
969                                         
970                                         _this.hasQuery -= 1;
971                                         r.commit();
972                                         return;
973                                     }
974                                     doupdate.defer(1000);
975                                 }
976                                 
977                                 doupdate();
978                                 
979                                 
980                             },
981                             beforeedit : function (e)
982                             {
983                                 // we can only edit if nothing is assigned to shipping or invoices..
984                                 if (_this.form.findField('cmhead_posted').getValue() == 'true') {
985                                     Roo.MessageBox.alert("Error", "credit memo is already posted");
986                                     e.cancel = true;
987                                     return;
988                                 }
989                                 
990                                 return;
991                                 
992                                 // seems below logic is useless...!!!!
993                                 var rec = e.record;
994                             
995                                 if (rec.data.coitem_qtyshipped > 0 || rec.data.cobill_billed > 0) {
996                                     Roo.MessageBox.alert("Error", "That item has been shipped or invoices - void the shipments/invoices first");
997                                     e.cancel = true;
998                                     return;
999                                 }
1000                                 
1001                                 if (rec.data.coitem_subnumber * 1 > 0) {
1002                                     Roo.log("Edit container event");
1003                                     Roo.log(e); // if it's a tab.. 
1004                             
1005                                     
1006                                     switch(e.field) {
1007                                         // allow editing of source / destination..
1008                                         case 'coitem_shipto_id':
1009                                         case 'coitem_location_src':            
1010                                             return;
1011                                         default : 
1012                                             break;
1013                                     }
1014                                     Roo.MessageBox.alert("Error", "That is a kit item, edit the container.");
1015                                     e.cancel = true;
1016                                     return;
1017                                 }
1018                                 // zero off values..
1019                                 //if (e.field == 'coitem_qtyord' && rec.data.coitem_qtyord == 0) {
1020                                 //        e.value ='';
1021                                 //    }
1022                                 //    if (e.field == 'coitem_custprice' && rec.data.coitem_qtyord == 0.0) {
1023                                 //        e.value ='';
1024                                 //    }
1025                                 
1026                                 if (rec.data.item_type == 'K' && e.field == 'item_number') {
1027                                     // you can not change the product type on kits' as it messing things up..
1028                                     Roo.MessageBox.alert("Error", "That is a kit item,if you need to change it, delete it first.");
1029                                     e.cancel = true;
1030                                     return;
1031                                 }
1032                                 
1033                             },
1034                             celldblclick : function (_self, rowIndex, columnIndex, e)
1035                             {
1036                                 var rec = this.ds.getAt(rowIndex);
1037                                 var di = this.cm.getDataIndex(columnIndex);
1038                                 if (di != 'avail_qty') {
1039                                     return;
1040                                 }
1041                                 Pman.Dialog.XtupleInvHistory.show({
1042                                     itemsite_item_id_item_number   : rec.data.item_number,
1043                                    // itemsite_item_id_item_descript1 : rec.data.item_descrip1,
1044                                     location_name : rec.data.coitem_location_src_location_name,
1045                                     location_descrip : rec.data.coitem_location_src_location_descrip,
1046                                     
1047                                     invhist_transdate : _this.form.findField('cohead_targetdate').getValue() 
1048                                 }); 
1049                                 
1050                             },
1051                             rowclass : function (gridview, rowcfg)
1052                             {
1053                                 if (rowcfg.record.data.coitem_status == 'C' &&
1054                                     rowcfg.record.data.shipitem_shipped * 1 < 1) {
1055                                     
1056                                     rowcfg.rowClass = 'strikethrough';
1057                                 }
1058                                  if (rowcfg.record.data.coitem_status == 'X'  ) {
1059                                     
1060                                     rowcfg.rowClass = 'strikethrough';
1061                                 }
1062                                // Roo.log(rowcfg);
1063                             //    shipitem_shipped
1064                             }
1065                         },
1066                         autoExpandColumn : 'cmitem_comments',
1067                         clicksToEdit : 1,
1068                         loadMask : true,
1069                         loadAvail : function() {
1070                             
1071                             return; //not used.
1072                                
1073                              var q = [];
1074                             this.ds.each(  function(r) {
1075                             
1076                                 // only update if we do not have the details.
1077                                 if (r.data.avail_qty == 0) {
1078                                 
1079                                 }
1080                                 q.push( { 
1081                                     item : r.data.item_number, 
1082                                     loc: r.data.coitem_location_src_location_name,
1083                                     id: r.data.coitem_linenumber + (r.data.coitem_subnumber ? ('.' + r.data.coitem_subnumber) : '')
1084                                  } );
1085                             });
1086                             
1087                             // needs to be a post to allow long lists of products..
1088                             
1089                             new Pman.Request({
1090                                 url : baseURL + '/Roo/itemloc',
1091                                 method : 'POST',
1092                                 mask : "Loading available qty",
1093                                 maskel : _this.grid.view.el,
1094                                 params : {
1095                                     _availqty : Roo.encode(q),
1096                                     curr_id : _this.form.findField('cohead_curr_id').getValue()
1097                                 },
1098                                 success : function(d) 
1099                                 {
1100                                     _this.grid.ds.each(function(r) { 
1101                                          var id = r.data.coitem_linenumber + (r.data.coitem_subnumber ? ('.' + r.data.coitem_subnumber) : '');
1102                                     
1103                                     
1104                                         if (typeof(d.data[id]) == 'undefined') {
1105                                             return;
1106                                         }
1107                                         r.set('avail_qty', d.data[id].qty);
1108                                         if (r.data.coitem_unitcost_in_order_cur * 1.0 < 0.1) {
1109                                             r.set('coitem_unitcost_in_order_cur', d.data[id].unitcost);
1110                                         }
1111                                     
1112                                     });
1113                                 
1114                                 }
1115                             });
1116                             
1117                                 
1118                                         
1119                         },
1120                         sm : {
1121                             xtype: 'CellSelectionModel',
1122                             xns: Roo.grid,
1123                             listeners : {
1124                                 tabend : function (_self)
1125                                 {
1126                                     _this.addItemBtn.fireEvent('click', _this.addItemBtn);
1127                                 },
1128                                 beforeeditnext : function (eventdata)
1129                                 {
1130                                     return;
1131                                     // this does not work, as the reload effect cancels editng.
1132                                     var rec = _this.grid.ds.getAt(eventdata.cell[0]);
1133                                     if (rec.data.coitem_subnumber *1 < 0 ) {
1134                                         return;
1135                                     }
1136                                     var r = eventdata.cell[0] + 1;
1137                                 
1138                                     while (true) {
1139                                         if (r > _this.grid.ds.getCount()-1 ) {
1140                                             eventdata.cell = false;
1141                                             return;
1142                                         }
1143                                         rec =  _this.grid.ds.getAt(r);
1144                                         if (rec.data.coitem_subnumber *1 < 0 ) {
1145                                            eventdata.cell = [ r, eventdata.cell[1] ];
1146                                            return;
1147                                         }
1148                                         r++;
1149                                     }
1150                                     
1151                                     
1152                                  
1153                                 
1154                                 }
1155                             },
1156                             enter_is_tab : true
1157                         },
1158                         dataSource : {
1159                             xtype: 'Store',
1160                             xns: Roo.data,
1161                             listeners : {
1162                                 beforeload : function (_self,o) {
1163                                 
1164                                     try {
1165                                        this.removeAll();
1166                                    } catch (e) { }
1167                                    
1168                                     Roo.log(_this.data);
1169                                     if (!_this.data || !_this.data.cmhead_id) {
1170                                         return false;
1171                                     }
1172                                     o.params = o.params || {};
1173                                     Roo.log(_this.data);
1174                                     o.params.cmitem_cmhead_id = _this.data.cmhead_id;
1175                                     o.params.limit = 999;
1176                                     
1177                                 },
1178                                 update : function (_self, rec, operation)
1179                                 {
1180                                    
1181                                    if (operation !=  Roo.data.Record.COMMIT) {
1182                                        return;
1183                                    }
1184                                    
1185                                    // row has been updated..
1186                                    // if the qty + item has been filled in, we should try and save it..
1187                                     if (!(rec.data.cmitem_itemsite_id * 1) || !(rec.data.cmitem_qtycredit*1) || !(rec.data.cmitem_unitprice*1)) {
1188                                         Roo.log('not saving - row not completed');
1189                                         return;
1190                                     }
1191                                 
1192                                     if (_this.itemsUpdating) {
1193                                         Roo.log('currently updating?');
1194                                         return;
1195                                     }
1196                                     _this.itemsUpdating = true;
1197                                 
1198                                 
1199                                     Roo.log('Running update');   
1200                                     
1201                                     new Pman.Request({
1202                                         url : baseURL+'/Roo/Cmitem',
1203                                         method : 'POST',
1204                                         
1205                                         params : rec.data,
1206                                         success: function(res)
1207                                         {
1208                                             Roo.log('GOT success');
1209                                             // update the data...
1210                                             if (rec.data.item_type == 'K') {
1211                                                  _this.itemsUpdating = false; 
1212                                                 _this.grid.ds.load({});
1213                                                 return;
1214                                             }
1215                                             if (_this.grid.activeEditor) {
1216                                                  rec.editing = true;
1217                                              } 
1218                                             rec.set('cmitem_id', res.data.cmitem_id);
1219                                             
1220                                             _this.itemsUpdating = false; 
1221                                             
1222                                             if(_this.hasQuery != 0){
1223                                                  
1224                                                 return;
1225                                             }
1226                                             
1227                                             rec.dirty = false;
1228                                             delete rec.modified;
1229                                             
1230                                             _this.grid.loadAvail();
1231                                             
1232                                         },
1233                                         failure : function(res)
1234                                         {
1235                                             Roo.MessageBox.alert(res.message);
1236                                             _this.itemsUpdating = false;
1237                                         }
1238                                         
1239                                         
1240                                     });
1241                                     
1242                                     
1243                                    
1244                                    
1245                                    
1246                                 },
1247                                 load : function (_self, records, options)
1248                                 {
1249                                     // need to fetch availablity from master data..
1250                                     // build a list of what to ask..
1251                                     Roo.log(records);
1252                                     // query: ITEM CODE - LOCATION
1253                                     
1254                                     //_this.grid.loadAvail.defer(100, _this.grid);
1255                                     var rate = 0;
1256                                         
1257                                     Roo.each(records, function(r){
1258                                         rate = 0;
1259                                         if(r.data.cmitem_taxtype_id == r.data.cmitem_taxable_id ){
1260                                             rate = _this.form.findField('taxzone_rate').getValue();
1261                                         }
1262                                         r.set('cmitem_tax_unitprice', r.data.cmitem_unitprice * (1 + rate * 1));
1263                                         r.set('cmitem_tax_listprice', r.data.cmitem_item_listprice * (1 + rate * 1));
1264                                         r.set('cmitem_line_tax_value', r.data.cmitem_line_value * (1 + rate * 1));
1265                                     })    
1266                                     
1267                                 }
1268                             },
1269                             remoteSort : true,
1270                             sortInfo : { field : 'cmitem_linenumber', direction: 'ASC' },
1271                             proxy : {
1272                                 xtype: 'HttpProxy',
1273                                 xns: Roo.data,
1274                                 method : 'GET',
1275                                 url : baseURL + '/Roo/Cmitem.php'
1276                             },
1277                             reader : {
1278                                 xtype: 'JsonReader',
1279                                 xns: Roo.data,
1280                                 totalProperty : 'total',
1281                                 root : 'data',
1282                                 id : 'id',
1283                                 fields : [
1284                                     {
1285                                         'name': 'coitem_linenumber',
1286                                         'type': 'int'
1287                                     },
1288                                     {
1289                                         'name': 'coitem_itemsite_id',
1290                                         'type': 'int'
1291                                     },
1292                                     {
1293                                         'name': 'coitem_qtyord'
1294                                     },
1295                                     {
1296                                         'name': 'coitem_unitcost'
1297                                     },
1298                                     {
1299                                         'name': 'coitem_price'
1300                                     },
1301                                     {
1302                                         'name': 'coitem_custprice'
1303                                     },
1304                                     {
1305                                         'name': 'coitem_qtyreturned'
1306                                     },
1307                                     {
1308                                         'name': 'coitem_prcost'
1309                                     },
1310                                     {
1311                                         'name': 'coitem_price_uom_id',
1312                                         'type': 'int'
1313                                     },
1314                                     {
1315                                         'name': 'coitem_qtyreserved'
1316                                     }
1317                                 ]
1318                             }
1319                         },
1320                         toolbar : {
1321                             xtype: 'Toolbar',
1322                             xns: Roo,
1323                             items : [
1324                                 {
1325                                     xtype: 'Button',
1326                                     xns: Roo.Toolbar,
1327                                     listeners : {
1328                                         click : function()
1329                                         {
1330                                             
1331                                             
1332                                             if (_this.form.findField('cmhead_posted').getValue() == 'true') {
1333                                                 Roo.MessageBox.alert("Error", "credit memo is already posted");
1334                                                 return;
1335                                             }
1336                                             // work out last 
1337                                                // work out last 
1338                                                         var grid = _this.grid;
1339                                                         var last = 0;
1340                                                         
1341                                                         _this.grid.ds.each(function(r) {
1342                                                             last = r.data.cmitem_linenumber;
1343                                                         });
1344                                                         
1345                                                         last++;
1346                                                         grid.stopEditing();
1347                                                         var nr = _this.grid.ds.reader.newRow({
1348                                                             cmitem_linenumber : last,
1349                                                             item_number : '',
1350                                                             item_descrip1 : '',
1351                                                             cmitem_cmhead_id : _this.form.findField('cmhead_id').getValue(),
1352                                                             cmitem_comments : '',
1353                                                             cmitem_taxtype_id : _this.data.default_taxtype_id,
1354                                                             cmitem_taxtype_id_taxtype_name : 'Taxable'
1355                                                         });
1356                                                         grid.stopEditing();
1357                                                         grid.ds.insert(grid.ds.getCount(), nr); 
1358                                                         grid.startEditing(grid.ds.getCount()-1, 1); // type..
1359                                         },
1360                                         render : function (_self)
1361                                         {
1362                                             _this.addItemBtn = _self;
1363                                         }
1364                                     },
1365                                     cls : 'x-btn-text-icon',
1366                                     text : "Add",
1367                                     icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
1368                                 },
1369                                 {
1370                                     xtype: 'Button',
1371                                     xns: Roo.Toolbar,
1372                                     listeners : {
1373                                         click : function (_self, e)
1374                                         {
1375                                             
1376                                             var last = 1;    
1377                                             _this.grid.ds.each(function(r) {
1378                                                 last = r.data.cmitem_linenumber +1;
1379                                         
1380                                             
1381                                             });
1382                                             
1383                                             var grid = _this.grid;
1384                                             var ct  =    _this.grid.ds.getCount();\r
1385                                             var lastrow = ct ?  _this.grid.ds.getAt(ct-1)  : false;\r
1386                                                  \r
1387                                             var cmhead_cust_id = _this.form.findField('cmhead_cust_id').getValue();
1388                                             var cmhead_id = _this.form.findField('cmhead_id').getValue();
1389                                             
1390                                             Pman.Dialog.XtupleSalesProductList.show( {cohead_cust_id : cmhead_cust_id, cmhead_id : cmhead_id} , function(res) {
1391                                             
1392                                                 Roo.log(res);
1393                                                 grid.stopEditing();
1394                                         
1395                                                 if (lastrow) {
1396                                                     var lr = lastrow;
1397                                                     if (!lr.data.cmitem_itemsite_id) {
1398                                                         lr.set('cmitem_itemsite_id', res.item_itemsite_id_itemsite_id);
1399                                                         lr.set('item_number',  res.item_number);
1400                                                         lr.set('item_descrip1', res.item_descrip1);                                
1401                                                         lr.set('cmitem_comments', res.item_descrip1);
1402                                                         lr.set('cmitem_cmhead_id', _this.form.findField('cmhead_id').getValue());
1403                                                         return;
1404                                                     }
1405                                                 }
1406                                                 var rate = _this.form.findField('taxzone_rate').getValue();\r
1407                                                  var nr = grid.ds.reader.newRow({
1408                                                             cmitem_linenumber : last,
1409                                                             cmitem_itemsite_id : res.item_itemsite_id_itemsite_id,
1410                                                             item_number :  res.item_number,
1411                                                             item_descrip1 : res.item_descrip1 ,
1412                                                             cmitem_cmhead_id : _this.form.findField('cmhead_id').getValue(),
1413                                                             cmitem_comments : res.item_descrip1,
1414                                                             cmitem_tax_listprice : res.item_price * ( 1 + rate * 1),
1415                                                             cmitem_item_listprice : res.item_price,
1416                                                             cmitem_tax_unitprice : res.item_price * ( 1 + rate * 1),
1417                                                             cmitem_unitprice : res.item_price,
1418                                                             cmitem_qtycredit : 1,
1419                                                             cmitem_line_value : res.item_price,
1420                                                             cmitem_line_tax_value : res.item_price * ( 1 + rate * 1)
1421                                                             
1422                                                         });
1423                                                 grid.ds.insert(grid.ds.getCount(), nr);
1424                                                 
1425                                                 var ar = grid.ds.getAt(grid.ds.getCount() - 1);
1426                                                 ar.commit();
1427                                                 
1428                                            }); 
1429                                         }
1430                                     },
1431                                     cls : 'x-btn-text-icon',
1432                                     text : "Find Products",
1433                                     icon : rootURL + '/Pman/templates/images/search.gif'
1434                                 },
1435                                 {
1436                                     xtype: 'Button',
1437                                     xns: Roo.Toolbar,
1438                                     listeners : {
1439                                         click : function ()
1440                                         {
1441                                             var cmhead_id = 1 * _this.form.findField('cmhead_id').getValue();
1442                                             if (!cmhead_id) {
1443                                                 Roo.MessageBox.alert("Error", "Save credit memo first!");
1444                                                 return;
1445                                             
1446                                             }
1447                                             
1448                                             new Pman.Download({
1449                                                 url : baseURL + '/Roo/Metasql',
1450                                                 method : 'GET',
1451                                                 timeout: 600000,
1452                                                 params : {
1453                                                     _group : 'cmhead',
1454                                                     _name : 'items',
1455                                                     'cmhead_id:number' : cmhead_id,
1456                                                     csvCols : '*',
1457                                                     csvTitles : '*', 
1458                                                     limit : 9999       
1459                                                 }
1460                                             });   
1461                                                     
1462                                            
1463                                         }
1464                                     },
1465                                     cls : 'x-btn-text-icon',
1466                                     text : "Download Excel",
1467                                     icon : rootURL + '/Pman/templates/images/spreadsheet.gif'
1468                                 },
1469                                 {
1470                                     xtype: 'Button',
1471                                     xns: Roo.Toolbar,
1472                                     listeners : {
1473                                         click : function (_self, e)
1474                                         {
1475                                            
1476                                             if (_this.form.findField('cmhead_posted').getValue() == 'true') {
1477                                                 Roo.MessageBox.alert("Error", "credit memo is already posted");
1478                                                 return;
1479                                             }
1480                                            
1481                                             Pman.Dialog.Image.show(
1482                                                {
1483                                                     _url : baseURL + '/Xtuple/Import/CreditMemo',
1484                                                     onid : _this.form.findField('cmhead_id').getValue()
1485                                                 
1486                                                },
1487                                                function (res) {
1488                                                     _this.grid.ds.load({});\r
1489                                                }
1490                                            );
1491                                         },
1492                                         render : function (_self)
1493                                         {
1494                                             _this.uploadBtn = _self;
1495                                         }
1496                                     },
1497                                     cls : 'x-btn-text-icon',
1498                                     text : "Upload Excel",
1499                                     icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
1500                                 },
1501                                 {
1502                                     xtype: 'Fill',
1503                                     xns: Roo.Toolbar
1504                                 },
1505                                 {
1506                                     xtype: 'Button',
1507                                     xns: Roo.Toolbar,
1508                                     listeners : {
1509                                         click : function()
1510                                         {
1511                                              _this.grid.stopEditing();
1512                                             // check that no shipments or invoices are done..
1513                                          if (_this.form.findField('cmhead_posted').getValue() == 'true') {
1514                                                 Roo.MessageBox.alert("Error", "credit memo is already posted");
1515                                                 return;
1516                                             }
1517                                                                                     // check that no shipments or invoices are done..
1518                                             var rc = _this.grid.getSelectionModel().getSelectedCell();
1519                                             
1520                                             var rec = _this.grid.ds.getAt(rc[0]);
1521                                             
1522                                             Roo.MessageBox.confirm("Confirm", "Are you sure you want to delete that line?", function(r)
1523                                             {
1524                                                 if (r != 'yes') {
1525                                                     return;
1526                                                 }
1527                                                 remove();
1528                                             });
1529                                             
1530                                             if (!rec.data.cmitem_id) {
1531                                                 _this.grid.ds.remove(rec);
1532                                                 return;
1533                                             }
1534                                             function remove()
1535                                             {
1536                                                 new  Pman.Request({
1537                                                     url : baseURL + '/Roo/cmitem',
1538                                                     method : 'POST',
1539                                                     params : {
1540                                                         _delete : rec.data.cmitem_id
1541                                                     
1542                                                     },
1543                                                     success : function() {
1544                                                         if (rec.data.item_type == 'K') {
1545                                                             _this.grid.ds.load({});
1546                                                             return;
1547                                                         }
1548                                                         _this.grid.ds.remove(rec);
1549                                                     }
1550                                                 
1551                                                 });
1552                                             }
1553                                         
1554                                             
1555                                             
1556                                         }
1557                                     },
1558                                     cls : 'x-btn-text-icon',
1559                                     text : "Delete",
1560                                     icon : rootURL + '/Pman/templates/images/trash.gif'
1561                                 }
1562                             ]
1563                         },
1564                         colModel : [
1565                             {
1566                                 xtype: 'ColumnModel',
1567                                 xns: Roo.grid,
1568                                 dataIndex : 'cmitem_linenumber',
1569                                 header : 'Item#',
1570                                 width : 60,
1571                                 renderer : function(v,x,r) {
1572                                 
1573                                     if (r.data.coitem_subnumber * 1 > 0) {
1574                                          return String.format('{0}.{1}', v,r.data.coitem_subnumber);
1575                                      }
1576                                      return String.format('{0}', v);
1577                                   }
1578                             },
1579                             {
1580                                 xtype: 'ColumnModel',
1581                                 xns: Roo.grid,
1582                                 dataIndex : 'item_number',
1583                                 header : 'Item Code',
1584                                 width : 75,
1585                                 renderer : function(v) { return String.format('{0}', v); },
1586                                 editor : {
1587                                     xtype: 'GridEditor',
1588                                     xns: Roo.grid,
1589                                     field : {
1590                                         xtype: 'ComboBox',
1591                                         xns: Roo.form,
1592                                         listeners : {
1593                                             beforeselect : function (combo, record, index)
1594                                             {
1595                                               // set _this.data values ..
1596                                               var ar = _this.grid.activeEditor.record;
1597                                               //Roo.log('beforeselect');
1598                                               
1599                                               var rate = _this.form.findField('taxzone_rate').getValue();
1600                                               
1601                                               (function() { 
1602                                                 
1603                                                 ar.set('item_descrip1', record.data.itemsite_item_id_item_descrip1);
1604                                                 ar.set('cmitem_comments', record.data.itemsite_item_id_item_descrip1);        
1605                                                 ar.set('cmitem_tax_listprice', record.data.item_listprice * ( 1 + rate * 1) );
1606                                                 ar.set('cmitem_item_listprice', record.data.item_listprice * 1);
1607                                                 ar.set('cmitem_tax_unitprice', record.data.item_price * ( 1 + rate * 1) );
1608                                                 ar.set('cmitem_unitprice', record.data.item_price * 1);
1609                                                 ar.set('cmitem_qtycredit', 1);
1610                                                 ar.set('cmitem_line_value', record.data.item_price * ar.data.cmitem_qtycredit);
1611                                                 ar.set('cmitem_line_tax_value', ar.data.cmitem_tax_unitprice * ar.data.cmitem_qtycredit);
1612                                                 ar.set('cmitem_itemsite_id', record.data.itemsite_id);
1613                                                 ar.set('item_number', record.data.itemsite_item_id_item_number);
1614                                                 ar.set('item_type', record.data.itemsite_item_id_item_type);
1615                                                 ar.set('avail_qty', 0);
1616                                               //  ar.updateFields = ['All'];
1617                                                 ar.commit();
1618                                               }).defer(100);
1619                                               
1620                                             }
1621                                         },
1622                                         allowBlank : false,
1623                                         displayField : 'itemsite_item_id_item_number',
1624                                         editable : true,
1625                                         emptyText : "Select item",
1626                                         forceSelection : true,
1627                                         hiddenName : 'itemsite_item_id_item_number',
1628                                         listWidth : 400,
1629                                         loadingText : "Searching...",
1630                                         minChars : 2,
1631                                         name : 'item_number',
1632                                         pageSize : 20,
1633                                         qtip : "Select item",
1634                                         queryParam : 'query[number]',
1635                                         selectOnFocus : true,
1636                                         tpl : '<div class="x-grid-cell-text x-btn button"><b>{itemsite_item_id_item_number}</b> ${item_price:toFixed(2)}- {itemsite_item_id_item_descrip1} </div>',
1637                                         triggerAction : 'all',
1638                                         typeAhead : false,
1639                                         valueField : 'item_number',
1640                                         store : {
1641                                             xtype: 'Store',
1642                                             xns: Roo.data,
1643                                             listeners : {
1644                                                 beforeload : function (_self, o){
1645                                                     o.params = o.params || {};
1646                                                     o.params.customer_id = _this.form.findField('cmhead_cust_id').getValue();
1647                                                     o.params['query[cmhead_id]'] = _this.form.findField('cmhead_id').getValue();
1648                                                     //o.params.shipto_cust_id = _this.data.cohead_cust_id;
1649                                                     // set more here
1650                                                 }
1651                                             },
1652                                             remoteSort : true,
1653                                             sortInfo : { direction : 'ASC', field: 'item_number' },
1654                                             proxy : {
1655                                                 xtype: 'HttpProxy',
1656                                                 xns: Roo.data,
1657                                                 method : 'GET',
1658                                                 url : baseURL + '/Roo/itemsite.php'
1659                                             },
1660                                             reader : {
1661                                                 xtype: 'JsonReader',
1662                                                 xns: Roo.data,
1663                                                 id : 'shipto_id',
1664                                                 root : 'data',
1665                                                 totalProperty : 'total',
1666                                                 fields : [{'name':'item_id','type':'int'},'item_number']
1667                                             }
1668                                         }
1669                                     }
1670                                 }
1671                             },
1672                             {
1673                                 xtype: 'ColumnModel',
1674                                 xns: Roo.grid,
1675                                 dataIndex : 'cmitem_comments',
1676                                 header : 'Item Description',
1677                                 width : '150.00',
1678                                 renderer : function(v,x,r) { 
1679                                 
1680                                     if (!v.length) {
1681                                         r.set('cmitem_comments', r.data.item_descrip1);
1682                                         v = r.data.item_descrip1;
1683                                     }
1684                                     if (v && v.length > 49) {
1685                                         return String.format('<span style="color:orange" qtip="line may be too long to print">{0}</span>', v);
1686                                     }
1687                                     return String.format('{0}', v); 
1688                                         
1689                                 },
1690                                 editor : {
1691                                     xtype: 'GridEditor',
1692                                     xns: Roo.grid,
1693                                     field : {
1694                                         xtype: 'TextField',
1695                                         xns: Roo.form,
1696                                         allowBlank : false
1697                                     }
1698                                 }
1699                             },
1700                             {
1701                                 xtype: 'ColumnModel',
1702                                 xns: Roo.grid,
1703                                 align : 'right',
1704                                 dataIndex : 'cmitem_qtycredit',
1705                                 header : 'Qty',
1706                                 width : 50,
1707                                 renderer : function(v)
1708                                 {
1709                                     return String.format('{0}', v ? parseInt(v) : '');
1710                                 },
1711                                 editor : {
1712                                     xtype: 'GridEditor',
1713                                     xns: Roo.grid,
1714                                     field : {
1715                                         xtype: 'NumberField',
1716                                         xns: Roo.form,
1717                                         listeners : {
1718                                             focus : function (_self)
1719                                             {
1720                                                 if (this.value == 0) {
1721                                                     this.el.dom.value = '';
1722                                                 }
1723                                             }
1724                                         },
1725                                         allowDecimals : false,
1726                                         decimalPrecision : 0,
1727                                         minValue : 1,
1728                                         style : 'text-align:right'
1729                                     }
1730                                 }
1731                             },
1732                             {
1733                                 xtype: 'ColumnModel',
1734                                 xns: Roo.grid,
1735                                 align : 'right',
1736                                 dataIndex : 'cmitem_tax_listprice',
1737                                 header : 'List Price w. tax',
1738                                 width : 80,
1739                                 renderer : function(v,x,r)
1740                                 {
1741                                  //   var rate = _this.form.findField('taxzone_rate').getValue();
1742                                  //   v = v * (1 + rate * 1);
1743                                  
1744                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1745                                 }
1746                             },
1747                             {
1748                                 xtype: 'ColumnModel',
1749                                 xns: Roo.grid,
1750                                 align : 'right',
1751                                 dataIndex : 'cmitem_taxtype_id',
1752                                 header : 'Taxed',
1753                                 width : 50,
1754                                 renderer : function(v,x,r) { return String.format('{0}', r.data.cmitem_taxtype_id_taxtype_name); },
1755                                 editor : {
1756                                     xtype: 'GridEditor',
1757                                     xns: Roo.grid,
1758                                     field : {
1759                                         xtype: 'ComboBox',
1760                                         xns: Roo.form,
1761                                         allowBlank : false,
1762                                         displayField : 'taxtype_name',
1763                                         editable : false,
1764                                         emptyText : "Select Tax Type",
1765                                         forceSelection : true,
1766                                         hiddenName : 'cmitem_taxtype_id',
1767                                         listWidth : 400,
1768                                         loadingText : "Searching...",
1769                                         minChars : 2,
1770                                         name : 'cmitem_taxtype_id_taxtype_name',
1771                                         pageSize : 20,
1772                                         qtip : "Select taxtype",
1773                                         queryParam : 'query[taxtype_id]',
1774                                         selectOnFocus : true,
1775                                         tpl : '<div class="x-grid-cell-text x-btn button"><b>{taxtype_name}</b> </div>',
1776                                         triggerAction : 'all',
1777                                         typeAhead : true,
1778                                         valueField : 'taxtype_id',
1779                                         width : 285,
1780                                         store : {
1781                                             xtype: 'Store',
1782                                             xns: Roo.data,
1783                                             listeners : {
1784                                                 beforeload : function (_self, o){
1785                                                     o.params = o.params || {};
1786                                                     // set more here
1787                                                     
1788                                                 /*    o.params.with_date = _this.form.findField('cohead_orderdate').getValue().format('Y-m-d'); 
1789                                                     Roo.log("with date?" + o.params.with_date);*/
1790                                                     
1791                                                     
1792                                                 }
1793                                             },
1794                                             remoteSort : true,
1795                                             sortInfo : { direction : 'ASC', field: 'taxtype_name' },
1796                                             proxy : {
1797                                                 xtype: 'HttpProxy',
1798                                                 xns: Roo.data,
1799                                                 method : 'GET',
1800                                                 url : baseURL + '/Roo/taxtype.php'
1801                                             },
1802                                             reader : {
1803                                                 xtype: 'JsonReader',
1804                                                 xns: Roo.data,
1805                                                 id : 'taxtype_id',
1806                                                 root : 'data',
1807                                                 totalProperty : 'total',
1808                                                 fields : [{'name':'taxtype_id','type':'int'},'taxtype_name']
1809                                             }
1810                                         }
1811                                     }
1812                                 }
1813                             },
1814                             {
1815                                 xtype: 'ColumnModel',
1816                                 xns: Roo.grid,
1817                                 align : 'right',
1818                                 dataIndex : 'cmitem_tax_unitprice',
1819                                 header : 'Unit Price w. tax',
1820                                 width : 90,
1821                                 renderer : function(v,x,r)
1822                                 {
1823                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1824                                 },
1825                                 editor : {
1826                                     xtype: 'GridEditor',
1827                                     xns: Roo.grid,
1828                                     field : {
1829                                         xtype: 'NumberField',
1830                                         xns: Roo.form,
1831                                         listeners : {
1832                                             focus : function (_self)
1833                                             {
1834                                                 if (this.value == 0.0) {
1835                                                     this.el.dom.value = '';
1836                                                 }
1837                                             }
1838                                         },
1839                                         decimalPrecision : 2,
1840                                         minValue : 0,
1841                                         style : 'text-align:right'
1842                                     }
1843                                 }
1844                             },
1845                             {
1846                                 xtype: 'ColumnModel',
1847                                 xns: Roo.grid,
1848                                 align : 'right',
1849                                 dataIndex : 'cmitem_unitprice',
1850                                 header : 'Unit Price',
1851                                 width : 75,
1852                                 renderer : function(v)
1853                                 {
1854                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1855                                 },
1856                                 editor : {
1857                                     xtype: 'GridEditor',
1858                                     xns: Roo.grid,
1859                                     field : {
1860                                         xtype: 'NumberField',
1861                                         xns: Roo.form,
1862                                         listeners : {
1863                                             focus : function (_self)
1864                                             {
1865                                                 if (this.value == 0.0) {
1866                                                     this.el.dom.value = '';
1867                                                 }
1868                                             }
1869                                         },
1870                                         decimalPrecision : 2,
1871                                         minValue : 0,
1872                                         style : 'text-align:right'
1873                                     }
1874                                 }
1875                             },
1876                             {
1877                                 xtype: 'ColumnModel',
1878                                 xns: Roo.grid,
1879                                 align : 'right',
1880                                 dataIndex : 'cmitem_line_value',
1881                                 header : 'Total',
1882                                 width : 75,
1883                                 renderer : function(v)
1884                                 {
1885                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1886                                 }
1887                             },
1888                             {
1889                                 align : 'right',
1890                                 dataIndex : 'cmitem_line_tax_value',
1891                                 header : 'Total w. tax',
1892                                 width : 80,
1893                                 renderer : function(v,x,r)
1894                                 {
1895                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1896                                 },
1897                                 xns : Roo.grid
1898                             }
1899                         ]
1900                     }
1901                 },
1902                 {
1903                     xtype: 'GridPanel',
1904                     xns: Roo,
1905                     listeners : {
1906                         activate : function() {
1907                             _this.cpanel = this;
1908                             
1909                             var id = _this.form.findField('cmhead_id').getValue() * 1;
1910                             if (id < 1) {
1911                                 Roo.MessageBox.alert("Error", "save the credit memo first!");
1912                                 _this.dialog.layout.getRegion('center').showPanel(0);
1913                                 return;
1914                             }
1915                             
1916                             if (_this.cgrid) {
1917                                 _this.cgrid.footer.onClick('first');
1918                             }
1919                         }
1920                     },
1921                     background : true,
1922                     fitContainer : true,
1923                     fitToframe : true,
1924                     region : 'center',
1925                     tableName : 'checkitem',
1926                     title : "Miscellaneous Check",
1927                     grid : {
1928                         xtype: 'Grid',
1929                         xns: Roo.grid,
1930                         listeners : {
1931                             render : function() 
1932                             {
1933                                 _this.cgrid = this; 
1934                                 //_this.dialog = Pman.Dialog.FILL_IN
1935                                 if (_this.cpanel.active) {
1936                                    this.footer.onClick('first');
1937                                 }
1938                             }
1939                         },
1940                         autoExpandColumn : 'checkitem_checkhead_id_checkhead_notes',
1941                         loadMask : true,
1942                         dataSource : {
1943                             xtype: 'Store',
1944                             xns: Roo.data,
1945                             listeners : {
1946                                 beforeload : function (_self, options)
1947                                 {
1948                                     options.params = options.params || {};
1949                                     options.params.checkitem_cmnumber = _this.form.findField('cmhead_number').getValue();
1950                                 }
1951                             },
1952                             remoteSort : true,
1953                             sortInfo : { field : 'checkitem_id', direction: 'DESC' },
1954                             proxy : {
1955                                 xtype: 'HttpProxy',
1956                                 xns: Roo.data,
1957                                 method : 'GET',
1958                                 url : baseURL + '/Roo/checkitem.php'
1959                             },
1960                             reader : {
1961                                 xtype: 'JsonReader',
1962                                 xns: Roo.data,
1963                                 id : 'id',
1964                                 root : 'data',
1965                                 totalProperty : 'total',
1966                                 fields : [
1967                                     {
1968                                         'name': 'id',
1969                                         'type': 'int'
1970                                     },
1971                                     {
1972                                         'name': 'name',
1973                                         'type': 'string'
1974                                     },
1975                                     {
1976                                         'name': 'type',
1977                                         'type': 'int'
1978                                     },
1979                                     {
1980                                         'name': 'leader',
1981                                         'type': 'int'
1982                                     },
1983                                     {
1984                                         'name': 'leader_id',
1985                                         'type': 'int'
1986                                     },
1987                                     {
1988                                         'name': 'leader_office_id',
1989                                         'type': 'int'
1990                                     },
1991                                     {
1992                                         'name': 'leader_name',
1993                                         'type': 'string'
1994                                     },
1995                                     {
1996                                         'name': 'leader_phone',
1997                                         'type': 'string'
1998                                     },
1999                                     {
2000                                         'name': 'leader_fax',
2001                                         'type': 'string'
2002                                     },
2003                                     {
2004                                         'name': 'leader_email',
2005                                         'type': 'string'
2006                                     },
2007                                     {
2008                                         'name': 'leader_company_id',
2009                                         'type': 'int'
2010                                     },
2011                                     {
2012                                         'name': 'leader_role',
2013                                         'type': 'string'
2014                                     },
2015                                     {
2016                                         'name': 'leader_active',
2017                                         'type': 'int'
2018                                     },
2019                                     {
2020                                         'name': 'leader_remarks',
2021                                         'type': 'string'
2022                                     },
2023                                     {
2024                                         'name': 'leader_passwd',
2025                                         'type': 'string'
2026                                     },
2027                                     {
2028                                         'name': 'leader_owner_id',
2029                                         'type': 'int'
2030                                     },
2031                                     {
2032                                         'name': 'leader_lang',
2033                                         'type': 'string'
2034                                     },
2035                                     {
2036                                         'name': 'leader_no_reset_sent',
2037                                         'type': 'int'
2038                                     },
2039                                     {
2040                                         'name': 'leader_action_type',
2041                                         'type': 'string'
2042                                     },
2043                                     {
2044                                         'name': 'leader_project_id',
2045                                         'type': 'int'
2046                                     },
2047                                     {
2048                                         'name': 'leader_deleted_by',
2049                                         'type': 'int'
2050                                     },
2051                                     {
2052                                         'name': 'leader_deleted_dt',
2053                                         'type': 'date'
2054                                     },
2055                                     {
2056                                         'name': 'leader_firstname',
2057                                         'type': 'string'
2058                                     },
2059                                     {
2060                                         'name': 'leader_lastname',
2061                                         'type': 'string'
2062                                     },
2063                                     {
2064                                         'name': 'leader_name_facebook',
2065                                         'type': 'string'
2066                                     },
2067                                     {
2068                                         'name': 'leader_url_blog',
2069                                         'type': 'string'
2070                                     },
2071                                     {
2072                                         'name': 'leader_url_twitter',
2073                                         'type': 'string'
2074                                     },
2075                                     {
2076                                         'name': 'leader_url_linkedin',
2077                                         'type': 'string'
2078                                     },
2079                                     {
2080                                         'name': 'leader_crm_lead_percentage',
2081                                         'type': 'int'
2082                                     },
2083                                     {
2084                                         'name': 'leader_crm_industry_id',
2085                                         'type': 'int'
2086                                     },
2087                                     {
2088                                         'name': 'leader_crm_updated_action_id',
2089                                         'type': 'int'
2090                                     },
2091                                     {
2092                                         'name': 'leader_crm_created_action_id',
2093                                         'type': 'int'
2094                                     },
2095                                     {
2096                                         'name': 'leader_crm_type_id',
2097                                         'type': 'int'
2098                                     }
2099                                 ]
2100                             }
2101                         },
2102                         footer : {
2103                             xtype: 'PagingToolbar',
2104                             xns: Roo,
2105                             displayInfo : true,
2106                             displayMsg : "Displaying check item{0} - {1} of {2}",
2107                             emptyMsg : "No check item found",
2108                             pageSize : 25
2109                         },
2110                         colModel : [
2111                             {
2112                                 xtype: 'ColumnModel',
2113                                 xns: Roo.grid,
2114                                 dataIndex : 'checkitem_docdate',
2115                                 header : 'Date',
2116                                 width : 75,
2117                                 renderer : function(v) { return String.format('{0}', v ? v.format('Y-m-d') : ''); }
2118                             },
2119                             {
2120                                 xtype: 'ColumnModel',
2121                                 xns: Roo.grid,
2122                                 dataIndex : 'checkitem_bankaccnt_id_bankaccnt_name',
2123                                 header : 'Bank Account',
2124                                 width : 100,
2125                                 renderer : function(v) { return String.format('{0}', v ? v : ''); }
2126                             },
2127                             {
2128                                 xtype: 'ColumnModel',
2129                                 xns: Roo.grid,
2130                                 dataIndex : 'checkitem_checkhead_id_checkhead_for',
2131                                 header : 'Memo',
2132                                 width : 75,
2133                                 renderer : function(v) { return String.format('{0}', v ? v : ''); }
2134                             },
2135                             {
2136                                 xtype: 'ColumnModel',
2137                                 xns: Roo.grid,
2138                                 dataIndex : 'checkitem_checkhead_id_checkhead_notes',
2139                                 header : 'Notes',
2140                                 width : 100,
2141                                 renderer : function(v) { return String.format('{0}', v ? v : ''); }
2142                             },
2143                             {
2144                                 xtype: 'ColumnModel',
2145                                 xns: Roo.grid,
2146                                 align : 'right',
2147                                 dataIndex : 'checkitem_curr_id_curr_name',
2148                                 header : 'Currency',
2149                                 width : 75,
2150                                 renderer : function(v) { return String.format('{0}', v ? v : ''); }
2151                             },
2152                             {
2153                                 xtype: 'ColumnModel',
2154                                 xns: Roo.grid,
2155                                 align : 'right',
2156                                 dataIndex : 'checkitem_amount',
2157                                 header : 'Amount',
2158                                 width : 75,
2159                                 renderer : function(v) { return String.format('{0}', v ? parseFloat(v).toFixed(2) : ''); }
2160                             }
2161                         ]
2162                     }
2163                 },
2164                 {
2165                     xtype: 'GridPanel',
2166                     xns: Roo,
2167                     listeners : {
2168                         activate : function() {
2169                             _this.apanel = this;
2170                             if (_this.agrid) {
2171                                 _this.agrid.footer.onClick('first');
2172                             }
2173                         }
2174                     },
2175                     background : false,
2176                     fitContainer : true,
2177                     fitToframe : true,
2178                     region : 'center',
2179                     tableName : 'invchead',
2180                     title : "Applications",
2181                     grid : {
2182                         xtype: 'Grid',
2183                         xns: Roo.grid,
2184                         listeners : {
2185                             render : function() 
2186                             {
2187                                 _this.agrid = this; 
2188                                 //_this.dialog = Pman.Dialog.FILL_IN
2189                                 if (_this.apanel.active) {
2190                                    this.footer.onClick('first');
2191                                 }
2192                             }
2193                         },
2194                         autoExpandColumn : 'arapply_target_docnumber',
2195                         loadMask : true,
2196                         dataSource : {
2197                             xtype: 'Store',
2198                             xns: Roo.data,
2199                             listeners : {
2200                                 beforeload : function (_self,o) {
2201                                 
2202                                     try {
2203                                        this.removeAll();
2204                                     } catch (e) { }
2205                                 
2206                                     if (!_this.data || !_this.data.cmhead_id) {
2207                                         return false;
2208                                     }
2209                                     o.params = o.params || {};
2210                                     
2211                                     o.params._application = _this.data.cmhead_id
2212                                     
2213                                 }
2214                             },
2215                             remoteSort : true,
2216                             sortInfo : { field : 'arapply_id', direction: 'ASC' },
2217                             proxy : {
2218                                 xtype: 'HttpProxy',
2219                                 xns: Roo.data,
2220                                 method : 'GET',
2221                                 url : baseURL + '/Roo/Arapply.php'
2222                             },
2223                             reader : {
2224                                 xtype: 'JsonReader',
2225                                 xns: Roo.data,
2226                                 totalProperty : 'total',
2227                                 root : 'data',
2228                                 id : 'id',
2229                                 fields : [
2230                                     {
2231                                         'name': 'id',
2232                                         'type': 'int'
2233                                     },
2234                                     {
2235                                         'name': 'name',
2236                                         'type': 'string'
2237                                     },
2238                                     {
2239                                         'name': 'type',
2240                                         'type': 'int'
2241                                     },
2242                                     {
2243                                         'name': 'leader',
2244                                         'type': 'int'
2245                                     },
2246                                     {
2247                                         'name': 'leader_id',
2248                                         'type': 'int'
2249                                     },
2250                                     {
2251                                         'name': 'leader_office_id',
2252                                         'type': 'int'
2253                                     },
2254                                     {
2255                                         'name': 'leader_name',
2256                                         'type': 'string'
2257                                     },
2258                                     {
2259                                         'name': 'leader_phone',
2260                                         'type': 'string'
2261                                     },
2262                                     {
2263                                         'name': 'leader_fax',
2264                                         'type': 'string'
2265                                     },
2266                                     {
2267                                         'name': 'leader_email',
2268                                         'type': 'string'
2269                                     },
2270                                     {
2271                                         'name': 'leader_company_id',
2272                                         'type': 'int'
2273                                     },
2274                                     {
2275                                         'name': 'leader_role',
2276                                         'type': 'string'
2277                                     },
2278                                     {
2279                                         'name': 'leader_active',
2280                                         'type': 'int'
2281                                     },
2282                                     {
2283                                         'name': 'leader_remarks',
2284                                         'type': 'string'
2285                                     },
2286                                     {
2287                                         'name': 'leader_passwd',
2288                                         'type': 'string'
2289                                     },
2290                                     {
2291                                         'name': 'leader_owner_id',
2292                                         'type': 'int'
2293                                     },
2294                                     {
2295                                         'name': 'leader_lang',
2296                                         'type': 'string'
2297                                     },
2298                                     {
2299                                         'name': 'leader_no_reset_sent',
2300                                         'type': 'int'
2301                                     },
2302                                     {
2303                                         'name': 'leader_action_type',
2304                                         'type': 'string'
2305                                     },
2306                                     {
2307                                         'name': 'leader_project_id',
2308                                         'type': 'int'
2309                                     },
2310                                     {
2311                                         'name': 'leader_deleted_by',
2312                                         'type': 'int'
2313                                     },
2314                                     {
2315                                         'name': 'leader_deleted_dt',
2316                                         'type': 'date'
2317                                     },
2318                                     {
2319                                         'name': 'leader_firstname',
2320                                         'type': 'string'
2321                                     },
2322                                     {
2323                                         'name': 'leader_lastname',
2324                                         'type': 'string'
2325                                     },
2326                                     {
2327                                         'name': 'leader_name_facebook',
2328                                         'type': 'string'
2329                                     },
2330                                     {
2331                                         'name': 'leader_url_blog',
2332                                         'type': 'string'
2333                                     },
2334                                     {
2335                                         'name': 'leader_url_twitter',
2336                                         'type': 'string'
2337                                     },
2338                                     {
2339                                         'name': 'leader_url_linkedin',
2340                                         'type': 'string'
2341                                     },
2342                                     {
2343                                         'name': 'leader_crm_lead_percentage',
2344                                         'type': 'int'
2345                                     },
2346                                     {
2347                                         'name': 'leader_crm_industry_id',
2348                                         'type': 'int'
2349                                     },
2350                                     {
2351                                         'name': 'leader_crm_updated_action_id',
2352                                         'type': 'int'
2353                                     },
2354                                     {
2355                                         'name': 'leader_crm_created_action_id',
2356                                         'type': 'int'
2357                                     },
2358                                     {
2359                                         'name': 'leader_crm_type_id',
2360                                         'type': 'int'
2361                                     }
2362                                 ]
2363                             }
2364                         },
2365                         footer : {
2366                             xtype: 'PagingToolbar',
2367                             xns: Roo,
2368                             displayInfo : true,
2369                             displayMsg : "Displaying application{0} - {1} of {2}",
2370                             emptyMsg : "No application found",
2371                             pageSize : 25
2372                         },
2373                         colModel : [
2374                             {
2375                                 xtype: 'ColumnModel',
2376                                 xns: Roo.grid,
2377                                 dataIndex : 'arapply_id',
2378                                 header : 'ID',
2379                                 width : 75,
2380                                 renderer : function(v) { return String.format('{0}', v); }
2381                             },
2382                             {
2383                                 xtype: 'ColumnModel',
2384                                 xns: Roo.grid,
2385                                 dataIndex : 'arapply_distdate',
2386                                 header : 'Date',
2387                                 width : 100,
2388                                 renderer : function(v) { return String.format('{0}', v ? v.format('Y-m-d') : ''); }
2389                             },
2390                             {
2391                                 xtype: 'ColumnModel',
2392                                 xns: Roo.grid,
2393                                 dataIndex : 'arapply_target_doctype',
2394                                 header : 'Target Doctype',
2395                                 width : 100,
2396                                 renderer : function(v) { return String.format('{0}', v); }
2397                             },
2398                             {
2399                                 xtype: 'ColumnModel',
2400                                 xns: Roo.grid,
2401                                 dataIndex : 'arapply_target_docnumber',
2402                                 header : 'Target Docnumber',
2403                                 width : 100,
2404                                 renderer : function(v) { return String.format('{0}', v); }
2405                             },
2406                             {
2407                                 xtype: 'ColumnModel',
2408                                 xns: Roo.grid,
2409                                 dataIndex : 'arapply_curr_id_curr_name',
2410                                 header : 'Currency',
2411                                 width : 75,
2412                                 renderer : function(v) { return String.format('{0}', v); }
2413                             },
2414                             {
2415                                 xtype: 'ColumnModel',
2416                                 xns: Roo.grid,
2417                                 dataIndex : 'arapply_applied',
2418                                 header : 'Applied',
2419                                 width : 75,
2420                                 renderer : function(v) { return String.format('{0}', v); }
2421                             }
2422                         ]
2423                     }
2424                 },
2425                 {
2426                     xtype: 'GridPanel',
2427                     xns: Roo,
2428                     listeners : {
2429                         activate : function() {
2430                             _this.hpanel = this;
2431                             if (_this.hgrid) {
2432                                 _this.hgrid.footer.onClick('first');
2433                             }
2434                         }
2435                     },
2436                     background : true,
2437                     fitContainer : true,
2438                     fitToframe : true,
2439                     region : 'center',
2440                     tableName : 'events',
2441                     title : "History",
2442                     grid : {
2443                         xtype: 'Grid',
2444                         xns: Roo.grid,
2445                         listeners : {
2446                             render : function() 
2447                             {
2448                                 _this.hgrid = this; 
2449                                 //_this.dialog = Pman.Dialog.FILL_IN
2450                                 if (_this.hpanel.active) {
2451                                    this.footer.onClick('first');
2452                                 }
2453                             },
2454                             rowdblclick : function (_self, rowIndex, e)
2455                             {
2456                                 if (!_this.dialog) return;
2457                                 _this.dialog.show( this.getDataSource().getAt(rowIndex).data, function() {
2458                                     _this.grid.footer.onClick('first');
2459                                 }); 
2460                             }
2461                         },
2462                         autoExpandColumn : 'remarks',
2463                         loadMask : true,
2464                         dataSource : {
2465                             xtype: 'Store',
2466                             xns: Roo.data,
2467                             listeners : {
2468                                 beforeload : function (_self, options)
2469                                 {
2470                                     options.params._related_on_table = 'cmhead';
2471                                     options.params._related_on_id = _this.form.findField('cmhead_id').getValue();
2472                                     
2473                                 }
2474                             },
2475                             remoteSort : true,
2476                             sortInfo : { field : 'event_when', direction: 'DESC' },
2477                             proxy : {
2478                                 xtype: 'HttpProxy',
2479                                 xns: Roo.data,
2480                                 method : 'GET',
2481                                 url : baseURL + '/Roo/events.php'
2482                             },
2483                             reader : {
2484                                 xtype: 'JsonReader',
2485                                 xns: Roo.data,
2486                                 totalProperty : 'total',
2487                                 root : 'data',
2488                                 id : 'id',
2489                                 fields : [
2490                                     {
2491                                         'name': 'event_when',
2492                                         'type': 'date'
2493                                     },
2494                                     {
2495                                         'name': 'action',
2496                                         'type': 'string'
2497                                     },
2498                                     {
2499                                         'name': 'ipaddr',
2500                                         'type': 'string'
2501                                     },
2502                                     {
2503                                         'name': 'person_id_name',
2504                                         'type': 'string'
2505                                     },
2506                                     {
2507                                         'name': 'remarks',
2508                                         'type': 'string'
2509                                     }
2510                                 ]
2511                             }
2512                         },
2513                         footer : {
2514                             xtype: 'PagingToolbar',
2515                             xns: Roo,
2516                             pageSize : 25,
2517                             displayInfo : true,
2518                             displayMsg : "Displaying events{0} - {1} of {2}",
2519                             emptyMsg : "No events found"
2520                         },
2521                         colModel : [
2522                             {
2523                                 xtype: 'ColumnModel',
2524                                 xns: Roo.grid,
2525                                 dataIndex : 'event_when',
2526                                 header : 'Changed',
2527                                 width : 120,
2528                                 renderer : function(v) { return String.format('{0}', v ? v.format('d/M/Y H:i:s') : ''); }
2529                             },
2530                             {
2531                                 xtype: 'ColumnModel',
2532                                 xns: Roo.grid,
2533                                 dataIndex : 'action',
2534                                 header : 'action',
2535                                 width : 120,
2536                                 renderer : function(v,x,r) { return String.format('{0} - {1}', v, r.data.on_table); }
2537                             },
2538                             {
2539                                 xtype: 'ColumnModel',
2540                                 xns: Roo.grid,
2541                                 dataIndex : 'ipaddr',
2542                                 header : 'IP address',
2543                                 width : 120,
2544                                 renderer : function(v) { return String.format('{0}', v); }
2545                             },
2546                             {
2547                                 xtype: 'ColumnModel',
2548                                 xns: Roo.grid,
2549                                 dataIndex : 'person_id_name',
2550                                 header : 'Who',
2551                                 width : 120,
2552                                 renderer : function(v) { return String.format('{0}', v); }
2553                             },
2554                             {
2555                                 xtype: 'ColumnModel',
2556                                 xns: Roo.grid,
2557                                 dataIndex : 'remarks',
2558                                 header : 'Notes',
2559                                 width : 200,
2560                                 renderer : function(v) { return String.format('{0}', v); }
2561                             }
2562                         ]
2563                     }
2564                 }
2565             ],
2566             center : {
2567                 xtype: 'LayoutRegion',
2568                 xns: Roo,
2569                 alwaysShowTabs : true,
2570                 tabPosition : 'top'
2571             },
2572             buttons : [
2573                 {
2574                     xtype: 'Button',
2575                     xns: Roo,
2576                     listeners : {
2577                         click : function (_self, e)
2578                         {
2579                               if (_this.grid)  _this.grid.stopEditing();
2580                             _this.dialog.hide();
2581                         }
2582                     },
2583                     text : "Cancel"
2584                 },
2585                 {
2586                     xtype: 'Button',
2587                     xns: Roo,
2588                     listeners : {
2589                         click : function (_self, e)
2590                         {
2591                           // do some checks?
2592                                if (_this.grid)  _this.grid.stopEditing();
2593                               if (_this.form.findField('cmhead_posted').getValue() == 'true') {
2594                                   Roo.MessageBox.alert("Error", "credit memo is already posted");
2595                                   return;
2596                               }
2597                               var loose = false;
2598                               var ar = [];
2599                               if (_this.grid && _this.grid.ds) {
2600                                   _this.grid.ds.each(function(rec) {
2601                                       if (!(rec.data.cmitem_itemsite_id * 1) || !(rec.data.cmitem_qtycredit*1) || !(rec.data.cmitem_unitprice*1)) {
2602                                           loose = true;
2603                                           return true;
2604                                       } 
2605                                       ar.push(rec);
2606                                       
2607                                   });
2608                                   if (loose) {
2609                                       Roo.MessageBox.alert("Error", "Some lines do not have product/qty/price set");
2610                                       return;
2611                                   }    
2612                               }
2613                               if(ar.length){
2614                                   _this.form.findField('has_item').setValue(ar.length);
2615                               } 
2616                           
2617                               _this.form.doAction("submit");
2618                         },
2619                         render : function (_self)
2620                         {
2621                         _this.saveBtn = _self;
2622                         }
2623                     },
2624                     text : "Save"
2625                 }
2626             ]
2627         });
2628     }
2629 };