Pman.Dialog.XtupleSalesOrder.bjs
[Pman.Xtuple] / Pman.Dialog.XtupleSalesOrder.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.XtupleSalesOrder = {
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 : 620,
41             modal : true,
42             resizable : true,
43             title : "Edit / Create Sales Order",
44             width : 1050,
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.cohead_id) {
54                                 return;
55                             }
56                             new Pman.Request({
57                                 method : 'GET',
58                                 url : baseURL + '/Roo/cohead',
59                                 params : { 
60                                     _id : _this.data.cohead_id
61                                 },
62                                 success : function(res) {
63                                     _this.form.findField('cohead_subtotal').setValue(res.data.cohead_subtotal);
64                                     _this.form.findField('cohead_tax').setValue(res.data.cohead_tax);
65                                     _this.form.findField('cohead_uninvoiced').setValue(res.data.cohead_uninvoiced);
66                                     _this.form.findField('cohead_unshipped').setValue(res.data.cohead_unshipped);
67                                    _this.form.findField('cohead_pretax_discount').setValue(res.data.cohead_pretax_discount); 
68                                     _this.form.findField('cohead_total').recalc(); 
69                                     _this.form.findField('cohead_misc').recalc(); 
70                                 }
71                             });
72                         }
73                     },
74                     region : 'center',
75                     title : "Order Details",
76                     items : [
77                         {
78                             xtype: 'Form',
79                             xns: Roo.form,
80                             listeners : {
81                                 actioncomplete : function(_self,action)
82                                 {
83                                     if (action.type == 'setdata') {
84                                         // clear the stock level cache...
85                                         _this.stockcache = [];
86                                         
87                                         _this.form.findField('billto_email').setValue('');
88                                         
89                                         _this.reversalBtn.hide();
90                                         
91                                         if (_this.data.cohead_id) {
92                                            this.load({ method: 'GET', params: { '_id' : _this.data.cohead_id }});
93                                            
94                                            return;
95                                         }
96                                         _this.dialog.setTitle("Edit New Sales Order");
97                                        
98                                         _this.closeBtn.hide();
99                                         _this.voidBtn.hide();
100                                         _this.saveBtn.show();
101                                             
102                                         if(_this.data.cohead_billto_cntct_id_cntct_id){
103                                             _this.form.findField('billto_address').update();
104                                             \r
105                                             _this.form.setValues({\r
106                                                 cohead_billto_cntct_id: _this.data.cohead_billto_cntct_id_cntct_id,\r
107                                                 cohead_billto_cntct_id_cntct_name : _this.data.cohead_billto_cntct_id_cntct_name,\r
108                                                 _shipto_same : 1,
109                                                 _same_as_order : 1
110                                                 \r
111                                             }); \r
112                                         }
113                                          
114                                        return;
115                                     }
116                                     if (action.type == 'load') {
117                                          
118                                         
119                                         _this.data = action.result.data;
120                                 
121                                         if (!_this.data.cohead_shipto_id && _this.data.shipto_id*1 > 0) {
122                                             _this.form.findField('cohead_shipto_id').setValue(_this.data.shipto_id);
123                                             _this.form.findField('cohead_shipto_id_shipto_name').setValue(_this.data.cohead_shipto_cntct_id_cntct_name);
124                                         
125                                         }
126                                         
127                                         
128                                         _this.dataloading = true;
129                                         if (_this.data.cohead_shipto_cntct_id == _this.data.cohead_billto_cntct_id) {
130                                             this.findField('_shipto_same').setValue(1);
131                                               Roo.log("set shipto 1");
132                                         } else {
133                                           this.findField('_shipto_same').setValue(0);
134                                            Roo.log("set shipto 0");
135                                         }
136                                         _this.dataloading = false;          
137                                         
138                                         
139                                         this.findField('billto_address').update();
140                                         this.findField('shipto_address').update();        
141                                         _this.dialog.setTitle("Edit Sales Order - " + this.findField('cohead_number').getValue());
142                                          
143                                         
144                                         if (_this.data.cohead_status == 'C') {
145                                             _this.closeBtn.show();
146                                             _this.closeBtn.setText('Re-open');
147                                              _this.voidBtn.hide();
148                                              _this.saveBtn.hide();
149                                              _this.reversalBtn.show();
150                                         }  else  if (_this.data.cohead_status == 'X') {
151                                             _this.closeBtn.hide(); 
152                                              _this.voidBtn.show(); 
153                                              _this.voidBtn.setText("Un-void / Re-open"); 
154                                              _this.saveBtn.hide();
155                                         } else {
156                                             // it's open
157                                              _this.closeBtn.show(); 
158                                             _this.closeBtn.setText('Complete and Close');             
159                                              _this.voidBtn.setText("Void");             
160                                              _this.voidBtn.show(); 
161                                              _this.saveBtn.show();        
162                                         }
163                                         
164                                          _this.form.findField('cohead_misc_per').update();
165                                        
166                                        // finally override the value for discount...
167                                        if ((''+ _this.data.cohead_misc_descrip).length) {
168                                             _this.form.findField('cohead_misc_descrip').setValue(_this.data.cohead_misc_descrip);
169                                        }
170                                        
171                                        // update the stockcache...
172                                 
173                                        new Pman.Request({
174                                             url : baseURL + '/Roo/cohead',
175                                             method : 'GET',
176                                             params : {
177                                                 _stockLevel : _this.data.cohead_id
178                                             },
179                                             success : function(res) 
180                                             {
181                                                 for (var i in res.data) {
182                                                    
183                                                     if(typeof(_this.stockcache[res.data[i].item]) == 'undefined'){
184                                                         _this.stockcache[res.data[i].item] = res.data[i];
185                                                     }
186                                                 }
187                                             }
188                                         });
189                                        
190                                         return;
191                                     }
192                                     if (action.type =='submit') {
193                                     
194                                 
195                                         var id = _this.form.findField('cohead_id').getValue() * 1;
196                                         if (id < 1) {
197                                 
198                                             _this.data.cohead_id = action.result.data.cohead_id;
199                                              this.load({ method: 'GET', params: { '_id' : _this.data.cohead_id }});
200                                             return;
201                                         }
202                                     
203                                         _this.dialog.hide();
204                                     
205                                          if (_this.callback) {
206                                             _this.callback.call(_this, _this.form.getValues());
207                                          }
208                                          _this.form.reset();
209                                          return;
210                                     }
211                                 },
212                                 rendered : function (form)
213                                 {
214                                     _this.form= form;
215                                 },
216                                 actionfailed : function (_self, action)
217                                 {
218                                     if (action.failureType == 'client') {
219                                         Roo.MessageBox.alert("Error", "Fill in all the required fields");
220                                     }
221                                     if (action.failureType == 'server') {    
222                                         Roo.log(action);
223                                         Roo.MessageBox.alert("Error", action.result.errorMsg);
224                                     }
225                                     _this.dialog.layout.getRegion('center').showPanel(0);
226                                 }
227                             },
228                             method : 'POST',
229                             style : 'margin:10px;',
230                             url : baseURL + '/Roo/cohead.php',
231                             items : [
232                                 {
233                                     xtype: 'Column',
234                                     xns: Roo.form,
235                                     width : '435',
236                                     items : [
237                                         {
238                                             xtype: 'FieldSet',
239                                             xns: Roo.form,
240                                             labelWidth : 120,
241                                             legend : "Order",
242                                             style : 'width:420px',
243                                             items : [
244                                                 {
245                                                     xtype: 'Column',
246                                                     xns: Roo.form,
247                                                     labelWidth : 100,
248                                                     width : 410,
249                                                     items : [
250                                                         {
251                                                             xtype: 'Row',
252                                                             xns: Roo.form,
253                                                             labelWidth : 100,
254                                                             width : 400,
255                                                             items : [
256                                                                 {
257                                                                     xtype: 'TextField',
258                                                                     xns: Roo.form,
259                                                                     allowBlank : true,
260                                                                     emptyText : "Automatic",
261                                                                     fieldLabel : 'Order#',
262                                                                     name : 'cohead_number',
263                                                                     readOnly : true,
264                                                                     width : 120
265                                                                 },
266                                                                 {
267                                                                     xtype: 'Row',
268                                                                     xns: Roo.form,
269                                                                     labelWidth : 50,
270                                                                     style : 'float:left',
271                                                                     width : 150,
272                                                                     items : [
273                                                                         {
274                                                                             xtype: 'TextField',
275                                                                             xns: Roo.form,
276                                                                             allowBlank : true,
277                                                                             fieldLabel : 'Cust#',
278                                                                             name : 'cohead_cust_id_cust_number',
279                                                                             readOnly : true,
280                                                                             width : 110
281                                                                         }
282                                                                     ]
283                                                                 }
284                                                             ]
285                                                         },
286                                                         {
287                                                             xtype: 'TextField',
288                                                             xns: Roo.form,
289                                                             editable : false,
290                                                             fieldLabel : 'Customer',
291                                                             forceSelection : true,
292                                                             hiddenName : 'cohead_cust_id',
293                                                             listWidth : 400,
294                                                             loadingText : "Searching...",
295                                                             minChars : 2,
296                                                             name : 'cohead_cust_id_cust_name',
297                                                             pageSize : 20,
298                                                             qtip : "Select custinfo",
299                                                             queryParam : 'query[cust_name]',
300                                                             readOnly : true,
301                                                             selectOnFocus : true,
302                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{cust_name}</b> </div>',
303                                                             triggerAction : 'all',
304                                                             typeAhead : true,
305                                                             valueField : 'cust_id',
306                                                             width : 300
307                                                         },
308                                                         {
309                                                             xtype: 'TextField',
310                                                             xns: Roo.form,
311                                                             fieldLabel : 'Customer PO#',
312                                                             name : 'cohead_custponumber',
313                                                             width : '150'
314                                                         },
315                                                         {
316                                                             xtype: 'DateField',
317                                                             xns: Roo.form,
318                                                             allowBlank : false,
319                                                             fieldLabel : 'Ordered',
320                                                             format : 'Y-m-d',
321                                                             name : 'cohead_orderdate',
322                                                             width : 100
323                                                         },
324                                                         {
325                                                             xtype: 'Row',
326                                                             xns: Roo.form,
327                                                             labelWidth : 100,
328                                                             width : 500,
329                                                             items : [
330                                                                 {
331                                                                     xtype: 'DateField',
332                                                                     xns: Roo.form,
333                                                                     allowBlank : false,
334                                                                     fieldLabel : 'Target Delivery',
335                                                                     format : 'Y-m-d',
336                                                                     name : 'cohead_targetdate',
337                                                                     width : 100
338                                                                 },
339                                                                 {
340                                                                     xtype: 'Row',
341                                                                     xns: Roo.form,
342                                                                     hideLabels : true,
343                                                                     items : [
344                                                                         {
345                                                                             xtype: 'Checkbox',
346                                                                             xns: Roo.form,
347                                                                             listeners : {
348                                                                                 check : function (_self, checked)
349                                                                                 {
350                                                                                     if(checked){
351                                                                                         _this.form.findField('cohead_targetdate').setValue(_this.form.findField('cohead_orderdate').getValue());
352                                                                                     }
353                                                                                 }
354                                                                             },
355                                                                             boxLabel : 'same as order',
356                                                                             inputValue : 1,
357                                                                             name : '_same_as_order'
358                                                                         }
359                                                                     ]
360                                                                 }
361                                                             ]
362                                                         },
363                                                         {
364                                                             xtype: 'ComboBox',
365                                                             xns: Roo.form,
366                                                             allowBlank : false,
367                                                             displayField : 'location_name',
368                                                             editable : true,
369                                                             emptyText : "Supply From",
370                                                             fieldLabel : 'Supply From',
371                                                             forceSelection : true,
372                                                             hiddenName : 'cohead_location_src',
373                                                             listWidth : 400,
374                                                             loadingText : "Searching...",
375                                                             minChars : 2,
376                                                             name : 'cohead_location_src_location_name',
377                                                             pageSize : 200,
378                                                             qtip : "Select terms",
379                                                             queryParam : 'query[location_name]',
380                                                             selectOnFocus : true,
381                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{location_name}</b> {location_descrip} </div>',
382                                                             triggerAction : 'all',
383                                                             typeAhead : false,
384                                                             valueField : 'location_id',
385                                                             width : 300,
386                                                             store : {
387                                                                 xtype: 'Store',
388                                                                 xns: Roo.data,
389                                                                 listeners : {
390                                                                     beforeload : function (_self, o){
391                                                                         o.params = o.params || {};
392                                                                         // set more here
393                                                                          o.params.location_netable = 1;
394                                                                           o.params.location_restrict = 0;
395                                                                           o.params._notinternalcompany = 1;
396                                                                     }
397                                                                 },
398                                                                 remoteSort : true,
399                                                                 sortInfo : { direction : 'ASC', field: 'location_name' },
400                                                                 proxy : {
401                                                                     xtype: 'HttpProxy',
402                                                                     xns: Roo.data,
403                                                                     method : 'GET',
404                                                                     url : baseURL + '/Roo/location.php'
405                                                                 },
406                                                                 reader : {
407                                                                     xtype: 'JsonReader',
408                                                                     xns: Roo.data,
409                                                                     id : 'location_id',
410                                                                     root : 'data',
411                                                                     totalProperty : 'total',
412                                                                     fields : [{"name":"location_id","type":"int"},"location_name"]
413                                                                 }
414                                                             }
415                                                         }
416                                                     ]
417                                                 }
418                                             ]
419                                         },
420                                         {
421                                             xtype: 'FieldSet',
422                                             xns: Roo.form,
423                                             labelWidth : 100,
424                                             legend : "Details",
425                                             style : 'width:420px',
426                                             items : [
427                                                 {
428                                                     xtype: 'Column',
429                                                     xns: Roo.form,
430                                                     labelWidth : 100,
431                                                     width : 420,
432                                                     items : [
433                                                         {
434                                                             xtype: 'ComboBox',
435                                                             xns: Roo.form,
436                                                             allowBlank : false,
437                                                             editable : false,
438                                                             emptyText : "Select terms",
439                                                             forceSelection : true,
440                                                             listWidth : 400,
441                                                             loadingText : "Searching...",
442                                                             minChars : 2,
443                                                             pageSize : 20,
444                                                             qtip : "Select terms",
445                                                             selectOnFocus : true,
446                                                             triggerAction : 'all',
447                                                             typeAhead : true,
448                                                             width : 300,
449                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{terms_descrip}</b> </div>',
450                                                             queryParam : 'query[terms_descrip]',
451                                                             fieldLabel : 'Terms',
452                                                             valueField : 'terms_id',
453                                                             displayField : 'terms_descrip',
454                                                             hiddenName : 'cohead_terms_id',
455                                                             name : 'cohead_terms_id_terms_descrip',
456                                                             store : {
457                                                                 xtype: 'Store',
458                                                                 xns: Roo.data,
459                                                                 listeners : {
460                                                                     beforeload : function (_self, o){
461                                                                         o.params = o.params || {};
462                                                                         // set more here
463                                                                     }
464                                                                 },
465                                                                 remoteSort : true,
466                                                                 sortInfo : { direction : 'ASC', field: 'terms_descrip' },
467                                                                 proxy : {
468                                                                     xtype: 'HttpProxy',
469                                                                     xns: Roo.data,
470                                                                     method : 'GET',
471                                                                     url : baseURL + '/Roo/terms.php'
472                                                                 },
473                                                                 reader : {
474                                                                     xtype: 'JsonReader',
475                                                                     xns: Roo.data,
476                                                                     id : 'terms_id',
477                                                                     root : 'data',
478                                                                     totalProperty : 'total',
479                                                                     fields : [{"name":"terms_id","type":"int"},"terms_descrip"]
480                                                                 }
481                                                             }
482                                                         },
483                                                         {
484                                                             xtype: 'ComboBox',
485                                                             xns: Roo.form,
486                                                             allowBlank : true,
487                                                             alwaysQuery : true,
488                                                             displayField : 'salesrep_name',
489                                                             editable : false,
490                                                             emptyText : "Select salesrep",
491                                                             fieldLabel : 'Sales Rep',
492                                                             forceSelection : true,
493                                                             hiddenName : 'cohead_display_salesrep_id',
494                                                             listWidth : 400,
495                                                             loadingText : "Searching...",
496                                                             minChars : 2,
497                                                             name : 'cohead_display_salesrep_id_salesrep_name',
498                                                             pageSize : 80,
499                                                             qtip : "Select salesrep",
500                                                             queryParam : 'query[salesrep_name]',
501                                                             selectOnFocus : true,
502                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{salesrep_name}</b> </div>',
503                                                             triggerAction : 'all',
504                                                             typeAhead : true,
505                                                             valueField : 'salesrep_id',
506                                                             width : 300,
507                                                             store : {
508                                                                 xtype: 'Store',
509                                                                 xns: Roo.data,
510                                                                 listeners : {
511                                                                     beforeload : function (_self, o){
512                                                                         o.params = o.params || {};
513                                                                         // set more here
514                                                                     }
515                                                                 },
516                                                                 remoteSort : true,
517                                                                 sortInfo : { direction : 'ASC', field: 'salesrep_name' },
518                                                                 proxy : {
519                                                                     xtype: 'HttpProxy',
520                                                                     xns: Roo.data,
521                                                                     method : 'GET',
522                                                                     url : baseURL + '/Roo/salesrep.php'
523                                                                 },
524                                                                 reader : {
525                                                                     xtype: 'JsonReader',
526                                                                     xns: Roo.data,
527                                                                     id : 'salesrep_id',
528                                                                     root : 'data',
529                                                                     totalProperty : 'total',
530                                                                     fields : [{"name":"salesrep_id","type":"int"},"salesrep_name"]
531                                                                 }
532                                                             }
533                                                         },
534                                                         {
535                                                             xtype: 'ComboBox',
536                                                             xns: Roo.form,
537                                                             allowBlank : false,
538                                                             alwaysQuery : true,
539                                                             displayField : 'salesrep_name',
540                                                             editable : false,
541                                                             emptyText : "Select Staff",
542                                                             fieldLabel : 'Staff I.C.',
543                                                             forceSelection : true,
544                                                             hiddenName : 'cohead_salesrep_id',
545                                                             listWidth : 400,
546                                                             loadingText : "Searching...",
547                                                             minChars : 2,
548                                                             name : 'cohead_salesrep_id_salesrep_name',
549                                                             pageSize : 80,
550                                                             qtip : "Select salesrep",
551                                                             queryParam : 'query[salesrep_name]',
552                                                             selectOnFocus : true,
553                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{salesrep_name}</b> </div>',
554                                                             triggerAction : 'all',
555                                                             typeAhead : true,
556                                                             valueField : 'salesrep_id',
557                                                             width : 300,
558                                                             store : {
559                                                                 xtype: 'Store',
560                                                                 xns: Roo.data,
561                                                                 listeners : {
562                                                                     beforeload : function (_self, o){
563                                                                         o.params = o.params || {};
564                                                                         // set more here
565                                                                     }
566                                                                 },
567                                                                 remoteSort : true,
568                                                                 sortInfo : { direction : 'ASC', field: 'salesrep_name' },
569                                                                 proxy : {
570                                                                     xtype: 'HttpProxy',
571                                                                     xns: Roo.data,
572                                                                     method : 'GET',
573                                                                     url : baseURL + '/Roo/salesrep.php'
574                                                                 },
575                                                                 reader : {
576                                                                     xtype: 'JsonReader',
577                                                                     xns: Roo.data,
578                                                                     id : 'salesrep_id',
579                                                                     root : 'data',
580                                                                     totalProperty : 'total',
581                                                                     fields : [{"name":"salesrep_id","type":"int"},"salesrep_name"]
582                                                                 }
583                                                             }
584                                                         },
585                                                         {
586                                                             xtype: 'TextArea',
587                                                             xns: Roo.form,
588                                                             fieldLabel : 'Order Comments',
589                                                             height : 80,
590                                                             name : 'cohead_ordercomments',
591                                                             width : 300
592                                                         }
593                                                     ]
594                                                 }
595                                             ]
596                                         },
597                                         {
598                                             xtype: 'FieldSet',
599                                             xns: Roo.form,
600                                             style : 'width:420px',
601                                             legend : "Billing",
602                                             items : [
603                                                 {
604                                                     xtype: 'Column',
605                                                     xns: Roo.form,
606                                                     width : '420',
607                                                     labelWidth : '50',
608                                                     items : [
609                                                         {
610                                                             xtype: 'ComboBox',
611                                                             xns: Roo.form,
612                                                             listeners : {
613                                                                 beforeselect : function (combo, record, index)
614                                                                 {
615                                                                     // set _this.data values ..
616                                                                 
617                                                                     // just add everything...
618                                                                     for(var i in record.data) {
619                                                                         _this.data['cohead_billto_cntct_id_' + i] = record.data[i];
620                                                                     }
621                                                                     
622                                                                     _this.form.findField('billto_address').update();
623                                                                     
624                                                                     
625                                                                  
626                                                                 },
627                                                                 add : function (combo)
628                                                                 {
629                                                                   
630                                                                   Pman.Dialog.XtupleQuickContact.show( 
631                                                                             {
632                                                                               _id : id,
633                                                                               customer_id : _this.form.findField('cohead_cust_id').getValue()
634                                                                             },
635                                                                             
636                                                                             function (data) {
637                                                                                
638                                                                                 for(var i in  data) {
639                                                                                     
640                                                                                     _this.data['cohead_billto_cntct_id_' + i] =  data[i];
641                                                                                 }
642                                                                                 
643                                                                                 _this.form.findField('billto_address').update();
644                                                                                 // fill in the select box..
645                                                                                 _this.form.setValues( {
646                                                                                     cohead_billto_cntct_id : data.cntct_id,
647                                                                                     cohead_billto_cntct_id_cntct_name : data.cntct_first_name + ' '+ 
648                                                                                             data.cntct_last_name
649                                                                                     
650                                                                                 });
651                                                                             }
652                                                                         );
653                                                                         //  Pman.Dialog.XtupleCustomer.show(
654                                                                             //{ cust_id : _this.form.findField('cohead_cust_id').getValue() }, 
655                                                                             //function(data) {
656                                                                         // refresh the data in the pulldown..
657                                                                     //    }); 
658                                                                 
659                                                                 }
660                                                             },
661                                                             allowBlank : false,
662                                                             alwaysQuery : true,
663                                                             displayField : 'cntct_name',
664                                                             editable : true,
665                                                             emptyText : "Select cntct",
666                                                             fieldLabel : 'Bill To (select)',
667                                                             forceSelection : true,
668                                                             hiddenName : 'cohead_billto_cntct_id',
669                                                             listWidth : 400,
670                                                             loadingText : "Searching...",
671                                                             minChars : 2,
672                                                             name : 'cohead_billto_cntct_id_cntct_name',
673                                                             pageSize : 20,
674                                                             qtip : "Select cntct",
675                                                             queryParam : 'query[cntct_name]',
676                                                             selectOnFocus : true,
677                                                             tpl : '<div class="x-grid-cell-text x-btn button"><b>{cntct_name}</b> {cntct_addr_id_addr_line1}</div>',
678                                                             triggerAction : 'all',
679                                                             typeAhead : true,
680                                                             valueField : 'cntct_id',
681                                                             width : 300,
682                                                             store : {
683                                                                 xtype: 'Store',
684                                                                 xns: Roo.data,
685                                                                 listeners : {
686                                                                     beforeload : function (_self, o){
687                                                                         o.params = o.params || {};
688                                                                         // set more here
689                                                                         o.params._customer_id = _this.data.cohead_cust_id;
690                                                                     }
691                                                                 },
692                                                                 remoteSort : true,
693                                                                 sortInfo : { direction : 'ASC', field: 'cntct_name' },
694                                                                 proxy : {
695                                                                     xtype: 'HttpProxy',
696                                                                     xns: Roo.data,
697                                                                     method : 'GET',
698                                                                     url : baseURL + '/Roo/cntct.php'
699                                                                 },
700                                                                 reader : {
701                                                                     xtype: 'JsonReader',
702                                                                     xns: Roo.data,
703                                                                     id : 'cntct_id',
704                                                                     root : 'data',
705                                                                     totalProperty : 'total',
706                                                                     fields : [{"name":"cntct_id","type":"int"},"cntct_name"]
707                                                                 }
708                                                             }
709                                                         },
710                                                         {
711                                                             xtype: 'TextArea',
712                                                             xns: Roo.form,
713                                                             listeners : {
714                                                                 render : function (_self)
715                                                                 {
716                                                                    Roo.log(this.el)
717                                                                    
718                                                                    
719                                                                    
720                                                                    this.el.on('click', function() { 
721                                                                        var id = _this.form.findField('cohead_billto_cntct_id').getValue();
722                                                                         Pman.Dialog.XtupleQuickContact.show( 
723                                                                             {
724                                                                               _id : id,
725                                                                               customer_id : _this.form.findField('cohead_cust_id').getValue()
726                                                                             },
727                                                                             
728                                                                             function (data) {
729                                                                             
730                                                                                 for(var i in  data) {
731                                                                                     
732                                                                                     _this.data['cohead_billto_cntct_id_' + i] =  data[i];
733                                                                                 }
734                                                                                 
735                                                                                 _this.form.findField('billto_address').update();
736                                                                                 // fill in the select box..
737                                                                                 _this.form.setValues( {
738                                                                                     cohead_billto_cntct_id : data.cntct_id,
739                                                                                     cohead_billto_cntct_id_cntct_name : data.cntct_first_name + ' '+ 
740                                                                                             data.cntct_last_name
741                                                                                     
742                                                                                 });
743                                                                                 
744                                                                                         
745                                                                                 Roo.log(data);
746                                                                             }
747                                                                         );
748                                                                    
749                                                                         Roo.log("Click text");
750                                                                         
751                                                                         
752                                                                      });
753                                                                 }
754                                                             },
755                                                             fieldLabel : 'or enter Address',
756                                                             name : 'billto_address',
757                                                             readOnly : true,
758                                                             update : function() {
759                                                             
760                                                                 var c = ['first_name', 'last_name' ] ;
761                                                                 var a = [ 'line1', 'line2', 'line3', 'city', 'state', 'country' ];
762                                                                 var v = [];
763                                                                 Roo.each(c, function(e) {
764                                                                     if (_this.data['cohead_billto_cntct_id_cntct_' +e].length) { 
765                                                                         v.push(_this.data['cohead_billto_cntct_id_cntct_' +e]);
766                                                                     }
767                                                                 });
768                                                                 Roo.each(a, function(e) {
769                                                                     if (_this.data['cohead_billto_cntct_id_cntct_addr_id_addr_' +e].length) {
770                                                                         v.push(_this.data['cohead_billto_cntct_id_cntct_addr_id_addr_' +e]);
771                                                                     }
772                                                                 });
773                                                             
774                                                                 this.setValue(v.join("\n"));
775                                                                 
776                                                                 if(_this.data.cohead_billto_cntct_id_cntct_email.length){
777                                                                     _this.form.findField('billto_email').setValue(_this.data.cohead_billto_cntct_id_cntct_email);
778                                                                 }
779                                                             }
780                                                         },
781                                                         {
782                                                             xtype: 'DisplayField',
783                                                             xns: Roo.form,
784                                                             fieldLabel : 'Email',
785                                                             name : 'billto_email',
786                                                             valueRenderer : function(v)
787                                                             {
788                                                              
789                                                                 return v ? String.format('<a href="mailto:{0}" target="_blank">{0}</a>',v) : '';
790                                                             }
791                                                         }
792                                                     ]
793                                                 }
794                                             ]
795                                         }
796                                     ]
797                                 },
798                                 {
799                                     xtype: 'Column',
800                                     xns: Roo.form,
801                                     width : '435',
802                                     style : 'margin-left:10px',
803                                     items : [
804                                         {
805                                             xtype: 'FieldSet',
806                                             xns: Roo.form,
807                                             legend : "Price Details",
808                                             style : 'width:420px',
809                                             labelWidth : '50',
810                                             items : [
811                                                 {
812                                                     xtype: 'Column',
813                                                     xns: Roo.form,
814                                                     labelAlign : 'right',
815                                                     labelWidth : 300,
816                                                     width : 420,
817                                                     items : [
818                                                         {
819                                                             xtype: 'Row',
820                                                             xns: Roo.form,
821                                                             labelAlign : 'top',
822                                                             labelSeparator : '&nbsp;',
823                                                             width : 500,
824                                                             items : [
825                                                                 {
826                                                                     xtype: 'ComboBox',
827                                                                     xns: Roo.form,
828                                                                     listeners : {
829                                                                         beforequery : function (combo, query, forceAll, cancel, e)
830                                                                         {
831                                                                             Roo.log('beforequery');
832                                                                            
833                                                                             if (_this.form.findField('cohead_number').getValue().length) {
834                                                                                 Roo.MessageBox.alert("Error", "You can not change the currency of this order");
835                                                                                 if (query) {
836                                                                                     query.cancel = true;
837                                                                                 }
838                                                                                 return false;
839                                                                             }
840                                                                         }
841                                                                     },
842                                                                     allowBlank : false,
843                                                                     displayField : 'curr_name',
844                                                                     editable : false,
845                                                                     emptyText : "Select curr_name",
846                                                                     fieldLabel : 'Currency',
847                                                                     forceSelection : true,
848                                                                     hiddenName : 'cohead_curr_id',
849                                                                     listWidth : 400,
850                                                                     loadingText : "Searching...",
851                                                                     minChars : 2,
852                                                                     name : 'cohead_curr_id_curr_name',
853                                                                     pageSize : 20,
854                                                                     qtip : "Select Currency",
855                                                                     queryParam : 'query[curr_name]',
856                                                                     selectOnFocus : true,
857                                                                     tpl : '<div class="x-grid-cell-text x-btn button"><b>{curr_name}</b> </div>',
858                                                                     triggerAction : 'all',
859                                                                     typeAhead : true,
860                                                                     valueField : 'curr_id',
861                                                                     width : 285,
862                                                                     store : {
863                                                                         xtype: 'Store',
864                                                                         xns: Roo.data,
865                                                                         listeners : {
866                                                                             beforeload : function (_self, o){
867                                                                                 o.params = o.params || {};
868                                                                                 // set more here
869                                                                                
870                                                                             }
871                                                                         },
872                                                                         remoteSort : true,
873                                                                         sortInfo : { direction : 'ASC', field: 'curr_symbol' },
874                                                                         proxy : {
875                                                                             xtype: 'HttpProxy',
876                                                                             xns: Roo.data,
877                                                                             method : 'GET',
878                                                                             url : baseURL + '/Roo/curr_symbol.php'
879                                                                         },
880                                                                         reader : {
881                                                                             xtype: 'JsonReader',
882                                                                             xns: Roo.data,
883                                                                             id : 'curr_id',
884                                                                             root : 'data',
885                                                                             totalProperty : 'total',
886                                                                             fields : [{"name":"curr_id","type":"int"},"curr_symbol"]
887                                                                         }
888                                                                     }
889                                                                 },
890                                                                 {
891                                                                     xtype: 'NumberField',
892                                                                     xns: Roo.form,
893                                                                     allowDecimals : true,
894                                                                     cls : 'roo-align-right',
895                                                                     decimalPrecision : 3,
896                                                                     fieldLabel : 'Products',
897                                                                     name : 'cohead_subtotal',
898                                                                     readOnly : true,
899                                                                     width : 100
900                                                                 }
901                                                             ]
902                                                         },
903                                                         {
904                                                             xtype: 'Row',
905                                                             xns: Roo.form,
906                                                             labelAlign : 'top',
907                                                             labelSeparator : '&nbsp;',
908                                                             width : 500,
909                                                             items : [
910                                                                 {
911                                                                     xtype: 'ComboBox',
912                                                                     xns: Roo.form,
913                                                                     listeners : {
914                                                                         select : function (combo, record, index)
915                                                                         {
916                                                                              _this.form.findField('cohead_tax').setValue(
917                                                                                      parseFloat(record.data.taxzone_rate) * 
918                                                                                      parseFloat(_this.form.findField('cohead_subtotal').getValue())
919                                                                              );
920                                                                               _this.form.findField('cohead_total').recalc();
921                                                                         }
922                                                                     },
923                                                                     allowBlank : false,
924                                                                     displayField : 'taxzone_descrip',
925                                                                     editable : false,
926                                                                     emptyText : "Select taxtype",
927                                                                     fieldLabel : 'Tax Zone',
928                                                                     forceSelection : true,
929                                                                     hiddenName : 'cohead_taxzone_id',
930                                                                     listWidth : 400,
931                                                                     loadingText : "Searching...",
932                                                                     minChars : 2,
933                                                                     name : 'cohead_taxzone_id_taxzone_descrip',
934                                                                     pageSize : 20,
935                                                                     qtip : "Select taxtype",
936                                                                     queryParam : 'query[taxzone_descrip]',
937                                                                     selectOnFocus : true,
938                                                                     tpl : '<div class="x-grid-cell-text x-btn button"><b>{taxzone_descrip}</b> </div>',
939                                                                     triggerAction : 'all',
940                                                                     typeAhead : true,
941                                                                     valueField : 'taxzone_id',
942                                                                     width : 285,
943                                                                     store : {
944                                                                         xtype: 'Store',
945                                                                         xns: Roo.data,
946                                                                         listeners : {
947                                                                             beforeload : function (_self, o){
948                                                                                 o.params = o.params || {};
949                                                                                 // set more here
950                                                                                 
951                                                                                 o.params.with_date = _this.form.findField('cohead_orderdate').getValue().format('Y-m-d'); 
952                                                                                 Roo.log("with date?" + o.params.with_date);
953                                                                                 
954                                                                                 
955                                                                             }
956                                                                         },
957                                                                         remoteSort : true,
958                                                                         sortInfo : { direction : 'ASC', field: 'taxzone_descrip' },
959                                                                         proxy : {
960                                                                             xtype: 'HttpProxy',
961                                                                             xns: Roo.data,
962                                                                             method : 'GET',
963                                                                             url : baseURL + '/Roo/taxzone.php'
964                                                                         },
965                                                                         reader : {
966                                                                             xtype: 'JsonReader',
967                                                                             xns: Roo.data,
968                                                                             id : 'taxzone_id',
969                                                                             root : 'data',
970                                                                             totalProperty : 'total',
971                                                                             fields : [{"name":"taxzone_id","type":"int"},"taxzone_descrip"]
972                                                                         }
973                                                                     }
974                                                                 },
975                                                                 {
976                                                                     xtype: 'NumberField',
977                                                                     xns: Roo.form,
978                                                                     allowDecimals : true,
979                                                                     cls : 'roo-align-right',
980                                                                     decimalPrecision : 3,
981                                                                     fieldLabel : 'Tax',
982                                                                     name : 'cohead_tax',
983                                                                     readOnly : true,
984                                                                     width : 100
985                                                                 }
986                                                             ]
987                                                         },
988                                                         {
989                                                             xtype: 'NumberField',
990                                                             xns: Roo.form,
991                                                             allowDecimals : true,
992                                                             cls : 'roo-align-right',
993                                                             decimalPrecision : 3,
994                                                             fieldLabel : 'Pre Tax discount',
995                                                             name : 'cohead_pretax_discount',
996                                                             readOnly : true,
997                                                             width : 100
998                                                         },
999                                                         {
1000                                                             xtype: 'Row',
1001                                                             xns: Roo.form,
1002                                                             labelAlign : 'top',
1003                                                             labelSeparator : '&nbsp;',
1004                                                             width : 500,
1005                                                             items : [
1006                                                                 {
1007                                                                     xtype: 'TextField',
1008                                                                     xns: Roo.form,
1009                                                                     fieldLabel : 'Discount after Tax  Description',
1010                                                                     name : 'cohead_misc_descrip',
1011                                                                     width : 230
1012                                                                 },
1013                                                                 {
1014                                                                     xtype: 'NumberField',
1015                                                                     xns: Roo.form,
1016                                                                     listeners : {
1017                                                                         keyup : function (_self, e)
1018                                                                         {
1019                                                                             var pv =  parseFloat(_this.form.findField('cohead_subtotal').getValue());
1020                                                                             var tax = parseFloat(_this.form.findField('cohead_tax').getValue());
1021                                                                             var pd = parseFloat(_this.form.findField('cohead_pretax_discount').getValue());
1022                                                                             
1023                                                                             var n = this.getValue();
1024                                                                             var discount = parseFloat(n * (pv + tax + pd) * 0.01);
1025                                                                             
1026                                                                             _this.form.findField('cohead_posttax_discount').setValue(discount);
1027                                                                         
1028                                                                             _this.form.findField('cohead_total').recalc();
1029                                                                             _this.form.findField('cohead_misc').recalc();
1030                                                                             var val = discount * -1.0;
1031                                                                              if (val > 0) {
1032                                                                                 _this.form.findField('cohead_misc_descrip').setValue("Discount of " + val.toFixed(1)+'%');
1033                                                                             }
1034                                                                            
1035                                                                             
1036                                                                         }
1037                                                                     },
1038                                                                     allowDecimals : true,
1039                                                                     cls : 'roo-align-right',
1040                                                                     decimalPrecision : 1,
1041                                                                     fieldLabel : '%',
1042                                                                     name : 'cohead_misc_per',
1043                                                                     width : 35,
1044                                                                     update : function() {
1045                                                                         var m = _this.form.findField('cohead_misc_per');
1046                                                                         var pv =  parseFloat(_this.form.findField('cohead_subtotal').getValue());
1047                                                                         var tax = parseFloat(_this.form.findField('cohead_tax').getValue());
1048                                                                         var pd = parseFloat(_this.form.findField('cohead_pretax_discount').getValue());
1049                                                                         
1050                                                                         var discount = parseFloat(_this.form.findField('cohead_posttax_discount').getValue());
1051                                                                         
1052                                                                         if (discount > 0.0) {
1053                                                                             this.setValue(0);        
1054                                                                             return;
1055                                                                         }
1056                                                                         if (pv < 0) {
1057                                                                             this.setValue(0);        
1058                                                                             return;
1059                                                                         }
1060                                                                         var val = ((discount) / (pv + tax + pd)) * -100;
1061                                                                         
1062                                                                         //Roo.log("update discount?" + val);
1063                                                                         this.setValue(val.toFixed(1));
1064                                                                         
1065                                                                          if (val > 0.0) {
1066                                                                             _this.form.findField('cohead_misc_descrip').setValue("Discount of " + val.toFixed(1)+'%');
1067                                                                         
1068                                                                         }
1069                                                                         
1070                                                                        
1071                                                                     }
1072                                                                 },
1073                                                                 {
1074                                                                     xtype: 'NumberField',
1075                                                                     xns: Roo.form,
1076                                                                     listeners : {
1077                                                                         keyup : function (_self, e)
1078                                                                         {
1079                                                                            _this.form.findField('cohead_misc_per').update();
1080                                                                             _this.form.findField('cohead_total').recalc();
1081                                                                             _this.form.findField('cohead_misc').recalc();
1082                                                                         }
1083                                                                     },
1084                                                                     allowDecimals : true,
1085                                                                     cls : 'roo-align-right',
1086                                                                     decimalPrecision : 3,
1087                                                                     fieldLabel : '&nbsp;',
1088                                                                     name : 'cohead_posttax_discount',
1089                                                                     width : 100
1090                                                                 }
1091                                                             ]
1092                                                         },
1093                                                         {
1094                                                             xtype: 'NumberField',
1095                                                             xns: Roo.form,
1096                                                             allowDecimals : true,
1097                                                             cls : 'roo-align-right',
1098                                                             decimalPrecision : 3,
1099                                                             fieldLabel : 'Total',
1100                                                             name : 'cohead_total',
1101                                                             readOnly : true,
1102                                                             width : 100,
1103                                                             recalc : function() {
1104                                                                 var d = _this.form.getValues();
1105                                                                 this.setValue( 
1106                                                                             parseFloat(d.cohead_subtotal) + 
1107                                                                             parseFloat(d.cohead_pretax_discount) + 
1108                                                                             parseFloat(d.cohead_posttax_discount) + 
1109                                                                             parseFloat(d.cohead_tax) + 
1110                                                                             parseFloat(d.cohead_freight));
1111                                                             }
1112                                                         },
1113                                                         {
1114                                                             xtype: 'NumberField',
1115                                                             xns: Roo.form,
1116                                                             listeners : {
1117                                                                 keyup : function (_self, e)
1118                                                                 {
1119                                                                     _this.form.findField('cohead_total').recalc();
1120                                                                 }
1121                                                             },
1122                                                             allowDecimals : true,
1123                                                             cls : 'roo-align-right',
1124                                                             decimalPrecision : 3,
1125                                                             fieldLabel : 'Shipping',
1126                                                             name : 'cohead_freight',
1127                                                             width : 100
1128                                                         },
1129                                                         {
1130                                                             xtype: 'NumberField',
1131                                                             xns: Roo.form,
1132                                                             allowDecimals : true,
1133                                                             cls : 'roo-align-right',
1134                                                             decimalPrecision : 3,
1135                                                             fieldLabel : 'Unshipped Total',
1136                                                             name : 'cohead_unshipped',
1137                                                             readOnly : true,
1138                                                             width : 100
1139                                                         },
1140                                                         {
1141                                                             xtype: 'NumberField',
1142                                                             xns: Roo.form,
1143                                                             allowDecimals : true,
1144                                                             cls : 'roo-align-right',
1145                                                             decimalPrecision : 3,
1146                                                             fieldLabel : 'Uninvoiced Total',
1147                                                             name : 'cohead_uninvoiced',
1148                                                             readOnly : true,
1149                                                             width : 100
1150                                                         }
1151                                                     ]
1152                                                 }
1153                                             ]
1154                                         },
1155                                         {
1156                                             xtype: 'FieldSet',
1157                                             xns: Roo.form,
1158                                             style : 'width:420px',
1159                                             legend : "Shipping",
1160                                             items : [
1161                                                 {
1162                                                     xtype: 'Column',
1163                                                     xns: Roo.form,
1164                                                     width : '420',
1165                                                     labelWidth : '50',
1166                                                     items : [
1167                                                         {
1168                                                             xtype: 'Row',
1169                                                             xns: Roo.form,
1170                                                             labelSeparator : '&nbsp;',
1171                                                             items : [
1172                                                                 {
1173                                                                     xtype: 'Checkbox',
1174                                                                     xns: Roo.form,
1175                                                                     listeners : {
1176                                                                         check : function (_self, checked)
1177                                                                         {
1178                                                                            if (!_this.form) {
1179                                                                                return;
1180                                                                            }
1181                                                                            
1182                                                                            if (_this.dataloading) {
1183                                                                            
1184                                                                                return;
1185                                                                            }
1186                                                                            
1187                                                                            
1188                                                                            if (checked) {
1189                                                                            
1190                                                                                 // copy the cohead_billto_cntct_id
1191                                                                                 for (var i in _this.data) {
1192                                                                                     if (!i.match(/^cohead_billto_cntct_id/)) {
1193                                                                                         continue;
1194                                                                                     }
1195                                                                                     var ni = i.replace(/^cohead_billto_cntct_id/, 'cohead_shipto_cntct_id');
1196                                                                                     _this.data[ni] = _this.data[i];
1197                                                                                 }
1198                                                                                  _this.form.findField('shipto_address').update();
1199                                                                                  _this.form.setValues( {
1200                                                                                         cohead_shipto_cntct_id : _this.data.cohead_shipto_cntct_id,
1201                                                                                         cohead_shipto_cntct_id_cntct_name : _this.data.cohead_shipto_cntct_id_cntct_first_name + ' ' + 
1202                                                                                                 _this.data.cohead_shipto_cntct_id_cntct_last_name 
1203                                                                                                 
1204                                                                                         
1205                                                                                     });
1206                                                                         
1207                                                                            
1208                                                                             } else {
1209                                                                                  for (var i in _this.data) {
1210                                                                                     if (!i.match(/^cohead_billto_cntct_id/)) {
1211                                                                                         continue;
1212                                                                                     }
1213                                                                                     var ni = i.replace(/^cohead_billto_cntct_id/, 'cohead_shipto_cntct_id');
1214                                                                                     
1215                                                                                     _this.data[ni] = '';
1216                                                                                 }
1217                                                                                  _this.form.findField('shipto_address').update();
1218                                                                                  
1219                                                                                 _this.form.setValues( {
1220                                                                                         cohead_shipto_cntct_id : '',
1221                                                                                         cohead_shipto_cntct_id_cntct_name : '' 
1222                                                                                         
1223                                                                                         
1224                                                                                     });
1225                                                                             }
1226                                                                             
1227                                                                         }
1228                                                                     },
1229                                                                     boxLabel : 'Same as Billing',
1230                                                                     name : '_shipto_same'
1231                                                                 }
1232                                                             ]
1233                                                         },
1234                                                         {
1235                                                             xtype: 'ComboBox',
1236                                                             xns: Roo.form,
1237                                                             listeners : {
1238                                                                 add : function (combo)
1239                                                                 {
1240                                                                  Pman.Dialog.XtupleQuickContact.show( 
1241                                                                             {
1242                                                                               _id : id,
1243                                                                               customer_id : _this.form.findField('cohead_cust_id').getValue()
1244                                                                             },
1245                                                                             
1246                                                                             function (data) {
1247                                                                                
1248                                                                                 for(var i in  data) {
1249                                                                                     
1250                                                                                     _this.data['cohead_shipto_cntct_id_' + i] =  data[i];
1251                                                                                 }
1252                                                                                 
1253                                                                                 _this.form.findField('shipto_address').update();
1254                                                                                 // fill in the select box..
1255                                                                                 _this.form.setValues( {
1256                                                                                     cohead_shipto_cntct_id : data.cntct_id,
1257                                                                                     cohead_shipto_cntct_id_cntct_name : data.cntct_first_name + ' '+ 
1258                                                                                             data.cntct_last_name
1259                                                                                     
1260                                                                                 });
1261                                                                             }
1262                                                                         ); 
1263                                                                 },
1264                                                                 beforeselect : function (combo, record, index)
1265                                                                 {
1266                                                                    
1267                                                                     // just add everything...
1268                                                                     for(var i in record.data) {
1269                                                                         //Roo.log('cohead_shipto_cntct_id_' + i +' ='  + record.data[i]);
1270                                                                         _this.data['cohead_shipto_cntct_id_' + i] = record.data[i];
1271                                                                     }
1272                                                                 
1273                                                                     _this.form.findField('shipto_address').update();
1274                                                                     
1275                                                                  
1276                                                                 }
1277                                                             },
1278                                                             allowBlank : false,
1279                                                             alwaysQuery : true,
1280                                                             displayField : 'cntct_name',
1281                                                             editable : true,
1282                                                             emptyText : "Select ship to",
1283                                                             fieldLabel : 'Ship to',
1284                                                             forceSelection : true,
1285                                                             hiddenName : 'cohead_shipto_cntct_id',
1286                                                             listWidth : 400,
1287                                                             loadingText : "Searching...",
1288                                                             minChars : 2,
1289                                                             name : 'cohead_shipto_cntct_id_cntct_name',
1290                                                             pageSize : 20,
1291                                                             qtip : "Select shiptoinfo",
1292                                                             queryParam : 'query[cntct_name]',
1293                                                             selectOnFocus : true,
1294                                                             tpl : '<div class="x-grid-cell-text x-btn button">{cntct_name} - <b>{cntct_addr_id_addr_line1}</b> </div>',
1295                                                             triggerAction : 'all',
1296                                                             typeAhead : true,
1297                                                             valueField : 'cntct_id',
1298                                                             width : 300,
1299                                                             store : {
1300                                                                 xtype: 'Store',
1301                                                                 xns: Roo.data,
1302                                                                 listeners : {
1303                                                                     beforeload : function (_self, o){
1304                                                                         o.params = o.params || {};
1305                                                                         // set more here
1306                                                                         o.params._customer_id = _this.data.cohead_cust_id;
1307                                                                     }
1308                                                                 },
1309                                                                 remoteSort : true,
1310                                                                 sortInfo : { direction : 'ASC', field: 'cntct_name' },
1311                                                                 proxy : {
1312                                                                     xtype: 'HttpProxy',
1313                                                                     xns: Roo.data,
1314                                                                     method : 'GET',
1315                                                                     url : baseURL + '/Roo/cntct.php'
1316                                                                 },
1317                                                                 reader : {
1318                                                                     xtype: 'JsonReader',
1319                                                                     xns: Roo.data,
1320                                                                     id : 'cntct_id',
1321                                                                     root : 'data',
1322                                                                     totalProperty : 'total',
1323                                                                     fields : [{"name":"cntct_id","type":"int"},"cntct_name"]
1324                                                                 }
1325                                                             }
1326                                                         },
1327                                                         {
1328                                                             xtype: 'TextArea',
1329                                                             xns: Roo.form,
1330                                                             listeners : {
1331                                                                 render : function (_self)
1332                                                                 {
1333                                                                    Roo.log(this.el)
1334                                                                    
1335                                                                    
1336                                                                    
1337                                                                    this.el.on('click', function() { 
1338                                                                        var id = _this.form.findField('cohead_shipto_cntct_id').getValue();
1339                                                                         Pman.Dialog.XtupleQuickContact.show( 
1340                                                                             {
1341                                                                               _id : id,
1342                                                                               customer_id : _this.form.findField('cohead_cust_id').getValue()
1343                                                                             },
1344                                                                             
1345                                                                             function (data) {
1346                                                                             
1347                                                                                 for(var i in  data) {
1348                                                                                     
1349                                                                                     _this.data['cohead_shipto_cntct_id_' + i] =  data[i];
1350                                                                                 }
1351                                                                                 
1352                                                                                 _this.form.findField('shipto_address').update();
1353                                                                                 // fill in the select box..
1354                                                                                 _this.form.setValues( {
1355                                                                                     cohead_shipto_cntct_id : data.cntct_id,
1356                                                                                     cohead_shipto_cntct_id_cntct_name : data.cntct_first_name + ' '+ 
1357                                                                                             data.cntct_last_name
1358                                                                                     
1359                                                                                 });
1360                                                                                 
1361                                                                                         
1362                                                                                 //Roo.log(data);
1363                                                                             }
1364                                                                         );
1365                                                                    
1366                                                                       //  Roo.log("Click text");
1367                                                                         
1368                                                                         
1369                                                                      });
1370                                                                 }
1371                                                             },
1372                                                             fieldLabel : 'Address',
1373                                                             name : 'shipto_address',
1374                                                             readOnly : true,
1375                                                             width : 300,
1376                                                             update : function() {
1377                                                             
1378                                                                 var c = ['first_name', 'last_name' ] ;
1379                                                                 var a = [ 'line1', 'line2', 'line3', 'city', 'state', 'country' ];
1380                                                                 var v = [];
1381                                                                 
1382                                                                 if (_this.data['cohead_shipto_vend_name'] && _this.data['cohead_shipto_vend_name'].length) {
1383                                                                      v.push(_this.data['cohead_shipto_vend_name']);
1384                                                                 }
1385                                                                     if (_this.data['cohead_shipto_vendaddr_code'] && _this.data['cohead_shipto_vendaddr_code'].length) {
1386                                                                      v.push(_this.data['cohead_shipto_vendaddr_code']);
1387                                                                 }
1388                                                                 Roo.each(c, function(e) {
1389                                                                     if (_this.data['cohead_shipto_cntct_id_cntct_' +e] && 
1390                                                                         _this.data['cohead_shipto_cntct_id_cntct_' +e].length) {
1391                                                                         v.push(_this.data['cohead_shipto_cntct_id_cntct_' +e]);
1392                                                                     }
1393                                                                 });
1394                                                                 Roo.each(a, function(e) {
1395                                                                     if (_this.data['cohead_shipto_cntct_id_cntct_addr_id_addr_' +e] && 
1396                                                                         _this.data['cohead_shipto_cntct_id_cntct_addr_id_addr_' +e].length) { 
1397                                                                         v.push(_this.data['cohead_shipto_cntct_id_cntct_addr_id_addr_' +e]);
1398                                                                     }
1399                                                                 });
1400                                                             
1401                                                                 this.setValue(v.join("\n"));
1402                                                             }
1403                                                         },
1404                                                         {
1405                                                             xtype: 'TextArea',
1406                                                             xns: Roo.form,
1407                                                             fieldLabel : 'Shipment Comments',
1408                                                             name : 'cohead_shipcomments',
1409                                                             width : 300
1410                                                         }
1411                                                     ]
1412                                                 }
1413                                             ]
1414                                         }
1415                                     ]
1416                                 },
1417                                 {
1418                                     xtype: 'Hidden',
1419                                     xns: Roo.form,
1420                                     name : 'cohead_id'
1421                                 },
1422                                 {
1423                                     xtype: 'Hidden',
1424                                     xns: Roo.form,
1425                                     name : 'cohead_shipto_id'
1426                                 },
1427                                 {
1428                                     xtype: 'Hidden',
1429                                     xns: Roo.form,
1430                                     name : 'cohead_shipto_id_shipto_name'
1431                                 },
1432                                 {
1433                                     xtype: 'Hidden',
1434                                     xns: Roo.form,
1435                                     name : 'cohead_max_linenumber'
1436                                 },
1437                                 {
1438                                     xtype: 'Hidden',
1439                                     xns: Roo.form,
1440                                     name : 'cohead_cust_id'
1441                                 },
1442                                 {
1443                                     xtype: 'Hidden',
1444                                     xns: Roo.form,
1445                                     name : 'cohead_misc',
1446                                     recalc : function() {
1447                                         var d = _this.form.getValues();
1448                                         this.setValue( 
1449                                             parseFloat(d.cohead_pretax_discount) + 
1450                                             parseFloat(d.cohead_posttax_discount) );
1451                                     }
1452                                 },
1453                                 {
1454                                     xtype: 'Hidden',
1455                                     xns: Roo.form,
1456                                     name : 'taxzone_rate'
1457                                 }
1458                             ]
1459                         }
1460                     ]
1461                 },
1462                 {
1463                     xtype: 'GridPanel',
1464                     xns: Roo,
1465                     listeners : {
1466                         activate : function() {
1467                             _this.panel = this;
1468                             
1469                             try { if (MODULE.isBuilder) {
1470                                 return;
1471                             } } catch(e) { }
1472                             
1473                             var id = _this.form.findField('cohead_id').getValue() * 1;
1474                             if (id < 1) {
1475                                 Roo.MessageBox.alert("Save First", "Save the order first, before adding items");
1476                                 _this.dialog.layout.getRegion('center').showPanel(0);
1477                                 return;
1478                             }
1479                             
1480                             if (_this.grid) {
1481                                 _this.grid.footer.onClick('first');
1482                             
1483                                  //if (_this.form.findField('cohead_cust_id_cust_name').getValue() == 'Bloom and Grow HK') {
1484                                  //   _this.xferAll.show();
1485                                 // 
1486                                 // } else {
1487                                 //     _this.xferAll.hide();
1488                                 // }
1489                              }
1490                             
1491                         },
1492                         deactivate : function (_self)
1493                         {
1494                             if(_this.grid){
1495                                 _this.grid.stopEditing();
1496                             }
1497                         }
1498                     },
1499                     background : true,
1500                     fitContainer : true,
1501                     fitToframe : true,
1502                     region : 'center',
1503                     tableName : 'coitem',
1504                     title : "Order Items",
1505                     grid : {
1506                         xtype: 'EditorGrid',
1507                         xns: Roo.grid,
1508                         listeners : {
1509                             render : function() 
1510                             {
1511                                 _this.grid = this; 
1512                                 //_this.dialog = Pman.Dialog.FILL_IN
1513                                 
1514                                 if (_this.panel.active) {
1515                                    this.footer.onClick('first');
1516                                 }
1517                             },
1518                             afteredit : function (e)
1519                             {
1520                                 Roo.log('afteredit:' + e.record.data.coitem_linenumber);
1521                             
1522                                 
1523                                // if ( e.record && e.record.data.coitem_id) {
1524                                     // as we disable update to the display on the ajax callback to 
1525                                     // allow editing flow to continue, and not refresh - we can only update
1526                                     // these values after something has actually been edited.
1527                                     // e.record.set('coitem_id', e.record.data.coitem_id);
1528                                     // e.record.set('coitem_status', e.record.data.coitem_status);
1529                                // }
1530                                 
1531                                 if (e.field == 'item_number' || e.originalValue == e.value) {
1532                                     // afterselect handles this...
1533                                     return;
1534                                 }
1535                                 if (e.field == 'item_descrip1') {
1536                                     e.record.set('coitem_memo', e.value);
1537                                 }
1538                                 var rate = _this.form.findField('taxzone_rate').getValue();
1539                                 switch(e.field) {
1540                                     case 'coitem_linedisc':
1541                                        
1542                                         var cp = parseFloat(e.record.data.coitem_custprice);
1543                                        
1544                                         if (isNaN(cp) || cp == 0.0) {
1545                                             break;
1546                                         }
1547                                         
1548                                         var dis = parseFloat(e.value);
1549                                        
1550                                         if (isNaN(dis)) {
1551                                             break;
1552                                         }
1553                                         
1554                                         e.record.set(
1555                                             'coitem_price',
1556                                             Math.max(0,cp * ((100.0 -  parseInt(e.value))/ 100.0) )  
1557                                         );
1558                                         
1559                                        // donot need to set the subtotal here, coz we will render it automatically
1560                                        // e.record.set(
1561                                        //     'coitem_subtotal',
1562                                        //      e.record.data.coitem_price * 1.0 * e.record.data.coitem_qtyord
1563                                        // );
1564                                         break;
1565                                     
1566                                     case  'coitem_price': // SELL@ price
1567                             
1568                                          var cp = parseFloat(e.record.data.coitem_custprice);
1569                                          // list price is < price -- update it..
1570                                          if (isNaN(cp) || cp == 0.0 || cp < e.value) {
1571                                             e.record.set('coitem_custprice', e.value);
1572                                             cp = e.value;
1573                                          }
1574                                          // update the discount calc.
1575                                          
1576                                          e.record.set('coitem_linedisc', Math.max(0, 100.0 -  (( e.value / cp) * 100.0)));  
1577                                          
1578                                          // donot need to set the subtotal here, coz we will render it automatically
1579                                          //e.record.set(
1580                                          //   'coitem_subtotal',
1581                                           //   e.record.data.coitem_price * 1.0 * e.record.data.coitem_qtyord
1582                                          //);
1583                                          break;
1584                                     
1585                                     case 'coitem_custprice':
1586                                         // modified customer price...
1587                                         // just modify the discount.. -- leave the entered price the same..
1588                                           var sp =  parseFloat(e.record.data.coitem_price);
1589                                           var cp = parseFloat(e.value);
1590                                           if (isNaN(cp) || cp == 0.0 || cp < e.value) {
1591                                                 break;
1592                                           }
1593                                           
1594                                          e.record.set('coitem_linedisc', Math.max(0, 100.0 -  (( sp / cp) * 100.0)));  
1595                                          
1596                                          break;
1597                                          
1598                                     case 'coitem_custprice_tax':
1599                                           var cp = parseFloat(e.value / ( 1 + rate * 1 ));
1600                                           var sp =  parseFloat(e.record.data.coitem_price);
1601                                           
1602                                           if (isNaN(cp) || cp == 0.0) {
1603                                                 break;
1604                                           }
1605                                           e.record.set('coitem_custprice', cp);
1606                                           e.record.set('coitem_linedisc', Math.max(0, 100.0 -  (( sp / cp) * 100.0)));  
1607                                          
1608                                          break; 
1609                                          
1610                                      case  'coitem_price_tax':
1611                             
1612                                          var cp = parseFloat(e.record.data.coitem_custprice);
1613                                          var sp = parseFloat(e.value / ( 1 + rate * 1 ));
1614                                          if (isNaN(cp) || cp == 0.0 || cp < sp) {
1615                                             e.record.set('coitem_custprice', sp);
1616                                             cp = sp;
1617                                          }
1618                                          e.record.set('coitem_price', sp);
1619                                          e.record.set('coitem_linedisc', Math.max(0, 100.0 -  (( sp / cp) * 100.0)));  
1620                                          
1621                                          break;
1622                                   
1623                                 }
1624                                 
1625                                  
1626                                 
1627                                 
1628                                 var doupdate = function() { 
1629                                    if (!e.record.updatePending) {
1630                                         Roo.log('doupdate...'  + e.record.data.coitem_linenumber);
1631                                         Roo.log(e.record);
1632                                         e.record.commit();
1633                                         return;
1634                                     }
1635                                    Roo.log('doupdate pending...'   + e.record.data.coitem_linenumber);
1636                                     // wait until it's not peding an update..
1637                                     doupdate.defer(500);
1638                                 }
1639                             
1640                                 if(e.record.data.coitem_id * 1 < 1 && !e.record.isInserting){ // insert
1641                                     e.record.isInserting = 1;
1642                                     e.record.isUpdating = 0;
1643                                     doupdate();
1644                                     return;
1645                                 }
1646                                 
1647                                 // update
1648                                 if(!e.record.isInserting && !e.record.isUpdating){
1649                                     e.record.isUpdating = 1;
1650                                     doupdate();
1651                                     return;
1652                                 }
1653                                 
1654                                 if(!e.record.updatePending){
1655                                     e.record.updatePending = 1;
1656                                     doupdate();
1657                                     return;
1658                                 }
1659                                 Roo.log("got to end without doing an update?"  + e.record.data.coitem_linenumber);
1660                                 
1661                             },
1662                             beforeedit : function (e)
1663                             {
1664                                 // we can only edit if nothing is assigned to shipping or invoices..
1665                                 
1666                                 var rec = e.record
1667                             
1668                                 if (rec.data.coitem_qtyshipped > 0 || rec.data.cobill_billed > 0 || (rec.data.shipitem_shipped - rec.data.coitem_qtyshipped) > 0 ) {
1669                                     Roo.MessageBox.alert("Error", "That item has been shipped, has a draft shipment  or invoices - void the shipments/invoices first");
1670                                     e.cancel = true;
1671                                     return;
1672                                 }
1673                                 
1674                                 if (rec.data.coitem_subnumber * 1 > 0) {
1675                                     Roo.log("Edit container event");
1676                                     Roo.log(e); // if it's a tab.. 
1677                             
1678                                     
1679                                     switch(e.field) {
1680                                         // allow editing of source / destination..
1681                                         case 'coitem_shipto_id':
1682                                         case 'coitem_location_src':            
1683                                             return;
1684                                         default : 
1685                                             break;
1686                                     }
1687                                     Roo.MessageBox.alert("Error", "That is a kit item, edit the container.");
1688                                     e.cancel = true;
1689                                     return;
1690                                 }
1691                                 // zero off values..
1692                                 //if (e.field == 'coitem_qtyord' && rec.data.coitem_qtyord == 0) {
1693                                 //        e.value ='';
1694                                 //    }
1695                                 //    if (e.field == 'coitem_custprice' && rec.data.coitem_qtyord == 0.0) {
1696                                 //        e.value ='';
1697                                 //    }
1698                                 
1699                                 if (rec.data.item_type == 'K' && e.field == 'item_number') {
1700                                     // you can not change the product type on kits' as it messing things up..
1701                                     Roo.MessageBox.alert("Error", "That is a kit item,if you need to change it, delete it first.");
1702                                     e.cancel = true;
1703                                     return;
1704                                 }
1705                                 
1706                             },
1707                             celldblclick : function (_self, rowIndex, columnIndex, e)
1708                             {
1709                                 var rec = this.ds.getAt(rowIndex);
1710                                 var di = this.cm.getDataIndex(columnIndex);
1711                                 if (di != 'avail_qty') {
1712                                     return;
1713                                 }
1714                                 
1715                                 Pman.Dialog.XtupleInvHistory.show({
1716                                     itemsite_item_id_item_number   : rec.data.item_number,
1717                                    // itemsite_item_id_item_descript1 : rec.data.item_descrip1,
1718                                     location_name : rec.data.coitem_location_src_location_name,
1719                                     location_descrip : rec.data.coitem_location_src_location_descrip,
1720                                     
1721                                     invhist_transdate : _this.form.findField('cohead_targetdate').getValue() 
1722                                 }); 
1723                                 
1724                             },
1725                             rowclass : function (gridview, rowcfg)
1726                             {
1727                                 if (rowcfg.record.data.coitem_status == 'C' &&
1728                                     rowcfg.record.data.shipitem_shipped * 1 < 1) {
1729                                     
1730                                     rowcfg.rowClass = 'strikethrough';
1731                                 }
1732                                  if (rowcfg.record.data.coitem_status == 'X'  ) {
1733                                     
1734                                     rowcfg.rowClass = 'strikethrough';
1735                                 }
1736                                 
1737                                 if (!rowcfg.record.data.coitem_id) { 
1738                                         rowcfg.rowClass = 'dragon-not-saved';
1739                                 }
1740                                 
1741                                 
1742                                // Roo.log(rowcfg);
1743                             //    shipitem_shipped
1744                             }
1745                         },
1746                         autoExpandColumn : 'item_descrip1',
1747                         clicksToEdit : 1,
1748                         loadMask : true,
1749                         loadAvail : function() {
1750                             
1751                             
1752                             this.ds.each(function(r) {
1753                                 
1754                                 if(!r.data.item_number.length){
1755                                     return;
1756                                 }
1757                                 
1758                                 if(typeof(_this.stockcache[r.data.item_number]) != 'undefined'){
1759                                 
1760                                     r.set('avail_qty', _this.stockcache[r.data.item_number].qty);
1761                                     if (r.data.coitem_unitcost_in_order_cur * 1.0 < 0.1) {
1762                                         r.set('coitem_unitcost_in_order_cur', _this.stockcache[r.data.item_number].unitcost);
1763                                     }
1764                                     return;
1765                                 }
1766                                 
1767                                 var q = [];
1768                                 
1769                                 q.push( { 
1770                                     item : r.data.item_number, 
1771                                     loc: r.data.coitem_location_src_location_name,
1772                                     id: r.data.coitem_linenumber + (r.data.coitem_subnumber ? ('.' + r.data.coitem_subnumber) : '')
1773                                  } );
1774                                  
1775                                  new Pman.Request({
1776                                     url : baseURL + '/Roo/itemloc',
1777                                     method : 'POST',
1778                                     params : {
1779                                         _availqty : Roo.encode(q),
1780                                         curr_name : _this.form.findField('cohead_curr_id').el.dom.value
1781                                     },
1782                                     success : function(res) 
1783                                     {
1784                                         for (var i in res.data) {
1785                                            _this.stockcache[res.data[i].item] = res.data[i];
1786                                         }
1787                                         r.set('avail_qty', _this.stockcache[r.data.item_number].qty);
1788                                         if (r.data.coitem_unitcost_in_order_cur * 1.0 < 0.1) {
1789                                             r.set('coitem_unitcost_in_order_cur', _this.stockcache[r.data.item_number].unitcost);
1790                                         }
1791                                         //_this.grid.ds.fireEvent("update", _this.grid.ds, r, Roo.data.Record.EDIT);
1792                                         return
1793                                     }
1794                                 });
1795                             });
1796                                 
1797                                         
1798                         },
1799                         sm : {
1800                             xtype: 'CellSelectionModel',
1801                             xns: Roo.grid,
1802                             listeners : {
1803                                 tabend : function (_self)
1804                                 {
1805                                     _this.addItemBtn.fireEvent('click', _this.addItemBtn);
1806                                 },
1807                                 beforeeditnext : function (eventdata)
1808                                 {
1809                                     
1810                                     return;
1811                                     // this does not work, as the reload effect cancels editng.
1812                                     var rec = _this.grid.ds.getAt(eventdata.cell[0]);
1813                                     if (rec.data.coitem_subnumber *1 < 0 ) {
1814                                         return;
1815                                     }
1816                                     var r = eventdata.cell[0] + 1;
1817                                 
1818                                     while (true) {
1819                                         if (r > _this.grid.ds.getCount()-1 ) {
1820                                             eventdata.cell = false;
1821                                             return;
1822                                         }
1823                                         rec =  _this.grid.ds.getAt(r);
1824                                         if (rec.data.coitem_subnumber *1 < 0 ) {
1825                                            eventdata.cell = [ r, eventdata.cell[1] ];
1826                                            return;
1827                                         }
1828                                         r++;
1829                                     }
1830                                     
1831                                     
1832                                  
1833                                 
1834                                 }
1835                             },
1836                             enter_is_tab : true
1837                         },
1838                         footer : {
1839                             xtype: 'PagingToolbar',
1840                             xns: Roo,
1841                             displayInfo : true,
1842                             emptyMsg : "No Items",
1843                             pageSize : 100,
1844                             updateSummary : function() {
1845                                 var f = this;
1846                                 new Pman.Request({
1847                                     url : baseURL + '/Roo/Coitem',
1848                                     method : 'GET',
1849                                     params : {
1850                                         _totals : 1,
1851                                         coitem_cohead_id : _this.form.findField('cohead_id').getValue()
1852                                     },
1853                                     success : function(d) {
1854                                         //Roo.log(d);
1855                                         f.displayEl.update(String.format(
1856                                             "{0} items |  Subtotal: {2} | Tax: {3} | List Discount {4} | Total : {1}{5}",
1857                                             d.data.total_qty,
1858                                             _this.form.findField('cohead_curr_id').el.dom.value,
1859                                             d.data.total_sub,
1860                                             d.data.total_tax,
1861                                             d.data.total_list_discount,                
1862                                             d.data.total_total
1863                                         ));
1864                                             
1865                                     }
1866                                 });
1867                             },
1868                             items : [
1869                                 {
1870                                     xtype: 'Button',
1871                                     xns: Roo.Toolbar,
1872                                     listeners : {
1873                                         click : function (_self, e)
1874                                         {
1875                                             new Pman.Request({
1876                                                 url : baseURL + '/Roo/Cohead',
1877                                                 method : 'GET',
1878                                                 params : {
1879                                                     _fill_shipto : _this.form.findField('cohead_id').getValue()
1880                                                 },
1881                                                 success : function() {
1882                                                     _this.grid.footer.onClick('first');
1883                                                 }
1884                                             });
1885                                         }
1886                                     },
1887                                     text : "Fill empty Ship To"
1888                                 },
1889                                 {
1890                                     xtype: 'Button',
1891                                     xns: Roo.Toolbar,
1892                                     listeners : {
1893                                         click : function (_self, e)
1894                                         {
1895                                             Roo.MessageBox.confirm("Confirm", "Are you sure?<BR>"+
1896                                                 "This will set all the locations to match the Sales Order - and remove all old locations.",
1897                                                 function (res) {
1898                                                     if(res!='yes') {
1899                                                         return;
1900                                                     
1901                                                     }
1902                                                     new Pman.Request({
1903                                                         url : baseURL + '/Roo/Cohead',
1904                                                         method : 'GET',
1905                                                         params : {
1906                                                             _fill_location : _this.form.findField('cohead_id').getValue(),
1907                                                             _location_id : _this.form.findField('cohead_location_src').getValue()
1908                                                         },
1909                                                         success : function() {
1910                                                             _this.grid.footer.onClick('first');
1911                                                         }
1912                                                     });
1913                                             });
1914                                             
1915                                         }
1916                                     },
1917                                     text : "Update Location to match S/O"
1918                                 },
1919                                 {
1920                                     xtype: 'Button',
1921                                     xns: Roo.Toolbar,
1922                                     listeners : {
1923                                         toggle : function (_self, pressed)
1924                                         {
1925                                             this.setText(pressed ? "Prices exc GST" : "Prices with GST");
1926                                             var cm = _this.grid.getColumnModel();
1927                                         
1928                                             cm.setHidden(cm.getIndexByDataIndex('coitem_price_tax'), pressed ? false : true);
1929                                             cm.setHidden(cm.getIndexByDataIndex('coitem_custprice_tax'), pressed ? false : true);
1930                                             cm.setHidden(cm.getIndexByDataIndex('coitem_subtotal_tax'), pressed ? false : true);
1931                                             
1932                                             cm.setHidden(cm.getIndexByDataIndex('coitem_price'), pressed ? true : false);
1933                                             cm.setHidden(cm.getIndexByDataIndex('coitem_custprice'), pressed ? true : false);
1934                                             cm.setHidden(cm.getIndexByDataIndex('coitem_subtotal'), pressed ? true : false);
1935                                             return;
1936                                         },
1937                                         render : function (_self)
1938                                         {
1939                                             _this.showgstBtn = _self;
1940                                         }
1941                                     },
1942                                     enableToggle : true,
1943                                     minWidth : 100,
1944                                     text : "Prices with GST"
1945                                 }
1946                             ]
1947                         },
1948                         dataSource : {
1949                             xtype: 'Store',
1950                             xns: Roo.data,
1951                             listeners : {
1952                                 beforeload : function (_self,o) {
1953                                 
1954                                     try {
1955                                        this.removeAll();
1956                                    } catch (e) { }
1957                                    
1958                                 
1959                                     if (!_this.data || !_this.data.cohead_id) {
1960                                         return false;
1961                                     }
1962                                     o.params = o.params || {};
1963                                     
1964                                     o.params.coitem_cohead_id = _this.data.cohead_id;
1965                                     o.params._without_list_discount =1;
1966                                     //o.params.limit = 999;
1967                                 
1968                                     
1969                                 },
1970                                 update : function (_self, rec, operation)
1971                                     {
1972                                    
1973                                    if (operation !=  Roo.data.Record.COMMIT) {
1974                                        return;
1975                                    }
1976                                 
1977                                    // row has been updated..
1978                                    // if the qty + item has been filled in, we should try and save it..
1979                                  
1980                                     
1981                                     var setRecord = function(){
1982                                     
1983                                          Roo.log("Clearing update?"  + rec.data.coitem_linenumber);
1984                                         rec.updatePending = 0;
1985                                         if(rec.isInserting){
1986                                             rec.isInserting = 0;
1987                                             return;
1988                                         }
1989                                         rec.isUpdating = 0;
1990                                 
1991                                     }
1992                                     if (!(rec.data.coitem_itemsite_id * 1) || !(rec.data.coitem_qtyord*1)) {
1993                                         setRecord();
1994                                         return;
1995                                     }
1996                                     var     doCommit = function() {
1997                                          Roo.log("Sending  data?"  + rec.data.coitem_linenumber);
1998                                         
1999                                         new Pman.Request({
2000                                             url : baseURL+'/Roo/coitem',
2001                                             method : 'POST',
2002                                             params : rec.data,
2003                                             success: function(res)
2004                                             {
2005                                                 try {
2006                                                     var row = _this.grid.ds.indexOf(rec);
2007                                                     Roo.get(_this.grid.view.getRow(row)).removeClass('dragon-not-saved');
2008                                                 } catch(e) {
2009                                                     Roo.log(e);
2010                                                 }
2011                                                     
2012                                                 
2013                                                  Roo.log("GOT success: "  + rec.data.coitem_linenumber);
2014                                                 //Roo.log("GOT success");
2015                                                 // update the data...
2016                                                 
2017                                                 if (rec.data.item_type == 'K') {
2018                                                      Roo.log("Kit??");
2019                                                     _this.grid.ds.load({});
2020                                                     
2021                                                     return;
2022                                                 }
2023                                                 
2024                                                 // why is this here.??
2025                                                 
2026                                                 if (_this.grid.activeEditor) {
2027                                                      rec.editing = true;
2028                                                      
2029                                                      
2030                                                  } 
2031                                                  
2032                                                 rec.set('coitem_id', res.data.coitem_id);
2033                                                 rec.set('coitem_status', res.data.coitem_status);
2034                                                  
2035                                                 
2036                                                 rec.dirty = false;
2037                                                 delete rec.modified;
2038                                                 try {
2039                                                     _this.grid.footer.updateSummary();          
2040                                                     _this.grid.loadAvail();
2041                                                 } catch (e) { }
2042                                                 
2043                                 
2044                                                 setRecord();
2045                                             },
2046                                             failure : function(res)
2047                                             {
2048                                                 setRecord();
2049                                                 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg : "Error updating");
2050                                             }
2051                                             
2052                                             
2053                                         });
2054                                       } ; 
2055                                    
2056                                     
2057                                     doCommit();
2058                                     
2059                                     
2060                                     
2061                                 },
2062                                 load : function (_self, records, options)
2063                                 {
2064                                     // need to fetch availablity from master data..
2065                                     // build a list of what to ask..
2066                                 
2067                                     // query: ITEM CODE - LOCATION
2068                                     _this.grid.footer.updateSummary();
2069                                     _this.grid.loadAvail.defer(100, _this.grid);
2070                                 
2071                                     
2072                                     
2073                                     
2074                                 }
2075                             },
2076                             remoteSort : true,
2077                             sortInfo : { field : 'coitem_linenumber,coitem_subnumber', direction: 'ASC' },
2078                             proxy : {
2079                                 xtype: 'HttpProxy',
2080                                 xns: Roo.data,
2081                                 method : 'GET',
2082                                 url : baseURL + '/Roo/coitem.php'
2083                             },
2084                             reader : {
2085                                 xtype: 'JsonReader',
2086                                 xns: Roo.data,
2087                                 id : 'coitem_id',
2088                                 root : 'data',
2089                                 totalProperty : 'total',
2090                                 fields : [
2091                                     {
2092                                         'name': 'coitem_linenumber',
2093                                         'type': 'int'
2094                                     },
2095                                     {
2096                                         'name': 'coitem_itemsite_id',
2097                                         'type': 'int'
2098                                     },
2099                                     {
2100                                         'name': 'coitem_qtyord'
2101                                     },
2102                                     {
2103                                         'name': 'coitem_unitcost'
2104                                     },
2105                                     {
2106                                         'name': 'coitem_price'
2107                                     },
2108                                     {
2109                                         'name': 'coitem_custprice'
2110                                     },
2111                                     {
2112                                         'name': 'coitem_qtyreturned'
2113                                     },
2114                                     {
2115                                         'name': 'coitem_prcost'
2116                                     },
2117                                     {
2118                                         'name': 'coitem_price_uom_id',
2119                                         'type': 'int'
2120                                     },
2121                                     {
2122                                         'name': 'coitem_qtyreserved'
2123                                     }
2124                                 ]
2125                             }
2126                         },
2127                         toolbar : {
2128                             xtype: 'Toolbar',
2129                             xns: Roo,
2130                             items : [
2131                                 {
2132                                     xtype: 'Button',
2133                                     xns: Roo.Toolbar,
2134                                     listeners : {
2135                                         click : function()
2136                                         {
2137                                            
2138                                             Roo.log("add presed");
2139                                              
2140                                             // work out last 
2141                                             var grid = _this.grid;
2142                                             var err = false;
2143                                             grid.ds.each(function(r) {
2144                                                 if (r.data.coitem_qtyord < 1) { 
2145                                                     Roo.MessageBox.alert("Error", "you must fill in a quantity for " + r.data.item_number);
2146                                                     err = true;
2147                                                     return true;
2148                                                 }
2149                                                 
2150                                             });
2151                                             if (err) {
2152                                                 return;
2153                                             }
2154                                             
2155                                         
2156                                         //    var last = 0;
2157                                             var last = _this.form.findField('cohead_max_linenumber').getValue();   
2158                                             last++; 
2159                                             if(last == 99999){
2160                                                 last++;
2161                                             }
2162                                             
2163                                         
2164                                             _this.form.findField('cohead_max_linenumber').setValue(last);    
2165                                             
2166                                             // this should be getting the previous row..??
2167                                              var ct  =    _this.grid.ds.getCount();
2168                                              var lastrow = ct ?  _this.grid.ds.getAt(ct-1)  : false;
2169                                              
2170                                              function lastor(k,d,kk) {
2171                                                 var def = d ? _this.form.findField(k).el.dom.value : _this.form.findField(k).getValue();
2172                                                 return lastrow ? lastrow.data[kk] : def;
2173                                              }
2174                                             
2175                                             // uses form defaults or last row value.
2176                                             var nr = _this.grid.ds.reader.newRow({
2177                                                 coitem_linenumber : last,
2178                                                 item_number : '',
2179                                                 item_descrip1 : '',
2180                                                 coitem_qtyord : 0,
2181                                                 coitem_cohead_id : _this.form.findField('cohead_id').getValue(),
2182                                                 coitem_qtyshipped : 0,
2183                                                 coitem_location_src : lastor('cohead_location_src',false, 'coitem_location_src'),
2184                                                 coitem_location_src_location_name : lastor('cohead_location_src',true, 'coitem_location_src_location_name'),
2185                                                 coitem_shipto_id    :  lastor('cohead_shipto_id',false, 'coitem_shipto_id'),
2186                                                 coitem_shipto_id_shipto_name  : lastor('cohead_shipto_id_shipto_name', false, 'coitem_shipto_id_shipto_name'),
2187                                                 coitem_unitcost_in_order_cur  : 0,
2188                                                 coitem_taxtype_id : _this.data.default_taxtype_id,
2189                                                 coitem_taxtype_id_taxtype_name : 'Taxable',
2190                                                 coitem_status : '',
2191                                                 avail_qty : 0
2192                                                         
2193                                             });
2194                                             grid.stopEditing();
2195                                             grid.ds.insert(grid.ds.getCount(), nr); 
2196                                             grid.startEditing(grid.ds.getCount()-1, 1); // type..
2197                                             nr.updatePending = 0;
2198                                             nr.isUpdating = 0;
2199                                             nr.isInserting =0;
2200                                             
2201                                         },
2202                                         render : function (_self)
2203                                         {
2204                                             _this.addItemBtn = _self;
2205                                         }
2206                                     },
2207                                     cls : 'x-btn-text-icon',
2208                                     text : "Add",
2209                                     icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
2210                                 },
2211                                 {
2212                                     xtype: 'Button',
2213                                     xns: Roo.Toolbar,
2214                                     listeners : {
2215                                         click : function (_self, e)
2216                                         {
2217                                             var last = _this.form.findField('cohead_max_linenumber').getValue();    \r
2218                                             last++;\r
2219                                             _this.form.findField('cohead_max_linenumber').setValue(last); 
2220                                         
2221                                             var ct  =    _this.grid.ds.getCount();\r
2222                                             var lastrow = ct ?  _this.grid.ds.getAt(ct-1)  : false;\r
2223                                                  \r
2224                                             function lastor(k,d,kk) {\r
2225                                                var def = d ? _this.form.findField(k).el.dom.value : _this.form.findField(k).getValue();\r
2226                                                return lastrow ? lastrow.data[kk] : def;\r
2227                                             }
2228                                             
2229                                             var cohead_cust_id = _this.form.findField('cohead_cust_id').getValue();
2230                                             var cohead_id = _this.form.findField('cohead_id').getValue();
2231                                             
2232                                             Pman.Dialog.XtupleSalesProductList.show( {cohead_cust_id : cohead_cust_id, cohead_id : cohead_id} , function(res) {
2233                                         
2234                                                 _this.grid.stopEditing();
2235                                         
2236                                                 if (_this.grid.ds.getCount() > 0) {
2237                                                     var lr = _this.grid.ds.getAt(_this.grid.ds.getCount()-1);
2238                                                     if (!lr.data.coitem_itemsite_id) {
2239                                                         lr.set('coitem_itemsite_id', res.item_itemsite_id_itemsite_id);
2240                                                         lr.set('item_number',  res.item_number);
2241                                                         lr.set('item_descrip1', res.item_descrip1);                                
2242                                                         lr.set('coitem_listprice', res.item_price);                
2243                                                         lr.set('coitem_price', res.item_price);                                
2244                                                         lr.set('coitem_custprice', res.item_price);                
2245                                                         return;
2246                                                     }
2247                                                 }
2248                                                 
2249                                                 
2250                                                 
2251                                                 var nr = _this.grid.ds.reader.newRow({
2252                                                     coitem_linenumber : last,
2253                                                     coitem_itemsite_id : res.item_itemsite_id_itemsite_id,
2254                                                     item_number : res.item_number,
2255                                                     item_descrip1 : res.item_descrip1,
2256                                                     coitem_qtyord : 0,
2257                                                     coitem_cohead_id : _this.form.findField('cohead_id').getValue(),
2258                                                     coitem_qtyshipped : 0,
2259                                                     coitem_listprice : res.item_price,
2260                                                     coitem_price : res.item_price,
2261                                                     coitem_custprice : res.item_price,
2262                                                     avail_qty : 0,
2263                                                     coitem_location_src : lastor('cohead_location_src',false, 'coitem_location_src'),
2264                                                     coitem_location_src_location_name : lastor('cohead_location_src',true, 'coitem_location_src_location_name'),
2265                                                     coitem_shipto_id    :  lastor('cohead_shipto_id',false, 'coitem_shipto_id'),
2266                                                     coitem_shipto_id_shipto_name  : lastor('cohead_shipto_id_shipto_name', false, 'coitem_shipto_id_shipto_name')
2267                                                             
2268                                                 });
2269                                                 _this.grid.ds.insert(_this.grid.ds.getCount(), nr); 
2270                                         
2271                                            }); 
2272                                         }
2273                                     },
2274                                     cls : 'x-btn-text-icon',
2275                                     text : "Find Products",
2276                                     icon : rootURL + '/Pman/templates/images/search.gif'
2277                                 },
2278                                 {
2279                                     xtype: 'Button',
2280                                     xns: Roo.Toolbar,
2281                                     listeners : {
2282                                         click : function()
2283                                         {
2284                                             // work out last 
2285                                             new Pman.Request({
2286                                                 url : baseURL + '/Roo/coitem',
2287                                                 mask : 'Loading Data',
2288                                                 method: 'GET',
2289                                                 params : {
2290                                         
2291                                                     _hk_xfer :_this.form.findField('cohead_id').getValue()
2292                                                 },
2293                                                 success : function() {
2294                                                     _this.grid.ds.load({});
2295                                                 }
2296                                             
2297                                             });
2298                                         },
2299                                         render : function (_self)
2300                                         {
2301                                             _this.xferAll  = _self;
2302                                         }
2303                                     },
2304                                     cls : 'x-btn-text-icon',
2305                                     hidden : true,
2306                                     text : "Xfer all stock to HK",
2307                                     icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
2308                                 },
2309                                 {
2310                                     xtype: 'Button',
2311                                     xns: Roo.Toolbar,
2312                                     listeners : {
2313                                         click : function (_self, e)
2314                                         {
2315                                            var c    = _this.grid.getSelectionModel().getSelectedCell();
2316                                            if (!c) {
2317                                                 Roo.MessageBox.alert("Error", "Select item to show history (you can also double click on the #avail number) ");
2318                                                 return;
2319                                            }
2320                                            
2321                                             var rec = _this.grid.ds.getAt(c[0]);
2322                                         
2323                                           Pman.Dialog.XtupleInvHistory.show({
2324                                                 itemsite_item_id_item_number   : rec.data.item_number,
2325                                                // itemsite_item_id_item_descript1 : rec.data.item_descrip1,
2326                                                 location_name : rec.data.coitem_location_src_location_name,
2327                                                 location_descrip : rec.data.coitem_location_src_location_descrip,
2328                                                 
2329                                                 
2330                                                 invhist_transdate : _this.form.findField('cohead_targetdate').getValue() 
2331                                             }); 
2332                                             
2333                                            
2334                                         }
2335                                     },
2336                                     text : "Show Inventory History"
2337                                 },
2338                                 {
2339                                     xtype: 'Separator',
2340                                     xns: Roo.Toolbar
2341                                 },
2342                                 {
2343                                     xtype: 'TextItem',
2344                                     xns: Roo.Toolbar,
2345                                     text : "Apply % Discount of : "
2346                                 },
2347                                 {
2348                                     xtype: 'TextField',
2349                                     xns: Roo.form,
2350                                     listeners : {
2351                                         render : function (_self)
2352                                         {
2353                                             _this._applyDiscount = _self;
2354                                         }
2355                                     },
2356                                     width : 40
2357                                 },
2358                                 {
2359                                     xtype: 'Button',
2360                                     xns: Roo.Toolbar,
2361                                     listeners : {
2362                                         click : function (_self, e)
2363                                         {
2364                                             var val =  parseFloat(_this._applyDiscount.getValue());
2365                                             
2366                                             var factor = (100.0 - val)/100.0;
2367                                             
2368                                             _this.grid.ds.each(function(rec) {
2369                                                 if (rec.data.coitem_qtyshipped > 0 || rec.data.cobill_billed > 0) {
2370                                                     return;
2371                                                 }
2372                                             
2373                                                 rec.set('coitem_price', rec.data.coitem_custprice * factor);
2374                                                 rec.set('coitem_linedisc', val);
2375                                                 rec.set('coitem_subtotal', rec.data.coitem_price  & rec.data.ordqty);
2376                                                 rec.commit();
2377                                             
2378                                             });
2379                                             
2380                                             
2381                                         }
2382                                     },
2383                                     text : "Apply To All"
2384                                 },
2385                                 {
2386                                     xtype: 'Button',
2387                                     xns: Roo.Toolbar,
2388                                     listeners : {
2389                                         click : function (_self, e)
2390                                         {
2391                                         
2392                                             if (!(1* _this.form.findField('cohead_id').getValue())) {
2393                                                 Roo.MessageBox.alert("Error", "save the order first");
2394                                             }
2395                                             
2396                                         
2397                                            Pman.Dialog.Image.show(
2398                                                {
2399                                                     timeout : 60000,
2400                                                     _url : baseURL+'/Xtuple/Import/SalesOrder',
2401                                                     onid : _this.form.findField('cohead_id').getValue()
2402                                                 
2403                                                },
2404                                                function (data) {
2405                                         
2406                                                     Roo.MessageBox.alert("Notice", "Uploaded");
2407                                                     _this.grid.footer.onClick('first');
2408                                         
2409                                                }
2410                                            );
2411                                         }
2412                                     },
2413                                     text : "Import"
2414                                 },
2415                                 {
2416                                     xtype: 'Fill',
2417                                     xns: Roo.Toolbar
2418                                 },
2419                                 {
2420                                     xtype: 'Button',
2421                                     xns: Roo.Toolbar,
2422                                     listeners : {
2423                                         click : function()
2424                                         {
2425                                              _this.grid.stopEditing();
2426                                             // check that no shipments or invoices are done..
2427                                             var rc = _this.grid.getSelectionModel().getSelectedCell();
2428                                             
2429                                             var rec = _this.grid.ds.getAt(rc[0]);
2430                                             
2431                                             if (rec.data.coitem_qtyshipped > 0 || rec.data.cobill_billed > 0) {
2432                                                 Roo.MessageBox.alert("Error", "That item has been shipped or invoices - void the shipments/invoices first");
2433                                                 return;
2434                                             }
2435                                            if (rec.data.coitem_subnumber*1  > 0) {
2436                                                 Roo.MessageBox.alert("Error", "Delete the kit item that that belongs to.");
2437                                                 return;
2438                                             }
2439                                             if (!rec.data.coitem_id) {
2440                                                 _this.grid.ds.remove(rec);
2441                                                 return;
2442                                             }
2443                                             function remove()
2444                                             {
2445                                             
2446                                                 new  Pman.Request({
2447                                                     url : baseURL + '/Roo/coitem',
2448                                                     method : 'POST',
2449                                                     params : {
2450                                                         _delete : rec.data.coitem_id
2451                                                     
2452                                                     },
2453                                                     success : function() {
2454                                                         if (rec.data.item_type == 'K') {
2455                                                             _this.grid.ds.load({});
2456                                                             return;
2457                                                         }
2458                                                         _this.grid.ds.remove(rec);
2459                                                     }
2460                                                 
2461                                                 });
2462                                             }
2463                                             if (rec.data_qtyord * 1 < 1) {
2464                                                 remove();
2465                                             }
2466                                             
2467                                             Roo.MessageBox.confirm("Confirm", "Are you sure you want to delete that line?", function(r)
2468                                             {
2469                                                 if (r != 'yes') {
2470                                                     return;
2471                                                 }
2472                                                 remove();
2473                                             
2474                                             });
2475                                         
2476                                             
2477                                             
2478                                         }
2479                                     },
2480                                     cls : 'x-btn-text-icon',
2481                                     text : "Delete",
2482                                     icon : rootURL + '/Pman/templates/images/trash.gif'
2483                                 },
2484                                 {
2485                                     xtype: 'Button',
2486                                     xns: Roo.Toolbar,
2487                                     listeners : {
2488                                         click : function()
2489                                         {
2490                                              _this.grid.stopEditing();
2491                                             // check that no shipments or invoices are done..
2492                                             var ids = [];
2493                                             _this.grid.ds.each(function(rec) {
2494                                             
2495                                         
2496                                             
2497                                                 if (rec.data.coitem_qtyshipped > 0 || rec.data.cobill_billed > 0) {
2498                                                     //Roo.MessageBox.alert("Error", "That item has been shipped or invoices - void the shipments/invoices first");
2499                                                     return;
2500                                                 }
2501                                                 if (rec.data.item_type == 'K') {
2502                                                     return;
2503                                                 }
2504                                         
2505                                                if (rec.data.coitem_subnumber*1  > 0) {
2506                                                     //Roo.MessageBox.alert("Error", "Delete the kit item that that belongs to.");
2507                                                     return;
2508                                                 }
2509                                                 if (!rec.data.coitem_id) {
2510                                                     _this.grid.ds.remove(rec);
2511                                                     return;
2512                                                 }
2513                                                 ids.push(rec.data.coitem_id);
2514                                             });
2515                                             function remove()
2516                                             {
2517                                             
2518                                                 new  Pman.Request({
2519                                                     url : baseURL + '/Roo/coitem',
2520                                                     method : 'POST',
2521                                                     params : {
2522                                                         _delete : ids.join(',')
2523                                                     
2524                                                     },
2525                                                     success : function() {
2526                                         
2527                                                         _this.grid.footer.onClick('first');
2528                                                     }
2529                                                 
2530                                                 });
2531                                             }
2532                                         //    if (rec.data_qtyord * 1 < 1) {
2533                                         //        remove();
2534                                         //    }
2535                                             
2536                                             Roo.MessageBox.confirm("Confirm", "Are you sure you want to delete everything?", function(r)
2537                                             {
2538                                                 if (r != 'yes') {
2539                                                     return;
2540                                                 }
2541                                                 remove();
2542                                             
2543                                             });
2544                                         
2545                                             
2546                                             
2547                                         }
2548                                     },
2549                                     cls : 'x-btn-text-icon',
2550                                     text : "Delete All",
2551                                     icon : rootURL + '/Pman/templates/images/trash.gif'
2552                                 }
2553                             ]
2554                         },
2555                         colModel : [
2556                             {
2557                                 xtype: 'ColumnModel',
2558                                 xns: Roo.grid,
2559                                 dataIndex : 'coitem_linenumber',
2560                                 header : 'Item#',
2561                                 width : 40,
2562                                 renderer : function(v,x,r) {
2563                                 
2564                                     if (r.data.coitem_subnumber * 1 > 0) {
2565                                          return String.format('{0}.{1}', v,r.data.coitem_subnumber);
2566                                      }
2567                                      return String.format('{0}', v);
2568                                   }
2569                             },
2570                             {
2571                                 xtype: 'ColumnModel',
2572                                 xns: Roo.grid,
2573                                 dataIndex : 'item_number',
2574                                 header : 'Item Code',
2575                                 width : 75,
2576                                 renderer : function(v) { return String.format('{0}', v); },
2577                                 editor : {
2578                                     xtype: 'GridEditor',
2579                                     xns: Roo.grid,
2580                                     field : {
2581                                         xtype: 'ComboBox',
2582                                         xns: Roo.form,
2583                                         listeners : {
2584                                             beforeselect : function (combo, record, index)
2585                                             {
2586                                               // set _this.data values ..
2587                                               var ar = _this.grid.activeEditor.record;
2588                                             //  Roo.log('beforeselect');
2589                                               
2590                                               
2591                                               (function() { 
2592                                                  //  Roo.log('beforeselect-cb');
2593                                                   ar.set('item_descrip1', record.data.itemsite_item_id_item_descrip1);
2594                                                   ar.set('coitem_listprice', record.data.item_listprice);
2595                                                   ar.set('coitem_price', record.data.item_price);
2596                                                   ar.set('coitem_custprice', record.data.item_price);
2597                                                   ar.set('coitem_itemsite_id', record.data.itemsite_id);
2598                                                   ar.set('item_number', record.data.itemsite_item_id_item_number);
2599                                                  ar.set('item_type', record.data.itemsite_item_id_item_type);
2600                                                   ar.set('avail_qty', 0);
2601                                                   ar.commit();
2602                                               }).defer(100);
2603                                               
2604                                             }
2605                                         },
2606                                         allowBlank : false,
2607                                         displayField : 'itemsite_item_id_item_number',
2608                                         editable : true,
2609                                         emptyText : "Select item",
2610                                         forceSelection : true,
2611                                         hiddenName : 'itemsite_item_id_item_number',
2612                                         listWidth : 400,
2613                                         loadingText : "Searching...",
2614                                         minChars : 2,
2615                                         name : 'item_number',
2616                                         pageSize : 20,
2617                                         qtip : "Select item",
2618                                         queryParam : 'query[number]',
2619                                         selectOnFocus : true,
2620                                         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>',
2621                                         triggerAction : 'all',
2622                                         typeAhead : false,
2623                                         valueField : 'item_number',
2624                                         store : {
2625                                             xtype: 'Store',
2626                                             xns: Roo.data,
2627                                             listeners : {
2628                                                 beforeload : function (_self, o){
2629                                                     o.params = o.params || {};
2630                                                     o.params.customer_id = _this.form.findField('cohead_cust_id').getValue();
2631                                                     o.params['query[cohead_id]'] = _this.form.findField('cohead_id').getValue();
2632                                                     //o.params.shipto_cust_id = _this.data.cohead_cust_id;
2633                                                     // set more here
2634                                                 }
2635                                             },
2636                                             remoteSort : true,
2637                                             sortInfo : { direction : 'ASC', field: 'item_number' },
2638                                             proxy : {
2639                                                 xtype: 'HttpProxy',
2640                                                 xns: Roo.data,
2641                                                 method : 'GET',
2642                                                 url : baseURL + '/Roo/itemsite.php'
2643                                             },
2644                                             reader : {
2645                                                 xtype: 'JsonReader',
2646                                                 xns: Roo.data,
2647                                                 id : 'shipto_id',
2648                                                 root : 'data',
2649                                                 totalProperty : 'total',
2650                                                 fields : [{"name":"item_id","type":"int"},"item_number"]
2651                                             }
2652                                         }
2653                                     }
2654                                 }
2655                             },
2656                             {
2657                                 xtype: 'ColumnModel',
2658                                 xns: Roo.grid,
2659                                 dataIndex : 'coitem_location_src',
2660                                 header : 'From',
2661                                 width : 75,
2662                                 renderer : function(v,x,r) { 
2663                                     return String.format('{0}', r.data.coitem_location_src_location_name); 
2664                                 },
2665                                 editor : {
2666                                     xtype: 'GridEditor',
2667                                     xns: Roo.grid,
2668                                     field : {
2669                                         xtype: 'ComboBox',
2670                                         xns: Roo.form,
2671                                         listeners : {
2672                                             beforeselect : function (combo, record, index)
2673                                             {
2674                                               // set _this.data values ..
2675                                               var ar = _this.grid.activeEditor.record;
2676                                               
2677                                               
2678                                               
2679                                               //Roo.log('beforeselect');
2680                                              
2681                                              /*
2682                                               (function() { 
2683                                                  //  Roo.log('beforeselect-cb');
2684                                                   ar.set('item_descrip1', record.data.itemsite_item_id_item_descrip1);
2685                                                   ar.set('coitem_price', record.data.item_listprice);
2686                                                   ar.set('coitem_custprice', record.data.item_price);
2687                                                    ar.set('coitem_itemsite_id', record.data.itemsite_id);
2688                                                   ar.set('item_number', record.data.itemsite_item_id_item_number);
2689                                             
2690                                                   ar.commit();
2691                                               }).defer(100);
2692                                               */
2693                                             }
2694                                         },
2695                                         allowBlank : false,
2696                                         alwaysQuery : true,
2697                                         displayField : 'location_name',
2698                                         editable : true,
2699                                         emptyText : "Select location",
2700                                         forceSelection : true,
2701                                         hiddenName : 'coitem_location_src',
2702                                         listWidth : 400,
2703                                         loadingText : "Searching...",
2704                                         minChars : 2,
2705                                         name : 'coitem_location_src_location_name',
2706                                         pageSize : 20,
2707                                         qtip : "Select item",
2708                                         queryParam : 'query[location_name]',
2709                                         selectOnFocus : true,
2710                                         tpl : '<div class="x-grid-cell-text x-btn button"><b> {location_name}</b> {location_descrip}</div>',
2711                                         triggerAction : 'all',
2712                                         typeAhead : false,
2713                                         valueField : 'location_id',
2714                                         store : {
2715                                             xtype: 'Store',
2716                                             xns: Roo.data,
2717                                             listeners : {
2718                                                 beforeload : function (_self, o){
2719                                                     o.params = o.params || {};
2720                                                     
2721                                                     var row = _this.grid.activeEditor.record;
2722                                                     
2723                                                     o.params['query[item_itemsite_id]'] = row.data.coitem_itemsite_id;
2724                                                     // need to know the date to calc the est. delivery time..
2725                                                     //o.params['query[avail_when]'] = _this.form.findField('cohead_targetdate').getValue().format('Y-m-d');
2726                                                     //o.params.location_netable = 1;
2727                                                     o.params['query[cohead_id]'] = _this.form.findField('cohead_id').getValue(); 
2728                                                     
2729                                                      o.params.location_restrict = 0;
2730                                                     o.params._notinternalcompany = 1;
2731                                                     
2732                                                   //  _this.grid;
2733                                                 
2734                                                 //    o.params.itemsite_id = _this.form.findField('cohead_cust_id').getValue();
2735                                                     //o.params.shipto_cust_id = _this.data.cohead_cust_id;
2736                                                     // set more here
2737                                                 }
2738                                             },
2739                                             remoteSort : true,
2740                                             sortInfo : { direction : 'ASC', field: 'location_name' },
2741                                             proxy : {
2742                                                 xtype: 'HttpProxy',
2743                                                 xns: Roo.data,
2744                                                 method : 'GET',
2745                                                 url : baseURL + '/Roo/location.php'
2746                                             },
2747                                             reader : {
2748                                                 xtype: 'JsonReader',
2749                                                 xns: Roo.data,
2750                                                 id : 'shipto_id',
2751                                                 root : 'data',
2752                                                 totalProperty : 'total',
2753                                                 fields : [{"name":"location_id","type":"int"},"location_name"]
2754                                             }
2755                                         }
2756                                     }
2757                                 }
2758                             },
2759                             {
2760                                 xtype: 'ColumnModel',
2761                                 xns: Roo.grid,
2762                                 dataIndex : 'coitem_shipto_id',
2763                                 header : 'To',
2764                                 width : 75,
2765                                 renderer : function(v,x,r) { 
2766                                     return String.format('{0}:{1}', v, r.data.coitem_shipto_id_shipto_name); 
2767                                 },
2768                                 editor : {
2769                                     xtype: 'GridEditor',
2770                                     xns: Roo.grid,
2771                                     field : {
2772                                         xtype: 'ComboBox',
2773                                         xns: Roo.form,
2774                                         listeners : {
2775                                             add : function (combo)
2776                                             {
2777                                             
2778                                                Pman.Dialog.XtupleCustomer.show({ cust_id : _this.form.findField('cohead_cust_id').getValue() }, function(data) {
2779                                                     // refresh the data in the pulldown..
2780                                                 }); 
2781                                             },
2782                                             beforeselect : function (combo, record, index)
2783                                             {
2784                                             
2785                                                 var v = [];
2786                                                 for(var i = 1; i <4; i++) {
2787                                                     _this.data['cohead_shiptoaddress'+  i] = record.data['cntct_addr_id_addr_line'+i];
2788                                                 }
2789                                                 _this.form.findField('shipto_address').update();
2790                                                 
2791                                              
2792                                             }
2793                                         },
2794                                         allowBlank : false,
2795                                         alwaysQuery : true,
2796                                         displayField : 'shipto_name',
2797                                         editable : false,
2798                                         emptyText : "Select cntct",
2799                                         forceSelection : true,
2800                                         hiddenName : 'cohead_shipto_id',
2801                                         listWidth : 400,
2802                                         loadingText : "Searching...",
2803                                         minChars : 2,
2804                                         name : 'coitem_shipto_id_shipto_name',
2805                                         pageSize : 20,
2806                                         qtip : "Select shipto",
2807                                         queryParam : 'query[shipto_name]',
2808                                         selectOnFocus : true,
2809                                         tpl : '<div class="x-grid-cell-text x-btn button"><b>{shipto_id}:{shipto_addr_id_addr_name}</b> </div>',
2810                                         triggerAction : 'all',
2811                                         typeAhead : false,
2812                                         valueField : 'shipto_id',
2813                                         width : 300,
2814                                         store : {
2815                                             xtype: 'Store',
2816                                             xns: Roo.data,
2817                                             listeners : {
2818                                                 beforeload : function (_self, o){
2819                                                     o.params = o.params || {};
2820                                                     o.params.shipto_cust_id = _this.data.cohead_cust_id; 
2821                                                     //o.params['query[with_shipinfo]'] = 1;
2822                                                     // set more here
2823                                                 }
2824                                             },
2825                                             remoteSort : true,
2826                                             sortInfo : { direction : 'ASC', field: 'shipto_name' },
2827                                             proxy : {
2828                                                 xtype: 'HttpProxy',
2829                                                 xns: Roo.data,
2830                                                 method : 'GET',
2831                                                 url : baseURL + '/Roo/shiptoinfo.php'
2832                                             },
2833                                             reader : {
2834                                                 xtype: 'JsonReader',
2835                                                 xns: Roo.data,
2836                                                 id : 'shipto_id',
2837                                                 root : 'data',
2838                                                 totalProperty : 'total',
2839                                                 fields : [{"name":"cntct_id","type":"int"},"cntct_name"]
2840                                             }
2841                                         }
2842                                     }
2843                                 }
2844                             },
2845                             {
2846                                 xtype: 'ColumnModel',
2847                                 xns: Roo.grid,
2848                                 dataIndex : 'item_descrip1',
2849                                 header : 'Item Description',
2850                                 width : '150.00',
2851                                 renderer : function(v,x,r) { 
2852                                 
2853                                     if (r.data.coitem_memo && r.data.coitem_memo.length) {
2854                                         return String.format('{0}', r.data.coitem_memo);
2855                                         if (r.data.coitem_memo != v) {
2856                                             r.set('item_descrip1', r.data.coitem_memo);
2857                                         }
2858                                         
2859                                     }
2860                                     if (v && v.length > 49) {
2861                                         return String.format('<span style="color:orange" qtip="line may be too long to print">{0}</span>', v);
2862                                     }
2863                                     
2864                                     return String.format('{0}', v); 
2865                                     
2866                                 },
2867                                 editor : {
2868                                     xtype: 'GridEditor',
2869                                     xns: Roo.grid,
2870                                     field : {
2871                                         xtype: 'TextField',
2872                                         xns: Roo.form,
2873                                         allowBlank : false
2874                                     }
2875                                 }
2876                             },
2877                             {
2878                                 xtype: 'ColumnModel',
2879                                 xns: Roo.grid,
2880                                 align : 'right',
2881                                 dataIndex : 'coitem_status',
2882                                 header : 'Status',
2883                                 width : 50,
2884                                 renderer : function(v) { return String.format('{0}', v); }
2885                             },
2886                             {
2887                                 xtype: 'ColumnModel',
2888                                 xns: Roo.grid,
2889                                 align : 'right',
2890                                 dataIndex : 'coitem_qtyord',
2891                                 header : 'Qty',
2892                                 width : 40,
2893                                 renderer : function(v,x,r) { 
2894                                     var v = parseInt(v);
2895                                     //var aq = parseInt(r.data.avail_qty);
2896                                     //aq = isNaN(aq) ? 0 : aq;
2897                                    
2898                                     var rate = _this.form.findField('taxzone_rate').getValue();
2899                                     r.data.coitem_subtotal =  v * r.data.coitem_price;
2900                                     r.data.coitem_subtotal_tax = v * r.data.coitem_price * ( 1 + rate * 1);
2901                                     return String.format('{0}', v); 
2902                                     
2903                                 },
2904                                 editor : {
2905                                     xtype: 'GridEditor',
2906                                     xns: Roo.grid,
2907                                     field : {
2908                                         xtype: 'NumberField',
2909                                         xns: Roo.form,
2910                                         listeners : {
2911                                             focus : function (_self)
2912                                             {
2913                                                 if (this.value == 0) {
2914                                                     this.el.dom.value = '';
2915                                                 }
2916                                             }
2917                                         },
2918                                         allowDecimals : false,
2919                                         decimalPrecision : 0,
2920                                         minValue : 1,
2921                                         style : 'text-align:right'
2922                                     }
2923                                 }
2924                             },
2925                             {
2926                                 xtype: 'ColumnModel',
2927                                 xns: Roo.grid,
2928                                 align : 'right',
2929                                 dataIndex : 'coitem_price',
2930                                 header : 'Sell @',
2931                                 width : 70,
2932                                 renderer : function(v,x,r) { 
2933                                 
2934                                     var rate = _this.form.findField('taxzone_rate').getValue();
2935                                     r.data.coitem_subtotal = v * r.data.coitem_qtyord;
2936                                     r.data.coitem_subtotal_tax = v * ( 1 + rate * 1 ) * r.data.coitem_qtyord;
2937                                     r.data.coitem_price_tax = v * ( 1 + rate * 1 );
2938                                     if (parseInt(v) < 1) {
2939                                         return String.format('<b style="color:red;">{0}</b>', Roo.util.Format.number(v,3)); 
2940                                     }
2941                                     
2942                                     
2943                                     //r.set('coitem_subtotal', v * r.data.coitem_qtyord);
2944                                     
2945                                     return  String.format('{0}', Roo.util.Format.number(v,3)); 
2946                                 },
2947                                 editor : {
2948                                     xtype: 'GridEditor',
2949                                     xns: Roo.grid,
2950                                     field : {
2951                                         xtype: 'NumberField',
2952                                         xns: Roo.form,
2953                                         listeners : {
2954                                             focus : function (_self)
2955                                             {
2956                                                 if (this.value == 0.0) {
2957                                                     this.el.dom.value = '';
2958                                                 }
2959                                             }
2960                                         },
2961                                         decimalPrecision : 3,
2962                                         minValue : 0,
2963                                         style : 'text-align:right'
2964                                     }
2965                                 }
2966                             },
2967                             {
2968                                 xtype: 'ColumnModel',
2969                                 xns: Roo.grid,
2970                                 align : 'right',
2971                                 dataIndex : 'coitem_price_tax',
2972                                 header : 'Sell @w/GST',
2973                                 hidden : true,
2974                                 width : 70,
2975                                 renderer : function(v,x,r) { 
2976                                     
2977                                     if (parseInt(v) < 1) {
2978                                         return String.format('<b style="color:red;">{0}</b>', Roo.util.Format.number(v,3)); 
2979                                     }
2980                                     
2981                                     return  String.format('{0}', Roo.util.Format.number(v,3)); 
2982                                 },
2983                                 editor : {
2984                                     xtype: 'GridEditor',
2985                                     xns: Roo.grid,
2986                                     field : {
2987                                         xtype: 'NumberField',
2988                                         xns: Roo.form,
2989                                         listeners : {
2990                                             focus : function (_self)
2991                                             {
2992                                                 if (this.value == 0.0) {
2993                                                     this.el.dom.value = '';
2994                                                 }
2995                                             }
2996                                         },
2997                                         decimalPrecision : 3,
2998                                         minValue : 0,
2999                                         style : 'text-align:right'
3000                                     }
3001                                 }
3002                             },
3003                             {
3004                                 xtype: 'ColumnModel',
3005                                 xns: Roo.grid,
3006                                 align : 'right',
3007                                 dataIndex : 'coitem_linedisc',
3008                                 header : 'Disc%',
3009                                 width : 50,
3010                                 renderer : function(v,x,r) { 
3011                                 
3012                                     // coitem_custprice = coitem_price * ((100 - coitem_disc)/ 100) 
3013                                     
3014                                     //                      12 * (( 100 - 0) / 100)
3015                                     // coitem_custprice / coitem_price = ((100 - coitem_disc)/ 100) 
3016                                     // 100 - ((coitem_custprice / coitem_price) * 100)  = coitem_disc
3017                                     //                             100 -  97 =        100 -3 
3018                                 
3019                                    // r.data.coitem_linedisc = 100 - (
3020                                     //           (parseFloat(r.data.coitem_custprice) /
3021                                      //               parseFloat(r.data.coitem_price)
3022                                        //        ) * 100.00);
3023                                     var fl = parseFloat(r.data.coitem_linedisc);
3024                                     if ( isNaN(fl) || fl == 0.0 || r.data.coitem_price > r.data.coitem_custprice)  {
3025                                         return '';
3026                                     }
3027                                     return  String.format('<span style="color:green">{0}%</span>', Roo.util.Format.number( r.data.coitem_linedisc,2)); 
3028                                 },
3029                                 editor : {
3030                                     xtype: 'GridEditor',
3031                                     xns: Roo.grid,
3032                                     field : {
3033                                         xtype: 'NumberField',
3034                                         xns: Roo.form,
3035                                         listeners : {
3036                                             focus : function (_self)
3037                                             {
3038                                                 if (this.value == 0.0) {
3039                                                     this.el.dom.value = '';
3040                                                 }
3041                                             }
3042                                         },
3043                                         decimalPrecision : 2,
3044                                         maxValue : 100,
3045                                         minValue : 0,
3046                                         style : 'text-align:right'
3047                                     }
3048                                 }
3049                             },
3050                             {
3051                                 xtype: 'ColumnModel',
3052                                 xns: Roo.grid,
3053                                 align : 'right',
3054                                 dataIndex : 'coitem_custprice',
3055                                 header : 'List Price',
3056                                 width : 70,
3057                                 renderer : function(v,x,r) { 
3058                                     
3059                                     var rate = _this.form.findField('taxzone_rate').getValue();
3060                                     r.data.coitem_custprice_tax = v * ( 1 + rate * 1 );
3061                                     
3062                                     var tip = 'No WRP available';
3063                                     if ((r.data.coitem_wrpprice * 1) > 0) {
3064                                         tip = "WRP : " + Roo.util.Format.number(r.data.coitem_wrpprice,3);
3065                                     }
3066                                     // less than zero, show as red..
3067                                     if (parseFloat(v) < 1) {
3068                                         return String.format('<b qtip="{1}" style="color:red;">{0}</b>', 
3069                                             Roo.util.Format.number(v,3), tip); 
3070                                     }
3071                                     if (r.data.customer_price_each != v) {
3072                                           return String.format('<b qtip="{1}" style="color:pink;">{0}</b>', 
3073                                             Roo.util.Format.number(v,3), 
3074                                             "List Price = " +  Roo.util.Format.number(r.data.customer_price_each,3)
3075                                         ); 
3076                                     }
3077                                     
3078                                     
3079                                     
3080                                     //r.data.coitem_subtotal = v * r.data.coitem_qtyord;
3081                                     
3082                                     //r.set('coitem_subtotal', v * r.data.coitem_qtyord);
3083                                     
3084                                     return  String.format('<span qtip="{1}">{0}</span>', Roo.util.Format.number(v,3), tip); 
3085                                 },
3086                                 editor : {
3087                                     xtype: 'GridEditor',
3088                                     xns: Roo.grid,
3089                                     field : {
3090                                         xtype: 'NumberField',
3091                                         xns: Roo.form,
3092                                         listeners : {
3093                                             focus : function (_self)
3094                                             {
3095                                                 if (this.value == 0.0) {
3096                                                     this.el.dom.value = '';
3097                                                 }
3098                                             }
3099                                         },
3100                                         decimalPrecision : 3,
3101                                         minValue : 0,
3102                                         style : 'text-align:right'
3103                                     }
3104                                 }
3105                             },
3106                             {
3107                                 xtype: 'ColumnModel',
3108                                 xns: Roo.grid,
3109                                 align : 'right',
3110                                 dataIndex : 'coitem_custprice_tax',
3111                                 header : 'List Price w/GST',
3112                                 hidden : true,
3113                                 width : 70,
3114                                 renderer : function(v,x,r) { 
3115                                     var rate = _this.form.findField('taxzone_rate').getValue();
3116                                     var tip = 'No WRP available';
3117                                     if ((r.data.coitem_wrpprice * 1) > 0) {
3118                                         tip = "WRP : " + Roo.util.Format.number(r.data.coitem_wrpprice,3);
3119                                     }
3120                                     
3121                                     if (parseFloat(v) < 1) {
3122                                         return String.format('<b qtip="{1}" style="color:red;">{0}</b>', 
3123                                             Roo.util.Format.number(v,3), tip); 
3124                                     }
3125                                     if (r.data.customer_price_each != v) {
3126                                           return String.format('<b qtip="{1}" style="color:pink;">{0}</b>', 
3127                                             Roo.util.Format.number(v,3), 
3128                                             "List Price = " +  Roo.util.Format.number(r.data.customer_price_each * ( 1 + rate * 1 ),3)
3129                                         ); 
3130                                     }
3131                                     
3132                                     return  String.format('<span qtip="{1}">{0}</span>', Roo.util.Format.number(v,3), tip); 
3133                                 },
3134                                 editor : {
3135                                     xtype: 'GridEditor',
3136                                     xns: Roo.grid,
3137                                     field : {
3138                                         xtype: 'NumberField',
3139                                         xns: Roo.form,
3140                                         listeners : {
3141                                             focus : function (_self)
3142                                             {
3143                                                 if (this.value == 0.0) {
3144                                                     this.el.dom.value = '';
3145                                                 }
3146                                             }
3147                                         },
3148                                         decimalPrecision : 3,
3149                                         minValue : 0,
3150                                         style : 'text-align:right'
3151                                     }
3152                                 }
3153                             },
3154                             {
3155                                 xtype: 'ColumnModel',
3156                                 xns: Roo.grid,
3157                                 align : 'right',
3158                                 dataIndex : 'coitem_subtotal',
3159                                 header : 'SubTotal',
3160                                 width : 70,
3161                                 renderer : function(v) {
3162                                 
3163                                     if (parseInt(v) < 1) {
3164                                         return String.format('<b style="color:red;">{0}</b>', Roo.util.Format.number(v,2)); 
3165                                     }
3166                                 
3167                                     
3168                                 
3169                                  return Roo.util.Format.number( v, 2);
3170                                   }
3171                             },
3172                             {
3173                                 xtype: 'ColumnModel',
3174                                 xns: Roo.grid,
3175                                 align : 'right',
3176                                 dataIndex : 'coitem_subtotal_tax',
3177                                 header : 'SubTotal w/GST',
3178                                 hidden : true,
3179                                 width : 70,
3180                                 renderer : function(v,x,r) { 
3181                                    
3182                                     if (parseInt(v) < 1) {
3183                                         return String.format('<b style="color:red;">{0}</b>', Roo.util.Format.number(v,2)); 
3184                                     }
3185                                     
3186                                     return  String.format('{0}', Roo.util.Format.number(v,2)); 
3187                                 }
3188                             },
3189                             {
3190                                 xtype: 'ColumnModel',
3191                                 xns: Roo.grid,
3192                                 align : 'right',
3193                                 dataIndex : 'coitem_unitcost_in_order_cur',
3194                                 header : 'Unit Cost',
3195                                 width : 50,
3196                                 renderer : function(v) { return Roo.util.Format.number( v, 2); }
3197                             },
3198                             {
3199                                 xtype: 'ColumnModel',
3200                                 xns: Roo.grid,
3201                                 align : 'right',
3202                                 dataIndex : 'coitem_taxtype_id',
3203                                 header : 'Taxed',
3204                                 width : 50,
3205                                 renderer : function(v,x,r) { return String.format('{0}', r.data.coitem_taxtype_id_taxtype_name); },
3206                                 editor : {
3207                                     xtype: 'GridEditor',
3208                                     xns: Roo.grid,
3209                                     field : {
3210                                         xtype: 'ComboBox',
3211                                         xns: Roo.form,
3212                                         allowBlank : false,
3213                                         displayField : 'taxtype_name',
3214                                         editable : false,
3215                                         emptyText : "Select Tax Type",
3216                                         forceSelection : true,
3217                                         hiddenName : 'coitem_taxtype_id',
3218                                         listWidth : 400,
3219                                         loadingText : "Searching...",
3220                                         minChars : 2,
3221                                         name : 'coitem_taxtype_id_taxtype_name',
3222                                         pageSize : 20,
3223                                         qtip : "Select taxtype",
3224                                         queryParam : 'query[taxtype_id]',
3225                                         selectOnFocus : true,
3226                                         tpl : '<div class="x-grid-cell-text x-btn button"><b>{taxtype_name}</b> </div>',
3227                                         triggerAction : 'all',
3228                                         typeAhead : true,
3229                                         valueField : 'taxtype_id',
3230                                         width : 40,
3231                                         store : {
3232                                             xtype: 'Store',
3233                                             xns: Roo.data,
3234                                             listeners : {
3235                                                 beforeload : function (_self, o){
3236                                                     o.params = o.params || {};
3237                                                     // set more here
3238                                                     
3239                                                     
3240                                                 }
3241                                             },
3242                                             remoteSort : true,
3243                                             sortInfo : { direction : 'ASC', field: 'taxtype_name' },
3244                                             proxy : {
3245                                                 xtype: 'HttpProxy',
3246                                                 xns: Roo.data,
3247                                                 method : 'GET',
3248                                                 url : baseURL + '/Roo/taxtype.php'
3249                                             },
3250                                             reader : {
3251                                                 xtype: 'JsonReader',
3252                                                 xns: Roo.data,
3253                                                 id : 'taxtype_id',
3254                                                 root : 'data',
3255                                                 totalProperty : 'total',
3256                                                 fields : [{"name":"taxtype_id","type":"int"},"taxtype_name"]
3257                                             }
3258                                         }
3259                                     }
3260                                 }
3261                             },
3262                             {
3263                                 xtype: 'ColumnModel',
3264                                 xns: Roo.grid,
3265                                 align : 'right',
3266                                 dataIndex : 'avail_qty',
3267                                 header : '#avail',
3268                                 width : 50,
3269                                 renderer : function(v,x,r) { 
3270                                 
3271                                     var oq  = parseInt(r.data.coitem_qtyord);
3272                                     var aq = parseInt(r.data.avail_qty);
3273                                     var sq = parseInt(r.data.coitem_qtyshipped);
3274                                     aq = isNaN(aq) ? 0 : aq;
3275                                     oq = isNaN(oq) ? 0 : oq;
3276                                     sq = isNaN(sq) ? 0 : sq;
3277                                     
3278                                     var unshipped = oq - sq;
3279                                     
3280                                     if ( aq < 0 || (unshipped > 0  &&  aq < unshipped))  {
3281                                        return String.format('<b style="color:red;">{0}</b>', parseInt(aq));
3282                                     }
3283                                      
3284                                     return String.format('{0}', aq); 
3285                                     
3286                                 }
3287                             },
3288                             {
3289                                 xtype: 'ColumnModel',
3290                                 xns: Roo.grid,
3291                                 align : 'right',
3292                                 dataIndex : 'shipitem_shipped',
3293                                 header : '#reserved',
3294                                 width : 50,
3295                                 renderer : function(v,x,r) { 
3296                                 
3297                                     
3298                                     var vv = parseInt(v);
3299                                     vv = isNaN(vv) ? 0 : vv;
3300                                     
3301                                     var ov = parseInt(r.data.coitem_qtyord);
3302                                     ov = isNaN(ov) ? 0 : ov;
3303                                     
3304                                     var qs = parseInt(r.data.coitem_qtyshipped);
3305                                     qs = isNaN(qs) ? 0 : qs;
3306                                     
3307                                     
3308                                     
3309                                     if (vv < ov) {
3310                                         // not enough reserved yet.
3311                                         return String.format('<b style="background-color:red;color:yellow">{0}</b>', vv - qs);
3312                                     }
3313                                     
3314                                     return String.format('{0}', vv -  qs); 
3315                                     
3316                                 }
3317                             },
3318                             {
3319                                 xtype: 'ColumnModel',
3320                                 xns: Roo.grid,
3321                                 align : 'right',
3322                                 dataIndex : 'coitem_qtyshipped',
3323                                 header : '#shipped',
3324                                 width : 50,
3325                                 renderer : function(v,x,r) { 
3326                                     
3327                                     var vv = parseInt(v);
3328                                     vv = isNaN(vv) ? 0 : vv;
3329                                     
3330                                     var ov = parseInt(r.data.coitem_qtyord);
3331                                     ov = isNaN(ov) ? 0 : ov;
3332                                     
3333                                 
3334                                     if (vv != ov) {
3335                                         // not enought shipped.
3336                                         // or too many shipped.
3337                                         return String.format('<b style="background-color:red;color:yellow">{0}</b>',  vv);
3338                                     }
3339                                 
3340                                     
3341                                     
3342                                     return String.format('{0}', vv); 
3343                                     
3344                                 }
3345                             },
3346                             {
3347                                 xtype: 'ColumnModel',
3348                                 xns: Roo.grid,
3349                                 align : 'right',
3350                                 dataIndex : 'cobill_billed',
3351                                 header : '#invoiced',
3352                                 width : 50,
3353                                 renderer : function(v,x,r) { 
3354                                     
3355                                     var vv = parseInt(v);
3356                                     vv = isNaN(vv) ? 0 : vv;
3357                                     
3358                                     var ov = parseInt(r.data.coitem_qtyord);
3359                                     ov = isNaN(ov) ? 0 : ov;
3360                                     
3361                                 
3362                                     if (vv !=ov) {
3363                                             return String.format('<b style="background-color:red;color:yellow">{0}</b>', vv); 
3364                                     }
3365                                     return String.format('{0}', vv); 
3366                                     
3367                                 }
3368                             }
3369                         ]
3370                     }
3371                 },
3372                 {
3373                     xtype: 'NestedLayoutPanel',
3374                     xns: Roo,
3375                     listeners : {
3376                         activate : function (_self)
3377                         {
3378                             _this.shipinvtab = _self;
3379                         }
3380                     },
3381                     region : 'center',
3382                     title : "Shipments / Invoices",
3383                     layout : {
3384                         xtype: 'BorderLayout',
3385                         xns: Roo,
3386                         items : [
3387                             {
3388                                 xtype: 'GridPanel',
3389                                 xns: Roo,
3390                                 listeners : {
3391                                     activate : function() {
3392                                         _this.shippanel = this;
3393                                         if (_this.shipgrid) {
3394                                             _this.shipgrid.ds.load({});
3395                                         }
3396                                     }
3397                                 },
3398                                 background : false,
3399                                 fitContainer : true,
3400                                 fitToframe : true,
3401                                 region : 'north',
3402                                 tableName : 'shiphead',
3403                                 title : "shiphead",
3404                                 grid : {
3405                                     xtype: 'Grid',
3406                                     xns: Roo.grid,
3407                                     listeners : {
3408                                         render : function() 
3409                                         {
3410                                             _this.shipgrid = this; 
3411                                             //_this.dialog = Pman.Dialog.FILL_IN
3412                                             if (_this.shippanel.active) {
3413                                                this.ds.load({});
3414                                             }
3415                                         },
3416                                         rowdblclick : function (_self, rowIndex, e)
3417                                         {
3418                                             var rec = this.ds.getAt(rowIndex);
3419                                             if (!rec.json.shiphead_shipdate.length) {
3420                                                  Roo.MessageBox.alert("Error", "You can not edit voided shipments, create a new one, and use the restore feature");
3421                                                  return;
3422                                              }
3423                                         
3424                                            Pman.Dialog.XtupleShipment.show({
3425                                                 shiphead_id : rec.data.shiphead_id
3426                                         
3427                                             }, function() {
3428                                                 _self.ds.load({});
3429                                             
3430                                             });
3431                                            
3432                                         }
3433                                     },
3434                                     autoExpandColumn : 'shiphead_shipvia',
3435                                     loadMask : true,
3436                                     sm : {
3437                                         xtype: 'RowSelectionModel',
3438                                         xns: Roo.grid,
3439                                         singleSelect : true
3440                                     },
3441                                     dataSource : {
3442                                         xtype: 'Store',
3443                                         xns: Roo.data,
3444                                         listeners : {
3445                                             beforeload : function (_self, options)
3446                                             {
3447                                                  options.params = options.params || {};
3448                                                  options.params.shiphead_order_id = _this.form.findField('cohead_id').getValue() * 1;
3449                                                 if (options.params.shiphead_order_id < 1) {
3450                                                     return false;
3451                                                 }
3452                                             }
3453                                         },
3454                                         remoteSort : true,
3455                                         sortInfo : { field : 'shiphead_shipvia', direction: 'ASC' },
3456                                         proxy : {
3457                                             xtype: 'HttpProxy',
3458                                             xns: Roo.data,
3459                                             method : 'GET',
3460                                             url : baseURL + '/Roo/shiphead.php'
3461                                         },
3462                                         reader : {
3463                                             xtype: 'JsonReader',
3464                                             xns: Roo.data,
3465                                             totalProperty : 'total',
3466                                             root : 'data',
3467                                             id : 'id',
3468                                             fields : [
3469                                                 {
3470                                                     'name': 'shiphead_number',
3471                                                     'type': 'string'
3472                                                 },
3473                                                 {
3474                                                     'name': 'shiphead_shipvia',
3475                                                     'type': 'string'
3476                                                 },
3477                                                 {
3478                                                     'name': 'shiphead_shipdate',
3479                                                     'type': 'date'
3480                                                 },
3481                                                 {
3482                                                     'name': 'shiphead_sfstatus'
3483                                                 },
3484                                                 {
3485                                                     'name': 'shiphead_tracknum',
3486                                                     'type': 'string'
3487                                                 }
3488                                             ]
3489                                         }
3490                                     },
3491                                     toolbar : {
3492                                         xtype: 'Toolbar',
3493                                         xns: Roo,
3494                                         items : [
3495                                             {
3496                                                 xtype: 'Button',
3497                                                 xns: Roo.Toolbar,
3498                                                 listeners : {
3499                                                     click : function()
3500                                                     {
3501                                                          var sel  = _this.shipgrid.getSelectionModel().getSelected();
3502                                                         if (!sel) {
3503                                                             Roo.MessageBox.alert("Error", "Select a shipment");
3504                                                             return;
3505                                                         }
3506                                                         if (sel.data.shiphead_shipped) {
3507                                                             Roo.MessageBox.alert("Error", "Shipment is already confirmed");
3508                                                             return;
3509                                                         }
3510                                                         
3511                                                         
3512                                                         // check current status of shipment..
3513                                                         
3514                                                             
3515                                                         new Pman.Request({ 
3516                                                                mask : 'Sending',
3517                                                             url : baseURL + '/Roo/shiphead',
3518                                                             method : 'POST',
3519                                                             timeout : 90000,
3520                                                             params : {
3521                                                                 shiphead_id : sel.data.shiphead_id,
3522                                                                 _confirm : 1
3523                                                             },
3524                                                             success : function() {
3525                                                                 _this.shipgrid.ds.load({});
3526                                                             }
3527                                                         });
3528                                                              
3529                                                          
3530                                                     }
3531                                                 },
3532                                                 cls : 'x-btn-text-icon',
3533                                                 text : "Confirm Shipment",
3534                                                 icon : rootURL + '/Pman/templates/images/lock.gif'
3535                                             },
3536                                             {
3537                                                 xtype: 'Button',
3538                                                 xns: Roo.Toolbar,
3539                                                 listeners : {
3540                                                     click : function()
3541                                                     {
3542                                                          var sel  = _this.shipgrid.getSelectionModel().getSelected();
3543                                                         if (!sel) {
3544                                                             Roo.MessageBox.alert("Error", "Select a shipment");
3545                                                             return;
3546                                                         }
3547                                                     
3548                                                         
3549                                                         // check current status of shipment..
3550                                                         
3551                                                             
3552                                                        new Pman.Download({
3553                                                             url : baseURL + '/Roo/shiphead',
3554                                                             method : 'GET',
3555                                                             params : {
3556                                                                _download :sel.data.shiphead_id
3557                                                                  
3558                                                             }
3559                                                         });
3560                                                              
3561                                                          
3562                                                     }
3563                                                 },
3564                                                 cls : 'x-btn-text-icon',
3565                                                 text : "Download (as xls)",
3566                                                 icon : rootURL + '/Pman/templates/images/save.gif'
3567                                             },
3568                                             {
3569                                                 xtype: 'Button',
3570                                                 xns: Roo.Toolbar,
3571                                                 listeners : {
3572                                                     click : function ()
3573                                                     {
3574                                                         var sel  = _this.shipgrid.getSelectionModel().getSelected();\r
3575                                                         if (!sel) {\r
3576                                                             Roo.MessageBox.alert("Error", "Select a shipment");\r
3577                                                             return;\r
3578                                                         }\r
3579                                                         // check current status of shipment..
3580                                                      
3581                                                             new Pman.Download({
3582                                                                 url : baseURL + '/Xtuple/Print',
3583                                                                 method : 'GET',
3584                                                                 params : {
3585                                                                     template : 'picking-slip',
3586                                                                     param : "shiphead_id:integer='" + sel.data.shiphead_id + "'",
3587                                                                     filename : 'picking-slip-' + sel.data.shiphead_number
3588                                                                 },
3589                                                                 success : function() {
3590                                                     
3591                                                                 }
3592                                                             })
3593                                                                 
3594                                                                 
3595                                                        
3596                                                     }
3597                                                 },
3598                                                 cls : 'x-btn-text-icon',
3599                                                 text : "Print Picking Slip",
3600                                                 icon : rootURL + '/Pman/templates/images/pdf.gif'
3601                                             },
3602                                             {
3603                                                 xtype: 'Button',
3604                                                 xns: Roo.Toolbar,
3605                                                 listeners : {
3606                                                     click : function ()
3607                                                     {
3608                                                         var sel  = _this.shipgrid.getSelectionModel().getSelected();\r
3609                                                         if (!sel) {\r
3610                                                             Roo.MessageBox.alert("Error", "Select a shipment");\r
3611                                                             return;\r
3612                                                         }\r
3613                                                         // check current status of shipment..
3614                                                     
3615                                                             new Pman.Download({
3616                                                                 url : baseURL + '/Xtuple/Print',
3617                                                                 method : 'GET',
3618                                                                 params : {
3619                                                                     template : 'delivery-note-',
3620                                                                     param : "shiphead_id:integer='" + sel.data.shiphead_id + "'",
3621                                                                     filename : 'delivery-note-' + sel.data.shiphead_number
3622                                                                 },
3623                                                                 success : function() {
3624                                                     
3625                                                                 }
3626                                                             })
3627                                                                 
3628                                                                 
3629                                                        
3630                                                     }
3631                                                 },
3632                                                 cls : 'x-btn-text-icon',
3633                                                 text : "Print Delivery Note",
3634                                                 icon : rootURL + '/Pman/templates/images/pdf.gif'
3635                                             },
3636                                             {
3637                                                 xtype: 'Button',
3638                                                 xns: Roo.Toolbar,
3639                                                 listeners : {
3640                                                     click : function()
3641                                                     {
3642                                                         var s = _this.shipgrid.getSelectionModel().getSelected();
3643                                                         
3644                                                         if(!s || s.data.shiphead_id * 1 < 1){
3645                                                             Roo.MessageBox.alert('Error', 'Please select a shipment');
3646                                                             return;
3647                                                         }
3648                                                         
3649                                                         new Pman.Request({
3650                                                             method: 'POST',
3651                                                             url: baseURL+ '/Roo/shiphead',
3652                                                             params : {
3653                                                                 shiphead_id : s.data.shiphead_id,
3654                                                                 _send_delivery : 1
3655                                                             },
3656                                                             success : function() {
3657                                                                 Roo.MessageBox.alert('Notice', 'SENT');
3658                                                             }
3659                                                         });
3660                                                         
3661                                                     }
3662                                                 },
3663                                                 cls : 'x-btn-text-icon',
3664                                                 text : "Send Delivery Email",
3665                                                 icon : Roo.rootURL + 'images/default/dd/email.gif'
3666                                             },
3667                                             {
3668                                                 xtype: 'Fill',
3669                                                 xns: Roo.Toolbar
3670                                             },
3671                                             {
3672                                                 xtype: 'Button',
3673                                                 xns: Roo.Toolbar,
3674                                                 listeners : {
3675                                                     click : function()
3676                                                     {   
3677                                                         if (!_this.form.findField('cohead_id').getValue()) {
3678                                                             Roo.MessageBox.alert("Error", "Save Order first");
3679                                                             return;
3680                                                         }
3681                                                         var rv = _this.form.getFieldValues();
3682                                                         
3683                                                         Pman.Dialog.XtupleShipmentNew.show({
3684                                                                 shiphead_order_id : rv.cohead_id,
3685                                                                 shiphead_shipdate :  _this.form.findField('cohead_targetdate').getValue().format('Y-m-d')
3686                                                             },
3687                                                             function() { 
3688                                                                  _this.shipgrid.ds.load({});
3689                                                             }
3690                                                         );
3691                                                     },
3692                                                     render : function (_self)
3693                                                     {
3694                                                       _this.addShipmentBtn = _self;
3695                                                     }
3696                                                 },
3697                                                 cls : 'x-btn-text-icon',
3698                                                 text : "Add",
3699                                                 icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
3700                                             },
3701                                             {
3702                                                 xtype: 'Button',
3703                                                 xns: Roo.Toolbar,
3704                                                 listeners : {
3705                                                     click : function()
3706                                                     {
3707                                                         var sel  = _this.shipgrid.getSelectionModel().getSelected();
3708                                                         if (!sel) {
3709                                                             Roo.MessageBox.alert("Error", "Select a shipment");
3710                                                             return;
3711                                                         }
3712                                                         // check current status of shipment..
3713                                                         
3714                                                         var msg = sel.data.shiphead_shipped ? 
3715                                                             "Are you sure you want to un-confirm that shipment? - It will remove items from unposted invoices" : 
3716                                                             "Are you sure you want to void that shipment?";
3717                                                         
3718                                                         Roo.MessageBox.confirm("Are you sure", msg,
3719                                                             function(r) {
3720                                                                 if (r != 'yes') {
3721                                                                     return;
3722                                                                 }
3723                                                                 new Pman.Request({
3724                                                                     mask : 'Sending',
3725                                                                      timeout : 90000,
3726                                                                     url : baseURL + '/Roo/shiphead',
3727                                                                     method : 'POST',
3728                                                                     
3729                                                                     params : {
3730                                                                         shiphead_id : sel.data.shiphead_id,
3731                                                                         _void : 1
3732                                                                     },
3733                                                                     success : function() {
3734                                                                         _this.shipgrid.ds.load({});
3735                                                                     }
3736                                                                 })
3737                                                                 
3738                                                             }
3739                                                         );
3740                                                                 
3741                                                                 
3742                                                         
3743                                                         
3744                                                         
3745                                                     }
3746                                                 },
3747                                                 cls : 'x-btn-text-icon',
3748                                                 text : "Void / Unconfirm",
3749                                                 icon : rootURL + '/Pman/templates/images/trash.gif'
3750                                             }
3751                                         ]
3752                                     },
3753                                     colModel : [
3754                                         {
3755                                             xtype: 'ColumnModel',
3756                                             xns: Roo.grid,
3757                                             dataIndex : 'shiphead_number',
3758                                             header : 'number',
3759                                             width : 80,
3760                                             renderer : function(v,x,r) {
3761                                                 if (r.json.shiphead_shipdate.length) {
3762                                                      return String.format('{0}', v); 
3763                                                  }
3764                                                  return String.format('<s>{0}</s>', v); 
3765                                              }
3766                                         },
3767                                         {
3768                                             xtype: 'ColumnModel',
3769                                             xns: Roo.grid,
3770                                             dataIndex : 'shiphead_location_id_location_name',
3771                                             header : 'From Location',
3772                                             width : 100,
3773                                             renderer : function(v) { return String.format('{0}', v); }
3774                                         },
3775                                         {
3776                                             xtype: 'ColumnModel',
3777                                             xns: Roo.grid,
3778                                             dataIndex : 'shiphead_shipto_id_shipto_name',
3779                                             header : 'Ship to',
3780                                             width : 100,
3781                                             renderer : function(v) { return String.format('{0}', v); }
3782                                         },
3783                                         {
3784                                             xtype: 'ColumnModel',
3785                                             xns: Roo.grid,
3786                                             header : 'shipdate',
3787                                             width : 75,
3788                                             dataIndex : 'shiphead_shipdate',
3789                                             renderer : function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }
3790                                         },
3791                                         {
3792                                             xtype: 'ColumnModel',
3793                                             xns: Roo.grid,
3794                                             dataIndex : 'shiphead_sfstatus',
3795                                             header : 'Status',
3796                                             width : 50,
3797                                             renderer : function(v,x,r) { 
3798                                             
3799                                             
3800                                             
3801                                               
3802                                                 if (r.json.shiphead_shipdate.length) {
3803                                                 
3804                                                     if (r.json.shiphead_shipped) {
3805                                                         return "Confirmed";
3806                                                     }
3807                                                 
3808                                                      return '<span style="color:red">Draft</span>';
3809                                                 }
3810                                                  
3811                                                 return 'VOID';
3812                                                
3813                                             
3814                                             }
3815                                         },
3816                                         {
3817                                             xtype: 'ColumnModel',
3818                                             xns: Roo.grid,
3819                                             header : 'shipvia',
3820                                             width : 200,
3821                                             dataIndex : 'shiphead_shipvia',
3822                                             renderer : function(v) { return String.format('{0}', v); }
3823                                         },
3824                                         {
3825                                             xtype: 'ColumnModel',
3826                                             xns: Roo.grid,
3827                                             header : 'tracknum',
3828                                             width : 200,
3829                                             dataIndex : 'shiphead_tracknum',
3830                                             renderer : function(v) { return String.format('{0}', v); }
3831                                         }
3832                                     ]
3833                                 }
3834                             },
3835                             {
3836                                 xtype: 'GridPanel',
3837                                 xns: Roo,
3838                                 listeners : {
3839                                     activate : function() {
3840                                         _this.invpanel = this;
3841                                         if (_this.invgrid) {
3842                                             _this.invgrid.ds.load({});
3843                                         }
3844                                     }
3845                                 },
3846                                 background : false,
3847                                 fitContainer : true,
3848                                 fitToframe : true,
3849                                 region : 'center',
3850                                 tableName : 'cobmisc',
3851                                 title : "Invoices",
3852                                 grid : {
3853                                     xtype: 'Grid',
3854                                     xns: Roo.grid,
3855                                     listeners : {
3856                                         render : function() 
3857                                         {
3858                                             _this.invgrid = this; 
3859                                             //_this.dialog = Pman.Dialog.FILL_IN
3860                                             if (_this.invpanel.active) {
3861                                                this.ds.load({});
3862                                             }
3863                                         },
3864                                         rowdblclick : function (_self, rowIndex, e)
3865                                         {
3866                                         
3867                                             var ri = this.ds.getAt(rowIndex);
3868                                             if ( ri.data.cobmisc_id < 0) { // skip summary row..
3869                                                 return;
3870                                             }
3871                                             var rv = _this.form.getFieldValues();
3872                                            Pman.Dialog.XtupleInvoice.show({
3873                                                 cobmisc_id : ri.data.cobmisc_id,
3874                                                 // below parms for add credit memo
3875                                                 cmdata : {
3876                                                     cm_cust_id : rv.cohead_cust_id,
3877                                                     cm_cust_id_cust_name : rv.cohead_cust_id_cust_name,
3878                                                     cm_curr_id : rv.cohead_curr_id,
3879                                                     cm_curr_id_curr_name : rv.cohead_curr_id_curr_name,
3880                                                     cm_terms_id : rv.cohead_terms_id,
3881                                                     cm_terms_id_terms_descrip : rv.cohead_terms_id_terms_descrip,
3882                                                     cm_salesrep_id : rv.cohead_salesrep_id,
3883                                                     cm_salesrep_id_salesrep_name : rv.cohead_salesrep_id_salesrep_name,
3884                                                     cm_docdate : new Date(),
3885                                                     cm_taxzone_id : rv.cohead_taxzone_id,
3886                                                     cm_taxzone_id_taxzone_descrip : rv.cohead_taxzone_id_taxzone_descrip,
3887                                                     cm_billto_cntct_id : rv.cohead_billto_cntct_id,
3888                                                     cm_billto_cntct_id_cntct_name : rv.cohead_billto_cntct_id_cntct_name,
3889                                                     cm_location_src : rv.cohead_location_src,
3890                                                     cm_location_src_location_name : rv.cohead_location_src_location_name,
3891                                                     cm_billto_address : rv.billto_address
3892                                                 }
3893                                             },
3894                                                 function() { 
3895                                                  _this.invgrid.ds.load({});
3896                                             });
3897                                            
3898                                         }
3899                                     },
3900                                     autoExpandColumn : 'invchead_invcnumber',
3901                                     loadMask : true,
3902                                     dataSource : {
3903                                         xtype: 'Store',
3904                                         xns: Roo.data,
3905                                         listeners : {
3906                                             beforeload : function (_self, options)
3907                                             {
3908                                             
3909                                                 options.params = options.params || {};
3910                                                 options.params.cobmisc_cohead_id = _this.form.findField('cohead_id').getValue() * 1;
3911                                                 if (options.params.cobmisc_cohead_id < 1) {
3912                                                     return false;
3913                                                 }
3914                                                 
3915                                                 options.params._with_other_payment = 1;
3916                                                 //options.params['query[invchead_ordernumber]']  = _this.form.findField('cohead_number').getValue()
3917                                                 
3918                                             },
3919                                             load : function (_self, records, options)
3920                                             {
3921                                                 var total = 0;
3922                                                 var done = 0;
3923                                                 var totalic = 0.0;
3924                                                 var totalfreight = 0.0;    
3925                                                 var totalmisc = 0.0;        
3926                                                 var totaltax = 0.0;        
3927                                                
3928                                                 Roo.each(records, function(r)
3929                                                 {
3930                                                     if(r.data.cobmisc_id > 1){
3931                                                         done += parseInt(r.data.cobmisc_qty);
3932                                                         totalic += parseFloat(r.data.cobmisc_itemcost).toFixed(2)*1;        
3933                                                         totalmisc += parseFloat(r.data.cobmisc_misc).toFixed(2)*1;        
3934                                                         totalfreight += parseFloat(r.data.cobmisc_freight).toFixed(2)*1;                
3935                                                         totaltax += parseFloat(r.data.cobmisc_tax).toFixed(2)*1;                
3936                                                         total = parseInt(r.data.cobmisc_total_qty);
3937                                                     }
3938                                                    
3939                                                 });
3940                                                 
3941                                                 _this.shipinvtab.layout.getRegion('center').getPanel(0).setTitle(
3942                                                     (total == done) ?
3943                                                          "Invoices (Complete)" : 
3944                                                         ("Invoices prepared for " +    done + '/' + total)
3945                                                 );
3946                                             
3947                                                 
3948                                                 var frtotal = (parseFloat(_this.form.findField('cohead_freight').getValue())  - totalfreight).toFixed(2);
3949                                                 var misctotal = (parseFloat(_this.form.findField('cohead_misc').getValue()) - totalmisc).toFixed(2);
3950                                                 var ictotal =  (parseFloat(_this.form.findField('cohead_subtotal').getValue()) - totalic).toFixed(2);
3951                                                 var taxtotal =  (parseFloat(_this.form.findField('cohead_tax').getValue())  - totaltax).toFixed(2);
3952                                                 var remtotal = frtotal*1 + misctotal*1 + ictotal*1 + taxtotal*1;
3953                                                 
3954                                                 var nr = this.reader.newRow({
3955                                                     cobmisc_id : -1,
3956                                                     cobmisc_invchead_id_invchead_invcnumber : "Total Remaining",
3957                                                     cobmisc_qty : total - done,
3958                                                     cobmisc_freight :frtotal,
3959                                                     cobmisc_misc : misctotal,        
3960                                                     cobmisc_itemcost  : ictotal,             
3961                                                     cobmisc_tax : taxtotal,
3962                                                     cobmisc_total : remtotal
3963                                             
3964                                                     
3965                                                 });
3966                                                 // do we need to add it somehow??
3967                                                 _this.invgrid.ds.add(nr);
3968                                             }
3969                                         },
3970                                         remoteSort : true,
3971                                         sortInfo : { field : 'invchead_invcnumber', direction: 'ASC' },
3972                                         proxy : {
3973                                             xtype: 'HttpProxy',
3974                                             xns: Roo.data,
3975                                             method : 'GET',
3976                                             url : baseURL + '/Roo/cobmisc.php'
3977                                         },
3978                                         reader : {
3979                                             xtype: 'JsonReader',
3980                                             xns: Roo.data,
3981                                             id : 'id',
3982                                             root : 'data',
3983                                             totalProperty : 'total',
3984                                             fields : [
3985                                                 {
3986                                                     'name': 'invchead_invcnumber',
3987                                                     'type': 'string'
3988                                                 },
3989                                                 {
3990                                                     'name': 'invchead_invcdate',
3991                                                     'type': 'date'
3992                                                 }
3993                                             ]
3994                                         }
3995                                     },
3996                                     toolbar : {
3997                                         xtype: 'Toolbar',
3998                                         xns: Roo,
3999                                         items : [
4000                                             {
4001                                                 xtype: 'Button',
4002                                                 xns: Roo.Toolbar,
4003                                                 listeners : {
4004                                                     click : function ()
4005                                                     {
4006                                                     
4007                                                          var sel  = _this.invgrid.getSelectionModel().getSelected();
4008                                                         if (!sel) {
4009                                                             Roo.MessageBox.alert("Error", "Select a invoice");
4010                                                             return;
4011                                                         }
4012                                                         // check current status of shipment..
4013                                                         
4014                                                         Roo.MessageBox.confirm("Are you sure", "Are you sure you want to Post that invoice?",
4015                                                             function(r) {
4016                                                                 if (r != 'yes') {
4017                                                                     return;
4018                                                                 }
4019                                                                 new Pman.Request({
4020                                                                    mask : 'Sending',
4021                                                                     url : baseURL + '/Roo/cobmisc',
4022                                                                     method : 'POST',
4023                                                                     params : {
4024                                                                         cobmisc_id : sel.data.cobmisc_id,
4025                                                                         _post : 1
4026                                                                     },
4027                                                                     success : function() {
4028                                                                         _this.invgrid.ds.load({});
4029                                                                     }
4030                                                                 })
4031                                                                 
4032                                                             }
4033                                                         );
4034                                                                 
4035                                                                 
4036                                                        
4037                                                     }
4038                                                 },
4039                                                 cls : 'x-btn-text-icon',
4040                                                 text : "Post Invoice",
4041                                                 icon : rootURL + '/Pman/templates/images/lock.gif'
4042                                             },
4043                                             {
4044                                                 xtype: 'Button',
4045                                                 xns: Roo.Toolbar,
4046                                                 cls : 'x-btn-text-icon',
4047                                                 text : "Print",
4048                                                 icon : rootURL + '/Pman/templates/images/pdf.gif',
4049                                                 menu : {
4050                                                     xtype: 'Menu',
4051                                                     xns: Roo.menu,
4052                                                     items : [
4053                                                         {
4054                                                             xtype: 'Item',
4055                                                             xns: Roo.menu,
4056                                                             listeners : {
4057                                                                 click : function ()
4058                                                                 {
4059                                                                 
4060                                                                      var sel  = _this.invgrid.getSelectionModel().getSelected();
4061                                                                     if (!sel) {
4062                                                                         Roo.MessageBox.alert("Error", "Select a invoice");
4063                                                                         return;
4064                                                                     }
4065                                                                     if (!sel.data.cobmisc_invchead_id) {
4066                                                                         Roo.MessageBox.alert("Error", "Invoice has not been posted");
4067                                                                         return;
4068                                                                     }
4069                                                                     // check current status of shipment..
4070                                                                 
4071                                                                         new Pman.Download({
4072                                                                             url : baseURL + '/Roo/invchead',
4073                                                                             method : 'GET',
4074                                                                             params : {
4075                                                                                 invchead_id : sel.data.cobmisc_invchead_id,
4076                                                                                 _print : 1
4077                                                                             },
4078                                                                             success : function() {
4079                                                                 
4080                                                                             }
4081                                                                         })
4082                                                                             
4083                                                                             
4084                                                                    
4085                                                                 }
4086                                                             },
4087                                                             text : "Print Standard Invoice"
4088                                                         },
4089                                                         {
4090                                                             xtype: 'Item',
4091                                                             xns: Roo.menu,
4092                                                             listeners : {
4093                                                                 click : function (_self, e)
4094                                                                 {
4095                                                                 
4096                                                                      var sel  = _this.invgrid.getSelectionModel().getSelected();
4097                                                                     if (!sel) {
4098                                                                         Roo.MessageBox.alert("Error", "Select a invoice");
4099                                                                         return;
4100                                                                     }
4101                                                                     if (!sel.data.cobmisc_invchead_id) {
4102                                                                         Roo.MessageBox.alert("Error", "Invoice has not been posted");
4103                                                                         return;
4104                                                                     }
4105                                                                     // check current status of shipment..
4106                                                                 
4107                                                                         new Pman.Download({
4108                                                                             url : baseURL + '/Roo/invchead',
4109                                                                             method : 'GET',
4110                                                                             params : {
4111                                                                                 invchead_id : sel.data.cobmisc_invchead_id,
4112                                                                                 _print : 'cn'
4113                                                                                 
4114                                                                             },
4115                                                                             success : function() {
4116                                                                 
4117                                                                             }
4118                                                                         })
4119                                                                             
4120                                                                           
4121                                                                 }
4122                                                             },
4123                                                             text : "Print Chinese Invoice With GST in line item",
4124                                                             hidden : !(baseURL.match(/(hk\.php|cn\.php)$/))
4125                                                         },
4126                                                         {
4127                                                             xtype: 'Item',
4128                                                             xns: Roo.menu,
4129                                                             listeners : {
4130                                                                 click : function (_self, e)
4131                                                                 {
4132                                                                 
4133                                                                      var sel  = _this.invgrid.getSelectionModel().getSelected();
4134                                                                     if (!sel) {
4135                                                                         Roo.MessageBox.alert("Error", "Select a invoice");
4136                                                                         return;
4137                                                                     }
4138                                                                     if (!sel.data.cobmisc_invchead_id) {
4139                                                                         Roo.MessageBox.alert("Error", "Invoice has not been posted");
4140                                                                         return;
4141                                                                     }
4142                                                                     // check current status of shipment..
4143                                                                 
4144                                                                         new Pman.Download({
4145                                                                             url : baseURL + '/Roo/invchead',
4146                                                                             method : 'GET',
4147                                                                             params : {
4148                                                                                 invchead_id : sel.data.cobmisc_invchead_id,
4149                                                                                 _print : 'cn-gst'
4150                                                                                 
4151                                                                             },
4152                                                                             success : function() {
4153                                                                 
4154                                                                             }
4155                                                                         })
4156                                                                             
4157                                                                           
4158                                                                 }
4159                                                             },
4160                                                             text : "Print Chinese Invoice",
4161                                                             hidden : !(baseURL.match(/(hk\.php|cn\.php)$/))
4162                                                         },
4163                                                         {
4164                                                             xtype: 'Item',
4165                                                             xns: Roo.menu,
4166                                                             listeners : {
4167                                                                 click : function (_self, e)
4168                                                                 {
4169                                                                 
4170                                                                      var sel  = _this.invgrid.getSelectionModel().getSelected();
4171                                                                     if (!sel) {
4172                                                                         Roo.MessageBox.alert("Error", "Select a invoice");
4173                                                                         return;
4174                                                                     }
4175                                                                     if (!sel.data.cobmisc_invchead_id) {
4176                                                                         Roo.MessageBox.alert("Error", "Invoice has not been posted");
4177                                                                         return;
4178                                                                     }
4179                                                                     // check current status of shipment..
4180                                                                 
4181                                                                         new Pman.Download({
4182                                                                             url : baseURL + '/Roo/invchead',
4183                                                                             method : 'GET',
4184                                                                             params : {
4185                                                                                 invchead_id : sel.data.cobmisc_invchead_id,
4186                                                                                 _print : 'au-gst'
4187                                                                                 
4188                                                                             },
4189                                                                             success : function() {
4190                                                                 
4191                                                                             }
4192                                                                         })
4193                                                                             
4194                                                                           
4195                                                                 }
4196                                                             },
4197                                                             text : "Print Aus - GST included Invoice",
4198                                                             hidden : !(baseURL.match(/au\.php$/))
4199                                                         },
4200                                                         {
4201                                                             xtype: 'Item',
4202                                                             xns: Roo.menu,
4203                                                             listeners : {
4204                                                                 click : function (_self, e)
4205                                                                 {
4206                                                                   var sel  = _this.invgrid.getSelectionModel().getSelected();
4207                                                                     if (!sel) {
4208                                                                         Roo.MessageBox.alert("Error", "Select a invoice");
4209                                                                         return;
4210                                                                     }
4211                                                                     if (!sel.data.cobmisc_invchead_id) {
4212                                                                         Roo.MessageBox.alert("Error", "Invoice has not been posted");
4213                                                                         return;
4214                                                                     }
4215                                                                     // check current status of shipment..
4216                                                                 
4217                                                                         new Pman.Download({
4218                                                                             url : baseURL + '/Roo/invchead',
4219                                                                             method : 'GET',
4220                                                                             params : {
4221                                                                                 invchead_id : sel.data.cobmisc_invchead_id,
4222                                                                 
4223                                                                                 _print : 'au-net',
4224                                                                                ts: Math.random()
4225                                                                             }
4226                                                                         })
4227                                                                 
4228                                                                 
4229                                                                 }
4230                                                             },
4231                                                             text : "Print Bambini Pronto Invoice",
4232                                                             hidden : !(baseURL.match(/au\.php$/))
4233                                                         },
4234                                                         {
4235                                                             xtype: 'Item',
4236                                                             xns: Roo.menu,
4237                                                             listeners : {
4238                                                                 click : function (_self, e)
4239                                                                 {
4240                                                                 
4241                                                                      var sel  = _this.invgrid.getSelectionModel().getSelected();
4242                                                                     if (!sel) {
4243                                                                         Roo.MessageBox.alert("Error", "Select a invoice");
4244                                                                         return;
4245                                                                     }
4246                                                                     if (!sel.data.cobmisc_invchead_id) {
4247                                                                         Roo.MessageBox.alert("Error", "Invoice has not been posted");
4248                                                                         return;
4249                                                                     }
4250                                                                     // check current status of shipment..
4251                                                                 
4252                                                                         new Pman.Download({
4253                                                                             url : baseURL + '/Roo/invchead',
4254                                                                             method : 'GET',
4255                                                                             params : {
4256                                                                                 invchead_id : sel.data.cobmisc_invchead_id,
4257                                                                                 _print : 'au-proforma'
4258                                                                                 
4259                                                                             },
4260                                                                             success : function() {
4261                                                                 
4262                                                                             }
4263                                                                         })
4264                                                                             
4265                                                                           
4266                                                                 }
4267                                                             },
4268                                                             text : "Print Bambini Pro Forma Invoice",
4269                                                             hidden : !(baseURL.match(/au\.php$/))
4270                                                         },
4271                                                         {
4272                                                             xtype: 'Item',
4273                                                             xns: Roo.menu,
4274                                                             listeners : {
4275                                                                 click : function (_self, e)
4276                                                                 {
4277                                                                     var sel  = _this.invgrid.getSelectionModel().getSelected();
4278                                                                     if (!sel) {
4279                                                                         Roo.MessageBox.alert("Error", "Select a invoice");
4280                                                                         return;
4281                                                                     }
4282                                                                     
4283                                                                     Pman.Dialog.XtupleDiscountOfInvoice.show({invchead_id : sel.data.cobmisc_invchead_id}, function(){
4284                                                                 
4285                                                                     });
4286                                                                 }
4287                                                             },
4288                                                             text : "Print Shipping / Commercial Invoice",
4289                                                             hidden : !( baseURL.match(/au\.php$/))
4290                                                         }
4291                                                     ]
4292                                                 }
4293                                             },
4294                                             {
4295                                                 xtype: 'Button',
4296                                                 xns: Roo.Toolbar,
4297                                                 cls : 'x-btn-text-icon',
4298                                                 text : "Payments / Refunds / Credit Memos",
4299                                                 icon : Roo.rootURL + 'images/default/tree/leaf.gif',
4300                                                 menu : {
4301                                                     xtype: 'Menu',
4302                                                     xns: Roo.menu,
4303                                                     items : [
4304                                                         {
4305                                                             xtype: 'Item',
4306                                                             xns: Roo.menu,
4307                                                             listeners : {
4308                                                                 click : function(_self,e)
4309                                                                     {
4310                                                                     
4311                                                                     var sel  = _this.invgrid.getSelectionModel().getSelected();
4312                                                                     if (!sel || sel.data.cobmisc_id < 1) {
4313                                                                         Roo.MessageBox.alert("Error", "Select a invoice");
4314                                                                         return;
4315                                                                     }
4316                                                                     if(!sel.data.cobmisc_posted){
4317                                                                         Roo.MessageBox.alert("Error", "This invoice has not been posted!");
4318                                                                         return;
4319                                                                     }
4320                                                                     if(sel.data.cobmisc_outstanding == 0){
4321                                                                         Roo.MessageBox.alert("Error", "There is no any outstanding of this invoice!");
4322                                                                         return;
4323                                                                     }
4324                                                                     
4325                                                                     var cust_id = _this.form.findField('cohead_cust_id').getValue();
4326                                                                     if(!cust_id){
4327                                                                         return;
4328                                                                     }
4329                                                                     var data = {
4330                                                                         cashrcpt_amount : sel.data.cobmisc_outstanding,
4331                                                                         cashrcpt_cust_id : cust_id,
4332                                                                         cashrcpt_aropen_id : sel.data.cobmisc_aropen_id_aropen_id,
4333                                                                         cashrcpt_distdate : new Date(),
4334                                                                         cashrcpt_fundstype : 'C',
4335                                                                         cashrcpt_curr_id : sel.data.cobmisc_curr_id_curr_id,
4336                                                                         cashrcpt_curr_id_curr_name : sel.data.cobmisc_curr_id_curr_name,
4337                                                                         cashrcpt_usecustdeposit : true,
4338                                                                         cashrcpt_docdate : new Date(),
4339                                                                         cashrcpt_salescat_id : -1,
4340                                                                         cashrcpt_applydate : new Date(),
4341                                                                         cashrcpt_discount : 0
4342                                                                     };
4343                                                                     Pman.Dialog.XtupleReceivePayment.show( data , function() {
4344                                                                         _this.invgrid.ds.load({});
4345                                                                     }); 
4346                                                                 }
4347                                                             },
4348                                                             cls : 'x-btn-text-icon',
4349                                                             text : "Receive Payment",
4350                                                             icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
4351                                                         },
4352                                                         {
4353                                                             xtype: 'Item',
4354                                                             xns: Roo.menu,
4355                                                             listeners : {
4356                                                                 click : function(_self,e)
4357                                                                 {
4358                                                                     var sel  = _this.invgrid.getSelectionModel().getSelected();
4359                                                                     if (!sel || sel.data.cobmisc_cobapply_aropen_id < 1) {
4360                                                                         Roo.MessageBox.alert("Error", "Select a credit memo");
4361                                                                         return;
4362                                                                     }
4363                                                                     if(!sel.data.cobmisc_posted){
4364                                                                         Roo.MessageBox.alert("Error", "The invoice that credit memo has been applied to has been not posted");
4365                                                                         return;
4366                                                                     }
4367                                                                     var cust_id = _this.form.findField('cohead_cust_id').getValue();
4368                                                                     if(!cust_id){
4369                                                                         return;
4370                                                                     }
4371                                                                     
4372                                                                     if(!sel.data.cobmisc_outstanding || sel.data.cobmisc_outstanding == 0){
4373                                                                         Roo.MessageBox.alert("Error", "The amount of this credit memo is 0!");
4374                                                                         return;
4375                                                                     }
4376                                                                     
4377                                                                     var d = _this.form.getFieldValues();
4378                                                                     
4379                                                                     var data = {
4380                                                                         'checkhead_recip_id' : cust_id,
4381                                                                         'checkhead_recip_type' : 'C',
4382                                                                         'checkhead_checkdate' : new Date(),
4383                                                                         'checkhead_amount' : sel.data.cobmisc_outstanding,
4384                                                                         'remaining_total' : sel.data.cobmisc_outstanding,
4385                                                                         'checkhead_curr_id' : d.cohead_curr_id,
4386                                                                         'checkhead_curr_id_curr_name' : d.cohead_curr_id_curr_name,
4387                                                                         'checkhead_misc' : true,
4388                                                                         'aropen_id' : sel.data.cobmisc_cobapply_aropen_id,
4389                                                                         'cmhead_number' : sel.data.cobmisc_invchead_id_invchead_invcnumber,
4390                                                                         'cust_name' : d.cohead_cust_id_cust_name,
4391                                                                         '_create_and_post' : 1
4392                                                                 
4393                                                                     };
4394                                                                     
4395                                                                     
4396                                                                     Pman.Dialog.XtupleMiscellaneousCheck.show( data , function() {
4397                                                                         _this.invgrid.ds.load({});
4398                                                                    }); 
4399                                                                 }
4400                                                             },
4401                                                             cls : 'x-btn-text-icon',
4402                                                             text : "Issue Refund",
4403                                                             icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
4404                                                         },
4405                                                         {
4406                                                             xtype: 'Separator',
4407                                                             xns: Roo.menu
4408                                                         },
4409                                                         {
4410                                                             xtype: 'Item',
4411                                                             xns: Roo.menu,
4412                                                             listeners : {
4413                                                                 click : function(_self,e)
4414                                                                 {
4415                                                                     var sel  = _this.invgrid.getSelectionModel().getSelected();
4416                                                                     if (!sel || sel.data.cobmisc_cashrcpt_id * 1 < 1) {
4417                                                                         Roo.MessageBox.alert("Error", "Select a Receive Payment");
4418                                                                         return;
4419                                                                     }
4420                                                                     
4421                                                                     Roo.MessageBox.confirm("Confirm", "Voiding receipt will mean you will have to re-enter the receipt refund - " + 
4422                                                                                     "please take note of the details so you can enter it again correctly later.", function(r) {
4423                                                                                             
4424                                                                         if (r !='yes') {
4425                                                                             return;
4426                                                                         }
4427                                                                         new Pman.Request({
4428                                                                             url : baseURL + '/Roo/cashrcpt',
4429                                                                             method : 'POST',
4430                                                                             params : {
4431                                                                                 cashrcpt_id : sel.data.cobmisc_cashrcpt_id,
4432                                                                                 _void : 1
4433                                                                             },
4434                                                                             success : function() 
4435                                                                             {
4436                                                                                 _this.invgrid.ds.load({});
4437                                                                             
4438                                                                             }
4439                                                                         });
4440                                                                     
4441                                                                    })
4442                                                                     
4443                                                                      
4444                                                                 }
4445                                                             },
4446                                                             cls : 'x-btn-text-icon',
4447                                                             text : "Void Payment",
4448                                                             icon : rootURL + '/Pman/templates/images/trash.gif'
4449                                                         },
4450                                                         {
4451                                                             xtype: 'Item',
4452                                                             xns: Roo.menu,
4453                                                             listeners : {
4454                                                                 click : function(_self,e)
4455                                                                 {
4456                                                                     var sel  = _this.invgrid.getSelectionModel().getSelected();
4457                                                                     if (!sel || sel.data.cobmisc_checkhead_id * 1 < 1) {
4458                                                                         Roo.MessageBox.alert("Error", "Select a Miscellaneours Check");
4459                                                                         return;
4460                                                                     }
4461                                                                     
4462                                                                     Roo.MessageBox.confirm("Confirm", "Voiding refund will mean you will have to re-enter the receipt refund - " + 
4463                                                                                     "please take note of the details so you can enter it again correctly later.", function(r) {
4464                                                                                             
4465                                                                         if (r !='yes') {
4466                                                                             return;
4467                                                                         }
4468                                                                         new Pman.Request({
4469                                                                             url : baseURL + '/Roo/checkhead',
4470                                                                             method : 'POST',
4471                                                                             params : {
4472                                                                                 checkhead_id : sel.data.cobmisc_checkhead_id,
4473                                                                                 _voidPosted : 1
4474                                                                             },
4475                                                                             success : function() 
4476                                                                             {
4477                                                                                 _this.invgrid.ds.load({});
4478                                                                             
4479                                                                             }
4480                                                                         });
4481                                                                     
4482                                                                    })
4483                                                                     
4484                                                                      
4485                                                                 }
4486                                                             },
4487                                                             cls : 'x-btn-text-icon',
4488                                                             text : "Void Refund",
4489                                                             icon : rootURL + '/Pman/templates/images/trash.gif'
4490                                                         },
4491                                                         {
4492                                                             xtype: 'Item',
4493                                                             xns: Roo.menu,
4494                                                             listeners : {
4495                                                                 click : function(_self,e)
4496                                                                 {
4497                                                                     var sel  = _this.invgrid.getSelectionModel().getSelected();
4498                                                                     if (!sel || sel.data.cobmisc_cobapply_aropen_id < 1) {
4499                                                                         Roo.MessageBox.alert("Error", "Select a credit memo");
4500                                                                         return;
4501                                                                     }
4502                                                                     
4503                                                                     if(sel.data.cobmisc_posted){
4504                                                                         Roo.MessageBox.alert("Error", "You cann't void this credit memo, since the invoice that credit memo has been applied to has been posted");
4505                                                                         return;
4506                                                                     }
4507                                                                     
4508                                                                     if (sel.data.cobmisc_cobapply_id < 1) {
4509                                                                         Roo.MessageBox.alert("Error", "invaild credit memo");
4510                                                                         return;
4511                                                                     }
4512                                                                     Roo.MessageBox.confirm("Confirm", "Are you sure want to void this applied credit memo, " + 
4513                                                                                 "it will also void all the check that belongs to this credit memo ", function(r) {
4514                                                                                             
4515                                                                         if (r !='yes') {
4516                                                                             return;
4517                                                                         }
4518                                                                         new Pman.Request({
4519                                                                             url : baseURL + '/Roo/cobapply',
4520                                                                             method : 'POST',
4521                                                                             params : {
4522                                                                                 _delete : sel.data.cobmisc_cobapply_id,
4523                                                                                 _void : 1
4524                                                                             },
4525                                                                             success : function() 
4526                                                                             {
4527                                                                                 _this.invgrid.ds.load({});
4528                                                                             
4529                                                                             }
4530                                                                         });
4531                                                                     
4532                                                                    })
4533                                                                      
4534                                                                 }
4535                                                             },
4536                                                             cls : 'x-btn-text-icon',
4537                                                             text : "Void Credit Memo",
4538                                                             icon : rootURL + '/Pman/templates/images/trash.gif'
4539                                                         }
4540                                                     ]
4541                                                 }
4542                                             },
4543                                             {
4544                                                 xtype: 'Fill',
4545                                                 xns: Roo.Toolbar
4546                                             },
4547                                             {
4548                                                 xtype: 'Button',
4549                                                 xns: Roo.Toolbar,
4550                                                 listeners : {
4551                                                     click : function ()
4552                                                     {
4553                                                     
4554                                                         if (!_this.form.findField('cohead_id').getValue()) {
4555                                                             Roo.MessageBox.alert("Error", "Save Order first");
4556                                                             return;
4557                                                         }
4558                                                         
4559                                                         var rv = _this.form.getFieldValues();
4560                                                         
4561                                                        
4562                                                        Pman.Dialog.XtupleInvoice.show({
4563                                                             cobmisc_cohead_id : rv.cohead_id,
4564                                                             cobmisc_shipdate :  _this.form.findField('cohead_targetdate').getValue(),
4565                                                             cobmisc_invcdate :  _this.form.findField('cohead_targetdate').getValue(),
4566                                                             cobmisc_curr_id : rv.cohead_curr_id,
4567                                                             cobmisc_curr_id_curr_name : rv.cohead_curr_id_curr_name,
4568                                                             // below parms for add credit memo
4569                                                             cmdata : {
4570                                                                 cm_cust_id : rv.cohead_cust_id,
4571                                                                 cm_cust_id_cust_name : rv.cohead_cust_id_cust_name,
4572                                                                 cm_curr_id : rv.cohead_curr_id,
4573                                                                 cm_curr_id_curr_name : rv.cohead_curr_id_curr_name,
4574                                                                 cm_terms_id : rv.cohead_terms_id,
4575                                                                 cm_terms_id_terms_descrip : rv.cohead_terms_id_terms_descrip,
4576                                                                 cm_salesrep_id : rv.cohead_salesrep_id,
4577                                                                 cm_salesrep_id_salesrep_name : rv.cohead_salesrep_id_salesrep_name,
4578                                                                 cm_docdate : new Date(),
4579                                                                 cm_taxzone_id : rv.cohead_taxzone_id,
4580                                                                 cm_taxzone_id_taxzone_descrip : rv.cohead_taxzone_id_taxzone_descrip,
4581                                                                 cm_billto_cntct_id : rv.cohead_billto_cntct_id,
4582                                                                 cm_billto_cntct_id_cntct_name : rv.cohead_billto_cntct_id_cntct_name,
4583                                                                 cm_location_src : rv.cohead_location_src,
4584                                                                 cm_location_src_location_name : rv.cohead_location_src_location_name,
4585                                                                 cm_billto_address : rv.billto_address
4586                                                             }
4587                                                             
4588                                                         },
4589                                                             function() { 
4590                                                              _this.invgrid.ds.load({});
4591                                                         });
4592                                                        
4593                                                     },
4594                                                     render : function (_self)
4595                                                     {
4596                                                       _this.addInvoiceBtn = _self;
4597                                                     }
4598                                                 },
4599                                                 cls : 'x-btn-text-icon',
4600                                                 text : "Add",
4601                                                 icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
4602                                             },
4603                                             {
4604                                                 xtype: 'Button',
4605                                                 xns: Roo.Toolbar,
4606                                                 listeners : {
4607                                                     click : function ()
4608                                                     {
4609                                                     
4610                                                          var sel  = _this.invgrid.getSelectionModel().getSelected();
4611                                                         if (!sel) {
4612                                                             Roo.MessageBox.alert("Error", "Select a invoice");
4613                                                             return;
4614                                                         }
4615                                                         // check current status of shipment..
4616                                                         
4617                                                         var params =  {
4618                                                     //        cobmisc_id : sel.data.cobmisc_id,
4619                                                             _void : 1
4620                                                         };
4621                                                         if (sel.data.cobmisc_id * 1) {
4622                                                             params.cobmisc_id = sel.data.cobmisc_id * 1 ;
4623                                                         }
4624                                                         if (sel.data.cobmisc_invchead_id_invchead_id * 1) {    
4625                                                             params.invchead_id  =  sel.data.cobmisc_invchead_id_invchead_id * 1;
4626                                                         }
4627                                                         
4628                                                         Roo.MessageBox.confirm("Are you sure", "Are you sure you want to VOID that invoice?",
4629                                                             function(r) {
4630                                                                 if (r != 'yes') {
4631                                                                     return;
4632                                                                 }
4633                                                                 new Pman.Request({
4634                                                                     mask : 'Sending',
4635                                                                     url : baseURL + '/Roo/cobmisc',
4636                                                                     method : 'POST',
4637                                                                     params :  params,
4638                                                                     success : function() {
4639                                                                         _this.invgrid.ds.load({});
4640                                                                     }
4641                                                                 })
4642                                                                 
4643                                                             }
4644                                                         );
4645                                                                 
4646                                                                 
4647                                                        
4648                                                     }
4649                                                 },
4650                                                 cls : 'x-btn-text-icon',
4651                                                 text : "Void / Unpost",
4652                                                 icon : rootURL + '/Pman/templates/images/trash.gif'
4653                                             }
4654                                         ]
4655                                     },
4656                                     colModel : [
4657                                         {
4658                                             xtype: 'ColumnModel',
4659                                             xns: Roo.grid,
4660                                             dataIndex : 'cobmisc_invchead_id_invchead_invcnumber',
4661                                             header : 'Invoice #',
4662                                             width : 100,
4663                                             renderer : function(v,x,r) { 
4664                                                    if (v && 
4665                                                          r.data.cobmisc_invchead_id_invchead_id * 1 && 
4666                                                         !r.data.cobmisc_invchead_id_invchead_posted) {
4667                                                        return  '<span style="color:red">' + 
4668                                                             "NEEDS Voiding then re-posted: " + 
4669                                                             String.format('{0}', v) +
4670                                                             '</span>'; 
4671                                                    }
4672                                             
4673                                                    if(v && r.data.cobmisc_id == -2){
4674                                                         return String.format('<span style="margin-left: 10px;"> - {0} (Credit Memo)</span>', v); 
4675                                                    }
4676                                                    if(v && r.data.cobmisc_id == -3){
4677                                                         return String.format('<span style="margin-left: 20px;"> - {0} (Miscellaneous Check)</span>', v); 
4678                                                    }
4679                                                   if(v && r.data.cobmisc_id == -4){
4680                                                         return String.format('<span style="margin-left: 10px;"> - {0} (Receive Payment to {2}) [ {1} ]</span>', 
4681                                                                 v, r.data.cobmisc_cashrcpt_amount, r.json.cashrcpt_bankaccnt_id_bankaccnt_bankname); 
4682                                                    }
4683                                                    
4684                                                     return v ? String.format('{0}', v) : 
4685                                                             '<span style="color:red">' + "Not Posted" + '</span>'; 
4686                                             }
4687                                         },
4688                                         {
4689                                             xtype: 'ColumnModel',
4690                                             xns: Roo.grid,
4691                                             dataIndex : 'cobmisc_invcdate',
4692                                             header : 'invcdate',
4693                                             width : 75,
4694                                             renderer : function(v,x,r) { 
4695                                                 
4696                                                 return String.format('{0}', v && v.format ? v.format('d/M/Y') : ''); 
4697                                             }
4698                                         },
4699                                         {
4700                                             xtype: 'ColumnModel',
4701                                             xns: Roo.grid,
4702                                             align : 'right',
4703                                             dataIndex : 'cobmisc_qty',
4704                                             header : 'Qty',
4705                                             width : 50,
4706                                             renderer : function(v) { return String.format('{0}', v ? parseInt(v) : 0); }
4707                                         },
4708                                         {
4709                                             xtype: 'ColumnModel',
4710                                             xns: Roo.grid,
4711                                             align : 'right',
4712                                             dataIndex : 'cobmisc_itemcost',
4713                                             header : 'Item Cost',
4714                                             width : 75,
4715                                             renderer : function(v) { return String.format('{0}', v ? (1.0*v).toFixed(2)  : 0); }
4716                                         },
4717                                         {
4718                                             xtype: 'ColumnModel',
4719                                             xns: Roo.grid,
4720                                             align : 'right',
4721                                             dataIndex : 'cobmisc_freight',
4722                                             header : 'Shipping',
4723                                             width : 75,
4724                                             renderer : function(v) { return String.format('{0}', v ? (1.0*v).toFixed(2)  : ''); }
4725                                         },
4726                                         {
4727                                             xtype: 'ColumnModel',
4728                                             xns: Roo.grid,
4729                                             align : 'right',
4730                                             dataIndex : 'cobmisc_tax',
4731                                             header : 'Tax',
4732                                             width : 75,
4733                                             renderer : function(v,x,r) { 
4734                                             
4735                                                 // tax is based on the % itemcost..
4736                                             //    var ic  = r.data.cobmisc_itemcost;
4737                                                 /*
4738                                                 var ic  =r.data.cobmisc_itemcost - (1* r.data.cobmisc_itemcost_taxfree);
4739                                                 
4740                                                 var tax= _this.form.findField('cohead_tax').getValue() * 1.0;
4741                                                 var totic = _this.form.findField('cohead_subtotal').getValue() * 1.0;    
4742                                                 if (tax < 0.1) {
4743                                                     return '';
4744                                                 }
4745                                                 var taxp = tax / totic;
4746                                                 var lv = taxp * ic;
4747                                                 */
4748                                                 return String.format('{0}', (1.0*v).toFixed(2) );
4749                                                 
4750                                                 
4751                                              }
4752                                         },
4753                                         {
4754                                             xtype: 'ColumnModel',
4755                                             xns: Roo.grid,
4756                                             align : 'right',
4757                                             dataIndex : 'cobmisc_cohead_id_cohead_pretax_discount',
4758                                             header : 'Discount (Pretax)',
4759                                             width : 100,
4760                                             renderer : function(v) { return String.format('{0}', v ? (1.0*v).toFixed(2) : 0); }
4761                                         },
4762                                         {
4763                                             xtype: 'ColumnModel',
4764                                             xns: Roo.grid,
4765                                             align : 'right',
4766                                             dataIndex : 'cobmisc_misc',
4767                                             header : 'Discount (Posttax)',
4768                                             width : 100,
4769                                             renderer : function(v,x,r) 
4770                                             {
4771                                                 var vv = v - r.data.cobmisc_cohead_id_cohead_pretax_discount;
4772                                                 
4773                                                 return String.format('{0}', vv ? (1.0*vv).toFixed(2) : 0); 
4774                                             }
4775                                         },
4776                                         {
4777                                             xtype: 'ColumnModel',
4778                                             xns: Roo.grid,
4779                                             align : 'right',
4780                                             dataIndex : 'cobmisc_total',
4781                                             header : 'Total ',
4782                                             width : 75,
4783                                             renderer : function(v,x,r) { 
4784                                             
4785                                             /*    
4786                                                 var ic  = r.data.cobmisc_itemcost;
4787                                                 var tax= _this.form.findField('cohead_tax').getValue() * 1.0;
4788                                                 var totic = _this.form.findField('cohead_subtotal').getValue() * 1.0;    
4789                                                 var     lv = 0.0;
4790                                                 if (tax > 0.0) {
4791                                                     var taxp = tax / totic;
4792                                                     lv = taxp * ic;
4793                                             
4794                                                 }
4795                                               */  
4796                                               
4797                                             
4798                                                 if(v){
4799                                                     return String.format('{0}',(v * 1.0).toFixed(2) );
4800                                                 }
4801                                                 var d= r.data;
4802                                             
4803                                                 return String.format('{0}',
4804                                                    ((d.cobmisc_itemcost * 1.0) + 
4805                                                    (d.cobmisc_freight * 1.0) + 
4806                                                    (d.cobmisc_tax * 1.0) +
4807                                                    (d.cobmisc_misc * 1.0)  
4808                                             
4809                                                    ).toFixed(2) );
4810                                             }
4811                                         },
4812                                         {
4813                                             xtype: 'ColumnModel',
4814                                             xns: Roo.grid,
4815                                             align : 'right',
4816                                             dataIndex : 'cobmisc_outstanding',
4817                                             header : 'Outstanding',
4818                                             width : 75,
4819                                             renderer : function(v,x,r) 
4820                                             { 
4821                                                 
4822                                                 return String.format('{0}', v ? (v * 1.0).toFixed(2) : 0 );
4823                                             }
4824                                         }
4825                                     ]
4826                                 }
4827                             }
4828                         ],
4829                         center : {
4830                             xtype: 'LayoutRegion',
4831                             xns: Roo,
4832                             titlebar : true
4833                         },
4834                         north : {
4835                             xtype: 'LayoutRegion',
4836                             xns: Roo,
4837                             height : 250,
4838                             title : "Reserve Stock / Shipments"
4839                         }
4840                     }
4841                 },
4842                 {
4843                     xtype: 'GridPanel',
4844                     xns: Roo,
4845                     listeners : {
4846                         activate : function() {
4847                             _this.hpanel = this;
4848                             if (_this.hgrid) {
4849                                 _this.hgrid.footer.onClick('first');
4850                             }
4851                         }
4852                     },
4853                     background : true,
4854                     fitContainer : true,
4855                     fitToframe : true,
4856                     region : 'center',
4857                     tableName : 'events',
4858                     title : "History",
4859                     grid : {
4860                         xtype: 'Grid',
4861                         xns: Roo.grid,
4862                         listeners : {
4863                             render : function() 
4864                             {
4865                                 _this.hgrid = this; 
4866                                 //_this.dialog = Pman.Dialog.FILL_IN
4867                                 if (_this.hpanel.active) {
4868                                    this.footer.onClick('first');
4869                                 }
4870                             },
4871                             rowdblclick : function (_self, rowIndex, e)
4872                             {
4873                                 if (!_this.dialog) return;
4874                                 _this.dialog.show( this.getDataSource().getAt(rowIndex).data, function() {
4875                                     _this.grid.footer.onClick('first');
4876                                 }); 
4877                             }
4878                         },
4879                         autoExpandColumn : 'remarks',
4880                         loadMask : true,
4881                         dataSource : {
4882                             xtype: 'Store',
4883                             xns: Roo.data,
4884                             listeners : {
4885                                 beforeload : function (_self, options)
4886                                 {
4887                                     options.params._related_on_table = 'cohead';
4888                                     options.params._related_on_id = _this.form.findField('cohead_id').getValue();
4889                                 }
4890                             },
4891                             remoteSort : true,
4892                             sortInfo : { field : 'event_when', direction: 'DESC' },
4893                             proxy : {
4894                                 xtype: 'HttpProxy',
4895                                 xns: Roo.data,
4896                                 method : 'GET',
4897                                 url : baseURL + '/Roo/events.php'
4898                             },
4899                             reader : {
4900                                 xtype: 'JsonReader',
4901                                 xns: Roo.data,
4902                                 id : 'id',
4903                                 root : 'data',
4904                                 totalProperty : 'total',
4905                                 fields : [
4906                                     {
4907                                         'name': 'event_when',
4908                                         'type': 'date'
4909                                     },
4910                                     {
4911                                         'name': 'action',
4912                                         'type': 'string'
4913                                     },
4914                                     {
4915                                         'name': 'ipaddr',
4916                                         'type': 'string'
4917                                     },
4918                                     {
4919                                         'name': 'person_id_name',
4920                                         'type': 'string'
4921                                     },
4922                                     {
4923                                         'name': 'remarks',
4924                                         'type': 'string'
4925                                     }
4926                                 ]
4927                             }
4928                         },
4929                         footer : {
4930                             xtype: 'PagingToolbar',
4931                             xns: Roo,
4932                             pageSize : 25,
4933                             displayInfo : true,
4934                             displayMsg : "Displaying events{0} - {1} of {2}",
4935                             emptyMsg : "No events found"
4936                         },
4937                         colModel : [
4938                             {
4939                                 xtype: 'ColumnModel',
4940                                 xns: Roo.grid,
4941                                 dataIndex : 'event_when',
4942                                 header : 'Changed',
4943                                 width : 120,
4944                                 renderer : function(v) { return String.format('{0}', v ? v.format('d/M/Y H:i:s') : ''); }
4945                             },
4946                             {
4947                                 xtype: 'ColumnModel',
4948                                 xns: Roo.grid,
4949                                 dataIndex : 'action',
4950                                 header : 'action',
4951                                 width : 120,
4952                                 renderer : function(v,x,r) { return String.format('{0} - {1}', v, r.data.on_table); }
4953                             },
4954                             {
4955                                 xtype: 'ColumnModel',
4956                                 xns: Roo.grid,
4957                                 dataIndex : 'ipaddr',
4958                                 header : 'IP address',
4959                                 width : 120,
4960                                 renderer : function(v) { return String.format('{0}', v); }
4961                             },
4962                             {
4963                                 xtype: 'ColumnModel',
4964                                 xns: Roo.grid,
4965                                 dataIndex : 'person_id_name',
4966                                 header : 'Who',
4967                                 width : 120,
4968                                 renderer : function(v) { return String.format('{0}', v); }
4969                             },
4970                             {
4971                                 xtype: 'ColumnModel',
4972                                 xns: Roo.grid,
4973                                 dataIndex : 'remarks',
4974                                 header : 'Notes',
4975                                 width : 200,
4976                                 renderer : function(v) { return String.format('{0}', v); }
4977                             }
4978                         ]
4979                     }
4980                 },
4981                 {
4982                     xtype: 'NestedLayoutPanel',
4983                     xns: Roo,
4984                     region : 'center',
4985                     title : "Stock Tx",
4986                     layout : {
4987                         xtype: 'BorderLayout',
4988                         xns: Roo,
4989                         items : [
4990                             {
4991                                 xtype: 'GridPanel',
4992                                 xns: Roo,
4993                                 listeners : {
4994                                     activate : function() {
4995                                         _this.txpanel = this;
4996                                         if (_this.txgrid) {
4997                                             _this.txgrid.footer.onClick('first');
4998                                         }
4999                                     }
5000                                 },
5001                                 background : false,
5002                                 fitContainer : true,
5003                                 fitToframe : true,
5004                                 region : 'center',
5005                                 tableName : 'invdetail',
5006                                 title : "invdetail",
5007                                 grid : {
5008                                     xtype: 'Grid',
5009                                     xns: Roo.grid,
5010                                     listeners : {
5011                                         render : function() 
5012                                         {
5013                                             _this.txgrid = this; 
5014                                             //_this.dialog = Pman.Dialog.FILL_IN
5015                                             if (_this.txpanel.active) {
5016                                                this.footer.onClick('first');
5017                                             }
5018                                         }
5019                                     },
5020                                     autoExpandColumn : 'item_number',
5021                                     loadMask : true,
5022                                     toolbar : {
5023                                         xtype: 'Toolbar',
5024                                         xns: Roo,
5025                                         items : [
5026                                             {
5027                                                 xtype: 'Button',
5028                                                 xns: Roo.Toolbar,
5029                                                 listeners : {
5030                                                     click : function (_self, e)
5031                                                     {
5032                                                         new Pman.Request({
5033                                                             mask : 'applying',
5034                                                             url : baseURL + '/Roo/Cohead',
5035                                                             method : 'GET',
5036                                                             params : {
5037                                                                 _apply_fifo : _this.form.findField('cohead_id').getValue()
5038                                                             },
5039                                                             success : function () {
5040                                                                 Roo.MessageBox.alert("Applied", "Succesfully Applied");
5041                                                             }
5042                                                         });
5043                                                     }
5044                                                 },
5045                                                 text : "Run Apply Fifo on order"
5046                                             },
5047                                             {
5048                                                 xtype: 'Button',
5049                                                 xns: Roo.Toolbar,
5050                                                 listeners : {
5051                                                     click : function (_self, e)
5052                                                     {
5053                                                         new Pman.Request({
5054                                                             mask : 'running',
5055                                                             url : baseURL + '/Roo/Cohead',
5056                                                             method : 'GET',
5057                                                             params : {
5058                                                                 _run_void_fix : _this.form.findField('cohead_id').getValue()
5059                                                             },
5060                                                             success : function () {
5061                                                                 Roo.MessageBox.alert("Applied", "Succesfully Run");
5062                                                             }
5063                                                         });
5064                                                     }
5065                                                 },
5066                                                 text : "Run Void flagger"
5067                                             },
5068                                             {
5069                                                 xtype: 'Button',
5070                                                 xns: Roo.Toolbar,
5071                                                 listeners : {
5072                                                     click : function (_self, e)
5073                                                     {
5074                                                         new Pman.Download({
5075                                                             newWindow : true,
5076                                                             mask : 'running',
5077                                                             url : baseURL + '/Roo/Invdetail',
5078                                                             method : 'GET',
5079                                                             timeout :90000,
5080                                                             params : {
5081                                                                 _post : 1,
5082                                                                 _reverse_all_bad : _this.form.findField('cohead_id').getValue()
5083                                                             },
5084                                                             success : function () {
5085                                                                 Roo.MessageBox.alert("Applied", "Succesfully Run");
5086                                                             }
5087                                                         });
5088                                                     }
5089                                                 },
5090                                                 text : "Auto Reverse"
5091                                             }
5092                                         ]
5093                                     },
5094                                     sm : {
5095                                         xtype: 'RowSelectionModel',
5096                                         xns: Roo.grid,
5097                                         listeners : {
5098                                             selectionchange : function (_self)
5099                                             {
5100                                                 _this.txdgrid.footer.onClick('first');;
5101                                             }
5102                                         },
5103                                         singleSelect : true
5104                                     },
5105                                     dataSource : {
5106                                         xtype: 'Store',
5107                                         xns: Roo.data,
5108                                         listeners : {
5109                                             beforeload : function (_self, o)
5110                                             {
5111                                                 o.params.cohead_id = _this.form.findField('cohead_id').getValue();
5112                                                 if (! o.params.cohead_id ) {
5113                                                     this.removeAll();
5114                                                     return false;
5115                                                 }
5116                                             }
5117                                         },
5118                                         remoteSort : true,
5119                                         sortInfo : { field : 'item_number', direction: 'ASC' },
5120                                         proxy : {
5121                                             xtype: 'HttpProxy',
5122                                             xns: Roo.data,
5123                                             method : 'GET',
5124                                             url : baseURL + '/Roo/invdetail.php'
5125                                         },
5126                                         reader : {
5127                                             xtype: 'JsonReader',
5128                                             xns: Roo.data,
5129                                             totalProperty : 'total',
5130                                             root : 'data',
5131                                             id : 'id',
5132                                             fields : [
5133                                                 {
5134                                                     'name': 'invdetail_id',
5135                                                     'type': 'int'
5136                                                 },
5137                                                 {
5138                                                     'name': 'invdetail_transtype',
5139                                                     'type': 'string'
5140                                                 },
5141                                                 {
5142                                                     'name': 'invdetail_invhist_id',
5143                                                     'type': 'int'
5144                                                 },
5145                                                 {
5146                                                     'name': 'invdetail_location_id',
5147                                                     'type': 'int'
5148                                                 },
5149                                                 {
5150                                                     'name': 'invdetail_qty',
5151                                                     'type': 'float'
5152                                                 },
5153                                                 {
5154                                                     'name': 'invdetail_comments',
5155                                                     'type': 'string'
5156                                                 },
5157                                                 {
5158                                                     'name': 'invdetail_qty_before',
5159                                                     'type': 'float'
5160                                                 },
5161                                                 {
5162                                                     'name': 'invdetail_qty_after',
5163                                                     'type': 'float'
5164                                                 },
5165                                                 {
5166                                                     'name': 'invdetail_invcitem_id',
5167                                                     'type': 'int'
5168                                                 },
5169                                                 {
5170                                                     'name': 'invdetail_expiration',
5171                                                     'type': 'date',
5172                                                     'dateFormat': 'Y-m-d'
5173                                                 },
5174                                                 {
5175                                                     'name': 'invdetail_warrpurc',
5176                                                     'type': 'date',
5177                                                     'dateFormat': 'Y-m-d'
5178                                                 },
5179                                                 {
5180                                                     'name': 'invdetail_ls_id',
5181                                                     'type': 'int'
5182                                                 }
5183                                             ]
5184                                         }
5185                                     },
5186                                     footer : {
5187                                         xtype: 'PagingToolbar',
5188                                         xns: Roo,
5189                                         displayInfo : true,
5190                                         displayMsg : "Displaying invdetail{0} - {1} of {2}",
5191                                         emptyMsg : "No invdetail found",
5192                                         pageSize : 9999
5193                                     },
5194                                     colModel : [
5195                                         {
5196                                             xtype: 'ColumnModel',
5197                                             xns: Roo.grid,
5198                                             dataIndex : 'item_number',
5199                                             header : 'Item',
5200                                             width : 75,
5201                                             renderer : function(v) { return String.format('{0}', v); }
5202                                         },
5203                                         {
5204                                             xtype: 'ColumnModel',
5205                                             xns: Roo.grid,
5206                                             align : 'right',
5207                                             dataIndex : 'rec_shipped',
5208                                             header : '#Ship',
5209                                             width : 50,
5210                                             renderer : function(v) { return String.format('{0}',  Roo.util.Format.number(v,0)); }
5211                                         },
5212                                         {
5213                                             xtype: 'ColumnModel',
5214                                             xns: Roo.grid,
5215                                             align : 'right',
5216                                             dataIndex : 'rec_returned',
5217                                             header : '#Ret',
5218                                             width : 50,
5219                                             renderer : function(v) { return String.format('{0}', Roo.util.Format.number(v,0)); }
5220                                         },
5221                                         {
5222                                             xtype: 'ColumnModel',
5223                                             xns: Roo.grid,
5224                                             align : 'right',
5225                                             dataIndex : 'tx_shipped',
5226                                             header : '#TX ship',
5227                                             width : 50,
5228                                             renderer : function(v) { return String.format('{0}',  Roo.util.Format.number(v,0)); }
5229                                         },
5230                                         {
5231                                             xtype: 'ColumnModel',
5232                                             xns: Roo.grid,
5233                                             align : 'right',
5234                                             dataIndex : 'tx_returned',
5235                                             header : '#TX ret',
5236                                             width : 50,
5237                                             renderer : function(v) { return String.format('{0}',  Roo.util.Format.number(v,0)); }
5238                                         },
5239                                         {
5240                                             xtype: 'ColumnModel',
5241                                             xns: Roo.grid,
5242                                             align : 'right',
5243                                             dataIndex : 'tx_total',
5244                                             header : '#TX Tot',
5245                                             width : 50,
5246                                             renderer : function(v) { return String.format('{0}', Roo.util.Format.number(v,0)); }
5247                                         },
5248                                         {
5249                                             xtype: 'ColumnModel',
5250                                             xns: Roo.grid,
5251                                             align : 'right',
5252                                             dataIndex : 'total_value',
5253                                             header : 'Value',
5254                                             width : 50,
5255                                             renderer : function(v,x,r) { 
5256                                             
5257                                                 return (v*1).toFixed(3);
5258                                             }
5259                                         },
5260                                         {
5261                                             xtype: 'ColumnModel',
5262                                             xns: Roo.grid,
5263                                             align : 'right',
5264                                             dataIndex : 'tx_total',
5265                                             header : '#Diff',
5266                                             width : 50,
5267                                             renderer : function(v,x,r) { 
5268                                             
5269                                                 var cototal = (r.data.rec_returned*1) - (r.data.rec_shipped*1);
5270                                             //    Roo.log(cototal);
5271                                                 var diff = (v*1) - cototal;
5272                                              //   Roo.log(diff);
5273                                                 if (diff == 0)  {
5274                                                     return '';
5275                                                 }
5276                                                 return String.format('<span style="color:red">{0}</span>',  diff ); 
5277                                                 }
5278                                         }
5279                                     ]
5280                                 }
5281                             },
5282                             {
5283                                 xtype: 'GridPanel',
5284                                 xns: Roo,
5285                                 listeners : {
5286                                     activate : function() {
5287                                         _this.txdpanel = this;
5288                                         if (_this.txdgrid) {
5289                                             _this.txdgrid.footer.onClick('first');
5290                                         }
5291                                     }
5292                                 },
5293                                 background : false,
5294                                 fitContainer : true,
5295                                 fitToframe : true,
5296                                 region : 'east',
5297                                 tableName : 'invdetail',
5298                                 title : "invdetail",
5299                                 grid : {
5300                                     xtype: 'Grid',
5301                                     xns: Roo.grid,
5302                                     listeners : {
5303                                         render : function() 
5304                                         {
5305                                             _this.txdgrid = this; 
5306                                             //_this.dialog = Pman.Dialog.FILL_IN
5307                                             if (_this.txdpanel.active) {
5308                                                this.footer.onClick('first');
5309                                             }
5310                                         },
5311                                         rowclick : function (_self, rowIndex, e)
5312                                         {
5313                                             var s = _this.txdgrid.ds.getAt(rowIndex);
5314                                             var dt = s.data.invhist_transdate.split(' ');
5315                                             
5316                                             _this.dateSel.setValue(new Date(dt[0]));
5317                                         }
5318                                     },
5319                                     autoExpandColumn : 'invhist_comments',
5320                                     loadMask : true,
5321                                     toolbar : {
5322                                         xtype: 'Toolbar',
5323                                         xns: Roo,
5324                                         items : [
5325                                             {
5326                                                 xtype: 'Button',
5327                                                 xns: Roo.Toolbar,
5328                                                 listeners : {
5329                                                     click : function (_self, e)
5330                                                     {
5331                                                         var s = _this.txdgrid.selModel.getSelected();
5332                                                     
5333                                                         if (!s) {
5334                                                             Roo.MessageBox.alert("Error", "Select a transaction");
5335                                                             return;
5336                                                         }
5337                                                         
5338                                                         var reverseSel = function() {
5339                                                         
5340                                                             new Pman.Request({
5341                                                                 mask : "Reversing",
5342                                                                 url : baseURL + '/Roo/invdetail',
5343                                                                 method : 'POST',
5344                                                                 params : {
5345                                                                     _duplicate : s.data.invdetail_id
5346                                                                 }, 
5347                                                                 success : function() {
5348                                                                     _this.txdgrid.footer.onClick('first');
5349                                                                 }
5350                                                             });
5351                                                         
5352                                                         }
5353                                                         
5354                                                         
5355                                                     
5356                                                         Roo.MessageBox.confirm(
5357                                                             "Confirm", 
5358                                                             "This should only be used by System Administrators - are you sure you know what you are doing!",
5359                                                             function(x) {
5360                                                                 if (x != 'yes') {
5361                                                                     return;
5362                                                                 }
5363                                                                 reverseSel();
5364                                                             }
5365                                                         );
5366                                                                             
5367                                                         
5368                                                         
5369                                                         
5370                                                     }
5371                                                 },
5372                                                 text : "Duplicate Selected"
5373                                             },
5374                                             {
5375                                                 xtype: 'Fill',
5376                                                 xns: Roo.Toolbar
5377                                             },
5378                                             {
5379                                                 xtype: 'TextItem',
5380                                                 xns: Roo.Toolbar,
5381                                                 text : "Issue Date"
5382                                             },
5383                                             {
5384                                                 xtype: 'DateField',
5385                                                 xns: Roo.form,
5386                                                 listeners : {
5387                                                     render : function (_self)
5388                                                     {
5389                                                         _this.dateSel = _self;
5390                                                     }
5391                                                 },
5392                                                 allowBlank : false,
5393                                                 fieldLabel : 'Issue Date',
5394                                                 format : 'Y-m-d',
5395                                                 name : 'issue_date',
5396                                                 width : 150
5397                                             },
5398                                             {
5399                                                 xtype: 'Button',
5400                                                 xns: Roo.Toolbar,
5401                                                 listeners : {
5402                                                     click : function (_self, e)
5403                                                     {
5404                                                         var s = _this.txdgrid.selModel.getSelected();
5405                                                         var dt = _this.dateSel.getValue();
5406                                                         if (!s) {
5407                                                             Roo.MessageBox.alert("Error", "Select a transaction");
5408                                                             return;
5409                                                         }
5410                                                         
5411                                                         var reverseSel = function(force) {
5412                                                         
5413                                                             new Pman.Request({
5414                                                                 mask : "Reversing",
5415                                                                 url : baseURL + '/Roo/invdetail',
5416                                                                 method : 'POST',
5417                                                                 params : {
5418                                                                     _reverse : s.data.invdetail_id,
5419                                                                     _as_of : typeof(dt) == 'string' ? dt : dt.format('Y-m-d'),
5420                                                                     _force : force
5421                                                                 }, 
5422                                                                 success : function() {
5423                                                                     _this.txdgrid.footer.onClick('first');
5424                                                                 },
5425                                                                 failure : function(res) {
5426                                                                     Roo.log(res);
5427                                                                     try {
5428                                                                         if (res.errors.confirm) {
5429                                                                                           
5430                                                                             Roo.MessageBox.confirm(
5431                                                                                 "Confirm", 
5432                                                                                 "are you really sure the totals will get messed up.",
5433                                                                                 function(x) {
5434                                                                                     if (x != 'yes') {
5435                                                                                         return;
5436                                                                                     }
5437                                                                                     reverseSel(1);
5438                                                                                 }
5439                                                                             );
5440                                                                             return;
5441                                                                         }
5442                                                                     } catch(e) { }
5443                                                                     Roo.MessageBox.alert("Error", res.errorMsg);
5444                                                                     
5445                                                                     
5446                                                                 }
5447                                                             });
5448                                                         
5449                                                         }
5450                                                         
5451                                                         
5452                                                     
5453                                                         Roo.MessageBox.confirm(
5454                                                             "Confirm", 
5455                                                             "This should only be used by System Administrators - are you sure you know what you are doing!",
5456                                                             function(x) {
5457                                                                 if (x != 'yes') {
5458                                                                     return;
5459                                                                 }
5460                                                                 reverseSel(0);
5461                                                             }
5462                                                         );
5463                                                                             
5464                                                         
5465                                                         
5466                                                         
5467                                                     }
5468                                                 },
5469                                                 text : "Reverse Selected"
5470                                             }
5471                                         ]
5472                                     },
5473                                     dataSource : {
5474                                         xtype: 'Store',
5475                                         xns: Roo.data,
5476                                         listeners : {
5477                                             beforeload : function (_self, o)
5478                                             {
5479                                                 o.params.cohead_id = _this.form.findField('cohead_id').getValue();
5480                                                 var s = _this.txgrid.selModel.getSelected();
5481                                                 if (!s) { 
5482                                                     this.removeAll();
5483                                                     return false;
5484                                                 }
5485                                                 o.params.itemsite_id = s.data.invhist_itemsite_id;
5486                                                 
5487                                             }
5488                                         },
5489                                         remoteSort : true,
5490                                         sortInfo : { field : 'invdetail_id', direction: 'ASC' },
5491                                         proxy : {
5492                                             xtype: 'HttpProxy',
5493                                             xns: Roo.data,
5494                                             method : 'GET',
5495                                             url : baseURL + '/Roo/invdetail.php'
5496                                         },
5497                                         reader : {
5498                                             xtype: 'JsonReader',
5499                                             xns: Roo.data,
5500                                             totalProperty : 'total',
5501                                             root : 'data',
5502                                             id : 'id',
5503                                             fields : [
5504                                                 {
5505                                                     'name': 'invdetail_id',
5506                                                     'type': 'int'
5507                                                 },
5508                                                 {
5509                                                     'name': 'invdetail_transtype',
5510                                                     'type': 'string'
5511                                                 },
5512                                                 {
5513                                                     'name': 'invdetail_invhist_id',
5514                                                     'type': 'int'
5515                                                 },
5516                                                 {
5517                                                     'name': 'invdetail_location_id',
5518                                                     'type': 'int'
5519                                                 },
5520                                                 {
5521                                                     'name': 'invdetail_qty',
5522                                                     'type': 'float'
5523                                                 },
5524                                                 {
5525                                                     'name': 'invdetail_comments',
5526                                                     'type': 'string'
5527                                                 },
5528                                                 {
5529                                                     'name': 'invdetail_qty_before',
5530                                                     'type': 'float'
5531                                                 },
5532                                                 {
5533                                                     'name': 'invdetail_qty_after',
5534                                                     'type': 'float'
5535                                                 },
5536                                                 {
5537                                                     'name': 'invdetail_invcitem_id',
5538                                                     'type': 'int'
5539                                                 },
5540                                                 {
5541                                                     'name': 'invdetail_expiration',
5542                                                     'type': 'date',
5543                                                     'dateFormat': 'Y-m-d'
5544                                                 },
5545                                                 {
5546                                                     'name': 'invdetail_warrpurc',
5547                                                     'type': 'date',
5548                                                     'dateFormat': 'Y-m-d'
5549                                                 },
5550                                                 {
5551                                                     'name': 'invdetail_ls_id',
5552                                                     'type': 'int'
5553                                                 }
5554                                             ]
5555                                         }
5556                                     },
5557                                     footer : {
5558                                         xtype: 'PagingToolbar',
5559                                         xns: Roo,
5560                                         displayInfo : true,
5561                                         displayMsg : "Displaying invdetail{0} - {1} of {2}",
5562                                         emptyMsg : "No invdetail found",
5563                                         pageSize : 9999
5564                                     },
5565                                     colModel : [
5566                                         {
5567                                             xtype: 'ColumnModel',
5568                                             xns: Roo.grid,
5569                                             dataIndex : 'invdetail_id',
5570                                             header : 'TX#',
5571                                             width : 60,
5572                                             renderer : function(v) { return String.format('{0}', v); }
5573                                         },
5574                                         {
5575                                             xtype: 'ColumnModel',
5576                                             xns: Roo.grid,
5577                                             dataIndex : 'invhist_transdate',
5578                                             header : 'Date',
5579                                             width : 75,
5580                                             renderer : function(v) { return String.format('{0}', v); }
5581                                         },
5582                                         {
5583                                             xtype: 'ColumnModel',
5584                                             xns: Roo.grid,
5585                                             dataIndex : 'invhist_ordnumber',
5586                                             header : 'Ref#',
5587                                             width : 75,
5588                                             renderer : function(v,x,r) {
5589                                                 if (r.data.invfifo_void *1 != 0) {
5590                                                     return String.format('<s>{0}</s>', v); 
5591                                                 }
5592                                             
5593                                                  return String.format('{0}', v); 
5594                                              }
5595                                         },
5596                                         {
5597                                             xtype: 'ColumnModel',
5598                                             xns: Roo.grid,
5599                                             dataIndex : 'location_name',
5600                                             header : 'TX#',
5601                                             width : 60,
5602                                             renderer : function(v) { return String.format('{0}', v); }
5603                                         },
5604                                         {
5605                                             xtype: 'ColumnModel',
5606                                             xns: Roo.grid,
5607                                             dataIndex : 'invhist_comments',
5608                                             header : 'Notes',
5609                                             width : 75,
5610                                             renderer : function(v) { return String.format('{0}', v); }
5611                                         },
5612                                         {
5613                                             xtype: 'ColumnModel',
5614                                             xns: Roo.grid,
5615                                             align : 'right',
5616                                             dataIndex : 'invdetail_qty',
5617                                             header : 'Qty Changed',
5618                                             width : 50,
5619                                             renderer : function(v,x,r) {
5620                                                 if ( r.data.coitem_shipped != v) {
5621                                                     return String.format('{0} <span style="color:red">({1})</span>',
5622                                                          Roo.util.Format.number(v,0),
5623                                                          r.data.coitem_shipped
5624                                                      );
5625                                                 
5626                                                 }
5627                                                  return String.format('{0}', Roo.util.Format.number(v,0));
5628                                             }
5629                                         },
5630                                         {
5631                                             xtype: 'ColumnModel',
5632                                             xns: Roo.grid,
5633                                             align : 'right',
5634                                             dataIndex : 'invhist_value_before',
5635                                             header : 'Qty Changed',
5636                                             width : 75,
5637                                             renderer : function(v,x,r) {
5638                                                 var tot = r.data.invhist_value_after*1 - v*1;
5639                                                 var f = '{0}';
5640                                                 if (r.data.invdetail_qty >0 && tot < 0) {
5641                                                     f = '<span style="color:red">{0}</span>';
5642                                                 }
5643                                                 if (r.data.invdetail_qty < 0 && tot > 0) {
5644                                                     f = '<span style="color:red">{0}</span>';
5645                                                 }    
5646                                                 return String.format(f, Roo.util.Format.number(tot));
5647                                             }
5648                                         },
5649                                         {
5650                                             xtype: 'ColumnModel',
5651                                             xns: Roo.grid,
5652                                             dataIndex : 'invhist_posted',
5653                                             header : 'Posted',
5654                                             width : 40,
5655                                             renderer : function(v,x,r) { 
5656                                                 
5657                                                 var state = v   ?  '-checked' : '';
5658                                                                                 
5659                                                 return '<img class="x-grid-check-icon' + state + '" src="' + Roo.BLANK_IMAGE_URL + '"/>';
5660                                              }
5661                                         }
5662                                     ]
5663                                 }
5664                             }
5665                         ],
5666                         center : {
5667                             xtype: 'LayoutRegion',
5668                             xns: Roo
5669                         },
5670                         east : {
5671                             xtype: 'LayoutRegion',
5672                             xns: Roo,
5673                             split : true,
5674                             width : 500
5675                         }
5676                     }
5677                 },
5678                 {
5679                     xtype: 'NestedLayoutPanel',
5680                     xns: Roo,
5681                     region : 'center',
5682                     title : "GL Tx",
5683                     layout : {
5684                         xtype: 'BorderLayout',
5685                         xns: Roo,
5686                         items : [
5687                             {
5688                                 xtype: 'GridPanel',
5689                                 xns: Roo,
5690                                 listeners : {
5691                                     activate : function() {
5692                                         _this.gltxpanel = this;
5693                                         if (_this.gltxgrid) {
5694                                             _this.gltxgrid.footer.onClick('first');
5695                                         }
5696                                     }
5697                                 },
5698                                 background : false,
5699                                 fitContainer : true,
5700                                 fitToframe : true,
5701                                 region : 'center',
5702                                 tableName : 'invdetail',
5703                                 title : "invdetail",
5704                                 grid : {
5705                                     xtype: 'Grid',
5706                                     xns: Roo.grid,
5707                                     listeners : {
5708                                         render : function() 
5709                                         {
5710                                             _this.gltxgrid = this; 
5711                                             //_this.dialog = Pman.Dialog.FILL_IN
5712                                             if (_this.gltxpanel.active) {
5713                                                this.footer.onClick('first');
5714                                             }
5715                                         },
5716                                         cellclick : function (_self, rowIndex, columnIndex, e)
5717                                         {
5718                                             if (columnIndex > 0) {
5719                                                 return;
5720                                             }
5721                                             var rec = this.ds.getAt(rowIndex);
5722                                             rec.set('gltrans_as_summary', rec.data.gltrans_as_summary *1 ? 0 : 1);
5723                                             _this.gltxdgrid.footer.onClick('first');
5724                                         }
5725                                     },
5726                                     autoExpandColumn : 'gltrans_accnt_id_accnt_descrip',
5727                                     loadMask : true,
5728                                     sm : {
5729                                         xtype: 'RowSelectionModel',
5730                                         xns: Roo.grid,
5731                                         listeners : {
5732                                             selectionchange : function (_self)
5733                                             {
5734                                                 _this.gltxdgrid.footer.onClick('first');;
5735                                             }
5736                                         },
5737                                         singleSelect : true
5738                                     },
5739                                     dataSource : {
5740                                         xtype: 'Store',
5741                                         xns: Roo.data,
5742                                         listeners : {
5743                                             beforeload : function (_self, o)
5744                                             {
5745                                                 o.params.cohead_id = _this.form.findField('cohead_id').getValue();
5746                                                 if (! o.params.cohead_id ) {
5747                                                     this.removeAll();
5748                                                     return false;
5749                                                 }
5750                                                 o.params._split_sales = _this.glsalesbtn.pressed ? 1 : 0;
5751                                             }
5752                                         },
5753                                         remoteSort : true,
5754                                         sortInfo : { field : 'item_number', direction: 'ASC' },
5755                                         proxy : {
5756                                             xtype: 'HttpProxy',
5757                                             xns: Roo.data,
5758                                             method : 'GET',
5759                                             url : baseURL + '/Roo/gltrans.php'
5760                                         },
5761                                         reader : {
5762                                             xtype: 'JsonReader',
5763                                             xns: Roo.data,
5764                                             totalProperty : 'total',
5765                                             root : 'data',
5766                                             id : 'id',
5767                                             fields : [
5768                                                 {
5769                                                     'name': 'invdetail_id',
5770                                                     'type': 'int'
5771                                                 },
5772                                                 {
5773                                                     'name': 'invdetail_transtype',
5774                                                     'type': 'string'
5775                                                 },
5776                                                 {
5777                                                     'name': 'invdetail_invhist_id',
5778                                                     'type': 'int'
5779                                                 },
5780                                                 {
5781                                                     'name': 'invdetail_location_id',
5782                                                     'type': 'int'
5783                                                 },
5784                                                 {
5785                                                     'name': 'invdetail_qty',
5786                                                     'type': 'float'
5787                                                 },
5788                                                 {
5789                                                     'name': 'invdetail_comments',
5790                                                     'type': 'string'
5791                                                 },
5792                                                 {
5793                                                     'name': 'invdetail_qty_before',
5794                                                     'type': 'float'
5795                                                 },
5796                                                 {
5797                                                     'name': 'invdetail_qty_after',
5798                                                     'type': 'float'
5799                                                 },
5800                                                 {
5801                                                     'name': 'invdetail_invcitem_id',
5802                                                     'type': 'int'
5803                                                 },
5804                                                 {
5805                                                     'name': 'invdetail_expiration',
5806                                                     'type': 'date',
5807                                                     'dateFormat': 'Y-m-d'
5808                                                 },
5809                                                 {
5810                                                     'name': 'invdetail_warrpurc',
5811                                                     'type': 'date',
5812                                                     'dateFormat': 'Y-m-d'
5813                                                 },
5814                                                 {
5815                                                     'name': 'invdetail_ls_id',
5816                                                     'type': 'int'
5817                                                 }
5818                                             ]
5819                                         }
5820                                     },
5821                                     footer : {
5822                                         xtype: 'PagingToolbar',
5823                                         xns: Roo,
5824                                         displayInfo : true,
5825                                         displayMsg : "Displaying invdetail{0} - {1} of {2}",
5826                                         emptyMsg : "No invdetail found",
5827                                         pageSize : 9999,
5828                                         items : [
5829                                             {
5830                                                 xtype: 'Button',
5831                                                 xns: Roo.Toolbar,
5832                                                 listeners : {
5833                                                     render : function (_self)
5834                                                     {
5835                                                         _this.glsalesbtn = _self;
5836                                                     },
5837                                                     click : function (_self, e)
5838                                                     {
5839                                                         (function()  { _this.gltxgrid.footer.onClick('first'); }).defer(100);
5840                                                     }
5841                                                 },
5842                                                 enableToggle : true,
5843                                                 pressed : true,
5844                                                 text : "Split sales"
5845                                             }
5846                                         ]
5847                                     },
5848                                     colModel : [
5849                                         {
5850                                             xtype: 'ColumnModel',
5851                                             xns: Roo.grid,
5852                                             dataIndex : 'gltrans_as_summary',
5853                                             header : 'Summary',
5854                                             width : 50,
5855                                             renderer : function(v,x,r) { 
5856                                                 
5857                                                 var state = v*1   ?  '-checked' : '';
5858                                                                                 
5859                                                 return '<img class="x-grid-check-icon' + state + '" src="' + Roo.BLANK_IMAGE_URL + '"/>';
5860                                              }
5861                                         },
5862                                         {
5863                                             xtype: 'ColumnModel',
5864                                             xns: Roo.grid,
5865                                             dataIndex : 'gltrans_accnt_id_accnt_descrip',
5866                                             header : 'Account',
5867                                             width : 75,
5868                                             renderer : function(v) { return String.format('{0}', v); }
5869                                         },
5870                                         {
5871                                             xtype: 'ColumnModel',
5872                                             xns: Roo.grid,
5873                                             align : 'right',
5874                                             dataIndex : 'gltrans_amount_credit',
5875                                             header : 'Credit',
5876                                             width : 75,
5877                                             renderer : function(v) { return String.format('{0}',  Roo.util.Format.number(v,3)); }
5878                                         },
5879                                         {
5880                                             xtype: 'ColumnModel',
5881                                             xns: Roo.grid,
5882                                             align : 'right',
5883                                             dataIndex : 'gltrans_amount_debit',
5884                                             header : 'Debit',
5885                                             width : 75,
5886                                             renderer : function(v) { return String.format('{0}', Roo.util.Format.number(v,3)); }
5887                                         },
5888                                         {
5889                                             xtype: 'ColumnModel',
5890                                             xns: Roo.grid,
5891                                             align : 'right',
5892                                             dataIndex : 'gltrans_amount_total',
5893                                             header : 'Sum',
5894                                             width : 75,
5895                                             renderer : function(v) { return String.format('<b>{0}</b>', Roo.util.Format.number(v,3)); }
5896                                         },
5897                                         {
5898                                             xtype: 'ColumnModel',
5899                                             xns: Roo.grid,
5900                                             align : 'right',
5901                                             dataIndex : 'gltrans_amount_total_unposted',
5902                                             header : 'Unposted',
5903                                             width : 75,
5904                                             renderer : function(v) { return String.format('<span style="color:red">{0}</span>', Roo.util.Format.number(v,3)); }
5905                                         }
5906                                     ]
5907                                 }
5908                             },
5909                             {
5910                                 xtype: 'GridPanel',
5911                                 xns: Roo,
5912                                 listeners : {
5913                                     activate : function() {
5914                                         _this.gltxdpanel = this;
5915                                         if (_this.gltxdgrid) {
5916                                             _this.gltxdgrid.footer.onClick('first');
5917                                         }
5918                                     }
5919                                 },
5920                                 background : false,
5921                                 fitContainer : true,
5922                                 fitToframe : true,
5923                                 region : 'east',
5924                                 tableName : 'invdetail',
5925                                 title : "invdetail",
5926                                 grid : {
5927                                     xtype: 'Grid',
5928                                     xns: Roo.grid,
5929                                     listeners : {
5930                                         render : function() 
5931                                         {
5932                                             _this.gltxdgrid = this; 
5933                                             //_this.dialog = Pman.Dialog.FILL_IN
5934                                             if (_this.gltxdpanel.active) {
5935                                                this.footer.onClick('first');
5936                                             }
5937                                         },
5938                                         cellclick : function (_self, ri, ci, e)
5939                                         {
5940                                             
5941                                               var deleteSel = function() {
5942                                             
5943                                                 new Pman.Request({
5944                                                     mask : "Reversing",
5945                                                     url : baseURL + '/Roo/gltrans',
5946                                                     method : 'POST',
5947                                                     params : {  
5948                                                         _void : 1,
5949                                                         gltrans_id : rec.data.gltrans_id
5950                                                         
5951                                                     }, 
5952                                                     success : function() {
5953                                                         rec.set('gltrans_posted', false);
5954                                                         rec.set('gltrans_deleted', true);
5955                                                         rec.set('gltrans_docnumber', rec.data.gltrans_docnumber);
5956                                                        _this.gltxgrid.footer.onClick('first');
5957                                                     }
5958                                                 });
5959                                             
5960                                             }
5961                                             var undeleteSel = function() {
5962                                             
5963                                                 new Pman.Request({
5964                                                     mask : "Reversing",
5965                                                     url : baseURL + '/Roo/gltrans',
5966                                                     method : 'POST',
5967                                                     params : {  
5968                                                         _unvoid : 1,
5969                                                         gltrans_id : rec.data.gltrans_id
5970                                                         
5971                                                     }, 
5972                                                     success : function() {
5973                                                         rec.set('gltrans_posted', true);
5974                                                         rec.set('gltrans_deleted', false);
5975                                                         rec.set('gltrans_docnumber', rec.data.gltrans_docnumber);
5976                                                        _this.gltxgrid.footer.onClick('first');
5977                                                     }
5978                                                 });
5979                                             
5980                                             }
5981                                             
5982                                             var di = this.colModel.config[ci].dataIndex;
5983                                             if (di != 'gltrans_posted') {
5984                                                 return;
5985                                             }
5986                                             
5987                                             
5988                                             
5989                                             var rec = this.ds.getAt(ri);
5990                                             if (rec.data.gltrans_deleted) {
5991                                             
5992                                                  Roo.MessageBox.confirm(
5993                                                     "Confirm", 
5994                                                     "This should only be used by System Administrators - are you sure you know what you are doing!",
5995                                                     function(x) {
5996                                                         if (x != 'yes') {
5997                                                             return;
5998                                                         }
5999                                                         undeleteSel();
6000                                                     }
6001                                                 );
6002                                             
6003                                                 return false;
6004                                             }
6005                                         
6006                                             
6007                                             
6008                                             if (!rec.data.gltrans_posted) {
6009                                         
6010                                                 
6011                                                 
6012                                                 new Pman.Request({
6013                                                     method : 'POST',
6014                                                     url : baseURL + '/Roo/gltrans',
6015                                                     mask : "Posting",
6016                                                     params : {
6017                                                         _post : 1,
6018                                                         gltrans_id : rec.data.gltrans_id
6019                                                     },
6020                                                     success : function (res)
6021                                                     {
6022                                                         rec.set('gltrans_posted', true);
6023                                                         _this.gltxgrid.footer.onClick('first');
6024                                                     
6025                                                     }
6026                                                 
6027                                                 
6028                                                 });
6029                                                 return;
6030                                             }
6031                                             
6032                                             // we have a posted transaction.
6033                                             // only allow recalled to be deleted..
6034                                             if (!rec.data.gltrans_notes.match(/(Recall|Ship Order)/)) {
6035                                                 return false;
6036                                             }
6037                                             
6038                                             
6039                                          
6040                                             
6041                                             
6042                                         
6043                                             Roo.MessageBox.confirm(
6044                                                 "Confirm", 
6045                                                 "This should only be used by System Administrators - are you sure you know what you are doing!",
6046                                                 function(x) {
6047                                                     if (x != 'yes') {
6048                                                         return;
6049                                                     }
6050                                                     deleteSel();
6051                                                 }
6052                                             );
6053                                                   
6054                                             
6055                                             
6056                                         }
6057                                     },
6058                                     autoExpandColumn : 'gltrans_notes',
6059                                     loadMask : true,
6060                                     dataSource : {
6061                                         xtype: 'Store',
6062                                         xns: Roo.data,
6063                                         listeners : {
6064                                             beforeload : function (_self, o)
6065                                             {
6066                                                 o.params.cohead_id = _this.form.findField('cohead_id').getValue();
6067                                                 var s = _this.gltxgrid.selModel.getSelected();
6068                                                 if (!s) { 
6069                                                     this.removeAll();
6070                                                     return false;
6071                                                 }
6072                                                 o.params.gltrans_accnt_id = s.data.gltrans_accnt_id;
6073                                                 o.params.gltrans_is_ship = s.data.gltrans_is_ship;
6074                                                 o.params.gltrans_as_summary =     s.data.gltrans_as_summary;
6075                                                 
6076                                                 
6077                                             }
6078                                         },
6079                                         remoteSort : true,
6080                                         sortInfo : { field : 'gltrans_docnumber', direction: 'ASC' },
6081                                         proxy : {
6082                                             xtype: 'HttpProxy',
6083                                             xns: Roo.data,
6084                                             method : 'GET',
6085                                             url : baseURL + '/Roo/gltrans.php'
6086                                         },
6087                                         reader : {
6088                                             xtype: 'JsonReader',
6089                                             xns: Roo.data,
6090                                             totalProperty : 'total',
6091                                             root : 'data',
6092                                             id : 'id',
6093                                             fields : [
6094                                                 {
6095                                                     'name': 'invdetail_id',
6096                                                     'type': 'int'
6097                                                 },
6098                                                 {
6099                                                     'name': 'invdetail_transtype',
6100                                                     'type': 'string'
6101                                                 },
6102                                                 {
6103                                                     'name': 'invdetail_invhist_id',
6104                                                     'type': 'int'
6105                                                 },
6106                                                 {
6107                                                     'name': 'invdetail_location_id',
6108                                                     'type': 'int'
6109                                                 },
6110                                                 {
6111                                                     'name': 'invdetail_qty',
6112                                                     'type': 'float'
6113                                                 },
6114                                                 {
6115                                                     'name': 'invdetail_comments',
6116                                                     'type': 'string'
6117                                                 },
6118                                                 {
6119                                                     'name': 'invdetail_qty_before',
6120                                                     'type': 'float'
6121                                                 },
6122                                                 {
6123                                                     'name': 'invdetail_qty_after',
6124                                                     'type': 'float'
6125                                                 },
6126                                                 {
6127                                                     'name': 'invdetail_invcitem_id',
6128                                                     'type': 'int'
6129                                                 },
6130                                                 {
6131                                                     'name': 'invdetail_expiration',
6132                                                     'type': 'date',
6133                                                     'dateFormat': 'Y-m-d'
6134                                                 },
6135                                                 {
6136                                                     'name': 'invdetail_warrpurc',
6137                                                     'type': 'date',
6138                                                     'dateFormat': 'Y-m-d'
6139                                                 },
6140                                                 {
6141                                                     'name': 'invdetail_ls_id',
6142                                                     'type': 'int'
6143                                                 }
6144                                             ]
6145                                         }
6146                                     },
6147                                     footer : {
6148                                         xtype: 'PagingToolbar',
6149                                         xns: Roo,
6150                                         displayInfo : true,
6151                                         displayMsg : "Displaying invdetail{0} - {1} of {2}",
6152                                         emptyMsg : "No invdetail found",
6153                                         pageSize : 9999,
6154                                         items : [
6155                                             {
6156                                                 xtype: 'Button',
6157                                                 xns: Roo.Toolbar,
6158                                                 listeners : {
6159                                                     click : function (_self, e)
6160                                                     {
6161                                                         new Pman.Download( {
6162                                                             grid : _this.gltxdgrid
6163                                                         
6164                                                         });
6165                                                     }
6166                                                 },
6167                                                 text : "Download"
6168                                             }
6169                                         ]
6170                                     },
6171                                     colModel : [
6172                                         {
6173                                             xtype: 'ColumnModel',
6174                                             xns: Roo.grid,
6175                                             dataIndex : 'gltrans_id',
6176                                             header : 'Ref#',
6177                                             sortable : true,
6178                                             width : 75,
6179                                             renderer : function(v) { return String.format('{0}', v ); }
6180                                         },
6181                                         {
6182                                             xtype: 'ColumnModel',
6183                                             xns: Roo.grid,
6184                                             dataIndex : 'gltrans_date',
6185                                             header : 'Date',
6186                                             width : 75,
6187                                             renderer : function(v) { return String.format('{0}', v ? v.format('d/M/Y') : ''); }
6188                                         },
6189                                         {
6190                                             xtype: 'ColumnModel',
6191                                             xns: Roo.grid,
6192                                             dataIndex : 'gltrans_docnumber',
6193                                             header : 'Doc#',
6194                                             width : 75,
6195                                             renderer : function(v,x,r) { 
6196                                                 if (r.data.gltrans_deleted) {
6197                                                    return String.format('<s>{0}</s>', v);     
6198                                                 }
6199                                                 return String.format('{0}', v); 
6200                                             }
6201                                         },
6202                                         {
6203                                             xtype: 'ColumnModel',
6204                                             xns: Roo.grid,
6205                                             dataIndex : 'gltrans_source',
6206                                             header : 'Source',
6207                                             width : 50,
6208                                             renderer : function(v) { return String.format('{0}', v); }
6209                                         },
6210                                         {
6211                                             xtype: 'ColumnModel',
6212                                             xns: Roo.grid,
6213                                             dataIndex : 'gltrans_notes',
6214                                             header : 'Notes',
6215                                             width : 75,
6216                                             renderer : function(v) { return String.format('{0}', v); }
6217                                         },
6218                                         {
6219                                             xtype: 'ColumnModel',
6220                                             xns: Roo.grid,
6221                                             align : 'right',
6222                                             dataIndex : 'gltrans_amount',
6223                                             header : 'Amount',
6224                                             width : 75,
6225                                             renderer : function(v) { return String.format('{0}', Roo.util.Format.number(v,3)); }
6226                                         },
6227                                         {
6228                                             xtype: 'ColumnModel',
6229                                             xns: Roo.grid,
6230                                             dataIndex : 'gltrans_posted',
6231                                             header : 'Posted',
6232                                             width : 50,
6233                                             renderer : function(v,x,r) { 
6234                                                 
6235                                                 var state = v   ?  '-checked' : '';
6236                                                                                 
6237                                                 return '<img class="x-grid-check-icon' + state + '" src="' + Roo.BLANK_IMAGE_URL + '"/>';
6238                                              }
6239                                         }
6240                                     ]
6241                                 }
6242                             }
6243                         ],
6244                         center : {
6245                             xtype: 'LayoutRegion',
6246                             xns: Roo
6247                         },
6248                         east : {
6249                             xtype: 'LayoutRegion',
6250                             xns: Roo,
6251                             split : true,
6252                             width : 500
6253                         }
6254                     }
6255                 },
6256                 {
6257                     xtype: 'GridPanel',
6258                     xns: Roo,
6259                     listeners : {
6260                         activate : function() {
6261                             _this.profitpanel = this;
6262                             
6263                             try { if (MODULE.isBuilder) {
6264                                 return;
6265                             } } catch(e) { }
6266                             
6267                             var id = _this.form.findField('cohead_id').getValue() * 1;
6268                             if (id < 1) {
6269                                 Roo.MessageBox.alert("Save First", "Save the order first, before adding items");
6270                                 _this.dialog.layout.getRegion('center').showPanel(0);
6271                                 return;
6272                             }
6273                             
6274                             if (_this.profitgrid) {
6275                                 _this.profitgrid.footer.onClick('first');
6276                              }
6277                             
6278                         }
6279                     },
6280                     background : true,
6281                     fitContainer : true,
6282                     fitToframe : true,
6283                     region : 'center',
6284                     tableName : 'coitem',
6285                     title : "Profit",
6286                     grid : {
6287                         xtype: 'Grid',
6288                         xns: Roo.grid,
6289                         listeners : {
6290                             render : function() 
6291                             {
6292                                 _this.profitgrid = this; 
6293                                 //_this.dialog = Pman.Dialog.FILL_IN
6294                                 if (_this.profitpanel.active) {
6295                                    this.footer.onClick('first');
6296                                 }
6297                             }
6298                         },
6299                         autoExpandColumn : 'item_number',
6300                         loadMask : true,
6301                         dataSource : {
6302                             xtype: 'Store',
6303                             xns: Roo.data,
6304                             listeners : {
6305                                 beforeload : function (_self,o) {
6306                                 
6307                                     try {
6308                                        this.removeAll();
6309                                    } catch (e) { }
6310                                    
6311                                 
6312                                     if (!_this.data || !_this.data.cohead_id) {
6313                                         return false;
6314                                     }
6315                                     o.params = o.params || {};
6316                                     
6317                                     o.params.coitem_cohead_id = _this.data.cohead_id;
6318                                     o.params._without_list_discount =1;
6319                                     o.params._with_profit = 1;
6320                                     
6321                                 }
6322                             },
6323                             remoteSort : true,
6324                             sortInfo : { field : 'coitem_linenumber,coitem_subnumber', direction: 'ASC' },
6325                             proxy : {
6326                                 xtype: 'HttpProxy',
6327                                 xns: Roo.data,
6328                                 method : 'GET',
6329                                 url : baseURL + '/Roo/coitem.php'
6330                             },
6331                             reader : {
6332                                 xtype: 'JsonReader',
6333                                 xns: Roo.data,
6334                                 totalProperty : 'total',
6335                                 root : 'data',
6336                                 id : 'id',
6337                                 fields : [
6338                                     {
6339                                         'name': 'id',
6340                                         'type': 'int'
6341                                     },
6342                                     {
6343                                         'name': 'name',
6344                                         'type': 'string'
6345                                     },
6346                                     {
6347                                         'name': 'type',
6348                                         'type': 'int'
6349                                     },
6350                                     {
6351                                         'name': 'leader',
6352                                         'type': 'int'
6353                                     },
6354                                     {
6355                                         'name': 'leader_id',
6356                                         'type': 'int'
6357                                     },
6358                                     {
6359                                         'name': 'leader_office_id',
6360                                         'type': 'int'
6361                                     },
6362                                     {
6363                                         'name': 'leader_name',
6364                                         'type': 'string'
6365                                     },
6366                                     {
6367                                         'name': 'leader_phone',
6368                                         'type': 'string'
6369                                     },
6370                                     {
6371                                         'name': 'leader_fax',
6372                                         'type': 'string'
6373                                     },
6374                                     {
6375                                         'name': 'leader_email',
6376                                         'type': 'string'
6377                                     },
6378                                     {
6379                                         'name': 'leader_company_id',
6380                                         'type': 'int'
6381                                     },
6382                                     {
6383                                         'name': 'leader_role',
6384                                         'type': 'string'
6385                                     },
6386                                     {
6387                                         'name': 'leader_active',
6388                                         'type': 'int'
6389                                     },
6390                                     {
6391                                         'name': 'leader_remarks',
6392                                         'type': 'string'
6393                                     },
6394                                     {
6395                                         'name': 'leader_passwd',
6396                                         'type': 'string'
6397                                     },
6398                                     {
6399                                         'name': 'leader_owner_id',
6400                                         'type': 'int'
6401                                     },
6402                                     {
6403                                         'name': 'leader_lang',
6404                                         'type': 'string'
6405                                     },
6406                                     {
6407                                         'name': 'leader_no_reset_sent',
6408                                         'type': 'int'
6409                                     },
6410                                     {
6411                                         'name': 'leader_action_type',
6412                                         'type': 'string'
6413                                     },
6414                                     {
6415                                         'name': 'leader_project_id',
6416                                         'type': 'int'
6417                                     },
6418                                     {
6419                                         'name': 'leader_deleted_by',
6420                                         'type': 'int'
6421                                     },
6422                                     {
6423                                         'name': 'leader_deleted_dt',
6424                                         'type': 'date'
6425                                     },
6426                                     {
6427                                         'name': 'leader_firstname',
6428                                         'type': 'string'
6429                                     },
6430                                     {
6431                                         'name': 'leader_lastname',
6432                                         'type': 'string'
6433                                     },
6434                                     {
6435                                         'name': 'leader_name_facebook',
6436                                         'type': 'string'
6437                                     },
6438                                     {
6439                                         'name': 'leader_url_blog',
6440                                         'type': 'string'
6441                                     },
6442                                     {
6443                                         'name': 'leader_url_twitter',
6444                                         'type': 'string'
6445                                     },
6446                                     {
6447                                         'name': 'leader_url_linkedin',
6448                                         'type': 'string'
6449                                     },
6450                                     {
6451                                         'name': 'leader_crm_lead_percentage',
6452                                         'type': 'int'
6453                                     },
6454                                     {
6455                                         'name': 'leader_crm_industry_id',
6456                                         'type': 'int'
6457                                     },
6458                                     {
6459                                         'name': 'leader_crm_updated_action_id',
6460                                         'type': 'int'
6461                                     },
6462                                     {
6463                                         'name': 'leader_crm_created_action_id',
6464                                         'type': 'int'
6465                                     },
6466                                     {
6467                                         'name': 'leader_crm_type_id',
6468                                         'type': 'int'
6469                                     }
6470                                 ]
6471                             }
6472                         },
6473                         footer : {
6474                             xtype: 'PagingToolbar',
6475                             xns: Roo,
6476                             displayInfo : true,
6477                             displayMsg : "Displaying coitem{0} - {1} of {2}",
6478                             emptyMsg : "No coitem found",
6479                             pageSize : 25
6480                         },
6481                         colModel : [
6482                             {
6483                                 xtype: 'ColumnModel',
6484                                 xns: Roo.grid,
6485                                 dataIndex : 'item_number',
6486                                 header : 'Item Code',
6487                                 width : 75,
6488                                 renderer : function(v) { return String.format('{0}', v); }
6489                             },
6490                             {
6491                                 xtype: 'ColumnModel',
6492                                 xns: Roo.grid,
6493                                 dataIndex : 'calc_subtotal',
6494                                 header : 'SubTotal',
6495                                 width : 200,
6496                                 renderer : function(v) {
6497                                 
6498                                 
6499                                      return Roo.util.Format.number( v, 2);
6500                                   }
6501                             },
6502                             {
6503                                 xtype: 'ColumnModel',
6504                                 xns: Roo.grid,
6505                                 dataIndex : 'calc_cost_total',
6506                                 header : 'Cost of goods',
6507                                 width : 200,
6508                                 renderer : function(v) {
6509                                 
6510                                     return Roo.util.Format.number( v, 2);
6511                                   }
6512                             },
6513                             {
6514                                 xtype: 'ColumnModel',
6515                                 xns: Roo.grid,
6516                                 dataIndex : 'profit',
6517                                 header : 'Profit',
6518                                 width : 200,
6519                                 renderer : function(v,x,r) {
6520                                         
6521                                    var profit = r.data.calc_subtotal - r.data.calc_cost_total;
6522                                    
6523                                    if(parseInt(profit) < 1){
6524                                        return String.format('<b style="color:red;">{0}</b>', Roo.util.Format.number(profit,2));      
6525                                    }
6526                                         
6527                                         
6528                                     return Roo.util.Format.number( profit, 2);
6529                                 }
6530                             }
6531                         ]
6532                     }
6533                 }
6534             ],
6535             center : {
6536                 xtype: 'LayoutRegion',
6537                 xns: Roo,
6538                 alwaysShowTabs : true,
6539                 tabPosition : 'top',
6540                 toolbar : {
6541                     xtype: 'Toolbar',
6542                     xns: Roo,
6543                     items : [
6544                         {
6545                             xtype: 'Fill',
6546                             xns: Roo.Toolbar
6547                         },
6548                         {
6549                             xtype: 'Button',
6550                             xns: Roo.Toolbar,
6551                             listeners : {
6552                                 click : function ()
6553                                 {
6554                                     var id = 1* _this.form.findField('cohead_id').getValue();
6555                                     if (!id) {
6556                                         Roo.MessageBox.alert("Error", "Save Sales order first");
6557                                         return;
6558                                     
6559                                     }
6560                                     // check current status of shipment..
6561                                 
6562                                         new Pman.Download({
6563                                             url : baseURL + '/Roo/cohead',
6564                                             method : 'GET',
6565                                             params : {
6566                                                 cohead_id :  id,
6567                                                 _excel : 1
6568                                             },
6569                                             success : function() {
6570                                 
6571                                             }
6572                                         })
6573                                             
6574                                             
6575                                    
6576                                 }
6577                             },
6578                             cls : 'x-btn-text-icon',
6579                             text : "Download Excel",
6580                             icon : rootURL + '/Pman/templates/images/spreadsheet.gif'
6581                         },
6582                         {
6583                             xtype: 'Button',
6584                             xns: Roo.Toolbar,
6585                             listeners : {
6586                                 click : function ()
6587                                 {
6588                                     var id = 1* _this.form.findField('cohead_id').getValue();
6589                                     if (!id) {
6590                                         Roo.MessageBox.alert("Error", "Save Sales order first");
6591                                         return;
6592                                     
6593                                     }
6594                                     // check current status of shipment..
6595                                 
6596                                         new Pman.Download({
6597                                             url : baseURL + '/Roo/cohead',
6598                                             method : 'GET',
6599                                             params : {
6600                                                 cohead_id :  id,
6601                                                 _print : 1
6602                                             },
6603                                             success : function() {
6604                                 
6605                                             }
6606                                         })
6607                                             
6608                                             
6609                                    
6610                                 }
6611                             },
6612                             cls : 'x-btn-text-icon',
6613                             text : "Print",
6614                             icon : rootURL + '/Pman/templates/images/pdf.gif'
6615                         },
6616                         {
6617                             xtype: 'Button',
6618                             xns: Roo.Toolbar,
6619                             listeners : {
6620                                 click : function (_self, e)
6621                                 {
6622                                 
6623                                     _this.addShipmentBtn.fireEvent('click');
6624                                    
6625                                 }
6626                             },
6627                             cls : 'x-btn-text-icon',
6628                             hidden : true,
6629                             text : "Add Shipment",
6630                             icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
6631                         },
6632                         {
6633                             xtype: 'Button',
6634                             xns: Roo.Toolbar,
6635                             listeners : {
6636                                 click : function (_self, e)
6637                                 {
6638                                 
6639                                    Pman.Dialog.XtupleCustomer.show({
6640                                          cust_id : _this.form.findField('cohead_cust_id').getValue()
6641                                   }); 
6642                                 }
6643                             },
6644                             cls : 'x-btn-text-icon',
6645                             text : "Edit Customer",
6646                             icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
6647                         },
6648                         {
6649                             xtype: 'Button',
6650                             xns: Roo.Toolbar,
6651                             listeners : {
6652                                 click : function (_self, e)
6653                                 {
6654                                 
6655                                         _this.addInvoiceBtn.fireEvent('click');
6656                                    
6657                                 }
6658                             },
6659                             cls : 'x-btn-text-icon',
6660                             hidden : true,
6661                             text : "Add Invoice",
6662                             icon : Roo.rootURL + 'images/default/dd/drop-add.gif'
6663                         },
6664                         {
6665                             xtype: 'Button',
6666                             xns: Roo.Toolbar,
6667                             listeners : {
6668                                 render : function (_self, e)
6669                                 {
6670                                     _this.voidBtn = _self;
6671                                 },
6672                                 click : function (_self, e)
6673                                 {
6674                                      var p = {         
6675                                          cohead_id : _this.form.findField('cohead_id').getValue() 
6676                                      };
6677                                     function call() {
6678                                 
6679                                             
6680                                        new Pman.Request({
6681                                             mask : 'Sending',
6682                                             url: baseURL + '/Roo/cohead',
6683                                             method : 'POST',
6684                                             params :  p,
6685                                             success : function()
6686                                             {
6687                                                 _this.callback();
6688                                                  if ( _this.data.cohead_status == 'X') {
6689                                                   _this.form.load({ method: 'GET', params: { '_id' : _this.data.cohead_id }});
6690                                           
6691                                                     return;
6692                                                  }
6693                                 
6694                                  
6695                                                 _this.dialog.hide();
6696                                             }
6697                                        });
6698                                    }
6699                                 
6700                                   
6701                                    if (_this.data.cohead_status == 'X') {
6702                                         p._unvoid = 1;
6703                                         call();
6704                                         return;
6705                                     }
6706                                     
6707                                     Roo.MessageBox.confirm("Confirm", "Are you sure you want to void this?", function(r) {
6708                                     
6709                                         if (r !='yes') {
6710                                             return;
6711                                         }
6712                                         p._void = 1;
6713                                         call();       
6714                                 
6715                                       
6716                                     });
6717                                 
6718                                 }
6719                             },
6720                             text : "Void"
6721                         },
6722                         {
6723                             xtype: 'Button',
6724                             xns: Roo.Toolbar,
6725                             listeners : {
6726                                 click : function (_self, e)
6727                                 {
6728                                     var p = {          cohead_id : _this.form.findField('cohead_id').getValue() };
6729                                     
6730                                     var close = 1;
6731                                     if (_this.data.cohead_status == 'C') {
6732                                         p._reopen = 1;
6733                                         close = 0;
6734                                         } else {
6735                                                 p._close = 1;
6736                                         }
6737                                         
6738                                    new Pman.Request({
6739                                     url: baseURL + '/Roo/cohead',
6740                                     method : 'POST',
6741                                     params :  p,
6742                                     mask : 'Sending',
6743                                     success : function()
6744                                     {
6745                                            _this.callback();
6746                                           if (!close) {
6747                                             _this.form.load({ method: 'GET', params: { '_id' : _this.data.cohead_id }});
6748                                           
6749                                             return;
6750                                           }
6751                                        
6752                                         _this.dialog.hide();
6753                                     }
6754                                    });
6755                                     
6756                                 },
6757                                 render : function (_self)
6758                                 {
6759                                    _this.closeBtn = _self;
6760                                 }
6761                             },
6762                             text : "Completed"
6763                         }
6764                     ]
6765                 }
6766             },
6767             buttons : [
6768                 {
6769                     xtype: 'Button',
6770                     xns: Roo,
6771                     listeners : {
6772                         click : function (_self, e)
6773                         {
6774                             var id = _this.form.findField('cohead_id').getValue() * 1;
6775                             
6776                             if(id < 1){
6777                                 Roo.MessageBox.alert('Error', 'Error occour on getting the sales order id');
6778                                 return;
6779                             }
6780                         
6781                             Roo.MessageBox.confirm("Confirm", "Are you sure to create a credit memo reversal?",
6782                                 function (res) {
6783                                     if(res!='yes') {
6784                                         return;
6785                                     
6786                                     }
6787                                     new Pman.Request({
6788                                         method: 'POST',
6789                                         url: baseURL+ '/Roo/cohead',
6790                                         params : {
6791                                             cohead_id : id,
6792                                             _cm_reversal : 1
6793                                         },
6794                                         success : function() {
6795                                             _this.form.load({ method: 'GET', params: { '_id' : id }});
6796                                         }
6797                                     });
6798                             }); 
6799                               
6800                         },
6801                         render : function (_self)
6802                         {
6803                             _this.reversalBtn = _self;
6804                         }
6805                     },
6806                     text : "Create CM reversal"
6807                 },
6808                 {
6809                     xtype: 'Button',
6810                     xns: Roo,
6811                     listeners : {
6812                         click : function (_self, e)
6813                         {
6814                               if (_this.grid)  _this.grid.stopEditing();
6815                             _this.dialog.hide();
6816                         }
6817                     },
6818                     text : "Cancel"
6819                 },
6820                 {
6821                     xtype: 'Button',
6822                     xns: Roo,
6823                     listeners : {
6824                         click : function (_self, e)
6825                         {
6826                             // do some checks?
6827                              
6828                               if (_this.grid)  _this.grid.stopEditing();
6829                          
6830                             _this.form.doAction("submit");
6831                         
6832                         },
6833                         render : function (_self)
6834                         {
6835                             _this.saveBtn = _self;
6836                         }
6837                     },
6838                     text : "Save"
6839                 }
6840             ]
6841         });
6842     }
6843 };