Beispiel #1
0
 public function actionTemplate($id = false, $term = false, $arraymap = false, $results = false)
 {
     $res = [];
     if ($term) {
         $model = Template::find()->select(["title"])->where(["like", "lower(title)", strtolower($term)])->asArray()->all();
         foreach ($model as $m) {
             if ($arraymap) {
                 $map = explode(",", $arraymap);
                 if (count($map) == 1) {
                     $obj = isset($m[$arraymap]) ? $m[$arraymap] : null;
                 } else {
                     $obj = [];
                     foreach ($map as $a) {
                         $k = explode(":", $a);
                         $v = count($k) > 1 ? $k[1] : $k[0];
                         $obj[$k[0]] = $v == "Obj" ? json_encode($m) : (isset($m[$v]) ? $m[$v] : null);
                     }
                 }
                 $res[] = $obj;
             } else {
                 $res[] = $m["title"];
             }
         }
     } elseif ($id) {
         $res = Template::find()->where("title = :t", ["t" => $id])->one();
     }
     if ($results) {
         return \yii\helpers\Json::encode(["results" => $res]);
     } else {
         return \yii\helpers\Json::encode($res);
     }
 }
Beispiel #2
0
 public function saveTemplate($p, $istemplate)
 {
     $t = [];
     $t['title'] = $p['title'];
     $t['tags'] = $p['tags'];
     $t['type'] = $p['type'];
     $t['subject'] = $p['subject'];
     $t['remarks'] = $p['remarks'];
     $t['total'] = $p['total'];
     $t['journals'] = [];
     foreach (['debet', 'credit'] as $a) {
         if (isset($p[$a])) {
             foreach ($p[$a] as $d) {
                 if (!empty($d['account_id']) && $d['account_id'] > 0 && $d['amount'] > 0) {
                     $v = [];
                     $v['account_id'] = $d['account_id'];
                     $v['type'] = $d['type'];
                     $v['remarks'] = $d['remarks'];
                     $v['quantity'] = $d['quantity'];
                     $v['amount'] = $d['amount'];
                     $v['ratio'] = $d['amount'] / $t['total'];
                     array_push($t['journals'], $v);
                 }
             }
         }
     }
     $template = Template::find()->where("title = :t", ["t" => $t['title']])->one();
     if (!$istemplate && $template) {
         return true;
     } else {
         if (!$template) {
             $template = new Template();
         }
         $template->title = $t['title'];
         $template->json = json_encode($t);
         return $template->save();
     }
 }