Skip to content

Commit

Permalink
dettaglio consegne: possibilità di scorporare utenti amici. closes #123
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Dec 1, 2024
1 parent f23e343 commit 3d36942
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
13 changes: 4 additions & 9 deletions code/app/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public function users(): HasMany

private static function sortByUserName($bookings)
{
usort($bookings, function($a, $b) {
return $a->user->printableName() <=> $b->user->printableName();
});

return $bookings;
return $bookings->sortBy(fn($b, $index) => $b->user->printableName());
}

private static function sortByPlace($bookings)
Expand Down Expand Up @@ -84,17 +80,16 @@ private static function sortByPlace($bookings)

public static function sortBookingsByShippingPlace($bookings, $shipping_place)
{
$bookings = collect($bookings);

if ($shipping_place == 0 || $shipping_place == 'all_by_name') {
$bookings = self::sortByUserName($bookings);
}
else if ($shipping_place == 'all_by_place') {
$bookings = self::sortByPlace($bookings);
}
else {
$tmp_bookings = array_filter($bookings, function($b) use ($shipping_place) {
return $b->shipping_place && $b->shipping_place->id == $shipping_place;
});

$tmp_bookings = $bookings->filter(fn($b) => $b->shipping_place && $b->shipping_place->id == $shipping_place);
$bookings = self::sortByUserName($tmp_bookings);
}

Expand Down
2 changes: 1 addition & 1 deletion code/app/Printers/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function handleShipping($obj, $request)

$temp_data = [];
foreach($obj->orders as $order) {
$temp_data[] = $this->formatShipping($order, $fields, $status, $shipping_place, true);
$temp_data[] = $this->formatShipping($order, $fields, $status, false, $shipping_place, true);
}

if (empty($temp_data)) {
Expand Down
48 changes: 33 additions & 15 deletions code/app/Printers/Concerns/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@

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

$modifiers = $booking->applyModifiersWithFriends($aggregate_data, false);
if ($isolate_friends) {
$modifiers = $booking->applyModifiers($aggregate_data, false);
}
else {
$modifiers = $booking->applyModifiersWithFriends($aggregate_data, false);
}

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

$aggregated_modifiers = ModifiedValue::aggregateByType($modifiers);
Expand All @@ -31,7 +37,7 @@ private function collectModifiersTotal($booking, $aggregate_data, $extra_modifie
prenotazione per far tornare i conti in fase di valutazione dei
modificatori
*/
private function fakeBookingStatus($status, $booking)
private function fakeBookingStatus($status, $isolate_friends, $booking)
{
$original_booking_status = null;

Expand All @@ -40,27 +46,31 @@ private function fakeBookingStatus($status, $booking)
$original_booking_status = $booking->status;
$booking->status = $status;

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

return $original_booking_status;
}

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

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

public function formatShipping($order, $fields, $status, $shipping_place, $extra_modifiers)
public function formatShipping($order, $fields, $status, $isolate_friends, $shipping_place, $extra_modifiers)
{
$ret = (object) [
'headers' => $fields->headers,
Expand All @@ -70,7 +80,15 @@ public function formatShipping($order, $fields, $status, $shipping_place, $extra
$formattable_product = OrderFormatter::formattableColumns('shipping');
$internal_offsets = $this->offsetsByStatus($status);

$bookings = $order->topLevelBookings(null);
if ($isolate_friends == false) {
$bookings = $order->topLevelBookings(null);
$products_source = 'products_with_friends';
}
else {
$bookings = $order->bookings;
$products_source = 'products';
}

$bookings = Delivery::sortBookingsByShippingPlace($bookings, $shipping_place);
$listed_products = [];

Expand All @@ -96,7 +114,7 @@ public function formatShipping($order, $fields, $status, $shipping_place, $extra
'notes' => !empty($booking->notes) ? [$booking->notes] : [],
];

foreach($booking->products_with_friends as $booked) {
foreach($booking->$products_source as $booked) {
if (isset($listed_products[$booked->product_id])) {
$product = $listed_products[$booked->product_id];
$booked->setRelation('product', $product);
Expand All @@ -118,13 +136,13 @@ public function formatShipping($order, $fields, $status, $shipping_place, $extra
continue;
}

$original_booking_status = $this->fakeBookingStatus($status, $booking);
$original_booking_status = $this->fakeBookingStatus($status, $isolate_friends, $booking);

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

$this->restoreBookingStatus($original_booking_status, $booking);
$this->restoreBookingStatus($original_booking_status, $isolate_friends, $booking);

$ret->contents[] = $obj;
}
Expand Down
8 changes: 7 additions & 1 deletion code/app/Printers/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ private function sendDocumentMail($request, $temp_file_path)
@unlink($temp_file_path);
}

/*
Se extra_modifiers == false (o non definito affatto): non contempla i
modificatori che hanno un tipo movimento contabile esplicito (e dunque
non sono destinati al fonitore)
*/
protected function handleShipping($obj, $request)
{
$action = $request['action'] ?? 'download';
$subtype = $request['format'] ?? 'pdf';
$status = $request['status'] ?? 'pending';
$isolate_friends = $request['isolate_friends'] ?? 0;
$extra_modifiers = $request['extra_modifiers'] ?? 0;
$required_fields = $request['fields'] ?? [];

Expand All @@ -70,7 +76,7 @@ protected function handleShipping($obj, $request)

$fields = splitFields($required_fields);

$data = $this->formatShipping($obj, $fields, $status, $shipping_place, $extra_modifiers);
$data = $this->formatShipping($obj, $fields, $status, $isolate_friends, $shipping_place, $extra_modifiers);

$title = _i('Dettaglio Consegne ordine %s presso %s', [$obj->internal_number, $obj->supplier->name]);
$filename = sanitizeFilename($title . '.' . $subtype);
Expand Down
6 changes: 6 additions & 0 deletions code/resources/views/order/exportshipping.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
<x-larastrap::checks name="fields" :label="_i('Colonne Prodotti')" :options="$options" :value="$currentgas->orders_shipping_product_columns" />

<x-larastrap::radios name="status" :label="_i('Stato Prenotazioni')" :options="['pending' => _i('Prenotate'), 'shipped' => _i('Consegnate')]" value="pending" />

@include('order.partials.export.modifiers', ['order' => $order])

@if(someoneCan('users.subusers'))
<x-larastrap::radios name="isolate_friends" :label="_i('Amici separati')" :options="['0' => _i('No'), '1' => _i('Sì')]" value="0" :pophelp="_i('Di default, le prenotazioni degli utenti \'amici\' vengono aggregate in quelle dei rispettivi utenti principali. Selezionando \'Sì\', vengono rappresentate nel documento come prenotazioni autonome.')" />
@endif

<x-larastrap::radios name="format" :label="_i('Formato')" :options="['pdf' => _i('PDF'), 'csv' => _i('CSV')]" value="pdf" />

@include('order.filesmail', ['contacts' => $order->supplier->involvedEmails()])
Expand Down

0 comments on commit 3d36942

Please sign in to comment.