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