public List <VMStock> GetStockPerWarehouse() { List <VMStock> result = new List <VMStock>(); try { List <VMStock> stocks = new List <VMStock>(); List <Product> products = new List <Product>(); List <Warehouse> warehouses = new List <Warehouse>(); warehouses = _context.Warehouse.ToList(); products = _context.Product.ToList(); foreach (var item in products) { foreach (var wh in warehouses) { VMStock stock = stock = GetStockByProductAndWarehouse(item.productId, wh.warehouseId); if (stock != null) { stocks.Add(stock); } } } result = stocks; } catch (Exception) { throw; } return(result); }
public VMStock GetStockByProductAndWarehouse(string productId, string warehouseId) { VMStock result = new VMStock(); try { Product product = _context.Product.Where(x => x.productId.Equals(productId)).FirstOrDefault(); Warehouse warehouse = _context.Warehouse.Where(x => x.warehouseId.Equals(warehouseId)).FirstOrDefault(); if (product != null && warehouse != null) { VMStock stock = new VMStock(); stock.Product = product.productCode; stock.Warehouse = warehouse.warehouseName; stock.QtyReceiving = _context.ReceivingLine.Where(x => x.productId.Equals(product.productId) && x.warehouseId.Equals(warehouse.warehouseId)).Sum(x => x.qtyReceive); stock.QtyShipment = _context.ShipmentLine.Where(x => x.productId.Equals(product.productId) && x.warehouseId.Equals(warehouse.warehouseId)).Sum(x => x.qtyShipment); stock.QtyTransferIn = _context.TransferInLine.Where(x => x.productId.Equals(product.productId) && x.transferIn.warehouseIdTo.Equals(warehouse.warehouseId)).Sum(x => x.qty); stock.QtyTransferOut = _context.TransferOutLine.Where(x => x.productId.Equals(product.productId) && x.transferOut.warehouseIdFrom.Equals(warehouse.warehouseId)).Sum(x => x.qty); stock.QtyOnhand = stock.QtyReceiving + stock.QtyTransferIn - stock.QtyShipment - stock.QtyTransferOut; result = stock; } } catch (Exception) { throw; } return(result); }
public VMStock GetStockByProductAndWarehouse(string productId, string warehouseId) { VMStock result = new VMStock(); try { Product product = _context.Product.Where(x => x.productId.Equals(productId)).FirstOrDefault(); Warehouse warehouse = _context.Warehouse.Where(x => x.warehouseId.Equals(warehouseId)).FirstOrDefault(); var productionorderline = from ProductionOrderLine in _context.ProductionOrderLine join ProductionOrder in _context.ProductionOrder on ProductionOrderLine.ProductionOrderId equals ProductionOrder.ProductionOrderId join Production in _context.Production on ProductionOrder.ProductionOrderId equals Production.ProductionOrderId select new { ProductionOrderLine.ProductionOrderLineId, ProductionOrderLine.ProductId, ProductionOrderLine.ProductionOrderId, ProductionOrderLine.Qty, ProductionOrderLine.createdAt, Production.warehouseId }; if (product != null && warehouse != null) { VMStock stock = new VMStock { Product = product.productCode, Warehouse = warehouse.warehouseName, QtyReceiving = _context.ReceivingLine.Where(x => x.productId.Equals(product.productId) && x.warehouseId.Equals(warehouse.warehouseId)).Sum(x => x.qtyReceive), QtyShipment = _context.ShipmentLine.Where(x => x.productId.Equals(product.productId) && x.warehouseId.Equals(warehouse.warehouseId)).Sum(x => x.qtyShipment), QtyTransferIn = _context.TransferInLine.Where(x => x.productId.Equals(product.productId) && x.transferIn.warehouseIdTo.Equals(warehouse.warehouseId)).Sum(x => x.qty), QtyTransferOut = _context.TransferOutLine.Where(x => x.productId.Equals(product.productId) && x.transferOut.warehouseIdFrom.Equals(warehouse.warehouseId)).Sum(x => x.qty), QtyRoasting = _context.ProductionLine.Where(x => x.productId.Equals(product.productId) && x.warehouseId.Equals(warehouse.warehouseId)).Sum(x => x.qty), QtyProduction = productionorderline.Where(x => x.ProductId.Equals(product.productId) && x.warehouseId.Equals(warehouse.warehouseId)).Sum(x => x.Qty) }; stock.QtyOnhand = stock.QtyReceiving + stock.QtyTransferIn - stock.QtyRoasting - stock.QtyShipment - stock.QtyTransferOut + stock.QtyProduction; result = stock; } } catch (Exception) { throw; } return(result); }
public async Task <IActionResult> Create([Bind("shipmentId,salesOrderId,shipmentNumber,shipmentDate,customerId,customerPO,invoice,branchId,warehouseId,expeditionType,expeditionMode,HasChild,createdAt")] Shipment shipment) { if (shipment.salesOrderId == "0" || shipment.warehouseId == "0") { TempData["StatusMessage"] = "Error. Sales order or warehouse is not valid. Please select valid sales order and warehouse"; return(RedirectToAction(nameof(Create))); } if (ModelState.IsValid) { //check sales order Shipment check = await _context.Shipment .Include(x => x.salesOrder) .SingleOrDefaultAsync(x => x.salesOrderId.Equals(shipment.salesOrderId)); if (check != null) { ViewData["StatusMessage"] = "Error. Sales order already shipped. " + check.shipmentNumber; ViewData["branchId"] = new SelectList(_context.Branch, "branchId", "branchName"); ViewData["customerId"] = new SelectList(_context.Customer, "customerId", "customerName"); ViewData["salesOrderId"] = new SelectList(_context.SalesOrder, "salesOrderId", "salesOrderNumber"); ViewData["warehouseId"] = new SelectList(_context.Warehouse, "warehouseId", "warehouseName"); return(View(shipment)); } //check stock bool isStockOK = true; string productList = ""; List <SalesOrderLine> stocklines = new List <SalesOrderLine>(); stocklines = _context.SalesOrderLine .Include(x => x.product) .Where(x => x.salesOrderId.Equals(shipment.salesOrderId)).ToList(); foreach (var item in stocklines) { VMStock stock = _netcoreService.GetStockByProductAndWarehouse(item.productId, shipment.warehouseId); if (stock != null) { if (stock.QtyOnhand < item.qty) { isStockOK = false; productList = productList + " [" + item.product.productCode + "] "; } } else { isStockOK = false; } } if (!isStockOK) { TempData["StatusMessage"] = "Error. Stock quantity problem, please check your on hand stock. " + productList; return(RedirectToAction(nameof(Create))); } shipment.warehouse = await _context.Warehouse.Include(x => x.branch).SingleOrDefaultAsync(x => x.warehouseId.Equals(shipment.warehouseId)); shipment.branch = shipment.warehouse.branch; shipment.salesOrder = await _context.SalesOrder.Include(x => x.customer).SingleOrDefaultAsync(x => x.salesOrderId.Equals(shipment.salesOrderId)); shipment.customer = shipment.salesOrder.customer; //change status of salesorder shipment.salesOrder.salesOrderStatus = SalesOrderStatus.Completed; _context.Update(shipment.salesOrder); _context.Add(shipment); await _context.SaveChangesAsync(); //auto create shipment line, full shipment List <SalesOrderLine> solines = new List <SalesOrderLine>(); solines = _context.SalesOrderLine.Include(x => x.product).Where(x => x.salesOrderId.Equals(shipment.salesOrderId)).ToList(); foreach (var item in solines) { ShipmentLine line = new ShipmentLine(); line.shipment = shipment; line.product = item.product; line.qty = item.qty; line.qtyShipment = item.qty; line.qtyInventory = line.qtyShipment * -1; line.branchId = shipment.branchId; line.warehouseId = shipment.warehouseId; _context.ShipmentLine.Add(line); await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Details), new { id = shipment.shipmentId })); } ViewData["branchId"] = new SelectList(_context.Branch, "branchId", "branchName", shipment.branchId); ViewData["customerId"] = new SelectList(_context.Customer, "customerId", "customerName", shipment.customerId); ViewData["salesOrderId"] = new SelectList(_context.SalesOrder, "salesOrderId", "salesOrderNumber", shipment.salesOrderId); ViewData["warehouseId"] = new SelectList(_context.Warehouse, "warehouseId", "warehouseName", shipment.warehouseId); return(View(shipment)); }
public async Task <IActionResult> Create([Bind("ProductionId,HasChild,ProductionDate,ProductionNumber,ProductionOrderId,createdAt,warehouseId,ProductionStatus,Description,Notes")] Production production) { if (production.ProductionOrderId == "0" || production.warehouseId == "0") { TempData["StatusMessage"] = "Σφάλμα. Η εντολή παραγωγής ή η αποθήκη δεν είναι έγκυρη. Επιλέξτε έγκυρη παραγγελία και αποθήκη παραγωγής"; return(RedirectToAction(nameof(Create))); } if (ModelState.IsValid) { var polines = from ProductionOrderLine in _context.ProductionOrderLine join Product in _context.Product on ProductionOrderLine.ProductId equals Product.productId join ProductLine in _context.ProductLine on Product.productId equals ProductLine.ProductId join Product_1 in _context.Product on new { ComponentId = ProductLine.ComponentId } equals new { ComponentId = Product_1.productId } group new { ProductLine, ProductionOrderLine, Product_1 } by new { ProductLine.ComponentId, ProductionOrderLine.ProductionOrderId, Product_1.productCode } into g select new { g.Key.ProductionOrderId, g.Key.ComponentId, g.Key.productCode, ComponentQty = (decimal?)g.Sum(p => p.ProductionOrderLine.Qty * System.Convert.ToSingle(p.ProductLine.Percentage)) }; polines = polines.Where(x => x.ProductionOrderId == production.ProductionOrderId); //check production order Production check = await _context.Production .Include(x => x.ProductionOrder) .Include(x => x.warehouse) .SingleOrDefaultAsync(x => x.ProductionOrderId.Equals(production.ProductionOrderId)); if (check != null) { ViewData["ProductionOrderId"] = new SelectList(_context.ProductionOrder.Where(x => x.ProductionOrderStatus == ProductionOrderStatus.Open), "ProductionOrderId", "ProductionNumber"); ViewData["StatusMessage"] = "Σφάλμα. Η εντολή παραγωγής έχει ήδη αποσταλεί. " + check.ProductionNumber; ViewData["warehouseId"] = new SelectList(_context.Warehouse, "warehouseId", "warehouseName"); return(View(production)); } //check stock bool isStockOK = true; string productList = ""; foreach (var item in polines) { VMStock stock = _netcoreService.GetStockByProductAndWarehouse(item.ComponentId, production.warehouseId); if (stock != null) { if (stock.QtyOnhand < System.Convert.ToSingle(item.ComponentQty)) { isStockOK = false; productList = productList + " [" + item.productCode + "] "; } } else { isStockOK = false; } } if (!isStockOK) { TempData["StatusMessage"] = "Σφάλμα. Υπάρχει πρόβλημα στην ποσότητα αποθεμάτων, ελέγξτε το απόθεμά σας. " + productList; return(RedirectToAction(nameof(Create))); } //change status of salesorder var pro = _context.ProductionOrder.Where(x => x.ProductionOrderId == production.ProductionOrderId).FirstOrDefault(); pro.ProductionOrderStatus = ProductionOrderStatus.Completed; _context.Update(pro); production.ProductionNumber = _numberSequence.GetNumberSequence("ΠΑΡ"); _context.Add(production); await _context.SaveChangesAsync(); //*auto create production line, full production foreach (var item in polines) { Product prod = _context.Product.Where(x => x.productId == item.ComponentId).FirstOrDefault(); ProductionLine line = new ProductionLine(); line.Production = production; line.product = prod; line.warehouse = production.warehouse; line.branch = production.ProductionOrder.branch; line.qty = System.Convert.ToInt32(item.ComponentQty); line.branchId = production.ProductionOrder.branchId; line.warehouseId = production.warehouseId; _context.ProductionLine.Add(line); await _context.SaveChangesAsync(); } TempData["TransMessage"] = "Η Δημιουργία της Παραγωγής " + production.ProductionNumber + " έγινε με Επιτυχία"; ViewData["ProductionOrderId"] = new SelectList(_context.ProductionOrder, "ProductionOrderId", "ProductionOrderNumber", production.ProductionOrderId); return(RedirectToAction(nameof(Details), new { id = production.ProductionId })); } ViewData["warehouseId"] = new SelectList(_context.Warehouse, "warehouseId", "warehouseName", production.warehouseId); return(View(production)); }
public async Task <IActionResult> Create([Bind("transferOutId,transferOrderId,transferOutNumber,transferOutDate,description,branchIdFrom,warehouseIdFrom,branchIdTo,warehouseIdTo,HasChild,createdAt")] TransferOut transferOut) { if (ModelState.IsValid) { //check transfer order TransferOut check = await _context.TransferOut .Include(x => x.transferOrder) .SingleOrDefaultAsync(x => x.transferOrderId.Equals(transferOut.transferOrderId)); if (check != null) { ViewData["StatusMessage"] = "Σφάλμα. Η εντολή μεταφοράς έχει ήδη εκδοθεί. " + check.transferOutNumber; ViewData["transferOrderId"] = new SelectList(_context.TransferOrder, "transferOrderId", "transferOrderNumber"); ViewData["branchIdFrom"] = new SelectList(_context.Branch, "branchId", "branchName"); ViewData["warehouseIdFrom"] = new SelectList(_context.Warehouse, "warehouseId", "warehouseName"); ViewData["branchIdTo"] = new SelectList(_context.Branch, "branchId", "branchName"); ViewData["warehouseIdTo"] = new SelectList(_context.Warehouse, "warehouseId", "warehouseName"); return(View(transferOut)); } //check stock bool isStockOK = true; string productList = ""; List <TransferOrderLine> stocklines = new List <TransferOrderLine>(); stocklines = _context.TransferOrderLine .Include(x => x.transferOrder) .Include(x => x.product) .Where(x => x.transferOrderId.Equals(transferOut.transferOrderId)).ToList(); foreach (var item in stocklines) { VMStock stock = _netcoreService.GetStockByProductAndWarehouse(item.productId, item.transferOrder.warehouseIdFrom); if (stock != null) { if (stock.QtyOnhand < item.qty) { isStockOK = false; productList = productList + " [" + item.product.productCode + "] "; } } else { isStockOK = false; } } if (!isStockOK) { TempData["StatusMessage"] = "Σφάλμα. Υπάρχει πρόβλημα στην ποσότητα αποθεμάτων, ελέγξτε το απόθεμά σας. " + productList; return(RedirectToAction(nameof(Create))); } TransferOrder to = await _context.TransferOrder.Where(x => x.transferOrderId.Equals(transferOut.transferOrderId)).FirstOrDefaultAsync(); transferOut.warehouseIdFrom = to.warehouseIdFrom; transferOut.warehouseIdTo = to.warehouseIdTo; transferOut.warehouseFrom = await _context.Warehouse.Include(x => x.branch).SingleOrDefaultAsync(x => x.warehouseId.Equals(transferOut.warehouseIdFrom)); transferOut.branchFrom = transferOut.warehouseFrom.branch; transferOut.warehouseTo = await _context.Warehouse.Include(x => x.branch).SingleOrDefaultAsync(x => x.warehouseId.Equals(transferOut.warehouseIdTo)); transferOut.branchTo = transferOut.warehouseTo.branch; to.isIssued = true; _context.Add(transferOut); await _context.SaveChangesAsync(); //auto create transfer out line, full shipment List <TransferOrderLine> lines = new List <TransferOrderLine>(); lines = _context.TransferOrderLine.Include(x => x.product).Where(x => x.transferOrderId.Equals(transferOut.transferOrderId)).ToList(); foreach (var item in lines) { TransferOutLine line = new TransferOutLine(); line.transferOut = transferOut; line.product = item.product; line.qty = item.qty; line.qtyInventory = line.qty * -1; _context.TransferOutLine.Add(line); await _context.SaveChangesAsync(); } TempData["TransMessage"] = "Η Δημιουργία Μεταφοράς " + transferOut.transferOutNumber + " έγινε με Επιτυχία!"; return(RedirectToAction(nameof(Details), new { id = transferOut.transferOutId })); } ViewData["transferOrderId"] = new SelectList(_context.TransferOrder, "transferOrderId", "transferOrderNumber", transferOut.transferOrderId); return(View(transferOut)); }
public async Task <IActionResult> Create([Bind("shipmentId,HasChild,branchId,createdAt,customerId,customerPO,expeditionMode,expeditionType,invoiceNumber,salesOrderId,shipmentDate,shipmentNumber,warehouseId,EmployeeId")] Shipment shipment) { if (shipment.salesOrderId == "0" || shipment.warehouseId == "0") { TempData["StatusMessage"] = "Σφάλμα. Η εντολή πώλησης ή η αποθήκη δεν είναι έγκυρη. Επιλέξτε έγκυρη παραγγελία και αποθήκη πωλήσεων"; return(RedirectToAction(nameof(Create))); } if (ModelState.IsValid) { //check sales order Shipment check = await _context.Shipment .Include(x => x.salesOrder) .Include(x => x.Employee) .SingleOrDefaultAsync(x => x.salesOrderId.Equals(shipment.salesOrderId)); if (check != null) { ViewData["salesOrderId"] = new SelectList(_context.SalesOrder.Where(x => x.salesOrderStatus == SalesOrderStatus.Open), "salesOrderId", "SalesOrderName"); ViewData["StatusMessage"] = "Σφάλμα. Η εντολή πώλησης έχει ήδη αποσταλεί. " + check.shipmentNumber; ViewData["branchId"] = new SelectList(_context.Branch, "branchId", "branchName"); ViewData["customerId"] = new SelectList(_context.Customer, "customerId", "customerName"); ViewData["warehouseId"] = new SelectList(_context.Warehouse, "warehouseId", "warehouseName"); ViewData["employeeId"] = new SelectList(_context.Employee, "EmployeeId", "DisplayName"); return(View(shipment)); } //check stock bool isStockOK = true; string productList = ""; List <SalesOrderLine> stocklines = new List <SalesOrderLine>(); stocklines = _context.SalesOrderLine .Include(x => x.Product) .Where(x => x.SalesOrderId.Equals(shipment.salesOrderId)).ToList(); foreach (var item in stocklines) { VMStock stock = _netcoreService.GetStockByProductAndWarehouse(item.ProductId, shipment.warehouseId); if (stock != null) { if (stock.QtyOnhand < item.Qty) { isStockOK = false; productList = productList + " [" + item.Product.productCode + "] "; } } else { isStockOK = false; } } if (!isStockOK) { TempData["StatusMessage"] = "Σφάλμα. Υπάρχει πρόβλημα στην ποσότητα αποθεμάτων, ελέγξτε το απόθεμά σας. " + productList; return(RedirectToAction(nameof(Create))); } shipment.warehouse = await _context.Warehouse.Include(x => x.branch).SingleOrDefaultAsync(x => x.warehouseId.Equals(shipment.warehouseId)); shipment.branch = shipment.warehouse.branch; shipment.salesOrder = await _context.SalesOrder.Include(x => x.customer).SingleOrDefaultAsync(x => x.salesOrderId.Equals(shipment.salesOrderId)); shipment.customer = shipment.salesOrder.customer; //change status of salesorder shipment.salesOrder.salesOrderStatus = SalesOrderStatus.Completed; _context.Update(shipment.salesOrder); shipment.shipmentNumber = _numberSequence.GetNumberSequence("ΔΑ"); _context.Add(shipment); await _context.SaveChangesAsync(); //auto create shipment line, full shipment List <SalesOrderLine> solines = new List <SalesOrderLine>(); solines = _context.SalesOrderLine.Include(x => x.Product).Where(x => x.SalesOrderId.Equals(shipment.salesOrderId)).ToList(); foreach (var item in solines) { ShipmentLine line = new ShipmentLine(); line.shipment = shipment; line.product = item.Product; line.qty = item.Qty; line.qtyShipment = item.Qty; line.qtyInventory = line.qtyShipment * -1; line.branchId = shipment.branchId; line.warehouseId = shipment.warehouseId; _context.ShipmentLine.Add(line); await _context.SaveChangesAsync(); } TempData["TransMessage"] = "Η Δημιουργία της Αποστολής " + shipment.shipmentNumber + " έγινε με Επιτυχία"; return(RedirectToAction(nameof(Details), new { id = shipment.shipmentId })); } ViewData["branchId"] = new SelectList(_context.Branch, "branchId", "branchName", shipment.branchId); ViewData["customerId"] = new SelectList(_context.Customer, "customerId", "customerName", shipment.customerId); ViewData["salesOrderId"] = new SelectList(_context.SalesOrder.Where(x => x.salesOrderStatus == SalesOrderStatus.Open), "salesOrderId", "SalesOrderName", shipment.salesOrderId); ViewData["warehouseId"] = new SelectList(_context.Warehouse, "warehouseId", "warehouseName", shipment.warehouseId); ViewData["employeeId"] = new SelectList(_context.Employee, "EmployeeId", "DisplayName", shipment.EmployeeId); return(View(shipment)); }