createAConstructionOrder.js 16.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
// tabWorkOrder/pages/createAConstructionOrder/createAConstructionOrder.js
const IMG_URL =
  'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
import commonApi from '~/api/api'
import { setConfigInfo, getPositionList } from '~/store/index'
import api from '~/tabWorkOrder/api'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
	showInventory:false,
	inventoryData: {
		upgradeGoodsCommodityCode: '',
		upgradeGoodsCustomCode: '',
		specification: '',
		stock: ''
	  },
    isFirst: true,
    subOrderRelationList: [{}],
    confirmInfo: {
      subOrderList: []
    },
    tabList: ['施工单1'],
    tabIndex: 0,
    showTips: true,
    IMG_URL,
    mainSelected: [], // 主技师已选
    deputySelected: [], // 副手技师已选
    goodsList: [] // 所有可关联商品 包含当前施工单商品
  },
  // 关联商品
  // handleEditGoods() {
  //   let { confirmInfo } = this.data
  //   // 编辑时的商品数据
  //   const editGoodsList = wx.$util.deepClone(confirmInfo.subOrderList)
  //   this.setData({ editGoodsFlag: true, editGoodsList })
  // },

  // 删除商品
  // handleDelgoods(e) {
  //   let { editGoodsList } = this.data
  //   let { index } = e.currentTarget.dataset
  //   editGoodsList?.splice(index, 1)
  //   this.setData({ editGoodsList })
  // },
  submitConstructionOrder() {
    this.tabClick()
    let {
      copySubOrderRelationList,
      orderCode,
      confirmInfo,
      subOrderRelationList,
      requesting
    } = this.data
    console.log(subOrderRelationList, 'copySubOrderRelationList')
    subOrderRelationList = wx.$util.deepClone(subOrderRelationList)
    // let { mainSelected, deputySelected } = subOrderRelationList[tabIndex]
    // confirmInfo.mainTechnicianList = mainSelected
    // confirmInfo.deputyTechnicianList = deputySelected
    subOrderRelationList = subOrderRelationList.map(item => {
      //.technicianType 技师类型,0-主技师,1-副技师
      //  item.mainTechnicianList =
      // mainTechnicianList
      item.subOrderList = item.confirmInfo.subOrderList
      item.mainTechnicianList = []
      item.mainSelected.map(it => {
        item.mainTechnicianList.push({
          technicianName: it.staffName,
          technicianType: 0,
          technicianUuid: it.uuid,
          staffPhone: it.staffPhone,
          shareRatio:
            it.shareRatio === '' ||
            it.shareRatio === undefined ||
            it.shareRatio === null ||
            isNaN(Number(it.shareRatio))
              ? 1
              : Number(it.shareRatio) / 100
        })
      })
      item.deputySelected.map(it => {
        item.mainTechnicianList.push({
          technicianName: it.staffName,
          technicianType: 1,
          technicianUuid: it.uuid,
          staffPhone: it.staffPhone,
          shareRatio:
            it.shareRatio === '' ||
            it.shareRatio === undefined ||
            it.shareRatio === null ||
            isNaN(Number(it.shareRatio))
              ? 0
              : Number(it.shareRatio) / 100
        })
      })
      delete item.mainSelected
      delete item.deputySelected
      item.serviceItemsList = item.servicesList.reduce((total, item) => {
        item.detailList.map(i => {
          if (i.active) {
            total.push({
              serviceItemsId: i.id,
              serviceItemsName: i.serviceItemsName,
              categoryId: i.categoryId,
              materialStandard: i.materialStandard,
              serviceCost: i.serviceCost,
              timeStandard: i.timeStandard,
              itemsConfigName: i.itemsConfigName,
              useNum: i.useNum < 1 ? 1 : Number(i.useNum) // 兜底处理 当输入框为空值或0时默认赋值1 ,防止失去焦点赋值导致数据错误
            })
          }
        })
        return total
      }, [])
      delete item.servicesList
      delete item.confirmInfo
      return item
    })

    let params = {
      constructionInfoList: subOrderRelationList,
      orderCode,
      subOrderRelationList: copySubOrderRelationList
    }

    let checkFlag = false

    for (let index = 0; index < subOrderRelationList.length; index++) {
      const item = subOrderRelationList[index]

      // Check if the sub-order has associated products
      if (!item.subOrderList.length) {
        wx.$util.showToast(`施工单${index + 1}关联商品不能为空`)
        checkFlag = true
        break // Move to the next index
      }

      // Check if the sub-order has a main technician
      if (
        !item.mainTechnicianList.length ||
        !item.mainTechnicianList.some(t => t.technicianType === 0)
      ) {
        wx.$util.showToast(`施工单${index + 1}主技师不能为空`)
        checkFlag = true
        break // Move to the next index
      }

      // Check if the sub-order has service items
      if (!item.serviceItemsList.length) {
        wx.$util.showToast(`施工单${index + 1}服务项目不能为空`)
        checkFlag = true
        break // Move to the next index
      }
    }

    console.log(params, 'params')
    if (requesting || checkFlag) return false
    this.setData({
      requesting: true
    })
    console.log(params, 'confirmInfo')
    api.batchAdd(params).then(res => {
      let { code, data, message } = res
      if (code == 0) {
        wx.$util.showToast('创建成功')
        wx.$router.redirectTo('tabWorkOrder.orderDetail', {
          orderCode: orderCode
        })
      } else {
        wx.$util.showToast(message)
      }
      setTimeout(() => {
        this.setData({
          requesting: false
        })
      }, 1000)
    })
  },
   // 监听弹窗关闭事件
	onInventoryClose() {
		this.setData({
			showInventory: false
		  });
	  },
	    // 打开弹窗
  openInventoryPopup(e) {
	console.log(e.target.dataset)
	let {inventory:{
	  upgradeGoodsCommodityCode,
	  upgradeGoodsCustomCode,
	  specification,
	  stock
	}} = e.target.dataset
	const inventoryData ={
	  upgradeGoodsCommodityCode,
	  upgradeGoodsCustomCode,
	  specification,
	  stock
	}
  this.setData({
	showInventory: true,
	inventoryData
  });
},
//   打开物料弹窗
openInventory(e){
	const inventoryData=e.detail
	this.setData({
		showInventory: true,
		inventoryData
	  });
},
  backup() {
    let { orderCode } = this.data
    wx.$router.back({ orderCode })
  },
  // 关联商品取消
  handelGoodsCancel() {
    this.setData({
      editGoodsFlag: false
    })
  },

  // 关联商品完成
  // handelGoodsConfirm() {
  //   let { editGoodsList } = this.data
  //   // 将修改后的商品数据赋值给confirmInfo
  //   this.setData({
  //     ['confirmInfo.subOrderList']: editGoodsList,

  //     editGoodsFlag: false
  //   })
  // },

  relateGoods() {
    this.setData({ relateGoodsVisable: true })
  },

  // 关联商品弹窗取消
  handelRelateGoodsCancel() {
    this.setData({ relateGoodsVisable: false })
  },

  // 关联商品弹窗确认
  handelRelateGoodsConfirm(e) {
    let { goodsList, tabList, subOrderRelationList, copyServicesList } =
      this.data
    let { detail } = e
    console.log(detail, 'detail')
    // detail为已选商品 是个数组对象 goodsList 设置为过滤掉已选商品后的数组
    goodsList = goodsList.filter(
      item => !detail.some(i => i.subOrderCode == item.subOrderCode)
    )
    if (goodsList.length > 0) {
      tabList.push('施工单' + (tabList.length + 1))
      let obj = {
        servicesList: wx.$util.deepClone(copyServicesList),
        mainSelected: [],
        deputySelected: []
      }
      obj.confirmInfo = {}
      obj.confirmInfo.subOrderList = []
      subOrderRelationList.push(obj)
    }
    this.setData({
      ['confirmInfo.subOrderList']: detail,
      ['confirmInfo.relateGoodsDisabled']: true,
      subOrderRelationList,
      goodsList,
      tabList
    })
    this.handelRelateGoodsCancel()
  },

  showPopup(e) {
    let { type } = e.currentTarget.dataset
    this.setData({
      technicianMultiple: type == 'deputy',
      artificerSelectPopup: true
    })
  },
  // 技师选择确认
  changeArtificer(e) {
    console.log(e, 'changeArtificer')
    let { technicianMultiple, mainSelected, deputySelected } = this.data
    if (technicianMultiple) {
      if (e.detail?.selectedTechnicians?.length > 2) {
        return wx.$util.showToast('副手技师最多选择两个')
      }
      const deputySelected = e.detail?.selectedTechnicians || []
      const sumDeputy = deputySelected
        .map(i => parseInt(i.shareRatio || 0))
        .reduce((a, b) => a + (isNaN(b) ? 0 : b), 0)
      let residual = 100 - sumDeputy
      if (residual < 0) residual = 0
      if (residual > 100) residual = 100
      const newMainSelected = Array.isArray(mainSelected)
        ? wx.$util.deepClone(mainSelected)
        : []
      if (newMainSelected.length) newMainSelected[0].shareRatio = residual
      this.setData({
        deputySelected,
        mainSelected: newMainSelected
      })
    } else {
      const newMainSelected = e.detail?.selectedTechnicians || []
      const hasDeputy = (deputySelected || []).length > 0
      if (newMainSelected.length) {
        if (hasDeputy) {
          const sumDeputy = (deputySelected || [])
            .map(i => parseInt(i.shareRatio || 0))
            .reduce((a, b) => a + (isNaN(b) ? 0 : b), 0)
          let residual = 100 - sumDeputy
          if (residual < 0) residual = 0
          if (residual > 100) residual = 100
          newMainSelected[0].shareRatio = residual
        } else {
          newMainSelected[0].shareRatio = ''
        }
      }
      this.setData({
        mainSelected: newMainSelected
      })
    }
    this.setData({
      artificerSelectPopup: false
    })
  },
  // 获取服务项目数据
  getServicesList() {
    let { goodsList } = this.data
    const subOrderCodeList = goodsList.map(item => item.subOrderCode)
    api.getServiceItemList({ subOrderCodeList }).then(res => {
      let { code, data, message } = res
      if (code == 0) {
        console.log(data, 'getServiceItemList')
        let { selectedData } = this.data
        const selectedIds =
          selectedData?.reduce((set, item) => {
            item?.detailList?.forEach(({ id }) => set.add(id))
            return set
          }, new Set()) ?? new Set()
        data?.forEach(val => {
          val.title = `${val.categoryName}-${val.itemName}`
          val.detailList = val.serviceItemList?.map(item => ({
            ...item,
            active: selectedIds.has(item.id)
          }))
        })
        this.setData({
          servicesList: res.data,
          copyServicesList: wx.$util.deepClone(res.data)
        })
      } else {
        wx.$util.showToast(message)
      }
    })
  },
  updateNumber(id, value, isBlur = false) {
    let { servicesList } = this.data
    const finalValue = isBlur ? (value < 1 ? 1 : value) : value
    const updatedList = servicesList?.map(item => ({
      ...item,
      detailList: item?.detailList?.map(child =>
        child.id === id ? { ...child, useNum: finalValue } : child
      )
    }))
    this.setData({ servicesList: updatedList })
  },

  handleCountInput(e) {
    let { value } = e.detail
    let { target } = e.currentTarget.dataset
    value = value.replace(/[^\d]/g, '')
    this.updateNumber(target.id, value, false)
  },

  handleCountBlur(e) {
    let { value } = e.detail
    let { target } = e.currentTarget.dataset
    value = value.replace(/[^\d]/g, '')
    this.updateNumber(target.id, value, true)
  },
  // 服务项目选择
  handleChecked(e) {
    let { target } = e.currentTarget.dataset
    let { servicesList } = this.data
    servicesList?.map(val => {
      val?.detailList?.map(v => {
        if (v.id == target.id) {
          v.active = !v.active
          v.useNum = 1
        }
        return v
      })
      return val
    })
    this.setData({ servicesList })
  },
  // 展开收起服务项目
  handleShowItem(e) {
    let { index } = e.currentTarget.dataset
    let { servicesList } = this.data
    this.setData({
      [`servicesList[${index}].showItemFlag`]: !servicesList[index].showItemFlag
    })
  },
  // 首次进入会触发该方法,因此不需要在attached调getList
  tabClick(e) {
    console.log('tabClick', e)
    let {
      tabList,
      isFirst,
      tabIndex,
      subOrderRelationList,
      servicesList,
      confirmInfo,
      mainSelected,
      deputySelected
    } = this.data
    // 是否是第一次进入 是不处理返回
    if (isFirst) {
      this.setData({
        isFirst: false
      })
      return
    }
    // 切换tab 重新赋值刷新数据
    const currentTabIndex =
      e?.detail !== undefined && e?.detail !== null ? e.detail : tabIndex
    console.log(tabIndex, currentTabIndex, 'tabIndex')
    subOrderRelationList[tabIndex] = wx.$util.deepClone({
      ...subOrderRelationList[tabIndex],
      confirmInfo,
      servicesList,
      mainSelected,
      deputySelected
    })

    let {
      confirmInfo: newConfirmInfo,
      servicesList: newServicesList,
      mainSelected: newMainSelected,
      deputySelected: newDeputySelected
    } = subOrderRelationList[currentTabIndex]
    this.setData({
      tabIndex: currentTabIndex,
      list: [],
      subOrderRelationList,
      confirmInfo: newConfirmInfo,
      servicesList: newServicesList,
      mainSelected: newMainSelected,
      deputySelected: newDeputySelected
    })
  },
  // 获取技师数据
  getTechnicianList() {
    let { userInfo } = this.data
    // 获取技师数据
    api
      .getQYTechnicianList({
        yqMerchantId: !wx.$hasPermission?.isSuper?.(userInfo)
          ? wx.$util.getStorageSync('dj_boutique_user_info')?.merchantId
          : ''
      })
      .then(respone => {
        wx.hideLoading()
        console.log(respone.data, 'getQYTechnicianList')
        if (respone?.code == 0) {
          this.setData({
            artificerList: respone.data || [],
            copyArtificerList: respone.data || []
          })
        }
      })
  },
  onRoute(options) {
    // this.setData({
    //   technicianMultiple: true,
    //   artificerSelectPopup: true
    // })
    console.log(options, 'options')
    if (options.constructionList) {
      options.constructionList = options.constructionList.map(val => {
        return {
          subOrderCode: val.subOrderCode,
          upgradeGoodsId: val.upgradeGoodsId,
          upgradeGoodsName: val.upgradeGoodsName,
		  constructionFlag: val.constructionFlag,
		  upgradeGoodsCommodityCode:val.upgradeGoodsCommodityCode,
		  upgradeGoodsCustomCode:val.upgradeGoodsCustomCode,
		  specification:val.specification,
		  stock:val.stock,
          number: val.number
        }
      })
      // 不施工的 过滤
      let filterList = options.constructionList.filter(
        val => val.constructionFlag == 1
      )
      this.setData({
        goodsList: filterList,
        copySubOrderRelationList: options.constructionList,
        orderCode: options.orderCode
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userInfo = wx.$util.getUserInfo()
    this.setData(
      {
        userInfo
      },
      async () => {
        // 获取配置服务数据
        let baseConfigData = await setConfigInfo(app.globalData)
        // this.getDetail()
        this.getTechnicianList()
        this.getServicesList()
        this.setData({
          baseConfigData
        })
      }
    )
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})