public ActionResult DuplicateQuotation(int? id, int customerID)
        {
            //# validation
            if (!id.HasValue)
                return Json(new { bIsSuccessful = false });

            bool isSuccessful = false;
            Member member = new Member("users");
            int newQuotationID = 0;
            string customerCode = "";

            try
            {
                //# get existing quotation
                recsys_quotation existingQuotation = this._db.recsys_quotation.FirstOrDefault(theQuotation => theQuotation.id == id.Value);

                if (existingQuotation != null)
                {
                    //# get customer data for the newly duplicated quotation
                    recsys_customers existingQuotationCustomerData = this._db.recsys_customers.FirstOrDefault(theCustomerData => theCustomerData.id == customerID);

                    if (existingQuotationCustomerData != null)
                    {
                        //# get existing quotation items
                        IEnumerable<recsys_quotation_items> existingQuotationItems = from r in this._db.recsys_relate
                                                                                     join qi in this._db.recsys_quotation_items on r.id2 equals qi.id
                                                                                     where r.id1 == id.Value
                                                                                     && r.table1 == "quotation"
                                                                                     && r.table2 == "quotation_items"
                                                                                     select qi;

                        //# construct new customer data
                        recsys_relate_customers newQuotationCustomerData = new recsys_relate_customers
                        {
                            address1 = existingQuotationCustomerData.address1,
                            address2 = existingQuotationCustomerData.address2,
                            center = existingQuotationCustomerData.center,
                            code = existingQuotationCustomerData.code,
                            contact = existingQuotationCustomerData.contact,
                            customer_code = existingQuotationCustomerData.customer_code,
                            customer_id = existingQuotationCustomerData.id,
                            district1 = existingQuotationCustomerData.district1,
                            district2 = existingQuotationCustomerData.district2,
                            email = existingQuotationCustomerData.email,
                            fax1 = existingQuotationCustomerData.fax1,
                            fax2 = existingQuotationCustomerData.fax2,
                            group_id = existingQuotationCustomerData.group_id,
                            name = existingQuotationCustomerData.name,
                            chi_name = existingQuotationCustomerData.chi_name,
                            prefix = existingQuotationCustomerData.prefix,
                            relate_type = 3,
                            remark = existingQuotationCustomerData.remark,
                            reference_number = existingQuotationCustomerData.reference_number,
                            tel1 = existingQuotationCustomerData.tel1,
                            tel2 = existingQuotationCustomerData.tel2,
                            title = existingQuotationCustomerData.title,
                            type = existingQuotationCustomerData.type
                        };
                        customerCode = newQuotationCustomerData.customer_code;

                        //# construct new quotation items
                        recsys_quotation_items[] newQuotationItems = existingQuotationItems.Select(theItem => QuotationsController.CopyQuotationItem(theItem)).ToArray();

                        this._db.recsys_relate_customers.AddObject(newQuotationCustomerData);
                        foreach (recsys_quotation_items item in newQuotationItems)
                        {
                            item.last_update = DateTime.Now;
                            item.update_user_id = (int)member.infoBySession("id");
                            this._db.recsys_quotation_items.AddObject(item);
                        }

                        //# save customer data
                        this._db.SaveChanges();

                        //# load new customer data
                        this._db.Refresh(RefreshMode.StoreWins, newQuotationCustomerData);

                        //# load new items
                        this._db.Refresh(RefreshMode.StoreWins, newQuotationItems);

                        if (newQuotationCustomerData.id > 0)
                        {

                            //# construct new quotation
                            recsys_quotation newQuotation = new recsys_quotation();

                            newQuotation.create_date = DateTime.Now;
                            newQuotation.customer_id = newQuotationCustomerData.id;
                            newQuotation.last_update = DateTime.Now;
                            newQuotation.update_user_id = (int)member.infoBySession("id");
                            newQuotation.status = 1;
                            newQuotation.issue_date = DateTime.Now.Date;
                            if (existingQuotation.lang != 0)
                                newQuotation.lang = existingQuotation.lang;
                            else newQuotation.lang = 1; //default chinese

                            this._db.recsys_quotation.AddObject(newQuotation);

                            //# save quotation
                            this._db.SaveChanges();

                            //# load new quotation
                            this._db.Refresh(RefreshMode.StoreWins, newQuotation);

                            newQuotationID = newQuotation.id;

                            if (newQuotation.id > 0 && newQuotationItems.Count(theItem => theItem.id > 0) > 0)
                            {
                                //# construct relations
                                IEnumerable<recsys_relate> relations = newQuotationItems.Where(theItem => theItem.id > 0)
                                    .Select(theItem => new recsys_relate()
                                    {
                                        id1 = newQuotation.id,
                                        id2 = theItem.id,
                                        table1 = "quotation",
                                        table2 = "quotation_items"
                                    });

                                foreach (recsys_relate relation in relations)
                                    this._db.recsys_relate.AddObject(relation);

                                //# save relations
                                this._db.SaveChanges();

                                isSuccessful = true;

                            }
                            else
                                isSuccessful = true;

                        }

                    }

                }
                //# duplicate quotation
                //# save changes
            }
            catch (OptimisticConcurrencyException)
            {
                //# log down
            }

            return Json(new { bIsSuccessful = true, iNewID = newQuotationID, code = customerCode });
        }
        public ActionResult CloneQuotation(int? id)
        {
            //# validation
            if (!id.HasValue)
                return Json(new { bIsSuccessful = false });

            bool isSuccessful = false;
            Member member = new Member("users");
            int newQuotationID = 0;

            try
            {
                //# get existing quotation
                recsys_quotation existingQuotation = this._db.recsys_quotation.FirstOrDefault(theQuotation => theQuotation.id == id.Value);
                if (existingQuotation != null)
                {
                    //# get existing quotation customer data
                    recsys_relate_customers existingQuotationCustomerData = this._db.recsys_relate_customers.FirstOrDefault(theCustomerData => theCustomerData.id == existingQuotation.customer_id);

                    if (existingQuotationCustomerData != null)
                    {
                        //# get existing quotation items
                        IEnumerable<recsys_quotation_items> existingQuotationItems = from r in this._db.recsys_relate
                                                                                     join qi in this._db.recsys_quotation_items on r.id2 equals qi.id
                                                                                     where r.id1 == id.Value
                                                                                     && r.table1 == "quotation"
                                                                                     && r.table2 == "quotation_items"
                                                                                     select qi;

                        //# construct new customer data
                        recsys_relate_customers newQuotationCustomerData = CustomersController.CopyCustomerData(existingQuotationCustomerData);

                        //# construct new quotation items
                        recsys_quotation_items[] newQuotationItems = existingQuotationItems.Select(theItem => QuotationsController.CopyQuotationItem(theItem)).ToArray();

                        this._db.recsys_relate_customers.AddObject(newQuotationCustomerData);
                        foreach (recsys_quotation_items item in newQuotationItems)
                        {
                            item.last_update = DateTime.Now;
                            item.update_user_id = (int)member.infoBySession("id");
                            this._db.recsys_quotation_items.AddObject(item);
                        }

                        //# save customer data
                        this._db.SaveChanges();

                        //# load new customer data
                        this._db.Refresh(RefreshMode.StoreWins, newQuotationCustomerData);

                        //# load new items
                        this._db.Refresh(RefreshMode.StoreWins, newQuotationItems);

                        if (newQuotationCustomerData.id > 0)
                        {

                            //# construct new quotation
                            recsys_quotation newQuotation = new recsys_quotation();

                            newQuotation.create_date = DateTime.Now;
                            newQuotation.customer_id = newQuotationCustomerData.id;
                            newQuotation.last_update = DateTime.Now;
                            newQuotation.update_user_id = (int)member.infoBySession("id");
                            newQuotation.status = 1;
                            newQuotation.issue_date = DateTime.Now.Date;
                            if (existingQuotation.lang != 0)
                                newQuotation.lang = existingQuotation.lang;
                            else newQuotation.lang = 1; //default chinese

                            this._db.recsys_quotation.AddObject(newQuotation);

                            //# save quotation
                            this._db.SaveChanges();

                            //# load new quotation
                            this._db.Refresh(RefreshMode.StoreWins, newQuotation);

                            newQuotationID = newQuotation.id;

                            if (newQuotation.id > 0 && newQuotationItems.Count(theItem => theItem.id > 0) > 0)
                            {
                                //# construct relations
                                IEnumerable<recsys_relate> relations = newQuotationItems.Where(theItem => theItem.id > 0)
                                    .Select(theItem => new recsys_relate()
                                    {
                                        id1 = newQuotation.id,
                                        id2 = theItem.id,
                                        table1 = "quotation",
                                        table2 = "quotation_items"
                                    });

                                foreach (recsys_relate relation in relations)
                                    this._db.recsys_relate.AddObject(relation);

                                //# save relations
                                this._db.SaveChanges();

                                isSuccessful = true;

                            }
                            else
                                isSuccessful = true;

                        }

                    }

                }

                //# clone quotation
                //# save changes
            }
            catch (OptimisticConcurrencyException)
            {
                //# log down
            }

            return Json(new { bIsSuccessful = isSuccessful, iNewID = newQuotationID });
        }
        public ActionResult AddSave(QuotationRecord data)
        {
            Member member;
            recsys_customers masterCustomer;
            recsys_relate_customers customerData;
            recsys_quotation quotation;

            //# validation
            if (!String.IsNullOrEmpty(data.invoice) && data.dummy)
            {
                data.SaveResult.WarningMessage = "請刪除Invoice No或取消選取'有Dummy Invoice'";
                data.SaveResult.SavedSuccessfully = false;
                return Json(data);
            }

            if (!String.IsNullOrEmpty(data.minor_work))
            {
                if (data.minor_work.Length > 3)
                {
                    data.SaveResult.WarningMessage = "請修改Minor Work至3字以內";
                    data.SaveResult.SavedSuccessfully = false;
                    return Json(data);
                }
            }

            //# validation
            if (data.MasterCustomerID < 0)
                throw new SystemException("Invalid Entity ID");

            //# get old master customer record
            masterCustomer = this._db.recsys_customers.FirstOrDefault(theCustomer => theCustomer.id == data.MasterCustomerID);

            //# validation
            if (masterCustomer == null)
                throw new RecordNotFoundException();

            member = new Member("users");

            bool createNewDistrict = false;
            if (!string.IsNullOrEmpty(data.WorkingDistrictName))
            {
                recsys_district workingDistrict = this._db.recsys_district.Where(x => x.name.Trim().ToUpper().Equals(data.WorkingDistrictName.Trim().ToUpper())).SingleOrDefault();
                if (workingDistrict != null)
                {
                    data.district2 = workingDistrict.id;
                    data.group_id = workingDistrict.group_id;
                }
                else
                    createNewDistrict = true;
            }

            if (createNewDistrict)
            {
                recsys_district addNewDistrict = new recsys_district()
                {
                    code = data.WorkingDistrictName.Trim(),
                    name = data.WorkingDistrictName.Trim(),
                    group_id = this._db.recsys_group.Where(x => x.name.Equals("Unknown Group For System Migration")).SingleOrDefault().id,
                    region = 4,
                    status = 1,
                    last_update = DateTime.Now,
                    update_user_id = (int)member.infoBySession("id")
                };

                this._db.recsys_district.AddObject(addNewDistrict);
                this._db.SaveChanges();

                data.district2 = this._db.recsys_district.Where(x => x.name.Equals(data.WorkingDistrictName.Trim())).SingleOrDefault().id;
                data.group_id = this._db.recsys_district.Where(x => x.name.Equals(data.WorkingDistrictName.Trim())).SingleOrDefault().group_id;
            }

            createNewDistrict = false;
            if (!string.IsNullOrEmpty(data.MailingDistrictName))
            {
                recsys_district mailingDistrict = this._db.recsys_district.Where(x => x.name.Trim().ToUpper().Equals(data.MailingDistrictName.Trim().ToUpper())).SingleOrDefault();
                if (mailingDistrict != null)
                {
                    data.district1 = mailingDistrict.id;
                    data.group_id = mailingDistrict.group_id;
                }
                else
                    createNewDistrict = true;
            }

            if (createNewDistrict)
            {
                recsys_district addNewDistrict = new recsys_district()
                {
                    code = data.MailingDistrictName.Trim(),
                    name = data.MailingDistrictName.Trim(),
                    group_id = this._db.recsys_group.Where(x => x.name.Equals("Unknown Group For System Migration")).SingleOrDefault().id,
                    region = 4,
                    status = 1,
                    last_update = DateTime.Now,
                    update_user_id = (int)member.infoBySession("id")
                };

                this._db.recsys_district.AddObject(addNewDistrict);
                this._db.SaveChanges();

                data.district1 = this._db.recsys_district.Where(x => x.name.Equals(data.MailingDistrictName.Trim())).SingleOrDefault().id;
                data.group_id = this._db.recsys_district.Where(x => x.name.Equals(data.MailingDistrictName.Trim())).SingleOrDefault().group_id;
            }

            //# mapping
            customerData = new recsys_relate_customers()
            {
                address1 = data.address1,
                address2 = data.address2,
                center = masterCustomer.center,
                code = masterCustomer.code,
                contact = data.contact,
                customer_code = masterCustomer.customer_code,
                customer_id = masterCustomer.id,
                district1 = data.district1,
                district2 = data.district2,
                email = data.email,
                fax1 = data.fax1,
                fax2 = data.fax2,
                group_id = data.group_id,
                manual_input_code = masterCustomer.manual_input_code,
                name = data.name,
                chi_name = data.chi_name,
                prefix = masterCustomer.prefix,
                relate_type = (int)Rec_System.Models.Customers.CustomerDataRelateType.Maintenance,
                reference_number = masterCustomer.reference_number,
                remark = data.remark,
                tel1 = data.tel1,
                tel2 = data.tel2,
                title = (byte)data.title,
                type = (byte)masterCustomer.type
            };

            this._db.recsys_relate_customers.AddObject(customerData);

            quotation = new recsys_quotation()
            {
                dummy = (byte)(data.dummy ? 1 : 0),
                dummy_date = DateChecking(data.dummy_date),
                initial = data.initial,
                lang = data.lang,
                order_number = data.order_number,
                number = data.number,
                subcon_estimation = data.subcon_estimation.HasValue ? data.subcon_estimation : 0.00,
                subcon_name = data.subcon_name,
                subcon_estimation2 = data.subcon_estimation2.HasValue ? data.subcon_estimation2 : 0.00,
                subcon_name2 = data.subcon_name2,
                subcon_estimation3 = data.subcon_estimation3.HasValue ? data.subcon_estimation3 : 0.00,
                subcon_name3 = data.subcon_name3,
                supervision = data.supervision.HasValue ? data.supervision : 0.00,
                supplier_name = data.supplier_name,
                supplier_name2 = data.supplier_name2,
                supplier_name3 = data.supplier_name3,
                supplier_estimation = data.supplier_estimation.HasValue ? data.supplier_estimation : 0.00,
                supplier_estimation2 = data.supplier_estimation2.HasValue ? data.supplier_estimation2 : 0.00,
                supplier_estimation3 = data.supplier_estimation3.HasValue ? data.supplier_estimation3 : 0.00,
                supplier_id = data.supplier_id.HasValue ? data.supplier_id : 0.00,
                material_estimation2 = data.material_estimation2.HasValue ? data.material_estimation2 : 0.00,
                material_estimation3 = data.material_estimation3.HasValue ? data.material_estimation3 : 0.00,
                payment_received_date = DateChecking(data.payment_received_date),
                tender_number = data.tender_number,
                direct_labour = data.direct_labour,
                direct_labour_cost = data.direct_labour_cost.HasValue ? data.direct_labour_cost : 0.00,
                billing_date = DateChecking(data.billing_date),
                confirm_date = DateChecking(data.confirm_date),
                create_date = DateTime.Now,
                invoice = data.invoice,
                invoice_date = DateChecking(data.billing_date),
                minor_work = data.minor_work,
                minor_work_currency = data.minor_work_currency.HasValue ? data.minor_work_currency : 0.00,
                gp = data.gp.HasValue ? data.gp : 0.00,
                last_update = DateTime.Now,
                remark = data.remark2,
                status = (byte)data.status,
                update_user_id = (int)member.infoBySession("id"),
                issue_date = DateChecking(data.issue_date)
            };

            this._db.recsys_quotation.AddObject(quotation);

            try
            {
                this._db.SaveChanges();

                this._db.Refresh(System.Data.Objects.RefreshMode.StoreWins, customerData);
                this._db.Refresh(System.Data.Objects.RefreshMode.StoreWins, quotation);

                quotation.customer_id = customerData.id;

                this._db.SaveChanges();

                data.id = quotation.id;

                data.SaveResult.SavedSuccessfully = true;
            }
            catch (OptimisticConcurrencyException)
            {
                //# log down
            }

            return Json(data);
        }
        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
                });
        }
Example #5
0
 /// <summary>
 /// Deprecated Method for adding a new object to the recsys_quotation EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddTorecsys_quotation(recsys_quotation recsys_quotation)
 {
     base.AddObject("recsys_quotation", recsys_quotation);
 }
Example #6
0
 /// <summary>
 /// Create a new recsys_quotation object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 /// <param name="customer_id">Initial value of the customer_id property.</param>
 /// <param name="dummy">Initial value of the dummy property.</param>
 /// <param name="create_date">Initial value of the create_date property.</param>
 /// <param name="status">Initial value of the status property.</param>
 /// <param name="lang">Initial value of the lang property.</param>
 public static recsys_quotation Createrecsys_quotation(global::System.Int32 id, global::System.Int32 customer_id, global::System.Byte dummy, global::System.DateTime create_date, global::System.Byte status, global::System.Byte lang)
 {
     recsys_quotation recsys_quotation = new recsys_quotation();
     recsys_quotation.id = id;
     recsys_quotation.customer_id = customer_id;
     recsys_quotation.dummy = dummy;
     recsys_quotation.create_date = create_date;
     recsys_quotation.status = status;
     recsys_quotation.lang = lang;
     return recsys_quotation;
 }