public Repair Save(ModelStateDictionary modelState, int userID, int employeeID, int bussinessID, string employeeName = null) { QueryOutput queryResult; if (!Validate(modelState)) { Result = false; return(null); } var query = ""; var action = ""; var id = ID.ToString(); if (ID > 0) { query = String.Format( @"update Repair set TransferDate = {1}, ReceivedDate = {2}, ProcessedDate = {3}, ReturnedDate = {4}, FinishDate = {5}, ProductState = N'{6}', Fee = {7}, Discount = {8}, Other = N'{9}', Note = N'{10}', ProductName = N'{11}', Service = N'{12}' where ID = {0}", new object[] { ID, TransferDate.DbValue(), ReceivedDate.DbValue(), ProcessedDate.DbValue(), ReturnedDate.DbValue(), FinishDate.DbValue(), ProductState, Fee, Discount, Other, Note, ProductName, Service }); action = DbAction.Repair.Modify; } else { Code = NewUniqueCode(userID, employeeID, bussinessID, "Repair"); EmployeeID = employeeID; BussinessID = bussinessID; EmployeeName = employeeName; SubmitDate = DateTime.Now; var clientID = "@ClientID"; query = String.Format("declare {0} int = {1}", clientID, ClientID.DbValue()); if ((!ClientID.HasValue || ClientID <= 0) && !String.IsNullOrEmpty(ClientName)) { query += String.Format( @" declare @client table (ID int) insert Client(BussinessID, Code, Name, Phone, Address, Point, Status) output inserted.ID into @client values ({0}, '{1}', N'{2}', '{3}', N'{4}', 0, 'active') set {5} = (select top 1 ID from @client)", bussinessID, String.IsNullOrEmpty(ClientCode) ? NewUniqueCode(userID, employeeID, bussinessID, "Client", 3, null) : ClientCode, ClientName, ClientPhone, ClientAddress, clientID); } query += String.Format( @" declare @output table (ID int) insert Repair(BussinessID, EmployeeID, WarehouseID, ProductID, Code, SubmitDate, TransferDate, ReceivedDate, ProcessedDate, ReturnedDate, FinishDate, ProductState, Fee, Discount, Other, Note, Status, ClientID, ProductName, ReceiveWarehouseID, Service) output inserted.ID into @output values ({0}, {1}, {2}, {3}, N'{4}', {5}, {6}, {7}, {8}, {9}, {10}, N'{11}', {12}, {13}, N'{14}', N'{15}', 'active', {16}, N'{17}', {18}, N'{19}')", new object[] { BussinessID, EmployeeID, WarehouseID, ProductID.DbValue(), Code, SubmitDate.DbValue(), TransferDate.DbValue(), ReceivedDate.DbValue(), ProcessedDate.DbValue(), ReturnedDate.DbValue(), FinishDate.DbValue(), ProductState, Fee, Discount, Other, Note, clientID, ProductName, ReceiveWarehouseID, Service }); id = "(select top 1 ID from @output)"; if (Transactions != null) { foreach (var tran in Transactions) { query += tran.AddTransactionQuery(employeeID, TransactionClass.Repair, ref id); } } action = DbAction.Repair.Create; } query += String.Format( @" select top 1 w.ID, w.BussinessID, w.EmployeeID, w.WarehouseID, w.ProductID, w.ClientID, w.Code, w.Service, case when w.ProductID is null then w.ProductName else p.Name end as [ProductName], w.SubmitDate, w.TransferDate, w.ReceivedDate, w.ProcessedDate, w.ReturnedDate, w.FinishDate, w.ProductState, w.Fee, w.Discount, w.Other, w.Note, wh.Name as [WarehouseName], rwh.Name as [ReceiveWarehouseName], e.Name as [EmployeeName], c.Name as [ClientName], c.Code as [ClientCode], c.Address as [ClientAddress], c.Phone as [ClientPhone], isnull(sum(t.Amount), 0) as [Paid] from Repair w join Warehouse wh on w.WarehouseID = wh.ID and w.Status = 'active' and ((select Username from Login where ID = {1}) = 'admin' or wh.ID in (select WarehouseID from LoginWarehouse where LoginID = {1})) join Warehouse rwh on w.ReceiveWarehouseID = rwh.ID join Employee e on w.EmployeeID = e.ID left join Product p on p.ID = w.ProductID left join Client c on w.ClientID = c.ID left join Transactions t on w.ID = t.RepairID where w.ID = {0} and w.Status = 'active' group by w.ID, w.BussinessID, w.EmployeeID, w.WarehouseID, w.ProductID, w.ProductName, w.ClientID, w.Code, w.Service, w.SubmitDate, w.TransferDate, w.ReceivedDate, w.ProcessedDate, w.ReturnedDate, w.FinishDate, w.ProductState, w.Fee, w.Discount, w.Other, w.Note, wh.Name, rwh.Name, p.Name, e.Name, c.Name, c.Code, c.Address, c.Phone" , id, userID); var record = Query <Repair>(new DbQuery(userID, employeeID, action, query, true, id), out queryResult).FirstOrDefault(); if (Result = (queryResult == QueryOutput.Success)) { Messages = new List <string>() { "Lưu thông tin thành công" } } ; return(record); }