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