Exemple #1
0
        private TblContractHeader UpdateOrInsertContractHeader(TblContractHeader newRow, int index, int userIserial, out int outindex, string company)
        {
            throw new Exception("Save Contracts On Stitch");

            outindex = index;
            using (var context = new WorkFlowManagerDBEntities())
            {
                try
                {
                    newRow.TblRetailOrderProductionType1 = null;
                    newRow.TblSubContractor1             = null;
                    newRow.TblLkpSeason1       = null;
                    newRow.TblLkpBrandSection1 = null;
                    var brandCode = newRow.BrandCode;
                    var report    = context.TblBrandContractReports.FirstOrDefault(bcr => bcr.BrandCode == brandCode);
                    if (report != null)
                    {
                        newRow.ContractReport = report.ReportName;
                    }
                    var oldRow = context.TblContractHeaders.FirstOrDefault(th => th.Iserial == newRow.Iserial);
                    newRow.Amount = newRow.TblContractDetails.Sum(r => r.Total);



                    var budgetAmount = CalculateBudget(newRow, context);

                    var contractsAmount = context.TblContractDetails
                                          .Include(nameof(TblContractDetail.TblContractHeader1)).Where(r =>
                                                                                                       r.TblContractHeader1.BrandCode == newRow.BrandCode && r.TblLkpBrandSection == newRow.TblLkpBrandSection &&
                                                                                                       r.TblContractHeader1.TblLkpSeason == newRow.TblLkpSeason).ToList()
                                          .Select(r => new Tuple <int?, decimal>(r.TblLkpBrandSection,
                                                                                 r.TblContractHeader1.TblRetailOrderProductionType == 1 ?
                                                                                 (r.Qty * r.Cost) : r.TblContractHeader1.TblRetailOrderProductionType == 2 ?
                                                                                 (r.Qty * (r.Cost - r.AccCost)) : r.TblContractHeader1.TblRetailOrderProductionType == 3 ?
                                                                                 (r.Qty * (r.Cost - r.AccCost - r.FabricCost)) : (r.Qty * r.Cost)
                                                                                 )).GroupBy(r => r.Item1).Select(r => new Tuple <int?, decimal>(r.Key, r.Sum(d => d.Item2))).ToList();

                    var tblContractDetail = new List <TblContractDetail>();
                    newRow.TblContractDetails.ToArray().ForEach(d => tblContractDetail.Add(d));
                    if (oldRow != null)// الهيدر موجود قبل كده
                    {
                        CheckBudget(newRow, oldRow, budgetAmount, contractsAmount);

                        if (!oldRow.Approved && newRow.Approved)// كده لسه معموله ابروف
                        {
                            newRow.ApproveDate = DateTime.Now;
                            newRow.ApprovedBy  = userIserial;
                        }
                        foreach (var item in newRow.TblContractDetails.ToArray())
                        {
                            // هشوف بقى الى اتعدل والجديد
                            int temp;
                            //headeriserial = item.TblContractHeader;
                            item.TblContractHeader1 = null;
                            item.TblContractHeader  = newRow.Iserial;
                            UpdateOrInsertContractDetail(item, userIserial, 1, out temp);
                            item.TblContractHeader1Reference = null;
                        }
                        foreach (var item in newRow.TblContractPaymentByPeriods.ToArray())
                        {
                            // هشوف بقى الى اتعدل والجديد
                            int temp;
                            item.TblContractHeader1 = null;
                            item.TblContractHeader  = newRow.Iserial;
                            UpdateOrInsertContractPaymentDetail(item, userIserial, 1, out temp);
                            item.TblContractHeader1Reference = null;
                        }
                        //context.Entry(oldRow).CurrentValues.SetValues(newRow);
                        var result = SharedOperation.GenericUpdate(oldRow, newRow, context);
                        if (result.Count() > 0)
                        {
                            newRow.LastChangeUser = userIserial;
                            newRow.LastChangeDate = DateTime.Now;
                        }
                    }
                    else// الهيدر ده جديد
                    {
                        CheckBudget(newRow, null, budgetAmount, contractsAmount);

                        var seqTo  = 443;//context.tblChainSetups.FirstOrDefault(s => s.sGlobalSettingCode == "ContractApproveEmailTo");
                        var seqRow = context.TblSequenceProductions.FirstOrDefault(x => x.Iserial == seqTo);
                        newRow.Code = SharedOperation.HandelSequence(seqRow);
                        if (newRow.Approved)// كده معموله ابروف
                        {
                            newRow.ApproveDate = DateTime.Now;
                            newRow.ApprovedBy  = userIserial;
                        }
                        newRow.TblContractDetails.ForEach(cd => {
                            cd.TblColor1 = null; cd.TblSalesOrderColor1 = null;
                            cd.CreatedBy = userIserial; cd.CreationDate = DateTime.Now;
                        });
                        newRow.TblContractPaymentByPeriods.ForEach(cd => {
                            cd.CreatedBy = userIserial; cd.CreationDate = DateTime.Now;
                        });

                        if (newRow.TblContractDetails.FirstOrDefault() != null)
                        {
                            newRow.TblLkpBrandSection = newRow.TblContractDetails.FirstOrDefault().TblLkpBrandSection;
                        }

                        //   newRow.TblLkpBrandSection = null;
                        newRow.CreatedBy      = userIserial;
                        newRow.CreationDate   = DateTime.Now;
                        newRow.LastChangeDate = DateTime.Now;
                        newRow.LastChangeUser = userIserial;

                        context.TblContractHeaders.AddObject(newRow);
                    }
                    context.SaveChanges();
                }
                catch (Exception ex) { throw ex; }
                TblContractHeader rowTemp = newRow.Clone();
                try
                {
                    if (newRow.Approved)
                    {
                        try
                        {
                            var emailTo      = context.tblChainSetups.FirstOrDefault(s => s.sGlobalSettingCode == "ContractApproveEmailTo");
                            var emailFrom    = context.tblChainSetups.FirstOrDefault(s => s.sGlobalSettingCode == "ContractApproveEmailFrom");
                            var emailBody    = context.tblChainSetups.FirstOrDefault(s => s.sGlobalSettingCode == "ContractApproveEmailBody");
                            var emailSubject = context.tblChainSetups.FirstOrDefault(s => s.sGlobalSettingCode == "ContractApproveEmailSubject");
                            if (emailTo != null && emailFrom != null && emailBody != null &&
                                emailSubject != null &&
                                //!emailTo.sSetupValue.Split(';').Any(e => !SharedOperation.IsValidEmail(e)) &&
                                SharedOperation.IsValidEmail(emailFrom.sSetupValue))
                            {
                                string storeMail = emailTo.sSetupValue;
                                SharedOperation.SendEmail(null, emailFrom.sSetupValue,
                                                          emailTo.sSetupValue.Split(';').ToList(), string.Format(emailSubject.sSetupValue,
                                                                                                                 newRow.Code), string.Format(emailBody.sSetupValue, newRow.Code));
                            }
                        }
                        catch (Exception ex) { }
                    }
                    foreach (var item in GetContractDetail(0, int.MaxValue, newRow.Iserial))
                    {
                        var tblContractDetail = item.Clone();
                        tblContractDetail.TblLkpBrandSection1 = item.TblLkpBrandSection1;
                        tblContractDetail.TblSalesOrderColor1 = item.TblSalesOrderColor1;
                        rowTemp.TblContractDetails.Add(tblContractDetail);
                    }
                    foreach (var item in GetContractPaymentDetail(0, int.MaxValue, newRow.Iserial))
                    {
                        var tblContractPaymentByPeriod = item.Clone();
                        rowTemp.TblContractPaymentByPeriods.Add(tblContractPaymentByPeriod);
                    }
                    using (var db = new ccnewEntities(SharedOperation.GetSqlConnectionString(company)))
                    {
                        rowTemp.Supplier = db.TBLsuppliers.FirstOrDefault(s => s.Iserial == newRow.SupplierIserial);
                        rowTemp.Currency = db.TblCurrencyTests.FirstOrDefault(c => c.Iserial == rowTemp.TblCurrency);
                    }
                }
                catch { }
                return(rowTemp);
            }
        }