private void btnOk_Click(object sender, EventArgs e) { refreshLineTotals(); Fiche = bindTool.DataObject; if (isNew) { Fiche.CreatedBy = StaticData.CurrentUserId; Fiche.CreatedDate = DateTime.Now; } Fiche.DocTypeId = DocumentType.Id; if (CreateInvoiceFromOrderFiche) { Fiche.DocTypeId = DocumentType.Id = 2; Fiche.CreatedDate = DateTime.Now; Fiche.CreatedBy = StaticData.CurrentUserId; Fiche.Id = 0; } Operation <FicheMasterView> postedFiche = OperationHandler.PostFiche(Fiche); if (postedFiche.Successful) { Close(); } SetError(postedFiche.Fail, false); }
//public static Operation<List<WarehouseMaster>> GetWarehouses() //{ // IServiceGate<List<WarehouseMaster>> sw = GateHandler.GetServiceGate<List<WarehouseMaster>>(); // Operation<List<WarehouseMaster>> op_ = sw.Get("GetWarehouses"); // return op_; //} //public static Operation<List<ItemPrice>> GetItemPricesList() //{ // IServiceGate<List<ItemPrice>> sw = GateHandler.GetServiceGate<List<ItemPrice>>(); // Operation<List<ItemPrice>> op_ = sw.Get("GetItemPricesList"); // return op_; //} public static Operation <FicheMasterView> PostFiche(FicheMasterView fiche) { ServiceClient client = new ServiceClient(); Dictionary <string, object> prms = new Dictionary <string, object>(); prms["fiche"] = fiche; Operation <FicheMasterView> operation = client.Post <FicheMasterView>("PostFiche", prms); return(operation); }
public Operation <FicheMasterView> ChangeFicheStatus(int Id, byte StatusId) { Operation <FicheMasterView> op_fiche = new Operation <FicheMasterView>(); try { FicheMasterView fiche = GetFicheById(Id).Value; fiche.Status_ = StatusId; connection.Update(fiche as FicheMaster); op_fiche.Value = fiche; op_fiche.Successful = true; } catch (Exception ex) { op_fiche.Fail = ex.Message; } return(op_fiche); }
public Operation <FicheMasterView> GetFicheById(int Id) { Operation <FicheMasterView> op_fiche = new Operation <FicheMasterView>(); try { FicheMasterView fiche = connection.Get <FicheMasterView>(Id); fiche.FicheLines = connection.Query <FicheLineView>("SELECT * FROM FicheLineView WHERE FicheId = " + Id.ToString()).ToList(); foreach (var line in fiche.FicheLines) { line.Services = connection.Query <FicheLineServiceView>("SELECT * FROM FicheLineServiceView WHERE FicheLineId = " + line.Id.ToString()).ToList(); } op_fiche.Value = fiche; op_fiche.Successful = true; } catch (Exception ex) { op_fiche.Fail = ex.Message; } return(op_fiche); }
public Operation <FicheMasterView> PostFiche(FicheMasterView fiche) { if (fiche == null) { return(new Operation <FicheMasterView>() { Fail = "Fiche is null !" }); } if (fiche.FicheLines == null) { return(new Operation <FicheMasterView>() { Fail = "Fiche Lines are null !" }); } if (fiche.FicheLines.Count == 0) { return(new Operation <FicheMasterView>() { Fail = "Fiche has no lines !" }); } Operation <FicheMasterView> op_fiche = new Operation <FicheMasterView>(); if (fiche.Id == 0) { IDbTransaction transaction = null; try { connection.Open(); transaction = connection.BeginTransaction(); // string _ficheno = connection.ExecuteScalar<string>("SP_GetNewDocumentNumber",new { DocumentTypeNumber = 1 },transaction); string _ficheno = connection.ExecuteScalar <string>("EXECUTE SP_GetNewDocumentNumber " + fiche.DocTypeId.ToString(), null, transaction); fiche.Ficheno = _ficheno; FicheMaster dbFicheModel = fiche; connection.Insert(dbFicheModel, transaction); int LineNumber = 0; foreach (FicheLine line in fiche.FicheLines) { LineNumber++; line.FicheId = fiche.Id; line.LineNumber = LineNumber; if (line.Note == null) { line.Note = ""; } } //List<FicheLine> dbLinesModel = fiche.FicheLines.Cast<FicheLine>().ToList(); //connection.Insert(dbLinesModel, transaction); foreach (var line in fiche.FicheLines) { FicheLine flmodel = line; connection.Insert(flmodel, transaction); foreach (var serviceLine in line.Services) { serviceLine.FicheLineId = line.Id; } line.Services.ForEach(x => x.Note = x.Note ?? string.Empty); connection.Insert(line.Services.Cast <FicheLineService>(), transaction); } transaction.Commit(); connection.Close(); op_fiche.Value = fiche; op_fiche.Successful = true; } catch (Exception ex) { try { transaction.Rollback(); } catch { } op_fiche.Fail = ex.Message; } finally { try { connection.Close(); } catch { } } } else { IDbTransaction transaction = null; try { FicheMasterView oldFiche = GetFicheById(fiche.Id).Value; connection.Open(); transaction = connection.BeginTransaction(); connection.Update((fiche as FicheMaster), transaction); CollectionChangesComparer <FicheLineView> ccf = new CollectionChangesComparer <FicheLineView>(); ccf.KeyFieldName = "Id"; ccf.SetInitialList(oldFiche.FicheLines); ccf.SetFinalList(fiche.FicheLines); var deletes = ccf.GetDeletes(); deletes.ForEach(x => connection.Delete(x.Services.Cast <FicheLineService>(), transaction)); connection.Delete(deletes.Cast <FicheLine>(), transaction); var inserts = ccf.GetInserts(); inserts.ForEach(x => x.FicheId = fiche.Id); foreach (var line in inserts) { FicheLine flmodel = line; connection.Insert(flmodel, transaction); inserts.ForEach(x => x.Services = x.Services ?? new List <FicheLineServiceView>()); foreach (var serviceLine in line.Services) { serviceLine.FicheLineId = line.Id; } line.Services.ForEach(x => x.Note = x.Note ?? string.Empty); connection.Insert(line.Services.Cast <FicheLineService>(), transaction); } // connection.Insert(inserts.Cast<FicheLine>(), transaction); //inserts.ForEach(x=>x.Services.ForEach(t=>t.FicheLineId= x.Id)); var updates = ccf.GetUpdates(); connection.Update(updates.Cast <FicheLine>(), transaction); foreach (var upItem in updates) { //var servicesOfLine = connection.Query<FicheLineService>(); } transaction.Commit(); connection.Close(); op_fiche.Value = fiche; op_fiche.Successful = true; } catch (Exception ex) { op_fiche.Fail = ex.Message; try { transaction.Rollback(); } catch { } } finally { try { connection.Close(); } catch { } } } return(op_fiche); }
private void Manp_FichesNew_Shown(object sender, EventArgs e) { btnAddCashTran.Enabled = btnDistributeExpences.Enabled = btnOk.Enabled = searchLookUpCard.Enabled = lookUpWarehouse.Enabled = IsEditMode; dataHolder = new DataHolder(); IBaseOperation loadOper = dataHolder.LoadEnumData(); if (!loadOper.Successful) { SetError(loadOper.Fail); return; } loadOper = dataHolder.LoadMasterData(); if (!loadOper.Successful) { SetError(loadOper.Fail); return; } searchLookUpCard.Properties.DataSource = dataHolder.Cards; rpLookupItem.DataSource = dataHolder.Items; rpLookupItem.DisplayMember = "ItemName"; rpLookupItem.ValueMember = "Id"; lookUpWarehouse.Properties.DataSource = dataHolder.Warehouses; foreach (ItemView item in dataHolder.Items.Where(x => x.IsLineService)) { ToolStripMenuItem menuItem = new ToolStripMenuItem(); menuItem.Tag = item.Id; menuItem.Text = item.ItemName; menuItem.Click += SrvBlukMenuItem_Click; tsBulkAddServices.DropDownItems.Add(menuItem); } if (isNew) { Fiche = new FicheMasterView(); Fiche.Lines = new List <FicheLineView>(); } else { Operation <FicheMasterView> op_ExistingFiche = OperationHandler.GetFicheById(Id); if (!op_ExistingFiche.Successful) { SetError(op_ExistingFiche.Fail); return; } Fiche = op_ExistingFiche.Value; txtDate.Text = Fiche.CreatedDate.ToString("dd.MM.yyyy"); DocumentType = dataHolder.DocumentTypes.Where(x => x.Id == Fiche.DocTypeId).First(); if (CreateInvoiceFromOrderFiche) { Fiche.SourceDocument = Fiche.Ficheno; Id = Fiche.Id = 0; Fiche.DocTypeId = 2; btnOk.Text = "Fakturala"; } } if (DocumentType.Id == 1 || DocumentType.Id == 2) { gvLines.Columns["LineExpense"].Visible = false; tabNavigationExpencesPage.PageVisible = false; } else { gvLines.Columns["LineServicesTotal"].Visible = false; gvLines.Columns["IsCustomerItem"].Visible = false; gvLines.Columns["IsSketched"].Visible = false; gvLines.Columns["IsTemplated"].Visible = false; gcServicesOfLine.Visible = false; gcLines.ContextMenuStrip = null; tabNavigationExpencesPage.PageVisible = true; } bindTool.BindControl(searchLookUpCard, nameof(Fiche.CardId)); bindTool.BindControl(lookUpWarehouse, nameof(Fiche.SourceWarehouse)); bindTool.BindControl(txtFicheno, nameof(Fiche.Ficheno)); bindTool.BindControl(txtNote1, nameof(Fiche.Note1)); bindTool.BindControl(txtNote2, nameof(Fiche.Note2)); bindTool.BindControl(txtNote3, nameof(Fiche.Note3)); bindTool.BindControl(txtNote4, nameof(Fiche.Note4)); bindTool.BindControl(txtSourceDoc, nameof(Fiche.SourceDocument)); bindTool.BindControl(spCurrencyRate, nameof(Fiche.CurrencyRate)); bindTool.DataObject = Fiche; if (dataHolder.Warehouses.Count > 0) { lookUpWarehouse.EditValue = dataHolder.Warehouses.First().Number; } gcLines.DataSource = null; gcLines.DataSource = Fiche.Lines; // gcLines.DataSource = Fiche.FicheLines; //cmbItemType.SelectedIndex = 0; RefreshCashTrans(); }
public Operation <FicheMasterView> PostFiche(FicheMasterView fiche) { Operation <FicheMasterView> operation = new Operation <FicheMasterView>(); IDbTransaction transaction = null; IDbConnection dbConnection = null; try { if (fiche == null) { return(new Operation <FicheMasterView>() { Fail = "Fiche is null !" }); } if (fiche.Lines == null) { return(new Operation <FicheMasterView>() { Fail = "Fiche has no lines !" }); } if (fiche.Lines.Count == 0) { return(new Operation <FicheMasterView>() { Fail = "Fiche has no lines !" }); } int LineNumber = 0; fiche.Lines.ForEach(x => x.LineTotalAcc = x.LineNetTotal + x.LineExpense); try { dbConnection = connection; dbConnection.Open(); transaction = dbConnection.BeginTransaction(); } catch (Exception ex) { return(new Operation <FicheMasterView>() { Fail = ex.Message }); } fiche.Lines.ForEach(x => x.LineTotalAcc = x.LineNetTotal + x.LineExpense + x.LineServices.Sum(y => y.LineNet)); fiche.LinesTotal = fiche.Lines.Sum(x => x.LineTotal); fiche.LineDiscountsTotal = fiche.Lines.Sum(x => x.LineDiscount); fiche.LinesNetTotal = fiche.Lines.Sum(x => x.LineNetTotal); fiche.FicheServicesNetTotal = fiche.Lines.Sum(x => x.LineServices.Sum(y => y.LineNet)); fiche.FicheNetTotal = fiche.LinesNetTotal + fiche.FicheServicesNetTotal - fiche.FicheDiscount; fiche.LineExpensesTotal = fiche.Lines.Sum(x => x.LineExpense); fiche.FicheTotalAcc = fiche.Lines.Sum(x => x.LineTotalAcc); FicheMaster ficheMaster = fiche.GetEligibleOjbect <FicheMaster>(); FicheMasterView existingFicheView = GetFicheById(fiche.Id.ToString()).Value; List <FicheLineView> oldLines = new List <FicheLineView>(); List <FicheLineView> newLines = fiche.Lines; if (fiche.Id == 0) { fiche.Ficheno = GetNewNumber(fiche.DocTypeId, transaction); ficheMaster.Ficheno = fiche.Ficheno; switch (fiche.DocTypeId) { case 1: ficheMaster.StatusId = 1; break; case 2: ficheMaster.StatusId = 5; break; case 3: ficheMaster.StatusId = 19; break; case 4: ficheMaster.StatusId = 17; break; } dbConnection.Insert(ficheMaster, transaction); } else { oldLines = existingFicheView.Lines; dbConnection.Update(ficheMaster, transaction); } fiche.Id = ficheMaster.Id; foreach (FicheLineView line in fiche.Lines) { line.LineServices = line.LineServices.Where(x => x.Quantity > 0).ToList(); LineNumber++; line.FicheId = fiche.Id; line.LineNumber = LineNumber; if (line.Note == null) { line.Note = ""; } } CollectionChangesComparer <FicheLineView> ccf = new CollectionChangesComparer <FicheLineView>(); ccf.KeyFieldName = "Id"; ccf.SetInitialList(oldLines); ccf.SetFinalList(newLines); var deletes = ccf.GetDeletes(); deletes.ForEach(x => dbConnection.Delete(x.LineServices.GetEligibleOjbect <List <FicheLineService> >(), transaction)); dbConnection.Delete(deletes.GetEligibleOjbect <List <FicheLine> >(), transaction); var inserts = ccf.GetInserts(); inserts.ForEach(x => x.FicheId = fiche.Id); foreach (var line in inserts) { line.FicheId = fiche.Id; FicheLine flmodel = line.GetEligibleOjbect <FicheLine>(); dbConnection.Insert(flmodel, transaction); line.Id = flmodel.Id; inserts.ForEach(x => x.LineServices = x.LineServices ?? new List <FicheLineServiceView>()); foreach (var serviceLine in line.LineServices) { serviceLine.FicheLineId = line.Id; } dbConnection.Insert(line.LineServices.GetEligibleOjbect <List <FicheLineService> >(), transaction); } var updates = ccf.GetUpdates(); dbConnection.Update(updates.GetEligibleOjbect <List <FicheLine> >(), transaction); foreach (FicheLineView lineView in newLines) { List <FicheLineServiceView> oldServicesOfLine = oldLines.Where(x => x.Id == lineView.Id).First().LineServices; List <FicheLineServiceView> currentServicesOfLine = lineView.LineServices; CollectionChangesComparer <FicheLineServiceView> ccs = new CollectionChangesComparer <FicheLineServiceView>(); ccs.KeyFieldName = "Id"; ccs.SetInitialList(oldServicesOfLine); ccs.SetFinalList(currentServicesOfLine); dbConnection.Delete(ccs.GetDeletes().GetEligibleOjbect <List <FicheLineService> >(), transaction); dbConnection.Update(ccs.GetUpdates().GetEligibleOjbect <List <FicheLineService> >(), transaction); List <FicheLineServiceView> srvInserts = ccs.GetInserts(); srvInserts.ForEach(x => x.FicheLineId = lineView.Id); dbConnection.Insert(srvInserts.GetEligibleOjbect <List <FicheLineService> >(), transaction); } transaction.Commit(); dbConnection.Close(); operation.Value = fiche; operation.Successful = true; } catch (Exception gex) { operation.Fail = gex.Message; if (transaction == null) { transaction.Rollback(); transaction.Dispose(); } if (dbConnection == null) { dbConnection.Dispose(); } } return(operation); }