<commit_before>import declare from 'dojo/_base/declare';
import lang from 'dojo/_base/lang';
import string from 'dojo/string';
import utility from 'argos/Utility';
import Edit from 'argos/Edit';
import getResource from 'argos/I18n';
import PricingAvailabilityService from '../../PricingAvailabilityService';
import validator from 'crm/Validator';
import array from 'dojo/_base/array';
import MODEL_NAMES from '../../Models/Names';
import MODEL_TYPES from 'argos/Models/Types';

const resource = getResource('salesOrderItemEdit');

/**
 * @class crm.Views.QuoteLines.Edit
 *
 * @extends argos.Edit
 *
 * @requires argos.Edit
 *
 */
const __class = declare('crm.Integrations.BOE.Views.SalesOrderItems.Edit', [Edit], {
  // View Properties
  id: 'salesorder_item_edit',
  detailView: 'salesorder_item_detail',
  insertSecurity: 'Entities/SalesOrder/Add',
  updateSecurity: 'Entities/SalesOrder/Edit',
  resourceKind: 'salesOrderItems',
  titleText: resource.titleText,
  productText: resource.productText,
  quantityText: resource.quantityText,
  orderText: resource.orderText,
  priceText: resource.priceText,
  baseAdjustedPriceText: resource.baseAdjustedPriceText,
  docAdjustedPriceText: resource.docAdjustedPriceText,
  baseExtendedPriceText: resource.baseExtendedPriceText,
  docExtendedPriceText: resource.docExtendedPriceText,
  unitOfMeasureText: resource.unitOfMeasureText,
  unitsOfMeasureTitle: resource.unitsOfMeasureTitle,
  docTotalAmountText: resource.docTotalAmountText,
  lineText: resource.lineText,
  warehouseText: resource.warehouseText,
  accountLinked: false,

  init: function init() {
    this.inherited(arguments);
    this.fields.ErpLineNumber.disable();
    if (App.warehouseDiscovery === 'auto') {
      this.fields.SlxLocation.disable();
    }

    this.fields.SalesOrder.disable();
    this.fields.Price.disable();
    this.fields.ExtendedPrice.disable();
    this.fields.DocExtendedPrice.disable();
    this.fields.CalculatedPrice.disable();
    this.fields.DocCalculatedPrice.disable();
    this.fields.DocTotalAmount.disable();
    this.connect(this.fields.Product, 'onChange', this.onProductChange);
    this.connect(this.fields.Quantity, 'onChange', this.onQuantityChange);
  },
  _applyLogicValues: function _applyLogicValues(values) {
    const product = this.fields.Product.getSelection();
    values.ProductName = product.Name;
    values.Family = product.Family;
    values.ActualID = product.ActualId;
    values.CommodityType = product.CommodityType;
  },
  applyContext: function applyContext() {
    this.inherited(arguments);
    if (this.options && this.options.context) {
      if (this.options.context.SalesOrder) {
        this.fields.SalesOrder.setSelection(this.options.context.SalesOrder);
        this.fields.SalesOrder.disable();
        if (this.options.context.SalesOrder.Account) {
          this.accountLinked = this.options.context.SalesOrder.Account.ErpExtId === true;
        }
      } else {
        this.fields.SalesOrder.enable();
      }
    }
    this.setProductDependentFields();
  },
  setProductDependentFields: function setProductDependentFields(product) {
    const dependants = this.getProductDependants();
    if (product) {
      dependants.forEach((f) => {
        this.fields[f].enable();
        this.fields[f].dependsOn = 'Product';
        this.fields[f].where = `Product.Id eq "${product.$key}"`;
      });
    } else {
      dependants.forEach((f) => {
        this.fields[f].disable();
        this.fields[f].dependsOn = null;
        this.fields[f].where = null;
      });
    }
  },
  getProductDependants: function getProductDependants() {
    return ['UnitOfMeasure'];
  },
  onInsert: function onInsert(values) {
    this._applyLogicValues(values);
    this.inherited(arguments);
  },
  onRefresh: function onRefresh() {
    this.inherited(arguments);
    this.setProductDependentFields();
  },
  processEntry: function processEntry(entry) {
    this.setProductDependentFields(entry.Product);
    return entry;
  },
  requestProductPricing: function requestProductPricing(product, quantity, slxLocation, unitOfMeasure) {
    let salesOrder = null;
    if (this.options && this.options.context && this.options.context.SalesOrder) {
      salesOrder = this.options.context.SalesOrder;
    } else {
      if (this.fields.SalesOrder) {
        salesOrder = this.fields.SalesOrder.getValue();
      }
    }
    if (salesOrder && salesOrder.$key && !this.isProcessingPricingRequest) {
      this.isProcessingPricingRequest = true;
      this.enablePricingControls(false);
      PricingAvailabilityService.getOrderItemPricing(this.entry, salesOrder, product, quantity, slxLocation, unitOfMeasure).then(
        (results) => {
          this.onProductPricingSuccess(results);
        }, (error) => {
        this.onProductPricingFailed(error);
      });
    }
  },
  onProductPricingSuccess: function onProductPricingSuccess(result) {
    this.processProductPricing(result).then(() => {
      this.reCalculate();
      this.isProcessingPricingRequest = false;
      this.enablePricingControls(true);
    }, () => {
      this.isProcessingPricingRequest = false;
      this.enablePricingControls(true);
    });
  },
  onProductPricingFailed: function onProductPricingFailed(result) {
    this.isProcessingPricingRequest = false;
    this.enablePricingControls(true);
    this.App.modal.createSimpleDialog({
      title: 'alert',
      content: result.Results,
    });
  },
  setUomFromCode: function setUomFromCode(uomCode) {
    const curremtUom = this.fields.UnitOfMeasure.getValue();
    const product = this.fields.Product.getValue();
    const promise = new Promise((resolve, reject) => {
      if (!this._uomModel) {
        this._uomModel = App.ModelManager.getModel(MODEL_NAMES.UNITOFMEASURE, MODEL_TYPES.SDATA);
      }
      if (this._uomModel && product) {
        if ((curremtUom && (curremtUom.Name !== uomCode)) || (!curremtUom)) {
          this._uomModel.getUnitOfMeasureFromCode(uomCode, product.$key).then((uom) => {
            this.fields.UnitOfMeasure.setValue(uom);
            resolve(true);
          }, (error) => {
            reject(error);
          });
        } else {
          resolve(true);
        }
      } else {
        resolve(true);
      }
    });
    return promise;
  },
  processProductPricing: function processProductPricing(pricingData) {
    const promise = new Promise((resolve, reject) => {
      if (pricingData) {
        if (pricingData.DocCalculatedPrice) {
          this.fields.DocCalculatedPrice.setValue(pricingData.DocCalculatedPrice.value);
        }
        if (pricingData.DocExtendedPrice) {
          this.fields.DocExtendedPrice.setValue(pricingData.DocExtendedPrice.value);
        }
        if (pricingData.DocTotalAmount) {
          this.fields.DocTotalAmount.setValue(pricingData.DocTotalAmount.value);
        }
        if (pricingData.SlxLocationId) {
          this.fields.SlxLocation.setValue({
            $key: pricingData.SlxLocationId.value,
            Description: pricingData.SlxLocationCode.value,
          });
        }
        if (pricingData.UnitOfMeasure) {
          this.setUomFromCode(pricingData.UnitOfMeasure.value).then(() => {
            resolve(true);
          }, (error) => {
            reject(error);
          });
        } else {
          resolve(true);
        }
      } else {
        resolve(true);
      }
    });
    return promise;
  },
  onProductChange: function onProductChange(value, field) {
    this.setProductDependentFields(field.currentSelection);
    this.fields.ProductName.setValue(value.text);
    this.fields.SlxLocation.setValue(null);
    this.requestProductPricing(
      field.currentSelection,
      this.fields.Quantity.getValue(),
      this.fields.SlxLocation.getValue(),
      this.fields.UnitOfMeasure.getValue()<commit_msg>INFORCRM-15235: Warehouse field in Sales Order Items now shows the name instead of ID for the lookup value<commit_after>import declare from 'dojo/_base/declare';
import lang from 'dojo/_base/lang';
import string from 'dojo/string';
import utility from 'argos/Utility';
import Edit from 'argos/Edit';
import getResource from 'argos/I18n';
import PricingAvailabilityService from '../../PricingAvailabilityService';
import validator from 'crm/Validator';
import array from 'dojo/_base/array';
import MODEL_NAMES from '../../Models/Names';
import MODEL_TYPES from 'argos/Models/Types';

const resource = getResource('salesOrderItemEdit');

/**
 * @class crm.Views.QuoteLines.Edit
 *
 * @extends argos.Edit
 *
 * @requires argos.Edit
 *
 */
const __class = declare('crm.Integrations.BOE.Views.SalesOrderItems.Edit', [Edit], {
  // View Properties
  id: 'salesorder_item_edit',
  detailView: 'salesorder_item_detail',
  insertSecurity: 'Entities/SalesOrder/Add',
  updateSecurity: 'Entities/SalesOrder/Edit',
  resourceKind: 'salesOrderItems',
  titleText: resource.titleText,
  productText: resource.productText,
  quantityText: resource.quantityText,
  orderText: resource.orderText,
  priceText: resource.priceText,
  baseAdjustedPriceText: resource.baseAdjustedPriceText,
  docAdjustedPriceText: resource.docAdjustedPriceText,
  baseExtendedPriceText: resource.baseExtendedPriceText,
  docExtendedPriceText: resource.docExtendedPriceText,
  unitOfMeasureText: resource.unitOfMeasureText,
  unitsOfMeasureTitle: resource.unitsOfMeasureTitle,
  docTotalAmountText: resource.docTotalAmountText,
  lineText: resource.lineText,
  warehouseText: resource.warehouseText,
  accountLinked: false,

  init: function init() {
    this.inherited(arguments);
    this.fields.ErpLineNumber.disable();
    if (App.warehouseDiscovery === 'auto') {
      this.fields.SlxLocation.disable();
    }

    this.fields.SalesOrder.disable();
    this.fields.Price.disable();
    this.fields.ExtendedPrice.disable();
    this.fields.DocExtendedPrice.disable();
    this.fields.CalculatedPrice.disable();
    this.fields.DocCalculatedPrice.disable();
    this.fields.DocTotalAmount.disable();
    this.connect(this.fields.Product, 'onChange', this.onProductChange);
    this.connect(this.fields.Quantity, 'onChange', this.onQuantityChange);
  },
  _applyLogicValues: function _applyLogicValues(values) {
    const product = this.fields.Product.getSelection();
    values.ProductName = product.Name;
    values.Family = product.Family;
    values.ActualID = product.ActualId;
    values.CommodityType = product.CommodityType;
  },
  applyContext: function applyContext() {
    this.inherited(arguments);
    if (this.options && this.options.context) {
      if (this.options.context.SalesOrder) {
        this.fields.SalesOrder.setSelection(this.options.context.SalesOrder);
        this.fields.SalesOrder.disable();
        if (this.options.context.SalesOrder.Account) {
          this.accountLinked = this.options.context.SalesOrder.Account.ErpExtId === true;
        }
      } else {
        this.fields.SalesOrder.enable();
      }
    }
    this.setProductDependentFields();
  },
  setProductDependentFields: function setProductDependentFields(product) {
    const dependants = this.getProductDependants();
    if (product) {
      dependants.forEach((f) => {
        this.fields[f].enable();
        this.fields[f].dependsOn = 'Product';
        this.fields[f].where = `Product.Id eq "${product.$key}"`;
      });
    } else {
      dependants.forEach((f) => {
        this.fields[f].disable();
        this.fields[f].dependsOn = null;
        this.fields[f].where = null;
      });
    }
  },
  getProductDependants: function getProductDependants() {
    return ['UnitOfMeasure'];
  },
  onInsert: function onInsert(values) {
    this._applyLogicValues(values);
    this.inherited(arguments);
  },
  onRefresh: function onRefresh() {
    this.inherited(arguments);
    this.setProductDependentFields();
  },
  processEntry: function processEntry(entry) {
    this.setProductDependentFields(entry.Product);
    return entry;
  },
  requestProductPricing: function requestProductPricing(product, quantity, slxLocation, unitOfMeasure) {
    let salesOrder = null;
    if (this.options && this.options.context && this.options.context.SalesOrder) {
      salesOrder = this.options.context.SalesOrder;
    } else {
      if (this.fields.SalesOrder) {
        salesOrder = this.fields.SalesOrder.getValue();
      }
    }
    if (salesOrder && salesOrder.$key && !this.isProcessingPricingRequest) {
      this.isProcessingPricingRequest = true;
      this.enablePricingControls(false);
      PricingAvailabilityService.getOrderItemPricing(this.entry, salesOrder, product, quantity, slxLocation, unitOfMeasure).then(
        (results) => {
          this.onProductPricingSuccess(results);
        }, (error) => {
        this.onProductPricingFailed(error);
      });
    }
  },
  onProductPricingSuccess: function onProductPricingSuccess(result) {
    this.processProductPricing(result).then(() => {
      this.reCalculate();
      this.isProcessingPricingRequest = false;
      this.enablePricingControls(true);
    }, () => {
      this.isProcessingPricingRequest = false;
      this.enablePricingControls(true);
    });
  },
  onProductPricingFailed: function onProductPricingFailed(result) {
    this.isProcessingPricingRequest = false;
    this.enablePricingControls(true);
    this.App.modal.createSimpleDialog({
      title: 'alert',
      content: result.Results,
    });
  },
  setUomFromCode: function setUomFromCode(uomCode) {
    const curremtUom = this.fields.UnitOfMeasure.getValue();
    const product = this.fields.Product.getValue();
    const promise = new Promise((resolve, reject) => {
      if (!this._uomModel) {
        this._uomModel = App.ModelManager.getModel(MODEL_NAMES.UNITOFMEASURE, MODEL_TYPES.SDATA);
      }
      if (this._uomModel && product) {
        if ((curremtUom && (curremtUom.Name !== uomCode)) || (!curremtUom)) {
          this._uomModel.getUnitOfMeasureFromCode(uomCode, product.$key).then((uom) => {
            this.fields.UnitOfMeasure.setValue(uom);
            resolve(true);
          }, (error) => {
            reject(error);
          });
        } else {
          resolve(true);
        }
      } else {
        resolve(true);
      }
    });
    return promise;
  },
  processProductPricing: function processProductPricing(pricingData) {
    const promise = new Promise((resolve, reject) => {
      if (pricingData) {
        if (pricingData.DocCalculatedPrice) {
          this.fields.DocCalculatedPrice.setValue(pricingData.DocCalculatedPrice.value);
        }
        if (pricingData.DocExtendedPrice) {
          this.fields.DocExtendedPrice.setValue(pricingData.DocExtendedPrice.value);
        }
        if (pricingData.DocTotalAmount) {
          this.fields.DocTotalAmount.setValue(pricingData.DocTotalAmount.value);
        }
        if (pricingData.SlxLocationId) {
          this.fields.SlxLocation.setValue({
            $key: pricingData.SlxLocationId.value,
            Name: pricingData.SlxLocationCode.value,
          });
        }
        if (pricingData.UnitOfMeasure) {
          this.setUomFromCode(pricingData.UnitOfMeasure.value).then(() => {
            resolve(true);
          }, (error) => {
            reject(error);
          });
        } else {
          resolve(true);
        }
      } else {
        resolve(true);
      }
    });
    return promise;
  },
  onProductChange: function onProductChange(value, field) {
    this.setProductDependentFields(field.currentSelection);
    this.fields.ProductName.setValue(value.text);
    this.fields.SlxLocation.setValue(null);
    this.requestProductPricing(
      field.currentSelection,
      this.fields.Quantity.getValue(),
      this.fields.SlxLocation.getValue(),
      this.fields.UnitOfMeasure.getValue()
{'fraction_non_alphanumeric': 0.12279365079365079, 'fraction_numerical': 0.0005714285714285715, 'mean_word_length': 3.3403141361256545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4115533', 'n_tokens_mistral': 4765, 'n_tokens_neox': 4687, 'n_words': 708}