File: /var/www/ipsremont-demo/app/Models/Order.php
<?php
namespace App\Models;
use App\Helpers\CurrencyHelper;
use App\Helpers\UserHelper;
use App\Models\Repair\Repair;
use App\Models\Service\Service;
use App\Services\Status\StatusService;
use App\Traits\Displayed;
use App\Traits\My;
use App\Traits\Sortable;
use App\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
/**
* @property int $id
* @property int $user_id
* @property int $service_id
* @property int $branch_id
* @property string $status
* @property int $created_by_manager
* @property string $external_id
* @property string $name
* @property string $email
* @property string $phone
* @property string $repair
* @property string $additional_info
* @property string $deleted_at
* @property string $created_at
* @property string $updated_at
* @property float $price
* @property int $repair_id
* @property int $bottom
* @property string $country
* @property string $city
* @property string $address
* @property string $last_1c_date
* @property int $errors
* @property string $error_text
* @property int $is_editable
* @property int $dispatch
* @property string $invoice
*
* @property OrderItems[] $order_items
* @property Service $service
*/
class Order extends BaseModel
{
use SoftDeletes, Sortable, Displayed, My;
const STATUS_NEW = 'new'; // Новая
const STATUS_AWAITING_CONFIRMATION = 'awaiting_confirmation'; // Ожидает подтверждения
const STATUS_CONFIRMED = 'confirmed'; // Подтверждено
const STATUS_RESERVED = 'reserved'; // Зарезервировано
const STATUS_PARTIALLY_SHIPPED = 'partially_shipped'; // Частично отгружено
const STATUS_SHIPPED = 'shipped'; // Отгружено
const STATUS_DELIVERY = 'delivery'; // Доставка
const STATUS_COMPLETED = 'completed'; // Завершена
const STATUS_CANCELED = 'canceled'; // Отменена
const REPAIR_WARRANTY = 'warranty';
const REPAIR_PAID = 'paid';
protected $table = 'orders';
protected $_status = null;
protected $fillable = [
'external_id',
'name',
'email',
'phone',
'status',
'repair',
'repair_id',
'additional_info',
];
protected static $labels = [
'user_id' => 'orders.user_id',
'service_id' => 'orders.service_id',
'branch_id' => 'orders.branch_id',
'created_by_manager' => 'orders.created_by_manager',
'name' => 'orders._name',
'email' => 'orders.email',
'phone' => 'orders.phone',
'status' => 'orders.status',
'price' => 'orders.price',
'parts' => 'orders.selectParts',
'bottom' => 'orders.bottom',
'repair_id' => 'orders.repair_id',
'address_id' => 'orders.address',
'external_id' => 'orders.external_id',
];
public static function columns()
{
$columns = [];
$columns['fields']['id'] = [
'displayName' => 'orders.id',
'field' => 'id',
'sort' => true,
'class' => 'w-200px',
'sortType' => 'asc',
];
$columns['fields']['shipment_id'] = [
'displayName' => 'shipment.id',
'field' => 'shipment_id',
'sort' => false,
'class' => 'w-200px',
];
$user = UserHelper::getUser();
$branches = $user->getBranchIds();
if (can(Permission::branchesAll) || count($branches) > 1) {
$columns['fields']['branch_id'] = [
'displayName' => 'orders.branch_id',
'field' => 'branch_id',
'sort' => true,
'sortType' => 'asc',
];
}
if (!$user->isService()) {
$columns['fields']['service_id'] = [
'displayName' => 'orders.service_id',
'field' => 'service_id',
'sort' => true,
'sortType' => 'asc',
];
}
if ($user->isService()) {
$columns['fields']['repair_id'] = [
'displayName' => 'orders.repair_id',
'field' => 'repair_id',
'sort' => true,
'sortType' => 'desc',
];
}
$columns['fields']['status'] = [
'displayName' => 'orders.status',
'field' => 'status',
'sort' => true,
'sortType' => 'asc',
];
$columns['fields']['repair'] = [
'displayName' => 'orders.repair',
'field' => 'repair',
'sort' => true,
'sortType' => 'asc',
];
$columns['fields']['price'] = [
'displayName' => 'orders.price',
'field' => 'price',
'sort' => true,
'class' => 'text-right w-100px',
'sortType' => 'asc',
];
$columns['fields']['createdAt'] = [
'displayName' => 'orders.createdAt',
'field' => 'created_at',
'sort' => true,
'sortType' => 'asc',
];
$columns['fields']['updatedAt'] = [
'displayName' => 'orders.updatedAt',
'field' => 'updated_at',
'sort' => true,
'sortType' => 'asc',
];
$columns['fields']['actions'] = [
'displayName' => '',
'field' => 'action',
'sort' => false,
];
$columns['sortDefault']['bottom'] = [
'field' => 'bottom',
'sort' => 'asc',
];
$columns['sortDefault']['created_at'] = [
'field' => 'created_at',
'sort' => 'desc',
];
return $columns;
}
public static function cart_columns($type = '')
{
$columns = [];
$columns['fields']['name'] = [
'displayName' => 'orders.partName',
'field' => 'name',
'sort' => false,
'class' => ($type == 'create') ? 'w-50' : 'w-75',
'sortType' => 'asc',
];
$columns['fields']['amount'] = [
'displayName' => 'orders.amount',
'field' => 'amount',
'sort' => false,
'class' => 'text-center w-200px',
'sortType' => 'asc',
];
$columns['fields']['price'] = [
'displayName' => 'orders.price',
'field' => 'price',
'sort' => false,
'class' => 'text-right w-200px',
'sortType' => 'asc',
];
if ($type == 'create') {
$columns['fields']['actions'] = [
'displayName' => '',
'field' => 'action',
'class' => 'w-200px',
'sort' => false,
];
}
return $columns;
}
public static function shipment_columns($type = '')
{
$columns = [];
$columns["fields"]["name"] = [
"displayName" => 'orders.partName',
"field" => "name",
"sort" => false,
"class" => ($type == 'create') ? 'w-50' : 'w-75',
"sortType" => 'asc',
];
$columns["fields"]["amount"] = [
"displayName" => 'orders.amount',
"field" => "amount",
"sort" => false,
"class" => 'text-center w-200px',
"sortType" => 'asc',
];
return $columns;
}
public function getNumber()
{
return '#' . sprintf("%'.06d", $this->id);
}
public function getId()
{
return $this->id;
}
public function getUserId()
{
return $this->user_id;
}
public function getService()
{
return ($this->service->name) ?? '';
}
public function getBranch()
{
return ($this->branch->name) ?? '';
}
public function getExternalId()
{
return $this->external_id;
}
public function getName()
{
return $this->name;
}
public function getEmail()
{
return $this->email;
}
public function getPhone()
{
return $this->phone;
}
public function getStatus()
{
return $this->status;
}
public function isAllowEdit(): bool
{
$user = UserHelper::getUser();
if (is_null($user)) {
return false;
}
$roleAllow = $user->isService() || $user->isManager();
$statusAllow = in_array($this->status, [self::STATUS_NEW, self::STATUS_AWAITING_CONFIRMATION]);
return $roleAllow && $statusAllow && $this->is_editable;
}
public function getRepair()
{
return $this->repair;
}
public function getPrice()
{
return $this->price;
}
public function getCountry()
{
return $this->country;
}
public function getCity()
{
return $this->city;
}
public function getAddress()
{
return $this->address;
}
public function getSelectedAddressId()
{
$address = Address::where('country', $this->country)->where('city', $this->city)
->where('address', $this->address)->first();
return ($address) ? $address->id : '';
}
public function getAdditionalInfo()
{
return $this->additional_info;
}
public function branch()
{
return $this->belongsTo(Branch::class);
}
public function stat()
{
return $this->belongsTo(Statuses::class, 'status', 'slug');
}
public function order_changes()
{
return $this->hasMany(OrderChanges::class)->orderBy('order_changes.id', 'desc');
}
public function shipment()
{
return $this->hasMany(Shipment::class);
}
public function status()
{
if (!$this->_status) {
$this->_status = StatusService::getOrderBySlug($this->status);
}
return $this->_status;
}
public function user()
{
return $this->belongsTo(User::class);
}
public function _save($request, $total_price, $address = null)
{
$this->user_id = Auth::user()->id;
$this->service_id = (Auth::user()->isManager()) ? $request->service_id : Auth::user()->getRealServiceId();
$this->branch_id = Service::where('id', $this->service_id)->first()->branch_id;
$this->created_by_manager = Auth::user()->isManager();
$this->name = (Auth::user()->isService()) ? $request->name : '';
$this->email = (Auth::user()->isService()) ? $request->email : '';
$this->phone = (Auth::user()->isService()) ? $request->phone : '';
$this->repair = $request->repair;
$this->additional_info = ($request->additional_info) ? $request->additional_info : '';
$this->status = 'new';
$this->price = $total_price;
$this->repair_id = $request->repair_id;
$this->country = $address->country ?? '';
$this->city = $address->city ?? '';
$this->address = $address->address ?? '';
$this->save();
}
public function save1c($item)
{
$service = Service::where('external_id', $item['service_code'])->first();
$branch = Branch::where('external_id', $item['branch_code'])->first();
$this->service_id = $service->id;
$this->branch_id = $branch->id;
$this->external_id = $item['code'];
$this->status = $item['status'];
$this->created_by_manager = $item['created_by_manager'];
$this->repair = $item['repair'];
$this->name = $item['name'];
$this->email = $item['email'];
$this->phone = $item['phone'];
$this->country = $item['country'];
$this->city = $item['city'];
$this->address = $item['address'];
$this->additional_info = $item['additional_info'];
$this->price = $item['price'];
$this->created_at = $item['created_at'];
$this->invoice = $item['invoice'];
$this->user_id = 1;
return $this;
}
public function _update($request, $address = null)
{
if (!$this->canByStatus()) {
$old_status = $this->status;
}
if ($this->external_id) {
$this->dispatch = 1;
}
$fields = $request->all();
$this->fill($fields);
if (isset($old_status)) {
$this->status = $old_status;
}
if ($address) {
$this->country = $address->country;
$this->city = $address->city;
$this->address = $address->address;
}
$this->update();
}
public function repairItem()
{
return $this->belongsTo(Repair::class, 'repair_id', 'id');
}
public function canByStatus()
{
return $this->status != 'completed' && $this->status != 'canceled';
}
public function getShipmentNumbers()
{
$shipment = $this->shipment()->get();
$names = [];
foreach ($shipment as $value) {
$names[$value->id] = $value->getNumber();
}
return $names;
}
public function getPriceConverted(bool $formatted = true)
{
$date = $this->isAllowEdit() ? null : $this->updated_at;
return CurrencyHelper::getPriceConverted($this->price, $date, $formatted);
}
// MARK: - Relations
public function order_items(): HasMany
{
return $this->hasMany(OrderItems::class);
}
public function service(): BelongsTo
{
return $this->belongsTo(Service::class);
}
}