private bool checkModelStateCreateEdit(ActionEnumForm action, business_purchase_order_model item)
        {
            if (string.IsNullOrEmpty(item.db.name))
            {
                ModelState.AddModelError("db.name", "required");
            }
            var search = repo.FindAll().Where(d => d.db.name == item.db.name && d.db.id != item.db.id).Count();

            if (search > 0)
            {
                ModelState.AddModelError("db.name", "existed");
            }
            if (item.db.id_customer == null)
            {
                ModelState.AddModelError("db.id_customer", "required");
            }
            //if (item.db.estimated_import_date == null)
            //{
            //    ModelState.AddModelError("db.estimated_import_date", "required");
            //}
            if (item.list_import_date.Count == 0)
            {
                ModelState.AddModelError("list_import_date", "required");
            }
            return(ModelState.IsValid);
        }
        public async Task <int> insert(business_purchase_order_model model)
        {
            model.db.status_del = 1;
            await _context.business_purchase_orders.AddAsync(model.db);

            _context.SaveChanges();
            saveDetail(model);
            return(1);
        }
        public void saveDetail(business_purchase_order_model model)
        {
            var delete1 = _context.business_purchase_order_import_dates.Where(t => t.id_business_purchase_order == model.db.id);

            _context.RemoveRange(delete1);
            _context.SaveChanges();
            model.list_import_date.ForEach(t =>
            {
                t.db.id            = 0;
                t.db.status_import = 1;
                t.db.id_business_purchase_order = model.db.id;
                t.db.quantity_import            = 0;
                t.db.create_day = model.db.create_date;
            });
            var listInsert1 = model.list_import_date.Select(d => d.db).ToList();

            _context.business_purchase_order_import_dates.AddRange(listInsert1);
            _context.SaveChanges();


            var listInsert = model.list_import_date.GroupBy(d => new
            {
                id_item                    = d.db.id_item,
                id_specification           = d.db.id_specification,
                id_unit                    = d.db.id_unit,
                type_add                   = d.db.type_add,
                conversion_factor          = d.db.conversion_factor,
                id_unit_main               = d.db.id_unit_main,
                id_business_purchase_order = d.db.id_business_purchase_order
            }).Select(d => new business_purchase_order_item_db()
            {
                id                         = 0,
                id_item                    = d.Key.id_item,
                id_specification           = d.Key.id_specification,
                conversion_factor          = d.Key.conversion_factor,
                id_business_purchase_order = d.Key.id_business_purchase_order,
                id_unit                    = d.Key.id_unit,
                id_unit_main               = d.Key.id_unit_main,
                quantity                   = d.Sum(d => d.db.quantity),
                quantity_unit_main         = d.Sum(d => d.db.quantity_unit_main),
                type_add                   = d.Key.type_add,
                note                       = "",
            }).ToList();


            var delete = _context.business_purchase_order_items.Where(t => t.id_business_purchase_order == model.db.id);

            _context.RemoveRange(delete);
            _context.SaveChanges();
            _context.business_purchase_order_items.AddRange(listInsert);
            _context.SaveChanges();
        }
        public int update(business_purchase_order_model model)
        {
            var db = _context.business_purchase_orders.Where(d => d.id == model.db.id).FirstOrDefault();

            db.name                = model.db.name;
            db.customer_address    = model.db.customer_address;
            db.customer_email      = model.db.customer_email;
            db.customer_fax        = model.db.customer_fax;
            db.customer_logo_path  = model.db.customer_logo_path;
            db.customer_name       = model.db.customer_name;
            db.customer_phone      = model.db.customer_phone;
            db.customer_tax_number = model.db.customer_tax_number;
            db.name                = model.db.name;
            db.note                = model.db.note;
            db.id_customer         = model.db.id_customer;
            db.update_by           = model.db.update_by;
            db.update_date         = model.db.update_date;
            _context.SaveChanges();
            saveDetail(model);
            return(1);
        }
 private bool checkModelStateEdit(business_purchase_order_model item)
 {
     return(checkModelStateCreateEdit(ActionEnumForm.edit, item));
 }