|
|
@ -1,9 +1,9 @@
|
|
|
|
import { useContext, useEffect, useState } from 'react'
|
|
|
|
import { useContext, useEffect, useState } from 'react'
|
|
|
|
import { FlatList, Text, View } from 'react-native'
|
|
|
|
import { FlatList, Text } from 'react-native'
|
|
|
|
import { Button } from '../../components/Button';
|
|
|
|
import { Button } from '../../components/Button';
|
|
|
|
import { Card } from '../../components/Card';
|
|
|
|
import { Card } from '../../components/Card';
|
|
|
|
import { Input } from '../../components/Input';
|
|
|
|
import { Input } from '../../components/Input';
|
|
|
|
import { useForm, useFieldArray } from 'react-hook-form';
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
import { NativeStackScreenProps } from '@react-navigation/native-stack';
|
|
|
|
import { NativeStackScreenProps } from '@react-navigation/native-stack';
|
|
|
|
import { useFetchWithAuth } from '../../hooks/useFetchWithAuth';
|
|
|
|
import { useFetchWithAuth } from '../../hooks/useFetchWithAuth';
|
|
|
|
import { saveWithAuth } from '../../hooks/saveWithAuth';
|
|
|
|
import { saveWithAuth } from '../../hooks/saveWithAuth';
|
|
|
@ -15,11 +15,10 @@ import { SalesOrderContext } from './SalesOrderContex';
|
|
|
|
interface Props extends NativeStackScreenProps<RootStackParams, 'SalesOrderFormScreen'> { };
|
|
|
|
interface Props extends NativeStackScreenProps<RootStackParams, 'SalesOrderFormScreen'> { };
|
|
|
|
|
|
|
|
|
|
|
|
export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
console.log("Apertura de screem :" + route.params?.productId)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const {lines, setLines, add} = useContext(SalesOrderContext);
|
|
|
|
const {lines, setLines, add} = useContext(SalesOrderContext);
|
|
|
|
|
|
|
|
|
|
|
|
const { control, handleSubmit, setValue, getValues, formState: { errors } } = useForm({
|
|
|
|
const { control, handleSubmit, setValue, formState: { errors } } = useForm({
|
|
|
|
defaultValues: {
|
|
|
|
defaultValues: {
|
|
|
|
id: 0,
|
|
|
|
id: 0,
|
|
|
|
numero: '',
|
|
|
|
numero: '',
|
|
|
@ -32,29 +31,6 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
id = String(route.params.id);
|
|
|
|
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 () => {
|
|
|
|
const getInitData = async () => {
|
|
|
|
if (!id || id == "0")
|
|
|
|
if (!id || id == "0")
|
|
|
|
return;
|
|
|
|
return;
|
|
|
@ -67,7 +43,6 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
setValue("numero", data['numero'])
|
|
|
|
setValue("numero", data['numero'])
|
|
|
|
setValue("cliente", data['cliente'])
|
|
|
|
setValue("cliente", data['cliente'])
|
|
|
|
const lines: SalesOrderLine[] = data['detalle'];
|
|
|
|
const lines: SalesOrderLine[] = data['detalle'];
|
|
|
|
console.log("Agregando lineas:")
|
|
|
|
|
|
|
|
setLines(lines);
|
|
|
|
setLines(lines);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -85,6 +60,7 @@ export const SalesOrderFormScreen = ({ route, navigation }: Props) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
let endpoint = "pedido";
|
|
|
|
let endpoint = "pedido";
|
|
|
|
entity['cliente'] = { id: entity['cliente'].id }
|
|
|
|
entity['cliente'] = { id: entity['cliente'].id }
|
|
|
|
|
|
|
|
entity['detalle'] = lines;
|
|
|
|
const { data, error } = await saveWithAuth(endpoint, id, entity);
|
|
|
|
const { data, error } = await saveWithAuth(endpoint, id, entity);
|
|
|
|
if (error) {
|
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
|
|
|
console.log(error);
|
|
|
|