Common.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\constant\CommonConstant;
  4. use app\common\constant\MaintainConstant;
  5. use app\common\model\Goods;
  6. use app\common\service\ApproveInfoService;
  7. use app\common\service\CommonService;
  8. use app\common\service\GoodsCategoryService;
  9. use app\common\service\UserService;
  10. use hg\apidoc\annotation as Apidoc;
  11. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  12. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  13. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  14. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  15. use think\db\exception\BindParamException;
  16. use think\Exception;
  17. use think\exception\PDOException;
  18. Use Jsyqw\PdfTable\DrawContent;
  19. Use Jsyqw\PdfTable\DrawContentTitle;
  20. Use Jsyqw\PdfTable\DrawTable;
  21. use Jsyqw\PdfTable\DrawTables;
  22. use Jsyqw\PdfTable\data\ContentData;
  23. Use Jsyqw\PdfTable\data\ContentTitleData;
  24. Use Jsyqw\PdfTable\data\TableData;
  25. Use Jsyqw\PdfTable\data\TableRowData;
  26. Use Jsyqw\PdfTable\data\TableTitleData;
  27. Use Jsyqw\PdfTable\models\ImageModel;
  28. Use Jsyqw\PdfTable\models\TextModel;
  29. /**
  30. * @Apidoc\Title("公用")
  31. * @Apidoc\Group("api")
  32. * @Apidoc\Sort("4")
  33. */
  34. class Common extends Base
  35. {
  36. // 需要登录
  37. protected $need_login = ['get_user_list'];
  38. protected $pdf;
  39. public function initialize()
  40. {
  41. parent::initialize();
  42. $this->pdf = new \TCPDF();
  43. }
  44. /**
  45. * 测试
  46. *
  47. * @Apidoc\Method("POST")
  48. * @Apidoc\Param("id", type="integer",require=true, desc="ID")
  49. */
  50. public function test()
  51. {
  52. //保存文件
  53. $subPath = date('Ymd');
  54. $savePath = './upload/pdf/' . $subPath;
  55. // 检查上传目录
  56. if (!is_dir($savePath)) {
  57. // 检查目录是否编码后的
  58. if (is_dir(base64_decode($savePath))) {
  59. $savePath = base64_decode($savePath);
  60. } else {
  61. // 尝试创建目录
  62. if (!mkdir($savePath, 0755, true)) {
  63. $this->error = '上传目录' . $savePath . '不存在';
  64. return false;
  65. }
  66. }
  67. } else {
  68. if (!is_writeable($savePath)) {
  69. $this->error = '上传目录' . $savePath . '不可写';
  70. return false;
  71. }
  72. }
  73. $filename = date('Ymd') . sha1(date('YmdHis', time()) . uniqid());
  74. $suffix = '.pdf';
  75. $realpath = substr($savePath, 1) . '/' . $filename . $suffix;
  76. self::exportPdf($_SERVER['DOCUMENT_ROOT'] . $realpath);
  77. // self::tablePdf('测试文件',$_SERVER['DOCUMENT_ROOT'] . $realpath);
  78. $this->success('测试');
  79. }
  80. // 设置PDF参数
  81. protected function setPdfAttr()
  82. {
  83. $this->pdf->setCreator(PDF_CREATOR);
  84. $this->pdf->setAuthor('Dya');
  85. $this->pdf->SetSubject('TCPDF Tutorial');
  86. $this->pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  87. //页眉页脚
  88. $this->pdf->setPrintHeader(false);
  89. $this->pdf->setPrintFooter(true);
  90. // 页脚信息
  91. $this->pdf->setFooterData(array(0,64,0), array(0,64,128));
  92. $this->pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  93. $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  94. // 自动分页 (第二个参数可以设置距离底部多少距离时分页)
  95. $this->pdf->setAutoPageBreak(true, 15);
  96. // 设置边距(左 上 右 下) 右边距默认左侧值 下边距是bool值(是否覆盖默认页边距)
  97. $this->pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  98. // 定义默认的单间距字体 (设置为等宽字体)
  99. $this->pdf->SetDefaultMonospacedFont('courier');
  100. // 设置图像比例因子
  101. $this->pdf->setImageScale(1.25);
  102. }
  103. /**
  104. * 绘制表格
  105. * @param $title
  106. * @param $fileName
  107. */
  108. public function tablePdf($title, $fileName)
  109. {
  110. $this->setPdfAttr();
  111. $this->pdf->setTitle($title);
  112. // 新增页面
  113. $this->pdf->AddPage();
  114. // 设置字体
  115. $this->pdf->SetFont('stsongstdlight', 'B', 24, '', true);
  116. // 参数 1:w 2:h 3:内容 4:边框 5:是否换行 6:内容居中等
  117. $this->pdf->Cell(0, 0, '财务收款收据', '0', 1, 'C');
  118. $this->pdf->SetFont('stsongstdlight', '', 18, '', true);
  119. $this->pdf->Cell(90, 30, 'TEL:888888', '0', 0, 'L');
  120. // 颜色 RGB颜色代码
  121. $this->pdf->setTextColor(255, 0, 0);
  122. $this->pdf->Cell(90, 30, 'NO.20221215', '0', 0, 'R');
  123. // 换行
  124. $this->pdf->Ln();
  125. // 重置颜色
  126. $this->pdf->setTextColor();
  127. $this->pdf->SetFont('stsongstdlight', '', 16, '', true);
  128. $this->pdf->Cell(90, 15, ' 客户名称:张三', '0', 0, 'L');
  129. $this->pdf->Cell(90, 15, '日期: 2022年12月15日 ', '0', 1, 'R');
  130. $h = 15;
  131. // 绘表 表头
  132. $this->pdf->Cell(40, $h, '规格及名称', '1', 0, 'C');
  133. $this->pdf->Cell(15, $h, '单位', '1', 0, 'C');
  134. $this->pdf->Cell(20, $h, '数量', '1', 0, 'C');
  135. $this->pdf->Cell(25, $h, '单价', '1', 0, 'C');
  136. $this->pdf->Cell(45, $h, '金额', '1', 0, 'C');
  137. $this->pdf->Cell(35, $h, '备注', '1', 1, 'C');
  138. $data = [
  139. ['name'=>'项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一项目一','unit'=>'件','num'=>13,'price'=>55.00,'money'=>'715.00','remark'=>''],
  140. ['name'=>'项目二','unit'=>'件','num'=>13,'price'=>54.00,'money'=>'702.00','remark'=>''],
  141. ['name'=>'项目三','unit'=>'件','num'=>19,'price'=>54.00,'money'=>'1026.00','remark'=>''],
  142. ['name'=>'项目四','unit'=>'件','num'=>24,'price'=>54.00,'money'=>'1296.00','remark'=>''],
  143. ];
  144. // 内容
  145. foreach ($data as $item) {
  146. $this->pdf->Cell(40, $h, $item['name'], '1', 0, 'C');
  147. $this->pdf->Cell(15, $h, $item['unit'], '1', 0, 'C');
  148. $this->pdf->Cell(20, $h, $item['num'], '1', 0, 'C');
  149. $this->pdf->Cell(25, $h, $item['price'], '1', 0, 'C');
  150. $this->pdf->Cell(45, $h, $item['money'], '1', 0, 'C');
  151. $this->pdf->Cell(35, $h, $item['remark'], '1', 1, 'C');
  152. }
  153. $total = sprintf('%.2F', array_sum(array_column($data, 'money')));
  154. $this->pdf->Cell(100, $h, '合计金额:', 'LTB', 0, 'L');
  155. $this->pdf->Cell(45, $h, $total, 'TBR', 0, 'R');
  156. $this->pdf->Cell(35, $h, '', '1', 0, 'C');
  157. $this->pdf->Output($fileName, 'F');
  158. }
  159. public function exportPdf($fileName)
  160. {
  161. $this->setPdfAttr();
  162. // $this->pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  163. $this->pdf->SetCreator(PDF_CREATOR);
  164. $this->pdf->SetAuthor('RoyalsZch');
  165. $this->pdf->SetTitle('123');
  166. $this->pdf->SetSubject('123pdf');
  167. $this->pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  168. // set default header data
  169. $this->pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
  170. $this->pdf->setFooterData(array(0, 64, 0), array(0, 64, 128));
  171. // set header and footer fonts
  172. $this->pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  173. $this->pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  174. // set default monospaced font
  175. $this->pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  176. // set margins
  177. $this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  178. $this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  179. $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  180. // set auto page breaks
  181. $this->pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  182. // set image scale factor
  183. $this->pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  184. $this->pdf->SetFont('stsongstdlight', '', 14, '', true);
  185. // $this->pdf->Output('pdf'.'.pdf', 'D');
  186. // add a page
  187. $this->pdf->AddPage();
  188. // 随便写HTML
  189. $htmlUrl=app()->getRootPath() . 'public/upload/1.html';
  190. $html=file_get_contents($htmlUrl);
  191. // output the HTML content
  192. $this->pdf->writeHTML($html, true, false, true, false, '');
  193. // $this->pdf->Image('https://gvrboxwit.obs.cn-east-2.myhuaweicloud.com:443/web/202303/202303301615200163657.png', 15, 140, 75, 113, 'JPG', 'http://www.tcpdf.org', '', true, 150, '', false, false, 1, false, false, false);
  194. // reset pointer to the last page
  195. $this->pdf->lastPage();
  196. //D是下载,I是在线写入
  197. $this->pdf->Output($fileName, 'F');
  198. }
  199. /**
  200. * 类型数据
  201. *
  202. * @Apidoc\Method("POST")
  203. * @Apidoc\Returned("module_list", type="array", desc="模块列表")
  204. * @Apidoc\Returned("degree_list", type="array", desc="缓急程度列表")
  205. * @Apidoc\Returned("pay_type_list", type="array", desc="采购支付方式列表")
  206. * @Apidoc\Returned("time_list", type="array", desc="请假周期列表")
  207. * @Apidoc\Returned("data1", type="array", desc="采购类型列表")
  208. * @Apidoc\Returned("data2", type="array", desc="呈批类型列表")
  209. * @Apidoc\Returned("data5", type="array", desc="出差类型列表")
  210. * @Apidoc\Returned("data6", type="array", desc="请假类型列表")
  211. * @Apidoc\Returned("data8", type="array", desc="维修类型列表")
  212. * @Apidoc\Returned("data9", type="array", desc="合同类型列表")
  213. */
  214. public function get_type_list()
  215. {
  216. $data = CommonService::get_data();
  217. $this->success('类型数据', $data);
  218. }
  219. /**
  220. * 发起人列表
  221. *
  222. * @Apidoc\Desc("入库模块")
  223. * @Apidoc\Method("POST")
  224. * @Apidoc\Param("page", type="integer",require=true, desc="页数")
  225. * @Apidoc\Param("page_num", type="integer", require=true, desc="每页数量")
  226. * @Apidoc\Returned("userid", type="string", desc="userid")
  227. * @Apidoc\Returned("name", type="string", desc="姓名")
  228. **/
  229. public function get_user_list()
  230. {
  231. $offset = $this->off_set;
  232. $length = $this->page_num;
  233. $user = $this->user;
  234. $list = UserService::get_list(0,$offset, $length);
  235. $this->success('发起人列表', $list);
  236. }
  237. /**
  238. * 维修人员列表
  239. *
  240. * @Apidoc\Desc("维修模块")
  241. * @Apidoc\Method("POST")
  242. * @Apidoc\Param("type", type="integer", require=true, desc="类型:1=物业主管,2=信息负责人")
  243. **/
  244. public function get_maintain_user(){
  245. $type = $this->request->post('type');
  246. if (!array_key_exists($type, MaintainConstant::get_type_list())) {
  247. $this->success('维修人员列表.');
  248. }
  249. $list = UserService::get_maintain_user($type);
  250. $this->success('维修人员列表',$list);
  251. }
  252. /**
  253. * 获取导入模板
  254. *
  255. * @Apidoc\Desc("申购模块,入库模块,领用模块")
  256. * @Apidoc\Method("POST")
  257. * @Apidoc\Param("module", type="integer", require=true, desc="模块类型:1=申购申请,3=入库申请,4=领用申请")
  258. * @Apidoc\Returned("url", type="string", desc="导入模板路径")
  259. **/
  260. public function get_import_template(){
  261. $module = $this->request->post('module');
  262. if (!in_array($module, [CommonConstant::MODULE_1, CommonConstant::MODULE_3, CommonConstant::MODULE_4])) {
  263. $this->error('请选择正确的模块类型');
  264. }
  265. switch ($module){
  266. case CommonConstant::MODULE_1:
  267. $url = $this->request->domain().'/导入模板-采购物品.xlsx';
  268. break;
  269. case CommonConstant::MODULE_3:
  270. $url = $this->request->domain().'/导入模板-入库物品.xlsx';
  271. break;
  272. case CommonConstant::MODULE_4:
  273. $url = $this->request->domain().'/导入模板-领用物品.xlsx';
  274. break;
  275. }
  276. $data = compact("url");
  277. $this->success('导入模板',$data);
  278. }
  279. /**
  280. * 导入
  281. *
  282. * @Apidoc\Desc("申购模块,入库模块,领用模块")
  283. * @Apidoc\Method("POST")
  284. * @Apidoc\Param("module", type="integer", require=true, desc="模块类型:1=申购申请,3=入库申请,4=领用申请")
  285. * @Apidoc\Param("file", type="string", require=true, desc="上传后的文件路径")
  286. * @return void
  287. * @throws PDOException
  288. * @throws BindParamException
  289. */
  290. public function import()
  291. {
  292. $module = $this->request->post('module');
  293. $file = $this->request->post('file');
  294. if (!in_array($module, [CommonConstant::MODULE_1, CommonConstant::MODULE_3, CommonConstant::MODULE_4])) {
  295. $this->error('请选择正确的模块类型');
  296. }
  297. if (!$file) {
  298. $this->error('请选择文件');
  299. }
  300. $filePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $file;
  301. if (!is_file($filePath)) {
  302. $this->error('文件未找到');
  303. }
  304. //实例化reader
  305. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  306. if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  307. $this->error('未知的数据格式');
  308. }
  309. if ($ext === 'csv') {
  310. $file = fopen($filePath, 'r');
  311. $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  312. $fp = fopen($filePath, 'w');
  313. $n = 0;
  314. while ($line = fgets($file)) {
  315. $line = rtrim($line, "\n\r\0");
  316. $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  317. if ($encoding !== 'utf-8') {
  318. $line = mb_convert_encoding($line, 'utf-8', $encoding);
  319. }
  320. if ($n == 0 || preg_match('/^".*"$/', $line)) {
  321. fwrite($fp, $line . "\n");
  322. } else {
  323. fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
  324. }
  325. $n++;
  326. }
  327. fclose($file) || fclose($fp);
  328. $reader = new Csv();
  329. } elseif ($ext === 'xls') {
  330. $reader = new Xls();
  331. } else {
  332. $reader = new Xlsx();
  333. }
  334. //加载文件
  335. $insert = [];
  336. $data = []; // 导入的同一一级分类和二级分类和商品名称的数组
  337. $category_first_data = []; // 导入的分类数组
  338. $use_data = []; // 筛选后的领用数组
  339. $apply_goods_data = []; // 返回的数组
  340. $error_data = []; // 错误提示数组
  341. $flag = 2; // 默认批量导入
  342. try {
  343. if (!$PHPExcel = $reader->load($filePath)) {
  344. $this->error('未知的数据格式!');
  345. }
  346. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  347. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  348. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  349. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  350. if ($module == CommonConstant::MODULE_4) {
  351. $fields = ['goods_no', 'goods_category_first', 'goods_category_id', 'goods_name', 'goods_sku_value', 'stock'];
  352. } else {
  353. $fields = ['goods_no', 'goods_category_first', 'goods_category_id', 'goods_brand', 'goods_name', 'goods_sku_value', 'stock', 'price', 'total_price'];
  354. }
  355. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  356. $values = [];
  357. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  358. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  359. $values[] = is_null($val) ? '' : trim($val); // 消除空格
  360. }
  361. $values = array_filter($values); // 过滤空值
  362. $temp = array_combine($fields, $values); // 合并两个数组来创建一个新数组,其中的一个数组元素为键名,另一个数组元素为键值
  363. if ($temp) {
  364. $insert[] = $temp;
  365. if ($module == CommonConstant::MODULE_4) {
  366. $data[$values[1] . ',' . $values[2] . ',' . $values[3]][] = $temp;
  367. $category_first_data[$values[1]][$values[2]] = $values[2];
  368. } else {
  369. $data[$values[1] . ',' . $values[2] . ',' . $values[4]][] = $temp;
  370. }
  371. }
  372. }
  373. } catch (Exception $exception) {
  374. $this->error('出现错误:' . $exception->getMessage());
  375. }
  376. if (!$insert) {
  377. $this->error('记录未找到');
  378. }
  379. if ($module == CommonConstant::MODULE_4) {
  380. $category_first_list = GoodsCategoryService::get_list([['name', 'in', array_keys($category_first_data)]],1);
  381. $category_first_object = $category_first_list ? array_column($category_first_list->toArray(), null, 'name') : [];
  382. $msg = '导入的物品:';
  383. foreach ($data as $key => $value) {
  384. $arr = explode(',', $key);
  385. $goods_name = $arr[2];
  386. if(array_key_exists($arr[0],$category_first_object)){
  387. // 一级里有该商品分类
  388. $category_first_info = $category_first_object[$arr[0]];
  389. $goods_category_first_id = $category_first_info['id'];
  390. $category_second_object = $category_first_info['childlist'] ? array_column($category_first_info['childlist'], null, 'name') : [];
  391. if(array_key_exists($arr[1],$category_second_object)){
  392. // 二级里有该商品分类
  393. $category_second_info = $category_second_object[$arr[1]];
  394. $goods_category_id = $category_second_info['id'];
  395. $goods_info = Goods::field('status,is_deleted,create_at', true)
  396. ->where('goods_category_first', $goods_category_first_id)
  397. ->where('goods_category_id', $goods_category_id)
  398. ->where('goods_name', $goods_name)
  399. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  400. ->with([
  401. 'goodsStock',
  402. ])
  403. ->find();
  404. if ($goods_info) {
  405. // 商品库里有该商品
  406. $goods_info = $goods_info->toArray();
  407. $goods_stock_object = array_column($goods_info['goods_stock'], null, 'name');
  408. foreach ($value as $kk=>$val) {
  409. if (array_key_exists($val['goods_sku_value'], $goods_stock_object)) {
  410. // 商品库里有该商品规格
  411. $goods_stock_info = $goods_stock_object[$val['goods_sku_value']];
  412. $total_stock = $goods_stock_info['stock']; // 商品库的库存
  413. $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0; // 要领用的数量
  414. $lack_stock = $stock - $total_stock;
  415. if($total_stock > 0){
  416. // 商品规格有库存
  417. $use_data[$key][] = $val;
  418. $use_data[$key][$kk]['goods_id'] = $goods_info['id'];
  419. $use_data[$key][$kk]['goods_category_first'] = $goods_category_first_id;
  420. $use_data[$key][$kk]['goods_category_id'] = $goods_category_id;
  421. $use_data[$key][$kk]['id'] = $goods_stock_info['id'];
  422. if($lack_stock >= 0){
  423. // 商品规格库存不足
  424. $use_data[$key][$kk]['stock'] = $total_stock; // 替换库存
  425. $error_data[] = [
  426. 'msg' => $msg . $goods_name . '-' . $val['goods_sku_value'] . '库存不足,缺少' . $lack_stock . '件',
  427. ];
  428. }
  429. } else{
  430. $error_data[] = [
  431. 'msg' => $msg . $goods_name . '-' . $val['goods_sku_value'] . '库存不足,缺少' . $lack_stock . '件',
  432. ];
  433. }
  434. } else {
  435. // 商品库里没有该商品规格
  436. $error_data[] = [
  437. 'msg' => $msg . $goods_name . '-' . $val['goods_sku_value'] . '商品库不存在',
  438. ];
  439. }
  440. }
  441. } else {
  442. // 商品库里没有该商品
  443. $error_data[] = [
  444. 'msg' => $msg . $goods_name . '商品库不存在',
  445. ];
  446. }
  447. } else{
  448. // 二级里没有该商品分类
  449. $error_data[] = [
  450. 'msg' => $msg . $goods_name . '商品库不存在',
  451. ];
  452. }
  453. } else{
  454. // 一级里没有该商品分类
  455. $error_data[] = [
  456. 'msg' => $msg . $goods_name . '商品库不存在',
  457. ];
  458. }
  459. }
  460. $data = $use_data;
  461. $flag = 3; // 商品库选择
  462. }
  463. foreach ($data as $key => $value) {
  464. // 商品
  465. $goods_data = [];
  466. $goods_stock_data = [];
  467. $total_price = 0;
  468. foreach ($value as $kk => $val) {
  469. // 规格值
  470. $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
  471. $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
  472. $total_price = bcadd($total_price, $stock * $price, 2);
  473. $goods_stock_data[] = [
  474. 'id' => isset($val['id']) ? $val['id'] : 0,
  475. 'name' => $val['goods_sku_value'],
  476. 'stock' => $stock,
  477. 'price' => $price,
  478. ];
  479. $goods_data = [
  480. 'goods_id' => isset($val['goods_id']) ? $val['goods_id'] : 0,
  481. 'goods_category_first' => $val['goods_category_first'],
  482. 'goods_category_id' => $val['goods_category_id'],
  483. 'goods_no' => $val['goods_no'],
  484. 'goods_name' => $val['goods_name'],
  485. 'goods_brand' => isset($val['goods_brand']) ? $val['goods_brand'] : '',
  486. ];
  487. }
  488. $goods_data['flag'] = $flag;
  489. $goods_data['total_price'] = $total_price;
  490. $goods_data['goods_stock'] = $goods_stock_data;
  491. $apply_goods_data[] = $goods_data;
  492. }
  493. $data = compact("apply_goods_data", "error_data");
  494. $error_data ? $this->success('领用物品提示', $data) : $this->success('导入成功', $data);
  495. }
  496. }