static void CreateSalesOrder(Args _args)
{
    SalesTable                              salesTable;
    SalesLine                               salesLine;
    NumberSeq                               numberSeq;
    date                                    ShippingDateRequestedDate;
    date                                    ReceiptDateRequestedDate;
    DimensionAttribute                      dimensionAttribute;
    DimensionAttributeValueSetStorage       dimensionStorage;
    DimensionAttributeValue                 newDimensonValue;
    InventTableModule                       inventTableModule;
    CustTable                               custTable;
    InventTable                             inventTable;
    InventDim                               inventDim;
    InventBatch                             inventBatch;
    InventSerial                            inventSerial;

    try
    {
        ttsBegin;

        //Create sales order header.
        salesTable.clear();

        //Number sequence automatically gets the next number as per system's configuration
        //SalesId is mandatory to create sales order
        numberSeq                           = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
        numberSeq.used();

        salesTable.SalesId                  = numberSeq.num();
        salesTable.initValue();

        //Create sales type
        salesTable.SalesType                = str2Enum(SalesType::Sales, "salesTypeValue");

        //CustAccount is mandatory to create sales order
        salesTable.CustAccount              = "CNMF-000033";
        salesTable.initFromCustTable();
        salesTable.InclTax                  = noyes::Yes;
        salesTable.TaxGroup                 = "taxGroupId";
        salesTable.LanguageId               = currentUserLanguage();
        salesTable.ShippingDateRequested    = today();
        salesTable.ReceiptDateRequested     = today();
        salesTable.modifiedFieldDDC(fieldNum(salesTable, ShippingDateRequested));
        salesTable.InvoiceAccount           = "CNMF-000001";
        salesTable.CurrencyCode             = "CNY";
        salesTable.CustGroup                = "40";
        //It will be used to set the Site & Warehouse and create inventory dimension id
        if(InventLocation::find("WarehouseNum").InventLocationId != "")
        {
            salesTable.InventSiteId         = InventLocation::find("WarehouseNum").InventSiteId;
            salesTable.InventLocationId     = inventlocation::find("WarehouseNum").InventLocationId;
        }
        salesTable.insert();

        try
        {
            salesLine.clear();
            salesLine.initValue();
            salesLine.initFromSalesTable(salesTable);

            salesLine.SalesStatus               = SalesStatus::Backorder;  //状态:未结订单

            //Init from itmeid.
            inventTable                         = InventTable::find("CNMF-6666666", false);
            salesLine.ItemId                    = inventTable.ItemId;
            salesLine.SalesUnit                 = inventTable.salesUnitId();
            salesLine.initFromInventTable(inventtable::find(salesLine.ItemId));
            salesLine.modifyField(fieldNum(SalesLine, ItemId));

            inventTableModule                   = InventTableModule::find(inventTable.ItemId, ModuleInventPurchSales::Sales);
            salesLine.TaxItemGroup              = InventTableModule.TaxItemGroupId; //销售税(物料)组
            salesLine.SalesQty                  = 20;
            SalesLine::modifySalesQty(salesLine, salesLine.inventDim(),false);
            salesLine.SalesPrice                = 10.1;
            salesLine.ShippingDateRequested     = today(); //要求装运日期
            salesLine.ReceiptDateRequested      = today(); //要求接受日期

            //Init from custTable
            custTable = CustTable::find("CNMF-000025", false);
            if(custTable)
            {
                salesLine.CustAccount               = custTable.AccountNum;
                salesLine.initFromCustTable();      //带出客户上的销售税组
            }
            else
            {
                throw error("客户编号不存在!");
            }

            inventBatch = InventBatch::find("inventBatchId", "000102", false);
            if(!inventBatch)
            {
                if (inventBatch.RecId == 0)
                {
                    inventBatch.inventBatchId   = "inventBatchId";
                    inventBatch.itemId          = "000102";
                    inventBatch.expDate         = dateNull();
                    inventBatch.insert();
                }
            }
            inventSerial = InventSerial::find("InventSerieId", "000102", false);
            if (inventSerial.RecId == 0)
            {
                inventSerial.InventSerialId     = "InventSerieId";
                inventSerial.ItemId             = "000102";
                inventSerial.insert();
            }
            inventdim                           = salesLine.inventDim();
            inventdim.inventBatchId             = inventBatch.inventBatchId;    //批次号
            inventdim.inventSerialId            = inventSerial.InventSerialId;  //序列号
            inventdim.InventSiteId              = "SH";                         //站点
            inventdim.InventLocationId          = "SH-01";                      //仓库
            inventdim.wMSLocationId             = "01-01-01";                   //库位

            inventdim                           = inventdim::findOrCreate(inventdim);
            salesLine.InventDimId               = inventdim.inventDimId;

            //客户维度为例
            if(salesLine.CustAccount && DimensionAttribute::findByName("Customer"))
            {
                dimensionAttribute  = DimensionAttribute::findByName("Customer");
                newDimensonValue    = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute, salesLine.CustAccount, false, true);
                dimensionStorage    = DimensionAttributeValueSetStorage::find(salesLine.DefaultDimension);
                dimensionStorage.addItem(newDimensonValue);
            }
            salesLine.DefaultDimension  = dimensionStorage.save();

            InventMovement::bufferSetRemainQty(salesLine);  //updating the inventory transactions.
            salesLine.insert();
            info(strFmt("已创建销售订单:%1", salesTable.SalesId));
        }
        catch(Exception::Error)
        {
            ttsAbort;
        }
        ttsCommit; 
    }
    catch(Exception::Error)
    {
        ttsAbort;
    }
}

  

posted on 2019-10-30 11:43  Sunny_Li  阅读(452)  评论(0编辑  收藏  举报