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