Exemplo n.º 1
0
        public static void CLIClientRun(string s)
        {
            string[] A = s.Split(' ');

            if (A.Length == 0 || A.Length == 1)
            {
                Log($"LankaStocks ClCLI help \n" +
                    $" Execetive Designer and Developer : Harindu Wijesinghe (Also known as Balla among 7th graders, Bawwa among girls, Otuwa among teachers and Greece Yaka among police) \n" +
                    $" Backend  Developer : Hasindu Lanka\n" +
                    $"\n" +
                    $"Client CLI commands : \n" +
                    $"sale <itemID> <quantity> ");
            }

            switch (A[0])
            {
            case "sale":
                if (A.Length >= 3)
                {
                    var sale = new BasicSale()
                    {
                        date = DateTime.Now, items = new List <BusinessItem>()
                        {
                            new BusinessItem()
                            {
                                itemID = uint.Parse(A[1]), Quantity = int.Parse(A[2])
                            }
                        }, SaleID = 0, special = (A.Length >= 4) ? (A[3] == "s") : false, UserID = user.ID
                    };
                    sale.CalculateTotal();
                    client.Sale(sale);
                }
                break;

            default:
                break;
            }
        }
Exemplo n.º 2
0
        public Response Sale(BasicSale sale)
        {
            //Check Errors

            List <(BusinessItem, Item)> Items = new List <(BusinessItem, Item)>(sale.items.Count);

            foreach (var i in sale.items)
            {
                if (!Live.Items.TryGetValue(i.itemID, out Item root))
                {
                    return(new Response(Response.Result.notfound, $"Item {i.itemID} not found"));
                }

                if (root.Quantity < i.Quantity)
                {
                    return(new Response(Response.Result.wrongInputs, $"We only have {root.Quantity} units of {root.name}. You Can't issue {i.Quantity}", null, root));
                }

                i.Value = root.outPrice;
                Items.Add((i, root));
            }

            if (sale.total == 0)
            {
                sale.CalculateTotal();
            }
            //Apply changes to DBs

            if (People.Users.TryGetValue(sale.UserID, out User user))
            {
                user.summary.date   = DateTime.Now;
                user.summary.total += sale.total;
            }
            else
            {
                return(new Response(Response.Result.failed, $"UserID {sale.UserID} not found."));
            }


            if (sale is SpecialSale special)
            {
                user.summary.Liability += special.transaction.Liability;
            }



            if (svr.IsHost && sale.SaleID == 0)
            {
                Live.IdMachine.SaleID++;
                sale.SaleID = Live.IdMachine.SaleID;
                sale.date   = DateTime.Now;
            }
            else
            {
                Live.IdMachine.SaleID = Math.Max(Live.IdMachine.SaleID, sale.SaleID);
            }


            foreach ((BusinessItem itm, Item root) in Items)
            {
                root.Quantity -= itm.Quantity;
            }



            Live.Session.Sales.Add(sale.SaleID, sale);

            return(new Response(Response.Result.ok, null, null, sale));
        }