/// <summary>
 /// Sets the value of the specified custom field.
 /// </summary>
 /// <param name="fieldId">The field id.</param>
 /// <param name="value">The value.</param>
 public CustomFieldRef SetValue(string fieldId, ListOrRecordRef value)
 {
     return(SetValue(fieldId, value, () => new SelectCustomFieldRef()
     {
         internalId = fieldId,
         value = value,
     }));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Sets the value of the specified custom field.
 /// </summary>
 /// <param name="fieldId">The field id.</param>
 /// <param name="value">The value.</param>
 public SearchColumnCustomField SetValue(string fieldId, ListOrRecordRef value)
 {
     return(SetValue(fieldId, value, () => new SearchColumnSelectCustomField()
     {
         internalId = fieldId,
         searchValue = value,
     }));
 }
Ejemplo n.º 3
0
        /*
            Creates the new Lead/Customer record in NetSuite.  Must be called first because we'll need the resulting Customer ID
            to place in the new Contact record, and both ids to run the 'attach' operation.  These are NetSuite's rules, sadly.
        */
        public void createCustomer()
        {
            Customer customer = new Customer();

            // Populate information provided by the user
            customer.companyName = _companyName;
            customer.email = _email;
            customer.phone = _phone;

            // Set as Lead - New
            RecordRef status = new RecordRef();
            status.internalId = "6"; // LEAD - New
            customer.entityStatus = status;

            /*
                Custom Fields require creating a reference record based on the type of field, and then we
                add each reference object to an array that gets passed into the XML renderer that NetSuite
                provides for us.

                Industry, IP address and Source are custom fields we've created.  Industry is a drop down list, which
                means our reference object must contain a reference to which drop down entry the new lead selected.
                This seems a bit overboard but lists can be multi-selects as well.  IP Address and source are just simple
                text fields, so nothing fancy is required, they're just a simple name/value pair.
            */

            System.Collections.ArrayList customFields = new System.Collections.ArrayList();

            if (_industry != "") {
                SelectCustomFieldRef custIndustry = new SelectCustomFieldRef();
                custIndustry.internalId = "custentity_customerindustry";
                ListOrRecordRef custIndustryList = new ListOrRecordRef();
                custIndustryList.internalId = _industry;
                custIndustry.value = custIndustryList;
                customFields.Add(custIndustry);
            }

            if (_ip != "") {
                StringCustomFieldRef ip = new StringCustomFieldRef();
                ip.internalId = "custentity_fnaipaddress";
                ip.value = _ip;
                customFields.Add(ip);
            }

            if (_source != "") {
                StringCustomFieldRef source = new StringCustomFieldRef();
                source.internalId = "custentity_custfnasource";
                source.value = _source;
                customFields.Add(source);
            }

            CustomFieldRef[] customFieldRefs = new CustomFieldRef[customFields.Count];
            IEnumerator ienum = customFields.GetEnumerator();
            for (int i = 0; ienum.MoveNext(); i++) {
                customFieldRefs[i] = (CustomFieldRef)ienum.Current;
            }
            customer.customFieldList = customFieldRefs;

            /*
                Now with our customer record created, we're going to add it to our XML renderer and pass it on NetSuite.
                NetSuite will be returning a response in XML so we're going to grab that so we can get the id of the newly
                created record in NetSuite.
            */
            WriteResponse response = _service.add(customer);
            _customerStatus = response.status;
            if (this.getCustomerStatus() == "") {
                _customerID = ((RecordRef)response.baseRef).internalId;
                _entityName = ((RecordRef)response.baseRef).name;
            }
        }
        public override Int64 Set(string parametersArr)
        {
            try
            {
                //Set Values of Netsuite Customer ID
                //  LogDAO.Integration_Exception(LogIntegrationType.Info, TaskRunType.POST, "InvoiceTask", "Start");

                //new GenericeDAO<Foodics.NetSuite.Shared.Model.Invoice.Integrate>().GetAndUpdateCustomerIDandCreditMemo();
                //get recentrly added invoices after creating the return
                List <Foodics.NetSuite.Shared.Model.Invoice> invoiceLst = new GenericeDAO <Foodics.NetSuite.Shared.Model.Invoice>().GetWhere(" Netsuite_Id IS NULL or Netsuite_Id =0");
                Setting objSetting = new GenericeDAO <Setting>().GetAll().FirstOrDefault();
                // = new NetsuiteDAO().SelectInvoicesForIntegration();
                //  LogDAO.Integration_Exception(LogIntegrationType.Info, TaskRunType.POST, "InvoiceTask", "Count: " + invoiceLst.Count.ToString());
                bool result = true;
                if (invoiceLst.Count > 0)
                {
                    #region variables
                    Invoice[] InvoiceArr = new Invoice[invoiceLst.Count];

                    List <Foodics.NetSuite.Shared.Model.InvoiceItem> itemLst;
                    List <Foodics.NetSuite.Shared.Model.GiftCertificate.Integrate> giftCertificateLst;
                    //List<Foodics.NetSuite.Shared.Model.ItemLotSerial.Sales> lotSerialLst;

                    Foodics.NetSuite.Shared.Model.Invoice     invoice_info;
                    Foodics.NetSuite.Shared.Model.InvoiceItem itemDetails;

                    GiftCertRedemption[] giftRedeem;
                    InvoiceItem[]        invoiceItems;

                    DateTime                invoice_date;
                    Invoice                 invoiceObject;
                    InvoiceItem             invoiceItemObject;
                    InvoiceItemList         items;
                    InventoryAssignmentList InventoryAssignmentlst;
                    InventoryAssignment[]   assignList;
                    InventoryAssignment     assign;
                    InventoryDetail         invDetails;

                    GiftCertRedemptionList redemptionLst;

                    DateCustomFieldRef   trans_time;
                    ListOrRecordRef      emirate_region, custSelectValue, custSelectCashier;
                    SelectCustomFieldRef emirate_ref = null, terminal, cashier;
                    RecordRef            authCodeRef, taxCode, item, unit, price, itmSerial, discItem,
                                         subsid, currency, entity, location, classification, department;
                    DoubleCustomFieldRef line_total, line_discount_amount, balance, paid, lineDiscount;
                    StringCustomFieldRef trans_no, pos_id, orderDiscount;

                    CustomFieldRef[] customFieldRefArr, customFieldRefArray;

                    int item_custom_cols = 2;
                    #endregion

                    for (int i = 0; i < invoiceLst.Count; i++)
                    {
                        try
                        {
                            invoice_info = invoiceLst[i];
                            //Netsuite invoice type
                            invoiceObject = new Invoice();

                            //get invoice items
                            itemLst = new GenericeDAO <Foodics.NetSuite.Shared.Model.InvoiceItem>().GetWhere("Invoice_Id =" + invoice_info.Id);//new NetsuiteDAO().SelectInvoicItems(invoice_info.Id);

                            //get invoice gift certificates
                            #region gift certificate redemption
                            //try
                            //{
                            //    giftCertificateLst = new NetsuiteDAO().SelectEntityGiftCertificates(invoice_info.Id, 1);
                            //    if (giftCertificateLst.Count > 0)
                            //    {
                            //        redemptionLst = new GiftCertRedemptionList();
                            //        giftRedeem = new GiftCertRedemption[giftCertificateLst.Count];
                            //        for (int r = 0; r < giftCertificateLst.Count; r++)
                            //        {
                            //            GiftCertRedemption gift = new GiftCertRedemption();
                            //            authCodeRef = new RecordRef();
                            //            authCodeRef.internalId = giftCertificateLst[r].Netsuite_Id.ToString();
                            //            authCodeRef.type = RecordType.giftCertificate;
                            //            gift.authCode = authCodeRef;
                            //            gift.authCodeAppliedSpecified = true;
                            //            gift.authCodeApplied = Convert.ToDouble(giftCertificateLst[r].Amount);
                            //            giftRedeem[r] = gift;
                            //        }

                            //        redemptionLst.giftCertRedemption = giftRedeem;
                            //        invoiceObject.giftCertRedemptionList = redemptionLst;
                            //    }
                            //}
                            //catch (Exception ex)
                            //{
                            //    //LogDAO.Integration_Exception(LogIntegrationType.Error, TaskRunType.POST, "InvoiceTask", "Error adding GiftCertificates: " + ex.Message + " Invoice id = " + invoiceLst[i].Id.ToString());
                            //}
                            #endregion

                            #region invoice items

                            //Define Invoice Items List
                            invoiceItems = new InvoiceItem[itemLst.Count];
                            try
                            {
                                item_custom_cols = 2;
                                for (int k = 0; k < itemLst.Count; k++)
                                {
                                    itemDetails       = itemLst[k];
                                    invoiceItemObject = new InvoiceItem();

                                    // TAX
                                    //invoiceItemObject.taxRate1Specified = true;
                                    //invoiceItemObject.taxRate1 = itemDetails.Tax_Rate;
                                    //invoiceItemObject.taxAmount = itemDetails.Tax_Amt;

                                    // tax code
                                    taxCode                   = new RecordRef();
                                    taxCode.internalId        = objSetting.TaxCode_Netsuite_Id.ToString(); //"15";//itemDetails.Tax_Code.ToString();
                                    taxCode.type              = RecordType.taxAcct;
                                    invoiceItemObject.taxCode = taxCode;

                                    // item
                                    item            = new RecordRef();
                                    item.internalId = itemDetails.Item_Id.ToString();
                                    item.type       = (RecordType)Enum.Parse(typeof(RecordType), itemDetails.Item_Type, true);
                                    //item.type = (RecordType)Enum.Parse(typeof(RecordType), "InventoryItem", true);
                                    invoiceItemObject.item = item;

                                    if (Utility.ConvertToInt(itemDetails.Units) > 0)
                                    {
                                        unit                    = new RecordRef();
                                        unit.internalId         = itemDetails.Units.ToString();
                                        unit.type               = RecordType.unitsType;
                                        invoiceItemObject.units = unit;
                                    }

                                    // price level
                                    #region price level
                                    price      = new RecordRef();
                                    price.type = RecordType.priceLevel;

                                    if (itemDetails.Amount > 0)
                                    {
                                        //if (itemDetails.Customer_Price_Level > 0)
                                        //{
                                        //    // customer price level
                                        //    price.internalId = itemDetails.Customer_Price_Level.ToString();
                                        //    invoiceItemObject.price = price;
                                        //}
                                        //else if (itemDetails.Price_Level_Id > 0)
                                        //{
                                        //    // default price level
                                        //    price.internalId = itemDetails.Price_Level_Id.ToString();
                                        //    invoiceItemObject.price = price;
                                        //}
                                        //else
                                        //{
                                        // amount
                                        invoiceItemObject.amountSpecified = true;
                                        invoiceItemObject.amount          = itemDetails.Amount;
                                        //}
                                    }

                                    #endregion

                                    // invoiceItemObject.costEstimateType = (ItemCostEstimateType)itemDetails.Cost_Estimate_Type;


                                    //if (itemDetails.Item_Type == "GiftCertificateItem")
                                    //{
                                    //    #region sell gift certificate
                                    //    try
                                    //    {
                                    //        invoiceItemObject.giftCertNumber = itemDetails.Gift_Code;
                                    //        invoiceItemObject.giftCertFrom = itemDetails.Sender;
                                    //        invoiceItemObject.giftCertMessage = itemDetails.Gift_Message;
                                    //        invoiceItemObject.giftCertRecipientName = itemDetails.Recipient_Name;
                                    //        invoiceItemObject.giftCertRecipientEmail = itemDetails.Recipient_Email;
                                    //    }
                                    //    catch { }
                                    //    #endregion
                                    //}
                                    //else
                                    //{
                                    // quantity
                                    invoiceItemObject.quantitySpecified = true;
                                    invoiceItemObject.quantity          = itemDetails.Quantity;

                                    #region serials/Lot
                                    //if (Utility.ItemTypeSerialized.Contains(itemDetails.Item_Type))
                                    //{
                                    //    lotSerialLst = new NetsuiteDAO().SelectInvoiceLotSerials(itemDetails.Id);
                                    //    InventoryAssignmentlst = new InventoryAssignmentList();
                                    //    assignList = new InventoryAssignment[lotSerialLst.Count];

                                    //    for (int z = 0; z < lotSerialLst.Count; z++)
                                    //    {
                                    //        assign = new InventoryAssignment();

                                    //        itmSerial = new RecordRef();
                                    //        itmSerial.internalId = lotSerialLst[z].Netsuite_Id.ToString();
                                    //        itmSerial.type = RecordType.lotNumberedInventoryItem;
                                    //        itmSerial.typeSpecified = true;

                                    //        if (Utility.ItemTypeLot.Contains(itemDetails.Item_Type))
                                    //        {
                                    //            assign.quantitySpecified = true;
                                    //            assign.quantity = lotSerialLst[z].Quantity;
                                    //        }
                                    //        assign.issueInventoryNumber = itmSerial;

                                    //        assignList[z] = assign;
                                    //    }

                                    //    InventoryAssignmentlst.inventoryAssignment = assignList;
                                    //    invDetails = new InventoryDetail();
                                    //    invDetails.inventoryAssignmentList = InventoryAssignmentlst;

                                    //    invoiceItemObject.inventoryDetail = invDetails;
                                    //}

                                    #endregion
                                    //  }

                                    #region custom fields

                                    try
                                    {
                                        #region line-item discount

                                        //line_total = new DoubleCustomFieldRef();
                                        //line_total.scriptId = "custcol_da_pos_line_item_total";
                                        //line_total.value = Math.Round(itemDetails.Total_Line_Amount, 3);

                                        //line_discount_amount = new DoubleCustomFieldRef();
                                        //line_discount_amount.scriptId = "custcol_da_pos_line_item_discount";
                                        //line_discount_amount.value = Math.Round(itemDetails.Line_Discount_Amount, 3);

                                        // line-item amount
                                        invoiceItemObject.amount = (itemDetails.Quantity * itemDetails.Amount);

                                        #endregion

                                        #region Transaction Region (Emirate)
                                        //item_custom_cols = 2;
                                        //if (invoice_info.Transaction_Region > 0)
                                        //{
                                        //    item_custom_cols = 3;
                                        //    emirate_region = new ListOrRecordRef();
                                        //    emirate_region.internalId = invoice_info.Transaction_Region.ToString();

                                        //    emirate_ref = new SelectCustomFieldRef();
                                        //    emirate_ref.scriptId = "custcol_emirate";
                                        //    emirate_ref.value = emirate_region;
                                        //}
                                        #endregion

                                        //customFieldRefArr = new CustomFieldRef[item_custom_cols];
                                        //customFieldRefArr[0] = line_total;
                                        //customFieldRefArr[1] = line_discount_amount;

                                        //if (item_custom_cols == 3)
                                        //    customFieldRefArr[2] = emirate_ref;

                                        //invoiceItemObject.customFieldList = customFieldRefArr;
                                    }
                                    catch (Exception ex)
                                    {
                                        //  LogDAO.Integration_Exception(LogIntegrationType.Error, TaskRunType.POST, "InvoiceTask", "Error adding customField: " + ex.Message + " Invoice id = " + invoiceLst[i].Id.ToString());
                                    }
                                    #endregion

                                    invoiceItems[k] = invoiceItemObject;
                                }
                            }
                            catch (Exception ex)
                            {
                                //  LogDAO.Integration_Exception(LogIntegrationType.Error, TaskRunType.POST, "InvoiceTask", "Error adding invoice: " + ex.Message + " Invoice id = " + invoiceLst[i].Id.ToString());
                            }
                            //Assign invoive items
                            items                  = new InvoiceItemList();
                            items.item             = invoiceItems;
                            invoiceObject.itemList = items;

                            //GiftCertRedemption
                            #endregion

                            #region Invoice Values

                            #region Standard Attributes
                            invoice_date = TimeZoneInfo.ConvertTimeToUtc(invoice_info.Date, TimeZoneInfo.Local);

                            invoiceObject.tranDateSpecified = true;
                            invoiceObject.dueDateSpecified  = true;
                            invoiceObject.tranDate          = invoice_date;
                            invoiceObject.dueDate           = invoice_date;
                            invoiceObject.exchangeRate      = invoice_info.Exchange_Rate;

                            invoiceObject.memo = invoice_info.Notes;

                            if (invoice_info.Subsidiary_Id > 0)
                            {
                                subsid                   = new RecordRef();
                                subsid.internalId        = objSetting.Subsidiary_Netsuite_Id.ToString();
                                subsid.type              = RecordType.subsidiary;
                                invoiceObject.subsidiary = subsid;
                            }

                            //RecordRef customForm = new RecordRef();
                            //customForm.internalId = "92";
                            //invoiceObject.customForm = customForm;

                            currency               = new RecordRef();
                            currency.internalId    = objSetting.Currency_Netsuite_Id.ToString();
                            currency.type          = RecordType.currency;
                            invoiceObject.currency = currency;

                            entity = new RecordRef();

                            entity.internalId    = invoice_info.Customer_Netsuite_Id > 0? invoice_info.Customer_Netsuite_Id.ToString(): objSetting.Customer_Netsuite_Id.ToString();
                            entity.type          = RecordType.customer;
                            invoiceObject.entity = entity;

                            location               = new RecordRef();
                            location.internalId    = invoice_info.Location_Id.ToString(); //objSetting.Location_Netsuite_Id.ToString();
                            location.type          = RecordType.location;
                            invoiceObject.location = location;

                            // department
                            //if (invoice_info.Department_Id > 0)
                            //{
                            //    department = new RecordRef();
                            //    department.internalId = invoice_info.Department_Id.ToString();
                            //    department.type = RecordType.department;
                            //    invoiceObject.department = department;
                            //}

                            //// class
                            //if (invoice_info.Class_Id > 0)
                            //{
                            //    classification = new RecordRef();
                            //    classification.internalId = invoice_info.Class_Id.ToString();
                            //    classification.type = RecordType.classification;
                            //    invoiceObject.@class = classification;
                            //}
                            // sales rep
                            if (invoice_info.Sales_Rep_Id > 0)
                            {
                                RecordRef sales_rep = new RecordRef();
                                sales_rep.internalId   = invoice_info.Sales_Rep_Id.ToString();
                                sales_rep.type         = RecordType.employee;
                                invoiceObject.salesRep = sales_rep;
                            }
                            #endregion

                            #region Invoice Custom Attributes

                            trans_no          = new StringCustomFieldRef();
                            trans_no.scriptId = "custbody_da_pos_trans_no";
                            trans_no.value    = invoice_info.BarCode.ToString();

                            pos_id          = new StringCustomFieldRef();
                            pos_id.scriptId = "custbody_da_pos_id";
                            pos_id.value    = invoice_info.Id.ToString();

                            custSelectValue = new ListOrRecordRef();

                            terminal = new SelectCustomFieldRef();
                            custSelectValue.internalId = invoice_info.Terminal_Id.ToString();
                            terminal.scriptId          = "custbody_da_terminal";
                            terminal.value             = custSelectValue;

                            custSelectCashier = new ListOrRecordRef();

                            cashier = new SelectCustomFieldRef();
                            custSelectCashier.internalId = invoice_info.Cashier.ToString();
                            cashier.scriptId             = "custbody_da_cashier";
                            cashier.value = custSelectCashier;

                            balance          = new DoubleCustomFieldRef();
                            balance.scriptId = "custbody_da_balance";
                            balance.value    = Math.Round(invoice_info.Balance, 3);


                            paid          = new DoubleCustomFieldRef();
                            paid.scriptId = "custbody_da_paid";
                            paid.value    = Math.Round(invoice_info.Paid, 3);

                            trans_time          = new DateCustomFieldRef();
                            trans_time.scriptId = "custbody_da_pos_trans_time";
                            trans_time.value    = invoice_date;

                            #region Discount

                            invoiceObject.discountRate = (Math.Round(invoice_info.Total_Discount, 3) * -1).ToString();

                            lineDiscount          = new DoubleCustomFieldRef();
                            lineDiscount.scriptId = "custbody_da_pos_line_item_discount";
                            lineDiscount.value    = Math.Round(invoice_info.Line_Discount_Amount, 3) * -1;


                            orderDiscount          = new StringCustomFieldRef();
                            orderDiscount.scriptId = "custbody_da_pos_order_discount";
                            orderDiscount.value    = (Math.Round(invoice_info.Invoice_Discount_Rate, 3) * -1).ToString();
                            if (invoice_info.Invoice_Discount_Type == 1)
                            {
                                orderDiscount.value = (Math.Round(invoice_info.Invoice_Discount_Rate, 3) * -1).ToString() + "%";
                            }

                            if (invoice_info.Accounting_Discount_Item != 0)
                            {
                                discItem                   = new RecordRef();
                                discItem.internalId        = invoice_info.Accounting_Discount_Item.ToString();
                                discItem.type              = RecordType.discountItem;
                                invoiceObject.discountItem = discItem;
                            }
                            #endregion

                            int length = 7;
                            if (invoice_info.Terminal_Id > 0)
                            {
                                length = 8;
                            }

                            customFieldRefArray    = new CustomFieldRef[length];
                            customFieldRefArray[0] = cashier;
                            customFieldRefArray[1] = balance;
                            customFieldRefArray[2] = paid;
                            customFieldRefArray[3] = trans_time;
                            customFieldRefArray[4] = trans_no;
                            customFieldRefArray[5] = lineDiscount;
                            customFieldRefArray[6] = orderDiscount;

                            if (invoice_info.Terminal_Id > 0)
                            {
                                customFieldRefArray[7] = terminal;
                            }

                            invoiceObject.customFieldList = customFieldRefArray;
                            #endregion

                            InvoiceArr[i] = invoiceObject;
                            #endregion
                        }
                        catch (Exception ex)
                        {
                            invoiceLst.RemoveAt(i);
                            //  LogDAO.Integration_Exception(LogIntegrationType.Error, TaskRunType.POST, "InvoiceTask", "Error adding invoice: " + ex.Message + " Invoice id = " + invoiceLst[i].Id.ToString());
                        }
                    }
                    // Send invoice list to netsuite
                    WriteResponseList wr = Service(true).addList(InvoiceArr);
                    result = wr.status.isSuccess;

                    // LogDAO.Integration_Exception(LogIntegrationType.Info, TaskRunType.POST, "InvoiceTask", "Status: " + result);
                    if (result)
                    {
                        //Update database with returned Netsuite ids
                        UpdatedInvoice(invoiceLst, wr);
                    }
                }

                // post customerPayment to netsuite
                //  bool postPayments = PostCustomerPayment();
            }
            catch (Exception ex)
            {
                //  LogDAO.Integration_Exception(LogIntegrationType.Error, TaskRunType.POST, "InvoiceTask Error", "Error " + ex.Message);
            }

            // release the the object
            //new ScheduleTaskDAO().Processing(taskType, TaskRunType.POST, false);

            return(0);
        }