Ejemplo n.º 1
0
        public Order Add(int clientId, int vendorId, int?accountId, int approverId, DateTime neededDate, bool oversized, int shippingMethodId, string notes, bool attention)
        {
            /*
             * INSERT INTO dbo.PurchaseOrder (ClientID, AccountID, VendorID, CreatedDate, NeededDate, ApproverID, Oversized, ShippingMethodID, Notes, Attention, StatusID)
             * VALUES (@ClientID, @AccountID, @VendorID, @CreatedDate, @NeededDate, @ApproverID, @Oversized, @ShippingMethodID, @Notes, @Attention, 1)
             *
             * 1 = Status.Draft
             */

            var po = new Ordering.PurchaseOrder()
            {
                Client         = Require <Data.Client>(x => x.ClientID, clientId),
                AccountID      = accountId,
                Vendor         = Require <Ordering.Vendor>(x => x.VendorID, vendorId),
                CreatedDate    = DateTime.Now,
                NeededDate     = neededDate,
                Approver       = Require <Data.Client>(x => x.ClientID, approverId),
                Oversized      = oversized,
                ShippingMethod = Require <Ordering.ShippingMethod>(x => x.ShippingMethodID, shippingMethodId),
                Notes          = notes,
                Attention      = attention,
                Status         = GetStatus(OrderStatus.Draft),
                Details        = new List <Ordering.PurchaseOrderDetail>()
            };

            DataSession.Insert(po);

            Tracking.Track(TrackingCheckpoints.DraftCreated, po.CreateModel <IPurchaseOrder>(), clientId, new { VendorID = vendorId, AccountID = accountId, ApproverID = approverId });

            return(CreateOrder(po));
        }
Ejemplo n.º 2
0
        private Data.Client GetApproverForCopy(Ordering.PurchaseOrder po)
        {
            var currentClientId = Context.CurrentUser.ClientID;

            Data.Client approver;

            if (po.Client.ClientID != currentClientId)
            {
                // check if the current user has the same approver
                var app = DataSession.Query <Ordering.Approver>().FirstOrDefault(x => x.ApproverID == po.Approver.ClientID && x.ClientID == currentClientId);

                if (app == null)
                {
                    // use the current user's primary approver
                    var defapp = DataSession.Query <Ordering.Approver>().FirstOrDefault(x => x.Active && x.ClientID == currentClientId && x.IsPrimary);

                    if (defapp == null)
                    {
                        // fall-back: copy the po approver
                        app = new Ordering.Approver()
                        {
                            Active     = true,
                            ApproverID = po.Approver.ClientID,
                            ClientID   = currentClientId,
                            IsPrimary  = true //because the current user has no active primary at the moment
                        };

                        DataSession.Insert(app);

                        approver = po.Approver;
                    }
                    else
                    {
                        approver = Require <Data.Client>(x => x.ClientID, defapp.ApproverID);
                    }
                }
                else
                {
                    app.Active = true; //just in case
                    approver   = Require <Data.Client>(x => x.ClientID, app.ApproverID);
                }
            }
            else
            {
                approver = po.Approver;
            }

            return(approver);
        }
Ejemplo n.º 3
0
        private Ordering.Vendor GetVendorForCopy(Ordering.PurchaseOrder po)
        {
            var currentClientId = Context.CurrentUser.ClientID;

            Ordering.Vendor vendor;

            if (po.Client.ClientID == currentClientId || po.Vendor.ClientID == 0)
            {
                // current user is copying own order, or store manager order
                vendor = po.Vendor;
            }
            else
            {
                // current user is copying another user's order

                // check for a vendor for the current user that has the same name as the po vendor
                vendor = DataSession.Query <Ordering.Vendor>().Where(x => x.ClientID == currentClientId).ToList().FirstOrDefault(x =>
                                                                                                                                 PurchaseOrderItems.CleanString(x.VendorName) == PurchaseOrderItems.CleanString(po.Vendor.VendorName));

                if (vendor == null)
                {
                    // make a copy
                    vendor = new Ordering.Vendor()
                    {
                        Active     = true,
                        Address1   = po.Vendor.Address1,
                        Address2   = po.Vendor.Address2,
                        Address3   = po.Vendor.Address3,
                        ClientID   = currentClientId,
                        Contact    = po.Vendor.Contact,
                        Email      = po.Vendor.Email,
                        Fax        = po.Vendor.Fax,
                        Items      = new List <Ordering.PurchaseOrderItem>(),
                        Phone      = po.Vendor.Phone,
                        URL        = po.Vendor.URL,
                        VendorName = po.Vendor.VendorName
                    };

                    DataSession.Insert(vendor);
                }
                else
                {
                    vendor.Active = true; //just in case
                }
            }

            return(vendor);
        }
Ejemplo n.º 4
0
        public Order Copy(int poid, int?accountId = null)
        {
            //INSERT INTO dbo.PurchaseOrder (ClientID, AccountID, VendorID, CreatedDate, NeededDate, ApproverID, Oversized, ShippingMethodID, Notes, Attention, StatusID)
            //SELECT ClientID, AccountID, VendorID, GETDATE(), DATEADD(DAY, 7, GETDATE()), ApproverID, Oversized, ShippingMethodID, Notes, Attention, 1

            // get the po to be copied
            var po = Require <Ordering.PurchaseOrder>(x => x.POID, poid);

            // po may be for a different user than current, this will get the correct vendor and approver - making copies if necessary
            var vendor   = GetVendorForCopy(po);
            var approver = GetApproverForCopy(po);

            var copy = new Ordering.PurchaseOrder()
            {
                Client         = Require <Data.Client>(x => x.ClientID, Context.CurrentUser.ClientID),
                AccountID      = accountId ?? po.AccountID,
                Vendor         = vendor,
                CreatedDate    = DateTime.Now,
                NeededDate     = DateTime.Now.AddDays(7),
                Approver       = approver,
                Oversized      = po.Oversized,
                ShippingMethod = po.ShippingMethod,
                Notes          = po.Notes,
                Attention      = po.Attention,
                Status         = GetStatus(OrderStatus.Draft)
            };

            DataSession.Insert(copy);

            // po may be for a different user than current, this will get the correct details - making item copies if necessary
            var details = GetDetailsForCopy(po, copy, vendor);

            DataSession.Insert(details);

            copy.Details = details;

            Tracking.Track(TrackingCheckpoints.DraftCreated, copy.CreateModel <IPurchaseOrder>(), Context.CurrentUser.ClientID);

            return(CreateOrder(copy));
        }
Ejemplo n.º 5
0
        private static Order CreateOrder(Ordering.PurchaseOrder po)
        {
            if (po == null)
            {
                return(null);
            }

            return(new Order()
            {
                POID = po.POID,
                ClientID = po.Client.ClientID,
                DisplayName = po.Client.DisplayName,
                VendorID = po.Vendor.VendorID,
                VendorName = po.Vendor.VendorName,
                AccountID = po.AccountID,
                ApproverID = po.Approver.ClientID,
                ApproverName = po.Approver.DisplayName,
                CreatedDate = po.CreatedDate,
                NeededDate = po.NeededDate,
                Oversized = po.Oversized,
                ShippingMethodID = po.ShippingMethod.ShippingMethodID,
                ShippingMethodName = po.ShippingMethod.ShippingMethodName,
                Notes = po.Notes,
                StatusID = po.Status.StatusID,
                StatusName = po.Status.StatusName,
                CompletedDate = po.CompletedDate,
                RealApproverID = po.RealApproverID,
                ApprovalDate = po.ApprovalDate,
                Attention = po.Attention,
                PurchaserID = po.PurchaserID,
                RealPO = po.RealPO,
                ReqNum = po.ReqNum,
                PurchaserNotes = po.PurchaserNotes,
                TotalPrice = po.Details.Sum(x => (double?)(x.Quantity * x.UnitPrice)).GetValueOrDefault(0)
            });
        }
Ejemplo n.º 6
0
        private IList <Ordering.PurchaseOrderDetail> GetDetailsForCopy(Ordering.PurchaseOrder po, Ordering.PurchaseOrder copy, Ordering.Vendor vendor)
        {
            var currentClientId = Context.CurrentUser.ClientID;

            IList <Ordering.PurchaseOrderDetail> details;

            if (po.Client.ClientID == currentClientId || po.Vendor.ClientID == 0)
            {
                // current user is copying own order, or store manager vendor
                details = po.Details.Select(x => new Ordering.PurchaseOrderDetail()
                {
                    Category = x.Category,
                    IsInventoryControlled = x.IsInventoryControlled,
                    Item            = x.Item,
                    PurchaseOrder   = copy,
                    Quantity        = x.Quantity,
                    ToInventoryDate = null,
                    Unit            = x.Unit,
                    UnitPrice       = x.UnitPrice
                }).ToList();
            }
            else
            {
                // current user is copying another user's order

                details = new List <Ordering.PurchaseOrderDetail>();

                // check for items for the current user that have the same description and partnum as the po detail items
                foreach (var d in po.Details)
                {
                    var i = vendor.Items.FirstOrDefault(x =>
                                                        PurchaseOrderItems.CleanString(x.Description) == PurchaseOrderItems.CleanString(d.Item.Description) &&
                                                        PurchaseOrderItems.CleanString(x.PartNum) == PurchaseOrderItems.CleanString(d.Item.PartNum));

                    if (i == null)
                    {
                        // make a copy of the item
                        i = new Ordering.PurchaseOrderItem()
                        {
                            Active          = true,
                            Description     = d.Item.Description,
                            Details         = new List <Ordering.PurchaseOrderDetail>(),
                            InventoryItemID = d.Item.InventoryItemID,
                            PartNum         = d.Item.PartNum,
                            UnitPrice       = d.Item.UnitPrice,
                            Vendor          = vendor
                        };

                        DataSession.Insert(i);

                        vendor.Items.Add(i);
                    }
                    else
                    {
                        i.Active = true; //just in case
                    }

                    var detail = new Ordering.PurchaseOrderDetail()
                    {
                        Category = d.Category,
                        IsInventoryControlled = d.IsInventoryControlled,
                        Item            = i,
                        PurchaseOrder   = copy,
                        Quantity        = d.Quantity,
                        ToInventoryDate = null,
                        Unit            = d.Unit,
                        UnitPrice       = d.UnitPrice
                    };

                    details.Add(detail);
                }
            }

            return(details);
        }