public bool InsertTransmittal(TransmittalViewModel newTrans)
        {
            var trans = new transmital {
                approved_by_id = newTrans.ApprovedById,
                approved_date  = newTrans.ApprovedDate,
                created_date   = newTrans.CreatedDate,
                prepared_by_id = newTrans.PreparedById,
                noted_by_id    = newTrans.NotedById,

                transmitted_date             = newTrans.TransmittedDate,
                transmitted_to_company_id    = newTrans.TransmittedToCompanyId,
                transmitted_to_department_id = newTrans.TransmittedToDepartmentId,
                transmitted_to_user_id       = newTrans.TransmittedToUserId
            };

            InventoryDatabase.transmitals.Add(trans);
            var result = InventoryDatabase.SaveChanges();

            if (result > 0)
            {
                foreach (TransmittalItemViewModel itm in newTrans.TransmittalItems)
                {
                    InventoryDatabase.transmittal_items.Add(new transmittal_items
                    {
                        item_id        = itm.ItemId,
                        item_quantity  = itm.Quantity,
                        transmittal_id = trans.transmital_id,
                    });
                    InventoryDatabase.SaveChanges();
                }
                return(true);
            }

            return(false);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var transmittal = new TransmittalViewModel();

            transmittal.ApprovedById              = (int)cbxApproved.SelectedValue;
            transmittal.ApprovedDate              = dtpApprovedDate.Value;
            transmittal.CreatedDate               = DateTime.Now;
            transmittal.PreparedById              = (int)cbxPrepared.SelectedValue;
            transmittal.TransmittedToCompanyId    = (int)cbxCompany.SelectedValue;
            transmittal.TransmittedToDepartmentId = (int)cbxDepartment.SelectedValue;
            transmittal.TransmittedToUserId       = (int)cbxUsers.SelectedValue;
            transmittal.NotedById = (int)cbxNotedBy.SelectedValue;

            foreach (DataGridViewRow dr in dvItems.Rows)
            {
                transmittal.TransmittalItems.Add(new TransmittalItemViewModel
                {
                    ItemId   = Convert.ToInt32(dr.Cells[0].Value.ToString()),
                    Quantity = 1
                });
            }


            var result = Singleton.Instance.TransmittalModel.InsertTransmittal(transmittal);

            if (result)
            {
                this.DialogResult = DialogResult.OK;
            }

            this.Close();
        }
Example #3
0
 public bool InsertTransmittal(TransmittalViewModel newTrans)
 {
     return(repo.InsertTransmittal(newTrans));
 }
Example #4
0
        public async Task <bool> Save(TransmittalViewModel vm, bool UpdateToIssued)
        {
            if (UpdateToIssued)
            {
                TransmittalStatus = TransmittalStatus.Issued;
                SentDate          = DateTime.Today;
            }

            using (SqlConnection connection = new SqlConnection(TransmitalManager.connString))
            {
                Task       cmd = connection.OpenAsync();
                Task <int> t;


                if (IsLoadedFromDb)///update
                {
                    string updates = "";
                    if (vm.IssueToWorkshop != IssueToWorkshop)
                    {
                        updates += $"ToWorkShop = {Convert.ToInt32(vm.IssueToWorkshop)}";
                    }
                    if (vm.IssueType != IssueType)
                    {
                        updates += $"IssueType = {vm.IssueType}";
                    }
                    if (vm.TransmittalStatus != TransmittalStatus)
                    {
                        updates += $"Status = {vm.TransmittalStatus}";
                    }
                    if (vm.Comments != Comments)
                    {
                        updates += $"Comments = {vm.Comments}";
                    }
                    if (vm.Project.Number != Project.Number)
                    {
                        updates += $"ProjectNo = {vm.Project.Number}";
                    }
                    if (vm.IssueBy?.Id != IssueBy.Id)
                    {
                        updates += $"IssuedBy = {vm.IssueBy}";
                    }
                    if (vm.CreatedBy?.Id != CreatedBy.Id)
                    {
                        updates += $"CreatedBy= {vm.CreatedBy.Id}";
                    }


                    string     query   = string.Format(SqlScripts.UpdateTransmittal, Id, updates);
                    SqlCommand command = new SqlCommand(query, connection);
                    await      cmd;
                    t = command.ExecuteNonQueryAsync();


                    ///**** Update Transmittal Recipients *****

                    //TODO Not remove all recipients, check if they need to be removed to stop thrashing.
                    query = string.Format(SqlScripts.DeleteTransmitalRecipients, Id);  //Remove all recipients
                    Task <int> rr = new SqlCommand(query, connection).ExecuteNonQueryAsync();
                    await      rr;
                    //Add them
                    Parallel.ForEach(Recipients, async rec => await new SqlCommand(string.Format(SqlScripts.AddTransmitalRecipients, Id, rec.Id), connection).ExecuteNonQueryAsync());

                    //**** Update Files Lists ****
                    List <Document>          toRemove = Files.ToArray().ToList();
                    List <FileDataViewModel> toAdd    = new List <FileDataViewModel>();

                    //Files to add
                    foreach (FileDataViewModel fileVm in vm.Files)
                    {
                        Document fnd = Files.FirstOrDefault(a => fileVm.Id == a.Id);
                        if (fnd == null)
                        {
                            toAdd.Add(fileVm);
                        }
                    }
                    //Files to remove
                    foreach (Document file in Files)
                    {
                        FileDataViewModel fnd = vm.Files.FirstOrDefault(a => file.Id == a.Id);
                        if (fnd == null)
                        {
                            toRemove.Remove(file);
                        }
                    }

                    //remove files
                    string removeId = "";
                    toRemove.ForEach(r => removeId += (r.Id + ","));
                    string     remove = string.Format(SqlScripts.RemoveFiles, removeId.TrimEnd(','));
                    Task <int> t4     = new SqlCommand(remove, connection).ExecuteNonQueryAsync();

                    //Add files
                    Parallel.ForEach(toAdd, async doc => await doc.Model.dbQueryInsert(connection));
                    return(true);
                }
                else //add new
                {
                    try
                    {
                        Dictionary <string, string> updates = new Dictionary <string, string>();


                        updates.Add("ToWorkShop", Convert.ToInt32(vm.IssueToWorkshop).ToString());
                        updates.Add("IssueType", ((int)vm.IssueType).ToString());
                        updates.Add("Status", ((int)vm.TransmittalStatus).ToString());
                        if (!string.IsNullOrEmpty(vm.Comments))
                        {
                            updates.Add("Comments", "'" + vm.Comments + "'");
                        }
                        if (vm.Project != null)
                        {
                            updates.Add("ProjectNo", vm.Project.Number.ToString());
                        }
                        if (vm.IssueBy != null)
                        {
                            updates.Add("IssueBy", "'" + vm.IssueBy + "'");
                        }
                        if (vm.CreatedBy?.Id != CreatedBy.Id)
                        {
                            updates.Add("CreatedBy", vm.CreatedBy?.Id.ToString() ?? User.ActiveUser.Id.ToString());
                        }



                        string header = "", values = "";
                        foreach (KeyValuePair <string, string> v in updates)
                        {
                            header += v.Key + ",";
                            values += v.Value + ",";
                        }

                        header = header.Trim(',');
                        values = values.Trim(',');

                        string     query   = string.Format(SqlScripts.NewTransmittal, header, values);
                        SqlCommand command = new SqlCommand(query, connection);
                        await      cmd;

                        //Get id from new added record
                        object id = await command.ExecuteScalarAsync();

                        if (id is decimal d)
                        {
                            Id = Convert.ToInt32(d);
                        }
                        else if (id is int i)
                        {
                            Id = i;
                        }

                        if (Id != null)
                        {
                            ///**** Add Transmittal Recipients *****
                            Parallel.ForEach(Recipients, async rec => await new SqlCommand(string.Format(SqlScripts.AddTransmitalRecipients, Id, rec.Id), connection).ExecuteNonQueryAsync());

                            //**** Update Files Lists ****
                            foreach (var doc in vm.Files)
                            {
                                doc.Model.TransmittalId = Id ?? 1;
                                await doc.Model.dbQueryInsert(connection);
                            }

                            //Add files
                            //    Parallel.ForEach(vm.Files, doc =>
                            //{
                            //    doc.Model.TransmittalId = Id ?? 1;
                            //    doc.Model.dbQueryInsert(connection);
                            //    //    string header2 = "TransId, FileName, FileId, Version, Revision";
                            //    //    string values2 = $"{Id}, {doc.Name}, {doc.Id}, {doc.Version}, {doc.Revision}";
                            //    //    string query2 = string.Format(SqlScripts.AddFilesToTransmittal, header2, values2);
                            //    //    SqlCommand command2 = new SqlCommand(query2, connection);
                            //    //    command2.ExecuteNonQueryAsync();
                            //});
                        }

                        return(true);
                    }
                    catch (Exception ex)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                        return(false);
                    }
                }
            }
        }