Example #1
0
        public Task <Tuple <SMU, SMU> > SplitSMU(SMU smuSelected, ObservableCollection <SMUDetail> originSource, ObservableCollection <SMUDetail> destinationSource)
        {
            using (var db = new OcphDbContext())
            {
                var trans = db.BeginTransaction();
                try
                {
                    //remove selected Move
                    foreach (var item in destinationSource.ToList())
                    {
                        if (!db.SMUDetails.Delete(O => O.SMUId == item.Id && O.colliesId == item.ColliesId))
                        {
                            throw new SystemException("Smu Gagal di Split");
                        }
                    }
                    var firstColly = originSource.FirstOrDefault();
                    var ptiModel   = new PTI {
                        Id = firstColly.PTIId, ShiperID = smuSelected.ShiperId, RecieverId = smuSelected.RecieverId, PayType = firstColly.PayType
                    };

                    var source = new ObservableCollection <collies>();
                    foreach (var item in destinationSource)
                    {
                        source.Add(new collies {
                            Id = item.ColliesId, Content = item.Content, IsSended = true, Pcs = item.Pcs, Price = item.Price, PtiId = item.PTIId, Weight = item.Weight
                        });
                    }

                    smuSelected.Pcs    = originSource.Sum(O => O.Pcs);
                    smuSelected.Weight = originSource.Sum(O => O.Weight);
                    smuSelected.Biaya  = originSource.Sum(O => O.Biaya);

                    //SUMU
                    var smudata = new smu {
                        PTIId = smuSelected.PTIId, CreatedDate = DateTime.Now
                    };
                    smudata.Id = db.SMU.InsertAndGetLastID(smudata);
                    if (smudata.Id <= 0)
                    {
                        throw new SystemException("Data Tidak Tersimpan");
                    }

                    foreach (var item in source.Where(O => O.IsSended))
                    {
                        var data = new smudetails {
                            colliesId = item.Id, SMUId = smudata.Id
                        };
                        if (!db.SMUDetails.Insert(data))
                        {
                            throw new SystemException("Data Tidak Tersimpan");
                        }
                    }


                    var memo = string.Format("Split Dari SMU T{0:D9}", smuSelected.Id);

                    var history = User.GenerateHistory(smudata.Id, BussinesType.SMU, ChangeType.Create, memo);
                    if (!db.Histories.Insert(history))
                    {
                        throw new SystemException("Gagal Simpan Data");
                    }

                    SMU sm = new SMU
                    {
                        CreatedDate  = smuSelected.CreatedDate,
                        Id           = smudata.Id,
                        IsSended     = false,
                        Pcs          = source.Sum(O => O.Pcs),
                        RecieverId   = smuSelected.RecieverId,
                        RecieverName = smuSelected.RecieverName,
                        ShiperId     = ptiModel.ShiperID,
                        ShiperName   = smuSelected.ShiperName,
                        Weight       = source.Sum(O => O.Weight),
                        Biaya        = source.Sum(O => O.Biaya)
                    };

                    //
                    trans.Commit();
                    return(Task.FromResult(new Tuple <SMU, SMU>(smuSelected, sm)));
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw new SystemException(ex.Message);
                }
            }
        }
Example #2
0
        public Task <SMU> CreateNewSMU(PTI pTISelected, ObservableCollection <collies> source)
        {
            using (var db = new OcphDbContext())
            {
                var date  = DateTime.Now;
                var trans = db.BeginTransaction();
                try
                {
                    var smudata = new smu {
                        PTIId = pTISelected.Id, CreatedDate = pTISelected.CreatedDate
                    };
                    smudata.Id = db.SMU.InsertAndGetLastID(smudata);
                    if (smudata.Id <= 0)
                    {
                        throw new SystemException("Data Tidak Tersimpan");
                    }

                    foreach (var item in source.Where(O => O.IsSended))
                    {
                        var data = new smudetails {
                            colliesId = item.Id, SMUId = smudata.Id
                        };
                        if (!db.SMUDetails.Insert(data))
                        {
                            throw new SystemException("Data Tidak Tersimpan");
                        }
                    }


                    if (source.Where(O => !O.IsSended).Count() <= 0)
                    {
                        if (!db.PTI.Update(O => new { O.OnSMU }, new pti {
                            Id = pTISelected.Id, OnSMU = true
                        }, O => O.Id == pTISelected.Id))
                        {
                            throw new SystemException("Data Tidak Tersimpan");
                        }
                        else
                        {
                            pTISelected.OnSMU = true;
                        }

                        var h = User.GenerateHistory(pTISelected.Id, BussinesType.PTI, ChangeType.Update, string.Format("Dibuatkan SMU dengan Nomor T{0:D9}", smudata.Id));
                        if (!db.Histories.Insert(h))
                        {
                            throw new SystemException("Gagal Simpan Data");
                        }
                    }



                    var history = User.GenerateHistory(smudata.Id, BussinesType.SMU, ChangeType.Create, "");
                    if (!db.Histories.Insert(history))
                    {
                        throw new SystemException("Gagal Simpan Data");
                    }

                    SMU sm = new SMU
                    {
                        CreatedDate  = date,
                        Id           = smudata.Id,
                        IsSended     = false,
                        Pcs          = source.Sum(O => O.Pcs),
                        RecieverId   = pTISelected.RecieverId,
                        RecieverName = pTISelected.RecieverName,
                        ShiperId     = pTISelected.ShiperID,
                        ShiperName   = pTISelected.ShiperName,
                        Weight       = source.Sum(O => O.Weight),
                        Biaya        = source.Sum(O => O.Biaya)
                    };

                    trans.Commit();
                    return(Task.FromResult(sm));
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw new SystemException(ex.Message);
                }
            }
        }