iT邦幫忙

0

[Aras筆記] 從Excel快速貼上受影響物件並建立變更單

2021-01-27 03:18:28308 瀏覽

###本文章內容皆由我本人開發撰寫與分享

在變更單建立時,經常會花時間編輯受影響物件
經常是為了從Excel檔案填寫到PLM系統上
此快速貼上並匯入變更單就因此而生

準備如下
1.自訂一個表單
2.在表單上的Import新增Click程式
3.在Action新增開啟表單程式

如下示意操作
https://ithelp.ithome.com.tw/upload/images/20210127/20106503psZONLBkhR.png
https://ithelp.ithome.com.tw/upload/images/20210127/20106503CjpI62BGh6.png
https://ithelp.ithome.com.tw/upload/images/20210127/20106503HqpJcaNZho.png
https://ithelp.ithome.com.tw/upload/images/20210127/20106503eFlT6YCMYa.png


1.新增一份表單,並設計出適合的介面,拉出一個按鈕且新增click程式,下面程式碼可參考自己的系統現況去調整
https://ithelp.ithome.com.tw/upload/images/20210127/20106503fJkjTIqzOd.png

var inn = new Innovator();
var prt = typeof(parent.document.item) == "object" ? parent.document : parent.parent;
var tmpThisItem = typeof(parent.document.thisItem) == "object" ? parent.document.thisItem : parent.thisItem;
var topWnd = aras.getMostTopWindowWithAras(window);
topWnd = topWnd.main || topWnd;

//console.log(topWnd.dialogArguments.cur_item);
var theItem = topWnd.cur_item;
var changesItemtype= theItem.getAttribute("type","");

//取得要匯入的文字框
var textarea_import = document.getElementsByName("txtBOM")[1];
var textarea_log = document.getElementsByName("txtLog")[1];
if(textarea_import.value==="" || textarea_import.value===undefined){
    aras.AlertError("Parts List is null.");
}

var logs = "";
//解析每一行的編號
var rows = textarea_import.value.split("\n");

for(var i=0;i<rows.length;i++){
    var part_num = rows[i];
    if(part_num==="") continue;
    //若為D_開頭編碼為Document或CAD
    if(part_num.substring(0,1)=='D'){
        var partItem = inn.newItem("Document","get");
            partItem.setProperty("item_number",part_num);
            partItem.setProperty("is_current","1");
            partItem = partItem.apply();
        if(partItem.isError()){
            partItem = inn.newItem("CAD","get");
            partItem.setProperty("item_number",part_num);
            partItem.setProperty("is_current","1");
            partItem = partItem.apply();
        }
        if(partItem.isError()==false){
            //若是草稿就新增料號
            if(partItem.getProperty("state","")==="Preliminary"){
                var changeItem = inn.newItem("Affected Item","add");
                    changeItem.setProperty("action","Add");
                    changeItem.setProperty("new_item_id",partItem.getID());
                    
                var affectItem = inn.newItem(changesItemtype+" Affected Item","add");
                    affectItem.setProperty("source_id",theItem.getID());
                    affectItem.setPropertyItem("related_id",changeItem);
                    affectItem = affectItem.apply();
                    if(affectItem.isError()){
                        logs+=part_num +" Error : "+affectItem.getErrorString()+"\n";
                    }else{
                        logs+=part_num +" OK.\n";
                    }
            }else{
                //反之則變更
                var changeItem = inn.newItem("Affected Item","add");
                    changeItem.setProperty("action","Change");
                    changeItem.setProperty("affected_id",partItem.getID());

                var affectItem = inn.newItem(changesItemtype+" Affected Item","add");
                    affectItem.setProperty("source_id",theItem.getID());
                    affectItem.setPropertyItem("related_id",changeItem);
                    affectItem = affectItem.apply();
                    if(affectItem.isError()){
                        logs+=part_num +" Error : "+affectItem.getErrorString()+"\n";
                    }else{
                        logs+=part_num +" OK.\n";
                    }
            }
        }else{
            logs+=part_num +" Error : "+partItem.getErrorString()+"\n";
        }
    }else{
        //搜尋該料號
        var partItem = inn.newItem("Part","get");
            partItem.setProperty("item_number",part_num);
            partItem.setProperty("is_current","1");
            partItem = partItem.apply();
        if(partItem.isError()){
            logs+=part_num +" Error : "+partItem.getErrorString()+"\n";
        }else{
            if(partItem.getProperty("state","")==="Preliminary"){
                var changeItem = inn.newItem("Affected Item","add");
                    changeItem.setProperty("action","Add");
                    changeItem.setProperty("new_item_id",partItem.getID());
                    
                var affectItem = inn.newItem(changesItemtype+" Affected Item","add");
                    affectItem.setProperty("source_id",theItem.getID());
                    affectItem.setPropertyItem("related_id",changeItem);
                    affectItem = affectItem.apply();
                    if(affectItem.isError()){
                        logs+=part_num +" Error : "+affectItem.getErrorString()+"\n";
                    }else{
                        logs+=part_num +" OK.\n";
                    }
            }else{
                var changeItem = inn.newItem("Affected Item","add");
                    changeItem.setProperty("action","Change");
                    changeItem.setProperty("affected_id",partItem.getID());

                var affectItem = inn.newItem(changesItemtype+" Affected Item","add");
                    affectItem.setProperty("source_id",theItem.getID());
                    affectItem.setPropertyItem("related_id",changeItem);
                    affectItem = affectItem.apply();
                    if(affectItem.isError()){
                        logs+=part_num +" Error : "+affectItem.getErrorString()+"\n";
                    }else{
                        logs+=part_num +" OK.\n";
                    }
            }
        }
    }
}
textarea_log.value = logs;

  1. 打開匯入視窗Action程式
    https://ithelp.ithome.com.tw/upload/images/20210127/20106503WZer7li5zn.png
var prt = typeof(parent.document.item) == "object" ? parent.document : parent.parent;
var tmpThisItem = typeof(parent.document.thisItem) == "object" ? parent.document.thisItem : parent.thisItem;

if(tmpThisItem.isNew()){
    aras.AlertError("Please save the item. 請先存檔並解鎖後再執行。");
}
if(tmpThisItem.isLocked()===1 || tmpThisItem.isLocked()===2){
    aras.AlertError("The item is Lock. 物件已上鎖中");
    return ;
}

if(tmpThisItem.getProperty("state","")==="New"){
    var topWnd = aras.getMostTopWindowWithAras(window);
    topWnd = topWnd.main || topWnd;


    // get form
    var formName = "JPC_ImportChangeData";
    var fetchForm = this.getInnovator().newItem("Form","get");
    fetchForm.setProperty("name",formName);
    fetchForm = fetchForm.apply("get"); 

    // check form for error
    if (fetchForm.isError())
        return alert("Can't find a form called " + formName);

    // get form params
    var params = {
        title: 'Parts Change Import', 
        formId: fetchForm.getID(),       // put your form's id here
        aras: aras,
        dialogWidth: 600,                // set width int
        dialogHeight: 400,               // set height int
        content: 'ShowFormAsADialog.html',
        cur_item : tmpThisItem
    };
    topWnd.cur_item = tmpThisItem;

    var callback = function(res) {
        //Perform logic using dialog result here
        var parentID = document.itemID;
        var parentAction = 'edit'; // or 'view'
        top.aras.uiReShowItem(parentID,parentID,parentAction,'tab view');
        return ;
    }
    // call form in dialog
    topWnd.ArasModules.MaximazableDialog.show("iframe", params).promise.then(callback);

    return null;
}else{
    aras.AlertError("Changes is not Preliminary.此非編輯系統狀態,不能執行。");
}


尚未有邦友留言

立即登入留言