Example #1
0
        public ActionResult Delivery(FormCollection collection)
        {
            try
            {
                int  branchId         = Convert.ToInt32(Session["BranchId"]);
                int  companyId        = Convert.ToInt32(Session["CompanyId"]);
                var  transport        = collection["ownTransport"];
                bool isOwnTransport   = transport != null;
                int  deliverebyUserId = ((ViewUser)Session["user"]).UserId;
                var  requisitionId    = Convert.ToInt64(collection["RequisitionId"]);
                var  requisition      = _iProductManager.GetGeneralRequisitionById(requisitionId);
                var  details          = _iProductManager.GetGeneralRequisitionDetailsById(requisitionId);

                string fileName = "Scanned_GR_Product_List_For_" + requisitionId;
                var    filePath = Server.MapPath("~/Files/" + fileName);
                //if the file is exists read the file
                var barcodeList = _iProductManager.GetScannedProductListFromTextFile(filePath).ToList();

                var deliveredProductList = new List <ViewGeneralRequistionDetailsModel>();
                foreach (ScannedProduct product in barcodeList.DistinctBy(n => n.ProductId))
                {
                    var model = details.ToList().Find(n => n.ProductId.Equals(product.ProductId));
                    var up    = _iProductManager.GetProductDetailsByProductId(product.ProductId).UnitPrice;
                    var qty   = barcodeList.ToList().FindAll(n => n.ProductId == product.ProductId).Count;
                    model.Quantity  = qty;
                    model.UnitPrice = up;
                    deliveredProductList.Add(model);
                }


                var grossAmount = deliveredProductList.Sum(n => (n.UnitPrice + n.Vat) * n.Quantity);


                var financialModel =
                    new FinancialTransactionModel
                {
                    //--------Expence Dr -------------------
                    ExpenceCode   = "2601011",
                    ExpenceAmount = grossAmount,
                    //--------Inventory Cr -------------------
                    InventoryCode   = "3301011",
                    InventoryAmount = grossAmount
                };


                var aDelivery = new Delivery
                {
                    IsOwnTransport            = isOwnTransport,
                    TransactionRef            = requisition.RequisitionRef,
                    InvoiceRef                = requisition.RequisitionRef,
                    DeliveredByUserId         = deliverebyUserId,
                    Transportation            = collection["Transportation"],
                    DriverName                = collection["DriverName"],
                    DriverPhone               = collection["DriverPhone"],
                    TransportationCost        = Convert.ToDecimal(collection["TransportationCost"]),
                    VehicleNo                 = collection["VehicleNo"],
                    DeliveryDate              = Convert.ToDateTime(collection["DeliveryDate"]).Date,
                    CompanyId                 = companyId,
                    ToBranchId                = branchId,
                    RequisitionId             = requisitionId,
                    FromBranchId              = branchId,
                    FinancialTransactionModel = financialModel
                };
                var result = _iFactoryDeliveryManager.SaveDeliveredGeneralRequisition(barcodeList, aDelivery);
                if (result)
                {
                    System.IO.File.Create(filePath).Close();
                    return(RedirectToAction("GeneralRequisitionList"));
                }
                return(View());
            }
            catch (Exception exception)
            {
                TempData["Error"] = exception.Message;
                Log.WriteErrorLog(exception);
                return(PartialView("_ErrorPartial", exception));
            }
        }