#69 feat(ReconciliationJar): link purchases with other payment methods.

This commit is contained in:
Mono Mono 2024-12-28 17:01:51 -05:00
parent 9c0eebd07d
commit 5cfefdf91a
3 changed files with 72 additions and 8 deletions

View File

@ -65,7 +65,9 @@ class ReconciliateJarView(APIView):
status=HTTP_400_BAD_REQUEST status=HTTP_400_BAD_REQUEST
) )
reconciliation = serializer.save() 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({'id': reconciliation.id})
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST) 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) calculated_total = sum(p.get_total() for p in purchases)
return calculated_total == Decimal(total) return calculated_total == Decimal(total)
def _link_purchases(self, reconciliation, purchases): def _get_other_purchases(self, other_totals):
for purchase in purchases: 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.reconciliation = reconciliation
purchase.clean() purchase.clean()
purchase.save() purchase.save()

View File

@ -105,8 +105,6 @@
cash_taken: 0, cash_taken: 0,
cash_discrepancy: 0, cash_discrepancy: 0,
other_totals: { other_totals: {
confiar: 0,
bancolombia: 0
}, },
cash_purchases: [], cash_purchases: [],
}, },
@ -141,6 +139,22 @@
} }
return 0; 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() { updateDiscrepancy() {
this.reconciliation.cash_discrepancy = (this.reconciliation.total_cash_purchases || 0 ) - (this.reconciliation.cash_taken || 0); this.reconciliation.cash_discrepancy = (this.reconciliation.total_cash_purchases || 0 ) - (this.reconciliation.cash_taken || 0);
}, },
@ -162,8 +176,9 @@
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
this.summary.purchases = 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.reconciliation.total_cash_purchases = this.totalByMethod('CASH');
this.processOtherMethods();
}) })
.catch(error => { .catch(error => {
console.error(error); console.error(error);
@ -176,7 +191,7 @@
const response = await fetch('/don_confiao/reconciliate_jar', { const response = await fetch('/don_confiao/reconciliate_jar', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(this.reconciliation), body: JSON.stringify(this.reconciliation),
}); });
@ -190,7 +205,7 @@
} }
} catch (error) { } catch (error) {
console.error('Error de red:', error); console.error('Error de red:', error);
} }
} }
}, },
}, },

View File

@ -165,6 +165,38 @@ class TestJarReconcliation(TestCase):
purchases = Sale.objects.filter(reconciliation_id=content['id']) purchases = Sale.objects.filter(reconciliation_id=content['id'])
self.assertEqual(len(purchases), 2) 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): def _create_simple_reconciliation(self):
reconciliation = ReconciliationJar() reconciliation = ReconciliationJar()
reconciliation.date_time = "2024-07-30" reconciliation.date_time = "2024-07-30"