Skip to content

Commit

Permalink
revisione formattazione documenti degli ordini
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Sep 10, 2023
1 parent 125ffbc commit 60d9ec9
Show file tree
Hide file tree
Showing 14 changed files with 685 additions and 518 deletions.
259 changes: 0 additions & 259 deletions code/app/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use App;
use Auth;
use DB;
use PDF;
use Mail;
use URL;
use Log;
Expand All @@ -26,8 +25,6 @@
use App\Models\Concerns\ExportableTrait;
use App\Models\Concerns\ReducibleTrait;
use App\Scopes\RestrictedGAS;
use App\Formatters\User as UserFormatter;
use App\Formatters\Order as OrderFormatter;
use App\Events\SluggableCreating;

class Order extends Model
Expand Down Expand Up @@ -489,262 +486,6 @@ public function calculateInvoicingSummary()
return $summary;
}

private function formatProduct($fields, $formattable, $product_redux, $product, $internal_offsets)
{
$ret = [];

if (is_null($product_redux) == false) {
if (!empty($product_redux->variants)) {
$offset = $internal_offsets->by_variant;

foreach ($product_redux->variants as $variant) {
if ($variant->$offset == 0) {
continue;
}

$row = [];
foreach($fields as $f) {
if (isset($formattable[$f])) {
if (isset($formattable[$f]->format_variant)) {
$row[] = call_user_func($formattable[$f]->format_variant, $product, $variant, $internal_offsets->alternate);
}
else {
$row[] = call_user_func($formattable[$f]->format_product, $product, $variant, $internal_offsets->alternate);
}
}
}

$ret[] = $row;
}

usort($ret, function($a, $b) {
return $a[0] <=> $b[0];
});
}
else {
$offset = $internal_offsets->by_product;
if ($product_redux->$offset != 0) {
$row = [];

foreach($fields as $f) {
if (isset($formattable[$f])) {
$row[] = call_user_func($formattable[$f]->format_product, $product, $product_redux, $internal_offsets->alternate);
}
}

$ret[] = $row;
}
}
}

return $ret;
}

/*
Questo serve a determinare quali valori prendere da prodotti e
prenotazioni a seconda che siano state chieste delle quantità prenotato
o consegnate
*/
private function offsetsByStatus($status)
{
if ($status == 'delivered') {
return (object)[
'alternate' => true,
'by_variant' => 'delivered',
'by_product' => 'delivered_pieces',
'by_booking' => 'delivered',
];
}
else {
return (object)[
'alternate' => false,
'by_variant' => 'quantity',
'by_product' => 'quantity_pieces',
'by_booking' => 'booked',
];
}
}

private function formatSummaryShipping($ret, $fields, $formattable, $internal_offsets, $shipping_place, $prepend)
{
$summary = $this->reduxData(['shipping_place' => $shipping_place]);

foreach ($this->products()->sorted()->get() as $product) {
$row = $this->formatProduct($fields, $formattable, $summary->products[$product->id] ?? null, $product, $internal_offsets);
if (!empty($row)) {
$final_row = [];

foreach($row as $r) {
$r = array_merge($prepend, $r);
$final_row[] = $r;
}

$ret->contents = array_merge($ret->contents, $final_row);
}
}

return $summary;
}

public function formatSummary($fields, $status, $shipping_place)
{
$ret = (object) [
'header' => [],
'contents' => []
];

$internal_offsets = $this->offsetsByStatus($status);
$formattable = OrderFormatter::formattableColumns('summary');

if ($shipping_place && $shipping_place == 'all_by_place') {
$ret->headers[] = _i('Luogo di Consegna');
$places = Delivery::orderBy('name', 'asc')->get();

$total_price_delivered = 0;
$total_price = 0;

foreach($places as $place) {
$prepend = [$place->name];
$summary = $this->formatSummaryShipping($ret, $fields, $formattable, $internal_offsets, $place->id, $prepend);
$total_price_delivered += $summary->price_delivered;
$total_price += $summary->price;
}
}
else {
$summary = $this->formatSummaryShipping($ret, $fields, $formattable, $internal_offsets, $shipping_place, []);
$total_price_delivered = $summary->price_delivered;
$total_price = $summary->price;
}

foreach($fields as $f) {
$ret->headers[] = $formattable[$f]->name;
}

if (in_array('price', $fields)) {
if ($shipping_place && $shipping_place == 'all_by_place') {
array_unshift($fields, 'shipping_place');
}

$row = array_fill(0, count($fields), '');

$row[0] = _i('Totale');
$price_offset = array_search('price', $fields);

if ($status == 'delivered') {
$row[$price_offset] = printablePrice($total_price_delivered);
}
else {
$row[$price_offset] = printablePrice($total_price);
}

$ret->contents[] = $row;
}

return $ret;
}

public function formatShipping($fields, $status, $shipping_place, $extra_modifiers)
{
$ret = (object) [
'headers' => $fields->headers,
'contents' => [],
];

$formattable_product = OrderFormatter::formattableColumns('shipping');
$internal_offsets = $this->offsetsByStatus($status);

$bookings = $this->topLevelBookings(null);
$bookings = Delivery::sortBookingsByShippingPlace($bookings, $shipping_place);
$listed_products = [];

$modifiers = $this->involvedModifiers(true);
$aggregate_data = $this->minimumRedux($modifiers);

foreach ($bookings as $booking) {
$obj = (object) [
'user_id' => $booking->user->id,

/*
Questi parametri vengono usati per riordinare le
prenotazioni rastrellate da diversi ordini, quando genero il
documento di Dettaglio Consegne per un aggregato
*/
'user_sorting' => $booking->user->lastname,
'gas_sorting' => $booking->user->gas_id,
'shipping_sorting' => $booking->user->shippingplace ? $booking->user->shippingplace->name : 'AAAA',

'user' => UserFormatter::format($booking->user, $fields->user_columns),
'products' => [],
'totals' => [],
'notes' => !empty($booking->notes) ? [$booking->notes] : [],
];

foreach($booking->products_with_friends as $booked) {
if (isset($listed_products[$booked->product_id])) {
$product = $listed_products[$booked->product_id];
$booked->setRelation('product', $product);
}
else {
$product = $booked->product;
$listed_products[$booked->product_id] = $product;
}

$summary = $booked->as_summary;

$row = $this->formatProduct($fields->product_columns, $formattable_product, $summary->products[$booked->product->id], $product, $internal_offsets);
if (!empty($row)) {
$obj->products = array_merge($obj->products, $row);
}
}

if (empty($obj->products)) {
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 = App\ModifiedValue::aggregateByType($modifiers);
foreach($aggregated_modifiers as $am) {
$obj->totals[$am->name] = printablePrice($am->amount);
$total_modifiers += $am->amount;
}

$obj->totals['total'] = $booking->getValue($internal_offsets->by_booking, true) + $total_modifiers;

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

$ret->contents[] = $obj;
}

return $ret;
}

public static function displayColumns()
{
$ret = [
Expand Down
Loading

0 comments on commit 60d9ec9

Please sign in to comment.