Esempio n. 1
0
        public void AddCommentAndAttachment(Guid workOrderId, Guid technicianId, string comment, HttpPostedFileBase attachment)
        {
            bool hasAttachment = attachment != null && attachment.ContentLength > 0;

            if (String.IsNullOrWhiteSpace(comment) && !hasAttachment)
            {
                return;
            }

            string techName = _technicianService.GetTechnicianName(technicianId);

            var annotation = new Annotation
            {
                NoteText = String.IsNullOrWhiteSpace(comment) ? techName : String.Format("{0} - {1}", techName, comment),
                ObjectId = new EntityReference(Incident.EntityLogicalName, workOrderId)
            };

            if (hasAttachment)
            {
                annotation.FileName     = attachment.GetNonEmptyFileName();
                annotation.DocumentBody = attachment.ConvertToBase64();
            }

            _context.AddObject(annotation);
            _context.SaveChanges();
        }
Esempio n. 2
0
        public void SetWorkItemStatus(Guid workItemId, Guid technicianId, bool isComplete, HttpPostedFileBase note)
        {
            bool hasAttachment = note != null && note.ContentLength > 0;
            Dictionary <string, int> workItemStatuses = GetWorkItemStatuses();

            _context.Execute(new SetStateRequest
            {
                EntityMoniker = new EntityReference(ars_workitem.EntityLogicalName, workItemId),
                State         = new OptionSetValue((int)(isComplete ? ars_workitemState.Inactive : ars_workitemState.Active)),
                Status        = new OptionSetValue(workItemStatuses[isComplete ? "Complete" : "Incomplete"])
            });

            if (hasAttachment)
            {
                string techName = _technicianService.GetTechnicianName(technicianId);

                var annotation = new Annotation
                {
                    NoteText     = String.Format("{0} changed status of work item to '{1}'", techName, isComplete ? "Complete" : "Incomplete"),
                    ObjectId     = new EntityReference(ars_workitem.EntityLogicalName, workItemId),
                    FileName     = note.GetNonEmptyFileName(),
                    DocumentBody = note.ConvertToBase64()
                };

                _context.AddObject(annotation);
                _context.SaveChanges();
            }
        }
Esempio n. 3
0
        public void SubmitPurchaseOrderRequest(Guid workOrderId, Guid technicianId, OrderItemModel[] orderItems, HttpPostedFileBase purchaseOrderReceipt, HttpPostedFileBase purchaseOrderReceipt2, string vendor, string store, string card)
        {
            ClientCredentials credential = new ClientCredentials();

            credential.UserName.UserName = "******";
            credential.UserName.Password = "******";
            Guid defaultUnit      = new Guid("054ED7A8-A1AE-4D5F-9108-60D387606730");
            Guid defaultUnitGroup = new Guid("682A0D63-FD3D-45DD-8C28-C2F5EB546ACE");
            Guid defaultPriceList = new Guid("2344F7E8-8FD3-E711-810F-E0071B66CFA1");
            Guid productId;
            Guid priceListId;

            // Set the org url
            var organizationURI          = "https://advancedretail.crm.dynamics.com/XRMServices/2011/Organization.svc";
            OrganizationServiceProxy svc = new OrganizationServiceProxy(new Uri(organizationURI), null, credential, null);

            svc.EnableProxyTypes();

            if (orderItems == null)
            {
                throw new ArgumentNullException("orderItems");
            }

            orderItems.ForEach(VaildateOrderItem);

            string techName  = _technicianService.GetTechnicianName(technicianId);
            var    workOrder = _context.IncidentSet.Single(w => w.IncidentId == workOrderId);


            //Using ticks as a way to have unique PO #'s. This does not "guarantee" a different number
            //in the case that two work orders are created at the EXACT same time, but the chance that
            //happens is miniscule.

            //long time = DateTime.Now.Ticks;
            //String fileName = Convert.ToString(time);

            var chars       = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var numbers     = "0123456789";
            var stringChars = new char[8];
            var random      = new Random();

            for (int i = 0; i < 2; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }
            for (int i = 2; i < 6; i++)
            {
                stringChars[i] = numbers[random.Next(numbers.Length)];
            }
            for (int i = 6; i < 8; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }

            var fileName = new String(stringChars);

            foreach (OrderItemModel orderItem in orderItems)
            {
                OptionSetValue myOptionSet = new OptionSetValue();
                if (vendor == "Visa")
                {
                    myOptionSet.Value = 100000000;
                }
                else if (vendor == "Vendor 2")
                {
                    myOptionSet.Value = 100000001;
                }
                else if (vendor == "Vendor 3")
                {
                    myOptionSet.Value = 100000002;
                }
                else if (vendor == "Vendor 4")
                {
                    myOptionSet.Value = 100000003;
                }
                else if (vendor == "Vendor 5")
                {
                    myOptionSet.Value = 100000004;
                }
                else
                {
                    myOptionSet.Value = 100000000;
                }

                OptionSetValue cardBool = new OptionSetValue();
                if (card == "Yes")
                {
                    cardBool.Value = 100000000;
                }
                else
                {
                    cardBool.Value = 100000001;
                }

                var item1 = new ars_technicianitem
                {
                    ars_Price       = new Money(orderItem.Price),
                    ars_description = orderItem.Item,
                    ars_Quantity    = orderItem.Quantity,
                    ars_OrderId     = workOrder.ars_Order,
                    new_PONumber    = fileName
                };

                _context.AddObject(item1);

                //var item = new SalesOrderDetail
                //// This is not creating a new Order, but rather adding line items to the Order.
                ////This is the difference between the Sales Order and Sales Order Detail.
                //{
                //    PricePerUnit = new Money(orderItem.RealPrice),
                //    ProductDescription = orderItem.Item,
                //    IsPriceOverridden = true,
                //    Quantity = orderItem.Quantity,
                //    SalesOrderId = workOrder.ars_Order,
                //    new_ponumber = fileName,
                //    IsProductOverridden = true,
                //    new_vendor = myOptionSet,
                //    new_storename = store,
                //    new_receipt = receiptBool,
                //    new_Cost = new Money(orderItem.RealPrice),
                //    new_ExtCost = new Money(orderItem.RealPrice*orderItem.Quantity),
                //    new_RetailPrice = new Money(orderItem.beforeTaxPrice),
                //    new_technician = techName,
                //    new_card = cardBool,
                //    new_date = DateTime.Now.ToString(),
                //};

                QueryByAttribute query = new QueryByAttribute("product");
                query.ColumnSet = new ColumnSet("productid", "defaultuomid");
                query.Attributes.AddRange("name");
                query.Values.AddRange(orderItem.Item);
                Entity productent = svc.RetrieveMultiple(query).Entities.ToList().FirstOrDefault();
                if (productent == null)
                {
                    //Entity productent_new = new Entity("product");
                    var newProduct = new Product
                    {
                        DefaultUoMId         = new EntityReference(UoM.EntityLogicalName, defaultUnit),
                        DefaultUoMScheduleId = new EntityReference(UoMSchedule.EntityLogicalName, defaultUnitGroup),
                        QuantityDecimal      = 2,
                        ProductNumber        = orderItem.Item,
                        Name        = orderItem.Item,
                        Description = orderItem.Item,
                    };

                    productId = svc.Create(newProduct);

                    //Create price list item
                    ProductPriceLevel newPriceListItem = new ProductPriceLevel
                    {
                        PriceLevelId = new EntityReference(PriceLevel.EntityLogicalName, defaultPriceList),
                        ProductId    = new EntityReference(Product.EntityLogicalName, productId),
                        UoMId        = new EntityReference(UoM.EntityLogicalName, defaultUnit),
                        Amount       = new Money(orderItem.RealPrice)
                    };

                    priceListId = svc.Create(newPriceListItem);
                }
                else
                {
                    //Grab ProductId from productent if it is not null
                    productId   = (Guid)productent.Attributes["productid"];
                    defaultUnit = ((EntityReference)productent.Attributes["defaultuomid"]).Id;
                }

                //Create SalesOrderDetail
                var item = new SalesOrderDetail
                {
                    PricePerUnit        = new Money(orderItem.RealPrice),
                    IsPriceOverridden   = true,
                    Quantity            = orderItem.Quantity,
                    SalesOrderId        = workOrder.ars_Order,
                    new_ponumber        = fileName,
                    IsProductOverridden = false,
                    //new_vendor = myOptionSet,
                    //new_storename = store,
                    //new_receipt = receiptBool,
                    new_Cost        = new Money(orderItem.RealPrice),
                    new_ExtCost     = new Money(orderItem.RealPrice * orderItem.Quantity),
                    new_RetailPrice = new Money(orderItem.beforeTaxPrice),
                    new_technician  = techName,
                    //new_card = cardBool,
                    new_date  = DateTime.Now.ToString(),
                    ProductId = new EntityReference(Product.EntityLogicalName, productId),
                    UoMId     = new EntityReference(UoM.EntityLogicalName, defaultUnit)
                };

                _context.AddObject(item);
            }

            if (purchaseOrderReceipt != null)
            {
                var annotation = new Annotation
                {
                    NoteText     = String.Format("Purchase order receipt from {0}", techName),
                    ObjectId     = workOrder.ars_Order,
                    FileName     = purchaseOrderReceipt.GetNonEmptyFileName(),
                    MimeType     = purchaseOrderReceipt.ContentType,
                    DocumentBody = purchaseOrderReceipt.ConvertToBase64()
                };

                _context.AddObject(annotation);
            }

            if (purchaseOrderReceipt2 != null)
            {
                var annotation = new Annotation
                {
                    NoteText     = String.Format("Purchase order receipt from {0}", techName),
                    ObjectId     = workOrder.ars_Order,
                    FileName     = purchaseOrderReceipt2.GetNonEmptyFileName(),
                    MimeType     = purchaseOrderReceipt2.ContentType,
                    DocumentBody = purchaseOrderReceipt2.ConvertToBase64()
                };

                _context.AddObject(annotation);
            }

            _context.SaveChanges();
        }