예제 #1
0
        public static UpdateDBResult AddProject2Cart(string mainItem, string ERPID, string cartID, string currency, string orgID)
        {
            UpdateDBResult udr = new UpdateDBResult();

            try
            {
                DataTable dt = DataCore.CBOMV2_ConfiguratorDAL.ExpandBOM(mainItem, "TWH1");
                if (dt == null || dt.Rows.Count == 0)
                {
                    udr.IsUpdated     = false;
                    udr.ServerMessage = "No data";
                    return(udr);
                }

                DataTable     dt2     = OracleProvider.GetDataTable("SAP_PRD", string.Format("select distinct mast.matnr as Parent_item, stpo.idnrk as child_item, stpo.potx1 as memo from saprdp.mast inner join saprdp.stas  on stas.stlal = mast.stlal AND stas.stlnr = mast.stlnr INNER JOIN saprdp.stpo on stpo.stlkn = stas.stlkn AND stpo.stlnr = stas.stlnr AND stpo.stlty = stas.stlty where stas.LKENZ<>'X' and mast.matnr='{0}'", mainItem));
                List <string> exclude = new List <string>();
                if (dt2 != null && dt2.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt2.Rows)
                    {
                        string pn = CBOMV2_EditorDAL.FormatSAPPartNoToNormal(dr["child_item"].ToString().Trim());
                        string m  = dr["memo"].ToString();
                        if ((m.IndexOf("耗材") > -1 || m.IndexOf("客供") > -1) && !exclude.Contains(pn))
                        {
                            exclude.Add(pn);
                        }
                    }
                }

                List <ConfiguredItems> items = new List <ConfiguredItems>();
                string prjOriginalPartNo     = mainItem; //保存原始料號 Ex. CM-10A3-T4A719901

                string[] pitem = mainItem.ToString().Split('-');
                if (pitem.Length != 3)
                {
                    udr.IsUpdated     = false;
                    udr.ServerMessage = string.Format("{0} does not have BTO parent item", mainItem);
                    return(udr);
                }

                mainItem = string.Format("{0}-{1}-BTO", pitem[0], pitem[1]);
                if (CheckItemOrdereable(mainItem, orgID) == false)
                {
                    udr.IsUpdated     = false;
                    udr.ServerMessage = "BTO item can not be sold";
                    return(udr);
                }

                object          mainItemDesc = SqlProvider.dbExecuteScalar("MY", string.Format("SELECT TOP 1 PRODUCT_DESC FROM SAP_PRODUCT WHERE PART_NO ='{0}'", mainItem));
                ConfiguredItems mi           = new ConfiguredItems();
                mi.name = mainItem;
                mi.desc = mainItemDesc != null?mainItemDesc.ToString() : string.Empty;

                mi.qty      = 1;
                mi.category = prjOriginalPartNo;
                items.Add(mi);

                foreach (DataRow dr in dt.Rows)
                {
                    string pn = CBOMV2_EditorDAL.FormatSAPPartNoToNormal(dr["IDNRK"].ToString().Trim());
                    if (!exclude.Contains(pn))
                    {
                        if (CheckItemOrdereable(pn, orgID) == false)
                        {
                            udr.IsUpdated     = false;
                            udr.ServerMessage = string.Format("{0} can not be sold", pn);
                            return(udr);
                        }

                        double qty = 1;
                        double.TryParse(dr["MENGE"].ToString().Trim(), out qty);
                        int             q    = Convert.ToInt32(Math.Floor(qty));
                        ConfiguredItems item = new ConfiguredItems();
                        item.name = pn;
                        item.desc = dr["OJTXP"].ToString().Trim();
                        item.qty  = q;
                        items.Add(item);
                    }
                }

                //ICC 20170731 中科組裝單要在最後附上 assembly part No.- 目前鎖定 AGS-CTOS-SYS-C
                ConfiguredItems ags     = new ConfiguredItems();
                object          agsDesc = SqlProvider.dbExecuteScalar("MY", "SELECT TOP 1 PRODUCT_DESC FROM SAP_PRODUCT WHERE PART_NO ='AGS-CTOS-SYS-C'");
                ags.name = "AGS-CTOS-SYS-C";
                ags.desc = agsDesc != null?agsDesc.ToString() : string.Empty;

                ags.qty = 1;
                items.Add(ags);

                if (string.IsNullOrEmpty(prjOriginalPartNo))
                {
                    udr.IsUpdated     = false;
                    udr.ServerMessage = "Original part No. is null";
                    return(udr);
                }

                udr = Configurator2Cart(Newtonsoft.Json.JsonConvert.SerializeObject(items), ERPID, cartID, currency, orgID, true);

                return(udr);
            }
            catch (Exception ex)
            {
                udr.IsUpdated     = false;
                udr.ServerMessage = ex.Message;
                return(udr);
            }
        }
예제 #2
0
        public static UpdateDBResult Configurator2Cart(String SelectedItems, String ERPID, String CartID, String Currency, String Org_ID, bool isPrj = false)
        {
            UpdateDBResult         updateresult = new UpdateDBResult();
            List <ConfiguredItems> items        = new List <ConfiguredItems>();

            try
            {
                // Remove all items in current cart for ACN
                if (Org_ID.StartsWith("CN"))
                {
                    CartDetailHelper.RemoveCartDetailByID(CartID);
                }

                List <ConfiguredItems> toAdditems = Newtonsoft.Json.JsonConvert.DeserializeObject <List <ConfiguredItems> >(SelectedItems);

                if (toAdditems == null || toAdditems.Count == 0)
                {
                    updateresult.IsUpdated     = false;
                    updateresult.ServerMessage = "No item was selected.";
                    return(updateresult);
                }

                // Split pipeline items
                foreach (ConfiguredItems c in toAdditems)
                {
                    if (c.name.Contains("|"))
                    {
                        //List<String> pipelineitems = c.name.Split('|').ToList();
                        //while (pipelineitems.Count > 0)
                        //{
                        //    ConfiguredItems newitem = new ConfiguredItems();
                        //    newitem.name = pipelineitems.First();
                        //    newitem.desc = c.desc;
                        //    newitem.qty = c.qty;
                        //    pipelineitems.RemoveAt(0);
                        //    toAdditems.Add(newitem);
                        //}
                        foreach (var i in c.name.Split('|').ToList())
                        {
                            SAP_PRODUCT sp = MyAdvantechDAL.GetSAPProduct(i);

                            ConfiguredItems newitem = new ConfiguredItems();
                            newitem.name        = i;
                            newitem.desc        = (sp != null && !String.IsNullOrEmpty(sp.PRODUCT_DESC)) ? sp.PRODUCT_DESC : c.desc;
                            newitem.qty         = c.qty;
                            newitem.isLooseItem = c.isLooseItem;
                            items.Add(newitem);
                        }
                    }
                    else
                    {
                        items.Add(c);
                    }
                }
                //if (toAdditems.Count > 0)
                //{
                //    items.RemoveAll(d => d.name.Contains("|"));
                //    items.AddRange(toAdditems);
                //}

                //ICC 2017/4/7 Add product compatibility check. To make sure all items are not incompatible.
                List <PRODUCT_COMPATIBILITY> pcList = MyAdvantechDAL.GetProductCompatibility(Compatibility.Incompatible);
                foreach (ConfiguredItems c in items)
                {
                    List <ConfiguredItems> copy = new List <ConfiguredItems>(items);
                    copy.Remove(c);
                    Tuple <bool, string> result = CheckCompatibility(c.name, copy.Select(p => p.name).ToList(), pcList);
                    if (result.Item1 == true)
                    {
                        updateresult.IsUpdated     = false;
                        updateresult.ServerMessage = string.Format("This part - {0} is {1} with {2}.", c.name, Compatibility.Incompatible.ToString().ToLower(), result.Item2);
                        return(updateresult);
                    }
                }

                //111
                //var _result = "'" + String.Join("','", items) + "'";
            }
            catch (Exception ex)
            {
                updateresult.IsUpdated     = false;
                updateresult.ServerMessage = ex.ToString();
                return(updateresult);
            }

            int NextHigherLevel    = 100;
            var objNextHigherLevel = SqlProvider.dbExecuteScalar("MY", "select max(line_no) from cart_DETAIL_V2 where cart_id = '" + CartID + "' and (otype = 0 or otype = 1)");

            if (objNextHigherLevel != null && !String.IsNullOrEmpty(objNextHigherLevel.ToString()))
            {
                NextHigherLevel = (Convert.ToInt32(objNextHigherLevel.ToString()) / 100 + 1) * 100;
            }
            int NextLooseItemLineNo    = 1;
            var objNextLooseItemLineNo = SqlProvider.dbExecuteScalar("MY", "select max(line_no) from cart_DETAIL_V2 where cart_id = '" + CartID + "' and otype = 0 ");

            if (objNextLooseItemLineNo != null && !String.IsNullOrEmpty(objNextLooseItemLineNo.ToString()))
            {
                NextLooseItemLineNo = Convert.ToInt32(objNextLooseItemLineNo.ToString()) + 1;
            }

            if (items.Count > 0)
            {
                try
                {
                    // For ACN, need to split BOTS root name with "/", ex: IPC-510-BTO/P4
                    items.Where(c => c.name.Contains("-BTO/")).ToList().ForEach(c => c.name = c.name.Split(new string[] { "/" }, StringSplitOptions.None)[0]);

                    // Simulate Product List to get price
                    Order _order = new Order();
                    _order.Currency    = Currency;
                    _order.OrgID       = Org_ID;
                    _order.DistChannel = "10";
                    _order.Division    = "00";
                    if (Org_ID.StartsWith("CN"))
                    {
                        if (System.Configuration.ConfigurationManager.AppSettings["ACNTaxRate"] != null)
                        {
                            _order.Tax = Convert.ToDecimal(System.Configuration.ConfigurationManager.AppSettings["ACNTaxRate"]);
                        }
                        else
                        {
                            _order.Tax = (Decimal)0.16;
                        }
                    }

                    foreach (ConfiguredItems c in items)
                    {
                        //Frank 20171222, if part's material is BTO then identify it as BTOS parent item
                        SAP_PRODUCT sap_p = MyAdvantechDAL.GetSAPProduct(c.name);

                        if (Org_ID.Equals("EU80") && c.isLooseItem)
                        {
                            _order.AddLooseItem(c.name, SAPDAL.GetPlantByOrg(Org_ID), NextLooseItemLineNo, c.qty);
                            NextLooseItemLineNo++;
                        }
                        else if (sap_p != null && (sap_p.MATERIAL_GROUP.Equals("BTOS", StringComparison.InvariantCultureIgnoreCase) || c.name.EndsWith("-BTO", StringComparison.OrdinalIgnoreCase)))
                        {
                            _order.AddBTOSParentItem(c.name, SAPDAL.GetPlantByOrg(Org_ID), NextHigherLevel, c.qty);
                        }
                        else
                        {
                            _order.AddBTOSChildItem(c.name, NextHigherLevel, SAPDAL.GetPlantByOrg(Org_ID), c.qty);
                        }
                    }
                    _order.SetOrderPartnet(new OrderPartner(ERPID, Org_ID, OrderPartnerType.SoldTo));
                    _order.SetOrderPartnet(new OrderPartner(ERPID, Org_ID, OrderPartnerType.ShipTo));
                    _order.SetOrderPartnet(new OrderPartner(ERPID, Org_ID, OrderPartnerType.BillTo));

                    String _errMsg = String.Empty;
                    Advantech.Myadvantech.DataAccess.SAPDAL.SimulateOrder(ref _order, ref _errMsg);
                    // End order simulation


                    if (_order != null && String.IsNullOrEmpty(_errMsg))
                    {
                        foreach (Product p in _order.LineItems)
                        {
                            cart_DETAIL_V2 CartDetail = new cart_DETAIL_V2();
                            CartDetail.Cart_Id = CartID;

                            if (p.LineItemType == LineItemType.LooseItem)
                            {
                                CartDetail.Line_No     = p.LineNumber;
                                CartDetail.higherLevel = 0;
                            }
                            else if (p.LineItemType == LineItemType.BTOSParent)
                            {
                                CartDetail.Line_No     = NextHigherLevel;
                                CartDetail.higherLevel = 0;
                            }
                            else
                            {
                                CartDetail.Line_No     = p.LineNumber;
                                CartDetail.higherLevel = NextHigherLevel;
                            }

                            CartDetail.Part_No     = p.PartNumber;
                            CartDetail.Description = items.Where(i => i.name == p.PartNumber).FirstOrDefault().desc;
                            CartDetail.Qty         = p.Quantity;
                            CartDetail.Ew_Flag     = 0;
                            CartDetail.SatisfyFlag = 0;
                            CartDetail.QUOTE_ID    = "";
                            CartDetail.Category    = items.Where(i => i.name == p.PartNumber).FirstOrDefault().category;

                            //For ACN project, set customer material value = original customer part No.
                            if (isPrj == true && p.LineItemType == LineItemType.BTOSParent && !string.IsNullOrEmpty(CartDetail.Category) && CartDetail.Category.ToUpper().StartsWith("CM-"))
                            {
                                CartDetail.CustMaterial = CartDetail.Category;
                            }
                            else
                            {
                                CartDetail.CustMaterial = "";
                            }

                            CartDetail.otype    = 0;
                            CartDetail.req_date = DateTime.Now.AddDays(2);
                            if (CartDetail.Line_No < 100)
                            {
                                CartDetail.otype = (int)QuoteItemType.Part;
                            }
                            else if (CartDetail.Line_No % 100 == 0 && CartDetail.higherLevel == 0)
                            {
                                CartDetail.otype = (int)QuoteItemType.BtosParent;
                            }
                            else if (CartDetail.Line_No % 100 > 0 && CartDetail.Line_No > 100)
                            {
                                CartDetail.otype = (int)QuoteItemType.BtosPart;
                            }
                            CartDetail.due_date = CartDetail.req_date;

                            CartDetail.Delivery_Plant = GetDeliveryPlant(ERPID, Org_ID, CartDetail.Part_No, (QuoteItemType)Enum.Parse(typeof(QuoteItemType), CartDetail.otype.ToString()));

                            // set price according to it's item type
                            if (CartDetail.otype == (int)QuoteItemType.BtosParent)
                            {
                                CartDetail.List_Price = 0;
                                CartDetail.Unit_Price = 0;
                                CartDetail.Itp        = 0;
                            }
                            else
                            {
                                CartDetail.List_Price = p.ListPrice;
                                CartDetail.Unit_Price = p.UnitPrice;
                                CartDetail.Itp        = 0;
                            }

                            // Get product info from sap_product table
                            DataTable sapproduct_dt = SqlProvider.dbGetDataTable("MY", "select * from sap_product where part_no = '" + p.PartNumber + "'");
                            if (sapproduct_dt != null && sapproduct_dt.Rows.Count > 0)
                            {
                                CartDetail.Model_No = sapproduct_dt.Rows[0]["Model_No"].ToString();
                            }


                            MyAdvantechContext.Current.cart_DETAIL_V2.Add(CartDetail);
                        }
                        MyAdvantechContext.Current.SaveChanges();
                        updateresult.IsUpdated = true;
                    }
                    else
                    {
                        updateresult.IsUpdated     = false;
                        updateresult.ServerMessage = _errMsg;
                    }
                }
                catch (Exception ex)
                {
                    updateresult.IsUpdated     = false;
                    updateresult.ServerMessage = ex.ToString();
                }
            }
            else
            {
                updateresult.IsUpdated = false;
            }

            return(updateresult);
        }