Pman.Dialog.XtupleCreditMemo.bjs
[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) + (1*vals.cmhead_taxfree_value) + (1*vals.cmhead_freight) + (1*vals.cmhead_tax_value) + (1*vals.cmhead_misc)
644                                                                         );
645                                                                     }
646                                                                 }
647                                                             ]
648                                                         }
649                                                     ]
650                                                 }
651                                             ]
652                                         },
653                                         {
654                                             xtype: 'FieldSet',
655                                             xns: Roo.form,
656                                             legend : "Billing",
657                                             style : 'width:420px',
658                                             items : [
659                                                 {
660                                                     xtype: 'Column',
661                                                     xns: Roo.form,
662                                                     width : '420',
663                                                     labelWidth : '50',
664                                                     items : [
665                                                         {
666                                                             xtype: 'ComboBox',
667                                                             xns: Roo.form,
668                                                             listeners : {
669                                                                 beforeselect : function (combo, record, index)
670                                                                 {
671                                                                     // set _this.data values ..
672                                                                     
673                                                                     // just add everything...
674                                                                     for(var i in record.data) {
675                                                                       //  Roo.log('cmhead_billto_cntct_id_' + i +' ='  + record.data[i]);
676                                                                         _this.data['cmhead_billto_cntct_id_' + i] = record.data[i];
677                                                                     }
678                                                                 
679                                                                     _this.form.findField('billto_address').update();
680                                                                     
681                                                                  
682                                                                 },
683                                                                 add : function (combo)
684                                                                 {
685                                                                   
686                                                                   Pman.Dialog.XtupleQuickContact.show( 
687                                                                             {
688                                                                               _id : id,
689                                                                               customer_id : _this.form.findField('cmhead_cust_id').getValue()
690                                                                             },
691                                                                             
692                                                                             function (data) {
693                                                                                
694                                                                                 for(var i in  data) {
695                                                                                     
696                                                                                     _this.data['cmhead_billto_cntct_id_' + i] =  data[i];
697                                                                                 }
698                                                                                 
699                                                                                 _this.form.findField('billto_address').update();
700                                                                                 // fill in the select box..
701                                                                                 _this.form.setValues( {
702                                                                                     cmhead_billto_cntct_id : data.cntct_id,
703                                                                                     cmhead_billto_cntct_id_cntct_name : data.cntct_first_name + ' '+ 
704                                                                                             data.cntct_last_name
705                                                                                     
706                                                                                 });
707                                                                             }
708                                                                         );
709                                                                         //  Pman.Dialog.XtupleCustomer.show(
710                                                                             //{ cust_id : _this.form.findField('cohead_cust_id').getValue() }, 
711                                                                             //function(data) {
712                                                                         // refresh the data in the pulldown..
713                                                                     //    }); 
714                                                                 
715                                                                 }
716                                                             },
717                                                             allowBlank : false,
718                                                             alwaysQuery : true,
719                                                             displayField : 'cntct_name',
720                                                             editable : true,
721                                                             emptyText : "Select Contact",
722                                                             fieldLabel : 'Bill To (select)',
723                                                             forceSelection : true,
724                                                             hiddenName : 'cmhead_billto_cntct_id',
725                                                             listWidth : 400,
726                                                             loadingText : "Searching...",
727                                                             minChars : 2,
728                                                             name : 'cmhead_billto_cntct_id_cntct_name',
729                                                             pageSize : 20,
730                                                             qtip : "Select Contact",
731                                                             queryParam : 'query[cntct_name]',
732                                                             selectOnFocus : true,
733                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{cntct_name}</b> {cntct_addr_id_addr_line1}</div>',
734                                                             triggerAction : 'all',
735                                                             typeAhead : true,
736                                                             valueField : 'cntct_id',
737                                                             width : 300,
738                                                             store : {
739                                                                 xtype: 'Store',
740                                                                 xns: Roo.data,
741                                                                 listeners : {
742                                                                     beforeload : function (_self, o){
743                                                                         o.params = o.params || {};
744                                                                         // set more here
745                                                                         o.params._customer_id = _this.data.cmhead_cust_id;
746                                                                     }
747                                                                 },
748                                                                 remoteSort : true,
749                                                                 sortInfo : { direction : 'ASC', field: 'cntct_name' },
750                                                                 proxy : {
751                                                                     xtype: 'HttpProxy',
752                                                                     xns: Roo.data,
753                                                                     method : 'GET',
754                                                                     url : baseURL + '/Roo/cntct.php'
755                                                                 },
756                                                                 reader : {
757                                                                     xtype: 'JsonReader',
758                                                                     xns: Roo.data,
759                                                                     id : 'cntct_id',
760                                                                     root : 'data',
761                                                                     totalProperty : 'total',
762                                                                     fields : [{'name':'cntct_id','type':'int'},'cntct_name']
763                                                                 }
764                                                             }
765                                                         },
766                                                         {
767                                                             xtype: 'TextArea',
768                                                             xns: Roo.form,
769                                                             listeners : {
770                                                                 render : function (_self)
771                                                                 {
772                                                                    Roo.log(this.el)
773                                                                    
774                                                                    
775                                                                    
776                                                                    this.el.on('click', function() { 
777                                                                        var id = _this.form.findField('cmhead_billto_cntct_id').getValue();
778                                                                         Pman.Dialog.XtupleQuickContact.show( 
779                                                                             {
780                                                                               _id : id,
781                                                                               customer_id : _this.form.findField('cmhead_cust_id').getValue()
782                                                                             },
783                                                                             
784                                                                             function (data) {
785                                                                             
786                                                                                 for(var i in  data) {
787                                                                                     
788                                                                                     _this.data['cmhead_billto_cntct_id_' + i] =  data[i];
789                                                                                 }
790                                                                                 
791                                                                                 _this.form.findField('billto_address').update();
792                                                                                 // fill in the select box..
793                                                                                 _this.form.setValues( {
794                                                                                     cmhead_billto_cntct_id : data.cntct_id,
795                                                                                     cmhead_billto_cntct_id_cntct_name : data.cntct_first_name + ' '+ 
796                                                                                             data.cntct_last_name
797                                                                                 });
798                                                                             }
799                                                                         );
800                                                                      });
801                                                                 }
802                                                             },
803                                                             fieldLabel : 'or enter Address',
804                                                             name : 'billto_address',
805                                                             readOnly : true,
806                                                             update : function() {
807                                                             
808                                                                 var c = ['first_name', 'last_name' ] ;
809                                                                 var a = [ 'line1', 'line2', 'line3', 'city', 'state', 'country' ];
810                                                                 var v = [];
811                                                                 Roo.each(c, function(e) {
812                                                                     if (_this.data['cmhead_billto_cntct_id_cntct_' +e] &&
813                                                                         _this.data['cmhead_billto_cntct_id_cntct_' +e].length) {
814                                                                         v.push(_this.data['cmhead_billto_cntct_id_cntct_' +e]);
815                                                                     }
816                                                                 });
817                                                                 Roo.each(a, function(e) {
818                                                                     if (_this.data['cmhead_billto_cntct_id_cntct_addr_id_addr_' +e] &&
819                                                                         _this.data['cmhead_billto_cntct_id_cntct_addr_id_addr_' +e].length) {
820                                                                         v.push(_this.data['cmhead_billto_cntct_id_cntct_addr_id_addr_' +e]);
821                                                                     }
822                                                                 });
823                                                                 this.setValue(v.join("\n"));
824                                                             }
825                                                         }
826                                                     ]
827                                                 }
828                                             ]
829                                         }
830                                     ]
831                                 },
832                                 {
833                                     xtype: 'Hidden',
834                                     xns: Roo.form,
835                                     name : 'cmhead_id'
836                                 },
837                                 {
838                                     xtype: 'Hidden',
839                                     xns: Roo.form,
840                                     name : 'cmhead_billto_addr_id'
841                                 },
842                                 {
843                                     xtype: 'Hidden',
844                                     xns: Roo.form,
845                                     name : 'cmhead_taxable_value'
846                                 },
847                                 {
848                                     xtype: 'Hidden',
849                                     xns: Roo.form,
850                                     name : 'cmhead_posted'
851                                 },
852                                 {
853                                     xtype: 'Hidden',
854                                     xns: Roo.form,
855                                     name : 'cmhead_cust_id'
856                                 },
857                                 {
858                                     xtype: 'Hidden',
859                                     xns: Roo.form,
860                                     name : 'taxzone_rate'
861                                 },
862                                 {
863                                     xtype: 'Hidden',
864                                     xns: Roo.form,
865                                     name : 'has_item'
866                                 }
867                             ]
868                         }
869                     ]
870                 },
871                 {
872                     xtype: 'GridPanel',
873                     xns: Roo,
874                     listeners : {
875                         deactivate : function (_self)
876                         {
877                             if(_this.grid){
878                                 _this.grid.stopEditing();
879                             }
880                         },
881                         activate : function (_self)
882                         {
883                         
884                             _this.panel = this;
885                             
886                             try { if (MODULE.isBuilder) {
887                                 return;
888                             } } catch(e) { }
889                             
890                             var id = _this.form.findField('cmhead_id').getValue() * 1;
891                             if (id < 1) {
892                                 Roo.MessageBox.alert("Save First", "Save the order first, before adding items");
893                                 _this.dialog.layout.getRegion('center').showPanel(0);
894                                 return;
895                             }
896                             if (_this.grid) {
897                                 _this.grid.ds.load({});
898                             }
899                         
900                         }
901                     },
902                     background : true,
903                     fitContainer : true,
904                     fitToframe : true,
905                     region : 'center',
906                     tableName : 'coitem',
907                     title : "Credit Items",
908                     grid : {
909                         xtype: 'EditorGrid',
910                         xns: Roo.grid,
911                         listeners : {
912                             render : function() 
913                             {
914                                 _this.grid = this; 
915                                 _this.hasQuery = 0;
916                                 //_this.dialog = Pman.Dialog.FILL_IN
917                                 if (_this.panel.active) {
918                                    this.ds.load({});
919                                 }
920                             },
921                             afteredit : function (e)
922                             {
923                                 //Roo.log('afteredit');
924                                 //Roo.log(e);
925                                 
926                                 if (e.field == 'item_number' || e.originalValue == e.value) {
927                                     // afterselect handles this...
928                                     return;
929                                 }
930                                 var r = e.record;
931                                 
932                                 if ( r && r.data.cmitem_id) {
933                                     // as we disable update to the display on the ajax callback to 
934                                     // allow editing flow to continue, and not refresh - we can only update
935                                     // these values after something has actually been edited.
936                                      r.set('coitem_id', r.data.cmitem_id);
937                                 }
938                                 
939                                 var rate = 0;
940                                 if(r.data.cmitem_taxtype_id_taxtype_name == 'Taxable' ){
941                                     rate = _this.form.findField('taxzone_rate').getValue();
942                                 }
943                                 if(e.field == 'cmitem_tax_unitprice'){
944                                     r.set('cmitem_unitprice', r.data.cmitem_tax_unitprice * 1 / (1 + rate * 1));
945                             //        fields.push('cmitem_unitprice');
946                                 }
947                                 if(e.field == 'cmitem_unitprice'){
948                                     r.set('cmitem_tax_unitprice', r.data.cmitem_unitprice * (1 + rate * 1) );
949                             //        fields.push('cmitem_tax_unitprice');
950                                 }
951                                 if(e.field == 'cmitem_taxtype_id'){
952                                     r.set('cmitem_tax_unitprice', r.data.cmitem_unitprice * (1 + rate * 1) );
953                             //        fields.push('cmitem_tax_unitprice');
954                                             
955                                 }
956                                 r.set('cmitem_tax_listprice', r.data.cmitem_item_listprice * (1 + rate * 1) );
957                             //    fields.push('cmitem_tax_listprice');
958                                 r.set('cmitem_line_value', r.data.cmitem_qtycredit * r.data.cmitem_unitprice );
959                             //    fields.push('cmitem_line_value');
960                                 r.set('cmitem_line_tax_value', r.data.cmitem_qtycredit * r.data.cmitem_tax_unitprice );
961                             //    fields.push('cmitem_line_tax_value'); 
962                                 
963                             //    fields.push('cmitem_id');
964                                 
965                                 _this.hasQuery += 1;
966                                 
967                                  var doupdate = function() { 
968                                    if (!_this.itemsUpdating) {
969                                         Roo.log('doupdate...');
970                                         
971                                         _this.hasQuery -= 1;
972                                         r.commit();
973                                         return;
974                                     }
975                                     doupdate.defer(1000);
976                                 }
977                                 
978                                 doupdate();
979                                 
980                                 
981                             },
982                             beforeedit : function (e)
983                             {
984                                 // we can only edit if nothing is assigned to shipping or invoices..
985                                 if (_this.form.findField('cmhead_posted').getValue() == 'true') {
986                                     Roo.MessageBox.alert("Error", "credit memo is already posted");
987                                     e.cancel = true;
988                                     return;
989                                 }
990                                 
991                                 return;
992                                 
993                                 // seems below logic is useless...!!!!
994                                 var rec = e.record;
995                             
996                                 if (rec.data.coitem_qtyshipped > 0 || rec.data.cobill_billed > 0) {
997                                     Roo.MessageBox.alert("Error", "That item has been shipped or invoices - void the shipments/invoices first");
998                                     e.cancel = true;
999                                     return;
1000                                 }
1001                                 
1002                                 if (rec.data.coitem_subnumber * 1 > 0) {
1003                                     Roo.log("Edit container event");
1004                                     Roo.log(e); // if it's a tab.. 
1005                             
1006                                     
1007                                     switch(e.field) {
1008                                         // allow editing of source / destination..
1009                                         case 'coitem_shipto_id':
1010                                         case 'coitem_location_src':            
1011                                             return;
1012                                         default : 
1013                                             break;
1014                                     }
1015                                     Roo.MessageBox.alert("Error", "That is a kit item, edit the container.");
1016                                     e.cancel = true;
1017                                     return;
1018                                 }
1019                                 // zero off values..
1020                                 //if (e.field == 'coitem_qtyord' && rec.data.coitem_qtyord == 0) {
1021                                 //        e.value ='';
1022                                 //    }
1023                                 //    if (e.field == 'coitem_custprice' && rec.data.coitem_qtyord == 0.0) {
1024                                 //        e.value ='';
1025                                 //    }
1026                                 
1027                                 if (rec.data.item_type == 'K' && e.field == 'item_number') {
1028                                     // you can not change the product type on kits' as it messing things up..
1029                                     Roo.MessageBox.alert("Error", "That is a kit item,if you need to change it, delete it first.");
1030                                     e.cancel = true;
1031                                     return;
1032                                 }
1033                                 
1034                             },
1035                             celldblclick : function (_self, rowIndex, columnIndex, e)
1036                             {
1037                                 var rec = this.ds.getAt(rowIndex);
1038                                 var di = this.cm.getDataIndex(columnIndex);
1039                                 if (di != 'avail_qty') {
1040                                     return;
1041                                 }
1042                                 Pman.Dialog.XtupleInvHistory.show({
1043                                     itemsite_item_id_item_number   : rec.data.item_number,
1044                                    // itemsite_item_id_item_descript1 : rec.data.item_descrip1,
1045                                     location_name : rec.data.coitem_location_src_location_name,
1046                                     location_descrip : rec.data.coitem_location_src_location_descrip,
1047                                     
1048                                     invhist_transdate : _this.form.findField('cohead_targetdate').getValue() 
1049                                 }); 
1050                                 
1051                             },
1052                             rowclass : function (gridview, rowcfg)
1053                             {
1054                                 if (rowcfg.record.data.coitem_status == 'C' &&
1055                                     rowcfg.record.data.shipitem_shipped * 1 < 1) {
1056                                     
1057                                     rowcfg.rowClass = 'strikethrough';
1058                                 }
1059                                  if (rowcfg.record.data.coitem_status == 'X'  ) {
1060                                     
1061                                     rowcfg.rowClass = 'strikethrough';
1062                                 }
1063                                // Roo.log(rowcfg);
1064                             //    shipitem_shipped
1065                             }
1066                         },
1067                         autoExpandColumn : 'cmitem_comments',
1068                         clicksToEdit : 1,
1069                         loadMask : true,
1070                         loadAvail : function() {
1071                             
1072                             return; //not used.
1073                                
1074                              var q = [];
1075                             this.ds.each(  function(r) {
1076                             
1077                                 // only update if we do not have the details.
1078                                 if (r.data.avail_qty == 0) {
1079                                 
1080                                 }
1081                                 q.push( { 
1082                                     item : r.data.item_number, 
1083                                     loc: r.data.coitem_location_src_location_name,
1084                                     id: r.data.coitem_linenumber + (r.data.coitem_subnumber ? ('.' + r.data.coitem_subnumber) : '')
1085                                  } );
1086                             });
1087                             
1088                             // needs to be a post to allow long lists of products..
1089                             
1090                             new Pman.Request({
1091                                 url : baseURL + '/Roo/itemloc',
1092                                 method : 'POST',
1093                                 mask : "Loading available qty",
1094                                 maskel : _this.grid.view.el,
1095                                 params : {
1096                                     _availqty : Roo.encode(q),
1097                                     curr_id : _this.form.findField('cohead_curr_id').getValue()
1098                                 },
1099                                 success : function(d) 
1100                                 {
1101                                     _this.grid.ds.each(function(r) { 
1102                                          var id = r.data.coitem_linenumber + (r.data.coitem_subnumber ? ('.' + r.data.coitem_subnumber) : '');
1103                                     
1104                                     
1105                                         if (typeof(d.data[id]) == 'undefined') {
1106                                             return;
1107                                         }
1108                                         r.set('avail_qty', d.data[id].qty);
1109                                         if (r.data.coitem_unitcost_in_order_cur * 1.0 < 0.1) {
1110                                             r.set('coitem_unitcost_in_order_cur', d.data[id].unitcost);
1111                                         }
1112                                     
1113                                     });
1114                                 
1115                                 }
1116                             });
1117                             
1118                                 
1119                                         
1120                         },
1121                         sm : {
1122                             xtype: 'CellSelectionModel',
1123                             xns: Roo.grid,
1124                             listeners : {
1125                                 tabend : function (_self)
1126                                 {
1127                                     _this.addItemBtn.fireEvent('click', _this.addItemBtn);
1128                                 },
1129                                 beforeeditnext : function (eventdata)
1130                                 {
1131                                     return;
1132                                     // this does not work, as the reload effect cancels editng.
1133                                     var rec = _this.grid.ds.getAt(eventdata.cell[0]);
1134                                     if (rec.data.coitem_subnumber *1 < 0 ) {
1135                                         return;
1136                                     }
1137                                     var r = eventdata.cell[0] + 1;
1138                                 
1139                                     while (true) {
1140                                         if (r > _this.grid.ds.getCount()-1 ) {
1141                                             eventdata.cell = false;
1142                                             return;
1143                                         }
1144                                         rec =  _this.grid.ds.getAt(r);
1145                                         if (rec.data.coitem_subnumber *1 < 0 ) {
1146                                            eventdata.cell = [ r, eventdata.cell[1] ];
1147                                            return;
1148                                         }
1149                                         r++;
1150                                     }
1151                                     
1152                                     
1153                                  
1154                                 
1155                                 }
1156                             },
1157                             enter_is_tab : true
1158                         },
1159                         dataSource : {
1160                             xtype: 'Store',
1161                             xns: Roo.data,
1162                             listeners : {
1163                                 beforeload : function (_self,o) {
1164                                 
1165                                     try {
1166                                        this.removeAll();
1167                                    } catch (e) { }
1168                                    
1169                                     Roo.log(_this.data);
1170                                     if (!_this.data || !_this.data.cmhead_id) {
1171                                         return false;
1172                                     }
1173                                     o.params = o.params || {};
1174                                     Roo.log(_this.data);
1175                                     o.params.cmitem_cmhead_id = _this.data.cmhead_id;
1176                                     o.params.limit = 999;
1177                                     
1178                                 },
1179                                 update : function (_self, rec, operation)
1180                                 {
1181                                    
1182                                    if (operation !=  Roo.data.Record.COMMIT) {
1183                                        return;
1184                                    }
1185                                    
1186                                    // row has been updated..
1187                                    // if the qty + item has been filled in, we should try and save it..
1188                                     if (!(rec.data.cmitem_itemsite_id * 1) || !(rec.data.cmitem_qtycredit*1) || !(rec.data.cmitem_unitprice*1)) {
1189                                         Roo.log('not saving - row not completed');
1190                                         return;
1191                                     }
1192                                 
1193                                     if (_this.itemsUpdating) {
1194                                         Roo.log('currently updating?');
1195                                         return;
1196                                     }
1197                                     _this.itemsUpdating = true;
1198                                 
1199                                 
1200                                     Roo.log('Running update');   
1201                                     
1202                                     new Pman.Request({
1203                                         url : baseURL+'/Roo/Cmitem',
1204                                         method : 'POST',
1205                                         
1206                                         params : rec.data,
1207                                         success: function(res)
1208                                         {
1209                                             Roo.log('GOT success');
1210                                             // update the data...
1211                                             if (rec.data.item_type == 'K') {
1212                                                  _this.itemsUpdating = false; 
1213                                                 _this.grid.ds.load({});
1214                                                 return;
1215                                             }
1216                                             if (_this.grid.activeEditor) {
1217                                                  rec.editing = true;
1218                                              } 
1219                                             rec.set('cmitem_id', res.data.cmitem_id);
1220                                             
1221                                             _this.itemsUpdating = false; 
1222                                             
1223                                             if(_this.hasQuery != 0){
1224                                                  
1225                                                 return;
1226                                             }
1227                                             
1228                                             rec.dirty = false;
1229                                             delete rec.modified;
1230                                             
1231                                             _this.grid.loadAvail();
1232                                             
1233                                         },
1234                                         failure : function(res)
1235                                         {
1236                                             Roo.MessageBox.alert(res.message);
1237                                             _this.itemsUpdating = false;
1238                                         }
1239                                         
1240                                         
1241                                     });
1242                                     
1243                                     
1244                                    
1245                                    
1246                                    
1247                                 },
1248                                 load : function (_self, records, options)
1249                                 {
1250                                     // need to fetch availablity from master data..
1251                                     // build a list of what to ask..
1252                                     Roo.log(records);
1253                                     // query: ITEM CODE - LOCATION
1254                                     
1255                                     //_this.grid.loadAvail.defer(100, _this.grid);
1256                                     var rate = 0;
1257                                         
1258                                     Roo.each(records, function(r){
1259                                         rate = 0;
1260                                         if(r.data.cmitem_taxtype_id == r.data.cmitem_taxable_id ){
1261                                             rate = _this.form.findField('taxzone_rate').getValue();
1262                                         }
1263                                         r.set('cmitem_tax_unitprice', r.data.cmitem_unitprice * (1 + rate * 1));
1264                                         r.set('cmitem_tax_listprice', r.data.cmitem_item_listprice * (1 + rate * 1));
1265                                         r.set('cmitem_line_tax_value', r.data.cmitem_line_value * (1 + rate * 1));
1266                                     })    
1267                                     
1268                                 }
1269                             },
1270                             remoteSort : true,
1271                             sortInfo : { field : 'cmitem_linenumber', direction: 'ASC' },
1272                             proxy : {
1273                                 xtype: 'HttpProxy',
1274                                 xns: Roo.data,
1275                                 method : 'GET',
1276                                 url : baseURL + '/Roo/Cmitem.php'
1277                             },
1278                             reader : {
1279                                 xtype: 'JsonReader',
1280                                 xns: Roo.data,
1281                                 totalProperty : 'total',
1282                                 root : 'data',
1283                                 id : 'id',
1284                                 fields : [
1285                                     {
1286                                         'name': 'coitem_linenumber',
1287                                         'type': 'int'
1288                                     },
1289                                     {
1290                                         'name': 'coitem_itemsite_id',
1291                                         'type': 'int'
1292                                     },
1293                                     {
1294                                         'name': 'coitem_qtyord'
1295                                     },
1296                                     {
1297                                         'name': 'coitem_unitcost'
1298                                     },
1299                                     {
1300                                         'name': 'coitem_price'
1301                                     },
1302                                     {
1303                                         'name': 'coitem_custprice'
1304                                     },
1305                                     {
1306                                         'name': 'coitem_qtyreturned'
1307                                     },
1308                                     {
1309                                         'name': 'coitem_prcost'
1310                                     },
1311                                     {
1312                                         'name': 'coitem_price_uom_id',
1313                                         'type': 'int'
1314                                     },
1315                                     {
1316                                         'name': 'coitem_qtyreserved'
1317                                     }
1318                                 ]
1319                             }
1320                         },
1321                         toolbar : {
1322                             xtype: 'Toolbar',
1323                             xns: Roo,
1324                             items : [
1325                                 {
1326                                     xtype: 'Button',
1327                                     xns: Roo.Toolbar,
1328                                     listeners : {
1329                                         click : function()
1330                                         {
1331                                             
1332                                             
1333                                             if (_this.form.findField('cmhead_posted').getValue() == 'true') {
1334                                                 Roo.MessageBox.alert("Error", "credit memo is already posted");
1335                                                 return;
1336                                             }
1337                                             // work out last 
1338                                                // work out last 
1339                                                         var grid = _this.grid;
1340                                                         var last = 0;
1341                                                         
1342                                                         _this.grid.ds.each(function(r) {
1343                                                             last = r.data.cmitem_linenumber;
1344                                                         });
1345                                                         
1346                                                         last++;
1347                                                         grid.stopEditing();
1348                                                         var nr = _this.grid.ds.reader.newRow({
1349                                                             cmitem_linenumber : last,
1350                                                             item_number : '',
1351                                                             item_descrip1 : '',
1352                                                             cmitem_cmhead_id : _this.form.findField('cmhead_id').getValue(),
1353                                                             cmitem_comments : '',
1354                                                             cmitem_taxtype_id : _this.data.default_taxtype_id,
1355                                                             cmitem_taxtype_id_taxtype_name : 'Taxable'
1356                                                         });
1357                                                         grid.stopEditing();
1358                                                         grid.ds.insert(grid.ds.getCount(), nr); 
1359                                                         grid.startEditing(grid.ds.getCount()-1, 1); // type..
1360                                         },
1361                                         render : function (_self)
1362                                         {
1363                                             _this.addItemBtn = _self;
1364                                         }
1365                                     },
1366                                     cls : 'x-btn-text-icon',
1367                                     text : "Add",
1368                                     icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
1369                                 },
1370                                 {
1371                                     xtype: 'Button',
1372                                     xns: Roo.Toolbar,
1373                                     listeners : {
1374                                         click : function (_self, e)
1375                                         {
1376                                             
1377                                             var last = 1;    
1378                                             _this.grid.ds.each(function(r) {
1379                                                 last = r.data.cmitem_linenumber +1;
1380                                         
1381                                             
1382                                             });
1383                                             
1384                                             var grid = _this.grid;
1385                                             var ct  =    _this.grid.ds.getCount();\r
1386                                             var lastrow = ct ?  _this.grid.ds.getAt(ct-1)  : false;\r
1387                                                  \r
1388                                             var cmhead_cust_id = _this.form.findField('cmhead_cust_id').getValue();
1389                                             var cmhead_id = _this.form.findField('cmhead_id').getValue();
1390                                             
1391                                             Pman.Dialog.XtupleSalesProductList.show( {cohead_cust_id : cmhead_cust_id, cmhead_id : cmhead_id} , function(res) {
1392                                             
1393                                                 Roo.log(res);
1394                                                 grid.stopEditing();
1395                                         
1396                                                 if (lastrow) {
1397                                                     var lr = lastrow;
1398                                                     if (!lr.data.cmitem_itemsite_id) {
1399                                                         lr.set('cmitem_itemsite_id', res.item_itemsite_id_itemsite_id);
1400                                                         lr.set('item_number',  res.item_number);
1401                                                         lr.set('item_descrip1', res.item_descrip1);                                
1402                                                         lr.set('cmitem_comments', res.item_descrip1);
1403                                                         lr.set('cmitem_cmhead_id', _this.form.findField('cmhead_id').getValue());
1404                                                         return;
1405                                                     }
1406                                                 }
1407                                                 var rate = _this.form.findField('taxzone_rate').getValue();\r
1408                                                  var nr = grid.ds.reader.newRow({
1409                                                             cmitem_linenumber : last,
1410                                                             cmitem_itemsite_id : res.item_itemsite_id_itemsite_id,
1411                                                             item_number :  res.item_number,
1412                                                             item_descrip1 : res.item_descrip1 ,
1413                                                             cmitem_cmhead_id : _this.form.findField('cmhead_id').getValue(),
1414                                                             cmitem_comments : res.item_descrip1,
1415                                                             cmitem_tax_listprice : res.item_price * ( 1 + rate * 1),
1416                                                             cmitem_item_listprice : res.item_price,
1417                                                             cmitem_tax_unitprice : res.item_price * ( 1 + rate * 1),
1418                                                             cmitem_unitprice : res.item_price,
1419                                                             cmitem_qtycredit : 1,
1420                                                             cmitem_line_value : res.item_price,
1421                                                             cmitem_line_tax_value : res.item_price * ( 1 + rate * 1)
1422                                                             
1423                                                         });
1424                                                 grid.ds.insert(grid.ds.getCount(), nr);
1425                                                 
1426                                                 var ar = grid.ds.getAt(grid.ds.getCount() - 1);
1427                                                 ar.commit();
1428                                                 
1429                                            }); 
1430                                         }
1431                                     },
1432                                     cls : 'x-btn-text-icon',
1433                                     text : "Find Products",
1434                                     icon : rootURL + '/Pman/templates/images/search.gif'
1435                                 },
1436                                 {
1437                                     xtype: 'Button',
1438                                     xns: Roo.Toolbar,
1439                                     listeners : {
1440                                         click : function ()
1441                                         {
1442                                             var cmhead_id = 1 * _this.form.findField('cmhead_id').getValue();
1443                                             if (!cmhead_id) {
1444                                                 Roo.MessageBox.alert("Error", "Save credit memo first!");
1445                                                 return;
1446                                             
1447                                             }
1448                                             
1449                                             new Pman.Download({
1450                                                 url : baseURL + '/Roo/Metasql',
1451                                                 method : 'GET',
1452                                                 timeout: 600000,
1453                                                 params : {
1454                                                     _group : 'cmhead',
1455                                                     _name : 'items',
1456                                                     'cmhead_id:number' : cmhead_id,
1457                                                     csvCols : '*',
1458                                                     csvTitles : '*', 
1459                                                     limit : 9999       
1460                                                 }
1461                                             });   
1462                                                     
1463                                            
1464                                         }
1465                                     },
1466                                     cls : 'x-btn-text-icon',
1467                                     text : "Download Excel",
1468                                     icon : rootURL + '/Pman/templates/images/spreadsheet.gif'
1469                                 },
1470                                 {
1471                                     xtype: 'Button',
1472                                     xns: Roo.Toolbar,
1473                                     listeners : {
1474                                         click : function (_self, e)
1475                                         {
1476                                            
1477                                             if (_this.form.findField('cmhead_posted').getValue() == 'true') {
1478                                                 Roo.MessageBox.alert("Error", "credit memo is already posted");
1479                                                 return;
1480                                             }
1481                                            
1482                                             Pman.Dialog.Image.show(
1483                                                {
1484                                                     _url : baseURL + '/Xtuple/Import/CreditMemo',
1485                                                     onid : _this.form.findField('cmhead_id').getValue()
1486                                                 
1487                                                },
1488                                                function (res) {
1489                                                     _this.grid.ds.load({});\r
1490                                                }
1491                                            );
1492                                         },
1493                                         render : function (_self)
1494                                         {
1495                                             _this.uploadBtn = _self;
1496                                         }
1497                                     },
1498                                     cls : 'x-btn-text-icon',
1499                                     text : "Upload Excel",
1500                                     icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
1501                                 },
1502                                 {
1503                                     xtype: 'Fill',
1504                                     xns: Roo.Toolbar
1505                                 },
1506                                 {
1507                                     xtype: 'Button',
1508                                     xns: Roo.Toolbar,
1509                                     listeners : {
1510                                         click : function()
1511                                         {
1512                                              _this.grid.stopEditing();
1513                                             // check that no shipments or invoices are done..
1514                                          if (_this.form.findField('cmhead_posted').getValue() == 'true') {
1515                                                 Roo.MessageBox.alert("Error", "credit memo is already posted");
1516                                                 return;
1517                                             }
1518                                                                                     // check that no shipments or invoices are done..
1519                                             var rc = _this.grid.getSelectionModel().getSelectedCell();
1520                                             
1521                                             var rec = _this.grid.ds.getAt(rc[0]);
1522                                             
1523                                             Roo.MessageBox.confirm("Confirm", "Are you sure you want to delete that line?", function(r)
1524                                             {
1525                                                 if (r != 'yes') {
1526                                                     return;
1527                                                 }
1528                                                 remove();
1529                                             });
1530                                             
1531                                             if (!rec.data.cmitem_id) {
1532                                                 _this.grid.ds.remove(rec);
1533                                                 return;
1534                                             }
1535                                             function remove()
1536                                             {
1537                                                 new  Pman.Request({
1538                                                     url : baseURL + '/Roo/cmitem',
1539                                                     method : 'POST',
1540                                                     params : {
1541                                                         _delete : rec.data.cmitem_id
1542                                                     
1543                                                     },
1544                                                     success : function() {
1545                                                         if (rec.data.item_type == 'K') {
1546                                                             _this.grid.ds.load({});
1547                                                             return;
1548                                                         }
1549                                                         _this.grid.ds.remove(rec);
1550                                                     }
1551                                                 
1552                                                 });
1553                                             }
1554                                         
1555                                             
1556                                             
1557                                         }
1558                                     },
1559                                     cls : 'x-btn-text-icon',
1560                                     text : "Delete",
1561                                     icon : rootURL + '/Pman/templates/images/trash.gif'
1562                                 }
1563                             ]
1564                         },
1565                         colModel : [
1566                             {
1567                                 xtype: 'ColumnModel',
1568                                 xns: Roo.grid,
1569                                 dataIndex : 'cmitem_linenumber',
1570                                 header : 'Item#',
1571                                 width : 60,
1572                                 renderer : function(v,x,r) {
1573                                 
1574                                     if (r.data.coitem_subnumber * 1 > 0) {
1575                                          return String.format('{0}.{1}', v,r.data.coitem_subnumber);
1576                                      }
1577                                      return String.format('{0}', v);
1578                                   }
1579                             },
1580                             {
1581                                 xtype: 'ColumnModel',
1582                                 xns: Roo.grid,
1583                                 dataIndex : 'item_number',
1584                                 header : 'Item Code',
1585                                 width : 75,
1586                                 renderer : function(v) { return String.format('{0}', v); },
1587                                 editor : {
1588                                     xtype: 'GridEditor',
1589                                     xns: Roo.grid,
1590                                     field : {
1591                                         xtype: 'ComboBox',
1592                                         xns: Roo.form,
1593                                         listeners : {
1594                                             beforeselect : function (combo, record, index)
1595                                             {
1596                                               // set _this.data values ..
1597                                               var ar = _this.grid.activeEditor.record;
1598                                               //Roo.log('beforeselect');
1599                                               
1600                                               var rate = _this.form.findField('taxzone_rate').getValue();
1601                                               
1602                                               (function() { 
1603                                                 
1604                                                 ar.set('item_descrip1', record.data.itemsite_item_id_item_descrip1);
1605                                                 ar.set('cmitem_comments', record.data.itemsite_item_id_item_descrip1);        
1606                                                 ar.set('cmitem_tax_listprice', record.data.item_listprice * ( 1 + rate * 1) );
1607                                                 ar.set('cmitem_item_listprice', record.data.item_listprice * 1);
1608                                                 ar.set('cmitem_tax_unitprice', record.data.item_price * ( 1 + rate * 1) );
1609                                                 ar.set('cmitem_unitprice', record.data.item_price * 1);
1610                                                 ar.set('cmitem_qtycredit', 1);
1611                                                 ar.set('cmitem_line_value', record.data.item_price * ar.data.cmitem_qtycredit);
1612                                                 ar.set('cmitem_line_tax_value', ar.data.cmitem_tax_unitprice * ar.data.cmitem_qtycredit);
1613                                                 ar.set('cmitem_itemsite_id', record.data.itemsite_id);
1614                                                 ar.set('item_number', record.data.itemsite_item_id_item_number);
1615                                                 ar.set('item_type', record.data.itemsite_item_id_item_type);
1616                                                 ar.set('avail_qty', 0);
1617                                               //  ar.updateFields = ['All'];
1618                                                 ar.commit();
1619                                               }).defer(100);
1620                                               
1621                                             }
1622                                         },
1623                                         allowBlank : false,
1624                                         displayField : 'itemsite_item_id_item_number',
1625                                         editable : true,
1626                                         emptyText : "Select item",
1627                                         forceSelection : true,
1628                                         hiddenName : 'itemsite_item_id_item_number',
1629                                         listWidth : 400,
1630                                         loadingText : "Searching...",
1631                                         minChars : 2,
1632                                         name : 'item_number',
1633                                         pageSize : 20,
1634                                         qtip : "Select item",
1635                                         queryParam : 'query[number]',
1636                                         selectOnFocus : true,
1637                                         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>',
1638                                         triggerAction : 'all',
1639                                         typeAhead : false,
1640                                         valueField : 'item_number',
1641                                         store : {
1642                                             xtype: 'Store',
1643                                             xns: Roo.data,
1644                                             listeners : {
1645                                                 beforeload : function (_self, o){
1646                                                     o.params = o.params || {};
1647                                                     o.params.customer_id = _this.form.findField('cmhead_cust_id').getValue();
1648                                                     o.params['query[cmhead_id]'] = _this.form.findField('cmhead_id').getValue();
1649                                                     //o.params.shipto_cust_id = _this.data.cohead_cust_id;
1650                                                     // set more here
1651                                                 }
1652                                             },
1653                                             remoteSort : true,
1654                                             sortInfo : { direction : 'ASC', field: 'item_number' },
1655                                             proxy : {
1656                                                 xtype: 'HttpProxy',
1657                                                 xns: Roo.data,
1658                                                 method : 'GET',
1659                                                 url : baseURL + '/Roo/itemsite.php'
1660                                             },
1661                                             reader : {
1662                                                 xtype: 'JsonReader',
1663                                                 xns: Roo.data,
1664                                                 id : 'shipto_id',
1665                                                 root : 'data',
1666                                                 totalProperty : 'total',
1667                                                 fields : [{'name':'item_id','type':'int'},'item_number']
1668                                             }
1669                                         }
1670                                     }
1671                                 }
1672                             },
1673                             {
1674                                 xtype: 'ColumnModel',
1675                                 xns: Roo.grid,
1676                                 dataIndex : 'cmitem_comments',
1677                                 header : 'Item Description',
1678                                 width : '150.00',
1679                                 renderer : function(v,x,r) { 
1680                                 
1681                                     if (!v.length) {
1682                                         r.set('cmitem_comments', r.data.item_descrip1);
1683                                         v = r.data.item_descrip1;
1684                                     }
1685                                     if (v && v.length > 49) {
1686                                         return String.format('<span style="color:orange" qtip="line may be too long to print">{0}</span>', v);
1687                                     }
1688                                     return String.format('{0}', v); 
1689                                         
1690                                 },
1691                                 editor : {
1692                                     xtype: 'GridEditor',
1693                                     xns: Roo.grid,
1694                                     field : {
1695                                         xtype: 'TextField',
1696                                         xns: Roo.form,
1697                                         allowBlank : false
1698                                     }
1699                                 }
1700                             },
1701                             {
1702                                 xtype: 'ColumnModel',
1703                                 xns: Roo.grid,
1704                                 align : 'right',
1705                                 dataIndex : 'cmitem_qtycredit',
1706                                 header : 'Qty',
1707                                 width : 50,
1708                                 renderer : function(v)
1709                                 {
1710                                     return String.format('{0}', v ? parseInt(v) : '');
1711                                 },
1712                                 editor : {
1713                                     xtype: 'GridEditor',
1714                                     xns: Roo.grid,
1715                                     field : {
1716                                         xtype: 'NumberField',
1717                                         xns: Roo.form,
1718                                         listeners : {
1719                                             focus : function (_self)
1720                                             {
1721                                                 if (this.value == 0) {
1722                                                     this.el.dom.value = '';
1723                                                 }
1724                                             }
1725                                         },
1726                                         allowDecimals : false,
1727                                         decimalPrecision : 0,
1728                                         minValue : 1,
1729                                         style : 'text-align:right'
1730                                     }
1731                                 }
1732                             },
1733                             {
1734                                 xtype: 'ColumnModel',
1735                                 xns: Roo.grid,
1736                                 align : 'right',
1737                                 dataIndex : 'cmitem_tax_listprice',
1738                                 header : 'List Price w. tax',
1739                                 width : 80,
1740                                 renderer : function(v,x,r)
1741                                 {
1742                                  //   var rate = _this.form.findField('taxzone_rate').getValue();
1743                                  //   v = v * (1 + rate * 1);
1744                                  
1745                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1746                                 }
1747                             },
1748                             {
1749                                 xtype: 'ColumnModel',
1750                                 xns: Roo.grid,
1751                                 align : 'right',
1752                                 dataIndex : 'cmitem_taxtype_id',
1753                                 header : 'Taxed',
1754                                 width : 50,
1755                                 renderer : function(v,x,r) { return String.format('{0}', r.data.cmitem_taxtype_id_taxtype_name); },
1756                                 editor : {
1757                                     xtype: 'GridEditor',
1758                                     xns: Roo.grid,
1759                                     field : {
1760                                         xtype: 'ComboBox',
1761                                         xns: Roo.form,
1762                                         allowBlank : false,
1763                                         displayField : 'taxtype_name',
1764                                         editable : false,
1765                                         emptyText : "Select Tax Type",
1766                                         forceSelection : true,
1767                                         hiddenName : 'cmitem_taxtype_id',
1768                                         listWidth : 400,
1769                                         loadingText : "Searching...",
1770                                         minChars : 2,
1771                                         name : 'cmitem_taxtype_id_taxtype_name',
1772                                         pageSize : 20,
1773                                         qtip : "Select taxtype",
1774                                         queryParam : 'query[taxtype_id]',
1775                                         selectOnFocus : true,
1776                                         tpl : '<div class="x-grid-cell-text x-btn button"><b>{taxtype_name}</b> </div>',
1777                                         triggerAction : 'all',
1778                                         typeAhead : true,
1779                                         valueField : 'taxtype_id',
1780                                         width : 285,
1781                                         store : {
1782                                             xtype: 'Store',
1783                                             xns: Roo.data,
1784                                             listeners : {
1785                                                 beforeload : function (_self, o){
1786                                                     o.params = o.params || {};
1787                                                     // set more here
1788                                                     
1789                                                 /*    o.params.with_date = _this.form.findField('cohead_orderdate').getValue().format('Y-m-d'); 
1790                                                     Roo.log("with date?" + o.params.with_date);*/
1791                                                     
1792                                                     
1793                                                 }
1794                                             },
1795                                             remoteSort : true,
1796                                             sortInfo : { direction : 'ASC', field: 'taxtype_name' },
1797                                             proxy : {
1798                                                 xtype: 'HttpProxy',
1799                                                 xns: Roo.data,
1800                                                 method : 'GET',
1801                                                 url : baseURL + '/Roo/taxtype.php'
1802                                             },
1803                                             reader : {
1804                                                 xtype: 'JsonReader',
1805                                                 xns: Roo.data,
1806                                                 id : 'taxtype_id',
1807                                                 root : 'data',
1808                                                 totalProperty : 'total',
1809                                                 fields : [{'name':'taxtype_id','type':'int'},'taxtype_name']
1810                                             }
1811                                         }
1812                                     }
1813                                 }
1814                             },
1815                             {
1816                                 xtype: 'ColumnModel',
1817                                 xns: Roo.grid,
1818                                 align : 'right',
1819                                 dataIndex : 'cmitem_tax_unitprice',
1820                                 header : 'Unit Price w. tax',
1821                                 width : 90,
1822                                 renderer : function(v,x,r)
1823                                 {
1824                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1825                                 },
1826                                 editor : {
1827                                     xtype: 'GridEditor',
1828                                     xns: Roo.grid,
1829                                     field : {
1830                                         xtype: 'NumberField',
1831                                         xns: Roo.form,
1832                                         listeners : {
1833                                             focus : function (_self)
1834                                             {
1835                                                 if (this.value == 0.0) {
1836                                                     this.el.dom.value = '';
1837                                                 }
1838                                             }
1839                                         },
1840                                         decimalPrecision : 2,
1841                                         minValue : 0,
1842                                         style : 'text-align:right'
1843                                     }
1844                                 }
1845                             },
1846                             {
1847                                 xtype: 'ColumnModel',
1848                                 xns: Roo.grid,
1849                                 align : 'right',
1850                                 dataIndex : 'cmitem_unitprice',
1851                                 header : 'Unit Price',
1852                                 width : 75,
1853                                 renderer : function(v)
1854                                 {
1855                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1856                                 },
1857                                 editor : {
1858                                     xtype: 'GridEditor',
1859                                     xns: Roo.grid,
1860                                     field : {
1861                                         xtype: 'NumberField',
1862                                         xns: Roo.form,
1863                                         listeners : {
1864                                             focus : function (_self)
1865                                             {
1866                                                 if (this.value == 0.0) {
1867                                                     this.el.dom.value = '';
1868                                                 }
1869                                             }
1870                                         },
1871                                         decimalPrecision : 2,
1872                                         minValue : 0,
1873                                         style : 'text-align:right'
1874                                     }
1875                                 }
1876                             },
1877                             {
1878                                 xtype: 'ColumnModel',
1879                                 xns: Roo.grid,
1880                                 align : 'right',
1881                                 dataIndex : 'cmitem_line_value',
1882                                 header : 'Total',
1883                                 width : 75,
1884                                 renderer : function(v)
1885                                 {
1886                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1887                                 }
1888                             },
1889                             {
1890                                 align : 'right',
1891                                 dataIndex : 'cmitem_line_tax_value',
1892                                 header : 'Total w. tax',
1893                                 width : 80,
1894                                 renderer : function(v,x,r)
1895                                 {
1896                                     return String.format('{0}', v ? parseFloat(v).toFixed(2) : '');
1897                                 },
1898                                 xns : Roo.grid
1899                             }
1900                         ]
1901                     }
1902                 },
1903                 {
1904                     xtype: 'GridPanel',
1905                     xns: Roo,
1906                     listeners : {
1907                         activate : function() {
1908                             _this.cpanel = this;
1909                             
1910                             var id = _this.form.findField('cmhead_id').getValue() * 1;
1911                             if (id < 1) {
1912                                 Roo.MessageBox.alert("Error", "save the credit memo first!");
1913                                 _this.dialog.layout.getRegion('center').showPanel(0);
1914                                 return;
1915                             }
1916                             
1917                             if (_this.cgrid) {
1918                                 _this.cgrid.footer.onClick('first');
1919                             }
1920                         }
1921                     },
1922                     background : true,
1923                     fitContainer : true,
1924                     fitToframe : true,
1925                     region : 'center',
1926                     tableName : 'checkitem',
1927                     title : "Miscellaneous Check",
1928                     grid : {
1929                         xtype: 'Grid',
1930                         xns: Roo.grid,
1931                         listeners : {
1932                             render : function() 
1933                             {
1934                                 _this.cgrid = this; 
1935                                 //_this.dialog = Pman.Dialog.FILL_IN
1936                                 if (_this.cpanel.active) {
1937                                    this.footer.onClick('first');
1938                                 }
1939                             }
1940                         },
1941                         autoExpandColumn : 'checkitem_checkhead_id_checkhead_notes',
1942                         loadMask : true,
1943                         dataSource : {
1944                             xtype: 'Store',
1945                             xns: Roo.data,
1946                             listeners : {
1947                                 beforeload : function (_self, options)
1948                                 {
1949                                     options.params = options.params || {};
1950                                     options.params.checkitem_cmnumber = _this.form.findField('cmhead_number').getValue();
1951                                 }
1952                             },
1953                             remoteSort : true,
1954                             sortInfo : { field : 'checkitem_id', direction: 'DESC' },
1955                             proxy : {
1956                                 xtype: 'HttpProxy',
1957                                 xns: Roo.data,
1958                                 method : 'GET',
1959                                 url : baseURL + '/Roo/checkitem.php'
1960                             },
1961                             reader : {
1962                                 xtype: 'JsonReader',
1963                                 xns: Roo.data,
1964                                 id : 'id',
1965                                 root : 'data',
1966                                 totalProperty : 'total',
1967                                 fields : [
1968                                     {
1969                                         'name': 'id',
1970                                         'type': 'int'
1971                                     },
1972                                     {
1973                                         'name': 'name',
1974                                         'type': 'string'
1975                                     },
1976                                     {
1977                                         'name': 'type',
1978                                         'type': 'int'
1979                                     },
1980                                     {
1981                                         'name': 'leader',
1982                                         'type': 'int'
1983                                     },
1984                                     {
1985                                         'name': 'leader_id',
1986                                         'type': 'int'
1987                                     },
1988                                     {
1989                                         'name': 'leader_office_id',
1990                                         'type': 'int'
1991                                     },
1992                                     {
1993                                         'name': 'leader_name',
1994                                         'type': 'string'
1995                                     },
1996                                     {
1997                                         'name': 'leader_phone',
1998                                         'type': 'string'
1999                                     },
2000                                     {
2001                                         'name': 'leader_fax',
2002                                         'type': 'string'
2003                                     },
2004                                     {
2005                                         'name': 'leader_email',
2006                                         'type': 'string'
2007                                     },
2008                                     {
2009                                         'name': 'leader_company_id',
2010                                         'type': 'int'
2011                                     },
2012                                     {
2013                                         'name': 'leader_role',
2014                                         'type': 'string'
2015                                     },
2016                                     {
2017                                         'name': 'leader_active',
2018                                         'type': 'int'
2019                                     },
2020                                     {
2021                                         'name': 'leader_remarks',
2022                                         'type': 'string'
2023                                     },
2024                                     {
2025                                         'name': 'leader_passwd',
2026                                         'type': 'string'
2027                                     },
2028                                     {
2029                                         'name': 'leader_owner_id',
2030                                         'type': 'int'
2031                                     },
2032                                     {
2033                                         'name': 'leader_lang',
2034                                         'type': 'string'
2035                                     },
2036                                     {
2037                                         'name': 'leader_no_reset_sent',
2038                                         'type': 'int'
2039                                     },
2040                                     {
2041                                         'name': 'leader_action_type',
2042                                         'type': 'string'
2043                                     },
2044                                     {
2045                                         'name': 'leader_project_id',
2046                                         'type': 'int'
2047                                     },
2048                                     {
2049                                         'name': 'leader_deleted_by',
2050                                         'type': 'int'
2051                                     },
2052                                     {
2053                                         'name': 'leader_deleted_dt',
2054                                         'type': 'date'
2055                                     },
2056                                     {
2057                                         'name': 'leader_firstname',
2058                                         'type': 'string'
2059                                     },
2060                                     {
2061                                         'name': 'leader_lastname',
2062                                         'type': 'string'
2063                                     },
2064                                     {
2065                                         'name': 'leader_name_facebook',
2066                                         'type': 'string'
2067                                     },
2068                                     {
2069                                         'name': 'leader_url_blog',
2070                                         'type': 'string'
2071                                     },
2072                                     {
2073                                         'name': 'leader_url_twitter',
2074                                         'type': 'string'
2075                                     },
2076                                     {
2077                                         'name': 'leader_url_linkedin',
2078                                         'type': 'string'
2079                                     },
2080                                     {
2081                                         'name': 'leader_crm_lead_percentage',
2082                                         'type': 'int'
2083                                     },
2084                                     {
2085                                         'name': 'leader_crm_industry_id',
2086                                         'type': 'int'
2087                                     },
2088                                     {
2089                                         'name': 'leader_crm_updated_action_id',
2090                                         'type': 'int'
2091                                     },
2092                                     {
2093                                         'name': 'leader_crm_created_action_id',
2094                                         'type': 'int'
2095                                     },
2096                                     {
2097                                         'name': 'leader_crm_type_id',
2098                                         'type': 'int'
2099                                     }
2100                                 ]
2101                             }
2102                         },
2103                         footer : {
2104                             xtype: 'PagingToolbar',
2105                             xns: Roo,
2106                             displayInfo : true,
2107                             displayMsg : "Displaying check item{0} - {1} of {2}",
2108                             emptyMsg : "No check item found",
2109                             pageSize : 25
2110                         },
2111                         colModel : [
2112                             {
2113                                 xtype: 'ColumnModel',
2114                                 xns: Roo.grid,
2115                                 dataIndex : 'checkitem_docdate',
2116                                 header : 'Date',
2117                                 width : 75,
2118                                 renderer : function(v) { return String.format('{0}', v ? v.format('Y-m-d') : ''); }
2119                             },
2120                             {
2121                                 xtype: 'ColumnModel',
2122                                 xns: Roo.grid,
2123                                 dataIndex : 'checkitem_bankaccnt_id_bankaccnt_name',
2124                                 header : 'Bank Account',
2125                                 width : 100,
2126                                 renderer : function(v) { return String.format('{0}', v ? v : ''); }
2127                             },
2128                             {
2129                                 xtype: 'ColumnModel',
2130                                 xns: Roo.grid,
2131                                 dataIndex : 'checkitem_checkhead_id_checkhead_for',
2132                                 header : 'Memo',
2133                                 width : 75,
2134                                 renderer : function(v) { return String.format('{0}', v ? v : ''); }
2135                             },
2136                             {
2137                                 xtype: 'ColumnModel',
2138                                 xns: Roo.grid,
2139                                 dataIndex : 'checkitem_checkhead_id_checkhead_notes',
2140                                 header : 'Notes',
2141                                 width : 100,
2142                                 renderer : function(v) { return String.format('{0}', v ? v : ''); }
2143                             },
2144                             {
2145                                 xtype: 'ColumnModel',
2146                                 xns: Roo.grid,
2147                                 align : 'right',
2148                                 dataIndex : 'checkitem_curr_id_curr_name',
2149                                 header : 'Currency',
2150                                 width : 75,
2151                                 renderer : function(v) { return String.format('{0}', v ? v : ''); }
2152                             },
2153                             {
2154                                 xtype: 'ColumnModel',
2155                                 xns: Roo.grid,
2156                                 align : 'right',
2157                                 dataIndex : 'checkitem_amount',
2158                                 header : 'Amount',
2159                                 width : 75,
2160                                 renderer : function(v) { return String.format('{0}', v ? parseFloat(v).toFixed(2) : ''); }
2161                             }
2162                         ]
2163                     }
2164                 },
2165                 {
2166                     xtype: 'GridPanel',
2167                     xns: Roo,
2168                     listeners : {
2169                         activate : function() {
2170                             _this.apanel = this;
2171                             if (_this.agrid) {
2172                                 _this.agrid.footer.onClick('first');
2173                             }
2174                         }
2175                     },
2176                     background : false,
2177                     fitContainer : true,
2178                     fitToframe : true,
2179                     region : 'center',
2180                     tableName : 'invchead',
2181                     title : "Applications",
2182                     grid : {
2183                         xtype: 'Grid',
2184                         xns: Roo.grid,
2185                         listeners : {
2186                             render : function() 
2187                             {
2188                                 _this.agrid = this; 
2189                                 //_this.dialog = Pman.Dialog.FILL_IN
2190                                 if (_this.apanel.active) {
2191                                    this.footer.onClick('first');
2192                                 }
2193                             }
2194                         },
2195                         autoExpandColumn : 'arapply_target_docnumber',
2196                         loadMask : true,
2197                         dataSource : {
2198                             xtype: 'Store',
2199                             xns: Roo.data,
2200                             listeners : {
2201                                 beforeload : function (_self,o) {
2202                                 
2203                                     try {
2204                                        this.removeAll();
2205                                     } catch (e) { }
2206                                 
2207                                     if (!_this.data || !_this.data.cmhead_id) {
2208                                         return false;
2209                                     }
2210                                     o.params = o.params || {};
2211                                     
2212                                     o.params._application = _this.data.cmhead_id
2213                                     
2214                                 }
2215                             },
2216                             remoteSort : true,
2217                             sortInfo : { field : 'arapply_id', direction: 'ASC' },
2218                             proxy : {
2219                                 xtype: 'HttpProxy',
2220                                 xns: Roo.data,
2221                                 method : 'GET',
2222                                 url : baseURL + '/Roo/Arapply.php'
2223                             },
2224                             reader : {
2225                                 xtype: 'JsonReader',
2226                                 xns: Roo.data,
2227                                 totalProperty : 'total',
2228                                 root : 'data',
2229                                 id : 'id',
2230                                 fields : [
2231                                     {
2232                                         'name': 'id',
2233                                         'type': 'int'
2234                                     },
2235                                     {
2236                                         'name': 'name',
2237                                         'type': 'string'
2238                                     },
2239                                     {
2240                                         'name': 'type',
2241                                         'type': 'int'
2242                                     },
2243                                     {
2244                                         'name': 'leader',
2245                                         'type': 'int'
2246                                     },
2247                                     {
2248                                         'name': 'leader_id',
2249                                         'type': 'int'
2250                                     },
2251                                     {
2252                                         'name': 'leader_office_id',
2253                                         'type': 'int'
2254                                     },
2255                                     {
2256                                         'name': 'leader_name',
2257                                         'type': 'string'
2258                                     },
2259                                     {
2260                                         'name': 'leader_phone',
2261                                         'type': 'string'
2262                                     },
2263                                     {
2264                                         'name': 'leader_fax',
2265                                         'type': 'string'
2266                                     },
2267                                     {
2268                                         'name': 'leader_email',
2269                                         'type': 'string'
2270                                     },
2271                                     {
2272                                         'name': 'leader_company_id',
2273                                         'type': 'int'
2274                                     },
2275                                     {
2276                                         'name': 'leader_role',
2277                                         'type': 'string'
2278                                     },
2279                                     {
2280                                         'name': 'leader_active',
2281                                         'type': 'int'
2282                                     },
2283                                     {
2284                                         'name': 'leader_remarks',
2285                                         'type': 'string'
2286                                     },
2287                                     {
2288                                         'name': 'leader_passwd',
2289                                         'type': 'string'
2290                                     },
2291                                     {
2292                                         'name': 'leader_owner_id',
2293                                         'type': 'int'
2294                                     },
2295                                     {
2296                                         'name': 'leader_lang',
2297                                         'type': 'string'
2298                                     },
2299                                     {
2300                                         'name': 'leader_no_reset_sent',
2301                                         'type': 'int'
2302                                     },
2303                                     {
2304                                         'name': 'leader_action_type',
2305                                         'type': 'string'
2306                                     },
2307                                     {
2308                                         'name': 'leader_project_id',
2309                                         'type': 'int'
2310                                     },
2311                                     {
2312                                         'name': 'leader_deleted_by',
2313                                         'type': 'int'
2314                                     },
2315                                     {
2316                                         'name': 'leader_deleted_dt',
2317                                         'type': 'date'
2318                                     },
2319                                     {
2320                                         'name': 'leader_firstname',
2321                                         'type': 'string'
2322                                     },
2323                                     {
2324                                         'name': 'leader_lastname',
2325                                         'type': 'string'
2326                                     },
2327                                     {
2328                                         'name': 'leader_name_facebook',
2329                                         'type': 'string'
2330                                     },
2331                                     {
2332                                         'name': 'leader_url_blog',
2333                                         'type': 'string'
2334                                     },
2335                                     {
2336                                         'name': 'leader_url_twitter',
2337                                         'type': 'string'
2338                                     },
2339                                     {
2340                                         'name': 'leader_url_linkedin',
2341                                         'type': 'string'
2342                                     },
2343                                     {
2344                                         'name': 'leader_crm_lead_percentage',
2345                                         'type': 'int'
2346                                     },
2347                                     {
2348                                         'name': 'leader_crm_industry_id',
2349                                         'type': 'int'
2350                                     },
2351                                     {
2352                                         'name': 'leader_crm_updated_action_id',
2353                                         'type': 'int'
2354                                     },
2355                                     {
2356                                         'name': 'leader_crm_created_action_id',
2357                                         'type': 'int'
2358                                     },
2359                                     {
2360                                         'name': 'leader_crm_type_id',
2361                                         'type': 'int'
2362                                     }
2363                                 ]
2364                             }
2365                         },
2366                         footer : {
2367                             xtype: 'PagingToolbar',
2368                             xns: Roo,
2369                             displayInfo : true,
2370                             displayMsg : "Displaying application{0} - {1} of {2}",
2371                             emptyMsg : "No application found",
2372                             pageSize : 25
2373                         },
2374                         colModel : [
2375                             {
2376                                 xtype: 'ColumnModel',
2377                                 xns: Roo.grid,
2378                                 dataIndex : 'arapply_id',
2379                                 header : 'ID',
2380                                 width : 75,
2381                                 renderer : function(v) { return String.format('{0}', v); }
2382                             },
2383                             {
2384                                 xtype: 'ColumnModel',
2385                                 xns: Roo.grid,
2386                                 dataIndex : 'arapply_distdate',
2387                                 header : 'Date',
2388                                 width : 100,
2389                                 renderer : function(v) { return String.format('{0}', v ? v.format('Y-m-d') : ''); }
2390                             },
2391                             {
2392                                 xtype: 'ColumnModel',
2393                                 xns: Roo.grid,
2394                                 dataIndex : 'arapply_target_doctype',
2395                                 header : 'Target Doctype',
2396                                 width : 100,
2397                                 renderer : function(v) { return String.format('{0}', v); }
2398                             },
2399                             {
2400                                 xtype: 'ColumnModel',
2401                                 xns: Roo.grid,
2402                                 dataIndex : 'arapply_target_docnumber',
2403                                 header : 'Target Docnumber',
2404                                 width : 100,
2405                                 renderer : function(v) { return String.format('{0}', v); }
2406                             },
2407                             {
2408                                 xtype: 'ColumnModel',
2409                                 xns: Roo.grid,
2410                                 dataIndex : 'arapply_curr_id_curr_name',
2411                                 header : 'Currency',
2412                                 width : 75,
2413                                 renderer : function(v) { return String.format('{0}', v); }
2414                             },
2415                             {
2416                                 xtype: 'ColumnModel',
2417                                 xns: Roo.grid,
2418                                 dataIndex : 'arapply_applied',
2419                                 header : 'Applied',
2420                                 width : 75,
2421                                 renderer : function(v) { return String.format('{0}', v); }
2422                             }
2423                         ]
2424                     }
2425                 },
2426                 {
2427                     xtype: 'GridPanel',
2428                     xns: Roo,
2429                     listeners : {
2430                         activate : function() {
2431                             _this.hpanel = this;
2432                             if (_this.hgrid) {
2433                                 _this.hgrid.footer.onClick('first');
2434                             }
2435                         }
2436                     },
2437                     background : true,
2438                     fitContainer : true,
2439                     fitToframe : true,
2440                     region : 'center',
2441                     tableName : 'events',
2442                     title : "History",
2443                     grid : {
2444                         xtype: 'Grid',
2445                         xns: Roo.grid,
2446                         listeners : {
2447                             render : function() 
2448                             {
2449                                 _this.hgrid = this; 
2450                                 //_this.dialog = Pman.Dialog.FILL_IN
2451                                 if (_this.hpanel.active) {
2452                                    this.footer.onClick('first');
2453                                 }
2454                             },
2455                             rowdblclick : function (_self, rowIndex, e)
2456                             {
2457                                 if (!_this.dialog) return;
2458                                 _this.dialog.show( this.getDataSource().getAt(rowIndex).data, function() {
2459                                     _this.grid.footer.onClick('first');
2460                                 }); 
2461                             }
2462                         },
2463                         autoExpandColumn : 'remarks',
2464                         loadMask : true,
2465                         dataSource : {
2466                             xtype: 'Store',
2467                             xns: Roo.data,
2468                             listeners : {
2469                                 beforeload : function (_self, options)
2470                                 {
2471                                     options.params._related_on_table = 'cmhead';
2472                                     options.params._related_on_id = _this.form.findField('cmhead_id').getValue();
2473                                     
2474                                 }
2475                             },
2476                             remoteSort : true,
2477                             sortInfo : { field : 'event_when', direction: 'DESC' },
2478                             proxy : {
2479                                 xtype: 'HttpProxy',
2480                                 xns: Roo.data,
2481                                 method : 'GET',
2482                                 url : baseURL + '/Roo/events.php'
2483                             },
2484                             reader : {
2485                                 xtype: 'JsonReader',
2486                                 xns: Roo.data,
2487                                 totalProperty : 'total',
2488                                 root : 'data',
2489                                 id : 'id',
2490                                 fields : [
2491                                     {
2492                                         'name': 'event_when',
2493                                         'type': 'date'
2494                                     },
2495                                     {
2496                                         'name': 'action',
2497                                         'type': 'string'
2498                                     },
2499                                     {
2500                                         'name': 'ipaddr',
2501                                         'type': 'string'
2502                                     },
2503                                     {
2504                                         'name': 'person_id_name',
2505                                         'type': 'string'
2506                                     },
2507                                     {
2508                                         'name': 'remarks',
2509                                         'type': 'string'
2510                                     }
2511                                 ]
2512                             }
2513                         },
2514                         footer : {
2515                             xtype: 'PagingToolbar',
2516                             xns: Roo,
2517                             pageSize : 25,
2518                             displayInfo : true,
2519                             displayMsg : "Displaying events{0} - {1} of {2}",
2520                             emptyMsg : "No events found"
2521                         },
2522                         colModel : [
2523                             {
2524                                 xtype: 'ColumnModel',
2525                                 xns: Roo.grid,
2526                                 dataIndex : 'event_when',
2527                                 header : 'Changed',
2528                                 width : 120,
2529                                 renderer : function(v) { return String.format('{0}', v ? v.format('d/M/Y H:i:s') : ''); }
2530                             },
2531                             {
2532                                 xtype: 'ColumnModel',
2533                                 xns: Roo.grid,
2534                                 dataIndex : 'action',
2535                                 header : 'action',
2536                                 width : 120,
2537                                 renderer : function(v,x,r) { return String.format('{0} - {1}', v, r.data.on_table); }
2538                             },
2539                             {
2540                                 xtype: 'ColumnModel',
2541                                 xns: Roo.grid,
2542                                 dataIndex : 'ipaddr',
2543                                 header : 'IP address',
2544                                 width : 120,
2545                                 renderer : function(v) { return String.format('{0}', v); }
2546                             },
2547                             {
2548                                 xtype: 'ColumnModel',
2549                                 xns: Roo.grid,
2550                                 dataIndex : 'person_id_name',
2551                                 header : 'Who',
2552                                 width : 120,
2553                                 renderer : function(v) { return String.format('{0}', v); }
2554                             },
2555                             {
2556                                 xtype: 'ColumnModel',
2557                                 xns: Roo.grid,
2558                                 dataIndex : 'remarks',
2559                                 header : 'Notes',
2560                                 width : 200,
2561                                 renderer : function(v) { return String.format('{0}', v); }
2562                             }
2563                         ]
2564                     }
2565                 }
2566             ],
2567             center : {
2568                 xtype: 'LayoutRegion',
2569                 xns: Roo,
2570                 alwaysShowTabs : true,
2571                 tabPosition : 'top'
2572             },
2573             buttons : [
2574                 {
2575                     xtype: 'Button',
2576                     xns: Roo,
2577                     listeners : {
2578                         click : function (_self, e)
2579                         {
2580                               if (_this.grid)  _this.grid.stopEditing();
2581                             _this.dialog.hide();
2582                         }
2583                     },
2584                     text : "Cancel"
2585                 },
2586                 {
2587                     xtype: 'Button',
2588                     xns: Roo,
2589                     listeners : {
2590                         click : function (_self, e)
2591                         {
2592                           // do some checks?
2593                                if (_this.grid)  _this.grid.stopEditing();
2594                               if (_this.form.findField('cmhead_posted').getValue() == 'true') {
2595                                   Roo.MessageBox.alert("Error", "credit memo is already posted");
2596                                   return;
2597                               }
2598                               var loose = false;
2599                               var ar = [];
2600                               if (_this.grid && _this.grid.ds) {
2601                                   _this.grid.ds.each(function(rec) {
2602                                       if (!(rec.data.cmitem_itemsite_id * 1) || !(rec.data.cmitem_qtycredit*1) || !(rec.data.cmitem_unitprice*1)) {
2603                                           loose = true;
2604                                           return true;
2605                                       } 
2606                                       ar.push(rec);
2607                                       
2608                                   });
2609                                   if (loose) {
2610                                       Roo.MessageBox.alert("Error", "Some lines do not have product/qty/price set");
2611                                       return;
2612                                   }    
2613                               }
2614                               if(ar.length){
2615                                   _this.form.findField('has_item').setValue(ar.length);
2616                               } 
2617                           
2618                               _this.form.doAction("submit");
2619                         },
2620                         render : function (_self)
2621                         {
2622                         _this.saveBtn = _self;
2623                         }
2624                     },
2625                     text : "Save"
2626                 }
2627             ]
2628         });
2629     }
2630 };