SHARE
TWEET

Untitled

a guest Jun 19th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func configureItemFetchedResultsController() {
  2.         print("configureItemFetchedResultsController(): started")
  3.         let itemFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Item")
  4.         itemFetchRequest.sortDescriptors = [NSSortDescriptor(key: "category", ascending: true),NSSortDescriptor(key: "itemId", ascending: true)]
  5.         let user = NSPredicate(format: "order.user.name == %@", UserDetails.fullName ?? "")
  6.         let dateFrom = Conversions.dateConvert(dateString: dateToFetchMin)!
  7.         let dateTo = Conversions.dateConvert(dateString: dateToFetchMax)!
  8.         print(dateFrom)
  9.         let from = NSPredicate(format: "date >= %@", dateFrom as CVarArg)
  10.         let to = NSPredicate(format: "date <= %@", dateTo as CVarArg)
  11.         itemFetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [user,from,to])
  12.         itemFetchRequest.propertiesToFetch = ["category","itemId","itemName"]]
  13.         itemFetchRequest.returnsDistinctResults = true
  14.         itemFetchRequest.propertiesToGroupBy = ["category","itemId","itemName"]
  15.         itemFetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType
  16.  
  17.         itemFetchedResultController = NSFetchedResultsController(fetchRequest: itemFetchRequest, managedObjectContext: context, sectionNameKeyPath: "category", cacheName: nil) as? NSFetchedResultsController<Item>
  18.         do {
  19.             try itemFetchedResultController?.performFetch()
  20.             let resultsDict = itemFetchedResultController!.fetchedObjects!
  21.             print(resultsDict as NSArray)
  22.             print("configureItemFetchedResultsController(): sold items fetched")
  23.         } catch  {
  24.             //            fatalError("failed to fetch entities: (error)")
  25.             print("configureItemFetchedResultsController(): failed to fetch Item entities: (error)")
  26.         }
  27.         self.statisticsTableView.reloadData()
  28.     }
  29.  
  30.  
  31. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  32.  
  33.         let cell = tableView.dequeueReusableCell(withIdentifier: "statisticsCell", for: indexPath) as! StatisticsTableViewCell
  34.         let item = itemFetchedResultController?.object(at: indexPath).itemName!
  35.         //        let item = itemResultsArray[indexPath.row]
  36.         let productRequest: NSFetchRequest<Product> = Product.fetchRequest()
  37.         productRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
  38.         productRequest.predicate = NSPredicate(format: "name == %@", item!)
  39.         productRequest.fetchLimit = 1
  40.         do {
  41.             let fetch = try context.fetch(productRequest)
  42.             cell.idInfoLabel.text = fetch[0].productId
  43.             cell.nameInfoLabel.text = fetch[0].name
  44.             cell.productImageView.image = UIImage(data: (fetch[0].productImage! as Data))
  45.             cell.minimumStockInfoLabel.text = fetch[0].minimumStock
  46.             let productPrice = fetch[0].price
  47.             //fetch item for amount of single object
  48.             let itemId = itemFetchedResultController?.object(at: indexPath).itemId!
  49.             let itemRequest = NSFetchRequest<Item>(entityName: "Item")
  50.             itemRequest.sortDescriptors = [NSSortDescriptor(key: "itemName", ascending: true)]
  51.             itemRequest.predicate = NSPredicate(format: "date BEGINSWITH %@", dateToFetchMin)
  52.             itemRequest.predicate = NSPredicate(format: "itemId == %@", itemId!)
  53.             do {
  54.                 let itemFetch = try context.fetch(itemRequest)
  55.                 print(productPrice!)
  56.                 print(itemFetch, itemFetch.count)
  57.                 cell.soldQuantityInfoLabel.text = String(describing: itemFetch.count)
  58.                 let amount = Double(productPrice!)! * Double(itemFetch.count)
  59.                 cell.salesAmountInfoLabel.text = String(describing: amount)
  60.             } catch  {
  61.                 print("Error in fetching sold items for cell: (error)")
  62.             }
  63.         } catch  {
  64.             print("Error in fetching product for cell: (error)")
  65.         }
  66.         return cell
  67.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top