|
|
|
@ -1,34 +1,60 @@
|
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
import { Text } from 'react-native'
|
|
|
|
|
import { Background } from '../../components/Background'
|
|
|
|
|
import { useContext, useEffect, useState } from 'react'
|
|
|
|
|
import { FlatList, Text, View } from 'react-native'
|
|
|
|
|
import { Button } from '../../components/Button';
|
|
|
|
|
import { Card } from '../../components/Card';
|
|
|
|
|
import { Input } from '../../components/Input';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
|
import { useForm, useFieldArray } from 'react-hook-form';
|
|
|
|
|
import { NativeStackScreenProps } from '@react-navigation/native-stack';
|
|
|
|
|
import { useFetchWithAuth } from '../../hooks/useFetchWithAuth';
|
|
|
|
|
import { saveWithAuth } from '../../hooks/saveWithAuth';
|
|
|
|
|
import { ManyToOne } from '../../components/ManyToOne';
|
|
|
|
|
import { RootStackParams } from './SalesOrderStack';
|
|
|
|
|
|
|
|
|
|
import OneToMany from '../../components/OneToMany';
|
|
|
|
|
import { SalesOrderContext } from './SalesOrderContex';
|
|
|
|
|
|
|
|
|
|
interface Props extends NativeStackScreenProps<RootStackParams, 'SalesOrderFormScreen'> { };
|
|
|
|
|
|
|
|
|
|
export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
|
console.log("Apertura de screem :" + route.params?.productId)
|
|
|
|
|
|
|
|
|
|
const {lines, setLines, add} = useContext(SalesOrderContext);
|
|
|
|
|
|
|
|
|
|
const { control, handleSubmit, setValue, formState: { errors } } = useForm({
|
|
|
|
|
const { control, handleSubmit, setValue, getValues, formState: { errors } } = useForm({
|
|
|
|
|
defaultValues: {
|
|
|
|
|
id: 0,
|
|
|
|
|
numero: '',
|
|
|
|
|
cliente: { id: 0 },
|
|
|
|
|
detalle: []
|
|
|
|
|
cliente: { id: 0 }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let id: string = "0";
|
|
|
|
|
if (route.params?.id != undefined) {
|
|
|
|
|
id = String(route.params.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (route.params?.productId != undefined && route.params?.productId > 0) {
|
|
|
|
|
const newId = route.params?.productId;
|
|
|
|
|
const exits = lines.filter(line => line.producto.id == newId)
|
|
|
|
|
if (exits.length == 0) {
|
|
|
|
|
const newLine: SalesOrderLine = {
|
|
|
|
|
producto: { id: newId, nombre: "test" },
|
|
|
|
|
cantidad: 1,
|
|
|
|
|
precio: 0
|
|
|
|
|
}
|
|
|
|
|
console.log("Nueva linea :" + route.params?.productId)
|
|
|
|
|
//append(newLine);
|
|
|
|
|
add(newLine);
|
|
|
|
|
//setValue("detalle", lines);
|
|
|
|
|
//console.table(fields)
|
|
|
|
|
}else{
|
|
|
|
|
console.log("Linea repetida");
|
|
|
|
|
console.log(exits)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}, [route.params?.productId]);
|
|
|
|
|
|
|
|
|
|
const getInitData = async () => {
|
|
|
|
|
if (!id || id == "0")
|
|
|
|
|
return;
|
|
|
|
@ -40,7 +66,9 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
|
setValue("id", data['id'])
|
|
|
|
|
setValue("numero", data['numero'])
|
|
|
|
|
setValue("cliente", data['cliente'])
|
|
|
|
|
setValue("detalle", data['detalle'])
|
|
|
|
|
const lines: SalesOrderLine[] = data['detalle'];
|
|
|
|
|
console.log("Agregando lineas:")
|
|
|
|
|
setLines(lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
@ -57,8 +85,6 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
|
try {
|
|
|
|
|
let endpoint = "pedido";
|
|
|
|
|
entity['cliente'] = { id: entity['cliente'].id }
|
|
|
|
|
console.log("Entidad:")
|
|
|
|
|
console.log(entity)
|
|
|
|
|
const { data, error } = await saveWithAuth(endpoint, id, entity);
|
|
|
|
|
if (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
@ -73,21 +99,30 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Background >
|
|
|
|
|
<Card>
|
|
|
|
|
<Text className='text-xl text-center my-4'>Registro de Pedido</Text>
|
|
|
|
|
<Input name="numero" label='Número' control={control} />
|
|
|
|
|
<ManyToOne entity='cliente' columname='cliente' columnvalue="nombre" control={control} />
|
|
|
|
|
<Button
|
|
|
|
|
title='Agrega produtos'
|
|
|
|
|
additionalStyle='mb-4'
|
|
|
|
|
onPress={() => navigation.navigate("ProductListScreen" as never)} />
|
|
|
|
|
<Button
|
|
|
|
|
title='Guardar'
|
|
|
|
|
onPress={handleSubmit(onSubmit)}
|
|
|
|
|
/>
|
|
|
|
|
</Card>
|
|
|
|
|
</Background>
|
|
|
|
|
<FlatList
|
|
|
|
|
|
|
|
|
|
data={[""]}
|
|
|
|
|
renderItem={({ item }) =>
|
|
|
|
|
<>
|
|
|
|
|
<Card>
|
|
|
|
|
<Text className='text-xl text-center my-4'>Registro de Pedido</Text>
|
|
|
|
|
<Input name="numero" label='Número' control={control} />
|
|
|
|
|
<ManyToOne entity='cliente' columname='cliente' columnvalue="nombre" control={control} />
|
|
|
|
|
<OneToMany />
|
|
|
|
|
<Button
|
|
|
|
|
title='Agrega produtos'
|
|
|
|
|
additionalStyle='mb-4'
|
|
|
|
|
onPress={() => navigation.navigate("ProductList" as never)} />
|
|
|
|
|
<Button
|
|
|
|
|
title='Guardar'
|
|
|
|
|
onPress={handleSubmit(onSubmit)}
|
|
|
|
|
/>
|
|
|
|
|
</Card>
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
keyExtractor={item2 => item2}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|