diff --git a/app/Designs/AbstractDesign.php b/app/Designs/AbstractDesign.php new file mode 100644 index 000000000000..495482201937 --- /dev/null +++ b/app/Designs/AbstractDesign.php @@ -0,0 +1,27 @@ +header = $data['header']; + + $this->body = $data['body']; + + $this->table = $data['table']; + + $this->footer = $data['footer']; + + $this->table_styles = $data['table_styles']; + + } + + public function header() + { + + return $this->header; + + } + + public function body() + { + + return $this->body; + + } + + public function table_styles() + { + + return $this->table_styles; + + } + + public function table() + { + + return $this->table; + + } + + public function footer() + { + + return $this->footer; + + } + +} \ No newline at end of file diff --git a/app/Designs/Designer.php b/app/Designs/Designer.php index 6a4456f6d904..efbe702694d0 100644 --- a/app/Designs/Designer.php +++ b/app/Designs/Designer.php @@ -14,8 +14,7 @@ namespace App\Designs; use App\Models\Company; use App\Models\Invoice; -class Designer -{ +class Designer { protected $design; @@ -26,33 +25,32 @@ class Designer protected $html; private static $custom_fields = [ - 'invoice1', - 'invoice2', - 'invoice3', - 'invoice4', - 'surcharge1', - 'surcharge2', - 'surcharge3', - 'surcharge4', - 'client1', - 'client2', - 'client3', - 'client4', - 'contact1', - 'contact2', - 'contact3', - 'contact4', - 'company1', - 'company2', - 'company3', - 'company4', + 'invoice1', + 'invoice2', + 'invoice3', + 'invoice4', + 'surcharge1', + 'surcharge2', + 'surcharge3', + 'surcharge4', + 'client1', + 'client2', + 'client3', + 'client4', + 'contact1', + 'contact2', + 'contact3', + 'contact4', + 'company1', + 'company2', + 'company3', + 'company4', ]; - public function __construct($design, $input_variables) - { + public function __construct($design, $input_variables) { $this->design = $design; - $this->input_variables = (array)$input_variables; + $this->input_variables = (array) $input_variables; } /** @@ -60,23 +58,21 @@ class Designer * formatted HTML * @return string The HTML design built */ - public function build(Invoice $invoice) :Designer - { + public function build(Invoice $invoice):Designer { $this->exportVariables($invoice) - ->setDesign($this->getSection('header')) - ->setDesign($this->getSection('body')) - ->setDesign($this->getTable($invoice)) - ->setDesign($this->getSection('footer')); + ->setDesign($this->getSection('header')) + ->setDesign($this->getSection('body')) + ->setDesign($this->getTable($invoice)) + ->setDesign($this->getSection('footer')); return $this; } - public function getTable(Invoice $invoice) :string - { + public function getTable(Invoice $invoice):string { $table_header = $invoice->table_header($this->input_variables['table_columns'], $this->design->table_styles()); - $table_body = $invoice->table_body($this->input_variables['table_columns'], $this->design->table_styles()); + $table_body = $invoice->table_body($this->input_variables['table_columns'], $this->design->table_styles()); $data = str_replace('$table_header', $table_header, $this->getSection('table')); $data = str_replace('$table_body', $table_body, $data); @@ -85,13 +81,11 @@ class Designer } - public function getHtml() :string - { + public function getHtml():string { return $this->html; } - private function setDesign($section) - { + private function setDesign($section) { $this->html .= $section; @@ -99,48 +93,44 @@ class Designer } /** - * Returns the template section on with the + * Returns the template section on with the * stacked variables replaced with single variables. - * + * * @param string $section the method name to be executed ie header/body/table/footer * @return string The HTML of the template section */ - public function getSection($section) :string - { - return str_replace(array_keys($this->exported_variables), array_values($this->exported_variables), $this->design->{$section}()); + public function getSection($section):string { + return str_replace(array_keys($this->exported_variables), array_values($this->exported_variables), $this->design->{ $section}()); } - private function exportVariables($invoice) - { + private function exportVariables($invoice) { $company = $invoice->company; - $this->exported_variables['$client_details'] = $this->processVariables($this->processInputVariables($company, $this->input_variables['client_details']), $this->clientDetails($company)); - $this->exported_variables['$company_details'] = $this->processVariables($this->processInputVariables($company, $this->input_variables['company_details']), $this->companyDetails($company)); - $this->exported_variables['$company_address'] = $this->processVariables($this->processInputVariables($company, $this->input_variables['company_address']), $this->companyAddress($company)); + $this->exported_variables['$client_details'] = $this->processVariables($this->processInputVariables($company, $this->input_variables['client_details']), $this->clientDetails($company)); + $this->exported_variables['$company_details'] = $this->processVariables($this->processInputVariables($company, $this->input_variables['company_details']), $this->companyDetails($company)); + $this->exported_variables['$company_address'] = $this->processVariables($this->processInputVariables($company, $this->input_variables['company_address']), $this->companyAddress($company)); $this->exported_variables['$invoice_details_labels'] = $this->processLabels($this->processInputVariables($company, $this->input_variables['invoice_details']), $this->invoiceDetails($company)); - $this->exported_variables['$invoice_details'] = $this->processVariables($this->processInputVariables($company, $this->input_variables['invoice_details']), $this->invoiceDetails($company)); + $this->exported_variables['$invoice_details'] = $this->processVariables($this->processInputVariables($company, $this->input_variables['invoice_details']), $this->invoiceDetails($company)); return $this; } - private function processVariables($input_variables, $variables) :string - { + private function processVariables($input_variables, $variables):string { $output = ''; - foreach($input_variables as $value) - $output .= $variables[$value]; + foreach ($input_variables as $value) + $output .= $variables[$value]; return $output; } - private function processLabels($input_variables, $variables) :string - { + private function processLabels($input_variables, $variables):string { $output = ''; - foreach($input_variables as $value) { - + foreach ($input_variables as $value) { + $tmp = str_replace("", "_label", $variables[$value]); $output .= $tmp; @@ -156,19 +146,19 @@ class Designer // * $invoice_details // */ // $header = $this->design->header(); - + // /* // * $company_logo - full URL // * $client_details // */ // $body = $this->design->body(); - // /* + // /* // * $table_header // * $table_body // * $total_labels // * $total_values - // */ + // */ // $table = $this->design->table(); // /* @@ -178,132 +168,124 @@ class Designer // $footer = $this->design->footer(); // } - private function clientDetails(Company $company) - { + private function clientDetails(Company $company) { $data = [ - 'name' => '
$client.name
', - 'id_number' => '$client.id_number
', - 'vat_number' => '$client.vat_number
', - 'address1' => '$client.address1
', - 'address2' => '$client.address2
', + 'name' => '$client.name
', + 'id_number' => '$client.id_number
', + 'vat_number' => '$client.vat_number
', + 'address1' => '$client.address1
', + 'address2' => '$client.address2
', 'city_state_postal' => '$client.city_state_postal
', 'postal_city_state' => '$client.postal_city_state
', - 'country' => '$client.country
', - 'email' => '$client.email
', - 'client1' => '$client1
', - 'client2' => '$client2
', - 'client3' => '$client3
', - 'client4' => '$client4
', - 'contact1' => '$contact1
', - 'contact2' => '$contact2
', - 'contact3' => '$contact3
', - 'contact4' => '$contact4
', + 'country' => '$client.country
', + 'email' => '$client.email
', + 'client1' => '$client1
', + 'client2' => '$client2
', + 'client3' => '$client3
', + 'client4' => '$client4
', + 'contact1' => '$contact1
', + 'contact2' => '$contact2
', + 'contact3' => '$contact3
', + 'contact4' => '$contact4
', ]; return $this->processCustomFields($company, $data); } - private function companyDetails(Company $company) - { + private function companyDetails(Company $company) { $data = [ 'company_name' => '$company.company_name', - 'id_number' => '$company.id_number', - 'vat_number' => '$company.vat_number', - 'website' => '$company.website', - 'email' => '$company.email', - 'phone' => '$company.phone', - 'company1' => '$company1', - 'company2' => '$company2', - 'company3' => '$company3', - 'company4' => '$company4', + 'id_number' => '$company.id_number', + 'vat_number' => '$company.vat_number', + 'website' => '$company.website', + 'email' => '$company.email', + 'phone' => '$company.phone', + 'company1' => '$company1', + 'company2' => '$company2', + 'company3' => '$company3', + 'company4' => '$company4', ]; return $this->processCustomFields($company, $data); } - private function companyAddress(Company $company) - { + private function companyAddress(Company $company) { $data = [ - 'address1' => '$company.address1', - 'address2' => '$company.address1', + 'address1' => '$company.address1', + 'address2' => '$company.address1', 'city_state_postal' => '$company.city_state_postal', 'postal_city_state' => '$company.postal_city_state', - 'country' => '$company.country', - 'company1' => '$company1', - 'company2' => '$company2', - 'company3' => '$company3', - 'company4' => '$company4', + 'country' => '$company.country', + 'company1' => '$company1', + 'company2' => '$company2', + 'company3' => '$company3', + 'company4' => '$company4', ]; return $this->processCustomFields($company, $data); } - private function invoiceDetails(Company $company) - { + private function invoiceDetails(Company $company) { $data = [ 'invoice_number' => '$invoice_number', - 'po_number' => '$po_number', - 'date' => '$date', - 'due_date' => '$due_date', - 'balance_due' => '$balance_due', - 'invoice_total' => '$invoice_total', - 'partial_due' => '$partial_due', - 'invoice1' => '$invoice1', - 'invoice2' => '$invoice2', - 'invoice3' => '$invoice3', - 'invoice4' => '$invoice4', - 'surcharge1' =>'$surcharge1', - 'surcharge2' =>'$surcharge2', - 'surcharge3' =>'$surcharge3', - 'surcharge4' =>'$surcharge4', + 'po_number' => '$po_number', + 'date' => '$date', + 'due_date' => '$due_date', + 'balance_due' => '$balance_due', + 'invoice_total' => '$invoice_total', + 'partial_due' => '$partial_due', + 'invoice1' => '$invoice1', + 'invoice2' => '$invoice2', + 'invoice3' => '$invoice3', + 'invoice4' => '$invoice4', + 'surcharge1' => '$surcharge1', + 'surcharge2' => '$surcharge2', + 'surcharge3' => '$surcharge3', + 'surcharge4' => '$surcharge4', ]; - return $this->processCustomFields($company, $data); } - private function processCustomFields(Company $company, $data) - { + private function processCustomFields(Company $company, $data) { $custom_fields = $company->custom_fields; - foreach(self::$custom_fields as $cf) - { + if (!$custom_fields) { + return []; + } - if(!property_exists($custom_fields, $cf) || (strlen($custom_fields->{$cf}) == 0)) + foreach (self::$custom_fields as $cf) { + + if (!property_exists($custom_fields, $cf) || (strlen($custom_fields->{ $cf}) == 0)) { unset($data[$cf]); + } } return $data; } - private function processInputVariables($company, $variables) - { + private function processInputVariables($company, $variables) { $custom_fields = $company->custom_fields; $matches = array_intersect(self::$custom_fields, $variables); - foreach($matches as $match) - { + foreach ($matches as $match) { - if(!property_exists($custom_fields, $match) || (strlen($custom_fields->{$match}) == 0)) - { - foreach($variables as $key => $value) - { - if($value == $match) - { + if (!property_exists($custom_fields, $match) || (strlen($custom_fields->{ $match}) == 0)) { + foreach ($variables as $key => $value) { + if ($value == $match) { unset($variables[$key]); } } } - } return $variables; diff --git a/app/Designs/Modern.php b/app/Designs/Modern.php index 11091cc1990b..c1fa9d1d1d3a 100644 --- a/app/Designs/Modern.php +++ b/app/Designs/Modern.php @@ -11,15 +11,13 @@ namespace App\Designs; -class Modern +class Modern extends AbstractDesign { - public function __construct() - { - } + public function __construct() { + } - public function header() - { + public function header() { return ' @@ -35,7 +33,7 @@ class Modern - +$terms_label
$terms