//儲存日報數量紀錄
        public void saveItemRow(FormCollection f)
        {
            SYS_USER u = (SYS_USER)Session["user"];

            log.Debug("projectId=" + Request["txtProjectId"] + ",prjUid=" + Request["txtPrjUid"] + ",ReportId=" + Request["reportID"]);
            log.Debug("form Data ItemId=" + Request["planItemId"]);
            log.Debug("form Data Qty=planItemQty" + Request["planItemQty"]);

            string projectid      = Request["txtProjectId"];
            int    prjuid         = int.Parse(Request["txtPrjUid"]);
            string strWeather     = Request["selWeather"];
            string strSummary     = Request["txtSummary"];
            string strSenceUser   = Request["txtSenceUser"];
            string strSupervision = Request["txtSupervision"];
            string strOwner       = Request["txtOwner"];
            string strRptDate     = Request["reportDate"];

            DailyReport newDailyRpt = new DailyReport();

            PLAN_DALIY_REPORT RptHeader = new PLAN_DALIY_REPORT();

            RptHeader.PROJECT_ID       = projectid;
            RptHeader.WEATHER          = strWeather;
            RptHeader.SUMMARY          = strSummary;
            RptHeader.SCENE_USER_NAME  = strSenceUser;
            RptHeader.SUPERVISION_NAME = strSupervision;
            RptHeader.OWNER_NAME       = strOwner;
            newDailyRpt.dailyRpt       = RptHeader;
            RptHeader.REPORT_DATE      = DateTime.Parse(strRptDate);
            //取得日報編號
            SerialKeyService snService = new SerialKeyService();

            if (null == Request["reportID"] || "" == Request["reportID"])
            {
                RptHeader.REPORT_ID      = snService.getSerialKey(planService.KEY_ID);
                RptHeader.CREATE_DATE    = DateTime.Now;
                RptHeader.CREATE_USER_ID = u.USER_ID;
            }
            else
            {
                RptHeader.REPORT_ID      = Request["ReportID"];
                RptHeader.CREATE_DATE    = DateTime.Parse(Request["txtCreateDate"]);
                RptHeader.CREATE_USER_ID = Request["txtCreateUserId"];
                RptHeader.MODIFY_DATE    = DateTime.Now;
                RptHeader.MODIFY_USER_ID = u.USER_ID;
            }

            //建立專案任務資料 (結構是支援多項任務,僅先使用一筆)
            newDailyRpt.lstRptTask = new List <PLAN_DR_TASK>();
            PLAN_DR_TASK RptTask = new PLAN_DR_TASK();

            RptTask.PROJECT_ID = projectid;
            RptTask.PRJ_UID    = prjuid;
            RptTask.REPORT_ID  = RptHeader.REPORT_ID;
            newDailyRpt.lstRptTask.Add(RptTask);
            //處理料件
            newDailyRpt.lstRptItem = new List <PLAN_DR_ITEM>();
            if (null != Request["planItemId"])
            {
                string[] aryPlanItem      = Request["planItemId"].Split(',');
                string[] aryPlanItemQty   = Request["planItemQty"].Split(',');
                string[] aryAccumulateQty = Request["accumulateQty"].Split(',');

                log.Debug("count ItemiD=" + aryPlanItem.Length + ",qty=" + aryPlanItemQty.Length);
                newDailyRpt.lstRptItem = new List <PLAN_DR_ITEM>();
                for (int i = 0; i < aryPlanItem.Length; i++)
                {
                    PLAN_DR_ITEM item = new PLAN_DR_ITEM();
                    item.PLAN_ITEM_ID = aryPlanItem[i];
                    item.PROJECT_ID   = projectid;
                    item.REPORT_ID    = RptHeader.REPORT_ID;
                    if ("" != aryPlanItemQty[i])
                    {
                        item.FINISH_QTY = decimal.Parse(aryPlanItemQty[i]);
                    }
                    if ("" != aryAccumulateQty[i])
                    {
                        item.LAST_QTY = decimal.Parse(aryAccumulateQty[i]);
                    }
                    newDailyRpt.lstRptItem.Add(item);
                }
            }
            //處理出工資料
            newDailyRpt.lstWokerType4Show = new List <PLAN_DR_WORKER>();
            if (null != Request["txtSupplierId"])
            {
                ///出工廠商
                string[] arySupplier = Request["txtSupplierId"].Split(',');
                ///出工人數
                string[] aryWorkerQty = Request["txtWorkerQty"].Split(',');
                ///備註
                string[] aryRemark = Request["txtRemark"].Split(',');
                for (int i = 0; i < arySupplier.Length; i++)
                {
                    PLAN_DR_WORKER item = new PLAN_DR_WORKER();
                    item.REPORT_ID   = RptHeader.REPORT_ID;
                    item.SUPPLIER_ID = arySupplier[i];
                    log.Debug("Supplier Info=" + item.SUPPLIER_ID);
                    if ("" != aryWorkerQty[i].Trim())
                    {
                        item.WORKER_QTY = decimal.Parse(aryWorkerQty[i]);
                        newDailyRpt.lstWokerType4Show.Add(item);
                    }
                    item.REMARK = aryRemark[i];
                }
                log.Debug("count WorkerD=" + arySupplier.Length);
            }
            //處理點工資料
            newDailyRpt.lstTempWoker4Show = new List <PLAN_DR_TEMPWORK>();
            if (null != Request["txtTempWorkSupplierId"])
            {
                string[] aryTempSupplier       = Request["txtTempWorkSupplierId"].Split(',');
                string[] aryTempWorkerQty      = Request["txtTempWorkerQty"].Split(',');
                string[] aryTempChargeSupplier = Request["txtChargeSupplierId"].Split(',');
                string[] aryTempWarkRemark     = Request["txtTempWorkRemark"].Split(',');
                string[] aryDoc = Request["doc"].Split(',');
                for (int i = 0; i < aryTempSupplier.Length; i++)
                {
                    PLAN_DR_TEMPWORK item = new PLAN_DR_TEMPWORK();
                    item.REPORT_ID   = RptHeader.REPORT_ID;
                    item.SUPPLIER_ID = aryTempSupplier[i];
                    item.CHARGE_ID   = aryTempChargeSupplier[i];
                    ///處理工人名單檔案
                    log.Debug("File Count=" + Request.Files.Count);
                    if (Request.Files[i].ContentLength > 0)
                    {
                        //存檔路徑
                        var    fileName     = Path.GetFileName(Request.Files[i].FileName);
                        string reportFolder = ContextService.strUploadPath + "\\" + projectid + "\\DailyReport\\";
                        //check 資料夾是否存在
                        string folder = reportFolder + RptHeader.REPORT_ID;
                        ZipFileCreator.CreateDirectory(folder);
                        var path = Path.Combine(folder, fileName);
                        Request.Files[i].SaveAs(path);
                        item.DOC = "DailyReport\\" + RptHeader.REPORT_ID + "\\" + fileName;
                        log.Debug("Upload Sign List File:" + Request.Files[i].FileName);
                    }
                    else
                    {
                        item.DOC = aryDoc[i];
                        log.Error("Not Upload Sign List File!Exist File=" + item.DOC);
                    }
                    log.Debug("Supplier Info=" + item.SUPPLIER_ID);
                    if ("" != aryTempWorkerQty[i].Trim())
                    {
                        item.WORKER_QTY = decimal.Parse(aryTempWorkerQty[i]);
                        newDailyRpt.lstTempWoker4Show.Add(item);
                    }
                    item.REMARK = aryTempWarkRemark[i];
                }
            }
            //處理機具資料
            newDailyRpt.lstRptWorkerAndMachine = new List <PLAN_DR_WORKER>();
            string[] aryMachineType = f["MachineKeyid"].Split(',');
            string[] aryMachineQty  = f["planMachineQty"].Split(',');
            for (int i = 0; i < aryMachineType.Length; i++)
            {
                PLAN_DR_WORKER item = new PLAN_DR_WORKER();
                item.REPORT_ID   = RptHeader.REPORT_ID;
                item.WORKER_TYPE = "MACHINE";
                item.PARA_KEY_ID = aryMachineType[i];
                if ("" != aryMachineQty[i])
                {
                    item.WORKER_QTY = decimal.Parse(aryMachineQty[i]);
                    newDailyRpt.lstRptWorkerAndMachine.Add(item);
                }
            }
            log.Debug("count MachineD=" + f["MachineKeyid"] + ",WorkerQty=" + f["planMachineQty"]);
            //處理重要事項資料
            newDailyRpt.lstRptNote = new List <PLAN_DR_NOTE>();
            string[] aryNote = f["planNote"].Split(',');
            for (int i = 0; i < aryNote.Length; i++)
            {
                PLAN_DR_NOTE item = new PLAN_DR_NOTE();
                item.REPORT_ID = RptHeader.REPORT_ID;
                if ("" != aryNote[i].Trim())
                {
                    item.SORT   = i + 1;
                    item.REMARK = aryNote[i].Trim();
                    newDailyRpt.lstRptNote.Add(item);
                }
            }
            //註記任務是否完成
            if (null == Request["taskDone"])
            {
                newDailyRpt.isDoneFlag = false;
            }
            else
            {
                newDailyRpt.isDoneFlag = true;
            }
            log.Debug("count Note=" + f["planNote"]);
            string msg = planService.createDailyReport(newDailyRpt);

            Response.Redirect("~/ProjectPlan/dailyReport/" + projectid);
            //ProjectPlan/dailyReport/P00061
        }
        public ActionResult AddItem(FormCollection f)
        {
            var inquiry = new topmeperp.Models.PLAN_SUP_INQUIRY();
            var formID  = Request["formID"];
            var order   = new topmeperp.Models.PLAN_CERT_ORDER();

            SerialKeyService snoservice = new SerialKeyService();
            string           ordKey     = snoservice.getSerialKey("EST");

            using (var context = new topmepEntities())
            {
                string sql;
                inquiry = context.PLAN_SUP_INQUIRY.SqlQuery("SELECT * FROM PLAN_SUP_INQUIRY WHERE INQUIRY_FORM_ID = '" + formID + "'").ToList().First();
                sql     = "INSERT INTO PLAN_CERT_ORDER (CERT_ORD_ID,INQUIRY_FORM_ID,SUPPLIER_ID,PROJECT_ID,CREATE_DATE) " +
                          "VALUES('" + ordKey + "','" + inquiry.INQUIRY_FORM_ID + "-" + ordKey + "','" + inquiry.SUPPLIER_ID + "','" + inquiry.PROJECT_ID + "','" + DateTime.Now.ToString() + "')";
                try
                {
                    //order.CERT_ORD_ID = "test";
                    //order.INQUIRY_FORM_ID = inquiry.INQUIRY_FORM_ID.ToString();
                    //order.SUPPLIER_ID = inquiry.SUPPLIER_ID.ToString();
                    //order.PROJECT_ID = inquiry.PROJECT_ID.ToString();
                    //context.PLAN_CERT_ORDER.Add(order);
                    context.Database.ExecuteSqlCommand(sql);
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.Write(e.StackTrace);
                }

                var qty          = Request["qty"];
                var plan_item_id = Request["plan_item_id"];
                var type         = Request["type"];
                var subType      = Request["subType"];
                var itemID       = Request["itemID"];
                var desc         = Request["desc"];

                int[]    nums          = Array.ConvertAll(qty.Split(','), int.Parse);
                string[] plan_item_ids = plan_item_id.Split(',');
                string[] types         = type.Split(',');
                string[] subTypes      = subType.Split(',');
                string[] itemIDs       = itemID.Split(',');
                string[] descs         = desc.Split(',');

                //var test = new PLAN_CERT_ORDER_ITEM { CERT_ORD_ITEM_ID = generate_id(), ORDER_QTY = nums[0] };
                var test2 = new PLAN_CERT_ORDER_ITEM {
                    CERT_ORD_ITEM_ID = generate_id(), ORDER_QTY = nums[1]
                };

                //string sql = "INSERT INTO PLAN_CERT_ORDER_ITEM (CERT_ORD_ITEM_ID,ORDER_QTY) VALUES(28," + "10" + ")";
                try
                {
                    for (int i = 0; i < nums.Length; i++)
                    {
                        context.PLAN_CERT_ORDER_ITEM.Add(new PLAN_CERT_ORDER_ITEM
                        {
                            CERT_ORD_ITEM_ID = generate_id(),
                            CERT_ORD_ID      = ordKey,
                            TYPE_CODE        = types[i],
                            SUB_TYPE_CODE    = subTypes[i],
                            ITEM_ID          = itemIDs[i],
                            ITEM_DESC        = descs[i],
                            ORDER_QTY        = nums[i],
                            PLAN_ITEM_ID     = plan_item_ids[i]
                        });
                        string updateSql = "UPDATE PLAN_SUP_INQUIRY_ITEM SET ACCUMULATE_QTY = ACCUMULATE_QTY + "
                                           + nums[i] + " WHERE INQUIRY_FORM_ID = '" + formID + "' and PLAN_ITEM_ID = '" + plan_item_ids[i] + "'";
                        context.Database.ExecuteSqlCommand(updateSql);
                    }
                    //context.PLAN_CERT_ORDER_ITEM.Add(test2);
                    //var x=0;
                    //context.Database.ExecuteSqlCommand(sql);
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.Write(e.StackTrace);
                }



                //generate random 64 int ID
                long testID = generate_id();

                string orderinfo = "{CERT_OrderID: " + order.CERT_ORD_ID + " FormID: " + inquiry.INQUIRY_FORM_ID + " Supplier: " + inquiry.SUPPLIER_ID + " ProjID: " + inquiry.PROJECT_ID;
                //return "Item added in " + formID + "\n" + nums[0] + ", " + nums[1] + sql+"\n("+testID+":"+ordKey+")"+plan_item_ids[0];
                return(RedirectToAction("Index/" + inquiry.PROJECT_ID));
            }
        }