Deliver.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import React, {Component} from 'react';
  2. import {
  3. StyleSheet,
  4. View,
  5. Text,
  6. Alert,
  7. ScrollView,
  8. TextInput,
  9. TouchableOpacity
  10. } from 'react-native';
  11. import {WhiteSpace, List, SwipeAction, InputItem, Modal, Provider, Toast} from '@ant-design/react-native';
  12. import {createAction} from "../../utils";
  13. import {connect} from 'react-redux'
  14. import {Button} from "../../components";
  15. let productItems = []
  16. let productDatas = []
  17. @connect(home => ({...home}))
  18. class DeliverHome extends Component {
  19. constructor(props) {
  20. super(props);
  21. this.state = {
  22. notes: '',
  23. distributor: '',
  24. distributor_name: '',
  25. type: this.props.navigation.state.params.type,
  26. productDatas: [],
  27. no: '',
  28. total: 0,
  29. };
  30. }
  31. componentDidMount() {
  32. // this._fetchData()
  33. }
  34. componentWillUnmount() {
  35. productItems = []
  36. productDatas = []
  37. }
  38. _delete = (product_id, no) => {
  39. for (let i in productDatas) {
  40. if (productDatas[i].product_id == product_id && productDatas[i].no == no) {
  41. this.state.total -= parseInt(productDatas[i].count)
  42. productDatas.splice(i, 1)
  43. productItems.splice(i, 1)
  44. }
  45. }
  46. this.setState({
  47. total: this.state.total,
  48. })
  49. }
  50. _addProduct = (data) => {
  51. const start_no_show = data.no.toUpperCase()
  52. const start_no = start_no_show.slice(data.cross_code.length,)
  53. const start_no_len = start_no.length
  54. let end_no = parseInt(start_no) + parseInt(data.count) - 1
  55. const end_no_len = end_no.toString().length
  56. //终止窜货号前面补零
  57. let zero = ''
  58. for (let i = 0; i < start_no_len - end_no_len; i++) {
  59. zero += '0';
  60. }
  61. end_no = data.cross_code + zero + end_no.toString()
  62. // 防止重复扫描
  63. for (let i in productDatas) {
  64. if (productDatas[i].product_id == data.product_id && productDatas[i].no == data.no) {
  65. this.state.total -= parseInt(productDatas[i].count)
  66. productDatas.splice(i, 1)
  67. productItems.splice(i, 1)
  68. }
  69. }
  70. productDatas.push(
  71. {product_id: data.product_id, return_count: data.count, count: data.count, start_num: start_no, no: data.no}
  72. )
  73. const count = this.state.total + parseInt(data.count)
  74. this.setState({total: count, no: ''})
  75. const right = [
  76. {
  77. text: '删除',
  78. onPress: () => this._delete(data.product_id, data.no),
  79. style: {backgroundColor: '#ff3d27', color: 'white'},
  80. },
  81. ];
  82. productItems.push(
  83. <View key={data.product_id} style={styles.cellContainer}>
  84. <SwipeAction
  85. autoClose
  86. style={{backgroundColor: 'transparent'}}
  87. right={right}
  88. >
  89. <TouchableOpacity onPress={() => this.props.navigation.navigate("DeliverAdd", {
  90. data: data,
  91. callback: this._addProduct
  92. })}>
  93. <View style={styles.rowPadding3}>
  94. <View style={styles.left}>
  95. <Text style={styles.blackText}>{data.name}</Text>
  96. </View>
  97. <Text style={styles.blackText}>数量:{data.count}</Text>
  98. </View>
  99. <View style={styles.rowPadding3}>
  100. <View style={styles.left}>
  101. <Text style={styles.blackText}>起始:{start_no_show}</Text>
  102. </View>
  103. <Text style={styles.blackText}>终止:{end_no}</Text>
  104. </View>
  105. </TouchableOpacity>
  106. </SwipeAction>
  107. </View>
  108. )
  109. }
  110. _onSave = () => {
  111. const type = this.props.navigation.state.params.type
  112. if (this.state.distributor == '') {
  113. Alert.alert('提示', "请选择经销商!");
  114. return
  115. }
  116. if (productDatas.length == 0) {
  117. Alert.alert('提示', "请添加产品!");
  118. return
  119. }
  120. const save_data = {
  121. "distributor": this.state.distributor,
  122. "notes": this.state.notes,
  123. "details": JSON.stringify(productDatas),
  124. "save_type": 'code',
  125. }
  126. if (type == 'Deliver') {
  127. this.props.dispatch({
  128. type: 'home/deliverSave',
  129. payload: save_data,
  130. callback: (data) => {
  131. if(data.msg){
  132. Alert.alert('提示', data.msg);
  133. }
  134. this.props.navigation.goBack();
  135. },
  136. });
  137. } else {
  138. this.props.dispatch({
  139. type: 'home/returnSave',
  140. payload: save_data,
  141. callback: (data) => {
  142. if(data.msg){
  143. Alert.alert('提示', data.msg);
  144. }
  145. this.props.navigation.goBack();
  146. },
  147. });
  148. }
  149. }
  150. _searchNo = (value) => {
  151. this.setState({no: value})
  152. if (value) {
  153. this.props.dispatch({
  154. type: 'home/fetchProductData',
  155. payload: {
  156. cross_code: value,
  157. },
  158. callback: (data) => {
  159. const item = {
  160. no: value,
  161. count: '1',
  162. name: data.name,
  163. cross_code: data.cross_code,
  164. product_id: data.id,
  165. }
  166. this._addProduct(item)
  167. // this.setState({no: ''})
  168. // Toast.info('扫码成功',1)
  169. }
  170. })
  171. }
  172. }
  173. render() {
  174. return (
  175. <View style={{flex: 1}}>
  176. <Provider>
  177. <ScrollView style={{flex: 1}}>
  178. <WhiteSpace/>
  179. <List>
  180. <List.Item extra={this.state.distributor_name} arrow="horizontal"
  181. multipleLine={true} wrap={true}
  182. onPress={() => this.props.navigation.navigate('SearchDistributor',
  183. {
  184. callback: (data) => this.setState({
  185. distributor_name: data.name,
  186. distributor: data.id
  187. })
  188. }
  189. )}><Text style={styles.redText}>经销商</Text></List.Item>
  190. <InputItem
  191. clear
  192. placeholder="请输入"
  193. value={this.state.notes}
  194. onChange={value => {
  195. this.setState({
  196. notes: value,
  197. });
  198. }}
  199. ><Text style={{fontSize: 17, color: '#333'}}>备注</Text></InputItem>
  200. </List>
  201. <View style={{borderTopWidth: 5, borderTopColor: '#eee'}}>
  202. <TextInput placeholder="请输入防窜货号" style={styles.input}
  203. value={this.state.no}
  204. autoFocus={true}
  205. onChangeText={value => this._searchNo(value)}></TextInput>
  206. </View>
  207. {productItems}
  208. </ScrollView>
  209. </Provider>
  210. <View style={{
  211. backgroundColor: '#eee', height: 20, justifyContent: 'flex-end',
  212. flexDirection: 'row', paddingRight: 10
  213. }}>
  214. <Text style={{fontSize: 14, textAlignVertical: 'center'}}>总数量:{this.state.total}</Text></View>
  215. <View style={{flexDirection: 'row', height: 40}}>
  216. <Button onPress={() =>
  217. this.props.navigation.navigate("DeliverAdd", {
  218. callback: this._addProduct
  219. })
  220. }
  221. textStyle={{color: 'white'}} style={{
  222. flex: 1,
  223. borderRadius: 0,
  224. borderWidth: 0,
  225. backgroundColor: '#0099FF',
  226. color: '#fff'
  227. }} title='批量添加'/>
  228. <Button onPress={() => Alert.alert(
  229. '提示',
  230. '确定要保存吗?',
  231. [
  232. {text: '确定', onPress: () => this._onSave()},
  233. {text: '取消'},
  234. ]
  235. )} textStyle={{color: 'white'}} style={{
  236. flex: 1,
  237. borderRadius: 0,
  238. borderWidth: 0,
  239. backgroundColor: '#7DBEFF',
  240. color: '#fff'
  241. }} title='保存'/>
  242. </View>
  243. </View>
  244. );
  245. }
  246. }
  247. const styles = StyleSheet.create({
  248. input: {
  249. borderBottomWidth: 1,
  250. borderBottomColor: '#eee',
  251. marginBottom: 15
  252. },
  253. redText: {
  254. color: 'red',
  255. fontSize: 15,
  256. },
  257. cellContainer: {
  258. paddingVertical: 5,
  259. paddingHorizontal: 5,
  260. borderBottomWidth: 1,
  261. borderColor: '#eee',
  262. marginHorizontal: 5,
  263. overflow: 'hidden'
  264. },
  265. rowPadding3: {
  266. flexDirection: 'row',
  267. padding: 3,
  268. },
  269. left: {
  270. flex: 3,
  271. flexDirection: 'row',
  272. },
  273. blackText: {
  274. color: '#333',
  275. fontSize: 14,
  276. },
  277. })
  278. export default DeliverHome;