Beispiel #1
0
 private RouteCardInvoiceHeader UpdateOrInsertRouteCardInvoiceHeader(RouteCardInvoiceHeader newRow, bool save, int index, out int outindex)
 {
     outindex = index;
     using (var context = new WorkFlowManagerDBEntities())
     {
         if (save)
         {
             var seqCode = SharedOperation.GetChainSetup("GlRouteInvoiceSequence");
             var seqProd = context.TblSequenceProductions.FirstOrDefault(w => w.Code == seqCode);
             newRow.Code = SharedOperation.HandelSequence(seqProd);
             context.RouteCardInvoiceHeaders.AddObject(newRow);
         }
         else
         {
             var oldRow = (from e in context.RouteCardInvoiceHeaders
                           where e.Iserial == newRow.Iserial
                           select e).SingleOrDefault();
             if (oldRow != null)
             {
                 SharedOperation.GenericUpdate(oldRow, newRow, context);
             }
         }
         context.SaveChanges();
         return(newRow);
     }
 }
Beispiel #2
0
        private int DeleteRouteCardInvoiceHeader(RouteCardInvoiceHeader row)
        {
            using (var context = new WorkFlowManagerDBEntities())
            {
                var oldRow = (from e in context.RouteCardInvoiceHeaders
                              where e.Iserial == row.Iserial
                              select e).SingleOrDefault();
                if (oldRow != null)
                {
                    context.DeleteObject(oldRow);
                }

                context.SaveChanges();
            }
            return(row.Iserial);
        }
Beispiel #3
0
        private RouteCardInvoiceHeader PostRouteCardInvoice(RouteCardInvoiceHeader row, int user, string company)
        {
            using (var scope = new TransactionScope())
            {
                short?lang = 0;
                using (var entity = new WorkFlowManagerDBEntities())
                {
                    var firstOrDefault = entity.TblAuthUsers.FirstOrDefault(x => x.Iserial == user);
                    if (firstOrDefault != null)
                    {
                        lang = firstOrDefault.CurrLang;
                    }

                    entity.CommandTimeout = 0;
                    var query = entity.RouteCardInvoiceHeaders.FirstOrDefault(x => x.Iserial == row.Iserial);

                    string desc        = "Route TransNo " + row.Code;
                    var    markuptrans =
                        entity.RouteCardInvoiceMarkupTransProds.Include("TblMarkupProd1.TblMarkupGroupProd1")
                        .Where(x => x.RouteCardInvoiceHeader == row.Iserial && x.Type == 0);

                    var cost =
                        entity.RouteCardInvoiceDetails.Where(x => x.RouteCardInvoiceHeader == row.Iserial).Sum(w => w.Cost * w.Qty);

                    double totalWithItemEffect    = 0;
                    double totalWithoutItemEffect = 0;
                    foreach (var variable in markuptrans)
                    {
                        if (variable.TblMarkupProd1.ItemEffect == false)
                        {
                            if (variable.MiscValueType == 0)
                            {
                                totalWithoutItemEffect = (double)(totalWithoutItemEffect + (cost * (variable.MiscValue / 100)));
                            }
                            else
                            {
                                totalWithoutItemEffect = (double)(totalWithoutItemEffect + variable.MiscValue);
                            }
                        }
                        else
                        {
                            if (variable.MiscValueType == 0)
                            {
                                totalWithItemEffect = (double)(totalWithItemEffect + (cost * (variable.MiscValue / 100)));
                            }
                            else
                            {
                                totalWithItemEffect = (double)(totalWithItemEffect + variable.MiscValue);
                            }
                        }
                    }
                    if (query != null)
                    {
                        query.MiscWithoutItemEffect = totalWithoutItemEffect;
                        query.Misc = totalWithItemEffect;
                        if (totalWithItemEffect != 0)
                        {
                            var queryDetail =
                                entity.RouteCardInvoiceDetails.Where(x => x.RouteCardInvoiceHeader == row.Iserial).ToList();
                            foreach (var variable in queryDetail)
                            {
                                variable.Misc = (variable.Cost / cost) * totalWithItemEffect;
                            }
                        }
                        entity.SaveChanges();
                        if (query != null)
                        {
                            query.Status   = 1;
                            query.TblUser  = user;
                            query.PostDate = DateTime.Now;
                            using (var db = new ccnewEntities(SharedOperation.GetSqlConnectionString(company)))
                            {
                                var journal = db.tblChainSetupTests.FirstOrDefault(x => x.sGlobalSettingCode == "GlRouteInvoice").sSetupValue;

                                int journalint = db.TblJournals.FirstOrDefault(x => x.Code == journal).Iserial;

                                var newLedgerHeaderProdRow = new TblLedgerHeader
                                {
                                    CreatedBy          = user,
                                    CreationDate       = DateTime.Now,
                                    Description        = desc,
                                    DocDate            = row.DocDate,
                                    TblJournal         = journalint,
                                    TblTransactionType = 9,
                                    TblJournalLink     = query.Iserial
                                };
                                int temp;
                                var glserive = new GlService();
                                glserive.UpdateOrInsertTblLedgerHeaders(newLedgerHeaderProdRow, true, 0, out temp, user, company);
                                var sqlParam = new List <SqlParameter> {
                                    new SqlParameter
                                    {
                                        ParameterName = "Iserial",
                                        Value         = row.Iserial.ToString(CultureInfo.InvariantCulture),
                                        SqlDbType     = SqlDbType.NVarChar
                                    },
                                };
                                var list = entity.ExecuteStoreQuery <GlGroupsDtp>("exec GlRouteCardInvoicePostingToGl  @Iserial",
                                                                                  sqlParam.ToArray()).ToList();


                                #region MarkUp

                                foreach (var rr in markuptrans)
                                {
                                    var currencyrow = db.TblCurrencyTests.First(w => w.Iserial == rr.TblCurrency);
                                    var glAccount   =
                                        db.Entities.FirstOrDefault(
                                            x => x.Iserial == rr.TblMarkupProd && x.scope == 0 && x.TblJournalAccountType == 9).AccountIserial;
                                    var vendorAccountMarkUp =
                                        db.Entities.FirstOrDefault(
                                            x => x.Iserial == rr.EntityAccount && x.scope == 0 && x.TblJournalAccountType == rr.TblJournalAccountType);

                                    var    drorCr = true;
                                    double?total  = 0;
                                    if (rr.MiscValueType == 0)
                                    {
                                        total = (total + cost * (rr.MiscValue / 100)) * rr.TblMarkupProd1.TblMarkupGroupProd1.Direction;
                                    }
                                    else
                                    {
                                        total = (total + rr.MiscValue) * rr.TblMarkupProd1.TblMarkupGroupProd1.Direction;
                                    }
                                    if (total > 0)
                                    {
                                        drorCr = false;
                                    }
                                    var markupdes = rr.TblMarkupProd1.Ename + query.Code;
                                    if (lang == 0)
                                    {
                                        markupdes = rr.TblMarkupProd1.Aname + query.Code;
                                    }
                                    decimal totalModified = (decimal)total;
                                    if (total < 0)
                                    {
                                        totalModified = (decimal)(total * -1);
                                    }
                                    var markupVendorDiscount = new TblLedgerMainDetail();

                                    markupVendorDiscount = new TblLedgerMainDetail
                                    {
                                        Amount                = totalModified,
                                        Description           = markupdes,
                                        ExchangeRate          = currencyrow.ExchangeRate,
                                        TblCurrency           = rr.TblCurrency,
                                        TransDate             = row.DocDate,
                                        TblJournalAccountType = 0,
                                        EntityAccount         = glAccount,
                                        GlAccount             = glAccount,
                                        TblLedgerHeader       = newLedgerHeaderProdRow.Iserial,
                                        PaymentRef            = query.Code,
                                        DrOrCr                = !drorCr
                                    };

                                    glserive.UpdateOrInsertTblLedgerMainDetails(markupVendorDiscount, true, 000, out temp, company,
                                                                                user);

                                    if (glAccount != 0)
                                    {
                                        var markupVendor = new TblLedgerMainDetail
                                        {
                                            Amount                = totalModified,
                                            Description           = markupdes,
                                            ExchangeRate          = currencyrow.ExchangeRate,
                                            TblCurrency           = rr.TblCurrency,
                                            TransDate             = row.DocDate,
                                            TblJournalAccountType = rr.TblJournalAccountType,
                                            EntityAccount         = vendorAccountMarkUp.Iserial,
                                            GlAccount             = vendorAccountMarkUp.AccountIserial,
                                            TblLedgerHeader       = newLedgerHeaderProdRow.Iserial,
                                            PaymentRef            = query.Code,
                                            DrOrCr                = drorCr
                                        };

                                        glserive.UpdateOrInsertTblLedgerMainDetails(markupVendor, true, 000, out temp, company, user);
                                    }
                                }

                                #endregion MarkUp

                                foreach (var rr in list.GroupBy(x => x.GroupName))
                                {
                                    glserive.PostInvPurchaseAndTax(query, newLedgerHeaderProdRow, rr, company, user, list, desc);
                                }
                                glserive.CorrectLedgerHeaderRouding(newLedgerHeaderProdRow.Iserial, company, user);
                            }

                            entity.SaveChanges();
                            scope.Complete();
                        }
                        return(query);
                    }
                    return(null);
                }
            }
        }