Ejemplo n.º 1
0
        internal static string ChangeHBStatus(int hbId, byte statusId)
        {
            string             isClosed = "true";
            OperationsEntities db       = new OperationsEntities();

            int operCostCount = db.OperationCosts.Where(x => x.HouseBillId == hbId).Count();

            if (operCostCount == 0)
            {
                return("Please add operation cost before close it.");
            }

            HouseBill hbObj = db.HouseBills.Where(x => x.HouseBillId == hbId).FirstOrDefault();

            hbObj.StatusId = statusId; //closed
            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                isClosed = "false " + e.Message;
            }
            catch (Exception e)
            {
                isClosed = "false " + e.InnerException;
            }
            return(isClosed);
        }
Ejemplo n.º 2
0
        internal static string AddEditHouseBill(HouseBillVm houseBillVm)
        {
            string             isSaved     = "true";
            int                houseBillId = houseBillVm.HouseBillId;
            OperationsEntities db          = new OperationsEntities();
            HouseBill          houseBillDb;

            if (houseBillId == 0)
            {
                houseBillDb = new HouseBill();
            }
            else
            {
                houseBillDb = db.HouseBills.Where(x => x.HouseBillId == houseBillId).FirstOrDefault();
            }

            Mapper.CreateMap <HouseBillVm, HouseBill>().IgnoreAllNonExisting();
            Mapper.Map(houseBillVm, houseBillDb);

            if (houseBillId == 0)
            {
                db.HouseBills.Add(houseBillDb);
                int       operationId  = houseBillDb.OperationId;
                Operation operationObj = db.Operations.Where(x => x.OperationId == operationId).FirstOrDefault();
                operationObj.StatusId = 2;
            }

            try
            {
                db.SaveChanges();
                isSaved = "true_" + houseBillDb.HouseBillId;
            }
            catch (DbEntityValidationException e)
            {
                isSaved = "false " + e.InnerException.InnerException;
            }
            catch (Exception e)
            {
                isSaved = "false " + e.InnerException.InnerException;
            }

            return(isSaved);
        }
Ejemplo n.º 3
0
        internal static HouseBillVm GetHbContent(int operationId, byte oprOrderFrom, int hbId = 0)
        {
            OperationsEntities db          = new OperationsEntities();
            HouseBillVm        houseBillVm = new HouseBillVm(oprOrderFrom);
            HouseBill          houseBillDb = null;

            //if(hbId == 0) // add new house bill
            //    houseBillDb = db.HouseBills.Where(x => x.OperationId == operationId).FirstOrDefault();
            if (hbId != 0) // add new house bill
            {
                houseBillDb = db.HouseBills.Where(x => x.HouseBillId == hbId).FirstOrDefault();
            }


            if (houseBillDb == null)
            {
                Operation operationDb = db.Operations.Where(x => x.OperationId == operationId).FirstOrDefault();

                Mapper.CreateMap <Operation, HouseBillVm>()
                .ForMember(x => x.OperationDate, y => y.Ignore())
                .ForMember(x => x.CreateBy, y => y.Ignore())
                .ForMember(x => x.CreateDate, y => y.Ignore())
                .ForMember(x => x.StatusId, y => y.Ignore())
                .ForMember(x => x.CollectedFreightCost, y => y.MapFrom(s => s.FreightCostAmount))
                .ForMember(x => x.CollectedThcCost, y => y.MapFrom(s => s.ThcCostAmount))
                .ForMember(x => x.OperationMinDate, y => y.MapFrom(s => s.OperationDate));

                Mapper.Map(operationDb, houseBillVm);

                //Check if has another HBLs get only the remaining amounts
                decimal nw = 0, gw = 0, cbm = 0, freightCost = 0, thcCost = 0;
                int     packagesNum = 0;
                int     hbCount     = GetHBCount(operationId);
                if (hbCount > 0)
                {
                    var pervSum = db.HouseBills.Where(x => x.OperationId == operationId)
                                  .Select(x => new
                    {
                        x.OperationId,
                        x.NetWeight,
                        x.GrossWeight,
                        x.CBM,
                        x.NumberOfPackages,
                        x.CollectedFreightCost,
                        x.CollectedThcCost
                    })
                                  .GroupBy(s => s.OperationId)
                                  .Select(g => new
                    {
                        nw          = g.Sum(x => x.NetWeight),
                        gw          = g.Sum(x => x.GrossWeight),
                        cbm         = g.Sum(x => x.CBM),
                        packagesNum = g.Sum(x => x.NumberOfPackages),
                        freightCost = g.Sum(x => x.CollectedFreightCost),
                        thcCost     = g.Sum(x => x.CollectedThcCost)
                    }).FirstOrDefault();

                    if (operationDb.NetWeight != null)
                    {
                        nw = operationDb.NetWeight.Value - pervSum.nw.Value;
                    }
                    if (operationDb.GrossWeight != null)
                    {
                        gw = operationDb.GrossWeight.Value - pervSum.gw.Value;
                    }
                    if (operationDb.CBM != null)
                    {
                        cbm = operationDb.CBM.Value - pervSum.cbm.Value;
                    }

                    if (operationDb.NumberOfPackages != null)
                    {
                        packagesNum = operationDb.NumberOfPackages.Value - pervSum.packagesNum.Value;
                    }

                    if (operationDb.FreightCostAmount != null)
                    {
                        freightCost = operationDb.FreightCostAmount.Value - pervSum.freightCost.Value;
                    }
                    if (operationDb.ThcCostAmount != null)
                    {
                        thcCost = operationDb.ThcCostAmount.Value - pervSum.thcCost.Value;
                    }

                    houseBillVm.NetWeight            = nw;
                    houseBillVm.GrossWeight          = gw;
                    houseBillVm.CBM                  = cbm;
                    houseBillVm.NumberOfPackages     = packagesNum;
                    houseBillVm.CollectedFreightCost = freightCost;
                    houseBillVm.CollectedThcCost     = thcCost;
                }
            }
            else
            {
                Mapper.CreateMap <HouseBill, HouseBillVm>().IgnoreAllNonExisting();
                Mapper.Map(houseBillDb, houseBillVm);
                //get departure and arrive date
                var operObj = db.Operations
                              .Where(x => x.OperationId == operationId)
                              .Select(x => new { x.DateOfDeparture, x.ArriveDate, x.OperationDate }).FirstOrDefault();

                houseBillVm.DateOfDeparture  = operObj.DateOfDeparture;
                houseBillVm.ArriveDate       = operObj.ArriveDate;
                houseBillVm.OperationMinDate = operObj.OperationDate;
            }

            return(houseBillVm);
        }
Ejemplo n.º 4
0
        public static string AddEditOperation(OperationVm operationVm, out int operId)
        {
            string                    isSaved     = "true";
            int                       operationId = operationVm.OperationId;
            OperationsEntities        db          = new OperationsEntities();
            Operation                 operationDb;
            List <OperationContainer> operationContListDb;
            // notifications
            NotificationMsgVm notifi = new NotificationMsgVm();

            if (operationId == 0)
            {
                operationDb         = new Operation();
                operationContListDb = new List <OperationContainer>();
            }
            else
            {
                operationDb = db.Operations.Include("OperationContainers")
                              .Where(x => x.OperationId == operationId).FirstOrDefault();
                if (operationDb.StatusId > 2)
                {
                    operId  = operationDb.OperationId;
                    isSaved = "false .. Cann't update operation is not open ";
                    return(isSaved);
                }
                operationContListDb = operationDb.OperationContainers.ToList();

                //Get quotContainers Ids sent from the screen
                List <long> containerVmIds = operationVm.OperationContainers.Select(x => x.OperConId).ToList();
                var         containerDel   = operationContListDb.Where(x => !containerVmIds.Contains(x.OperConId)).ToList();

                foreach (var item in containerDel)
                {
                    db.OperationContainers.Remove(item);
                }
            }

            Mapper.CreateMap <OperationVm, Operation>().IgnoreAllNonExisting();
            // .ForMember(x => x.OperationContainers, v=> v.Ignore());
            Mapper.CreateMap <OperationContainerVm, OperationContainer>().IgnoreAllNonExisting();
            Mapper.Map(operationVm, operationDb);

            bool updateHB = false;

            if (operationId == 0)
            {
                //Generate code at save event
                if (operationDb.OrderFrom == 1)
                {
                    operationDb.OperationCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.Export, true);
                }
                else
                {
                    operationDb.OperationCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.Import, true);
                }

                db.Operations.Add(operationDb);
                //update quotation status if any
                int?quoteId = operationVm.QuoteId;
                if (quoteId != null)
                {
                    //status = 2 -- opened
                    Quotation quoteToUpdate = db.Quotations.Where(x => x.QuoteId == quoteId).FirstOrDefault();
                    quoteToUpdate.StatusId = 2;
                }

                notifi.NotificationTypeID = (operationDb.OrderFrom == 1) ? 1 : 3;
                notifi.ObjectID           = -1;
            }
            else
            {
                List <HouseBillListVm> hbList = HouseBillHelper.GetHBList(operationId, operationDb.OrderFrom);
                if (hbList.Count == 1)
                {
                    if (!operationDb.IsConsolidation)
                    {
                        HouseBill hb = db.HouseBills.Where(x => x.OperationId == operationDb.OperationId).FirstOrDefault();
                        hb.CBM = operationDb.CBM;
                        hb.GoodsDescription     = operationDb.GoodsDescription;
                        hb.GrossWeight          = operationDb.GrossWeight;
                        hb.NetWeight            = operationDb.NetWeight;
                        hb.NumberOfPackages     = operationDb.NumberOfPackages;
                        hb.CollectedFreightCost = operationDb.FreightCostAmount;
                        hb.CollectedThcCost     = operationDb.ThcCostAmount;



                        // hb.ShipperId = operationDb.ShipperId;
                        //  hb.ConsigneeId = operationDb.ConsigneeId;
                        // hb.NotifierId = operationDb.NotifierId;
                        //  hb.NotifierAsConsignee = operationDb.NotifierAsConsignee;
                        //  hb.AgentId = operationDb.AgentId;

                        hb.FromPortId = operationDb.FromPortId;
                        hb.ToPortId   = operationDb.ToPortId;
                    }
                }
            }
            operId = 0;
            try
            {
                db.SaveChanges();
                operId = operationDb.OperationId;
                if (notifi.ObjectID == -1)
                {
                    notifi.ObjectID        = operId;
                    notifi.NotificationMsg = " New " + (operationDb.OrderFrom == 1 ? "Export " : "Import ") + " Operation Code: " + operationDb.OperationCode;
                    NotificationHelper.Create_Notification(notifi);
                }

                //update hb
                if (updateHB)
                {
                }
            }
            catch (DbEntityValidationException e)
            {
                isSaved = "false " + e.Message;
            }
            catch (Exception e)
            {
                isSaved = "false " + e.Message;
            }

            return(isSaved);
        }