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