Exemplo n.º 1
0
        public ActionResult GetProductsList()
        {
            string         query       = Request.QueryString["q[term]"] != null ? Request.QueryString["q[term]"].ToString() : "";
            List <Product> ProductList = productsFacade.GetAllProductsbyQuery(query);

            foreach (var item in ProductList.ToList())
            {
                List <ProductWarehouseMap> pwmList = pWMFacade.GetAll().Where(x => x.ProductId == item.ProductId).ToList();
                if (pwmList.Count > 0)
                {
                    item.RemainQantity = item.Quantity - pwmList.Sum(x => Convert.ToInt32(x.Quantity));
                }
                else
                {
                    item.RemainQantity = item.Quantity;
                }
                if (item.RemainQantity <= 0)
                {
                    ProductList.Remove(item);
                }
                if (ProductList.Count() <= 0)
                {
                    break;
                }
            }
            var CarNameList = ToDataTable(ProductList);

            List <Dictionary <string, object> >
            lstRows = new List <Dictionary <string, object> >();
            Dictionary <string, object> dictRow = null;

            foreach (DataRow dr in CarNameList.Rows)
            {
                dictRow = new Dictionary <string, object>();
                foreach (DataColumn col in CarNameList.Columns)
                {
                    dictRow.Add(col.ColumnName, dr[col]);
                }
                lstRows.Add(dictRow);
            }

            return(Json(lstRows, JsonRequestBehavior.AllowGet));
        }
        public ActionResult AddInvoice(SalesOrderModel SalesOrderModel)
        {
            SalesOrder salesOrder = new SalesOrder();
            bool       result     = false;
            string     massege    = "";

            try
            {
                if (SalesOrderModel.SalesOrder.Id == 0)
                {
                    if (SalesOrderModel.SalesOrder.PaymentAmount >= 0 && SalesOrderModel.SalesOrder.Total >= SalesOrderModel.SalesOrder.PaymentAmount)
                    {
                        SalesOrderModel.SalesOrder.SalesOrderId = Guid.NewGuid();
                        SalesOrderModel.SalesOrder.CreatedDate  = DateTime.Now;
                        SalesOrderModel.SalesOrder.WarehouseId  = Guid.NewGuid();
                        SalesOrderModel.SalesOrder.OrderDate    = DateTime.Now;
                        SalesOrderModel.SalesOrder.DelivaryDate = DateTime.Now;
                        SalesOrderModel.SalesOrder.SubTotal     = SalesOrderModel.SalesOrder.Amount - SalesOrderModel.SalesOrder.PaymentAmount;
                        salesFacade.Insert(SalesOrderModel.SalesOrder);
                        if (SalesOrderModel.SalesOrderDetails.Count > 0)
                        {
                            foreach (var item in SalesOrderModel.SalesOrderDetails)
                            {
                                item.SalesOrderDetailId = Guid.NewGuid();
                                item.SalesOrderId       = SalesOrderModel.SalesOrder.SalesOrderId;
                                #region Product Deduct
                                ProductWarehouseMap checkWarehouseProduct = pWMFacade.GetAll().Where(x => x.ProductId == item.ProductId && x.WarehouseId == item.WarehouseId).FirstOrDefault();
                                checkWarehouseProduct.Quantity = checkWarehouseProduct.Quantity - item.Quantity;
                                pWMFacade.Update(checkWarehouseProduct);

                                Product oldProduct = productsFacade.GetByProductId(item.ProductId);
                                oldProduct.Quantity = oldProduct.Quantity - item.Quantity;
                                productsFacade.Update(oldProduct);
                                #endregion
                                salesDetailFacade.Insert(item);
                            }
                        }

                        #region PaymentReceive

                        PaymentReceive payment = new PaymentReceive();
                        payment.PaymentId     = Guid.NewGuid();
                        payment.SalesOrderId  = SalesOrderModel.SalesOrder.SalesOrderId;
                        payment.BalanceDue    = SalesOrderModel.SalesOrder.Amount - SalesOrderModel.SalesOrder.PaymentAmount;
                        payment.PaymentAmount = SalesOrderModel.SalesOrder.PaymentAmount;
                        if (payment.BalanceDue == 0)
                        {
                            payment.PaymentStatus = "Paid";
                        }
                        else if (payment.BalanceDue == SalesOrderModel.SalesOrder.Amount)
                        {
                            payment.PaymentStatus = "UnPaid";
                        }
                        else
                        {
                            payment.PaymentStatus = "Partialy Paid";
                        }

                        payment.PaymentDate = SalesOrderModel.SalesOrder.PaymentDate != null ? SalesOrderModel.SalesOrder.PaymentDate.Value : DateTime.Now;
                        payment.Note        = SalesOrderModel.SalesOrder.PaymentNote;
                        payFacade.Insert(payment);
                    }
                    else
                    {
                        result  = false;
                        massege = "Payment amount should be less or equal to the Total amount";
                        return(Json(new { result = result, message = massege }));
                    }
                    #endregion
                }
                else
                {
                    PaymentReceive oldPayment = payFacade.GetPaymentBySOId(SalesOrderModel.SalesOrder.SalesOrderId);
                    SalesOrder     sales      = salesFacade.Get(SalesOrderModel.SalesOrder.Id);
                    if (SalesOrderModel.SalesOrder.PaymentAmount >= 0 && sales.Amount >= (oldPayment.PaymentAmount + SalesOrderModel.SalesOrder.PaymentAmount))
                    {
                        sales.OrderDate      = SalesOrderModel.SalesOrder.OrderDate;
                        sales.DelivaryDate   = SalesOrderModel.SalesOrder.DelivaryDate;
                        sales.DiscountAmount = SalesOrderModel.SalesOrder.DiscountAmount;
                        sales.Amount         = SalesOrderModel.SalesOrder.Amount;
                        sales.Freight        = SalesOrderModel.SalesOrder.Freight;
                        sales.Total          = SalesOrderModel.SalesOrder.Total;
                        salesFacade.Update(sales);
                        List <SalesOrderDetailVM> salesdetaillist = salesDetailFacade.GetAllSalesDetailsBySaleOrderId(sales.SalesOrderId);
                        if (SalesOrderModel.SalesOrderDetails.Count > 0)
                        {
                            foreach (var item in salesdetaillist)
                            {
                                ProductWarehouseMap productWarehouseMap = pWMFacade.GetAll().Where(x => x.ProductId == item.ProductId && x.WarehouseId == item.WarehouseId).FirstOrDefault();
                                if (productWarehouseMap != null)
                                {
                                    productWarehouseMap.Quantity = productWarehouseMap.Quantity + item.Quantity;
                                    pWMFacade.Update(productWarehouseMap);
                                }

                                Product oldProduct = productsFacade.GetByProductId(item.ProductId);
                                if (oldProduct != null)
                                {
                                    oldProduct.Quantity = oldProduct.Quantity + item.Quantity;
                                    productsFacade.Update(oldProduct);
                                }

                                salesDetailFacade.Delete(item.Id);
                            }
                            foreach (var item in SalesOrderModel.SalesOrderDetails)
                            {
                                SalesOrderDetail tempSalesOrderDetail = new SalesOrderDetail();
                                tempSalesOrderDetail.Price              = item.Price;
                                tempSalesOrderDetail.ProductId          = item.ProductId;
                                tempSalesOrderDetail.Quantity           = item.Quantity;
                                tempSalesOrderDetail.SalesOrderDetailId = Guid.NewGuid();
                                tempSalesOrderDetail.SalesOrderId       = SalesOrderModel.SalesOrder.SalesOrderId;
                                tempSalesOrderDetail.SubTotal           = item.SubTotal;
                                tempSalesOrderDetail.Total              = item.Total;
                                tempSalesOrderDetail.Amount             = item.Amount;
                                tempSalesOrderDetail.WarehouseId        = item.WarehouseId;

                                #region Product Deduct
                                ProductWarehouseMap checkWarehouseProduct = pWMFacade.GetAll().Where(x => x.ProductId == item.ProductId && x.WarehouseId == item.WarehouseId).FirstOrDefault();
                                if (checkWarehouseProduct != null)
                                {
                                    checkWarehouseProduct.Quantity = checkWarehouseProduct.Quantity - item.Quantity;
                                    pWMFacade.Update(checkWarehouseProduct);
                                }

                                Product oldProduct = productsFacade.GetByProductId(item.ProductId);
                                if (oldProduct != null)
                                {
                                    oldProduct.Quantity = oldProduct.Quantity - item.Quantity;
                                    productsFacade.Update(oldProduct);
                                }
                                #endregion

                                salesDetailFacade.Insert(tempSalesOrderDetail);
                            }
                        }

                        #region PaymentReceive



                        oldPayment.BalanceDue    = SalesOrderModel.SalesOrder.Amount - SalesOrderModel.SalesOrder.PaymentAmount;
                        oldPayment.PaymentAmount = (oldPayment.PaymentAmount + SalesOrderModel.SalesOrder.PaymentAmount);
                        oldPayment.BalanceDue    = SalesOrderModel.SalesOrder.Amount - oldPayment.PaymentAmount;
                        sales.SubTotal           = oldPayment.BalanceDue;
                        salesFacade.Update(sales);

                        if (oldPayment.BalanceDue == 0)
                        {
                            oldPayment.PaymentStatus = "Paid";
                        }
                        else if (oldPayment.BalanceDue == SalesOrderModel.SalesOrder.Amount)
                        {
                            oldPayment.PaymentStatus = "UnPaid";
                        }
                        else
                        {
                            oldPayment.PaymentStatus = "Partially Paid";
                        }

                        oldPayment.PaymentDate = SalesOrderModel.SalesOrder.PaymentDate != null ? SalesOrderModel.SalesOrder.PaymentDate.Value : DateTime.Now;
                        oldPayment.Note        = SalesOrderModel.SalesOrder.PaymentNote;
                        payFacade.Update(oldPayment);
                    }
                    else
                    {
                        result  = false;
                        massege = "Payment amount should be less or equal to the Total amount";
                        return(Json(new { result = result, message = massege }));
                    }
                    #endregion
                }
                result  = true;
                massege = "Invoice saved successfully";
            }
            catch (Exception ex)
            {
                result  = false;
                massege = "Invoice not saved";
            }
            return(Json(new { result = result, message = massege }));
        }