private void button1_Click(object sender, EventArgs e) { var viewModel = new SellingViewModel(); viewModel.PartNo = (string)listBox1.SelectedValue; viewModel.SalesJobNumber = (int)listBox2.SelectedValue; viewModel.Quantity = (int)numericUpDown1.Value; viewModel.UnitPrice = (int)numericUpDown2.Value; viewModel.SellingDay = dateTimePicker1.Value; if (CanSale(viewModel.PartNo, viewModel.Quantity)) { var service = new SellingService(); var result = service.Create(viewModel); if (result.IsSuccessful) { MessageBox.Show("新增進貨資料成功"); } else { var path = result.WriteLog(); MessageBox.Show($"發生錯誤,請參考 {path}"); } } else { MessageBox.Show("庫存不足"); } }
public OperationResult Create(SellingViewModel input) { var result = new OperationResult(); try { BizModel context = new BizModel(); using (var transaction = context.Database.BeginTransaction())//Rollback { var sellCount = input.Quantity; var procurementRepo = new BizRepository <Procurement>(context); var sellingSourceRepo = new BizRepository <SellingSource>(context); var sellingRepo = new BizRepository <Selling>(context); Selling entity = new Selling() { PartNo = input.PartNo, Quantity = input.Quantity, SalesJobNumber = input.SalesJobNumber, SellingDay = input.SellingDay, UnitPrice = input.UnitPrice }; sellingRepo.Create(entity); context.SaveChanges(); var products = procurementRepo.GetAll() .Where((x) => x.PartNo == input.PartNo).OrderBy((x) => x.PurchasingDay); foreach (var p in products) { if (sellCount <= 0) { break; } else { if (p.InvetoryQuantity >= sellCount) { p.InvetoryQuantity = p.InvetoryQuantity - sellCount; CreateSellingSource(sellingSourceRepo, entity.SellingId, p.ProcurementId, sellCount); sellCount = 0; } else { sellCount = sellCount - p.InvetoryQuantity; CreateSellingSource(sellingSourceRepo, entity.SellingId, p.ProcurementId, p.InvetoryQuantity); p.InvetoryQuantity = 0; } } } context.SaveChanges(); result.IsSuccessful = true; transaction.Commit(); } } catch (Exception ex) { result.IsSuccessful = false; result.exception = ex; } return(result); }
public IActionResult Buy(int id) { SellingViewModel sell = new SellingViewModel(); sell.PortFolioID = id; return(View(sell)); }
public async Task <IActionResult> Selling(SellingViewModel model) { model.RegistrationId = _db.Registrations.GetRegID_ByUserName(User.Identity.Name); if (!ModelState.IsValid) { UnprocessableEntity(ModelState); } var response = await _db.Selling.AddCustomAsync(model, _db).ConfigureAwait(false); return(Json(response)); }