public void HandleEvent(OrderPaidEvent eventMessage)
        {
            var order     = eventMessage.Order;
            var vendorIds = order.OrderItems.Select(m => m.Product.VendorId).Distinct();

            //to store the vendor based order items
            var vendorItems = new Dictionary <int, IList <OrderItem> >();
            //to store the vendors in a dictionary because there may be more than one vendors in one order
            var vendorsExtended = new Dictionary <int, Domain.ExtendedVendor>();

            foreach (var vid in vendorIds)
            {
                vendorItems.Add(vid, order.OrderItems.Where(m => m.Product.VendorId == vid).ToList());
                var ex = _extendedVendorService.GetExtendedVendor(vid);
                vendorsExtended.Add(vid, ex);
            }

            foreach (var vi in vendorItems)
            {
                var vendorId   = vi.Key;
                var orderItems = vi.Value;

                var orderItemTotal = decimal.Zero;
                var discountTotal  = decimal.Zero;
                if (_taxSettings.PricesIncludeTax)
                {
                    orderItemTotal = orderItems.Sum(m => m.PriceInclTax);
                    discountTotal  = orderItems.Sum(m => m.DiscountAmountInclTax);
                }
                else
                {
                    orderItemTotal = orderItems.Sum(m => m.PriceExclTax);
                    discountTotal  = orderItems.Sum(m => m.DiscountAmountExclTax);
                }
                orderItemTotal = orderItemTotal - discountTotal;

                //create a new payout for each vendor
                var vendorPayout = new VendorPayout
                {
                    VendorId             = vendorId,
                    CommissionPercentage =
                        vendorsExtended[vendorId] == null
                            ? _extendedVendorSettings.DefaultCommissionPercentage
                            : vendorsExtended[vendorId].CommissionPercentage,
                    OrderId          = order.Id,
                    PayoutDate       = null,
                    Remarks          = "",
                    VendorOrderTotal = orderItemTotal,
                    PayoutStatus     = PayoutStatus.Pending,
                    ShippingCharge   =
                        (vendorsExtended[vendorId] == null
                            ? _extendedVendorSettings.DefaultShippingCharge
                            : vendorsExtended[vendorId].ShippingCharge) * orderItems.Count
                };

                _extendedVendorService.SaveVendorPayout(vendorPayout);
            }
        }
        public void HandleEvent(OrderCancelledEvent eventMessage)
        {
            var order   = eventMessage.Order;
            var payouts = _extendedVendorService.GetVendorPayoutsByOrder(order.Id);

            //mark each payout as cancelled as order has been cancelled
            foreach (var p in payouts)
            {
                p.PayoutStatus = PayoutStatus.Cancelled;
                p.Remarks     += " (Order cancelled on " + DateTime.Now.ToString("dd MMM yyyy") + ")";
                _extendedVendorService.SaveVendorPayout(p);
            }
        }
        public ActionResult UpdatePayout(VendorPayoutModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageVendors))
            {
                return(Content("Access denied"));
            }


            var payout = _extendedVendorService.GetVendorPayout(model.Id);

            if (payout != null)
            {
                payout.CommissionPercentage = model.CommissionPercentage;
                payout.PayoutDate           = model.PayoutDate;
                payout.PayoutStatus         = model.PayoutStatus;
                payout.Remarks          = model.Remarks;
                payout.VendorOrderTotal = model.VendorOrderTotal;
                payout.ShippingCharge   = model.ShippingCharge;
                _extendedVendorService.SaveVendorPayout(payout);
            }
            return(new NullJsonResult());
        }