19626
2023-05-17 03f01ce3cd914880401e4635901ce6a58668416f
force-app/main/default/lwc/lexPCLLostReportPage/lexPCLLostReportPage.js
@@ -4,88 +4,104 @@
 * @Author: chen jing wu
 * @Date: 2023-04-20 15:04:03
 * @LastEditors: chen jing wu
 * @LastEditTime: 2023-04-26 11:45:00
 * @LastEditTime: 2023-05-16 11:43:27
 */
/*
 * @Description:
 * @version:
 * @Author: chen jing wu
 * @Date: 2023-04-20 15:04:03
 * @LastEditors: chen jing wu
 * @LastEditTime: 2023-04-20 17:11:01
 */
const columns = [
    {
      label: '失单品牌',
      fieldName: 'LostBrandName__c',
      type: 'list',
      editable: true
    },
    {
      label: '失单对手型号',
      fieldName: 'LostProductName__c',
      type: 'text',
      editable: true
    },
    {
        label: '失单数量',
        fieldName: 'Quantity__c',
        type: 'number',
        editable: true
    },
    {
    label: '失单对手型号(手动)',
    fieldName: 'LostProductMannual__c',
    type: 'text',
    editable: true
    },
    {
    label: '失单产品类别',
    fieldName: 'ProductClass__c',
    type: 'list',
    editable: true
    },
    {
    label: '失单产品区分',
    fieldName: 'ProductCategory__c',
    type: 'list',
    editable: true
    }
    // ...
const columns2=[
    { label: '--无--', value: '' },
    { label: '无信息,跟进不及时', value: '无信息,跟进不及时' },
    { label: '价格', value: '价格' },
    { label: '医生偏好', value: '医生偏好' },
    { label: '服务不到位', value: '服务不到位' },
    { label: '行政决策', value: '行政决策' },
    { label: '竞品原有基础上增添设备', value: '竞品原有基础上增添设备' },
    { label: '渠道因素', value: '渠道因素' },
    { label: '某种产品缺失', value: '某种产品缺失' },
    { label: '采购平衡', value: '采购平衡' }
  ];
import { api, wire,track,LightningElement } from 'lwc';
import { CurrentPageReference } from "lightning/navigation";
import save from '@salesforce/apex/lexPCLLostReportLwcController.save';
import dataEntry from '@salesforce/apex/lexPCLLostReportLwcController.dataEntry';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { loadStyle, loadScript } from 'lightning/platformResourceLoader';
import setbrand from '@salesforce/apex/lexPCLLostReportLwcController.setBrand';
import init from '@salesforce/apex/lexPCLLostReportLwcController.init';
import initForApex from '@salesforce/apex/lexPCLLostReportLwcController.initForApex';
import multiSelectCombobox from 'c/multiSelectCombobox'
import lexMultiSelectCombobox from 'c/lexmultiSelectCombobox';
import searchBrands from '@salesforce/apex/lexPCLLostReportLwcController.searchBrands';
export default class LexPCLLostReportPage extends LightningElement {
    @api oppId = '0061000001R2xjWAAR';
    @api lostReportId;
    @api pageStatus = 'Create';
    @api lostType = '失单';
    @api submitFlag;
    searchResult;
    LostReport;
    brandCount;
    brandOptions = [];
import getNewLostBrand from '@salesforce/apex/lexPCLLostReportLwcController.getNewLostBrand';
import { NavigationMixin } from 'lightning/navigation';
import {CloseActionScreenEvent} from 'lightning/actions';
export default class LexPCLLostReportPage extends NavigationMixin(LightningElement) {
    @track records;
    @api recordJson;
    @track columns2=columns2;
    @api reasonValue;
    @api oppId;
    lostReportId = '';
    pageStatus = 'Create';
    lostType = '失单';
    submitFlag = '';
    flag;
    @track LostReport = {
        LostBrands: [
        ],
        lostReport: {}
    };
    @track brandOptions = [];
    columns=[
        { "label" : "失单品牌", "apiName" : "LostBrandName__c" ,"fieldType":"picklist","objectName":"PCLLostProduct__c", "readOnly":true,"value":"--无--","required":false},
        { "label" : "失单对手型号", "apiName" : "LostProduct__c" ,"fieldType":"lookup","objectName":"PCLLostProduct__c","disable":"false","searchfield":"'无'","required":false},
        { "label" : "失单数量", "apiName" : "Quantity__c","fieldType":"number","objectName":"PCLLostProduct__c" ,"required":true},
        { "label" : "失单对手型号(手动)", "apiName" : "LostProductMannual__c","fieldType":"text","objectName":"PCLLostProduct__c" ,"required":false},
        { "label" : "失单产品类别", "apiName" : "ProductClass__c","fieldType":"picklist","objectName":"PCLLostProduct__c" ,"required":true},
        { "label" : "失单产品区分", "apiName" : "ProductCategory__c","fieldType":"picklist","objectName":"PCLLostProduct__c","required":true}
    ];
    connectedCallback() {
        this.initAll();
        init({
            oppId1: this.oppId,
            lostReportId1: this.lostReportId,
            pageStatus1: this.pageStatus,
            lostType1: this.lostType,
            submitFlag1: this.submitFlag
        }).then(result=>{
            console.log(result);
            if(result.message != null){
                if(result.message == '提交成功!'){
                    this.showToast(result.message,"success");
                    this.LostReport = result.LostReport;
                }else{
                    this.showToast(result.message,"error");
                }
            }else{
                this.LostReport = result.LostReport;
                this.LostReport.lostReport.LostTotalAmount__c = 0.0;
                this.LostReport.LostBrands[0].columns = JSON.parse(JSON.stringify(this.columns));
            }
            }).catch(error=>{
                console.log("error");
                console.log(error);
            });
        searchBrands().then(result=>{
            this.brandOptions = JSON.parse(result);
            console.log(this.brandOptions);
            const multiCombobox = this.template.querySelector('c-multi-select-combobox');
            const multiComboboxs = this.template.querySelectorAll('c-multi-select-combobox');
            multiComboboxs.forEach(multiCombobox=>{
            multiCombobox.refreshOptions(this.brandOptions);
            });
        }).catch(error=>{
            console.log("error");
            console.log(error);
        });
    }
    get isInit(){
        if(this.LostReport.LostBrands[0].columns == undefined){
            return false;
        }else{
            return true;
        }
    }
    
    //失单类型
    RecordTypeOptions = [{ label: '--无--', value: '' },
@@ -106,80 +122,20 @@
            }
        }
    }
    // connectedCallback(){
    //     this.initAll();
    // }
    // connectedCallback(){
    //     console.log("1");
    //     initForApex({
    //         oppId1: this.oppId,
    //         lostReportId1: this.lostReportId,
    //         pageStatus1: this.pageStatus,
    //         lostType1: this.lostType,
    //         submitFlag1: this.submitFlag
    //     }).then(()=>{
    //         console.log("2");
    //         init().then(result=>{
    //             console.log("3");
    //             console.log(result);
    //             if(result.message == '提交成功!'){
    //                 this.showToast(result.message,"success");
    //                 this.LostReport = result.LostReport;
    //             }else{
    //                 console.log("211");
    //                 this.showToast(result.message,"error");
    //                 console.log("985");
    //             }
    //         });
    //     });
    // }
    initAll(){
        console.log(this.oppId);
        console.log(this.lostReportId);
        console.log(this.pageStatus);
        console.log(this.lostType);
        console.log(this.submitFlag);
        initForApex({
                    oppId1: this.oppId,
                    lostReportId1: this.lostReportId,
                    pageStatus1: this.pageStatus,
                    lostType1: this.lostType,
                    submitFlag1: this.submitFlag
                }).then(()=>{
                    console.log("2");
                    init().then(result=>{
                        console.log("3");
                        console.log(result);
                        if(result.message == '提交成功!'){
                            this.showToast(result.message,"success");
                            this.LostReport = result.LostReport;
                        }else{
                            console.log("211");
                            //this.showToast(result.message,"error");
                            console.log("985");
    getBrandLabel(index){
        return "失单品牌" + (this.LostReport.LostBrands[index].lineno + 1);
                        }
                    });
                });
    }
    getParamValue(paramName) {
        // Use the URLSearchParams API to get the value of a query parameter
        const params = new URLSearchParams(window.location.search);
        return params.get(paramName);
    }
    saveJs() {
        save().then(result=>{
            if(result){
                this.showToast(result,"success");
                this.clearBrandMannualName();
            }
        })
    }
    addBrandJs() {
        addBrand().then(()=>{
        getNewLostBrand({
            lineNo: this.LostReport.LostBrands.length
        }).then(result=>{
            this.LostReport.LostBrands.push(result);
            this.LostReport.LostBrands[this.LostReport.LostBrands.length - 1].columns = JSON.parse(JSON.stringify(this.columns));
        });
    }
    submitJS() {
@@ -199,70 +155,66 @@
            }
        });
    }
    addProductJs(number) {
        addProduct().then(()=>{
        });
    }
    RemoveJs(number) {
        Remove().then(()=>{
        });
    }
    // add tcm 20211118 start
    searchJs(topNum, secondNum) {
        search().then(()=>{
        });
    }
    get pageStatusIsCreateOrEdit() {
        return this.pageStatus === 'Create' || this.pageStatus === 'Edit';
    }
    
    get isBrandCount2(){
        var flag = true;
        if(this.LostReport.LostBrands.length > 1){
            flag = false;
        }
        return flag;
    }
    deleteBrandJs(event){
        var str = event.target.name;
        this.columnsArrIndex = 0;
        this.LostReport.LostBrands.splice(str,1);
    }
    // add tcm 20211118 end
    setLostTotalAmount() {
        //console.log('setLostTotalAmount start:');
        var totalAmount = this.template.querySelector('[data-id="LostTotalAmount"]');
        var totalAmountHidden = this.template.querySelector('[data-id="LostTotalAmountHidden"]');
        var brandAmountObjects = this.template.querySelectorAll('[data-id^="BrandContent:LostPriceOut"]');
        var brandCompanyObjects = this.template.querySelectorAll('[data-id^="BrandContent:Lost_By_CompanyOut"]');
        if (!!totalAmount && !!totalAmountHidden) {
        var elements = this.template.querySelectorAll('[data-id="TotalAmount"]');
            var tempLostAmount = 0.0;
            var brandCount = parseInt(this.brandCount);
            //console.log(brandCount);
            for (var i = 0; i < brandCount; i++) {
                var brandAmountObject = brandAmountObjects[i];
                var brandCompanyObject = brandCompanyObjects[i];
                if (!!brandAmountObject && !!brandCompanyObject) {
                    var brandCompanyText = brandCompanyObject.innerText;
                    var brandCompanyValue = brandCompanyObject.value;
                    if (!!brandCompanyText && brandCompanyText !== '--无--' && !!brandCompanyValue) {
                        var brandAmount = parseFloat(brandAmountObject.value);
                        tempLostAmount = tempLostAmount + brandAmount;
        elements.forEach(element => {
            console.log(element.value);
            if(element.value != 0){
                tempLostAmount = tempLostAmount + parseFloat(element.value);
                    }
                }
            }
            totalAmount.innerHTML = toNumComma(tempLostAmount);
            totalAmountHidden.value = tempLostAmount;
        }
        //console.log('setLostTotalAmount end');
        });
        this.LostReport.lostReport.LostTotalAmount__c = tempLostAmount;
    }
    setBrandName(brandNumber) {
        setbrand().then(()=>{
            this.clearBrandMannualName();
        });
    }
    setBrandMannualName(brandNumber) {
        setbrandmannual().then(()=>{
        });
    setBrandMannualName(event) {
        var index = event.target.name;
        const payload = event.detail.payload;
        const payloadType = event.detail.payloadType;
        this.LostReport.LostBrands[index].lostBrand.Lost_By_Company__c = payload.value;
        if(payloadType === 'multi-select'){
            this.setDefaultBrand(payload.value,index);
            this.clearBrandMannualName(payload.value,index);
    }
    }
    setDefaultBrand(value,index){
        this.LostReport.LostBrands[index].columns[0].value = value;
    }
    // 失单品牌不等于其他时,失单品牌(手动)清空并且不允许填写,失单品牌等于其他时,失单对手型号不可用  thh 2022-01-13 start
    clearBrandMannualName(){
        if (this.template.querySelector('[id$="Lost_By_CompanyOut"]').value === '其他') {
            this.template.querySelector('[id$="LostProduct"]').setAttribute('disabled', true);
    clearBrandMannualName(value,index){
        if (value == '其他') {
            var elements = this.template.querySelectorAll('[data-id="Lost_By_Company_Mannual"]');
            elements[index].disabled = false;
            this.LostReport.LostBrands[index].columns[1].disable = true;
        } else {
            this.template.querySelector('[id$="Lost_By_Company_MannualOut:Lost_By_Company_Mannual"]').value = '';
            this.template.querySelector('[id$="Lost_By_Company_MannualOut:Lost_By_Company_Mannual"]').setAttribute('disabled', true);
            var elements = this.template.querySelectorAll('[data-id="Lost_By_Company_Mannual"]');
            elements[index].value = '';
            elements[index].disabled = true;
            this.LostReport.LostBrands[index].columns[1].disable = false;
        }
    }
    cancel() {
@@ -274,6 +226,7 @@
                actionName: 'view'
            }
        });
    }
    showToast(msg,type) {
        const event = new ShowToastEvent({
@@ -284,5 +237,206 @@
        this.dispatchEvent(event);
        this.dispatchEvent(new CloseActionScreenEvent());
    }
    saveBrandJs(event) {
        var index = event.target.name;
        var tables = this.template.querySelectorAll("c-lex-dynamic-table");
            if(tables.length > 1){
                if(index == "save"){
                    for(let i=0;i < table.length;i++){
                        var products = this.LostReport.LostBrands[i].LostProducts;
                        var table = tables[i];
                        if(table!=undefined){
                            var records = table.retrieveRecords();
                            for(let j=0;j<records.length - 1;j++){
                                products.push(JSON.parse(JSON.stringify(products[0])));
                            }
                            for(let j=0;j<records.length;j++){
                                products[j].LostProductss.LostBrandName__c = records[j].LostBrandName__c;
                                products[j].LostProductss.Quantity__c = records[j].Quantity__c;
                                products[j].LostProductss.LostProductMannual__c = records[j].LostProductMannual__c;
                                products[j].LostProductss.ProductClass__c = records[j].ProductClass__c;
                                products[j].LostProductss.ProductCategory__c = records[j].ProductCategory__c;
                                products[j].LostProductss.LostProduct__c = records[j].LostProduct__c;
                            }
                        }
                    }
                }else{
                    var products = this.LostReport.LostBrands[index].LostProducts;
                    var table = tables[index];
                    if(table!=undefined){
                        var records = table.retrieveRecords();
                        for(let i=0;i<records.length - 1;i++){
                            products.push(JSON.parse(JSON.stringify(products[0])));
                        }
                        for(let i=0;i<records.length;i++){
                            products[i].LostProductss.LostBrandName__c = records[i].LostBrandName__c;
                            products[i].LostProductss.Quantity__c = records[i].Quantity__c;
                            products[i].LostProductss.LostProductMannual__c = records[i].LostProductMannual__c;
                            products[i].LostProductss.ProductClass__c = records[i].ProductClass__c;
                            products[i].LostProductss.ProductCategory__c = records[i].ProductCategory__c;
                            products[i].LostProductss.LostProduct__c = records[i].LostProduct__c;
                        }
                    }
                }
            }else{
                var products = this.LostReport.LostBrands[0].LostProducts;
                var table = tables[0];
                if(table!=undefined){
                    var records = table.retrieveRecords();
                    console.log(JSON.stringify(records));
                    if(records.length > 1){
                        for(let i=0;i<records.length - 1;i++){
                            products.push(JSON.parse(JSON.stringify(products[0])));
                        }
                    }
                    for(let i=0;i<records.length;i++){
                        products[i].LostProductss.LostBrandName__c = records[i].LostBrandName__c;
                        products[i].LostProductss.Quantity__c = records[i].Quantity__c;
                        products[i].LostProductss.LostProductMannual__c = records[i].LostProductMannual__c;
                        products[i].LostProductss.ProductClass__c = records[i].ProductClass__c;
                        products[i].LostProductss.ProductCategory__c = records[i].ProductCategory__c;
                        products[i].LostProductss.LostProduct__c = records[i].LostProduct__c;
                    }
                }
            }
        this.dataCheck();
        if(!this.flag){
            return;
        }
        dataEntry({
            report1 : JSON.stringify(this.LostReport)
        }).then(result=>{
            if(result){
                this.showToast(result,"error")
            }
        }).catch(error=>{
            console.log("error");
            console.log(error);
        });
    
}
    handleLostTypeChange(event){
        var value = event.target.value;
        this.LostReport.lostReport.LostType__c = value;
    }
    handleLostReasonMainChange(event){
        var index = event.target.name;
        var value = event.target.value;
        this.LostReport.LostBrands[index].lostBrand.Lost_reason_main__c = value;
    }
    handleLostReasonSubChange(event){
        var index = event.target.name;
        var value = event.target.value;
        this.LostReport.LostBrands[index].lostBrand.Lost_Reason_Sub__c = value;
    }
    handleLostByCompanyMannualChange(event){
        var index = event.target.name;
        var value = event.target.value;
        this.LostReport.LostBrands[index].lostBrand.Lost_By_Company_Mannual__c = value;
    }
    handleLostPriceOutChange(event){
        var index = event.target.name;
        var value = event.target.value;
        this.LostReport.LostBrands[index].lostBrand.LostPrice__c = value;
        this.setLostTotalAmount();
    }
    handleAgencyOutChange(event){
        var index = event.target.name;
        var value = event.target.value;
        this.LostReport.LostBrands[index].lostBrand.Agency__c = value;
    }
    handleAgencyMannualOutChange(event){
        var index = event.target.name;
        var value = event.target.value;
        this.LostReport.LostBrands[index].lostBrand.AgencyMannual__c = value;
    }
    dataCheck(){
        this.flag = true;
        if(this.LostReport.lostReport.LostType__c == undefined || this.isBlank(this.LostReport.lostReport.LostType__c))
      {
            this.flag = false;
            this.showToast('必须填写失单类型!','error');
      }
        this.LostReport.LostBrands.forEach(tempLostBrand=>{
         if(tempLostBrand.lostBrand.Lost_By_Company__c == undefined || this.isBlank(tempLostBrand.lostBrand.Lost_By_Company__c)) {
                this.flag = false;
                this.showToast('请填写失单品牌!','error');
         }// fy SWAG-CCC6F6 start
         else if(tempLostBrand.lostBrand.Lost_By_Company__c == '其他' && (tempLostBrand.lostBrand.Lost_By_Company_Mannual__c == undefined || this.isBlank(tempLostBrand.lostBrand.Lost_By_Company_Mannual__c))){
                this.flag = false;
                this.showToast('请填写失单品牌(手动)!','error');
         }// fy SWAG-CCC6F6 end
         if(tempLostBrand.lostBrand.LostPrice__c == undefined || this.isBlank(tempLostBrand.lostBrand.LostPrice__c)) {
                this.flag = false;
                this.showToast('失单金额必填!','error');
         }
         if(tempLostBrand.lostBrand.Lost_reason_main__c == undefined || this.isBlank(tempLostBrand.lostBrand.Lost_reason_main__c)) {
                this.flag = false;
                this.showToast('失单理由(主)必填!','error');
         }
         if(tempLostBrand.lostBrand.Agency__c == undefined || this.isBlank(tempLostBrand.lostBrand.Agency__c)) {
                this.flag= false;
                this.showToast('中标经销商必填!','error');
         }
         // 检查是否有超过1个有数的产品
         var productCount = 0;
            tempLostBrand.LostProducts.forEach(temlostProduct=>{
                if ((temlostProduct.LostProductss.LostProduct__c != undefined && !this.isBlank(temlostProduct.LostProductss.LostProduct__c)) || (temlostProduct.LostProductss.LostProductMannual__c != undefined && !this.isBlank(temlostProduct.LostProductss.LostProductMannual__c))) {
               productCount ++;
            }
            // update tcm 20211123 start
            if(((temlostProduct.LostProductss.LostProduct__c!= undefined && !this.isBlank(temlostProduct.LostProductss.LostProduct__c)) || (temlostProduct.LostProductss.LostProductMannual__c!=undefined && !this.isBlank(temlostProduct.LostProductss.LostProductMannual__c)))&&(temlostProduct.LostProductss.Quantity__c==undefined || this.isBlank(temlostProduct.LostProductss.Quantity__c) || temlostProduct.LostProductss.Quantity__c ==0)) {
                    this.flag = false;
                    this.showToast('请填写失单数量!','error');
            }
            if(((temlostProduct.LostProductss.LostProduct__c!=undefined && !this.isBlank(temlostProduct.LostProductss.LostProduct__c)) || (temlostProduct.LostProductss.LostProductMannual__c!=undefined && !this.isBlank(temlostProduct.LostProductss.LostProductMannual__c)))&&(temlostProduct.LostProductss.ProductCategory__c==undefined || this.isBlank(temlostProduct.LostProductss.ProductCategory__c))) {
                    this.flag = false;
               if (temlostProduct.LostProductss.ProductClass__c==undefined || this.isBlank(temlostProduct.LostProductss.ProductClass__c)) {
                        this.showToast('失单产品类别必填!','error');
                        this.showToast('失单产品必填!','error');
               }else {
                        this.showToast('失单产品必填!','error');
               }
            }
            // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 start
            if (((temlostProduct.LostProductss.LostProduct__c==undefined || this.isBlank(temlostProduct.LostProductss.LostProduct__c)) && (temlostProduct.LostProductss.LostProductMannual__c==undefined || this.isBlank(temlostProduct.LostProductss.LostProductMannual__c)))&&((temlostProduct.LostProductss.ProductCategory__c!=undefined && !this.isBlank(temlostProduct.LostProductss.ProductCategory__c))||(temlostProduct.LostProductss.Quantity__c!=undefined && !this.isBlank(temlostProduct.LostProductss.Quantity__c)))) {
                    this.flag = false;
               if(tempLostBrand.lostBrand.Lost_By_Company__c != '其他'){
                        this.showToast('失单对手型号或失单对手型号(手动)必填!','error');
               } else{
                        this.showToast('失单对手型号或失单对手型号(手动)必填!','error');
               }
            }
            // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 end
            // update tcm 20211123 end
         });
         // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 start
         if (productCount == 0 && (tempLostBrand.LostProducts != undefined && !this.isBlank(tempLostBrand.LostProducts)) && tempLostBrand.LostProducts.length > 0) {
                this.flag = false;
            if(tempLostBrand.lostBrand.Lost_By_Company__c != '其他'){
                    this.showToast('至少录入1条失单对手型号信息!','error');
            }else{
                    this.showToast('至少录入1条失单对手型号信息!','error');
            }
            }
            // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 end
        });
    }
    isBlank(str) {
        return /^\s*$/.test(str);
      }
}