<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Web.Http;
using Picking.Lib_Primavera;
using Picking.Lib_Primavera.Model;

namespace Picking.Controllers
{
    public class PickingListsController : ApiController
    {
        // POST /api/pickinglists/
        public PickingList Post(PickingSelection selection)
        {
            var pickingItems = new List<PickingItem>();
            var skippedOrders = new List<OrderLine>();

            foreach (var orderId in selection.Orders)
            {
                Order order;
                try
                {
                    order = _company.GetOrder(orderId);
                    if (order == null)
                        continue;
                }
                catch (Exception)
                {
                    continue;
                }

                foreach (var orderLine in order.OrderLines)
                {
                    if (orderLine.Picked)
                        continue;

                    if (Math.Abs(orderLine.PickedQuantity - orderLine.Quantity) < Double.Epsilon * 100)
                        continue;

                    var stock = GetStock(orderLine.Item.Id)
                        .Where(itemStock => itemStock.Stock > 0 && itemStock.StorageFacility == selection.Facility)
                        .OrderByDescending(itemStock => itemStock.Stock) // Prioritize by stock quantity
                        .Where(itemStock => Location.FromString(itemStock.StorageLocation) != null) // Only valid locations
                        .ToList();

                    if (stock.Sum(itemStock => itemStock.Stock) < orderLine.Quantity)
                    {
                        skippedOrders.Add(orderLine);
                        continue;
                    }

                    ItemStock previousStockLocation = null;
                    while (orderLine.Quantity > 0)
                    {
                        var stockLocation = previousStockLocation == null ? stock[0] : GetClosestStockLocation(stock, previousStockLocation);
                        previousStockLocation = stockLocation;
                        if (stockLocation == null)
                            continue;

                        double quantity;
                        if (stockLocation.Stock > orderLine.Quantity)
                        {
                            quantity = orderLine.Quantity;
                            orderLine.Quantity = 0;
                        }
                        else
                        {
                            quantity = stockLocation.Stock;
                            orderLine.Quantity -= stockLocation.Stock;
                        }

                        stockLocation.Stock -= quantity;

                        if (Math.Abs(quantity) < Double.Epsilon)
                            break;

                        var pickingItem = new PickingItem
                        {
                            OrderLineId = orderLine.Id,
                            Item = orderLine.Item,
                            Quantity = quantity,
                            Unit = orderLine.Unit,
                            StorageFacility = stockLocation.StorageFacility,
                            StorageLocation = stockLocation.StorageLocation,
                        };

                        pickingItems.Add(pickingItem);
                        _company.MarkOrderLinePicked(orderLine.Id);
                    }
                }
            }

            return new PickingList {Items = pickingItems, SkippedOrders = skippedOrders};
        }

        // PATCH /api/pickinglists
        public IEnumerable<string> Patch(PickingList pickingList)
        {
            var errors = new List<string>();
            var itemsList = pickingList.Items.ToList();
            if (itemsList.Count == 0)
            {
                errors.Add("Empty input.");
                return errors;
            }

            foreach (var item in itemsList)
            {
                if (item.PickedQuantity < item.Quantity)
                {
                    var quantity = item.Quantity - item.PickedQuantity;
                    var err1 = _company.GenerateStockRemovalDocument(item, quantity);
                    if (!string.IsNullOrWhiteSpace(err1))
                        errors.Add(err1);

                    _company.MarkOrderLinePicked(item.OrderLineId, false);
                }
            }

            _company.InsertPickingItems(itemsList);

            var err2 = _company.GenerateStockTransferDocument(itemsList);
            if (!string.IsNullOrWhiteSpace(err2))
                errors.Add(err2);

            return errors;
        }

        // GET /api/pickinglists
        public IEnumerable<PickingList> Get()
        {
            return _company.ListPickingLists();
        }

        // GET /api/pickinglists/<id>
        public PickingList Get(int id)<commit_msg>Add missing call to SetOrderLinePickedQuantity in Picking 'algorithm'<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Web.Http;
using Picking.Lib_Primavera;
using Picking.Lib_Primavera.Model;

namespace Picking.Controllers
{
    public class PickingListsController : ApiController
    {
        // POST /api/pickinglists/
        public PickingList Post(PickingSelection selection)
        {
            var pickingItems = new List<PickingItem>();
            var skippedOrders = new List<OrderLine>();

            foreach (var orderId in selection.Orders)
            {
                Order order;
                try
                {
                    order = _company.GetOrder(orderId);
                    if (order == null)
                        continue;
                }
                catch (Exception)
                {
                    continue;
                }

                foreach (var orderLine in order.OrderLines)
                {
                    if (orderLine.Picked)
                        continue;

                    if (Math.Abs(orderLine.PickedQuantity - orderLine.Quantity) < Double.Epsilon * 100)
                        continue;

                    var stock = GetStock(orderLine.Item.Id)
                        .Where(itemStock => itemStock.Stock > 0 && itemStock.StorageFacility == selection.Facility)
                        .OrderByDescending(itemStock => itemStock.Stock) // Prioritize by stock quantity
                        .Where(itemStock => Location.FromString(itemStock.StorageLocation) != null) // Only valid locations
                        .ToList();

                    if (stock.Sum(itemStock => itemStock.Stock) < orderLine.Quantity)
                    {
                        skippedOrders.Add(orderLine);
                        continue;
                    }

                    ItemStock previousStockLocation = null;
                    while (orderLine.Quantity > 0)
                    {
                        var stockLocation = previousStockLocation == null ? stock[0] : GetClosestStockLocation(stock, previousStockLocation);
                        previousStockLocation = stockLocation;
                        if (stockLocation == null)
                            continue;

                        double quantity;
                        if (stockLocation.Stock > orderLine.Quantity)
                        {
                            quantity = orderLine.Quantity;
                            orderLine.Quantity = 0;
                        }
                        else
                        {
                            quantity = stockLocation.Stock;
                            orderLine.Quantity -= stockLocation.Stock;
                        }

                        stockLocation.Stock -= quantity;

                        if (Math.Abs(quantity) < Double.Epsilon)
                            break;

                        var pickingItem = new PickingItem
                        {
                            OrderLineId = orderLine.Id,
                            Item = orderLine.Item,
                            Quantity = quantity,
                            Unit = orderLine.Unit,
                            StorageFacility = stockLocation.StorageFacility,
                            StorageLocation = stockLocation.StorageLocation,
                        };

                        pickingItems.Add(pickingItem);
                        _company.MarkOrderLinePicked(orderLine.Id);
                    }
                }
            }

            return new PickingList {Items = pickingItems, SkippedOrders = skippedOrders};
        }

        // PATCH /api/pickinglists
        public IEnumerable<string> Patch(PickingList pickingList)
        {
            var errors = new List<string>();
            var itemsList = pickingList.Items.ToList();
            if (itemsList.Count == 0)
            {
                errors.Add("Empty input.");
                return errors;
            }

            foreach (var item in itemsList)
            {
                if (item.PickedQuantity < item.Quantity)
                {
                    var quantity = item.Quantity - item.PickedQuantity;
                    var err1 = _company.GenerateStockRemovalDocument(item, quantity);
                    if (!string.IsNullOrWhiteSpace(err1))
                        errors.Add(err1);

                    _company.MarkOrderLinePicked(item.OrderLineId, false);
                }

                _company.SetOrderLinePickedQuantity(item.OrderLineId, item.PickedQuantity);
            }

            _company.InsertPickingItems(itemsList);

            var err2 = _company.GenerateStockTransferDocument(itemsList);
            if (!string.IsNullOrWhiteSpace(err2))
                errors.Add(err2);

            return errors;
        }

        // GET /api/pickinglists
        public IEnumerable<PickingList> Get()
        {
            return _company.ListPickingLists();
        }

        // GET /api/pickinglists/<id>
        public PickingList Get(int id)
{'fraction_non_alphanumeric': 0.07314716397719587, 'fraction_numerical': 0.0027055754179147744, 'mean_word_length': 1.1743697478991597, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4934450', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2230, 'n_words': 458}