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