id; $deliveries['type'] = 1; self::store($deliveries); } $invoices = $data['invoices'] ?? false; if ($invoices && $invoices['zipcode'] && $invoices['city']) { $invoices['customer_id'] = $customer->id; $invoices['type'] = 2; self::store($invoices); } } public static function createByCustomer($customerId, $data) { self::addDeliveryAddress($customerId, $data); self::addInvoiceAddress($customerId, $data); } public static function addDeliveryAddress($customerId, $data) { $delivery = $data['use_for_delivery'] ?? false; $data = array_merge($data, [ 'address' => $delivery ? $data['delivery_address'] ?? $data['address'] : $data['address'], 'address2' => $delivery ? $data['delivery_address2'] ?? $data['address2'] : $data['address2'], 'zipcode' => $delivery ? $data['delivery_zipcode'] ?? $data['zipcode'] : $data['zipcode'], 'city' => $delivery ? $data['delivery_city'] ?? $data['city'] : $data['city'], ]); return self::addAddress($customerId, $data, 2); } public static function addInvoiceAddress($customerId, $data) { return self::addAddress($customerId, $data, 1); } public static function addAddress($customerId, $data, $type) { $name = $data['company'] ? $data['company'] : $data['first_name'].' '.$data['last_name']; $data = [ 'customer_id' => $customerId, 'type' => $type, 'name' => $name, 'address' => $data['address'], 'address2' => $data['address2'], 'zipcode' => $data['zipcode'], 'city' => $data['city'], ]; return self::store($data); } public static function getInvoiceAddress($customerId) { $addresses = CustomerAddress::byCustomer($customerId)->byInvoicing()->get(); return count($addresses) ? $addresses->first() : self::getByCustomer($customerId); } public static function getDeliveryAddress($customerId) { $addresses = CustomerAddress::byCustomer($customerId)->byDelivery()->get(); return count($addresses) ? $addresses->first() : self::getByCustomer($customerId); } public static function getByCustomer($customerId = false) { $customer = Customers::get($customerId); return $customer ? $customer->only(['address', 'adress2', 'zipcode', 'city']) : false; } public static function getIconByType($type) { return ((int) $type === 1) ? '' : ''; } public static function toggleActive($id, $active) { return self::update(['active' => $active], $id); } public static function getModel() { return CustomerAddress::query(); } }