Exemplo n.º 1
0
        public ResultAPI SaveData(latReceive_Save data)
        {
            latReceive_Save result = new latReceive_Save();

            db = new PTTGSP_DWSContext();
            CultureInfo TH = new CultureInfo("th-TH");
            CultureInfo EN = new CultureInfo("en-US");

            DateTime dt = DateTime.Now;

            if (data.nRequestID == 0)
            {
                int nRequestID = data.nRequestID;
                var Update     = db.TB_Goods_Receive.FirstOrDefault(w => w.nRequestID == nRequestID);
                if (Update == null)
                {
                    string RequestNo = "";
                    var    nID_Last  = db.TB_Goods_Receive.Any() ? db.TB_Goods_Receive.Max(m => m.nRequestID) : 0;
                    var    GenCode   = db.TB_Goods_Receive.Where(w => w.dCreate.Month == DateTime.Now.Month && w.dCreate.Year == DateTime.Now.Year).Count();
                    if (GenCode != 0)
                    {
                        string plusone = (GenCode + 1) + "";
                        RequestNo = plusone.PadLeft(4, '0');
                    }
                    else
                    {
                        RequestNo = "0001";
                    }
                    string sYear      = dt.ToString("yy", TH);
                    string sdate      = dt.ToString("dd", TH);
                    string sRequestNo = "GR-" + sYear + sdate + RequestNo;

                    Update = new TB_Goods_Receive()
                    {
                        nRequestID = nID_Last + 1,
                        sRequestNo = sRequestNo,
                        sCreateBy  = "",
                        dUpdate    = DateTime.Now,
                        sUpdateBy  = "",
                        dCreate    = DateTime.Now
                    };

                    db.TB_Goods_Receive.Add(Update);
                }
                Update.nSourceID = data.sSourceID.ToIntOrZero();
                Update.sRefNo    = data.sRefNo;

                Update.dDocDate     = Convert.ToDateTime(data.sDocDate, cultures);
                Update.dReceiveDate = Convert.ToDateTime(data.sReceiveDate, cultures);
                Update.nVendorID    = data.sVendorID.ToIntOrZero();
                Update.sNote        = data.sNote;
                Update.nTotalPrice  = data.nTotalPrice.ToIntOrZero();
                Update.dCreate      = DateTime.Now;
                Update.sUpdateBy    = "";

                foreach (var item in data.file)
                {
                    if (item.IsNewFile)
                    {
                        string sNewPath        = "Good_Receive/Good_Receive_" + Update.nRequestID;
                        string sTepmPath       = "Temp";
                        string foldersTempPath = MapCurrentPath(sTepmPath + "/");
                        string foldersNewPath  = MapCurrentPath(sNewPath + "/");
                        string sNewTemp        = "UploadFile/" + sNewPath + "/" + item.sSaveToFileName;

                        Update.sFile_Name    = item.sFileName;
                        Update.sFile_SysName = item.sSaveToFileName;
                        Update.sFile_Path    = "/" + sNewTemp;

                        SysFunc.FolderCreate(sNewPath);

                        string OldTemp = foldersTempPath + item.sSaveToFileName;
                        string NewTemp = foldersNewPath + item.sSaveToFileName;

                        System.IO.File.Move(OldTemp, NewTemp);
                    }
                }
                foreach (var i in data.sub_TB)
                {
                    TB_Goods_Receive_Material s = new TB_Goods_Receive_Material();
                    s.nRequestID  = Update.nRequestID;
                    s.nMaterialID = i.nMaterialID;
                    s.nAmount     = i.nNumber.ToIntOrZero();
                    s.nPrice      = i.nPriceperpiece.ToDecimalOrZero();
                    s.nTotalPrice = i.nSum;

                    db.TB_Goods_Receive_Material.Add(s);
                }

                db.SaveChanges();
            }
            result.Status = ResultStatus.Success;
            return(result);
        }
Exemplo n.º 2
0
        public ActionResult SaveData(SaveSupplies data)
        {
            db = new PTTGSP_DWSContext();
            SaveSupplies result      = new SaveSupplies();
            int          nMaterialID = data.nMaterialID.ToIntOrZero();
            var          ChackData   = db.TB_Materials.FirstOrDefault(f => f.nMaterialID == nMaterialID);

            if (ChackData != null) //Update
            {
                ChackData.sName            = data.sName;
                ChackData.nUnitID          = data.nUnitID.ToIntOrZero();
                ChackData.nPrice           = data.nPrice.ToDecimalOrZero();
                ChackData.nLocationID      = data.nLocationID.ToIntOrZero();
                ChackData.sBinLocation     = data.sBinLocation;
                ChackData.nMin             = data.nMin.ToIntOrZero();
                ChackData.nMax             = data.nMax.ToIntOrZero();
                ChackData.nReOrderPoint    = data.nReOrderPoint.ToIntOrZero();
                ChackData.nMaxReserve      = data.nMaxReserve.ToIntOrZero();
                ChackData.nDuplicateDay    = data.nDuplicateDay.ToIntOrZero();
                ChackData.sDetail_Material = data.sDetail_Material;
                ChackData.sDetail          = data.sDetail;
                ChackData.IsActive         = data.IsActive;

                foreach (var item in data.file)
                {
                    if (item.IsNewFile)
                    {
                        string sNewPath        = "Supplies";
                        string sTepmPath       = "Temp";
                        string foldersTempPath = MapCurrentPath(sTepmPath + "/");
                        string foldersNewPath  = MapCurrentPath(sNewPath + "/");
                        string sNewTemp        = "UploadFile/" + sNewPath + "/" + item.sSaveToFileName;

                        ChackData.sFile_Name    = item.sFileName;
                        ChackData.sFile_SysName = item.sSaveToFileName;
                        ChackData.sFile_Path    = "/" + sNewTemp;

                        SysFunc.FolderCreate(sNewPath);

                        string OldTemp = foldersTempPath + item.sSaveToFileName;
                        string NewTemp = foldersNewPath + item.sSaveToFileName;

                        System.IO.File.Move(OldTemp, NewTemp);
                    }
                }

                ChackData.dUpdate   = DateTime.Now;
                ChackData.sUpdateBy = "";

                if (data.listfile.Any())
                {
                    foreach (var item in data.listfile)
                    {
                        if (item.IsNewFile)
                        {
                            string sNewPath        = "Supplies/Supplies_" + nMaterialID;
                            string sTepmPath       = "Temp";
                            string foldersTempPath = MapCurrentPath(sTepmPath + "/");
                            string foldersNewPath  = MapCurrentPath(sNewPath + "/");
                            string sNewTemp        = "UploadFile/" + sNewPath + "/" + item.sSaveToFileName;

                            var Countfile = db.TB_Materials_File.Where(w => w.nMaterialID == nMaterialID).Count();
                            Countfile = Countfile + 1;

                            TB_Materials_File f = new TB_Materials_File();
                            f.nMaterialID   = nMaterialID;
                            f.nItem         = Countfile;
                            f.sFile_Name    = item.sFileName;
                            f.sFile_Path    = "/" + sNewTemp;
                            f.sFile_SysName = item.sSaveToFileName;

                            SysFunc.FolderCreate(sNewPath);
                            string OldTemp = foldersTempPath + item.sSaveToFileName;
                            string NewTemp = foldersNewPath + item.sSaveToFileName;

                            System.IO.File.Move(OldTemp, NewTemp);

                            db.TB_Materials_File.Add(f);
                        }
                    }
                }
                db.SaveChanges();
            }
            else //create
            {
                int CategoryID = data.nCategoryID.ToIntOrZero();
                var CreateCode = db.TB_Material_Category.FirstOrDefault(w => w.nCategoryID == CategoryID);
                if (CreateCode != null)
                {
                    string MaterialCode  = "";
                    var    CountCategory = db.TB_Materials.Where(w => w.nCategoryID == CreateCode.nCategoryID).Count();
                    if (CountCategory != 0)
                    {
                        string plusone = (CountCategory + 1) + "";
                        MaterialCode = plusone.PadLeft(3, '0');
                    }
                    else
                    {
                        MaterialCode = "001";
                    }

                    TB_Materials s = new TB_Materials();
                    s.sMaterialCode    = CreateCode.sCategoryCode + "-" + MaterialCode;
                    s.nGroupID         = data.nGroupID.ToIntOrZero();
                    s.nCategoryID      = data.nCategoryID.ToIntOrZero();
                    s.sName            = data.sName;
                    s.nUnitID          = data.nUnitID.ToIntOrZero();
                    s.nPrice           = data.nPrice.ToDecimalOrZero();
                    s.nLocationID      = data.nLocationID.ToIntOrZero();
                    s.sBinLocation     = data.sBinLocation;
                    s.nMin             = data.nMin.ToIntOrZero();
                    s.nMax             = data.nMax.ToIntOrZero();
                    s.nReOrderPoint    = data.nReOrderPoint.ToIntOrZero();
                    s.nMaxReserve      = data.nMaxReserve.ToIntOrZero();
                    s.nDuplicateDay    = data.nDuplicateDay.ToIntOrZero();
                    s.sDetail_Material = data.sDetail_Material;
                    s.sDetail          = data.sDetail;

                    foreach (var item in data.file)
                    {
                        string sNewPath        = "Supplies";
                        string sTepmPath       = "Temp";
                        string foldersTempPath = MapCurrentPath(sTepmPath + "/");
                        string foldersNewPath  = MapCurrentPath(sNewPath + "/");
                        string sNewTemp        = "UploadFile/" + sNewPath + "/" + item.sSaveToFileName;

                        s.sFile_Name    = item.sFileName;
                        s.sFile_SysName = item.sSaveToFileName;
                        s.sFile_Path    = "/" + sNewTemp;

                        if (item.IsNewFile)
                        {
                            SysFunc.FolderCreate(sNewPath);
                            string OldTemp = foldersTempPath + item.sSaveToFileName;
                            string NewTemp = foldersNewPath + item.sSaveToFileName;
                            System.IO.File.Move(OldTemp, NewTemp);
                        }
                    }
                    s.IsActive  = data.IsActive;
                    s.dCreate   = DateTime.Now;
                    s.dUpdate   = DateTime.Now;
                    s.sCreateBy = "";
                    s.sUpdateBy = "";
                    db.TB_Materials.Add(s);
                    db.SaveChanges();

                    if (data.listfile.Any())
                    {
                        db = new PTTGSP_DWSContext();
                        int Countfile = 1;
                        foreach (var item in data.listfile)
                        {
                            string sNewPath        = "Supplies/Supplies_" + s.nMaterialID;
                            string sTepmPath       = "Temp";
                            string foldersTempPath = MapCurrentPath(sTepmPath + "/");
                            string foldersNewPath  = MapCurrentPath(sNewPath + "/");
                            string sNewTemp        = "UploadFile/" + sNewPath + "/" + item.sSaveToFileName;

                            TB_Materials_File f = new TB_Materials_File();
                            f.nMaterialID   = s.nMaterialID;
                            f.nItem         = Countfile;
                            f.sFile_Name    = item.sFileName;
                            f.sFile_Path    = "/" + sNewTemp;
                            f.sFile_SysName = item.sSaveToFileName;
                            Countfile++;

                            if (item.IsNewFile)
                            {
                                SysFunc.FolderCreate(sNewPath);
                                string OldTemp = foldersTempPath + item.sSaveToFileName;
                                string NewTemp = foldersNewPath + item.sSaveToFileName;

                                System.IO.File.Move(OldTemp, NewTemp);
                            }
                            db.TB_Materials_File.Add(f);
                        }
                        db.SaveChanges();
                    }
                    db = new PTTGSP_DWSContext();
                    TB_Materials_BP BP = new TB_Materials_BP();
                    BP.nMaterialID  = s.nMaterialID;
                    BP.nMat_Balance = 0;
                    BP.nMat_Pending = 0;
                    BP.nMat_BP      = 0;
                    db.TB_Materials_BP.Add(BP);
                    db.SaveChanges();
                }
            }
            result.Status = ResultStatus.Success;
            return(Ok(result));
        }