Ejemplo n.º 1
0
        private void GenerateRouteFromMarker(int iserial, int userIserial)
        {
            using (var entities = new WorkFlowManagerDBEntities())
            {
                var h = (from head in entities.tbl_MarkerTransactionHeader
                         where head.Iserial == iserial
                         select head).SingleOrDefault();
                h.Posted = true;

                var markerDetailQuery = (from f in entities.tbl_MarkerDetail.Include("Tbl_Wf_CuttingOrder")
                                         where f.MarkerTransactionHeader == iserial &&
                                         f.Tbl_Wf_CuttingOrder.Count() != 0
                                         select f).ToList();

                var salesorder        = markerDetailQuery.FirstOrDefault().SalesOrder;
                var salesOrderIserial = entities.TblSalesOrders.FirstOrDefault(w => w.SalesOrderCode == salesorder && w.SalesOrderType == 2).Iserial;
                var bomline           = entities.BOMs.FirstOrDefault(w => w.TblSalesOrder == salesOrderIserial && w.BOM_IsMainFabric == true);
                if (bomline == null)
                {
                    throw new InvalidDataException("Please Check Main fabric");
                }
                var routeHeaderOld = (from head in entities.RouteCardHeaders
                                      where head.MarkerTransaction == iserial
                                      select head).SingleOrDefault();

                if (routeHeaderOld != null && routeHeaderOld.AxRouteCardFabricsJournalId != null)
                {
                    return;
                }
                else
                {
                    if (routeHeaderOld != null)
                    {
                        entities.RouteCardHeaders.DeleteObject(routeHeaderOld);
                    }
                }
                var transactionType = 5;

                //var singleOrDefault = entities.tblChainSetups.SingleOrDefault(x => x.sGlobalSettingCode == "DefaultVendor");

                //if (singleOrDefault != null)
                //{
                //    var defaultVendor = singleOrDefault.sSetupValue;

                //    transactionType = h != null && defaultVendor == h.Vendor ? 5:5;
                //}
                if (bomline.BOM_FabricRout == null)
                {
                    throw new InvalidDataException("Please Check Main fabric Route Group");
                }
                h.Operation = (int)bomline.BOM_FabricRout;
                if (h != null)
                {
                    var routeHeader = new RouteCardHeader
                    {
                        tblTransactionType = transactionType,
                        DocDate            = h.TransDate,
                        Direction          = 0,
                        MarkerTransaction  = h.Iserial,
                        RoutGroupID        = h.Operation,
                        Vendor             = h.Vendor,
                        RoutID             = h.Workstation,
                        IsPosted           = false,
                        TransID            = Operations.SharedOperation.GetMaxRouteCardTransactionID(h.Operation, 0, transactionType) + 1,
                        RouteType          = 5,
                        DeliveryDate       = h.TransDate
                    };

                    entities.AddToRouteCardHeaders(routeHeader);
                    entities.SaveChanges();
                    foreach (var d in markerDetailQuery)
                    {
                        foreach (var item in d.Tbl_Wf_CuttingOrder)
                        {
                            var fabricaccsearch = entities.FabricAccSearches.FirstOrDefault(w => w.Code == d.FabricCode);
                            var total           = GetNetRollFromTotal(item.Barcode, item.RollAssignedQty);
                            var inspection      =
                                entities.Tbl_fabricInspectionDetail.FirstOrDefault(x => x.Iserial == item.InspectionIserial);

                            var site        = entities.GetLocations.Where(x => x.INVENTLOCATIONID == inspection.FinishedWarehouse).Select(x => x.INVENTSITEID).FirstOrDefault();
                            var routeFabric = new RouteCardFabric
                            {
                                ItemGroup              = fabricaccsearch.ItemGroup,
                                TblSalesOrder          = salesOrderIserial,
                                Barcode                = item.Barcode,
                                Batch                  = inspection.BatchNo,
                                FabricColor            = d.FabricColorCode,
                                ItemId                 = d.FabricCode,
                                Location               = inspection.FinishedWarehouse,
                                Warehouse              = inspection.FinishedWarehouse,
                                Qty                    = total,
                                RemainingQty           = total,
                                RouteCardHeaderIserial = routeHeader.Iserial,
                                Site                   = site,
                                StyleColor             = d.StyleColorCode,
                                Unit                   = item.RollUnit,
                                Size                   = inspection.BatchNo,
                            };

                            entities.AddObject("RouteCardFabrics", routeFabric);
                            entities.SaveChanges();
                        }
                    }
                    var discStyleColors =
                        markerDetailQuery.GroupBy(x => x.StyleColorCode).Select(x => x.Key).Distinct();
                    var salesordercode     = markerDetailQuery.FirstOrDefault().SalesOrder;
                    var salesOrderIserials = entities.TblSalesOrders.FirstOrDefault(w => w.SalesOrderCode == salesordercode && w.SalesOrderType == 2).Iserial;
                    var salesOrder         = entities.TblSalesOrders.Include("TblStyle1.TblSizeGroup1.TblSizes").FirstOrDefault(w => w.Iserial == salesOrderIserials);

                    foreach (var variable in discStyleColors)
                    {
                        var objectIndex = Guid.NewGuid().ToString("D");

                        var rr            = new WorkFlowManagerDBEntities().markerPostToRouteProcedure(iserial, variable).ToList();
                        var warehousecode = GetChainSetupBycode("DefaultFPWarehouse1st");

                        var warehouseIserial = entities.TblWarehouses.FirstOrDefault(w => w.Code == warehousecode).Iserial;
                        if (rr.Any())
                        {
                            foreach (var row in rr)
                            {
                                var newrow = new RouteCardDetail
                                {
                                    TblColor                = variable,
                                    Degree                  = "1st",
                                    TblSalesOrder           = salesOrderIserials,
                                    Trans_TransactionHeader = routeHeader.TransID,
                                    Size                   = row.meterpersizecode,
                                    SizeQuantity           = (int?)row.sizeQtyReal,
                                    RoutGroupID            = h.Operation,
                                    Direction              = 0,
                                    ObjectIndex            = objectIndex,
                                    RouteCardHeaderIserial = routeHeader.Iserial,
                                    TblWarehouse           = warehouseIserial
                                };
                                entities.RouteCardDetails.AddObject(newrow);
                            }
                        }
                        else
                        {
                            var objectIndexnew = Guid.NewGuid().ToString("D");
                            foreach (var VARIABLE in salesOrder.TblStyle1.TblSizeGroup1.TblSizes)
                            {
                                var newrow = new RouteCardDetail
                                {
                                    TblColor                = variable,
                                    Degree                  = "1st",
                                    TblSalesOrder           = salesOrderIserials,
                                    Trans_TransactionHeader = routeHeader.TransID,
                                    Size                   = VARIABLE.SizeCode,
                                    SizeQuantity           = 0,
                                    RoutGroupID            = h.Operation,
                                    Direction              = 0,
                                    ObjectIndex            = objectIndexnew,
                                    RouteCardHeaderIserial = routeHeader.Iserial,
                                    TblWarehouse           = warehouseIserial
                                };

                                entities.RouteCardDetails.AddObject(newrow);
                            }
                        }
                    }
                }
                entities.SaveChanges();

                if (h.Status == 0)
                {
                    // SenderMarkerMail("MarkerMail", "Marker", "", h.Iserial);
                }
            }
        }