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