private object LoadForm(long HeaderId)
    {
        ReportDocument rptDoc = ReportFactory.GetReport();
        var dc = DCFactory.GetDataContext<PartDataContext>();

        header = OrderDAO.GetOrderHeader(HeaderId);
        if (header != null)
        {
            rptDoc.Load(HttpContext.Current.Server.MapPath(@"~/Part/Report/Crystal/VMEPOrderSheet.rpt"));

            IList<PartOrderItem> data = header.OrderDetails
                            .Join(dc.Parts.Where(p => p.DatabaseCode == header.Dealer.DatabaseCode),
                                    od => od.PartCode, p => p.PartCode,
                                    (od, p) => new PartOrderItem(od) { PartName = p.VietnamName })
                            .ToList();
            rptDoc.SetDataSource(data);

            string dealer = header.Dealer.DealerName;

            rptDoc.SetParameterValue("OrderDate", header.OrderDate);
            rptDoc.SetParameterValue("Address", header.Warehouse.Address);
            rptDoc.SetParameterValue("Dealer", dealer);
        }

        return rptDoc;
    }
 private void detach_OrderHeaders1(OrderHeader entity)
 {
     this.SendPropertyChanging();
     entity.OrderHeader2 = null;
 }
 private void attach_OrderHeaders1(OrderHeader entity)
 {
     this.SendPropertyChanging();
     entity.OrderHeader2 = this;
 }
 private void attach_OrderHeaders(OrderHeader entity)
 {
     this.SendPropertyChanging();
     entity.Dealer = this;
 }
 private void detach_OrderHeaders(OrderHeader entity)
 {
     this.SendPropertyChanging();
     entity.Warehouse = null;
 }
 private void detach_OrderHeaders(OrderHeader entity)
 {
     this.SendPropertyChanging();
     entity.SystemData = null;
 }
    protected void cmdSave_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;
        UpdateOrderData();
        if (!CheckPartNo(true)) return;

        var db = DCFactory.GetDataContext<PartDataContext>();
        db.SubmitChanges();
        if (_commandName == CommandName.Create)
        {
            if (PartOrderDAO.Parts.Count(p => p.Quantity > 0) == 0) return;
            PartOrderDAO.CleanUp();
            PartOrderDAO.Merge();
            PartOrderDAO.InitPrice();
            PartOrderDAO.GetPrice();
            CheckQuantityForPacking();

            DateTime d = UserHelper.ParseDate(txtOrderDate.Text, true);
            var h = new OrderHeader
            {
                CreatedDate = DateTime.Now,
                CreatedBy = UserHelper.Username,
                Status = OrderStatus.OrderOpen,
                OrderType = "N",
                OrderSource = "V",
                ToDealer = ddlDealer.SelectedValue,
                DealerCode = UserHelper.DealerCode,
                ToLocation = int.Parse(ddlWH.SelectedValue),
                OrderDate = d,
                CanUndoAutoReceive = true,
                ChangeRemark = "N",
                TipTopProcessed = "N",
                Amount = 0,
                SentWarningOverQuotation = false,
                SentWarningOverShipping = false,
                AlreadyInStock = false
            };
            int line = 1;
            foreach (var item in PartOrderDAO.Parts)
            {
                var obj = new OrderDetail
                {
                    LineNumber = line++,
                    PartCode = item.PartCode,
                    OrderQuantity = item.Quantity,
                    QuotationQuantity = 0,
                    UnitPrice = item.UnitPrice,
                    ModifyFlag = "N",
                    Note = DateTime.Now.ToString(),
                    OrderHeader = h,
                    OriginalQty = item.Quantity,
                    Quo_Status = PartOrderQuoStatus.OrderNew
                };
            }
            db.OrderHeaders.InsertOnSubmit(h);
            db.SubmitChanges();
            PartOrderDAO.LoadFromDB(h.OrderHeaderId);
        }
        else
        {
            // check if tip-top has process
            var oh = db.OrderHeaders.Single(p => p.OrderHeaderId == OrderId);
            if (oh.TipTopProcessed == "Y")
            {
                lblTipTopProcessed.Visible = true;
                PartOrderDAO.LoadFromDB(OrderId);
                gv1.DataBind();
                DisableButton();
                return;
            }

            oh.OrderDate = UserHelper.ParseDate(txtOrderDate.Text, true);
            oh.ToDealer = ddlDealer.SelectedValue;
            oh.ToLocation = int.Parse(ddlWH.SelectedValue);
            db.SubmitChanges();

            PartOrderDAO.CleanUp();
            PartOrderDAO.Merge();
            PartOrderDAO.InitPrice();
            PartOrderDAO.GetPrice();

            int line = 1;
            var od = db.OrderDetails.Where(d => d.OrderHeaderId == OrderId).ToList();
            db.OrderDetails.DeleteAllOnSubmit(db.OrderDetails.Where(d => d.OrderHeaderId == OrderId));
            foreach (var item in PartOrderDAO.Parts)
            {
                //if (item.OrderDetailId != 0)
                //{
                //    var od = db.OrderDetails.SingleOrDefault(p => p.OrderDetailId == item.OrderDetailId);
                //    if (string.IsNullOrEmpty(item.PartCode) || item.Quantity == 0)
                //        db.OrderDetails.DeleteOnSubmit(od);
                //    else
                //    {
                //        od.LineNumber = line++;
                //        od.PartCode = item.PartCode;
                //        od.OrderQuantity = item.Quantity;
                //        od.Note = od.Note + ";" + DateTime.Now.ToString();
                //    };
                //}
                //else
                {

                    OrderDetail tod = new OrderDetail
                    {
                        LineNumber = line++,
                        PartCode = item.PartCode,
                        OrderQuantity = item.Quantity,
                        QuotationQuantity = 0,
                        UnitPrice = item.UnitPrice,
                        ModifyFlag = "N",
                        Note = DateTime.Now.ToString(),
                        OrderHeaderId = OrderId
                    };
                    if (oh.Status == OrderStatus.OrderOpen)
                    {
                        tod.OriginalQty = item.Quantity;
                        tod.Quo_Status = PartOrderQuoStatus.OrderNew;
                    }
                    else
                    {
                        OrderDetail odd = od.SingleOrDefault(d => d.LineNumber == item.Line && d.PartCode == item.PartCode);
                        if (odd != null)
                        {
                            if (odd.OrderQuantity == item.Quantity)
                            {
                                tod.OriginalQty = odd.OriginalQty;
                                tod.Quo_Status = odd.Quo_Status;
                            }
                            else
                            {
                                tod.OriginalQty = odd.OriginalQty;
                                tod.Quo_Status = PartOrderQuoStatus.OrderEditByDealer;
                            }
                        }
                        else
                        {
                            tod.OriginalQty = item.Quantity;
                            tod.Quo_Status = PartOrderQuoStatus.OrderNew;
                        }
                    }
                    db.OrderDetails.InsertOnSubmit(tod);
                }
                db.SubmitChanges();
            }

            if (oh.Status == OrderStatus.OrderSent)
                OrderDAO.SendOrder(OrderId, true);
            PartOrderDAO.LoadFromDB(oh.OrderHeaderId);
        }
        gv1.DataBind();
        PartOrderDAO.Clear();
        lblSaveOk.Visible = true;
        DisableButton();
    }
 private void attach_OrderHeaders(OrderHeader entity)
 {
     this.SendPropertyChanging("OrderHeaders");
     entity.Warehouse = this;
 }
 private void attach_OrderHeaders(OrderHeader entity)
 {
     this.SendPropertyChanging("OrderHeaders");
     entity.SystemData = this;
 }
 private void detach_OrderHeaders(OrderHeader entity)
 {
     this.SendPropertyChanging("OrderHeaders");
     entity.OrderHeader1 = null;
 }