public ActionResult QuickCreate(int? quotationID)
        {
            QuotationQuickCreateModel quotation;
            if (quotationID == null)
                quotation = new QuotationQuickCreateModel() { Mode = PageMode.Add, bIsDirectLabour = false, bIsDummy = false, dIssueDate = DateTime.Now.ToString("dd-MM-yyyy") };
            else
                quotation = GetQuotationInfo(quotationID);

            return View(quotation);
        }
        public ActionResult QuickCreateSave(QuotationQuickCreateModel model, List<QuotationQuickCreateQuotationItemModel> items)
        {
            bool isSuccess = true;
            string exceptionMessage = string.Empty;
            string errorType = string.Empty;
            Member member = new Member("users");
            int quotationID=0;
            recsys_quotation quotation = null;
            recsys_relate_customers relateCustomer = null;

            try
            {
                #region Validation
                if (!model.nCustomerID.HasValue || model.nCustomerID == 0)
                {
                    return Json(new
                    {
                        isSuccess = false,
                        errorType = "Invalidation",
                        exceptionMessage = "請選擇Customer"
                    });
                }
                if (!model.nWorkingDistrictID.HasValue || model.nWorkingDistrictID == 0)
                {
                    return Json(new
                    {
                        isSuccess = false,
                        errorType = "Invalidation",
                        exceptionMessage = "請選擇工作地址地區"
                    });
                }
                if (!string.IsNullOrEmpty(model.sInvoiceNumber) && model.bIsDummy)
                {
                     return Json(new
                    {
                         isSuccess = false,
                        errorType = "Invalidation",
                        exceptionMessage = "請刪除Invoice No或取消選取'Dummy'"
                    });
                }
                if (!string.IsNullOrEmpty(model.sMinorWorkExtension) && model.sMinorWorkExtension.Length > 3)
                {
                    return Json(new
                    {
                        isSuccess = false,
                        errorType = "Invalidation",
                        exceptionMessage = "請修改Minor Work至3字以內'"
                    });
                }
                if (items != null && items.Count() > 0)
                {
                    List<string> quotationItemCodes = items.Select(i => i.sCode).ToList();
                    for (int i = 0; i < quotationItemCodes.Count(); i++)
                    {
                        if (!string.IsNullOrWhiteSpace(quotationItemCodes[i]))
                        {
                            for (int j = i + 1; j < quotationItemCodes.Count(); j++)
                            {
                                if (!string.IsNullOrEmpty(quotationItemCodes[j]) && !string.IsNullOrEmpty(quotationItemCodes[j].Trim()) && quotationItemCodes[j].Trim().ToUpper() == quotationItemCodes[i].Trim().ToUpper())
                                {
                                    return Json(new
                                    {
                                        isSuccess = false,
                                        errorType = "Quotation-Item-Duplication",
                                        exceptionMessage = "重覆Quotation item (" + quotationItemCodes[j] + ")",
                                    });
                                }
                            }
                        }
                    }
                }
                #endregion Validation

                if (model.Mode == PageMode.Add)
                {
                    #region Create Relate Customer
                    recsys_customers rawCustomer = this._db.recsys_customers.Where(c => c.id == model.nCustomerID.Value).FirstOrDefault();
                    relateCustomer = new recsys_relate_customers()
                    {
                        address1 = rawCustomer.address1,
                        address2 = model.sWorkingAddress,
                        center = rawCustomer.center,
                        code = rawCustomer.code,
                        contact = rawCustomer.contact,
                        customer_code = rawCustomer.customer_code,
                        customer_id = rawCustomer.id,
                        district1 = rawCustomer.district1,
                        district2 = model.nWorkingDistrictID,
                        email = rawCustomer.email,
                        fax1 = rawCustomer.fax1,
                        fax2 = rawCustomer.fax2,
                        group_id = rawCustomer.group_id,
                        manual_input_code = rawCustomer.manual_input_code,
                        name = rawCustomer.name,
                        chi_name = rawCustomer.chi_name,
                        prefix = rawCustomer.prefix,
                        relate_type = (int)Rec_System.Models.Customers.CustomerDataRelateType.Quotation,
                        reference_number = rawCustomer.reference_number,
                        remark = rawCustomer.remark,
                        tel1 = rawCustomer.tel1,
                        tel2 = rawCustomer.tel2,
                        title = rawCustomer.title,
                        type = rawCustomer.type
                    };
                    this._db.recsys_relate_customers.AddObject(relateCustomer);
                    this._db.SaveChanges();
                    #endregion Create Relate Customer

                    #region Create Quotation
                    quotation = new recsys_quotation()
                    {
                        customer_id = relateCustomer.id,
                        dummy = (byte)(model.bIsDummy ? 1 : 0),
                        dummy_date = this.ConvertDateStringFormat(model.dDummyDueDate, DATEPICKER_DATE_FORMAT),
                        initial = model.sInitial,
                        lang = 1,
                        order_number = model.sOrderNumber,
                        number = model.sACQNumber,
                        subcon_estimation = model.nSubConEstimation.HasValue ? model.nSubConEstimation : 0.00,
                        subcon_name = model.sSubConName,
                        subcon_estimation2 = model.nSubConEstimation2.HasValue ? model.nSubConEstimation2 : 0.00,
                        subcon_name2 = model.sSubConName2,
                        subcon_estimation3 = model.nSubConEstimation3.HasValue ? model.nSubConEstimation3 : 0.00,
                        subcon_name3 = model.sSubConName3,
                        supervision = model.nSupervision.HasValue ? model.nSupervision : 0.00,
                        supplier_id = model.nMaterialEstimation.HasValue ? model.nMaterialEstimation : 0.00,
                        material_estimation2 = model.nMaterialEstimation2.HasValue ? model.nMaterialEstimation2 : 0.00,
                        material_estimation3 = model.nMaterialEstimation3.HasValue ? model.nMaterialEstimation3 : 0.00,
                        supplier_estimation = model.nSupplierEstimation.HasValue ? model.nSupplierEstimation : 0.00,
                        supplier_name = model.sSupplierName,
                        supplier_estimation2 = model.nSupplierEstimation2.HasValue ? model.nSupplierEstimation2 : 0.00,
                        supplier_name2 = model.sSupplierName2,
                        supplier_estimation3 = model.nSupplierEstimation3.HasValue ? model.nSupplierEstimation3 : 0.00,
                        supplier_name3 = model.sSupplierName3,
                        payment_received_date = this.ConvertDateStringFormat(model.dPaymentRecdDate, DATEPICKER_DATE_FORMAT),
                        tender_number = model.sTenderNumber,
                        direct_labour = model.bIsDirectLabour,
                        direct_labour_cost = model.nDirectLabourCost.HasValue ? model.nDirectLabourCost : 0.00,
                        billing_date = this.ConvertDateStringFormat(model.dBillingDate, DATEPICKER_DATE_FORMAT),
                        confirm_date = this.ConvertDateStringFormat(model.dConfirmDate, DATEPICKER_DATE_FORMAT),
                        create_date = DateTime.Now,
                        invoice = model.sInvoiceNumber,
                        invoice_date = this.ConvertDateStringFormat(model.dInvoiceDate, DATEPICKER_DATE_FORMAT),
                        minor_work = model.sMinorWorkExtension,
                        minor_work_currency = model.nMinorWork.HasValue ? model.nMinorWork : 0.00,
                        gp = model.nGP.HasValue ? model.nGP : 0.00,
                        last_update = DateTime.Now,
                        remark = model.sRemark,
                        status = 1,
                        update_user_id = (int)member.infoBySession("id"),
                        issue_date = this.ConvertDateStringFormat(model.dIssueDate, DATEPICKER_DATE_FORMAT)
                    };
                    if (model.Mode==PageMode.Add)
                        this._db.recsys_quotation.AddObject(quotation);
                    this._db.SaveChanges();
                    #endregion Create Update Quotation
                }
                else
                {
                    #region Update Quotation
                    quotation = (from q in this._db.recsys_quotation
                                 where q.id == model.nQuotationID
                                 select q).FirstOrDefault();

                    quotation.dummy = (byte)(model.bIsDummy ? 1 : 0);
                    quotation.dummy_date = this.ConvertDateStringFormat(model.dDummyDueDate, DATEPICKER_DATE_FORMAT);
                    quotation.initial = model.sInitial;
                    quotation.order_number = model.sOrderNumber;
                    quotation.number = model.sACQNumber;
                    quotation.subcon_estimation = model.nSubConEstimation.HasValue ? model.nSubConEstimation : 0.00;
                    quotation.subcon_name = model.sSubConName;
                    quotation.subcon_estimation2 = model.nSubConEstimation2.HasValue ? model.nSubConEstimation2 : 0.00;
                    quotation.subcon_name2 = model.sSubConName2;
                    quotation.subcon_estimation3 = model.nSubConEstimation3.HasValue ? model.nSubConEstimation3 : 0.00;
                    quotation.subcon_name3 = model.sSubConName3;
                    quotation.supervision = model.nSupervision.HasValue ? model.nSupervision : 0.00;
                    quotation.supplier_id = model.nMaterialEstimation.HasValue ? model.nMaterialEstimation : 0.00;
                    quotation.material_estimation2 = model.nMaterialEstimation2.HasValue ? model.nMaterialEstimation2 : 0.00;
                    quotation.material_estimation3 = model.nMaterialEstimation3.HasValue ? model.nMaterialEstimation3 : 0.00;
                    quotation.supplier_estimation = model.nSupplierEstimation.HasValue ? model.nSupplierEstimation : 0.00;
                    quotation.supplier_name = model.sSupplierName;
                    quotation.supplier_estimation = model.nSupplierEstimation2.HasValue ? model.nSupplierEstimation2 : 0.00;
                    quotation.supplier_name2 = model.sSupplierName2;
                    quotation.supplier_estimation3 = model.nSupplierEstimation3.HasValue ? model.nSupplierEstimation3 : 0.00;
                    quotation.supplier_name3 = model.sSupplierName3;
                    quotation.payment_received_date = this.ConvertDateStringFormat(model.dPaymentRecdDate, DATEPICKER_DATE_FORMAT);
                    quotation.tender_number = model.sTenderNumber;
                    quotation.direct_labour = model.bIsDirectLabour;
                    quotation.direct_labour_cost = model.nDirectLabourCost.HasValue ? model.nDirectLabourCost : 0.00;
                    quotation.billing_date = this.ConvertDateStringFormat(model.dBillingDate, DATEPICKER_DATE_FORMAT);
                    quotation.confirm_date = this.ConvertDateStringFormat(model.dConfirmDate, DATEPICKER_DATE_FORMAT);
                    quotation.invoice = model.sInvoiceNumber;
                    quotation.invoice_date = this.ConvertDateStringFormat(model.dInvoiceDate, DATEPICKER_DATE_FORMAT);
                    quotation.minor_work = model.sMinorWorkExtension;
                    quotation.minor_work_currency = model.nMinorWork.HasValue ? model.nMinorWork : 0.00;
                    quotation.gp = model.nGP.HasValue ? model.nGP : 0.00;
                    quotation.last_update = DateTime.Now;
                    quotation.remark = model.sRemark;
                    quotation.update_user_id = (int)member.infoBySession("id");
                    quotation.issue_date = this.ConvertDateStringFormat(model.dIssueDate, DATEPICKER_DATE_FORMAT);

                    this._db.SaveChanges();
                    #endregion Update Quotation

                    #region Delete Original Quotation Items
                    var quotationItemsObject = (from qi in this._db.recsys_quotation_items
                                                  join r in this._db.recsys_relate on qi.id equals r.id2
                                                  where r.id1 == model.nQuotationID && r.table1 == "quotation" && r.table2 == "quotation_items"
                                                  select new { qi, r }).ToList();
                    foreach (var qio in quotationItemsObject)
                    {
                        this._db.recsys_quotation_items.DeleteObject(qio.qi);
                        this._db.recsys_relate.DeleteObject(qio.r);
                        this._db.SaveChanges();
                    }
                    #endregion Delete Original Quotation Items
                }

                quotationID = quotation.id;

                #region Create Quotation Items
                if (items != null && items.Count() > 0)
                {
                    int sequence = 1;
                    foreach (QuotationQuickCreateQuotationItemModel item in items)
                    {
                        #region Validation
                        if (string.IsNullOrWhiteSpace(item.sCode) && string.IsNullOrWhiteSpace(item.sDetail) && string.IsNullOrWhiteSpace(item.sDetailChi))
                            continue;
                        #endregion Validation

                        #region Create Quotation Item
                        recsys_quotation_items quotationItem = new recsys_quotation_items
                        {
                            nSequence = sequence,
                            code = string.IsNullOrEmpty(item.sCode) ? string.Empty : item.sCode.Trim(),
                            detail = string.IsNullOrEmpty(item.sDetailChi) ? null : item.sDetailChi.Trim(),
                            detail2 = string.IsNullOrEmpty(item.sDetail) ? null : item.sDetail.Trim(),
                            price = item.nPrice.HasValue ? item.nPrice.Value : 0.00,
                            last_update = DateTime.Now,
                            update_user_id = (int)member.infoBySession("id")
                        };
                        this._db.recsys_quotation_items.AddObject(quotationItem);
                        this._db.SaveChanges();
                        #endregion Create Quotation Item

                        #region Create Relate Record
                        recsys_relate relation = new recsys_relate
                        {
                            table1 = "quotation",
                            table2 = "quotation_items",
                            id1 = quotation.id,
                            id2 = quotationItem.id
                        };
                        this._db.recsys_relate.AddObject(relation);
                        this._db.SaveChanges();
                        #endregion Create Relate Record

                        sequence++;
                    }
                }
                #endregion Create Quotation Items
            }
            catch
            {
                isSuccess = false;
            }
            return Json(
                new
                {
                    isSuccess = isSuccess,
                    quotationID = quotationID
                });
        }
        public QuotationQuickCreateModel GetQuotationInfo(int? quotationID)
        {
            QuotationQuickCreateModel quotation_info = null;

                if (quotationID>0)
                {
                    var dummy = (from q in this._db.recsys_quotation
                                 where q.id == quotationID
                                 select q.dummy).FirstOrDefault();

                    var record = (from q in this._db.recsys_quotation
                                      join rc in this._db.recsys_relate_customers on q.customer_id equals rc.id into rcc
                                      from rc in rcc.DefaultIfEmpty()
                                      join d2 in this._db.recsys_district on rc.district2 equals d2.id
                                      where q.id == quotationID
                                      select new
                                      {
                                           Mode = PageMode.Edit,
                                           nQuotationID = q.id,
                                           nCustomerID = rc.customer_id,
                                           sCustomerCode = rc.customer_code,
                                           nWorkingDistrictID = rc.district2,
                                           sWorkingDistrictName = d2.name,
                                           sWorkingAddress = rc.address2,
                                           sACQNumber = q.number,
                                           sInitial = q.initial,
                                           dPaymentRecdDate = q.payment_received_date,
                                           sTenderNumber = q.tender_number,
                                           sOrderNumber = q.order_number,
                                           dIssueDate = q.issue_date,
                                           dConfirmDate = q.confirm_date,
                                           sRemark = q.remark,
                                           sSubConName = q.subcon_name,
                                           sSubConName2 = q.subcon_name2,
                                           sSubConName3 = q.subcon_name3,
                                           nSubConEstimation = q.subcon_estimation,
                                           nSubConEstimation2 = q.subcon_estimation2,
                                           nSubConEstimation3 = q.subcon_estimation3,
                                           nMaterialEstimation = q.supplier_id,
                                           nMaterialEstimation2 = q.material_estimation2,
                                           nMaterialEstimation3 = q.material_estimation3,
                                           sSupplierName = q.supplier_name,
                                           sSupplierName2 = q.supplier_name2,
                                           sSupplierName3 = q.supplier_name3,
                                           nSupplierEstimation = q.supplier_estimation,
                                           nSupplierEstimation2 = q.supplier_estimation2,
                                           nSupplierEstimation3 = q.supplier_estimation3,
                                           nSupervision = q.supervision,
                                           bIsDirectLabour = q.direct_labour.Value,
                                           nDirectLabourCost = q.direct_labour_cost,
                                           nMinorWork = q.minor_work_currency,
                                           sMinorWorkExtension = q.minor_work,
                                           nTotalEstCost = (q.subcon_estimation ?? 0) + (q.subcon_estimation2 ?? 0) + (q.subcon_estimation3 ?? 0) + (q.supplier_estimation ?? 0) + (q.supplier_estimation2 ?? 0) + (q.supplier_estimation3 ?? 0) + (q.supplier_id ?? 0) + (q.material_estimation2 ?? 0) + (q.material_estimation3 ?? 0) + (q.supervision ?? 0) + (q.direct_labour_cost ?? 0) + (q.minor_work_currency ?? 0),
                                           nGP = q.gp,
                                           sInvoiceNumber = q.invoice,
                                           dInvoiceDate = q.invoice_date,
                                           dBillingDate = q.billing_date,
                                           bIsDummy = (q.dummy == 1)?true:false,
                                           dDummyDueDate = q.dummy_date
                                       }).FirstOrDefault();

                    //convert record to model
                    quotation_info = new QuotationQuickCreateModel
                                     {
                                         Mode = record.Mode,
                                         nQuotationID = record.nQuotationID,
                                         nCustomerID = record.nCustomerID,
                                         sCustomerCode = record.sCustomerCode,
                                         nWorkingDistrictID = record.nWorkingDistrictID,
                                         sWorkingDistrictName = record.sWorkingDistrictName,
                                         sWorkingAddress = record.sWorkingAddress,
                                         sACQNumber = record.sACQNumber,
                                         sInitial = record.sInitial,
                                         dPaymentRecdDate = record.dPaymentRecdDate.HasValue? record.dPaymentRecdDate.Value.ToString("dd-MM-yyyy") : string.Empty,
                                         sTenderNumber = record.sTenderNumber,
                                         sOrderNumber = record.sOrderNumber,
                                         dIssueDate = record.dIssueDate.HasValue? record.dIssueDate.Value.ToString("dd-MM-yyyy") : string.Empty,
                                         dConfirmDate = record.dConfirmDate.HasValue ? record.dConfirmDate.Value.ToString("dd-MM-yyyy") : string.Empty,
                                         sRemark = record.sRemark,
                                         sSubConName = record.sSubConName,
                                         sSubConName2 = record.sSubConName2,
                                         sSubConName3 = record.sSubConName3,
                                         nSubConEstimation = record.nSubConEstimation,
                                         nSubConEstimation2 = record.nSubConEstimation2,
                                         nSubConEstimation3 = record.nSubConEstimation3,
                                         nMaterialEstimation = record.nMaterialEstimation,
                                         nMaterialEstimation2 = record.nMaterialEstimation2,
                                         nMaterialEstimation3 = record.nMaterialEstimation3,
                                         sSupplierName = record.sSupplierName,
                                         sSupplierName2 = record.sSupplierName2,
                                         sSupplierName3 = record.sSupplierName3,
                                         nSupplierEstimation = record.nSupplierEstimation,
                                         nSupplierEstimation2 = record.nSupplierEstimation2,
                                         nSupplierEstimation3 = record.nSupplierEstimation3,
                                         nSupervision = record.nSupervision,
                                         bIsDirectLabour = record.bIsDirectLabour,
                                         nDirectLabourCost = record.nDirectLabourCost,
                                         nMinorWork = record.nMinorWork,
                                         sMinorWorkExtension = record.sMinorWorkExtension,
                                         nTotalEstCost = record.nTotalEstCost,
                                         nGP = record.nGP,
                                         nQuotationTotalPrice = record.nTotalEstCost + (record.nGP ?? 0),
                                         sInvoiceNumber = record.sInvoiceNumber,
                                         dInvoiceDate = record.dInvoiceDate.HasValue ? record.dInvoiceDate.Value.ToString("dd-MM-yyyy") : string.Empty,
                                         dBillingDate = record.dBillingDate.HasValue ? record.dBillingDate.Value.ToString("dd-MM-yyyy") : string.Empty,
                                         bIsDummy = record.bIsDummy,
                                         dDummyDueDate = record.dDummyDueDate.HasValue ? record.dDummyDueDate.Value.ToString("dd-MM-yyyy") : string.Empty
                                     };

                }

            return quotation_info;
        }