--- /dev/null
+<?php
+
+include_once(dirname(__DIR__, 2) . "/startup.php");
+include_once(dirname(__DIR__, 2) . "/inc/db.php");
+include_once(dirname(__DIR__, 2) . "/inc/base_new.php");
+include_once(dirname(__DIR__, 2) . "/inc/design_new.php");
+
+error_reporting(E_ALL ^ E_NOTICE);
+
+$modul="write-offs";
+
+
+if(!empty($_REQUEST["write_offs_id"])) $write_offs_id=htmlentities($_REQUEST["write_offs_id"]);
+
+$stores=$db::mapping("SELECT id,name FROM products_1c WHERE tip='city_store'");
+$products=$db::mapping("SELECT id,name FROM products_1c WHERE tip='products'");
+
+
+
+
+
+$select=array("-все-","Брак","Списание по инвентаризации","Брак с поставки","Списание на затраты (подарки)");
+
+echo'<div class="e-table"><div class="table-responsive table-lg mt-3">';
+
+
+// группировка для автозаказа
+$commentsArray=[
+ 1=>"Перетарка",
+ 2=>"Брак с поставки",
+ 3=>"Ошибка в работе с цветком",
+];
+
+// причины списаний для администраторов
+$commentsArray=[
+ 1=>"Перетарка",
+ 2=>"Брак с поставки",
+ 3=>"Плохой цветок с поставки",
+ 4=>"Флорист неверно обработал цветок",
+ 5=>"Собранный букет был не из матрицы",
+ 6=>"Администратор вовремя не поставил задачу по обработке"
+];
+
+
+
+
+
+
+if(!empty($_POST["save"])) {
+
+ foreach($_POST["comments"] as $product_id => $comArr ) {
+ $product_id=htmlentities($product_id);
+
+ $db::sql("DELETE FROM write_offs_comments WHERE write_offs_id=? AND product_id=? ",[$write_offs_id,$product_id]);
+
+ foreach($comArr as $com_id => $quantity) {
+
+ $com_id=(int)$com_id;
+
+ if(!empty($com_id) and !empty($quantity)) {
+ $sql="INSERT IGNORE INTO write_offs_comments SET write_offs_id=?,
+ product_id=?, comment_id=? , quantity=? ON DUPLICATE KEY UPDATE comment_id=?, quantity=?, date_update=NOW() ";
+ $db::sql($sql,[$write_offs_id,$product_id,$com_id, $quantity, $com_id,$quantity]);
+ }
+ }
+
+ }
+
+
+// суммируем количество по всем причинам
+ $quantityCom=array();
+ $data=$db::getRows("SELECT product_id, sum(quantity) as quantity FROM write_offs_comments WHERE write_offs_id=? group by product_id",[$write_offs_id]);
+ foreach($data as $row) $quantityCom[$row["product_id"]]=$row["quantity"];
+
+
+ $data=$db::getRows("SELECT product_id,quantity FROM write_offs_products WHERE write_offs_id=?",[$write_offs_id]);
+ foreach($data as $row) {
+ $itemsProductsArr[$row["product_id"]]=$row["quantity"];
+ }
+
+ foreach($itemsProductsArr ?? [] as $pid =>$q){
+ if($quantityCom[$pid]>$itemsProductsArr[$pid]) {
+
+ error_mess("Суммарное значение ".$products[$pid]." больше количества списания! ");
+ }
+ }
+
+
+ mess("Сохранили");
+}
+
+
+$comments=array();
+$data=$db::getRows("SELECT * FROM write_offs_comments WHERE write_offs_id=?",[$write_offs_id]);
+foreach($data as $row) $comments[$row["product_id"]][$row["comment_id"]]=$row["quantity"];
+
+
+
+$data=$db::getRows("SELECT *,DATE_FORMAT(date, '%d.%m.%Y %H:%i') as date2 FROM write_offs WHERE id=?",[$write_offs_id]);
+foreach($data as $row) {
+ $items=json_decode($row["items"],true,512,JSON_UNESCAPED_UNICODE);
+
+ echo'<h1>Списание '.$row["number"].' <span class="tag m-1">'.$stores[$row["store_id"]].'</span> '.$row["summ"].' руб.</h1>
+дата '.$row["date2"].' тип '.$row["type"].'
+'.$row["based_on"].' Комментарий '.$row["comment"];
+
+ echo'<form method=post action="/'.$modul.'/comments/?write_offs_id='.$write_offs_id.'">
+<input type="hidden" name="_csrf" value="' . Yii::$app->request->getCsrfToken() . '" />
+<table class="zak"><thead>
+<th class="w-10">Наименование</th><th style="width:80px">кол-во</th>';
+ foreach($commentsArray as $nameC) echo'<th><b>'.$nameC.'</b></th>';
+
+ /*<td><select class="form-control" name=comments['.$mass["product_id"].']>';
+
+ foreach($commentsArray as $com_id => $name){
+ echo'<option value='.$com_id.''; if($com_id==$comments[$mass["product_id"]]) echo' selected';echo'>'.$name.'</option>';
+
+ }
+
+ echo'</select></td>*/
+
+ echo'</thead><tbody>';
+ rsort($items);
+
+ $j=0;
+
+ foreach($items as $k => $mass) {
+
+ if($j==14) { echo'<tr><th class="w-10">Наименование</th><th style="width:80px">кол-во</th>';
+ foreach($commentsArray as $nameC) echo'<th>'.$nameC.'</th>';
+ echo'</tr>';
+ $j=0;
+ }
+
+
+ $j++;
+ echo'<tr><td>'.$products[$mass["product_id"]].'</td><td><b>'.$mass["quantity"].'</b></td>';
+
+
+ foreach($commentsArray as $comId => $nameC) echo'<th><input type=number class="form-control" name=comments['.$mass["product_id"].']['.$comId.'] value="'.$comments[$mass["product_id"]][$comId].'"></th>';
+
+
+
+ echo'</tr>';
+ }
+
+
+
+ echo'</tbody></table>
+<button class="btn-success btn btn-lg" name=save value=1>сохранить</button>';
+
+
+}
+echo'</form></div></div>';
+
+
+
+echo'<style>
+
+
+</style>';
+
+
+
+
+$_CONFIG["jscss"]='
+<style>
+
+input[type="number"]::-webkit-outer-spin-button,
+input[type="number"]::-webkit-inner-spin-button {
+ -webkit-appearance: none;
+}
+
+input[type="number"],
+input[type="number"]:hover,
+input[type="number"]:focus {
+ appearance: none;
+ -moz-appearance: textfield;
+}
+
+
+.zak tbody tr:hover td{background:#cdcdcd}
+.zak> thead> tr>th, .zak tr.zg th, .zak tr.zg td{font-size:0.7rem;min-width:30px;text-align:center; font-weight:normal; max-height:60px;overflow:hidden}
+.zak> thead> tr>th b{font-weight:normal;}
+.zak> :not(caption)>*>* {
+ padding: 0.05rem;
+ background-color: 1px;
+ box-shadow: none;
+}
+.zak td.fs-5{text-align:left;padding:0.3rem 1rem;}
+.zak {
+ border-collapse: collapse;
+ border-spacing: 0;
+ overflow: hidden;
+}
+.zak th, .zak td {
+ border: 1px solid #e6e6e6;
+ padding: 0.1rem;
+ vertical-align: middle;
+ position: relative;
+}
+
+
+.zak td:hover:before {
+ background-color: #e3e3e3;
+ content: \'\';
+ height: 100%;
+ left: -5000px;
+ position: absolute;
+ top: 0;
+ width: 10000px;
+ z-index: -2;
+}
+
+.zak td:hover:after {
+ background-color: #cdcdcd;
+ content: \'\';
+ height: 10000px;
+ left: 0;
+ position: absolute;
+ top: -5000px;
+ width: 100%;
+ z-index: -1;
+}
+
+
+
+ .zak tbody td input.form-control{font-size:0.8rem;min-width:70px;max-width:100px;font-weight:bold;border:0;border-radius:0;color:#333;padding:0.2rem;margin:0;}
+ .zak tbody td input.form-control:focus{border:2px #cd0202 solid;background:#ffccdb}
+.zak td.bg-danger,.zak td.bg-indigo,.zak td.bg-info, .zak tr th.bg-danger, .zak tr th.bg-info, .zak tr th.bg-indigo{color:#fff;}
+
+
+table.zak thead th:not(:first-child),
+table.zak tr.zg th th:not(:first-child) {
+ vertical-align: bottom;
+ line-height: normal;
+}
+table.zak thead b {
+ writing-mode: vertical-rl;
+ transform: scale(-1);
+
+}
+
+
+table.zak tr.zg th b,table.zak tr.zg td b {
+ writing-mode: vertical-rl;
+ transform: scale(-1);
+ font-weight:normal; font-size:0.6rem;
+}
+
+ .zak tr th .th_p_summ{text-align:center;font-weight:bold;}
+ .app-content .side-app {
+ padding: 25px 0.5rem 0 0.5rem;
+}
+
+.zak tbody tr td.td_product,.zak td.td_product {tex-align:left;}
+.zak td.td_product:hover {background:#fff;}
+
+</style>
+
+
+';
+
+
+
+include_once dirname(__DIR__, 2) . '/templates/bottom_light.php';
+
+
+
+
--- /dev/null
+<?php
+
+include_once(dirname(__DIR__, 2) . "/startup.php");
+include_once(dirname(__DIR__, 2) . "/inc/db.php");
+include_once(dirname(__DIR__, 2) . "/inc/base_new.php");
+include_once(dirname(__DIR__, 2) . "/inc/design_new.php");
+
+error_reporting(E_ALL ^ E_NOTICE);
+
+$modul="write-offs";
+
+$where=""; $get="";
+if(!empty($_REQUEST["date1"])) {$date1=htmlentities($_REQUEST["date1"]); $where .=" AND date>='$date1'"; $get .="&date1=$date1"; }
+if(!empty($_REQUEST["date2"])) {$date2=htmlentities($_REQUEST["date2"]); $where .=" AND date<='$date2'"; $get .="&date2=$date2";}
+if(!empty($_REQUEST["store_id"])) {$store_id=htmlentities($_REQUEST["store_id"]); $where .=" AND store_id='$store_id'"; $get .="&store_id=$store_id";}
+
+
+if(!empty($_REQUEST["type"]) and $_REQUEST["type"]!="-все-") {$type=htmlentities($_REQUEST["type"]); $where .=" AND type='$type'"; $get .="&type=$type";}
+
+$stores=$db::mapping("SELECT id,name FROM products_1c WHERE tip='city_store' AND view='1' ");
+$products=$db::mapping("SELECT id,name FROM products_1c WHERE tip='products' ");
+
+$stiresDostup="";
+$where .=" AND store_id in ('";
+$k=0;
+foreach( $stores as $guid => $store) {
+ if(in_array($guid,$_SESSION["store_arr_guid_dostup"])) {
+ if($k!=0) $where .="','";
+ $where .=$guid;
+ $stiresDostup .="<a href=\"/$modul/?store_id=$guid\" class=\"btn btn-";
+ if($guid==$store_id) $stiresDostup .="success";
+ else $stiresDostup .="info";
+
+ $stiresDostup .=" mt-2 me-2 btn-sm\">".$store."</a>";
+
+ $k++;
+ }
+}
+$where .="')";
+
+
+echo"<div class=row><div class=\"col-12\">Доступные магазины $stiresDostup</div></div>";
+
+$limit = 100;
+$offset = !empty($_GET['p'])?(($_GET['p']-1)*$limit):0;
+$offset=(int)$offset;
+//получаем количество записей
+$resultNum = $db::getRow("SELECT COUNT(*) as postNum FROM write_offs WHERE 1 $where",);
+$rowCount = $resultNum['postNum'];
+//инициализируем класс pagination
+$pagConfig = array('baseURL'=>'/'.$modul.'/?'.$get.'', 'totalRows'=>$rowCount, 'perPage'=>$limit );
+$pagination = new Pagination($pagConfig); //получаем записи
+
+
+
+$select=array("-все-","Брак","Списание по инвентаризации","Брак с поставки","Списание на затраты (подарки)");
+
+echo'<h1>Списания</h1>';
+
+
+echo'<form method=post>
+<input type="hidden" name="_csrf" value="' . Yii::$app->request->getCsrfToken() . '" />
+<table><td><input type=date name=date1 value="'.$date1.'" class="form-control"></td>
+<td><input type=date name=date2 value="'.$date2.'" class="form-control"> </td>
+<td><select class="form-control" name="type">';
+foreach($select as $name) {
+ echo'<option value="'.$name.'" ';
+ if($name==$type) echo' selected';
+ echo' >'.$name.'</option>';
+}
+echo'</select></td><td><button class="btn btn-success" value="1" name=show>сохранить</button></td></table></form>';
+
+echo'<div class="e-table"><div class="table-responsive table-lg mt-3">
+<table class="table table-bordered border-top text-nowrap">
+<thead><tr><th class="border-bottom-0" width=100>сумма закупочные</th>
+<th class="border-bottom-0" width=100>сумма в розничных</th>
+<th class="border-bottom-0">место</th>
+<th class="border-bottom-0 w-10">магазин дата</th><th class="border-bottom-0 w-80">наименование</th>
+
+</tr></thead><tbody>';
+$summ_retail=0;
+$data=$db::getRows("SELECT *,DATE_FORMAT(date, '%d.%m.%Y %H:%i') as date2 FROM write_offs WHERE 1 $where order by date DESC LIMIT $offset,$limit");
+foreach($data as $row) {
+
+
+ $items=json_decode($row["items"],true,512,JSON_UNESCAPED_UNICODE);
+
+ echo'<tr onclick="$(\'#dd__'.$row["id"].'\').toggle();">
+<td>'.$row["summ"].'</td><td>'.$row["summ_retail"].'</td><td>'.$row["write_downs"].'</td><td>';
+
+
+ $summ_retail +=$row["summ_retail"];
+ $summ +=$row["summ"];
+
+ if(empty($store_id)) echo'<span class="tag m-1">'.$stores[$row["store_id"]].'</span> ';
+
+ echo'
+
+<a href="/'.$modul.'/comments/?write_offs_id='.$row["id"].'" class="btn btn-info">комментарии</a>
+'.$row["date2"].' '.$row["type"].'</td>
+<td><a href=/'.$modul.'/show/?id='.$row["id"].'>'.$row["number"].' '.$row["based_on"].' '.$row["comment"].'</a>
+';
+ echo'</td></tr>';
+
+ echo'<tr id="dd__'.$row["id"].'" style="display:none;"><td colspan=5>';
+
+ echo'<table class="table table-hover table-sm"><thead><th>Наименование</th><th>количество списаного</th><th>сумма</th></thead><tbody>';
+ rsort($items);
+ foreach($items as $k => $mass) {
+//.[product_id] => 7c73baa8-de69-11e8-96c5-1c6f659fb563 [color] => [quantity] => 10 [price] => 150 [summ] => 1500
+ echo'<tr><td>'.$products[$mass["product_id"]].'</td><td>'.$mass["quantity"].'</td><td>'.$mass["summ"].'</td></tr>';
+ }
+ echo'</tbody></table>';
+ echo'</td></tr>';
+
+}
+echo'</tbody></table>';
+
+echo'Итого в розничных <b>'.$summ_retail.'</b> Итого в закупке <b>'.$summ.'</b>';
+
+echo'</div></div>';
+
+if(!empty($data)) echo $pagination->createLinks();
+
+include_once dirname(__DIR__, 2) . '/templates/bottom_light.php';