Creating sales orders with the SalesAutoCreate class

这个例子来自Google,有点绕,通过类SalesAutoCreate和中间表实现销售订单导入,中间表主要储存销售订单行关键信息。

 

Many projects use an interface to import their sales orders, because of this a SalesAutoCreate class was created. This class is easily extendable and customizable.The first thing to do is designing a buffer table, like this one for.

After this we can start extending a new class from the SalesAutoCreate class and modifying the construct on the SalesAutoCreateClass .

 1 class TSTSalesAutoCreate extends SalesAutoCreate
 2 {
 3     TSTSalesImport  TSTSalesImport;
 4 }
 5 static SalesAutoCreate construct(Common       buffer = null,
 6                                  Object       object = null,
 7                                  Common       buffer2 = null)  //Factory method, instance the class
 8 {
 9     switch (buffer.TableId)
10     {
11         case tablenum(SalesCreateReleaseOrderLineTmp)     : return new SalesAutoCreate_ReleaseOrder(buffer,object,buffer2);
12         case tablenum(PurchLine)                          : return new SalesAutoCreate_ProjPurchLine(buffer,object);
13         case tablenum(SalesBasketLine)                    : return new SalesAutoCreate_Basket(buffer,object);
14         //-> TST
15         case tablenum(TSTSalesImport)                     : return new TSTSalesAutoCreate(buffer,object);  //Instance the class
16         //<- TST
17     }
18 
19    throw error(strfmt("@SYS23419",tableid2name(buffer.TableId)));
20 }

After the construct your buffer record should be passed in the new or a parm method could also be an option.

1 void new(Common _initFromBuffer, Object _callBackClass)
2 {
3     ;
4 
5     super(_initFromBuffer,_callBackClass);
6  
7     TSTSalesImport  = _initFromBuffer;
8 }

When extending from the SalesAutoCreate class some methods must be implemented:

  • setCust
  • salesType
  • recordExist
  • nextRecord
  • invoiceAccount

In my example I’ve implemented them like this:

 1 protected void setCust()
 2 {
 3     ;
 4 
 5     custTable   = CustTable::find(TSTSalesImport.CustAccount);
 6  
 7     if(!custTable)
 8     {
 9         throw error("Custtable not found!");
10     }
11 }
12  
13 protected SalesType salesType()
14 {
15     ;
16 
17     return SalesType::Sales;
18 }
19  
20 protected boolean recordExist()
21 {
22     ;
23 
24     return TSTSalesImport.RecId != 0;
25 }
26  
27 protected void nextRecord()
28 {
29    ;
30 
31    next TSTSalesImport;
32 }
33  
34 protected CustInvoiceAccount invoiceAccount()
35 {
36     CustInvoiceAccount  ret = custTable.InvoiceAccount;
37     ;
38  
39     if(!ret)
40     {
41         ret = custTable.AccountNum;
42     }
43  
44     return ret;
45 }

The next step is setting our table and line fields by overriding the setSalesTable and setSalesLine methods and make sure that you always call the super first. Notice that you need to call the createSalesTable and createSalesLine to do the insert.

 1 protected void setSalesTable()
 2 {
 3     ;
 4 
 5     super();
 6  
 7     this.createSalesTable();
 8 }
 9  
10 protected void setSalesLine()
11 {
12     ;
13 
14     super();
15  
16     salesLine.ItemId    = TSTSalesImport.ItemId;
17     salesLine.itemIdChanged();
18     salesLine.SalesQty  = TSTSalesImport.SalesOrderedQty;
19  
20     this.createSalesLine();
21 }

you should have a class looking like this.

the final step is to call the logic from a job or a RunBaseBatch class, make sure that you select records on the same tier as the nextRecord will run or else it will fail. Preferably on the server tier.

1 select forupdate TSTSalesImport;
2  
3 SalesAutoCreate = SalesAutoCreate::construct(TSTSalesImport);
4 SalesAutoCreate.create();

This example lacks some validation whether the record has already been processed or not, so it will created the same records every time it is called. You could implement your own method on the SalesAutoCreate class, call it from the create method and override it on your custom class, like this.

 1 void  create()
 2 {
 3     #OCCRetryCount
 4     try
 5     {
 6         setprefix("@SYS55110");
 7         ttsbegin;
 8          while (this.recordExist())
 9         {
10              this.setCust();
11             setprefix(#PreFixField(CustTable,AccountNum));
12              this.setSalesTable();
13             this.setSalesLine();
14             setprefix(#PreFixField(SalesLine,ItemId));
15             //-> TST
16             this.deleteProcessed();
17             //<- TST
18             this.nextRecord();
19         }
20         this.endUpdate();
21 
22        ttscommit;
23     }
24     catch (Exception::Deadlock)
25     {
26         retry;
27     }
28     catch (Exception::UpdateConflict)
29     {
30         if (appl.ttsLevel() == 0)
31         {
32             if (xSession::currentRetryCount() >= #RetryNum)
33             {
34                 throw Exception::UpdateConflictNotRecovered;
35             }
36             else
37             {
38                 retry;
39             }
40         }
41         else
42         {
43             throw Exception::UpdateConflict;
44         }
45     }
46  
47 }
48  
49 //SalesAutoCreate
50 protected void deleteProcessed()
51 {
52 }
53  
54 //TSTSalesImport
55 protected void deleteProcessed()
56 {;
57     super();
58  
59     TSTSalesImport.selectForUpdate(true);
60     TSTSalesImport.delete();
61 }
posted @ 2012-08-28 08:56  Sprite.z  Views(425)  Comments(0Edit  收藏  举报