public async Task<IHttpActionResult> PostSetCustomerAgent([FromBody]ParmSetCustomerAgent parm)
        {
            ResultInfo r = new ResultInfo();

            try
            {
                #region working a
                db0 = getDB0();
                var map = await db0.MapCustomerAgnet.FindAsync(parm.customer_id, parm.agent_id);

                if (map != null && parm.is_take == false)
                {
                    db0.MapCustomerAgnet.Remove(map);
                }

                if (map == null && parm.is_take == true)
                {
                    var add_map = new MapCustomerAgnet()
                    {
                        customer_id = parm.customer_id,
                        agent_id = parm.agent_id,
                        i_InsertUserID = this.UserId,
                        i_InsertDateTime = DateTime.Now,
                        i_InsertDeptID = this.departmentId,
                        i_Lang = "zh-TW"
                    };
                    db0.MapCustomerAgnet.Add(add_map);
                }
                await db0.SaveChangesAsync();

                r.result = true;

                return Ok(r);
                #endregion
            }
            catch (Exception ex)
            {
                r.result = false;
                r.message = ex.Message;
                return Ok(r);
            }
            finally
            {
                db0.Dispose();
            }
        }
        public async Task<IHttpActionResult> PostSetStockSelectCustomer([FromBody]ParmPostSetStockSelectCustomer parm)
        {
            try
            {
                #region working a
                db0 = getDB0();

                foreach (var p in parm.products)
                {
                    foreach (var c in parm.customers)
                    {
                        var md = new StockDetailQty()
                        {
                            stock_detail_qty_id = GetNewId(),
                            stock_detail_id = p.stock_detail_id,
                            customer_id = c.customer_id,
                            i_InsertUserID = this.UserId,
                            i_InsertDateTime = DateTime.Now,
                            i_InsertDeptID = this.departmentId,
                            i_Lang = "zh-TW"
                        };
                        db0.StockDetailQty.Add(md);

                    }
                }
                #region 新增經銷商對應
                foreach (var c in parm.customers)
                {
                    var exist = db0.MapCustomerAgnet.Any(x => x.agent_id == parm.agent_id && x.customer_id == c.customer_id);
                    if (!exist)
                    {
                        var item = new MapCustomerAgnet()
                        {
                            agent_id = parm.agent_id,
                            customer_id = c.customer_id,
                            i_InsertUserID = this.UserId,
                            i_InsertDateTime = DateTime.Now,
                            i_InsertDeptID = this.departmentId,
                            i_Lang = "zh-TW"
                        };
                        db0.MapCustomerAgnet.Add(item);
                    }
                }

                #endregion

                await db0.SaveChangesAsync();

                var getStockDeatil = db0.StockDetail.Where(x => x.stock_detail_id == parm.detail_id).First();

                var items = await db0.StockDetailQty.Where(x => x.StockDetail.stock_id == parm.stock_id && x.StockDetail.item_no == getStockDeatil.item_no)
                            .GroupBy(x => x.Customer)
                            .Select(x => new
                            {
                                customers = new { x.Key.customer_id, x.Key.customer_name },
                                products = x.Select(y => new { y.stock_detail_qty_id, y.StockDetail.product_id, y.qty })
                            })
                            .ToListAsync();

                return Ok(new { result = true, data = items });
                #endregion
            }
            catch (Exception ex)
            {
                return Ok(new { result = false, data = ex.Message });
            }
            finally
            {
                db0.Dispose();
            }
        }
        public async Task<IHttpActionResult> PostMapAgentCustomer([FromBody]ParmMapAgentCustomer parm)
        {
            ResultInfo r = new ResultInfo();

            try
            {
                #region working a
                db0 = getDB0();
                var item = db0.MapCustomerAgnet.Where(x => x.agent_id == parm.agent_id && x.customer_id == parm.customer_id).FirstOrDefault();
                if (item == null)
                {
                    item = new MapCustomerAgnet()
                    {
                        agent_id = parm.agent_id,
                        customer_id = parm.customer_id,
                        i_InsertUserID = this.UserId,
                        i_InsertDateTime = DateTime.Now,
                        i_InsertDeptID = this.departmentId,
                        i_Lang = "zh-TW"
                    };
                    db0.MapCustomerAgnet.Add(item);
                }

                await db0.SaveChangesAsync();

                r.result = true;
                r.id = item.agent_id;
                return Ok(r);
                #endregion
            }
            catch (Exception ex)
            {
                r.result = false;
                r.message = ex.Message;
                return Ok(r);
            }
            finally
            {
                db0.Dispose();
            }
        }