Пример #1
0
        public int addStock(Stock stock)
        {
            SSGetService    getService    = new SSGetService();
            SSUpdateService updateService = new SSUpdateService();

            Product existingProduct = getService.getProductByName(stock.name);

            if (existingProduct.name != null)
            {
                Location existingLocation = getService.getLocationByName(stock.location);
                if (existingLocation.name != null)
                {
                    string    batchName;
                    string    batchFormat = DateTime.Now.ToString("MMyyyy");
                    DataTable batchData   = getService.getDataWithFilter(app.objects["batches"], " name like '%" + batchFormat + "' order by created_date desc");
                    if (batchData.Rows.Count > 0)
                    {
                        DataRow row  = batchData.Rows[0];
                        string  name = row.Field <string>("name");
                        string  val  = name.Substring(name.IndexOf('B') + 1, name.IndexOf('-') - 1);
                        if (app.isAllDigits(val))
                        {
                            batchName = "B" + (int.Parse(val) + 1) + "-" + batchFormat;
                        }
                        else
                        {
                            batchName = "";
                        }
                    }
                    else
                    {
                        batchName = "B1-" + batchFormat;
                    }
                    string batchKey = app.generateId(20);

                    if (batchName != null && batchName != "")
                    {
                        using (SqlCommand batchCommand = new SqlCommand("INSERT INTO ss_batches(name,reference_key,pid,total,balance,sold)VALUES(@name,@reference_key,@pid,@total,@balance,@sold)"))
                        {
                            batchCommand.Parameters.AddWithValue("@name", batchName);
                            batchCommand.Parameters.AddWithValue("@reference_key", batchKey);
                            batchCommand.Parameters.AddWithValue("@pid", existingProduct.id);
                            batchCommand.Parameters.AddWithValue("@total", stock.quantity);
                            batchCommand.Parameters.AddWithValue("@balance", stock.quantity);
                            batchCommand.Parameters.AddWithValue("@sold", 0);

                            int batchResponse = service.execute(batchCommand);
                            if (batchResponse > 0)
                            {
                                Batch batch = getService.getBatchByKey(batchKey);
                                if (batch.key != null)
                                {
                                    using (SqlCommand command = new SqlCommand("INSERT INTO ss_stocks(pid,bid,quantity,cost,description,last_modified_date)VALUES(@pid,@bid,@quantity,@cost,@description, getdate())"))
                                    {
                                        command.Parameters.AddWithValue("@pid", existingProduct.id);
                                        command.Parameters.AddWithValue("@bid", batch.id);
                                        command.Parameters.AddWithValue("@quantity", stock.quantity);
                                        command.Parameters.AddWithValue("@cost", stock.cost);
                                        command.Parameters.AddWithValue("@description", stock.description);
                                        int response = service.execute(command);
                                        if (response > 0)
                                        {
                                            return(response);
                                        }
                                        else
                                        {
                                            return(-1);
                                        }
                                    }
                                }
                                else
                                {
                                    return(-1);
                                }
                            }
                            else
                            {
                                return(-402);
                            }
                        }
                    }
                    else
                    {
                        return(-500);
                    }
                }
                else
                {
                    return(-404);
                }
            }
            else
            {
                return(-403);
            }
        }