234 lines
6.6 KiB
PHP
234 lines
6.6 KiB
PHP
<?php
|
|
|
|
class PreorderBillingInvoiceFile extends mfBaseModel {
|
|
private $file;
|
|
private $creator;
|
|
private $editor;
|
|
|
|
public function getProperty($name) {
|
|
if($this->$name == null) {
|
|
|
|
if(!$this->id) {
|
|
return null;
|
|
}
|
|
|
|
if($name == "creator") {
|
|
$this->creator = new User($this->create_by);
|
|
return $this->creator;
|
|
}
|
|
|
|
if($name == "editor") {
|
|
$this->editor = new User($this->edit_by);
|
|
return $this->editor;
|
|
}
|
|
|
|
$classname = ucfirst($name);
|
|
$idfield = $name."_id";
|
|
$this->$name = new $classname($this->$idfield);
|
|
|
|
if($this->$name->id) {
|
|
return $this->$name;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return $this->$name;
|
|
}
|
|
|
|
/********************************
|
|
* Begin static Model functions
|
|
*/
|
|
|
|
public static function create(Array $data) {
|
|
$model = new PreorderBillingInvoiceFile();
|
|
|
|
$table_fields = [
|
|
"invoice_id", "file_id", "name", "description",
|
|
"create_by","edit_by","create","edit"
|
|
];
|
|
|
|
foreach($data as $field => $value) {
|
|
if(in_array($field, $table_fields)) {
|
|
$model->$field = $value;
|
|
}
|
|
}
|
|
|
|
$me = new User();
|
|
$me->loadMe();
|
|
|
|
if($model->create_by === null) {
|
|
$model->create_by = $me->id;
|
|
}
|
|
if($model->edit_by === null) {
|
|
$model->edit_by = $me->id;
|
|
}
|
|
|
|
return $model;
|
|
}
|
|
|
|
public static function createFromInvoice(PreorderBillingInvoice $invoice) {
|
|
$log = mfLoghandler::singleton();
|
|
$invoice_path_base = MFUPLOAD_FILE_SAVE_PATH."/".TT_PREORDER_BILLING[$invoice->netowner_id]["subfolder"];
|
|
|
|
$invoice_date = new DateTime($invoice->invoice_date);
|
|
$year = $invoice_date->format("Y");
|
|
$invoice_subfolder = TT_PREORDER_BILLING[$invoice->netowner_id]["subfolder"]."/$year";
|
|
$invoice_path = "$invoice_path_base/$year";
|
|
|
|
|
|
if(!file_exists($invoice_path)) {
|
|
mkdir($invoice_path, 0777, true);
|
|
}
|
|
|
|
// create PDF
|
|
$tmp_filename = $invoice->createPdf();
|
|
if(!$tmp_filename) {
|
|
$log->debug("Error creating PDF file");
|
|
return false;
|
|
}
|
|
|
|
$new_filename = $invoice->invoice_number.".pdf";
|
|
|
|
// move pdf to correct folder
|
|
$log->debug(__METHOD__.": invoice path: $invoice_path");
|
|
$log->debug(__METHOD__.": new filename: $invoice_path/$new_filename");
|
|
if(!rename($tmp_filename, "$invoice_path/$new_filename")) {
|
|
$log->debug(__METHOD__.": Error moving created PDF file");
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
// create File
|
|
$file = FileModel::create([
|
|
"name" => $invoice->invoice_number,
|
|
"filename" => $new_filename,
|
|
"subfolder" => $invoice_subfolder,
|
|
"store_filename" => $new_filename,
|
|
"orig_filename" => $new_filename,
|
|
]);
|
|
|
|
if (!$file->save()) {
|
|
$log->error(__METHOD__ . ": Error saving PDF file");
|
|
return false;
|
|
}
|
|
} catch(Exception $e) {
|
|
$log->error(__METHOD__ . ": Exception while creating File object");
|
|
throw($e);
|
|
}
|
|
|
|
// create PreorderBillingInvoiceFile
|
|
$ifile = PreorderBillingInvoiceFile::create([
|
|
"invoice_id" => $invoice->id,
|
|
"file_id" => $file->id,
|
|
"name" => $new_filename,
|
|
"description" => ""
|
|
]);
|
|
|
|
if(!$ifile->save()) {
|
|
$log->error(__METHOD__.": Error saving PDF Invoice file");
|
|
return false;
|
|
}
|
|
|
|
return $ifile;
|
|
}
|
|
|
|
public static function getAll() {
|
|
$items = [];
|
|
|
|
$db = FronkDB::singleton();
|
|
|
|
$res = $db->select("PreorderBillingInvoiceFile", "*", "1=1 ORDER BY name");
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new PreorderBillingInvoiceFile($data);
|
|
}
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
public static function getFirst($filter = []) {
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$res = $db->select("PreorderBillingInvoiceFile", "*", "$where ORDER BY name");
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new PreorderBillingInvoiceFile($data);
|
|
if($item->id) {
|
|
return $item;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function search($filter) {
|
|
$items = [];
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
|
|
$sql = "SELECT PreorderBillingInvoiceFile.* FROM PreorderBillingInvoiceFile
|
|
LEFT JOIN File ON (PreorderBillingInvoiceFile.file_id = File.id)
|
|
WHERE $where
|
|
ORDER BY invoice_id, name";
|
|
|
|
$res = $db->query($sql);
|
|
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new PreorderBillingInvoiceFile($data);
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter) {
|
|
$where = "1=1 ";
|
|
|
|
|
|
if(array_key_exists("file_id", $filter)) {
|
|
$file_id = $filter['file_id'];
|
|
if(is_numeric($file_id)) {
|
|
$where .= " AND file_id=$file_id";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("invoice_id", $filter)) {
|
|
$invoice_id = $filter['invoice_id'];
|
|
if(is_numeric($invoice_id)) {
|
|
$where .= " AND invoice_id=$invoice_id";
|
|
}
|
|
}
|
|
|
|
//var_dump($filter);exit;
|
|
if(array_key_exists("name", $filter)) {
|
|
$name = FronkDB::singleton()->escape($filter['name']);
|
|
if($name) {
|
|
$where .= " AND name LIKE '$name'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("filename", $filter)) {
|
|
$filename = FronkDB::singleton()->escape($filter['filename']);
|
|
if($filename) {
|
|
$where .= " AND File.filename='$filename'";
|
|
}
|
|
}
|
|
|
|
if(array_key_exists("subfolder", $filter)) {
|
|
$subfolder = FronkDB::singleton()->escape($filter['subfolder']);
|
|
if($subfolder) {
|
|
$where .= " AND File.subfolder='$subfolder'";
|
|
}
|
|
}
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
}
|
|
|