transferShipmentActions.js 4.14 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';

import {Actions} from 'react-native-router-flux';
import TransferShipmentService from '../../services/TransferShipmentService';
import {Alert, Linking} from 'react-native';
import CONFIG from '../../constants/config';

const {

	GO_TO_OUT_STOCK,
	GO_TO_OUT_DELIVERGOODS,

	GET_PURCHASE_LIST_REQUEST,
	GET_PURCHASE_LIST_SUCCESS,
	GET_PURCHASE_LIST_FAILURE,

	CHECKUP_REQUEST,
	SET_PURCHASE_NUM,

	RETSET_DATA,

} = require('../../constants/actionTypes').default;

export function goToStatsPage(type,params) {
	switch (type) {
        case CONFIG.statsKey.outOfStock:
		{
			Actions.OutOfStock({initialRes: params});
			return {
				type: GO_TO_OUT_STOCK,
			};
		}
		case CONFIG.statsKey.deliverGoods:
		{
			if (params.length <= 0) {
				Alert.alert(
                    '请选择订单和填写数量!',
                    '',
                    [{text: 'OK'},]
                );
			}else {
				Actions.DeliverGoods({initialRes: params});
			}
			return {
				type: GO_TO_OUT_DELIVERGOODS,
			};
		}
    }
}

export function resetData() {
	return {
		type: RETSET_DATA,
	};
}

export function goDeliverGoods() {
	return (dispatch,getState) => {
		let {transferShipment,home} = getState();
		let {purchaseList} = transferShipment;
		let data = purchaseList?purchaseList.toJS():null;
		let records = data?data.records:[];
		let list = [];

		for (var i = 0; i < records.length; i++) {
			let value = records[i];
			if (value.checked && value.num > 0) {
				let  proRequisitionFormId = value.proRequisitionFormId;
				let hasPush = false;

				for (var i = 0; i < list.length; i++) {
					let item = list[i];
					if (item.proRequisitionFormId == proRequisitionFormId) {
						item.value.push(value);
						hasPush = true;
					}
				}
				if (!hasPush) {
					let param = {proRequisitionFormId: value.proRequisitionFormId,value: [value]};
					list.push(param);
				}
			}
		}
		dispatch(goToStatsPage(CONFIG.statsKey.deliverGoods,list))
	};
}

export function getPurchaseListRequest() {
	return {
		type: GET_PURCHASE_LIST_REQUEST,
	};
}

export function getPurchaseListSuccess(json) {
	return {
		type: GET_PURCHASE_LIST_SUCCESS,
		payload: json,
	};
}

export function getPurchaseListFailure(error) {
	return {
		type: GET_PURCHASE_LIST_FAILURE,
		payload: error,
	};
}

export function getPurchaseList() {
	return (dispatch,getState) => {
		let {transferShipment,home,user} = getState();
		let {purchaseList} = transferShipment;

		if (purchaseList.isFetching || purchaseList.endReached) {
			return;
		}

		let shopId = home.shopId;
		let pageSize = purchaseList.pageSize;
		let pageNo = purchaseList.pageNo + 1;

		let pid = user.profile.pid;

		dispatch(getPurchaseListRequest())
		return new TransferShipmentService().getPurchaseList(shopId,pageSize,pageNo,pid)
			.then((json) => {

				json.endReached = json.pageNo == json.totalPage || json.records.length < pageSize;
				if (json.pageNo > 1) {
					let oldList = purchaseList.records.toJS();
					let list = [...oldList, ...json.records];
					json.records = list;
				}

				dispatch(getPurchaseListSuccess(json))
			})
			.catch((error) => {
				dispatch(getPurchaseListFailure(error))
			});
	};
}


export function checkUpSuccess(json) {
	return {
		type: CHECKUP_REQUEST,
		payload: json,
	};
}

export function checkUp(checked,sku) {
	return (dispatch,getState) => {
		let {transferShipment,home} = getState();
		let {purchaseList} = transferShipment;
		let list = purchaseList && purchaseList.records && purchaseList.records.toJS();
		list.map((item, i) => {
			let p_sku = item.productSku;
			if (sku == p_sku) {
				item.checked = checked;
			}
		});
		dispatch(checkUpSuccess(list))
	};
}

export function setPurchaseNumSuccess(json) {
	return {
		type: SET_PURCHASE_NUM,
		payload: json,
	};
}

export function setPurchaseNum(num,sku) {
	return (dispatch,getState) => {
		let {transferShipment,home} = getState();
		let {purchaseList} = transferShipment;
		let list = purchaseList && purchaseList.records && purchaseList.records.toJS();

		list.map((item, i) => {
			let p_sku = item.productSku
			if (sku == p_sku) {
				item.num = num;
			}
		});
		dispatch(setPurchaseNumSuccess(list))
	};
}