#69 feat(ReconciliationJar): link purchases with other payment methods.
This commit is contained in:
		| @@ -65,7 +65,9 @@ class ReconciliateJarView(APIView): | ||||
|                     status=HTTP_400_BAD_REQUEST | ||||
|                 ) | ||||
|             reconciliation = serializer.save() | ||||
|             self._link_purchases(reconciliation, cash_purchases) | ||||
|             other_purchases = self._get_other_purchases(data.get('other_totals')) | ||||
|  | ||||
|             self._link_purchases(reconciliation, cash_purchases, other_purchases) | ||||
|             return Response({'id': reconciliation.id}) | ||||
|         return Response(serializer.errors, status=HTTP_400_BAD_REQUEST) | ||||
|  | ||||
| @@ -78,8 +80,23 @@ class ReconciliateJarView(APIView): | ||||
|         calculated_total = sum(p.get_total() for p in purchases) | ||||
|         return calculated_total == Decimal(total) | ||||
|  | ||||
|     def _link_purchases(self, reconciliation, purchases): | ||||
|         for purchase in purchases: | ||||
|     def _get_other_purchases(self, other_totals): | ||||
|         if not other_totals: | ||||
|             return [] | ||||
|         purchases = [] | ||||
|         for method in other_totals: | ||||
|             purchases.extend(other_totals[method]['purchases']) | ||||
|         if purchases: | ||||
|             return Sale.objects.filter(pk__in=purchases) | ||||
|         return [] | ||||
|  | ||||
|     def _link_purchases(self, reconciliation, cash_purchases, other_purchases): | ||||
|         for purchase in cash_purchases: | ||||
|             purchase.reconciliation = reconciliation | ||||
|             purchase.clean() | ||||
|             purchase.save() | ||||
|  | ||||
|         for purchase in other_purchases: | ||||
|             purchase.reconciliation = reconciliation | ||||
|             purchase.clean() | ||||
|             purchase.save() | ||||
|   | ||||
| @@ -105,8 +105,6 @@ | ||||
|           cash_taken: 0, | ||||
|           cash_discrepancy: 0, | ||||
|           other_totals: { | ||||
|             confiar: 0, | ||||
|             bancolombia: 0 | ||||
|           }, | ||||
|           cash_purchases: [], | ||||
|         }, | ||||
| @@ -141,6 +139,22 @@ | ||||
|         } | ||||
|         return 0; | ||||
|       }, | ||||
|       idsBymethod(method) { | ||||
|         if (method in this.summary.purchases) { | ||||
|           return this.summary.purchases[method].map(purchase => purchase.id) | ||||
|         } | ||||
|         return []; | ||||
|       }, | ||||
|       processOtherMethods() { | ||||
|         for (const method of Object.keys(this.summary.purchases)) { | ||||
|           if (method !== 'CASH') { | ||||
|             this.reconciliation.other_totals[method] = { | ||||
|               total: this.totalByMethod(method), | ||||
|               purchases: this.idsBymethod(method), | ||||
|             } | ||||
|           } | ||||
|         } | ||||
|       }, | ||||
|       updateDiscrepancy() { | ||||
|         this.reconciliation.cash_discrepancy = (this.reconciliation.total_cash_purchases || 0 ) - (this.reconciliation.cash_taken || 0); | ||||
|       }, | ||||
| @@ -162,8 +176,9 @@ | ||||
|           .then(response => response.json()) | ||||
|           .then(data => { | ||||
|             this.summary.purchases = data; | ||||
|             this.reconciliation.cash_purchases = this.summary.purchases['CASH'].map(purchase => purchase.id); | ||||
|             this.reconciliation.cash_purchases = this.idsBymethod('CASH'); | ||||
|             this.reconciliation.total_cash_purchases = this.totalByMethod('CASH'); | ||||
|             this.processOtherMethods(); | ||||
|           }) | ||||
|           .catch(error => { | ||||
|             console.error(error); | ||||
| @@ -176,7 +191,7 @@ | ||||
|             const response = await fetch('/don_confiao/reconciliate_jar', { | ||||
|               method: 'POST', | ||||
|               headers: { | ||||
|               'Content-Type': 'application/json' | ||||
|                 'Content-Type': 'application/json' | ||||
|               }, | ||||
|               body: JSON.stringify(this.reconciliation), | ||||
|             }); | ||||
| @@ -190,7 +205,7 @@ | ||||
|             } | ||||
|           } catch (error) { | ||||
|             console.error('Error de red:', error); | ||||
|       } | ||||
|           } | ||||
|         } | ||||
|       }, | ||||
|     }, | ||||
|   | ||||
| @@ -165,6 +165,38 @@ class TestJarReconcliation(TestCase): | ||||
|         purchases = Sale.objects.filter(reconciliation_id=content['id']) | ||||
|         self.assertEqual(len(purchases), 2) | ||||
|  | ||||
|     def test_create_reconciliation_with_purchases_and_other_totals(self): | ||||
|         url = '/don_confiao/reconciliate_jar' | ||||
|         total_purchases = (11 * 72500) + (27 * 72500) | ||||
|         data = { | ||||
|             'date_time': '2024-12-02T21:07', | ||||
|             'reconcilier': 'carlos', | ||||
|             'total_cash_purchases': total_purchases, | ||||
|             'cash_taken': total_purchases, | ||||
|             'cash_discrepancy': 0, | ||||
|             'cash_purchases': [ | ||||
|                 self.purchase.id, | ||||
|                 self.purchase2.id, | ||||
|             ], | ||||
|             'other_totals': { | ||||
|                 'Confiar': { | ||||
|                     'total': (47 * 72500) + 1, | ||||
|                     'purchases': [self.purchase4.id], | ||||
|                 }, | ||||
|             }, | ||||
|         } | ||||
|         response = self.client.post(url, data=json.dumps(data).encode('utf-8'), | ||||
|                                     content_type='application/json') | ||||
|  | ||||
|         rawContent = response.content.decode('utf-8') | ||||
|         content = json.loads(rawContent) | ||||
|  | ||||
|         self.assertEqual(response.status_code, 200) | ||||
|         self.assertIn('id', content) | ||||
|  | ||||
|         purchases = Sale.objects.filter(reconciliation_id=content['id']) | ||||
|         self.assertEqual(len(purchases), 3) | ||||
|  | ||||
|     def _create_simple_reconciliation(self): | ||||
|         reconciliation = ReconciliationJar() | ||||
|         reconciliation.date_time = "2024-07-30" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user