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