Filing.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\facade\Validate;
  5. //报备单
  6. class Filing extends Base
  7. {
  8. //列表
  9. public function getList()
  10. {
  11. $data = $this->request->only(['page' => 1, 'size' => 10, 'cat_id' => '', 'start_date' => '', 'end_date' => '', 'filingCode' => '', 'status' => '', 'good_name' => '', 'companyName' => '', 'companyCode' => '', 'orderCode' => '', 'supplierNo' => ''], 'post');
  12. $where = [['is_del', '=', '0']];
  13. if ($data['cat_id'] != '') $where[] = ['cat_id', '=', $data['cat_id']];
  14. if ($data['start_date'] != '' && $data['end_date'] != '') $where[] = ['addtime', 'between', [$data['start_date'] . ' 00:00:00', $data['end_date'] . ' 23:59:59']];
  15. if ($data['filingCode'] != '') $where[] = ['filingCode', 'like', '%' . $data['filingCode'] . '%'];
  16. if ($data['status'] != '') $where[] = ['status', '=', $data['status']];
  17. if ($data['good_name'] != '') $where[] = ['good_name', 'like', '%' . $data['good_name'] . '%'];
  18. if ($data['companyName'] != '') $where[] = ['companyName', 'like', '%' . $data['companyName'] . '%'];
  19. if ($data['companyCode'] != '') $where[] = ['companyCode', 'like', '%' . $data['companyCode'] . '%'];
  20. if ($data['orderCode'] != '') $where[] = ['orderCode', 'like', '%' . $data['orderCode'] . '%'];
  21. if ($data['supplierNo'] != '') $where[] = ['supplierNo', '=', $data['supplierNo']];
  22. $count = Db::name('filing')
  23. ->where($where)
  24. ->count('id');
  25. $list = Db::name('filing')
  26. ->field('id,filingCode,cat_id,good_name,num,wait_num,transfer_num,good_img,price,expect_service,companyName,companyCode,status,addtime,"" catinfo,orderCode,cgd_charge')
  27. ->where($where)
  28. ->page($data['page'], $data['size'])
  29. ->order('id', 'desc')
  30. ->withAttr('good_img', function ($val) {
  31. return explode(',', $val);
  32. })
  33. ->select()
  34. ->toArray();
  35. $all_cat = [];
  36. foreach ($list as &$value) {
  37. if (!isset($all_cat[$value['cat_id']])) $all_cat[$value['cat_id']] = implode('/', array_column(made($value['cat_id']), 'name'));
  38. $value['catinfo'] = $all_cat[$value['cat_id']];
  39. }
  40. return json_show(0, '获取报备单列表成功', ['count' => $count, 'list' => $list]);
  41. }
  42. //添加
  43. public function add()
  44. {
  45. $param = $this->request->filter(["trim",'strip_tags'])->only([
  46. 'customerCode',
  47. 'supplierNo',
  48. 'companyName',
  49. 'num',
  50. 'is_determine_price',
  51. 'price' => 0,
  52. 'expect_service_proportion',
  53. 'expect_service',
  54. 'cgd_charge' => 0,
  55. 'brand_id',
  56. 'fill_url',
  57. 'preservation_day',
  58. 'delivery_day',
  59. 'make_day',
  60. 'tax',
  61. 'unit_id',
  62. 'cat_id',
  63. 'spec_list' => [],
  64. 'good_name',
  65. 'origin_place',
  66. 'delivery_place',
  67. 'weight',
  68. 'supply_area',
  69. 'pay_way',
  70. 'gold_weight',
  71. 'noble_metal',
  72. 'config',
  73. 'other_config',
  74. 'remark',
  75. 'cost_desc',
  76. 'good_img'], 'post');
  77. $val = Validate::rule([
  78. 'customerCode|业务公司' => 'require|max:255',
  79. 'supplierNo|供应商' => 'require|max:255',
  80. 'companyName|客户名称' => 'require|max:255',
  81. 'num|销售数量' => 'require|number|gt:0|lt:999999999',
  82. 'is_determine_price|是否确定售价' => 'require|number|in:1,0',
  83. 'price|销售价' => 'requireIf:is_determine_price,1|float|max:99999999.99',
  84. 'expect_service_proportion|期望服务费比例' => 'require|float|between:0,100',
  85. 'expect_service|期望服务费' => 'require|float|max:99999999.99',
  86. 'cgd_charge|采购价' => 'requireIf:is_determine_price,0|float|max:99999999.99',
  87. 'brand_id|品牌' => 'require|number|gt:0',
  88. 'fill_url|报备附件' => 'require',
  89. 'preservation_day|有效期' => 'require|number|max:999999999',
  90. 'delivery_day|物流时间' => 'require|number|max:999999999',
  91. 'make_day|生产工期' => 'require|number|max:999999999',
  92. 'tax|税点' => 'require|number|between:0,100',
  93. 'unit_id|单位' => 'require|number|gt:0',
  94. 'cat_id|分类' => 'require|number|gt:0',
  95. 'spec_list|规格类型' => 'array|max:100',
  96. 'good_name|商品名称' => 'require|max:255',
  97. 'origin_place|产地' => 'require|array|length:3',
  98. 'delivery_place|发货地' => 'require|array|length:3',
  99. 'weight|总重量' => 'require|float|max:99999999.99',
  100. 'supply_area|供货区域' => 'require|number|in:1,2',
  101. 'pay_way|付款方式' => 'require|number|in:0,1,2',
  102. 'remark|采购备注' => 'require|max:255',
  103. 'cost_desc|工艺说明' => 'max:255',
  104. 'good_img|商品图片' => 'require|array|max:10',
  105. ]);
  106. if (!$val->check($param)) return json_show(1004, $val->getError());
  107. //如果是贵金属的话,额外判断
  108. $cat = made($param['cat_id']);
  109. if (isset($cat[0]['id']) && $cat[0]['id'] == 6) {
  110. $val_gold = Validate::rule([
  111. 'gold_weight|贵金属重量' => 'require|float|max:99999999.99',
  112. 'noble_metal|贵金属类型' => 'require|number|in:1,2,3',
  113. 'config|配置要求' => 'require|max:255',
  114. 'other_config|其他配置要求' => 'require|max:255',
  115. ]);
  116. if (!$val_gold->check($param)) return json_show(1004, $val_gold->getError());
  117. }
  118. $val2 = Validate::rule([
  119. 'spec_id|规格id' => 'require|number|gt:0',
  120. 'spec_value_id|规格值id' => 'require|number|gt:0',
  121. ]);
  122. $all_spec = Db::name('specs')
  123. ->whereIn('id', array_column($param['spec_list'], 'spec_id'))
  124. ->column('spec_name', 'id');
  125. $all_spec_value = Db::name('spec_value')
  126. ->whereIn('id', array_column($param['spec_list'], 'spec_value_id'))
  127. ->column('spec_value', 'id');
  128. foreach ($param['spec_list'] as &$spec_list) {
  129. if (!$val2->check($spec_list)) return json_show(1004, $val2->getError());
  130. $spec_list['spec_name'] = $all_spec[$spec_list['spec_id']] ?? '';
  131. $spec_list['spec_value_name'] = $all_spec_value[$spec_list['spec_value_id']] ?? '';
  132. }
  133. if ($param['is_determine_price'] == 1) $param['cgd_charge'] = round(bcsub($param['price'], $param['expect_service'], 3), 2);//确定售价时,采购价=售价-服务费
  134. else $param['price'] = round(bcadd($param['cgd_charge'], $param['expect_service'], 3), 2);//不确定售价时,售价=采购价+服务费
  135. $userCommon = \app\admin\common\User::getIns();
  136. $names = $userCommon->handle('getCodeAndName', ['code' => [$param['customerCode'], $param['supplierNo']]]);
  137. if (!isset($names['data'][$param['customerCode']]) || $names['data'][$param['customerCode']] == '') return json_show(1004, '该业务公司不存在');
  138. if (!isset($names['data'][$param['supplierNo']]) || $names['data'][$param['supplierNo']] == '') return json_show(1004, '该供应商不存在');
  139. $rs = Db::name('filing')
  140. ->strict(false)
  141. ->insert(array_merge($param, [
  142. 'customerName' => $names['data'][$param['customerCode']],
  143. 'supplierName' => $names['data'][$param['supplierNo']],
  144. 'filingCode' => makeNo('BM'),
  145. 'service_proportion' => $param['expect_service_proportion'],//服务费比例与期望服务费比例一致
  146. 'service_charge' => $param['expect_service'],//服务费与期望服务费一致
  147. 'specinfo' => json_encode($param['spec_list']),
  148. "wait_num"=>$param["num"],
  149. 'send_way' => 2,
  150. 'cert_fee' => 0,
  151. 'pakge_fee' => 0,
  152. 'cost_fee' => 0,
  153. 'mark_fee' => 0,
  154. 'demo_fee' => 0,
  155. 'open_fee' => 0,
  156. 'fill_url' =>$param['fill_url'],
  157. 'delivery_fee' => 0,
  158. 'is_gold_price' => 0,
  159. 'is_diff' => 0,
  160. 'apply_id' => $this->uid,
  161. 'apply_name' => $this->uname,
  162. 'status' => 0,
  163. 'is_del' => 0,
  164. 'addtime' => date('Y-m-d H:i:s'),
  165. 'updaterid' => $this->uid,
  166. 'updater' => $this->uname,
  167. 'updatetime' => date('Y-m-d H:i:s'),
  168. 'gold_weight' => $param['gold_weight'] ?? 0,
  169. 'noble_metal' => $param['noble_metal'] ?? 0,
  170. 'config' => $param['config'] ?? '',
  171. 'other_config' => $param['other_config'] ?? '',
  172. 'good_img' => implode(',', $param['good_img']),
  173. 'origin_place' => implode(',', $param['origin_place']),
  174. 'delivery_place' => implode(',', $param['delivery_place']),
  175. ]));
  176. return $rs ? json_show(0, '创建报备单成功') : json_show(1004, '创建报备单失败');
  177. }
  178. //添加
  179. public function save()
  180. {
  181. $param = $this->request->filter(['trim','strip_tags'])->only(["id",
  182. 'customerCode',
  183. 'supplierNo',
  184. 'companyName',
  185. 'num',
  186. 'is_determine_price',
  187. 'price' => 0,
  188. 'expect_service_proportion',
  189. 'expect_service',
  190. 'cgd_charge' => 0,
  191. 'brand_id',
  192. 'preservation_day',
  193. 'delivery_day',
  194. 'make_day',
  195. 'tax',
  196. 'fill_url' ,
  197. 'unit_id',
  198. 'cat_id',
  199. 'spec_list' => [],
  200. 'good_name',
  201. 'origin_place',
  202. 'delivery_place',
  203. 'weight',
  204. 'supply_area',
  205. 'pay_way',
  206. 'gold_weight',
  207. 'noble_metal',
  208. 'config',
  209. 'other_config',
  210. 'remark',
  211. 'cost_desc',
  212. 'good_img'], 'post');
  213. $val = Validate::rule([
  214. 'id|报备单主键id' => 'require',
  215. 'customerCode|业务公司' => 'require|max:255',
  216. 'supplierNo|供应商' => 'require|max:255',
  217. 'companyName|客户名称' => 'require|max:255',
  218. 'num|销售数量' => 'require|number|gt:0|lt:999999999',
  219. 'is_determine_price|是否确定售价' => 'require|number|in:1,0',
  220. 'price|销售价' => 'requireIf:is_determine_price,1|float|max:99999999.99',
  221. 'expect_service_proportion|期望服务费比例' => 'require|float|between:0,100',
  222. 'expect_service|期望服务费' => 'require|float|max:99999999.99',
  223. 'cgd_charge|采购价' => 'requireIf:is_determine_price,0|float|max:99999999.99',
  224. 'brand_id|品牌' => 'require|number|gt:0',
  225. 'preservation_day|有效期' => 'require|number|max:999999999',
  226. 'delivery_day|物流时间' => 'require|number|max:999999999',
  227. 'make_day|生产工期' => 'require|number|max:999999999',
  228. 'fill_url|报备附件' =>"require",
  229. 'tax|税点' => 'require|number|between:0,100',
  230. 'unit_id|单位' => 'require|number|gt:0',
  231. 'cat_id|分类' => 'require|number|gt:0',
  232. 'spec_list|规格类型' => 'array|max:100',
  233. 'good_name|商品名称' => 'require|max:255',
  234. 'origin_place|产地' => 'require|array|length:3',
  235. 'delivery_place|发货地' => 'require|array|length:3',
  236. 'weight|总重量' => 'require|float|max:99999999.99',
  237. 'supply_area|供货区域' => 'require|number|in:1,2',
  238. 'pay_way|付款方式' => 'require|number|in:0,1,2',
  239. 'remark|采购备注' => 'require|max:255',
  240. 'cost_desc|工艺说明' => 'max:255',
  241. 'good_img|商品图片' => 'require|array|max:10',
  242. ]);
  243. if (!$val->check($param)) return json_show(1004, $val->getError());
  244. $row= Db::name("filing")->findOrEmpty($param["id"]);
  245. if(empty($row)) return json_show(1004, "未找到数据");
  246. //如果是贵金属的话,额外判断
  247. $cat = made($param['cat_id']);
  248. if (isset($cat[0]['id']) && $cat[0]['id'] == 6) {
  249. $val_gold = Validate::rule([
  250. 'gold_weight|贵金属重量' => 'require|float|max:99999999.99',
  251. 'noble_metal|贵金属类型' => 'require|number|in:1,2,3',
  252. 'config|配置要求' => 'require|max:255',
  253. 'other_config|其他配置要求' => 'require|max:255',
  254. ]);
  255. if (!$val_gold->check($param)) return json_show(1004, $val_gold->getError());
  256. }
  257. $val2 = Validate::rule([
  258. 'spec_id|规格id' => 'require|number|gt:0',
  259. 'spec_value_id|规格值id' => 'require|number|gt:0',
  260. ]);
  261. $all_spec = Db::name('specs')
  262. ->whereIn('id', array_column($param['spec_list'], 'spec_id'))
  263. ->column('spec_name', 'id');
  264. $all_spec_value = Db::name('spec_value')
  265. ->whereIn('id', array_column($param['spec_list'], 'spec_value_id'))
  266. ->column('spec_value', 'id');
  267. foreach ($param['spec_list'] as &$spec_list) {
  268. if (!$val2->check($spec_list)) return json_show(1004, $val2->getError());
  269. $spec_list['spec_name'] = $all_spec[$spec_list['spec_id']] ?? '';
  270. $spec_list['spec_value_name'] = $all_spec_value[$spec_list['spec_value_id']] ?? '';
  271. }
  272. if ($param['is_determine_price'] == 1) $param['cgd_charge'] = round(bcsub($param['price'], $param['expect_service'], 3), 2);//确定售价时,采购价=售价-服务费
  273. else $param['price'] = round(bcadd($param['cgd_charge'], $param['expect_service'], 3), 2);//不确定售价时,售价=采购价+服务费
  274. $userCommon = \app\admin\common\User::getIns();
  275. $names = $userCommon->handle('getCodeAndName', ['code' => [$param['customerCode'], $param['supplierNo']]]);
  276. if (!isset($names['data'][$param['customerCode']]) || $names['data'][$param['customerCode']] == '') return json_show(1004, '该业务公司不存在');
  277. if (!isset($names['data'][$param['supplierNo']]) || $names['data'][$param['supplierNo']] == '') return json_show(1004, '该供应商不存在');
  278. $data=array_merge($param,[
  279. 'customerName' => $names['data'][$param['customerCode']],
  280. 'supplierName' => $names['data'][$param['supplierNo']],
  281. 'service_proportion' => $param['expect_service_proportion'],//服务费比例与期望服务费比例一致
  282. 'service_charge' => $param['expect_service'],//服务费与期望服务费一致
  283. 'specinfo' => json_encode($param['spec_list']),
  284. 'wait_num'=>$param['num'],
  285. 'send_way' => 2,
  286. 'status'=>0,
  287. 'is_check'=>0,
  288. 'fill_url'=>$param['fill_url'],
  289. 'apply_id' => $this->uid,
  290. 'apply_name' => $this->uname,
  291. 'updaterid' => $this->uid,
  292. 'updater' => $this->uname,
  293. 'updatetime' => date('Y-m-d H:i:s'),
  294. 'gold_weight' => $param['gold_weight'] ?? 0,
  295. 'noble_metal' => $param['noble_metal'] ?? 0,
  296. 'config' => $param['config'] ?? '',
  297. 'other_config' => $param['other_config'] ?? '',
  298. 'good_img' => implode(',', $param['good_img']),
  299. 'origin_place' => implode(',', $param['origin_place']),
  300. 'delivery_place' => implode(',', $param['delivery_place']),
  301. ]);
  302. $rs = Db::name('filing')
  303. ->save(array_intersect_key($data,$row));
  304. return $rs ? json_show(0, '报备单更新成功') : json_show(1004, '报备单更新失败');
  305. }
  306. //详情
  307. public function detail()
  308. {
  309. $param = $this->request->only(['id' => 0], 'post', 'trim');
  310. $rs = Db::name('filing')
  311. ->alias('a')
  312. ->field('a.*,b.brand_name,d.unit unit_name,p.platform_name,"" cat_info')
  313. ->leftJoin('brand b', 'b.id=a.brand_id')
  314. ->leftJoin('unit d', 'd.id=a.unit_id')
  315. ->leftJoin('platform p', 'p.id=a.platform_id')
  316. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  317. ->withAttr('specinfo', function ($val) {
  318. return json_decode($val);
  319. })
  320. ->withAttr('good_img', function ($val) {
  321. return explode(',', $val);
  322. })->withAttr('origin_place', function ($val) {
  323. return explode(',', $val);
  324. })->withAttr('delivery_place', function ($val) {
  325. return explode(',', $val);
  326. })->withAttr('cat_info', function ($val, $da) {
  327. return made($da['cat_id']);
  328. })
  329. ->findOrEmpty();
  330. if(!empty($rs['origin_place']))$origin = ["provice_code"=>$rs['origin_place'][0]??"","city_code"=>$rs['origin_place'][1]??'','area_code'=>$rs['origin_place'][2]??''];
  331. $rs["origin_place_cn"] = GetAddr(json_encode($origin));
  332. if(!empty($rs['delivery_place']))$deliev = ['provice_code'=>$rs['delivery_place'][0]??'','city_code'=>$rs['delivery_place'][1]??'','area_code'=>$rs['delivery_place'][2]??''];
  333. $rs['delivery_place_cn'] = GetAddr(json_encode($deliev));
  334. return json_show(0, '获取报备单详情成功', $rs);
  335. // return FilingLogic::detail($param);
  336. }
  337. //0 待审核 1 待修改 不合规 2 合规可转单 3 部分 4 转单成功 5取消转单
  338. public function status()
  339. {
  340. if ($this->level != 2) return json_show(1004, '业务公司账号不能审核');
  341. $param = $this->request->only(['id', 'companyCode', 'plat_code' => '','platform_id',"remark"=>"",'is_check'=>''], 'post');
  342. $val = Validate::rule([
  343. 'id' => 'require|number|gt:0',
  344. 'companyCode|客户' => 'requireIf:is_check,2|length:18',
  345. 'platform_id|平台id' => 'requireIf:is_check,2|number|gt:0',
  346. 'plat_code|平台商品编号' => 'requireIf:is_check,2|max:255',
  347. "is_check|是否合规"=> 'require|number|in:1,2',
  348. "remark|备注"=> 'requireIf:is_check,1',
  349. ]);
  350. if (!$val->check($param)) return json_show(1004, $val->getError());
  351. $rs = Db::name('filing')
  352. ->field('id,status,is_determine_price,price,cgd_charge,platform_id,customerCode,customerName')
  353. ->where(['is_del' => 0, 'id' => $param['id'],"status"=>0])
  354. ->findOrEmpty();
  355. if (empty($rs)) return json_show(1005, '该报备单不存在或不允许审核');
  356. $userCommon = \app\admin\common\User::getIns();
  357. $company = $userCommon->handle('cInfo', ['companyNo' => $param['companyCode']]);
  358. if (!isset($company['data'])) return json_show(1005, '该客户不存在');
  359. $tmp = Db::name('platform')
  360. ->where(['is_del' => 0, 'id' => $param['platform_id']])
  361. ->field('id,is_select_pay_rate,status')
  362. ->findOrEmpty();
  363. if (empty($tmp)) return json_show(1005, '该平台不存在');
  364. if ($tmp['status'] != 1) return error_show(1004, "平台信息已禁用");
  365. if($tmp['is_select_pay_rate']==1 && channel_is_company($param['platform_id'],$rs['customerCode']))return
  366. error_show(1004, "平台渠道包含当前业务公司 {$rs['customerName']}");
  367. //校验支付渠道中的业务公司是否与报备单业务公司重复
  368. $update = array_merge($param, [
  369. 'companyName' => $company['data']['companyName'],
  370. "status"=>$param["is_check"],
  371. 'updaterid' => $this->uid,
  372. 'updater' => $this->uname,
  373. 'updatetime' => date('Y-m-d H:i:s'),
  374. ]);
  375. //
  376. // if ($rs['is_determine_price'] == 1) $update['cgd_charge'] = round(bcsub($rs['price'], $param['service_charge'], 3), 2);//确定售价时,采购价=售价-服务费
  377. // else $update['price'] = round(bcadd($rs['cgd_charge'], $param['service_charge'], 3), 2);//不确定售价时,售价=采购价+服务费
  378. $res = Db::name('filing')
  379. ->where(['is_del' => 0, 'id' => $param['id']])
  380. ->whereIn('status', [0, 2])
  381. ->update($update);
  382. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  383. // return FilingLogic::status($param);
  384. }
  385. //取消转单
  386. public function cancel()
  387. {
  388. if ($this->level == 1) return json_show(1004, '超管不允许操作');
  389. $param = $this->request->only(['id'], 'post');
  390. $where = [
  391. ['is_del', '=', 0],
  392. ['id', '=', $param['id']],
  393. ['status', 'in', [0, 1, 2, 4]]
  394. ];
  395. $rs = Db::name('filing')
  396. ->field('id,status,supplierNo')
  397. ->where($where)
  398. ->findOrEmpty();
  399. if (empty($rs)) return json_show(1005, '该报备单不存在或不允许取消');
  400. if ($this->level == 2) {
  401. $supp = get_personid_by_supplierNo([$rs['supplierNo']]);
  402. if ($this->uid != $supp[$rs['supplierNo']]) return json_show(1004, '只有供应商负责人才能操作');
  403. }
  404. $res = Db::name('filing')
  405. ->where($where)
  406. ->update(['status' => 5, 'updatetime' => date('Y-m-d H:i:s'), 'updaterid' => $this->uid, 'updater' => $this->uname,]);
  407. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  408. }
  409. //转单
  410. public function transfer()
  411. {
  412. if ($this->level == 1) return json_show(1004, '超管不允许操作');
  413. $param = $this->request->only([
  414. 'id',
  415. 'addr_list'=>[],
  416. "customerCode"=>""], 'post');
  417. $val = Validate::rule([
  418. 'id|报备的主键id' => 'require|number|gt:0',
  419. "customerCode|客户编号"=>"require|max:255",
  420. 'addr_list|收货地址' => 'require|array|max:100',
  421. ]);
  422. if (!$val->check($param)) return json_show(1004, $val->getError());
  423. $val2 = Validate::rule([
  424. 'receipt_quantity|收货数量' => 'require|number|gt:0|max:999999999',
  425. 'contactor|联系人' => 'require|max:255',
  426. 'mobile|联系电话' => 'require|mobile',
  427. 'addr_code|收货省市区编码' => 'array|length:3',
  428. 'addr|详细地址' => 'require|max:255',
  429. ]);
  430. foreach ($param['addr_list'] as $addr_list) {
  431. if (!$val2->check($addr_list)) return json_show(1004, $val2->getError());
  432. }
  433. $filing = Db::name('filing')
  434. ->field(true)
  435. ->where(['is_del' => 0, 'id' => $param['id'], 'status' => [2,3]])
  436. ->findOrEmpty();
  437. if (empty($filing)) return json_show(1005, '该报备单不存在或不允许转单');
  438. $send_num = array_sum(array_column($param['addr_list'], 'receipt_quantity'));
  439. $wait_num = $filing['wait_num'] - $send_num ;
  440. if ($wait_num<0) return json_show(1004, '收货地址的收货总数超过报备单可转单数量');
  441. $userCommon = \app\admin\common\User::getIns();
  442. $supplier = $userCommon->handle('sInfo', ['code' => $filing['supplierNo']]);
  443. $names = $userCommon->handle('getCodeAndName', ['code' => [ $param['customerCode']]]);
  444. if (!isset($names['data'])) return json_show(1005, '该客户不存在');
  445. //level3账号都可以操作
  446. //level2账号的话,只有供应商负责人能操作
  447. if (($this->level == 2) && ($this->uid != $supplier['data']['personid'])) return json_show(1004, '只有供应商负责人才能操作');
  448. $date = date('Y-m-d H:i:s');
  449. Db::startTrans();
  450. try {
  451. $order_type = 4;//报备商品
  452. $order_source = 7;//报备转单
  453. $spuCode = makeNo('SKU');
  454. //商品表
  455. $goodIn= Db::name('good_zixun')
  456. ->insert([
  457. 'spuCode' => $spuCode,
  458. 'good_name' => $filing['good_name'],
  459. 'brand_id' => $filing['brand_id'],
  460. 'cat_id' => $filing['cat_id'],
  461. 'good_unit' => $filing['unit_id'],
  462. 'good_type' => 0,
  463. 'moq' => 1,
  464. 'customized' => $filing['make_day'],
  465. 'tax' => $filing['tax'],
  466. 'platform_id' => $filing['platform_id'],
  467. 'supplierNo' => $filing['supplierNo'],
  468. 'supplierName' => $supplier['data']['name'],
  469. 'is_auth' => 0,
  470. 'craft_desc' => $filing['good_name'],
  471. 'good_remark' => '',
  472. 'good_img' => $filing['good_img'],
  473. 'good_thumb_img' => '',
  474. 'good_info_img' => '',
  475. 'status' => 1,
  476. 'createrid' => $filing['updaterid'],
  477. 'creater' => $filing['updater'],
  478. 'is_del' => 0,
  479. 'addtime' => $date,
  480. 'updatetime' => $date,
  481. 'specinfo' => $filing['specinfo'],
  482. 'work_day' => $filing['make_day'],
  483. 'noble_metal' => $filing['noble_metal'],
  484. 'is_gold_price' => $filing['is_gold_price'],
  485. 'good_weight' => $filing['gold_weight'],
  486. 'config' => $filing['config'],
  487. 'other_config' => $filing['other_config'],
  488. 'weight' => $filing['weight'],
  489. 'supply_area' => $filing['supply_area'],
  490. 'is_diff' => $filing['is_diff'],
  491. 'pay_way' => $filing['pay_way'],
  492. 'send_way' => $filing['send_way'],
  493. 'companyNo' => $filing['customerCode'],
  494. 'companyName' => $filing['customerName'],
  495. 'proof_type' => '',
  496. 'proof_url' => '',
  497. 'order_type' => $order_type
  498. ]);
  499. if($goodIn==false) throw new \Exception("商品录入商品库失败");
  500. $orderCode = makeNo('QR');
  501. //订单表
  502. //sale
  503. $sale_id = Db::name('sale')->insertGetId([
  504. 'orderCode' => $orderCode,
  505. 'apply_id' => $filing['updaterid'],
  506. 'apply_name' => $filing['updater'],
  507. 'order_type' => $order_type,//报备商品
  508. 'order_source' => $order_source,//报备转单
  509. 'platform_id' => $filing['platform_id'],
  510. 'good_code' => $spuCode,
  511. 'skuCode' => '',
  512. 'cat_id' => $filing['cat_id'],
  513. 'good_name' => $filing['good_name'],
  514. 'good_num' => $send_num,
  515. 'good_type' => 1,
  516. 'origin_price' => $filing['cgd_charge'],
  517. 'sale_price' => $filing['price'],
  518. 'total_price' => round(bcmul($filing['price'], $filing['num'], 3), 2),
  519. 'post_fee' => 0,
  520. 'is_diff' => $filing['is_diff'],
  521. 'is_activity' => 0,
  522. 'activity_code' => '',
  523. 'is_stock' => 0,
  524. 'customer_code' => $param['customerCode'],
  525. 'customerName' => $names['data'][$param['customerCode']]??"",
  526. 'supplierNo' => $filing['customerCode'],
  527. 'supplierName' => $filing['customerName'],
  528. 'supNo' => $filing['supplierNo'],
  529. 'supName' => $filing['supplierName'],
  530. 'zxNo' => '',
  531. 'platform_order' =>$filing['filingCode'],
  532. 'send_num' => 0,
  533. 'wsend_num' => $send_num,
  534. 'th_num' => 0,
  535. 'th_fee' => 0,
  536. 'send_status' => 1,
  537. 'send_type' => 1,//直接发货
  538. 'remark' => '',
  539. 'status' => 0,
  540. 'is_del' => 0,
  541. 'proof_id' => 0,
  542. 'other_orderNo' => '',
  543. 'workNo' => '',
  544. 'poNo' => '',
  545. 'use_order' => 0,
  546. 'good_weight' => 0,
  547. 'gold_price' => 0,
  548. 'cost_price' => $filing['cost_fee'],
  549. 'diff_weight' => 0,
  550. 'diff_fee' => 0,
  551. 'returnCode' => '',
  552. 'addtime' => $date,
  553. 'updatetime' => $date,
  554. 'cgderid' => $supplier['data']['personid'],
  555. 'cgder' => $supplier['data']['person'],
  556. 'good_createrid' => $filing['updaterid'],
  557. 'good_creater' => $filing['updater'],
  558. ]);
  559. if($sale_id==false) throw new \Exception('订单生成失败');
  560. //仓库
  561. $wsm = Db::name('warehouse_info')
  562. ->field('id,wsm_code')
  563. ->where(['supplierNo' => $filing['supplierNo'], 'companyNo' => $filing['customerCode'], 'is_del' => 0])
  564. ->findOrEmpty();
  565. if (empty($wsm)) {
  566. $wsm_code = makeNo('WSM');
  567. $ws= Db::name('warehouse_info')->insert([
  568. 'wsm_code' => $wsm_code,
  569. 'name' => $supplier['data']['name'],
  570. 'wsm_type' => 2,
  571. 'supplierNo' => $supplier['data']['code'],
  572. 'addr' => '',
  573. 'addrs_code' => '',
  574. 'contactor' => 0,
  575. 'contactor_name' => 0,
  576. 'mobile' => '',
  577. 'position' => '',
  578. 'companyNo' => $filing['supplierNo'],
  579. 'status' => 1,
  580. 'is_del' => 0,
  581. 'addtime' => $date,
  582. 'updatetime' => $date,
  583. ]);
  584. if($ws==false)throw new \Exception('仓库生成失败');
  585. } else $wsm_code = $wsm['wsm_code'];
  586. //采购单
  587. $cgdCode = makeNo('CG');
  588. $cg= Db::name('purchease_order')->insert([
  589. 'cgdNo' => $cgdCode,
  590. 'bkcode' => '',
  591. 'wsm_code' => $wsm_code,
  592. 'cgder' => $supplier['data']['person'],
  593. 'cgder_id' => $supplier['data']['personid'],
  594. 'spuCode' => $spuCode,
  595. 'good_name' => $filing['good_name'],
  596. 'good_num' => $send_num,
  597. 'good_price' => $filing['cgd_charge'],
  598. 'total_fee' => round(bcmul($filing['cgd_charge'], $filing['num']), 2),
  599. 'pakge_fee' => $filing['pakge_fee'],
  600. 'cert_fee' => $filing['cert_fee'],
  601. 'open_fee' => $filing['open_fee'],
  602. 'delivery_fee' => $filing['delivery_fee'],
  603. 'mark_fee' => $filing['mark_fee'],
  604. 'demo_fee' => $filing['demo_fee'],
  605. 'diff_weight' => '0',
  606. 'diff_fee' => '0',
  607. 'gold_price' => '0',
  608. 'supplierNo' => $filing['supplierNo'],
  609. 'supplier_name' => $filing['supplierName'],
  610. 'companyNo' => $filing['customerCode'],
  611. 'companyName' => $filing['customerName'],
  612. 'send_status' => 1,
  613. 'send_num' => '0',
  614. 'wsend_num' => $send_num,
  615. 'remark' => '',
  616. 'status' => 0,
  617. 'lasttime' => $date,
  618. 'is_del' => 0,
  619. 'order_type' => $order_type,
  620. 'order_source' => $order_source,
  621. 'good_type' => 1,
  622. 'addtime' => $date,
  623. 'updatetime' => $date,
  624. 'good_createrid' => $filing['updaterid'],
  625. 'good_creater' => $filing['updater'],
  626. ]);
  627. if($cg==false)throw new \Exception('采购单生成失败');
  628. //台账
  629. $standing_bood_data = [
  630. 'standBookNo' => makeNo('IO'),
  631. 'orderCode' => $orderCode,
  632. 'sale_id' => $sale_id,
  633. 'customer_code' => $filing['companyCode'],
  634. 'supplierNo' => $filing['supplierNo'],
  635. 'order_type' => $order_type,
  636. 'order_source' => $order_source,
  637. 'spuCode' => $spuCode,
  638. 'addtime' => $date,
  639. 'updatetime' => $date,
  640. 'cgdNo' => $cgdCode,
  641. ];
  642. $i = 0;
  643. $order_send_insert = [];
  644. foreach ($param['addr_list'] as $addr) {
  645. //地址addr
  646. $addrid = Db::name('order_addr')->insertGetId([
  647. 'orderCode' => $orderCode,
  648. 'addr' => $addr['addr'],
  649. 'addr_code' => implode(',', $addr['addr_code']),
  650. 'contactor' => $addr['contactor'],
  651. 'mobile' => $addr['mobile'],
  652. 'customer_code' => $param['customerCode'],
  653. 'post_fee' => 0,
  654. 'receipt_quantity' => $addr['receipt_quantity'],
  655. 'is_del' => 0,
  656. 'addtime' => $date,
  657. 'updatetime' => $date,
  658. ]);
  659. if($addrid==false)throw new \Exception('地址生成失败');
  660. //发货单
  661. $outCode = makeNo('DF');
  662. //改变编码规则,将原来的编码后两位换成序列号
  663. //str_pad字符串填充
  664. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  665. $send= Db::name('order_out')->insert([
  666. 'orderCode' => $orderCode,
  667. 'outCode' => $outCode,
  668. 'apply_id' => $filing['updaterid'],
  669. 'apply_name' => $filing['updater'],
  670. 'addrid' => $addrid,
  671. 'post_name' => '',
  672. 'post_code' => '',
  673. 'post_fee' => 0,
  674. 'sendtime' => $date,
  675. 'send_num' => $addr['receipt_quantity'],
  676. 'check_num' => 0,
  677. 'error_num' => 0,
  678. 'wsm_code' => $wsm_code,
  679. 'order_type' => $order_type,
  680. 'status' => 0,
  681. 'addtime' => $date,
  682. 'updatetime' => $date,
  683. ]);
  684. if($send==false) throw new \Exception('发货单生成失败');
  685. $standing_bood_data['outCode'][] = $outCode;
  686. $order_send_insert[] = [
  687. 'cgdNo' => $cgdCode,
  688. 'outCode' => $outCode,
  689. 'send_num' => $addr['receipt_quantity'],
  690. 'bnCode' => '',
  691. 'status' => 1,
  692. 'addtime' => $date,
  693. 'updatetime' => $date,
  694. ];
  695. $i++;
  696. }
  697. //更新报备单
  698. $fillup= Db::name('filing')
  699. ->where(['is_del' => 0, 'id' => $param['id'], 'status' => [2,3]])
  700. ->update([
  701. 'status' =>$wait_num>0?3:4,
  702. 'wait_num' =>$wait_num,
  703. 'transfer_num' =>$filing['transfer_num']+$send_num,
  704. 'updatetime' => $date,
  705. 'spuCode' => $spuCode,
  706. 'orderCode' =>$filing["orderCode"].$orderCode.",",
  707. 'reason' => '',
  708. 'updaterid' => $this->uid,
  709. 'updater' => $this->uname]);
  710. if($fillup==false)throw new \Exception('报备单更新失败');
  711. //待办已办先不处理
  712. //关联表
  713. $gu= Db::name('order_num')
  714. ->insert([
  715. 'orderCode' => $orderCode,
  716. 'cgdNo' => $cgdCode,
  717. 'spuCode' => $spuCode,
  718. 'companyNo' => $filing['customerCode'],
  719. 'good_num' => $send_num,
  720. 'wsend_num' =>$send_num,
  721. 'send_num' => 0,
  722. 'wait_num' => $send_num,
  723. ]);
  724. if($gu==false) throw new \Exception('关联表新建失败');
  725. if ($order_send_insert) Db::name('order_send')->insertAll($order_send_insert);
  726. //处理台账
  727. if (isset($standing_bood_data['outCode']) && is_array($standing_bood_data['outCode'])) $standing_bood_data['outCode'] = implode(',', $standing_bood_data['outCode']);
  728. Db::name('standing_book')->insert($standing_bood_data);
  729. Db::commit();
  730. return json_show(0, '转单成功');
  731. } catch (\Exception $exception) {
  732. Db::rollback();
  733. return json_show(1005, '转单失败,' . $exception->getMessage());
  734. }
  735. }
  736. //订单录入
  737. public function orderAdd()
  738. {
  739. $param = $this->request->param([
  740. "companyNo" => '',
  741. "poCode" => "",
  742. "workCode" => "",
  743. "khNo" => "",
  744. "qrdType" => 3,
  745. "goodName" => "",
  746. "tax" => "",
  747. "goodNum" => "",
  748. "goodPrice" => "",
  749. "sale_total" => "",
  750. "goodUnit" => "",
  751. "mobile" => "",
  752. "addr" => "",
  753. "addr_code" => "",
  754. "contactor" => "",
  755. "sendtime" => "",
  756. "cat_id" => '',
  757. "supplierNo" => "",
  758. "cgdPrice" => "",
  759. "cgd_total" => "",
  760. "platform_id" => "0",
  761. ], "post", "trim");
  762. $valid = Validate::rule([
  763. "companyNo|业务公司编号" => "require|max:255|min:1",
  764. "supplierNo|业务公司编号" => "require|max:255|min:1",
  765. "poCode|PO编号" => "require|max:255|min:1",
  766. "khNo|客户公司编号" => "require|max:255|min:1",
  767. "qrdType|订单类型" => "require|number|in:1,2,3",
  768. "goodName|商品名称" => "require|max:255|min:1",
  769. "goodUnit|商品单位" => "require|number|gt:0",
  770. "tax|税率" => "require|number|gt:0",
  771. "goodNum|商品数量" => "require|number|gt:0",
  772. "sale_total|销售总额" => "require|float|gt:0",
  773. "cgd_total|采购总额" => "require|float|gt:0",
  774. // "goodPrice|商品单价" => "require|float|gt:0",
  775. "mobile|联系人电话" => "require|number|mobile",
  776. "contactor|联系人" => "require|max:255|min:1",
  777. "addr|收货地址" => "require|max:255|min:1",
  778. "sendtime|发货时间" => "require|date",
  779. "cat_id|商品分类id" => "require|number|gt:0",
  780. // "cgdPrice|采购单价" => "require|float|gt:0",
  781. "platform_id|平台" => "require|number"
  782. ]);
  783. if ($valid->check($param) == false) return error_show(1004, $valid->getError());
  784. $qrdcode = makeNo("QR");
  785. $cgddcode = makeNo("CG");
  786. $goodCode = makeNo("SKU");
  787. $userCommon = \app\admin\common\User::getIns();
  788. $tmp = $userCommon->handle('getCodeAndName',['code'=>[$param['khNo'],$param['supplierNo'],$param['companyNo']]]);
  789. if(!isset($tmp['code']) || $tmp['code']!=0) return json_show($tmp['code'],$tmp['message'],$tmp['data']);
  790. if(!isset($tmp['data'][$param['khNo']])) return json_show(1004, "未找到客户数据");
  791. if(!isset($tmp['data'][$param['supplierNo']])) return json_show(1004, "未找到平台供应商数据");
  792. if(!isset($tmp['data'][$param['companyNo']])) return json_show(1004, "未找到平台业务公司数据");
  793. $payinfo = Db::name("platform")->where(["id"=> $param['platform_id'],"is_del"=>0])->findOrEmpty();
  794. if (empty($payinfo)) return error_show(1004, "平台信息未找到");
  795. if ($payinfo['status'] != 1) return error_show(1004, "平台信息已禁用");
  796. if($payinfo['is_select_pay_rate']==1 && channel_is_company($param['platform_id'],$param['companyNo']))return error_show(1004, "平台渠道包含当前业务公司 {$tmp['data'][$param['companyNo']]}");
  797. $supplierinfo = $userCommon->handle("hqInfo",["code"=>$param['supplierNo']]);
  798. if(!isset($supplierinfo['data'])|| empty($supplierinfo['data'])) return json_show(1004, "未找到供应商公司数据");
  799. $person =$supplierinfo['data']['child']??["person"=>'',"person_id"=>0];
  800. if($param['companyNo']== $supplierinfo['data']['relation_code']) return json_show(1004,'供应商和业务公司不能为同一家公司');
  801. $cgdprice = round($param['cgd_total']/$param['goodNum'],2);
  802. $saleprice = round($param['sale_total']/$param['goodNum'],2);
  803. $qrddata = [
  804. "orderCode" => $qrdcode,
  805. "apply_id" => $this->uid,
  806. "apply_name" => $this->uname,
  807. "order_type" => $param['qrdType'],
  808. "order_source" => 9,
  809. "platform_id" => $param['platform_id'],
  810. "good_code" => $goodCode,
  811. "skuCode" => '',
  812. "cat_id" => $param['cat_id'],
  813. "good_name" => $param['goodName'],
  814. "good_num" => $param['goodNum'],
  815. "good_type" => 1,
  816. "origin_price" => $cgdprice,
  817. "sale_price" => $saleprice,
  818. "total_price" =>$param['sale_total'],
  819. "post_fee" => 0,
  820. "is_diff" => 0,
  821. "is_activity" => 0,
  822. "activity_code" => '',
  823. "is_stock" => 0,
  824. "arrive_time" => $param['sendtime'],
  825. "customer_code" => $param['khNo'],
  826. "customerName" => $tmp['data'][$param['khNo']],
  827. "supplierNo" => $param['companyNo'],
  828. "supplierName" =>$tmp['data'][$param['companyNo']],
  829. "supName" => $tmp['data'][$param['supplierNo']],
  830. "supNo" => $param['supplierNo'],
  831. "platform_order" => $param['poCode'],
  832. "send_num" => $param['goodNum'],
  833. "wsend_num" => 0,
  834. "send_status" => 3,
  835. "send_type" => 1,
  836. "status" => 2,
  837. "is_del" => 0,
  838. "pay_id" => 0,
  839. "workNo" => $param['workCode'],
  840. "addtime" => date("Y-m-d H:i:s"),
  841. "updatetime" => date("Y-m-d H:i:s"),
  842. "cgderid" => $person['personid'],
  843. "cgder" => $person['person'],
  844. "good_createrid" => $this->uid,
  845. "good_creater" => $this->uname
  846. ];
  847. $orderCgd = [
  848. "orderCode" => $qrdcode,
  849. "cgdNo" => $cgddcode,
  850. "spuCode" => $goodCode,
  851. "good_num" => $param['goodNum'],
  852. "wsend_num" => 0,
  853. "send_num" => $param['goodNum'],
  854. "wait_num" => 0
  855. ];
  856. $cgddata = [
  857. "cgdNo" => $cgddcode,
  858. "bkcode" => '',
  859. "wsm_code" => "",
  860. "cgder_id" => $person['personid'],
  861. 'cgder' => $person['person'],
  862. "spuCode" => $goodCode,
  863. "good_name" => $param['goodName'],
  864. "good_num" => $param['goodNum'],
  865. "good_price" => $cgdprice,
  866. "total_fee" => $param['cgd_total'],
  867. "supplierNo" => $param['supplierNo'],
  868. "supplier_name" =>$tmp['data'][$param['supplierNo']],
  869. "companyNo" => $param['companyNo'],
  870. "companyName" => $tmp['data'][$param['companyNo']],
  871. "send_status" => 3,
  872. "send_num" => $param['goodNum'],
  873. "wsend_num" => 0,
  874. "status" => 3,
  875. "lasttime" => date("Y-m-d H:i:s"),
  876. "order_type" => $param['qrdType'],
  877. "order_source" => 9,
  878. "addtime" => date("Y-m-d H:i:s"),
  879. "updatetime" => date("Y-m-d H:i:s"),
  880. "good_createrid" => $this->uid,
  881. "good_creater" => $this->uname,
  882. ];
  883. $goodinfo = [
  884. "spuCode" => $goodCode,
  885. "good_name" => $param['goodName'],
  886. "brand_id" => 0,
  887. "cat_id" => $param['cat_id'],
  888. "good_unit" => $param['goodUnit'],
  889. "good_type" => 1,
  890. "moq" => 1,
  891. "customized" => 0,
  892. "platform_id" => $param['platform_id'],
  893. "tax" => $param['tax'],
  894. "supplierNo" => $param['supplierNo'],
  895. "supplierName" =>$tmp['data'][$param['supplierNo']],
  896. "is_auth" => 1,
  897. "craft_desc" => '',
  898. "good_remark" => '',
  899. "good_img" => '',
  900. "good_thumb_img" => '',
  901. "good_info_img" => '',
  902. "createrid" => $this->uid,
  903. "creater" => $this->uname,
  904. "specinfo" => '',
  905. "work_day" => 0,
  906. "noble_metal" => 0,
  907. "good_weight" => 0,
  908. "config" => "",
  909. "weight" => 0,
  910. "supply_area" => 0,
  911. "is_diff" => 0,
  912. "pay_way" => 0,
  913. "send_way" => 0,
  914. "companyNo" => $param['companyNo'],
  915. "companyName" => $tmp['data'][$param['companyNo']],
  916. "addtime" => date("Y-m-d H:i:s"),
  917. "updatetime" => date("Y-m-d H:i:s"),
  918. ];
  919. $orderAddr = [
  920. "orderCode" => $qrdcode,
  921. "addr" => $param['addr'],
  922. "addr_code" => $param['addr_code'],
  923. "contactor" => $param['contactor'],
  924. "mobile" => $param['mobile'],
  925. "arrive_time" => $param['sendtime'],
  926. "customer_code" => $param['khNo'],
  927. "receipt_quantity" => $param['goodNum'],
  928. "addtime" => date("Y-m-d H:i:s"),
  929. "updatetime" => date("Y-m-d H:i:s"),
  930. ];
  931. Db::startTrans();
  932. try {
  933. $qrd = Db::name("sale")->insert($qrddata);
  934. if ($qrd == false) throw new \Exception("销售单生成失败");
  935. $qrd = Db::name("order_num")->insert($orderCgd);
  936. if ($qrd == false) throw new \Exception("销售单采购管联生成失败");
  937. $qrd = Db::name("purchease_order")->insert($cgddata);
  938. if ($qrd == false) throw new \Exception("采购单生成失败");
  939. $qrd = Db::name("good_zixun")->insert($goodinfo);
  940. if ($qrd == false) throw new \Exception("商品信息录入生成失败");
  941. $qrd = Db::name("order_addr")->insert($orderAddr);
  942. if ($qrd == false) throw new \Exception("销售单地址生成失败");
  943. Db::commit();
  944. return app_show(0, '订单生成成功');
  945. } catch (\Exception $exception) {
  946. Db::rollback();
  947. return error_show(1004, $exception->getMessage());
  948. }
  949. }
  950. public function orderInfo()
  951. {
  952. $param = $this->request->param(["orderCode"], "post", "trim");
  953. if ($param['orderCode'] == '') return error_show(1004, "订单编号不能为空");
  954. $saleinfo = Db::name("sale")->where(["orderCode" => $param['orderCode']])->findOrEmpty();
  955. if (empty($saleinfo)) return error_show(1004, "销售单不存在");
  956. if ($saleinfo['order_source'] != 9) return error_show(1004, "销售单来源不是网络录入");
  957. $cgd = Db::name("purchease_order")->alias("a")
  958. ->leftJoin("order_num b", "a.cgdNo=b.cgdNo")
  959. ->where(["b.orderCode" => $param['orderCode']])
  960. ->field("a.*")
  961. ->findOrEmpty();
  962. if (empty($cgd)) return error_show(1004, "采购单不存在");
  963. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $saleinfo['good_code']])->findOrEmpty();
  964. if (empty($goodinfo)) return error_show(1004, "商品信息不存在");
  965. $addr = Db::name("order_addr")->where(["orderCode" => $param['orderCode']])->findOrEmpty();
  966. if (empty($addr)) return error_show(1004, "地址信息不存在");
  967. $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : '';
  968. if (is_string($addinfo) && $addinfo != '') {
  969. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  970. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']);
  971. }
  972. $platform = Db::name("platform")->where(["id" => $saleinfo['platform_id']])->field("platform_name,pay_title")
  973. ->findOrEmpty();
  974. $userCommon =\app\admin\common\User::getIns();
  975. $names = $userCommon->handle('getCodeAndName', ['code' => [$saleinfo['supplierNo'], $saleinfo['customer_code']]]);
  976. $data = [
  977. "orderCode" => $saleinfo['orderCode'],
  978. "companyNo" => $saleinfo['supplierNo'],
  979. "companyName" => $names['data'][$saleinfo['supplierNo']] ?? '',//Db::name("supplier")->where(["code" => $saleinfo['supplierNo']])->value("name", ''),
  980. "poCode" => $saleinfo['platform_order'],
  981. "workCode" => $saleinfo['workNo'],
  982. "platform_id" => $saleinfo['platform_id'],
  983. "platform_name" =>$platform['platform_name']??"",
  984. "khNo" => $saleinfo['customer_code'],
  985. "khName" => $names['data'][$saleinfo['customer_code']] ?? '',//Db::name("customer_info")->where(["companyNo" => $saleinfo['customer_code']])->value("companyName", ''),
  986. "qrdType" => $saleinfo['order_type'],
  987. "goodName" => $saleinfo['good_name'],
  988. "tax" => $goodinfo['tax'],
  989. "goodUnit" => $goodinfo['good_unit'],
  990. "unitName" => Db::name("unit")->where(["id" => $goodinfo['good_unit']])->value("unit", ""),
  991. "goodNum" => $saleinfo['good_num'],
  992. "goodPrice" => $saleinfo['sale_price'],
  993. "sale_total" => $saleinfo['total_price'],
  994. "mobile" => $addr['mobile'],
  995. "addr" => $addr['addr'],
  996. "addr_cn" => GetAddr(json_encode($addinfo)),
  997. "addr_code" => $addr['addr_code'],
  998. "contactor" => $addr['contactor'],
  999. "buyerid" => $saleinfo['cgderid'],
  1000. "buyer_name" => $saleinfo['cgder'],
  1001. "sendtime" => $saleinfo['arrive_time'],
  1002. "cat_id" => made($saleinfo['cat_id']),
  1003. "cat_name" => Db::name("cat")->where(["id" => $saleinfo['cat_id']])->value("cat_name", ''),
  1004. "supplierNo" => $cgd['supplierNo'],
  1005. "supplierNanme" => $cgd['supplier_name'],
  1006. "cgd_tax" => round((1 - $cgd['total_fee']/ $saleinfo['total_price']), 4) * 100,
  1007. "cgdPrice" => $saleinfo['origin_price'],
  1008. "cgd_total" => $cgd['total_fee'],
  1009. "pay_id" => $saleinfo['pay_id'],
  1010. "pay_name" =>$platform['pay_title']??"",
  1011. ];
  1012. return app_show(0, "获取成功", $data);
  1013. }
  1014. public function orderEdit()
  1015. {
  1016. $param = $this->request->param([
  1017. "orderCode" => "",
  1018. "poCode" => "",
  1019. "workCode" => "",
  1020. "tax" => "",
  1021. "goodUnit" => "",
  1022. "mobile" => "",
  1023. "addr" => "",
  1024. "addr_code" => "",
  1025. "contactor" => "",
  1026. "sendtime" => "",
  1027. "supplierNo" => "",
  1028. "cgd_total" => "",
  1029. // "sale_total" => "",
  1030. ], "post", "trim");
  1031. $valid = Validate::rule([
  1032. "orderCode|销售单号" => "require|max:255",
  1033. "supplierNo|供应商公司编号" => "require|max:255|min:1",
  1034. "poCode|PO编号" => "require|max:255|min:1",
  1035. "mobile|联系人电话" => "require|number|mobile",
  1036. "goodUnit|商品单位" => "require|number|gt:0",
  1037. "contactor|联系人" => "require|max:255|min:1",
  1038. "addr|收货地址" => "require|max:255|min:1",
  1039. "sendtime|发货时间" => "require|date",
  1040. "tax|商品利率" => "require|number",
  1041. "cgd_total|采购总额" => "require|float|gt:0",
  1042. // "sale_total|销售总额" => "require|float|gt:0",
  1043. ]);
  1044. if ($valid->check($param) == false) return error_show(1004, $valid->getError());
  1045. $saleinfo = Db::name("sale")->where(["orderCode" => $param['orderCode']])->findOrEmpty();
  1046. if (empty($saleinfo)) return error_show(1004, "销售单不存在");
  1047. if ($saleinfo['order_source'] != 9) return error_show(1004, "销售单来源不是网络录入");
  1048. $cgd = Db::name("purchease_order")->alias("a")
  1049. ->leftJoin("order_num b", "a.cgdNo=b.cgdNo")
  1050. ->where(["b.orderCode" => $param['orderCode']])
  1051. ->field("a.*")
  1052. ->findOrEmpty();
  1053. if (empty($cgd)) return error_show(1004, "采购单不存在");
  1054. $userCommon = \app\admin\common\User::getIns();
  1055. $good = Db::name("good_zixun")->where(["spuCode" => $saleinfo['good_code']])->findOrEmpty();
  1056. if (empty($good)) return error_show(1004, "商品信息不存在");
  1057. $addr = Db::name("order_addr")->where(["orderCode" => $param['orderCode']])->findOrEmpty();
  1058. if (empty($addr)) return error_show(1004, "地址信息不存在");
  1059. $cgdPrice = round($param['cgd_total']/$saleinfo['good_num'],2);
  1060. // $salePrice = round($param['sale_total']/$saleinfo['good_num'],2);
  1061. $supplierinfo = $userCommon->handle("hqInfo",["code"=>$param['supplierNo']]);
  1062. if(!isset($supplierinfo['data'])|| empty($supplierinfo['data'])) return json_show(1004, "未找到供应商公司数据");
  1063. $person =$supplierinfo['data']['child']??["person"=>'',"person_id"=>0];
  1064. if($cgd['companyNo']== $supplierinfo['data']['relation_code']) return json_show(1004,'供应商和业务公司不能为同一家公司');
  1065. $qrddata = [
  1066. "origin_price" => $cgdPrice,
  1067. "arrive_time" => $param['sendtime'],
  1068. "platform_order" => $param['poCode'],
  1069. "workNo" => $param['workCode'],
  1070. "updatetime" => date("Y-m-d H:i:s"),
  1071. "cgderid" => $person['personid'],
  1072. "cgder" => $person['person']
  1073. ];
  1074. $cgddata = [
  1075. "cgder_id" => $person['personid'],
  1076. 'cgder' => $person['person'],
  1077. "good_price" => $cgdPrice,
  1078. "total_fee" => $param['cgd_total'],
  1079. "supplierNo" => $param['supplierNo'],
  1080. "supplier_name" => $supplierinfo['data']['name'],
  1081. // "companyNo"=>$param['companyNo'],
  1082. "updatetime" => date("Y-m-d H:i:s"),
  1083. ];
  1084. $goodinfo = [
  1085. "good_unit" => $param['goodUnit'],
  1086. "tax" => $param['tax'],
  1087. "supplierNo" => $param['supplierNo'],
  1088. // "companyNo" => $param['companyNo'],
  1089. "updatetime" => date("Y-m-d H:i:s"),
  1090. ];
  1091. $orderAddr = [
  1092. "addr" => $param['addr'],
  1093. "addr_code" => $param['addr_code'],
  1094. "contactor" => $param['contactor'],
  1095. "mobile" => $param['mobile'],
  1096. "arrive_time" => $param['sendtime'],
  1097. // "customer_code" => $param['khNo'],
  1098. "updatetime" => date("Y-m-d H:i:s"),
  1099. ];
  1100. Db::startTrans();
  1101. try {
  1102. $qrd = Db::name("sale")->where($saleinfo)->update($qrddata);
  1103. if ($qrd == false) throw new \Exception("销售单更新失败");
  1104. $qrd = Db::name("purchease_order")->where($cgd)->update($cgddata);
  1105. if ($qrd == false) throw new \Exception("采购单更新失败");
  1106. $qrd = Db::name("good_zixun")->where($good)->update($goodinfo);
  1107. if ($qrd == false) throw new \Exception("商品信息更新失败");
  1108. $qrd = Db::name("order_addr")->where($addr)->update($orderAddr);
  1109. if ($qrd == false) throw new \Exception("销售单地址更新失败");
  1110. Db::commit();
  1111. return app_show(0, '订单更新成功');
  1112. } catch (\Exception $exception) {
  1113. Db::rollback();
  1114. return error_show(1004, $exception->getMessage());
  1115. }
  1116. }
  1117. }