Exemple #1
0
 public Export(List <WarehouseInfo> warehouses, OrderRecord record, bool editable = false, string message = "", bool result = false)
 {
     ID           = record.ID;
     Code         = record.Code;
     EmployeeID   = record.EmployeeID;
     EmployeeName = record.EmployeeName;
     WarehouseID  = record.WarehouseID;
     if (record.WarehouseID == 0 && warehouses.Count >= 1)
     {
         WarehouseID = warehouses.FirstOrDefault().ID;
     }
     Warehouses    = warehouses;
     ClientID      = record.ClientID;
     ClientCode    = record.ClientCode;
     ClientName    = record.ClientName;
     ClientPhone   = record.ClientPhone;
     ClientAddress = record.ClientAddress;
     ClientType    = record.ClientType;
     DiscountType  = record.DiscountType;
     DiscountValue = record.DiscountValue;
     DeliveryID    = record.DeliveryID;
     OrderReceipt  = record.Receipt;
     PayMethod     = record.PayMethod;
     ToExport      = record.ToExport;
     Discount      = record.Discount;
     Paid          = record.Paid;
     Data          = new List <ImexItem>();
     if (record.Items == null)
     {
         Selected = new ImexItem[] { }
     }
     ;
     else
     {
         Selected = record.Items;
     }
     Note     = record.Note;
     Status   = record.Status;
     Return   = record.Return;
     Message  = message;
     Result   = result;
     Editable = editable;
     Filter   = new ProductFilter();
 }
Exemple #2
0
        public ActionResult Copy(int id)
        {
            var record = new OrderRecord(Export.GetOrder(UserID, Employee.ID, id));

            record.ID = 0;
            var login = Login.Get(UserID);
            var model = new Export(
                WarehouseInfo.FindAuthorized(UserID, Employee.ID, Employee.BussinessID, UserID, DbAction.Order.View),
                record,
                login.Username == "admin");

            if (Request.IsAjaxRequest())
            {
                return(Json(new
                {
                    html = RenderPartialViewToString(Views.ExportPartial, model)
                }, JsonRequestBehavior.AllowGet));
            }
            return(View(Views.Export, model));
        }
Exemple #3
0
        public ActionResult Submit(OrderRecord record, FormCollection collection)
        {
            record.EmployeeName = Employee.Name;
            var action      = record.ID > 0 ? DbAction.Order.Modify : DbAction.Order.Create;
            var unavailable = ImexItem.Unavailable(UserID, Employee.ID, record.WarehouseID, action, record.Items, record.ID);
            var orderID     = 0;
            var message     = "";
            var result      = false;
            var login       = Login.Get(UserID);

            //if (record.ID > 0 && login.Username != "admin" && login.Type != LoginType.Office)
            //    message = "Bạn không có quyền cập nhật hóa đơn";
            if (unavailable.Count > 0)
            {
                message = String.Format("Số lượng vượt mức tồn kho:<br/>{0}", String.Join("<br/>", unavailable.Select(i => i.Name)));
            }
            else if (record.ClientID == 0 && (String.IsNullOrEmpty(record.ClientName) || String.IsNullOrEmpty(record.ClientPhone)))
            {
                message = "Thiếu thông tin khách hàng (tên, số điện thoại)";
            }
            else if (record.Items.Sum(i => i.Quantity * i.Price) - record.Discount - record.Paid != 0)
            {
                message = "Tổng tiền khuyến mãi và đã trả không bằng tổng tiền hóa đơn";
            }
            else
            {
                message = "Lưu thông tin không thành công";
                if (record.Items != null && record.Items.Length > 0)
                {
                    if (login.Username != "admin" && record.ID == 0)
                    {
                        record.SubmitDate = DateTime.Now;
                    }
                    if (login.Username != "admin")
                    {
                        record.NewEmployeeID = null;
                    }
                    if ((orderID = SKtimeManagement.Export.SaveOrder(UserID, Employee.ID, Employee.BussinessID, record)) > 0)
                    {
                        message = "Lưu thông tin thành công";
                        result  = true;
                    }
                }
                else
                {
                    message = "Không có sản phẩm được chọn";
                }
            }
            if (result)
            {
                // trigger creating income when do exchange product on order
                if (collection["Action"] == "Exchange")
                {
                    using (var data = new DataClassesDataContext())
                    {
                        data.TriggerInsertIncome(record.ID, Employee.ID, Employee.BussinessID, record.WarehouseID, record.Code, BaseModel.NewUniqueCode(UserID, Employee.ID, Employee.BussinessID, "Income"), DateTime.Now, Convert.ToDecimal(collection["TotalTransactionPaid"]));
                    }
                }

                var model = new ExportList();
                model.List    = Export.OrderHistory(UserID, Employee.ID, Employee.BussinessID);
                model.Current = Export.GetOrder(UserID, Employee.ID, orderID);
                foreach (var product in model.Current.Products)
                {
                    product.ProductUrl = RenderPartialViewToString("ProductUrl", product);
                }
                foreach (var product in model.Current.ReturnProducts)
                {
                    product.ProductUrl = RenderPartialViewToString("ProductUrl", product);
                }
                if (Request.IsAjaxRequest())
                {
                    return(Json(new
                    {
                        html = RenderPartialViewToString(Views.HistoryPartial, model)
                    }, JsonRequestBehavior.DenyGet));
                }
                return(RedirectToAction(Views.History));
            }
            else
            {
                var data  = ImexItem.Find(UserID, Employee.ID, action, Employee.BussinessID);
                var model = new Export(
                    WarehouseInfo.FindAuthorized(UserID, Employee.ID, Employee.BussinessID, UserID, DbAction.Order.View),
                    record, login.Username == "admin", message, result);
                if (record.Return)
                {
                    var returnRecord = new OrderRecord(Export.GetOrder(UserID, Employee.ID, record.ID), true);
                    var items        = model.Selected.ToList();
                    items.AddRange(returnRecord.Items);
                    model.Selected = items.ToArray();
                }
                if (Request.IsAjaxRequest())
                {
                    return(Json(new
                    {
                        html = RenderPartialViewToString(Views.ExportPartial, model)
                    }, JsonRequestBehavior.DenyGet));
                }
                return(RedirectToAction(Views.Export));
            }
        }
Exemple #4
0
        public static int SaveOrder(int userID, int employeeID, int bussinessID, OrderRecord record)
        {
            QueryOutput queryResult;
            var         exportID = "@ExportID";
            var         orderID  = "@OrderID";
            var         clientID = "@ClientID";
            var         now      = DateTime.Now;
            var         discount = record.Discount;
            var         paid     = record.Paid + record.ReturnValue;
            var         action   = DbAction.Order.Create;

            if (record.ID > 0)
            {
                action = DbAction.Order.Modify;
            }
            var sql = String.Format(
                @"declare {0} int = null declare {1} int = null declare {2} int = null
                declare @status nvarchar(10) = null",
                orderID, exportID, clientID);
            var code = record.Code;

            if (record.ID <= 0)
            {
                code = NewUniqueCode(userID, employeeID, bussinessID, "Order");
            }
            code = String.Format("'{0}'", code);
            if ((!record.ClientID.HasValue || record.ClientID <= 0) && !String.IsNullOrEmpty(record.ClientName))
            {
                var clientCode = String.IsNullOrEmpty(record.ClientCode) ? NewUniqueCode(userID, employeeID, bussinessID, "Client", 3, null) : record.ClientCode;
                sql += String.Format(
                    @" set {5} = (select top 1 ID from Client where BussinessID = {0} and Code = '{1}' and Status = 'active')
                    if ({5} is null) begin
                    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) end",
                    bussinessID, clientCode, record.ClientName, record.ClientPhone, record.ClientAddress, clientID);
            }
            else
            {
                sql += String.Format(" set {0} = {1}", clientID, record.ClientID.DbValue());
            }
            if (record.ID > 0)
            {
                sql += String.Format(
                    @" set {0} = {1}
                    set @status = (select Status from SKtimeManagement..[Order] where ID = {0})
                    declare @point int = (select sum(Point) from ExportProduct where OrderID = {0} and Returned = 0)
                    update Client set Point = (case when Point - @point < 0 then 0 else Point - @point end) where ID = {2}",
                    orderID, record.ID, clientID);
                if (!record.Return)
                {
                    sql += String.Format(" delete ExportProduct where OrderID = {0} and Returned = 0", orderID);
                }
                else
                {
                    sql += String.Format(" update ExportProduct set Returned = 1 where OrderID = {0}", orderID);
                }
                //var isRefund = record.Status == OrderStatus.Refunded;
                //sql += String.Format(@" if (isnull((select top 1 Status from SKtimeManagement..[Order] where ID = {0}), '') != N'{1}')
                //begin
                //    update q set q.Quantity = {2} from ProductQuantity q join ExportProduct o on q.ProductID = o.ProductID where o.OrderID = {0}
                //    {3}
                //end",
                //record.ID, record.Status,
                //isRefund ? "q.Quantity + o.Quantity" : "case when q.Quantity - o.Quantity < 0 then 0 else q.Quantity - o.Quantity end",
                //record.ClientID.HasValue ?
                //    String.Format(
                //        @"declare @point int = (select isnull(sum(Point), 0) from ExportProduct where OrderID = {0})
                //        update c set c.Point = {2} from Client c where c.ID = {1}",
                //        record.ID, record.ClientID, isRefund ? "case when c.Point - @point < 0 then 0 else c.Point - @point end" : "c.Point + @point") : "");
                sql += String.Format(
                    @" update SKtimeManagement..[Order] 
                    set Receipt = '{0}', Note = N'{2}', Status = N'{3}', DeliveryID = {5},
                    PayMethod = N'{6}', ClientID = {7}, Discount = {8}, Total = {9}, Returned = {10} {11}
                    where ID = {4}",
                    record.Receipt, paid, record.Note, record.Status, record.ID, record.DeliveryID.DbValue(), record.PayMethod,
                    clientID, discount, Math.Round(record.Items.Sum(i => i.Price * i.Quantity), 2), record.Return ? 1 : 0,
                    record.NewEmployeeID.HasValue ? String.Format(", EmployeeID = {0}", record.NewEmployeeID.Value) : "");
            }
            else
            {
                paid = record.Transactions != null?record.Transactions.Sum(i => i.Amount) : 0;

                sql += String.Format(
                    @" declare @ID table (ID int)
                    insert into SKtimeManagement..[Order](EmployeeID, BussinessID, ClientID, Code, SubmitDate, Receipt, Total, Discount, Paid, Note, PayMethod, DeliveryID, WarehouseID, Status) 
                    output inserted.ID into @ID
                    values ({0}, {1}, {2}, {3}, {4}, '{5}', {6}, {7}, {8}, N'{9}', N'{11}', {12}, {13}, '')
                    set {10} = (select top 1 ID from @ID) delete @ID",
                    employeeID, bussinessID, clientID,
                    code, record.SubmitDate.DbValue(), record.Receipt,
                    Math.Round(record.Items.Sum(i => i.Price * i.Quantity), 2), discount,
                    paid, record.Note, orderID, record.PayMethod, record.DeliveryID.DbValue(), record.WarehouseID
                    );
                if (record.Transactions != null)
                {
                    foreach (var tran in record.Transactions)
                    {
                        sql += tran.AddTransactionQuery(employeeID, TransactionClass.Order, ref orderID);
                    }
                }
            }
            if (record.ToExport)
            {
                if (record.ID > 0)
                {
                    code = String.Format("(select top 1 Code from SKtimeManagement..[Order] where ID = {0})", orderID);
                }
                sql += String.Format(
                    @" set {4} = (select top 1 ID from Export where Code = {2})
                    if ({4} is null)
                    begin
                        declare @export table (ID int)
                        insert Export(EmployeeID, BussinessID, Code, SubmitDate, WarehouseID, Status)
                        output inserted.ID into @export
                        values ({0}, {1}, {2}, '{3}', {5}, N'{6}')
                        set {4} = (select top 1 ID from @export) delete @export
                    end",
                    employeeID, bussinessID, code, now.ToString(Constants.DatabaseDatetimeString), exportID, record.WarehouseID, ExportStatus.Exported);
            }
            sql += String.Format(
                @" insert into ExportProduct(OrderID, ExportID, ProductID, Quantity, Price, Point) values {0}",
                String.Join(",", record.Items.Select(i => String.Format("({0}, {1}, {2}, {3}, {4}, {5})", orderID, exportID, i.ID, i.Quantity, i.Price, i.Point)))
                );
            sql += String.Format(
                @" update Client set Point = Point + {1} where ID = {0}
                delete ClientType where ClientID = {0}
                declare @type int = (select top 1 ID from Type where BussinessID = {7} and Status = 'active' and Revenue <= (select sum(Total - Discount) from [Order] where ClientID = {0} and Status <> N'{8}') order by Revenue desc, DiscountValue)
                if (@type is not null) begin
                    insert into ClientType(ClientID, TypeID, CreateDate, Status) 
                    values({0}, @type, getdate(), 'active')
                end
                select {4}",
                clientID, record.Items.Sum(i => i.Point), record.Status, OrderStatus.Refunded, orderID, record.WarehouseID,
                record.Status == OrderStatus.Refunded ? "q.Returned + p.Quantity" : "(case when q.Returned - p.Quantity < 0 then 0 else q.Returned - p.Quantity end)",
                bussinessID, OrderStatus.Refunded);
            var result = Query <int>(new DbQuery(userID, employeeID, action, sql, true, orderID, String.Format("(select top 1 Code + ' (Kho ' + (select Name from Warehouse where ID = WarehouseID) + '-' + format(Total, 'c0', 'vi-vn') + ')' from [Order] where ID = {0})", orderID)), out queryResult).FirstOrDefault();

            return(result);
        }