Пример #1
0
        public ActionResult Create(purchaseOrderHeader purchaseorderheader)
        {
            if (acm.cekSession() == false)
                return RedirectToAction("Logout", "Account");

            lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;
            if (acm.cekValidation(Url.Action().ToString()) == false && lvm.isAdmin == false)
                return RedirectToAction("NotAuthorized", "Account", new { menu = Url.Action().ToString() });

            //ViewBag.ddlVendor = DDLVendor(string.Empty);
            ViewBag.ddlVendor = DDLVendorByPRId(purchaseorderheader.prId);
            var xz = db.purchaseRequestHeaders.AsNoTracking().Where(x => !db.purchaseOrderHeaders.Any(y => y.prId == x.prId) && x.proposedStatus == true && x.approvedStatus == true)
                                //.Join(db.departments, a => a.requestDeptId, b => b.deptID, (a, b) => new { a, b }) ~old process ~sweeping department kebalik dgn divisi
                                .Join(db.divisis, a => a.requestDeptId, b => b.divisiID, (a, b) => new { a, b })
                                .Select(c => new { c.a.prId, c.a.requestDeptId, deptName = c.b.divisiName, c.a.typeOrder, c.a.proposalInclude, c.a.specialInstruction, c.a.projectTimeDelivery }).ToList();

            ViewData["PRPopUp"] = xz;

            var model = new purchaseOrderHeader();
            var editor = new purchaseOrderHeader.prPopUp()
            {
                prId = xz[0].prId,
                requestDeptId = xz[0].requestDeptId,
                deptName = xz[0].deptName,
                typeOrder = xz[0].typeOrder,
                proposalInclude = xz[0].proposalInclude,
                specialInstruction = xz[0].specialInstruction,
                projectTimeDelivery = xz[0].projectTimeDelivery
            };
            model.PopUpPR.Add(editor);
            ViewBag.PRPopUp = model.PopUpPR.ToList();
            
            var modelx = new purchaseRequestHeader();
            foreach (var a in db.purchaseRequestDetails.Where(x => x.prId == purchaseorderheader.prId).ToList() as IEnumerable<purchaseRequestHeader.purchaseRequestDetail>)
            {
                var editorx = new purchaseRequestHeader.purchaseRequestDetail()
                {
                    productId = a.productId,
                    description = a.description,
                    qty = a.qty,
                    unit = a.unit
                };
                modelx.detailPurchaseRequest.Add(editorx);
            }
            ViewData["dataDetail"] = modelx.detailPurchaseRequest.ToList();

            if (ModelState.IsValid)
            {
                var countChk = 0;
                var countChkMultiple = 0;
                bool isProposed = false;
                for (int i = 0; i < Request.Form.Count; i++)
                {
                    if (Request.Form.AllKeys.ToList()[i].Contains("txtProductID_"))
                        countChk++;
                    else if (Request.Form.AllKeys.ToList()[i].Contains("hdnTypeSubmit"))
                        if (Request.Form["hdnTypeSubmit"].ToString() == "requested")
                            isProposed = true;

                    if (purchaseorderheader.topType == "Multiple")
                        if (Request.Form.AllKeys.ToList()[i].Contains("txtBuyDate_"))
                            countChkMultiple++;
                }

                try
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        string generateID = prefix + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("d2");
                        var prIDCreated = db.purchaseOrderHeaders.Where(x => x.poId.Contains(generateID)).OrderByDescending(x => x.createdDate).Select(x => x.poId).ToList();
                        if (prIDCreated.Count == 0)
                        {
                            generateID = generateID + "0001";
                        }
                        else
                        {
                            generateID = generateID + (Convert.ToInt32(prIDCreated[0].Substring((prIDCreated[0].Length - 4))) + 1).ToString().PadLeft(4, '0');
                        }

                        #region ubah vendorID ke vendorCode
                        int idVendor = 0;
                        try{
                            idVendor = int.Parse(purchaseorderheader.vendorId) * 1;
                        }
                        catch
                        {

                        }

                        if(idVendor > 0 )
                        {
                            var ven = db.vendors.AsNoTracking().Where(x => x.id == idVendor).ToList();
                            purchaseorderheader.vendorId = ven[0].vendorID;
                        }
                        #endregion
                        purchaseorderheader.poId = generateID;
                        lvm = Session["sessionUserLogin"] as LoginViewModel.userLogin;
                        purchaseorderheader.createdDate = DateTime.Now;
                        purchaseorderheader.createdUser = lvm.userID;
                        purchaseorderheader.proposedBy = lvm.userID;
                        purchaseorderheader.proposedDate = DateTime.Now;
                        purchaseorderheader.proposedStatus = isProposed;
                        db.purchaseOrderHeaders.Add(purchaseorderheader);

                        #region insertDetail
                        for (int i = 1; i <= countChk; i++)
                        {
                            var colVal = Request.Form["txtProductID_" + i];
                            if (colVal == "")
                                break;

                            int t_productID = int.Parse(Request.Form["txtProductID_" + i].ToString());
                            int t_qty = Request.Form["txtQty_" + i].ToString() != "" ? Convert.ToInt32(Request.Form["txtQty_" + i].ToString().Replace('.', ',')) : 0;
                            decimal t_price = Request.Form["txtPrice_" + i].ToString() != "" ? Convert.ToDecimal(Convert.ToDouble(Request.Form["txtPrice_" + i].ToString().Replace('.', ','))) : 0;
                            decimal t_disc = Request.Form["txtDisc_" + i].ToString() != "" ? Convert.ToDecimal(Convert.ToDouble(Request.Form["txtDisc_" + i].ToString().Replace('.', ','))) : 0;

                            var editorx = new purchaseOrderHeader.purchaseOrderDetail()
                            {
                                poId = purchaseorderheader.poId,
                                productID = t_productID,
                                qty = t_qty,
                                price = t_price,
                                disc = t_disc
                            };
                            db.purchaseOrderDetails.Add(editorx);
                        }
                        #endregion

                        #region insertTOPMultiple
                        if(purchaseorderheader.topType == "Multiple")
                        { 
                            for (int i = 0; i < countChkMultiple; i++)
                            {
                                var colVal = Request.Form["txtBuyDate_" + i];
                                if (colVal == "")
                                    break;

                                DateTime t_BuyDate = Convert.ToDateTime(Request.Form["txtBuyDate_" + i].ToString());
                                decimal t_BuyPercent = Request.Form["txtBuyPercent_" + i].ToString() != "" ? Convert.ToDecimal(Convert.ToDouble(Request.Form["txtBuyPercent_" + i].ToString().Replace('.', ','))) : 0;

                                var editort = new purchaseOrderHeader.purchaseOrderTop()
                                {
                                    poId = purchaseorderheader.poId,
                                    buyDate = t_BuyDate,
                                    buyPercent = t_BuyPercent
                                };
                                db.purchaseOrderTOPs.Add(editort);
                            }
                        }
                        #endregion

                        db.SaveChanges();
                        ts.Complete();
                    }

                    return RedirectToAction("Index");
                }
                catch (Exception exc)
                {
                    string a = exc.Message;
                }
            }
            return View(purchaseorderheader);
        }
Пример #2
0
        public void getListPR(string keywordId, string keywordSearch, int? maxRows, int? pageStart, bool isResultAll)
        {
            DataTable dtPRforPO = new DataTable();
            DataTable dtPRforPOAll = new DataTable();
            using (SqlConnection conn = new SqlConnection(con.conString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter();


                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "[dbo].[sp_pr_for_po]";
                cmd.Parameters.AddWithValue("@keywordId",string.IsNullOrEmpty(keywordId) ? (object)DBNull.Value : keywordId);
                cmd.Parameters.AddWithValue("@keywordSearch",string.IsNullOrEmpty(keywordSearch) ? (object)DBNull.Value : keywordSearch);
                cmd.Parameters.AddWithValue("@maxRows", maxRows == null ? (object)DBNull.Value : maxRows);
                cmd.Parameters.AddWithValue("@pageStart", pageStart == null ? (object)DBNull.Value : pageStart);
                cmd.Parameters.AddWithValue("@isResultAll", false);

                conn.Open();
                da.SelectCommand = cmd;
                da.Fill(dtPRforPO);
            }

            using (SqlConnection conn = new SqlConnection(con.conString))
            {
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter();


                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "[dbo].[sp_pr_for_po]";
                cmd.Parameters.AddWithValue("@keywordId", string.IsNullOrEmpty(keywordId) ? (object)DBNull.Value : keywordId);
                cmd.Parameters.AddWithValue("@keywordSearch", string.IsNullOrEmpty(keywordSearch) ? (object)DBNull.Value : keywordSearch);
                cmd.Parameters.AddWithValue("@maxRows", maxRows == null ? (object)DBNull.Value : maxRows);
                cmd.Parameters.AddWithValue("@pageStart", pageStart == null ? (object)DBNull.Value : pageStart);
                cmd.Parameters.AddWithValue("@isResultAll", true);

                conn.Open();
                da.SelectCommand = cmd;
                da.Fill(dtPRforPOAll);
            }

            var model = new purchaseOrderHeader();
            foreach(DataRow dr in dtPRforPO.Rows)
            {
                var editor = new purchaseOrderHeader.prPopUp()
                {
                    prId = dr["prId"].ToString(),
                    requestDeptId = int.Parse(dr["requestDeptId"].ToString()),
                    deptName = dr["deptName"].ToString(),
                    typeOrder = dr["typeOrder"].ToString(),
                    proposalInclude = bool.Parse(dr["proposalInclude"].ToString()),
                    specialInstruction = dr["specialInstruction"].ToString(),
                    projectTimeDelivery = dr["projectTimeDelivery"].ToString()
                };
                model.PopUpPR.Add(editor);
            }
            ViewBag.PRPopUp = model.PopUpPR.ToList();
            ViewBag.totalRows = dtPRforPOAll.Rows.Count; 
            ViewBag.currentPage = pageStart;
        }