StatSaleClass.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatStockInClass;
  4. use biz\stat\classes\StatStockOutClass;
  5. use bizGhs\cg\classes\CgRefundClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\expend\classes\ExpendClass;
  8. use bizGhs\item\classes\ItemClassClass;
  9. use bizGhs\order\classes\OrderItemClass;
  10. use bizGhs\order\classes\PurchaseOrderClass;
  11. use bizGhs\order\classes\PurchaseOrderItemClass;
  12. use bizGhs\order\classes\RefundOrderClass;
  13. use bizGhs\order\classes\StockWastageOrderClass;
  14. use bizGhs\product\classes\ProductClass;
  15. use bizGhs\staff\classes\SalaryClass;
  16. use bizHd\goods\classes\PlantCgClass;
  17. use bizHd\order\classes\OrderClass as HdOrderClass;
  18. use bizGhs\order\classes\OrderClass;
  19. use bizHd\purchase\classes\PurchaseClass;
  20. use bizHd\refund\classes\HdRefundClass;
  21. use common\components\arrayUtil;
  22. use common\components\dateUtil;
  23. use common\components\dict;
  24. use common\components\util;
  25. use Yii;
  26. use bizGhs\base\classes\BaseClass;
  27. class StatSaleClass extends BaseClass
  28. {
  29. public static $baseFile = '\bizGhs\stat\models\StatSale';
  30. public static function wastage($mainId)
  31. {
  32. $get = Yii::$app->request->get();
  33. $where = [];
  34. $where['mainId'] = $mainId;
  35. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  36. $startTime = $get['startTime'] ?? '';
  37. $endTime = $get['endTime'] ?? '';
  38. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  39. $start = $period['startTime'];
  40. $end = $period['endTime'];
  41. $currentStartDate = date('Y-m-d', strtotime($start));
  42. $currentEndDate = date('Y-m-d', strtotime($end));
  43. $currentStartTime = $currentStartDate . ' 00:00:00';
  44. $currentEndTime = $currentEndDate . ' 23:59:59';
  45. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  46. $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
  47. $totalPrice = 0;
  48. if (!empty($list)) {
  49. foreach ($list as $order) {
  50. $price = $order['price'] ?? 0;
  51. $totalPrice = bcadd($totalPrice, $price, 2);
  52. }
  53. }
  54. $totalPrice = floatval($totalPrice);
  55. return ['totalPrice' => $totalPrice];
  56. }
  57. //结账跟踪 ssh 20230503
  58. public static function OrderClear($mainId)
  59. {
  60. $get = Yii::$app->request->get();
  61. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  62. $timeStyle = $get['timeStyle'] ?? 0;
  63. if (isset($get['searchTime']) && !empty($get['searchTime'])) {
  64. $searchTime = $get['searchTime'];
  65. $startTime = $get['startTime'] ?? '';
  66. $endTime = $get['endTime'] ?? '';
  67. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  68. $start = $period['startTime'];
  69. $end = $period['endTime'];
  70. $currentStartDate = date('Y-m-d', strtotime($start));
  71. $currentEndDate = date('Y-m-d', strtotime($end));
  72. $currentStartTime = $currentStartDate . ' 00:00:00';
  73. $currentEndTime = $currentEndDate . ' 23:59:59';
  74. if ($timeStyle == 0) {
  75. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  76. } else {
  77. $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
  78. }
  79. } else {
  80. $payTime = 'today';
  81. $startTime = $get['startTime'] ?? '';
  82. $endTime = $get['endTime'] ?? '';
  83. $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
  84. $start = $period['startTime'];
  85. $end = $period['endTime'];
  86. $currentStartDate = date('Y-m-d', strtotime($start));
  87. $currentEndDate = date('Y-m-d', strtotime($end));
  88. $currentStartTime = $currentStartDate . ' 00:00:00';
  89. $currentEndTime = $currentEndDate . ' 23:59:59';
  90. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  91. }
  92. $customId = $get['customId'] ?? 0;
  93. if (!empty($customId)) {
  94. $where['customId'] = $customId;
  95. }
  96. $unClear = $get['unClear'] ?? 0;
  97. if ($unClear == 1) {
  98. $where['clearId'] = 0;
  99. $where['remainDebtPrice>'] = 0;
  100. }
  101. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  102. return ['list' => $ghsOrderList];
  103. }
  104. //客户下单榜 ssh 20230502
  105. public static function statCustomCg($mainId)
  106. {
  107. $get = Yii::$app->request->get();
  108. $where = [];
  109. $where['mainId'] = $mainId;
  110. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  111. $startTime = $get['startTime'] ?? '';
  112. $endTime = $get['endTime'] ?? '';
  113. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  114. $start = $period['startTime'];
  115. $end = $period['endTime'];
  116. $where['time'] = ['between', [$start, $end]];
  117. $currentStartDate = date('Y-m-d', strtotime($start));
  118. $currentEndDate = date('Y-m-d', strtotime($end));
  119. $currentStartTime = $currentStartDate . ' 00:00:00';
  120. $currentEndTime = $currentEndDate . ' 23:59:59';
  121. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  122. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  123. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
  124. $arr = [];
  125. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  126. $totalAmount = 0;
  127. $totalNum = 0;
  128. if (!empty($ghsOrderList)) {
  129. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  130. $actPrice = $ghsOrder['actPrice'] ?? 0;
  131. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  132. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  133. $customId = $ghsOrder['customId'] ?? 0;
  134. $status = $ghsOrder['status'] ?? 0;
  135. $book = $ghsOrder['book'] ?? 0;
  136. if ($status == 1 || $status == 5) {
  137. continue;
  138. }
  139. if ($book == 1 && $status == 2) {
  140. continue;
  141. }
  142. $customName = $customList[$customId]['name'] ?? '';
  143. $py = $customList[$customId]['py'] ?? '';
  144. $totalNum++;
  145. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  146. if (isset($arr[$customId])) {
  147. $arr[$customId]['num']++;
  148. $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
  149. $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
  150. $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
  151. } else {
  152. $arr[$customId] = [
  153. 'customId' => $customId,
  154. 'customName' => $customName,
  155. 'py' => $py,
  156. 'num' => 1,
  157. 'amount' => $actPrice,
  158. 'levelName' => '',
  159. 'orderPrice' => $orderPrice,
  160. 'tkPrice' => $tkPrice,
  161. ];
  162. }
  163. }
  164. $arr = arrayUtil::arraySort($arr, 'amount');
  165. }
  166. $totalAmount = floatval($totalAmount);
  167. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum];
  168. }
  169. public static function profile($mainId)
  170. {
  171. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  172. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  173. $get = Yii::$app->request->get();
  174. $where = [];
  175. $where['mainId'] = $mainId;
  176. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  177. $startTime = $get['startTime'] ?? '';
  178. $endTime = $get['endTime'] ?? '';
  179. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  180. $start = $period['startTime'];
  181. $end = $period['endTime'];
  182. $where['time'] = ['between', [$start, $end]];
  183. $currentStartDate = date('Y-m-d', strtotime($start));
  184. $currentEndDate = date('Y-m-d', strtotime($end));
  185. $currentStartTime = $currentStartDate . ' 00:00:00';
  186. $currentEndTime = $currentEndDate . ' 23:59:59';
  187. //出库
  188. $stockOut = StatStockOutClass::getSum($where);
  189. //采购入库
  190. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  191. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  192. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  193. $cg = 0;
  194. if (!empty($cgList)) {
  195. foreach ($cgList as $cgKey => $cgItem) {
  196. $actPrice = $cgItem['actPrice'] ?? 0;
  197. $paidPrice = $cgItem['paidPrice'] ?? 0;
  198. if ($actPrice <= 0) {
  199. // 如果$actPrice=0,表示这个采购单已经全部退款,不需要再统计成本,本来$paidPrice要同步清零,但没有,这个问题由徐州邂逅花坊马总,提出的需求发现
  200. $cg = bcadd($cg, 0, 2);
  201. } else {
  202. $total = bcadd($actPrice, $paidPrice, 2);
  203. //物流费用不算成本。意思是说,长短途运费+提货费+本地运费,这四项和打包费一起,参与到平摊花材运费成本的计算,但那四项不参与总的采购成本登记,因为,有另外地方会独立登记运费成本。
  204. //$paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  205. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  206. if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
  207. $shortCharge = $cgItem['shortCharge'] ?? 0;
  208. $longCharge = $cgItem['longCharge'] ?? 0;
  209. $pickCharge = $cgItem['pickCharge'] ?? 0;
  210. $localCharge = $cgItem['localCharge'] ?? 0;
  211. $a1 = bcadd($shortCharge, $longCharge, 2);
  212. $a2 = bcadd($pickCharge, $localCharge, 2);
  213. $a = bcadd($a1, $a2, 2);
  214. //注意下面是减号 bcsub
  215. $total = bcsub($total, $a, 2);
  216. }
  217. $cg = bcadd($cg, $total, 2);
  218. }
  219. }
  220. }
  221. $cg = floatval($cg);
  222. $lsCgWhere = ['mainId' => $mainId, 'status' => 4, 'belongCost' => 1];
  223. $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  224. $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
  225. $lsCgAmount = 0;
  226. if (!empty($lsCgList)) {
  227. foreach ($lsCgList as $lsCgItem) {
  228. $actPrice = $lsCgItem['actPrice'] ?? 0;
  229. $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
  230. }
  231. }
  232. $lsCgAmount = floatval($lsCgAmount);
  233. $lzWhere = ['mainId' => $mainId, 'status' => 3];
  234. $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  235. $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
  236. $lzCgAmount = 0;
  237. if (!empty($lzCgList)) {
  238. foreach ($lzCgList as $lzInfo) {
  239. $actPrice = $lzInfo['actPrice'] ?? 0;
  240. $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
  241. }
  242. }
  243. $lzCgAmount = floatval($lzCgAmount);
  244. //调拨入库
  245. $stockIn = StatStockInClass::getSum($where);
  246. $totalOrderNum = 0;
  247. //批发开单
  248. $pfIncome = 0;
  249. $pfSendCost = 0;
  250. $pfPack = 0;
  251. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  252. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  253. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  254. if (!empty($ghsOrderList)) {
  255. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  256. $actPrice = $ghsOrder['actPrice'] ?? 0;
  257. $sendCost = $ghsOrder['sendCost'] ?? 0;
  258. $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
  259. $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
  260. $packCost = $ghsOrder['packCost'] ?? 0;
  261. $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
  262. $remainPackCost = bcsub($packCost, $refundPackCost, 2);
  263. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  264. $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
  265. $pfPack = bcadd($pfPack, $remainPackCost, 2);
  266. $totalOrderNum++;
  267. }
  268. }
  269. //零售开单
  270. $payCodeIncome = 0;
  271. $lsAfterSale = 0;
  272. $lsGiveBalance = 0;
  273. $lsIncome = 0;
  274. $lsSendCost = 0;
  275. $lsServiceFee = 0;
  276. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  277. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  278. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  279. foreach ($hdOrderList as $hdKey => $hdOrder) {
  280. $actPrice = $hdOrder['mainPay'] ?? 0;
  281. $onlinePay = $hdOrder['onlinePay'] ?? 1;
  282. $repeat = $hdOrder['repeat'] ?? 0;
  283. $forward = $hdOrder['forward'] ?? 0;
  284. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  285. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  286. }
  287. if ($forward == 1) {
  288. //零售售后付款
  289. $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
  290. } else {
  291. $cash = $hdOrder['cash'] ?? 0;
  292. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  293. $sendCost = $hdOrder['sendCost'] ?? 0;
  294. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  295. $lsIncome = bcadd($lsIncome, $cash, 2);
  296. //如果$actPrice<=0,则退款取消先不计算成本
  297. if ($actPrice > 0) {
  298. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  299. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  300. }
  301. $totalOrderNum++;
  302. if ($onlinePay == 2 && floatval($cash) <= 0 && $repeat == 1) {
  303. $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
  304. $payCodeIncome = floatval($payCodeIncome);
  305. }
  306. $giveBalance = $hdOrder['giveBalance'] ?? 0;
  307. $giveBalanceTk = $hdOrder['giveBalanceTk'] ?? 0;
  308. $remainGiveBalance = bcsub($giveBalance, $giveBalanceTk, 2);
  309. $lsGiveBalance = bcadd($lsGiveBalance, $remainGiveBalance, 2);
  310. }
  311. }
  312. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  313. $salary = 0;
  314. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  315. if (!empty($salaryList)) {
  316. foreach ($salaryList as $item) {
  317. $salary = bcadd($salary, $item->amount, 2);
  318. }
  319. }
  320. $condition = [
  321. 'mainId' => $mainId,
  322. 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
  323. 'delStatus' => 0,
  324. ];
  325. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  326. $expendTypeMap = dict::getDict('expendTypeMap');
  327. $expendAmount = [];
  328. if (!empty($expendList)) {
  329. foreach ($expendList as $item) {
  330. $type = $item->type ?? 0;
  331. $amount = $item->amount ?? 0;
  332. $name = $expendTypeMap[$type] ?? '暂无';
  333. if (!isset($expendAmount[$type]['amount'])) {
  334. $expendAmount[$type]['amount'] = 0;
  335. }
  336. $expendAmount[$type]['name'] = $name;
  337. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  338. }
  339. }
  340. $income = [
  341. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  342. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  343. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  344. ];
  345. $expend = [
  346. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  347. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  348. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  349. ['name' => '零售售后付款', 'id' => 'lsSendCost', 'amount' => floatval($lsAfterSale)],
  350. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  351. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  352. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  353. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  354. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  355. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  356. ['name' => '收款码收入', 'id' => 'salary', 'amount' => floatval($payCodeIncome)],
  357. ['name' => '零售充值赠送', 'id' => 'salary', 'amount' => floatval($lsGiveBalance)],
  358. ];
  359. if (!empty($expendAmount)) {
  360. foreach ($expendAmount as $it) {
  361. $expend[] = $it;
  362. }
  363. }
  364. $balance = 0;
  365. foreach ($income as $it) {
  366. $amount = $it['amount'] ?? 0;
  367. $balance = bcadd($amount, $balance, 2);
  368. }
  369. foreach ($expend as $it) {
  370. $amount = $it['amount'] ?? 0;
  371. $balance = bcsub($balance, $amount, 2);
  372. }
  373. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
  374. }
  375. //按花材统计收入 ssh 20231111
  376. public static function incomeOutItem($mainId)
  377. {
  378. $get = Yii::$app->request->get();
  379. $export = $get['export'] ?? 0;
  380. $where = [];
  381. $where['mainId'] = $mainId;
  382. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  383. $startTime = $get['startTime'] ?? '';
  384. $endTime = $get['endTime'] ?? '';
  385. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  386. $start = $period['startTime'];
  387. $end = $period['endTime'];
  388. $where['time'] = ['between', [$start, $end]];
  389. $currentStartDate = date('Y-m-d', strtotime($start));
  390. $currentEndDate = date('Y-m-d', strtotime($end));
  391. $currentStartTime = $currentStartDate . ' 00:00:00';
  392. $currentEndTime = $currentEndDate . ' 23:59:59';
  393. $s = strtotime($currentStartTime);
  394. $e = strtotime($currentEndTime);
  395. $p = bcsub($e, $s);
  396. $xx = bcmul(86400, 90);
  397. if ($p > $xx) {
  398. util::fail('查询时间不能超过三个月,编号666');
  399. }
  400. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  401. if (empty($productList)) {
  402. util::fail('没有找到花材');
  403. }
  404. $arr = [];
  405. //打包费和运费
  406. $packFreight = 0;
  407. $onlyRefundCg = 0;
  408. $onlyRefund = 0;
  409. $totalDiscountAmount = 0;
  410. $totalLabourCost = 0;
  411. //采购入库
  412. $tbCgIncome = [];
  413. $tbCgIncomeStaff = [];
  414. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  415. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  416. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  417. if (!empty($cgList)) {
  418. foreach ($cgList as $cgKey => $cgItem) {
  419. $packingCharge = $cgItem['packingCharge'] ?? 0;
  420. $shortCharge = $cgItem['shortCharge'] ?? 0;
  421. $longCharge = $cgItem['longCharge'] ?? 0;
  422. $pickCharge = $cgItem['pickCharge'] ?? 0;
  423. $localCharge = $cgItem['localCharge'] ?? 0;
  424. $packFreight = bcadd($packFreight, $packingCharge, 2);
  425. $packFreight = bcadd($packFreight, $shortCharge, 2);
  426. $packFreight = bcadd($packFreight, $longCharge, 2);
  427. $packFreight = bcadd($packFreight, $pickCharge, 2);
  428. $packFreight = bcadd($packFreight, $localCharge, 2);
  429. $tkPrice = $cgItem['tkPrice'] ?? 0;
  430. $orderSn = $cgItem['orderSn'] ?? '';
  431. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  432. if (!empty($cgItemInfoList)) {
  433. foreach ($cgItemInfoList as $cgData) {
  434. $itemNum = $cgData->itemNum ?? 0;
  435. $productId = $cgData->productId ?? 0;
  436. $refundNum = $cgData->refundNum ?? 0;
  437. $remainNum = bcsub($itemNum, $refundNum);
  438. $bigPrice = $cgData->bigPrice ?? 0;
  439. $amount = bcmul($remainNum, $bigPrice, 2);
  440. $itemName = $productList[$productId]['name'] ?? '已删除';
  441. $arr[$productId]['name'] = $itemName;
  442. $arr[$productId]['id'] = $productId;
  443. if (isset($arr[$productId]['expend'])) {
  444. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  445. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  446. } else {
  447. $arr[$productId]['expend'] = $amount;
  448. $arr[$productId]['outNum'] = $remainNum;
  449. $arr[$productId]['inNum'] = 0;
  450. $arr[$productId]['income'] = 0;
  451. }
  452. }
  453. if ($tkPrice > 0) {
  454. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  455. if (!empty($cgRefundOrderList)) {
  456. foreach ($cgRefundOrderList as $cgRefundOrder) {
  457. if ($cgRefundOrder->refundType == 2) {
  458. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  459. }
  460. }
  461. }
  462. }
  463. }
  464. }
  465. }
  466. //批发开单
  467. $pfIncome = 0;
  468. $tbSaleIncome = [];
  469. $tbSaleIncomeStaff = [];
  470. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  471. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  472. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  473. if (!empty($ghsOrderList)) {
  474. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  475. $book = $ghsOrder['book'] ?? 0;
  476. $status = $ghsOrder['status'] ?? 0;
  477. if ($book == 1 && $status == 2) {
  478. //预订单待发货的不算收入
  479. continue;
  480. }
  481. if ($status == 0 || $status == 5) {
  482. continue;
  483. }
  484. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  485. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  486. $orderSn = $ghsOrder['orderSn'] ?? '';
  487. $actPrice = $ghsOrder['actPrice'] ?? 0;
  488. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  489. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  490. $labourCost = $ghsOrder['labourCost'] ?? 0;
  491. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  492. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  493. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  494. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  495. if (!empty($orderItemList)) {
  496. foreach ($orderItemList as $orderItem) {
  497. $refundNum = $orderItem->refundNum ?? 0;
  498. $name = $orderItem->name ?? '';
  499. $productId = $orderItem->productId ?? 0;
  500. $num = $orderItem->xhNum ?? 0;
  501. $remainNum = bcsub($num, $refundNum);
  502. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  503. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  504. $itemName = $productList[$productId]['name'] ?? '已删除';
  505. $arr[$productId]['name'] = $itemName;
  506. $arr[$productId]['id'] = $productId;
  507. if (isset($arr[$productId]['income'])) {
  508. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  509. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  510. } else {
  511. $arr[$productId]['expend'] = 0;
  512. $arr[$productId]['outNum'] = 0;
  513. $arr[$productId]['inNum'] = $remainNum;
  514. $arr[$productId]['income'] = $amount;
  515. }
  516. //单独统计
  517. $belongCost = $orderItem->belongCost ?? 0;
  518. if ($belongCost == 1) {
  519. if (isset($tbSaleIncome[$productId])) {
  520. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  521. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  522. } else {
  523. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  524. }
  525. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  526. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  527. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  528. } else {
  529. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  530. }
  531. }
  532. }
  533. if ($tkPrice > 0) {
  534. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  535. if (!empty($refundList)) {
  536. foreach ($refundList as $refund) {
  537. if ($refund->refundType == 1) {
  538. //退货退款前面已经考虑,这里不再处理 OK
  539. } else {
  540. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  541. }
  542. }
  543. }
  544. }
  545. }
  546. }
  547. }
  548. //零售开单
  549. $lsIncome = 0;
  550. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  551. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  552. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  553. foreach ($hdOrderList as $hdKey => $hdOrder) {
  554. $actPrice = $hdOrder['mainPay'] ?? 0;
  555. $cash = $hdOrder['cash'] ?? 0;
  556. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  557. $lsIncome = bcadd($lsIncome, $cash, 2);
  558. $orderSn = $hdOrder['orderSn'] ?? '';
  559. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  560. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  561. if (!empty($orderItemList)) {
  562. foreach ($orderItemList as $orderItem) {
  563. $unitPrice = $orderItem->unitPrice ?? 0;
  564. $refundNum = $orderItem->refundNum ?? 0;
  565. $num = $orderItem->num ?? 0;
  566. $productId = $orderItem->itemId ?? 0;
  567. $remainNum = bcsub($num, $refundNum);
  568. $amount = bcmul($remainNum, $unitPrice, 2);
  569. $itemName = $productList[$productId]['name'] ?? '已删除';
  570. $arr[$productId]['name'] = $itemName;
  571. $arr[$productId]['id'] = $productId;
  572. if (isset($arr[$productId]['income'])) {
  573. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  574. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  575. } else {
  576. $arr[$productId]['expend'] = 0;
  577. $arr[$productId]['outNum'] = 0;
  578. $arr[$productId]['inNum'] = $remainNum;
  579. $arr[$productId]['income'] = $amount;
  580. }
  581. }
  582. if ($tkPrice > 0) {
  583. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  584. if (!empty($refundList)) {
  585. foreach ($refundList as $refund) {
  586. if ($refund->refundType == 2) {
  587. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  588. }
  589. }
  590. }
  591. }
  592. }
  593. }
  594. $totalIncome = 0;
  595. $totalExpend = 0;
  596. if (!empty($arr)) {
  597. foreach ($arr as $key => $val) {
  598. $income = $val['income'] ?? 0;
  599. $income = floatval($income);
  600. $totalIncome = bcadd($totalIncome, $income, 2);
  601. $expend = $val['expend'] ?? 0;
  602. $expend = floatval($expend);
  603. $totalExpend = bcadd($totalExpend, $expend, 2);
  604. $profit = bcsub($income, $expend, 2);
  605. $arr[$key]['profit'] = floatval($profit);
  606. $arr[$key]['income'] = floatval($income);
  607. $arr[$key]['expend'] = floatval($expend);
  608. }
  609. }
  610. //总成本算上物流打包等费用
  611. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  612. //总收入算上采购仅退款
  613. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  614. //总收入加上人工费
  615. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  616. //总收入减掉优惠金额
  617. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  618. //总成本算上销售仅退款
  619. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  620. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  621. $totalIncome = floatval($totalIncome);
  622. $totalExpend = floatval($totalExpend);
  623. $totalProfit = floatval($totalProfit);
  624. $arr = array_values($arr);
  625. $arr = arrayUtil::arraySort($arr, 'profit');
  626. if ($export == 1) {
  627. $statData = [
  628. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  629. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  630. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  631. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  632. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  633. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  634. ];
  635. $list = array_merge($arr, $statData);
  636. self::exportIncomeOutItem($list, $mainId);
  637. }
  638. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  639. 'totalIncome' => $totalIncome,
  640. 'totalExpend' => $totalExpend,
  641. 'totalProfit' => $totalProfit,
  642. 'tbSaleIncome' => $tbSaleIncome,
  643. 'tbCgIncome' => $tbCgIncome,
  644. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  645. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  646. 'totalLabourCost' => $totalLabourCost,
  647. 'totalDiscountAmount' => $totalDiscountAmount,
  648. ]);
  649. }
  650. //按分类统计收入 ssh 20231111
  651. public static function classProfile($mainId)
  652. {
  653. $get = Yii::$app->request->get();
  654. $export = $get['export'] ?? 0;
  655. $where = [];
  656. $where['mainId'] = $mainId;
  657. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  658. $startTime = $get['startTime'] ?? '';
  659. $endTime = $get['endTime'] ?? '';
  660. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  661. $start = $period['startTime'];
  662. $end = $period['endTime'];
  663. $where['time'] = ['between', [$start, $end]];
  664. $currentStartDate = date('Y-m-d', strtotime($start));
  665. $currentEndDate = date('Y-m-d', strtotime($end));
  666. $currentStartTime = $currentStartDate . ' 00:00:00';
  667. $currentEndTime = $currentEndDate . ' 23:59:59';
  668. $s = strtotime($currentStartTime);
  669. $e = strtotime($currentEndTime);
  670. $p = bcsub($e, $s);
  671. $xx = bcmul(86400, 90);
  672. if ($p > $xx) {
  673. util::fail('查询时间不能超过三个月,编号780');
  674. }
  675. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  676. if (empty($classList)) {
  677. util::fail('没有找到花材');
  678. }
  679. $arr = [];
  680. //打包费和运费
  681. $packFreight = 0;
  682. $onlyRefundCg = 0;
  683. $onlyRefund = 0;
  684. $totalDiscountAmount = 0;
  685. $totalLabourCost = 0;
  686. //采购入库
  687. $tbCgIncome = [];
  688. $tbCgIncomeStaff = [];
  689. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  690. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  691. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  692. if (!empty($cgList)) {
  693. foreach ($cgList as $cgKey => $cgItem) {
  694. $packingCharge = $cgItem['packingCharge'] ?? 0;
  695. $shortCharge = $cgItem['shortCharge'] ?? 0;
  696. $longCharge = $cgItem['longCharge'] ?? 0;
  697. $pickCharge = $cgItem['pickCharge'] ?? 0;
  698. $localCharge = $cgItem['localCharge'] ?? 0;
  699. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  700. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  701. $actPrice = $cgItem['actPrice'] ?? 0;
  702. if ($actPrice <= 0) {
  703. continue;
  704. }
  705. $packFreight = bcadd($packFreight, $packingCharge, 2);
  706. $packFreight = bcadd($packFreight, $shortCharge, 2);
  707. $packFreight = bcadd($packFreight, $longCharge, 2);
  708. $packFreight = bcadd($packFreight, $pickCharge, 2);
  709. $packFreight = bcadd($packFreight, $localCharge, 2);
  710. $tkPrice = $cgItem['tkPrice'] ?? 0;
  711. $orderSn = $cgItem['orderSn'] ?? '';
  712. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  713. if (!empty($cgItemInfoList)) {
  714. foreach ($cgItemInfoList as $cgData) {
  715. $itemNum = $cgData->itemNum ?? 0;
  716. $productId = $cgData->productId ?? 0;
  717. $name = $cgData->name ?? '';
  718. $refundNum = $cgData->refundNum ?? 0;
  719. $remainNum = bcsub($itemNum, $refundNum);
  720. $bigPrice = $cgData->bigPrice ?? 0;
  721. $amount = bcmul($remainNum, $bigPrice, 2);
  722. $classId = $cgData->classId;
  723. $className = $classList[$classId]['name'] ?? '已删除';
  724. if ($className == '运费') {
  725. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  726. }
  727. $arr[$classId]['className'] = $className;
  728. $arr[$classId]['classId'] = $classId;
  729. if (isset($arr[$classId]['expend'])) {
  730. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  731. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  732. } else {
  733. $arr[$classId]['expend'] = $amount;
  734. $arr[$classId]['income'] = 0;
  735. $arr[$classId]['cgNum'] = $remainNum;
  736. $arr[$classId]['saleNum'] = 0;
  737. }
  738. //单独统计
  739. $belongCost = $cgData->belongCost ?? 0;
  740. if ($belongCost == 1) {
  741. if (isset($tbCgIncome[$productId])) {
  742. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  743. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  744. } else {
  745. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  746. }
  747. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  748. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  749. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  750. } else {
  751. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  752. }
  753. }
  754. }
  755. if ($tkPrice > 0) {
  756. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  757. if (!empty($cgRefundOrderList)) {
  758. foreach ($cgRefundOrderList as $cgRefundOrder) {
  759. if ($cgRefundOrder->refundType == 2) {
  760. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  761. }
  762. }
  763. }
  764. }
  765. }
  766. }
  767. }
  768. //批发开单
  769. $pfIncome = 0;
  770. $tbSaleIncome = [];
  771. $tbSaleIncomeStaff = [];
  772. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  773. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  774. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  775. if (!empty($ghsOrderList)) {
  776. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  777. $book = $ghsOrder['book'] ?? 0;
  778. $status = $ghsOrder['status'] ?? 0;
  779. if ($book == 1 && $status == 2) {
  780. //预订单待发货的不算收入
  781. continue;
  782. }
  783. if ($status == 0 || $status == 5) {
  784. continue;
  785. }
  786. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  787. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  788. $orderSn = $ghsOrder['orderSn'] ?? '';
  789. $actPrice = $ghsOrder['actPrice'] ?? 0;
  790. if ($actPrice <= 0) {
  791. continue;
  792. }
  793. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  794. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  795. $labourCost = $ghsOrder['labourCost'] ?? 0;
  796. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  797. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  798. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  799. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  800. if (!empty($orderItemList)) {
  801. foreach ($orderItemList as $orderItem) {
  802. $refundNum = $orderItem->refundNum ?? 0;
  803. $name = $orderItem->name ?? '';
  804. $productId = $orderItem->productId ?? 0;
  805. $num = $orderItem->xhNum ?? 0;
  806. $remainNum = bcsub($num, $refundNum);
  807. $classId = $orderItem->classId ?? 0;
  808. $className = $classList[$classId]['name'] ?? '已删除';
  809. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  810. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  811. $arr[$classId]['className'] = $className;
  812. $arr[$classId]['classId'] = $classId;
  813. if (isset($arr[$classId]['income'])) {
  814. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  815. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  816. } else {
  817. $arr[$classId]['expend'] = 0;
  818. $arr[$classId]['income'] = $amount;
  819. $arr[$classId]['saleNum'] = $remainNum;
  820. $arr[$classId]['cgNum'] = 0;
  821. }
  822. //单独统计
  823. $belongCost = $orderItem->belongCost ?? 0;
  824. if ($belongCost == 1) {
  825. if (isset($tbSaleIncome[$productId])) {
  826. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  827. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  828. } else {
  829. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  830. }
  831. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  832. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  833. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  834. } else {
  835. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  836. }
  837. }
  838. }
  839. if ($tkPrice > 0) {
  840. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  841. if (!empty($refundList)) {
  842. foreach ($refundList as $refund) {
  843. if ($refund->refundType == 1) {
  844. //退货退款前面已经考虑,这里不再处理 OK
  845. } else {
  846. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  847. }
  848. }
  849. }
  850. }
  851. }
  852. }
  853. }
  854. //零售开单
  855. $lsIncome = 0;
  856. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  857. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  858. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  859. foreach ($hdOrderList as $hdKey => $hdOrder) {
  860. $actPrice = $hdOrder['mainPay'] ?? 0;
  861. $cash = $hdOrder['cash'] ?? 0;
  862. if ($actPrice <= 0 && $cash <= 0) {
  863. continue;
  864. }
  865. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  866. $lsIncome = bcadd($lsIncome, $cash, 2);
  867. $orderSn = $hdOrder['orderSn'] ?? '';
  868. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  869. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  870. if (!empty($orderItemList)) {
  871. foreach ($orderItemList as $orderItem) {
  872. $classId = $orderItem->classId ?? 0;
  873. $unitPrice = $orderItem->unitPrice ?? 0;
  874. $refundNum = $orderItem->refundNum ?? 0;
  875. $num = $orderItem->num ?? 0;
  876. $productId = $orderItem->itemId ?? 0;
  877. $name = $orderItem->name ?? '';
  878. $remainNum = bcsub($num, $refundNum);
  879. $amount = bcmul($remainNum, $unitPrice, 2);
  880. $className = $classList[$classId]['name'] ?? '已删除';
  881. $arr[$classId]['className'] = $className;
  882. $arr[$classId]['classId'] = $classId;
  883. if (isset($arr[$classId]['income'])) {
  884. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  885. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  886. } else {
  887. $arr[$classId]['expend'] = 0;
  888. $arr[$classId]['income'] = $amount;
  889. $arr[$classId]['saleNum'] = $remainNum;
  890. $arr[$classId]['cgNum'] = 0;
  891. }
  892. $belongCost = $orderItem->belongCost ?? 0;
  893. if ($belongCost == 1) {
  894. if (isset($tbSaleIncome[$productId])) {
  895. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  896. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  897. } else {
  898. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  899. }
  900. }
  901. }
  902. if ($tkPrice > 0) {
  903. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  904. if (!empty($refundList)) {
  905. foreach ($refundList as $refund) {
  906. if ($refund->refundType == 2) {
  907. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  908. }
  909. }
  910. }
  911. }
  912. }
  913. }
  914. $totalIncome = 0;
  915. $totalExpend = 0;
  916. if (!empty($arr)) {
  917. foreach ($arr as $key => $val) {
  918. $income = $val['income'] ?? 0;
  919. $income = floatval($income);
  920. $totalIncome = bcadd($totalIncome, $income, 2);
  921. $expend = $val['expend'] ?? 0;
  922. $expend = floatval($expend);
  923. $totalExpend = bcadd($totalExpend, $expend, 2);
  924. $profit = bcsub($income, $expend, 2);
  925. $arr[$key]['profit'] = floatval($profit);
  926. $arr[$key]['income'] = floatval($income);
  927. $arr[$key]['expend'] = floatval($expend);
  928. }
  929. }
  930. //总成本算上物流打包等费用
  931. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  932. //总收入算上采购仅退款
  933. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  934. //总收入加上人工费
  935. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  936. //总收入减掉优惠金额
  937. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  938. //总成本算上销售仅退款
  939. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  940. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  941. $totalIncome = floatval($totalIncome);
  942. $totalExpend = floatval($totalExpend);
  943. $totalProfit = floatval($totalProfit);
  944. $arr = array_values($arr);
  945. $arr = arrayUtil::arraySort($arr, 'profit');
  946. if ($export == 1) {
  947. $statData = [
  948. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  949. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  950. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  951. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  952. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  953. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  954. ];
  955. $list = array_merge($arr, $statData);
  956. self::exportIncomeClass($list, $mainId);
  957. }
  958. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  959. 'totalIncome' => $totalIncome,
  960. 'totalExpend' => $totalExpend,
  961. 'totalProfit' => $totalProfit,
  962. 'tbSaleIncome' => $tbSaleIncome,
  963. 'tbCgIncome' => $tbCgIncome,
  964. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  965. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  966. 'totalLabourCost' => $totalLabourCost,
  967. 'totalDiscountAmount' => $totalDiscountAmount,
  968. ]);
  969. }
  970. public static function exportIncomeOutItem($list, $mainId)
  971. {
  972. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  973. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  974. $objPHPExcel = new \PHPExcel();
  975. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  976. ->setLastModifiedBy("Maarten Balliauw")
  977. ->setTitle("Office 2007 XLSX Document")
  978. ->setSubject("Office 2007 XLSX Document")
  979. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  980. ->setKeywords("office 2007 openxml php")
  981. ->setCategory("file");
  982. $ghsTitle = '供货商';
  983. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  984. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  985. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  986. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  987. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  988. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  989. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  990. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  991. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  992. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  993. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  994. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  995. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  996. //设置宽度
  997. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  998. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  999. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1000. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1001. //加粗
  1002. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1003. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1004. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1005. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1006. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1007. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1008. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1009. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1010. $baseRow = 2;
  1011. foreach ($list as $key => $custom) {
  1012. $i = $baseRow + $key;
  1013. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1014. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  1015. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  1016. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  1017. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  1018. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1019. //居左
  1020. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1021. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1022. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1023. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1024. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1025. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1026. }
  1027. $fileName = '收支花材-' . date("m-d");
  1028. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1029. $objPHPExcel->setActiveSheetIndex(0);
  1030. $dir = './priceTable/' . $mainId;
  1031. if (file_exists($dir) == false) {
  1032. mkdir($dir, 0777, true);
  1033. }
  1034. $date = $fileName;
  1035. $file = $date . '.xls';
  1036. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1037. if (file_exists($dir . '/' . $file)) {
  1038. unlink($dir . '/' . $file);
  1039. }
  1040. $objWriter->save($dir . '/' . $file);
  1041. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1042. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1043. }
  1044. public static function exportIncomeClass($list, $mainId)
  1045. {
  1046. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1047. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1048. $objPHPExcel = new \PHPExcel();
  1049. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1050. ->setLastModifiedBy("Maarten Balliauw")
  1051. ->setTitle("Office 2007 XLSX Document")
  1052. ->setSubject("Office 2007 XLSX Document")
  1053. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1054. ->setKeywords("office 2007 openxml php")
  1055. ->setCategory("file");
  1056. $ghsTitle = '供货商';
  1057. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1058. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1059. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1060. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1061. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1062. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1063. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1064. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1065. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1066. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1067. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1068. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1069. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1070. //设置宽度
  1071. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1072. //加粗
  1073. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1074. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1075. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1076. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1077. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1078. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1079. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1080. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1081. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1082. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1083. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1084. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1085. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1086. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1087. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1088. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1089. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1090. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1091. $baseRow = 2;
  1092. foreach ($list as $key => $custom) {
  1093. $i = $baseRow + $key;
  1094. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1095. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1096. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1097. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1098. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1099. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1100. //居左
  1101. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1102. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1103. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1104. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1105. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1106. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1107. }
  1108. $fileName = '收支分类-' . time();
  1109. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1110. $objPHPExcel->setActiveSheetIndex(0);
  1111. $dir = './priceTable/' . $mainId;
  1112. if (file_exists($dir) == false) {
  1113. mkdir($dir, 0777, true);
  1114. }
  1115. $date = $fileName;
  1116. $file = $date . '.xls';
  1117. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1118. if (file_exists($dir . '/' . $file)) {
  1119. unlink($dir . '/' . $file);
  1120. }
  1121. $objWriter->save($dir . '/' . $file);
  1122. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1123. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1124. }
  1125. public static function exportItemSale($list, $mainId)
  1126. {
  1127. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1128. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1129. $objPHPExcel = new \PHPExcel();
  1130. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1131. ->setLastModifiedBy("Maarten Balliauw")
  1132. ->setTitle("Office 2007 XLSX Document")
  1133. ->setSubject("Office 2007 XLSX Document")
  1134. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1135. ->setKeywords("office 2007 openxml php")
  1136. ->setCategory("file");
  1137. $ghsTitle = '供货商';
  1138. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1139. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1140. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1141. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1142. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1143. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1144. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1145. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1146. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1147. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1148. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1149. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1150. //设置宽度
  1151. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1152. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1153. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1154. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1155. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1156. //加粗
  1157. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1158. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1159. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1160. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1161. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1162. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1163. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1164. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1165. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1166. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1167. $baseRow = 2;
  1168. foreach ($list as $key => $custom) {
  1169. $i = $baseRow + $key;
  1170. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1171. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1172. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1173. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1174. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1175. //居左
  1176. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1177. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1178. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1179. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1180. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1181. }
  1182. $fileName = '花材销量-' . date("m-d");
  1183. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1184. $objPHPExcel->setActiveSheetIndex(0);
  1185. $dir = './priceTable/' . $mainId;
  1186. if (file_exists($dir) == false) {
  1187. mkdir($dir, 0777, true);
  1188. }
  1189. $date = $fileName;
  1190. $file = $date . '.xls';
  1191. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1192. if (file_exists($dir . '/' . $file)) {
  1193. unlink($dir . '/' . $file);
  1194. }
  1195. $objWriter->save($dir . '/' . $file);
  1196. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1197. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1198. }
  1199. public static function send($mainId)
  1200. {
  1201. $get = Yii::$app->request->get();
  1202. $where = [];
  1203. $where['mainId'] = $mainId;
  1204. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1205. $startTime = $get['startTime'] ?? '';
  1206. $endTime = $get['endTime'] ?? '';
  1207. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1208. $start = $period['startTime'];
  1209. $end = $period['endTime'];
  1210. $where['time'] = ['between', [$start, $end]];
  1211. $currentStartDate = date('Y-m-d', strtotime($start));
  1212. $currentEndDate = date('Y-m-d', strtotime($end));
  1213. $currentStartTime = $currentStartDate . ' 00:00:00';
  1214. $currentEndTime = $currentEndDate . ' 23:59:59';
  1215. $s = strtotime($currentStartTime);
  1216. $e = strtotime($currentEndTime);
  1217. $p = bcsub($e, $s);
  1218. $xx = bcmul(86400, 90);
  1219. if ($p > $xx) {
  1220. util::fail('查询时间不能超过三个月,编号699');
  1221. }
  1222. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1223. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1224. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1225. $arr = [];
  1226. if (!empty($ghsOrderList)) {
  1227. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  1228. $book = $ghsOrder['book'] ?? 0;
  1229. $status = $ghsOrder['status'] ?? 0;
  1230. if ($book == 1 && $status == 2) {
  1231. //预订单待发货的不算收入
  1232. continue;
  1233. }
  1234. if ($status == 0 || $status == 5) {
  1235. continue;
  1236. }
  1237. $orderSn = $ghsOrder['orderSn'] ?? '';
  1238. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1239. if ($actPrice <= 0) {
  1240. continue;
  1241. }
  1242. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1243. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1244. if (isset($arr[$sendStaffId])) {
  1245. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1246. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1247. } else {
  1248. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1249. }
  1250. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1251. if (!empty($orderItemList)) {
  1252. foreach ($orderItemList as $orderItem) {
  1253. $refundNum = $orderItem->refundNum ?? 0;
  1254. $num = $orderItem->xhNum ?? 0;
  1255. $remainNum = bcsub($num, $refundNum);
  1256. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1257. }
  1258. }
  1259. }
  1260. }
  1261. if (!empty($arr)) {
  1262. $arr = array_values($arr);
  1263. }
  1264. return $arr;
  1265. }
  1266. }