Пример #1
0
        public int Save(ChannelShipping chs)
        {
            chs.Replace4MySQL();
            strSql = string.Format(@"insert into channel_shipping(channel_id,shipping_carrior,n_threshold,l_threshold,n_fee,l_fee,n_return_fee,l_return_fee,shipco,retrieve_mode,createdate,updatedate) 
                values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", chs.channel_id, chs.shipping_carrior, chs.n_threshold, chs.l_threshold, chs.n_fee, chs.l_fee, chs.n_return_fee, chs.l_return_fee, chs.shipco, chs.retrieve_mode, chs.createdate.ToString("yyyy/MM/dd hh:mm:ss"), chs.updatedate.ToString("yyyy/MM/dd hh:mm:ss"));

            return _accessMySql.execCommand(strSql);
        }
Пример #2
0
        public int Edit(ChannelShipping chs, int shippingCarrior,string shipco_content)
        {
            chs.Replace4MySQL();
            shipco_content = Common.CommonFunction.StringTransfer(shipco_content);
            strSql = string.Format(@"update channel_shipping set shipping_carrior='{0}',n_threshold='{1}',l_threshold='{2}',n_fee='{3}',l_fee='{4}',n_return_fee='{5}',l_return_fee='{6}',shipco='{7}',retrieve_mode='{10}',updatedate = '{11}'
                where channel_id='{8}' and shipping_carrior='{9}' and shipco='{12}'", chs.shipping_carrior, chs.n_threshold, chs.l_threshold, chs.n_fee, chs.l_fee, chs.n_return_fee, chs.l_return_fee,
                chs.shipco, chs.channel_id, shippingCarrior, chs.retrieve_mode, chs.updatedate.ToString("yyyy/MM/dd hh:mm:ss"), shipco_content);

            return _accessMySql.execCommand(strSql);
        }
Пример #3
0
 public List<ChannelShipping> Query(ChannelShipping channelShipping)
 {
     try
     {
         return _chsDao.Query(channelShipping);
     }
     catch (Exception ex)
     {
         throw new Exception("ChannelShippingMgr-->Query(ChannelShipping channelShipping)-->" + ex.Message, ex);
     }
     
 }
Пример #4
0
        public List<ChannelShipping> Query(ChannelShipping channelShipping)
        {
            channelShipping.Replace4MySQL();
            StringBuilder strSql = new StringBuilder("select channel_id,shipping_carrior,shipco,n_threshold,n_fee,n_return_fee,l_threshold,l_fee,");
            strSql.Append("l_return_fee,createdate,updatedate,retrieve_mode from channel_shipping where 1=1 ");

            if (channelShipping.channel_id != 0)
            {
                strSql.AppendFormat(" and channel_id={0}", channelShipping.channel_id);
            }
            if (!string.IsNullOrEmpty(channelShipping.shipco))
            {
                strSql.AppendFormat(" and shipco='{0}'", channelShipping.shipco);
            }
            if (channelShipping.shipping_carrior != 0)
            {
                strSql.AppendFormat(" and shipping_carrior={0}",channelShipping.shipping_carrior);
            }

            return _accessMySql.getDataTableForObj<ChannelShipping>(strSql.ToString());
        }
Пример #5
0
 public int Save(ChannelShipping chs)
 {
     
     try
     {
         return _chsDao.Save(chs);
     }
     catch (Exception ex)
     {
         throw new Exception("ChannelShippingMgr-->SingleCompareSave-->" + ex.Message, ex);
     }
 }
Пример #6
0
 public int Edit(ChannelShipping chs, int shippingCarrior,string shipco_content)
 {
     
     try
     {
         return _chsDao.Edit(chs, shippingCarrior, shipco_content);
     }
     catch (Exception ex)
     {
         throw new Exception("ChannelShippingMgr-->Edit-->" + ex.Message, ex);
     }
 }
Пример #7
0
        public HttpResponseBase OrderSave()
        {
            int orderType = Request.UrlReferrer.AbsolutePath == "/Order/InteriorOrderAdd" ? 2 : 1;//訂單類型,:1為普通訂單輸入,2為內部訂單輸入
            string jsonStr = "{success:false,msg:'" + Resources.OrderAdd.ORDER_ADD_FAIL + "'}";
            try
            {
                Caller _caller = (Session["caller"] as Caller);

                uint normalFright = 0, lowFright = 0, receipt_to = 0, productTotalPrice = 0, combChannelId = 0, combPayMent = 0, combStoreMode = 0, combOrderStatus = 0, storeType = 0, retrieve_mode = 0;
                string combOrderDate = "", combLatestDeliverDate = "", txtAdminNote = "", txtCartNote = "", txtTradeNumber = "", txtOrderId = "", txtDeliverNumber = "";
                uint.TryParse(Request.Params["retrieve_mode"] ?? "0", out retrieve_mode);
                uint.TryParse(Request.Params["receipt_to"] ?? "0", out receipt_to);
                uint.TryParse(Request.Params["normalFright"] ?? "0", out normalFright);
                uint.TryParse(Request.Params["lowFright"] ?? "0", out lowFright);
                uint.TryParse(Request.Params["comboPrice"] ?? "0", out productTotalPrice);
                uint.TryParse(Request.Params["combChannelId"] ?? "0", out combChannelId);
                uint.TryParse(Request.Params["combPayMent"] ?? "0", out combPayMent);//付款方式
                uint.TryParse(Request.Params["combStoreMode"] ?? "0", out combStoreMode);
                uint.TryParse(Request.Params["combOrderStatus"] ?? "0", out combOrderStatus);//訂單狀態
                uint.TryParse(Request.Params["storeType"] ?? "0", out storeType);

                combOrderDate = Request.Params["combOrderDate"];
                //txtOrderDateHour = Request.Params["txtOrderDateHour"];
                //txtOrderDateMinute = Request.Params["txtOrderDateMinute"];
                combLatestDeliverDate = Request.Params["combLatestDeliverDate"];
                txtAdminNote = Request.Params["txtareAdminNote"];
                txtCartNote = Request.Params["txtareCartNote"];
                txtTradeNumber = Request.Params["txtTradeNumber"];
                txtOrderId = Request.Params["txtOrderId"];
                txtDeliverNumber = Request.Params["txtDeliverNumber"];
                //combOrderDate += " " + txtOrderDateHour + ":" + txtOrderDateMinute;

                if (!string.IsNullOrEmpty(txtAdminNote))
                {
                    txtAdminNote = Resources.OrderAdd.NOTE + ":" + txtAdminNote;
                }

                #region 訂購人信息
                string b_txtName = "", b_txtMobil = "", b_txtPhoneHead = "", b_txtPhoneContent = "", b_txtAddress = "";
                uint b_combZip = 0;
                b_txtName = Request.Params["b_txtName"];
                b_txtMobil = Request.Params["b_txtMobel"];
                b_txtPhoneHead = Request.Params["b_txtPhoneHead"];
                b_txtPhoneContent = Request.Params["b_txtPhoneContent"];
                b_txtAddress = Request.Params["b_txtAddress"];
                uint.TryParse(Request.Params["b_combZip"] ?? "0", out b_combZip);
                #endregion

                #region 收件人信息
                string r_txtCNFullName = "", r_txtActionPhone = "", r_txtContactPhoneHead = "", r_txtContactPhoneContent = "", r_txtContactAddress = "";
                uint r_combZip = 0;
                r_txtCNFullName = Request.Params["r_txtCNFullName"];
                r_txtActionPhone = Request.Params["r_txtActionPhone"];
                r_txtContactPhoneHead = Request.Params["r_txtContactPhoneHead"];
                r_txtContactPhoneContent = Request.Params["r_txtContactPhoneContent"];
                r_txtContactAddress = Request.Params["r_txtContactAddress"];
                uint.TryParse(Request.Params["r_combZip"] ?? "0", out r_combZip);
                #endregion

                string dataStr = Request.Params["gridData"];

                OrderAddCustom odc = new OrderAddCustom();
                JavaScriptSerializer jss = new JavaScriptSerializer();

                List<OrderAddCustom> errorOrder = new List<OrderAddCustom>();
                List<CooperatorOrderCustom> coopErrorList = new List<CooperatorOrderCustom>();
                //添加總和 抵用金 和 購物金  add by zhuoqin0830w
                uint productTotal = 0, deduct_bonusTotal = 0, deduct_welfareTotal = 0, acc_bonusTotal = 0;

                OrderImportMgr orderImportMgr = new OrderImportMgr(connectionString, 0);
                Resource.CoreMessage = new CoreResource("OrderImport");
                List<OrderSlave> slaves = new List<OrderSlave>();
                List<ChannelOrder> channelList = new List<ChannelOrder>();
                //add by zhuoqin0830w  2015/02/26  公關單與報廢單功能  獲取前臺傳來的單據類型
                string billtype = orderType == 1 ? "" : Request.Form["BillType"];
                string dep = orderType == 1 ? "" : Request.Form["dep"];
                //獲取前臺傳來 的 Cart_Delivery 和 Site_Id  add by zhuoqin0830w  2015/07/03
                uint Cart_Delivery = uint.Parse(Request.Params["Cart_Delivery"]);
                uint Site_Id = orderType == 1 ? 0 : uint.Parse(Request.Form["Site_Id"]);

                switch (storeType)
                {
                    case 1:
                        #region 合作外站新增訂單
                        List<CooperatorOrderCustom> odcList1 = jss.Deserialize<List<CooperatorOrderCustom>>(dataStr);
                        IPriceMasterImplMgr _priceMgr = new PriceMasterMgr(connectionString);
                        var parentList1 = from rec in odcList1 where rec.parent_id == "0" && rec.Item_Id == 0 select rec;//rec.price_type!=2:各自定價的價格是從表中讀取的,所以到後臺不需要重新計算價格
                        var singleList1 = from rec in odcList1 where rec.Item_Id != 0 && rec.parent_id == "0" select rec;

                        foreach (var item in parentList1)
                        {
                            var totalPrice = item.Event_Item_Money == 0 ? item.product_cost : item.Event_Item_Money;//add by zhuoqin0830w 2015/12/02  判斷是否使用活動價
                            productTotal += uint.Parse((totalPrice * item.buynum).ToString());

                            productItemMapMgr = new ProductItemMapMgr(connectionString);
                            ProductItemMap pMap = productItemMapMgr.QueryAll(new ProductItemMap { channel_id = uint.Parse(combChannelId.ToString()), channel_detail_id = item.coop_product_id }).FirstOrDefault();

                            PriceMaster pMaster = _priceMgr.QueryPriceMaster(new PriceMaster
                            {
                                product_id = uint.Parse(pMap.product_id.ToString()),
                                //user_id = 0,
                                //user_level = 1,
                                //site_id = 1,
                                price_master_id = pMap.price_master_id,
                                child_id = int.Parse(pMap.product_id.ToString())
                            });
                            odcList1.Find(rec => rec.Product_Id == item.Product_Id && rec.group_id == item.group_id).price_master_id = pMaster.price_master_id;

                            ////查找該父商品下的子商品
                            var childList = from rec in odcList1 where rec.parent_id == item.coop_product_id select rec;
                            foreach (var child in childList)
                            {
                                IItemPriceImplMgr iPMgr = new ItemPriceMgr(connectionString);
                                PriceMaster query = new PriceMaster { user_id = pMaster.user_id, user_level = pMaster.user_level, site_id = pMaster.site_id };
                                if (item.price_type == 1)
                                {
                                    query.product_id = uint.Parse(child.coop_product_id);
                                    query.child_id = 0;
                                }
                                else if (item.price_type == 2)
                                {
                                    query.product_id = uint.Parse(pMap.product_id.ToString());
                                    query.child_id = int.Parse(child.coop_product_id);
                                }
                                PriceMaster pM = _priceMgr.QueryPriceMaster(query);
                                List<ItemPriceCustom> ipList = new List<ItemPriceCustom>();
                                if (pM != null)
                                {
                                    odcList1.Find(rec => rec.coop_product_id == child.coop_product_id && rec.group_id == child.group_id).price_master_id = pM.price_master_id;
                                    ipList = iPMgr.Query(new ItemPrice
                                    {
                                        item_id = child.Item_Id,
                                        price_master_id = pM.price_master_id
                                    });
                                    if ((item.price_type == 1 && pM.same_price == 1) || (item.price_type == 2 && pMaster.same_price == 1))
                                    {
                                        odcList1.Find(rec => rec.coop_product_id == child.coop_product_id && rec.group_id == child.group_id).Item_Money = uint.Parse(pM.price.ToString());
                                        //odcList1.Find(rec => rec.coop_product_id == child.coop_product_id && rec.group_id == child.group_id).Item_Cost = uint.Parse(pM.cost.ToString());
                                        odcList1.Find(rec => rec.coop_product_id == child.coop_product_id && rec.group_id == child.group_id).Event_Item_Cost = uint.Parse(pM.event_cost.ToString());
                                    }
                                    else
                                    {
                                        odcList1.Find(rec => rec.coop_product_id == child.coop_product_id && rec.group_id == child.group_id).Item_Money = ipList[0].item_money;
                                        //odcList1.Find(rec => rec.coop_product_id == child.coop_product_id && rec.group_id == child.group_id).Item_Cost = ipList[0].item_cost;
                                        odcList1.Find(rec => rec.coop_product_id == child.coop_product_id && rec.group_id == child.group_id).Event_Item_Cost = ipList[0].event_cost;
                                    }
                                }

                            }
                        }
                        //單一商品
                        #region Channel_Order 資料
                        ChannelOrder chOrd;
                        foreach (CooperatorOrderCustom coop in odcList1)
                        {
                            ChannelOrder existChl = channelList.Where(m => m.Channel_Detail_Id == coop.coop_product_id).FirstOrDefault();
                            if (existChl != null)
                            {
                                continue;
                            }
                            chOrd = new ChannelOrder();
                            chOrd.Channel_Detail_Id = coop.coop_product_id;
                            chOrd.Channel_Id = int.Parse(combChannelId.ToString());
                            chOrd.Createtime = DateTime.Now;
                            if (!string.IsNullOrEmpty(combOrderDate))
                            {
                                chOrd.Ordertime = Convert.ToDateTime(combOrderDate);
                            }
                            chOrd.Order_Id = txtTradeNumber;
                            if (!string.IsNullOrEmpty(combLatestDeliverDate))
                            {
                                chOrd.Latest_Deliver_Date = Convert.ToDateTime(combLatestDeliverDate);
                            }
                            chOrd.Dispatch_Seq = txtDeliverNumber;
                            channelList.Add(chOrd);
                        }
                        #endregion

                        slaves = orderImportMgr.FillSlaveCooperator(odcList1, coopErrorList, combChannelId, combOrderStatus, txtOrderId);

                        foreach (var item in singleList1)
                        {
                            var totalPrice = item.Event_Item_Money == 0 ? item.product_cost : item.Event_Item_Money;//add by zhuoqin0830w 2015/12/02  判斷是否使用活動價
                            productTotal += uint.Parse((totalPrice * item.buynum).ToString());
                        }
                        #endregion
                        break;
                    case 2:     //Gigade新增訂單
                        List<OrderAddCustom> odcList2 = jss.Deserialize<List<OrderAddCustom>>(dataStr);
                        //將前臺傳來的 Site_Id 的值傳入到後臺 并插入數據庫  add by zhuoqin0830w  2015/07/03
                        odcList2.ForEach(m =>
                        {
                            m.Site_Id = Site_Id;
                        });

                        if (orderType == 1)
                        {
                            #region 重新計算組合商品價格
                            IPriceMasterImplMgr priceMgr = new PriceMasterMgr(connectionString);

                            var parentList = from rec in odcList2 where rec.parent_id == 0 && rec.Item_Id == 0 && rec.price_type != 2 select rec;  //rec.price_type!=2:各自定價的價格是從表中讀取的,所以到後臺不需要重新計算價格

                            var singleList = from rec in odcList2 where rec.Item_Id != 0 && rec.parent_id == 0 select rec;

                            var priceSelfList = from rec in odcList2 where rec.Item_Id == 0 && rec.parent_id == 0 && rec.price_type == 2 select rec;

                            //單一商品計算價格
                            foreach (var item in singleList)
                            {
                                //使 總價  減去 抵用金 和 購物金  edit by zhuoqin0830w  2015/05/14
                                var totalPrice = item.Event_Item_Money == 0 ? item.product_cost : item.Event_Item_Money;//add by zhuoqin0830w 2015/12/02  判斷是否使用活動價
                                productTotal += uint.Parse((totalPrice * item.buynum - item.deduct_bonus - item.deduct_welfare).ToString());
                                // 計算 購物金 和 抵用金  的 總和  add by zhuoqin0830w  2015/05/14
                                deduct_bonusTotal += uint.Parse(item.deduct_bonus.ToString());
                                deduct_welfareTotal += uint.Parse(item.deduct_welfare.ToString());
                                PriceMaster pM = priceMgr.QueryPriceMaster(new PriceMaster
                                {
                                    product_id = uint.Parse(item.Product_Id.ToString()),
                                    user_id = 0,
                                    user_level = 1,
                                    site_id = Site_Id,
                                    child_id = 0
                                });
                                if (pM != null)
                                {
                                    odcList2.Find(rec => rec.Product_Id == item.Product_Id).price_master_id = pM.price_master_id;
                                }
                            }

                            //組合商品各自定價計算價格
                            foreach (var item in priceSelfList)
                            {
                                //使 總價  減去 抵用金 和 購物金  edit by zhuoqin0830w  2015/05/14
                                var totalPrice = item.Event_Item_Money == 0 ? item.product_cost : item.Event_Item_Money;//add by zhuoqin0830w 2015/12/02  判斷是否使用活動價
                                productTotal += uint.Parse((totalPrice * item.buynum - item.deduct_bonus - item.deduct_welfare).ToString());

                                // 計算 購物金 和 抵用金  的 總和  add by zhuoqin0830w  2015/05/14
                                deduct_bonusTotal += uint.Parse(item.deduct_bonus.ToString());
                                deduct_welfareTotal += uint.Parse(item.deduct_welfare.ToString());

                                PriceMaster pM = priceMgr.QueryPriceMaster(new PriceMaster { product_id = item.Product_Id, user_id = 0, user_level = 1, site_id = 1, child_id = Convert.ToInt32(item.Product_Id) });
                                if (pM != null)
                                {
                                    odcList2.Find(rec => rec.Product_Id == item.Product_Id && rec.group_id == item.group_id).price_master_id = pM.price_master_id;
                                }
                                //找出當前父商品的子商品
                                var childList = from rec in odcList2 where rec.parent_id == item.Product_Id && rec.group_id == item.group_id select rec;
                                foreach (var child in childList)
                                {
                                    pM = priceMgr.QueryPriceMaster(new PriceMaster { product_id = item.Product_Id, user_id = 0, user_level = 1, site_id = 1, child_id = Convert.ToInt32(child.Product_Id) });
                                    if (pM != null)
                                    {
                                        IItemPriceImplMgr iPMgr = new ItemPriceMgr(connectionString);
                                        ItemPrice price = iPMgr.Query(new ItemPrice { item_id = child.Item_Id, price_master_id = pM.price_master_id }).FirstOrDefault();
                                        if (price != null)
                                        {
                                            odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Item_Cost = price.item_cost;       //成本
                                            odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Event_Item_Cost = price.event_cost;//活動成本
                                            odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Item_Money = price.item_money;//售價
                                        }
                                    }
                                }
                            }

                            //組合商品按比例拆分計算價格
                            foreach (var item in parentList)
                            {
                                var TotalPrice = 0.0;
                                var TotalCost = 0.0;
                                //使 總價  減去 抵用金 和 購物金  edit by zhuoqin0830w  2015/05/14
                                var totalPrice = item.Event_Item_Money == 0 ? item.product_cost : item.Event_Item_Money;//add by zhuoqin0830w 2015/12/02  判斷是否使用活動價
                                productTotal += uint.Parse((totalPrice * item.buynum - item.deduct_bonus - item.deduct_welfare).ToString());

                                // 計算 購物金 和 抵用金  的 總和  add by zhuoqin0830w  2015/05/14
                                deduct_bonusTotal += uint.Parse(item.deduct_bonus.ToString());
                                deduct_welfareTotal += uint.Parse(item.deduct_welfare.ToString());

                                PriceMaster pMaster = priceMgr.QueryPriceMaster(new PriceMaster
                                {
                                    product_id = uint.Parse(item.Product_Id.ToString()),
                                    user_id = 0,
                                    user_level = 1,
                                    site_id = Site_Id,
                                    child_id = int.Parse(item.Product_Id.ToString())
                                });
                                odcList2.Find(rec => rec.Product_Id == item.Product_Id && rec.group_id == item.group_id).price_master_id = pMaster.price_master_id;

                                //找出當前父商品的子商品
                                var childList = from rec in odcList2 where rec.parent_id == item.Product_Id && rec.group_id == item.group_id select rec;

                                var parentPrice = item.product_cost;
                                var parentCost = pMaster != null ? pMaster.cost : 0;

                                //子商品總價
                                foreach (var child in childList)
                                {
                                    //價格
                                    IItemPriceImplMgr iPMgr = new ItemPriceMgr(connectionString);

                                    PriceMaster pM = priceMgr.QueryPriceMaster(new PriceMaster
                                    {
                                        product_id = uint.Parse(child.Product_Id.ToString()),
                                        user_id = 0,
                                        user_level = 1,
                                        site_id = Site_Id,
                                        child_id = 0
                                    });
                                    List<ItemPriceCustom> ipList = new List<ItemPriceCustom>();
                                    if (pM != null)
                                    {
                                        //必購數量
                                        _prodCombMgr = new ProductComboMgr(connectionString);
                                        List<ProductComboCustom> prodComList = _prodCombMgr.combQuery(new ProductComboCustom
                                        {
                                            Parent_Id = int.Parse(item.Product_Id.ToString()),
                                            Child_Id = child.Product_Id.ToString() //add by wangwei02016w 2014/9/24 
                                        });
                                        var s_must_buy = prodComList[0].S_Must_Buy == 0 ? child.s_must_buy : prodComList[0].S_Must_Buy;

                                        if (pM.same_price == 1)//同價
                                        {
                                            TotalPrice += pM.price * s_must_buy;
                                            TotalCost += pM.cost * s_must_buy;
                                        }
                                        else
                                        {
                                            ipList = iPMgr.Query(new ItemPrice
                                            {
                                                item_id = child.Item_Id,
                                                price_master_id = pM.price_master_id
                                            });

                                            if (ipList.Count != 0)
                                            {
                                                TotalPrice += ipList[0].item_money * s_must_buy;
                                                TotalCost += ipList[0].item_cost * s_must_buy;
                                            }
                                        }
                                    }
                                }

                                //子商品按比例拆分后的價格
                                foreach (var child in childList)
                                {
                                    _prodCombMgr = new ProductComboMgr(connectionString);
                                    List<ProductComboCustom> prodComList = _prodCombMgr.combQuery(new ProductComboCustom
                                    {
                                        Parent_Id = int.Parse(item.Product_Id.ToString()),
                                        Child_Id = child.Product_Id.ToString() //add by wangwei02016w 2014/9/24 
                                    });
                                    var s_must_buy = prodComList[0].S_Must_Buy == 0 ? child.s_must_buy : prodComList[0].S_Must_Buy;

                                    IItemPriceImplMgr iPMgr = new ItemPriceMgr(connectionString);

                                    PriceMaster pM = priceMgr.QueryPriceMaster(new PriceMaster
                                    {
                                        product_id = uint.Parse(child.Product_Id.ToString()),
                                        user_id = 0,
                                        user_level = 1,
                                        site_id = Site_Id,
                                        child_id = 0
                                    });
                                    List<ItemPriceCustom> ipList = new List<ItemPriceCustom>();
                                    if (pM != null)
                                    {
                                        odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).price_master_id = pM.price_master_id;
                                        if (pM.same_price == 1)
                                        {
                                            odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Item_Cost = uint.Parse(pM.cost.ToString());       //成本
                                            odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Event_Item_Cost = uint.Parse(pM.event_cost.ToString());//活動成本

                                            var afterPrice = 0;
                                            var afterCost = 0;
                                            if (TotalPrice <= 0)
                                            {
                                                afterPrice = 0;
                                            }
                                            else
                                            {
                                                afterPrice = Convert.ToInt16(Math.Round(parentPrice * (Convert.ToDouble(pM.price * s_must_buy) / TotalPrice / s_must_buy)));
                                            }
                                            if (TotalCost <= 0)
                                            {
                                                afterCost = 0;
                                            }
                                            else
                                            {
                                                afterCost = Convert.ToInt16(Math.Round(parentCost * (Convert.ToDouble(pM.cost * s_must_buy) / TotalCost / s_must_buy)));
                                            }

                                            //var singleTotal = afterPrice * s_must_buy;
                                            var singleTotal = afterPrice;
                                            parentPrice -= uint.Parse(singleTotal.ToString());
                                            TotalPrice -= Convert.ToDouble(pM.price) * s_must_buy;
                                            odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).product_cost = uint.Parse(afterPrice.ToString());

                                            //var singleCostTotal = afterCost * s_must_buy;
                                            var singleCostTotal = afterCost;
                                            parentCost -= singleCostTotal;
                                            TotalCost -= Convert.ToDouble(pM.cost) * s_must_buy;
                                            odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Item_Cost = uint.Parse(afterCost.ToString());
                                        }
                                        else
                                        {
                                            ipList = iPMgr.Query(new ItemPrice { item_id = child.Item_Id, price_master_id = pM.price_master_id });
                                            if (ipList.Count != 0)
                                            {
                                                odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Item_Cost = ipList[0].item_cost;       //成本
                                                odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Event_Item_Cost = ipList[0].event_cost;//活動成本

                                                //var afterPrice = CommonFunction.Math4Cut5Plus(parentPrice * ipList[0].item_money * s_must_buy / TotalPrice / s_must_buy);
                                                //beforePrice += afterPrice * s_must_buy;

                                                //new logic
                                                var afterPrice = 0;
                                                var afterCost = 0;
                                                if (TotalPrice <= 0)
                                                {
                                                    afterPrice = 0;
                                                }
                                                else
                                                {
                                                    afterPrice = Convert.ToInt16(Math.Round(parentPrice * (Convert.ToDouble(ipList[0].item_money * s_must_buy) / TotalPrice / s_must_buy)));

                                                }

                                                if (TotalCost <= 0)
                                                {
                                                    afterCost = 0;
                                                }
                                                else
                                                {
                                                    afterCost = Convert.ToInt16(Math.Round(parentCost * (Convert.ToDouble(ipList[0].item_cost * s_must_buy) / TotalCost / s_must_buy)));
                                                }

                                                //var singleTotal = afterPrice * s_must_buy;
                                                var singleTotal = afterPrice;
                                                parentPrice -= uint.Parse(singleTotal.ToString());
                                                TotalPrice -= Convert.ToDouble(ipList[0].item_money) * s_must_buy;
                                                // beforePrice += singleTotal;

                                                //var singleCostTotal = afterCost * s_must_buy;
                                                var singleCostTotal = afterCost;
                                                parentCost -= singleCostTotal;
                                                TotalCost -= Convert.ToDouble(ipList[0].item_cost) * s_must_buy;
                                                odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).product_cost = uint.Parse(afterPrice.ToString());
                                                odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).Item_Cost = uint.Parse(afterCost.ToString());
                                                odcList2.Find(rec => rec.Product_Id == child.Product_Id && rec.group_id == item.group_id).s_must_buy = s_must_buy;
                                            }
                                        }
                                    }

                                }
                            }
                            #endregion
                        }
                        else
                        {
                            foreach (var item in odcList2.FindAll(m => (m.parent_id == 0 && m.Item_Id == 0) || (m.Item_Id != 0 && m.parent_id == 0)))
                            {
                                //使 總價  減去 抵用金 和 購物金  edit by zhuoqin0830w  2015/05/14
                                var totalPrice = item.Event_Item_Money == 0 ? item.product_cost : item.Event_Item_Money;//add by zhuoqin0830w 2015/12/02  判斷是否使用活動價
                                productTotal += uint.Parse((totalPrice * item.buynum - item.deduct_bonus - item.deduct_welfare).ToString());
                                // 計算 購物金 和 抵用金  的 總和  add by zhuoqin0830w  2015/05/14
                                deduct_bonusTotal += uint.Parse(item.deduct_bonus.ToString());
                                deduct_welfareTotal += uint.Parse(item.deduct_welfare.ToString());
                                acc_bonusTotal += uint.Parse(item.accumulated_bonus.ToString());
                            }
                        }

                        slaves = orderImportMgr.FillSlave(odcList2, errorOrder, combOrderStatus, txtOrderId);
                        break;
                    default:
                        break;
                }

                if (slaves != null && errorOrder.Count() == 0 && coopErrorList.Count() == 0)
                {
                    channelMgr = new ChannelMgr(connectionString);

                    #region OrderMaster 信息
                    #region receipt_to 發票開立方式
                    switch (receipt_to)
                    {
                        case 1: receipt_to = 2; break;
                        case 2: receipt_to = 0; break;
                        case 3: receipt_to = 2; break;
                    }
                    #endregion

                    #region retrieve_mode 取貨方式
                    channelShipMgr = new ChannelShippingMgr(connectionString);
                    ChannelShipping chip = new ChannelShipping();
                    chip.channel_id = int.Parse(combChannelId.ToString());
                    chip.shipping_carrior = int.Parse(combStoreMode.ToString());
                    retrieve_mode = uint.Parse(channelShipMgr.Query(chip)[0].retrieve_mode.ToString());
                    #endregion

                    OrderMaster ordermaster = new OrderMaster();
                    //ordermaster.Order_Id = Convert.ToUInt32(orderImportMgr.NextOrderId());
                    ordermaster.Channel_Order_Id = txtTradeNumber;
                    ordermaster.Invoice_Status = receipt_to;
                    ordermaster.Order_Freight_Normal = normalFright;
                    ordermaster.Order_Freight_Low = lowFright;
                    ordermaster.Delivery_Store = combStoreMode;
                    ordermaster.Channel = combChannelId;
                    ordermaster.Order_Status = combOrderStatus;
                    //orderType=2為内部订单输入 edit by xiangwang0413w 2014/10/29
                    ordermaster.User_Id = (orderType == 1) ? (uint)channelMgr.GetUserIdByChannelId((int)combChannelId) : uint.Parse(Request.Params["userId"]);//订单人的user_id
                    ordermaster.Order_Product_Subtotal = productTotal;
                    ordermaster.Order_Amount = productTotal + normalFright + lowFright;
                    ordermaster.Order_Payment = combPayMent;
                    //向 order_master 裱中 添加 從前臺獲取的 Cart_Delivery  edit by zhuoqin0830w  2015/07/03
                    ordermaster.Cart_Id = Cart_Delivery;
                    ordermaster.Note_Admin = string.Format(Resources.OrderAdd.NOTE_CONTENT, _caller.user_username, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), txtAdminNote);
                    ordermaster.Note_Order = txtCartNote;
                    ordermaster.Order_Date_Pay = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime());
                    ordermaster.Order_Createdate = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime(combOrderDate));
                    ordermaster.Import_Time = DateTime.Now;
                    ordermaster.Retrieve_Mode = retrieve_mode;
                    //向 order_master 裱中 添加 抵用金 和 購物金  add by zhuoqin0830w  2015/05/14
                    ordermaster.Deduct_Welfare = deduct_welfareTotal;
                    ordermaster.Deduct_Bonus = deduct_bonusTotal;
                    //向 order_master 表中 添加 返還購物金  add by zhuoqin0830w  2015/09/01
                    ordermaster.Accumulated_Bonus = (int)acc_bonusTotal;
                    #endregion

                    #region 出貨時間
                    if (orderType == 2)//內部訂單輸入
                    {
                        ordermaster.Holiday_Deliver = Convert.ToInt32(Request.Form["HolidayDeliver"]);
                        ordermaster.Estimated_Arrival_Period = Convert.ToInt32(Request.Form["EstimatedArrivalPeriod"]);
                    }
                    #endregion

                    #region 訂購人信息
                    ordermaster.Order_Name = b_txtName;
                    ordermaster.Order_Mobile = b_txtMobil;
                    b_txtPhoneHead = !string.IsNullOrEmpty(b_txtPhoneHead) ? b_txtPhoneHead + "-" : b_txtPhoneHead;
                    ordermaster.Order_Phone = b_txtPhoneHead + b_txtPhoneContent;
                    ordermaster.Order_Zip = b_combZip;
                    ordermaster.Order_Address = b_txtAddress;
                    ordermaster.Order_Gender = Request.Params["ServiceSex"] == "1" ? true : false; //add by wwei0216w 2015/1/21 訂購人性別
                    #endregion

                    #region 收件人信息
                    ordermaster.Delivery_Name = r_txtCNFullName;
                    ordermaster.Delivery_Mobile = r_txtActionPhone;
                    ordermaster.Delivery_Phone = r_txtContactPhoneHead + "-" + r_txtContactPhoneContent;
                    ordermaster.Delivery_Zip = r_combZip;
                    ordermaster.Delivery_Address = r_txtContactAddress;
                    ordermaster.Delivery_Gender = Request.Params["AddresseeSex"] == "1" ? true : false; //add by wwei0216w 收件人性別
                    #endregion

                    #region add by zhuoqin0830w  2015/02/26  公關單與報廢單功能   判斷單據類型是否為空值 如果不為空則執行添加功能
                    OrderMasterPattern op = null;
                    if (!string.IsNullOrEmpty(billtype))
                    {
                        op = new OrderMasterPattern { Pattern = int.Parse(billtype), Dep = int.Parse(dep) };
                    }
                    #endregion

                    #region add by zhuoqin0830w 2015/08/24  在內部訂單輸入時 同時將 輸入的 購物金 和 返還購物金 以及 抵用券 添加至 bonus_master 和 bonus_record 表中
                    BonusMaster bm = null; BonusRecord brBonus = null; BonusRecord brWelfare = null;
                    //判斷返還購物金 是否 大於0 如果大於 0 表示需要在 bonus_master 新增一筆數據 
                    if (acc_bonusTotal > 0)
                    {
                        bm = new BonusMaster
                        {
                            user_id = ordermaster.User_Id,
                            type_id = 30,// 默認為 商品回饋購物金
                            master_writer = "商品回饋購物金",
                            master_total = acc_bonusTotal,
                            master_balance = (int)acc_bonusTotal,
                            master_start = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime()),
                            master_end = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime(DateTime.Now.AddDays(90).ToString())),//默認過期時間是當前時間+90天
                            master_createdate = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime()),
                            master_updatedate = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime()),
                            bonus_type = 1//默認為  購物金
                        };
                    }
                    //判斷購物金是否大於 0 如果是 則表示 需要在 bonus_record 增加一筆數據 
                    //並且需要 在 bonus_master 中 按照 過期日期 減去 相應的 參數
                    if (deduct_bonusTotal > 0)
                    {
                        brBonus = new BonusRecord
                        {
                            user_id = ordermaster.User_Id,
                            record_use = deduct_bonusTotal,
                            record_createdate = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime()),
                            record_updatedate = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime())
                        };
                        //判斷購物金是否小於或等於數據庫中的購物金
                        _bonusMasterMgr = new BonusMasterMgr(connectionString);
                        int userBonusTotal = _bonusMasterMgr.GetSumBouns(brBonus);
                        if (deduct_bonusTotal > userBonusTotal)
                        {
                            jsonStr = "{success:false,msg:'" + Resources.OrderAdd.BONUS_PRICE_NULL + "'}";
                            this.Response.Clear();
                            this.Response.Write(jsonStr);
                            this.Response.End();
                            return this.Response;
                        }
                    }
                    //判斷 抵用卷 金額 是否 大於 0 
                    if (deduct_welfareTotal > 0)
                    {
                        brWelfare = new BonusRecord
                        {
                            user_id = ordermaster.User_Id,
                            record_use = deduct_welfareTotal,
                            record_createdate = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime()),
                            record_updatedate = Convert.ToUInt32(BLL.gigade.Common.CommonFunction.GetPHPTime())
                        };
                        //判斷購物金是否小於或等於數據庫中的購物金
                        _bonusMasterMgr = new BonusMasterMgr(connectionString);
                        int userWelfateTotal = _bonusMasterMgr.GetSumWelfare(brWelfare);
                        if (deduct_welfareTotal > userWelfateTotal)
                        {
                            jsonStr = "{success:false,msg:'" + Resources.OrderAdd.WELFARE_PRICE_NULL + "'}";
                            this.Response.Clear();
                            this.Response.Write(jsonStr);
                            this.Response.End();
                            return this.Response;
                        }
                    }
                    #endregion

                    #region 保存至數據庫
                    bool result = orderImportMgr.Save2DB(ordermaster, slaves, channelList, null, op, bm, brBonus, brWelfare);
                    #endregion

                    if (result)
                    {
                        jsonStr = "{success:true,msg:'" + Resources.OrderAdd.ORDER_ADD_SUCCESS + "'}";
                    }
                }
                else
                {
                    StringBuilder stb = new StringBuilder();
                    if (storeType == 1)
                    {
                        foreach (CooperatorOrderCustom item in coopErrorList)
                        {
                            stb.Append(item.coop_product_id + "\\n");
                        }
                        stb.Append("\\n" + Resources.OrderAdd.EMPTY_NULL_FAIL);
                    }
                    else if (storeType == 2)
                    {
                        foreach (OrderAddCustom item in errorOrder)
                        {
                            stb.Append(item.Product_Id + item.Spec_Name_1 + item.Spec_Name_2 + "\\n");
                        }
                        stb.Append("\\n" + Resources.OrderAdd.EMPTY_NULL_FAIL);
                    }
                    jsonStr = "{success:false,msg:'" + stb.ToString() + "'}";
                }
            }
            catch (Exception ex)
            {
                Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
                logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
                logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                log.Error(logMessage);
            }

            this.Response.Clear();
            this.Response.Write(jsonStr);
            this.Response.End();
            return this.Response;
        }