Exemple #1
0
        public SellViewModel()
        {
            loadUserCurrentLogin();
            TotalPrice   = 0;
            IsEnableName = false;
            List         = new List <ProductTable>(DataProvider.Ins.Entities.ProductTable);
            ListSupplier = new List <SupplierTable>(DataProvider.Ins.Entities.SupplierTable);
            ListUnit     = new List <UnitTable>(DataProvider.Ins.Entities.UnitTable);
            ListBill     = new List <BillModel>();
            _ListTemp    = new List <BillModel>();

            LoadEditCommand = new RelayCommand <object>((p) => { return(true); }, (p) => { LoadDialogAccountEdit(); });

            AddBillCommand = new RelayCommand <object>((p) =>
            {
                if (SelectedItems == null)
                {
                    return(false);
                }
                SelectedItemsBill = null;
                return(true);
            },
                                                       (p) =>
            {
                BillModel bill            = new BillModel();
                ProductTable billProduct  = new ProductTable();
                billProduct.ID            = SelectedItems.ID;
                billProduct.DisplayName   = SelectedItems.DisplayName;
                billProduct.SupplierTable = SelectedItems.SupplierTable;
                billProduct.UnitTable     = SelectedItems.UnitTable;
                int Price     = 0;
                var listPrice = DataProvider.Ins.Entities.ProductTable.Where(x => x.ID == SelectedItems.ID).Select(x => x.Price);

                foreach (var item in listPrice)
                {
                    Price = item;
                }

                bill.product      = billProduct;
                bill.Price        = Price;
                bill.Amount       = 1;
                bill.PriceItems   = Price;
                bill.AmountChange = AmountChange;

                if (LoadInventory(bill.product.ID) <= 0)
                {
                    LoadDialogError("Sản Phẩm Không Còn Trong Kho! Vui Lòng Liên Hệ Quản Trị Viên!");
                    return;
                }

                TotalPrice += Price;

                var listCheckDuplicate = _ListTemp.Where(x => x.product.ID == bill.product.ID);
                if (listCheckDuplicate.Count() != 0)
                {
                    int DuplicateProduct = _ListTemp.Where(x => x.product.ID == bill.product.ID).FirstOrDefault().Amount;
                    _ListTemp.Where(x => x.product.ID == bill.product.ID).FirstOrDefault().Amount      = DuplicateProduct + 1;
                    _ListTemp.Where(x => x.product.ID == bill.product.ID).FirstOrDefault().PriceItems += Price;
                    ListBill      = new List <BillModel>(_ListTemp);
                    SelectedItems = null;
                    return;
                }
                _ListTemp.Add(bill);
                ListBill      = new List <BillModel>(_ListTemp);
                SelectedItems = null;
            });

            DeleteItemInBill = new RelayCommand <object>((p) =>
            {
                if (SelectedItemsBill == null)
                {
                    return(false);
                }
                SelectedItems = null;
                return(true);
            },
                                                         (p) =>
            {
                var FindToDelete = _ListTemp.Where(x => x.product.ID == SelectedItemsBill.product.ID).FirstOrDefault();
                TotalPrice      -= FindToDelete.PriceItems;
                _ListTemp.Remove(FindToDelete);
                ListBill          = new List <BillModel>(_ListTemp);
                SelectedItemsBill = null;
            });

            AmountChange = new RelayCommand <object>((p) =>
            {
                return(true);
            },
                                                     (p) =>
            {
                try
                {
                    var FindToEdit        = _ListTemp.Where(x => x.product.ID == SelectedItemsBill.product.ID).FirstOrDefault();
                    TotalPrice           -= FindToEdit.PriceItems;
                    FindToEdit.Amount     = SelectedItemsBill.Amount;
                    FindToEdit.PriceItems = FindToEdit.Amount * FindToEdit.Price;
                    TotalPrice           += FindToEdit.PriceItems;
                    ListBill = new List <BillModel>(_ListTemp);
                }
                catch
                {
                    MessageBox.Show(Message);
                }
            });

            PhoneCustomerChange = new RelayCommand <object>((p) =>
            {
                if (PhoneCustomer.ToString().Length < 9)
                {
                    return(false);
                }
                return(true);
            },
                                                            (p) =>
            {
                var Customer = DataProvider.Ins.Entities.CustomerTable.Where(x => x.Phone.ToLower().Contains(PhoneCustomer.ToString())).FirstOrDefault();
                if (Customer != null)
                {
                    NameCustomer = Customer.FullName;
                    IsEnableName = false;
                    return;
                }
                NameCustomer = "";
                IsEnableName = true;
            });

            CreateBill = new RelayCommand <object>((p) =>
            {
                if (TotalPrice <= 0 || NameCustomer == null || PhoneCustomer.ToString().Length < 9)
                {
                    return(false);
                }
                return(true);
            },
                                                   (p) =>
            {
                foreach (var item in _ListTemp)
                {
                    if (LoadInventory(item.product.ID) - item.Amount < 0)
                    {
                        LoadDialogError("Số Lượng Sản Phẩm Không Đủ Đáp Ứng Nhu Cầu!");
                        return;
                    }
                }

                if (IsEnableName)
                {
                    CustomerTable customer = new CustomerTable();
                    customer.FullName      = NameCustomer;
                    customer.Phone         = "0" + PhoneCustomer.ToString();
                    customer.Address       = "";
                    DataProvider.Ins.Entities.CustomerTable.Add(customer);
                    DataProvider.Ins.Entities.SaveChanges();
                }
                var ID_Customer = DataProvider.Ins.Entities.CustomerTable.Where(x => x.Phone.ToLower().Contains(PhoneCustomer.ToString())).FirstOrDefault();

                OutputTable output = new OutputTable();
                output.ID_User     = (int)App.Current.Properties["UserID"];
                output.DateOutput  = DateTime.Now;
                output.ID_Customer = ID_Customer.ID;
                DataProvider.Ins.Entities.OutputTable.Add(output);
                DataProvider.Ins.Entities.SaveChanges();
                var ID_Output = DataProvider.Ins.Entities.OutputTable.Where(x => x.ID_User == output.ID_User &&
                                                                            x.ID_Customer == output.ID_Customer && x.DateOutput == output.DateOutput.Date).FirstOrDefault();

                int ID = ID_Output.ID;
                OutPutDetailTable outPutDetail = new OutPutDetailTable();
                foreach (var item in _ListTemp)
                {
                    outPutDetail.Count      = item.Amount;
                    outPutDetail.ID_Product = item.product.ID;
                    outPutDetail.ID_Output  = ID;
                    outPutDetail.Price      = item.Price;
                    outPutDetail.TotalPrice = item.PriceItems;
                    DataProvider.Ins.Entities.OutPutDetailTable.Add(outPutDetail);
                    DataProvider.Ins.Entities.SaveChanges();
                }
                LoadDialogSuccessSale();
                TotalPrice    = 0;
                _ListBill     = null;
                ListBill      = null;
                NameCustomer  = null;
                PhoneCustomer = 0;
                IsEnableName  = false;
            });

            DestroyCommand = new RelayCommand <object>((p) =>
            {
                return(true);
            },
                                                       (p) =>
            {
                TotalPrice    = 0;
                ListBill      = null;
                _ListBill     = null;
                NameCustomer  = null;
                PhoneCustomer = 0;
                IsEnableName  = false;
                LoadDialogDeleted();
            });

            SearchCommand = new RelayCommand <object>((p) =>
            {
                return(true);
            },
                                                      (p) =>
            {
                if (SearchTerm == null)
                {
                    return;
                }

                List = new List <ProductTable>(DataProvider.Ins.Entities.ProductTable.Where(
                                                   x => x.DisplayName.ToLower().Contains(SearchTerm) ||
                                                   x.SupplierTable.DisplayName.ToLower().Contains(SearchTerm) || x.UnitTable.Descriptions.ToLower().Contains(SearchTerm)));
            });

            OpenBillWindow = new RelayCommand <Window>((p) =>
            {
                return(true);
            },
                                                       (p) =>
            {
                p.Close();
                BillWindow wd = new BillWindow();
                wd.ShowDialog();
            });
        }