Example #1
0
 public function s003()
 {
     try {
         $req = App::request();
         if ($req->isPost()) {
             $post = $req->post();
             $rows = json_decode($post['rows'], true);
             if (!empty($post["entry_kode"])) {
                 if (is_null($post["gen_id"]) || $post["gen_id"] == "") {
                     $total_amount = 0;
                     $incoming = 0;
                     $outgoing = 0;
                     foreach ($rows as $row) {
                         $basic_amount = ZiUtil::check_int($row["item_amount"]);
                         // beda receipt dg transfer item_price ikut valuation rate
                         $basic_rate = ZiUtil::check_int($row["item_price"]);
                         $row["item_batch"] = !array_key_exists("item_batch", $row) ? null : $row["item_batch"];
                         $additional_cost = 0;
                         // valuation_rate
                         $amount_rate = $basic_rate + $additional_cost;
                         $amount = ZiUtil::check_int($row["item_qty"]) * $amount_rate;
                         $attr_detail = array("created" => date("Y-m-d H:i:s"), "stok_entry" => $post["entry_kode"], "to_warehouse" => $row["to_warehouse"], "from_warehouse" => $row["from_warehouse"], "item_kode" => $row["item_kode"], "item_nama" => $row["item_nama"], "item_uom" => $row["item_uom"], "batch_no" => $row["item_batch"], "keterangan" => $row["keterangan"], "actual_qty" => $row["actual_qty"], "qty" => $row["item_qty"], "basic_amount" => $basic_amount, "basic_rate" => $basic_rate, "valuation_rate" => $amount_rate, "additional_cost" => $additional_cost, "amount" => $amount);
                         $voucher_detail_no = ZiUtil::GetNowID();
                         $attr_detail["id"] = $voucher_detail_no;
                         $tableDetail = StokEntryD::table();
                         $tableDetail->insert($attr_detail);
                         // incoming
                         if (isset($row["to_warehouse"]) && !empty($row["to_warehouse"])) {
                             $incoming += $amount;
                             // insert stock_queue
                             // klo jenis transfer incoming_rate pake hasil dari valuation_rate
                             // receipt pake $amount_rate
                             $stock_queue = new ZiStockQueue();
                             $stock_queue->incoming_stock($row["item_kode"], $row["to_warehouse"], $amount_rate, $row["item_qty"], $row["item_batch"]);
                             $valuation_rate = ZiStockQueue::valuation_rate($row["item_kode"], $row["to_warehouse"], $row["item_batch"]);
                             $valuation_rate = $valuation_rate > 0 ? $valuation_rate : $amount_rate;
                             $qty_after_trans = ZiUtil::check_int($row["actual_qty"]) + ZiUtil::check_int($row["item_qty"]);
                             $stock_value_diff = ZiUtil::check_int($row["item_qty"]) * $amount_rate;
                             $stock_value = $qty_after_trans * $valuation_rate;
                             // saat nya entry data stock ledger
                             $stockLedger = new ZiStockLedger();
                             $stockLedger->insert_db($row["item_kode"], "Stock Entry", $post["entry_kode"], $voucher_detail_no, ZiUtil::check_int($row["item_qty"]), $row["to_warehouse"], $qty_after_trans, $valuation_rate, $stock_value, $stock_value_diff, $amount_rate, $row["item_batch"]);
                         }
                         // outgoing
                         if (isset($row["from_warehouse"]) && !empty($row["from_warehouse"])) {
                             $outgoing += $amount;
                             $stock_queue = new ZiStockQueue();
                             $stock_queue->outgoing_stock($row["item_kode"], $row["from_warehouse"], $row["item_qty"], $row["item_batch"]);
                             $valuation_rate = ZiStockQueue::valuation_rate($row["item_kode"], $row["from_warehouse"], $row["item_batch"]);
                             $valuation_rate = $valuation_rate > 0 ? $valuation_rate : $amount_rate;
                             $qty_after_trans = ZiUtil::check_int($row["actual_qty"]) - ZiUtil::check_int($row["item_qty"]);
                             $stock_value_diff = (0 - ZiUtil::check_int($row["item_qty"])) * $valuation_rate;
                             $stock_value = $qty_after_trans * $valuation_rate;
                             // $valuation_rate diperoleh dari total all incoming stock
                             // saat nya entry data stock ledger
                             $stockLedger = new ZiStockLedger();
                             $stockLedger->insert_db($row["item_kode"], "Stock Entry", $post["entry_kode"], $voucher_detail_no, 0 - ZiUtil::check_int($row["item_qty"]), $row["from_warehouse"], $qty_after_trans, $valuation_rate, $stock_value, $stock_value_diff, 0, $row["item_batch"]);
                         }
                         $total_amount += $amount;
                     }
                     $attr_master = array("created" => date("Y-m-d H:i:s"), "modified" => date("Y-m-d H:i:s"), "title" => "Material " . $post["stok_entry_tipe"], "naming_series" => "STE", "fiscal_year" => date('Y'), "company" => "RSMM", "posting_date" => $post["posting_date"], "posting_time" => $post["posting_time"], "stok_entry_kode" => $post["entry_kode"], "stok_entry_tipe" => $post["stok_entry_tipe"], "to_warehouse" => $post["to_warehouse"], "from_warehouse" => $post["from_warehouse"], "total_incoming_value" => $incoming, "total_outgoing_value" => $outgoing, "total_additional_costs" => 0, "value_difference" => $incoming - $outgoing);
                     $attr_master["total_amount"] = $total_amount;
                     $attr_master["id"] = ZiUtil::GetNowID();
                     $table = StokEntry::table();
                     $table->insert($attr_master);
                 }
                 App::flash('info', 'Data Tersimpan.');
                 App::redirect('stok.list_stock_entry');
             }
         }
         App::flash('error', 'Terjadi kesalahan pada inputan anda.');
         App::redirect('stok.entry');
     } catch (Exception $e) {
         echo 'Caught exception: ', $e->getMessage(), "\n";
     }
 }