diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/ObservFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/ObservFragment.kt index 6dd87e7a..1712e2e2 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/ObservFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/ObservFragment.kt @@ -1,12 +1,18 @@ package es.verdnatura.presentation.view.feature.packaging.fragment import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.graphics.Color +import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable +import android.net.Uri import android.os.Build +import android.os.Environment import android.util.Log.d import android.view.View import android.widget.ImageView +import androidx.core.graphics.drawable.toBitmap import androidx.recyclerview.widget.LinearLayoutManager import com.esafirm.imagepicker.features.ImagePickerConfig import com.esafirm.imagepicker.features.registerImagePicker @@ -18,14 +24,27 @@ import es.verdnatura.domain.ConstAndValues.ENTRYNUMBERIMAGES import es.verdnatura.domain.ConstAndValues.ENTRYOBSERVATIONS import es.verdnatura.domain.ConstAndValues.ENTRYTYPE import es.verdnatura.domain.ConstAndValues.SUPPLIERNAME +import es.verdnatura.domain.ConstAndValues.WAREHOUSEFK import es.verdnatura.presentation.base.BaseFragment -import es.verdnatura.presentation.common.* +import es.verdnatura.presentation.common.OnImageTrashClickListener +import es.verdnatura.presentation.common.OnImageUpdateClickListener +import es.verdnatura.presentation.common.OnOptionsSelectedListener +import es.verdnatura.presentation.common.OnPasillerosItemClickListener +import es.verdnatura.presentation.common.ToolBarAdapterTooltip import es.verdnatura.presentation.view.component.CustomDialog import es.verdnatura.presentation.view.component.CustomDialogList import es.verdnatura.presentation.view.feature.packaging.adapter.ListImageAdapter +import es.verdnatura.presentation.view.feature.packaging.fragment.ObservFragment.ImageUtils.getDrawableFromImageUri import es.verdnatura.presentation.view.feature.packaging.fragment.PackagingViewModel.PackagingViewModel import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO import kotlinx.coroutines.runBlocking +import java.io.File +import java.io.FileOutputStream +import java.io.IOException +import java.io.InputStream +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale @Suppress("UNUSED_ANONYMOUS_PARAMETER") class ObservFragment( @@ -64,10 +83,10 @@ class ObservFragment( private fun setToolBar() { binding.mainToolbar.toolbarTitle.text = buildString { - append(mobileApplication.dataStoreApp.readDataStoreKey(ENTRYTYPE)) - append(": ") - append(mobileApplication.dataStoreApp.readDataStoreKey(SUPPLIERNAME)) - } + append(mobileApplication.dataStoreApp.readDataStoreKey(ENTRYTYPE)) + append(": ") + append(mobileApplication.dataStoreApp.readDataStoreKey(SUPPLIERNAME)) + } val listIcons: ArrayList = ArrayList() val iconNext = ImageView(context) @@ -82,23 +101,30 @@ class ObservFragment( when (item) { iconNext.drawable -> { runBlocking { - mobileApplication.dataStoreApp.editDataStoreKey(ENTRYNUMBERIMAGES, + mobileApplication.dataStoreApp.editDataStoreKey( + ENTRYNUMBERIMAGES, if (listImages.isNullOrEmpty()) 0 else { listImages!!.size - } ) - mobileApplication.dataStoreApp.editDataStoreKey(ENTRYOBSERVATIONS,binding.observations.text.toString()) + } + ) + mobileApplication.dataStoreApp.editDataStoreKey( + ENTRYOBSERVATIONS, + binding.observations.text.toString() + ) } - viewModel.entry_updateObserv(mobileApplication.dataStoreApp.readDataStoreKey(ENTRYID),binding.observations.text.toString()) + viewModel.entry_updateObserv( + mobileApplication.dataStoreApp.readDataStoreKey( + ENTRYID + ), binding.observations.text.toString() + ) ma.onPasillerosItemClickListener( PasillerosItemVO(title = getString(R.string.titlePackagingSummary)), getString(R.string.titlePackagingSummary) ) } - } - } }) @@ -152,7 +178,6 @@ class ObservFragment( launcher.launch(config) } - } override fun observeViewModel() { @@ -167,19 +192,30 @@ class ObservFragment( //http://localhost:3000/api/Entries/1/uploadFile?warehouseId=1&companyId=442&dmsTypeId=21&reference=1&description // ="Example description"&hasFile=false (tiene que ser de tipo Multipart y llevar adjunto un fichero) + override fun OnImageUpdateClickListener(item: Any) { println("a subir imagen") - /* viewModel.entry_uploadPhotoSalix( - warehouseId = mobileApplication.dataStoreApp.readDataStoreKey( - WAREHOUSEFK), - companyId = 442, - dmsTypeId = 21, - reference = mobileApplication.dataStoreApp.readDataStoreKey(ENTRYID).toString(), - description = "Entry id", - hasFile = false, - file= MultipartBody.Part - )*/ - } + + val drawable = getDrawableFromImageUri(requireContext(), (item as Image).uri) + val bitmap = drawable!!.toBitmap() + + viewModel.entry_uploadPhotoSalix( + idEntry = mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID"), + warehouseId = mobileApplication.dataStoreApp.readDataStoreKey( + WAREHOUSEFK + ), + companyId = 442, + dmsTypeId = 27, + reference = mobileApplication.dataStoreApp.readDataStoreKey( + ENTRYID + ).toString(), + description = "Entry id", + hasFile = false, + urlImage = saveBitmapAsTempFile(requireContext(), bitmap) + ) + + } + }, object : OnImageTrashClickListener { override fun OnImageTrashClickListener(item: Any) { d("VERDNATURA::", "Seleccionado trash" + (item as Image).name) @@ -194,5 +230,55 @@ class ObservFragment( LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) binding.listImagesRecycler.layoutManager = layoutManager } + + private fun saveBitmapAsTempFile(context: Context, bitmap: Bitmap): String { + val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) + val fileName = "IMG_$timeStamp" + + println("urlImage tmp fileName ${fileName}") + + val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + val tempFile = File.createTempFile(fileName, ".jpg", storageDir) + + println("urlImage tmp ${tempFile}") + + return try { + val fileOutputStream = FileOutputStream(tempFile) + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream) + fileOutputStream.close() + println("urlImage tmp Absol ${tempFile.absolutePath}") + tempFile.absolutePath + + } catch (e: IOException) { + e.printStackTrace() + "" + } + } + + object ImageUtils { + + fun getDrawableFromImageUri(context: Context, imageUri: Uri): Drawable? { + return try { + // Obtener el InputStream de la URI + val inputStream: InputStream? = context.contentResolver.openInputStream(imageUri) + + // Convertir el InputStream a un Bitmap + val bitmap: Bitmap? = BitmapFactory.decodeStream(inputStream) + + // Convertir el Bitmap a un Drawable + val drawable = bitmap?.let { BitmapDrawable(context.resources, it) } + + // Cerrar el InputStream + inputStream?.close() + + // Retornar el Drawable + drawable + } catch (e: Exception) { + e.printStackTrace() + null + } + } + } + } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingCountFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingCountFragment.kt index 14efff7f..2acbb294 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingCountFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingCountFragment.kt @@ -8,6 +8,7 @@ import android.widget.ImageView import androidx.recyclerview.widget.LinearLayoutManager import es.verdnatura.R import es.verdnatura.databinding.FragmentPackagingCountBinding +import es.verdnatura.domain.notNull import es.verdnatura.presentation.base.BaseFragment import es.verdnatura.presentation.common.OnEditSubQuantityListener import es.verdnatura.presentation.common.OnImageTrashClickListener @@ -42,11 +43,11 @@ class PackagingCountFragment( override fun getLayoutId(): Int = R.layout.fragment_packaging_count - override fun init() { viewModel.getItemsPackaging( - mobileApplication.dataStoreApp.readDataStoreKey("SUPPLIERID"), mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID") + mobileApplication.dataStoreApp.readDataStoreKey("SUPPLIERID"), + mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID") ) customDialogInput = CustomDialogInput(requireContext()) customDialog = CustomDialog(requireContext()) @@ -93,24 +94,29 @@ class PackagingCountFragment( private fun setEvents() { binding.mainToolbar.backButton.setOnClickListener { - ma.onMyBackPressed() + ma.onMyBackPressed() } } - override fun observeViewModel() { with(viewModel) { - entryAdd.observe(viewLifecycleOwner) { + loadEntryAdd.observe(viewLifecycleOwner) { event -> + event.getContentIfNotHandled().notNull { - if (it.isError) { - ma.messageWithSound(it.errorMessage, true, true) + //entryAdd.observe(viewLifecycleOwner) { - } else { - ma.messageWithSound(it.errorMessage, false, true) - viewModel.getItemsPackaging( - mobileApplication.dataStoreApp.readDataStoreKey("SUPPLIERID"), mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID")) + if (it.isError) { + ma.messageWithSound(it.errorMessage, true, true) + + } else { + ma.messageWithSound(it.errorMessage, false, true, isToasted = false) + viewModel.getItemsPackaging( + mobileApplication.dataStoreApp.readDataStoreKey("SUPPLIERID"), + mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID") + ) + } } } @@ -121,7 +127,6 @@ class PackagingCountFragment( override fun OnImageUpdateClickListener(item: Any) { showCustomDialog("quantity", item as ItemSupplier) - } }, object : OnImageTrashClickListener { @@ -157,7 +162,8 @@ class PackagingCountFragment( requireActivity().hideKeyboard() } viewModel.entry_addFromBuy( - mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID"), item = item.id!!.toInt(), + mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID"), + item = item.id!!.toInt(), (if (mobileApplication.dataStoreApp.readDataStoreKey("ENTRYTYPE") == "Rec") { (item.printedStickers.plus(customDialogInput.getValue().toInt())) } else { @@ -178,6 +184,7 @@ class PackagingCountFragment( customDialogInput.currentFocus customDialogInput.setFocusText() } + "delete" -> { customDialog.setTitle(getString(R.string.deleteEntryReviewed)) .setDescription(getString(R.string.sure)) @@ -189,9 +196,13 @@ class PackagingCountFragment( ) { customDialog.cancel() customDialog.dismiss() + println("entryID - ") + println("entryID ${item.id}") + viewModel.entry_addFromBuy( - mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID"), item = item.id!!.toInt(), - 0 + mobileApplication.dataStoreApp.readDataStoreKey("ENTRYID"), + item = item.id!!.toInt(), + printedStickers = 0 ) } @@ -199,7 +210,6 @@ class PackagingCountFragment( customDialog.cancel() customDialog.dismiss() - } .show() } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingSummaryFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingSummaryFragment.kt index 17c37375..48e12a41 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingSummaryFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingSummaryFragment.kt @@ -103,7 +103,7 @@ class PackagingSummaryFragment( itemSupplierList.observe(viewLifecycleOwner) { - var mySummaryList = it.list.filter { it.quantity > 0 } + var mySummaryList = it.list.filter { it.printedStickers > 0 } adapteritemsupplier = ItemSupplierAdapter(mySummaryList, null, null, null, showItems = false) binding.itemsupplierRecyclerview.adapter = adapteritemsupplier diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingViewModel/SupplierViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingViewModel/SupplierViewModel.kt index 62a93328..09748690 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingViewModel/SupplierViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/PackagingViewModel/SupplierViewModel.kt @@ -4,6 +4,7 @@ import android.content.Context import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.map +import es.verdnatura.domain.SalixCallback import es.verdnatura.domain.SilexCallback import es.verdnatura.presentation.base.BaseViewModel import es.verdnatura.presentation.base.getMessageFromAllResponse @@ -18,8 +19,11 @@ import es.verdnatura.presentation.view.feature.packaging.model.SupplierList import es.verdnatura.presentation.view.feature.packaging.model.itemSupplierList import es.verdnatura.presentation.view.feature.packaging.model.notificationQueue import es.verdnatura.presentation.view.feature.packaging.model.obervationEntry +import okhttp3.MediaType import okhttp3.MultipartBody +import okhttp3.RequestBody import retrofit2.Response +import java.io.File class PackagingViewModel(val context: Context) : BaseViewModel(context) { @@ -97,7 +101,7 @@ class PackagingViewModel(val context: Context) : BaseViewModel(context) { fun uploadFile(entry: EntrySalix) { salix.add_entry(entry) .enqueue(object : - SilexCallback(context) { + SalixCallback(context) { override fun onError(t: Throwable) { _entry.value = EntrySalix( isError = true, @@ -126,7 +130,13 @@ class PackagingViewModel(val context: Context) : BaseViewModel(context) { } fun getEntries_fromSupplier(supplier: Int) { - salix.getEntries_fromSupplier("""{"where": {"supplierFk": "$supplier"},"limit":5, "order":"created DESC"}""") + + /* salix.getEntries_fromSupplier("""{"where": { + "supplierFk": "$supplier", + "created": { "gte": "$yesterday" } + },"limit":5, "order":"created DESC"}""".trimMargin())*/ + + salix.getEntries_fromSupplier("""{"where": {"supplierFk": "$supplier"},"limit":5, "order":"created ASC"}""") .enqueue(object : SilexCallback>(context) { override fun onError(t: Throwable) { @@ -166,35 +176,35 @@ class PackagingViewModel(val context: Context) : BaseViewModel(context) { } fun supplier_getItems(supplierFk: Any) { - //pendiente de borrado si la tarea no se lleva a cabo - /* silex.supplier_getItems(supplierFk) - .enqueue(object : SilexCallback>(context) { - override fun onError(t: Throwable) { - val listError: ArrayList = ArrayList() - listError.add( - ItemSupplier( - isError = true, - errorMessage = getMessageFromAllResponse( - nameofFunction(this), - t.message!! - ) - ) - ) - _itemSupplierList.value = itemSupplierList(listError) - } + //pendiente de borrado si la tarea no se lleva a cabo + /* silex.supplier_getItems(supplierFk) + .enqueue(object : SilexCallback>(context) { + override fun onError(t: Throwable) { + val listError: ArrayList = ArrayList() + listError.add( + ItemSupplier( + isError = true, + errorMessage = getMessageFromAllResponse( + nameofFunction(this), + t.message!! + ) + ) + ) + _itemSupplierList.value = itemSupplierList(listError) + } - override fun onSuccess(response: Response>) { - if (response.body() != null) { - _itemSupplierList.value = - response.body()?.let { itemSupplierList(it) } - } else { - val listError: ArrayList = ArrayList() - listError.add(ItemSupplier(null, null)) - _itemSupplierList.value = itemSupplierList(listError) - } - } + override fun onSuccess(response: Response>) { + if (response.body() != null) { + _itemSupplierList.value = + response.body()?.let { itemSupplierList(it) } + } else { + val listError: ArrayList = ArrayList() + listError.add(ItemSupplier(null, null)) + _itemSupplierList.value = itemSupplierList(listError) + } + } - })*/ + })*/ } fun entry_addSalix(entry: EntrySalix) { @@ -260,15 +270,26 @@ class PackagingViewModel(val context: Context) : BaseViewModel(context) { } fun entry_uploadPhotoSalix( - idEntry:Number, + idEntry: Number, warehouseId: Number, companyId: Number, dmsTypeId: Number, reference: String, description: String, hasFile: Boolean, - file: MultipartBody.Part - ) { + urlImage: String, + + ) { + + println("urlImage + $urlImage") + println("urlImage + ${File(urlImage)}") + + + val file = File(urlImage) + println("urlImage + ${file.name}") + + val fileRequestBody: RequestBody = RequestBody.create(MediaType.parse("image/jpeg"), file) + salix.uploadEntryPhoto( idEntry, warehouseId, @@ -277,14 +298,17 @@ class PackagingViewModel(val context: Context) : BaseViewModel(context) { reference, description, hasFile, - file + file = MultipartBody.Part.createFormData( + "file", file.name, fileRequestBody + ) ) .enqueue(object : SilexCallback(context) {}) } fun entry_addFromBuy(id: Number, item: Number, printedStickers: Number) { + println("entryID -- addFromBuy") salix.addFromBuy(id, item, printedStickers) - .enqueue(object : SilexCallback(context) { + .enqueue(object : SalixCallback(context) { override fun onError(t: Throwable) { _entryAdd.value = EntrySalix( isError = true, @@ -341,7 +365,7 @@ class PackagingViewModel(val context: Context) : BaseViewModel(context) { fun entry_addFromPackaging(supplier: Int, isPackaging: Boolean) { salix.addFromPackaging(supplier, isPackaging) - .enqueue(object : SilexCallback(context) { + .enqueue(object : SalixCallback(context) { override fun onError(t: Throwable) { _entryAdd.value = EntrySalix( isError = true, diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierFragment.kt index 9d214d7a..d25c4a09 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/packaging/fragment/SupplierFragment.kt @@ -6,10 +6,10 @@ import android.view.View import androidx.recyclerview.widget.LinearLayoutManager import es.verdnatura.R import es.verdnatura.databinding.FragmentPackagingBinding +import es.verdnatura.domain.isoToString import es.verdnatura.domain.notNull import es.verdnatura.presentation.base.BaseFragment import es.verdnatura.presentation.common.OnPasillerosItemClickListener -import es.verdnatura.presentation.common.convertToDateString import es.verdnatura.presentation.view.component.CustomDialog import es.verdnatura.presentation.view.feature.inventario.fragment.SearchSupplierModel import es.verdnatura.presentation.view.feature.packaging.fragment.PackagingViewModel.PackagingViewModel @@ -273,7 +273,7 @@ class SupplierFragment( try { entries.add( SearchSupplierModel( - entry.id.toString() + "->" + convertToDateString(entry.created), + entry.id.toString() + "->" + (entry.created).isoToString(returnOnlyDate = true), id = entry.id.toString() ) ) diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/PalletScanFragment.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/PalletScanFragment.kt index 163cf950..4355379b 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/PalletScanFragment.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/PalletScanFragment.kt @@ -57,12 +57,20 @@ class PalletScanFragment( with(viewModel) { responsePalletGet.observe(viewLifecycleOwner) { - ma.messageWithSound(message = "", isError = false, isPlayed = true, isToasted = false) - ma.onComprobarPalletFromPalletScan( - ItemExpeditionTruckVO(it.truckFk), ItemPalletVO(Pallet = itemScaned) - ) - customDialogList.dismiss() + if (it.truckFk == 0) { + ma.messageWithSound( + message = getString(R.string.palletNotFound), + isError = true, + isPlayed = true, + isToasted = false + ) + }else{ + ma.onComprobarPalletFromPalletScan( + ItemExpeditionTruckVO(it.truckFk), ItemPalletVO(Pallet = itemScaned) + ) + customDialogList.dismiss() + } } } diff --git a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/PalletScanViewModel.kt b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/PalletScanViewModel.kt index 100da749..678ccd41 100644 --- a/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/PalletScanViewModel.kt +++ b/app/src/main/java/es/verdnatura/presentation/view/feature/paletizador/fragment/PalletScanViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.MutableLiveData import es.verdnatura.domain.SalixCallback import es.verdnatura.domain.formatWithQuotes import es.verdnatura.presentation.base.BaseViewModel +import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpedition import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckSalix import retrofit2.Response @@ -35,8 +36,13 @@ class PalletScanViewModel(val context: Context) : BaseViewModel(context) { .enqueue(object : SalixCallback>(context) { override fun onSuccess(response: Response>) { - _responsePalletGet.value = - response.body()?.get(0) + + if (response.body()!!.isNotEmpty()) { + _responsePalletGet.value = + response.body()?.get(0) + } else { + _responsePalletGet.value = ItemExpeditionTruckSalix(0, ItemExpedition(0)) + } } })