Skip to content

Commit

Permalink
fix assortiti esportazione documenti ordini
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Nov 20, 2023
1 parent 2af56e7 commit 03cb11e
Show file tree
Hide file tree
Showing 28 changed files with 301 additions and 107 deletions.
25 changes: 16 additions & 9 deletions code/app/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,10 @@ public function scopeAngryload($query)
]);
}

private function localModifiedValues($id, $with_friends)
private function localModifiedValues($id)
{
$values = $this->modifiedValues;

if ($with_friends) {
foreach($this->friends_bookings as $friend) {
$values = $values->merge($friend->localModifiedValues($id, true));
}
}

if ($id) {
$values = $values->filter(function($i) use ($id) {
return $i->modifier_id == $id;
Expand All @@ -128,7 +122,7 @@ private function localModifiedValues($id, $with_friends)

public function allModifiedValues($id, $with_friends)
{
$values = $this->localModifiedValues($id, false);
$values = $this->localModifiedValues($id);

$products = $this->products;
$values = $products->reduce(function($carry, $product) {
Expand Down Expand Up @@ -472,6 +466,19 @@ public function getShowURL()
return route('booking.user.show', ['booking' => $this->order->aggregate_id, 'user' => $this->user_id]);
}

public function unsetModifiedValues()
{
$this->unsetRelation('modifiedValues');

foreach($this->products as $prod) {
$prod->unsetRelation('modifiedValues');
}

foreach($this->friends_bookings as $friend) {
$friend->unsetModifiedValues();
}
}

public function wipeStatus()
{
if ($this->payment) {
Expand All @@ -483,7 +490,7 @@ public function wipeStatus()
$this->deleteModifiedValues();

$this->unsetRelation('payment');
$this->unsetRelation('modifiedValues');
$this->unsetModifiedValues();
$this->unsetRelation('products');
}

Expand Down
2 changes: 1 addition & 1 deletion code/app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function revisioned(Request $request, $id)

public function promote(Request $request, $id)
{
return $this->easyExecute(function() use ($id, $request) {
return $this->easyExecute(function() use ($id) {
$subject = $this->service->promoteFriend($id);
return $this->commonSuccessResponse($subject);
});
Expand Down
8 changes: 4 additions & 4 deletions code/app/Jobs/NotifyClosedOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private function dispatchToSupplier($order)
I files vengono già rimossi dopo l'invio della
notifica al fornitore
*/
$pdf_file_path = $printer->document($order, 'summary', ['format' => 'pdf', 'status' => 'booked', 'extra_modifiers' => 0, 'send_mail' => true]);
$csv_file_path = $printer->document($order, 'summary', ['format' => 'csv', 'status' => 'booked', 'extra_modifiers' => 0, 'send_mail' => true]);
$pdf_file_path = $printer->document($order, 'summary', ['format' => 'pdf', 'status' => 'pending', 'extra_modifiers' => 0, 'send_mail' => true]);
$csv_file_path = $printer->document($order, 'summary', ['format' => 'csv', 'status' => 'pending', 'extra_modifiers' => 0, 'send_mail' => true]);
$order->supplier->notify(new SupplierOrderShipping($gas, $order, $pdf_file_path, $csv_file_path));

$this->hub->enable(true);
Expand Down Expand Up @@ -69,8 +69,8 @@ protected function realHandle()
Nota: il flag send_mail serve solo a farsi restituire il
path del file generato. Cfr. il TODO in Order::document()
*/
$pdf_file_path = $printer->document($order, 'summary', ['format' => 'pdf', 'status' => 'booked', 'send_mail' => true]);
$csv_file_path = $printer->document($order, 'summary', ['format' => 'csv', 'status' => 'booked', 'send_mail' => true]);
$pdf_file_path = $printer->document($order, 'summary', ['format' => 'pdf', 'status' => 'pending', 'send_mail' => true]);
$csv_file_path = $printer->document($order, 'summary', ['format' => 'csv', 'status' => 'pending', 'send_mail' => true]);

$all_files[] = $pdf_file_path;
$all_files[] = $csv_file_path;
Expand Down
11 changes: 11 additions & 0 deletions code/app/Models/Concerns/ManagesInnerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ private function globalKey($name)
return sprintf('%s_%s', $this->id, $name);
}

protected function hasInnerCache($name)
{
if ($this->uses_global_cache) {
$name = $this->globalKey($name);
return $this->global_store->has($name);
}
else {
return isset($this->inner_runtime_cache[$name]);
}
}

protected function innerCache($name, $function)
{
if ($this->uses_global_cache) {
Expand Down
29 changes: 24 additions & 5 deletions code/app/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ public function notifiableUsers($gas)

public function angryBookings()
{
$had_cache = $this->hasInnerCache('angry_bookings');

$bookings = $this->innerCache('angry_bookings', function($obj) {
$bookings = $obj->bookings()->angryload()->get();

Expand All @@ -401,6 +403,24 @@ public function angryBookings()
return $bookings;
});

/*
Questa funzione viene usata in particolare in applyModifiers(),
talvolta in due invocazioni consecutive per ottenere i modificatori
del prenotato e del consegnato. Ma la prima invocazione popola i
modificatori all'interno dell'albero delle prenotazioni dell'ordine,
e la seconda - trovandoli già a posto - li riusa impropriamente. Il
risultato è che i modificatori del consegnato risultano gli stessi
del prenotato.
Anziché rinunciare completamente alla cache aggressiva sulle
prenotazioni, preferisco invalidare e ricalcolare solo i
modificatori quando necessario
*/
if ($had_cache) {
foreach($bookings as $booking) {
$booking->unsetModifiedValues();
}
}

$this->setRelation('bookings', $bookings);
return $bookings;
}
Expand Down Expand Up @@ -635,7 +655,8 @@ public function involvedModifiers($include_shipping_places = false)

public function applyModifiers($aggregate_data = null, $enforce_status = false)
{
$modifiers = $this->involvedModifiers(true);
$modifiers = new Collection();
$order_modifiers = $this->involvedModifiers(true);

/*
TODO: se ci sono prenotazioni consegnate, non posso fidarmi
Expand All @@ -651,15 +672,13 @@ public function applyModifiers($aggregate_data = null, $enforce_status = false)
*/
$has_shipped_bookings = $this->bookings->where('status', '!=', 'pending')->count() != 0;

if ($modifiers->isEmpty() == false || $has_shipped_bookings) {
if ($order_modifiers->isEmpty() == false || $has_shipped_bookings) {
DB::beginTransaction();

if (is_null($aggregate_data)) {
$aggregate_data = $this->minimumRedux($modifiers);
$aggregate_data = $this->minimumRedux($order_modifiers);
}

$modifiers = new Collection();

$old_status = $this->status;
if ($enforce_status !== false) {
$this->status = $enforce_status;
Expand Down
6 changes: 3 additions & 3 deletions code/app/Printers/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function handleShipping($obj, $request)
$required_fields = $request['fields'] ?? [];

$fields = splitFields($required_fields);
$status = $request['status'] ?? 'booked';
$status = $request['status'] ?? 'pending';
$shipping_place = $request['shipping_place'] ?? 'all_by_name';

$temp_data = [];
Expand Down Expand Up @@ -149,7 +149,7 @@ private function handleGDXP($obj)
diverse iterazioni sovrascrivono sempre lo
stesso file
*/
$f = $printer->document($order, 'summary', ['format' => 'gdxp', 'status' => 'booked']);
$f = $printer->document($order, 'summary', ['format' => 'gdxp', 'status' => 'pending']);
$new_f = Str::random(10);
rename($f, $new_f);
$files[] = $new_f;
Expand Down Expand Up @@ -255,7 +255,7 @@ private function formatTableRows($aggregate, $shipping_place, $status, $fields,

protected function handleTable($obj, $request)
{
$status = $request['status'] ?? 'booked';
$status = $request['status'] ?? 'pending';
$shipping_place = $request['shipping_place'] ?? 0;

$required_fields = $request['fields'] ?? [];
Expand Down
17 changes: 17 additions & 0 deletions code/app/Printers/Concerns/OrderPrintType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Printers\Concerns;

trait OrderPrintType
{
protected function filterExtraModifiers($modifiers, $extras)
{
if ($extras == false) {
$modifiers = $modifiers->filter(function($mod) {
return is_null($mod->modifier->movementType);
});
}

return $modifiers;
}
}
4 changes: 2 additions & 2 deletions code/app/Printers/Concerns/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait Orders

protected function bookingsRules($status)
{
if ($status == 'saved' || $status == 'delivered') {
if ($status == 'saved' || $status == 'shipped') {
$get_total = 'delivered';
$get_function = 'getDeliveredQuantity';
}
Expand All @@ -27,7 +27,7 @@ protected function bookingsRules($status)
*/
protected static function offsetsByStatus($status)
{
if ($status == 'delivered') {
if ($status == 'shipped') {
return (object)[
'alternate' => true,
'by_variant' => 'delivered',
Expand Down
95 changes: 62 additions & 33 deletions code/app/Printers/Concerns/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,64 @@

trait Shipping
{
use OrderPrintType;

private function collectModifiersTotal($booking, $aggregate_data, $extra_modifiers)
{
$total_modifiers = 0;
$labels = [];

$modifiers = $booking->applyModifiers($aggregate_data, false);
foreach($booking->friends_bookings as $friend) {
$friend_modifiers = $friend->applyModifiers($aggregate_data, false);
$modifiers = $modifiers->merge($friend_modifiers);
}

$modifiers = $this->filterExtraModifiers($modifiers, $extra_modifiers);

$aggregated_modifiers = ModifiedValue::aggregateByType($modifiers);
foreach($aggregated_modifiers as $am) {
$labels[$am->name] = printablePrice($am->amount);
$total_modifiers += $am->amount;
}

return [$labels, $total_modifiers];
}

/*
All'occorrenza, qui "falsifico" temporaneamente lo stato della
prenotazione per far tornare i conti in fase di valutazione dei
modificatori
*/
private function fakeBookingStatus($status, $booking)
{
$original_booking_status = null;

if ($status == 'pending') {
if ($booking->status == 'shipped' || $booking->status == 'saved') {
$original_booking_status = $booking->status;
$booking->status = $status;

foreach($booking->friends_bookings as $friend) {
$friend->status = $status;
}
}
}

return $original_booking_status;
}

private function restoreBookingStatus($original_booking_status, $booking)
{
if ($original_booking_status != null) {
$booking->status = $original_booking_status;

foreach($booking->friends_bookings as $friend) {
$friend->status = $original_booking_status;
}
}
}

public function formatShipping($order, $fields, $status, $shipping_place, $extra_modifiers)
{
$ret = (object) [
Expand Down Expand Up @@ -67,42 +125,13 @@ public function formatShipping($order, $fields, $status, $shipping_place, $extra
continue;
}

/*
All'occorrenza, qui "falsifico" temporaneamente lo stato della
prenotazione per far tornare i conti in fase di valutazione dei
modificatori
*/
$original_booking_status = null;
if (($booking->status == 'shipped' || $booking->status == 'saved') && $status == 'booked') {
$original_booking_status = $booking->status;
$booking->status = 'pending';
}

$modifiers = $booking->applyModifiers($aggregate_data, false);

foreach($booking->friends_bookings as $friend) {
$friend_modifiers = $friend->applyModifiers($aggregate_data, false);
$modifiers = $modifiers->merge($friend_modifiers);
}

if ($extra_modifiers == false) {
$modifiers = $modifiers->filter(function($mod) {
return is_null($mod->modifier->movementType);
});
}

$total_modifiers = 0;
$aggregated_modifiers = ModifiedValue::aggregateByType($modifiers);
foreach($aggregated_modifiers as $am) {
$obj->totals[$am->name] = printablePrice($am->amount);
$total_modifiers += $am->amount;
}
$original_booking_status = $this->fakeBookingStatus($status, $booking);

list($labels_modifiers, $total_modifiers) = $this->collectModifiersTotal($booking, $aggregate_data, $extra_modifiers);
$obj->totals = array_merge($obj->totals, $labels_modifiers);
$obj->totals['total'] = $booking->getValue($internal_offsets->by_booking, true) + $total_modifiers;

if ($original_booking_status != null) {
$booking->status = $original_booking_status;
}
$this->restoreBookingStatus($original_booking_status, $booking);

$ret->contents[] = $obj;
}
Expand Down
Loading

0 comments on commit 03cb11e

Please sign in to comment.